diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2017-08-09 22:50:26 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2017-08-09 22:50:26 +0200 |
| commit | 4013d1a167850bb1649d592b24d03f63e19338ef (patch) | |
| tree | 4fd38f1b8f1bb63c0d4a22756c0ad7ee4b8009ca /src | |
| parent | 5f760687a4e81edf3dc4011129688a2eddf8b697 (diff) | |
Remember main window size
Diffstat (limited to 'src')
| -rw-r--r-- | src/gui_gtk.cc | 49 | ||||
| -rw-r--r-- | src/gui_main.hh | 3 | ||||
| -rw-r--r-- | src/gui_qt.cc | 42 | ||||
| -rw-r--r-- | src/monitor-gui.cc | 19 |
4 files changed, 112 insertions, 1 deletions
diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index 6913921..96fa7a0 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -597,6 +597,19 @@ public: Looper* create_looper() override; + void resize(uint32_t width, uint32_t height) override { + if (width == width_ && height_ == height) return; + width_ = width; + height_ = height; + + auto win = window(); + if (win) { + gtk_window_resize(win, width_, height_); + } else { + notify_resize(); + } + } + void set_menu(GuiMenu* menu) override { assert(menu); menu_ = menu; @@ -780,6 +793,27 @@ public: return ret_file; } + void check_size() { + auto win = window(); + if (!win) { + assert(false); + return; + } + gint width, height; + gtk_window_get_size(win, &width, &height); + if (width < 0 || height < 0) { + assert(false); + return; + } + if (static_cast<uint32_t>(width) == width_ + && static_cast<uint32_t>(height) == height_) { + return; + } + width_ = width; + height_ = height; + notify_resize(); + } + private: bool notify_about_to_exit() { auto it = observers_.notify(); @@ -789,6 +823,13 @@ private: return true; } + void notify_resize() { + auto it = observers_.notify(); + while (it.has_next()) { + it.next()->resize(width_, height_); + } + } + Config* config() override { return config_.get(); } @@ -1914,6 +1955,12 @@ G_DEFINE_TYPE(MainApp, main_app, GTK_TYPE_APPLICATION); G_DEFINE_TYPE(MainAppWindow, main_app_window, GTK_TYPE_APPLICATION_WINDOW); +gboolean configure_event(GtkWidget*, GdkEvent*, gpointer data) { + auto main = reinterpret_cast<GtkGuiMain*>(data); + main->check_size(); + return false; +} + void top_selection_changed(GtkTreeSelection* selection, gpointer data) { auto main = reinterpret_cast<GtkGuiMain*>(data); GtkTreeIter iter; @@ -1934,6 +1981,8 @@ MainAppWindow* main_app_window_new(MainApp *app) { "application", app, nullptr)); gtk_window_set_default_size(GTK_WINDOW(ret), app->main_->default_width(), app->main_->default_height()); + g_signal_connect(G_OBJECT(ret), "configure-event", + G_CALLBACK(configure_event), app->main_); auto box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); ret->paned_ = gtk_paned_new(GTK_ORIENTATION_VERTICAL); auto listmodel = app->main_->listmodel(); diff --git a/src/gui_main.hh b/src/gui_main.hh index ab8499d..ca34dee 100644 --- a/src/gui_main.hh +++ b/src/gui_main.hh @@ -24,6 +24,7 @@ public: virtual ~Listener() {} virtual bool about_to_exit(GuiMain* main) = 0; + virtual void resize(uint32_t width, uint32_t height) = 0; virtual void selected_row(GuiMain* main, size_t index) = 0; virtual void lost_selection(GuiMain* main) = 0; virtual void open(GuiMain* main, std::string const& file) = 0; @@ -36,6 +37,8 @@ public: uint32_t width, uint32_t height); virtual Looper* create_looper() = 0; + virtual void resize(uint32_t width, uint32_t height) = 0; + virtual void set_menu(GuiMenu* menu) = 0; virtual GuiMenu* menu() const = 0; diff --git a/src/gui_qt.cc b/src/gui_qt.cc index 4ad04e8..16102d6 100644 --- a/src/gui_qt.cc +++ b/src/gui_qt.cc @@ -556,6 +556,18 @@ public: observers_.erase(listener); } + void resize(uint32_t width, uint32_t height) override { + if (width == width_ && height == height_) return; + width_ = width; + height_ = height; + auto w = widget(); + if (w) { + w->resize(width_, height_); + } else { + notify_resize(); + } + } + void set_title(std::string const& title) override { title_ = title; QtGuiWindow::set_title(title); @@ -641,6 +653,13 @@ private: return true; } + void notify_resize() { + auto it = observers_.notify(); + while (it.has_next()) { + it.next()->resize(width_, height_); + } + } + void notify_selected_row(size_t row) { auto it = observers_.notify(); while (it.has_next()) { @@ -1802,6 +1821,22 @@ private: QProgressBar* progress_; }; +class QNotifyResizeMainWindow : public QMainWindow { +public: + typedef std::function<void(uint32_t,uint32_t)> ResizeCallback; + + explicit QNotifyResizeMainWindow(ResizeCallback const& callback) + : callback_(callback) { + } + + void resizeEvent(QResizeEvent* event) override { + callback_(event->size().width(), event->size().height()); + } + +private: + ResizeCallback const callback_; +}; + bool QtGuiMain::run(int argc, char** argv) { QApplication app(argc, argv); app.setApplicationName(QString::fromStdString(title_)); @@ -1816,7 +1851,12 @@ bool QtGuiMain::run(int argc, char** argv) { parser.process(app); app.setStyleSheet("QStatusBar::item { border: 0px }"); - main_.reset(new QMainWindow()); + main_.reset(new QNotifyResizeMainWindow( + [=](uint32_t width, uint32_t height) { + width_ = width; + height_ = height; + notify_resize(); + })); main_->setWindowTitle(QString::fromStdString(title_)); center_.reset(new QWidget()); layout_.reset(new SizeHintLayout(center_.get(), QSize(width_, height_))); diff --git a/src/monitor-gui.cc b/src/monitor-gui.cc index eb1f34d..6805814 100644 --- a/src/monitor-gui.cc +++ b/src/monitor-gui.cc @@ -695,6 +695,18 @@ public: selection_(0), modified_(false) { + auto size = main_->config()->get("main-size", nullptr); + if (size) { + char* end = nullptr; + auto w = strtoul(size, &end, 10); + if (w > 0 && end && *end == 'x') { + auto h = strtoul(end + 1, &end, 10); + if (h > 0 && end && *end == '\0') { + main_->resize(w, h); + } + } + } + pem_filter_.emplace_back(); pem_filter_.back().name = "PEM (*.pem)"; pem_filter_.back().masks.emplace_back("*.pem"); @@ -1008,6 +1020,13 @@ public: return true; } + void resize(uint32_t width, uint32_t height) override { + char tmp[50]; + snprintf(tmp, sizeof(tmp), "%lux%lu", static_cast<unsigned long>(width), + static_cast<unsigned long>(height)); + main_->config()->set("main-size", std::string(tmp)); + } + void selected_row(GuiMain* main, size_t index) override { assert(main_.get() == main); if (selection_content_) { |
