]> Dogcows Code - chaz/yoink/blob - src/moof/modal_dialog.hh
e1d773989bebcf41c8912c0004b05221eeb686ac
[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 "config.h"
22
23 #include <string>
24
25 #if defined(_WIN32)
26 #include <windows.h>
27 #elif USE_GTK
28 #include <gtk/gtk.h>
29 #elif USE_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
46 struct modal_dialog
47 {
48 enum type
49 {
50 information = 1,
51 warning = 2,
52 error = 3
53 };
54
55
56 std::string title;
57 type type;
58 std::string text1;
59 std::string text2;
60
61
62 /**
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.
68 */
69 modal_dialog(enum type type = information,
70 const std::string& title = "",
71 const std::string& text1 = "",
72 const std::string& text2 = "") :
73 title(title),
74 type(type),
75 text1(text1),
76 text2(text2) {}
77
78
79 void run() const
80 {
81 switch (type)
82 {
83 case warning:
84 log_warning(text1);
85 log_warning(text2);
86 break;
87 case error:
88 log_error(text1);
89 log_error(text2);
90 break;
91 default:
92 log_info(text1);
93 log_info(text2);
94 break;
95 }
96
97 #if defined(_WIN32)
98
99 int icon_type;
100 switch (type)
101 {
102 case warning:
103 icon_type = MB_ICONWARNING;
104 break;
105 case error:
106 icon_type = MB_ICONERROR;
107 break;
108 default:
109 icon_type = MB_ICONINFORMATION;
110 break;
111 }
112
113 MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(),
114 MB_OK | icon_type);
115
116 #elif USE_GTK
117
118 int argc = 0;
119 char** argv;
120 gtk_init(&argc, &argv);
121
122 GtkMessageType icon_type;
123 switch (type)
124 {
125 case warning:
126 icon_type = GTK_MESSAGE_WARNING;
127 break;
128 case error:
129 icon_type = GTK_MESSAGE_ERROR;
130 break;
131 default:
132 icon_type = GTK_MESSAGE_INFO;
133 break;
134 }
135
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());
142
143 std::string icon_path = resource::find_file(PACKAGE".png");
144 if (!icon_path.empty())
145 {
146 GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(icon_path.c_str(),
147 NULL);
148 gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
149 }
150
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
155
156 #elif USE_QT4
157
158 int argc = 0;
159 char** argv;
160 QApplication qt_app(argc, argv);
161
162 QMessageBox::Icon icon_type;
163 switch (type)
164 {
165 case warning:
166 icon_type = QMessageBox::Warning;
167 break;
168 case error:
169 icon_type = QMessageBox::Critical;
170 break;
171 default:
172 icon_type = QMessageBox::Information;
173 break;
174 }
175
176 QMessageBox dialog;
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);
182
183 std::string icon_path = resource::find_file(PACKAGE".png");
184 if (!icon_path.empty())
185 {
186 QIcon icon(icon_path.c_str());
187 dialog.setWindowIcon(icon);
188 }
189
190 dialog.exec();
191
192 #endif
193 }
194 };
195
196
197 } // namespace moof
198
199 #endif // _MOOF_MODAL_DIALOG_HH_
200
This page took 0.041479 seconds and 3 git commands to generate.