How do I find a windows position?

I seem to be unable to figure out how to get a TGMainFrame’s screen position. Neither GetWMPosition(Int_t&,Int_t&) nor GetX()/GetY() return anything useful. The following code is a small example of how i try to get the position:

[code]#include <TApplication.h>
#include <TGFrame.h>
using std::cout;
using std::endl;

TGMainFrame* mainframe;

int main(int argc, char** argv)
TApplication runroot(“PositionTest”, &argc, argv);
mainframe = new TGMainFrame();
mainframe->Connect(“ProcessedEvent(Event_t*)”,NULL,NULL, “printPos(Event_t*)”);
mainframe->Connect(“CloseWindow()”,“TApplication”,gApplication, “Terminate()”);
return 0;

void printPos(Event_t* event)
cout << "X: " << mainframe->GetX() << endl;
cout << "Y: " << mainframe->GetY() << endl;
Int_t x, y;
cout << "WM X: " << x << endl;
cout << "WM Y: " << y << endl;
The ouput always looks likeX: 0 Y: 0 WM X: -1 WM Y: -1 no matter where the window is positioned.

Using ROOT-2.6.27 and have tested this in both Metacity and Openbox (Linux).

Grateful of any help.


You have to translate the Window (TGMainFrame) coordinates into the Root (desktop) coordinates:

void printPos(Event_t* event) { Window_t wdummy; Int_t x, y; gVirtualX->TranslateCoordinates(mainframe->GetId(), gClient->GetDefaultRoot()->GetId(), mainframe->GetX(), mainframe->GetY(), x, y, wdummy); cout << "X: " << x << endl; cout << "Y: " << y << endl; }
You can also use 0,0, instead of GetX() and GetY(), since they are always 0.
And note it doesn’t take into account the Window decoration (it is WM dependent).
So to get the real position (top left coner of the decoration), you should try something like this:

void printPos(Event_t* event) { Window_t wdummy; Int_t x, y; WindowAttributes_t attr; gVirtualX->TranslateCoordinates(mainframe->GetId(), gClient->GetDefaultRoot()->GetId(), 0, 0, x, y, wdummy); cout << "X: " << x << endl; cout << "Y: " << y << endl; gVirtualX->GetWindowAttributes(mainframe->GetId(), attr); cout << "Real X: " << (x - attr.fX) << endl; cout << "Real Y: " << (y - attr.fY) << endl; }
Cheers, Bertrand.

Thank you!

You’re welcome :slight_smile: