]> Dogcows Code - chaz/yoink/blob - src/Moof/ModalDialog.hh
8bb6741f80148dfbf5b9d3d8c3d1d11348d91a24
[chaz/yoink] / src / Moof / ModalDialog.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_MODALDIALOG_HH_
13 #define _MOOF_MODALDIALOG_HH_
14
15 #include <string>
16
17 #if HAVE_CONFIG_H
18 #include "../config.h"
19 #endif
20
21 #if defined(_WIN32) || defined(__WIN32__)
22 #include <windows.h>
23 #elif defined(__APPLE__) && defined(__MACH__)
24 #include <Carbon/Carbon.h>
25 #elif USE_GTK
26 #include <gtk/gtk.h>
27 #elif USE_QT4
28 #include <QApplication>
29 #include <QIcon>
30 #include <QMessageBox>
31 #endif
32
33 #include <Moof/Log.hh>
34 #include <Moof/Resource.hh>
35
36
37 namespace Mf {
38
39
40 /**
41 * Small wrapper over various user interface modal dialog windows.
42 */
43
44 struct ModalDialog
45 {
46 enum Type
47 {
48 INFO = 1,
49 WARNING = 2,
50 CRITICAL = 3
51 };
52
53
54 std::string title;
55 Type type;
56 std::string text1;
57 std::string text2;
58
59
60 void run() const
61 {
62 switch (type)
63 {
64 case WARNING:
65 logWarning(text1);
66 logWarning(text2);
67 break;
68 case CRITICAL:
69 logError(text1);
70 logError(text2);
71 break;
72 default:
73 logInfo(text1);
74 logInfo(text2);
75 break;
76 }
77
78 #if defined(_WIN32) || defined(__WIN32__)
79
80 int iconType;
81 switch (type)
82 {
83 case WARNING:
84 iconType = MB_ICONWARNING;
85 break;
86 case CRITICAL:
87 iconType = MB_ICONERROR;
88 break;
89 default:
90 iconType = MB_ICONINFORMATION;
91 break;
92 }
93
94 MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(),
95 MB_OK | iconType);
96
97 #elif USE_GTK
98
99 int argc = 0;
100 char** argv;
101 gtk_init(&argc, &argv);
102
103 GtkMessageType iconType;
104 switch (type)
105 {
106 case WARNING:
107 iconType = GTK_MESSAGE_WARNING;
108 break;
109 case CRITICAL:
110 iconType = GTK_MESSAGE_ERROR;
111 break;
112 default:
113 iconType = GTK_MESSAGE_INFO;
114 break;
115 }
116
117 GtkWidget* dialog = gtk_message_dialog_new(NULL,
118 GTK_DIALOG_DESTROY_WITH_PARENT, iconType,
119 GTK_BUTTONS_CLOSE, "%s", text1.c_str());
120 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
121 "%s", text2.c_str());
122 gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
123
124 std::string iconPath = Resource::getPath(PACKAGE".png");
125 GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(),
126 NULL);
127 gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
128
129 gtk_dialog_run(GTK_DIALOG(dialog));
130 gtk_widget_destroy(dialog);
131 // FIXME - this doesn't seem to actually remove the window from the
132 // screen when it closes
133
134 #elif USE_QT4
135
136 int argc = 0;
137 char** argv;
138 QApplication qtApp(argc, argv);
139
140 QMessageBox::Icon iconType;
141 switch (type)
142 {
143 case WARNING:
144 iconType = QMessageBox::Warning;
145 break;
146 case CRITICAL:
147 iconType = QMessageBox::Critical;
148 break;
149 default:
150 iconType = QMessageBox::Information;
151 break;
152 }
153
154 QMessageBox dialog;
155 dialog.setWindowTitle(title.c_str());
156 dialog.setIcon(iconType);
157 dialog.setText(text1.c_str());
158 dialog.setInformativeText(text2.c_str());
159 dialog.setStandardButtons(QMessageBox::Close);
160
161 std::string iconPath = Resource::getPath(PACKAGE".png");
162 QIcon icon(iconPath.c_str());
163 dialog.setWindowIcon(icon);
164
165 dialog.exec();
166
167 #endif
168 }
169 };
170
171
172 } // namespace Mf
173
174 #endif // _MOOF_MODALDIALOG_HH_
175
This page took 0.035508 seconds and 3 git commands to generate.