]> Dogcows Code - chaz/yoink/blob - src/Moof/ModalDialog.hh
destroyed global classes; view hierarchy instead
[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)
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
61 ModalDialog(Type pType = INFO,
62 const std::string& pTitle = "",
63 const std::string& pText1 = "",
64 const std::string& pText2 = "") :
65 title(pTitle),
66 type(pType),
67 text1(pText1),
68 text2(pText2) {}
69
70
71 void run() const
72 {
73 switch (type)
74 {
75 case WARNING:
76 logWarning(text1);
77 logWarning(text2);
78 break;
79 case CRITICAL:
80 logError(text1);
81 logError(text2);
82 break;
83 default:
84 logInfo(text1);
85 logInfo(text2);
86 break;
87 }
88
89 #if defined(_WIN32)
90
91 int iconType;
92 switch (type)
93 {
94 case WARNING:
95 iconType = MB_ICONWARNING;
96 break;
97 case CRITICAL:
98 iconType = MB_ICONERROR;
99 break;
100 default:
101 iconType = MB_ICONINFORMATION;
102 break;
103 }
104
105 MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(),
106 MB_OK | iconType);
107
108 #elif USE_GTK
109
110 int argc = 0;
111 char** argv;
112 gtk_init(&argc, &argv);
113
114 GtkMessageType iconType;
115 switch (type)
116 {
117 case WARNING:
118 iconType = GTK_MESSAGE_WARNING;
119 break;
120 case CRITICAL:
121 iconType = GTK_MESSAGE_ERROR;
122 break;
123 default:
124 iconType = GTK_MESSAGE_INFO;
125 break;
126 }
127
128 GtkWidget* dialog = gtk_message_dialog_new(NULL,
129 GTK_DIALOG_DESTROY_WITH_PARENT, iconType,
130 GTK_BUTTONS_CLOSE, "%s", text1.c_str());
131 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
132 "%s", text2.c_str());
133 gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
134
135 std::string iconPath = Resource::getPath(PACKAGE".png");
136 GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(),
137 NULL);
138 gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
139
140 gtk_dialog_run(GTK_DIALOG(dialog));
141 gtk_widget_destroy(dialog);
142 // FIXME - this doesn't seem to actually remove the window from the
143 // screen when it closes
144
145 #elif USE_QT4
146
147 int argc = 0;
148 char** argv;
149 QApplication qtApp(argc, argv);
150
151 QMessageBox::Icon iconType;
152 switch (type)
153 {
154 case WARNING:
155 iconType = QMessageBox::Warning;
156 break;
157 case CRITICAL:
158 iconType = QMessageBox::Critical;
159 break;
160 default:
161 iconType = QMessageBox::Information;
162 break;
163 }
164
165 QMessageBox dialog;
166 dialog.setWindowTitle(title.c_str());
167 dialog.setIcon(iconType);
168 dialog.setText(text1.c_str());
169 dialog.setInformativeText(text2.c_str());
170 dialog.setStandardButtons(QMessageBox::Close);
171
172 std::string iconPath = Resource::getPath(PACKAGE".png");
173 QIcon icon(iconPath.c_str());
174 dialog.setWindowIcon(icon);
175
176 dialog.exec();
177
178 #endif
179 }
180 };
181
182
183 } // namespace Mf
184
185 #endif // _MOOF_MODALDIALOG_HH_
186
This page took 0.039667 seconds and 4 git commands to generate.