2011-07-27 07:36:02 +02:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file persist_location.cpp
|
|
|
|
/// @see persist_location.h
|
|
|
|
/// @ingroup utility
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "persist_location.h"
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <wx/dialog.h>
|
2011-07-27 19:21:39 +02:00
|
|
|
#include <wx/display.h>
|
2011-07-27 07:36:02 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-03 00:42:37 +02:00
|
|
|
PersistLocation::PersistLocation(wxDialog *dialog, std::string options_prefix, bool size_too)
|
2011-07-27 07:36:02 +02:00
|
|
|
: x_opt(OPT_SET(options_prefix + "/Last/X"))
|
|
|
|
, y_opt(OPT_SET(options_prefix + "/Last/Y"))
|
2012-05-03 00:42:37 +02:00
|
|
|
, w_opt(size_too ? OPT_SET(options_prefix + "/Last/Width") : 0)
|
|
|
|
, h_opt(size_too ? OPT_SET(options_prefix + "/Last/Height") : 0)
|
2011-07-27 07:36:02 +02:00
|
|
|
, maximize_opt(OPT_SET(options_prefix + "/Maximized"))
|
2012-02-01 01:48:07 +01:00
|
|
|
, dialog(dialog)
|
2011-07-27 07:36:02 +02:00
|
|
|
{
|
|
|
|
int x = x_opt->GetInt();
|
|
|
|
int y = y_opt->GetInt();
|
|
|
|
if (x == -1 && y == -1)
|
|
|
|
dialog->CenterOnParent();
|
|
|
|
else {
|
|
|
|
// First move to the saved place so that it ends up on the right monitor
|
|
|
|
dialog->Move(x, y);
|
|
|
|
|
2012-05-03 00:42:37 +02:00
|
|
|
if (size_too && w_opt->GetInt() > 0 && h_opt->GetInt() > 0)
|
|
|
|
dialog->SetSize(w_opt->GetInt(), h_opt->GetInt());
|
|
|
|
|
2011-07-27 07:36:02 +02:00
|
|
|
int display_index = wxDisplay::GetFromWindow(dialog);
|
|
|
|
|
|
|
|
// If it's moved offscreen center on the parent and try again
|
|
|
|
if (display_index == wxNOT_FOUND) {
|
|
|
|
dialog->CenterOnParent();
|
|
|
|
display_index = wxDisplay::GetFromWindow(dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's still offscreen just give up
|
|
|
|
if (display_index == wxNOT_FOUND) return;
|
|
|
|
|
|
|
|
wxRect display_area = wxDisplay(display_index).GetClientArea();
|
|
|
|
wxSize dialog_size = dialog->GetSize();
|
|
|
|
|
|
|
|
// Ensure that the top-left corner is onscreen
|
|
|
|
if (x < display_area.x) x = display_area.x;
|
|
|
|
if (y < display_area.y) y = display_area.y;
|
|
|
|
|
|
|
|
// Ensure that the bottom-right corner is onscreen as long as doing so
|
|
|
|
// wouldn't force the top-left corner offscreen
|
|
|
|
if (x + dialog_size.x > display_area.GetRight())
|
|
|
|
x = std::max(display_area.x, display_area.GetRight() - dialog_size.x);
|
|
|
|
if (y + dialog_size.y > display_area.GetBottom())
|
|
|
|
y = std::max(display_area.y, display_area.GetBottom() - dialog_size.y);
|
|
|
|
|
|
|
|
dialog->Move(x, y);
|
|
|
|
}
|
2012-02-07 02:21:56 +01:00
|
|
|
|
2012-02-07 02:22:06 +01:00
|
|
|
dialog->Bind(wxEVT_MOVE, &PersistLocation::OnMove, this);
|
|
|
|
|
2012-05-03 00:42:37 +02:00
|
|
|
dialog->Bind(wxEVT_SIZE, &PersistLocation::OnSize, this);
|
|
|
|
if ((dialog->GetWindowStyle() & wxMAXIMIZE_BOX) && maximize_opt->GetBool())
|
|
|
|
dialog->Maximize();
|
2011-07-27 07:36:02 +02:00
|
|
|
}
|
|
|
|
|
2012-02-07 02:21:56 +01:00
|
|
|
void PersistLocation::OnMove(wxMoveEvent &e) {
|
2012-02-01 01:48:07 +01:00
|
|
|
wxPoint pos = dialog->GetPosition();
|
2011-07-27 07:36:02 +02:00
|
|
|
x_opt->SetInt(pos.x);
|
|
|
|
y_opt->SetInt(pos.y);
|
2012-02-07 02:21:56 +01:00
|
|
|
e.Skip();
|
2011-07-27 07:36:02 +02:00
|
|
|
}
|
|
|
|
|
2012-02-07 02:21:56 +01:00
|
|
|
void PersistLocation::OnSize(wxSizeEvent &e) {
|
|
|
|
maximize_opt->SetBool(dialog->IsMaximized());
|
2012-05-03 00:42:37 +02:00
|
|
|
if (w_opt) {
|
|
|
|
w_opt->SetInt(dialog->GetSize().GetWidth());
|
|
|
|
h_opt->SetInt(dialog->GetSize().GetHeight());
|
|
|
|
}
|
2012-02-07 02:21:56 +01:00
|
|
|
e.Skip();
|
2011-07-27 07:36:02 +02:00
|
|
|
}
|