Page 1 of 1

How to loop through all gadgets of a window?

Posted: Fri Dec 16, 2011 2:04 pm
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

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

Posted: Fri Dec 16, 2011 2:10 pm
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.

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

Posted: Fri Dec 16, 2011 2:18 pm
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

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

Posted: Fri Dec 16, 2011 2:48 pm
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

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

Posted: Fri Dec 16, 2011 3:00 pm
by Kukulkan
Thanks, good ideas! I will take a look.

Best,

Kukulkan