d9b15094 |
1 | /* |
2 | * gtkdlg.c - GTK implementation of the PuTTY configuration box. |
3 | */ |
4 | |
1d0d4a3b |
5 | /* |
6 | * TODO: |
7 | * |
1d0d4a3b |
8 | * - keyboard stuff |
9 | * + accelerators |
1d0d4a3b |
10 | * + default button |
11 | * |
0f5e1f75 |
12 | * - focus stuff |
13 | * + `last focused' for nasty sessionsaver hack |
14 | * + set focus into a sensible control to start with |
15 | * + perhaps we need uc->primary as the right widget to |
16 | * forcibly focus? |
17 | * |
18 | * - dlg_error_msg |
19 | * |
20 | * - must return a value from the dialog box! |
21 | * |
22 | * - font selection hiccup: the default `fixed' is not |
23 | * automatically translated into its expanded XLFD form when the |
24 | * font selector is started. It should be. |
25 | * |
1d0d4a3b |
26 | * - cosmetics: |
27 | * + can't we _somehow_ have less leading between radio buttons? |
28 | * + wrapping text widgets, the horror, the horror |
29 | * + labels and their associated edit boxes don't line up |
30 | * properly |
31 | */ |
32 | |
d9b15094 |
33 | #include <assert.h> |
34 | #include <gtk/gtk.h> |
35 | |
36 | #include "gtkcols.h" |
37 | #include "gtkpanel.h" |
38 | |
39 | #ifdef TESTMODE |
40 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
41 | #endif |
42 | |
43 | #include "putty.h" |
44 | #include "dialog.h" |
0f5e1f75 |
45 | #include "tree234.h" |
46 | |
47 | struct uctrl { |
48 | union control *ctrl; |
49 | GtkWidget *toplevel; |
50 | void *privdata; |
51 | int privdata_needs_free; |
52 | GtkWidget **buttons; int nbuttons; /* for radio buttons */ |
53 | GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */ |
54 | GtkWidget *list; /* for combobox, listbox */ |
55 | GtkWidget *menu; /* for optionmenu (==droplist) */ |
56 | GtkWidget *optmenu; /* also for optionmenu */ |
57 | GtkWidget *text; /* for text */ |
58 | }; |
59 | |
60 | struct dlgparam { |
61 | tree234 *byctrl, *bywidget; |
62 | void *data; |
63 | struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */ |
64 | /* `flags' are set to indicate when a GTK signal handler is being called |
65 | * due to automatic processing and should not flag a user event. */ |
66 | int flags; |
67 | }; |
68 | #define FLAG_UPDATING_COMBO_LIST 1 |
69 | |
70 | /* |
71 | * Forward references. |
72 | */ |
73 | static void listitem_button(GtkWidget *item, GdkEventButton *event, |
74 | gpointer data); |
75 | static void menuitem_activate(GtkMenuItem *item, gpointer data); |
76 | static void coloursel_ok(GtkButton *button, gpointer data); |
77 | static void coloursel_cancel(GtkButton *button, gpointer data); |
78 | |
79 | static int uctrl_cmp_byctrl(void *av, void *bv) |
80 | { |
81 | struct uctrl *a = (struct uctrl *)av; |
82 | struct uctrl *b = (struct uctrl *)bv; |
83 | if (a->ctrl < b->ctrl) |
84 | return -1; |
85 | else if (a->ctrl > b->ctrl) |
86 | return +1; |
87 | return 0; |
88 | } |
89 | |
90 | static int uctrl_cmp_byctrl_find(void *av, void *bv) |
91 | { |
92 | union control *a = (union control *)av; |
93 | struct uctrl *b = (struct uctrl *)bv; |
94 | if (a < b->ctrl) |
95 | return -1; |
96 | else if (a > b->ctrl) |
97 | return +1; |
98 | return 0; |
99 | } |
100 | |
101 | static int uctrl_cmp_bywidget(void *av, void *bv) |
102 | { |
103 | struct uctrl *a = (struct uctrl *)av; |
104 | struct uctrl *b = (struct uctrl *)bv; |
105 | if (a->toplevel < b->toplevel) |
106 | return -1; |
107 | else if (a->toplevel > b->toplevel) |
108 | return +1; |
109 | return 0; |
110 | } |
111 | |
112 | static int uctrl_cmp_bywidget_find(void *av, void *bv) |
113 | { |
114 | GtkWidget *a = (GtkWidget *)av; |
115 | struct uctrl *b = (struct uctrl *)bv; |
116 | if (a < b->toplevel) |
117 | return -1; |
118 | else if (a > b->toplevel) |
119 | return +1; |
120 | return 0; |
121 | } |
122 | |
123 | static void dlg_init(struct dlgparam *dp) |
124 | { |
125 | dp->byctrl = newtree234(uctrl_cmp_byctrl); |
126 | dp->bywidget = newtree234(uctrl_cmp_bywidget); |
127 | dp->coloursel_result.ok = FALSE; |
128 | } |
129 | |
130 | static void dlg_cleanup(struct dlgparam *dp) |
131 | { |
132 | struct uctrl *uc; |
133 | |
134 | freetree234(dp->byctrl); /* doesn't free the uctrls inside */ |
135 | while ( (uc = index234(dp->bywidget, 0)) != NULL) { |
136 | del234(dp->bywidget, uc); |
137 | if (uc->privdata_needs_free) |
138 | sfree(uc->privdata); |
139 | sfree(uc->buttons); |
140 | sfree(uc); |
141 | } |
142 | freetree234(dp->bywidget); |
143 | } |
144 | |
145 | static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc) |
146 | { |
147 | add234(dp->byctrl, uc); |
148 | add234(dp->bywidget, uc); |
149 | } |
150 | |
151 | static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl) |
152 | { |
153 | return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find); |
154 | } |
155 | |
156 | static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w) |
157 | { |
158 | struct uctrl *ret = NULL; |
159 | do { |
160 | ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find); |
161 | if (ret) |
162 | return ret; |
163 | w = w->parent; |
164 | } while (w); |
165 | return ret; |
166 | } |
d9b15094 |
167 | |
d9b15094 |
168 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
169 | { |
0f5e1f75 |
170 | struct dlgparam *dp = (struct dlgparam *)dlg; |
171 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
172 | return uc->privdata; |
d9b15094 |
173 | } |
174 | |
175 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
176 | { |
0f5e1f75 |
177 | struct dlgparam *dp = (struct dlgparam *)dlg; |
178 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
179 | uc->privdata = ptr; |
180 | uc->privdata_needs_free = FALSE; |
d9b15094 |
181 | } |
182 | |
183 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
184 | { |
0f5e1f75 |
185 | struct dlgparam *dp = (struct dlgparam *)dlg; |
186 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
187 | uc->privdata = smalloc(size); |
188 | uc->privdata_needs_free = FALSE; |
189 | return uc->privdata; |
d9b15094 |
190 | } |
191 | |
192 | union control *dlg_last_focused(void *dlg) |
193 | { |
0f5e1f75 |
194 | struct dlgparam *dp = (struct dlgparam *)dlg; |
d9b15094 |
195 | return NULL; /* FIXME */ |
196 | } |
197 | |
0f5e1f75 |
198 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int which) |
d9b15094 |
199 | { |
0f5e1f75 |
200 | struct dlgparam *dp = (struct dlgparam *)dlg; |
201 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
202 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
203 | assert(uc->buttons != NULL); |
204 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE); |
d9b15094 |
205 | } |
206 | |
207 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
208 | { |
0f5e1f75 |
209 | struct dlgparam *dp = (struct dlgparam *)dlg; |
210 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
211 | int i; |
212 | |
213 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
214 | assert(uc->buttons != NULL); |
215 | for (i = 0; i < uc->nbuttons; i++) |
216 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i]))) |
217 | return i; |
218 | return 0; /* got to return something */ |
d9b15094 |
219 | } |
220 | |
221 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
222 | { |
0f5e1f75 |
223 | struct dlgparam *dp = (struct dlgparam *)dlg; |
224 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
225 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
226 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked); |
d9b15094 |
227 | } |
228 | |
229 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
230 | { |
0f5e1f75 |
231 | struct dlgparam *dp = (struct dlgparam *)dlg; |
232 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
233 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
234 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel)); |
d9b15094 |
235 | } |
236 | |
237 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
238 | { |
0f5e1f75 |
239 | struct dlgparam *dp = (struct dlgparam *)dlg; |
240 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
241 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
242 | assert(uc->entry != NULL); |
243 | gtk_entry_set_text(GTK_ENTRY(uc->entry), text); |
d9b15094 |
244 | } |
245 | |
246 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
247 | { |
0f5e1f75 |
248 | struct dlgparam *dp = (struct dlgparam *)dlg; |
249 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
250 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
251 | assert(uc->entry != NULL); |
252 | strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
253 | length); |
254 | buffer[length-1] = '\0'; |
255 | } |
256 | |
257 | static void container_remove_and_destroy(GtkWidget *w, gpointer data) |
258 | { |
259 | GtkContainer *cont = GTK_CONTAINER(data); |
260 | /* gtk_container_remove will unref the widget for us; we need not. */ |
261 | gtk_container_remove(cont, w); |
d9b15094 |
262 | } |
263 | |
264 | /* The `listbox' functions can also apply to combo boxes. */ |
265 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
266 | { |
0f5e1f75 |
267 | struct dlgparam *dp = (struct dlgparam *)dlg; |
268 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
269 | GtkContainer *cont; |
270 | |
271 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
272 | uc->ctrl->generic.type == CTRL_LISTBOX); |
273 | assert(uc->menu != NULL || uc->list != NULL); |
274 | |
275 | cont = (uc->menu ? GTK_CONTAINER(uc->menu) : GTK_CONTAINER(uc->list)); |
276 | |
277 | gtk_container_foreach(cont, container_remove_and_destroy, cont); |
d9b15094 |
278 | } |
279 | |
280 | void dlg_listbox_del(union control *ctrl, void *dlg, int index) |
281 | { |
0f5e1f75 |
282 | struct dlgparam *dp = (struct dlgparam *)dlg; |
283 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
284 | |
285 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
286 | uc->ctrl->generic.type == CTRL_LISTBOX); |
287 | assert(uc->menu != NULL || uc->list != NULL); |
288 | |
289 | if (uc->menu) { |
290 | gtk_container_remove |
291 | (GTK_CONTAINER(uc->menu), |
292 | g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index)); |
293 | } else { |
294 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
295 | } |
d9b15094 |
296 | } |
297 | |
298 | void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) |
299 | { |
0f5e1f75 |
300 | dlg_listbox_addwithindex(ctrl, dlg, text, 0); |
d9b15094 |
301 | } |
302 | |
303 | /* |
304 | * Each listbox entry may have a numeric id associated with it. |
305 | * Note that some front ends only permit a string to be stored at |
306 | * each position, which means that _if_ you put two identical |
307 | * strings in any listbox then you MUST not assign them different |
308 | * IDs and expect to get meaningful results back. |
309 | */ |
310 | void dlg_listbox_addwithindex(union control *ctrl, void *dlg, |
311 | char const *text, int id) |
312 | { |
0f5e1f75 |
313 | struct dlgparam *dp = (struct dlgparam *)dlg; |
314 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
315 | |
316 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
317 | uc->ctrl->generic.type == CTRL_LISTBOX); |
318 | assert(uc->menu != NULL || uc->list != NULL); |
319 | |
320 | dp->flags |= FLAG_UPDATING_COMBO_LIST; |
321 | |
322 | if (uc->menu) { |
323 | /* |
324 | * List item in a drop-down (but non-combo) list. Tabs are |
325 | * ignored; we just provide a standard menu item with the |
326 | * text. |
327 | */ |
328 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
329 | |
330 | gtk_container_add(GTK_CONTAINER(uc->menu), menuitem); |
331 | gtk_widget_show(menuitem); |
332 | |
333 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", (gpointer)id); |
334 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
335 | GTK_SIGNAL_FUNC(menuitem_activate), dp); |
336 | } else if (!uc->entry) { |
337 | /* |
338 | * List item in a non-combo-box list box. We make all of |
339 | * these Columns containing GtkLabels. This allows us to do |
340 | * the nasty force_left hack irrespective of whether there |
341 | * are tabs in the thing. |
342 | */ |
343 | GtkWidget *listitem = gtk_list_item_new(); |
523fd1da |
344 | GtkWidget *cols = columns_new(10); |
0f5e1f75 |
345 | gint *percents; |
346 | int i, ncols; |
347 | |
348 | /* Count the tabs in the text, and hence determine # of columns. */ |
349 | ncols = 1; |
350 | for (i = 0; text[i]; i++) |
351 | if (text[i] == '\t') |
352 | ncols++; |
353 | |
354 | assert(ncols <= |
355 | (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1)); |
356 | percents = smalloc(ncols * sizeof(gint)); |
357 | percents[ncols-1] = 100; |
358 | for (i = 0; i < ncols-1; i++) { |
359 | percents[i] = uc->ctrl->listbox.percentages[i]; |
360 | percents[ncols-1] -= percents[i]; |
361 | } |
362 | columns_set_cols(COLUMNS(cols), ncols, percents); |
363 | sfree(percents); |
364 | |
365 | for (i = 0; i < ncols; i++) { |
366 | int len = strcspn(text, "\t"); |
367 | char *dup = dupprintf("%.*s", len, text); |
368 | GtkWidget *label; |
369 | |
370 | text += len; |
371 | if (*text) text++; |
372 | label = gtk_label_new(dup); |
373 | sfree(dup); |
374 | |
375 | columns_add(COLUMNS(cols), label, i, 1); |
376 | columns_force_left_align(COLUMNS(cols), label); |
377 | gtk_widget_show(label); |
378 | } |
379 | gtk_container_add(GTK_CONTAINER(listitem), cols); |
380 | gtk_widget_show(cols); |
381 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
382 | gtk_widget_show(listitem); |
383 | |
384 | gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event", |
385 | GTK_SIGNAL_FUNC(listitem_button), dp); |
386 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
387 | } else { |
388 | /* |
389 | * List item in a combo-box list, which means the sensible |
390 | * thing to do is make it a perfectly normal label. Hence |
391 | * tabs are disregarded. |
392 | */ |
393 | GtkWidget *listitem = gtk_list_item_new_with_label(text); |
394 | |
395 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
396 | gtk_widget_show(listitem); |
397 | |
398 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
399 | } |
400 | |
401 | dp->flags &= ~FLAG_UPDATING_COMBO_LIST; |
d9b15094 |
402 | } |
403 | |
404 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
405 | { |
0f5e1f75 |
406 | struct dlgparam *dp = (struct dlgparam *)dlg; |
407 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
408 | GList *children; |
409 | GtkObject *item; |
410 | |
411 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
412 | uc->ctrl->generic.type == CTRL_LISTBOX); |
413 | assert(uc->menu != NULL || uc->list != NULL); |
414 | |
415 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
416 | uc->list)); |
417 | item = GTK_OBJECT(g_list_nth_data(children, index)); |
418 | |
419 | return (int)gtk_object_get_data(GTK_OBJECT(item), "user-data"); |
d9b15094 |
420 | } |
421 | |
422 | /* dlg_listbox_index returns <0 if no single element is selected. */ |
423 | int dlg_listbox_index(union control *ctrl, void *dlg) |
424 | { |
0f5e1f75 |
425 | struct dlgparam *dp = (struct dlgparam *)dlg; |
426 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
427 | GList *children; |
428 | GtkWidget *item, *activeitem; |
429 | int i; |
430 | int selected = -1; |
431 | |
432 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
433 | uc->ctrl->generic.type == CTRL_LISTBOX); |
434 | assert(uc->menu != NULL || uc->list != NULL); |
435 | |
436 | if (uc->menu) |
437 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
438 | |
439 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
440 | uc->list)); |
441 | for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL; |
442 | i++, children = children->next) { |
443 | if (uc->menu ? activeitem == item : |
444 | GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) { |
445 | if (selected < 0) |
446 | selected = i; |
447 | else |
448 | return -1; |
449 | } |
450 | } |
451 | return selected; |
d9b15094 |
452 | } |
453 | |
454 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
455 | { |
0f5e1f75 |
456 | struct dlgparam *dp = (struct dlgparam *)dlg; |
457 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
458 | GList *children; |
459 | GtkWidget *item, *activeitem; |
460 | |
461 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
462 | uc->ctrl->generic.type == CTRL_LISTBOX); |
463 | assert(uc->menu != NULL || uc->list != NULL); |
464 | |
465 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
466 | uc->list)); |
467 | item = GTK_WIDGET(g_list_nth_data(children, index)); |
468 | |
469 | if (uc->menu) { |
470 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
471 | return item == activeitem; |
472 | } else { |
473 | return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED; |
474 | } |
d9b15094 |
475 | } |
476 | |
477 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
478 | { |
0f5e1f75 |
479 | struct dlgparam *dp = (struct dlgparam *)dlg; |
480 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
481 | |
482 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
483 | uc->ctrl->generic.type == CTRL_LISTBOX); |
484 | assert(uc->optmenu != NULL || uc->list != NULL); |
485 | |
486 | if (uc->optmenu) { |
487 | gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index); |
488 | } else { |
489 | gtk_list_select_item(GTK_LIST(uc->list), index); |
490 | } |
d9b15094 |
491 | } |
492 | |
493 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
494 | { |
0f5e1f75 |
495 | struct dlgparam *dp = (struct dlgparam *)dlg; |
496 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
497 | |
498 | assert(uc->ctrl->generic.type == CTRL_TEXT); |
499 | assert(uc->text != NULL); |
500 | |
501 | gtk_label_set_text(GTK_LABEL(uc->text), text); |
d9b15094 |
502 | } |
503 | |
504 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
505 | { |
0f5e1f75 |
506 | struct dlgparam *dp = (struct dlgparam *)dlg; |
507 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
508 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
509 | assert(uc->entry != NULL); |
510 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path); |
d9b15094 |
511 | } |
512 | |
513 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
514 | { |
0f5e1f75 |
515 | struct dlgparam *dp = (struct dlgparam *)dlg; |
516 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
517 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
518 | assert(uc->entry != NULL); |
519 | strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
520 | lenof(fn->path)); |
521 | fn->path[lenof(fn->path)-1] = '\0'; |
d9b15094 |
522 | } |
523 | |
524 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs) |
525 | { |
0f5e1f75 |
526 | struct dlgparam *dp = (struct dlgparam *)dlg; |
527 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
528 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
529 | assert(uc->entry != NULL); |
530 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name); |
d9b15094 |
531 | } |
532 | |
533 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs) |
534 | { |
0f5e1f75 |
535 | struct dlgparam *dp = (struct dlgparam *)dlg; |
536 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
537 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
538 | assert(uc->entry != NULL); |
539 | strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
540 | lenof(fs->name)); |
541 | fs->name[lenof(fs->name)-1] = '\0'; |
d9b15094 |
542 | } |
543 | |
544 | /* |
545 | * Bracketing a large set of updates in these two functions will |
546 | * cause the front end (if possible) to delay updating the screen |
547 | * until it's all complete, thus avoiding flicker. |
548 | */ |
549 | void dlg_update_start(union control *ctrl, void *dlg) |
550 | { |
0f5e1f75 |
551 | /* |
552 | * Apparently we can't do this at all in GTK. GtkCList supports |
553 | * freeze and thaw, but not GtkList. Bah. |
554 | */ |
d9b15094 |
555 | } |
556 | |
557 | void dlg_update_done(union control *ctrl, void *dlg) |
558 | { |
0f5e1f75 |
559 | /* |
560 | * Apparently we can't do this at all in GTK. GtkCList supports |
561 | * freeze and thaw, but not GtkList. Bah. |
562 | */ |
d9b15094 |
563 | } |
564 | |
565 | void dlg_set_focus(union control *ctrl, void *dlg) |
566 | { |
0f5e1f75 |
567 | struct dlgparam *dp = (struct dlgparam *)dlg; |
d9b15094 |
568 | /* FIXME */ |
569 | } |
570 | |
571 | /* |
572 | * During event processing, you might well want to give an error |
573 | * indication to the user. dlg_beep() is a quick and easy generic |
574 | * error; dlg_error() puts up a message-box or equivalent. |
575 | */ |
576 | void dlg_beep(void *dlg) |
577 | { |
0f5e1f75 |
578 | gdk_beep(); |
d9b15094 |
579 | } |
580 | |
581 | void dlg_error_msg(void *dlg, char *msg) |
582 | { |
0f5e1f75 |
583 | struct dlgparam *dp = (struct dlgparam *)dlg; |
d9b15094 |
584 | /* FIXME */ |
585 | } |
586 | |
587 | /* |
588 | * This function signals to the front end that the dialog's |
589 | * processing is completed, and passes an integer value (typically |
590 | * a success status). |
591 | */ |
592 | void dlg_end(void *dlg, int value) |
593 | { |
0f5e1f75 |
594 | struct dlgparam *dp = (struct dlgparam *)dlg; |
595 | gtk_main_quit(); |
596 | /* FIXME: don't forget to faff about with returning a value */ |
d9b15094 |
597 | } |
598 | |
599 | void dlg_refresh(union control *ctrl, void *dlg) |
600 | { |
0f5e1f75 |
601 | struct dlgparam *dp = (struct dlgparam *)dlg; |
602 | struct uctrl *uc; |
603 | |
604 | if (ctrl) { |
605 | if (ctrl->generic.handler != NULL) |
606 | ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH); |
607 | } else { |
608 | int i; |
609 | |
610 | for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) { |
611 | assert(uc->ctrl != NULL); |
612 | if (uc->ctrl->generic.handler != NULL) |
613 | uc->ctrl->generic.handler(uc->ctrl, dp, |
614 | dp->data, EVENT_REFRESH); |
615 | } |
616 | } |
d9b15094 |
617 | } |
618 | |
619 | void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b) |
620 | { |
0f5e1f75 |
621 | struct dlgparam *dp = (struct dlgparam *)dlg; |
622 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
623 | gdouble cvals[4]; |
624 | |
625 | GtkWidget *coloursel = |
626 | gtk_color_selection_dialog_new("Select a colour"); |
627 | GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel); |
628 | |
629 | dp->coloursel_result.ok = FALSE; |
630 | |
631 | gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE); |
632 | gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE); |
633 | cvals[0] = r / 255.0; |
634 | cvals[1] = g / 255.0; |
635 | cvals[2] = b / 255.0; |
636 | cvals[3] = 1.0; /* fully opaque! */ |
637 | gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals); |
638 | |
639 | gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data", |
640 | (gpointer)coloursel); |
641 | gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data", |
642 | (gpointer)coloursel); |
643 | gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc); |
644 | gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked", |
645 | GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp); |
646 | gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked", |
647 | GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp); |
648 | gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked", |
649 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
650 | (gpointer)coloursel); |
651 | gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked", |
652 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
653 | (gpointer)coloursel); |
654 | gtk_widget_show(coloursel); |
d9b15094 |
655 | } |
656 | |
657 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
658 | int *r, int *g, int *b) |
659 | { |
0f5e1f75 |
660 | struct dlgparam *dp = (struct dlgparam *)dlg; |
661 | if (dp->coloursel_result.ok) { |
662 | *r = dp->coloursel_result.r; |
663 | *g = dp->coloursel_result.g; |
664 | *b = dp->coloursel_result.b; |
665 | return 1; |
666 | } else |
667 | return 0; |
d9b15094 |
668 | } |
669 | |
0f5e1f75 |
670 | /* ---------------------------------------------------------------------- |
671 | * Signal handlers while the dialog box is active. |
672 | */ |
673 | |
674 | static void button_clicked(GtkButton *button, gpointer data) |
675 | { |
676 | struct dlgparam *dp = (struct dlgparam *)data; |
677 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
678 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
679 | } |
680 | |
681 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
682 | { |
683 | struct dlgparam *dp = (struct dlgparam *)data; |
684 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb)); |
685 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
686 | } |
687 | |
688 | static void editbox_changed(GtkEditable *ed, gpointer data) |
689 | { |
690 | struct dlgparam *dp = (struct dlgparam *)data; |
691 | if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) { |
692 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
693 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
694 | } |
695 | } |
696 | |
697 | static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event, |
698 | gpointer data) |
699 | { |
700 | struct dlgparam *dp = (struct dlgparam *)data; |
701 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
702 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH); |
703 | } |
704 | |
705 | static void listitem_button(GtkWidget *item, GdkEventButton *event, |
706 | gpointer data) |
707 | { |
708 | struct dlgparam *dp = (struct dlgparam *)data; |
709 | if (event->type == GDK_2BUTTON_PRESS || |
710 | event->type == GDK_3BUTTON_PRESS) { |
711 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item)); |
712 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
713 | } |
714 | } |
715 | |
716 | static void list_selchange(GtkList *list, gpointer data) |
717 | { |
718 | struct dlgparam *dp = (struct dlgparam *)data; |
719 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list)); |
720 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
721 | } |
722 | |
723 | static void menuitem_activate(GtkMenuItem *item, gpointer data) |
724 | { |
725 | struct dlgparam *dp = (struct dlgparam *)data; |
726 | GtkWidget *menushell = GTK_WIDGET(item)->parent; |
727 | gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data"); |
728 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu)); |
729 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
730 | } |
731 | |
732 | static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction) |
733 | { |
734 | int index = dlg_listbox_index(uc->ctrl, dp); |
735 | GList *children = gtk_container_children(GTK_CONTAINER(uc->list)); |
736 | GtkWidget *child; |
737 | |
738 | if ((index < 0) || |
739 | (index == 0 && direction < 0) || |
740 | (index == g_list_length(children)-1 && direction > 0)) { |
741 | gdk_beep(); |
742 | return; |
743 | } |
744 | |
745 | child = g_list_nth_data(children, index); |
746 | gtk_widget_ref(child); |
747 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
748 | children = NULL; |
749 | children = g_list_append(children, child); |
750 | gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction); |
751 | gtk_list_select_item(GTK_LIST(uc->list), index + direction); |
752 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
753 | } |
754 | |
755 | static void draglist_up(GtkButton *button, gpointer data) |
756 | { |
757 | struct dlgparam *dp = (struct dlgparam *)data; |
758 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
759 | draglist_move(dp, uc, -1); |
760 | } |
761 | |
762 | static void draglist_down(GtkButton *button, gpointer data) |
763 | { |
764 | struct dlgparam *dp = (struct dlgparam *)data; |
765 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
766 | draglist_move(dp, uc, +1); |
767 | } |
768 | |
769 | static void filesel_ok(GtkButton *button, gpointer data) |
770 | { |
771 | struct dlgparam *dp = (struct dlgparam *)data; |
772 | gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
773 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data"); |
774 | char *name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); |
775 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
776 | } |
777 | |
778 | static void fontsel_ok(GtkButton *button, gpointer data) |
779 | { |
780 | struct dlgparam *dp = (struct dlgparam *)data; |
781 | gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
782 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data"); |
783 | char *name = gtk_font_selection_dialog_get_font_name |
784 | (GTK_FONT_SELECTION_DIALOG(fontsel)); |
785 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
786 | } |
787 | |
788 | static void coloursel_ok(GtkButton *button, gpointer data) |
789 | { |
790 | struct dlgparam *dp = (struct dlgparam *)data; |
791 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
792 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
793 | gdouble cvals[4]; |
794 | gtk_color_selection_get_color |
795 | (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel), |
796 | cvals); |
797 | dp->coloursel_result.r = (int) (255 * cvals[0]); |
798 | dp->coloursel_result.g = (int) (255 * cvals[1]); |
799 | dp->coloursel_result.b = (int) (255 * cvals[2]); |
800 | dp->coloursel_result.ok = TRUE; |
801 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
802 | } |
803 | |
804 | static void coloursel_cancel(GtkButton *button, gpointer data) |
805 | { |
806 | struct dlgparam *dp = (struct dlgparam *)data; |
807 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
808 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
809 | dp->coloursel_result.ok = FALSE; |
810 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
811 | } |
812 | |
813 | static void filefont_clicked(GtkButton *button, gpointer data) |
814 | { |
815 | struct dlgparam *dp = (struct dlgparam *)data; |
816 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
817 | |
818 | if (uc->ctrl->generic.type == CTRL_FILESELECT) { |
819 | GtkWidget *filesel = |
820 | gtk_file_selection_new(uc->ctrl->fileselect.title); |
821 | gtk_window_set_modal(GTK_WINDOW(filesel), TRUE); |
822 | gtk_object_set_data |
823 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data", |
824 | (gpointer)filesel); |
825 | gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc); |
826 | gtk_signal_connect |
827 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
828 | GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp); |
829 | gtk_signal_connect_object |
830 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
831 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
832 | gtk_signal_connect_object |
833 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", |
834 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
835 | gtk_widget_show(filesel); |
836 | } |
837 | |
838 | if (uc->ctrl->generic.type == CTRL_FONTSELECT) { |
839 | gchar *spacings[] = { "c", "m", NULL }; |
840 | GtkWidget *fontsel = |
841 | gtk_font_selection_dialog_new("Select a font"); |
842 | gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE); |
843 | gtk_font_selection_dialog_set_filter |
844 | (GTK_FONT_SELECTION_DIALOG(fontsel), |
845 | GTK_FONT_FILTER_BASE, GTK_FONT_ALL, |
846 | NULL, NULL, NULL, NULL, spacings, NULL); |
847 | gtk_font_selection_dialog_set_font_name |
848 | (GTK_FONT_SELECTION_DIALOG(fontsel), |
849 | gtk_entry_get_text(GTK_ENTRY(uc->entry))); |
850 | gtk_object_set_data |
851 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
852 | "user-data", (gpointer)fontsel); |
853 | gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc); |
854 | gtk_signal_connect |
855 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
856 | "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp); |
857 | gtk_signal_connect_object |
858 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
859 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
860 | (gpointer)fontsel); |
861 | gtk_signal_connect_object |
862 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button), |
863 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
864 | (gpointer)fontsel); |
865 | gtk_widget_show(fontsel); |
866 | } |
867 | } |
868 | |
869 | /* ---------------------------------------------------------------------- |
d9b15094 |
870 | * This function does the main layout work: it reads a controlset, |
871 | * it creates the relevant GTK controls, and returns a GtkWidget |
872 | * containing the result. (This widget might be a title of some |
873 | * sort, it might be a Columns containing many controls, or it |
874 | * might be a GtkFrame containing a Columns; whatever it is, it's |
875 | * definitely a GtkWidget and should probably be added to a |
876 | * GtkVbox.) |
877 | */ |
0f5e1f75 |
878 | GtkWidget *layout_ctrls(struct dlgparam *dp, struct controlset *s, |
879 | int listitemheight) |
d9b15094 |
880 | { |
881 | Columns *cols; |
882 | GtkWidget *ret; |
883 | int i; |
884 | |
885 | if (!s->boxname && s->boxtitle) { |
886 | /* This controlset is a panel title. */ |
887 | return gtk_label_new(s->boxtitle); |
888 | } |
889 | |
890 | /* |
891 | * Otherwise, we expect to be laying out actual controls, so |
892 | * we'll start by creating a Columns for the purpose. |
893 | */ |
894 | cols = COLUMNS(columns_new(4)); |
895 | ret = GTK_WIDGET(cols); |
896 | gtk_widget_show(ret); |
897 | |
898 | /* |
899 | * Create a containing frame if we have a box name. |
900 | */ |
901 | if (*s->boxname) { |
902 | ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */ |
903 | gtk_container_set_border_width(GTK_CONTAINER(cols), 4); |
904 | gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols)); |
905 | gtk_widget_show(ret); |
906 | } |
907 | |
908 | /* |
909 | * Now iterate through the controls themselves, create them, |
910 | * and add them to the Columns. |
911 | */ |
912 | for (i = 0; i < s->ncontrols; i++) { |
913 | union control *ctrl = s->ctrls[i]; |
0f5e1f75 |
914 | struct uctrl uc; |
d9b15094 |
915 | GtkWidget *w = NULL; |
916 | |
0f5e1f75 |
917 | uc.ctrl = ctrl; |
918 | uc.privdata = NULL; |
919 | uc.privdata_needs_free = FALSE; |
920 | uc.buttons = NULL; |
921 | uc.entry = uc.list = uc.menu = uc.optmenu = uc.text = NULL; |
922 | |
d9b15094 |
923 | switch (ctrl->generic.type) { |
924 | case CTRL_COLUMNS: |
925 | { |
926 | static const int simplecols[1] = { 100 }; |
927 | columns_set_cols(cols, ctrl->columns.ncols, |
928 | (ctrl->columns.percentages ? |
929 | ctrl->columns.percentages : simplecols)); |
930 | } |
931 | continue; /* no actual control created */ |
932 | case CTRL_TABDELAY: |
0f5e1f75 |
933 | { |
934 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl); |
935 | if (uc) |
936 | columns_taborder_last(cols, uc->toplevel); |
937 | } |
d9b15094 |
938 | continue; /* no actual control created */ |
939 | case CTRL_BUTTON: |
940 | w = gtk_button_new_with_label(ctrl->generic.label); |
0f5e1f75 |
941 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
942 | GTK_SIGNAL_FUNC(button_clicked), dp); |
d9b15094 |
943 | break; |
944 | case CTRL_CHECKBOX: |
945 | w = gtk_check_button_new_with_label(ctrl->generic.label); |
0f5e1f75 |
946 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
947 | GTK_SIGNAL_FUNC(button_toggled), dp); |
d9b15094 |
948 | break; |
949 | case CTRL_RADIO: |
950 | /* |
951 | * Radio buttons get to go inside their own Columns, no |
952 | * matter what. |
953 | */ |
954 | { |
955 | gint i, *percentages; |
956 | GSList *group; |
957 | |
958 | w = columns_new(1); |
959 | if (ctrl->generic.label) { |
960 | GtkWidget *label = gtk_label_new(ctrl->generic.label); |
961 | columns_add(COLUMNS(w), label, 0, 1); |
962 | columns_force_left_align(COLUMNS(w), label); |
963 | gtk_widget_show(label); |
964 | } |
965 | percentages = g_new(gint, ctrl->radio.ncolumns); |
966 | for (i = 0; i < ctrl->radio.ncolumns; i++) { |
967 | percentages[i] = |
968 | ((100 * (i+1) / ctrl->radio.ncolumns) - |
969 | 100 * i / ctrl->radio.ncolumns); |
970 | } |
971 | columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns, |
972 | percentages); |
973 | g_free(percentages); |
974 | group = NULL; |
0f5e1f75 |
975 | |
976 | uc.nbuttons = ctrl->radio.nbuttons; |
977 | uc.buttons = smalloc(uc.nbuttons * sizeof(GtkWidget *)); |
978 | |
d9b15094 |
979 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
980 | GtkWidget *b; |
981 | gint colstart; |
982 | |
983 | b = (gtk_radio_button_new_with_label |
984 | (group, ctrl->radio.buttons[i])); |
0f5e1f75 |
985 | uc.buttons[i] = b; |
d9b15094 |
986 | group = gtk_radio_button_group(GTK_RADIO_BUTTON(b)); |
987 | colstart = i % ctrl->radio.ncolumns; |
988 | columns_add(COLUMNS(w), b, colstart, |
989 | (i == ctrl->radio.nbuttons-1 ? |
990 | ctrl->radio.ncolumns - colstart : 1)); |
991 | gtk_widget_show(b); |
0f5e1f75 |
992 | gtk_signal_connect(GTK_OBJECT(b), "toggled", |
993 | GTK_SIGNAL_FUNC(button_toggled), dp); |
d9b15094 |
994 | } |
995 | } |
996 | break; |
997 | case CTRL_EDITBOX: |
998 | if (ctrl->editbox.has_list) { |
999 | w = gtk_combo_new(); |
0f5e1f75 |
1000 | gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE); |
1001 | uc.entry = GTK_COMBO(w)->entry; |
1002 | uc.list = GTK_COMBO(w)->list; |
d9b15094 |
1003 | } else { |
1004 | w = gtk_entry_new(); |
1005 | if (ctrl->editbox.password) |
1006 | gtk_entry_set_visibility(GTK_ENTRY(w), FALSE); |
0f5e1f75 |
1007 | uc.entry = w; |
d9b15094 |
1008 | } |
0f5e1f75 |
1009 | gtk_signal_connect(GTK_OBJECT(uc.entry), "changed", |
1010 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
d9b15094 |
1011 | /* |
1012 | * Edit boxes, for some strange reason, have a minimum |
1013 | * width of 150 in GTK 1.2. We don't want this - we'd |
1014 | * rather the edit boxes acquired their natural width |
1015 | * from the column layout of the rest of the box. |
1016 | */ |
1017 | { |
1018 | GtkRequisition req; |
1019 | gtk_widget_size_request(w, &req); |
1020 | gtk_widget_set_usize(w, 10, req.height); |
1021 | } |
1022 | if (ctrl->generic.label) { |
1023 | GtkWidget *label, *container; |
1024 | |
1025 | label = gtk_label_new(ctrl->generic.label); |
1026 | |
1027 | container = columns_new(4); |
1028 | if (ctrl->editbox.percentwidth == 100) { |
1029 | columns_add(COLUMNS(container), label, 0, 1); |
1030 | columns_force_left_align(COLUMNS(container), label); |
1031 | columns_add(COLUMNS(container), w, 0, 1); |
1032 | } else { |
1033 | gint percentages[2]; |
1034 | percentages[1] = ctrl->editbox.percentwidth; |
1035 | percentages[0] = 100 - ctrl->editbox.percentwidth; |
1036 | columns_set_cols(COLUMNS(container), 2, percentages); |
1037 | columns_add(COLUMNS(container), label, 0, 1); |
1038 | columns_force_left_align(COLUMNS(container), label); |
1039 | columns_add(COLUMNS(container), w, 1, 1); |
1040 | } |
1041 | gtk_widget_show(label); |
1042 | gtk_widget_show(w); |
1043 | |
1044 | w = container; |
1045 | } |
0f5e1f75 |
1046 | gtk_signal_connect(GTK_OBJECT(uc.entry), "focus_out_event", |
1047 | GTK_SIGNAL_FUNC(editbox_lostfocus), dp); |
d9b15094 |
1048 | break; |
1d0d4a3b |
1049 | case CTRL_FILESELECT: |
1050 | case CTRL_FONTSELECT: |
1051 | { |
1052 | GtkWidget *ww; |
1053 | GtkRequisition req; |
1054 | char *browsebtn = |
1055 | (ctrl->generic.type == CTRL_FILESELECT ? |
1056 | "Browse..." : "Change..."); |
1057 | |
1058 | gint percentages[] = { 75, 25 }; |
1059 | w = columns_new(4); |
1060 | columns_set_cols(COLUMNS(w), 2, percentages); |
1061 | |
1062 | if (ctrl->generic.label) { |
1063 | ww = gtk_label_new(ctrl->generic.label); |
1064 | columns_add(COLUMNS(w), ww, 0, 2); |
1065 | columns_force_left_align(COLUMNS(w), ww); |
1066 | gtk_widget_show(ww); |
1067 | } |
1068 | |
0f5e1f75 |
1069 | uc.entry = ww = gtk_entry_new(); |
1d0d4a3b |
1070 | gtk_widget_size_request(ww, &req); |
1071 | gtk_widget_set_usize(ww, 10, req.height); |
1072 | columns_add(COLUMNS(w), ww, 0, 1); |
1073 | gtk_widget_show(ww); |
1074 | |
1075 | ww = gtk_button_new_with_label(browsebtn); |
1076 | columns_add(COLUMNS(w), ww, 1, 1); |
1077 | gtk_widget_show(ww); |
0f5e1f75 |
1078 | |
1079 | gtk_signal_connect(GTK_OBJECT(uc.entry), "changed", |
1080 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
1081 | gtk_signal_connect(GTK_OBJECT(ww), "clicked", |
1082 | GTK_SIGNAL_FUNC(filefont_clicked), dp); |
1d0d4a3b |
1083 | } |
1084 | break; |
1085 | case CTRL_LISTBOX: |
1086 | if (ctrl->listbox.height == 0) { |
0f5e1f75 |
1087 | uc.optmenu = w = gtk_option_menu_new(); |
1088 | uc.menu = gtk_menu_new(); |
1089 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc.menu); |
1090 | gtk_object_set_data(GTK_OBJECT(uc.menu), "user-data", |
1091 | (gpointer)uc.optmenu); |
1d0d4a3b |
1092 | } else { |
0f5e1f75 |
1093 | uc.list = gtk_list_new(); |
1094 | gtk_list_set_selection_mode(GTK_LIST(uc.list), |
1d0d4a3b |
1095 | (ctrl->listbox.multisel ? |
1096 | GTK_SELECTION_MULTIPLE : |
1097 | GTK_SELECTION_SINGLE)); |
1098 | w = gtk_scrolled_window_new(NULL, NULL); |
1099 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w), |
0f5e1f75 |
1100 | uc.list); |
1d0d4a3b |
1101 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w), |
1102 | GTK_POLICY_NEVER, |
1103 | GTK_POLICY_AUTOMATIC); |
0f5e1f75 |
1104 | gtk_widget_show(uc.list); |
1105 | gtk_signal_connect(GTK_OBJECT(uc.list), "selection-changed", |
1106 | GTK_SIGNAL_FUNC(list_selchange), dp); |
1d0d4a3b |
1107 | |
1108 | /* |
1109 | * Adjust the height of the scrolled window to the |
1110 | * minimum given by the height parameter. |
1111 | * |
1112 | * This piece of guesswork is a horrid hack based |
1113 | * on looking inside the GTK 1.2 sources |
1114 | * (specifically gtkviewport.c, which appears to be |
1115 | * the widget which provides the border around the |
1116 | * scrolling area). Anyone lets me know how I can |
1117 | * do this in a way which isn't at risk from GTK |
1118 | * upgrades, I'd be grateful. |
1119 | */ |
1120 | { |
0f5e1f75 |
1121 | int edge = GTK_WIDGET(uc.list)->style->klass->ythickness; |
1d0d4a3b |
1122 | gtk_widget_set_usize(w, 10, |
1123 | 2*edge + (ctrl->listbox.height * |
1124 | listitemheight)); |
1125 | } |
1d0d4a3b |
1126 | |
1127 | if (ctrl->listbox.draglist) { |
1128 | /* |
1129 | * GTK doesn't appear to make it easy to |
1130 | * implement a proper draggable list; so |
1131 | * instead I'm just going to have to put an Up |
1132 | * and a Down button to the right of the actual |
1133 | * list box. Ah well. |
1134 | */ |
1135 | GtkWidget *cols, *button; |
1136 | static const gint percentages[2] = { 80, 20 }; |
1137 | |
1138 | cols = columns_new(4); |
1139 | columns_set_cols(COLUMNS(cols), 2, percentages); |
1140 | columns_add(COLUMNS(cols), w, 0, 1); |
1141 | gtk_widget_show(w); |
1142 | button = gtk_button_new_with_label("Up"); |
1143 | columns_add(COLUMNS(cols), button, 1, 1); |
1144 | gtk_widget_show(button); |
0f5e1f75 |
1145 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1146 | GTK_SIGNAL_FUNC(draglist_up), dp); |
1d0d4a3b |
1147 | button = gtk_button_new_with_label("Down"); |
1148 | columns_add(COLUMNS(cols), button, 1, 1); |
1149 | gtk_widget_show(button); |
0f5e1f75 |
1150 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1151 | GTK_SIGNAL_FUNC(draglist_down), dp); |
1d0d4a3b |
1152 | |
1153 | w = cols; |
1154 | } |
1155 | |
1156 | } |
1157 | if (ctrl->generic.label) { |
1158 | GtkWidget *label, *container; |
1159 | |
1160 | label = gtk_label_new(ctrl->generic.label); |
1161 | |
1162 | container = columns_new(4); |
1163 | if (ctrl->listbox.percentwidth == 100) { |
1164 | columns_add(COLUMNS(container), label, 0, 1); |
1165 | columns_force_left_align(COLUMNS(container), label); |
1166 | columns_add(COLUMNS(container), w, 0, 1); |
1167 | } else { |
1168 | gint percentages[2]; |
1169 | percentages[1] = ctrl->listbox.percentwidth; |
1170 | percentages[0] = 100 - ctrl->listbox.percentwidth; |
1171 | columns_set_cols(COLUMNS(container), 2, percentages); |
1172 | columns_add(COLUMNS(container), label, 0, 1); |
1173 | columns_force_left_align(COLUMNS(container), label); |
1174 | columns_add(COLUMNS(container), w, 1, 1); |
1175 | } |
1176 | gtk_widget_show(label); |
1177 | gtk_widget_show(w); |
1178 | |
1179 | w = container; |
1180 | } |
1181 | break; |
d9b15094 |
1182 | case CTRL_TEXT: |
0f5e1f75 |
1183 | uc.text = w = gtk_label_new(ctrl->generic.label); |
d9b15094 |
1184 | gtk_label_set_line_wrap(GTK_LABEL(w), TRUE); |
1d0d4a3b |
1185 | /* FIXME: deal with wrapping! */ |
d9b15094 |
1186 | break; |
1187 | } |
1188 | if (w) { |
0f5e1f75 |
1189 | struct uctrl *ucptr; |
1190 | |
d9b15094 |
1191 | columns_add(cols, w, |
1192 | COLUMN_START(ctrl->generic.column), |
1193 | COLUMN_SPAN(ctrl->generic.column)); |
1194 | gtk_widget_show(w); |
0f5e1f75 |
1195 | |
1196 | ucptr = smalloc(sizeof(struct uctrl)); |
1197 | *ucptr = uc; /* structure copy */ |
1198 | ucptr->toplevel = w; |
1199 | dlg_add_uctrl(dp, ucptr); |
d9b15094 |
1200 | } |
1201 | } |
1202 | |
1203 | return ret; |
1204 | } |
1205 | |
1206 | struct selparam { |
1207 | Panels *panels; |
1208 | GtkWidget *panel, *treeitem; |
1209 | }; |
1210 | |
1211 | static void treeitem_sel(GtkItem *item, gpointer data) |
1212 | { |
1213 | struct selparam *sp = (struct selparam *)data; |
1214 | |
1215 | panels_switch_to(sp->panels, sp->panel); |
1216 | } |
1217 | |
1218 | void destroy(GtkWidget *widget, gpointer data) |
1219 | { |
1220 | gtk_main_quit(); |
1221 | } |
1222 | |
1223 | void do_config_box(void) |
1224 | { |
1225 | GtkWidget *window, *hbox, *vbox, *cols, *label, |
1226 | *tree, *treescroll, *panels, *panelvbox; |
1d0d4a3b |
1227 | int index, level, listitemheight; |
d9b15094 |
1228 | struct controlbox *ctrlbox; |
1229 | char *path; |
1230 | GtkTreeItem *treeitemlevels[8]; |
1231 | GtkTree *treelevels[8]; |
1232 | Config cfg; |
0f5e1f75 |
1233 | struct dlgparam dp; |
1234 | struct sesslist sl; |
d9b15094 |
1235 | |
1236 | struct selparam *selparams = NULL; |
1237 | int nselparams = 0, selparamsize = 0; |
1238 | |
1239 | do_defaults(NULL, &cfg); |
1240 | |
0f5e1f75 |
1241 | dlg_init(&dp); |
1242 | |
1d0d4a3b |
1243 | { |
1244 | GtkWidget *listitem = gtk_list_item_new_with_label("foo"); |
1245 | GtkRequisition req; |
1246 | gtk_widget_size_request(listitem, &req); |
1247 | listitemheight = req.height; |
1248 | gtk_widget_unref(listitem); |
1249 | } |
1250 | |
0f5e1f75 |
1251 | sl.nsessions = 0; |
1252 | |
d9b15094 |
1253 | ctrlbox = ctrl_new_box(); |
0f5e1f75 |
1254 | setup_config_box(ctrlbox, &sl, FALSE, 0); |
1d0d4a3b |
1255 | unix_setup_config_box(ctrlbox, FALSE); |
d9b15094 |
1256 | |
1257 | window = gtk_dialog_new(); |
d9b15094 |
1258 | hbox = gtk_hbox_new(FALSE, 4); |
1259 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0); |
1d0d4a3b |
1260 | gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
d9b15094 |
1261 | gtk_widget_show(hbox); |
1262 | vbox = gtk_vbox_new(FALSE, 4); |
1263 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
1264 | gtk_widget_show(vbox); |
1265 | cols = columns_new(4); |
1266 | gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0); |
1267 | gtk_widget_show(cols); |
1268 | label = gtk_label_new("Category:"); |
1269 | columns_add(COLUMNS(cols), label, 0, 1); |
1270 | columns_force_left_align(COLUMNS(cols), label); |
1271 | gtk_widget_show(label); |
1272 | treescroll = gtk_scrolled_window_new(NULL, NULL); |
1273 | tree = gtk_tree_new(); |
1274 | gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM); |
1d0d4a3b |
1275 | gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE); |
d9b15094 |
1276 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll), |
1277 | tree); |
1278 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll), |
1279 | GTK_POLICY_NEVER, |
1280 | GTK_POLICY_AUTOMATIC); |
1281 | gtk_widget_show(tree); |
1282 | gtk_widget_show(treescroll); |
1283 | gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0); |
1284 | panels = panels_new(); |
1285 | gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0); |
1286 | gtk_widget_show(panels); |
1287 | |
1288 | panelvbox = NULL; |
1289 | path = NULL; |
1290 | level = 0; |
1291 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
1292 | struct controlset *s = ctrlbox->ctrlsets[index]; |
0f5e1f75 |
1293 | GtkWidget *w = layout_ctrls(&dp, s, listitemheight); |
d9b15094 |
1294 | |
1295 | if (!*s->pathname) { |
1296 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
1297 | w, TRUE, TRUE, 0); |
1298 | } else { |
1299 | int j = path ? ctrl_path_compare(s->pathname, path) : 0; |
1300 | if (j != INT_MAX) { /* add to treeview, start new panel */ |
1301 | char *c; |
1302 | GtkWidget *treeitem; |
1303 | int first; |
1304 | |
1305 | /* |
1306 | * We expect never to find an implicit path |
1307 | * component. For example, we expect never to see |
1308 | * A/B/C followed by A/D/E, because that would |
1309 | * _implicitly_ create A/D. All our path prefixes |
1310 | * are expected to contain actual controls and be |
1311 | * selectable in the treeview; so we would expect |
1312 | * to see A/D _explicitly_ before encountering |
1313 | * A/D/E. |
1314 | */ |
1315 | assert(j == ctrl_path_elements(s->pathname) - 1); |
1316 | |
1317 | c = strrchr(s->pathname, '/'); |
1318 | if (!c) |
1319 | c = s->pathname; |
1320 | else |
1321 | c++; |
1322 | |
1323 | treeitem = gtk_tree_item_new_with_label(c); |
1324 | assert(j-1 < level); |
1325 | if (j > 0) { |
1326 | if (!treelevels[j-1]) { |
1327 | treelevels[j-1] = GTK_TREE(gtk_tree_new()); |
1328 | gtk_tree_item_set_subtree |
1329 | (treeitemlevels[j-1], |
1330 | GTK_WIDGET(treelevels[j-1])); |
1331 | gtk_tree_item_expand(treeitemlevels[j-1]); |
1332 | } |
1333 | gtk_tree_append(treelevels[j-1], treeitem); |
1334 | } else { |
1335 | gtk_tree_append(GTK_TREE(tree), treeitem); |
1336 | } |
1337 | treeitemlevels[j] = GTK_TREE_ITEM(treeitem); |
1338 | treelevels[j] = NULL; |
1339 | level = j+1; |
1340 | |
1341 | gtk_widget_show(treeitem); |
1342 | |
1343 | path = s->pathname; |
1344 | |
1345 | first = (panelvbox == NULL); |
1346 | |
1347 | panelvbox = gtk_vbox_new(FALSE, 4); |
1348 | gtk_container_add(GTK_CONTAINER(panels), panelvbox); |
1349 | if (first) { |
1350 | panels_switch_to(PANELS(panels), panelvbox); |
1351 | gtk_tree_select_child(GTK_TREE(tree), treeitem); |
1352 | } |
1353 | |
1354 | if (nselparams >= selparamsize) { |
1355 | selparamsize += 16; |
1356 | selparams = srealloc(selparams, |
1357 | selparamsize * sizeof(*selparams)); |
1358 | } |
1359 | selparams[nselparams].panels = PANELS(panels); |
1360 | selparams[nselparams].panel = panelvbox; |
1361 | selparams[nselparams].treeitem = treeitem; |
1362 | nselparams++; |
1363 | |
1364 | } |
1365 | |
1366 | gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0); |
1d0d4a3b |
1367 | gtk_widget_show(w); |
d9b15094 |
1368 | } |
1369 | } |
1370 | |
1371 | for (index = 0; index < nselparams; index++) { |
1372 | gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select", |
1373 | GTK_SIGNAL_FUNC(treeitem_sel), |
1374 | &selparams[index]); |
1375 | } |
1376 | |
0f5e1f75 |
1377 | dp.data = &cfg; |
1378 | dlg_refresh(NULL, &dp); |
1379 | |
d9b15094 |
1380 | gtk_widget_show(window); |
1381 | |
1382 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
1383 | GTK_SIGNAL_FUNC(destroy), NULL); |
1384 | |
1385 | gtk_main(); |
1386 | |
0f5e1f75 |
1387 | dlg_cleanup(&dp); |
d9b15094 |
1388 | sfree(selparams); |
1389 | } |
1390 | |
1391 | /* ====================================================================== |
1392 | * Below here is a stub main program which allows the dialog box |
1393 | * code to be compiled and tested with a minimal amount of the rest |
1394 | * of PuTTY. |
1395 | */ |
1396 | |
1397 | #ifdef TESTMODE |
1398 | |
1399 | /* Compile command for testing: |
1400 | |
1d0d4a3b |
1401 | gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \ |
1402 | ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \ |
d9b15094 |
1403 | -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs` |
1404 | */ |
1405 | |
1406 | void modalfatalbox(char *p, ...) |
1407 | { |
1408 | va_list ap; |
1409 | fprintf(stderr, "FATAL ERROR: "); |
1410 | va_start(ap, p); |
1411 | vfprintf(stderr, p, ap); |
1412 | va_end(ap); |
1413 | fputc('\n', stderr); |
1414 | exit(1); |
1415 | } |
1416 | |
1417 | char *cp_name(int codepage) |
1418 | { |
1419 | return (codepage == 123 ? "testing123" : |
1420 | codepage == 234 ? "testing234" : |
1421 | codepage == 345 ? "testing345" : |
1422 | "unknown"); |
1423 | } |
1424 | |
1425 | char *cp_enumerate(int index) |
1426 | { |
1427 | return (index == 0 ? "testing123" : |
1428 | index == 1 ? "testing234" : |
1429 | NULL); |
1430 | } |
1431 | |
1432 | int decode_codepage(char *cp_name) |
1433 | { |
1434 | return (!strcmp(cp_name, "testing123") ? 123 : |
1435 | !strcmp(cp_name, "testing234") ? 234 : |
1436 | !strcmp(cp_name, "testing345") ? 345 : |
1437 | -2); |
1438 | } |
1439 | |
1440 | struct printer_enum_tag { int dummy; } printer_test; |
1441 | |
1442 | printer_enum *printer_start_enum(int *nprinters_ptr) { |
1443 | *nprinters_ptr = 2; |
1444 | return &printer_test; |
1445 | } |
1446 | char *printer_get_name(printer_enum *pe, int i) { |
1447 | return (i==0 ? "lpr" : i==1 ? "lpr -Pfoobar" : NULL); |
1448 | } |
1449 | void printer_finish_enum(printer_enum *pe) { } |
1450 | |
1451 | char *platform_default_s(const char *name) |
1452 | { |
1453 | return NULL; |
1454 | } |
1455 | |
1456 | int platform_default_i(const char *name, int def) |
1457 | { |
1458 | return def; |
1459 | } |
1460 | |
1461 | FontSpec platform_default_fontspec(const char *name) |
1462 | { |
1463 | FontSpec ret; |
0f5e1f75 |
1464 | if (!strcmp(name, "Font")) |
1465 | strcpy(ret.name, "fixed"); |
1466 | else |
1467 | *ret.name = '\0'; |
d9b15094 |
1468 | return ret; |
1469 | } |
1470 | |
1471 | Filename platform_default_filename(const char *name) |
1472 | { |
1473 | Filename ret; |
0f5e1f75 |
1474 | if (!strcmp(name, "LogFileName")) |
1475 | strcpy(ret.path, "putty.log"); |
1476 | else |
1477 | *ret.path = '\0'; |
d9b15094 |
1478 | return ret; |
1479 | } |
1480 | |
1481 | char *x_get_default(const char *key) |
1482 | { |
1483 | return NULL; |
1484 | } |
1485 | |
1486 | int main(int argc, char **argv) |
1487 | { |
1488 | gtk_init(&argc, &argv); |
1489 | do_config_box(); |
1490 | return 0; |
1491 | } |
1492 | |
1493 | #endif |