Page 1 of 1

How to find the Container Gadget?

Posted: Wed Nov 07, 2012 10:02 am
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

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 10:55 am
by NicknameFJ
Hello Said,

maybe GetParent_(hwnd)

will help.

NicknameFJ

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 11:25 am
by Polo
That's a missing feature in my opinion. GetGadgetParent/GetGadgetChildren would be really useful :)

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 11:52 am
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.

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 11:57 am
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

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 12:04 pm
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!

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 12:16 pm
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:

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 12:23 pm
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:

Re: How to find the Container Gadget?

Posted: Wed Nov 07, 2012 6:09 pm
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.

Re: How to find the Container Gadget?

Posted: Thu Nov 08, 2012 9:34 am
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!

Re: How to find the Container Gadget?

Posted: Thu Nov 08, 2012 6:15 pm
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

Re: How to find the Container Gadget?

Posted: Sat Nov 10, 2012 2:31 am
by said
@BorisTheOld thank you, very helpful