'===========================================================================
' Subject: HIGHLIGHTING MENU                  Date: 01-27-98 (05:26)       
'  Author: Juan Carlos Hernandez              Code: QB, QBasic, PDS        
'  Origin: juan.c.hernandez@usa.net         Packet: MENU.ABC
'===========================================================================
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'                             Highlighting Menu
'                             -----------------
'
' Written By: Juan Carlos Hern ndez A., Dominican Republic
' Date      : May 17, 1997
'
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Hi everybody, the following code was originally designed in Qbasic
'to serve as a reference for future programs, creating a menu screen
'that could be used repeatly. When programming was a hobby for me, a tried
'to figure how a menu really works. I had to guess !
'
'The tecnic is very simple, the program keeps a loop wating for the  user to
'press the keys up and down, once the user presses one of them, a counter
'registers into a variable called counter% each strike, this new value is then
'passed to a subroutine named "Menu" which will print again the labels
'on screen highlighting the last selection registered; this gives what
'I called, a mirage to the user. For the purpose, I have chosen a fictitious
'menu for an address book.
'
'
'It would be a great honor if you use it in your won programs, of course, if
'you find it useful.
'
'Perhaps this may give you an idea of how
'to create a highlighting menu choice.
'                                                        
'I apologize if you find words difficult to understand, I wrote them in
'Spanish, but I try to translate them in my comments.
'
'                 Cuidense mucho ! ("Take care")
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Here we go:
'First the subroutines, that as teaches Gregg Perry in his wonderful book
'Qbasic by example, can be called as many times you need them in your program
'using meaningful names for them.

DECLARE SUB Message ()
DECLARE SUB EndProgram ()
DECLARE SUB Help (COUNTER%)
DECLARE SUB DialogBox ()
DECLARE FUNCTION FechaHispana$ () 'This subroutine name means in Spanish
                                  'that the date will be presented with the day before the month.
DECLARE SUB Clock ()
DECLARE SUB Reiniciar (F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%) 'Restarts the menu.
DECLARE SUB OpcionSelect (COUNTER%)
DECLARE SUB menu (F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%, COUNTER%)
DECLARE SUB Formato ()
DECLARE SUB Poem ()


DIM SHARED Keycode

COMMON SHARED KeyP$ 'Makes the variable "shared" so that the program can used
                    'inside the separated subroutines.

DO ' This loop will be kept until the user presses the arrows Up and Down


     'Initialization of the variables which will determine which must appear
     'highlighted.
     'I use these numeric variables to set the color of the labels in the menu
     '"F" stands for foreground and "B" for background.

F1% = 7: B1% = 0: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 6


COUNTER% = 1  'Counter used to keep track of the last position selected by user.

CALL Formato  'This subroutine draws the borders of our menu
              'printing first the external square and after that
              'the inner square. You can translate this name as "format".


CALL menu(F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%, COUNTER%) 'Calls the routine that will print the menu
                                                    'options passing the parameters of color
                                                    'that way the new printing will show a different
                                                    'selection highlighted, given the mirage to the
                                                    'user that actually he is moving to a different
                                                    'label on screen.
                                                    
LOOP UNTIL (KeyP$ = CHR$(27)) 'If user presses the Escape key, the loop ends.

COLOR 7, 0  'Sets the foreground in white and background in black
  
CLS

CALL Poem

END

SUB Clock
LOCATE 1, 1, 0   'Lets make the cursor desappear.
                
                 'Here we disassemble the hour to convert it to a new format
                 'from 24 digits hours to just 12 digits.
Tiempo$ = TIME$  'Asigns the hour to the variable string.
Hora$ = LEFT$(Tiempo$, 2)     'Saves the hour without minutes or seconds.
Minuto$ = MID$(Tiempo$, 4, 2) 'Retains only minutes.

'Determina si la hora es mayor a el numero 12, si es cierto restamos doce
'y obtenemos la hora normal sin ser mayor a 12.
'This section determines if the hour is greater than 12, if true, substracts
'12 and obtains the normal hour that is 12.

IF (VAL(Hora$) > 12) THEN
  Hora$ = STR$(VAL(Hora$) - 12)
  MyTime$ = Hora$ + ":" + Minuto$ + " " + "pm"
ELSEIF (VAL(Hora$) < 10) THEN 'Si la hora es menor a diez, sera igual a un solo digito en pantalla eliminandose el cero delante de la hora que normalmente aparece.
  MyTime$ = RIGHT$(Hora$, 1) + ":" + Minuto$ + " " + "am"
ELSEIF (VAL(Hora$) = 12) THEN
  MyTime$ = Hora$ + ":" + Minuto$ + " " + "pm"
ELSEIF (VAL(Hora$) = 11) THEN
  MyTime$ = Hora$ + ":" + Minuto$ + " " + "am"
END IF

LOCATE 2, 55: COLOR 7, 1
PRINT FechaHispana$; "  "; MyTime$
END SUB

SUB DialogBox

COLOR 7, 1

 '-----Section to display a dialog box on the lower area of the screen.
 '-----explaning the task of the highlighted selection.

LOCATE 21, 11
PRINT CHR$(201); STRING$(59, 205); CHR$(187)
LOCATE 22, 11
 PRINT CHR$(186); STRING$(59, " "); CHR$(186)
LOCATE 23, 11
 PRINT CHR$(200); STRING$(59, 205); CHR$(188)
LOCATE 22, 13
PRINT "Option:"


END SUB

SUB EndProgram
   COLOR 7, 0
    CLS
   END
END SUB

FUNCTION FechaHispana$
'-----------------------------------------------------------------------------
'Function used to convert the North American time format to Latin American
'format, displaying first the day followed by the month and later the year
'------------------------------------------------------------------------------
Fecha$ = DATE$

 ME$ = LEFT$(Fecha$, 2)
 
  DIA$ = MID$(Fecha$, 4, 2)

 ANO$ = RIGHT$(Fecha$, 4)

FechaHispana$ = DIA$ + "-" + ME$ + "-" + ANO$  ' The data contained in the
                                               ' function will be equal to
                                               ' the concatenation of
                                               ' DIA(DAY) MES(MONTH) A¥O(YEAR)
END FUNCTION

SUB Formato

'Rutina para dibujar el cuadro que engloba el menu.
'Code section to draw a square.
'
COLOR 7, 1

CLS
LOCATE 1, 1         'Draws External box or square
PRINT CHR$(201); STRING$(78, 205); CHR$(187)
FOR a% = 2 TO 21
LOCATE a%, 1
PRINT CHR$(186); SPACE$(78); CHR$(186)
NEXT a%
LOCATE 22, 1
PRINT CHR$(200); STRING$(78, 205); CHR$(188)
                   
                    'Here we begin to draw an internal square.
COLOR 7, 6: LOCATE 5, 13
PRINT CHR$(201); STRING$(55, 205); CHR$(187)
FOR a% = 6 TO 17
LOCATE a%, 13
PRINT CHR$(186); SPACE$(55); CHR$(186)
NEXT a%
LOCATE 18, 13
PRINT CHR$(200); STRING$(55, 205); CHR$(188)
COLOR 7, 1

END SUB

SUB Help (COUNTER%)
'******************************************************************************
'Subroutine to determine the help text to be printed in the lower dialog box
'according to the highlighted label in the menu.
'******************************************************************************

IF (COUNTER% = 1) OR (COUNTER% = 5) OR (COUNTER% = -3) THEN
    Ayuda$ = "1.Permit to enter new data in book.     "
 ELSEIF (COUNTER% = 0) OR (COUNTER% = -4) OR (COUNTER% = 4) THEN
    Ayuda$ = "4.Finalize program and return to system."
 ELSEIF (COUNTER% = -1) OR (COUNTER% = 3) THEN
    Ayuda$ = "3.Permit to edit or erase data in book. "
 ELSEIF (COUNTER% = 2) OR (COUNTER% = -2) THEN
    Ayuda$ = "2.Presents on screen the selected data. "
END IF

'----This small section place the help message in correct position on screen.
COLOR 7, 1
  LOCATE 22, 21
PRINT Ayuda$

END SUB

SUB menu (F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%, COUNTER%)
  'Rutina para imprimir los labels en pantalla de las opciones disponibles
  'para el usuario.
COLOR 7, 1
LOCATE 4, 29
PRINT "MENU ADDRESS BOOK SYSTEM"

COLOR F1%, B1%
LOCATE 8, 29
PRINT "1.Enter new data.           "

COLOR F2%, B2%
LOCATE 10, 29
PRINT "2.Display records on screen."

COLOR F3%, B3%
LOCATE 12, 29
PRINT "3.Edit data in book.        "

COLOR F4%, B4%
LOCATE 14, 29
PRINT "4.End the session.          "
   'Display a message on screen to use the menu.
COLOR 7, 1

LOCATE 19, 25
PRINT "Use the arrow keys or press option"

LOCATE 20, 28
PRINT "number to select a function"

CALL Clock                   'Llama la rutina que imprime la hora.
 CALL DialogBox              'Display help box at the foot of screen.
 CALL Help(COUNTER%)         'Display help text inside dialog box.
CALL OpcionSelect(COUNTER%)  'Starts the routine that handles the menu selection.

END SUB

SUB Message

CLS

LOCATE 10, 32, 0
PRINT "Label 1 pressed."
LOCATE 12, 32
PRINT "Press Esc to continue."




DO                 'We create a loop until the user presses Esc.
 Answer$ = INKEY$
LOOP UNTIL (Answer$ = CHR$(27))

LOCATE 1, 1, 1

END SUB

SUB OpcionSelect (COUNTER%)
      
DO
 
 DO                    'Inner loop to constantly print the time until
  CALL Clock           'the user decides to strike a key (of course on the keybord.)
  KeyP$ = INKEY$
 LOOP UNTIL LEN(KeyP$) 'It detects when a key is pressed.

LOCATE 1, 1, 1         'Here we make the cursor visible.

 IF (KeyP$ = CHR$(0) + CHR$(72)) THEN          '""  Depending upon the key pressed
     COUNTER% = COUNTER% - 1                   '     Up or Down, the counter will increase or decrease.
 ELSEIF (KeyP$ = CHR$(0) + CHR$(80)) THEN      '""
     COUNTER% = COUNTER% + 1
 END IF


'This section makes the mirage possible.
IF COUNTER% = 2 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 0: F3% = 7: B3% = 6: F4% = 7: B4% = 6
ELSEIF COUNTER% = 3 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 0: F4% = 7: B4% = 6
ELSEIF COUNTER% = 4 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 0
ELSEIF COUNTER% = 5 THEN
   F1% = 7: B1% = 0: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 6
   COUNTER% = 1
ELSEIF COUNTER% = 0 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 0
ELSEIF COUNTER% = -1 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 0: F4% = 7: B4% = 6
ELSEIF COUNTER% = -2 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 0: F3% = 7: B3% = 6: F4% = 7: B4% = 6
ELSEIF COUNTER% = -3 THEN
   F1% = 7: B1% = 0: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 6
ELSEIF COUNTER% = -4 THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 0
   COUNTER% = 4
ELSEIF COUNTER% = 1 THEN
   F1% = 7: B1% = 0: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 6
END IF
    CALL Reiniciar(F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%)'Calls routine that will re-print the labels of our menu.

    CALL Help(COUNTER%)

'Our loop is gonna repeat again and again until the Keys Escape, Enter or a number ranging from 1 to four be pressed.

LOOP UNTIL (KeyP$ = CHR$(27)) OR (KeyP$ = CHR$(13)) OR (KeyP$ = CHR$(49)) OR (KeyP$ = CHR$(50)) OR (KeyP$ = CHR$(51)) OR (KeyP$ = CHR$(52))

IF (KeyP$ = CHR$(27)) THEN EXIT SUB 'We assure the program will stop if the
                                    'the user presses 'Esc

                                    'Aqui se verifica cual de los numeros fue presionado, para hacer la elecci¢n en el menu
                                    'hecho esto el programa sombreara el label correcto.
IF (KeyP$ = CHR$(50)) THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 0: F3% = 7: B3% = 6: F4% = 7: B4% = 6
ELSEIF (KeyP$ = CHR$(49)) THEN
   GOTO CaptData
ELSEIF (KeyP$ = CHR$(51)) THEN
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 0: F4% = 7: B4% = 6
ELSEIF (KeyP$ = CHR$(52)) THEN      'Ends the program.
   F1% = 7: B1% = 6: F2% = 7: B2% = 6: F3% = 7: B3% = 6: F4% = 7: B4% = 0
   CALL EndProgram
END IF

   CALL Reiniciar(F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%) 'Re-prints the menu with the
                                                  'new specifications.


 IF (COUNTER% = 1 OR COUNTER% = -3 OR KeyP$ = CHR$(49)) THEN 'Call caption of data if the test is true.

CaptData:
   CALL Message
 ELSEIF (COUNTER% = 0 OR COUNTER% = 4) THEN                  'Ends the program.
   CALL EndProgram
 END IF
END SUB

SUB Poem
LOCATE 2, 1
PRINT "Last night  I  tasted  the flavor of"
PRINT "your lips, I felt  the  heat of your"
PRINT "body, and I carried home  your scent"
PRINT "Oh my cherub!, sadness  is something"
PRINT "of the past, no more tears in my eyes"
PRINT
PRINT "Now, everything looks more beautiful"
PRINT "to  me, the  mornings  are  full of"
PRINT "colors  with roses  everywhere, the"
PRINT "falling  rain is a blessing from the"
PRINT "sky."
PRINT
PRINT "From now on, when I see a flower it"
PRINT "will be  difficult  not to think of"
PRINT "you and  remenber the splendor of"
PRINT "your hair and softness of your skin."
PRINT
PRINT "Juan Carlos"

END SUB

SUB Reiniciar (F1%, B1%, F2%, B2%, F3%, B3%, F4%, B4%)

COLOR 7, 1
LOCATE 4, 29
PRINT "MENU ADDRESS BOOK SYSTEM"

COLOR F1%, B1%
LOCATE 8, 29
PRINT "1.Enter new data.           "

COLOR F2%, B2%
LOCATE 10, 29
PRINT "2.Display records on screen."

COLOR F3%, B3%
LOCATE 12, 29
PRINT "3.Edit data in book.        "

COLOR F4%, B4%
LOCATE 14, 29
PRINT "4.End the session.          "
   'Display a message on screen to use the menu.
COLOR 7, 1

LOCATE 19, 25
PRINT "Use the arrow keys or press option"

LOCATE 20, 28
PRINT "number to select a function"

END SUB
