Pushbutton Test Toggling

Just starting out? Need help? Post your questions and find answers here.
dfw1417
User
User
Posts: 16
Joined: Tue Mar 01, 2022 1:45 am

Pushbutton Test Toggling

Post by dfw1417 »

Tried to do the toggle thing using the NOT keyword and even the gadgettoggle but just could not figure it out and apparently you cannot use the NOT without a structure which I also was not interested in doing. So here is what I came up with a full example in hopes it may help someone else who desires a simplified way of doing it. Please go easy on me since I am not an advanced purebasic coder just have a lot of different basic coding experience and a lot of VB prior to net and some Powerbasic as well. So I hope this will help someone but if I am out of line then I apologize in advance. It does compile on both v6.04 and v6.10.

Code: Select all

;4/8/2024 - Test F12 Information Toggle On/Off
EnableExplicit
;----- Support Definitions
Global Event.i
Global Result.i
Global PBState.i
;----- Procedures
Procedure Load_Default_Fonts()
 Protected DFID.i                                          ;Status 
 DFID = LoadFont(0, "Arial Bold", 8)                        ;Font ID Default Font
 SetGadgetFont(#PB_Default, DFID)                           ;Assign Default Font to Gadgets 
EndProcedure
Procedure ToggleInfoOn()
  HideGadget(9001, #False)
  HideGadget(122, #False)
  HideGadget(123, #False)  
  PBState = 0
EndProcedure
Procedure ToggleInfoOff()
  HideGadget(9001, #True)
  HideGadget(122, #True)
  HideGadget(123, #True) 
  PBState = 1
EndProcedure
Procedure HideInfo()
  HideGadget(9001, #True)
  HideGadget(122, #True)
  HideGadget(123, #True)  
  PBState = 1
EndProcedure
;----- Main Program Prep
InitMouse()
InitKeyboard()
Load_Default_Fonts()
;----- Main Program Code
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 1024, 768, " Test Toggle - Press F12 to Toggle Developer Information", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  SetWindowColor(0, $9BB1C8);Tan
  AddKeyboardShortcut(0, #PB_Shortcut_F12 | #PB_Shortcut_F12, 7)
  ButtonGadget(1113, 450, 25, 115, 25, "End Program", #PB_Button_Default )                ;Exit Button 
  ;----- Text Editor Frame
  FrameGadget(122, 590, 515, 420, 240, "")
  ;----- Text Editor Frame Title
  TextGadget(123, 595, 516, 120, 15, "    Developer Info")
  SetGadgetColor(123, #PB_Gadget_BackColor, $9BB1C8)
  ;----- Text Editor Box
  EditorGadget(9001, 590, 530, 420, 230)
  SetGadgetColor(9001, #PB_Gadget_BackColor, $C0E0FF)
  HideInfo()
;----- Events Loop         
 Repeat
   Event = WaitWindowEvent(20)  
   Select Event
    Case #PB_Event_Menu
 		  Select EventMenu() 
  ;----- F12 Function Toggle Develop Information      
 		    Case 7: 
 		      If PBState
 		        ToggleInfoOn()
 		      Else
 		        ToggleInfoOff()
 		      EndIf            
      EndSelect
  EndSelect			         
 If Event = #PB_Event_Gadget   
 	  Result = EventGadget()  
 	  If Result = 1113
 	    End
 	  EndIf
 EndIf 
 Until Event = #PB_Event_CloseWindow
EndIf
Experience is what you get when you didn't get what you thought you wanted!
Acer TC895 32gb i5-2.9ghz Win10 1.5tb ssd Asus Gsyn 4k and Vizio 1080 hd monitors.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Pushbutton Test Toggling

Post by netmaestro »

When you assign a large static gadget number to a gadget you reserve room for (gadgetnumber-1) gadgets. In your case up to 11 thousand. Consider using low static numbers or better still, #PB_Any. This way you don't waste scads and scads of space in the executable.
BERESHEIT
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Pushbutton Test Toggling

Post by infratec »

Hi

I don't know exactly what you mean.
Maybe it is something like this:

Code: Select all

Procedure SetInfo(State.i)
  State = ~State
  HideGadget(9001, State)
  HideGadget(122, State)
  HideGadget(123, State)  
EndProcedure

Code: Select all

Case 7: 
 		      PBState = ~PBState
 		      SetInfo(PBState)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Pushbutton Test Toggling

Post by netmaestro »

Code: Select all

 EditorGadget(9001, 590, 530, 420, 230)
With this line you reserve space in the executable for 9000 gadgets you're not using.
Much better is:

Code: Select all

Global gEdit = EditorGadget(#PB_Any, 590, 530, 420, 230)
where you don't waste any space in the executable at all. Static (hardcoded) gadget numbers have the unique quality that whatever number you choose and hardcode, space will be reserved for gadgets from 0 up to that number. So if you're going to use static gadget numbers, keep them low so as not to waste any space. But using #PB_Any provides you with a descriptive variable in addition to not reserving space for any more gadgets than the one you defined. For these two reasons, it is the better choice. Remember, you'll have to read and understand your code in the coming years and after much time goes by a number by itself won't mean anything to you.
BERESHEIT
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Pushbutton Test Toggling

Post by infratec »

I always use Enumeration for Gadgets, Windows ...
They starts at 0 and you don't need variables for them.
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Pushbutton Test Toggling

Post by mk-soft »

I'll put my template for a small test application here.
Named enumeration is used here. The numbering can also be continued with the same name in another include file.
Each object type of Purebasic has its own numbering. See F1 PureBasic objects overview.

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButtonOk
  #MainButtonCancel
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx - 10, dy - 45)
  ResizeGadget(#MainButtonok, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonok, 10, dy - 35, 120, 30, "Ok")
    ButtonGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30, "Abbruch")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButtonOk
              ;
            Case #MainButtonCancel
              ;
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Pushbutton Test Toggling

Post by SMaag »

Code: Select all

Procedure SetInfo(State.i)
  State = ~State  			; dont do this!		
  HideGadget(9001, State)
  HideGadget(122, State)
  HideGadget(123, State)  
EndProcedure
Don't do
State = ~State
because it is a bitwise NOT
You have to be sure you pass only 0 for false and -1 for True, otherwise it will not worik correct!

if State is PurBasic #TRUE what is +1
a bitwise NOT will result in %11111..1110 and that's still #TRUE!

Code: Select all

; Inverting BOOL is only safe in this way
If State : State = #False : Else State = #True : Endif

; or in this way
x = #True
y = #False

x = Bool(Not Bool(x))
Y = Bool(Not Bool(y))

Debug x
Debug y
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Pushbutton Test Toggling

Post by infratec »

SMaag wrote: Tue Apr 09, 2024 1:44 pm You have to be sure you pass only 0 for false and -1 for True, otherwise it will not worik correct!
Since #False is 0 and everything else is true, it works up to now.
What else will you use as false?

Yes, it will fail if Fred changes the constants:
#False = 1
#True = 2

Or if he checks if true is exactly 1
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Pushbutton Test Toggling

Post by charvista »

Welcome to the magic world of PureBasic!
You can use the bitwise XOR to toggle between 0 and 1.
I quickly simplified your program by merging ToggleInfoOn() and ToggleInfoOff() to simply ToggleInfo().
Inside the ToggleInfo(), the PBState is toggled, and you toggle at the same time the gadgets.

Code: Select all

;4/8/2024 - Test F12 Information Toggle On/Off
EnableExplicit
;----- Support Definitions
Global Event.i
Global Result.i
Global PBState.i
;----- Procedures
Procedure Load_Default_Fonts()
 Protected DFID.i                                          ;Status 
 DFID = LoadFont(0, "Arial Bold", 8)                        ;Font ID Default Font
 SetGadgetFont(#PB_Default, DFID)                           ;Assign Default Font to Gadgets 
EndProcedure
Procedure ToggleInfo()
  PBState ! 1; this will toggle between 0 and 1 (the ! is a Bitwise XOR)
  HideGadget(9001, PBState) ; you can use the PBState here because #True is always 1 and #False is always 0
  HideGadget(122, PBState)
  HideGadget(123, PBState)  
EndProcedure
Procedure HideInfo()
  HideGadget(9001, #True)
  HideGadget(122, #True)
  HideGadget(123, #True)  
  PBState = 1
EndProcedure
;----- Main Program Prep
InitMouse()
InitKeyboard()
Load_Default_Fonts()
;----- Main Program Code
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 1024, 768, " Test Toggle - Press F12 to Toggle Developer Information", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  SetWindowColor(0, $9BB1C8);Tan
  AddKeyboardShortcut(0, #PB_Shortcut_F12 | #PB_Shortcut_F12, 7)
  ButtonGadget(1113, 450, 25, 115, 25, "End Program", #PB_Button_Default )                ;Exit Button 
  ;----- Text Editor Frame
  FrameGadget(122, 590, 515, 420, 240, "")
  ;----- Text Editor Frame Title
  TextGadget(123, 595, 516, 120, 15, "    Developer Info")
  SetGadgetColor(123, #PB_Gadget_BackColor, $9BB1C8)
  ;----- Text Editor Box
  EditorGadget(9001, 590, 530, 420, 230)
  SetGadgetColor(9001, #PB_Gadget_BackColor, $C0E0FF)
  HideInfo()
;----- Events Loop         
 Repeat
   Event = WaitWindowEvent(20)  
   Select Event
    Case #PB_Event_Menu
 		  Select EventMenu() 
  ;----- F12 Function Toggle Develop Information      
 		    Case 7: 
 		      ToggleInfo()
      EndSelect
  EndSelect			         
 If Event = #PB_Event_Gadget   
 	  Result = EventGadget()  
 	  If Result = 1113
 	    End
 	  EndIf
 EndIf 
 Until Event = #PB_Event_CloseWindow
EndIf
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply