Simple Method for Multiple ToolBars (uses TBPro Library)

Share your advanced PureBasic knowledge/code with the community.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Simple Method for Multiple ToolBars (uses TBPro Library)

Post by ebs »

I needed a way to change the toolbar on a window based on a user selection.
I created the toolbars using Danilo's great TBPro library, and used a combo box to switch between them.
The interesting part was getting a combo box in all the toolbars without having to create/manage more than one combo box.

This example shows a simple way to switch toolbars using a combo box:

Code: Select all

Declare MakeToolBar(TBNumber.l, StartButton.l, WindowHandle.l, IconSize.l, Style.l)
Declare ShowToolBar(ToolBar.l, Show.l)
   
; create window and gadget list
hWnd.l = OpenWindow(0, 100, 200, 200, 260, #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_Invisible, "ToolBar example")
CreateGadgetList(WindowID())

; make three similar toolbars
Style.l = #TBPro_FLAT|#TBpro_TRANSPARENT|#TBpro_BORDER
MakeToolBar(0, #TBpro_CUT, hWnd, 16, Style)
MakeToolBar(1, #TBpro_DELETE, hWnd, 16, Style)
MakeToolBar(2, #TBpro_FIND, hWnd, 16, Style)

; show first toolbar
ShowToolBar(0, #True)

; show window
HideWindow(0, #False)

Repeat
  EventID.l = WaitWindowEvent()
  Select EventID
    Case #PB_EventGadget
      If EventGadgetID() = 1
        ; combobox changed - switch toolbars
        If EventType() = #PB_EventType_RightClick
          ; show selected toolbar, hide others
          Index.l = GetGadgetState(1)
          For TB.l = 0 To 2
            If TB = Index
              ShowToolBar(TB, #True)
            Else
              ShowToolBar(TB, #False)
            EndIf
          Next
        EndIf
      EndIf
    Case #PB_EventMenu
      ; toolbar button clicked
      MessageRequester("Information", "ToolBar " + Str(GetGadgetState(1)) + "  Button " + Str(EventMenuID()), 0) 
  EndSelect 
Until EventID = #PB_Event_CloseWindow

;- make toolbar
Procedure MakeToolBar(TBNumber.l, StartButton.l, WindowHandle.l, IconSize.l, Style.l)
  ; create toolbar
  CreateTB(TBNumber, WindowHandle, IconSize, IconSize, Style)
  SetTBimage(TBNumber, 0, #TBpro_NORMAL)
  SetTBimage(TBNumber, 0, #TBpro_Hot)
  SetTBimage(TBNumber, 0, #TBpro_Disabled)
  AddTBsysIcons()
  
  ; make room for combobox
  AddTBseparator(85)
  
  ; add buttons
  For b.l = 0 To 4
    AddTBbutton(b+1, StartButton+b, 0)
  Next 
  
  ; keep toolbar hidden
  HideTB(TBNumber, #True)
EndProcedure
  
;- show/hide specified toolbar
Procedure ShowToolBar(ToolBar.l, Show.l)
  If Show
    ; put combobox in specified toolbar
    UseGadgetList(WindowID())
    FreeGadget(1)
    combo.l = ComboBoxGadget(1, 0, 0, 80, 100)
    AddGadgetItem(1, -1, "One")
    AddGadgetItem(1, -1, "Two")
    AddGadgetItem(1, -1, "Three")
    SetGadgetState(1, ToolBar)  
    UseTB(ToolBar)
    AddTBgadget(combo, 2, 2)
    
    ; show toolbar
    HideTB(ToolBar, #False)
  Else
    ; hide toolbar
    HideTB(ToolBar, #True)
  EndIf
EndProcedure
Hope somebody finds this useful!

Eric
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

I don't use the ToolbarPRO lib ...
Therefore here a way with 'Pure' PB...

Code: Select all

If OpenWindow(0, 100, 200, 200, 200, #PB_Window_SystemMenu | #PB_Window_SizeGadget, "Multiple ToolBar example")

  CreateGadgetList(WindowID())

  Panel1 = ContainerGadget(0,10,10,124,24)
  If CreateToolBar(0, Panel1)
    ToolBarStandardButton(0, #PB_ToolBarIcon_New)
    ToolBarStandardButton(1, #PB_ToolBarIcon_Open)
    ToolBarStandardButton(2, #PB_ToolBarIcon_Save)
    ToolBarSeparator()
    ToolBarStandardButton(3, #PB_ToolBarIcon_Print)
    ToolBarStandardButton(4, #PB_ToolBarIcon_Find)
    CloseGadgetList()
  EndIf

  Panel2 = ContainerGadget(1,20,40,124,24)
  If CreateToolBar(1, Panel2)
    ToolBarStandardButton(5, #PB_ToolBarIcon_New)
    ToolBarStandardButton(6, #PB_ToolBarIcon_Open)
    ToolBarStandardButton(7, #PB_ToolBarIcon_Save)
    ToolBarSeparator()
    ToolBarStandardButton(8, #PB_ToolBarIcon_Print)
    ToolBarStandardButton(9, #PB_ToolBarIcon_Find)
    CloseGadgetList()
  EndIf

  Panel3 = ContainerGadget(2,10,80,124,24)
  If CreateToolBar(2, Panel3)
    ToolBarStandardButton(10, #PB_ToolBarIcon_New)
    ToolBarStandardButton(11, #PB_ToolBarIcon_Open)
    ToolBarStandardButton(12, #PB_ToolBarIcon_Save)
    ToolBarSeparator()
    ToolBarStandardButton(13, #PB_ToolBarIcon_Print)
    ToolBarStandardButton(14, #PB_ToolBarIcon_Find)
    CloseGadgetList()
  EndIf

  
  Repeat : Until WaitWindowEvent() = #PB_EventCloseWindow
  
EndIf

End


I am to provide the public with beneficial shocks.
Alfred Hitshock
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

No offense, but that doesn't do what my example does.
My example creates three different toolbars with a combobox in them, and lets you switch the toolbar using the combobox.
Your code just creates three identical toolbars.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

No offense taken :wink:

Didn't try to make your code work without the ToolbarPRO lib.
Just wanted to show that it's possible to have more than one toolbar at the same time.
That's all...
Post Reply