2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_MODAL_DIALOG_HH_
13 #define _MOOF_MODAL_DIALOG_HH_
16 * \file modal_dialog.hh
17 * A class for creating and running modal dialogs. Several toolkits are
18 * supported, but only one can be used as determined at build time.
30 #include <QApplication>
32 #include <QMessageBox>
35 #include <moof/log.hh>
36 #include <moof/resource.hh>
43 * Small wrapper over various user interface modal dialog windows.
63 * Construct a dialog box.
64 * \param type The type of dialog, indicating its priority.
65 * \param title The text used as the title of the dialog.
66 * \param text1 The first line of the dialog window.
67 * \param text2 The second line.
69 modal_dialog(enum type type
= information
,
70 const std::string
& title
= "",
71 const std::string
& text1
= "",
72 const std::string
& text2
= "") :
103 icon_type
= MB_ICONWARNING
;
106 icon_type
= MB_ICONERROR
;
109 icon_type
= MB_ICONINFORMATION
;
113 MessageBox(0, (text1
+ "\n" + text2
).c_str(), title
.c_str(),
120 gtk_init(&argc
, &argv
);
122 GtkMessageType icon_type
;
126 icon_type
= GTK_MESSAGE_WARNING
;
129 icon_type
= GTK_MESSAGE_ERROR
;
132 icon_type
= GTK_MESSAGE_INFO
;
136 GtkWidget
* dialog
= gtk_message_dialog_new(NULL
,
137 GTK_DIALOG_DESTROY_WITH_PARENT
, icon_type
,
138 GTK_BUTTONS_CLOSE
, "%s", text1
.c_str());
139 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
140 "%s", text2
.c_str());
141 gtk_window_set_title(GTK_WINDOW(dialog
), title
.c_str());
143 std::string icon_path
= resource::find_file(PACKAGE
".png");
144 if (!icon_path
.empty())
146 GdkPixbuf
* iconPixbuf
= gdk_pixbuf_new_from_file(icon_path
.c_str(),
148 gtk_window_set_icon(GTK_WINDOW(dialog
), iconPixbuf
);
151 gtk_dialog_run(GTK_DIALOG(dialog
));
152 gtk_widget_destroy(dialog
);
153 // FIXME - this doesn't seem to actually remove the window from the
154 // screen when it closes
160 QApplication
qt_app(argc
, argv
);
162 QMessageBox::Icon icon_type
;
166 icon_type
= QMessageBox::Warning
;
169 icon_type
= QMessageBox::Critical
;
172 icon_type
= QMessageBox::Information
;
177 dialog
.setWindowTitle(title
.c_str());
178 dialog
.setIcon(icon_type
);
179 dialog
.setText(text1
.c_str());
180 dialog
.setInformativeText(text2
.c_str());
181 dialog
.setStandardButtons(QMessageBox::Close
);
183 std::string icon_path
= resource::find_file(PACKAGE
".png");
184 if (!icon_path
.empty())
186 QIcon
icon(icon_path
.c_str());
187 dialog
.setWindowIcon(icon
);
199 #endif // _MOOF_MODAL_DIALOG_HH_