Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 315 - (show annotations)
Wed Dec 16 20:07:58 2009 UTC (14 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 13596 byte(s)
Various fremantleization
1 /*
2 * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3 *
4 * 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 static void vmessagef(GtkWidget *parent, int type, int buttons,
23 char *title, const char *fmt,
24 va_list args) {
25
26 char *buf = g_strdup_vprintf(fmt, args);
27
28 #if !defined(USE_HILDON) || (MAEMO_VERSION_MAJOR < 5)
29 GtkWidget *dialog = gtk_message_dialog_new(
30 GTK_WINDOW(parent),
31 GTK_DIALOG_DESTROY_WITH_PARENT,
32 type, buttons, buf);
33
34 gtk_window_set_title(GTK_WINDOW(dialog), title);
35 #else
36 GtkWidget *dialog =
37 hildon_note_new_information(GTK_WINDOW(parent), buf);
38 #endif
39
40 gtk_dialog_run(GTK_DIALOG(dialog));
41 gtk_widget_destroy(dialog);
42
43 g_free(buf);
44 }
45
46 void messagef(GtkWidget *parent, char *title, const char *fmt, ...) {
47 va_list args;
48 va_start( args, fmt );
49 vmessagef(parent, GTK_MESSAGE_INFO,
50 GTK_BUTTONS_OK, title, fmt, args);
51 va_end( args );
52 }
53
54 void errorf(GtkWidget *parent, const char *fmt, ...) {
55 va_list args;
56 va_start( args, fmt );
57
58 vmessagef(parent, GTK_MESSAGE_ERROR,
59 GTK_BUTTONS_CLOSE, _("Error"), fmt, args);
60 va_end( args );
61 }
62
63 void warningf(GtkWidget *parent, const char *fmt, ...) {
64 va_list args;
65 va_start( args, fmt );
66 vmessagef(parent, GTK_MESSAGE_WARNING,
67 GTK_BUTTONS_CLOSE, _("Warning"), fmt, args);
68 va_end( args );
69 }
70
71 #ifndef FREMANTLE
72 #define RESPONSE_YES GTK_RESPONSE_YES
73 #define RESPONSE_NO GTK_RESPONSE_NO
74 #else
75 /* hildon names the yes/no buttons ok/cancel ??? */
76 #define RESPONSE_YES GTK_RESPONSE_OK
77 #define RESPONSE_NO GTK_RESPONSE_CANCEL
78 #endif
79
80 static void on_toggled(GtkWidget *button, gpointer data) {
81 gboolean active = check_button_get_active(button);
82
83 GtkWidget *dialog = gtk_widget_get_toplevel(button);
84
85 if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_NO)
86 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
87 RESPONSE_NO, !active);
88
89 if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_YES)
90 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
91 RESPONSE_YES, !active);
92 }
93
94 gboolean yes_no_f(GtkWidget *parent, appdata_t *appdata, gulong again_bit,
95 gint flags, char *title, const char *fmt, ...) {
96
97 if(appdata && again_bit && (appdata->dialog_again.not & again_bit))
98 return((appdata->dialog_again.reply & again_bit) != 0);
99
100 va_list args;
101 va_start( args, fmt );
102 char *buf = g_strdup_vprintf(fmt, args);
103 va_end( args );
104
105 printf("%s: \"%s\"\n", title, buf);
106
107 #ifndef FREMANTLE
108 GtkWidget *dialog = gtk_message_dialog_new(
109 GTK_WINDOW(parent),
110 GTK_DIALOG_DESTROY_WITH_PARENT,
111 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
112 buf);
113
114 gtk_window_set_title(GTK_WINDOW(dialog), title);
115 #else
116 GtkWidget *dialog =
117 hildon_note_new_confirmation(GTK_WINDOW(parent), buf);
118 #endif
119
120 GtkWidget *cbut = NULL;
121 if(appdata && again_bit) {
122 /* make sure there's some space before the checkbox */
123 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
124 gtk_label_new(" "));
125
126 GtkWidget *alignment = gtk_alignment_new(0.5, 0, 0, 0);
127
128 cbut = check_button_new_with_label(_("Don't ask this question again"));
129 g_signal_connect(cbut, "toggled", G_CALLBACK(on_toggled), &flags);
130
131 gtk_container_add(GTK_CONTAINER(alignment), cbut);
132 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), alignment);
133
134 gtk_widget_show_all(dialog);
135 }
136
137 gboolean yes = (gtk_dialog_run(GTK_DIALOG(dialog)) == RESPONSE_YES);
138
139 if(cbut && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cbut))) {
140 /* the user doesn't want to see this dialog again */
141
142 appdata->dialog_again.not |= again_bit;
143 if(yes) appdata->dialog_again.reply |= again_bit;
144 else appdata->dialog_again.reply &= ~again_bit;
145 }
146
147 gtk_widget_destroy(dialog);
148
149 g_free(buf);
150 return yes;
151 }
152
153 static const char *data_paths[] = {
154 "~/." PACKAGE, // in home directory
155 DATADIR , // final installation path
156 #ifdef USE_HILDON
157 "/media/mmc1/" PACKAGE, // path to external memory card
158 "/media/mmc2/" PACKAGE, // path to internal memory card
159 #endif
160 "./data", "../data", // local paths for testing
161 NULL
162 };
163
164 char *find_file(char *name) {
165 const char **path = data_paths;
166 char *p = getenv("HOME");
167
168 while(*path) {
169 char *full_path = NULL;
170
171 if(*path[0] == '~')
172 full_path = g_strdup_printf("%s/%s/%s", p, *path+2, name);
173 else
174 full_path = g_strdup_printf("%s/%s", *path, name);
175
176 if(g_file_test(full_path, G_FILE_TEST_IS_REGULAR))
177 return full_path;
178
179 g_free(full_path);
180 path++;
181 }
182
183 return NULL;
184 }
185
186 /* scan all data directories for the given file pattern and */
187 /* return a list of files matching this pattern */
188 file_chain_t *file_scan(char *pattern) {
189 file_chain_t *chain = NULL, **chainP = &chain;
190
191 const char **path = data_paths;
192 char *p = getenv("HOME");
193
194 while(*path) {
195 GDir *dir = NULL;
196
197 /* scan for projects */
198 const char *dirname = *path;
199
200 if(*path[0] == '~')
201 dirname = g_strdup_printf("%s/%s", p, *path+2);
202
203 if((dir = g_dir_open(dirname, 0, NULL))) {
204 const char *name = NULL;
205 do {
206 name = g_dir_read_name(dir);
207
208 if(name) {
209 char *fullname = g_strdup_printf("%s/%s", dirname, name);
210 if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR)) {
211 if(g_pattern_match_simple(pattern, name)) {
212 *chainP = g_new0(file_chain_t, 1);
213 (*chainP)->name = fullname;
214 chainP = &(*chainP)->next;
215 } else
216 g_free(fullname);
217 } else
218 g_free(fullname);
219 }
220 } while(name);
221
222 g_dir_close(dir);
223
224 if(*path[0] == '~')
225 g_free((char*)dirname);
226 }
227
228 path++;
229 }
230
231 return chain;
232 }
233
234
235 #ifdef USE_HILDON
236 static const gint dialog_sizes[][2] = {
237 { 400, 100 }, // SMALL
238 #if MAEMO_VERSION_MAJOR < 5
239 { 450, 300 }, // MEDIUM
240 { 800, 480 }, // LARGE
241 #else
242 /* in maemo5 most dialogs are full screen */
243 { 800, 480 }, // MEDIUM
244 { 800, 380 }, // LARGE
245 #endif
246 { 640, 100 }, // WIDE
247 { 450, 480 }, // HIGH
248 };
249 #else
250 static const gint dialog_sizes[][2] = {
251 { 300, 100 }, // SMALL
252 { 400, 300 }, // MEDIUM
253 { 500, 350 }, // LARGE
254 { 450, 100 }, // WIDE
255 { 200, 350 }, // HIGH
256 };
257 #endif
258
259 /* create a modal dialog using one of the predefined size hints */
260 GtkWidget *misc_dialog_new(guint hint, const char *title,
261 GtkWindow *parent, ...) {
262 va_list args;
263 va_start( args, parent );
264
265 /* create dialog itself */
266 GtkWidget *dialog = gtk_dialog_new();
267
268 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
269 if(title) gtk_window_set_title(GTK_WINDOW(dialog), title);
270 if(parent) gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
271
272 const gchar *button_text = va_arg(args, const gchar *);
273 while(button_text) {
274 gtk_dialog_add_button(GTK_DIALOG(dialog), button_text, va_arg(args, gint));
275 button_text = va_arg(args, const gchar *);
276 }
277
278 va_end( args );
279
280 if(hint != MISC_DIALOG_NOSIZE)
281 gtk_window_set_default_size(GTK_WINDOW(dialog),
282 dialog_sizes[hint][0], dialog_sizes[hint][1]);
283
284 return dialog;
285 }
286
287 #if defined(USE_HILDON) && (MAEMO_VERSION_MAJOR == 5)
288 #include <hildon/hildon-pannable-area.h>
289 /* create a pannable area */
290 GtkWidget *misc_scrolled_window_new(gboolean etched_in) {
291 return hildon_pannable_area_new();
292 }
293
294 void misc_scrolled_window_add_with_viewport(GtkWidget *win, GtkWidget *child) {
295 hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(win), child);
296 }
297
298 #else
299 /* create a scrolled window */
300 GtkWidget *misc_scrolled_window_new(gboolean etched_in) {
301 GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
302 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
303 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
304 if(etched_in)
305 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window),
306 GTK_SHADOW_ETCHED_IN);
307 return scrolled_window;
308 }
309
310 void misc_scrolled_window_add_with_viewport(GtkWidget *win, GtkWidget *child) {
311 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(win), child);
312 }
313
314
315 #endif
316
317 const char *misc_get_proxy_uri(settings_t *settings) {
318 static char proxy_buffer[64];
319
320 /* use environment settings if preset */
321 const char *proxy = g_getenv("http_proxy");
322 if(proxy) {
323 printf("http_proxy: %s\n", proxy);
324 return proxy;
325 }
326
327 /* otherwise try settings */
328 if(!settings || !settings->proxy ||
329 !settings->proxy->host) return NULL;
330
331 snprintf(proxy_buffer, sizeof(proxy_buffer), "%s%s:%u",
332 strncmp(settings->proxy->host, "http://", 7)?"http://":"",
333 settings->proxy->host, settings->proxy->port);
334
335 proxy_buffer[sizeof(proxy_buffer)-1] = 0;
336 printf("gconf_proxy: %s\n", proxy_buffer);
337 return proxy_buffer;
338 }
339
340 void misc_table_attach(GtkWidget *table, GtkWidget *widget, int x, int y) {
341 gtk_table_attach_defaults(GTK_TABLE(table), widget, x, x+1, y, y+1);
342 }
343
344 /* ---------- unified widgets for fremantle/others --------------- */
345
346 GtkWidget *entry_new(void) {
347 #ifndef FREMANTLE
348 return gtk_entry_new();
349 #else
350 return hildon_entry_new(HILDON_SIZE_AUTO);
351 #endif
352 }
353
354 GtkWidget *button_new(void) {
355 GtkWidget *button = gtk_button_new();
356 #ifdef FREMANTLE
357 hildon_gtk_widget_set_theme_size(button,
358 (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
359 #endif
360 return button;
361 }
362
363 GtkWidget *button_new_with_label(char *label) {
364 GtkWidget *button = gtk_button_new_with_label(label);
365 #ifdef FREMANTLE
366 hildon_gtk_widget_set_theme_size(button,
367 (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
368 #endif
369 return button;
370 }
371
372 GtkWidget *check_button_new_with_label(char *label) {
373 #ifndef FREMANTLE
374 return gtk_check_button_new_with_label(label);
375 #else
376 GtkWidget *cbut =
377 hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT |
378 HILDON_SIZE_AUTO_WIDTH);
379 gtk_button_set_label(GTK_BUTTON(cbut), label);
380 return cbut;
381 #endif
382 }
383
384 void check_button_set_active(GtkWidget *button, gboolean active) {
385 #ifndef FREMANTLE
386 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
387 #else
388 hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active);
389 #endif
390 }
391
392 gboolean check_button_get_active(GtkWidget *button) {
393 #ifndef FREMANTLE
394 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
395 #else
396 return hildon_check_button_get_active(HILDON_CHECK_BUTTON(button));
397 #endif
398 }
399
400 GtkWidget *notebook_new(void) {
401 #ifdef FREMANTLE
402 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
403
404 GtkWidget *notebook = gtk_notebook_new();
405
406 /* solution for fremantle: we use a row of ordinary buttons instead */
407 /* of regular tabs */
408
409 /* hide the regular tabs */
410 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
411
412 gtk_box_pack_start_defaults(GTK_BOX(vbox), notebook);
413
414 /* store a reference to the notebook in the vbox */
415 g_object_set_data(G_OBJECT(vbox), "notebook", (gpointer)notebook);
416
417 /* create a hbox for the buttons */
418 GtkWidget *hbox = gtk_hbox_new(TRUE, 0);
419 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
420 g_object_set_data(G_OBJECT(vbox), "hbox", (gpointer)hbox);
421
422 return vbox;
423 #else
424 return gtk_notebook_new();
425 #endif
426 }
427
428 GtkWidget *notebook_get_gtk_notebook(GtkWidget *notebook) {
429 #ifdef FREMANTLE
430 return GTK_WIDGET(g_object_get_data(G_OBJECT(notebook), "notebook"));
431 #else
432 return notebook;
433 #endif
434 }
435
436
437 #ifdef FREMANTLE
438 static void on_notebook_button_clicked(GtkWidget *button, gpointer data) {
439 GtkNotebook *nb =
440 GTK_NOTEBOOK(g_object_get_data(G_OBJECT(data), "notebook"));
441
442 gint page = (gint)g_object_get_data(G_OBJECT(button), "page");
443 gtk_notebook_set_current_page(nb, page);
444 }
445 #endif
446
447 void notebook_append_page(GtkWidget *notebook,
448 GtkWidget *page, char *label) {
449 #ifdef FREMANTLE
450 GtkNotebook *nb =
451 GTK_NOTEBOOK(g_object_get_data(G_OBJECT(notebook), "notebook"));
452
453 gint page_num = gtk_notebook_append_page(nb, page, gtk_label_new(label));
454 GtkWidget *button = NULL;
455
456 /* select button for page 0 by default */
457 if(!page_num) {
458 button = gtk_radio_button_new_with_label(NULL, label);
459 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
460 g_object_set_data(G_OBJECT(notebook), "group_master", (gpointer)button);
461 } else {
462 GtkWidget *master = g_object_get_data(G_OBJECT(notebook), "group_master");
463 button = gtk_radio_button_new_with_label_from_widget(
464 GTK_RADIO_BUTTON(master), label);
465 }
466
467 gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(button), FALSE);
468 g_object_set_data(G_OBJECT(button), "page", (gpointer)page_num);
469
470 gtk_signal_connect(GTK_OBJECT(button), "clicked",
471 GTK_SIGNAL_FUNC(on_notebook_button_clicked), notebook);
472
473 #if defined(USE_HILDON) && (MAEMO_VERSION_MAJOR == 5)
474 hildon_gtk_widget_set_theme_size(button,
475 (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
476 #endif
477
478 gtk_box_pack_start_defaults(
479 GTK_BOX(g_object_get_data(G_OBJECT(notebook), "hbox")),
480 button);
481
482 #else
483 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page, gtk_label_new(label));
484 #endif
485 }
486