Page 1 of 1

absolute position of a gadget on a panelgadget

Posted: Fri Mar 10, 2006 12:06 am
by blueznl
gadgets are drawn relative to the window 0,0 coordinate, now let's assume i have a panelgadget and i have drawn gadgets on top of that panel gadget... how do i find their real coordinates in relation to the window 0,0 position?

i have no clue... anybody?

Posted: Fri Mar 10, 2006 12:47 am
by netmaestro
Relative to the window position, this should do:

Code: Select all

 If OpenWindow(0, 0, 0, 322, 220,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"PanelGadget") And CreateGadgetList(WindowID(0))
    PanelGadget     (0, 8, 8, 306, 203)
      AddGadgetItem (0, -1,"Panel 2")
        ButtonGadget(1, 100, 110, 80, 24,"Button 1")
        Debug GadgetX(0)+GadgetX(1)
        Debug GadgetY(0)+GadgetY(1)
    CloseGadgetList()
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Relative to the desktop, add the WindowX() and WindowY() in there too.

Posted: Fri Mar 10, 2006 5:35 pm
by Trond
Netmaestro forgot to account for borders.

Posted: Fri Mar 10, 2006 5:37 pm
by netmaestro
Yes, I remembered later but I was too lazy to post an edit!

Posted: Sat Mar 11, 2006 8:49 pm
by blueznl
thx, but euh... well, how to get the bordersize then of a panelgadget then?

Posted: Sat Mar 11, 2006 9:29 pm
by Edwin Knoppert
You can calculate everything using GetWindowRect_() and ClientToScreen_() API's.

Usually you subract the rect from the point.

Like Left = PA.X - R.Left etc..

Posted: Sun Mar 12, 2006 9:44 am
by blueznl
ah yes, thank you edwin, i actually used getwinrect_() in the past, but forgot to apply it to this case :oops:

Re: absolute position of a gadget on a panelgadget

Posted: Mon Apr 09, 2012 9:45 pm
by Polo
Hello,

Basically I have the same question - but I need a crossplatform solution :)
This can't be achieved through the solution proposed by netmaestro, as it doesn't take into account the panel menu height.

Does anyone know how to achieve this?

Re: absolute position of a gadget on a panelgadget

Posted: Mon Apr 09, 2012 10:10 pm
by ts-soft

Code: Select all

EnableExplicit

Define posx, posy
If OpenWindow(0, 0, 0, 322, 220, "PanelGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  PanelGadget(0, 8, 8, 306, 203)
  AddGadgetItem(0, -1,"Panel 2")
  ButtonGadget(1, 100, 110, 80, 24,"Button 1")
  CloseGadgetList()
  posx = GadgetX(0) + GadgetX(1) + 3
  posy = GadgetY(0) + GadgetY(1) + GetGadgetAttribute(0, #PB_Panel_TabHeight) + 1
  ButtonGadget(2, posx, posy, 80, 24, "Button 2")
  HideGadget(2, #True)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            HideGadget(0, #True)
            HideGadget(2, #False)
          Case 2
            HideGadget(0, #False)
            HideGadget(2, #True)
        EndSelect
    EndSelect
  ForEver
EndIf

Re: absolute position of a gadget on a panelgadget

Posted: Mon Apr 09, 2012 10:58 pm
by Polo
Thanks, didn't know there was that attribute. I wanted some "universal" solution that worked without having to know which is the parent gadget (as this can't be retrieved programmatically).
Anyway I found a solution, as I use the canvas gadget I just compare the mouse position returned by the gadget to the windows position, minus the gadget position, and that does what I want :)

Re: absolute position of a gadget on a panelgadget

Posted: Tue Apr 10, 2012 9:36 pm
by Shardik
@ts-soft,

Thomas, you should NEVER assume that source code is cross-platform
only because it doesn't use API functions. Nothing beats a real test... :mrgreen:

I had to modify your code to be indeed cross-platform and verified it in
Windows 7 x86, andLinux/Kubuntu 9.04 x86, Linux Mint 12 x86 and
Mac OS X 10.6.8 (Snow Leopard):

Code: Select all

EnableExplicit

Define posx, posy
If OpenWindow(0, 0, 0, 322, 220, "PanelGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  PanelGadget(0, 8, 8, 306, 203)
  AddGadgetItem(0, -1,"Panel 2")
  ButtonGadget(1, 100, 110, 80, 24,"Button 1")
  CloseGadgetList()

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      posx = GadgetX(0) + GadgetX(1)
      posy = GadgetY(0) + GadgetY(1)
    CompilerCase #PB_OS_MacOS
      posx = GadgetX(0) + GadgetX(1) + 3
      posy = GadgetY(0) + GadgetY(1) + GetGadgetAttribute(0, #PB_Panel_TabHeight)
    CompilerCase #PB_OS_Windows
      posx = GadgetX(0) + GadgetX(1) + 3
      posy = GadgetY(0) + GadgetY(1) + GetGadgetAttribute(0, #PB_Panel_TabHeight) + 1
  CompilerEndSelect

  ButtonGadget(2, posx, posy, 80, 24, "Button 2")
  HideGadget(2, #True)

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            HideGadget(0, #True)
            HideGadget(2, #False)
          Case 2
            HideGadget(0, #False)
            HideGadget(2, #True)
        EndSelect
    EndSelect
  ForEver
EndIf