+
+/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+**] All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#ifndef _MOOF_MODAL_DIALOG_HH_
+#define _MOOF_MODAL_DIALOG_HH_
+
+/**
+ * \file modal_dialog.hh
+ * A class for creating and running modal dialogs. Several toolkits are
+ * supported, but only one can be used as determined at build time.
+ */
+
+#include <string>
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(_WIN32)
+#include <windows.h>
+#elif USE_GTK
+#include <gtk/gtk.h>
+#elif USE_QT4
+#include <QApplication>
+#include <QIcon>
+#include <QMessageBox>
+#endif
+
+#include <moof/log.hh>
+#include <moof/resource.hh>
+
+
+namespace moof {
+
+
+/**
+ * Small wrapper over various user interface modal dialog windows.
+ */
+
+struct modal_dialog
+{
+ enum type
+ {
+ information = 1,
+ warning = 2,
+ error = 3
+ };
+
+
+ std::string title;
+ type type;
+ std::string text1;
+ std::string text2;
+
+
+ /**
+ * Construct a dialog box.
+ * \param type The type of dialog, indicating its priority.
+ * \param title The text used as the title of the dialog.
+ * \param text1 The first line of the dialog window.
+ * \param text2 The second line.
+ */
+ modal_dialog(enum type type = information,
+ const std::string& title = "",
+ const std::string& text1 = "",
+ const std::string& text2 = "") :
+ title(title),
+ type(type),
+ text1(text1),
+ text2(text2) {}
+
+
+ void run() const
+ {
+ switch (type)
+ {
+ case warning:
+ log_warning(text1);
+ log_warning(text2);
+ break;
+ case error:
+ log_error(text1);
+ log_error(text2);
+ break;
+ default:
+ log_info(text1);
+ log_info(text2);
+ break;
+ }
+
+#if defined(_WIN32)
+
+ int icon_type;
+ switch (type)
+ {
+ case warning:
+ icon_type = MB_ICONWARNING;
+ break;
+ case error:
+ icon_type = MB_ICONERROR;
+ break;
+ default:
+ icon_type = MB_ICONINFORMATION;
+ break;
+ }
+
+ MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(),
+ MB_OK | icon_type);
+
+#elif USE_GTK
+
+ int argc = 0;
+ char** argv;
+ gtk_init(&argc, &argv);
+
+ GtkMessageType icon_type;
+ switch (type)
+ {
+ case warning:
+ icon_type = GTK_MESSAGE_WARNING;
+ break;
+ case error:
+ icon_type = GTK_MESSAGE_ERROR;
+ break;
+ default:
+ icon_type = GTK_MESSAGE_INFO;
+ break;
+ }
+
+ GtkWidget* dialog = gtk_message_dialog_new(NULL,
+ GTK_DIALOG_DESTROY_WITH_PARENT, icon_type,
+ GTK_BUTTONS_CLOSE, "%s", text1.c_str());
+ gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
+ "%s", text2.c_str());
+ gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
+
+ std::string icon_path(PACKAGE".png");
+ if (resource::find_path(icon_path))
+ {
+ GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(icon_path.c_str(),
+ NULL);
+ gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
+ }
+
+ gtk_dialog_run(GTK_DIALOG(dialog));
+ gtk_widget_destroy(dialog);
+ // FIXME - this doesn't seem to actually remove the window from the
+ // screen when it closes
+
+#elif USE_QT4
+
+ int argc = 0;
+ char** argv;
+ QApplication qt_app(argc, argv);
+
+ QMessageBox::Icon icon_type;
+ switch (type)
+ {
+ case warning:
+ icon_type = QMessageBox::Warning;
+ break;
+ case error:
+ icon_type = QMessageBox::Critical;
+ break;
+ default:
+ icon_type = QMessageBox::Information;
+ break;
+ }
+
+ QMessageBox dialog;
+ dialog.setWindowTitle(title.c_str());
+ dialog.setIcon(icon_type);
+ dialog.setText(text1.c_str());
+ dialog.setInformativeText(text2.c_str());
+ dialog.setStandardButtons(QMessageBox::Close);
+
+ std::string icon_path(PACKAGE".png");
+ if (resource::find_path(icon_path))
+ {
+ QIcon icon(icon_path.c_str());
+ dialog.setWindowIcon(icon);
+ }
+
+ dialog.exec();
+
+#endif
+ }
+};
+
+
+} // namespace moof
+
+#endif // _MOOF_MODAL_DIALOG_HH_
+