/*] 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_MODALDIALOG_HH_ #define _MOOF_MODALDIALOG_HH_ #include #if HAVE_CONFIG_H #include "config.h" #endif #if defined(_WIN32) #include #elif defined(__APPLE__) && defined(__MACH__) #include #elif USE_GTK #include #elif USE_QT4 #include #include #include #endif #include #include namespace Mf { /** * Small wrapper over various user interface modal dialog windows. */ struct ModalDialog { enum Type { INFO = 1, WARNING = 2, CRITICAL = 3 }; std::string title; Type type; std::string text1; std::string text2; ModalDialog(Type pType = INFO, const std::string& pTitle = "", const std::string& pText1 = "", const std::string& pText2 = "") : title(pTitle), type(pType), text1(pText1), text2(pText2) {} void run() const { switch (type) { case WARNING: logWarning(text1); logWarning(text2); break; case CRITICAL: logError(text1); logError(text2); break; default: logInfo(text1); logInfo(text2); break; } #if defined(_WIN32) int iconType; switch (type) { case WARNING: iconType = MB_ICONWARNING; break; case CRITICAL: iconType = MB_ICONERROR; break; default: iconType = MB_ICONINFORMATION; break; } MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(), MB_OK | iconType); #elif USE_GTK int argc = 0; char** argv; gtk_init(&argc, &argv); GtkMessageType iconType; switch (type) { case WARNING: iconType = GTK_MESSAGE_WARNING; break; case CRITICAL: iconType = GTK_MESSAGE_ERROR; break; default: iconType = GTK_MESSAGE_INFO; break; } GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, iconType, 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 iconPath(PACKAGE".png"); if (Resource::getPath(iconPath)) { GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.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 qtApp(argc, argv); QMessageBox::Icon iconType; switch (type) { case WARNING: iconType = QMessageBox::Warning; break; case CRITICAL: iconType = QMessageBox::Critical; break; default: iconType = QMessageBox::Information; break; } QMessageBox dialog; dialog.setWindowTitle(title.c_str()); dialog.setIcon(iconType); dialog.setText(text1.c_str()); dialog.setInformativeText(text2.c_str()); dialog.setStandardButtons(QMessageBox::Close); std::string iconPath(PACKAGE".png"); if (Resource::getPath(iconPath)) { QIcon icon(iconPath.c_str()); dialog.setWindowIcon(icon); } dialog.exec(); #endif } }; } // namespace Mf #endif // _MOOF_MODALDIALOG_HH_