absolute position of a gadget on a panelgadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

absolute position of a gadget on a panelgadget

Post 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?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Netmaestro forgot to account for borders.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Yes, I remembered later but I was too lazy to post an edit!
BERESHEIT
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

thx, but euh... well, how to get the bordersize then of a panelgadget then?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post 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..
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

ah yes, thank you edwin, i actually used getwinrect_() in the past, but forgot to apply it to this case :oops:
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: absolute position of a gadget on a panelgadget

Post 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?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: absolute position of a gadget on a panelgadget

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: absolute position of a gadget on a panelgadget

Post 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 :)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: absolute position of a gadget on a panelgadget

Post 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
Post Reply