Convenience class to remember window position

23 views
Skip to first unread message

Kenneth Porter

unread,
Nov 3, 2022, 11:51:18 AM11/3/22
to wxWidgets Users
wxFileHistory is a convenience class to remember recently used files in the
registry. Is there a similar class to remember an app's main window
position and size?

Igor Korot

unread,
Nov 3, 2022, 12:01:29 PM11/3/22
to wx-u...@googlegroups.com
Hi, Kenneth,
You are probably looking for wxPersistentManager.

Thank you.

>
> --
> Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
> ---
> You received this message because you are subscribed to the Google Groups "wx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/19CC394937870DD68FF84883%40%5B192.168.69.69%5D.

Kenneth Porter

unread,
Nov 3, 2022, 12:36:27 PM11/3/22
to wx-u...@googlegroups.com
On 11/3/2022 9:01 AM, Igor Korot wrote:
> You are probably looking for wxPersistentManager.

Thanks. This looks promising.

https://docs.wxwidgets.org/3.0/overview_persistence.html

I realized I wanted it when I got annoyed with the behavior of the
Facebook Messenger app for Windows, which always opens in the center of
the screen and is pretty small on a 40 inch 4k display. I realized that
its sin is my own and I want to fix it in my own apps. ;)


Kenneth Porter

unread,
Nov 4, 2022, 6:15:14 PM11/4/22
to wx-u...@googlegroups.com
This was pretty easy to add. I invoke this before the Show() for my main
window:

wxPersistentRegisterAndRestore(frame, "MainFrame");

One problem remains: If I closed the window while it was off-screen, it
gets restored that way. If I change the desktop size to something
smaller, this can leave the restored window where it can't be
manipulated and I have to remember how to blindly move an off-screen
window. So is there a convenience function for repositioning a window so
all four corners are visible? I know that can be tricky with a
multi-monitor setup. I guess only top-left and bottom-right need to be
visible (allowing for the window to straddle two monitors of different
sizes).

I see I also should add a "View | Default" menuitem to restore the frame
to "factory settings". (I don't see an obvious method for discarding the
persisted settings so that the next startup uses the defaults, but I
don't see much use for that except when debugging the default layout.
For now I can use regedit to kill the appropriate tree.)


Igor Korot

unread,
Nov 4, 2022, 7:11:34 PM11/4/22
to wx-u...@googlegroups.com
Kwenneth,

On Fri, Nov 4, 2022 at 5:15 PM Kenneth Porter <sh...@sewingwitch.com> wrote:
>
> This was pretty easy to add. I invoke this before the Show() for my main
> window:
>
> wxPersistentRegisterAndRestore(frame, "MainFrame");
>
> One problem remains: If I closed the window while it was off-screen, it
> gets restored that way. If I change the desktop size to something
> smaller, this can leave the restored window where it can't be
> manipulated and I have to remember how to blindly move an off-screen
> window. So is there a convenience function for repositioning a window so
> all four corners are visible? I know that can be tricky with a
> multi-monitor setup. I guess only top-left and bottom-right need to be
> visible (allowing for the window to straddle two monitors of different
> sizes).

Is it reproducible in the persist sample?

You are working with the latest release/HEAD, right?
I think this class is DPI aware and should not behave this way...

Thank you.

>
> I see I also should add a "View | Default" menuitem to restore the frame
> to "factory settings". (I don't see an obvious method for discarding the
> persisted settings so that the next startup uses the defaults, but I
> don't see much use for that except when debugging the default layout.
> For now I can use regedit to kill the appropriate tree.)
>
>
> --
> Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
> ---
> You received this message because you are subscribed to the Google Groups "wx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/bd64a1fb-1f89-b14c-0306-ddad86cdbae3%40sewingwitch.com.

Kenneth Porter

unread,
Nov 4, 2022, 7:51:30 PM11/4/22
to wx-u...@googlegroups.com
On 11/4/2022 4:11 PM, Igor Korot wrote:
> Is it reproducible in the persist sample?
>
> You are working with the latest release/HEAD, right?
> I think this class is DPI aware and should not behave this way...

I'm a few commits back, but I don't recall seeing any commits addressing
this. I don't think it's a bug that the persistence classes need to address.

If I close a persisted Frame while 1/4 of it is off the bottom left
corner (so I can still hit the X close button), the Frame opens at that
same position and size when the app is restarted. Is there something in
the layout-and-fit engine that should be preventing that?

I see the widgets sample does persistence. I'll see what happens with it...



Vadim Zeitlin

unread,
Nov 8, 2022, 8:28:21 AM11/8/22
to wx-u...@googlegroups.com
On Fri, 4 Nov 2022 15:15:04 -0700 Kenneth Porter wrote:

KP> This was pretty easy to add.

Yes, that's the whole point -- this class makes it completely trivial to
retain the geometry and other UI state between program runs. It's a bit sad
that many people are still not aware of its existence, I should advertise
it even more than I did...

KP> I invoke this before the Show() for my main
KP> window:
KP>
KP> wxPersistentRegisterAndRestore(frame, "MainFrame");
KP>
KP> One problem remains: If I closed the window while it was off-screen, it
KP> gets restored that way.

Hmm, this is not supposed to happen, see the code at

https://github.com/wxWidgets/wxWidgets/blob/fec8c06a53f5f2eb65e3328eadddd22334e7971f/include/wx/private/tlwgeom.h#L127-L139

KP> So is there a convenience function for repositioning a window so
KP> all four corners are visible?

No, maybe it should indeed be added to wxTLW itself and be called from the
function above instead of being done just there, as it could be more
generally useful.

KP> I see I also should add a "View | Default" menuitem to restore the frame
KP> to "factory settings". (I don't see an obvious method for discarding the
KP> persisted settings so that the next startup uses the defaults, but I
KP> don't see much use for that except when debugging the default layout.
KP> For now I can use regedit to kill the appropriate tree.)

Yes, some way to forget (all?) the stored settings might be useful to have
too, e.g. after a major version upgrade. PRs adding it would be welcome!

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Reply all
Reply to author
Forward
0 new messages