]> Dogcows Code - chaz/yoink/blob - src/Moof/ModalDialog.hh
f630cb6a21c64c5cab567a46e8880a10f376813a
[chaz/yoink] / src / Moof / ModalDialog.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_MODALDIALOG_HH_
30 #define _MOOF_MODALDIALOG_HH_
31
32 #include <string>
33
34 #if HAVE_CONFIG_H
35 #include "../config.h"
36 #endif
37
38 #if defined(_WIN32) || defined(__WIN32__)
39 #include <windows.h>
40 #elif defined(__APPLE__) && defined(__MACH__)
41 #include <Carbon/Carbon.h>
42 #elif USE_GTK
43 #include <gtk/gtk.h>
44 #elif USE_QT4
45 #include <QApplication>
46 #include <QIcon>
47 #include <QMessageBox>
48 #endif
49
50 #include <Moof/Log.hh>
51 #include <Moof/Resource.hh>
52
53
54 namespace Mf {
55
56
57 /**
58 * Small wrapper over various user interface modal dialog windows.
59 */
60
61 struct ModalDialog
62 {
63 enum Type
64 {
65 INFO = 1,
66 WARNING = 2,
67 CRITICAL = 3
68 };
69
70
71 std::string title;
72 Type type;
73 std::string text1;
74 std::string text2;
75
76
77 void run() const
78 {
79 switch (type)
80 {
81 case WARNING:
82 logWarning(text1);
83 logWarning(text2);
84 break;
85 case CRITICAL:
86 logError(text1);
87 logError(text2);
88 break;
89 default:
90 logInfo(text1);
91 logInfo(text2);
92 break;
93 }
94
95 #if defined(_WIN32) || defined(__WIN32__)
96
97 int iconType;
98 switch (type)
99 {
100 case WARNING:
101 iconType = MB_ICONWARNING;
102 break;
103 case CRITICAL:
104 iconType = MB_ICONERROR;
105 break;
106 default:
107 iconType = MB_ICONINFORMATION;
108 break;
109 }
110
111 MessageBox(0, (text1 + "\n" + text2).c_str(), title.c_str(),
112 MB_OK | iconType);
113
114 #elif USE_GTK
115
116 int argc = 0;
117 char** argv;
118 gtk_init(&argc, &argv);
119
120 GtkMessageType iconType;
121 switch (type)
122 {
123 case WARNING:
124 iconType = GTK_MESSAGE_WARNING;
125 break;
126 case CRITICAL:
127 iconType = GTK_MESSAGE_ERROR;
128 break;
129 default:
130 iconType = GTK_MESSAGE_INFO;
131 break;
132 }
133
134 GtkWidget* dialog = gtk_message_dialog_new(NULL,
135 GTK_DIALOG_DESTROY_WITH_PARENT, iconType,
136 GTK_BUTTONS_CLOSE, "%s", text1.c_str());
137 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
138 "%s", text2.c_str());
139 gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
140
141 std::string iconPath = Resource::getPath(PACKAGE".png");
142 GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(), NULL);
143 gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
144
145 gtk_dialog_run(GTK_DIALOG(dialog));
146 gtk_widget_destroy(dialog);
147 // FIXME - this doesn't seem to actually remove the window from the
148 // screen when it closes
149
150 #elif USE_QT4
151
152 int argc = 0;
153 char** argv;
154 QApplication qtApp(argc, argv);
155
156 QMessageBox::Icon iconType;
157 switch (type)
158 {
159 case WARNING:
160 iconType = QMessageBox::Warning;
161 break;
162 case CRITICAL:
163 iconType = QMessageBox::Critical;
164 break;
165 default:
166 iconType = QMessageBox::Information;
167 break;
168 }
169
170 QMessageBox dialog;
171 dialog.setWindowTitle(title.c_str());
172 dialog.setIcon(iconType);
173 dialog.setText(text1.c_str());
174 dialog.setInformativeText(text2.c_str());
175 dialog.setStandardButtons(QMessageBox::Close);
176
177 std::string iconPath = Resource::getPath(PACKAGE".png");
178 QIcon icon(iconPath.c_str());
179 dialog.setWindowIcon(icon);
180
181 dialog.exec();
182
183 #endif
184 }
185 };
186
187
188 } // namespace Mf
189
190 #endif // _MOOF_MODALDIALOG_HH_
191
192 /** vim: set ts=4 sw=4 tw=80: *************************************************/
193
This page took 0.036133 seconds and 3 git commands to generate.