Having recently started developing a cross platform app in Purebasic (coming "soon") I found the need for a robust, easy to use and feature rich cross platform button control.

There's two main ways to use this library. You can either code the buttons yourself (see Example.pb), or you can use the form designer with ButtonImage's (see Example Form.pb and Example Form.pbf). The library will replace all ButtonImage's on a form with a BetterButton so no additional programming will be needed if you don't want to.
States
There is a very simple system of adding additional states using the ToolTip text. Sates are entered as a comma separated list surrounded by [[ ]]. Each button has a default "up" state and can have any combination of the following states:
- down - used for a toggle state, when the button is down (checked)
- up_hover - shown when the mouse if over the up (unchecked) state button
- down_hover - shown when the mouse is over the down (checked) state button
- up_disabled - shown when the button is disabled in its up (unchecked) state
- down_disabled - shown then the button is disabled in its down (checked) state
- focus - enables button to retain focus, uses hover state as focus
- focusno - enables button to retain focus, does NOT use hover state as focus so you can create a custom focus using callbacks
These states can be added at form design time by entering a comma separated list into the ToolTip of a button surrounded by [[ ]] at any position in the Tooltip. For example:
- [[down]]Tooltip goes here
- [[up_hover,down_hover]]Tooltip goes here
- Tooltip goes here[[focus,down,up_disabled,down_disabled]]
- plus_up.png
plus_down.png
plus_up_hover.png
plus_down_hover.png
plus_up_disabled.png
plus_down_disabled.png
Callbacks can now be entered at design time using the form editor. This is done in a similar way to the states above using the tooltips of the buttons. Callback procedures and events are separated by a comma, and events are separated using vertical pipes |. There can be only one callback procedure per set (( )). However you can add multiple callbacks for the same event by creating another set ((NameOfCallbackProcedure,Event1))
To add a callback use the following format:
((NameOfCallbackProcedure,Event1|Event2|Event3|...))
For example:
- ((ButtonClick,#B8_Event_ButtonPressed))
This will call ButtonClick when the button is pressed - ((ButtonClick,#B8_Event_ButtonPressed))((ButtonCallbackFocus,#B8_Event_DrawFocusFront|#B8_Event_DrawFocusBack))
This will call ButtonClick when the button is pressed and ButtonCallbackFocus when we want to render the front and back of the button for special button effects - ((ButtonClick,#B8_Event_ButtonPressed))((ButtonImage_11Callback,#B8_Event_Draw|#B8_Event_ButtonPressed))
This will call ButtonClick when the button is pressed and ButtonImage_11Callback when we want to render the button or the button is pressed
Code: Select all
Runtime Procedure CallbackProcedureName(event)
Protected *button.B8_Button = B8_GetEventButton() ; find out which button caused the callback, this can be left out if you have one callback per button
EndProcedure
Cutom events include:
- #B8_Event_LeftButtonUp
#B8_Event_LeftButtonDown
#B8_Event_ButtonPressed
#B8_Event_Draw
#B8_Event_DrawBack
#B8_Event_DrawFocusBack
#B8_Event_DrawFront
#B8_Event_DrawFocusFront
#B8_Event_KeyUp
#B8_Event_KeyDown
#B8_Event_MouseEnter
#B8_Event_MouseLeave
#B8_Event_Focus
#B8_Event_LostFocus
- Cross platform
- 6 states, up, down, up (mouse over), down (mouse over), up disabled and down disabled
- Callbacks, no need to clutter up your main loop
- Replace those radio buttons, push buttons and toggle buttons
- State management inside the button with direct access via Get/Set to find out what the button is doing
- Focus tracking (tab and shift+tab enabled)
- Simulated transparency of the canvas button so you can have graduated backgrounds on your forms
- Correct state management for situations such as mouse down, drag off then back on button
- Callbacks for multiple render layers, so you can draw in-front / behind the button, want a button with a counter on it? No problem!
- Custom focus callbacks so you can draw the focus how you like
- Callback to replace the main draw routine if you want to perform some custom button rendering
- Dont like making multiple images for your states, one simple callback for an XOR'd button
This library also ships with my new ImageLibrary which I needed to code to allow certain features in BetterButtons. See ImageLibrary.pb for more information (link to follow).
I'm open to constructive comments on coding style, ease of use and feature requests. If you find any bugs/problems or have any questions please post them here. I'm currently testing this on Windows 8.1 and the latest OSX, feedback from other platforms would be much appreciated. The example app might have some funky formatting on other platforms, it was just done to show the features and how to achieve them.
I hope you find it useful.
Regards,
Julian
Version History
28/04/15 - v0.1.0
- Initial release
- Bug fixes + AddImages()
- Purebaseic Form compatability added, automatically change ButtonImage to Canvas
- [[down,up_hover,down_hover,up_disabled,down_disabled]] can now be added to ToolTip to set up states
- ((NameOfCallbackProcedure,Event1|Event2|Event3)) can now be added to ToolTip for custom callbacks
- GetGadgetDisabled(Gadget) returns #True / #False if the gadget is currentl disabled
- SetGadgetState can be used to set button state, 3rd parameter added to trigger callback on #True / #False
- GetGadgetState(Gadget) returns checked status of button #True / #False
- Uses new ImageLibrary
- Overloaded the following procedures to allow seamless integration:
ButtonImageGadget
CloseWindow
DisableGadget
FreeGadget
GadgetToolTip
GetGadgetDisabled
GetGadgetState
OpenWindow
SetGadgetState (additional optional parameter, triggerCallback #True/#False)
- Added GetGadgetTooltip(canvas) - Returns a string containing the Tooltip of the BetterButton
- Added GetGadgetTooltipRaw(canvas) - Returns a string containing the raw Tooltip data of the BetterButton (includes bracketed data)
- Added more events for callbacks:
#B8_Event_MouseEnter (see button 2 for example)
#B8_Event_MouseLeave (see button 2 for example)
#B8_Event_Focus
#B8_Event_LostFocus - Bug fixes:
When debugging is enabled via Tooltips (was incorrectly enabling focus)
Fixed error when trying to set a tooltip of a non-BetterButton