home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / CLIPPER / CLIPWIND / WN_ADD.PRG < prev    next >
Text File  |  1993-12-01  |  13KB  |  326 lines

  1. ******************************************************************************
  2. ***** Author: Jim Holley                                                 *****
  3. ***** Date  : 07/27/87                                                   *****
  4. ***** Comments:                                                          *****
  5. ***** This is an example showing some features and uses of the Windows   *****
  6. ***** For Clipper Library. This routine performs a windowed database     *****
  7. ***** view, with an add routine. The add routine is unique in operation. *****
  8. ***** Special attention should be paid to this routine. The purpose of   *****
  9. ***** this routine is to show how an operator can add a record to the    *****
  10. ***** database if the information required can not be found through use  *****
  11. ***** of the Windows For Clipper functions.                              *****
  12. ***** The database used is the Customer.DBF file included with the       *****
  13. ***** Windows For Clipper package.                                       *****
  14. ***** This program is being release to the Windows For Clipper Library   *****
  15. ***** owners. You may use any portion of it anyway you see fit.          *****
  16. ******************************************************************************
  17. ***** this is the controller code *****
  18. SET SCOREBOARD OFF
  19. SET CONFIRM ON
  20. ***** clear the screen *****
  21. CLEAR
  22. ***** initialize window *****
  23. start_col = 8
  24. start_row = 12
  25. num_col = 60
  26. num_rows = 10
  27. select_wn = _SINIT_WN(start_col, start_row, num_col, num_rows)
  28. ***** set window border character *****
  29. _SST_WNBC(select_wn, 177)
  30. ***** open the database *****
  31. USE customer
  32. ***** set up infinite loop *****
  33. DO WHILE .T.
  34.    ***** draw window on the screen *****
  35.    _SDRW_WN(select_wn)
  36.    ***** write some text *****
  37.    @ 00,10 SAY "The following will simulate an operator searching a database"
  38.    @ 01,10 SAY "for specific information. If the information cannot be"
  39.    @ 02,10 SAY "be found, the operator presses the escape key. At that"
  40.    @ 03,10 SAY "point, a routine will ask the operator if he/she wishes"
  41.    @ 04,10 SAY "to add information. The program will take action based" 
  42.    @ 05,10 SAY "on the operators answer. If the operator answers yes, an add"
  43.    @ 06,10 SAY "routine will be called to get the information needed."
  44.    @ 07,10 SAY "This is not an example of something that should be done"
  45.    @ 08,10 SAY "in a real life situation. I don't advise any one to do this"
  46.    @ 09,10 SAY "unless proper precautions have been made to guard against"
  47.    @ 10,10 SAY "data coruption."
  48.    @ 11,22 SAY "PRESS ANY KEY TO BEGIN SIMULATION."
  49.    INKEY(0)
  50.    ***** show the customer info in the window *****
  51.    IF show_info() = 0
  52.       response = option_wn("Would You Like To Add Information", "YN")
  53.       IF response = "Y"
  54.          DO add_info WITH start_row, num_rows, start_col
  55.       ELSE
  56.          EXIT      
  57.       ENDIF
  58.    ENDIF
  59.    GO TOP
  60.    ***** erase the window *****
  61.    _SWNERASE(select_wn)
  62. ENDDO
  63. RETURN
  64.  
  65.  
  66. ********************************************************************
  67. ***** This function opens a window in the center of the screen *****
  68. ***** and asks the question specified. It will validate the    *****
  69. ***** response based upon the valid answer parameter.          *****
  70. ********************************************************************
  71. FUNCTION option_wn
  72. PARAMETER question, vald_ans
  73. PRIVATE qlen, wn_col, ans_col, wn_width, ans_wn, answer
  74. ***** be sure all parameters were passed *****
  75. IF PCOUNT() <> 2
  76.    ***** invalid number of parameters, return null *****
  77.    RETURN('')
  78. ENDIF
  79. ***** compute the windows width *****
  80. wn_width = LEN(question) + 4
  81. ***** compute the window starting column *****
  82. wn_col = INT((80 - wn_width) / 2)
  83. ***** compute the answer column *****
  84. ans_col = wn_col + wn_width - 1
  85. ***** initialize the window *****
  86. ans_wn = _SINIT_WN(wn_col, 11, wn_width, 1)
  87. ***** set window border character *****
  88. _SST_WNBC(ans_wn, 201)
  89. ***** draw the window *****
  90. _SDRW_WN(ans_wn)
  91. ***** write the question to the window *****
  92. _SWTE_TXT(ans_wn, ' ' + question)
  93. ***** initialize the answer variable *****
  94. answer = ' '
  95. ***** get the answer *****
  96. @ 12, ans_col GET answer PICTURE "!" VALID(answer $vald_ans)
  97. READ
  98. ***** remove the window *****
  99. _SREM_WN(ans_wn)
  100. ***** return the operators answer *****
  101. RETURN(answer)
  102.  
  103.  
  104. ***** all procedures and functions follow *****
  105. FUNCTION show_info
  106. ***** declare private variables *****
  107. private srec
  108. ***** initialize variables *****
  109. srec = 0
  110. ***** stuff the keyboard with various keystrokes  *****
  111. KEYBOARD CHR(1) + CHR(6) + CHR(5) + CHR(24) + CHR(3) + CHR(18) + CHR(27)
  112. ***** now call the _wn_dbf function *****
  113. srec = _WN_DBF(select_wn, "custno", "comp_name", "comp_addr1")
  114. ***** return *****
  115. RETURN(srec)
  116.  
  117.  
  118. PROCEDURE add_info
  119. PARAMETER a_rows, b_rows, c_rows
  120. private num_flds, scroll_rows, cnt1, cnt2, fldname, fldtype, fldsize
  121. private targ_row, targ_col, out1, out2, out3, out4, out5, out6, dummy
  122. ***** init dummy to a space *****
  123. dummy = ' '
  124. ***** get the number of fields in the database *****
  125. num_flds = FCOUNT()
  126. ***** calculate target row for reads *****
  127. targ_row = a_rows + b_rows
  128. ***** calculate target column for reads *****
  129. targ_col = c_rows + 3
  130. ***** calculate number or rows to redisplay *****
  131. scroll_rows = b_rows - 1
  132. ***** declare arrays with the same number of *****
  133. ***** elements as there are fields *****
  134. declare input_arr[num_flds]
  135. declare output_arr[num_flds]
  136. declare pict_arr[num_flds]
  137. ***** This step will initialize an array to the type and *****
  138. ***** and size of the corresponding fields in the database *****
  139. ***** in use. It also initializes an array containing the *****
  140. ***** code necessary to display any type of field using the *****
  141. ***** Windows For Clipper routine _SWTE_RECS(). *****
  142. ***** This step also selects a picture to use based on the field *****
  143. ***** type. If character, it will use the "@!" picture function, if *****
  144. ***** numeric it will use "999.999". The number of digits before *****
  145. ***** and after the decimal place will be accurate according to the *****
  146. ***** fields definition within the database. If a date field, an *****
  147. ***** "@D" picture will be used. If logical, an "L" picture will be used. *****
  148. ***** Please NOTE: The picture building portion of this step may be *****
  149. ***** modified to your taste but, the numeric fields need to be formatted *****
  150. ***** because transfering to a memory variable or array causes the *****
  151. ***** data in question to become 14 characters in length. *****
  152. ***** PLEASE NOTE that MEMO fields are not supported. *****
  153. ***** MEMO fields should be handled in a seperate routine. *****
  154. ***** If you need this routine to support memo fields and *****
  155. ***** have a seperate module to edit the memo field, you can *****
  156. ***** can add the following case statement: 
  157. *****        CASE fldtype = "M"
  158.         * <<<<< initialize a memo field. NOTE: The memo field is initialized
  159.         * <<<<< to a maximum size of 5000 bytes. This is in accordance with 
  160.         * <<<<< dBASE III +. You may change this size as desired. 
  161. *****        input_arr[cnt1] = SPACE(5000)
  162. *****        output_arr[cnt1] = fldname
  163. ***** The memo edit routine should be called after all other information *****
  164. ***** has been processed. This routine can be made generic, but the code *****
  165. ***** code to do so is not presented here. If this code is desired, you *****
  166. ***** may call me and we can work out the coding techniques. ***** 
  167. FOR cnt1 = 1 TO num_flds
  168.   fldname = fieldname(cnt1)
  169.   fldtype = TYPE("&fldname")
  170.   IF fldtype = "C"
  171.      fldsize = LEN(&fldname)
  172.   ELSE 
  173.      fldsize = 0
  174.   ENDIF
  175.   DO CASE
  176.      CASE fldtype = "C"
  177.         ***** initialize character type element *****
  178.         input_arr[cnt1] = SPACE(fldsize)
  179.         output_arr[cnt1] = fldname
  180.         pict_arr[cnt1] = ["@!"]
  181.      CASE fldtype = "N"
  182.         picttemp = "99999999999999"
  183.         ***** initialize a numeric element *****
  184.         fldval = str(&fldname)
  185.         ***** is there a decimal point *****
  186.         IF AT('.', fldval) <> 0
  187.            ***** yes, get the length of the field before the decimal *****
  188.            before_dec = AT('.',fldval) - 1
  189.            ***** now figure out how many digits past the decimal *****
  190.            after_dec = LEN(SUBSTR(fldval,AT('.',fldval) + 1))
  191.            ***** build the picture string *****
  192.            fldpict = ["] + SUBSTR(picttemp, 1, before_dec) + [.] + SUBSTR(picttemp,1,after_dec) + ["]
  193.            input_arr[cnt1] = 0.0
  194.            pict_arr[cnt1] = fldpict
  195.         ELSE
  196.            ***** no decimal point. Just store a 0 *****
  197.            input_arr[cnt1] = 0
  198.            pict_arr[cnt1] = ["] + SUBSTR(picttemp, 1, LEN(fldval)) + ["]
  199.         ENDIF
  200.         output_arr[cnt1] = "STR(" + fldname + ")"
  201.      CASE fldtype = "L"
  202.         ***** initialize a logical element *****
  203.         input_arr[cnt1] = .F.
  204.         output_arr[cnt1] = "IF(" + fldname + ",'Yes','No')"
  205.         pict_arr[cnt1] = ["L"]
  206.      CASE fldtype = "D"
  207.         ***** initialize a date element *****
  208.         input_arr[cnt1] = CTOD("  /  /  ")
  209.         output_arr[cnt1] = "CTOD(" + fldname + ")"
  210.         pict_arr[cnt1] = ["@D"]
  211.    ENDCASE
  212. NEXT
  213. ***** move to the bottom last record in the database *****
  214. GO BOTTOM
  215. ***** make sure we are at the end of file *****
  216. SKIP
  217. ***** main control loop *****
  218. FOR cnt1 = 1 TO num_flds
  219.     ***** back up scroll_rows records *****
  220.     SKIP (scroll_rows * -1)
  221.     ***** store the contents of the output array into regular *****
  222.     ***** memory variable because arrays have difficulty *****
  223.     ***** with macro expansion. The subscript has to be check *****
  224.     ***** to be sure that we do not exceed the array's size. *****
  225.     ***** The field type has to be checked also, to be sure we *****
  226.     ***** don't process a memo field. *****
  227.     IF cnt1 <= num_flds
  228.        IF TYPE(fieldname(cnt1)) <> "M"
  229.           out1 = output_arr[cnt1]
  230.        ELSE
  231.           out1 = "dummy"
  232.        ENDIF
  233.     ELSE
  234.        out1 = "dummy"
  235.     ENDIF
  236.     IF (cnt1 + 1) <= num_flds
  237.        IF TYPE(fieldname(cnt1 + 1)) <> "M"
  238.           out2 = output_arr[cnt1 + 1]
  239.        ELSE
  240.           out2 = "dummy"
  241.        ENDIF
  242.     ELSE
  243.        out2 = "dummy"
  244.     ENDIF
  245.     IF (cnt1 + 2) <= num_flds
  246.        IF TYPE(fieldname(cnt1 + 2)) <> "M"
  247.           out3 = output_arr[cnt1 + 2]
  248.        ELSE
  249.           out3 = "dummy"
  250.        ENDIF
  251.     ELSE
  252.        out3 = "dummy"
  253.     ENDIF
  254.     IF (cnt1 + 3) <= num_flds
  255.        IF TYPE(fieldname(cnt1 + 3)) <> "M"
  256.           out4 = output_arr[cnt1 + 3]
  257.        ELSE
  258.           out4 = "dummy"
  259.        ENDIF
  260.     ELSE
  261.        out4 = "dummy"
  262.     ENDIF
  263.     IF (cnt1 + 4) <= num_flds
  264.        IF TYPE(fieldname(cnt1 + 4)) <> "M"
  265.           out5 = output_arr[cnt1 + 4]
  266.        ELSE
  267.           out5 = "dummy"
  268.        ENDIF
  269.     ELSE
  270.        out5 = "dummy"
  271.     ENDIF
  272.     IF (cnt1 + 5) <= num_flds
  273.        IF TYPE(fieldname(cnt1 + 5)) <> "M"
  274.           out6 = output_arr[cnt1 + 5]
  275.        ELSE
  276.           out6 = "dummy"
  277.        ENDIF
  278.     ELSE
  279.        out6 = "dummy"
  280.     ENDIF
  281.     ***** loop to redisplay info *****
  282.     ***** clear the window *****
  283.     _SCLS_WN(select_wn)
  284.     FOR cnt2 = 1 TO scroll_rows
  285.        ***** write the field information by using the _swte_recs function *****
  286.        ***** no scroll value is needed because we will reference the *****
  287.        ***** field at the current array position and then forward. *****
  288.        ***** Also, only six fields are presented at a time. This should *****
  289.        ***** be enough to let the operator know what is expected next. *****
  290.        ***** DO not allow display of memo fields. This will cause strange *****
  291.        ***** results using the _SWTE_RECS() function. *****
  292.        IF TYPE(fieldname(cnt1)) <> "M"
  293.           _SWTE_RECS(select_wn, &out1, &out2, &out3, &out4, &out5, &out6)
  294.        ENDIF
  295.        SKIP
  296.     NEXT
  297.     ***** print field name on window border so operator will *****
  298.     ***** know what to enter *****
  299.     @ 12,11 SAY fieldname(cnt1) + SPACE(10 - LEN(fieldname(cnt1)))
  300.     ***** okay, now ready for operator to input data *****
  301.     ***** read all but memo fields *****
  302.     IF TYPE(fieldname(cnt1)) <> "M"
  303.        ***** get the picture string *****
  304.        pic_format = pict_arr[cnt1]
  305.        @ targ_row, targ_col GET input_arr[cnt1] PICTURE &pic_format
  306.        READ
  307.     ENDIF
  308. NEXT
  309. ***** This next section is not active, but the code is in place *****
  310. ***** so that you may see it. If this routine is used in your *****
  311. ***** application, some data formating (such as converting to UPPER CASE) *****
  312. ***** may be needed before allowing the information to go into the *****
  313. ***** database. *****
  314. ***** add a blank record *****
  315. APPEND BLANK
  316. ***** update field info with data *****
  317. FOR cnt1 = 1 to num_flds
  318.    fldname = fieldname(cnt1)
  319.    ***** don't do anything with memo fields *****
  320.    IF TYPE(fieldname(cnt1)) <> "M"
  321.       REPLACE &fldname WITH input_arr[cnt1]
  322.    ENDIF
  323. NEXT
  324. ***** return *****
  325. RETURN
  326.