count number of gadgets in a container/gadget list

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: count number of gadgets in a container/gadget list

Post by BarryG »

netmaestro wrote:reading and listing them by type comes in very handy when you're doing say, a visual designer.
Good point. This would save the user having to manually keep track until needed (they can just parse when needed instead).
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: count number of gadgets in a container/gadget list

Post by Danilo »

VB6_to_PBx wrote:thank you RASHAD , for your Code
i modified your Code a little :

Global.i containerID
Why remove the Protected and make the variable Global? :shock:
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: count number of gadgets in a container/gadget list

Post by VB6_to_PBx »

Danilo wrote:
VB6_to_PBx wrote:thank you RASHAD , for your Code
i modified your Code a little :

Global.i containerID
Why remove the Protected and make the variable Global? :shock:
with EnableExplicit
that's the only way i could think of to get the containerID

Code: Select all

      If GetGadgetChilds(0, childGadgets())
        ForEach childGadgets()
         Debug "Found child gadget: " + childGadgets() + Space(3) + "in containerID = " + containerID ; found gadgets 3, 5, 9
        Next
        Debug "No. of found child gadgets = " + ListSize(childGadgets.i())
      EndIf
Danilo , do you know of another way ?
maybe a way using GadgetID() instead of using containerID

i couldn't make GadgetID() work :(
in
Debug "Found child gadget: " + childGadgets() + Space(3) + "in containerID = " + containerID ; found gadgets 3, 5, 9
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: count number of gadgets in a container/gadget list

Post by Danilo »

VB6_to_PBx wrote:Danilo , do you know of another way ?
maybe a way using GadgetID() instead of using containerID

i couldn't make GadgetID() work :(
The container is created as gadget 0, so GadgetID(0) is fine.

Code: Select all

      If GetGadgetChilds(0, childGadgets())
        ForEach childGadgets()
         Debug "Found child gadget: " + childGadgets() + Space(3) + "in containerID = " + GadgetID(0) ; found gadgets 3, 5, 9
        Next
        Debug "No. of found child gadgets = " + ListSize(childGadgets.i())
      EndIf
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: count number of gadgets in a container/gadget list

Post by VB6_to_PBx »

Danilo , thanks for Code correction : GadgetID(0)

seeing 'containerID' has the same Gadget's # as GadgetID(0)
containerID = GadgetID(gadget) : Debug "containerID = " + containerID
i previously tried GadgetID(containerID) ... thinking containerID should hold the Gadget ID # :oops:
but that didn't work .
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
nsstudios
Enthusiast
Enthusiast
Posts: 275
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: count number of gadgets in a container/gadget list

Post by nsstudios »

Thanks everyone. This seems a lot more complicated than I would've hoped for, but good to know there is a solution.
:)
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: count number of gadgets in a container/gadget list

Post by mk-soft »

Code Danilo -> Rashad -> mk-soft

Added Linux Version and correct internal gadget structure ...

Code: Select all

;-TOP

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Import ""
    PB_Object_EnumerateStart(Object)
    PB_Object_EnumerateNext(Object,*ID.Integer)
    PB_Object_EnumerateAbort(Object)
    PB_Object_Count( Objects )
    
    PB_Object_FreeID           (*Object, DynamicOrArrayID)
    PB_Object_GetObject.i      (*Object, DynamicOrArrayID)
    PB_Object_GetObjectDirect  (*Object, DynamicOrArrayID)
    PB_Object_IsObject.i       (*Object, DynamicOrArrayID)
    PB_Object_GetOrAllocateID.i(*Object, ID)
    PB_Gadget_Objects.i
  EndImport
CompilerElse
  ImportC ""
    PB_Object_EnumerateStart(Object)
    PB_Object_EnumerateNext(Object,*ID.Integer)
    PB_Object_EnumerateAbort(Object)
    PB_Object_Count( Objects )
    
    PB_Object_FreeID           (*Object, DynamicOrArrayID)
    PB_Object_GetObject.i      (*Object, DynamicOrArrayID)
    PB_Object_GetObjectDirect  (*Object, DynamicOrArrayID)
    PB_Object_IsObject.i       (*Object, DynamicOrArrayID)
    PB_Object_GetOrAllocateID.i(*Object, ID)
    PB_Gadget_Objects.i
  EndImport
CompilerEndIf

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    ;- PB Interne Struktur Gadget Windows
    Structure PB_Gadget
      *Gadget
      *vt.GadgetVT
      UserData.i
      OldCallback.i
      Daten.i[4]
    EndStructure
  CompilerCase #PB_OS_Linux
    ;- PB Interne Struktur Gadget Linux
    Structure PB_Gadget
      *Gadget
      *GadgetContainer
      *vt.GadgetVT
      UserData.i
      Daten.i[4]
    EndStructure
  CompilerCase #PB_OS_MacOS
    ; PB Interne Struktur Gadget MacOS
    Structure PB_Gadget
      *Gadget
      *Container
      *Functions
      UserData.i
      WindowID.i
      Type.l
      Flags.l
    EndStructure
CompilerEndSelect

EnableExplicit

Procedure.i GetGadgetChilds(gadget, List childs.i())
  Protected r1, gadgetID = -1, *theGadget.PB_Gadget
  Protected count, containerID = GadgetID(gadget)
  ClearList( childs() )
  If PB_Gadget_Objects And containerID
    PB_Object_EnumerateStart(PB_Gadget_Objects)
    While PB_Object_EnumerateNext(PB_Gadget_Objects, @gadgetID)
      If gadgetID <> -1 And IsGadget(gadgetID)
        *theGadget = PB_Object_GetObject(PB_Gadget_Objects,gadgetID)
        If *theGadget And *theGadget\Gadget <> containerID
          r1 = 0
          CompilerSelect #PB_Compiler_OS
            CompilerCase #PB_OS_Windows
              r1 = Bool(GetAncestor_(*theGadget\Gadget, #GA_PARENT) = containerID)
            CompilerCase #PB_OS_MacOS
              r1 = CocoaMessage(0, *theGadget\Gadget, "isDescendantOf:", containerID)
            CompilerCase #PB_OS_Linux
              Protected widget = gtk_widget_get_parent_(*theGadget\Gadget)
              r1 = Bool(widget = containerID)
              If Not r1
                widget = gtk_widget_get_parent_(widget)
              EndIf
              r1 = Bool(widget = containerID)
              
          CompilerEndSelect
          If r1
            count + 1
            If AddElement( childs() )
              childs() = gadgetID
            EndIf
          EndIf
          
        EndIf
      EndIf
      gadgetID = -1
    Wend
    PB_Object_EnumerateAbort(PB_Gadget_Objects)
  EndIf  
  ProcedureReturn count
EndProcedure

; ****

NewList childGadgets.i()

If OpenWindow(0, 0, 0, 322, 250, "ContainerGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ContainerGadget(0, 8, 8, 306, 133, #PB_Container_Raised)
  ButtonGadget(3, 10, 15, 80, 24, "Button 1")
  ButtonGadget(5, 95, 15, 80, 24, "Button 2")
  ListViewGadget(9, 10,50,286,70)
  CloseGadgetList()
  ListViewGadget(11, 10, 150,302,70)
  
  If GetGadgetChilds(0, childGadgets())
    ForEach childGadgets()
      Debug "Found child gadget: " + childGadgets() ; found gadgets 3, 5, 9
    Next
    Debug "No. of founded child gadget: " + ListSize(childGadgets.i())
  EndIf
  ClearList(childGadgets())
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: count number of gadgets in a container/gadget list

Post by Denis »

Thanks to: Danilo -> Rashad -> mk-soft

I'm going to use this code for an app. :wink:
A+
Denis
nsstudios
Enthusiast
Enthusiast
Posts: 275
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: count number of gadgets in a container/gadget list

Post by nsstudios »

Thanks a bunch, everyone.
Post Reply