;Demo for dialog boxes                       v1.00

;*****************************************************************************
;*                                                                           *
;* DBCtrls.was                                                               *
;* Copyright (C) 1998 Symantec Corp.								                         *
;* All rights reserved.                                                      *
;*                                                                           *
;*                                                                           *
;* This script is written for other programmers to learn about dialog boxes  *
;* and their controls.  The multitude of comments explain the various        *
;* commands and why particular choices were made.  You can execute the       *
;* script and see the controls then compare that with the source code.       *
;*                                                                           *
;* This ASPECT SCRIPT is intended only as a sample of ASPECT programming.    *
;* Symantec makes no warranty of any kind, express or implied, including 		 *
;* without limitation, any warranties of merchantability and/or fitness      *
;* for a particular purpose.  Use of this program is at your own risk.       *
;*****************************************************************************

;*****************************************************************************
;*   GLOBAL VARIABLES                                                        *
;*****************************************************************************

string EditBoxString                   ;contents of the Edit Box
string FComboBoxInputFile              ;file name for list in the FCombo Box
string FComboBoxSelection              ;selection from the FCombo Box
string ListBoxList                     ;comma-delimited list for the List Box
string ListBoxSelection                ;selections from the List Box
string DirListBoxFileSpec              ;path/file spec for the Dir List Box
string DirListBoxSelectionFile         ;selected filespec from the DirList Box
string DirPathDirectory                ;current directory in DirList Box
integer Radio200                       ;identifier for radio buttons


;*****************************************************************************
;*                                                                           *
;* MAIN                                                                      *
;* The Main procedure calls BeginStart to display the first dialog box.  It  *
;* also initializes the list for the ListBox and the input file for the      *
;* FCombo box.  It defines the WHEN for events in dialog Box 0.              *
;*                                                                           *
;* Calls: BeginStart                                                         *
;* Modifies globals: ListBoxList, FComboBoxInputFile, DirListBoxSelectionFile*
;*                                                                           *
;*****************************************************************************
proc Main

                   ;When an event occurs in Dialog Box 0, call the process
                   ;Handler0 to process the event.
   when dialog 0 call Handler0
   
                   ;initialize the list for the ListBox
   ListBoxList = "Ford,Toyota,Nissan,Lincoln,Dodge,Studebaker"
                   ;Specify a file for the FComboBox input list
   FComboBoxInputFile = "FComboList.txt"
                   ;Specify a file for the file selections in the DirListBox.
   DirListBoxSelectionFile = "DirListBox.txt"
                   ;I want to start out with no files selected in the
                   ;DirListBox so delete any file selection files that might 
                   ;be existing.
   if isfile DirListBoxSelectionFile
      delfile DirListBoxSelectionFile
   endif
   
                   ;Call the process that displays the first Dialog Box
   BeginStart ()
   
endproc


;*****************************************************************************
;*                                                                           *
;* BeginStart                                                                *
;* The procedure BeginStart defines and displays Dialog Box 0.               *
;*                                                                           *
;* Calls: none                                                               *
;* Called by: Main                                                           *
;* Modifies globals: EditBoxString, ListBoxSelection, FComboBoxSelection,    *
;*     Radio200, DirPathDirectory                                            *
;*                                                                           *
;*****************************************************************************
proc BeginStart

                   ;
                   ;The dialog box is a style 71 which is
                   ;64 -- suspend script while the dialog box is displayed
                   ; 4 -- modeless 
                   ; 2 -- movable with caption
                   ; 1 -- centered
   dialogbox 0 64 56 500 230 71 "Cool Exceptional Stuff" 
        text 2 18 8 94 19 "This is an edit box.  Write in it then press Tab." left
                   ;
                   ;EditBoxString is what shows up in the Edit Box.  It can
                   ;be initialized and it can also report what the user 
                   ;typed in.
      editbox 3 18 32 94 11 EditBoxString 
      
      text 4 31 67 73 24 "This is a List Box.   Choose one or more cars then \
click Show." left 
                   ;
                   ;ListBoxList is the comma-delimited string that contains the 
                   ;list of items.  MULTIPLE says to let the user choose more
                   ;than one item.  ListBoxSelection is the comma-delimited
                   ;string that contains the selections.  Because it is a 
                   ;MULTIPLE, we need a PushButton to signal when all the 
                   ;selections have been made
      listbox 5 32 99 48 32 ListBoxList MULTIPLE ListBoxSelection 
      pushbutton 17 87 107 26 13 "Show" 
      
      text 6 15 148 106 19 "This is an FCombo Box.  Choose one product." left 
                   ;
                   ;SIMPLE says to display the list box at all times.
                   ;FComboBoxInputFile is the filespec of the text file that
                   ;contains the items, one to a line, for the list.
                   ;FcomboBoxSelection is the string with the selection.
      fcombobox 7 18 172 65 45 SIMPLE FComboBoxInputFile FComboBoxSelection 
      text 8 145 24 106 34 "These are Radio Buttons surrounded by a Group \
Box.  Click a Radio button and see what happens." left 
                   ;
                   ;the group box is only for decoration
      groupbox 9 149 68 100 51 "Group box label" 
                   ;
                   ;Radio200 is an integer that reports which Radio Button
                   ;was clicked.  The Event will be 200 for either button.
      radiogroup 200 Radio200
         radiobutton 201 167 83 42 11 "Top" 
         radiobutton 202 167 98 42 11 "Bottom" 
      endgroup
      text 12 158 172 76 24 "This is a Push Button.  Click on this button to \
exit." left 
                   ;
                   ;OK means that the box will be destroyed and any 
                   ;file-related variables will be updated
      pushbutton 13 182 200 40 13 "Exit" OK 
      text 14 306 35 140 32 "This is a DirListBox with a DirPath above it.  \
Click on another file or directory and watch DirPath change automatically.  \
Click on Report and see the chosen files." left 
                   ;
                   ;DirListBoxFileSpec is the filespec of the initial directory
                   ;listing.  MULTIPLE says more than one file may be selected.
                   ;DirListBoxSelectionFile is the name of the file that 
                   ;contains the selected files, one to a line.  Because the
                   ;user can choose MULTIPLE files, we need a PushButton to
                   ;segnify when all the choices have been made.
      dirlistbox 15 306 94 112 73 DirListBoxFileSpec MULTIPLE \
         DirListBoxSelectionFile 16 
      dirpath 16 306 73 114 11 DirPathDirectory
      pushbutton 18 350 170 40 13 "Report"
      
   enddialog 

endproc



;*****************************************************************************
;*                                                                           *
;* Handler0                                                                  *
;* The procedure Handler0 processes the events from dialog Box 0.            *
;*                                                                           *
;* Calls: none                                                               *
;* Called by: Main                                                           *
;* Modifies globals: EditBoxString, ListBoxSelection, FComboBoxSelection     *
;*                                                                           *
;*****************************************************************************
proc Handler0
  integer event0             ;which event happened in Dialog Box 0
  integer NumberOfCommas     ;# of commas tells us how many items
  integer Counter            ;counter in the For-loop
  string ListBoxReport       ;formatted string to report the ListBox Items
  string Token               ;holder for the tokenized ListBoxSelection
  string FileSelection       ;selected full file name from DirListBox
  string FileNameSelection   ;file name with path stripped off
  string SelectedFiles       ;concatenated string of selected files--DirListBox
  
                   ;query to find out which event happened
   dlgevent 0 event0

                   ;switch depending on which event occurred
   switch event0
                   ;Element 3 is the Edit Box.
      case 3
                   ;report what was entered into the Edit Box
         usermsg "The edit box contains `"%s`" " EditBoxString
                   ;blank out the Edit Box contents
         EditBoxString = ""
                   ;update the Edit Box
         dlgupdate 0 3
      endcase
                   ;Element 5 is the List Box.  Because the user can choose
                   ;multiple items, don't do anything on each individual
                   ;choice.  Wait until the Show button is clicked, then show
                   ;the choices.
      case 5
      endcase
                   ;Element 17 is the Show button to signal the multiple
                   ;have been made and should now be reported.
        case 17
                   ;The multiple choices come back to the script as a 
                   ;comma-delimited list.  One way to pull things out of the
                   ;list is to count the commas to see how many items there are.
                   ;Then use a For-loop to pull them out.
         strsearch ListBoxSelection "," NumberOfCommas
                   ;init the report string with the top line
         ListBoxReport = "You chose:"
                   ;strtok is 0-indexed so this will get all entries in the list
         for Counter = 0 upto NumberOfCommas
                   ;subtract the first item in the list and put it in Token
              strtok Token ListBoxSelection ","
                   ;add a CR/LF to the report string
              strcat ListBoxReport "`n`r"
                   ;add the list item to the report string
              strcat ListBoxReport Token
           endfor
                   ;report the list of items
           usermsg ListBoxReport
                   ;blank out the selections
           ListBoxSelection = ""
                   ;update the List Box
           dlgupdate 0 5
        endcase
                   ;Element 7 is the FCombo Box.  It can only have a single 
                   ;choice so we can report that as soon as it is chosen.
                   ;A button like the Show button would give the user a chance
                   ;to change his mind.
      case 7
                   ;format the report with the choice
           usermsg "Your favorite program is %s?" FComboBoxSelection
                   ;blank out the selection
           FComboBoxSelection = ""
                   ;update the FCombo Box
           dlgupdate 0 7
        endcase
                   ;
                   ;Element 13 is the OK button so exit the script here
        case 13
           exit
        endcase
                   ;
                   ;Element 15 is the DirList Box and Element 16 is the 
                   ;DirPath.  Don't do anything here because DirPath is
                   ;updated automatically.  the DirListBox is a MULTIPLE
                   ;selection so don't do anything until the user clicks
                   ;on the pushbutton to signify all the selections have
                   ;been made.
        case 15
        case 16
        endcase
                   ;
                   ;Element 18 is the PushButton that signifies when all
                   ;the choices have been made for the DirList Box.  The
                   ;choices are placed in a text file, DirListBoxSelectionFile,
                   ;which can be opened and processed.
        case 18
                   ;save the dialog to force writing and closing the file
           dlgsave 0 15
                   ;open the File Selections file with an index of 0.  Use 
                   ;READ because we only need to read.  Use TEXT to strip 
                   ;the CR/LF off the extracted string.
           fopen 0 DirListBoxSelectionFile READ TEXT
                   ;initialize the output string
           SelectedFiles = "You selected the fillowing file(s):"
                   ;Extract lines out of the file until we hit the End
                   ;of File
           while not FEOF 0
              fgets  0 FileSelection
                   ;Strip the full path off to make the report easier to
                   ;read and to lessen the chances that SelectedFiles will
                   ;exceed 256 characters
              getfilename FileNameSelection FileSelection
                   ;Add a CR/LF to the end of the output string
              strcat SelectedFiles "`n`r"
                   ;Add the file name to the output string
              strcat SelectedFiles FileNameSelection
           endwhile
                   ;Report the selected files
           usermsg SelectedFiles
                   ;Delete the current list of selected files
         delfile DirListBoxSelectionFile
                   ;Update the DirListBox so no files will be selected
         dlgupdate 0 15
       endcase
                    ;
                   ;Element 200 is the Radio Group.  When one of the radio
                   ;buttons is clicked, it generates an event 200 and the
                   ;element # of the radio button is stored in Radio200.
        case 200
                   ;Now switch on which of the radio buttons was clicked.
           switch Radio200
              case 201
                 usermsg "You chose the top one"
              endcase
              case 202
                 usermsg "You chose the bottom one"
                endcase
           endswitch
        endcase
                   ;this is the fall-through if the programmer hasn't
                   ;accounted for all the Elements in the Dialog Box.
      default
         usermsg "default %i"  event0
      endcase
  endswitch
 
endproc
