Check gadget enable/disable status

Just starting out? Need help? Post your questions and find answers here.
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Check gadget enable/disable status

Post by TI-994A »

Here's another cross-platform approach that handles the DisableGadget() and HideGadget() functions. Instead of making separate calls to enable/disable or show/hide the gadgets, only single calls are required to toggle their states. It makes use of the gadget-data integer on a bitwise level, allowing more than one flag to be set for each gadget:

Code: Select all

EnableExplicit
#bitShow = 0
#bitEnable = 1
Define gadgetData, wFlags, appQuit, vStat.s, eStat.s

Macro EnableGadget(gadgetNo)
  gadgetData = GetGadgetData(gadgetNo)
  gadgetData!(1<<#bitEnable)
  DisableGadget(gadgetNo,
  (gadgetData&(1<<#bitEnable))>>#bitEnable)
  SetGadgetData(gadgetNo, gadgetData)
EndMacro

Macro ShowGadget(gadgetNo)
  gadgetData = GetGadgetData(gadgetNo)
  gadgetData!(1<<#bitShow)
  HideGadget(gadgetNo,
  (gadgetData&(1<<#bitShow))>>#bitShow)
  SetGadgetData(gadgetNo, gadgetData)
EndMacro

Macro GadgetEnabled(gadgetNo)
  -(((GetGadgetData(gadgetNo)&(1<<#bitEnable))>>#bitEnable) - 1)
EndMacro

Macro GadgetVisible(gadgetNo)
  -(((GetGadgetData(gadgetNo)&(1<<#bitShow))>>#bitShow) - 1)
EndMacro

vStat = "Visible" : eStat = "Enabled"
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered 
OpenWindow(0, 50, 50, 220, 100, "Gadget States", wFlags)
ButtonGadget(0, 15, 10, 120, 25, "ENABLE/DISABLE -->")
ButtonGadget(1, 15, 40, 120, 25, "SHOW/HIDE -->")
ButtonGadget(2, 150, 10, 50, 55, "TEST")
TextGadget(3, 15, 75, 200, 20, "Test Button Status: Visible && Enabled")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      If EventGadget() < 2
        Select EventGadget()
          Case 0
            EnableGadget(2)
            eStat = StringField("Disabled Enabled", 
                                GadgetEnabled(2) + 1, " ")
          Case 1
            ShowGadget(2)
            vStat = StringField("Hidden Visible", 
                                GadgetVisible(2) + 1, " ")
        EndSelect
        SetGadgetText(3, "Test Button Status: " +
                          vStat + " && " + eStat)
       EndIf
  EndSelect
Until appQuit = 1
It essentially expands the single gadget-data integer into its multiple bits. :wink:
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 :D
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Check gadget enable/disable status

Post by Josh »

Can't understand the discussion here. Shardik shows here a perfect solution. It's clear, short, cross-platform, probably the fastest and will not make problems (can only speak from window, but I think API's in MacOS and Linux will work similar).

All other suggestions here are (sorry) bullsh.... and will bring only problems.

Apart from this, it's a shame that PB don't support such simple and essential commands native.
sorry for my bad english
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Check gadget enable/disable status

Post by TI-994A »

Josh wrote:Can't understand the discussion here. Shardik shows here a perfect solution. It's clear, short, cross-platform, probably the fastest and will not make problems (can only speak from window, but I think API's in MacOS and Linux will work similar).

All other suggestions here are (sorry) bullsh.... and will bring only problems.
With all due respect to Shardik, I can't understand your blatant contempt for the other suggestions. This is, after all, a programming forum, and solution or not, every contribution has its own value.

In any case, if you had bothered to see his edit history, you'd have realised that Shardik's full cross-platform solution was completed only after most of the other suggestions had been posted.

I don't mean to offend anyone, but I simply feel that such comments are not polite.
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 :D
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Check gadget enable/disable status

Post by Josh »

TI-994A wrote:In any case, if you had bothered to see his edit history, you'd have realised that Shardik's full cross-platform solution was completed only after most of the other suggestions had been posted.
As far as I know, the first code from Shardik included a Api solution for Window and Linux.

Last edited by Shardik on 31 Jul 2013 18:21, edited 4 times in total.
TI-994A wrote:This is, after all, a programming forum, and solution or not, every contribution has its own value.
You are right, everybody who wants to catch problems, should use one of the other solutions. The safest way to get problems is to change the status from the gadget.
sorry for my bad english
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Check gadget enable/disable status

Post by TI-994A »

Josh wrote:You are right, everybody who wants to catch problems, should use one of the other solutions. The safest way to get problems is to change the status from the gadget.
Still too much cynicism Josh; not enough reading.

How does my solution change the gadget status? 8)
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 :D
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Check gadget enable/disable status

Post by Josh »

TI-994A wrote:Still too much cynicism Josh; not enough reading.
How does my solution change the gadget status? 8)
Who says, that I was speaking about your code? I sayed "The safest way to get problems is to change the status from the gadget."
Not enough reading?

But I can confirm, you have chosen the second safest way to get problems :wink:
sorry for my bad english
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Check gadget enable/disable status

Post by TI-994A »

Josh wrote:...everybody who wants to catch problems, should use one of the other solutions.
Josh wrote:Who says, that I was speaking about your code?
Mine was not one of the other solutions?
Josh wrote:But I can confirm, you have chosen the second safest way to get problems :?:
You're making less and less sense with each post. :lol:
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 :D
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Check gadget enable/disable status

Post by RASHAD »

No code is safe unless the users are sure that there is no bugs
Are PB safe?
The answer is no
Are MS Windows is safe ?
The answer is no

Keep saying safe and not safe and you are away from being creative :)
Egypt my love
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Check gadget enable/disable status

Post by Shardik »

TI-994A wrote:In any case, if you had bothered to see his edit history, you'd have realised that Shardik's full cross-platform solution was completed only after most of the other suggestions had been posted.
Josh wrote:As far as I know, the first code from Shardik included a Api solution for Window and Linux.
To clarify this: my first posting at 31 Jul 2013 10:15 was a pure Windows API solution because I thought that the probability was high that the original poster would look for a Windows solution. But directly after posting I tried a Linux API solution and while editing my first posting to add the Linux API part and submitting it, I saw that Rashad had just posted his first example code needing no API at all and so being cross-platform. I remarked in my first edit that I currently wouldn't have the opportunity for a Mac solution because I don't have a Mac at work to try. In the evening at home on my iMac I tested the Mac API part and added it to my posting...

Of course I always studied the newly posted code examples but instead of responding to them I decided to edit again my first posting and put in the remark that my API solution won't have the disadvantages of the following non-API code examples.

I understand fully the wish to get around the use of API functions whenever possible. But I have the same oppinion as Josh that the disadvantage of the current non-API solutions in having to change a gadget's state speak against their use.

Furthermore for some time I have tried to collect all cross-platform API solutions in this posting in the hope that my cross-platform examples save some time for the devs if they decide to implement them natively in PureBasic. And indeed some of my cross-platform functions have already actually been implemented in PureBasic... 8)
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Check gadget enable/disable status

Post by TI-994A »

Shardik wrote:I understand fully the wish to get around the use of API functions whenever possible. But I have the same oppinion as Josh that the disadvantage of the current non-API solutions in having to change a gadget's state speak against their use.
Hello Shardik. I hope you don't think that I was disparaging your solution. And I hope you don't share Josh's impolite sentiments either. Because this is not an argument of which is the better solution, but simply one of common courtesy, which Josh clearly lacks. And when push came to shove, he couldn't even stand by his statements.

Hands down, yours is clearly the de facto solution here. :)
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 :D
Post Reply