gail_p
asked on
How do I keep track of a windows location
I am trying to store the location of a window in my preferences using a Rect. Anyone have any sample code of saving a windows location and then placing it in the same spot when you open the window.
ASKER
I had tired the following code and it works fine when I have only one window open but if I have more than one window open the window reopens at 0,0 - under the menu. Any Suggestions. I increased the points by 50 but I only had 200. Thats why I was stingy. ;}
if( gMovedSplashWindow ) {
myPrefs.prefChanged = true;
SetPort(&gSplashDialogPtr) ;
theWindowPeek = (DocumentPeek)gSplashDialo gPtr;
/* myPrefs.splashWindRect.top = theWindowPeek->docwindow.p ort.portRe ct.top;
myPrefs.splashWindRect.bot tom = theWindowPeek->docwindow.p ort.portRe ct.bottom;
myPrefs.splashWindRect.lef t = theWindowPeek->docwindow.p ort.portRe ct.left;
myPrefs.splashWindRect.rig ht = theWindowPeek->docwindow.p ort.portRe ct.right;
LocalToGlobal( &topLeft(myPrefs.splashWin dRect));
LocalToGlobal( &botRight(myPrefs.splashWi ndRect));* /
gWindowPoint.v = theWindowPeek->docwindow.p ort.portRe ct.top;
gWindowPoint.h = theWindowPeek->docwindow.p ort.portRe ct.left;
LocalToGlobal(&gWindowPoin t);
myPrefs.splashWindRect.top = gWindowPoint.v;
myPrefs.splashWindRect.lef t = gWindowPoint.h;
gWindowPoint.v = theWindowPeek->docwindow.p ort.portRe ct.bottom;
gWindowPoint.h = theWindowPeek->docwindow.p ort.portRe ct.right;
LocalToGlobal(&gWindowPoin t);
myPrefs.splashWindRect.bot tom = gWindowPoint.v;
myPrefs.splashWindRect.rig ht = gWindowPoint.h;
}
if( gMovedSplashWindow ) {
myPrefs.prefChanged = true;
SetPort(&gSplashDialogPtr)
theWindowPeek = (DocumentPeek)gSplashDialo
/* myPrefs.splashWindRect.top
myPrefs.splashWindRect.bot
myPrefs.splashWindRect.lef
myPrefs.splashWindRect.rig
LocalToGlobal( &topLeft(myPrefs.splashWin
LocalToGlobal( &botRight(myPrefs.splashWi
gWindowPoint.v = theWindowPeek->docwindow.p
gWindowPoint.h = theWindowPeek->docwindow.p
LocalToGlobal(&gWindowPoin
myPrefs.splashWindRect.top
myPrefs.splashWindRect.lef
gWindowPoint.v = theWindowPeek->docwindow.p
gWindowPoint.h = theWindowPeek->docwindow.p
LocalToGlobal(&gWindowPoin
myPrefs.splashWindRect.bot
myPrefs.splashWindRect.rig
}
This is a little verbose; a simple
myPrefs.splashWindRect = theWindowPeek->docwindow.p ort.portRe ct;
LocalToGlobal( &topLeft(myPrefs.splashWin dRect));
LocalToGlobal( &botRight(myPrefs.splashWi ndRect));
really ought to work just fine.
First thing I'd do is check the values in splashWindRect, to make sure that 'docwindow' really is valid when you're saving it. Be nice to see your DocumentPeek definition and the code you're using to set the window position too.
myPrefs.splashWindRect = theWindowPeek->docwindow.p
LocalToGlobal( &topLeft(myPrefs.splashWin
LocalToGlobal( &botRight(myPrefs.splashWi
really ought to work just fine.
First thing I'd do is check the values in splashWindRect, to make sure that 'docwindow' really is valid when you're saving it. Be nice to see your DocumentPeek definition and the code you're using to set the window position too.
This is a little verbose; a simple
myPrefs.splashWindRect = theWindowPeek->docwindow.p ort.portRe ct;
LocalToGlobal( &topLeft(myPrefs.splashWin dRect));
LocalToGlobal( &botRight(myPrefs.splashWi ndRect));
really ought to work just fine.
First thing I'd do is check the values in splashWindRect, to make sure that 'docwindow' really is valid when you're saving it. Be nice to see your DocumentPeek definition and the code you're using to set the window position too.
myPrefs.splashWindRect = theWindowPeek->docwindow.p
LocalToGlobal( &topLeft(myPrefs.splashWin
LocalToGlobal( &botRight(myPrefs.splashWi
really ought to work just fine.
First thing I'd do is check the values in splashWindRect, to make sure that 'docwindow' really is valid when you're saving it. Be nice to see your DocumentPeek definition and the code you're using to set the window position too.
ASKER
I have tried your code but I am still having a problem. I checked to see if the DialogPeek's address is the same as the DialogPtr and it is. Then I checked the contents of the portRect and it is:
top 0
left 0
bottom 140
right 201
this seems to be correct (it is the window size), when I assign this:
myPrefs.splashWindRect = theDialogPeek->window.port .portRect;
everything works but the LocalToGlobal does not change the contents of myPrefs.splashWindRect.
My window opening code looks like this:
gSplashDialogPtr = GetNewDialog( kSplashDialog, nil, (DialogPtr)-1);
if( gSplashDialogPtr != nil) {
SetPort(gSplashDialogPtr);
TextFont(3);
TextSize(9);
MoveWindow(gSplashDialogPt r, myPrefs.splashWindRect.lef t, myPrefs.splashWindRect.top , true );
Draw_Splash();
}
I have replaced the values in the MoveWindow and this works. I am confused as to why this doesn't work. It ends up opening the window at 0,0
top 0
left 0
bottom 140
right 201
this seems to be correct (it is the window size), when I assign this:
myPrefs.splashWindRect = theDialogPeek->window.port
everything works but the LocalToGlobal does not change the contents of myPrefs.splashWindRect.
My window opening code looks like this:
gSplashDialogPtr = GetNewDialog( kSplashDialog, nil, (DialogPtr)-1);
if( gSplashDialogPtr != nil) {
SetPort(gSplashDialogPtr);
TextFont(3);
TextSize(9);
MoveWindow(gSplashDialogPt
Draw_Splash();
}
I have replaced the values in the MoveWindow and this works. I am confused as to why this doesn't work. It ends up opening the window at 0,0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all your help, after stepping through the code I found that a function I use to check for changes was disposing of the window I was trying to get the location of if there was more than one window open.
Thanks Again for all your help.
Thanks Again for all your help.
mPlaylistWindow->GetGlobal
(or for a regular WindowPtr, cast it to a WindowPeek and store the port.portRect of the WindowRecord that points to)
where mPrefs is a structure which includes the Rect "playlistRect" and gets saved and loaded however you save and load your preferences, sounds like you're doing that already.
Once you've loaded your preferences, the main trick is to make sure that the bounds saved make sense on the current screen, whose dimensions might have changed in the meantime. Here's what I use in PowerPlant programs, after creating the window but before showing it, to insist that the saved position leave at least 40 pixels showing in each axis on the current screen:
Rect saved, intersection;
Rect screen = (**(GetGrayRgn())).rgnBBox
saved = mPrefs.playlistRect;
if (!::EmptyRect(&saved))
if (::SectRect(&saved, &screen, &intersection))
if ((intersection.right - intersection.left) > 40)
if ((intersection.bottom - intersection.top) > 40)
mPlaylistWindow->DoSetBoun
(if you're not using PowerPlant, you would use MoveWindow() and SizeWindow() on your WindowPtr, deriving the location/size from the saved Rect)
....and that's about all the advice 50 points is worth I think :)