Check gadget enable/disable status
-
- Enthusiast
- Posts: 339
- Joined: Tue Jan 30, 2007 5:47 am
- Location: Hong Kong
Check gadget enable/disable status
Hi,
We can use disableGadget() function to set the gadget enable/disable status
BUT
How can we check the gadget enable/disable status
something like,
getGadgetEnableStatus(#GadgetID)
We can use disableGadget() function to set the gadget enable/disable status
BUT
How can we check the gadget enable/disable status
something like,
getGadgetEnableStatus(#GadgetID)
Re: Check gadget enable/disable status
Cross-platform API-solution for Linux, MacOS and Windows:
Update: I extended the Windows API code with API code for Linux and MacOS to offer a true cross-platform solution without the disadvantages of the code examples of the following postings... 
For those of you who don't need a cross-platform solution and don't want the additional source code lines for the other operating systems you only need to remove the unwanted source code lines. For Windows you simply change the procedure to
Code: Select all
Procedure IsGadgetDisabled(GadgetNumber)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
Protected *Widget.GtkWidget = GadgetID(GadgetNumber)
ProcedureReturn Bool(*Widget\state)
CompilerCase #PB_OS_MacOS
ProcedureReturn CocoaMessage(0, GadgetID(GadgetNumber), "isEnabled") ! 1
CompilerCase #PB_OS_Windows
ProcedureReturn IsWindowEnabled_(GadgetID(GadgetNumber)) ! 1
CompilerEndSelect
EndProcedure
OpenWindow(0, 0, 0, 250, 105, "Disable/enable buttons...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 15, 230, 30, "Disabled Button") : DisableGadget(0, 1)
ButtonGadget(1, 10, 60, 230, 30, "Enabled Button") : DisableGadget(1, 0)
Debug IsGadgetDisabled(0)
Debug IsGadgetDisabled(1)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

For those of you who don't need a cross-platform solution and don't want the additional source code lines for the other operating systems you only need to remove the unwanted source code lines. For Windows you simply change the procedure to
Code: Select all
Procedure IsGadgetDisabled(GadgetNumber)
ProcedureReturn IsWindowEnabled_(GadgetID(GadgetNumber)) ! 1
EndProcedure
Last edited by Shardik on Wed Jul 31, 2013 5:21 pm, edited 4 times in total.
Re: Check gadget enable/disable status
Cross platform
Code: Select all
Procedure IsGadgetDisabled(GadgetNumber)
SetActiveGadget(GadgetNumber)
If GetActiveGadget() < 0
Debug "Gadget "+Str(GadgetNumber)+" Disabled"
Else
Debug "Gadget "+Str(GadgetNumber)+" Enabled"
EndIf
EndProcedure
OpenWindow(0, 0, 0, 250, 105, "Disable/enable buttons...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 15, 230, 30, "Disabled Button") : DisableGadget(0, 1)
ButtonGadget(1, 10, 60, 230, 30, "Enabled Button") : DisableGadget(1, 0)
IsGadgetDisabled(0)
IsGadgetDisabled(1)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Egypt my love
Re: Check gadget enable/disable status
> SetActiveGadget(GadgetNumber)
Eww, yuck!
You shouldn't change the focus to test a state.
The gadget with the focus should stay that way at all times:
Eww, yuck!

The gadget with the focus should stay that way at all times:
Code: Select all
Procedure IsGadgetDisabled(GadgetNumber)
SetActiveGadget(GadgetNumber)
If GetActiveGadget() < 0
Debug "Gadget "+Str(GadgetNumber)+" Disabled"
Else
Debug "Gadget "+Str(GadgetNumber)+" Enabled"
EndIf
EndProcedure
OpenWindow(0, 0, 0, 250, 150, "Disable/enable buttons...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 15, 230, 30, "Disabled Button") : DisableGadget(0, 1)
ButtonGadget(1, 10, 60, 230, 30, "Enabled Button") : DisableGadget(1, 0)
StringGadget(2, 10, 110, 230, 30, "This is meant to have the focus!")
SetActiveGadget(2)
IsGadgetDisabled(0)
IsGadgetDisabled(1)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: Check gadget enable/disable status
Hi SkyManager. Here's a simple macro-based solution with no additional variables or arrays to maintain.SkyManager wrote:How can we check the gadget enable/disable status
something like getGadgetEnableStatus(#GadgetID)
Code: Select all
Macro EnableGadget(gadgetNo)
DisableGadget(gadgetNo, 0)
SetGadgetData(gadgetNo, 0)
EndMacro
Macro DisableGadgetEx(gadgetNo)
DisableGadget(gadgetNo, 1)
SetGadgetData(gadgetNo, 1)
EndMacro
Procedure GadgetStatus(gadgetNo)
ProcedureReturn -(GetGadgetData(gadgetNo) - 1)
EndProcedure
OpenWindow(0, 50, 50, 220, 75, "Gadget Enabled/Disabled")
ButtonGadget(0, 60, 15, 100, 20, "Click to toggle")
ButtonGadget(1, 60, 40, 100, 20, "TEST BUTTON")
DisableGadgetEx(1)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 0
If GadgetStatus(1)
DisableGadgetEx(1)
Else
EnableGadget(1)
EndIf
Case 1
DisableGadgetEx(1)
EndSelect
EndSelect
Until appQuit = 1

Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Check gadget enable/disable status
OK PB how about that

Code: Select all
Procedure IsGadgetDisabled(GadgetNumber)
Result = GetActiveGadget()
If IsGadget(result) And Result <> GadgetNumber
DisableGadget(Result,1)
EndIf
SetActiveGadget(GadgetNumber)
If GetActiveGadget() < 0
Debug "Gadget "+Str(GadgetNumber)+" Disabled"
Else
Debug "Gadget "+Str(GadgetNumber)+" Enabled"
EndIf
If IsGadget(result)
DisableGadget(Result,0)
SetActiveGadget(Result)
EndIf
EndProcedure
OpenWindow(0, 0, 0, 250, 150, "Disable/enable buttons...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 15, 230, 30, "Disabled Button") : DisableGadget(0, 1)
ButtonGadget(1, 10, 60, 230, 30, "Enabled Button") : DisableGadget(1, 0)
StringGadget(2, 10, 110, 230, 30, "This is meant to have the focus!")
SetActiveGadget(2)
IsGadgetDisabled(0)
IsGadgetDisabled(1)
Debug GetActiveGadget()
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Egypt my love
-
- Enthusiast
- Posts: 542
- Joined: Tue Apr 24, 2012 5:08 pm
- Location: Ontario, Canada
Re: Check gadget enable/disable status
@TI-994A -- This will only work if the gadget data is not already in use.
@RASHAD -- In an event driven system it's bad policy to change the program state in order to test the program state. All testing needs to be passive, otherwise the act of temporarily moving focus can cause big problems in the code.
In our own applications we assign a structure to each gadget to track it's status. The address of the structure is stored in the gadget data field. We do this because of the limitations in PB, such as the lack of many events and features that are standard in the native operating systems.
Each time we change the application state we modify the gadget status information. We also create custom events to simulate missing PB events, such as GotFocus and LostFocus for buttons.
@RASHAD -- In an event driven system it's bad policy to change the program state in order to test the program state. All testing needs to be passive, otherwise the act of temporarily moving focus can cause big problems in the code.
In our own applications we assign a structure to each gadget to track it's status. The address of the structure is stored in the gadget data field. We do this because of the limitations in PB, such as the lack of many events and features that are standard in the native operating systems.
Each time we change the application state we modify the gadget status information. We also create custom events to simulate missing PB events, such as GotFocus and LostFocus for buttons.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
~ Spike Milligan
Re: Check gadget enable/disable status
Sorry BorisTheOld
I do not agree with you
Change the status of a an object (not the program) temporary then get the right status back at the same time will not cause any harm
sorry
I do not agree with you
Change the status of a an object (not the program) temporary then get the right status back at the same time will not cause any harm
sorry
Egypt my love
-
- Enthusiast
- Posts: 542
- Joined: Tue Apr 24, 2012 5:08 pm
- Location: Ontario, Canada
Re: Check gadget enable/disable status
When you change the state of a gadget, you're changing the state of the program. Events will be created that can have unexpected consequences. As TI-994A said, it's a bad idea.RASHAD wrote:Change the status of a an object (not the program) temporary then get the right status back at the same time will not cause any harm
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
~ Spike Milligan
Re: Check gadget enable/disable status
Of course, but the gadget data is perfect for these types of flags. After all, not much can be done with this single numeric field.BorisTheOld wrote:@TI-994A -- This will only work if the gadget data is not already in use.
I believe that's a misquote.BorisTheOld wrote:As TI-994A said, it's a bad idea.

However, both points have merit. On the one hand, it may be alright to programmatically change focus, if such a change is associated with some user-initiated event. But, on the other hand, it may not be a good idea to automate such changes, as it can interfere with normal input and work flow. IMHO.
Last edited by TI-994A on Wed Jul 31, 2013 3:14 pm, edited 1 time in total.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Check gadget enable/disable status
I still do not agree with you
Shall we ask Fred to not support anymore HideGadget(),DisableGadget()
and MicroSoft as well ?
TI-994A did not say it's a bad idea
PB who said that
Shall we ask Fred to not support anymore HideGadget(),DisableGadget()
and MicroSoft as well ?
TI-994A did not say it's a bad idea
PB who said that
Egypt my love
-
- Enthusiast
- Posts: 542
- Joined: Tue Apr 24, 2012 5:08 pm
- Location: Ontario, Canada
Re: Check gadget enable/disable status
That's a specious argument.RASHAD wrote:Shall we ask Fred to not support anymore HideGadget(),DisableGadget()
and MicroSoft as well ?
I'm saying that if you temporarily move focus from one gadget to another, then move focus back to the original, you will create a series of LostFocus/GotFocus events that might cause problems in the code. Depending on the program logic, fields may get re-initialized out of sequence, or data may be written to files. It's just not a good idea.
I misspelt PB's name.RASHAD wrote:TI-994A did not say it's a bad idea
PB who said that

For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
~ Spike Milligan
Re: Check gadget enable/disable status
> you will create a series of LostFocus/GotFocus events
+1. Of course, you can code around it (set a flag when
you're doing focus change) but why should we need to?
A cross-platform PureBasic command is needed for this.
+1. Of course, you can code around it (set a flag when
you're doing focus change) but why should we need to?
A cross-platform PureBasic command is needed for this.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: Check gadget enable/disable status
Not sure it makes a difference: PB 5.20 Beta 8 x86 (Windows 7 64bit)
Even though the documentation states there is no return value...
- and yes I realize that the state is being changed (but only half of the time)
*** I would use the Shardik solution posted above before my example ***
Even though the documentation states there is no return value...
- and yes I realize that the state is being changed (but only half of the time)

*** I would use the Shardik solution posted above before my example ***
Code: Select all
Procedure.s IsGadgetEnabled(gNumber)
Result.s = ""
Select 8
Case DisableGadget(gNumber, 0)
DisableGadget(gNumber, 1)
Result = "Disabled"
Default
Result = "Enabled"
EndSelect
ProcedureReturn Result
EndProcedure
If OpenWindow(0, 0, 0, 250, 105, "Disable/enable buttons...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 15, 230, 30, "Disabled Button") : DisableGadget(0, 1)
ButtonGadget(1, 10, 60, 230, 30, "Enabled Button") : DisableGadget(1, 0)
Debug IsGadgetEnabled(0)
Debug IsGadgetEnabled(1)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
If you're not investing in yourself, you're falling behind.
My PureBasic Stuff ➤ FREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Re: Check gadget enable/disable status
> if you temporarily move focus from one gadget to another,
> then move focus back to the original
You will interrupt the user when they're typing or selecting text.
> then move focus back to the original
You will interrupt the user when they're typing or selecting text.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.