ButtonGadgetEx.pbi (buttons with image and text), v0.4

Share your advanced PureBasic knowledge/code with the community.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

ButtonGadgetEx.pbi (buttons with image and text), v0.4

Post by Inf0Byt3 »

Description:
I have just finished a small include file that can help create buttons with both icons and text. The buttons are theme aware, so they will look and behave exactly like the ones that are drawn by the system. The code should work on all major OS's (tested on NT4, 98, 2K, XP, Vista, 7, 32 bit only). Unicode is also supported. Please note that this is the first time I use GDI all by myself so there could be leaks inside the code (although there were no obvious ones I could spot). If you see anything that doesn't fit please let me know.

Screenshot:
Image

License:
The include is licensed under the Zlib license.

Download:
Here's where you can download the package from :
Size: 7.16 KB
MD5: 8A8B251AB7D8B776C315607BD2F6BB6E
URL: http://www.bytessence.com/download/temp ... dgetEx.zip

Changelog:
* Version 0.4, 02 Jun 2010
- Made some minor changes, made possible to use this include with more than 1 window callback

* Version 0.3, 09 Feb 2010
- Added a 'disabled' state for the icon when the buttons are disabled (uses the DrawState API call - ugly but works)
- Made some optimizations, removed the #ODS_DEFAULT state (it is never used for the buttons it seems). Now the buttons also show the focus state correctly

* Version 0.2, 08 Feb 2010
- Implemented SetProp/GetProp calls instead of the linked list (thanks to Srod for the suggestion)

* Version 0.1, 07 Feb 2010
- First release
I hope this will be useful to someone :D.
Last edited by Inf0Byt3 on Wed Jun 02, 2010 7:50 pm, edited 3 times in total.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.1

Post by srod »

Very nice Info, great work. 8)

One tiny suggestion perhaps.... because of your use of a linked list, this library is not strictly threadsafe. Imagine one thread to be adding a button whilst another thread is owner-drawing a different button on a different window... crash! I know that most 'sensible' applications will not attempt such things, but, well, there's always idiots like me around creating non-sensible apps! :mrgreen:

Rather than employ mutex protection (which is not a good idea for a streamlined painting process) I would abandon the list and instead place a pointer to each button's structure within a 'window property'. Use SetProp_() to do this. Just remember to use RemoveProp_() when the button is destroyed because Windows will not do this automatically.

Just a suggestion! :)

Thanks again.
I may look like a mule, but I'm not a complete ass.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.1

Post by Inf0Byt3 »

Oops, I knew I forgot something. I'll have a look at SetProp/RemoveProp and update the code accordingly. Thank you for the suggestion.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by Inf0Byt3 »

Version 0.2 released, SetProp_() implemented :D.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by srod »

:)

That's the ticket! :wink:

Nice code there Info. Anyone wondering how to use XP's theming engine would do well to take a look at this code.

One observation Info... the disabled buttons still show the full icon. It would of course look better if you could 'grey' the icon in these cases. :wink:
I may look like a mule, but I'm not a complete ass.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by Inf0Byt3 »

srod wrote: :)

That's the ticket! :wink:
Thank you for the tip :D.
One observation Info... the disabled buttons still show the full icon. It would of course look better if you could 'grey' the icon in these cases. :wink:
Yes you're right. I wanted to draw them as grayed-out but didn't manage to find anything good for that. While searching on the net for a solution I found the DrawState_() API function but the results are really bad, the 'disabled' icon looked awful. Will search more and see what's to be done about this :).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by srod »

Yes I never managed to get any decent results from the DrawState_() api either.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by srod »

I may look like a mule, but I'm not a complete ass.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by Rook Zimbabwe »

Inf0byt3 is the MAN!!! :mrgreen: Thanks buddy this is going to be fun!

and SRod ain't not half bad at this codizing thing either! 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.2

Post by SFSxOI »

Very nice Inf0Byt3, Thank You :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.3

Post by Inf0Byt3 »

Thank you for the kind words lads :D.

I've uploaded version 0.3 which fixes some of the button states and makes the icon look disabled when the buttons are disabled. Note that I used DrawState_() for the moment as it was quicker to implement. I will be working to add Srod's excellent procedure for disabling icons as soon as I have more time.

Another feature to think about is to have total control over the state icons e.g. to define an icon for each button state (normal, hot, pressed, disabled).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.3

Post by Rook Zimbabwe »

My only want for this brilliant piece of code is:

If I could put the text on TOP of the icon! :D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.3

Post by Inf0Byt3 »

You can already do that :). To select where the icon will be placed (thus the text), use the following 'flags' when creating the buttons:
#BIGE_Top ;For putting the image on top of the text
#BIGE_Right ;For putting the text on the left and the image on the right
#BIGE_Bottom ;For putting the text on top of the image
#BIGE_Left ;For putting the image on the left and the text on the right
For example for your need you can use:

Code: Select all

ButtonGadgetEx(0, 10, 10, 100, 30, "Button", MyImage, #BIGE_Bottom)
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.3

Post by Rook Zimbabwe »

Ay no... I too that as text ABOVE the image or BELOW the image... I want teh text in FRONT of the image...

Hmmm... Subclassing?

http://www.codeproject.com/KB/miscctrl/ ... sdemo.aspx
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: ButtonGadgetEx.pbi (buttons with image and text), v0.3

Post by Rook Zimbabwe »

Just found this example of imagegadget and what I think is subclassing:

http://www.purebasic.fr/english/viewtop ... adget+text
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply