9
|
1 /*
|
|
2 * (SLIK) SimpLIstic sKin functions
|
|
3 * (C) 2004 John Ellis
|
|
4 *
|
|
5 * Author: John Ellis
|
|
6 *
|
|
7 * This software is released under the GNU General Public License (GNU GPL).
|
|
8 * Please read the included file COPYING for more information.
|
|
9 * This software comes with no warranty of any kind, use at your own risk!
|
|
10 */
|
|
11
|
|
12 #ifdef HAVE_CONFIG_H
|
|
13 # include "config.h"
|
|
14 #endif
|
|
15 #include "intl.h"
|
|
16
|
|
17 #include <stdio.h>
|
|
18 #include <stdlib.h>
|
|
19 #include <string.h>
|
|
20
|
|
21 #include <gtk/gtk.h>
|
|
22 #include <gdk/gdkkeysyms.h>
|
|
23
|
|
24 #include "ui_misc.h"
|
|
25
|
|
26 #include "ui_bookmark.h"
|
|
27
|
|
28
|
|
29 /*
|
|
30 *-----------------------------------------------------------------------------
|
|
31 * widget and layout utilities
|
|
32 *-----------------------------------------------------------------------------
|
|
33 */
|
|
34
|
|
35 GtkWidget *pref_box_new(GtkWidget *parent_box, gint fill,
|
|
36 GtkOrientation orientation, gint padding)
|
|
37 {
|
|
38 GtkWidget *box;
|
|
39
|
|
40 if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
41 {
|
|
42 box = gtk_hbox_new(FALSE, padding);
|
|
43 }
|
|
44 else
|
|
45 {
|
|
46 box = gtk_vbox_new(FALSE, padding);
|
|
47 }
|
|
48
|
|
49 gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
|
|
50 gtk_widget_show(box);
|
|
51
|
|
52 return box;
|
|
53 }
|
|
54
|
|
55 GtkWidget *pref_group_new(GtkWidget *parent_box, gint fill,
|
|
56 const gchar *text, GtkOrientation orientation)
|
|
57 {
|
|
58 GtkWidget *box;
|
|
59 GtkWidget *vbox;
|
|
60 GtkWidget *hbox;
|
|
61 GtkWidget *label;
|
|
62
|
|
63 vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
|
|
64
|
|
65 /* add additional spacing if necessary */
|
|
66 if (GTK_IS_VBOX(parent_box) && GTK_BOX(parent_box)->children != NULL)
|
|
67 {
|
|
68 pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
|
|
69 }
|
|
70
|
|
71 gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
|
|
72 gtk_widget_show(vbox);
|
|
73
|
|
74 label = gtk_label_new(text);
|
|
75 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
|
|
76 pref_label_bold(label, TRUE, FALSE);
|
|
77
|
|
78 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
|
|
79 gtk_widget_show(label);
|
|
80
|
|
81 hbox = gtk_hbox_new(FALSE, PREF_PAD_INDENT);
|
|
82 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
|
|
83 gtk_widget_show(hbox);
|
|
84
|
|
85 /* indent using empty box */
|
|
86 pref_spacer(hbox, 0);
|
|
87
|
|
88 if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
89 {
|
|
90 box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
|
|
91 }
|
|
92 else
|
|
93 {
|
|
94 box = gtk_vbox_new(FALSE, PREF_PAD_GAP);
|
|
95 }
|
|
96 gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
|
|
97 gtk_widget_show(box);
|
|
98
|
|
99 g_object_set_data(G_OBJECT(box), "pref_group", vbox);
|
|
100
|
|
101 return box;
|
|
102 }
|
|
103
|
|
104 GtkWidget *pref_group_parent(GtkWidget *child)
|
|
105 {
|
|
106 GtkWidget *parent;
|
|
107
|
|
108 parent = child;
|
|
109 while (parent)
|
|
110 {
|
|
111 GtkWidget *group;
|
|
112
|
|
113 group = g_object_get_data(G_OBJECT(parent), "pref_group");
|
|
114 if (group && GTK_IS_WIDGET(group)) return group;
|
|
115
|
|
116 parent = gtk_widget_get_parent(parent);
|
|
117 }
|
|
118
|
|
119 return child;
|
|
120 }
|
|
121
|
|
122 GtkWidget *pref_frame_new(GtkWidget *parent_box, gint fill,
|
|
123 const gchar *text,
|
|
124 GtkOrientation orientation, gint padding)
|
|
125 {
|
|
126 GtkWidget *box;
|
|
127 GtkWidget *frame = NULL;
|
|
128
|
|
129 frame = gtk_frame_new(text);
|
|
130 gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
|
|
131 gtk_widget_show(frame);
|
|
132
|
|
133 if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
134 {
|
|
135 box = gtk_hbox_new(FALSE, padding);
|
|
136 }
|
|
137 else
|
|
138 {
|
|
139 box = gtk_vbox_new(FALSE, padding);
|
|
140 }
|
|
141 gtk_container_add(GTK_CONTAINER(frame), box);
|
|
142 gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
|
|
143 gtk_widget_show(box);
|
|
144
|
|
145 return box;
|
|
146 }
|
|
147
|
|
148 GtkWidget *pref_spacer(GtkWidget *parent_box, gint padding)
|
|
149 {
|
|
150 GtkWidget *spacer;
|
|
151
|
|
152 spacer = gtk_hbox_new(FALSE, 0);
|
|
153 gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
|
|
154 gtk_widget_show(spacer);
|
|
155
|
|
156 return spacer;
|
|
157 }
|
|
158
|
|
159 GtkWidget *pref_line(GtkWidget *parent_box, gint padding)
|
|
160 {
|
|
161 GtkWidget *spacer;
|
|
162
|
|
163 if (GTK_IS_HBOX(parent_box))
|
|
164 {
|
|
165 spacer = gtk_vseparator_new();
|
|
166 }
|
|
167 else
|
|
168 {
|
|
169 spacer = gtk_hseparator_new();
|
|
170 }
|
|
171
|
|
172 gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
|
|
173 gtk_widget_show(spacer);
|
|
174
|
|
175 return spacer;
|
|
176 }
|
|
177
|
|
178 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
|
|
179 {
|
|
180 GtkWidget *label;
|
|
181
|
|
182 label = gtk_label_new(text);
|
|
183 gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
|
|
184 gtk_widget_show(label);
|
|
185
|
|
186 return label;
|
|
187 }
|
|
188
|
|
189 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
|
|
190 {
|
|
191 GtkWidget *label;
|
|
192
|
|
193 label = gtk_label_new_with_mnemonic(text);
|
|
194 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
|
|
195 gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
|
|
196 gtk_widget_show(label);
|
|
197
|
|
198 return label;
|
|
199 }
|
|
200
|
|
201 void pref_label_bold(GtkWidget *label, gint bold, gint increase_size)
|
|
202 {
|
|
203 PangoAttrList *pal;
|
|
204 PangoAttribute *pa;
|
|
205
|
|
206 if (!bold && !increase_size) return;
|
|
207
|
|
208 pal = pango_attr_list_new();
|
|
209
|
|
210 if (bold)
|
|
211 {
|
|
212 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
|
|
213 pa->start_index = 0;
|
|
214 pa->end_index = G_MAXINT;
|
|
215 pango_attr_list_insert(pal, pa);
|
|
216 }
|
|
217
|
|
218 if (increase_size)
|
|
219 {
|
|
220 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
|
|
221 pa->start_index = 0;
|
|
222 pa->end_index = G_MAXINT;
|
|
223 pango_attr_list_insert(pal, pa);
|
|
224 }
|
|
225
|
|
226 gtk_label_set_attributes(GTK_LABEL(label), pal);
|
|
227 pango_attr_list_unref(pal);
|
|
228 }
|
|
229
|
|
230 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *stock_id,
|
|
231 const gchar *text, gint hide_stock_text,
|
|
232 GCallback func, gpointer data)
|
|
233 {
|
|
234 GtkWidget *button;
|
|
235
|
|
236 if (stock_id && !text && !hide_stock_text)
|
|
237 {
|
|
238 button = gtk_button_new_from_stock(stock_id);
|
|
239 }
|
|
240 else
|
|
241 {
|
|
242 GtkWidget *image = NULL;
|
|
243 GtkWidget *label = NULL;
|
|
244
|
|
245 button = gtk_button_new();
|
|
246
|
|
247 if (stock_id) image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
|
|
248 if (text)
|
|
249 {
|
|
250 label = gtk_label_new_with_mnemonic(text);
|
|
251 gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
|
|
252 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
|
|
253 }
|
|
254
|
|
255 if (image && label)
|
|
256 {
|
|
257 GtkWidget *align;
|
|
258 GtkWidget *hbox;
|
|
259
|
|
260 hbox = gtk_hbox_new (FALSE, PREF_PAD_BUTTON_ICON_GAP);
|
|
261
|
|
262 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
|
|
263 gtk_container_add(GTK_CONTAINER(button), align);
|
|
264 gtk_widget_show(align);
|
|
265
|
|
266 gtk_container_add(GTK_CONTAINER(align), hbox);
|
|
267 gtk_widget_show(hbox);
|
|
268
|
|
269 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
|
|
270 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
|
|
271 }
|
|
272 else
|
|
273 {
|
|
274 if (image)
|
|
275 {
|
|
276 gtk_container_add(GTK_CONTAINER(button), image);
|
|
277 }
|
|
278 else if (label)
|
|
279 {
|
|
280 gtk_container_add(GTK_CONTAINER(button), label);
|
|
281 }
|
|
282 }
|
|
283
|
|
284 if (image) gtk_widget_show(image);
|
|
285 if (label) gtk_widget_show(label);
|
|
286 }
|
|
287
|
|
288 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
|
|
289
|
|
290 if (parent_box)
|
|
291 {
|
|
292 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
|
|
293 gtk_widget_show(button);
|
|
294 }
|
|
295
|
|
296 return button;
|
|
297 }
|
|
298
|
|
299 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gint mnemonic_text,
|
|
300 gint active, GCallback func, gpointer data)
|
|
301 {
|
|
302 GtkWidget *button;
|
|
303
|
|
304 if (mnemonic_text)
|
|
305 {
|
|
306 button = gtk_check_button_new_with_mnemonic(text);
|
|
307 }
|
|
308 else
|
|
309 {
|
|
310 button = gtk_check_button_new_with_label(text);
|
|
311 }
|
|
312 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
|
|
313 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
|
|
314
|
|
315 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
|
|
316 gtk_widget_show(button);
|
|
317
|
|
318 return button;
|
|
319 }
|
|
320
|
|
321 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gint active,
|
|
322 GCallback func, gpointer data)
|
|
323 {
|
|
324 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
|
|
325 }
|
|
326
|
|
327 GtkWidget *pref_checkbox_new_mnemonic(GtkWidget *parent_box, const gchar *text, gint active,
|
|
328 GCallback func, gpointer data)
|
|
329 {
|
|
330 return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
|
|
331 }
|
|
332
|
|
333 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
|
|
334 {
|
|
335 gint *result = data;
|
|
336
|
|
337 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
|
|
338 }
|
|
339
|
|
340 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gint active,
|
|
341 gint *result)
|
|
342 {
|
|
343 GtkWidget *button;
|
|
344
|
|
345 button = pref_checkbox_new(parent_box, text, active,
|
|
346 G_CALLBACK(pref_checkbox_int_cb), result);
|
|
347 *result = active;
|
|
348
|
|
349 return button;
|
|
350 }
|
|
351
|
|
352 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
|
|
353 {
|
|
354 GtkWidget *widget = data;
|
|
355
|
|
356 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
|
|
357 }
|
|
358
|
|
359 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
|
|
360 {
|
|
361 g_signal_connect(G_OBJECT(button), "toggled",
|
|
362 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
|
|
363
|
|
364 pref_checkbox_link_sensitivity_cb(button, widget);
|
|
365 }
|
|
366
|
|
367 static void pref_checkbox_link_sensitivity_swap_cb(GtkWidget *button, gpointer data)
|
|
368 {
|
|
369 GtkWidget *widget = data;
|
|
370
|
|
371 gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
|
|
372 }
|
|
373
|
|
374 void pref_checkbox_link_sensitivity_swap(GtkWidget *button, GtkWidget *widget)
|
|
375 {
|
|
376 g_signal_connect(G_OBJECT(button), "toggled",
|
|
377 G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb), widget);
|
|
378
|
|
379 pref_checkbox_link_sensitivity_swap_cb(button, widget);
|
|
380 }
|
|
381
|
|
382 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
|
|
383 const gchar *text, gint mnemonic_text, gint active,
|
|
384 GCallback func, gpointer data)
|
|
385 {
|
|
386 GtkWidget *button;
|
|
387 GSList* group;
|
|
388
|
|
389 if (sibling)
|
|
390 {
|
|
391 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
|
|
392 }
|
|
393 else
|
|
394 {
|
|
395 group = NULL;
|
|
396 }
|
|
397
|
|
398 if (mnemonic_text)
|
|
399 {
|
|
400 button = gtk_radio_button_new_with_mnemonic(group, text);
|
|
401 }
|
|
402 else
|
|
403 {
|
|
404 button = gtk_radio_button_new_with_label(group, text);
|
|
405 }
|
|
406
|
|
407 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
|
|
408 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
|
|
409
|
|
410 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
|
|
411 gtk_widget_show(button);
|
|
412
|
|
413 return button;
|
|
414 }
|
|
415
|
|
416 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
|
|
417 const gchar *text, gint active,
|
|
418 GCallback func, gpointer data)
|
|
419 {
|
|
420 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
|
|
421 }
|
|
422
|
|
423 GtkWidget *pref_radiobutton_new_mnemonic(GtkWidget *parent_box, GtkWidget *sibling,
|
|
424 const gchar *text, gint active,
|
|
425 GCallback func, gpointer data)
|
|
426 {
|
|
427 return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
|
|
428 }
|
|
429
|
|
430 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
|
|
431
|
|
432 static void pref_radiobutton_int_cb(GtkWidget *widget, gpointer data)
|
|
433 {
|
|
434 gint *result = data;
|
|
435
|
|
436 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
|
|
437 {
|
|
438 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
|
|
439 }
|
|
440 }
|
|
441
|
|
442 GtkWidget *pref_radiobutton_new_int(GtkWidget *parent_box, GtkWidget *sibling,
|
|
443 const gchar *text, gint active,
|
|
444 gint *result, gint value,
|
|
445 GCallback func, gpointer data)
|
|
446 {
|
|
447 GtkWidget *button;
|
|
448
|
|
449 button = pref_radiobutton_new(parent_box, sibling, text, active,
|
|
450 G_CALLBACK(pref_radiobutton_int_cb), result);
|
|
451 g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
|
|
452 if (active) *result = value;
|
|
453
|
|
454 return button;
|
|
455 }
|
|
456
|
|
457 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
|
|
458 gint mnemonic_text,
|
|
459 gdouble min, gdouble max, gdouble step, gint digits,
|
|
460 gdouble value,
|
|
461 GCallback func, gpointer data)
|
|
462 {
|
|
463 GtkWidget *spin;
|
|
464 GtkWidget *box;
|
|
465 GtkWidget *label;
|
|
466
|
|
467 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
|
|
468
|
|
469 spin = gtk_spin_button_new_with_range(min, max, step);
|
|
470 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
|
|
471 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
|
|
472
|
|
473 if (func)
|
|
474 {
|
|
475 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
|
|
476 }
|
|
477
|
|
478 if (text)
|
|
479 {
|
|
480 if (mnemonic_text)
|
|
481 {
|
|
482 label = pref_label_new_mnemonic(box, text, spin);
|
|
483 }
|
|
484 else
|
|
485 {
|
|
486 label = pref_label_new(box, text);
|
|
487 }
|
|
488 pref_link_sensitivity(label, spin);
|
|
489 }
|
|
490
|
|
491 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
|
|
492 gtk_widget_show(spin);
|
|
493
|
|
494 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
|
|
495 if (suffix)
|
|
496 {
|
|
497 label = pref_label_new(box, suffix);
|
|
498 pref_link_sensitivity(label, spin);
|
|
499 }
|
|
500
|
|
501 return spin;
|
|
502 }
|
|
503
|
|
504 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
|
|
505 gdouble min, gdouble max, gdouble step, gint digits,
|
|
506 gdouble value,
|
|
507 GCallback func, gpointer data)
|
|
508 {
|
|
509 return real_pref_spin_new(parent_box, text, suffix, FALSE,
|
|
510 min, max, step, digits, value, func, data);
|
|
511 }
|
|
512
|
|
513 GtkWidget *pref_spin_new_mnemonic(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
|
|
514 gdouble min, gdouble max, gdouble step, gint digits,
|
|
515 gdouble value,
|
|
516 GCallback func, gpointer data)
|
|
517 {
|
|
518 return real_pref_spin_new(parent_box, text, suffix, TRUE,
|
|
519 min, max, step, digits, value, func, data);
|
|
520 }
|
|
521
|
|
522 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
|
|
523 {
|
|
524 gint *var = data;
|
|
525 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
|
|
526 }
|
|
527
|
|
528 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
|
|
529 gint min, gint max, gint step,
|
|
530 gint value, gint *value_var)
|
|
531 {
|
|
532 *value_var = value;
|
|
533 return pref_spin_new(parent_box, text, suffix,
|
|
534 (gdouble)min, (gdouble)max, (gdouble)step, 0,
|
|
535 value,
|
|
536 G_CALLBACK(pref_spin_int_cb), value_var);
|
|
537 }
|
|
538
|
|
539 #if 0
|
|
540 void pref_spin_set_blocking(GtkWidget *spin, gdouble value, gpointer block_data)
|
|
541 {
|
|
542 g_signal_handlers_block_matched(G_OBJECT(spin), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, block_data);
|
|
543 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
|
|
544 g_signal_handlers_unblock_matched(G_OBJECT(spin), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, block_data);
|
|
545 }
|
|
546 #endif
|
|
547
|
|
548 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType prev_state, gpointer data)
|
|
549 {
|
|
550 GtkWidget *widget = data;
|
|
551
|
|
552 gtk_widget_set_sensitive(widget, GTK_WIDGET_IS_SENSITIVE(watch));
|
|
553 }
|
|
554
|
|
555 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
|
|
556 {
|
|
557 g_signal_connect(G_OBJECT(watch), "state_changed",
|
|
558 G_CALLBACK(pref_link_sensitivity_cb), widget);
|
|
559 }
|
|
560
|
|
561 void pref_signal_block_data(GtkWidget *widget, gpointer data)
|
|
562 {
|
|
563 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
|
|
564 0, 0, NULL, NULL, data);
|
|
565 }
|
|
566
|
|
567 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
|
|
568 {
|
|
569 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
|
|
570 0, 0, NULL, NULL, data);
|
|
571 }
|
|
572
|
|
573 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
|
|
574 gint homogenious, gint fill)
|
|
575 {
|
|
576 GtkWidget *table;
|
|
577
|
|
578 table = gtk_table_new(rows, columns, homogenious);
|
|
579 gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
|
|
580 gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
|
|
581
|
|
582 if (parent_box)
|
|
583 {
|
|
584 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
|
|
585 gtk_widget_show(table);
|
|
586 }
|
|
587
|
|
588 return table;
|
|
589 }
|
|
590
|
|
591 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
|
|
592 GtkOrientation orientation, const gchar *text)
|
|
593 {
|
|
594 GtkWidget *box;
|
|
595 GtkWidget *shell;
|
|
596
|
|
597 if (text)
|
|
598 {
|
|
599 shell = gtk_vbox_new(FALSE, 0);
|
|
600 box = pref_group_new(shell, TRUE, text, orientation);
|
|
601 }
|
|
602 else
|
|
603 {
|
|
604 if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
605 {
|
|
606 box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
|
|
607 }
|
|
608 else
|
|
609 {
|
|
610 box = gtk_vbox_new(FALSE, PREF_PAD_GAP);
|
|
611 }
|
|
612 shell = box;
|
|
613 }
|
|
614
|
|
615 gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
|
|
616 GTK_EXPAND | GTK_FILL, FALSE, 0, 0);
|
|
617
|
|
618 gtk_widget_show(shell);
|
|
619
|
|
620 return box;
|
|
621 }
|
|
622
|
|
623 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
|
|
624 const gchar *text, gfloat alignment)
|
|
625 {
|
|
626 GtkWidget *label;
|
|
627 GtkWidget *align;
|
|
628
|
|
629 align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
|
|
630 gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
|
|
631 GTK_FILL, FALSE, 0, 0);
|
|
632 gtk_widget_show(align);
|
|
633 label = gtk_label_new(text);
|
|
634 gtk_container_add(GTK_CONTAINER(align), label);
|
|
635 gtk_widget_show(label);
|
|
636
|
|
637 return label;
|
|
638 }
|
|
639
|
|
640 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
|
|
641 const gchar *stock_id, const gchar *text, gint hide_stock_text,
|
|
642 GCallback func, gpointer data)
|
|
643 {
|
|
644 GtkWidget *button;
|
|
645
|
|
646 button = pref_button_new(NULL, stock_id, text, hide_stock_text, func, data);
|
|
647 gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
|
|
648 GTK_FILL, FALSE, 0, 0);
|
|
649 gtk_widget_show(button);
|
|
650
|
|
651 return button;
|
|
652 }
|
|
653
|
|
654 #if 0
|
|
655 static GtkWidget *pref_table_checkbox(GtkWidget *table, gint column, gint row,
|
|
656 const gchar *text, gint active,
|
|
657 GCallback func, gpointer data)
|
|
658 {
|
|
659 GtkWidget *button;
|
|
660
|
|
661 button = gtk_check_button_new_with_label(text);
|
|
662 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
|
|
663 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
|
|
664
|
|
665 gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
|
|
666 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
|
|
667 gtk_widget_show(button);
|
|
668
|
|
669 return button;
|
|
670 }
|
|
671 #endif
|
|
672
|
|
673 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
|
|
674 const gchar *text, const gchar *suffix,
|
|
675 gdouble min, gdouble max, gdouble step, gint digits,
|
|
676 gdouble value,
|
|
677 GCallback func, gpointer data)
|
|
678 {
|
|
679 GtkWidget *spin;
|
|
680 GtkWidget *box;
|
|
681 GtkWidget *label;
|
|
682
|
|
683 spin = gtk_spin_button_new_with_range(min, max, step);
|
|
684 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
|
|
685 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
|
|
686 if (func)
|
|
687 {
|
|
688 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
|
|
689 }
|
|
690
|
|
691 if (text)
|
|
692 {
|
|
693 label = pref_table_label(table, column, row, text, 1.0);
|
|
694 pref_link_sensitivity(label, spin);
|
|
695 column++;
|
|
696 }
|
|
697
|
|
698 if (suffix)
|
|
699 {
|
|
700 box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
|
|
701 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
|
|
702 gtk_widget_show(spin);
|
|
703
|
|
704 label = pref_label_new(box, suffix);
|
|
705 pref_link_sensitivity(label, spin);
|
|
706 }
|
|
707 else
|
|
708 {
|
|
709 box = spin;
|
|
710 }
|
|
711
|
|
712 gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
|
|
713 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
|
|
714 gtk_widget_show(box);
|
|
715
|
|
716 return spin;
|
|
717 }
|
|
718
|
|
719 static void pref_toolbar_destroy_cb(GtkWidget *widget, gpointer data)
|
|
720 {
|
|
721 GtkTooltips *tips = data;
|
|
722
|
|
723 g_object_unref(G_OBJECT(tips));
|
|
724 }
|
|
725
|
|
726 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
|
|
727 {
|
|
728 GtkWidget *tbar;
|
|
729 GtkTooltips *tips;
|
|
730
|
|
731 tbar = gtk_toolbar_new();
|
|
732 gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
|
|
733
|
|
734 if (parent_box)
|
|
735 {
|
|
736 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
|
|
737 gtk_widget_show(tbar);
|
|
738 }
|
|
739
|
|
740 tips = gtk_tooltips_new();
|
|
741 g_object_ref(G_OBJECT(tips));
|
|
742 gtk_object_sink(GTK_OBJECT(tips));
|
|
743 g_object_set_data(G_OBJECT(tbar), "tooltips", tips);
|
|
744 g_signal_connect(G_OBJECT(tbar), "destroy",
|
|
745 G_CALLBACK(pref_toolbar_destroy_cb), tips);
|
|
746
|
|
747 gtk_tooltips_enable(tips);
|
|
748
|
|
749 return tbar;
|
|
750 }
|
|
751
|
|
752 GtkWidget *pref_toolbar_button(GtkWidget *toolbar, const gchar *stock_id, const gchar *label,
|
|
753 const gchar *description,
|
|
754 GCallback func, gpointer data)
|
|
755 {
|
|
756 GtkWidget *item;
|
|
757
|
|
758 item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
|
|
759 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
|
|
760
|
|
761 if (func) g_signal_connect(item, "clicked", func, data);
|
|
762 gtk_container_add(GTK_CONTAINER(toolbar), item);
|
|
763 gtk_widget_show(item);
|
|
764
|
|
765 if (description)
|
|
766 {
|
|
767 GtkTooltips *tips;
|
|
768
|
|
769 tips = g_object_get_data(G_OBJECT(toolbar), "tooltips");
|
|
770 gtk_tool_item_set_tooltip(GTK_TOOL_ITEM(item), tips, description, NULL);
|
|
771 }
|
|
772
|
|
773 return item;
|
|
774 }
|
|
775
|
|
776 GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar)
|
|
777 {
|
|
778 GtkWidget *item;
|
|
779
|
|
780 item = GTK_WIDGET(gtk_separator_tool_item_new());
|
|
781 gtk_container_add(GTK_CONTAINER(toolbar), item);
|
|
782 gtk_widget_show(item);
|
|
783
|
|
784 return item;
|
|
785 }
|
|
786
|
|
787
|
|
788 /*
|
|
789 *-----------------------------------------------------------------------------
|
|
790 * date selection entry
|
|
791 *-----------------------------------------------------------------------------
|
|
792 */
|
|
793
|
|
794 #define DATE_SELECION_KEY "date_selection_data"
|
|
795
|
|
796
|
|
797 typedef struct _DateSelection DateSelection;
|
|
798 struct _DateSelection
|
|
799 {
|
|
800 GtkWidget *box;
|
|
801
|
|
802 GtkWidget *spin_d;
|
|
803 GtkWidget *spin_m;
|
|
804 GtkWidget *spin_y;
|
|
805
|
|
806 GtkWidget *button;
|
|
807
|
|
808 GtkWidget *window;
|
|
809 GtkWidget *calendar;
|
|
810 };
|
|
811
|
|
812
|
|
813 static void date_selection_popup_hide(DateSelection *ds)
|
|
814 {
|
|
815 if (!ds->window) return;
|
|
816
|
|
817 if (GTK_WIDGET_HAS_GRAB(ds->window))
|
|
818 {
|
|
819 gtk_grab_remove(ds->window);
|
|
820 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
|
|
821 gdk_pointer_ungrab(GDK_CURRENT_TIME);
|
|
822 }
|
|
823
|
|
824 gtk_widget_hide(ds->window);
|
|
825
|
|
826 gtk_widget_destroy(ds->window);
|
|
827 ds->window = NULL;
|
|
828 ds->calendar = NULL;
|
|
829
|
|
830 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
|
|
831 }
|
|
832
|
|
833 static gint date_selection_popup_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
|
|
834 {
|
|
835 DateSelection *ds = data;
|
|
836
|
|
837 date_selection_popup_hide(ds);
|
|
838 return TRUE;
|
|
839 }
|
|
840
|
|
841 static gint date_selection_popup_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
|
|
842 {
|
|
843 DateSelection *ds = data;
|
|
844 gint x, y;
|
|
845 gint w, h;
|
|
846 gint xr, yr;
|
|
847
|
|
848 xr = (gint)event->x_root;
|
|
849 yr = (gint)event->y_root;
|
|
850
|
|
851 gdk_window_get_origin(ds->window->window, &x, &y);
|
|
852 gdk_drawable_get_size(ds->window->window, &w, &h);
|
|
853
|
|
854 if (xr < x || yr < y || xr > x + w || yr > y + h)
|
|
855 {
|
|
856 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
|
|
857 G_CALLBACK(date_selection_popup_release_cb), ds);
|
|
858 return TRUE;
|
|
859 }
|
|
860
|
|
861 return FALSE;
|
|
862 }
|
|
863
|
|
864 static void date_selection_popup_sync(DateSelection *ds)
|
|
865 {
|
|
866 gint day, month, year;
|
|
867
|
|
868 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
|
|
869 date_selection_set(ds->box, day, month + 1, year);
|
|
870 }
|
|
871
|
|
872 static gint date_selection_popup_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
|
|
873 {
|
|
874 DateSelection *ds = data;
|
|
875
|
|
876 switch (event->keyval)
|
|
877 {
|
|
878 case GDK_Return:
|
|
879 case GDK_KP_Enter:
|
|
880 case GDK_Tab:
|
|
881 case GDK_ISO_Left_Tab:
|
|
882 date_selection_popup_sync(ds);
|
|
883 date_selection_popup_hide(ds);
|
|
884 break;
|
|
885 case GDK_Escape:
|
|
886 date_selection_popup_hide(ds);
|
|
887 break;
|
|
888 default:
|
|
889 break;
|
|
890 }
|
|
891
|
|
892 return FALSE;
|
|
893 }
|
|
894
|
|
895 static void date_selection_day_cb(GtkWidget *widget, gpointer data)
|
|
896 {
|
|
897 DateSelection *ds = data;
|
|
898
|
|
899 date_selection_popup_sync(ds);
|
|
900 }
|
|
901
|
|
902 static void date_selection_doubleclick_cb(GtkWidget *widget, gpointer data)
|
|
903 {
|
|
904 DateSelection *ds = data;
|
|
905
|
|
906 date_selection_popup_hide(ds);
|
|
907 }
|
|
908
|
|
909 static void date_selection_popup(DateSelection *ds)
|
|
910 {
|
|
911 gint x, y;
|
|
912 gint wx, wy;
|
|
913 gint day, month, year;
|
|
914
|
|
915 if (ds->window) return;
|
|
916
|
|
917 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
|
|
918 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
|
|
919 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
|
|
920 G_CALLBACK(date_selection_popup_press_cb), ds);
|
|
921 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
|
|
922 G_CALLBACK(date_selection_popup_keypress_cb), ds);
|
|
923
|
|
924 ds->calendar = gtk_calendar_new();
|
|
925 gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
|
|
926 gtk_widget_show(ds->calendar);
|
|
927
|
|
928 date_selection_get(ds->box, &day, &month, &year);
|
|
929 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
|
|
930 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
|
|
931
|
|
932 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
|
|
933 G_CALLBACK(date_selection_day_cb), ds);
|
|
934 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
|
|
935 G_CALLBACK(date_selection_doubleclick_cb), ds);
|
|
936
|
|
937 gtk_widget_realize(ds->window);
|
|
938
|
|
939 gdk_window_get_origin(ds->button->window, &wx, &wy);
|
|
940
|
|
941 x = wx + ds->button->allocation.x + ds->button->allocation.width - ds->window->allocation.width;
|
|
942 y = wy + ds->button->allocation.y + ds->button->allocation.height;
|
|
943
|
|
944 if (y + ds->window->allocation.height > gdk_screen_height())
|
|
945 {
|
|
946 y = wy + ds->button->allocation.y - ds->window->allocation.height;
|
|
947 }
|
|
948 if (x < 0) x = 0;
|
|
949 if (y < 0) y = 0;
|
|
950
|
|
951 gtk_window_move(GTK_WINDOW(ds->window), x, y);
|
|
952 gtk_widget_show(ds->window);
|
|
953
|
|
954 gtk_widget_grab_focus(ds->calendar);
|
|
955 gdk_pointer_grab(ds->window->window, TRUE,
|
|
956 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
|
|
957 NULL, NULL, GDK_CURRENT_TIME);
|
|
958 gdk_keyboard_grab(ds->window->window, TRUE, GDK_CURRENT_TIME);
|
|
959 gtk_grab_add(ds->window);
|
|
960
|
|
961 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
|
|
962 }
|
|
963
|
|
964 static void date_selection_button_cb(GtkWidget *widget, gpointer data)
|
|
965 {
|
|
966 DateSelection *ds = data;
|
|
967
|
|
968 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
|
|
969 {
|
|
970 date_selection_popup(ds);
|
|
971 }
|
|
972 }
|
|
973
|
|
974 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
|
|
975 {
|
|
976 GtkWidget *spin = data;
|
|
977
|
|
978 if (allocation->height > spin->requisition.height)
|
|
979 {
|
|
980 GtkAllocation button_allocation;
|
|
981
|
|
982 button_allocation = button->allocation;
|
|
983 button_allocation.height = spin->requisition.height;
|
|
984 button_allocation.y = spin->allocation.y +
|
|
985 (spin->allocation.height - spin->requisition.height) / 2;
|
|
986 gtk_widget_size_allocate(button, &button_allocation);
|
|
987 }
|
|
988 }
|
|
989
|
|
990 static void spin_increase(GtkWidget *spin, gint value)
|
|
991 {
|
|
992 GtkRequisition req;
|
|
993
|
|
994 gtk_widget_size_request(spin, &req);
|
|
995 gtk_widget_set_size_request(spin, req.width + value, -1);
|
|
996 }
|
|
997
|
|
998 static void date_selection_destroy_cb(GtkWidget *widget, gpointer data)
|
|
999 {
|
|
1000 DateSelection *ds = data;
|
|
1001
|
|
1002 date_selection_popup_hide(ds);
|
|
1003
|
|
1004 g_free(ds);
|
|
1005 }
|
|
1006
|
|
1007 GtkWidget *date_selection_new(void)
|
|
1008 {
|
|
1009 DateSelection *ds;
|
|
1010 GtkWidget *arrow;
|
|
1011
|
|
1012 ds = g_new0(DateSelection, 1);
|
|
1013
|
|
1014 ds->box = gtk_hbox_new(FALSE, 2);
|
|
1015 g_signal_connect(G_OBJECT(ds->box), "destroy",
|
|
1016 G_CALLBACK(date_selection_destroy_cb), ds);
|
|
1017
|
|
1018 /* FIXME: use option menu with text format of month instead of a spin button */
|
|
1019 ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
|
|
1020 ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
|
|
1021 ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
|
|
1022 spin_increase(ds->spin_y, 5);
|
|
1023
|
|
1024 ds->button = gtk_toggle_button_new();
|
|
1025 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
|
|
1026 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
|
|
1027
|
|
1028 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
|
|
1029 gtk_container_add(GTK_CONTAINER(ds->button), arrow);
|
|
1030 gtk_widget_show(arrow);
|
|
1031
|
|
1032 gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
|
|
1033 g_signal_connect(G_OBJECT(ds->button), "clicked",
|
|
1034 G_CALLBACK(date_selection_button_cb), ds);
|
|
1035 gtk_widget_show(ds->button);
|
|
1036
|
|
1037 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
|
|
1038
|
|
1039 return ds->box;
|
|
1040 }
|
|
1041
|
|
1042 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
|
|
1043 {
|
|
1044 DateSelection *ds;
|
|
1045
|
|
1046 ds = g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY);
|
|
1047 if (!ds) return;
|
|
1048
|
|
1049 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), (gdouble)day);
|
|
1050 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), (gdouble)month);
|
|
1051 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), (gdouble)year);
|
|
1052 }
|
|
1053
|
|
1054
|
|
1055 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
|
|
1056 {
|
|
1057 DateSelection *ds;
|
|
1058
|
|
1059 ds = g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY);
|
|
1060 if (!ds) return;
|
|
1061
|
|
1062 if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
|
|
1063 if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
|
|
1064 if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
|
|
1065 }
|
|
1066
|
|
1067 void date_selection_time_set(GtkWidget *widget, time_t t)
|
|
1068 {
|
|
1069 struct tm *lt;
|
|
1070
|
|
1071 lt = localtime(&t);
|
|
1072 if (!lt) return;
|
|
1073
|
|
1074 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
|
|
1075 }
|
|
1076
|
|
1077 time_t date_selection_time_get(GtkWidget *widget)
|
|
1078 {
|
|
1079 struct tm lt;
|
|
1080 gint day = 0;
|
|
1081 gint month = 0;
|
|
1082 gint year = 0;
|
|
1083
|
|
1084 date_selection_get(widget, &day, &month ,&year);
|
|
1085
|
|
1086 lt.tm_sec = 0;
|
|
1087 lt.tm_min = 0;
|
|
1088 lt.tm_hour = 0;
|
|
1089 lt.tm_mday = day;
|
|
1090 lt.tm_mon = month - 1;
|
|
1091 lt.tm_year = year - 1900;
|
|
1092 lt.tm_isdst = 0;
|
|
1093
|
|
1094 return mktime(<);
|
|
1095 }
|
|
1096
|
|
1097
|
|
1098 /*
|
|
1099 *-----------------------------------------------------------------------------
|
|
1100 * Sizer, without using a GtkPaned
|
|
1101 *-----------------------------------------------------------------------------
|
|
1102 */
|
|
1103
|
|
1104 #define SIZER_DATA_KEY "sizer_data"
|
|
1105
|
|
1106 typedef struct _SizerData SizerData;
|
|
1107 struct _SizerData
|
|
1108 {
|
|
1109 GtkWidget *sizer;
|
|
1110 GtkWidget *parent;
|
|
1111 GtkWidget *bounding_widget;
|
|
1112 SizerPositionType position;
|
|
1113
|
|
1114 gint hsize_min;
|
|
1115 gint hsize_max;
|
|
1116 gint vsize_min;
|
|
1117 gint vsize_max;
|
|
1118
|
|
1119 gint in_drag;
|
|
1120 gint press_x;
|
|
1121 gint press_y;
|
|
1122 gint press_width;
|
|
1123 gint press_height;
|
|
1124
|
|
1125 gint handle_prelit;
|
|
1126 };
|
|
1127
|
|
1128
|
|
1129 static gint sizer_default_handle_size(void)
|
|
1130 {
|
|
1131 gint handle_size = 5;
|
|
1132 GtkWidget *paned;
|
|
1133 GtkStyle *style;
|
|
1134
|
|
1135 paned = gtk_hpaned_new();
|
|
1136
|
|
1137 style = gtk_rc_get_style(paned);
|
|
1138 gtk_widget_set_style(paned, style);
|
|
1139 gtk_widget_style_get(paned, "handle_size", &handle_size, NULL);
|
|
1140
|
|
1141 gtk_widget_destroy(paned);
|
|
1142
|
|
1143 return handle_size;
|
|
1144 }
|
|
1145
|
|
1146 static gint sizer_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
|
|
1147 {
|
|
1148 SizerData *sd = data;
|
|
1149 gint x, y;
|
|
1150 gint w, h;
|
|
1151
|
|
1152 if (!sd->in_drag) return FALSE;
|
|
1153
|
|
1154 x = sd->press_x - bevent->x_root;
|
|
1155 y = sd->press_y - bevent->y_root;
|
|
1156
|
|
1157 w = sd->press_width;
|
|
1158 h = sd->press_height;
|
|
1159
|
|
1160 if (sd->position & SIZER_POS_LEFT)
|
|
1161 {
|
|
1162 w += x;
|
|
1163 }
|
|
1164 else if (sd->position & SIZER_POS_RIGHT)
|
|
1165 {
|
|
1166 w -= x;
|
|
1167 }
|
|
1168
|
|
1169 if (sd->position & SIZER_POS_TOP)
|
|
1170 {
|
|
1171 h += y;
|
|
1172 }
|
|
1173 else if (sd->position & SIZER_POS_BOTTOM)
|
|
1174 {
|
|
1175 h -= y;
|
|
1176 }
|
|
1177
|
|
1178 if (sd->hsize_min >= 0) w = MAX(w, sd->hsize_min);
|
|
1179 if (sd->vsize_min >= 0) h = MAX(h, sd->vsize_min);
|
|
1180
|
|
1181 if (sd->bounding_widget)
|
|
1182 {
|
|
1183 w = CLAMP(w, sd->sizer->allocation.width, sd->bounding_widget->allocation.width);
|
|
1184 h = CLAMP(h, sd->sizer->allocation.height, sd->bounding_widget->allocation.height);
|
|
1185 }
|
|
1186 else
|
|
1187 {
|
|
1188 if (w < sd->sizer->allocation.width) w = sd->sizer->allocation.width;
|
|
1189 if (h < sd->sizer->allocation.height) h = sd->sizer->allocation.height;
|
|
1190 }
|
|
1191
|
|
1192 if (sd->hsize_max >= 0) w = MIN(w, sd->hsize_max);
|
|
1193 if (sd->vsize_max >= 0) h = MIN(h, sd->vsize_max);
|
|
1194
|
|
1195 if (w == sd->parent->allocation.width) w = -1;
|
|
1196 if (h == sd->parent->allocation.height) h = -1;
|
|
1197
|
|
1198 if (w > 0 || h > 0) gtk_widget_set_size_request(sd->parent, w, h);
|
|
1199
|
|
1200 return TRUE;
|
|
1201 }
|
|
1202
|
|
1203 static gint sizer_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
|
|
1204 {
|
|
1205 SizerData *sd = data;
|
|
1206
|
|
1207 if (bevent->button != 1) return FALSE;
|
|
1208
|
|
1209 sd->in_drag = TRUE;
|
|
1210 sd->press_x = bevent->x_root;
|
|
1211 sd->press_y = bevent->y_root;
|
|
1212
|
|
1213 sd->press_width = sd->parent->allocation.width;
|
|
1214 sd->press_height = sd->parent->allocation.height;
|
|
1215
|
|
1216 gdk_pointer_grab(sd->sizer->window, FALSE,
|
|
1217 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
|
|
1218 NULL, NULL, bevent->time);
|
|
1219 gtk_grab_add(sd->sizer);
|
|
1220
|
|
1221 return TRUE;
|
|
1222 }
|
|
1223
|
|
1224 static gint sizer_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
|
|
1225 {
|
|
1226 SizerData *sd = data;
|
|
1227
|
|
1228 if (bevent->button != 1) return FALSE;
|
|
1229
|
|
1230 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(sd->sizer))
|
|
1231 {
|
|
1232 gtk_grab_remove(sd->sizer);
|
|
1233 gdk_pointer_ungrab(bevent->time);
|
|
1234 }
|
|
1235
|
|
1236 sd->in_drag = FALSE;
|
|
1237
|
|
1238 return TRUE;
|
|
1239 }
|
|
1240
|
|
1241 static void sizer_set_prelight(SizerData *sd, gint prelit)
|
|
1242 {
|
|
1243 sd->handle_prelit = prelit;
|
|
1244 gtk_widget_queue_draw_area(sd->sizer, 0, 0,
|
|
1245 sd->sizer->allocation.width, sd->sizer->allocation.height);
|
|
1246 }
|
|
1247
|
|
1248 static gint sizer_enter_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
|
|
1249 {
|
|
1250 SizerData *sd = data;
|
|
1251
|
|
1252 sizer_set_prelight(sd, TRUE);
|
|
1253 return TRUE;
|
|
1254 }
|
|
1255
|
|
1256 static gint sizer_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
|
|
1257 {
|
|
1258 SizerData *sd = data;
|
|
1259
|
|
1260 sizer_set_prelight(sd, FALSE);
|
|
1261 return TRUE;
|
|
1262 }
|
|
1263
|
|
1264 static gint sizer_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
|
|
1265 {
|
|
1266 SizerData *sd = data;
|
|
1267 GdkRectangle clip;
|
|
1268 GtkOrientation orientation;
|
|
1269 GtkStateType state;
|
|
1270
|
|
1271 gdk_region_get_clipbox(event->region, &clip);
|
|
1272
|
|
1273 if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
|
|
1274 {
|
|
1275 orientation = GTK_ORIENTATION_VERTICAL;
|
|
1276 }
|
|
1277 else
|
|
1278 {
|
|
1279 orientation = GTK_ORIENTATION_HORIZONTAL;
|
|
1280 }
|
|
1281
|
|
1282 if (sd->handle_prelit)
|
|
1283 {
|
|
1284 state = GTK_STATE_PRELIGHT;
|
|
1285 }
|
|
1286 else
|
|
1287 {
|
|
1288 state = widget->state;
|
|
1289 }
|
|
1290
|
|
1291 gtk_paint_handle(widget->style, widget->window, state,
|
|
1292 GTK_SHADOW_NONE, &clip, widget, "paned",
|
|
1293 0, 0,
|
|
1294 widget->allocation.width, widget->allocation.height,
|
|
1295 orientation);
|
|
1296
|
|
1297 return TRUE;
|
|
1298 }
|
|
1299
|
|
1300 static void sizer_realize_cb(GtkWidget *widget, gpointer data)
|
|
1301 {
|
|
1302 SizerData *sd = data;
|
|
1303 GdkCursorType n;
|
|
1304
|
|
1305 n = 0;
|
|
1306 if (sd->position & SIZER_POS_TOP || sd->position & SIZER_POS_BOTTOM)
|
|
1307 {
|
|
1308 n = GDK_SB_V_DOUBLE_ARROW;
|
|
1309 }
|
|
1310 if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
|
|
1311 {
|
|
1312 n = (n != 0) ? GDK_FLEUR : GDK_SB_H_DOUBLE_ARROW;
|
|
1313 }
|
|
1314
|
|
1315 if (n != 0 && widget->window)
|
|
1316 {
|
|
1317 GdkCursor *cursor;
|
|
1318 cursor = gdk_cursor_new(n);
|
|
1319 gdk_window_set_cursor(widget->window, cursor);
|
|
1320 gdk_cursor_unref(cursor);
|
|
1321 }
|
|
1322 }
|
|
1323
|
|
1324 static void sizer_destroy_cb(GtkWidget *widget, gpointer data)
|
|
1325 {
|
|
1326 SizerData *sd = data;
|
|
1327
|
|
1328 g_free(sd);
|
|
1329 }
|
|
1330
|
|
1331 GtkWidget *sizer_new(GtkWidget *parent, GtkWidget *bounding_widget,
|
|
1332 SizerPositionType position)
|
|
1333 {
|
|
1334 SizerData *sd;
|
|
1335 gint handle_size;
|
|
1336
|
|
1337 sd = g_new0(SizerData, 1);
|
|
1338
|
|
1339 sd->sizer = gtk_event_box_new();
|
|
1340 sd->parent = parent;
|
|
1341 sd->bounding_widget = bounding_widget;
|
|
1342 sd->position = position;
|
|
1343 sd->hsize_min = -1;
|
|
1344 sd->hsize_max = -1;
|
|
1345 sd->vsize_min = -1;
|
|
1346 sd->vsize_max = -1;
|
|
1347
|
|
1348 sd->in_drag = FALSE;
|
|
1349 sd->handle_prelit = FALSE;
|
|
1350
|
|
1351 g_signal_connect(G_OBJECT(sd->sizer), "destroy",
|
|
1352 G_CALLBACK(sizer_destroy_cb), sd);
|
|
1353
|
|
1354 g_signal_connect(G_OBJECT(sd->sizer), "motion_notify_event",
|
|
1355 G_CALLBACK(sizer_motion_cb), sd);
|
|
1356 g_signal_connect(G_OBJECT(sd->sizer), "button_press_event",
|
|
1357 G_CALLBACK(sizer_press_cb), sd);
|
|
1358 g_signal_connect(G_OBJECT(sd->sizer), "button_release_event",
|
|
1359 G_CALLBACK(sizer_release_cb), sd);
|
|
1360
|
|
1361 g_signal_connect(G_OBJECT(sd->sizer), "enter_notify_event",
|
|
1362 G_CALLBACK(sizer_enter_cb), sd);
|
|
1363 g_signal_connect(G_OBJECT(sd->sizer), "leave_notify_event",
|
|
1364 G_CALLBACK(sizer_leave_cb), sd);
|
|
1365
|
|
1366 gtk_widget_set_events(sd->sizer, GDK_POINTER_MOTION_MASK |
|
|
1367 GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
|
|
1368 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
|
|
1369
|
|
1370 g_signal_connect(sd->sizer, "realize",
|
|
1371 G_CALLBACK(sizer_realize_cb), sd);
|
|
1372 g_signal_connect(sd->sizer, "expose_event",
|
|
1373 G_CALLBACK(sizer_expose_cb), sd);
|
|
1374
|
|
1375 handle_size = sizer_default_handle_size();
|
|
1376
|
|
1377 gtk_widget_set_size_request(sd->sizer, handle_size, handle_size);
|
|
1378 #if 0
|
|
1379 /* use this if you add a shadow border to the handle */
|
|
1380 gtk_widget_set_size_request(sd->sizer, handle_size + sd->sizer->style->xthickness * 2,
|
|
1381 handle_size + sd->sizer->style->ythickness * 2);
|
|
1382 #endif
|
|
1383
|
|
1384 g_object_set_data(G_OBJECT(sd->sizer), SIZER_DATA_KEY,sd);
|
|
1385
|
|
1386 return sd->sizer;
|
|
1387 }
|
|
1388
|
|
1389 void sizer_set_limits(GtkWidget *sizer,
|
|
1390 gint hsize_min, gint hsize_max,
|
|
1391 gint vsize_min, gint vsize_max)
|
|
1392 {
|
|
1393 SizerData *sd;
|
|
1394
|
|
1395 sd = g_object_get_data(G_OBJECT(sizer), SIZER_DATA_KEY);
|
|
1396 if (!sd) return;
|
|
1397
|
|
1398 sd->hsize_min = hsize_min;
|
|
1399 sd->hsize_max = hsize_max;
|
|
1400 sd->vsize_min = vsize_min;
|
|
1401 sd->vsize_max = vsize_max;
|
|
1402 }
|
|
1403
|
|
1404
|
|
1405 /*
|
|
1406 *-----------------------------------------------------------------------------
|
|
1407 * storing data in a history list with key,data pairs
|
|
1408 *-----------------------------------------------------------------------------
|
|
1409 */
|
|
1410
|
|
1411 #define PREF_LIST_MARKER_INT "[INT]:"
|
|
1412 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
|
|
1413 #define PREF_LIST_MARKER_STRING "[STRING]:"
|
|
1414
|
|
1415 static GList *pref_list_find(const gchar *group, const gchar *token)
|
|
1416 {
|
|
1417 GList *work;
|
|
1418 gint l;
|
|
1419
|
|
1420 l = strlen(token);
|
|
1421
|
|
1422 work = history_list_get_by_key(group);
|
|
1423 while (work)
|
|
1424 {
|
|
1425 const gchar *text = work->data;
|
|
1426
|
|
1427 if (strncmp(text, token, l) == 0) return work;
|
|
1428
|
|
1429 work = work->next;
|
|
1430 }
|
|
1431
|
|
1432 return NULL;
|
|
1433 }
|
|
1434
|
|
1435 static gint pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
|
|
1436 {
|
|
1437 gchar *token;
|
|
1438 GList *work;
|
|
1439 gint ret;
|
|
1440
|
|
1441 if (!group || !key || !marker)
|
|
1442 {
|
|
1443 *result = NULL;
|
|
1444 return FALSE;
|
|
1445 }
|
|
1446
|
|
1447 token = g_strconcat(key, marker, NULL);
|
|
1448
|
|
1449 work = pref_list_find(group, token);
|
|
1450 if (work)
|
|
1451 {
|
|
1452 *result = (const gchar *)work->data + strlen(token);
|
|
1453 if (strlen(*result) == 0) *result = NULL;
|
|
1454 ret = TRUE;
|
|
1455 }
|
|
1456 else
|
|
1457 {
|
|
1458 *result = NULL;
|
|
1459 ret = FALSE;
|
|
1460 }
|
|
1461
|
|
1462 g_free(token);
|
|
1463
|
|
1464 return ret;
|
|
1465 }
|
|
1466
|
|
1467 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
|
|
1468 {
|
|
1469 gchar *token;
|
|
1470 gchar *path;
|
|
1471 GList *work;
|
|
1472
|
|
1473 if (!group || !key || !marker) return;
|
|
1474
|
|
1475 token = g_strconcat(key, marker, NULL);
|
|
1476 path = g_strconcat(token, text, NULL);
|
|
1477
|
|
1478 work = pref_list_find(group, token);
|
|
1479 if (work)
|
|
1480 {
|
|
1481 gchar *old_path = work->data;
|
|
1482
|
|
1483 if (text)
|
|
1484 {
|
|
1485 work->data = path;
|
|
1486 path = NULL;
|
|
1487
|
|
1488 g_free(old_path);
|
|
1489 }
|
|
1490 else
|
|
1491 {
|
|
1492 history_list_item_remove(group, old_path);
|
|
1493 }
|
|
1494 }
|
|
1495 else if (text)
|
|
1496 {
|
|
1497 history_list_add_to_key(group, path, 0);
|
|
1498 }
|
|
1499
|
|
1500 g_free(path);
|
|
1501 g_free(token);
|
|
1502 }
|
|
1503
|
|
1504 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
|
|
1505 {
|
|
1506 gchar *text;
|
|
1507
|
|
1508 text = g_strdup_printf("%d", value);
|
|
1509 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
|
|
1510 g_free(text);
|
|
1511 }
|
|
1512
|
|
1513 gint pref_list_int_get(const gchar *group, const gchar *key, gint *result)
|
|
1514 {
|
|
1515 const gchar *text;
|
|
1516
|
|
1517 if (!group || !key)
|
|
1518 {
|
|
1519 *result = 0;
|
|
1520 return FALSE;
|
|
1521 }
|
|
1522
|
|
1523 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
|
|
1524 {
|
|
1525 *result = (gint)strtol(text, NULL, 10);
|
|
1526 return TRUE;
|
|
1527 }
|
|
1528
|
|
1529 *result = 0;
|
|
1530 return FALSE;
|
|
1531 }
|
|
1532
|
|
1533 void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
|
|
1534 {
|
|
1535 gchar text[G_ASCII_DTOSTR_BUF_SIZE];
|
|
1536
|
|
1537 g_ascii_dtostr(text, sizeof(text), value);
|
|
1538 pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
|
|
1539 }
|
|
1540
|
|
1541 gint pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
|
|
1542 {
|
|
1543 const gchar *text;
|
|
1544
|
|
1545 if (!group || !key)
|
|
1546 {
|
|
1547 *result = 0;
|
|
1548 return FALSE;
|
|
1549 }
|
|
1550
|
|
1551 if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
|
|
1552 {
|
|
1553 *result = g_ascii_strtod(text, NULL);
|
|
1554 return TRUE;
|
|
1555 }
|
|
1556
|
|
1557 *result = 0;
|
|
1558 return FALSE;
|
|
1559 }
|
|
1560
|
|
1561 void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
|
|
1562 {
|
|
1563 pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
|
|
1564 }
|
|
1565
|
|
1566 gint pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
|
|
1567 {
|
|
1568 return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
|
|
1569 }
|
|
1570
|
|
1571
|