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