]> Dogcows Code - chaz/yoink/blob - src/moof/modal_dialog.hh
remove some unused stlplus modules
[chaz/yoink] / src / moof / modal_dialog.hh
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #ifndef _MOOF_MODAL_DIALOG_HH_
11 #define _MOOF_MODAL_DIALOG_HH_
12
13 /**
14 * \file modal_dialog.hh
15 * A class for creating and running modal dialogs. Several toolkits are
16 * supported, but only one can be used as determined at build time.
17 */
18
19 #if HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <string>
24
25 #if PLATFORM_WIN32
26 #include <windows.h>
27 #elif WITH_GUI_GTK
28 #include <gtk/gtk.h>
29 #elif WITH_GUI_QT4
30 #include <QApplication>
31 #include <QIcon>
32 #include <QMessageBox>
33 #endif
34
35 #include <moof/log.hh>
36 #include <moof/resource.hh>
37
38
39 namespace moof {
40
41
42 /**
43 * Small wrapper over various user interface modal dialog windows.
44 */
45 struct modal_dialog
46 {
47 enum type
48 {
49 information = 1,
50 warning = 2,
51 error = 3
52 };
53
54 std::string title;
55 type type;
56 std::string text1;
57 std::string text2;
58
59 /**
60 * Construct a dialog box.
61 * \param type The type of dialog, indicating its priority.
62 * \param title The text used as the title of the dialog.
63 * \param text1 The first line of the dialog window.
64 * \param text2 The second line.
65 */
66 modal_dialog(enum type type = information,
67 const std::string& title = "",
68 const std::string& text1 = "",
69 const std::string& text2 = "") :
70 title(title),
71 type(type),
72 text1(text1),
73 text2(text2) {}
74
75 void run() const
76 {
77 switch (type)
78 {
79 case warning:
80 log_warning(text1);
81 log_warning(text2);
82 break;
83 case error:
84 log_error(text1);
85 log_error(text2);
86 break;
87 default:
88 log_info(text1);
89 log_info(text2);
90 break;
91 }
92
93 #if PLATFORM_WIN32
94 int icon_type;
95 switch (type)
96 {
97 case warning:
98 icon_type = MB_ICONWARNING;
99 break;
100 case error:
101 icon_type = MB_ICONERROR;
102 break;
103 default:
104 icon_type = MB_ICONINFORMATION;
105 break;
106 }
107
108 MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(),
109 MB_OK | icon_type);
110 #elif WITH_GUI_GTK
111 int argc = 0;
112 char** argv;
113 gtk_init(&argc, &argv);
114
115 GtkMessageType icon_type;
116 switch (type)
117 {
118 case warning:
119 icon_type = GTK_MESSAGE_WARNING;
120 break;
121 case error:
122 icon_type = GTK_MESSAGE_ERROR;
123 break;
124 default:
125 icon_type = GTK_MESSAGE_INFO;
126 break;
127 }
128
129 GtkWidget* dialog = gtk_message_dialog_new(NULL,
130 GTK_DIALOG_DESTROY_WITH_PARENT, icon_type,
131 GTK_BUTTONS_CLOSE, "%s", text1.c_str());
132 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
133 "%s", text2.c_str());
134 gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
135
136 std::string icon_path = resource::find_file(PACKAGE_TARNAME".png");
137 if (!icon_path.empty())
138 {
139 GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(icon_path.c_str(),
140 NULL);
141 gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
142 }
143
144 gtk_dialog_run(GTK_DIALOG(dialog));
145 gtk_widget_destroy(dialog);
146 // FIXME - this doesn't seem to actually remove the window from the
147 // screen when it closes
148 #elif WITH_GUI_QT4
149 int argc = 0;
150 char** argv;
151 QApplication qt_app(argc, argv);
152
153 QMessageBox::Icon icon_type;
154 switch (type)
155 {
156 case warning:
157 icon_type = QMessageBox::Warning;
158 break;
159 case error:
160 icon_type = QMessageBox::Critical;
161 break;
162 default:
163 icon_type = QMessageBox::Information;
164 break;
165 }
166
167 QMessageBox dialog;
168 dialog.setWindowTitle(title.c_str());
169 dialog.setIcon(icon_type);
170 dialog.setText(text1.c_str());
171 dialog.setInformativeText(text2.c_str());
172 dialog.setStandardButtons(QMessageBox::Close);
173
174 std::string icon_path = resource::find_file(PACKAGE_TARNAME".png");
175 if (!icon_path.empty())
176 {
177 QIcon icon(icon_path.c_str());
178 dialog.setWindowIcon(icon);
179 }
180
181 dialog.exec();
182 #endif
183 }
184 };
185
186
187 } // namespace moof
188
189 #endif // _MOOF_MODAL_DIALOG_HH_
190
This page took 0.04116 seconds and 4 git commands to generate.