Restore frame size when app startup Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Restore frame size when app startup

Post by benedicte »

Hello,

I am trying to make my app size (and maybe position too) restored when restarting.
For the moment, I save 2 data:
* if the frame is maximized,
* the height/width of the frame before it was maximized (these info are correct at saving time).

When I restore my app, I first set the size of the frame (in all cases), then I call Maximize(true) if the frame was maximized.

Everything seems to work except when I un-maximize the frame (by clicking on the "maximize" button, or double click on the app title bar). It does not restore to the size I set.

Is this normal? How can I force the "default restore" size when un-maximizing?

Here is a piece of my code:

Code: Select all

// header
class MyFrame: public wxFrame
{
public:
// constructor
   MyFrame (/*params*/);

// event handlers
   void OnSize (wxSizeEvent &evt);

// ...

protected:
   wxSize  frame_size;
};

// implementation
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
   EVT_SIZE (MyFrame::OnSize)
// ...
END_EVENT_TABLE ()

MyFrame::MyFrame (/*params*/)
{
// ...

   if (app_options.last_size is not set)
      frame_size = wxSize (800, 600);
   else
      frame_size = app_options.last_size;

   SetSize (frame_size);

   if (app_options.was_maximized)
      Maximize (true);

// ...
}

MyFrame::~MyFrame
{

   app_options.last_size = frame_size;
   app_options.was_maximized = IsMaximized ();

// ...
}

void MyFrame::OnSize (wxSizeEvent &evt)
{
   if (!IsMaximized())
      frame_size = evt.GetSize();

   evt.Skip();
   wxFrame::OnSize (evt);
}

I use wxWidgets 2.6.3 on Windows and GTK(Linux, Solaris, maybe MacOS also).
I also use wxAUI, 0.9.2 with a patch of mine (to allow size events to be processed by my frame, because the frame manager eats them).

Thanks for your help.
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

Nobody knows?
User avatar
doublemax
Moderator
Moderator
Posts: 19164
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

i just tested it with one of my applications and it works like you expected.

Is your MyFrame derived from wxFrame? When you store the frame size in the OnSize() handler, did you verify that you don't save the maximized size there?
Use the source, Luke!
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

my main frame derives from wxFrame.

I am sure to store the correct frame size (ie the last one before the frame was maximized).
User avatar
doublemax
Moderator
Moderator
Posts: 19164
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

very strange. I just put a SetSize(...) and Maximize() call into my wxFrame ctor and it worked right away.

To which size does your frame restore when you unmaximize it?
Does it restore to the correct position?

Code: Select all

if (app_options.last_size is not set)
i assume this is pseudo code?
Use the source, Luke!
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

doublemax wrote:very strange. I just put a SetSize(...) and Maximize() call into my wxFrame ctor and it worked right away.

To which size does your frame restore when you unmaximize it?
Does it restore to the correct position?

Code: Select all

if (app_options.last_size is not set)
i assume this is pseudo code?
Yes, it's pseudo code. It is just to handle correctly the first app startup, when config is empty.

Let's explain with an example:

1/ The config contains the "last size" 800*600, not maximized. When I start the app, the size is correct. I do not restore the frame position for the moment, so Windows moves a bit the frame each time I launch the app.
2/ I maximize the frame. the "last size" is still 800*600. I close the app. The config contains the correct "last size" and the maximized flag to "true".
3/ I launch the app again. I set the size to 800*600, then I maximize the frame (in the frame constructor). I un-maximize the frame manually dy double clicking on the app bar. The new frame size is the same as the screen size, but the position changed a bit. The "last size" was still "800*600" when processing the EVT_SIZE for un-maximizing, but it was updated to let's say "1024*768" during:

Code: Select all

void MyFrame::OnSize (wxSizeEvent &evt)
{
   if (!IsMaximized())
      frame_size = evt.GetSize();

   evt.Skip();
   wxFrame::OnSize (evt);
}
because "IsMaximized()" returns false.

I use 2 displays on my computer, but I don't think this may have an impact on the frame restoring.
I tried to step into the "Maximize()" code, to check where the "last size" was saved... didn't find. Maybe it's platform-dependant.
micros
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Sat Mar 18, 2006 10:41 am
Location: Ustek, Bohemia

Post by micros »

Hi, I just wanted to do the same thing, and faced the same problem :(
Now I come with my piece, which seams to have solved it... Practically only swapped the SetSize() and Maximize() calls in frame construction. I guess the problem was that Maximize() (either the one called upon startup or the one triggered by clicking the title bar) tried to guess the restored size from somewhere, but not the size you set.

Here's my code. The fact that I put it in onCreate doesn't matter, I just prefer onCreate to putting everything in the ctor (well, my preference changes from week to week).

Code: Select all

void
frameMain::onCreate(wxWindowCreateEvent& event)
{
  wxConfigBase *conf = wxConfigBase::Get();
  wxSize size = GetSize(); // get some default

  if (conf->Read(wxT("ui/frame.maximized"), 0L)) {
    // this only sets wxTopLevelWindow::m_maximizeOnShow
    Maximize(true);
  }
  // these won't overwrite the values if the keys are not present
  conf->Read(wxT("ui/frame.width"), &size.x);
  conf->Read(wxT("ui/frame.height"), &size.y);
  SetSize(size);
}

void
frameMain::onSize(wxSizeEvent& event)
{
  wxConfigBase *conf = wxConfigBase::Get();
  conf->Write(wxT("ui/frame.maximized"), IsMaximized());
  if (!IsMaximized()) {
    conf->Write(wxT("ui/frame.width"), event.GetSize().x);
    conf->Write(wxT("ui/frame.height"), event.GetSize().y);
  }
  event.Skip();
}
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

You won your points :!:

I was getting crazy with this silly thing.