Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 172 - (show annotations)
Tue Apr 28 14:37:34 2009 UTC (15 years ago) by harbaum
File MIME type: text/plain
File size: 7076 byte(s)
Thumb gui for maemo5
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 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 "~/." PACKAGE, // in home directory
134 PREFIX "/share/" PACKAGE , // final installation path
135 #ifdef USE_HILDON
136 "/media/mmc1/" PACKAGE, // path to external memory card
137 "/media/mmc2/" PACKAGE, // path to internal memory card
138 #endif
139 "./data", "../data", // local paths for testing
140 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
213
214 #ifdef USE_HILDON
215 static const gint dialog_sizes[][2] = {
216 { 400, 100 }, // SMALL
217 { 450, 300 }, // MEDIUM
218 #if MAEMO_VERSION_MAJOR < 5
219 { 800, 480 }, // LARGE
220 #else
221 { 800, 380 }, // LARGE
222 #endif
223 { 640, 100 }, // WIDE
224 { 0, 0 }, // HIGH
225 };
226 #else
227 static const gint dialog_sizes[][2] = {
228 { 300, 100 }, // SMALL
229 { 400, 300 }, // MEDIUM
230 { 500, 350 }, // LARGE
231 { 450, 100 }, // WIDE
232 { 0, 0 }, // HIGH
233 };
234 #endif
235
236 /* create a modal dialog using one of the predefined size hints */
237 GtkWidget *misc_dialog_new(guint hint, const char *title,
238 GtkWindow *parent, ...) {
239 va_list args;
240 va_start( args, parent );
241
242 /* create dialog itself */
243 GtkWidget *dialog = gtk_dialog_new();
244
245 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
246 if(title) gtk_window_set_title(GTK_WINDOW(dialog), title);
247 if(parent) gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
248
249 const gchar *button_text = va_arg(args, const gchar *);
250 while(button_text) {
251 gtk_dialog_add_button(GTK_DIALOG(dialog), button_text, va_arg(args, gint));
252 button_text = va_arg(args, const gchar *);
253 }
254
255 va_end( args );
256
257 if(hint != MISC_DIALOG_NOSIZE)
258 gtk_window_set_default_size(GTK_WINDOW(dialog),
259 dialog_sizes[hint][0], dialog_sizes[hint][1]);
260
261 return dialog;
262 }