Add any gadget to toolbar (using a container)

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Add any gadget to toolbar (using a container)

Post 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

Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Thats cool! Thanks for shareing.
Tranquil
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Nice. Thank you.
Dare2 cut down to size
staringfrog
User
User
Posts: 58
Joined: Wed Feb 27, 2013 9:36 am

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

Post 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!
Coding's men's knitwork.
Post Reply