summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2017-07-28 23:31:36 +0200
committerJoel Klinghed <the_jk@yahoo.com>2017-07-28 23:31:36 +0200
commit00bb1dc48e8b4f0e607adc1d60b456cac4fbd1e1 (patch)
tree7e52daf789195124ffaecd8972b51abc7c05221d
parent3eb752a7359f3714e603b36a77514d8f6fd4741c (diff)
Add GuiMessage, simple message dialog
-rw-r--r--src/gui_gtk.cc71
-rw-r--r--src/gui_message.hh38
-rw-r--r--src/gui_qt.cc63
3 files changed, 172 insertions, 0 deletions
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<GtkWindow*>(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<GuiMessage::Listener*> observers_;
+ GtkWidget* dialog_;
+};
+
class GtkGuiTextWindow : public virtual GuiTextWindow, public GtkGuiWindow {
public:
GtkGuiTextWindow(std::string const& title, uint32_t width, uint32_t height,
@@ -2140,6 +2204,13 @@ GuiFormApply* GuiFormApply::create(std::string const& title,
}
// 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();
}
diff --git a/src/gui_message.hh b/src/gui_message.hh
new file mode 100644
index 0000000..c44cce1
--- /dev/null
+++ b/src/gui_message.hh
@@ -0,0 +1,38 @@
+// -*- mode: c++; c-basic-offset: 2; -*-
+
+#ifndef GUI_MESSAGE_HH
+#define GUI_MESSAGE_HH
+
+#include <string>
+#include <vector>
+
+#include "gui_window.hh"
+
+class GuiMessage : public GuiWindow {
+public:
+ class Listener : public GuiWindow::Listener {
+ public:
+ virtual ~Listener() {}
+
+ protected:
+ Listener() {}
+ };
+
+ enum Type {
+ ERROR,
+ OTHER,
+ };
+
+ static GuiMessage* create(Type type, std::string const& title,
+ std::string const& text);
+
+ virtual void add_listener(Listener* listener) = 0;
+ virtual void remove_listener(Listener* listener) = 0;
+
+ virtual void show(GuiWindow* parent) = 0;
+
+protected:
+ GuiMessage() {}
+};
+
+#endif // GUI_MESSAGE_HH
diff --git a/src/gui_qt.cc b/src/gui_qt.cc
index 17eda8f..9ffdfc9 100644
--- a/src/gui_qt.cc
+++ b/src/gui_qt.cc
@@ -46,6 +46,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"
@@ -1334,6 +1335,61 @@ private:
bool applied_;
};
+class QtGuiMessage : public virtual GuiMessage, public QtGuiWindow {
+public:
+ QtGuiMessage(Type type, std::string const& title, std::string const& text)
+ : type_(type), title_(title), text_(text) {
+ }
+
+ void add_listener(GuiMessage::Listener* listener) override {
+ observers_.insert(listener);
+ }
+
+ void remove_listener(GuiMessage::Listener* listener) override {
+ observers_.erase(listener);
+ }
+
+ void set_title(std::string const& title) override {
+ title_ = title;
+ }
+ std::string const& title() const override {
+ return title_;
+ }
+
+ void* impl() const override {
+ return QtGuiWindow::impl();
+ }
+
+ bool showWidget() override {
+ show(QApplication::activeWindow());
+ return true;
+ }
+
+ void show(GuiWindow* parent) override {
+ auto wnd = static_cast<QtGuiWindow*>(parent->impl());
+ show(wnd->widget());
+ }
+
+protected:
+ void show(QWidget* parent) {
+ switch (type_) {
+ case ERROR:
+ QMessageBox::critical(parent, QString::fromStdString(title_),
+ QString::fromStdString(text_));
+ break;
+ case OTHER:
+ QMessageBox::information(parent, QString::fromStdString(title_),
+ QString::fromStdString(text_));
+ break;
+ }
+ }
+
+ Type type_;
+ std::string title_;
+ std::string text_;
+ Observers<GuiMessage::Listener*> observers_;
+};
+
class OptionalCloseWidget : public QWidget {
public:
class Delegate {
@@ -1733,6 +1789,13 @@ GuiFormApply* GuiFormApply::create(std::string const& title,
}
// static
+GuiMessage* GuiMessage::create(Type type,
+ std::string const& title,
+ std::string const& text) {
+ return new QtGuiMessage(type, title, text);
+}
+
+// static
Looper* GuiMain::createLooper() {
return new QtLooper();
}