How to place a StringGadget on the Toolbar?

Just starting out? Need help? Post your questions and find answers here.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

How to place a StringGadget on the Toolbar?

Post by rndrei »

How to place a StringGadget on the Toolbar?
User avatar
jacdelad
Addict
Addict
Posts: 2002
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How to place a StringGadget on the Toolbar?

Post by jacdelad »

You can't.

Programs doing this don't use toolbars, but Ribbons or other custom things.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Kiffi
Addict
Addict
Posts: 1493
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: How to place a StringGadget on the Toolbar?

Post 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
Hygge
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: How to place a StringGadget on the Toolbar?

Post by rndrei »

Great, but I need it for Linux! :D
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to place a StringGadget on the Toolbar?

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2002
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: How to place a StringGadget on the Toolbar?

Post by jacdelad »

Ok, you can. :D This is not intended by M$.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply