Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


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