Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


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