Count gadget windows [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Count gadget windows [Resolved]

Post by Kwai chang caine »

Hello everyone

I am looking for a code for counting the number of each type gadget.

A procedure that return the number of button StringGadget, TextGadget, etc. ....
If they return the ID gadget, it would be paradise :D

Thank you for your help
Last edited by Kwai chang caine on Mon Feb 04, 2008 4:51 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Code: Select all

Structure _countgadgets
  className$
  count.l
EndStructure

Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets) 
  Protected buffer$
  buffer$=Space(100)
  GetClassName_(hwnd, @buffer$, 100)
  If buffer$ = *cg\className$
    *cg\count + 1
  EndIf
  ProcedureReturn 1
EndProcedure


;The following function takes a window handle and a gadget handle and returns a count of the
;number of similar controls on the given window.
;This count includes the specified gadget.
Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd)
  Protected buffer$
  Protected cg._countgadgets
  If IsWindow_(winhWnd) And IsWindow_(gadgethWnd)
    buffer$=Space(100)
    GetClassName_(gadgethWnd, @buffer$, 100)
    cg\className$ = buffer$
    EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg)
  EndIf
  ProcedureReturn cg\count
EndProcedure


If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
  ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
  ButtonGadget(3, 10,100, 200, 60, "Multiline Button  (longer text gets automatically wrapped)", #PB_Button_MultiLine)
  ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
  count = CountSimilarGadgets(WindowID(0), GadgetID(0))
  Debug "There are " + Str(count) + " buttons!"

  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf
End
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Yahoo !!!

Thanks a lot for your great code. 8)
It's exactly, what i search.

I have add some lines, for the beginners worse than me, if he exists :oops:

Code: Select all

; http://www.purebasic.fr/english/viewtopic.php?t=30961

; Code de SROD
; Agrandi par KCC pour montrer plusieurs exemples

Structure _countgadgets 
  className$ 
  count.l 
EndStructure 

Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets) 
  Protected buffer$ 
  buffer$=Space(100) 
  GetClassName_(hwnd, @buffer$, 100) 
  If buffer$ = *cg\className$ 
    *cg\count + 1 
  EndIf 
  ProcedureReturn 1 
EndProcedure 

;The following function takes a window handle and a gadget handle and returns a count of the 
;number of similar controls on the given window. 
;This count includes the specified gadget. 

Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd) 
  Protected buffer$ 
  Protected cg._countgadgets 
  If IsWindow_(winhWnd) And IsWindow_(gadgethWnd) 
    buffer$=Space(100) 
    GetClassName_(gadgethWnd, @buffer$, 100) 
    cg\className$ = buffer$ 
    EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg) 
  EndIf 
  ProcedureReturn cg\count 
EndProcedure 


If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button") 
  ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left) 
  ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right) 
  ButtonGadget(3, 10,100, 200, 60, "Multiline Button  (longer text gets automatically wrapped)", #PB_Button_MultiLine) 
  ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle) 
  StringGadget(5, 10, 200, 100, 20, "Coucou")
  ComboBoxGadget(6, 10,240, 100, 50)
  ComboBoxGadget(7, 10, 300, 100, 50)
  ComboBoxGadget(8, 10, 350, 100, 50)
  ListViewGadget(9, 10, 400, 100, 30)
  ListViewGadget(10, 10, 450, 100, 30)
  
    
  CountButton = CountSimilarGadgets(WindowID(0), GadgetID(4)) 
  CountText = CountSimilarGadgets(WindowID(0), GadgetID(5)) 
  CountBox = CountSimilarGadgets(WindowID(0), GadgetID(6)) 
  CountList = CountSimilarGadgets(WindowID(0), GadgetID(9)) 
  
  Debug "There are " + Str(countButton) + " buttons!" 
  Debug "There are " + Str(countText) + " Texte!" 
  Debug "There are " + Str(countBox) + " ComboBox!" 
  Debug "There are " + Str(countList) + " ListBox!" 

  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf 
End
I wish you a very very good day.
And the pleasure of having the honor to speak to you again 8)
ImageThe happiness is a road...
Not a destination
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post by hallodri »

another way :

Code: Select all

Import "gadget.lib"
	PB_Gadget_Objects
 	PB_Object_EnumerateStart	(*Object)
 	PB_Object_EnumerateNext	(*Object, *id)	
EndImport 	

Macro ForEachObject(obj,cursor)
   PB_Object_EnumerateStart(obj) 
   While PB_Object_EnumerateNext(obj,@cursor) 
EndMacro

Macro NextObject :   Wend : EndMacro

If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
  ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
  ButtonGadget(3, 10,100, 200, 60, "Multiline Button  (longer text gets automatically wrapped)", #PB_Button_MultiLine)
  ButtonGadget(99, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)  
  ButtonGadget(-1, 10,210, 200, 20, "Toggle Button", #PB_Button_Toggle)  
  
  ForEachObject(PB_Gadget_Objects,id)  	
  	Debug id
  	Debug GadgetType(id)
  	Debug "-----------"
  NextObject


  Repeat
    event = WaitWindowEvent()
  Until event = #PB_Event_CloseWindow
EndIf
End
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thanks Allodri for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

@ Hello SROD

I'm so sad :cry:

Why :
There are 2 TextGadget :D
There are 2 Combo :D
There are 4 String :shock:

Code: Select all

; http://www.purebasic.fr/english/viewtopic.php?t=30961
; Codé par SROD

Structure _countgadgets 
  className$ 
  count.l 
EndStructure 

Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets) 
  Protected buffer$ 
  buffer$=Space(100) 
  GetClassName_(hwnd, @buffer$, 100) 
  If buffer$ = *cg\className$ 
    *cg\count + 1 
  EndIf 
  ProcedureReturn 1 
EndProcedure 

;The following function takes a window handle and a gadget handle and returns a count of the 
;number of similar controls on the given window. 
;This count includes the specified gadget. 

Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd) 
  Protected buffer$ 
  Protected cg._countgadgets 
  If IsWindow_(winhWnd) And IsWindow_(gadgethWnd) 
    buffer$=Space(100) 
    GetClassName_(gadgethWnd, @buffer$, 100) 
    cg\className$ = buffer$ 
    EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg) 
  EndIf 
  ProcedureReturn cg\count 
EndProcedure 


If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
 
 For o = 1 To 2
  TextGadget(100 + o, 10, 30 + (o * 30), 47, 20, "Base", #PB_Text_Center)
  ComboBoxGadget(120 + o, 50, 30 + (o * 30), 90, 300, #PB_ComboBox_Editable)
  StringGadget(140 + o, 150, 30 + (o * 30), 330, 20, "")
 Next 
        
 Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(101)))  + " TextGadget"
 Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(121))) + " Combo"
 Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(141))) + " String" 

  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf 
End
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The reason is because an editable combo gadget has an edit contol attached to it (so that you can type directly into the control) and this is being counted as an extra string gadget because, technically, it is a string gadget! :)

The following should work okay and will ignore all controls which are not 'immediate childs' of the main window etc.

Code: Select all

; http://www.purebasic.fr/english/viewtopic.php?t=30961 
; Codé par SROD 

Structure _countgadgets 
  win.l
  className$ 
  count.l 
EndStructure 

Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets) 
  Protected buffer$ 
  If GetParent_(hWnd) = *cg\win
    buffer$=Space(100) 
    GetClassName_(hwnd, @buffer$, 100) 
    If buffer$ = *cg\className$ 
      *cg\count + 1 
    EndIf 
  EndIf
  ProcedureReturn 1 
EndProcedure 

;The following function takes a window handle and a gadget handle and returns a count of the 
;number of similar controls on the given window. 
;This count includes the specified gadget. 

Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd) 
  Protected buffer$ 
  Protected cg._countgadgets 
  If IsWindow_(winhWnd) And IsWindow_(gadgethWnd)
    buffer$=Space(100) 
    GetClassName_(gadgethWnd, @buffer$, 100) 
    cg\win = winhWnd
    cg\className$ = buffer$ 
    EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg) 
  EndIf 
  ProcedureReturn cg\count 
EndProcedure 


If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  
 For o = 1 To 2 
  TextGadget(100 + o, 10, 30 + (o * 30), 47, 20, "Base", #PB_Text_Center) 
  ComboBoxGadget(120 + o, 50, 30 + (o * 30), 90, 300, #PB_ComboBox_Editable) 
  StringGadget(140 + o, 150, 30 + (o * 30), 330, 20, "") 
 Next 
        
 Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(101)))  + " TextGadget" 
 Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(121))) + " Combo" 
 Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(141))) + " String" 

  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf 
End
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks and ...........thanks :D

You are a mother for me :D

I wish you a very good day
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Kwaï chang caïne wrote:You are a mother for me :D
No offense, but I bloody hope not! :)
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

:lol:
ImageThe happiness is a road...
Not a destination
Post Reply