[Module] ButtonEx (all OS)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] ButtonEx (all OS)

Post by Andre »

I made a little example to show the selecting of only one button (which is toggled then, previously toggled buttons loose this state).

Code: Select all

; ButtonEx example: 
; Select one button and set it in 'Toggle' (pressed) mode, but release other buttons which were toggled/pressed before...
; by André
; last update: 20th May 2020

XIncludeFile "ButtonExModule.pbi"

UsePNGImageDecoder()

#Window = 0

Enumeration 1
  #ButtonImg1
  #ButtonImg2
  #ButtonImg3
  #Image
  #Font
EndEnumeration

LoadFont(#Font, "Arial", 9, #PB_Font_Bold)
LoadImage(#Image, "Delete.png")

If OpenWindow(#Window, 0, 0, 450, 80, "ButtonEx: Select one button...", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
  
  ButtonEx::Gadget(#ButtonImg1, 10, 20, 100, 25, "Delete 1", ButtonEx::#MacOS, #Window) ; ButtonEx::#Toggle|ButtonEx::#MacOS
  ButtonEx::AddImage(#ButtonImg1, #Image, 16, 16, ButtonEx::#Left)
  
  ButtonEx::Gadget(#ButtonImg2, 120, 20, 100, 25, "Delete 2", ButtonEx::#MacOS, #Window) ; ButtonEx::#Toggle|ButtonEx::#MacOS
  ButtonEx::AddImage(#ButtonImg2, #Image, 16, 16, ButtonEx::#Right)
  ButtonEx::SetFont(#ButtonImg2, #Font)
  
  ButtonEx::Gadget(#ButtonImg3, 230, 20, 100, 25, "Delete 3", 0, #Window) ; ButtonEx::#Toggle|ButtonEx::#MacOS
  ButtonEx::AddImage(#ButtonImg3, #Image, 16, 16, ButtonEx::#Right)
  ButtonEx::SetFont(#ButtonImg3, #Font)
  ButtonEx::SetColor(#ButtonImg3, ButtonEx::#BackColor, $AADDDD)
  ButtonEx::SetColor(#ButtonImg3, ButtonEx::#BorderColor, $FF0000)
  
  
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case ButtonEx::#Event_Gadget ; works with or without EventType()
        Select EventGadget()
          Case #ButtonImg1
            Debug "Delete1 button pressed"
            If ActiveButton > 0
              ;Debug "Last selected button: " + ActiveButton
              ;Debug "State of this button: " + ButtonEx::GetState(ActiveButton)
              ButtonEx::SetState(ActiveButton, 0)   ; reset the 'Toggle' state for the last selected button
            EndIf
            ButtonEx::SetState(#ButtonImg1, ButtonEx::#Toggle)
            ActiveButton = #ButtonImg1
          Case #ButtonImg2
            Debug "Delete2 button pressed"
            If ActiveButton > 0
              ;Debug "Last selected button: " + ActiveButton
              ;Debug "State of this button: " + ButtonEx::GetState(ActiveButton)
              ButtonEx::SetState(ActiveButton, 0)   ; reset the 'Toggle' state for the last selected button
            EndIf
            ButtonEx::SetState(#ButtonImg2, ButtonEx::#Toggle)
            ActiveButton = #ButtonImg2
          Case #ButtonImg3
            Debug "Delete3 button pressed"
            If ActiveButton > 0
              ;Debug "Last selected button: " + ActiveButton
              ;Debug "State of this button: " + ButtonEx::GetState(ActiveButton)
              ButtonEx::SetState(ActiveButton, 0)   ; reset the 'Toggle' state for the last selected button
            EndIf
            ButtonEx::SetState(#ButtonImg3, ButtonEx::#Toggle)
            ActiveButton = #ButtonImg3
        EndSelect
    EndSelect
  Until Event = #PB_Event_CloseWindow
  
  CloseWindow(#Window)
EndIf
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: [Module] ButtonEx (all OS)

Post by Thorsten1867 »

Update:
  • Added: ButtonEx::CombineToogle()
  • Added: ButtonEx::Free()
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] ButtonEx (all OS)

Post by Andre »

Thorsten1867 wrote:Update:
  • Added: ButtonEx::CombineToogle()
  • Added: ButtonEx::Free()
Thank you for this extension, Thorsten! :D

Would you be so kind, to provide a small example how to use the new 'CombineToggle' (you have a little spelling mistake in your command name, I think) modus? Thanks!
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: [Module] ButtonEx (all OS)

Post by Thorsten1867 »

Code: Select all

XIncludeFile "ButtonExModule.pbi"

UsePNGImageDecoder()

#Window = 0

Enumeration 1
  #ButtonImg1
  #ButtonImg2
  #ButtonImg3
  #Image
  #Font
EndEnumeration

LoadFont(#Font, "Arial", 9, #PB_Font_Bold)
LoadImage(#Image, "Delete.png")

If OpenWindow(#Window, 0, 0, 450, 80, "ButtonEx: Select one button...", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
 
  ButtonEx::Gadget(#ButtonImg1, 10, 20, 100, 25, "Delete 1", ButtonEx::#MacOS|ButtonEx::#Toggle, #Window) ; ButtonEx::#Toggle|ButtonEx::#MacOS
  ButtonEx::AddImage(#ButtonImg1, #Image, 16, 16, ButtonEx::#Left)
 
  ButtonEx::Gadget(#ButtonImg2, 120, 20, 100, 25, "Delete 2", ButtonEx::#MacOS|ButtonEx::#Toggle, #Window) ; ButtonEx::#Toggle|ButtonEx::#MacOS
  ButtonEx::AddImage(#ButtonImg2, #Image, 16, 16, ButtonEx::#Right)
  ButtonEx::SetFont(#ButtonImg2, #Font)
 
  ButtonEx::Gadget(#ButtonImg3, 230, 20, 100, 25, "Delete 3", ButtonEx::#Toggle, #Window) ; ButtonEx::#Toggle|ButtonEx::#MacOS
  ButtonEx::AddImage(#ButtonImg3, #Image, 16, 16, ButtonEx::#Right)
  ButtonEx::SetFont(#ButtonImg3, #Font)
  ButtonEx::SetColor(#ButtonImg3, ButtonEx::#BackColor, $AADDDD)
  ButtonEx::SetColor(#ButtonImg3, ButtonEx::#BorderColor, $FF0000)
 
  ButtonEx::CombineToggle(#ButtonImg1, "Delete")
  ButtonEx::CombineToggle(#ButtonImg2, "Delete")
  ButtonEx::CombineToggle(#ButtonImg3, "Delete")
  
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case ButtonEx::#Event_Gadget ; works with or without EventType()
        Select EventGadget()
          Case #ButtonImg1
            Debug "Delete1 button pressed"
          Case #ButtonImg2
            Debug "Delete2 button pressed"
          Case #ButtonImg3
            Debug "Delete3 button pressed"
        EndSelect
    EndSelect
  Until Event = #PB_Event_CloseWindow
 
  CloseWindow(#Window)
EndIf
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] ButtonEx (all OS)

Post by Andre »

Thank you, Thorsten for this additional example! :D

It works well, but you need to fix the two spelling error in the ButtonEx module: replace the "old" ButtonEx::CombineToogle() in declaration and procedure with the correct "CombineToggle()" ;-)

When testing the module on MacOS I found a bug:
The round corners on MacOS are painted with black instead of the real (MacOS) window background. Are you able to fix this?
Image

Thank you!
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
X0r
Enthusiast
Enthusiast
Posts: 138
Joined: Tue May 01, 2007 3:49 am
Location: Germany

Re: [Module] ButtonEx (all OS)

Post by X0r »

Great lib; thank you so much!

However, changing the focus of the buttons via tab seems not to work? (Tested with Win10)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] ButtonEx (all OS)

Post by Andre »

@XOr: I don't think that ButtonEx supports focus (visible rectangle)... at least for now. Doesn't work on MacOS too.

@All: anyone has an idea, what need to be changed in the ButtonEx module to avoid the black-painted round corners, when using ButtonEx on MacOS with the related flag? (see my screenshot above)
As far as I can see, Thorsten used the code snippets successfully mentioned here: viewtopic.php?f=27&t=75014&p=552167&hil ... or#p552167
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: [Module] ButtonEx (all OS)

Post by Thorsten1867 »

Update: Added: OSX_GadgetColor()
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] ButtonEx (all OS)

Post by Andre »

Thorsten1867 wrote:Update: Added: OSX_GadgetColor()
Thank you, Thorsten!

But unfortunately the 'black corner' problem shown above is still present...
Anyone of the MacOS experts can take a look what's still wrong? Thank you in advance! :-)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: [Module] ButtonEx (all OS)

Post by doctorized »

Downloaded the latest version and tried this:

Code: Select all

ButtonEx::Gadget(#ButtonML, 345, 20, 120, 100, "Asdfghjkpoiuytreq card", ButtonEx::#MultiLine|ButtonEx::#MacOS, #Window)
LoadFont(#Font_Custom,  "Arial", 11)
ButtonEx::SetFont(#ButtonML,#Font_Custom)
custom font and text width almost the same as the button's width, the text appears a little bit upper than it should be. Any suggestions why?
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: [Module] ButtonEx (all OS)

Post by doctorized »

I have a window with some buttonEx buttons and I use the following code to attach menu to them:

Code: Select all

MarkIcon = CatchImage(#PB_Any, ?MarkStart, ?MarkEnd - ?MarkStart)
UnMarkIcon = CatchImage(#PB_Any, ?UnMarkStart, ?UnMarkEnd - ?UnMarkStart)
MarkIcon2 = CatchImage(#PB_Any, ?MarkStart, ?MarkEnd - ?MarkStart)
UnMarkIcon2 = CatchImage(#PB_Any, ?UnMarkStart, ?UnMarkEnd - ?UnMarkStart)
For i=0 To 11
	If CreatePopupImageMenu(#Btn_menu1 + i)
      MenuItem(#MenuMark1 + i, "item 1", ImageID(MarkIcon))
      MenuItem(#MenuUnMark1 + i, "item 2", ImageID(MarkIcon))
      MenuBar()
      MenuItem(#MenuMarkNext1 + i, "item 3", ImageID(MarkIcon2))
      MenuItem(#MenuUnMarkNext1 + i, "item 4", ImageID(MarkIcon2))
   EndIf
   ButtonEx::AttachPopupMenu(i,#Btn_menu1 + i)
Next
Is there a better/faster way to attach the menu? Now I am creating a custom menu with RASHAD's code and the code to create and attach the menus is now slow (it takes some seconds instead of a second that was required before). It would be nice to have one menu to attach on every button and get the button id with EventGadget() or something like that. Any suggestions?
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: [Module] ButtonEx (all OS)

Post by doctorized »

Where did AttachPopupMenu() go?
Post Reply