How to find the Container Gadget?

Just starting out? Need help? Post your questions and find answers here.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

How to find the Container Gadget?

Post by said »

Hi experts,

is there a way to find out what the container gadget of a child gadget is, knowing the child gadget? there must be a way - but i could not find it. Since PB ResizeGadget() fucntion does resize a gadget within its container, so there should be an easy way to find out the 'parent' gadget? Any idea ?

Thanks
User avatar
NicknameFJ
User
User
Posts: 90
Joined: Tue Mar 17, 2009 6:36 pm
Location: Germany

Re: How to find the Container Gadget?

Post by NicknameFJ »

Hello Said,

maybe GetParent_(hwnd)

will help.

NicknameFJ
PS: Sorry for my weird english, but english is not my native language.



Image
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: How to find the Container Gadget?

Post by Polo »

That's a missing feature in my opinion. GetGadgetParent/GetGadgetChildren would be really useful :)
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to find the Container Gadget?

Post by TI-994A »

said wrote:...is there a way to find out what the container gadget of a child gadget is, knowing the child gadget? ...an easy way to find out the 'parent' gadget?
Hi said. NicknameFJ's correct; you can use the GetParent() API function to get the handle of the parent window, but you'll also need to use GetDlgCtrlID() to determine its PureBasic identifier. Here's a sample code:

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 250, 100, "GetParent ID:", wFlags)
ContainerGadget(123, 25, 25, 200, 50)
  SetGadgetColor(123, #PB_Gadget_BackColor, #Cyan)
  ButtonGadget(2, 10, 10, 180, 30, "Button in Container")
CloseGadgetList()
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 2
          Select EventType()
            Case #PB_EventType_LeftClick 
              newLabel.s = Str(GetDlgCtrlID_(GetParent_(GadgetID(2))))
              SetGadgetText(2, "My Papa is Gadget #" + newLabel)
          EndSelect
      EndSelect
  EndSelect
Until appQuit
It's a Windows only solution, but I hope it helps.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: How to find the Container Gadget?

Post by said »

Thank you NicknameFJ and Polo, however i missed to mention that i am more interested in preferably a native PB way or at least a crossplatform.

So you think we should add this in the new feature requests along with the suggested GetGadgetChildren, i find this quite useful
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: How to find the Container Gadget?

Post by said »

TI-994A wrote:
said wrote:...is there a way to find out what the container gadget of a child gadget is, knowing the child gadget? ...an easy way to find out the 'parent' gadget?
Hi said. NicknameFJ's correct; you can use the GetParent() API function to get the handle of the parent window, but you'll also need to use GetDlgCtrlID() to determine its PureBasic identifier. Here's a sample code:

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 250, 100, "GetParent ID:", wFlags)
ContainerGadget(123, 25, 25, 200, 50)
  SetGadgetColor(123, #PB_Gadget_BackColor, #Cyan)
  ButtonGadget(2, 10, 10, 180, 30, "Button in Container")
CloseGadgetList()
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 2
          Select EventType()
            Case #PB_EventType_LeftClick 
              newLabel.s = Str(GetDlgCtrlID_(GetParent_(GadgetID(2))))
              SetGadgetText(2, "My Papa is Gadget #" + newLabel)
          EndSelect
      EndSelect
  EndSelect
Until appQuit
It's a Windows only solution, but I hope it helps.

Thank you TI-994A for the effort and the sample code, we have a windows part so far!
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: How to find the Container Gadget?

Post by Polo »

said wrote:Thank you NicknameFJ and Polo, however i missed to mention that i am more interested in preferably a native PB way or at least a crossplatform.

So you think we should add this in the new feature requests along with the suggested GetGadgetChildren, i find this quite useful
http://www.forums.purebasic.com/english ... =3&t=49900

:oops: :oops:
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: How to find the Container Gadget?

Post by said »

Polo wrote:
said wrote:Thank you NicknameFJ and Polo, however i missed to mention that i am more interested in preferably a native PB way or at least a crossplatform.

So you think we should add this in the new feature requests along with the suggested GetGadgetChildren, i find this quite useful
http://www.forums.purebasic.com/english ... =3&t=49900

:oops: :oops:
:oops:
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: How to find the Container Gadget?

Post by BorisTheOld »

The following technique works well - we use it all the time. It's cross platform and doesn't require any new PB features.

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 250, 100, "GetParent ID:", wFlags)
ContainerGadget(123, 25, 25, 200, 50)
  ButtonGadget(2, 10, 10, 180, 30, "Button in Container")
  SetGadgetData(2, 123)
CloseGadgetList()
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
          Select EventType()
            Case #PB_EventType_LeftClick
              SetGadgetText(2, "My Papa is Gadget #" + Str(GetGadgetData(2)))
          EndSelect
      EndSelect
  EndSelect
Until appQuit
In practice, we store the address of the OOP instance that represents the gadget. One of the properties in the gadget's class always points to the parent container.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 575
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Re: How to find the Container Gadget?

Post by bembulak »

Boris wrote:The following technique works well - we use it all the time. It's cross platform and doesn't require any new PB features.

Code:
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 250, 100, "GetParent ID:", wFlags)
ContainerGadget(123, 25, 25, 200, 50)
ButtonGadget(2, 10, 10, 180, 30, "Button in Container")
SetGadgetData(2, 123)
CloseGadgetList()
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 2
Select EventType()
Case #PB_EventType_LeftClick
SetGadgetText(2, "My Papa is Gadget #" + Str(GetGadgetData(2)))
EndSelect
EndSelect
EndSelect
Until appQuit


In practice, we store the address of the OOP instance that represents the gadget. One of the properties in the gadget's class always points to the parent container.
Basically I like the idea of having something like GetParent/SetParent, but I really like your workaround as well. It's a good idea and works flawlessly! Thanks!
cheers,

bembulak
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: How to find the Container Gadget?

Post by BorisTheOld »

The above example can be modified to show how large amounts of data can be linked to a gadget. This technique also eliminates the need to have huge select statements in the event routine, as the event handling code can be located immediately. This is particulary important in large applications that may have hundreds or thousands of gadgets.

Code: Select all

Structure GadgetInfo
  *EventProcedure.i
  ParentContainer.i
  ;
  ;  other gadget properties can be included here
  ;
EndStructure

Define Button1.GadgetInfo
Define *GadgetPtr.GadgetInfo

Procedure Button1Events(*Ptr.GadgetInfo)
  Select EventType()
    Case #PB_EventType_LeftClick
      SetGadgetText(2, "My Papa is Gadget #" + Str(*Ptr\ParentContainer))
  EndSelect
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 250, 100, "GetParent ID:", wFlags)
ContainerGadget(123, 25, 25, 200, 50)
  ButtonGadget(2, 10, 10, 180, 30, "Button in Container")
  Button1\EventProcedure = @Button1Events()
  Button1\ParentContainer = 123
  SetGadgetData(2, @Button1)
CloseGadgetList()
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      *GadgetPtr = GetGadgetData(EventGadget())
      If *GadgetPtr <> 0
        CallFunctionFast(*GadgetPtr\EventProcedure, *GadgetPtr)
      EndIf    
  EndSelect
Until appQuit
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: How to find the Container Gadget?

Post by said »

@BorisTheOld thank you, very helpful
Post Reply