Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadgets

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadgets

Post by eddy »

:arrow: Features :
- ADDED : option to auto center all gadgets
- ADDED : toolbar container to include PB common gadgets (button, text, spin,etc .... )
- ADDED : toolbar grouped toggle buttons
- ADDED : toolbar drop-down style button
- ADDED : toolbar popup menu
- ADDED : toolbar custom popup
  • new event type #PB_EventType_ToolbarPopup associated to event #PB_Event_Menu
  • ToolbarPopupX() and ToolbarPopupY() to determine popup position
Image

Code: Select all

;:=============================================================================
;:- ToolbarPlus.pbi
;:- Author          : Eddy
;:- Date            : August 31, 2013
;:- Compiler        : PureBasic 5.20 beta 15
;:- Target OS       : Windows
;:- Source --------------------------------------------------------------------
;:- http://www.purebasic.fr/english/viewtopic.php?f=40&t=56155
;:=============================================================================

EnableExplicit

Global CurrentGadgetListToolbar=#PB_Ignore
Enumeration 1
   #PB_EventType_ToolbarPopup
EndEnumeration

Procedure ToolBarCustomCallback(hWnd, message, wParam, lParam) ;ToolbarPlus callback procedure.
   ;detect click on toolbar buttons
   Protected *ToolbarID
   Protected buttonID
   Select message
      Case #WM_COMMAND
         *ToolbarID=lParam
         buttonID=wParam ;(wParam -> high word = 0)
         
      Case #WM_NOTIFY
         Protected *NMTOOLBAR.NMTOOLBAR=lParam
         With *NMTOOLBAR
            If \hdr\code=#TBN_DROPDOWN
               *ToolbarID=\hdr\hwndFrom
               buttonID=\iItem
               Protected dropdown=1
            EndIf
         EndWith
   EndSelect
   
   ;check if toolbar is customized
   If *ToolbarID And GetProp_(*ToolbarID, "PurebasicCustomToolbar")
      
      ;get button info
      Protected buttonRect.RECT
      Protected buttonInfo.TBBUTTONINFO
      buttonInfo\cbSize=SizeOf(TBBUTTONINFO)
      buttonInfo\dwMask=#TBIF_COMMAND|#TBIF_STATE|#TBIF_STYLE|#TBIF_IMAGE|#TBIF_SIZE|#TBIF_LPARAM;|#TBIF_TEXT
      Protected buttonIndex=SendMessage_(*ToolBarID, #TB_GETBUTTONINFO, buttonID, @buttonInfo)
      
      ;get parameters for displaying popup
      SendMessage_(*ToolBarID, #TB_GETRECT, buttonID, @buttonRect.RECT)
      MapWindowPoints_(*ToolBarID, 0, @buttonRect, 2)
      Protected popupPos=buttonRect\left +(buttonRect\bottom << 16)
      Protected popupMenu=buttonInfo\lParam
      Protected popupParentWindow=GetProp_(*ToolBarID, "PurebasicCustomToolbarWindow")
      
      ;check if toolbar button can display popup
      If dropdown Or (buttonInfo\fsStyle & #TBSTYLE_DROPDOWN)=0 Or (buttonInfo\fsStyle & #BTNS_WHOLEDROPDOWN)
         If popupMenu=#PB_Any
            ;post custom event for displaying popup
            PostEvent(#PB_Event_Menu, popupParentWindow, buttonID, #PB_EventType_ToolbarPopup, popupPos)
            ProcedureReturn 0
         ElseIf IsMenu(popupMenu)
            ;display popup menu
            DisplayPopupMenu(popupMenu, WindowID(popupParentWindow), buttonRect\left, buttonRect\bottom)
            ProcedureReturn 0
         EndIf
      EndIf
   EndIf
   
   ProcedureReturn CallWindowProc_(GetProp_(hWnd, "PurebasicCustomToolbarParentCB"), hWnd, message, wParam, lParam)
EndProcedure

Procedure ToolbarCenterGadget(Gadget, Toolbar=#PB_Any) ;Center gadget vertically inside ToolbarContainer
   If Toolbar=#PB_Any : Toolbar=CurrentGadgetListToolbar : EndIf
   ResizeGadget(Gadget, #PB_Ignore, (ToolBarHeight(Toolbar)-GadgetHeight(Gadget)-2)/2, #PB_Ignore, #PB_Ignore)
EndProcedure

Procedure ToolbarFindGadget(*childControl, Flags) ;Examine child controls to find gadget
   Protected gadget=GetDlgCtrlID_(*childControl)
   If IsGadget(gadget) And GadgetType(gadget)<>#PB_GadgetType_Container
      If Flags
         ToolbarCenterGadget(gadget)
      EndIf
   EndIf
   
   ProcedureReturn #True
EndProcedure

Procedure.i ToolbarPopupX() ;Determines popup position X
   Protected popupPos=EventData()
   ProcedureReturn PeekW(@popupPos)
EndProcedure

Procedure.i ToolbarPopupY() ;Determines popup position Y
   Protected popupPos=EventData()>> 16
   ProcedureReturn PeekW(@popupPos)
EndProcedure

Procedure.i OpenToolbarGadgetList(Toolbar, useLargeIcons=#False, AutoCenterGadgets=#True) ;Open toolbar gadget list
   If IsToolBar(Toolbar)
      Protected *ToolBarID=ToolBarID(Toolbar)
      
      ;use bigger standard icon
      If useLargeIcons
         Protected *standardIcons = ImageList_Create_(24, 24, #ILC_COLOR32 | #ILC_MASK, 14, 30)
         SendMessage_(*ToolBarID, #TB_SETIMAGELIST, 0, *standardIcons)
         SendMessage_(*ToolBarID, #TB_LOADIMAGES, #IDB_STD_LARGE_COLOR, #HINST_COMMCTRL)
         SendMessage_(*ToolBarID, #TB_LOADIMAGES, #IDB_STD_LARGE_COLOR, #HINST_COMMCTRL)
         #ILCF_MOVE=0
         #ILCF_SWAP=1
         *standardIcons=SendMessage_(*ToolBarID, #TB_GETIMAGELIST, 0, 0)
         Protected i
         For i=30 To 0 Step -1
            If i>15
               ImageList_Remove_(*standardIcons, i)
            Else
               ImageList_Copy_(*standardIcons, i, *standardIcons, i+1, #ILCF_SWAP)
            EndIf 
         Next         
         SendMessage_(*ToolBarID, #TB_SETIMAGELIST, 0, *standardIcons)
         SendMessage_(*ToolBarID, #TB_AUTOSIZE, 0, 0)
      EndIf
      
      ;change toolbar style to allow dropdown arrow buttons
      Protected newToolBarStyle=SendMessage_(*ToolBarID, #TB_GETEXTENDEDSTYLE, 0, 0)| #TBSTYLE_EX_DRAWDDARROWS
      SendMessage_(*ToolBarID, #TB_SETEXTENDEDSTYLE, 0, newToolBarStyle)
      SetProp_(*ToolBarID, "PurebasicCustomToolbar", #True)
      
      ;allow to center all gadgets
      SetProp_(*ToolBarID, "AutoCenterGadgets", AutoCenterGadgets)
      
      ;allow popup menu
      Protected popupParentWindow=GetProp_(GetAncestor_(*ToolBarID, #GA_ROOT), "PB_WindowID")-1
      SetProp_(*ToolBarID, "PurebasicCustomToolbarWindow", popupParentWindow)
      Protected *toolbarParent=GetAncestor_(*ToolBarID, #GA_PARENT)
      If GetProp_(*toolbarParent, "PurebasicCustomToolbarParentCB")=0
         SetProp_(*toolbarParent, "PurebasicCustomToolbarParentCB", SetWindowLong_(*toolbarParent, #GWL_WNDPROC, @ToolBarCustomCallback()))
      EndIf
      
      ;save previous gadget list and current toolbar
      If SetProp_(*ToolBarID, "PreviousGadgetList", UseGadgetList(*ToolBarID))
         CurrentGadgetListToolbar=Toolbar
         ProcedureReturn Bool(UseGadgetList(0))
      EndIf
   EndIf
EndProcedure

Procedure CloseToolbarGadgetList() ;Close toolbar gadget list
   Protected Toolbar=CurrentGadgetListToolbar
   Protected *ToolBarID=ToolBarID(Toolbar)
   
   ;close last container gadget
   If GetProp_(*ToolBarID, "LastOpenContainer")
      CloseGadgetList()
   EndIf
   
   ;auto center gadgets
   If GetProp_(*ToolBarID, "AutoCenterGadgets")
      EnumChildWindows_(*ToolBarID, @ToolbarFindGadget(), #True)
   EndIf
   
   ;restore previous gadget list
   UseGadgetList(GetProp_(*ToolBarID, "PreviousGadgetList"))
   
   ;clean all properties
   RemoveProp_(*ToolBarID, "PreviousGadgetList")
   RemoveProp_(*ToolBarID, "LastOpenContainer")
   RemoveProp_(*ToolBarID, "AutoCenterGadgets")
   
   ;reset current toolbar
   CurrentGadgetListToolbar=#PB_Ignore
EndProcedure

Procedure.i AddToolbarContainer(Width, Flag=#PB_Container_BorderLess) ;Add toolbar container
   Protected Toolbar=CurrentGadgetListToolbar
   Protected *ToolBarID=ToolBarID(Toolbar)
   Protected buttonIndex=SendMessage_(*ToolBarID, #TB_BUTTONCOUNT, 0, 0)
   ToolBarSeparator()
   
   ;set separator width
   Protected separator.TBBUTTON
   SendMessage_(*ToolBarID, #TB_GETBUTTON, buttonIndex, @separator)
   separator\iBitmap=width
   SendMessage_(*ToolBarID, #TB_DELETEBUTTON, buttonIndex, 0)
   SendMessage_(*ToolBarID, #TB_INSERTBUTTON, buttonIndex, separator)
   
   ;get separator position
   Protected rc.RECT
   SendMessage_(*ToolBarID, #TB_GETITEMRECT, buttonIndex, @rc.RECT)
   Protected x=rc\left
   Protected y=0
   Protected h=ToolBarHeight(Toolbar)
   Protected w=width
   
   ;close last container gadget inside toolbar
   If GetProp_(*ToolBarID, "LastOpenContainer")
      CloseGadgetList()
   EndIf
   
   ;create new container gadget inside toolbar
   SetProp_(*ToolBarID, "LastOpenContainer", #True)
   ProcedureReturn ContainerGadget(#PB_Any, x, y, w, h, Flag)
EndProcedure

Procedure SetToolBarArrow(SimpleArrow=#False) ;Creates a drop-down style button
   Protected Toolbar=CurrentGadgetListToolbar
   Protected *ToolBarID=ToolBarID(Toolbar)
   
   ;get button info
   Protected buttonIndex=SendMessage_(*ToolBarID, #TB_BUTTONCOUNT, 0, 0)-1
   Protected button.TBBUTTON
   SendMessage_(*ToolBarID, #TB_GETBUTTON, buttonIndex, @button)
   If (button\fsStyle & #TBSTYLE_SEP) : ProcedureReturn 0 : EndIf
   
   ;change button style
   If SimpleArrow
      button\fsStyle=button\fsStyle | #TBSTYLE_DROPDOWN | #BTNS_WHOLEDROPDOWN
   Else
      button\fsStyle=button\fsStyle | #TBSTYLE_DROPDOWN
   EndIf
   SendMessage_(*ToolBarID, #TB_DELETEBUTTON, buttonIndex, 0)
   SendMessage_(*ToolBarID, #TB_INSERTBUTTON, buttonIndex+1, button)
   
EndProcedure

Procedure SetToolBarToggle(Grouped=#False) ;Creates toggle button (buttons can be grouped)
   Protected Toolbar=CurrentGadgetListToolbar
   Protected *ToolBarID=ToolBarID(Toolbar)
   
   ;get button info
   Protected buttonIndex=SendMessage_(*ToolBarID, #TB_BUTTONCOUNT, 0, 0)-1
   Protected button.TBBUTTON
   SendMessage_(*ToolBarID, #TB_GETBUTTON, buttonIndex, @button)
   If (button\fsStyle & #TBSTYLE_SEP) : ProcedureReturn 0 : EndIf
   
   ;change button
   If Grouped
      button\fsStyle=button\fsStyle | #TBSTYLE_CHECKGROUP
   Else
      button\fsStyle=button\fsStyle | #TBSTYLE_CHECK
   EndIf
   SendMessage_(*ToolBarID, #TB_DELETEBUTTON, buttonIndex, 0)
   SendMessage_(*ToolBarID, #TB_INSERTBUTTON, buttonIndex+1, button)
EndProcedure

Procedure SetToolBarPopup(PopupMenu=#PB_Any) ;Specifies that the button will display a popup menu or custom popup.
   Protected Toolbar=CurrentGadgetListToolbar
   Protected *ToolBarID=ToolBarID(Toolbar)
   
   ;get button info
   Protected buttonIndex=SendMessage_(*ToolBarID, #TB_BUTTONCOUNT, 0, 0)-1
   Protected button.TBBUTTON
   SendMessage_(*ToolBarID, #TB_GETBUTTON, buttonIndex, @button)
   If (button\fsStyle & #TBSTYLE_SEP) : ProcedureReturn 0 : EndIf
   
   ;change button
   button\dwData=PopupMenu
   SendMessage_(*ToolBarID, #TB_DELETEBUTTON, buttonIndex, 0)
   SendMessage_(*ToolBarID, #TB_INSERTBUTTON, buttonIndex+1, button)
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
   DisableExplicit
   
   ; ******************************************
   ; EXAMPLE 1 : CUSTOM TOOLBAR GADGETS 
   ; ******************************************
   
   win=OpenWindow(#PB_Any, 0, 0, 700, 200, "ToolBar", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
   
   ;{/// CREATE TOOLBAR TOP
   toolbar=CreateToolBar(#PB_Any, WindowID(win))
   If OpenToolbarGadgetList(toolbar)
      If AddToolbarContainer(100)
         txt=TextGadget(#PB_Any, 0, 0, 60, 14, "ToolbarPlus")
      EndIf
      ToolBarStandardButton(10, #PB_ToolBarIcon_New, #PB_ToolBar_Toggle)
      SetToolBarButtonState(toolbar, 10, 1)
      ToolBarStandardButton(11, #PB_ToolBarIcon_Save)
      SetToolBarPopup() ; <----- allow custom popup
      ToolBarSeparator()
      If AddToolbarContainer(40)
         combo=ComboBoxGadget(#PB_Any, 0, 0, 40, #PB_Ignore)
      EndIf
      ToolBarSeparator()
      If AddToolbarContainer(40)
         button=ButtonGadget(#PB_Any, 0, 0, 40, ToolBarHeight(toolbar)-4, "OK", #PB_Button_Toggle)
      EndIf
      ToolBarSeparator()
      If AddToolbarContainer(125)
         string=StringGadget(#PB_Any, 0, 0, 50, 20, "String", #PB_String_UpperCase)
         check=CheckBoxGadget(#PB_Any, 55, 0, 70, 20, "CheckMe")
      EndIf
      ToolBarSeparator()
      If AddToolbarContainer(90)
         opt1=OptionGadget(#PB_Any, 0, 0, 30, 20, "1")
         opt2=OptionGadget(#PB_Any, 30, 0, 30, 20, "2")
         opt3=OptionGadget(#PB_Any, 60, 0, 30, 20, "3")
      EndIf
      ToolBarSeparator()
      If AddToolbarContainer(65)
         bar1=ProgressBarGadget(#PB_Any, 0, 0, 20, 20, 0, 100, #PB_ProgressBar_Vertical)
         bar2=ProgressBarGadget(#PB_Any, 25, 0, 40, 20, 0, 100)
      EndIf
      ToolBarSeparator()
      If AddToolbarContainer(150)
         link=HyperLinkGadget(#PB_Any, 0, 0, 150, 14, "http://www.purebasic.com", $0000FF, #PB_HyperLink_Underline)
      EndIf
      CloseToolbarGadgetList()
   EndIf
   
   SetGadgetColor(txt, #PB_Gadget_BackColor, RGB(255, 0, 0))
   SetGadgetColor(txt, #PB_Gadget_FrontColor, RGB(255, 255, 255))
   
   SetGadgetState(check, 1)
   SetGadgetState(bar1, 20)
   SetGadgetState(bar2, 60)
   
   AddGadgetItem(combo, -1, "A")
   AddGadgetItem(combo, -1, "B")
   
   ToolBarToolTip(toolbar, 10, "New document")
   GadgetToolTip(bar1, "veritcal progressbar")
   ;}
   
   ;{/// CREATE TOOLBAR BOTTOM
   bottomContainer=ContainerGadget(#PB_Any, 0, ToolBarHeight(toolbar), WindowWidth(win), 32, #PB_Container_Double)
   If bottomContainer
      
      If CreateImage(0, 32, 16)
         StartDrawing(ImageOutput(0))
         Box(0, 0, 32, 16, RGB(10, 152, 100))
         Box(8, 4, 16, 8, RGB(255, 0, 0))
         StopDrawing()
      EndIf
      
      If CreatePopupMenu(100)
         MenuItem(10, "MenuID 10")
         MenuItem(20, "MenuID 20")
      EndIf
      
      bottomToolbar=CreateToolBar(#PB_Any, GadgetID(bottomContainer))
      If OpenToolbarGadgetList(bottomToolbar)
         If AddToolbarContainer(110)
            spin=SpinGadget(#PB_Any, 5, 0, 40, 20, 0, 100, #PB_Spin_ReadOnly)
            track=TrackBarGadget(#PB_Any, 50, 0, 60, 20, 0, 10, #PB_TrackBar_Ticks)
         EndIf
         ToolBarSeparator()
         If AddToolbarContainer(133)
            scroll=ScrollBarGadget(#PB_Any, 0, 0, 64, 20, 0, 100, 10)
            btImage=ButtonImageGadget(#PB_Any, 69, 0, 64, ToolBarHeight(bottomToolbar)-2, ImageID(0))
         EndIf
         ToolBarSeparator()
         ToolBarImageButton(20, ImageID(0))
         
         ToolBarStandardButton(15, #PB_ToolBarIcon_Find)
         SetToolBarArrow()
         SetToolBarPopup(100)
         ToolBarStandardButton(16, #PB_ToolBarIcon_Print)
         SetToolBarArrow(#True)
         SetToolBarPopup(100)
         ToolBarStandardButton(17, #PB_ToolBarIcon_Paste)
         SetToolBarPopup(100)
         ToolBarSeparator()
         
         ToolBarStandardButton(20, #PB_ToolBarIcon_Help)
         SetToolBarToggle(#True)
         ToolBarStandardButton(21, #PB_ToolBarIcon_Help)
         SetToolBarToggle(#True)
         ToolBarStandardButton(22, #PB_ToolBarIcon_Help)
         SetToolBarToggle(#True)
         If AddToolbarContainer(225)
            ip=IPAddressGadget(#PB_Any, 0, 0, 120, 20)
            date=DateGadget(#PB_Any, 125, 0, 100, 20)
         EndIf
         ToolBarSeparator()
         CloseToolbarGadgetList()
      EndIf
      
      SetGadgetColor(spin, #PB_Gadget_BackColor, RGB(255, 225, 10))
      SetGadgetFont(spin, LoadFont(1, "Arial", 12, #PB_Font_Bold))
      SetGadgetText(spin, "1")
      
      CloseGadgetList()
   EndIf
   ;}
   
   Repeat
      e=WaitWindowEvent()
      w=EventWindow()
      g=EventGadget()
      m=EventMenu()
      t=EventType()
      d=EventData()
      
      Select e
         Case #PB_Event_ActivateWindow
            ;set random ip address
            SetGadgetState(ip, MakeIPAddress(127, 0, 0, Random(255)))
         Case #PB_Event_Menu
            Debug "eventMenu="+Str(m)+" eventWindow="+Str(w)+" eventType="+Str(t)+" eventData="+Str(d)
            
            ;toolbar custom eventType to display custom popup
            If m=11 And t=#PB_EventType_ToolbarPopup
               DisplayPopupMenu(100, WindowID(w), ToolbarPopupX(), ToolbarPopupY())
            EndIf
            
         Case #PB_Event_Gadget
            If IsGadget(g)
               Debug "eventGadget="+Str(g)+" eventType="+Str(t)+" eventData="+Str(d)+" state="+Str(GetGadgetState(g))
            EndIf
            
            ;update spin
            SetGadgetText(spin, Str(GetGadgetState(spin)))
      EndSelect
   Until e=#PB_Event_CloseWindow
   End
CompilerEndIf
Last edited by eddy on Sat Aug 31, 2013 12:31 am, edited 15 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by kvitaliy »

PB5.20b11, WinXPx86
Elements of the second line (ToolBarStandardButton)
Line 171: Invalid memory access
User_Russian
Addict
Addict
Posts: 1550
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by User_Russian »

Confirm, have problem.
You need to remove the constant #TBIF_TEXT from line 170.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by eddy »

User_Russian wrote:Confirm, have problem.
You need to remove the constant #TBIF_TEXT from line 170.
Fixed

Code: Select all

XIncludeFile "ToolbarPlus.pbi" ; <--- specify your library path here

CompilerIf #PB_Compiler_IsMainFile
   DisableExplicit
   
   ; ******************************************
   ; EXAMPLE 2 : TOOLBAR WITH CUSTOM POPUP BUTTONS
   ; ******************************************
   
   win=OpenWindow(#PB_Any, 0, 0, 700, 200, "ToolBar", #PB_Window_SystemMenu)
   If win
      toolbar=CreateToolBar(#PB_Any, WindowID(win))
      If IsToolBar(toolbar) And OpenToolbarGadgetList(toolbar)
         ToolBarStandardButton(100, #PB_ToolBarIcon_Save)
         SetToolBarPopup() ; <----- allow custom popup
         ToolBarSeparator()
         ToolBarStandardButton(110, #PB_ToolBarIcon_Print)
         SetToolBarPopup() ; <----- allow custom popup
         
         CloseToolbarGadgetList()
      EndIf
      
      popupmenu=CreatePopupMenu(#PB_Any)
      If IsMenu(popupmenu)
         MenuItem(10, "&Save All...")
         MenuItem(20, "&Save As...")
         MenuBar()
         MenuItem(30, "Ex&it")
      EndIf
   EndIf
   
   popupwindow=OpenWindow(#PB_Any, 0, 0, 300, 200, "Form", #PB_Window_Invisible|#PB_Window_SystemMenu|#PB_Window_Tool, WindowID(win))
   If popupwindow
      StringGadget(#PB_Any, 5, 5, 290, 150, "Type here...")
   EndIf
   
   Repeat
      e=WaitWindowEvent()
      w=EventWindow()
      g=EventGadget()
      m=EventMenu()
      t=EventType()
      d=EventData()
      
      If w=popupwindow
         Select e
            Case #PB_Event_DeactivateWindow, #PB_Event_CloseWindow
               HideWindow(popupwindow, 1)
         EndSelect 
      EndIf 
      If w=win
         Select e
            Case #PB_Event_CloseWindow
               End               
            Case #PB_Event_Menu
               If m=100 And t=#PB_EventType_ToolbarPopup
                  DisplayPopupMenu(popupmenu, WindowID(w), ToolbarPopupX(), ToolbarPopupY())
               EndIf
               If m=110 And t=#PB_EventType_ToolbarPopup
                  HideWindow(popupwindow, 0)
                  ResizeWindow(popupwindow, ToolbarPopupX(), ToolbarPopupY(), #PB_Ignore, #PB_Ignore)
               EndIf
         EndSelect
      EndIf 
   ForEver
CompilerEndIf
Last edited by eddy on Sat Aug 31, 2013 12:23 am, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by eddy »

Code: Select all

XIncludeFile "ToolbarPlus.pbi" ; <--- specify your library path here

CompilerIf #PB_Compiler_IsMainFile
   DisableExplicit
   
   ; ******************************************
   ; EXAMPLE 3 : LARGER TOOLBAR ICONS 
   ; ******************************************
   
   win=OpenWindow(#PB_Any, 0, 0, 850, 100, "ToolBar", #PB_Window_SystemMenu | #PB_Window_SizeGadget|#PB_Window_Tool|#PB_Window_ScreenCentered)
   
   toolbar=CreateToolBar(#PB_Any, WindowID(win))
   If OpenToolbarGadgetList(toolbar, #True)

      If CreateImage(0, 24, 24)
         StartDrawing(ImageOutput(0))
         Box(0, 0, 32, 24, RGB(10, 152, 100))
         Box(8, 4, 14, 14, RGB(255, 0, 0))
         StopDrawing()
      EndIf
      ToolBarImageButton(111, ImageID(0))

      m=20
      For icon=#PB_ToolBarIcon_Cut To #PB_ToolBarIcon_Print
         ToolBarStandardButton(m, icon) : m+1
      Next 
      
      ToolBarSeparator()
      
      LoadFont(0, "Tahoma", 8, #PB_Font_Bold)
      If AddToolbarContainer(70)
         combo=ComboBoxGadget(#PB_Any, 0, 0, 70, #PB_Ignore)
         AddGadgetItem(combo, -1, "Debug")
         AddGadgetItem(combo, -1, "Release")
         SetGadgetState(combo, 0)
         SetGadgetFont(combo, FontID(0))
      EndIf
      ToolBarSeparator()
      toolbarContainer=AddToolbarContainer(200, #PB_Container_Single)
      If toolbarContainer
         GadgetToolTip(toolbarContainer, "Parsing your text")
         progress=ProgressBarGadget(#PB_Any, 5, 0, 105, ToolBarHeight(toolbar)-12, 0, 100)
         SetGadgetState(progress, 90)
         runButton=ButtonGadget(#PB_Any, 115, 0, 80, ToolBarHeight(toolbar)-5, "Parse", #PB_Button_Toggle)
         SetGadgetFont(runButton, FontID(0))
      EndIf
      
      
      CloseToolbarGadgetList()
   EndIf
   
   container=ContainerGadget(#PB_Any, 0, ToolBarHeight(toolbar), WindowWidth(win), WindowHeight(win)-ToolBarHeight(toolbar), #PB_Container_Double)
  
   Repeat
      e=WaitWindowEvent()
      w=EventWindow()
      g=EventGadget()
      m=EventMenu()
      t=EventType()
      d=EventData()
      
      Select e
         Case #PB_Event_Menu
            Debug "eventMenu="+Str(m)+" eventWindow="+Str(w)+" eventType="+Str(t)+" eventData="+Str(d)
            
         Case #PB_Event_Gadget
            If IsGadget(g)
               Debug "eventGadget="+Str(g)+" eventType="+Str(t)+" eventData="+Str(d)+" state="+Str(GetGadgetState(g))
            EndIf

      EndSelect
   Until e=#PB_Event_CloseWindow
   End
CompilerEndIf
Last edited by eddy on Sat Aug 31, 2013 12:45 am, edited 7 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by eddy »

Updated
- ADDED : option to auto center all gadgets
- UPDATED : you don't need to use "setWindowCallback" anymore
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by morosh »

in the old version, it was possible to give the height of the toolbar (Size)

Code: Select all

ProcedureDLL CreateToolBarPlus(ID, Parent, Size=20) ;Create toolbar plus
isn't possible now?

thanks
PureBasic: Surprisingly simple, diabolically powerful
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by eddy »

I didn't found any "icon size" parameter in old versions of this function.
It's probably a mod of this library.

I'll try to add this parameter in the next version.
viewtopic.php?f=13&t=46609
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by morosh »

I'm referring to this file

Code: Select all

;/// ToolbarPlus for PB 4.XX (open source)

;{ Utils
Macro ValidateGadgetID(Result, ID, gadget)
   If ID=#PB_Any
      ID=Result
      gadget=GadgetID(ID)
   Else
      gadget=Result
   EndIf
EndMacro
;}

;{ ToolbarPlus Functions
Structure ToolbarPlusMenu
   ToolbarPlusParent.l
   ToolbarPlus.l
   ToolbarPlusID.l
   ButtonID.l
   ButtonArrow.l
   MenuID.l
EndStructure
Global NewList ToolbarPlusMenu.ToolbarPlusMenu()

CompilerIf Defined(TBN_DROPDOWN, #PB_Constant)=#False
   #TBN_DROPDOWN=-710
   #TBSTYLE_DROPDOWN=$8
   #BTNS_WHOLEDROPDOWN=$80
CompilerEndIf

ProcedureDLL ToolbarContainerHeight() ;Get default height for toolbar gadget
   Shared ToolbarPlus
   If ToolbarPlus
      GetClientRect_(ToolbarPlus, sz.RECT)
      ProcedureReturn sz\bottom-sz\top
   EndIf
EndProcedure
ProcedureDLL ToolbarCenterGadget(ID) ;Center gadget vertically inside ToolbarContainer
   ResizeGadget(ID, #PB_Ignore, GadgetY(ID)+(ToolbarContainerHeight()-GadgetHeight(ID))/2, #PB_Ignore, #PB_Ignore)
EndProcedure
ProcedureDLL SetToolBarArrow(SimpleArrow.b=#False) ;Creates a drop-down style button
   Shared ToolbarPlusParent
   Shared ToolbarPlus
   Shared ToolbarPlusID
   
   ;get button
   Protected pos.l=SendMessage_(ToolbarPlus, #TB_BUTTONCOUNT, 0, 0)-1
   Protected button.TBBUTTON
   SendMessage_(ToolbarPlus, #TB_GETBUTTON, pos, @button)
   If (button\fsStyle & #TBSTYLE_SEP) : ProcedureReturn 0 : EndIf
   
   ;change button style
   If SimpleArrow
      button\fsStyle=button\fsStyle | #TBSTYLE_DROPDOWN | #BTNS_WHOLEDROPDOWN
   Else
      button\fsStyle=button\fsStyle | #TBSTYLE_DROPDOWN
   EndIf
   SendMessage_(ToolbarPlus, #TB_DELETEBUTTON, pos, 0)
   SendMessage_(ToolbarPlus, #TB_INSERTBUTTON, pos+1, button)
   
EndProcedure
ProcedureDLL SetToolBarMenu(MenuID) ;Specifies that the button will display a popup menu.
   Shared ToolbarPlusParent
   Shared ToolbarPlus
   Shared ToolbarPlusID
   
   ;get button
   Protected pos.l=SendMessage_(ToolbarPlus, #TB_BUTTONCOUNT, 0, 0)-1
   Protected button.TBBUTTON
   SendMessage_(ToolbarPlus, #TB_GETBUTTON, pos, @button)
   If (button\fsStyle & #TBSTYLE_SEP) : ProcedureReturn 0 : EndIf
   
   ;get dropdown-arrow mode
   Protected ButtonArrow=0
   If (button\fsStyle & #TBSTYLE_DROPDOWN) : ButtonArrow=1 : EndIf
   
   AddElement(ToolbarPlusMenu())
   ToolbarPlusMenu()\ToolbarPlusParent=ToolbarPlusParent
   ToolbarPlusMenu()\ToolbarPlus=ToolbarPlus
   ToolbarPlusMenu()\ToolbarPlusID=ToolbarPlusID
   ToolbarPlusMenu()\ButtonArrow=ButtonArrow
   ToolbarPlusMenu()\ButtonID=button\idCommand
   ToolbarPlusMenu()\MenuID=MenuID
EndProcedure
ProcedureDLL SetToolBarToggle(Grouped)
   Shared ToolbarPlusParent
   Shared ToolbarPlus
   Shared ToolbarPlusID
   
   ;get button info
   Protected pos.l=SendMessage_(ToolbarPlus, #TB_BUTTONCOUNT, 0, 0)-1
   Protected button.TBBUTTON
   SendMessage_(ToolbarPlus, #TB_GETBUTTON, pos, @button)
   If (button\fsStyle & #TBSTYLE_SEP) : ProcedureReturn 0 : EndIf
   
   ;change button
   If Grouped
      button\fsStyle=button\fsStyle | #TBSTYLE_CHECKGROUP
   Else
      button\fsStyle=button\fsStyle | #TBSTYLE_CHECK
   EndIf
   SendMessage_(ToolbarPlus, #TB_DELETEBUTTON, pos, 0)
   SendMessage_(ToolbarPlus, #TB_INSERTBUTTON, pos+1, button)
   
EndProcedure
ProcedureDLL ToolBarPlusCB(Window.l, message.l, wParam.l, lParam.l, Result.l) ;ToolbarPlus callback procedure.
   Shared ToolbarPlus_Toolbar.l
   Shared ToolbarPlus_ToolbarParent.l
   Shared ToolbarPlus_ButtonID.l
   Shared ToolbarPlus_PressedArrow.b
   Shared ToolbarPlus_PopupX.l
   Shared ToolbarPlus_PopupY.l
   ToolbarPlus_Toolbar=0
   ToolbarPlus_ButtonID=0
   ToolbarPlus_PressedArrow=0
   ToolbarPlus_PopupX=0
   ToolbarPlus_PopupY=0
   
   If Result<>#PB_ProcessPureBasicEvents : ProcedureReturn Result : EndIf
   
   ;detect toolbar events
   Select message
      Case #WM_COMMAND
         ToolbarPlus_Toolbar=lParam
         ToolbarPlus_ButtonID=wParam ;(wParam -> high word = 0)
         ToolbarPlus_PressedArrow=0
         
      Case #WM_NOTIFY
         *msgTB.NMTOOLBAR=lParam
         If *msgTB\hdr\code=#TBN_DROPDOWN
            ToolbarPlus_Toolbar=*msgTB\hdr\hwndFrom
            ToolbarPlus_ButtonID=*msgTB\iItem
            ToolbarPlus_PressedArrow=1
         EndIf
   EndSelect
   ;validate found results
   If ToolbarPlus_Toolbar
      Protected classname.s=Space(1000)
      Protected lg=GetClassName_(ToolbarPlus_Toolbar, @classname, Len(classname))
      classname=LCase(Left(classname, lg))
     
      If classname="toolbarwindow32"
         SendMessage_(ToolbarPlus_Toolbar, #TB_GETRECT, ToolbarPlus_ButtonID, @rc.RECT)
         MapWindowPoints_(ToolbarPlus_Toolbar, 0, @rc, 2)
         ToolbarPlus_PopupX=rc\left
         ToolbarPlus_PopupY=rc\bottom
         ToolbarPlus_ToolbarParent=GetParent_(ToolbarPlus_Toolbar)
      Else
         ToolbarPlus_Toolbar=0
         ToolbarPlus_ButtonID=0
         ToolbarPlus_PressedArrow=0
         ToolbarPlus_PopupX=0
         ToolbarPlus_PopupY=0
      EndIf
   EndIf
   
   ; ===================
   ; toolbar popup menu
   ; ===================
   If ToolbarPlus_Toolbar
      ForEach ToolbarPlusMenu()
         Protected ButtonID=ToolbarPlusMenu()\ButtonID
         Protected ButtonArrow=ToolbarPlusMenu()\ButtonArrow
         Protected ToolbarPlusParent=ToolbarPlusMenu()\ToolbarPlusParent
         Protected ToolbarPlus=ToolbarPlusMenu()\ToolbarPlus
         Protected MenuID=ToolbarPlusMenu()\MenuID
         
         If ToolbarPlus=ToolbarPlus_Toolbar And ButtonID=ToolbarPlus_ButtonID
            If IsMenu(MenuID) And ButtonArrow=ToolbarPlus_PressedArrow
               DisplayPopupMenu(MenuID, ToolbarPlus, ToolbarPlus_PopupX, ToolbarPlus_PopupY)
               ProcedureReturn 0
            EndIf
         EndIf
      Next
   EndIf
   
   ProcedureReturn Result
EndProcedure
ProcedureDLL ToolbarPlusCB_Toolbar() ;ToolbarPlusCB() -> active toolbar
   Shared ToolbarPlus_Toolbar
   ProcedureReturn ToolbarPlus_Toolbar
EndProcedure
ProcedureDLL ToolbarPlusCB_ButtonID() ;ToolbarPlusCB() -> pressed button ID
   Shared ToolbarPlus_ButtonID
   ProcedureReturn ToolbarPlus_ButtonID
EndProcedure
ProcedureDLL ToolbarPlusCB_PressedArrow() ;ToolbarPlusCB() -> pressed button arrow
   Shared ToolbarPlus_PressedArrow
   ProcedureReturn ToolbarPlus_PressedArrow
EndProcedure
ProcedureDLL ToolbarPlusCB_PopupX() ;ToolbarPlusCB() -> popup position X
   Shared ToolbarPlus_PopupX
   ProcedureReturn ToolbarPlus_PopupX
EndProcedure
ProcedureDLL ToolbarPlusCB_PopupY() ;ToolbarPlusCB() -> popup position Y
   Shared ToolbarPlus_PopupY
   ProcedureReturn ToolbarPlus_PopupY
EndProcedure
;}

;{ ToolbarPlus Gadgets

ProcedureDLL CreateToolBarPlus(ID, Parent, Size=20) ;Create toolbar plus

	Shared ToolbarPlusParent
	Shared ToolbarPlus
	Shared ToolbarPlusID
	Result=CreateToolBar(ID, parent)
	ToolbarPlusID=ID
	ToolbarPlusParent=parent
	ValidateGadgetID(Result, ToolbarPlusID, ToolbarPlus)
	
	; ====================
	; Change Style
	; ====================
	#TB_SETEXTENDEDSTYLE=$400+84
	#TB_GETEXTENDEDSTYLE=$400+85
	#TBSTYLE_EX_DRAWDDARROWS=$1
	Protected es=SendMessage_(ToolbarPlus, #TB_GETEXTENDEDSTYLE, 0, 0) | #TBSTYLE_EX_DRAWDDARROWS
	SendMessage_(ToolbarPlus, #TB_SETEXTENDEDSTYLE, 0, es)

	hImageList = ImageList_Create_(Size, Size, #ILC_COLOR32 | #ILC_MASK, 20, 0)
	If hImageList
		SendMessage_(ToolbarPlus, #TB_SETIMAGELIST, 0, hImageList) 
		Select Size
			Case 24
				SendMessage_(ToolbarPlus, #TB_LOADIMAGES, #IDB_STD_LARGE_COLOR, #HINST_COMMCTRL)
				SendMessage_(ToolbarPlus, #TB_LOADIMAGES, #IDB_HIST_LARGE_COLOR, #HINST_COMMCTRL)
				SendMessage_(ToolbarPlus, #TB_LOADIMAGES, #IDB_VIEW_LARGE_COLOR, #HINST_COMMCTRL) 
			Default
				SendMessage_(ToolbarPlus, #TB_LOADIMAGES, #IDB_STD_SMALL_COLOR, #HINST_COMMCTRL)
				SendMessage_(ToolbarPlus, #TB_LOADIMAGES, #IDB_HIST_SMALL_COLOR, #HINST_COMMCTRL)
				SendMessage_(ToolbarPlus, #TB_LOADIMAGES, #IDB_VIEW_SMALL_COLOR, #HINST_COMMCTRL)
		EndSelect
	EndIf
		
	SendMessage_(ToolbarPlus, #TB_AUTOSIZE, 0, 0)
	ShowWindow_(ToolbarPlus,  #True)
	
	ProcedureReturn Result
		
EndProcedure


ProcedureDLL ToolbarContainer(ID, offsetX, width, flag=0) ;Add toolbar container
   Shared ToolbarPlusParent
   Shared ToolbarPlus
   Shared ToolbarPlusID
   
   ; ====================
   ; Prepare
   ; ====================
   Protected pos.l=SendMessage_(ToolbarPlus, #TB_BUTTONCOUNT, 0, 0)
   Protected separator.TBBUTTON
   ToolBarSeparator()
   ;set separator width
   SendMessage_(ToolbarPlus, #TB_GETBUTTON, pos, @separator)
   separator\iBitmap=width+offsetX
   SendMessage_(ToolbarPlus, #TB_DELETEBUTTON, pos, 0)
   SendMessage_(ToolbarPlus, #TB_INSERTBUTTON, pos, separator)
   ;get separator position
   SendMessage_(ToolbarPlus, #TB_GETITEMRECT, pos, @rc.RECT)
   
   ; ====================
   ; Create
   ; ====================
   Protected x.l=rc\left
   Protected y.l=0
   Protected h.l=ToolbarContainerHeight()
   Protected w.l=width+offsetX
   CompilerIf #PB_Compiler_Version<430
      CreateGadgetList(ToolbarPlus)
   CompilerElse
      UseGadgetList(ToolbarPlus)
   CompilerEndIf
   Result=ContainerGadget(ID, x, y, w, h, flag)
   
   ProcedureReturn Result
EndProcedure
ProcedureDLL ToolBarSpace(width=5) ;Add toolbar space
   result=ToolbarContainer(#PB_Any, width, 0)
   CloseGadgetList()
   ProcedureReturn result
EndProcedure

ProcedureDLL ToolbarComboBox(ID, offsetX, offsetY, width, height, flags=0) ;Add toolbar ComboboxGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=ComboBoxGadget(ID, offsetX, offsetY, width, height, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarCheckBox(ID, offsetX, offsetY, width, height, text$, flags=0) ;Add toolbar CheckBoxGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=CheckBoxGadget(ID, offsetX, offsetY, width, height, text$, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarOption(ID, offsetX, offsetY, width, height, text$, close.b=#False) ;Add toolbar OptionGadget (with close flag)
   Static ToolbarOptionContainerID
   If ToolbarOptionContainerID=0
      ; ====================
      ; open option list
      ; ====================
      ToolbarOptionContainerID=ToolbarContainer(#PB_Any, offsetX, width)
   Else
      Shared ToolbarPlus
      Protected pos.l=SendMessage_(ToolbarPlus, #TB_BUTTONCOUNT, 0, 0)
      Protected separator.TBBUTTON
      ToolBarSeparator()
      ;set separator width
      SendMessage_(ToolbarPlus, #TB_GETBUTTON, pos, @separator)
      separator\iBitmap=width+offsetX
      SendMessage_(ToolbarPlus, #TB_DELETEBUTTON, pos, 0)
      SendMessage_(ToolbarPlus, #TB_INSERTBUTTON, pos, separator)
     
      ;set container width
      offsetX+GadgetWidth(ToolbarOptionContainerID)
      ResizeGadget(ToolbarOptionContainerID, #PB_Ignore, #PB_Ignore, offsetX+width, #PB_Ignore)
     
   EndIf
   If ToolbarOptionContainerID
      ; ====================
      ; creation new option
      ; ====================
      Result=OptionGadget(ID, offsetX, offsetY, width, height, text$)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      ; ====================
      ; close option list
      ; ====================
      If close
         CloseGadgetList()
         ToolbarOptionContainerID=0
      EndIf
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarButton(ID, offsetX, offsetY, width, height, text$, flags=0) ;Add toolbar ButtonGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=ButtonGadget(ID, offsetX, offsetY, width, height, text$, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarButtonImage(ID, offsetX, offsetY, width, height, imageID, flags=0) ;Add toolbar ButtonImage
   If ToolbarContainer(#PB_Any, offsetX, width)
      CompilerIf #PB_Compiler_Version<420
         Result=ButtonImageGadget(ID, offsetX, offsetY, width, height, imageID)
      CompilerElse
         Result=ButtonImageGadget(ID, offsetX, offsetY, width, height, imageID, flags)
      CompilerEndIf
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure

ProcedureDLL ToolbarString(ID, offsetX, offsetY, width, height, content$, flags=0) ;Add toolbar StringGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=StringGadget(ID, offsetX, offsetY, width, height, content$, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarIPAddress(ID, offsetX, offsetY, width, height) ;Add toolbar IPAddressGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=IPAddressGadget(ID, offsetX, offsetY, width, height)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarHyperlink(ID, offsetX, offsetY, width, height, text$, color, flags=0) ;Add toolbar HyperLinkGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=HyperLinkGadget(ID, offsetX, offsetY, width, height, text$, color, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure

ProcedureDLL ToolbarDate(ID, offsetX, offsetY, width, height, formatDate$="", date=0, flags=0) ;Add toolbar DateGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=DateGadget(ID, offsetX, offsetY, width, height, formatDate$, date, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarProgressBar(ID, offsetX, offsetY, width, height, min, max, flags=0) ;Add toolbar ProgressBarGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=ProgressBarGadget(ID, offsetX, offsetY, width, height, min, max, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarSpin(ID, offsetX, offsetY, width, height, min, max, flags=0) ;Add toolbar SpinGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=SpinGadget(ID, offsetX, offsetY, width, height, min, max, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarTrackBar(ID, offsetX, offsetY, width, height, min, max, flags=0) ;Add toolbar TrackBarGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=TrackBarGadget(ID, offsetX, offsetY, width, height, min, max, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarScrollbar(ID, offsetX, offsetY, width, height, min, max, pageLegnth, flags=0) ;Add toolbar ScrollBarGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=ScrollBarGadget(ID, offsetX, offsetY, width, height, min, max, pageLegnth, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure

ProcedureDLL ToolbarImage(ID, offsetX, offsetY, width, height, imageID, flags=0) ;Add toolbar ImageGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=ImageGadget(ID, offsetX, offsetY, width, height, imageID, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
ProcedureDLL ToolbarText(ID, offsetX, offsetY, width, height, text$, flags=0) ;Add toolbar TextGadget
   If ToolbarContainer(#PB_Any, offsetX, width)
      Result=TextGadget(ID, offsetX, offsetY, width, height, text$, flags)
      ValidateGadgetID(Result, ID, gadget)
      ToolbarCenterGadget(ID)
      CloseGadgetList()
      ProcedureReturn Result
   EndIf
EndProcedure
;}

; ====================
; EXAMPLE
; ====================
Enumeration
   #WIN=0
   #TOOLBAR
   #BUTTON
   #COMBO
   #STRINGS
   #CHECK
   #BAR
   #BAR2
   #TEXT
   
   #CONTAINER_BOTTOM
   #TOOLBAR_BOTTOM
   #SPIN
   #IPADRESS
EndEnumeration
If OpenWindow(#WIN, 100, 100, 700, 200, "Toolbar Plus For PB 4.X", #PB_Window_SystemMenu)
   ;{/// CREATE TOOLBAR TOP
   CreateToolBarPlus(#TOOLBAR, WindowID(#WIN),40)
   ToolbarText(#TEXT, 0, 0, 60, 34, "ToolbarPlus")
   ToolBarSpace(40)
   ToolBarStandardButton(10, #PB_ToolBarIcon_New, #PB_ToolBar_Toggle)
   ToolBarStandardButton(11, #PB_ToolBarIcon_Save)
   ToolBarSeparator()
   ToolbarCombobox(#COMBO, 0, 0, 40, #PB_Ignore)
   ToolBarSeparator()
   ToolbarButton(#BUTTON, 0, 0, 80, ToolbarContainerHeight()-2, "OK", #PB_Button_Toggle)
   ToolBarSeparator()
   ToolbarString(#STRINGS, 0, 0, 50, 20, "String", #PB_String_UpperCase)
   ToolbarCheckBox(#CHECK, 5, 0, 70, 20, "CheckMe")
   ToolBarSeparator()
   ToolbarOption(#PB_Any, 0, 0, 30, 20, "1")
   ToolbarOption(#PB_Any, 0, 0, 30, 20, "2")
   ToolbarOption(#PB_Any, 0, 0, 30, 20, "3", #True)
   ToolBarSeparator()
   ToolbarProgressBar(#BAR, 0, 0, 20, 20, 0, 100, #PB_ProgressBar_Vertical)
   ToolbarProgressBar(#BAR2, 5, 0, 40, 20, 0, 100)
   ToolBarSeparator()
   ToolbarHyperlink(#PB_Any, 0, 0, 150, 14, "http://www.purebasic.com", $0000FF, #PB_HyperLink_Underline)
   
   AddGadgetItem(#COMBO, -1, "A")
   AddGadgetItem(#COMBO, -1, "B")
   
   SetToolBarButtonState(#TOOLBAR, 10, 1)
   SetGadgetState(#CHECK, 1)
   SetGadgetState(#BAR, 20)
   SetGadgetState(#BAR2, 60)
   
   ToolBarToolTip(#TOOLBAR, 10, "New document")
   GadgetToolTip(#BAR, "veritcal progressbar")
   
   SetGadgetColor(#TEXT, #PB_Gadget_BackColor, RGB(255, 0, 0))
   SetGadgetColor(#TEXT, #PB_Gadget_FrontColor, RGB(255, 255, 255))
   ;}
   ;{/// CREATE TOOLBAR BOTTOM (inside ContainerGadget)
   CreateImage(0, 32, 16)
   StartDrawing(ImageOutput(0))
      Box(0, 0, 32, 16, RGB(10, 152, 100))
      Box(8, 4, 16, 8, RGB(255, 0, 0))
   StopDrawing()
   
   CreatePopupMenu(100)
   MenuItem(10, "MenuID 10")
   MenuItem(20, "MenuID 20")
   
   UseGadgetList(WindowID(#WIN))
   ContainerGadget(#CONTAINER_BOTTOM, 0, ToolBarHeight(#TOOLBAR), WindowWidth(#WIN), 62, #PB_Container_Double)
   
   CreateToolBarPlus(#TOOLBAR_BOTTOM, GadgetID(#CONTAINER_BOTTOM))
   ToolBarSeparator()
   ToolbarIPAddress(#IPADRESS, 0, 0, 120, 20)
   ToolbarDate(#PB_Any, 5, 0, 100, 20)
   ToolBarSeparator()
   ToolbarSpin(#SPIN, 0, 0, 40, 20, 0, 100, #PB_Spin_ReadOnly)
   ToolbarTrackBar(#PB_Any, 5, 0, 60, 20, 0, 10, #PB_TrackBar_Ticks)
   ToolBarSeparator()
   ToolbarScrollbar(#PB_Any, 0, 0, 64, 20, 0, 100, 10)
   ToolbarButtonImage(#PB_Any, 5, 0, 64, ToolbarContainerHeight(), ImageID(0))
   ToolBarSeparator()
   ToolBarImageButton(20, ImageID(0))
   ToolBarStandardButton(15, #PB_ToolBarIcon_Find)
   SetToolBarArrow()
   SetToolBarMenu(100)
   ToolBarStandardButton(16, #PB_ToolBarIcon_Print)
   SetToolBarArrow(#True)
   SetToolBarMenu(100)
   ToolBarStandardButton(17, #PB_ToolBarIcon_Paste)
   SetToolBarMenu(100)
   ToolBarSeparator()
   ToolBarStandardButton(100, #PB_ToolBarIcon_Help)
   SetToolBarToggle(#True)
   ToolBarStandardButton(101, #PB_ToolBarIcon_Help)
   SetToolBarToggle(#True)
   ToolBarStandardButton(102, #PB_ToolBarIcon_Help)
   SetToolBarToggle(#True)
   
   SetGadgetColor(#SPIN, #PB_Gadget_BackColor, RGB(255, 225, 10))
   SetGadgetFont(#SPIN, LoadFont(1, "Arial", 12, #PB_Font_Bold))
   SetGadgetText(#SPIN, "1")
   ;}
EndIf

Procedure mycallback(Window, message, wParam, lParam)
   Protected Result=#PB_ProcessPureBasicEvents
   ;
   ;
   ; your code
   ;
   
   ; ===============================================
   ; Enable automatic popup menu
   ; ===============================================
   Result=ToolBarPlusCB(Window, message, wParam, lParam, Result)
   
   ; ===============================================
   ; custom popup menu (without using SetToolbarMenu)
   ; ===============================================
   If ToolbarPlusCB_Toolbar()=ToolBarID(#TOOLBAR)
      If ToolbarPlusCB_ButtonID()=11 And ToolbarPlusCB_PressedArrow()=#False
         Protected owner=ToolbarPlusCB_Toolbar()
         Protected x=ToolbarPlusCB_PopupX()
         Protected y=ToolbarPlusCB_PopupY()
         DisplayPopupMenu(100, owner, x, y)
         ProcedureReturn 1
      EndIf
   EndIf
   
   ;
   ;
   ; your code
   ;
   ProcedureReturn Result
EndProcedure
SetWindowCallback(@mycallback(), #WIN)

; ====================
; EVENT LOOP
; ====================
Repeat
   e=WaitWindowEvent()
   If e=#PB_Event_ActivateWindow
      ;update ipadress
      SetGadgetState(#IPADRESS, MakeIPAddress(127, 0, 0, Random(255)))
   EndIf
   
   If e=#PB_Event_Menu
      m=EventMenu()
      Debug "menuID="+Str(m)
   EndIf
   If e=#PB_Event_Gadget
      g=EventGadget()
      t=EventType()
     ; Debug "gadgetID="+Str(g)+" eventType="+Str(t)+" state="+Str(GetGadgetState(g))
     
      ;update spin
      SetGadgetText(#SPIN, Str(GetGadgetState(#SPIN)))
   EndIf
Until e=#PB_Event_CloseWindow
I don't remember where I donwnloaded it, isn't you who made it?
PureBasic: Surprisingly simple, diabolically powerful
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Toolbar+ dropdown arrow, popupmenu, toggle buttons, gadg

Post by eddy »

updated
- ADDED: useLargeIcons : 24x24 icon size
- ADDED: a 3rd example

Code: Select all

OpenToolbarGadgetList(Toolbar, useLargeIcons=#False, AutoCenterGadgets=#True)
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply