Parent Directory | Revision Log
More error 417 fixes and dialog size framework
1 | harbaum | 1 | /* |
2 | achadwick | 101 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * | ||
4 | harbaum | 1 | * This file is part of OSM2Go. |
5 | * | ||
6 | * OSM2Go is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * OSM2Go is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include "appdata.h" | ||
21 | |||
22 | void messagef(GtkWidget *parent, char *title, const char *fmt, ...) { | ||
23 | va_list args; | ||
24 | va_start( args, fmt ); | ||
25 | char *buf = g_strdup_vprintf(fmt, args); | ||
26 | va_end( args ); | ||
27 | |||
28 | printf("%s: \"%s\"\n", title, buf); | ||
29 | |||
30 | GtkWidget *dialog = gtk_message_dialog_new( | ||
31 | GTK_WINDOW(parent), | ||
32 | GTK_DIALOG_DESTROY_WITH_PARENT, | ||
33 | GTK_MESSAGE_INFO, GTK_BUTTONS_OK, | ||
34 | buf); | ||
35 | |||
36 | gtk_window_set_title(GTK_WINDOW(dialog), title); | ||
37 | |||
38 | gtk_dialog_run(GTK_DIALOG(dialog)); | ||
39 | gtk_widget_destroy(dialog); | ||
40 | |||
41 | g_free(buf); | ||
42 | } | ||
43 | |||
44 | static void on_toggled(GtkWidget *button, gpointer data) { | ||
45 | gboolean active = | ||
46 | gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); | ||
47 | |||
48 | GtkWidget *dialog = gtk_widget_get_toplevel(button); | ||
49 | |||
50 | if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_NO) | ||
51 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), | ||
52 | GTK_RESPONSE_NO, !active); | ||
53 | |||
54 | if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_YES) | ||
55 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), | ||
56 | GTK_RESPONSE_YES, !active); | ||
57 | } | ||
58 | |||
59 | gboolean yes_no_f(GtkWidget *parent, appdata_t *appdata, gulong again_bit, | ||
60 | gint flags, char *title, const char *fmt, ...) { | ||
61 | |||
62 | if(appdata && again_bit && (appdata->dialog_again.not & again_bit)) | ||
63 | return((appdata->dialog_again.reply & again_bit) != 0); | ||
64 | |||
65 | va_list args; | ||
66 | va_start( args, fmt ); | ||
67 | char *buf = g_strdup_vprintf(fmt, args); | ||
68 | va_end( args ); | ||
69 | |||
70 | printf("%s: \"%s\"\n", title, buf); | ||
71 | |||
72 | GtkWidget *dialog = gtk_message_dialog_new( | ||
73 | GTK_WINDOW(parent), | ||
74 | GTK_DIALOG_DESTROY_WITH_PARENT, | ||
75 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, | ||
76 | buf); | ||
77 | |||
78 | gtk_window_set_title(GTK_WINDOW(dialog), title); | ||
79 | |||
80 | GtkWidget *cbut = NULL; | ||
81 | if(appdata && again_bit) { | ||
82 | GtkWidget *alignment = gtk_alignment_new(0.5, 0, 0, 0); | ||
83 | |||
84 | cbut = gtk_check_button_new_with_label( | ||
85 | _("Don't ask this question again this session")); | ||
86 | g_signal_connect(cbut, "toggled", G_CALLBACK(on_toggled), &flags); | ||
87 | |||
88 | gtk_container_add(GTK_CONTAINER(alignment), cbut); | ||
89 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), alignment); | ||
90 | |||
91 | gtk_widget_show_all(dialog); | ||
92 | } | ||
93 | |||
94 | gboolean yes = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_YES); | ||
95 | |||
96 | if(cbut && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cbut))) { | ||
97 | /* the user doesn't want to see this dialog again */ | ||
98 | |||
99 | appdata->dialog_again.not |= again_bit; | ||
100 | if(yes) appdata->dialog_again.reply |= again_bit; | ||
101 | else appdata->dialog_again.reply &= ~again_bit; | ||
102 | } | ||
103 | |||
104 | gtk_widget_destroy(dialog); | ||
105 | |||
106 | g_free(buf); | ||
107 | return yes; | ||
108 | } | ||
109 | |||
110 | void errorf(GtkWidget *parent, const char *fmt, ...) { | ||
111 | va_list args; | ||
112 | va_start( args, fmt ); | ||
113 | char *buf = g_strdup_vprintf(fmt, args); | ||
114 | va_end( args ); | ||
115 | |||
116 | printf("errorf(\"%s\")\n", buf); | ||
117 | |||
118 | GtkWidget *dialog = gtk_message_dialog_new( | ||
119 | GTK_WINDOW(parent), | ||
120 | GTK_DIALOG_DESTROY_WITH_PARENT, | ||
121 | GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, | ||
122 | buf); | ||
123 | |||
124 | gtk_window_set_title(GTK_WINDOW(dialog), _("ERROR")); | ||
125 | |||
126 | gtk_dialog_run(GTK_DIALOG(dialog)); | ||
127 | gtk_widget_destroy(dialog); | ||
128 | |||
129 | g_free(buf); | ||
130 | } | ||
131 | |||
132 | static const char *data_paths[] = { | ||
133 | harbaum | 41 | "~/." PACKAGE, // in home directory |
134 | PREFIX "/share/" PACKAGE , // final installation path | ||
135 | harbaum | 1 | #ifdef USE_HILDON |
136 | harbaum | 41 | "/media/mmc1/" PACKAGE, // path to external memory card |
137 | "/media/mmc2/" PACKAGE, // path to internal memory card | ||
138 | harbaum | 1 | #endif |
139 | harbaum | 41 | "./data", "../data", // local paths for testing |
140 | harbaum | 1 | NULL |
141 | }; | ||
142 | |||
143 | char *find_file(char *name) { | ||
144 | const char **path = data_paths; | ||
145 | char *p = getenv("HOME"); | ||
146 | |||
147 | while(*path) { | ||
148 | char *full_path = NULL; | ||
149 | |||
150 | if(*path[0] == '~') | ||
151 | full_path = g_strdup_printf("%s/%s/%s", p, *path+2, name); | ||
152 | else | ||
153 | full_path = g_strdup_printf("%s/%s", *path, name); | ||
154 | |||
155 | if(g_file_test(full_path, G_FILE_TEST_IS_REGULAR)) | ||
156 | return full_path; | ||
157 | |||
158 | g_free(full_path); | ||
159 | path++; | ||
160 | } | ||
161 | |||
162 | return NULL; | ||
163 | } | ||
164 | |||
165 | /* scan all data directories for the given file pattern and */ | ||
166 | /* return a list of files matching this pattern */ | ||
167 | file_chain_t *file_scan(char *pattern) { | ||
168 | file_chain_t *chain = NULL, **chainP = &chain; | ||
169 | |||
170 | const char **path = data_paths; | ||
171 | char *p = getenv("HOME"); | ||
172 | |||
173 | while(*path) { | ||
174 | GDir *dir = NULL; | ||
175 | |||
176 | /* scan for projects */ | ||
177 | const char *dirname = *path; | ||
178 | |||
179 | if(*path[0] == '~') | ||
180 | dirname = g_strdup_printf("%s/%s", p, *path+2); | ||
181 | |||
182 | if((dir = g_dir_open(dirname, 0, NULL))) { | ||
183 | const char *name = NULL; | ||
184 | do { | ||
185 | name = g_dir_read_name(dir); | ||
186 | |||
187 | if(name) { | ||
188 | char *fullname = g_strdup_printf("%s/%s", dirname, name); | ||
189 | if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR)) { | ||
190 | if(g_pattern_match_simple(pattern, name)) { | ||
191 | *chainP = g_new0(file_chain_t, 1); | ||
192 | (*chainP)->name = fullname; | ||
193 | chainP = &(*chainP)->next; | ||
194 | } else | ||
195 | g_free(fullname); | ||
196 | } else | ||
197 | g_free(fullname); | ||
198 | } | ||
199 | } while(name); | ||
200 | |||
201 | g_dir_close(dir); | ||
202 | |||
203 | if(*path[0] == '~') | ||
204 | g_free((char*)dirname); | ||
205 | } | ||
206 | |||
207 | path++; | ||
208 | } | ||
209 | |||
210 | return chain; | ||
211 | } | ||
212 | harbaum | 167 | |
213 | |||
214 | #ifdef USE_HILDON | ||
215 | static const gint dialog_sizes[][2] = { | ||
216 | { 400, 100 }, // SMALL | ||
217 | { 450, 300 }, // MEDIUM | ||
218 | { 800, 480 }, // LARGE | ||
219 | { 640, 100 }, // WIDE | ||
220 | { 0, 0 }, // HIGH | ||
221 | }; | ||
222 | #else | ||
223 | static const gint dialog_sizes[][2] = { | ||
224 | { 300, 100 }, // SMALL | ||
225 | { 400, 300 }, // MEDIUM | ||
226 | { 500, 350 }, // LARGE | ||
227 | { 450, 100 }, // WIDE | ||
228 | { 0, 0 }, // HIGH | ||
229 | }; | ||
230 | #endif | ||
231 | |||
232 | /* create a modal dialog using one of the predefined size hints */ | ||
233 | GtkWidget *misc_dialog_new(guint hint, const char *title, | ||
234 | GtkWindow *parent, ...) { | ||
235 | va_list args; | ||
236 | va_start( args, parent ); | ||
237 | |||
238 | /* create dialog itself */ | ||
239 | GtkWidget *dialog = gtk_dialog_new(); | ||
240 | |||
241 | gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); | ||
242 | if(title) gtk_window_set_title(GTK_WINDOW(dialog), title); | ||
243 | if(parent) gtk_window_set_transient_for(GTK_WINDOW(dialog), parent); | ||
244 | |||
245 | const gchar *button_text = va_arg(args, const gchar *); | ||
246 | while(button_text) { | ||
247 | gtk_dialog_add_button(GTK_DIALOG(dialog), button_text, va_arg(args, gint)); | ||
248 | button_text = va_arg(args, const gchar *); | ||
249 | } | ||
250 | |||
251 | va_end( args ); | ||
252 | |||
253 | if(hint != MISC_DIALOG_NOSIZE) | ||
254 | gtk_window_set_default_size(GTK_WINDOW(dialog), | ||
255 | dialog_sizes[hint][0], dialog_sizes[hint][1]); | ||
256 | |||
257 | return dialog; | ||
258 | } |