Page 1 of 1

How to place a StringGadget on the Toolbar?

Posted: Mon Mar 24, 2025 11:30 am
by rndrei
How to place a StringGadget on the Toolbar?

Re: How to place a StringGadget on the Toolbar?

Posted: Mon Mar 24, 2025 12:00 pm
by jacdelad
You can't.

Programs doing this don't use toolbars, but Ribbons or other custom things.

Re: How to place a StringGadget on the Toolbar?

Posted: Mon Mar 24, 2025 12:12 pm
by Kiffi
jacdelad wrote: Mon Mar 24, 2025 12:00 pmYou can't.
You can! :P (Windows only!)

With:

Code: Select all

SetParent_(GadgetID(#StringGadget), ToolBarID(#ToolBar))

Simple example:

Code: Select all

Enumeration
  #Window
  #ToolBar
  #ToolBarImageButton
  #StringGadget
EndEnumeration

OpenWindow(#Window, 0, 0, 150, 50, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CreateToolBar(#ToolBar, WindowID(#Window))

UsePNGImageDecoder()
Path$ = #PB_Compiler_Home  + "Examples" + #PS$ + "Sources" + #PS$ + "Data" + #PS$ + "ToolBar" + #PS$ + ""
ToolBarImageButton(#ToolBarImageButton, LoadImage(0, Path$ + "New.png"))

StringGadget(#StringGadget, 30, 2, 100, 20, "Hello!")

SetParent_(GadgetID(#StringGadget), ToolBarID(#ToolBar))

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

Re: How to place a StringGadget on the Toolbar?

Posted: Mon Mar 24, 2025 12:42 pm
by rndrei
Great, but I need it for Linux! :D

Re: How to place a StringGadget on the Toolbar?

Posted: Mon Mar 24, 2025 9:41 pm
by Shardik
rndrei wrote: Mon Mar 24, 2025 12:42 pm Great, but I need it for Linux! :D
I have modified Kiffi's example to also work on Linux and MacOS. I have tested it successfully with PB 6.20 x64 on these operating systems:
- Linux Mint 21.3 'Virginia' with Cinnamon
- MacOS 13.7.4 'Ventura'
- RaspberryPi OS 64-Bit (Debian 12.9 'Bookworm')
- Windows 10 23H2

Code: Select all

Enumeration
  #Window
  #ToolBar
  #ToolBarImageButton
  #StringGadget
EndEnumeration

OpenWindow(#Window, 0, 0, 170, 50, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CreateToolBar(#ToolBar, WindowID(#Window))

UsePNGImageDecoder()
Path$ = #PB_Compiler_Home  + "examples" + #PS$ + "sources" + #PS$ + "Data" +
  #PS$ + "ToolBar" + #PS$ + ""
ToolBarImageButton(#ToolBarImageButton, LoadImage(0, Path$ + "New.png"))

StringGadget(#StringGadget, 30, 2, 100, 20, "Hello!")

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    ToolItem = gtk_tool_item_new_()
    gtk_tool_item_set_visible_horizontal_(ToolItem, #True)
    gtk_toolbar_insert_(ToolBarID(#ToolBar), ToolItem, -1)
    gtk_widget_reparent_(GadgetID(#StringGadget), ToolItem)
    gtk_widget_show_all_(WindowID(#Window))
  CompilerCase #PB_OS_MacOS
    Define Size.NSSize
    Size\width = GadgetWidth(#StringGadget)
    Size\height = GadgetHeight(#StringGadget)
    CreateImage(0, 24, 24)
    ToolBarImageButton(#StringGadget, ImageID(0))
    ItemArray = CocoaMessage(0, ToolBarID(#ToolBar), "items")
    Item = CocoaMessage(0, ItemArray, "objectAtIndex:", 1)
    CocoaMessage(0, Item, "setView:", GadgetID(#StringGadget))
    CocoaMessage(0, Item, "setMaxSize:@", @Size)
    CocoaMessage(0, Item, "setMinSize:@", @Size)
  CompilerCase #PB_OS_Windows
    SetParent_(GadgetID(#StringGadget), ToolBarID(#ToolBar))
CompilerEndSelect

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

Re: How to place a StringGadget on the Toolbar?

Posted: Tue Mar 25, 2025 6:32 am
by jacdelad
Ok, you can. :D This is not intended by M$.