Saturday, April 3, 2010

Saving window position with FXRegistry and using those settings at startup

In my last post I described my discovery of where the FXRegistry settings are saved in the Windows Registry.  I used the 'imageviewer.rb' sample/example program code to help me figure out how to read/write these settings.

I started off by simply using the x, y, width, height settings in my test app.  I found that the width and height settings were getting saved and retrieved correctly because the window size (when the app started) always matched what it was when it last closed. (good)  However, the window always started in the middle of the screen. (not so good)  Why is that?

Many frustrating hours later of searching and tinkering, I eventually thought to start playing with the "show( PLACEMENT_SCREEN )" line of code in the application.  That was it!

If you read the FXTopWindow API doc, you'll see a description of the Initial window placement options.  In particular, I found these three interesting:
  1. PLACEMENT_SCREEN - place it centered on the screen
  2. PLACEMENT_VISIBLE - place window to be fully visible
  3. PLACEMENT_DEFAULT - place it at the default size and location
So, I replaced the 'show' command option in imageviewer.rb and here's what I noticed. (BTW, if you try this with the same sample program make sure you  quit the program using CTRL+Q. Otherwise the settings won't get saved!)

The imageviewer code includes PLACEMENT_SCREEN, and regardless of what 'position' settings you quit the app with, it will always open in the centre of the screen - just as advertised.

I changed the line to use PLACEMENT_DEFAULT, and the window now opened exactly where I left it when it closed.  Yay!  That's exactly what I was expecting the code to do but it wasn't.  Anywhere in the primary or secondary monitor or even with the window app partially off the screen completely - it keeps appearing exactly where it last was.

Then my 'tester' brain kicked in (because I'm a tester by trade, not a programmer) and I wondered if there was a way to prevent a user from exiting the app off-screen (i.e. where you can't see most of the app window at all) and having it open in the same spot, partially hidden off screen?  It seemed like a lot of icky code to check the x & y coordinates and enforce some kind of 'good' placement settings.  And it gets ickier when you consider that every monitor has different resolution settings, and multiple monitors and all that bother.  blah.

So I tried PLACEMENT_VISIBLE.  This one was neat.  When I exited the app anywhere on screen, it opened the next time exactly where I left it. (good)  Then I tried exiting the app with the window partially or mostly hidden off screen, and when I start it the next time it pops on screen close to where it was, except I can see the whole window -- i.e. it's "visible".  Therefore, working as advertised! (good!)

I moved the app to my second monitor and exited the app and it appeared in the second monitor too.  The difference between this setting and PLACEMENT_DEFAULT is that with 'default' I can save the coordinates off-screen and that's where the window will next appear, but with 'visible' the window always appears in a way where you can see the whole app UI.

Cool!  Problem solved.  I don't have to write and maintain a whole bunch of complex code to idiot-proof a minor, undocumented feature of my app.  =)

The only thing left is to hopefully update the imageviewer.rb code so that it includes a different show/placement line so that it uses the x/y position settings it saves.  I'll forward this note to Lyle to see what he thinks.

Cheers!

No comments:

Post a Comment