November 25, 2020, 04:25:14 AM

Author Topic: [bb] Multiple Options by Vic 3 Babes [ 1+ years ago ]  (Read 505 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Multiple Options by Vic 3 Babes [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Multiple Options
Author : Vic 3 Babes
Posted : 1+ years ago

Description : A simple way to handle loads of boolean options in a program

Code :
Code: BlitzBasic
  1. One way of handling lots of boolean options that your program may have.  A lot better than typing loads of CreateButton and SetButtonState lines anyway.
  2.  
  3. [codebox]
  4. Const PICSON=$1, SOUNDON=$2, MUSICON=$4, VISAIDON=$8, AUTOON=$10, BLOCKON=$20
  5. Const BIGWINON=$40, CLEARON=$80, NINTHON=$100, TENTHON=$200
  6. Const BITSUSED=$3F, NUMPREFS=10
  7. Const DEFAULTPREFS=SOUNDON Or MUSICON Or BIGWINON
  8.  
  9. Const CANCELLED=0, CHANGED=1
  10.  
  11. Const ESCAPE=1
  12.  
  13. Const KEY_PRESSED=$101
  14. Const GADGET_ACTION=$401
  15. Const CLOSE_WINDOW=$803, ACTIVATE_WINDOW=$804
  16.  
  17. Const CLICK_BUTTON=1, CHECK_BOX=2, RADIO_BUTTON=3
  18.  
  19. Global preferences, newprefs, changedprefs, centrex, centrey, prefsloop, testbit, gadloop
  20.  
  21. Dim gadgets(9)
  22.  
  23. ;we don't really need these next four lines - they just make the main loop
  24. ;a bit shorter in reporting actions - see the commented lines in the
  25. ;change_prefs function where the checkbox gadgets are created - if you uncomment
  26. ;those lines - then remove or comment these ones out.
  27. Dim gadtext$(9)
  28. Restore gadnames
  29. For gadloop=0 To 9
  30.         Read gadtext$(gadloop)
  31. Next
  32. ;-----------------------------------------
  33. centrex=(ClientWidth(Desktop()) Shr 1)
  34. centrey=(ClientHeight(Desktop()) Shr 1)
  35. main_window=CreateWindow( "Main Program",centrex-125,centrey-150,250,300,0,35)
  36. SetMinWindowSize main_window
  37. prefsbutton=CreateButton("Prefs",10,10,60,20,main_window)
  38. textbox=CreateTextArea(10,40,230,250,main_window)
  39. SetTextAreaColor textbox,$FF,$FF,$80,True
  40.  
  41. preferences=DEFAULTPREFS
  42. Repeat
  43.         WaitEvent()
  44.         event=EventID()
  45.         Select event
  46.                 Case GADGET_ACTION
  47.                         evsource=EventSource()
  48.                         If evsource=prefsbutton
  49.                                 If change_prefs()
  50.                                         ;make changes to program
  51.                                         change$=""
  52.                                         changedprefs=newprefs Xor preferences
  53.                                         For prefsloop=0 To NUMPREFS-1
  54.                                                 testbit=1 Shl prefsloop
  55.                                                 If changedprefs And testbit
  56.                                                         ;this preference has changed
  57.                                                         change$=gadtext$(prefsloop)
  58.                                                         If newprefs And testbit Then change$=change$+" ON" Else change$=change$+" OFF"
  59.                                                         ;alternative example
  60.                                                         ;Select testbit
  61.                                                         ;       Case PICSON                     If newprefs and testbit then turn pics on else turn pics off
  62.                                                         ;       Case MUSICON            If newprefs and testbit then turn music on else turn it off
  63.                                                         ;       etc...
  64.                                                         ;End Select
  65.                                                 EndIf
  66.                                                 If change$<>"" Then AddTextAreaText textbox,change$+Chr$(13)+Chr$(10): change$=""
  67.                                         Next
  68.                                         preferences=newprefs
  69.                                 Else
  70.                                         AddTextAreaText textbox,"Cancelled"+Chr$(13)+Chr$(10)
  71.                                 EndIf
  72.                                 AddTextAreaText textbox,"============="+Chr$(13)+Chr$(10)
  73.                         EndIf
  74.                 Case CLOSE_WINDOW
  75.                         finished=True
  76.                 Case KEY_PRESSED
  77.                         If EventData()=ESCAPE Then finished=True
  78.         End Select
  79. Until finished
  80. End
  81.  
  82. ;#########################################
  83.  
  84. Function change_prefs()
  85. Local numgads, enabled;,gadtext$
  86.  
  87. Window=CreateWindow( "Change Prefs",centrex-100,centrey-150,200,300,main_window,49)
  88. SetMinWindowSize window
  89. ;see comment at top for these next three commented lines, and commented local string above
  90. ;Restore gadnames
  91. For gadnum=0 To NUMPREFS-1
  92. ;       Read gadtext$
  93. ;       gadgets(gadnum)=CreateButton(gadtext$,10,5+(gadnum * 25),150,20,window,CHECKBOX)
  94. ;remove or comment next line if uncommenting above
  95.         gadgets(gadnum)=CreateButton(gadtext$(gadnum),10,5+(gadnum * 25),150,20,window,CHECK_BOX)
  96.         SetButtonState gadgets(gadnum),1 And (preferences Shr gadnum)
  97. Next
  98.  
  99. ok=CreateButton("OK",10,270,60,20,window)
  100. cancel=CreateButton("Cancel",130,270,60,20,window)
  101. DisableGadget ok
  102. enabled=False
  103. action=-1
  104. Repeat
  105.         WaitEvent()
  106.         event=EventID()
  107.         Select event
  108.                 Case CLOSE_WINDOW
  109.                         If EventSource()=window
  110.                                 action=CANCELLED
  111.                         Else
  112.                                 End
  113.                         EndIf
  114.                 Case GADGET_ACTION
  115.                         Select EventSource()
  116.                                 Case ok         action=CHANGED
  117.                                 Case cancel     action=CANCELLED
  118.                                 Default
  119.                                         newprefs=0
  120.                                         For gadnum=0 To NUMPREFS-1
  121.                                                 newprefs=newprefs Or (ButtonState(gadgets(gadnum)) Shl gadnum)
  122.                                         Next
  123.                                         If (newprefs Xor preferences)
  124.                                                 If Not(enabled) Then EnableGadget ok: enabled=True
  125.                                         Else
  126.                                                 If enabled Then DisableGadget ok: enabled=False
  127.                                         EndIf
  128.                         End Select
  129.                 Case ACTIVATE_WINDOW
  130.                         If EventSource()<>window
  131.                                 ActivateWindow window
  132.                         EndIf
  133.                 Case KEY_PRESSED        ;doesn't work if checkbox clicked - but $105 event reported
  134.                         If EventData()=ESCAPE Then action=CANCELLED
  135.         End Select
  136. Until action > -1
  137.  
  138. FreeGadget window
  139.  
  140. Return action
  141. End Function
  142. ;#########################################
  143.  
  144. .gadnames
  145. Data "Pictures","Sound","Music","Visual-aid","Auto-change number","Block illegal moves"
  146. Data "Big window","Clear selected number","Ninth","Tenth"
  147. [/codebox]
  148.  
  149. N.B. The action performed by this code so far as the Activate_Window and Close_Window actions are concerned in the change_prefs() function depend upon whether the program is running in debug mode or not.  I haven't quite figured out why the Activate_Window takes precedence over Close_Window when debugging is switched off.
  150.  
  151. Try clicking the Main Window's close button when the prefs window is open - in both debug and non-debug modes to see what I mean.  And no - it's not Blitz that ends the program in debug mode - you can check that by change the "End" command to "ActivateWindow window" in change_prefs.
  152.  
  153. -----
  154.  
  155. One thing of note - if you click on a check-box so that there is a dotted rectangle around it, and then press F10 - have a look at what event is reported in the debug window.  The EventSource() and EventData() are both 0 though.  This happens when any gadget has a dotted rectangle around it - even ordinary buttons.
  156.  
  157. Nothing serious - but thought I'd mention it since I noticed it.  As far as I know, $2004 is when you grab something with the mouse - like the title-bar or a slider bar, and $2005 is when you release the mouse in the same circumstances.


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal