From 00bb1dc48e8b4f0e607adc1d60b456cac4fbd1e1 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Fri, 28 Jul 2017 23:31:36 +0200 Subject: Add GuiMessage, simple message dialog --- src/gui_gtk.cc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'src/gui_gtk.cc') diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index 0c251a9..062ac4c 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -16,6 +16,7 @@ #include "gui_listmodel.hh" #include "gui_main.hh" #include "gui_menu.hh" +#include "gui_message.hh" #include "gui_statusbar.hh" #include "gui_textwnd.hh" #include "looper.hh" @@ -1501,6 +1502,69 @@ private: bool applied_; }; +class GtkGuiMessage : public virtual GuiMessage, public GtkGuiWindow { +public: + GtkGuiMessage(Type type, std::string const& title, std::string const& text) + : type_(type), title_(title), text_(text), dialog_(nullptr) { + } + + ~GtkGuiMessage() override { + assert(!dialog_); + } + + void set_title(std::string const& title) override { + title_ = title; + GtkGuiWindow::set_title(title); + } + std::string const& title() const override { + return title_; + } + + void* impl() const override { + return dialog_; + } + + void add_listener(GuiMessage::Listener* listener) override { + observers_.insert(listener); + } + + void remove_listener(GuiMessage::Listener* listener) override { + observers_.erase(listener); + } + + void show(GuiWindow* parent) override { + if (dialog_) { + assert(false); + return; + } + GtkMessageType type = GTK_MESSAGE_OTHER; + switch (type_) { + case ERROR: + type = GTK_MESSAGE_ERROR; + break; + case OTHER: + break; + } + dialog_ = gtk_message_dialog_new( + reinterpret_cast(parent->impl()), + GTK_DIALOG_MODAL, + type, + GTK_BUTTONS_CLOSE, + "%s", + text_.c_str()); + gtk_dialog_run(GTK_DIALOG(dialog_)); + gtk_widget_destroy(dialog_); + dialog_ = nullptr; + } + +protected: + Type type_; + std::string title_; + std::string text_; + Observers observers_; + GtkWidget* dialog_; +}; + class GtkGuiTextWindow : public virtual GuiTextWindow, public GtkGuiWindow { public: GtkGuiTextWindow(std::string const& title, uint32_t width, uint32_t height, @@ -2139,6 +2203,13 @@ GuiFormApply* GuiFormApply::create(std::string const& title, return new GtkGuiFormApply(title, text, apply_button, delegate); } +// static +GuiMessage* GuiMessage::create(Type type, + std::string const& title, + std::string const& text) { + return new GtkGuiMessage(type, title, text); +} + // static Looper* GuiMain::createLooper() { return new GtkLooper(); -- cgit v1.2.3-70-g09d2