Page 1 of 1

Add any gadget to toolbar (using a container)

Posted: Sat Jun 09, 2007 8:33 pm
by Trond
Based on the code from eddy, this code allows you to add any kind of gadgets to a toolbar easily.

Code: Select all

Procedure ToolbarContainer(Width, Height, ToolbarID)
  Protected Pos.l
  Protected Sep.TBBUTTON
  Protected Rect.RECT
  Pos = SendMessage_(ToolbarID, #TB_BUTTONCOUNT, 0, 0)
  ToolBarSeparator()
  SendMessage_(ToolbarID, #TB_GETBUTTON, Pos, @Sep)
  Sep\iBitmap = Width
  SendMessage_(ToolbarID, #TB_DELETEBUTTON, Pos, 0)
  SendMessage_(ToolbarID, #TB_INSERTBUTTON, Pos, @Sep)
  SendMessage_(ToolbarID, #TB_GETITEMRECT, Pos, @Rect)
  UseGadgetList(ToolbarID)
  ProcedureReturn ContainerGadget(#PB_Any, Rect\left, 0, Width, Height)
EndProcedure


OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))

CreateToolBar(0, WindowID(0))
  ToolBarStandardButton(0, 0)
  ToolBarStandardButton(1, 1)
  ToolBarStandardButton(2, 3)
  ToolbarContainer(200, ToolBarHeight(0), ToolBarID(0))
    StringGadget(0, 0, 1, 150, 20, "Testing")
    ButtonGadget(1, 150, 1, 50, 20, "Go!")
  ToolBarStandardButton(3, 2)

UseGadgetList(WindowID(0))
StringGadget(2, 50, 50, 123, 20, "asdf")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver


Posted: Sun Jun 10, 2007 12:06 am
by Tranquil
Thats cool! Thanks for shareing.

Posted: Sun Jun 10, 2007 1:55 am
by Dare
Nice. Thank you.

Re: Add any gadget to toolbar (using a container)

Posted: Wed Jun 25, 2014 4:02 am
by staringfrog
A very helpful piece of code indeed, and it is still working with 5.2, except for deprecated CreateGadgetList(WindowID(0)), which can be simply omitted now. Any control added before UseGadgetList(WindowID(0)) would belong to the Toolbar, and any control added after it (and before the subsequent CloseGadgetList()) would belong to a parent window or container (in case you've added your ToolBar to a ContainerGadget).

One other minor detail is a bit confusing order of parameters (Width, Height, ToolbarID), as putting ToolbarID in the first place would better comply with PB standards.

Again, a great snippet, thanks a lot!