d9b15094 |
1 | /* |
2 | * gtkdlg.c - GTK implementation of the PuTTY configuration box. |
3 | */ |
4 | |
1d0d4a3b |
5 | /* |
6 | * TODO: |
7 | * |
0bd8d76d |
8 | * - colour selection woe: what to do about GTK's colour selector |
9 | * not allowing us full resolution in our own colour selections? |
10 | * Perhaps making the colour resolution per-platform, at least |
11 | * at the config level, is actually the least unpleasant |
12 | * alternative. |
0f5e1f75 |
13 | * |
1d0d4a3b |
14 | * - cosmetics: |
15 | * + can't we _somehow_ have less leading between radio buttons? |
16 | * + wrapping text widgets, the horror, the horror |
17 | * + labels and their associated edit boxes don't line up |
18 | * properly |
1c291fcf |
19 | * + don't suppose we can fix the vertical offset labels get |
20 | * from their underlines? |
0bd8d76d |
21 | * + apparently Left/Right on the treeview should be expanding |
22 | * and collapsing branches. |
1c291fcf |
23 | * + why the hell are the Up/Down focus movement keys sorting |
24 | * things by _width_? (See the Logging and Features panels |
25 | * for good examples.) |
0bd8d76d |
26 | * + the error message box is in totally the wrong place and |
27 | * also looks ugly. Try to fix; look at how (frex) AisleRiot |
28 | * does it better. |
1c291fcf |
29 | */ |
30 | |
31 | /* |
32 | * TODO when porting to GTK 2.0: |
33 | * |
34 | * - GtkTree is apparently deprecated and we should switch to |
35 | * GtkTreeView instead. |
36 | * - GtkLabel has a built-in mnemonic scheme, so we should at |
37 | * least consider switching to that from the current adhockery. |
1d0d4a3b |
38 | */ |
39 | |
d9b15094 |
40 | #include <assert.h> |
1c291fcf |
41 | #include <ctype.h> |
d9b15094 |
42 | #include <gtk/gtk.h> |
1c291fcf |
43 | #include <gdk/gdkkeysyms.h> |
0bd8d76d |
44 | #include <gdk/gdkx.h> |
45 | #include <X11/Xlib.h> |
46 | #include <X11/Xutil.h> |
d9b15094 |
47 | |
48 | #include "gtkcols.h" |
49 | #include "gtkpanel.h" |
50 | |
51 | #ifdef TESTMODE |
52 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
53 | #endif |
54 | |
55 | #include "putty.h" |
56 | #include "dialog.h" |
0f5e1f75 |
57 | #include "tree234.h" |
58 | |
1c291fcf |
59 | struct Shortcut { |
60 | GtkWidget *widget; |
61 | struct uctrl *uc; |
62 | int action; |
63 | }; |
64 | |
65 | struct Shortcuts { |
66 | struct Shortcut sc[128]; |
67 | }; |
68 | |
0f5e1f75 |
69 | struct uctrl { |
70 | union control *ctrl; |
71 | GtkWidget *toplevel; |
72 | void *privdata; |
73 | int privdata_needs_free; |
74 | GtkWidget **buttons; int nbuttons; /* for radio buttons */ |
75 | GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */ |
1c291fcf |
76 | GtkWidget *button; /* for filesel, fontsel */ |
0f5e1f75 |
77 | GtkWidget *list; /* for combobox, listbox */ |
78 | GtkWidget *menu; /* for optionmenu (==droplist) */ |
79 | GtkWidget *optmenu; /* also for optionmenu */ |
80 | GtkWidget *text; /* for text */ |
0bd8d76d |
81 | GtkAdjustment *adj; /* for the scrollbar in a list box */ |
0f5e1f75 |
82 | }; |
83 | |
84 | struct dlgparam { |
85 | tree234 *byctrl, *bywidget; |
86 | void *data; |
87 | struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */ |
88 | /* `flags' are set to indicate when a GTK signal handler is being called |
89 | * due to automatic processing and should not flag a user event. */ |
90 | int flags; |
1c291fcf |
91 | struct Shortcuts *shortcuts; |
0bd8d76d |
92 | GtkWidget *cancelbutton, *currtreeitem, **treeitems; |
93 | union control *currfocus, *lastfocus; |
94 | int ntreeitems; |
95 | int retval; |
0f5e1f75 |
96 | }; |
97 | #define FLAG_UPDATING_COMBO_LIST 1 |
98 | |
1c291fcf |
99 | enum { /* values for Shortcut.action */ |
100 | SHORTCUT_EMPTY, /* no shortcut on this key */ |
101 | SHORTCUT_FOCUS, /* focus the supplied widget */ |
102 | SHORTCUT_UCTRL, /* do something sane with uctrl */ |
103 | SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */ |
104 | SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */ |
105 | }; |
106 | |
0f5e1f75 |
107 | /* |
108 | * Forward references. |
109 | */ |
0bd8d76d |
110 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
111 | gpointer data); |
1c291fcf |
112 | static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
113 | int chr, int action, void *ptr); |
0bd8d76d |
114 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
115 | gpointer data); |
116 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
117 | gpointer data); |
118 | static int listitem_button(GtkWidget *item, GdkEventButton *event, |
0f5e1f75 |
119 | gpointer data); |
120 | static void menuitem_activate(GtkMenuItem *item, gpointer data); |
121 | static void coloursel_ok(GtkButton *button, gpointer data); |
122 | static void coloursel_cancel(GtkButton *button, gpointer data); |
0bd8d76d |
123 | static void window_destroy(GtkWidget *widget, gpointer data); |
0f5e1f75 |
124 | |
125 | static int uctrl_cmp_byctrl(void *av, void *bv) |
126 | { |
127 | struct uctrl *a = (struct uctrl *)av; |
128 | struct uctrl *b = (struct uctrl *)bv; |
129 | if (a->ctrl < b->ctrl) |
130 | return -1; |
131 | else if (a->ctrl > b->ctrl) |
132 | return +1; |
133 | return 0; |
134 | } |
135 | |
136 | static int uctrl_cmp_byctrl_find(void *av, void *bv) |
137 | { |
138 | union control *a = (union control *)av; |
139 | struct uctrl *b = (struct uctrl *)bv; |
140 | if (a < b->ctrl) |
141 | return -1; |
142 | else if (a > b->ctrl) |
143 | return +1; |
144 | return 0; |
145 | } |
146 | |
147 | static int uctrl_cmp_bywidget(void *av, void *bv) |
148 | { |
149 | struct uctrl *a = (struct uctrl *)av; |
150 | struct uctrl *b = (struct uctrl *)bv; |
151 | if (a->toplevel < b->toplevel) |
152 | return -1; |
153 | else if (a->toplevel > b->toplevel) |
154 | return +1; |
155 | return 0; |
156 | } |
157 | |
158 | static int uctrl_cmp_bywidget_find(void *av, void *bv) |
159 | { |
160 | GtkWidget *a = (GtkWidget *)av; |
161 | struct uctrl *b = (struct uctrl *)bv; |
162 | if (a < b->toplevel) |
163 | return -1; |
164 | else if (a > b->toplevel) |
165 | return +1; |
166 | return 0; |
167 | } |
168 | |
169 | static void dlg_init(struct dlgparam *dp) |
170 | { |
171 | dp->byctrl = newtree234(uctrl_cmp_byctrl); |
172 | dp->bywidget = newtree234(uctrl_cmp_bywidget); |
173 | dp->coloursel_result.ok = FALSE; |
174 | } |
175 | |
176 | static void dlg_cleanup(struct dlgparam *dp) |
177 | { |
178 | struct uctrl *uc; |
179 | |
180 | freetree234(dp->byctrl); /* doesn't free the uctrls inside */ |
181 | while ( (uc = index234(dp->bywidget, 0)) != NULL) { |
182 | del234(dp->bywidget, uc); |
183 | if (uc->privdata_needs_free) |
184 | sfree(uc->privdata); |
185 | sfree(uc->buttons); |
186 | sfree(uc); |
187 | } |
188 | freetree234(dp->bywidget); |
0bd8d76d |
189 | sfree(dp->treeitems); |
0f5e1f75 |
190 | } |
191 | |
192 | static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc) |
193 | { |
194 | add234(dp->byctrl, uc); |
195 | add234(dp->bywidget, uc); |
196 | } |
197 | |
198 | static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl) |
199 | { |
200 | return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find); |
201 | } |
202 | |
203 | static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w) |
204 | { |
205 | struct uctrl *ret = NULL; |
206 | do { |
207 | ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find); |
208 | if (ret) |
209 | return ret; |
210 | w = w->parent; |
211 | } while (w); |
212 | return ret; |
213 | } |
d9b15094 |
214 | |
d9b15094 |
215 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
216 | { |
0f5e1f75 |
217 | struct dlgparam *dp = (struct dlgparam *)dlg; |
218 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
219 | return uc->privdata; |
d9b15094 |
220 | } |
221 | |
222 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
223 | { |
0f5e1f75 |
224 | struct dlgparam *dp = (struct dlgparam *)dlg; |
225 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
226 | uc->privdata = ptr; |
227 | uc->privdata_needs_free = FALSE; |
d9b15094 |
228 | } |
229 | |
230 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
231 | { |
0f5e1f75 |
232 | struct dlgparam *dp = (struct dlgparam *)dlg; |
233 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
234 | uc->privdata = smalloc(size); |
235 | uc->privdata_needs_free = FALSE; |
236 | return uc->privdata; |
d9b15094 |
237 | } |
238 | |
0bd8d76d |
239 | union control *dlg_last_focused(union control *ctrl, void *dlg) |
d9b15094 |
240 | { |
0f5e1f75 |
241 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
242 | if (dp->currfocus != ctrl) |
243 | return dp->currfocus; |
244 | else |
245 | return dp->lastfocus; |
d9b15094 |
246 | } |
247 | |
0f5e1f75 |
248 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int which) |
d9b15094 |
249 | { |
0f5e1f75 |
250 | struct dlgparam *dp = (struct dlgparam *)dlg; |
251 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
252 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
253 | assert(uc->buttons != NULL); |
254 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE); |
d9b15094 |
255 | } |
256 | |
257 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
258 | { |
0f5e1f75 |
259 | struct dlgparam *dp = (struct dlgparam *)dlg; |
260 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
261 | int i; |
262 | |
263 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
264 | assert(uc->buttons != NULL); |
265 | for (i = 0; i < uc->nbuttons; i++) |
266 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i]))) |
267 | return i; |
268 | return 0; /* got to return something */ |
d9b15094 |
269 | } |
270 | |
271 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
272 | { |
0f5e1f75 |
273 | struct dlgparam *dp = (struct dlgparam *)dlg; |
274 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
275 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
276 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked); |
d9b15094 |
277 | } |
278 | |
279 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
280 | { |
0f5e1f75 |
281 | struct dlgparam *dp = (struct dlgparam *)dlg; |
282 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
283 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
284 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel)); |
d9b15094 |
285 | } |
286 | |
287 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
288 | { |
0f5e1f75 |
289 | struct dlgparam *dp = (struct dlgparam *)dlg; |
290 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
291 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
292 | assert(uc->entry != NULL); |
293 | gtk_entry_set_text(GTK_ENTRY(uc->entry), text); |
d9b15094 |
294 | } |
295 | |
296 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
297 | { |
0f5e1f75 |
298 | struct dlgparam *dp = (struct dlgparam *)dlg; |
299 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
300 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
301 | assert(uc->entry != NULL); |
302 | strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
303 | length); |
304 | buffer[length-1] = '\0'; |
305 | } |
306 | |
307 | static void container_remove_and_destroy(GtkWidget *w, gpointer data) |
308 | { |
309 | GtkContainer *cont = GTK_CONTAINER(data); |
310 | /* gtk_container_remove will unref the widget for us; we need not. */ |
311 | gtk_container_remove(cont, w); |
d9b15094 |
312 | } |
313 | |
314 | /* The `listbox' functions can also apply to combo boxes. */ |
315 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
316 | { |
0f5e1f75 |
317 | struct dlgparam *dp = (struct dlgparam *)dlg; |
318 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
319 | GtkContainer *cont; |
320 | |
321 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
322 | uc->ctrl->generic.type == CTRL_LISTBOX); |
323 | assert(uc->menu != NULL || uc->list != NULL); |
324 | |
325 | cont = (uc->menu ? GTK_CONTAINER(uc->menu) : GTK_CONTAINER(uc->list)); |
326 | |
327 | gtk_container_foreach(cont, container_remove_and_destroy, cont); |
d9b15094 |
328 | } |
329 | |
330 | void dlg_listbox_del(union control *ctrl, void *dlg, int index) |
331 | { |
0f5e1f75 |
332 | struct dlgparam *dp = (struct dlgparam *)dlg; |
333 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
334 | |
335 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
336 | uc->ctrl->generic.type == CTRL_LISTBOX); |
337 | assert(uc->menu != NULL || uc->list != NULL); |
338 | |
339 | if (uc->menu) { |
340 | gtk_container_remove |
341 | (GTK_CONTAINER(uc->menu), |
342 | g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index)); |
343 | } else { |
344 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
345 | } |
d9b15094 |
346 | } |
347 | |
348 | void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) |
349 | { |
0f5e1f75 |
350 | dlg_listbox_addwithindex(ctrl, dlg, text, 0); |
d9b15094 |
351 | } |
352 | |
353 | /* |
354 | * Each listbox entry may have a numeric id associated with it. |
355 | * Note that some front ends only permit a string to be stored at |
356 | * each position, which means that _if_ you put two identical |
357 | * strings in any listbox then you MUST not assign them different |
358 | * IDs and expect to get meaningful results back. |
359 | */ |
360 | void dlg_listbox_addwithindex(union control *ctrl, void *dlg, |
361 | char const *text, int id) |
362 | { |
0f5e1f75 |
363 | struct dlgparam *dp = (struct dlgparam *)dlg; |
364 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
365 | |
366 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
367 | uc->ctrl->generic.type == CTRL_LISTBOX); |
368 | assert(uc->menu != NULL || uc->list != NULL); |
369 | |
370 | dp->flags |= FLAG_UPDATING_COMBO_LIST; |
371 | |
372 | if (uc->menu) { |
373 | /* |
374 | * List item in a drop-down (but non-combo) list. Tabs are |
375 | * ignored; we just provide a standard menu item with the |
376 | * text. |
377 | */ |
378 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
379 | |
380 | gtk_container_add(GTK_CONTAINER(uc->menu), menuitem); |
381 | gtk_widget_show(menuitem); |
382 | |
383 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", (gpointer)id); |
384 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
385 | GTK_SIGNAL_FUNC(menuitem_activate), dp); |
386 | } else if (!uc->entry) { |
387 | /* |
388 | * List item in a non-combo-box list box. We make all of |
389 | * these Columns containing GtkLabels. This allows us to do |
390 | * the nasty force_left hack irrespective of whether there |
391 | * are tabs in the thing. |
392 | */ |
393 | GtkWidget *listitem = gtk_list_item_new(); |
523fd1da |
394 | GtkWidget *cols = columns_new(10); |
0f5e1f75 |
395 | gint *percents; |
396 | int i, ncols; |
397 | |
398 | /* Count the tabs in the text, and hence determine # of columns. */ |
399 | ncols = 1; |
400 | for (i = 0; text[i]; i++) |
401 | if (text[i] == '\t') |
402 | ncols++; |
403 | |
404 | assert(ncols <= |
405 | (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1)); |
406 | percents = smalloc(ncols * sizeof(gint)); |
407 | percents[ncols-1] = 100; |
408 | for (i = 0; i < ncols-1; i++) { |
409 | percents[i] = uc->ctrl->listbox.percentages[i]; |
410 | percents[ncols-1] -= percents[i]; |
411 | } |
412 | columns_set_cols(COLUMNS(cols), ncols, percents); |
413 | sfree(percents); |
414 | |
415 | for (i = 0; i < ncols; i++) { |
416 | int len = strcspn(text, "\t"); |
417 | char *dup = dupprintf("%.*s", len, text); |
418 | GtkWidget *label; |
419 | |
420 | text += len; |
421 | if (*text) text++; |
422 | label = gtk_label_new(dup); |
423 | sfree(dup); |
424 | |
425 | columns_add(COLUMNS(cols), label, i, 1); |
426 | columns_force_left_align(COLUMNS(cols), label); |
427 | gtk_widget_show(label); |
428 | } |
429 | gtk_container_add(GTK_CONTAINER(listitem), cols); |
430 | gtk_widget_show(cols); |
431 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
432 | gtk_widget_show(listitem); |
433 | |
0bd8d76d |
434 | if (ctrl->listbox.multisel) { |
435 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
436 | GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj); |
437 | } else { |
438 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
439 | GTK_SIGNAL_FUNC(listitem_single_key), uc->adj); |
440 | } |
441 | gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event", |
442 | GTK_SIGNAL_FUNC(widget_focus), dp); |
0f5e1f75 |
443 | gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event", |
444 | GTK_SIGNAL_FUNC(listitem_button), dp); |
445 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
446 | } else { |
447 | /* |
448 | * List item in a combo-box list, which means the sensible |
449 | * thing to do is make it a perfectly normal label. Hence |
450 | * tabs are disregarded. |
451 | */ |
452 | GtkWidget *listitem = gtk_list_item_new_with_label(text); |
453 | |
454 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
455 | gtk_widget_show(listitem); |
456 | |
457 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
458 | } |
459 | |
460 | dp->flags &= ~FLAG_UPDATING_COMBO_LIST; |
d9b15094 |
461 | } |
462 | |
463 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
464 | { |
0f5e1f75 |
465 | struct dlgparam *dp = (struct dlgparam *)dlg; |
466 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
467 | GList *children; |
468 | GtkObject *item; |
469 | |
470 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
471 | uc->ctrl->generic.type == CTRL_LISTBOX); |
472 | assert(uc->menu != NULL || uc->list != NULL); |
473 | |
474 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
475 | uc->list)); |
476 | item = GTK_OBJECT(g_list_nth_data(children, index)); |
0bd8d76d |
477 | g_list_free(children); |
0f5e1f75 |
478 | |
479 | return (int)gtk_object_get_data(GTK_OBJECT(item), "user-data"); |
d9b15094 |
480 | } |
481 | |
482 | /* dlg_listbox_index returns <0 if no single element is selected. */ |
483 | int dlg_listbox_index(union control *ctrl, void *dlg) |
484 | { |
0f5e1f75 |
485 | struct dlgparam *dp = (struct dlgparam *)dlg; |
486 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
487 | GList *children; |
488 | GtkWidget *item, *activeitem; |
489 | int i; |
490 | int selected = -1; |
491 | |
492 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
493 | uc->ctrl->generic.type == CTRL_LISTBOX); |
494 | assert(uc->menu != NULL || uc->list != NULL); |
495 | |
496 | if (uc->menu) |
497 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
498 | |
499 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
500 | uc->list)); |
501 | for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL; |
502 | i++, children = children->next) { |
503 | if (uc->menu ? activeitem == item : |
504 | GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) { |
0bd8d76d |
505 | if (selected == -1) |
0f5e1f75 |
506 | selected = i; |
507 | else |
0bd8d76d |
508 | selected = -2; |
0f5e1f75 |
509 | } |
510 | } |
0bd8d76d |
511 | g_list_free(children); |
512 | return selected < 0 ? -1 : selected; |
d9b15094 |
513 | } |
514 | |
515 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
516 | { |
0f5e1f75 |
517 | struct dlgparam *dp = (struct dlgparam *)dlg; |
518 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
519 | GList *children; |
520 | GtkWidget *item, *activeitem; |
521 | |
522 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
523 | uc->ctrl->generic.type == CTRL_LISTBOX); |
524 | assert(uc->menu != NULL || uc->list != NULL); |
525 | |
526 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
527 | uc->list)); |
528 | item = GTK_WIDGET(g_list_nth_data(children, index)); |
0bd8d76d |
529 | g_list_free(children); |
0f5e1f75 |
530 | |
531 | if (uc->menu) { |
532 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
533 | return item == activeitem; |
534 | } else { |
535 | return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED; |
536 | } |
d9b15094 |
537 | } |
538 | |
539 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
540 | { |
0f5e1f75 |
541 | struct dlgparam *dp = (struct dlgparam *)dlg; |
542 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
543 | |
544 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
545 | uc->ctrl->generic.type == CTRL_LISTBOX); |
546 | assert(uc->optmenu != NULL || uc->list != NULL); |
547 | |
548 | if (uc->optmenu) { |
549 | gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index); |
550 | } else { |
551 | gtk_list_select_item(GTK_LIST(uc->list), index); |
552 | } |
d9b15094 |
553 | } |
554 | |
555 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
556 | { |
0f5e1f75 |
557 | struct dlgparam *dp = (struct dlgparam *)dlg; |
558 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
559 | |
560 | assert(uc->ctrl->generic.type == CTRL_TEXT); |
561 | assert(uc->text != NULL); |
562 | |
563 | gtk_label_set_text(GTK_LABEL(uc->text), text); |
d9b15094 |
564 | } |
565 | |
566 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
567 | { |
0f5e1f75 |
568 | struct dlgparam *dp = (struct dlgparam *)dlg; |
569 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
570 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
571 | assert(uc->entry != NULL); |
572 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path); |
d9b15094 |
573 | } |
574 | |
575 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
576 | { |
0f5e1f75 |
577 | struct dlgparam *dp = (struct dlgparam *)dlg; |
578 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
579 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
580 | assert(uc->entry != NULL); |
581 | strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
582 | lenof(fn->path)); |
583 | fn->path[lenof(fn->path)-1] = '\0'; |
d9b15094 |
584 | } |
585 | |
586 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs) |
587 | { |
0f5e1f75 |
588 | struct dlgparam *dp = (struct dlgparam *)dlg; |
589 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
590 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
591 | assert(uc->entry != NULL); |
592 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name); |
d9b15094 |
593 | } |
594 | |
595 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs) |
596 | { |
0f5e1f75 |
597 | struct dlgparam *dp = (struct dlgparam *)dlg; |
598 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
599 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
600 | assert(uc->entry != NULL); |
601 | strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
602 | lenof(fs->name)); |
603 | fs->name[lenof(fs->name)-1] = '\0'; |
d9b15094 |
604 | } |
605 | |
606 | /* |
607 | * Bracketing a large set of updates in these two functions will |
608 | * cause the front end (if possible) to delay updating the screen |
609 | * until it's all complete, thus avoiding flicker. |
610 | */ |
611 | void dlg_update_start(union control *ctrl, void *dlg) |
612 | { |
0f5e1f75 |
613 | /* |
614 | * Apparently we can't do this at all in GTK. GtkCList supports |
615 | * freeze and thaw, but not GtkList. Bah. |
616 | */ |
d9b15094 |
617 | } |
618 | |
619 | void dlg_update_done(union control *ctrl, void *dlg) |
620 | { |
0f5e1f75 |
621 | /* |
622 | * Apparently we can't do this at all in GTK. GtkCList supports |
623 | * freeze and thaw, but not GtkList. Bah. |
624 | */ |
d9b15094 |
625 | } |
626 | |
627 | void dlg_set_focus(union control *ctrl, void *dlg) |
628 | { |
0f5e1f75 |
629 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
630 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
631 | |
632 | switch (ctrl->generic.type) { |
633 | case CTRL_CHECKBOX: |
634 | case CTRL_BUTTON: |
635 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
636 | gtk_widget_grab_focus(uc->toplevel); |
637 | break; |
638 | case CTRL_FILESELECT: |
639 | case CTRL_FONTSELECT: |
640 | case CTRL_EDITBOX: |
641 | /* Anything containing an edit box gets that focused. */ |
642 | gtk_widget_grab_focus(uc->entry); |
643 | break; |
644 | case CTRL_RADIO: |
645 | /* |
646 | * Radio buttons: we find the currently selected button and |
647 | * focus it. |
648 | */ |
649 | { |
650 | int i; |
651 | for (i = 0; i < ctrl->radio.nbuttons; i++) |
652 | if (gtk_toggle_button_get_active |
653 | (GTK_TOGGLE_BUTTON(uc->buttons[i]))) { |
654 | gtk_widget_grab_focus(uc->buttons[i]); |
655 | } |
656 | } |
657 | break; |
658 | case CTRL_LISTBOX: |
659 | /* |
660 | * If the list is really an option menu, we focus it. |
661 | * Otherwise we tell it to focus one of its children, which |
662 | * appears to do the Right Thing. |
663 | */ |
664 | if (uc->optmenu) { |
665 | gtk_widget_grab_focus(uc->optmenu); |
666 | } else { |
667 | assert(uc->list != NULL); |
668 | gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD); |
669 | } |
670 | break; |
671 | } |
d9b15094 |
672 | } |
673 | |
674 | /* |
675 | * During event processing, you might well want to give an error |
676 | * indication to the user. dlg_beep() is a quick and easy generic |
677 | * error; dlg_error() puts up a message-box or equivalent. |
678 | */ |
679 | void dlg_beep(void *dlg) |
680 | { |
0f5e1f75 |
681 | gdk_beep(); |
d9b15094 |
682 | } |
683 | |
0bd8d76d |
684 | static void errmsg_button_clicked(GtkButton *button, gpointer data) |
685 | { |
686 | gtk_widget_destroy(GTK_WIDGET(data)); |
687 | } |
688 | |
d9b15094 |
689 | void dlg_error_msg(void *dlg, char *msg) |
690 | { |
0f5e1f75 |
691 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
692 | GtkWidget *window, *text, *ok; |
693 | |
694 | window = gtk_dialog_new(); |
695 | text = gtk_label_new(msg); |
696 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
697 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
698 | text, FALSE, FALSE, 0); |
699 | gtk_widget_show(text); |
700 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
701 | ok = gtk_button_new_with_label("OK"); |
702 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
703 | ok, FALSE, FALSE, 0); |
704 | gtk_widget_show(ok); |
705 | GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT); |
706 | gtk_window_set_default(GTK_WINDOW(window), ok); |
707 | gtk_signal_connect(GTK_OBJECT(ok), "clicked", |
708 | GTK_SIGNAL_FUNC(errmsg_button_clicked), window); |
709 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
710 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
711 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
712 | gtk_widget_show(window); |
713 | gtk_main(); |
d9b15094 |
714 | } |
715 | |
716 | /* |
717 | * This function signals to the front end that the dialog's |
718 | * processing is completed, and passes an integer value (typically |
719 | * a success status). |
720 | */ |
721 | void dlg_end(void *dlg, int value) |
722 | { |
0f5e1f75 |
723 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
724 | dp->retval = value; |
0f5e1f75 |
725 | gtk_main_quit(); |
d9b15094 |
726 | } |
727 | |
728 | void dlg_refresh(union control *ctrl, void *dlg) |
729 | { |
0f5e1f75 |
730 | struct dlgparam *dp = (struct dlgparam *)dlg; |
731 | struct uctrl *uc; |
732 | |
733 | if (ctrl) { |
734 | if (ctrl->generic.handler != NULL) |
735 | ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH); |
736 | } else { |
737 | int i; |
738 | |
739 | for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) { |
740 | assert(uc->ctrl != NULL); |
741 | if (uc->ctrl->generic.handler != NULL) |
742 | uc->ctrl->generic.handler(uc->ctrl, dp, |
743 | dp->data, EVENT_REFRESH); |
744 | } |
745 | } |
d9b15094 |
746 | } |
747 | |
748 | void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b) |
749 | { |
0f5e1f75 |
750 | struct dlgparam *dp = (struct dlgparam *)dlg; |
751 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
752 | gdouble cvals[4]; |
753 | |
754 | GtkWidget *coloursel = |
755 | gtk_color_selection_dialog_new("Select a colour"); |
756 | GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel); |
757 | |
758 | dp->coloursel_result.ok = FALSE; |
759 | |
760 | gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE); |
761 | gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE); |
762 | cvals[0] = r / 255.0; |
763 | cvals[1] = g / 255.0; |
764 | cvals[2] = b / 255.0; |
765 | cvals[3] = 1.0; /* fully opaque! */ |
766 | gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals); |
767 | |
768 | gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data", |
769 | (gpointer)coloursel); |
770 | gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data", |
771 | (gpointer)coloursel); |
772 | gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc); |
773 | gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked", |
774 | GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp); |
775 | gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked", |
776 | GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp); |
777 | gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked", |
778 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
779 | (gpointer)coloursel); |
780 | gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked", |
781 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
782 | (gpointer)coloursel); |
783 | gtk_widget_show(coloursel); |
d9b15094 |
784 | } |
785 | |
786 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
787 | int *r, int *g, int *b) |
788 | { |
0f5e1f75 |
789 | struct dlgparam *dp = (struct dlgparam *)dlg; |
790 | if (dp->coloursel_result.ok) { |
791 | *r = dp->coloursel_result.r; |
792 | *g = dp->coloursel_result.g; |
793 | *b = dp->coloursel_result.b; |
794 | return 1; |
795 | } else |
796 | return 0; |
d9b15094 |
797 | } |
798 | |
0f5e1f75 |
799 | /* ---------------------------------------------------------------------- |
800 | * Signal handlers while the dialog box is active. |
801 | */ |
802 | |
0bd8d76d |
803 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
804 | gpointer data) |
805 | { |
806 | struct dlgparam *dp = (struct dlgparam *)data; |
807 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
808 | union control *focus; |
809 | |
810 | if (uc && uc->ctrl) |
811 | focus = uc->ctrl; |
812 | else |
813 | focus = NULL; |
814 | |
815 | if (focus != dp->currfocus) { |
816 | dp->lastfocus = dp->currfocus; |
817 | dp->currfocus = focus; |
818 | } |
819 | |
820 | return FALSE; |
821 | } |
822 | |
0f5e1f75 |
823 | static void button_clicked(GtkButton *button, gpointer data) |
824 | { |
825 | struct dlgparam *dp = (struct dlgparam *)data; |
826 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
827 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
828 | } |
829 | |
830 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
831 | { |
832 | struct dlgparam *dp = (struct dlgparam *)data; |
833 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb)); |
834 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
835 | } |
836 | |
1c291fcf |
837 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
838 | { |
839 | /* |
840 | * GtkEntry has a nasty habit of eating the Return key, which |
841 | * is unhelpful since it doesn't actually _do_ anything with it |
842 | * (it calls gtk_widget_activate, but our edit boxes never need |
843 | * activating). So I catch Return before GtkEntry sees it, and |
844 | * pass it straight on to the parent widget. Effect: hitting |
845 | * Return in an edit box will now activate the default button |
846 | * in the dialog just like it will everywhere else. |
847 | */ |
848 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
849 | gint return_val; |
0bd8d76d |
850 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
1c291fcf |
851 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
852 | event, &return_val); |
853 | return return_val; |
854 | } |
855 | return FALSE; |
856 | } |
857 | |
0f5e1f75 |
858 | static void editbox_changed(GtkEditable *ed, gpointer data) |
859 | { |
860 | struct dlgparam *dp = (struct dlgparam *)data; |
861 | if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) { |
862 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
863 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
864 | } |
865 | } |
866 | |
867 | static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event, |
868 | gpointer data) |
869 | { |
870 | struct dlgparam *dp = (struct dlgparam *)data; |
871 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
872 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH); |
873 | } |
874 | |
0bd8d76d |
875 | static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data, |
876 | int multiple) |
877 | { |
878 | GtkAdjustment *adj = GTK_ADJUSTMENT(data); |
879 | |
880 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
881 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down || |
882 | event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up || |
883 | event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) { |
884 | /* |
885 | * Up, Down, PgUp or PgDn have been pressed on a ListItem |
886 | * in a list box. So, if the list box is single-selection: |
887 | * |
888 | * - if the list item in question isn't already selected, |
889 | * we simply select it. |
890 | * - otherwise, we find the next one (or next |
891 | * however-far-away) in whichever direction we're going, |
892 | * and select that. |
893 | * + in this case, we must also fiddle with the |
894 | * scrollbar to ensure the newly selected item is |
895 | * actually visible. |
896 | * |
897 | * If it's multiple-selection, we do all of the above |
898 | * except actually selecting anything, so we move the focus |
899 | * and fiddle the scrollbar to follow it. |
900 | */ |
901 | GtkWidget *list = item->parent; |
902 | |
903 | gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event"); |
904 | |
905 | if (!multiple && |
906 | GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) { |
907 | gtk_list_select_child(GTK_LIST(list), item); |
908 | } else { |
909 | int direction = |
910 | (event->keyval==GDK_Up || event->keyval==GDK_KP_Up || |
911 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
912 | ? -1 : +1; |
913 | int step = |
914 | (event->keyval==GDK_Page_Down || |
915 | event->keyval==GDK_KP_Page_Down || |
916 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
917 | ? 2 : 1; |
918 | int i, n; |
919 | GtkWidget *thisitem; |
920 | GList *children, *chead; |
921 | |
922 | chead = children = gtk_container_children(GTK_CONTAINER(list)); |
923 | |
924 | n = g_list_length(children); |
925 | |
926 | if (step == 2) { |
927 | /* |
928 | * Figure out how many list items to a screenful, |
929 | * and adjust the step appropriately. |
930 | */ |
931 | step = 0.5 + adj->page_size * n / (adj->upper - adj->lower); |
932 | step--; /* go by one less than that */ |
933 | } |
934 | |
935 | i = 0; |
936 | while (children != NULL) { |
937 | if (item == children->data) |
938 | break; |
939 | children = children->next; |
940 | i++; |
941 | } |
942 | |
943 | while (step > 0) { |
944 | if (direction < 0 && i > 0) |
945 | children = children->prev, i--; |
946 | else if (direction > 0 && i < n-1) |
947 | children = children->next, i++; |
948 | step--; |
949 | } |
950 | |
951 | if (children && children->data) { |
952 | if (!multiple) |
953 | gtk_list_select_child(GTK_LIST(list), |
954 | GTK_WIDGET(children->data)); |
955 | gtk_widget_grab_focus(GTK_WIDGET(children->data)); |
956 | gtk_adjustment_clamp_page |
957 | (adj, |
958 | adj->lower + (adj->upper-adj->lower) * i / n, |
959 | adj->lower + (adj->upper-adj->lower) * (i+1) / n); |
960 | } |
961 | |
962 | g_list_free(chead); |
963 | } |
964 | return TRUE; |
965 | } |
966 | |
967 | return FALSE; |
968 | } |
969 | |
970 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
971 | gpointer data) |
972 | { |
973 | listitem_key(item, event, data, FALSE); |
974 | } |
975 | |
976 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
977 | gpointer data) |
978 | { |
979 | listitem_key(item, event, data, TRUE); |
980 | } |
981 | |
982 | static int listitem_button(GtkWidget *item, GdkEventButton *event, |
0f5e1f75 |
983 | gpointer data) |
984 | { |
985 | struct dlgparam *dp = (struct dlgparam *)data; |
986 | if (event->type == GDK_2BUTTON_PRESS || |
987 | event->type == GDK_3BUTTON_PRESS) { |
988 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item)); |
989 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
0bd8d76d |
990 | return TRUE; |
0f5e1f75 |
991 | } |
0bd8d76d |
992 | return FALSE; |
0f5e1f75 |
993 | } |
994 | |
995 | static void list_selchange(GtkList *list, gpointer data) |
996 | { |
997 | struct dlgparam *dp = (struct dlgparam *)data; |
998 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list)); |
999 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
1000 | } |
1001 | |
1002 | static void menuitem_activate(GtkMenuItem *item, gpointer data) |
1003 | { |
1004 | struct dlgparam *dp = (struct dlgparam *)data; |
1005 | GtkWidget *menushell = GTK_WIDGET(item)->parent; |
1006 | gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data"); |
1007 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu)); |
1008 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
1009 | } |
1010 | |
1011 | static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction) |
1012 | { |
1013 | int index = dlg_listbox_index(uc->ctrl, dp); |
1014 | GList *children = gtk_container_children(GTK_CONTAINER(uc->list)); |
1015 | GtkWidget *child; |
1016 | |
1017 | if ((index < 0) || |
1018 | (index == 0 && direction < 0) || |
1019 | (index == g_list_length(children)-1 && direction > 0)) { |
1020 | gdk_beep(); |
1021 | return; |
1022 | } |
1023 | |
1024 | child = g_list_nth_data(children, index); |
1025 | gtk_widget_ref(child); |
1026 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
0bd8d76d |
1027 | g_list_free(children); |
1028 | |
0f5e1f75 |
1029 | children = NULL; |
1030 | children = g_list_append(children, child); |
1031 | gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction); |
1032 | gtk_list_select_item(GTK_LIST(uc->list), index + direction); |
1033 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
1034 | } |
1035 | |
1036 | static void draglist_up(GtkButton *button, gpointer data) |
1037 | { |
1038 | struct dlgparam *dp = (struct dlgparam *)data; |
1039 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1040 | draglist_move(dp, uc, -1); |
1041 | } |
1042 | |
1043 | static void draglist_down(GtkButton *button, gpointer data) |
1044 | { |
1045 | struct dlgparam *dp = (struct dlgparam *)data; |
1046 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1047 | draglist_move(dp, uc, +1); |
1048 | } |
1049 | |
1050 | static void filesel_ok(GtkButton *button, gpointer data) |
1051 | { |
1052 | struct dlgparam *dp = (struct dlgparam *)data; |
1053 | gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1054 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data"); |
1055 | char *name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); |
1056 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
1057 | } |
1058 | |
1059 | static void fontsel_ok(GtkButton *button, gpointer data) |
1060 | { |
1061 | struct dlgparam *dp = (struct dlgparam *)data; |
1062 | gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1063 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data"); |
1064 | char *name = gtk_font_selection_dialog_get_font_name |
1065 | (GTK_FONT_SELECTION_DIALOG(fontsel)); |
1066 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
1067 | } |
1068 | |
1069 | static void coloursel_ok(GtkButton *button, gpointer data) |
1070 | { |
1071 | struct dlgparam *dp = (struct dlgparam *)data; |
1072 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1073 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
1074 | gdouble cvals[4]; |
1075 | gtk_color_selection_get_color |
1076 | (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel), |
1077 | cvals); |
1078 | dp->coloursel_result.r = (int) (255 * cvals[0]); |
1079 | dp->coloursel_result.g = (int) (255 * cvals[1]); |
1080 | dp->coloursel_result.b = (int) (255 * cvals[2]); |
1081 | dp->coloursel_result.ok = TRUE; |
1082 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
1083 | } |
1084 | |
1085 | static void coloursel_cancel(GtkButton *button, gpointer data) |
1086 | { |
1087 | struct dlgparam *dp = (struct dlgparam *)data; |
1088 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1089 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
1090 | dp->coloursel_result.ok = FALSE; |
1091 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
1092 | } |
1093 | |
1094 | static void filefont_clicked(GtkButton *button, gpointer data) |
1095 | { |
1096 | struct dlgparam *dp = (struct dlgparam *)data; |
1097 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1098 | |
1099 | if (uc->ctrl->generic.type == CTRL_FILESELECT) { |
1100 | GtkWidget *filesel = |
1101 | gtk_file_selection_new(uc->ctrl->fileselect.title); |
1102 | gtk_window_set_modal(GTK_WINDOW(filesel), TRUE); |
1103 | gtk_object_set_data |
1104 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data", |
1105 | (gpointer)filesel); |
1106 | gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc); |
1107 | gtk_signal_connect |
1108 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1109 | GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp); |
1110 | gtk_signal_connect_object |
1111 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1112 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1113 | gtk_signal_connect_object |
1114 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", |
1115 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1116 | gtk_widget_show(filesel); |
1117 | } |
1118 | |
1119 | if (uc->ctrl->generic.type == CTRL_FONTSELECT) { |
1120 | gchar *spacings[] = { "c", "m", NULL }; |
0bd8d76d |
1121 | gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry)); |
0f5e1f75 |
1122 | GtkWidget *fontsel = |
1123 | gtk_font_selection_dialog_new("Select a font"); |
1124 | gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE); |
1125 | gtk_font_selection_dialog_set_filter |
1126 | (GTK_FONT_SELECTION_DIALOG(fontsel), |
1127 | GTK_FONT_FILTER_BASE, GTK_FONT_ALL, |
1128 | NULL, NULL, NULL, NULL, spacings, NULL); |
0bd8d76d |
1129 | if (!gtk_font_selection_dialog_set_font_name |
1130 | (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) { |
1131 | /* |
1132 | * If the font name wasn't found as it was, try opening |
1133 | * it and extracting its FONT property. This should |
1134 | * have the effect of mapping short aliases into true |
1135 | * XLFDs. |
1136 | */ |
1137 | GdkFont *font = gdk_font_load(fontname); |
1138 | if (font) { |
1139 | XFontStruct *xfs = GDK_FONT_XFONT(font); |
1140 | Display *disp = GDK_FONT_XDISPLAY(font); |
1141 | Atom fontprop = XInternAtom(disp, "FONT", False); |
1142 | unsigned long ret; |
1143 | if (XGetFontProperty(xfs, fontprop, &ret)) { |
1144 | char *name = XGetAtomName(disp, (Atom)ret); |
1145 | if (name) |
1146 | gtk_font_selection_dialog_set_font_name |
1147 | (GTK_FONT_SELECTION_DIALOG(fontsel), name); |
1148 | } |
1149 | gdk_font_unref(font); |
1150 | } |
1151 | } |
0f5e1f75 |
1152 | gtk_object_set_data |
1153 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1154 | "user-data", (gpointer)fontsel); |
1155 | gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc); |
1156 | gtk_signal_connect |
1157 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1158 | "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp); |
1159 | gtk_signal_connect_object |
1160 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1161 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
1162 | (gpointer)fontsel); |
1163 | gtk_signal_connect_object |
1164 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button), |
1165 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
1166 | (gpointer)fontsel); |
1167 | gtk_widget_show(fontsel); |
1168 | } |
1169 | } |
1170 | |
1171 | /* ---------------------------------------------------------------------- |
d9b15094 |
1172 | * This function does the main layout work: it reads a controlset, |
1173 | * it creates the relevant GTK controls, and returns a GtkWidget |
1174 | * containing the result. (This widget might be a title of some |
1175 | * sort, it might be a Columns containing many controls, or it |
1176 | * might be a GtkFrame containing a Columns; whatever it is, it's |
1177 | * definitely a GtkWidget and should probably be added to a |
1178 | * GtkVbox.) |
1c291fcf |
1179 | * |
1180 | * `listitemheight' is used to calculate a usize for list boxes: it |
1181 | * should be the height from the size request of a GtkListItem. |
1182 | * |
1183 | * `win' is required for setting the default button. If it is |
1184 | * non-NULL, all buttons created will be default-capable (so they |
1185 | * have extra space round them for the default highlight). |
d9b15094 |
1186 | */ |
1c291fcf |
1187 | GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs, |
1188 | struct controlset *s, int listitemheight, |
1189 | GtkWindow *win) |
d9b15094 |
1190 | { |
1191 | Columns *cols; |
1192 | GtkWidget *ret; |
1193 | int i; |
1194 | |
1195 | if (!s->boxname && s->boxtitle) { |
1196 | /* This controlset is a panel title. */ |
1197 | return gtk_label_new(s->boxtitle); |
1198 | } |
1199 | |
1200 | /* |
1201 | * Otherwise, we expect to be laying out actual controls, so |
1202 | * we'll start by creating a Columns for the purpose. |
1203 | */ |
1204 | cols = COLUMNS(columns_new(4)); |
1205 | ret = GTK_WIDGET(cols); |
1206 | gtk_widget_show(ret); |
1207 | |
1208 | /* |
1209 | * Create a containing frame if we have a box name. |
1210 | */ |
1211 | if (*s->boxname) { |
1212 | ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */ |
1213 | gtk_container_set_border_width(GTK_CONTAINER(cols), 4); |
1214 | gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols)); |
1215 | gtk_widget_show(ret); |
1216 | } |
1217 | |
1218 | /* |
1219 | * Now iterate through the controls themselves, create them, |
1220 | * and add them to the Columns. |
1221 | */ |
1222 | for (i = 0; i < s->ncontrols; i++) { |
1223 | union control *ctrl = s->ctrls[i]; |
1c291fcf |
1224 | struct uctrl *uc; |
1225 | int left = FALSE; |
d9b15094 |
1226 | GtkWidget *w = NULL; |
1227 | |
1228 | switch (ctrl->generic.type) { |
1229 | case CTRL_COLUMNS: |
1230 | { |
1231 | static const int simplecols[1] = { 100 }; |
1232 | columns_set_cols(cols, ctrl->columns.ncols, |
1233 | (ctrl->columns.percentages ? |
1234 | ctrl->columns.percentages : simplecols)); |
1235 | } |
1236 | continue; /* no actual control created */ |
1237 | case CTRL_TABDELAY: |
0f5e1f75 |
1238 | { |
1239 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl); |
1240 | if (uc) |
1241 | columns_taborder_last(cols, uc->toplevel); |
1242 | } |
d9b15094 |
1243 | continue; /* no actual control created */ |
1c291fcf |
1244 | } |
1245 | |
1246 | uc = smalloc(sizeof(struct uctrl)); |
1247 | uc->ctrl = ctrl; |
1248 | uc->privdata = NULL; |
1249 | uc->privdata_needs_free = FALSE; |
1250 | uc->buttons = NULL; |
1251 | uc->entry = uc->list = uc->menu = NULL; |
1252 | uc->button = uc->optmenu = uc->text = NULL; |
1253 | |
1254 | switch (ctrl->generic.type) { |
d9b15094 |
1255 | case CTRL_BUTTON: |
1256 | w = gtk_button_new_with_label(ctrl->generic.label); |
1c291fcf |
1257 | if (win) { |
1258 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
1259 | if (ctrl->button.isdefault) |
1260 | gtk_window_set_default(win, w); |
0bd8d76d |
1261 | if (ctrl->button.iscancel) |
1262 | dp->cancelbutton = w; |
1c291fcf |
1263 | } |
0f5e1f75 |
1264 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
1265 | GTK_SIGNAL_FUNC(button_clicked), dp); |
0bd8d76d |
1266 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
1267 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1268 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut, |
1269 | SHORTCUT_UCTRL, uc); |
d9b15094 |
1270 | break; |
1271 | case CTRL_CHECKBOX: |
1272 | w = gtk_check_button_new_with_label(ctrl->generic.label); |
0f5e1f75 |
1273 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
1274 | GTK_SIGNAL_FUNC(button_toggled), dp); |
0bd8d76d |
1275 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
1276 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1277 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut, |
1278 | SHORTCUT_UCTRL, uc); |
1279 | left = TRUE; |
d9b15094 |
1280 | break; |
1281 | case CTRL_RADIO: |
1282 | /* |
1283 | * Radio buttons get to go inside their own Columns, no |
1284 | * matter what. |
1285 | */ |
1286 | { |
1287 | gint i, *percentages; |
1288 | GSList *group; |
1289 | |
1290 | w = columns_new(1); |
1291 | if (ctrl->generic.label) { |
1292 | GtkWidget *label = gtk_label_new(ctrl->generic.label); |
1293 | columns_add(COLUMNS(w), label, 0, 1); |
1294 | columns_force_left_align(COLUMNS(w), label); |
1295 | gtk_widget_show(label); |
1c291fcf |
1296 | shortcut_add(scs, label, ctrl->radio.shortcut, |
1297 | SHORTCUT_UCTRL, uc); |
d9b15094 |
1298 | } |
1299 | percentages = g_new(gint, ctrl->radio.ncolumns); |
1300 | for (i = 0; i < ctrl->radio.ncolumns; i++) { |
1301 | percentages[i] = |
1302 | ((100 * (i+1) / ctrl->radio.ncolumns) - |
1303 | 100 * i / ctrl->radio.ncolumns); |
1304 | } |
1305 | columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns, |
1306 | percentages); |
1307 | g_free(percentages); |
1308 | group = NULL; |
0f5e1f75 |
1309 | |
1c291fcf |
1310 | uc->nbuttons = ctrl->radio.nbuttons; |
1311 | uc->buttons = smalloc(uc->nbuttons * sizeof(GtkWidget *)); |
0f5e1f75 |
1312 | |
d9b15094 |
1313 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
1314 | GtkWidget *b; |
1315 | gint colstart; |
1316 | |
1317 | b = (gtk_radio_button_new_with_label |
1318 | (group, ctrl->radio.buttons[i])); |
1c291fcf |
1319 | uc->buttons[i] = b; |
d9b15094 |
1320 | group = gtk_radio_button_group(GTK_RADIO_BUTTON(b)); |
1321 | colstart = i % ctrl->radio.ncolumns; |
1322 | columns_add(COLUMNS(w), b, colstart, |
1323 | (i == ctrl->radio.nbuttons-1 ? |
1324 | ctrl->radio.ncolumns - colstart : 1)); |
1c291fcf |
1325 | columns_force_left_align(COLUMNS(w), b); |
d9b15094 |
1326 | gtk_widget_show(b); |
0f5e1f75 |
1327 | gtk_signal_connect(GTK_OBJECT(b), "toggled", |
1328 | GTK_SIGNAL_FUNC(button_toggled), dp); |
0bd8d76d |
1329 | gtk_signal_connect(GTK_OBJECT(b), "focus_in_event", |
1330 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1331 | if (ctrl->radio.shortcuts) { |
1332 | shortcut_add(scs, GTK_BIN(b)->child, |
1333 | ctrl->radio.shortcuts[i], |
1334 | SHORTCUT_UCTRL, uc); |
1335 | } |
d9b15094 |
1336 | } |
1337 | } |
1338 | break; |
1339 | case CTRL_EDITBOX: |
1340 | if (ctrl->editbox.has_list) { |
1341 | w = gtk_combo_new(); |
0f5e1f75 |
1342 | gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE); |
1c291fcf |
1343 | uc->entry = GTK_COMBO(w)->entry; |
1344 | uc->list = GTK_COMBO(w)->list; |
d9b15094 |
1345 | } else { |
1346 | w = gtk_entry_new(); |
1347 | if (ctrl->editbox.password) |
1348 | gtk_entry_set_visibility(GTK_ENTRY(w), FALSE); |
1c291fcf |
1349 | uc->entry = w; |
d9b15094 |
1350 | } |
1c291fcf |
1351 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
0f5e1f75 |
1352 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
1c291fcf |
1353 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
1354 | GTK_SIGNAL_FUNC(editbox_key), dp); |
0bd8d76d |
1355 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
1356 | GTK_SIGNAL_FUNC(widget_focus), dp); |
d9b15094 |
1357 | /* |
1358 | * Edit boxes, for some strange reason, have a minimum |
1359 | * width of 150 in GTK 1.2. We don't want this - we'd |
1360 | * rather the edit boxes acquired their natural width |
1361 | * from the column layout of the rest of the box. |
1362 | */ |
1363 | { |
1364 | GtkRequisition req; |
1365 | gtk_widget_size_request(w, &req); |
1366 | gtk_widget_set_usize(w, 10, req.height); |
1367 | } |
1368 | if (ctrl->generic.label) { |
1369 | GtkWidget *label, *container; |
1370 | |
1371 | label = gtk_label_new(ctrl->generic.label); |
1c291fcf |
1372 | shortcut_add(scs, label, ctrl->editbox.shortcut, |
1373 | SHORTCUT_FOCUS, uc->entry); |
d9b15094 |
1374 | |
1375 | container = columns_new(4); |
1376 | if (ctrl->editbox.percentwidth == 100) { |
1377 | columns_add(COLUMNS(container), label, 0, 1); |
1378 | columns_force_left_align(COLUMNS(container), label); |
1379 | columns_add(COLUMNS(container), w, 0, 1); |
1380 | } else { |
1381 | gint percentages[2]; |
1382 | percentages[1] = ctrl->editbox.percentwidth; |
1383 | percentages[0] = 100 - ctrl->editbox.percentwidth; |
1384 | columns_set_cols(COLUMNS(container), 2, percentages); |
1385 | columns_add(COLUMNS(container), label, 0, 1); |
1386 | columns_force_left_align(COLUMNS(container), label); |
1387 | columns_add(COLUMNS(container), w, 1, 1); |
1388 | } |
1389 | gtk_widget_show(label); |
1390 | gtk_widget_show(w); |
1391 | |
1392 | w = container; |
1393 | } |
1c291fcf |
1394 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event", |
0f5e1f75 |
1395 | GTK_SIGNAL_FUNC(editbox_lostfocus), dp); |
d9b15094 |
1396 | break; |
1d0d4a3b |
1397 | case CTRL_FILESELECT: |
1398 | case CTRL_FONTSELECT: |
1399 | { |
1400 | GtkWidget *ww; |
1401 | GtkRequisition req; |
1402 | char *browsebtn = |
1403 | (ctrl->generic.type == CTRL_FILESELECT ? |
1404 | "Browse..." : "Change..."); |
1405 | |
1406 | gint percentages[] = { 75, 25 }; |
1407 | w = columns_new(4); |
1408 | columns_set_cols(COLUMNS(w), 2, percentages); |
1409 | |
1410 | if (ctrl->generic.label) { |
1411 | ww = gtk_label_new(ctrl->generic.label); |
1412 | columns_add(COLUMNS(w), ww, 0, 2); |
1413 | columns_force_left_align(COLUMNS(w), ww); |
1414 | gtk_widget_show(ww); |
1c291fcf |
1415 | shortcut_add(scs, ww, |
1416 | (ctrl->generic.type == CTRL_FILESELECT ? |
1417 | ctrl->fileselect.shortcut : |
1418 | ctrl->fontselect.shortcut), |
1419 | SHORTCUT_UCTRL, uc); |
1d0d4a3b |
1420 | } |
1421 | |
1c291fcf |
1422 | uc->entry = ww = gtk_entry_new(); |
1d0d4a3b |
1423 | gtk_widget_size_request(ww, &req); |
1424 | gtk_widget_set_usize(ww, 10, req.height); |
1425 | columns_add(COLUMNS(w), ww, 0, 1); |
1426 | gtk_widget_show(ww); |
1427 | |
1c291fcf |
1428 | uc->button = ww = gtk_button_new_with_label(browsebtn); |
1d0d4a3b |
1429 | columns_add(COLUMNS(w), ww, 1, 1); |
1430 | gtk_widget_show(ww); |
0f5e1f75 |
1431 | |
1c291fcf |
1432 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
1433 | GTK_SIGNAL_FUNC(editbox_key), dp); |
1434 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
0f5e1f75 |
1435 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
0bd8d76d |
1436 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
1437 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1438 | gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event", |
1439 | GTK_SIGNAL_FUNC(widget_focus), dp); |
0f5e1f75 |
1440 | gtk_signal_connect(GTK_OBJECT(ww), "clicked", |
1441 | GTK_SIGNAL_FUNC(filefont_clicked), dp); |
1d0d4a3b |
1442 | } |
1443 | break; |
1444 | case CTRL_LISTBOX: |
1445 | if (ctrl->listbox.height == 0) { |
1c291fcf |
1446 | uc->optmenu = w = gtk_option_menu_new(); |
1447 | uc->menu = gtk_menu_new(); |
1448 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu); |
1449 | gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data", |
1450 | (gpointer)uc->optmenu); |
0bd8d76d |
1451 | gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event", |
1452 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1453 | } else { |
1c291fcf |
1454 | uc->list = gtk_list_new(); |
0bd8d76d |
1455 | if (ctrl->listbox.multisel) { |
1456 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1457 | GTK_SELECTION_MULTIPLE); |
1458 | } else { |
1459 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1460 | GTK_SELECTION_SINGLE); |
1461 | } |
1d0d4a3b |
1462 | w = gtk_scrolled_window_new(NULL, NULL); |
1463 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w), |
1c291fcf |
1464 | uc->list); |
1d0d4a3b |
1465 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w), |
1466 | GTK_POLICY_NEVER, |
1467 | GTK_POLICY_AUTOMATIC); |
0bd8d76d |
1468 | uc->adj = gtk_scrolled_window_get_vadjustment |
1469 | (GTK_SCROLLED_WINDOW(w)); |
1470 | |
1c291fcf |
1471 | gtk_widget_show(uc->list); |
1472 | gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed", |
0f5e1f75 |
1473 | GTK_SIGNAL_FUNC(list_selchange), dp); |
0bd8d76d |
1474 | gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event", |
1475 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1476 | |
1477 | /* |
1478 | * Adjust the height of the scrolled window to the |
1479 | * minimum given by the height parameter. |
1480 | * |
1481 | * This piece of guesswork is a horrid hack based |
1482 | * on looking inside the GTK 1.2 sources |
1483 | * (specifically gtkviewport.c, which appears to be |
1484 | * the widget which provides the border around the |
1485 | * scrolling area). Anyone lets me know how I can |
1486 | * do this in a way which isn't at risk from GTK |
1487 | * upgrades, I'd be grateful. |
1488 | */ |
1489 | { |
1c291fcf |
1490 | int edge = GTK_WIDGET(uc->list)->style->klass->ythickness; |
1d0d4a3b |
1491 | gtk_widget_set_usize(w, 10, |
1492 | 2*edge + (ctrl->listbox.height * |
1493 | listitemheight)); |
1494 | } |
1d0d4a3b |
1495 | |
1496 | if (ctrl->listbox.draglist) { |
1497 | /* |
1498 | * GTK doesn't appear to make it easy to |
1499 | * implement a proper draggable list; so |
1500 | * instead I'm just going to have to put an Up |
1501 | * and a Down button to the right of the actual |
1502 | * list box. Ah well. |
1503 | */ |
1504 | GtkWidget *cols, *button; |
1505 | static const gint percentages[2] = { 80, 20 }; |
1506 | |
1507 | cols = columns_new(4); |
1508 | columns_set_cols(COLUMNS(cols), 2, percentages); |
1509 | columns_add(COLUMNS(cols), w, 0, 1); |
1510 | gtk_widget_show(w); |
1511 | button = gtk_button_new_with_label("Up"); |
1512 | columns_add(COLUMNS(cols), button, 1, 1); |
1513 | gtk_widget_show(button); |
0f5e1f75 |
1514 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1515 | GTK_SIGNAL_FUNC(draglist_up), dp); |
0bd8d76d |
1516 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
1517 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1518 | button = gtk_button_new_with_label("Down"); |
1519 | columns_add(COLUMNS(cols), button, 1, 1); |
1520 | gtk_widget_show(button); |
0f5e1f75 |
1521 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1522 | GTK_SIGNAL_FUNC(draglist_down), dp); |
0bd8d76d |
1523 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
1524 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1525 | |
1526 | w = cols; |
1527 | } |
1528 | |
1529 | } |
1530 | if (ctrl->generic.label) { |
1531 | GtkWidget *label, *container; |
1532 | |
1533 | label = gtk_label_new(ctrl->generic.label); |
1534 | |
1535 | container = columns_new(4); |
1536 | if (ctrl->listbox.percentwidth == 100) { |
1537 | columns_add(COLUMNS(container), label, 0, 1); |
1538 | columns_force_left_align(COLUMNS(container), label); |
1539 | columns_add(COLUMNS(container), w, 0, 1); |
1540 | } else { |
1541 | gint percentages[2]; |
1542 | percentages[1] = ctrl->listbox.percentwidth; |
1543 | percentages[0] = 100 - ctrl->listbox.percentwidth; |
1544 | columns_set_cols(COLUMNS(container), 2, percentages); |
1545 | columns_add(COLUMNS(container), label, 0, 1); |
1546 | columns_force_left_align(COLUMNS(container), label); |
1547 | columns_add(COLUMNS(container), w, 1, 1); |
1548 | } |
1549 | gtk_widget_show(label); |
1550 | gtk_widget_show(w); |
1c291fcf |
1551 | shortcut_add(scs, label, ctrl->listbox.shortcut, |
1552 | SHORTCUT_UCTRL, uc); |
1d0d4a3b |
1553 | w = container; |
1554 | } |
1555 | break; |
d9b15094 |
1556 | case CTRL_TEXT: |
1c291fcf |
1557 | uc->text = w = gtk_label_new(ctrl->generic.label); |
0bd8d76d |
1558 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0); |
d9b15094 |
1559 | gtk_label_set_line_wrap(GTK_LABEL(w), TRUE); |
1d0d4a3b |
1560 | /* FIXME: deal with wrapping! */ |
d9b15094 |
1561 | break; |
1562 | } |
0f5e1f75 |
1563 | |
1c291fcf |
1564 | assert(w != NULL); |
0f5e1f75 |
1565 | |
1c291fcf |
1566 | columns_add(cols, w, |
1567 | COLUMN_START(ctrl->generic.column), |
1568 | COLUMN_SPAN(ctrl->generic.column)); |
1569 | if (left) |
1570 | columns_force_left_align(cols, w); |
1571 | gtk_widget_show(w); |
1572 | |
1573 | uc->toplevel = w; |
1574 | dlg_add_uctrl(dp, uc); |
d9b15094 |
1575 | } |
1576 | |
1577 | return ret; |
1578 | } |
1579 | |
1580 | struct selparam { |
1c291fcf |
1581 | struct dlgparam *dp; |
d9b15094 |
1582 | Panels *panels; |
1583 | GtkWidget *panel, *treeitem; |
1c291fcf |
1584 | struct Shortcuts shortcuts; |
d9b15094 |
1585 | }; |
1586 | |
1587 | static void treeitem_sel(GtkItem *item, gpointer data) |
1588 | { |
1589 | struct selparam *sp = (struct selparam *)data; |
1590 | |
1591 | panels_switch_to(sp->panels, sp->panel); |
1c291fcf |
1592 | |
1593 | sp->dp->shortcuts = &sp->shortcuts; |
0bd8d76d |
1594 | sp->dp->currtreeitem = sp->treeitem; |
d9b15094 |
1595 | } |
1596 | |
0bd8d76d |
1597 | static void window_destroy(GtkWidget *widget, gpointer data) |
d9b15094 |
1598 | { |
1599 | gtk_main_quit(); |
1600 | } |
1601 | |
1c291fcf |
1602 | int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
1603 | { |
1604 | struct dlgparam *dp = (struct dlgparam *)data; |
1605 | |
0bd8d76d |
1606 | if (event->keyval == GDK_Escape && dp->cancelbutton) { |
1607 | gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked"); |
1c291fcf |
1608 | return TRUE; |
1609 | } |
1610 | |
1611 | if ((event->state & GDK_MOD1_MASK) && |
1612 | (unsigned char)event->string[0] > 0 && |
1613 | (unsigned char)event->string[0] <= 127) { |
1614 | int schr = (unsigned char)event->string[0]; |
1615 | struct Shortcut *sc = &dp->shortcuts->sc[schr]; |
1616 | |
1617 | switch (sc->action) { |
1618 | case SHORTCUT_FOCUS: |
1619 | gtk_widget_grab_focus(sc->widget); |
1620 | break; |
1621 | case SHORTCUT_UCTRL: |
1622 | /* |
1623 | * We must do something sensible with a uctrl. |
1624 | * Precisely what this is depends on the type of |
1625 | * control. |
1626 | */ |
1627 | switch (sc->uc->ctrl->generic.type) { |
1628 | case CTRL_CHECKBOX: |
1629 | case CTRL_BUTTON: |
1630 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
1631 | gtk_widget_grab_focus(sc->uc->toplevel); |
1632 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel), |
1633 | "clicked"); |
1634 | break; |
1635 | case CTRL_FILESELECT: |
1636 | case CTRL_FONTSELECT: |
1637 | /* File/font selectors have their buttons pressed (ooer), |
1638 | * and focus transferred to the edit box. */ |
1639 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button), |
1640 | "clicked"); |
1641 | gtk_widget_grab_focus(sc->uc->entry); |
1642 | break; |
1643 | case CTRL_RADIO: |
1644 | /* |
1645 | * Radio buttons are fun, because they have |
1646 | * multiple shortcuts. We must find whether the |
1647 | * activated shortcut is the shortcut for the whole |
1648 | * group, or for a particular button. In the former |
1649 | * case, we find the currently selected button and |
1650 | * focus it; in the latter, we focus-and-click the |
1651 | * button whose shortcut was pressed. |
1652 | */ |
1653 | if (schr == sc->uc->ctrl->radio.shortcut) { |
1654 | int i; |
1655 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
1656 | if (gtk_toggle_button_get_active |
1657 | (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) { |
1658 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
1659 | } |
1660 | } else if (sc->uc->ctrl->radio.shortcuts) { |
1661 | int i; |
1662 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
1663 | if (schr == sc->uc->ctrl->radio.shortcuts[i]) { |
1664 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
1665 | gtk_signal_emit_by_name |
1666 | (GTK_OBJECT(sc->uc->buttons[i]), "clicked"); |
1667 | } |
1668 | } |
1669 | break; |
1670 | case CTRL_LISTBOX: |
1671 | /* |
0bd8d76d |
1672 | * If the list is really an option menu, we focus |
1673 | * and click it. Otherwise we tell it to focus one |
1674 | * of its children, which appears to do the Right |
1675 | * Thing. |
1c291fcf |
1676 | */ |
1677 | if (sc->uc->optmenu) { |
1678 | GdkEventButton bev; |
1679 | gint returnval; |
1680 | |
1681 | gtk_widget_grab_focus(sc->uc->optmenu); |
1682 | /* Option menus don't work using the "clicked" signal. |
1683 | * We need to manufacture a button press event :-/ */ |
1684 | bev.type = GDK_BUTTON_PRESS; |
1685 | bev.button = 1; |
1686 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu), |
1687 | "button_press_event", |
1688 | &bev, &returnval); |
1689 | } else { |
0bd8d76d |
1690 | assert(sc->uc->list != NULL); |
1691 | |
1692 | gtk_container_focus(GTK_CONTAINER(sc->uc->list), |
1693 | GTK_DIR_TAB_FORWARD); |
1c291fcf |
1694 | } |
1695 | break; |
1696 | } |
1697 | break; |
1698 | } |
1699 | } |
1700 | |
1701 | return FALSE; |
1702 | } |
1703 | |
0bd8d76d |
1704 | int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
1705 | { |
1706 | struct dlgparam *dp = (struct dlgparam *)data; |
1707 | |
1708 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
1709 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down) { |
1710 | int i, j = -1; |
1711 | for (i = 0; i < dp->ntreeitems; i++) |
1712 | if (widget == dp->treeitems[i]) { |
1713 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) { |
1714 | if (i > 0) |
1715 | j = i-1; |
1716 | } else { |
1717 | if (i < dp->ntreeitems-1) |
1718 | j = i+1; |
1719 | } |
1720 | break; |
1721 | } |
1722 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1723 | "key_press_event"); |
1724 | if (j >= 0) { |
1725 | gint return_val; |
1726 | gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle"); |
1727 | gtk_widget_grab_focus(dp->treeitems[j]); |
1728 | } |
1729 | return TRUE; |
1730 | } |
1731 | |
1732 | return FALSE; |
1733 | } |
1734 | |
1735 | gint tree_focus(GtkContainer *container, GtkDirectionType direction, |
1736 | gpointer data) |
1737 | { |
1738 | struct dlgparam *dp = (struct dlgparam *)data; |
1739 | int i, f, dir; |
1740 | |
1741 | gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus"); |
1742 | dir = (direction == GTK_DIR_UP || direction == GTK_DIR_LEFT || |
1743 | direction == GTK_DIR_TAB_BACKWARD) ? -1 : +1; |
1744 | |
1745 | /* |
1746 | * See if any of the treeitems has the focus. |
1747 | */ |
1748 | f = -1; |
1749 | for (i = 0; i < dp->ntreeitems; i++) |
1750 | if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) { |
1751 | f = i; |
1752 | break; |
1753 | } |
1754 | |
1755 | /* |
1756 | * If there's a focused treeitem, we return FALSE to cause the |
1757 | * focus to move on to some totally other control. If not, we |
1758 | * focus the selected one. |
1759 | */ |
1760 | if (f >= 0) |
1761 | return FALSE; |
1762 | else { |
1763 | gtk_widget_grab_focus(dp->currtreeitem); |
1764 | return TRUE; |
1765 | } |
1766 | } |
1767 | |
1c291fcf |
1768 | void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
1769 | int chr, int action, void *ptr) |
1770 | { |
1771 | GtkLabel *label = GTK_LABEL(labelw); |
1772 | gchar *currstr, *pattern; |
1773 | int i; |
1774 | |
1775 | if (chr == NO_SHORTCUT) |
1776 | return; |
1777 | |
1778 | chr = tolower((unsigned char)chr); |
1779 | |
1780 | assert(scs->sc[chr].action == SHORTCUT_EMPTY); |
1781 | |
1782 | scs->sc[chr].action = action; |
1783 | |
1784 | if (action == SHORTCUT_FOCUS) { |
1785 | scs->sc[chr].uc = NULL; |
1786 | scs->sc[chr].widget = (GtkWidget *)ptr; |
1787 | } else { |
1788 | scs->sc[chr].widget = NULL; |
1789 | scs->sc[chr].uc = (struct uctrl *)ptr; |
1790 | } |
1791 | |
1792 | gtk_label_get(label, &currstr); |
1793 | for (i = 0; currstr[i]; i++) |
1794 | if (tolower((unsigned char)currstr[i]) == chr) { |
1795 | GtkRequisition req; |
1796 | |
1797 | pattern = dupprintf("%*s_", i, ""); |
1798 | |
1799 | gtk_widget_size_request(GTK_WIDGET(label), &req); |
1800 | gtk_label_set_pattern(label, pattern); |
1801 | gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height); |
1802 | |
1803 | sfree(pattern); |
1804 | break; |
1805 | } |
1806 | } |
1807 | |
0bd8d76d |
1808 | int do_config_box(void) |
d9b15094 |
1809 | { |
1810 | GtkWidget *window, *hbox, *vbox, *cols, *label, |
1811 | *tree, *treescroll, *panels, *panelvbox; |
1d0d4a3b |
1812 | int index, level, listitemheight; |
d9b15094 |
1813 | struct controlbox *ctrlbox; |
1814 | char *path; |
1815 | GtkTreeItem *treeitemlevels[8]; |
1816 | GtkTree *treelevels[8]; |
1817 | Config cfg; |
0f5e1f75 |
1818 | struct dlgparam dp; |
1819 | struct sesslist sl; |
1c291fcf |
1820 | struct Shortcuts scs; |
d9b15094 |
1821 | |
1822 | struct selparam *selparams = NULL; |
1823 | int nselparams = 0, selparamsize = 0; |
1824 | |
1825 | do_defaults(NULL, &cfg); |
1826 | |
0f5e1f75 |
1827 | dlg_init(&dp); |
1828 | |
1d0d4a3b |
1829 | { |
1830 | GtkWidget *listitem = gtk_list_item_new_with_label("foo"); |
1831 | GtkRequisition req; |
1832 | gtk_widget_size_request(listitem, &req); |
1833 | listitemheight = req.height; |
1834 | gtk_widget_unref(listitem); |
1835 | } |
1836 | |
0f5e1f75 |
1837 | sl.nsessions = 0; |
1838 | |
1c291fcf |
1839 | for (index = 0; index < lenof(scs.sc); index++) { |
1840 | scs.sc[index].action = SHORTCUT_EMPTY; |
1841 | } |
1842 | |
d9b15094 |
1843 | ctrlbox = ctrl_new_box(); |
0f5e1f75 |
1844 | setup_config_box(ctrlbox, &sl, FALSE, 0); |
1d0d4a3b |
1845 | unix_setup_config_box(ctrlbox, FALSE); |
d9b15094 |
1846 | |
1847 | window = gtk_dialog_new(); |
d9b15094 |
1848 | hbox = gtk_hbox_new(FALSE, 4); |
1849 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0); |
1d0d4a3b |
1850 | gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
d9b15094 |
1851 | gtk_widget_show(hbox); |
1852 | vbox = gtk_vbox_new(FALSE, 4); |
1853 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
1854 | gtk_widget_show(vbox); |
1855 | cols = columns_new(4); |
1856 | gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0); |
1857 | gtk_widget_show(cols); |
1858 | label = gtk_label_new("Category:"); |
1859 | columns_add(COLUMNS(cols), label, 0, 1); |
1860 | columns_force_left_align(COLUMNS(cols), label); |
1861 | gtk_widget_show(label); |
1862 | treescroll = gtk_scrolled_window_new(NULL, NULL); |
1863 | tree = gtk_tree_new(); |
0bd8d76d |
1864 | gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event", |
1865 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
1c291fcf |
1866 | shortcut_add(&scs, label, 'g', SHORTCUT_FOCUS, tree); |
d9b15094 |
1867 | gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM); |
1d0d4a3b |
1868 | gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE); |
d9b15094 |
1869 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll), |
1870 | tree); |
1871 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll), |
1872 | GTK_POLICY_NEVER, |
1873 | GTK_POLICY_AUTOMATIC); |
0bd8d76d |
1874 | gtk_signal_connect(GTK_OBJECT(tree), "focus", |
1875 | GTK_SIGNAL_FUNC(tree_focus), &dp); |
d9b15094 |
1876 | gtk_widget_show(tree); |
1877 | gtk_widget_show(treescroll); |
1878 | gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0); |
1879 | panels = panels_new(); |
1880 | gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0); |
1881 | gtk_widget_show(panels); |
1882 | |
1883 | panelvbox = NULL; |
1884 | path = NULL; |
1885 | level = 0; |
1886 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
1887 | struct controlset *s = ctrlbox->ctrlsets[index]; |
1c291fcf |
1888 | GtkWidget *w; |
d9b15094 |
1889 | |
1890 | if (!*s->pathname) { |
1c291fcf |
1891 | w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window)); |
d9b15094 |
1892 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
1893 | w, TRUE, TRUE, 0); |
1894 | } else { |
1895 | int j = path ? ctrl_path_compare(s->pathname, path) : 0; |
1896 | if (j != INT_MAX) { /* add to treeview, start new panel */ |
1897 | char *c; |
1898 | GtkWidget *treeitem; |
1899 | int first; |
1900 | |
1901 | /* |
1902 | * We expect never to find an implicit path |
1903 | * component. For example, we expect never to see |
1904 | * A/B/C followed by A/D/E, because that would |
1905 | * _implicitly_ create A/D. All our path prefixes |
1906 | * are expected to contain actual controls and be |
1907 | * selectable in the treeview; so we would expect |
1908 | * to see A/D _explicitly_ before encountering |
1909 | * A/D/E. |
1910 | */ |
1911 | assert(j == ctrl_path_elements(s->pathname) - 1); |
1912 | |
1913 | c = strrchr(s->pathname, '/'); |
1914 | if (!c) |
1915 | c = s->pathname; |
1916 | else |
1917 | c++; |
1918 | |
1919 | treeitem = gtk_tree_item_new_with_label(c); |
1920 | assert(j-1 < level); |
1921 | if (j > 0) { |
1922 | if (!treelevels[j-1]) { |
1923 | treelevels[j-1] = GTK_TREE(gtk_tree_new()); |
1924 | gtk_tree_item_set_subtree |
1925 | (treeitemlevels[j-1], |
1926 | GTK_WIDGET(treelevels[j-1])); |
1927 | gtk_tree_item_expand(treeitemlevels[j-1]); |
1928 | } |
1929 | gtk_tree_append(treelevels[j-1], treeitem); |
1930 | } else { |
1931 | gtk_tree_append(GTK_TREE(tree), treeitem); |
1932 | } |
1933 | treeitemlevels[j] = GTK_TREE_ITEM(treeitem); |
1934 | treelevels[j] = NULL; |
1935 | level = j+1; |
1936 | |
0bd8d76d |
1937 | gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event", |
1938 | GTK_SIGNAL_FUNC(tree_key_press), &dp); |
1939 | gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event", |
1940 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
1941 | |
d9b15094 |
1942 | gtk_widget_show(treeitem); |
1943 | |
1944 | path = s->pathname; |
1945 | |
1946 | first = (panelvbox == NULL); |
1947 | |
1948 | panelvbox = gtk_vbox_new(FALSE, 4); |
1949 | gtk_container_add(GTK_CONTAINER(panels), panelvbox); |
1950 | if (first) { |
1951 | panels_switch_to(PANELS(panels), panelvbox); |
1952 | gtk_tree_select_child(GTK_TREE(tree), treeitem); |
1953 | } |
1954 | |
1955 | if (nselparams >= selparamsize) { |
1956 | selparamsize += 16; |
1957 | selparams = srealloc(selparams, |
1958 | selparamsize * sizeof(*selparams)); |
1959 | } |
1c291fcf |
1960 | selparams[nselparams].dp = &dp; |
d9b15094 |
1961 | selparams[nselparams].panels = PANELS(panels); |
1962 | selparams[nselparams].panel = panelvbox; |
1c291fcf |
1963 | selparams[nselparams].shortcuts = scs; /* structure copy */ |
d9b15094 |
1964 | selparams[nselparams].treeitem = treeitem; |
1965 | nselparams++; |
1966 | |
1967 | } |
1968 | |
1c291fcf |
1969 | w = layout_ctrls(&dp, |
1970 | &selparams[nselparams-1].shortcuts, |
1971 | s, listitemheight, NULL); |
d9b15094 |
1972 | gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0); |
1d0d4a3b |
1973 | gtk_widget_show(w); |
d9b15094 |
1974 | } |
1975 | } |
1976 | |
0bd8d76d |
1977 | dp.ntreeitems = nselparams; |
1978 | dp.treeitems = smalloc(dp.ntreeitems * sizeof(GtkWidget *)); |
1979 | |
d9b15094 |
1980 | for (index = 0; index < nselparams; index++) { |
1981 | gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select", |
1982 | GTK_SIGNAL_FUNC(treeitem_sel), |
1983 | &selparams[index]); |
0bd8d76d |
1984 | dp.treeitems[index] = selparams[index].treeitem; |
d9b15094 |
1985 | } |
1986 | |
0f5e1f75 |
1987 | dp.data = &cfg; |
1988 | dlg_refresh(NULL, &dp); |
1989 | |
1c291fcf |
1990 | dp.shortcuts = &selparams[0].shortcuts; |
0bd8d76d |
1991 | dp.currtreeitem = dp.treeitems[0]; |
1992 | dp.lastfocus = NULL; |
1993 | dp.retval = 0; |
1c291fcf |
1994 | |
d9b15094 |
1995 | gtk_widget_show(window); |
1996 | |
0bd8d76d |
1997 | /* |
1998 | * Set focus into the first available control. |
1999 | */ |
2000 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
2001 | struct controlset *s = ctrlbox->ctrlsets[index]; |
2002 | int done = 0; |
2003 | int j; |
2004 | |
2005 | if (*s->pathname) { |
2006 | for (j = 0; j < s->ncontrols; j++) |
2007 | if (s->ctrls[j]->generic.type != CTRL_TABDELAY && |
2008 | s->ctrls[j]->generic.type != CTRL_COLUMNS && |
2009 | s->ctrls[j]->generic.type != CTRL_TEXT) { |
2010 | dlg_set_focus(s->ctrls[j], &dp); |
2011 | dp.lastfocus = s->ctrls[j]; |
2012 | done = 1; |
2013 | break; |
2014 | } |
2015 | } |
2016 | if (done) |
2017 | break; |
2018 | } |
2019 | |
d9b15094 |
2020 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
0bd8d76d |
2021 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
1c291fcf |
2022 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
2023 | GTK_SIGNAL_FUNC(win_key_press), &dp); |
d9b15094 |
2024 | |
2025 | gtk_main(); |
2026 | |
0f5e1f75 |
2027 | dlg_cleanup(&dp); |
d9b15094 |
2028 | sfree(selparams); |
0bd8d76d |
2029 | |
2030 | return dp.retval; |
d9b15094 |
2031 | } |
2032 | |
2033 | /* ====================================================================== |
2034 | * Below here is a stub main program which allows the dialog box |
2035 | * code to be compiled and tested with a minimal amount of the rest |
2036 | * of PuTTY. |
2037 | */ |
2038 | |
2039 | #ifdef TESTMODE |
2040 | |
2041 | /* Compile command for testing: |
2042 | |
1d0d4a3b |
2043 | gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \ |
2044 | ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \ |
d9b15094 |
2045 | -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs` |
2046 | */ |
2047 | |
2048 | void modalfatalbox(char *p, ...) |
2049 | { |
2050 | va_list ap; |
2051 | fprintf(stderr, "FATAL ERROR: "); |
2052 | va_start(ap, p); |
2053 | vfprintf(stderr, p, ap); |
2054 | va_end(ap); |
2055 | fputc('\n', stderr); |
2056 | exit(1); |
2057 | } |
2058 | |
2059 | char *cp_name(int codepage) |
2060 | { |
2061 | return (codepage == 123 ? "testing123" : |
2062 | codepage == 234 ? "testing234" : |
2063 | codepage == 345 ? "testing345" : |
2064 | "unknown"); |
2065 | } |
2066 | |
2067 | char *cp_enumerate(int index) |
2068 | { |
2069 | return (index == 0 ? "testing123" : |
2070 | index == 1 ? "testing234" : |
2071 | NULL); |
2072 | } |
2073 | |
2074 | int decode_codepage(char *cp_name) |
2075 | { |
2076 | return (!strcmp(cp_name, "testing123") ? 123 : |
2077 | !strcmp(cp_name, "testing234") ? 234 : |
2078 | !strcmp(cp_name, "testing345") ? 345 : |
2079 | -2); |
2080 | } |
2081 | |
2082 | struct printer_enum_tag { int dummy; } printer_test; |
2083 | |
2084 | printer_enum *printer_start_enum(int *nprinters_ptr) { |
2085 | *nprinters_ptr = 2; |
2086 | return &printer_test; |
2087 | } |
2088 | char *printer_get_name(printer_enum *pe, int i) { |
2089 | return (i==0 ? "lpr" : i==1 ? "lpr -Pfoobar" : NULL); |
2090 | } |
2091 | void printer_finish_enum(printer_enum *pe) { } |
2092 | |
2093 | char *platform_default_s(const char *name) |
2094 | { |
2095 | return NULL; |
2096 | } |
2097 | |
2098 | int platform_default_i(const char *name, int def) |
2099 | { |
2100 | return def; |
2101 | } |
2102 | |
2103 | FontSpec platform_default_fontspec(const char *name) |
2104 | { |
2105 | FontSpec ret; |
0f5e1f75 |
2106 | if (!strcmp(name, "Font")) |
2107 | strcpy(ret.name, "fixed"); |
2108 | else |
2109 | *ret.name = '\0'; |
d9b15094 |
2110 | return ret; |
2111 | } |
2112 | |
2113 | Filename platform_default_filename(const char *name) |
2114 | { |
2115 | Filename ret; |
0f5e1f75 |
2116 | if (!strcmp(name, "LogFileName")) |
2117 | strcpy(ret.path, "putty.log"); |
2118 | else |
2119 | *ret.path = '\0'; |
d9b15094 |
2120 | return ret; |
2121 | } |
2122 | |
2123 | char *x_get_default(const char *key) |
2124 | { |
2125 | return NULL; |
2126 | } |
2127 | |
2128 | int main(int argc, char **argv) |
2129 | { |
2130 | gtk_init(&argc, &argv); |
0bd8d76d |
2131 | printf("returned %d\n", do_config_box()); |
d9b15094 |
2132 | return 0; |
2133 | } |
2134 | |
2135 | #endif |