How to loop through all gadgets of a window?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

How to loop through all gadgets of a window?

Post by Kukulkan »

Hi,

I like to do a function that loops through all gadgets of a window to change their alignment or font. For example, I want to set the font of all text- and string-gadgets of a window to some smaller one. No, I don't want to use SetGadgetFont() on more than 30 gadgets (big configuration dialog). Is there a way to get a list of all gadgets of a window and their gadget type?

I need this for Linux and Windows.

Best

Volker
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: How to loop through all gadgets of a window?

Post by Shield »

Not natively. Just write our own functions that add the gadgets to a list
to be able to keep track of them. ;) Just be creative.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: How to loop through all gadgets of a window?

Post by Trond »

If you use enumerated constants it's straightforward, just loop from the first to the last:

Code: Select all

For I = = #FirstGadget to #LastGadget

Next
If you use #PB_Any, you need to put all gadgets in a list or map and use a foreach loop.

This is the only crossplatform way of looping over all gadgets. Though if you want all gadgets of a certain type to have a particular appearance, I suggest you make a procedure to create them like you want them to be:

Code: Select all

Procedure H3TextGadget(Gadget, x, y, w, h, Text.s, Flags=0)
  Static Font.i
  Protected Handle.i
  If Not Font
    Font = LoadFont(#PB_Any, "Trebuchet MS", 10, #PB_Font_Bold)
  EndIf
  Handle = TextGadget(Gadget, x, y, w, h, Text, Flags)
  If Gadget = #PB_Any
    Gadget = Handle
  EndIf
  SetGadgetFont(Gadget, FontID(Font))
  ProcedureReturn Handle
EndProcedure

W = 512
H = 384
OpenWindow(0, 0, 0, W, H, "", #PB_Window_ScreenCentered | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget)

H3TextGadget(#PB_Any, 10, 10, 200, 25, "Header Size 3")

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      CloseWindow(0)
      Break
  EndSelect
ForEver
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: How to loop through all gadgets of a window?

Post by Bisonte »

Windows :

Code: Select all

EnableExplicit

Define EventID

Procedure ListWindows(hwnd,Param)
  Protected String$
  Protected String2$
  
  String$ = Space(250)
  String2$ = Space(1024)
  GetWindowText_(hwnd,String$,250)
  GetClassName_(hwnd,@String2$,Len(String2$))
  
  Debug "OS ID of Gadget: "+Str(hwnd)
  Debug "PureBasic-ID of Gadgets: "+Str(GetProp_(hwnd, "PB_ID"))
  Debug "GadgetType: "+String2$
  Debug "Gadgettext: "+String$
  Debug "--------------------------------------------------------------------"
  
  ProcedureReturn #True
EndProcedure

If OpenWindow(0,0,0,500,400,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  ButtonGadget(1,10,10,100,20,"Hallo Button",0)
  StringGadget(2,120,10,100,20,"Hallo Edit",0)
  ListViewGadget(3,10,40,100,100,0)
  
  EnumChildWindows_(WindowID(0),@ListWindows(),0)
  
  Repeat
    EventID=WaitWindowEvent()
    If EventID = #PB_Event_CloseWindow
      End
    EndIf
  ForEver
EndIf
From RSBasics WinAPI-Library
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to loop through all gadgets of a window?

Post by Kukulkan »

Thanks, good ideas! I will take a look.

Best,

Kukulkan
Post Reply