Page 1 of 1

ComboBox in Toolbar

Posted: Fri Nov 22, 2013 3:39 pm
by Perkin
Is there any way you can put a ComboBox (or any 'standard' gadget) inside a toolbar.

Using XCode 5, I can create a basic window with toolbar which has one in, it gets put in a customview inside the toolbar. So can it be done in PB?

Re: ComboBox in Toolbar

Posted: Sat Mar 22, 2014 6:52 pm
by Shardik
It's very difficult to modify PB's ToolBar in MacOS X. For a proper implemention you have to implement your own toolbar with API functions and define several required delegate methods. On MacOS X most toolbar programming tasks are done in these delegate methods.

But you may try the wierd hack I have posted below. It uses a standard PB ToolBar and adds a dummy button which is replaced by a ComboBox. This trick even allows to react to ComboBox changes in the regular PB event loop... 8)

Code: Select all

EnableExplicit

#NSToolbarSizeModeRegular = 1

Define Item.I
Define ItemArray.I
Define Size.NSSize

OpenWindow(0, 270, 100, 260, 50, "ToolBar with ComboBox",
  #PB_Window_SystemMenu | #PB_Window_Invisible)

; ----- Define standard ComboBox
ComboBoxGadget(0, 10, 20, 100, 22)
AddGadgetItem(0, 0, "MacOS X")
AddGadgetItem(0, 1, "Linux")
AddGadgetItem(0, 2, "Windows")
SetGadgetState(0, 0)

Size\width = GadgetWidth(0)
Size\height = GadgetHeight(0)

; ----- Create empty dummy image
CreateImage(0, 24, 24)

; ----- Create standard ToolBar
CreateToolBar(0, WindowID(0))

; ----- Change size of ToolBar to larger icons
CocoaMessage(0, ToolBarID(0), "setSizeMode:", #NSToolbarSizeModeRegular)

; ----- Add empty dummy image to ToolBar
ToolBarImageButton(0, ImageID(0))

; ----- Get item object of ToolBar button
ItemArray = CocoaMessage(0, ToolBarID(0), "items")
Item = CocoaMessage(0, ItemArray, "objectAtIndex:", 0)

; ----- Replace ToolBar button by ComboBox
CocoaMessage(0, Item, "setView:", GadgetID(0))

; ----- Change ToolBar button size to that of ComboBox
CocoaMessage(0, Item, "setMaxSize:@", @Size)
CocoaMessage(0, Item, "setMinSize:@", @Size)

; ----- Make window visible now in order to hide previous ToolBar modification
HideWindow(0, #False)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        If EventType() = #PB_EventType_Change
          Debug "ComboBox selection changed to " + GetGadgetText(0)
        EndIf
      EndIf
  EndSelect
ForEver

Re: ComboBox in Toolbar

Posted: Sat Mar 22, 2014 9:09 pm
by Mindphazer
Very smart Shardik :-)

Thank you

Re: ComboBox in Toolbar

Posted: Sat Mar 22, 2014 9:48 pm
by Shardik
Mindphazer wrote:Very smart Shardik :-)

Thank you
Thank you for your kind words... :D

I was even able to add other gadgets to the ToolBar which are all fully functional. The only drawback is that you have to define all dummy buttons to be replaced with gadgets in the ToolBar in advance because an error will be displayed when you try to add additional buttons to the ToolBar dynamically:

Code: Select all

#NSToolbarSizeModeRegular = 1

Procedure AddGadgetToToolBar(GadgetID.I, ButtonID.I)
  Protected Item.I
  Protected ItemArray.I
  Protected Size.NSSize
  
  ; ----- Get item object of ToolBar button
  ItemArray = CocoaMessage(0, ToolBarID(0), "items")
  Item = CocoaMessage(0, ItemArray, "objectAtIndex:", ButtonID)

  ; ----- Get width and height of gadget
  Size\width = GadgetWidth(GadgetID)
  Size\height = GadgetHeight(GadgetID)
  
  ; ----- Replace ToolBar button by gadget
  CocoaMessage(0, Item, "setView:", GadgetID(GadgetID))
  
  ; ----- Change ToolBar button size to that of gadget
  CocoaMessage(0, Item, "setMaxSize:@", @Size)
  CocoaMessage(0, Item, "setMinSize:@", @Size)
EndProcedure


OpenWindow(0, 270, 100, 350, 50, "ToolBar with different gadgets",
  #PB_Window_SystemMenu | #PB_Window_Invisible)

ComboBoxGadget(0, 0, 0, 100, 22)
AddGadgetItem(0, 0, "MacOS X")
AddGadgetItem(0, 1, "Linux")
AddGadgetItem(0, 2, "Windows")
SetGadgetState(0, 0)

ProgressBarGadget(1, 10, 10, 100, 16, 0, 100)
SetGadgetState(1, 0)

TrackBarGadget(2, 0, 0, 100, 20, 0, 100)
SetGadgetState(2, 50)

; ----- Create standard ToolBar
CreateToolBar(0, WindowID(0))

; ----- Change size of ToolBar to larger icons
CocoaMessage(0, ToolBarID(0), "setSizeMode:", #NSToolbarSizeModeRegular)

; ----- Create empty dummy image
CreateImage(0, 24, 24)

; ----- Add buttons with dummy image to ToolBar
ToolBarImageButton(0, ImageID(0))
ToolBarImageButton(1, ImageID(0))
ToolBarImageButton(2, ImageID(0))

; ----- Free empty dummy image  
FreeImage(0)

; ----- Add gadgets to ToolBar
AddGadgetToToolBar(0, 0)
AddGadgetToToolBar(1, 1)
AddGadgetToToolBar(2, 2)

; ----- Make window visible now in order to hide previous ToolBar modifications
HideWindow(0, #False)

AddWindowTimer(0, 0, 50)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Timer
      If EventTimer() = 0
        CurrentState = GetGadgetState(1) + 1

        If CurrentState > 100
          CurrentState = 0
        EndIf

        SetGadgetState(1, CurrentState)
      EndIf
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_Change
            Debug "ComboBox selection changed to " + GetGadgetText(0)
          EndIf
        Case 2
          Debug "Trackbar position changed to " + GetGadgetState(2)
      EndSelect
  EndSelect
ForEver

Re: ComboBox in Toolbar

Posted: Wed Mar 26, 2014 8:56 pm
by Perkin
Nice one Shardik, works well.