]> Dogcows Code - chaz/yoink/blob - src/Moof/ModalDialog.hh
big batch of changes
[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 defined(_WIN32) || defined(__WIN32__)
35 #include <windows.h>
36 #elif defined(__APPLE__) && defined(__MACH__)
37 #include <Carbon/Carbon.h>
38 #elif USE_GTK
39 #include <gtk/gtk.h>
40 #elif USE_QT4
41 #include <QApplication>
42 #include <QIcon>
43 #include <QMessageBox>
44 #endif
45
46 #include <Moof/Resource.hh>
47
48 #if HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52
53 namespace Mf {
54
55
56 /**
57 * Small wrapper over various user interface modal dialog windows.
58 */
59
60 struct ModalDialog
61 {
62 enum Type
63 {
64 INFO = 1,
65 WARNING = 2,
66 CRITICAL = 3
67 };
68
69 std::string title;
70 Type type;
71 std::string text1;
72 std::string text2;
73
74 void run() const
75 {
76 #if USE_GTK
77
78 int argc = 0;
79 char** argv;
80 gtk_init(&argc, &argv);
81
82 GtkMessageType iconType;
83 switch (type)
84 {
85 case WARNING:
86 iconType = GTK_MESSAGE_WARNING;
87 break;
88 case CRITICAL:
89 iconType = GTK_MESSAGE_ERROR;
90 break;
91 default:
92 iconType = GTK_MESSAGE_INFO;
93 break;
94 }
95
96 GtkWidget* dialog = gtk_message_dialog_new(NULL,
97 GTK_DIALOG_DESTROY_WITH_PARENT, iconType,
98 GTK_BUTTONS_CLOSE, "%s", text1.c_str());
99 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
100 "%s", text2.c_str());
101 gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
102
103 std::string iconPath = Resource::getPath(PACKAGE".png");
104 GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(), NULL);
105 gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
106
107 gtk_dialog_run(GTK_DIALOG(dialog));
108 gtk_widget_destroy(dialog);
109 // FIXME - this doesn't seem to actually remove the window from the
110 // screen when it closes, not sure why...
111
112 #elif USE_QT4
113
114 int argc = 0;
115 char** argv;
116 QApplication qtApp(argc, argv);
117
118 QMessageBox::Icon iconType;
119 switch (type)
120 {
121 case WARNING:
122 iconType = QMessageBox::Warning;
123 break;
124 case CRITICAL:
125 iconType = QMessageBox::Critical;
126 break;
127 default:
128 iconType = QMessageBox::Information;
129 break;
130 }
131
132 QMessageBox dialog;
133 dialog.setWindowTitle(title.c_str());
134 dialog.setIcon(iconType);
135 dialog.setText(text1.c_str());
136 dialog.setInformativeText(text2.c_str());
137 dialog.setStandardButtons(QMessageBox::Close);
138
139 std::string iconPath = Resource::getPath(PACKAGE".png");
140 QIcon icon(iconPath.c_str());
141 dialog.setWindowIcon(icon);
142
143 dialog.exec();
144
145 #endif
146 }
147 };
148
149
150 } // namespace Mf
151
152 #endif // _MOOF_MODALDIALOG_HH_
153
154 /** vim: set ts=4 sw=4 tw=80: *************************************************/
155
This page took 0.036972 seconds and 5 git commands to generate.