Place toolbars anywhere

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

Place toolbars anywhere

Post by Trond »

This lets you place a toolbar anywhere on a window, which is a simple way to get a bunch of flat image buttons.

Code: Select all

Procedure StopToolbarAutoAlign(Toolbar)
  T = ToolBarID(Toolbar)
  SetWindowLong_(T, #GWL_STYLE, GetWindowLong_(T, #GWL_STYLE) | #CCS_NODIVIDER)
  SetWindowLong_(T, #GWL_STYLE, GetWindowLong_(T, #GWL_STYLE) | #CCS_NOPARENTALIGN)
  SetWindowLong_(T, #GWL_STYLE, GetWindowLong_(T, #GWL_STYLE) | #CCS_NORESIZE)
EndProcedure

Procedure MakeToolbarVertical(Toolbar)
  Protected Count
  Protected hTB = ToolBarID(Toolbar)
  SetWindowLong_(hTB, #GWL_STYLE, GetWindowLong_(hTB, #GWL_STYLE) | #TBSTYLE_WRAPABLE | #CCS_NOPARENTALIGN)
  Count = SendMessage_(hTB, #TB_BUTTONCOUNT, 0, 0)
  SendMessage_(hTB, #TB_SETROWS, Count | 1 << 16, @tbr.RECT)
EndProcedure

OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
CreateToolBar(0, WindowID(0))
ToolBarStandardButton(0, 0)
ToolBarStandardButton(0, 1)
ToolBarStandardButton(0, 2)
ToolBarStandardButton(0, 3)


StopToolbarAutoAlign(0)
T = ToolBarID(0)

MoveWindow_(ToolBarID(0), 0, 100, 512, 22, 1)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Support functions:

Code: Select all

Procedure ToolbarX(Toolbar)
  GetWindowRect_(ToolBarID(Toolbar), @R.RECT)
  W = GetParent_(ToolBarID(Toolbar))
  ScreenToClient_(W, @R)
  ProcedureReturn R\left
EndProcedure

Procedure ToolbarY(Toolbar)
  GetWindowRect_(ToolBarID(Toolbar), @R.RECT)
  W = GetParent_(ToolBarID(Toolbar))
  ScreenToClient_(W, @R)
  ProcedureReturn R\top
EndProcedure

Procedure ToolbarR(Toolbar)
  GetWindowRect_(ToolBarID(Toolbar), @R.RECT)
  W = GetParent_(ToolBarID(Toolbar))
  ScreenToClient_(W, @R+8)
  ProcedureReturn R\Right
EndProcedure

Procedure ToolbarB(Toolbar)
  GetWindowRect_(ToolBarID(Toolbar), @R.RECT)
  W = GetParent_(ToolBarID(Toolbar))
  ScreenToClient_(W, @R+8)
  ProcedureReturn R\Bottom
EndProcedure

Procedure ToolbarButtonSize(Toolbar, *S.SIZE)
  SendMessage_(ToolBarID(Toolbar), #TB_GETITEMRECT, 0, @R.RECT)
  *S\cx = R\right-r\left
  *S\cy = R\bottom-r\top
EndProcedure
Last edited by Trond on Sun Feb 03, 2008 2:12 pm, edited 6 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Nice. I've always used containers to position toolbars, but not any longer! :wink:

Thanks.
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Oups, it gave me a strange redraw problem... I should have tested it a bit more.

Edit: I fixed it. It should work now.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I added functions to get the position of the toolbar and the position of the right/bottom borders, and fixed a small error in the original function.

Also I added an adapted version of Sparkie's vertical toolbar.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Can

Code: Select all

MakeToolbarVertical(0)
be used in conjunction with

Code: Select all

StopToolbarAutoAlign(0)
and/or

Code: Select all

MoveWindow_(ToolBarID(0), 50, 100, 200, 22, 1)
?

I cannot seem to get a combination to work.
Anthony Jordan
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

For the toolbar to stay vertical you need to resize it to the correct dimensions. If you set the width to more than the width of one button it will become horizontal again. And if you set the height to 22 pixels it won't be very vertical either.

Use ToolbarButtonSize() to get the correct width for vertical toolbars.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@Trond
Thank you for your clues. The relevant code I produced is

Code: Select all

MakeToolbarVertical(0)
StopToolbarAutoAlign(0)
width = 40
MoveWindow_(ToolBarID(0), 50, 100, width, 200, 1)
Under Windows ME, the results of changing the width value are interesting:
Width 23 (or slightly less) .. 45 gives a vertical toolbar
Width 46 .. 68 gives a square toolbar
Width 69 .. 91 gives an L-shaped toolbar
Width >=92 gives a horizontal toolbar.
Anthony Jordan
Amnesty
User
User
Posts: 54
Joined: Wed Jul 04, 2007 4:34 pm
Location: Germany

Post by Amnesty »

I m too stupid to get the vertical toolbar work in combination with MDIGadget.

Is it possible ?
Amnesty
User
User
Posts: 54
Joined: Wed Jul 04, 2007 4:34 pm
Location: Germany

Post by Amnesty »

Sorry, its possible, but only without Autosize...

Code: Select all

Procedure StopToolbarAutoAlign(Toolbar) 
  T = ToolBarID(Toolbar) 
  SetWindowLong_(T, #GWL_STYLE, GetWindowLong_(T, #GWL_STYLE) | #CCS_NODIVIDER) 
  SetWindowLong_(T, #GWL_STYLE, GetWindowLong_(T, #GWL_STYLE) | #CCS_NOPARENTALIGN) 
  SetWindowLong_(T, #GWL_STYLE, GetWindowLong_(T, #GWL_STYLE) | #CCS_NORESIZE) 
EndProcedure 

Procedure MakeToolbarVertical(Toolbar) 
  Protected Count 
  Protected hTB = ToolBarID(Toolbar) 
  SetWindowLong_(hTB, #GWL_STYLE, GetWindowLong_(hTB, #GWL_STYLE) | #TBSTYLE_WRAPABLE | #CCS_NOPARENTALIGN) 
  Count = SendMessage_(hTB, #TB_BUTTONCOUNT, 0, 0) 
  SendMessage_(hTB, #TB_SETROWS, Count | 1 << 16, @tbr.RECT) 
EndProcedure 

OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
CreateGadgetList(WindowID(0)) 
CreateToolBar(0, WindowID(0)) 
ToolBarStandardButton(0, 0) 
ToolBarStandardButton(0, 1) 
ToolBarStandardButton(0, 2) 
ToolBarStandardButton(0, 3) 


StopToolbarAutoAlign(0) 
T = ToolBarID(0) 
maketoolbarvertical(0)
MDIGadget(0,25,0,WindowWidth(0) - 25,WindowHeight(0),0,0) ; Don't use #PB_MDI_AutoSize       
Repeat 
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow 
      Break 
  EndSelect 
ForEver 
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I'm not sure where you got the @R+8, Trond but I don't think it's correct.

Code: Select all

Procedure ToolbarX(ToolbarID)
  GetWindowRect_(ToolBarID(ToolbarID),@R.RECT)
  W=GetParent_(ToolBarID(ToolbarID))
  ScreenToClient_(W,@R)
  ProcedureReturn R\left
EndProcedure

Procedure ToolbarY(ToolbarID)
  GetWindowRect_(ToolBarID(ToolbarID),@R.RECT)
  W=GetParent_(ToolBarID(ToolbarID))
  ScreenToClient_(W,@R)
  ProcedureReturn R\top
EndProcedure

Procedure ToolbarW(ToolbarID)
  GetWindowRect_(ToolBarID(ToolbarID),@R.RECT)
  W=GetParent_(ToolBarID(ToolbarID))
  ProcedureReturn R\Right-R\Left
EndProcedure

Procedure ToolBarH(ToolbarID)
  GetWindowRect_(ToolBarID(ToolbarID),@R.RECT)
  W=GetParent_(ToolBarID(ToolbarID))
  ProcedureReturn R\Bottom-R\Top
EndProcedure

Procedure ToolbarButtonSize(ToolbarID, *S.SIZE)
  SendMessage_(ToolBarID(ToolbarID),#TB_GETITEMRECT,0,@R.RECT)
  *S\cx=R\right-r\left
  *S\cy=R\bottom-r\top
EndProcedure

Procedure StopToolbarAutoAlign(ToolbarID)
  T=ToolBarID(ToolbarID)
  SetWindowLong_(T,#GWL_STYLE,GetWindowLong_(T,#GWL_STYLE)|#CCS_NODIVIDER)
  SetWindowLong_(T,#GWL_STYLE,GetWindowLong_(T,#GWL_STYLE)|#CCS_NOPARENTALIGN)
  SetWindowLong_(T,#GWL_STYLE,GetWindowLong_(T,#GWL_STYLE)|#CCS_NORESIZE)
EndProcedure

Procedure MakeToolbarVertical(ToolbarID)
  Protected Count
  Protected hTB=ToolBarID(ToolbarID)
  SetWindowLong_(hTB,#GWL_STYLE,GetWindowLong_(hTB,#GWL_STYLE)|#TBSTYLE_WRAPABLE|#CCS_NOPARENTALIGN)
  Count=SendMessage_(hTB,#TB_BUTTONCOUNT,0,0)
  SendMessage_(hTB,#TB_SETROWS,Count|1<<16,@tbr.RECT)
EndProcedure
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Mistrel wrote:I'm not sure where you got the @R+8, Trond but I don't think it's correct.
It is surely correct. ToolbarR() returns the position of the right edge of the toolbar inside the parent. ToolbarB() returns the position of the bottom edge inside the parent. They do not return the width/height.
Post Reply