d9b15094 |
1 | /* |
2 | * gtkdlg.c - GTK implementation of the PuTTY configuration box. |
3 | */ |
4 | |
1d0d4a3b |
5 | /* |
1c291fcf |
6 | * TODO when porting to GTK 2.0: |
7 | * |
8 | * - GtkTree is apparently deprecated and we should switch to |
9 | * GtkTreeView instead. |
10 | * - GtkLabel has a built-in mnemonic scheme, so we should at |
11 | * least consider switching to that from the current adhockery. |
1d0d4a3b |
12 | */ |
13 | |
d9b15094 |
14 | #include <assert.h> |
5bf9955d |
15 | #include <stdarg.h> |
1c291fcf |
16 | #include <ctype.h> |
8eed910d |
17 | #include <time.h> |
d9b15094 |
18 | #include <gtk/gtk.h> |
1c291fcf |
19 | #include <gdk/gdkkeysyms.h> |
0bd8d76d |
20 | #include <gdk/gdkx.h> |
21 | #include <X11/Xlib.h> |
22 | #include <X11/Xutil.h> |
d9b15094 |
23 | |
24 | #include "gtkcols.h" |
d9b15094 |
25 | |
26 | #ifdef TESTMODE |
27 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
28 | #endif |
29 | |
30 | #include "putty.h" |
5bf9955d |
31 | #include "storage.h" |
d9b15094 |
32 | #include "dialog.h" |
0f5e1f75 |
33 | #include "tree234.h" |
34 | |
1c291fcf |
35 | struct Shortcut { |
36 | GtkWidget *widget; |
37 | struct uctrl *uc; |
38 | int action; |
39 | }; |
40 | |
41 | struct Shortcuts { |
42 | struct Shortcut sc[128]; |
43 | }; |
44 | |
0f5e1f75 |
45 | struct uctrl { |
46 | union control *ctrl; |
47 | GtkWidget *toplevel; |
48 | void *privdata; |
49 | int privdata_needs_free; |
50 | GtkWidget **buttons; int nbuttons; /* for radio buttons */ |
51 | GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */ |
1c291fcf |
52 | GtkWidget *button; /* for filesel, fontsel */ |
0f5e1f75 |
53 | GtkWidget *list; /* for combobox, listbox */ |
54 | GtkWidget *menu; /* for optionmenu (==droplist) */ |
55 | GtkWidget *optmenu; /* also for optionmenu */ |
56 | GtkWidget *text; /* for text */ |
aef05b78 |
57 | GtkWidget *label; /* for dlg_label_change */ |
0bd8d76d |
58 | GtkAdjustment *adj; /* for the scrollbar in a list box */ |
c5d64b49 |
59 | guint textsig; |
b4ac18ae |
60 | int nclicks; |
0f5e1f75 |
61 | }; |
62 | |
63 | struct dlgparam { |
64 | tree234 *byctrl, *bywidget; |
65 | void *data; |
66 | struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */ |
67 | /* `flags' are set to indicate when a GTK signal handler is being called |
68 | * due to automatic processing and should not flag a user event. */ |
69 | int flags; |
1c291fcf |
70 | struct Shortcuts *shortcuts; |
23897ed0 |
71 | GtkWidget *window, *cancelbutton, *currtreeitem, **treeitems; |
0bd8d76d |
72 | union control *currfocus, *lastfocus; |
73 | int ntreeitems; |
74 | int retval; |
0f5e1f75 |
75 | }; |
76 | #define FLAG_UPDATING_COMBO_LIST 1 |
77 | |
1c291fcf |
78 | enum { /* values for Shortcut.action */ |
79 | SHORTCUT_EMPTY, /* no shortcut on this key */ |
c5d64b49 |
80 | SHORTCUT_TREE, /* focus a tree item */ |
1c291fcf |
81 | SHORTCUT_FOCUS, /* focus the supplied widget */ |
82 | SHORTCUT_UCTRL, /* do something sane with uctrl */ |
83 | SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */ |
84 | SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */ |
85 | }; |
86 | |
0f5e1f75 |
87 | /* |
88 | * Forward references. |
89 | */ |
0bd8d76d |
90 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
91 | gpointer data); |
1c291fcf |
92 | static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
93 | int chr, int action, void *ptr); |
aef05b78 |
94 | static void shortcut_highlight(GtkWidget *label, int chr); |
0bd8d76d |
95 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
96 | gpointer data); |
97 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
98 | gpointer data); |
b4ac18ae |
99 | static int listitem_button_press(GtkWidget *item, GdkEventButton *event, |
100 | gpointer data); |
101 | static int listitem_button_release(GtkWidget *item, GdkEventButton *event, |
102 | gpointer data); |
0f5e1f75 |
103 | static void menuitem_activate(GtkMenuItem *item, gpointer data); |
104 | static void coloursel_ok(GtkButton *button, gpointer data); |
105 | static void coloursel_cancel(GtkButton *button, gpointer data); |
0bd8d76d |
106 | static void window_destroy(GtkWidget *widget, gpointer data); |
0f5e1f75 |
107 | |
108 | static int uctrl_cmp_byctrl(void *av, void *bv) |
109 | { |
110 | struct uctrl *a = (struct uctrl *)av; |
111 | struct uctrl *b = (struct uctrl *)bv; |
112 | if (a->ctrl < b->ctrl) |
113 | return -1; |
114 | else if (a->ctrl > b->ctrl) |
115 | return +1; |
116 | return 0; |
117 | } |
118 | |
119 | static int uctrl_cmp_byctrl_find(void *av, void *bv) |
120 | { |
121 | union control *a = (union control *)av; |
122 | struct uctrl *b = (struct uctrl *)bv; |
123 | if (a < b->ctrl) |
124 | return -1; |
125 | else if (a > b->ctrl) |
126 | return +1; |
127 | return 0; |
128 | } |
129 | |
130 | static int uctrl_cmp_bywidget(void *av, void *bv) |
131 | { |
132 | struct uctrl *a = (struct uctrl *)av; |
133 | struct uctrl *b = (struct uctrl *)bv; |
134 | if (a->toplevel < b->toplevel) |
135 | return -1; |
136 | else if (a->toplevel > b->toplevel) |
137 | return +1; |
138 | return 0; |
139 | } |
140 | |
141 | static int uctrl_cmp_bywidget_find(void *av, void *bv) |
142 | { |
143 | GtkWidget *a = (GtkWidget *)av; |
144 | struct uctrl *b = (struct uctrl *)bv; |
145 | if (a < b->toplevel) |
146 | return -1; |
147 | else if (a > b->toplevel) |
148 | return +1; |
149 | return 0; |
150 | } |
151 | |
152 | static void dlg_init(struct dlgparam *dp) |
153 | { |
154 | dp->byctrl = newtree234(uctrl_cmp_byctrl); |
155 | dp->bywidget = newtree234(uctrl_cmp_bywidget); |
156 | dp->coloursel_result.ok = FALSE; |
5bf9955d |
157 | dp->treeitems = NULL; |
158 | dp->window = dp->cancelbutton = dp->currtreeitem = NULL; |
c989dbc4 |
159 | dp->flags = 0; |
160 | dp->currfocus = NULL; |
0f5e1f75 |
161 | } |
162 | |
163 | static void dlg_cleanup(struct dlgparam *dp) |
164 | { |
165 | struct uctrl *uc; |
166 | |
167 | freetree234(dp->byctrl); /* doesn't free the uctrls inside */ |
8eed910d |
168 | dp->byctrl = NULL; |
0f5e1f75 |
169 | while ( (uc = index234(dp->bywidget, 0)) != NULL) { |
170 | del234(dp->bywidget, uc); |
171 | if (uc->privdata_needs_free) |
172 | sfree(uc->privdata); |
173 | sfree(uc->buttons); |
174 | sfree(uc); |
175 | } |
176 | freetree234(dp->bywidget); |
8eed910d |
177 | dp->bywidget = NULL; |
0bd8d76d |
178 | sfree(dp->treeitems); |
0f5e1f75 |
179 | } |
180 | |
181 | static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc) |
182 | { |
183 | add234(dp->byctrl, uc); |
184 | add234(dp->bywidget, uc); |
185 | } |
186 | |
187 | static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl) |
188 | { |
8eed910d |
189 | if (!dp->byctrl) |
190 | return NULL; |
0f5e1f75 |
191 | return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find); |
192 | } |
193 | |
194 | static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w) |
195 | { |
196 | struct uctrl *ret = NULL; |
8eed910d |
197 | if (!dp->bywidget) |
198 | return NULL; |
0f5e1f75 |
199 | do { |
200 | ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find); |
201 | if (ret) |
202 | return ret; |
203 | w = w->parent; |
204 | } while (w); |
205 | return ret; |
206 | } |
d9b15094 |
207 | |
d9b15094 |
208 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
209 | { |
0f5e1f75 |
210 | struct dlgparam *dp = (struct dlgparam *)dlg; |
211 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
212 | return uc->privdata; |
d9b15094 |
213 | } |
214 | |
215 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
216 | { |
0f5e1f75 |
217 | struct dlgparam *dp = (struct dlgparam *)dlg; |
218 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
219 | uc->privdata = ptr; |
220 | uc->privdata_needs_free = FALSE; |
d9b15094 |
221 | } |
222 | |
223 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
224 | { |
0f5e1f75 |
225 | struct dlgparam *dp = (struct dlgparam *)dlg; |
226 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
3d88e64d |
227 | /* |
228 | * This is an internal allocation routine, so it's allowed to |
229 | * use smalloc directly. |
230 | */ |
0f5e1f75 |
231 | uc->privdata = smalloc(size); |
232 | uc->privdata_needs_free = FALSE; |
233 | return uc->privdata; |
d9b15094 |
234 | } |
235 | |
0bd8d76d |
236 | union control *dlg_last_focused(union control *ctrl, void *dlg) |
d9b15094 |
237 | { |
0f5e1f75 |
238 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
239 | if (dp->currfocus != ctrl) |
240 | return dp->currfocus; |
241 | else |
242 | return dp->lastfocus; |
d9b15094 |
243 | } |
244 | |
0f5e1f75 |
245 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int which) |
d9b15094 |
246 | { |
0f5e1f75 |
247 | struct dlgparam *dp = (struct dlgparam *)dlg; |
248 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
249 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
250 | assert(uc->buttons != NULL); |
251 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE); |
d9b15094 |
252 | } |
253 | |
254 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
255 | { |
0f5e1f75 |
256 | struct dlgparam *dp = (struct dlgparam *)dlg; |
257 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
258 | int i; |
259 | |
260 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
261 | assert(uc->buttons != NULL); |
262 | for (i = 0; i < uc->nbuttons; i++) |
263 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i]))) |
264 | return i; |
265 | return 0; /* got to return something */ |
d9b15094 |
266 | } |
267 | |
268 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
269 | { |
0f5e1f75 |
270 | struct dlgparam *dp = (struct dlgparam *)dlg; |
271 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
272 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
273 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked); |
d9b15094 |
274 | } |
275 | |
276 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
277 | { |
0f5e1f75 |
278 | struct dlgparam *dp = (struct dlgparam *)dlg; |
279 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
280 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
281 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel)); |
d9b15094 |
282 | } |
283 | |
284 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
285 | { |
0f5e1f75 |
286 | struct dlgparam *dp = (struct dlgparam *)dlg; |
287 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
288 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
289 | assert(uc->entry != NULL); |
290 | gtk_entry_set_text(GTK_ENTRY(uc->entry), text); |
d9b15094 |
291 | } |
292 | |
293 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
294 | { |
0f5e1f75 |
295 | struct dlgparam *dp = (struct dlgparam *)dlg; |
296 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
297 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
298 | assert(uc->entry != NULL); |
299 | strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
300 | length); |
301 | buffer[length-1] = '\0'; |
302 | } |
303 | |
304 | static void container_remove_and_destroy(GtkWidget *w, gpointer data) |
305 | { |
306 | GtkContainer *cont = GTK_CONTAINER(data); |
307 | /* gtk_container_remove will unref the widget for us; we need not. */ |
308 | gtk_container_remove(cont, w); |
d9b15094 |
309 | } |
310 | |
311 | /* The `listbox' functions can also apply to combo boxes. */ |
312 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
313 | { |
0f5e1f75 |
314 | struct dlgparam *dp = (struct dlgparam *)dlg; |
315 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
0f5e1f75 |
316 | |
317 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
318 | uc->ctrl->generic.type == CTRL_LISTBOX); |
319 | assert(uc->menu != NULL || uc->list != NULL); |
320 | |
b4c61ce2 |
321 | if (uc->menu) { |
322 | gtk_container_foreach(GTK_CONTAINER(uc->menu), |
323 | container_remove_and_destroy, |
324 | GTK_CONTAINER(uc->menu)); |
325 | } else { |
326 | gtk_list_clear_items(GTK_LIST(uc->list), 0, -1); |
327 | } |
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 | { |
4eaff8d4 |
350 | dlg_listbox_addwithid(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 | */ |
4eaff8d4 |
360 | void dlg_listbox_addwithid(union control *ctrl, void *dlg, |
361 | char const *text, int id) |
d9b15094 |
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 | |
f7f9fb5c |
383 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
384 | GINT_TO_POINTER(id)); |
0f5e1f75 |
385 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
386 | GTK_SIGNAL_FUNC(menuitem_activate), dp); |
387 | } else if (!uc->entry) { |
388 | /* |
389 | * List item in a non-combo-box list box. We make all of |
390 | * these Columns containing GtkLabels. This allows us to do |
391 | * the nasty force_left hack irrespective of whether there |
392 | * are tabs in the thing. |
393 | */ |
394 | GtkWidget *listitem = gtk_list_item_new(); |
523fd1da |
395 | GtkWidget *cols = columns_new(10); |
0f5e1f75 |
396 | gint *percents; |
397 | int i, ncols; |
398 | |
399 | /* Count the tabs in the text, and hence determine # of columns. */ |
400 | ncols = 1; |
401 | for (i = 0; text[i]; i++) |
402 | if (text[i] == '\t') |
403 | ncols++; |
404 | |
405 | assert(ncols <= |
406 | (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1)); |
3d88e64d |
407 | percents = snewn(ncols, gint); |
0f5e1f75 |
408 | percents[ncols-1] = 100; |
409 | for (i = 0; i < ncols-1; i++) { |
410 | percents[i] = uc->ctrl->listbox.percentages[i]; |
411 | percents[ncols-1] -= percents[i]; |
412 | } |
413 | columns_set_cols(COLUMNS(cols), ncols, percents); |
414 | sfree(percents); |
415 | |
416 | for (i = 0; i < ncols; i++) { |
417 | int len = strcspn(text, "\t"); |
418 | char *dup = dupprintf("%.*s", len, text); |
419 | GtkWidget *label; |
420 | |
421 | text += len; |
422 | if (*text) text++; |
423 | label = gtk_label_new(dup); |
424 | sfree(dup); |
425 | |
426 | columns_add(COLUMNS(cols), label, i, 1); |
427 | columns_force_left_align(COLUMNS(cols), label); |
428 | gtk_widget_show(label); |
429 | } |
430 | gtk_container_add(GTK_CONTAINER(listitem), cols); |
431 | gtk_widget_show(cols); |
432 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
433 | gtk_widget_show(listitem); |
434 | |
0bd8d76d |
435 | if (ctrl->listbox.multisel) { |
436 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
437 | GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj); |
438 | } else { |
439 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
440 | GTK_SIGNAL_FUNC(listitem_single_key), uc->adj); |
441 | } |
442 | gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event", |
443 | GTK_SIGNAL_FUNC(widget_focus), dp); |
0f5e1f75 |
444 | gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event", |
b4ac18ae |
445 | GTK_SIGNAL_FUNC(listitem_button_press), dp); |
446 | gtk_signal_connect(GTK_OBJECT(listitem), "button_release_event", |
447 | GTK_SIGNAL_FUNC(listitem_button_release), dp); |
f7f9fb5c |
448 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", |
449 | GINT_TO_POINTER(id)); |
0f5e1f75 |
450 | } else { |
451 | /* |
452 | * List item in a combo-box list, which means the sensible |
453 | * thing to do is make it a perfectly normal label. Hence |
454 | * tabs are disregarded. |
455 | */ |
456 | GtkWidget *listitem = gtk_list_item_new_with_label(text); |
457 | |
458 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
459 | gtk_widget_show(listitem); |
460 | |
f7f9fb5c |
461 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", |
462 | GINT_TO_POINTER(id)); |
0f5e1f75 |
463 | } |
464 | |
465 | dp->flags &= ~FLAG_UPDATING_COMBO_LIST; |
d9b15094 |
466 | } |
467 | |
468 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
469 | { |
0f5e1f75 |
470 | struct dlgparam *dp = (struct dlgparam *)dlg; |
471 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
472 | GList *children; |
473 | GtkObject *item; |
474 | |
475 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
476 | uc->ctrl->generic.type == CTRL_LISTBOX); |
477 | assert(uc->menu != NULL || uc->list != NULL); |
478 | |
479 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
480 | uc->list)); |
481 | item = GTK_OBJECT(g_list_nth_data(children, index)); |
0bd8d76d |
482 | g_list_free(children); |
0f5e1f75 |
483 | |
f7f9fb5c |
484 | return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "user-data")); |
d9b15094 |
485 | } |
486 | |
487 | /* dlg_listbox_index returns <0 if no single element is selected. */ |
488 | int dlg_listbox_index(union control *ctrl, void *dlg) |
489 | { |
0f5e1f75 |
490 | struct dlgparam *dp = (struct dlgparam *)dlg; |
491 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
492 | GList *children; |
493 | GtkWidget *item, *activeitem; |
494 | int i; |
495 | int selected = -1; |
496 | |
497 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
498 | uc->ctrl->generic.type == CTRL_LISTBOX); |
499 | assert(uc->menu != NULL || uc->list != NULL); |
500 | |
501 | if (uc->menu) |
502 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
7ffdbc1a |
503 | else |
504 | activeitem = NULL; /* unnecessarily placate gcc */ |
0f5e1f75 |
505 | |
506 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
507 | uc->list)); |
508 | for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL; |
509 | i++, children = children->next) { |
510 | if (uc->menu ? activeitem == item : |
511 | GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) { |
0bd8d76d |
512 | if (selected == -1) |
0f5e1f75 |
513 | selected = i; |
514 | else |
0bd8d76d |
515 | selected = -2; |
0f5e1f75 |
516 | } |
517 | } |
0bd8d76d |
518 | g_list_free(children); |
519 | return selected < 0 ? -1 : selected; |
d9b15094 |
520 | } |
521 | |
522 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
523 | { |
0f5e1f75 |
524 | struct dlgparam *dp = (struct dlgparam *)dlg; |
525 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
526 | GList *children; |
527 | GtkWidget *item, *activeitem; |
528 | |
529 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
530 | uc->ctrl->generic.type == CTRL_LISTBOX); |
531 | assert(uc->menu != NULL || uc->list != NULL); |
532 | |
533 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
534 | uc->list)); |
535 | item = GTK_WIDGET(g_list_nth_data(children, index)); |
0bd8d76d |
536 | g_list_free(children); |
0f5e1f75 |
537 | |
538 | if (uc->menu) { |
539 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
540 | return item == activeitem; |
541 | } else { |
542 | return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED; |
543 | } |
d9b15094 |
544 | } |
545 | |
546 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
547 | { |
0f5e1f75 |
548 | struct dlgparam *dp = (struct dlgparam *)dlg; |
549 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
550 | |
551 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
552 | uc->ctrl->generic.type == CTRL_LISTBOX); |
553 | assert(uc->optmenu != NULL || uc->list != NULL); |
554 | |
555 | if (uc->optmenu) { |
556 | gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index); |
557 | } else { |
4d352164 |
558 | int nitems; |
559 | GList *items; |
560 | gdouble newtop, newbot; |
561 | |
0f5e1f75 |
562 | gtk_list_select_item(GTK_LIST(uc->list), index); |
4d352164 |
563 | |
564 | /* |
565 | * Scroll the list box if necessary to ensure the newly |
566 | * selected item is visible. |
567 | */ |
568 | items = gtk_container_children(GTK_CONTAINER(uc->list)); |
569 | nitems = g_list_length(items); |
570 | if (nitems > 0) { |
571 | int modified = FALSE; |
572 | g_list_free(items); |
573 | newtop = uc->adj->lower + |
574 | (uc->adj->upper - uc->adj->lower) * index / nitems; |
575 | newbot = uc->adj->lower + |
576 | (uc->adj->upper - uc->adj->lower) * (index+1) / nitems; |
577 | if (uc->adj->value > newtop) { |
578 | modified = TRUE; |
579 | uc->adj->value = newtop; |
580 | } else if (uc->adj->value < newbot - uc->adj->page_size) { |
581 | modified = TRUE; |
582 | uc->adj->value = newbot - uc->adj->page_size; |
583 | } |
584 | if (modified) |
585 | gtk_adjustment_value_changed(uc->adj); |
586 | } |
0f5e1f75 |
587 | } |
d9b15094 |
588 | } |
589 | |
590 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
591 | { |
0f5e1f75 |
592 | struct dlgparam *dp = (struct dlgparam *)dlg; |
593 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
594 | |
595 | assert(uc->ctrl->generic.type == CTRL_TEXT); |
596 | assert(uc->text != NULL); |
597 | |
598 | gtk_label_set_text(GTK_LABEL(uc->text), text); |
d9b15094 |
599 | } |
600 | |
7374c779 |
601 | void dlg_label_change(union control *ctrl, void *dlg, char const *text) |
602 | { |
aef05b78 |
603 | struct dlgparam *dp = (struct dlgparam *)dlg; |
604 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
605 | |
606 | switch (uc->ctrl->generic.type) { |
607 | case CTRL_BUTTON: |
608 | gtk_label_set_text(GTK_LABEL(uc->toplevel), text); |
609 | shortcut_highlight(uc->toplevel, ctrl->button.shortcut); |
610 | break; |
611 | case CTRL_CHECKBOX: |
612 | gtk_label_set_text(GTK_LABEL(uc->toplevel), text); |
613 | shortcut_highlight(uc->toplevel, ctrl->checkbox.shortcut); |
614 | break; |
615 | case CTRL_RADIO: |
616 | gtk_label_set_text(GTK_LABEL(uc->label), text); |
617 | shortcut_highlight(uc->label, ctrl->radio.shortcut); |
618 | break; |
619 | case CTRL_EDITBOX: |
620 | gtk_label_set_text(GTK_LABEL(uc->label), text); |
621 | shortcut_highlight(uc->label, ctrl->editbox.shortcut); |
622 | break; |
623 | case CTRL_FILESELECT: |
624 | gtk_label_set_text(GTK_LABEL(uc->label), text); |
625 | shortcut_highlight(uc->label, ctrl->fileselect.shortcut); |
626 | break; |
627 | case CTRL_FONTSELECT: |
628 | gtk_label_set_text(GTK_LABEL(uc->label), text); |
629 | shortcut_highlight(uc->label, ctrl->fontselect.shortcut); |
630 | break; |
631 | case CTRL_LISTBOX: |
632 | gtk_label_set_text(GTK_LABEL(uc->label), text); |
633 | shortcut_highlight(uc->label, ctrl->listbox.shortcut); |
634 | break; |
635 | default: |
636 | assert(!"This shouldn't happen"); |
637 | break; |
638 | } |
7374c779 |
639 | } |
640 | |
d9b15094 |
641 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
642 | { |
0f5e1f75 |
643 | struct dlgparam *dp = (struct dlgparam *)dlg; |
644 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
645 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
646 | assert(uc->entry != NULL); |
647 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path); |
d9b15094 |
648 | } |
649 | |
650 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
651 | { |
0f5e1f75 |
652 | struct dlgparam *dp = (struct dlgparam *)dlg; |
653 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
654 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
655 | assert(uc->entry != NULL); |
656 | strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
657 | lenof(fn->path)); |
658 | fn->path[lenof(fn->path)-1] = '\0'; |
d9b15094 |
659 | } |
660 | |
661 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs) |
662 | { |
0f5e1f75 |
663 | struct dlgparam *dp = (struct dlgparam *)dlg; |
664 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
665 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
666 | assert(uc->entry != NULL); |
667 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name); |
d9b15094 |
668 | } |
669 | |
670 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs) |
671 | { |
0f5e1f75 |
672 | struct dlgparam *dp = (struct dlgparam *)dlg; |
673 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
674 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
675 | assert(uc->entry != NULL); |
676 | strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
677 | lenof(fs->name)); |
678 | fs->name[lenof(fs->name)-1] = '\0'; |
d9b15094 |
679 | } |
680 | |
681 | /* |
682 | * Bracketing a large set of updates in these two functions will |
683 | * cause the front end (if possible) to delay updating the screen |
684 | * until it's all complete, thus avoiding flicker. |
685 | */ |
686 | void dlg_update_start(union control *ctrl, void *dlg) |
687 | { |
0f5e1f75 |
688 | /* |
689 | * Apparently we can't do this at all in GTK. GtkCList supports |
690 | * freeze and thaw, but not GtkList. Bah. |
691 | */ |
d9b15094 |
692 | } |
693 | |
694 | void dlg_update_done(union control *ctrl, void *dlg) |
695 | { |
0f5e1f75 |
696 | /* |
697 | * Apparently we can't do this at all in GTK. GtkCList supports |
698 | * freeze and thaw, but not GtkList. Bah. |
699 | */ |
d9b15094 |
700 | } |
701 | |
702 | void dlg_set_focus(union control *ctrl, void *dlg) |
703 | { |
0f5e1f75 |
704 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
705 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
706 | |
707 | switch (ctrl->generic.type) { |
708 | case CTRL_CHECKBOX: |
709 | case CTRL_BUTTON: |
710 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
711 | gtk_widget_grab_focus(uc->toplevel); |
712 | break; |
713 | case CTRL_FILESELECT: |
714 | case CTRL_FONTSELECT: |
715 | case CTRL_EDITBOX: |
716 | /* Anything containing an edit box gets that focused. */ |
717 | gtk_widget_grab_focus(uc->entry); |
718 | break; |
719 | case CTRL_RADIO: |
720 | /* |
721 | * Radio buttons: we find the currently selected button and |
722 | * focus it. |
723 | */ |
724 | { |
725 | int i; |
726 | for (i = 0; i < ctrl->radio.nbuttons; i++) |
727 | if (gtk_toggle_button_get_active |
728 | (GTK_TOGGLE_BUTTON(uc->buttons[i]))) { |
729 | gtk_widget_grab_focus(uc->buttons[i]); |
730 | } |
731 | } |
732 | break; |
733 | case CTRL_LISTBOX: |
734 | /* |
735 | * If the list is really an option menu, we focus it. |
736 | * Otherwise we tell it to focus one of its children, which |
737 | * appears to do the Right Thing. |
738 | */ |
739 | if (uc->optmenu) { |
740 | gtk_widget_grab_focus(uc->optmenu); |
741 | } else { |
742 | assert(uc->list != NULL); |
743 | gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD); |
744 | } |
745 | break; |
746 | } |
d9b15094 |
747 | } |
748 | |
749 | /* |
750 | * During event processing, you might well want to give an error |
751 | * indication to the user. dlg_beep() is a quick and easy generic |
752 | * error; dlg_error() puts up a message-box or equivalent. |
753 | */ |
754 | void dlg_beep(void *dlg) |
755 | { |
0f5e1f75 |
756 | gdk_beep(); |
d9b15094 |
757 | } |
758 | |
0bd8d76d |
759 | static void errmsg_button_clicked(GtkButton *button, gpointer data) |
760 | { |
761 | gtk_widget_destroy(GTK_WIDGET(data)); |
762 | } |
763 | |
7718480a |
764 | static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child) |
765 | { |
766 | gint x, y, w, h, dx, dy; |
767 | GtkRequisition req; |
768 | gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE); |
769 | gtk_widget_size_request(GTK_WIDGET(child), &req); |
770 | |
771 | gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y); |
772 | gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h); |
773 | |
774 | /* |
775 | * One corner of the transient will be offset inwards, by 1/4 |
776 | * of the parent window's size, from the corresponding corner |
777 | * of the parent window. The corner will be chosen so as to |
778 | * place the transient closer to the centre of the screen; this |
779 | * should avoid transients going off the edge of the screen on |
780 | * a regular basis. |
781 | */ |
782 | if (x + w/2 < gdk_screen_width() / 2) |
783 | dx = x + w/4; /* work from left edges */ |
784 | else |
785 | dx = x + 3*w/4 - req.width; /* work from right edges */ |
786 | if (y + h/2 < gdk_screen_height() / 2) |
787 | dy = y + h/4; /* work from top edges */ |
788 | else |
789 | dy = y + 3*h/4 - req.height; /* work from bottom edges */ |
790 | gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy); |
791 | } |
792 | |
d9b15094 |
793 | void dlg_error_msg(void *dlg, char *msg) |
794 | { |
0f5e1f75 |
795 | struct dlgparam *dp = (struct dlgparam *)dlg; |
23897ed0 |
796 | GtkWidget *window, *hbox, *text, *ok; |
0bd8d76d |
797 | |
798 | window = gtk_dialog_new(); |
799 | text = gtk_label_new(msg); |
800 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
23897ed0 |
801 | hbox = gtk_hbox_new(FALSE, 0); |
802 | gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20); |
0bd8d76d |
803 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
23897ed0 |
804 | hbox, FALSE, FALSE, 20); |
0bd8d76d |
805 | gtk_widget_show(text); |
23897ed0 |
806 | gtk_widget_show(hbox); |
807 | gtk_window_set_title(GTK_WINDOW(window), "Error"); |
0bd8d76d |
808 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
809 | ok = gtk_button_new_with_label("OK"); |
810 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
811 | ok, FALSE, FALSE, 0); |
812 | gtk_widget_show(ok); |
813 | GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT); |
814 | gtk_window_set_default(GTK_WINDOW(window), ok); |
815 | gtk_signal_connect(GTK_OBJECT(ok), "clicked", |
816 | GTK_SIGNAL_FUNC(errmsg_button_clicked), window); |
817 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
818 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
819 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
23897ed0 |
820 | gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window)); |
7718480a |
821 | set_transient_window_pos(dp->window, window); |
0bd8d76d |
822 | gtk_widget_show(window); |
823 | gtk_main(); |
d9b15094 |
824 | } |
825 | |
826 | /* |
827 | * This function signals to the front end that the dialog's |
828 | * processing is completed, and passes an integer value (typically |
829 | * a success status). |
830 | */ |
831 | void dlg_end(void *dlg, int value) |
832 | { |
0f5e1f75 |
833 | struct dlgparam *dp = (struct dlgparam *)dlg; |
0bd8d76d |
834 | dp->retval = value; |
1d009ae7 |
835 | gtk_widget_destroy(dp->window); |
d9b15094 |
836 | } |
837 | |
838 | void dlg_refresh(union control *ctrl, void *dlg) |
839 | { |
0f5e1f75 |
840 | struct dlgparam *dp = (struct dlgparam *)dlg; |
841 | struct uctrl *uc; |
842 | |
843 | if (ctrl) { |
844 | if (ctrl->generic.handler != NULL) |
845 | ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH); |
846 | } else { |
847 | int i; |
848 | |
849 | for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) { |
850 | assert(uc->ctrl != NULL); |
851 | if (uc->ctrl->generic.handler != NULL) |
852 | uc->ctrl->generic.handler(uc->ctrl, dp, |
853 | dp->data, EVENT_REFRESH); |
854 | } |
855 | } |
d9b15094 |
856 | } |
857 | |
858 | void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b) |
859 | { |
0f5e1f75 |
860 | struct dlgparam *dp = (struct dlgparam *)dlg; |
861 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
862 | gdouble cvals[4]; |
863 | |
864 | GtkWidget *coloursel = |
865 | gtk_color_selection_dialog_new("Select a colour"); |
866 | GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel); |
867 | |
868 | dp->coloursel_result.ok = FALSE; |
869 | |
870 | gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE); |
871 | gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE); |
872 | cvals[0] = r / 255.0; |
873 | cvals[1] = g / 255.0; |
874 | cvals[2] = b / 255.0; |
875 | cvals[3] = 1.0; /* fully opaque! */ |
876 | gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals); |
877 | |
878 | gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data", |
879 | (gpointer)coloursel); |
880 | gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data", |
881 | (gpointer)coloursel); |
882 | gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc); |
883 | gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked", |
884 | GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp); |
885 | gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked", |
886 | GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp); |
887 | gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked", |
888 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
889 | (gpointer)coloursel); |
890 | gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked", |
891 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
892 | (gpointer)coloursel); |
893 | gtk_widget_show(coloursel); |
d9b15094 |
894 | } |
895 | |
896 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
897 | int *r, int *g, int *b) |
898 | { |
0f5e1f75 |
899 | struct dlgparam *dp = (struct dlgparam *)dlg; |
900 | if (dp->coloursel_result.ok) { |
901 | *r = dp->coloursel_result.r; |
902 | *g = dp->coloursel_result.g; |
903 | *b = dp->coloursel_result.b; |
904 | return 1; |
905 | } else |
906 | return 0; |
d9b15094 |
907 | } |
908 | |
0f5e1f75 |
909 | /* ---------------------------------------------------------------------- |
910 | * Signal handlers while the dialog box is active. |
911 | */ |
912 | |
0bd8d76d |
913 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
914 | gpointer data) |
915 | { |
916 | struct dlgparam *dp = (struct dlgparam *)data; |
917 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
918 | union control *focus; |
919 | |
920 | if (uc && uc->ctrl) |
921 | focus = uc->ctrl; |
922 | else |
923 | focus = NULL; |
924 | |
925 | if (focus != dp->currfocus) { |
926 | dp->lastfocus = dp->currfocus; |
927 | dp->currfocus = focus; |
928 | } |
929 | |
930 | return FALSE; |
931 | } |
932 | |
0f5e1f75 |
933 | static void button_clicked(GtkButton *button, gpointer data) |
934 | { |
935 | struct dlgparam *dp = (struct dlgparam *)data; |
936 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
937 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
938 | } |
939 | |
940 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
941 | { |
942 | struct dlgparam *dp = (struct dlgparam *)data; |
943 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb)); |
944 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
945 | } |
946 | |
1c291fcf |
947 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
948 | { |
949 | /* |
950 | * GtkEntry has a nasty habit of eating the Return key, which |
951 | * is unhelpful since it doesn't actually _do_ anything with it |
952 | * (it calls gtk_widget_activate, but our edit boxes never need |
953 | * activating). So I catch Return before GtkEntry sees it, and |
954 | * pass it straight on to the parent widget. Effect: hitting |
955 | * Return in an edit box will now activate the default button |
956 | * in the dialog just like it will everywhere else. |
957 | */ |
958 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
959 | gint return_val; |
0bd8d76d |
960 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
1c291fcf |
961 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
962 | event, &return_val); |
963 | return return_val; |
964 | } |
965 | return FALSE; |
966 | } |
967 | |
0f5e1f75 |
968 | static void editbox_changed(GtkEditable *ed, gpointer data) |
969 | { |
970 | struct dlgparam *dp = (struct dlgparam *)data; |
971 | if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) { |
972 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
973 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
974 | } |
975 | } |
976 | |
977 | static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event, |
978 | gpointer data) |
979 | { |
980 | struct dlgparam *dp = (struct dlgparam *)data; |
981 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
982 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH); |
983 | } |
984 | |
0bd8d76d |
985 | static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data, |
986 | int multiple) |
987 | { |
988 | GtkAdjustment *adj = GTK_ADJUSTMENT(data); |
989 | |
990 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
991 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down || |
992 | event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up || |
993 | event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) { |
994 | /* |
995 | * Up, Down, PgUp or PgDn have been pressed on a ListItem |
996 | * in a list box. So, if the list box is single-selection: |
997 | * |
998 | * - if the list item in question isn't already selected, |
999 | * we simply select it. |
1000 | * - otherwise, we find the next one (or next |
1001 | * however-far-away) in whichever direction we're going, |
1002 | * and select that. |
1003 | * + in this case, we must also fiddle with the |
1004 | * scrollbar to ensure the newly selected item is |
1005 | * actually visible. |
1006 | * |
1007 | * If it's multiple-selection, we do all of the above |
1008 | * except actually selecting anything, so we move the focus |
1009 | * and fiddle the scrollbar to follow it. |
1010 | */ |
1011 | GtkWidget *list = item->parent; |
1012 | |
1013 | gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event"); |
1014 | |
1015 | if (!multiple && |
1016 | GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) { |
1017 | gtk_list_select_child(GTK_LIST(list), item); |
1018 | } else { |
1019 | int direction = |
1020 | (event->keyval==GDK_Up || event->keyval==GDK_KP_Up || |
1021 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
1022 | ? -1 : +1; |
1023 | int step = |
1024 | (event->keyval==GDK_Page_Down || |
1025 | event->keyval==GDK_KP_Page_Down || |
1026 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
1027 | ? 2 : 1; |
1028 | int i, n; |
0bd8d76d |
1029 | GList *children, *chead; |
1030 | |
1031 | chead = children = gtk_container_children(GTK_CONTAINER(list)); |
1032 | |
1033 | n = g_list_length(children); |
1034 | |
1035 | if (step == 2) { |
1036 | /* |
1037 | * Figure out how many list items to a screenful, |
1038 | * and adjust the step appropriately. |
1039 | */ |
1040 | step = 0.5 + adj->page_size * n / (adj->upper - adj->lower); |
1041 | step--; /* go by one less than that */ |
1042 | } |
1043 | |
1044 | i = 0; |
1045 | while (children != NULL) { |
1046 | if (item == children->data) |
1047 | break; |
1048 | children = children->next; |
1049 | i++; |
1050 | } |
1051 | |
1052 | while (step > 0) { |
1053 | if (direction < 0 && i > 0) |
1054 | children = children->prev, i--; |
1055 | else if (direction > 0 && i < n-1) |
1056 | children = children->next, i++; |
1057 | step--; |
1058 | } |
1059 | |
1060 | if (children && children->data) { |
1061 | if (!multiple) |
1062 | gtk_list_select_child(GTK_LIST(list), |
1063 | GTK_WIDGET(children->data)); |
1064 | gtk_widget_grab_focus(GTK_WIDGET(children->data)); |
1065 | gtk_adjustment_clamp_page |
1066 | (adj, |
1067 | adj->lower + (adj->upper-adj->lower) * i / n, |
1068 | adj->lower + (adj->upper-adj->lower) * (i+1) / n); |
1069 | } |
1070 | |
1071 | g_list_free(chead); |
1072 | } |
1073 | return TRUE; |
1074 | } |
1075 | |
1076 | return FALSE; |
1077 | } |
1078 | |
1079 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
1080 | gpointer data) |
1081 | { |
1d009ae7 |
1082 | return listitem_key(item, event, data, FALSE); |
0bd8d76d |
1083 | } |
1084 | |
1085 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
1086 | gpointer data) |
1087 | { |
1d009ae7 |
1088 | return listitem_key(item, event, data, TRUE); |
0bd8d76d |
1089 | } |
1090 | |
b4ac18ae |
1091 | static int listitem_button_press(GtkWidget *item, GdkEventButton *event, |
1092 | gpointer data) |
1093 | { |
1094 | struct dlgparam *dp = (struct dlgparam *)data; |
1095 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item)); |
1096 | switch (event->type) { |
1097 | default: |
1098 | case GDK_BUTTON_PRESS: uc->nclicks = 1; break; |
1099 | case GDK_2BUTTON_PRESS: uc->nclicks = 2; break; |
1100 | case GDK_3BUTTON_PRESS: uc->nclicks = 3; break; |
1101 | } |
1102 | return FALSE; |
1103 | } |
1104 | |
1105 | static int listitem_button_release(GtkWidget *item, GdkEventButton *event, |
1106 | gpointer data) |
0f5e1f75 |
1107 | { |
1108 | struct dlgparam *dp = (struct dlgparam *)data; |
b4ac18ae |
1109 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item)); |
1110 | if (uc->nclicks>1) { |
0f5e1f75 |
1111 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
0bd8d76d |
1112 | return TRUE; |
0f5e1f75 |
1113 | } |
0bd8d76d |
1114 | return FALSE; |
0f5e1f75 |
1115 | } |
1116 | |
1117 | static void list_selchange(GtkList *list, gpointer data) |
1118 | { |
1119 | struct dlgparam *dp = (struct dlgparam *)data; |
1120 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list)); |
8dacc30e |
1121 | if (!uc) return; |
0f5e1f75 |
1122 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
1123 | } |
1124 | |
1125 | static void menuitem_activate(GtkMenuItem *item, gpointer data) |
1126 | { |
1127 | struct dlgparam *dp = (struct dlgparam *)data; |
1128 | GtkWidget *menushell = GTK_WIDGET(item)->parent; |
1129 | gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data"); |
1130 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu)); |
1131 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
1132 | } |
1133 | |
1134 | static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction) |
1135 | { |
1136 | int index = dlg_listbox_index(uc->ctrl, dp); |
1137 | GList *children = gtk_container_children(GTK_CONTAINER(uc->list)); |
1138 | GtkWidget *child; |
1139 | |
1140 | if ((index < 0) || |
1141 | (index == 0 && direction < 0) || |
1142 | (index == g_list_length(children)-1 && direction > 0)) { |
1143 | gdk_beep(); |
1144 | return; |
1145 | } |
1146 | |
1147 | child = g_list_nth_data(children, index); |
1148 | gtk_widget_ref(child); |
1149 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
0bd8d76d |
1150 | g_list_free(children); |
1151 | |
0f5e1f75 |
1152 | children = NULL; |
1153 | children = g_list_append(children, child); |
1154 | gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction); |
1155 | gtk_list_select_item(GTK_LIST(uc->list), index + direction); |
1156 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
1157 | } |
1158 | |
1159 | static void draglist_up(GtkButton *button, gpointer data) |
1160 | { |
1161 | struct dlgparam *dp = (struct dlgparam *)data; |
1162 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1163 | draglist_move(dp, uc, -1); |
1164 | } |
1165 | |
1166 | static void draglist_down(GtkButton *button, gpointer data) |
1167 | { |
1168 | struct dlgparam *dp = (struct dlgparam *)data; |
1169 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1170 | draglist_move(dp, uc, +1); |
1171 | } |
1172 | |
1173 | static void filesel_ok(GtkButton *button, gpointer data) |
1174 | { |
1d009ae7 |
1175 | /* struct dlgparam *dp = (struct dlgparam *)data; */ |
0f5e1f75 |
1176 | gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1177 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data"); |
1178 | char *name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); |
1179 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
1180 | } |
1181 | |
1182 | static void fontsel_ok(GtkButton *button, gpointer data) |
1183 | { |
1d009ae7 |
1184 | /* struct dlgparam *dp = (struct dlgparam *)data; */ |
0f5e1f75 |
1185 | gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1186 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data"); |
1187 | char *name = gtk_font_selection_dialog_get_font_name |
1188 | (GTK_FONT_SELECTION_DIALOG(fontsel)); |
1189 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
1190 | } |
1191 | |
1192 | static void coloursel_ok(GtkButton *button, gpointer data) |
1193 | { |
1194 | struct dlgparam *dp = (struct dlgparam *)data; |
1195 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1196 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
1197 | gdouble cvals[4]; |
1198 | gtk_color_selection_get_color |
1199 | (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel), |
1200 | cvals); |
1201 | dp->coloursel_result.r = (int) (255 * cvals[0]); |
1202 | dp->coloursel_result.g = (int) (255 * cvals[1]); |
1203 | dp->coloursel_result.b = (int) (255 * cvals[2]); |
1204 | dp->coloursel_result.ok = TRUE; |
1205 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
1206 | } |
1207 | |
1208 | static void coloursel_cancel(GtkButton *button, gpointer data) |
1209 | { |
1210 | struct dlgparam *dp = (struct dlgparam *)data; |
1211 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1212 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
1213 | dp->coloursel_result.ok = FALSE; |
1214 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
1215 | } |
1216 | |
1217 | static void filefont_clicked(GtkButton *button, gpointer data) |
1218 | { |
1219 | struct dlgparam *dp = (struct dlgparam *)data; |
1220 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
1221 | |
1222 | if (uc->ctrl->generic.type == CTRL_FILESELECT) { |
1223 | GtkWidget *filesel = |
1224 | gtk_file_selection_new(uc->ctrl->fileselect.title); |
1225 | gtk_window_set_modal(GTK_WINDOW(filesel), TRUE); |
1226 | gtk_object_set_data |
1227 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data", |
1228 | (gpointer)filesel); |
1229 | gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc); |
1230 | gtk_signal_connect |
1231 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1232 | GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp); |
1233 | gtk_signal_connect_object |
1234 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1235 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1236 | gtk_signal_connect_object |
1237 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", |
1238 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1239 | gtk_widget_show(filesel); |
1240 | } |
1241 | |
1242 | if (uc->ctrl->generic.type == CTRL_FONTSELECT) { |
1243 | gchar *spacings[] = { "c", "m", NULL }; |
0bd8d76d |
1244 | gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry)); |
0f5e1f75 |
1245 | GtkWidget *fontsel = |
1246 | gtk_font_selection_dialog_new("Select a font"); |
1247 | gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE); |
1248 | gtk_font_selection_dialog_set_filter |
1249 | (GTK_FONT_SELECTION_DIALOG(fontsel), |
1250 | GTK_FONT_FILTER_BASE, GTK_FONT_ALL, |
1251 | NULL, NULL, NULL, NULL, spacings, NULL); |
0bd8d76d |
1252 | if (!gtk_font_selection_dialog_set_font_name |
1253 | (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) { |
1254 | /* |
1255 | * If the font name wasn't found as it was, try opening |
1256 | * it and extracting its FONT property. This should |
1257 | * have the effect of mapping short aliases into true |
1258 | * XLFDs. |
1259 | */ |
1260 | GdkFont *font = gdk_font_load(fontname); |
1261 | if (font) { |
1262 | XFontStruct *xfs = GDK_FONT_XFONT(font); |
1263 | Display *disp = GDK_FONT_XDISPLAY(font); |
1264 | Atom fontprop = XInternAtom(disp, "FONT", False); |
1265 | unsigned long ret; |
1266 | if (XGetFontProperty(xfs, fontprop, &ret)) { |
1267 | char *name = XGetAtomName(disp, (Atom)ret); |
1268 | if (name) |
1269 | gtk_font_selection_dialog_set_font_name |
1270 | (GTK_FONT_SELECTION_DIALOG(fontsel), name); |
1271 | } |
1272 | gdk_font_unref(font); |
1273 | } |
1274 | } |
0f5e1f75 |
1275 | gtk_object_set_data |
1276 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1277 | "user-data", (gpointer)fontsel); |
1278 | gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc); |
1279 | gtk_signal_connect |
1280 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1281 | "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp); |
1282 | gtk_signal_connect_object |
1283 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
1284 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
1285 | (gpointer)fontsel); |
1286 | gtk_signal_connect_object |
1287 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button), |
1288 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
1289 | (gpointer)fontsel); |
1290 | gtk_widget_show(fontsel); |
1291 | } |
1292 | } |
1293 | |
c5d64b49 |
1294 | static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc, |
1295 | gpointer data) |
1296 | { |
1297 | struct dlgparam *dp = (struct dlgparam *)data; |
1298 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
1299 | |
1300 | gtk_widget_set_usize(uc->text, alloc->width, -1); |
1301 | gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label); |
1302 | gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig); |
1303 | } |
1304 | |
0f5e1f75 |
1305 | /* ---------------------------------------------------------------------- |
d9b15094 |
1306 | * This function does the main layout work: it reads a controlset, |
1307 | * it creates the relevant GTK controls, and returns a GtkWidget |
1308 | * containing the result. (This widget might be a title of some |
1309 | * sort, it might be a Columns containing many controls, or it |
1310 | * might be a GtkFrame containing a Columns; whatever it is, it's |
1311 | * definitely a GtkWidget and should probably be added to a |
1312 | * GtkVbox.) |
1c291fcf |
1313 | * |
1314 | * `listitemheight' is used to calculate a usize for list boxes: it |
1315 | * should be the height from the size request of a GtkListItem. |
1316 | * |
1317 | * `win' is required for setting the default button. If it is |
1318 | * non-NULL, all buttons created will be default-capable (so they |
1319 | * have extra space round them for the default highlight). |
d9b15094 |
1320 | */ |
1c291fcf |
1321 | GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs, |
1322 | struct controlset *s, int listitemheight, |
1323 | GtkWindow *win) |
d9b15094 |
1324 | { |
1325 | Columns *cols; |
1326 | GtkWidget *ret; |
1327 | int i; |
1328 | |
1329 | if (!s->boxname && s->boxtitle) { |
1330 | /* This controlset is a panel title. */ |
1331 | return gtk_label_new(s->boxtitle); |
1332 | } |
1333 | |
1334 | /* |
1335 | * Otherwise, we expect to be laying out actual controls, so |
1336 | * we'll start by creating a Columns for the purpose. |
1337 | */ |
1338 | cols = COLUMNS(columns_new(4)); |
1339 | ret = GTK_WIDGET(cols); |
1340 | gtk_widget_show(ret); |
1341 | |
1342 | /* |
1343 | * Create a containing frame if we have a box name. |
1344 | */ |
1345 | if (*s->boxname) { |
1346 | ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */ |
1347 | gtk_container_set_border_width(GTK_CONTAINER(cols), 4); |
1348 | gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols)); |
1349 | gtk_widget_show(ret); |
1350 | } |
1351 | |
1352 | /* |
1353 | * Now iterate through the controls themselves, create them, |
1354 | * and add them to the Columns. |
1355 | */ |
1356 | for (i = 0; i < s->ncontrols; i++) { |
1357 | union control *ctrl = s->ctrls[i]; |
1c291fcf |
1358 | struct uctrl *uc; |
1359 | int left = FALSE; |
d9b15094 |
1360 | GtkWidget *w = NULL; |
1361 | |
1362 | switch (ctrl->generic.type) { |
1363 | case CTRL_COLUMNS: |
1364 | { |
1365 | static const int simplecols[1] = { 100 }; |
1366 | columns_set_cols(cols, ctrl->columns.ncols, |
1367 | (ctrl->columns.percentages ? |
1368 | ctrl->columns.percentages : simplecols)); |
1369 | } |
1370 | continue; /* no actual control created */ |
1371 | case CTRL_TABDELAY: |
0f5e1f75 |
1372 | { |
1373 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl); |
1374 | if (uc) |
1375 | columns_taborder_last(cols, uc->toplevel); |
1376 | } |
d9b15094 |
1377 | continue; /* no actual control created */ |
1c291fcf |
1378 | } |
1379 | |
3d88e64d |
1380 | uc = snew(struct uctrl); |
1c291fcf |
1381 | uc->ctrl = ctrl; |
1382 | uc->privdata = NULL; |
1383 | uc->privdata_needs_free = FALSE; |
1384 | uc->buttons = NULL; |
1385 | uc->entry = uc->list = uc->menu = NULL; |
1386 | uc->button = uc->optmenu = uc->text = NULL; |
aef05b78 |
1387 | uc->label = NULL; |
b4ac18ae |
1388 | uc->nclicks = 0; |
1c291fcf |
1389 | |
1390 | switch (ctrl->generic.type) { |
d9b15094 |
1391 | case CTRL_BUTTON: |
1392 | w = gtk_button_new_with_label(ctrl->generic.label); |
1c291fcf |
1393 | if (win) { |
1394 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
1395 | if (ctrl->button.isdefault) |
1396 | gtk_window_set_default(win, w); |
0bd8d76d |
1397 | if (ctrl->button.iscancel) |
1398 | dp->cancelbutton = w; |
1c291fcf |
1399 | } |
0f5e1f75 |
1400 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
1401 | GTK_SIGNAL_FUNC(button_clicked), dp); |
0bd8d76d |
1402 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
1403 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1404 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut, |
1405 | SHORTCUT_UCTRL, uc); |
d9b15094 |
1406 | break; |
1407 | case CTRL_CHECKBOX: |
1408 | w = gtk_check_button_new_with_label(ctrl->generic.label); |
0f5e1f75 |
1409 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
1410 | GTK_SIGNAL_FUNC(button_toggled), dp); |
0bd8d76d |
1411 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
1412 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1413 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut, |
1414 | SHORTCUT_UCTRL, uc); |
1415 | left = TRUE; |
d9b15094 |
1416 | break; |
1417 | case CTRL_RADIO: |
1418 | /* |
1419 | * Radio buttons get to go inside their own Columns, no |
1420 | * matter what. |
1421 | */ |
1422 | { |
1423 | gint i, *percentages; |
1424 | GSList *group; |
1425 | |
c5d64b49 |
1426 | w = columns_new(0); |
d9b15094 |
1427 | if (ctrl->generic.label) { |
1428 | GtkWidget *label = gtk_label_new(ctrl->generic.label); |
1429 | columns_add(COLUMNS(w), label, 0, 1); |
1430 | columns_force_left_align(COLUMNS(w), label); |
1431 | gtk_widget_show(label); |
1c291fcf |
1432 | shortcut_add(scs, label, ctrl->radio.shortcut, |
1433 | SHORTCUT_UCTRL, uc); |
aef05b78 |
1434 | uc->label = label; |
d9b15094 |
1435 | } |
1436 | percentages = g_new(gint, ctrl->radio.ncolumns); |
1437 | for (i = 0; i < ctrl->radio.ncolumns; i++) { |
1438 | percentages[i] = |
1439 | ((100 * (i+1) / ctrl->radio.ncolumns) - |
1440 | 100 * i / ctrl->radio.ncolumns); |
1441 | } |
1442 | columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns, |
1443 | percentages); |
1444 | g_free(percentages); |
1445 | group = NULL; |
0f5e1f75 |
1446 | |
1c291fcf |
1447 | uc->nbuttons = ctrl->radio.nbuttons; |
3d88e64d |
1448 | uc->buttons = snewn(uc->nbuttons, GtkWidget *); |
0f5e1f75 |
1449 | |
d9b15094 |
1450 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
1451 | GtkWidget *b; |
1452 | gint colstart; |
1453 | |
1454 | b = (gtk_radio_button_new_with_label |
1455 | (group, ctrl->radio.buttons[i])); |
1c291fcf |
1456 | uc->buttons[i] = b; |
d9b15094 |
1457 | group = gtk_radio_button_group(GTK_RADIO_BUTTON(b)); |
1458 | colstart = i % ctrl->radio.ncolumns; |
1459 | columns_add(COLUMNS(w), b, colstart, |
1460 | (i == ctrl->radio.nbuttons-1 ? |
1461 | ctrl->radio.ncolumns - colstart : 1)); |
1c291fcf |
1462 | columns_force_left_align(COLUMNS(w), b); |
d9b15094 |
1463 | gtk_widget_show(b); |
0f5e1f75 |
1464 | gtk_signal_connect(GTK_OBJECT(b), "toggled", |
1465 | GTK_SIGNAL_FUNC(button_toggled), dp); |
0bd8d76d |
1466 | gtk_signal_connect(GTK_OBJECT(b), "focus_in_event", |
1467 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1c291fcf |
1468 | if (ctrl->radio.shortcuts) { |
1469 | shortcut_add(scs, GTK_BIN(b)->child, |
1470 | ctrl->radio.shortcuts[i], |
1471 | SHORTCUT_UCTRL, uc); |
1472 | } |
d9b15094 |
1473 | } |
1474 | } |
1475 | break; |
1476 | case CTRL_EDITBOX: |
c5d64b49 |
1477 | { |
d9b15094 |
1478 | GtkRequisition req; |
c5d64b49 |
1479 | |
1480 | if (ctrl->editbox.has_list) { |
1481 | w = gtk_combo_new(); |
1482 | gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE); |
1483 | uc->entry = GTK_COMBO(w)->entry; |
1484 | uc->list = GTK_COMBO(w)->list; |
1485 | } else { |
1486 | w = gtk_entry_new(); |
1487 | if (ctrl->editbox.password) |
1488 | gtk_entry_set_visibility(GTK_ENTRY(w), FALSE); |
1489 | uc->entry = w; |
1490 | } |
1491 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
1492 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
1493 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
1494 | GTK_SIGNAL_FUNC(editbox_key), dp); |
1495 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
1496 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1497 | /* |
1498 | * Edit boxes, for some strange reason, have a minimum |
1499 | * width of 150 in GTK 1.2. We don't want this - we'd |
1500 | * rather the edit boxes acquired their natural width |
1501 | * from the column layout of the rest of the box. |
1502 | * |
1503 | * Also, while we're here, we'll squirrel away the |
1504 | * edit box height so we can use that to centre its |
1505 | * label vertically beside it. |
1506 | */ |
d9b15094 |
1507 | gtk_widget_size_request(w, &req); |
1508 | gtk_widget_set_usize(w, 10, req.height); |
d9b15094 |
1509 | |
c5d64b49 |
1510 | if (ctrl->generic.label) { |
1511 | GtkWidget *label, *container; |
1512 | |
1513 | label = gtk_label_new(ctrl->generic.label); |
1514 | |
1515 | shortcut_add(scs, label, ctrl->editbox.shortcut, |
1516 | SHORTCUT_FOCUS, uc->entry); |
1517 | |
1518 | container = columns_new(4); |
1519 | if (ctrl->editbox.percentwidth == 100) { |
1520 | columns_add(COLUMNS(container), label, 0, 1); |
1521 | columns_force_left_align(COLUMNS(container), label); |
1522 | columns_add(COLUMNS(container), w, 0, 1); |
1523 | } else { |
1524 | gint percentages[2]; |
1525 | percentages[1] = ctrl->editbox.percentwidth; |
1526 | percentages[0] = 100 - ctrl->editbox.percentwidth; |
1527 | columns_set_cols(COLUMNS(container), 2, percentages); |
1528 | columns_add(COLUMNS(container), label, 0, 1); |
1529 | columns_force_left_align(COLUMNS(container), label); |
1530 | columns_add(COLUMNS(container), w, 1, 1); |
1531 | /* Centre the label vertically. */ |
1532 | gtk_widget_set_usize(label, -1, req.height); |
1533 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); |
1534 | } |
1535 | gtk_widget_show(label); |
1536 | gtk_widget_show(w); |
d9b15094 |
1537 | |
c5d64b49 |
1538 | w = container; |
aef05b78 |
1539 | uc->label = label; |
c5d64b49 |
1540 | } |
1541 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event", |
1542 | GTK_SIGNAL_FUNC(editbox_lostfocus), dp); |
1543 | } |
d9b15094 |
1544 | break; |
1d0d4a3b |
1545 | case CTRL_FILESELECT: |
1546 | case CTRL_FONTSELECT: |
1547 | { |
1548 | GtkWidget *ww; |
1549 | GtkRequisition req; |
1550 | char *browsebtn = |
1551 | (ctrl->generic.type == CTRL_FILESELECT ? |
1552 | "Browse..." : "Change..."); |
1553 | |
1554 | gint percentages[] = { 75, 25 }; |
1555 | w = columns_new(4); |
1556 | columns_set_cols(COLUMNS(w), 2, percentages); |
1557 | |
1558 | if (ctrl->generic.label) { |
1559 | ww = gtk_label_new(ctrl->generic.label); |
1560 | columns_add(COLUMNS(w), ww, 0, 2); |
1561 | columns_force_left_align(COLUMNS(w), ww); |
1562 | gtk_widget_show(ww); |
1c291fcf |
1563 | shortcut_add(scs, ww, |
1564 | (ctrl->generic.type == CTRL_FILESELECT ? |
1565 | ctrl->fileselect.shortcut : |
1566 | ctrl->fontselect.shortcut), |
1567 | SHORTCUT_UCTRL, uc); |
aef05b78 |
1568 | uc->label = ww; |
1d0d4a3b |
1569 | } |
1570 | |
1c291fcf |
1571 | uc->entry = ww = gtk_entry_new(); |
1d0d4a3b |
1572 | gtk_widget_size_request(ww, &req); |
1573 | gtk_widget_set_usize(ww, 10, req.height); |
1574 | columns_add(COLUMNS(w), ww, 0, 1); |
1575 | gtk_widget_show(ww); |
1576 | |
1c291fcf |
1577 | uc->button = ww = gtk_button_new_with_label(browsebtn); |
1d0d4a3b |
1578 | columns_add(COLUMNS(w), ww, 1, 1); |
1579 | gtk_widget_show(ww); |
0f5e1f75 |
1580 | |
1c291fcf |
1581 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
1582 | GTK_SIGNAL_FUNC(editbox_key), dp); |
1583 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
0f5e1f75 |
1584 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
0bd8d76d |
1585 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
1586 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1587 | gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event", |
1588 | GTK_SIGNAL_FUNC(widget_focus), dp); |
0f5e1f75 |
1589 | gtk_signal_connect(GTK_OBJECT(ww), "clicked", |
1590 | GTK_SIGNAL_FUNC(filefont_clicked), dp); |
1d0d4a3b |
1591 | } |
1592 | break; |
1593 | case CTRL_LISTBOX: |
1594 | if (ctrl->listbox.height == 0) { |
1c291fcf |
1595 | uc->optmenu = w = gtk_option_menu_new(); |
1596 | uc->menu = gtk_menu_new(); |
1597 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu); |
1598 | gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data", |
1599 | (gpointer)uc->optmenu); |
0bd8d76d |
1600 | gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event", |
1601 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1602 | } else { |
1c291fcf |
1603 | uc->list = gtk_list_new(); |
8eed910d |
1604 | if (ctrl->listbox.multisel == 2) { |
1605 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1606 | GTK_SELECTION_EXTENDED); |
1607 | } else if (ctrl->listbox.multisel == 1) { |
0bd8d76d |
1608 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1609 | GTK_SELECTION_MULTIPLE); |
1610 | } else { |
1611 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
1612 | GTK_SELECTION_SINGLE); |
1613 | } |
1d0d4a3b |
1614 | w = gtk_scrolled_window_new(NULL, NULL); |
1615 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w), |
1c291fcf |
1616 | uc->list); |
1d0d4a3b |
1617 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w), |
1618 | GTK_POLICY_NEVER, |
1619 | GTK_POLICY_AUTOMATIC); |
0bd8d76d |
1620 | uc->adj = gtk_scrolled_window_get_vadjustment |
1621 | (GTK_SCROLLED_WINDOW(w)); |
1622 | |
1c291fcf |
1623 | gtk_widget_show(uc->list); |
1624 | gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed", |
0f5e1f75 |
1625 | GTK_SIGNAL_FUNC(list_selchange), dp); |
0bd8d76d |
1626 | gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event", |
1627 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1628 | |
1629 | /* |
1630 | * Adjust the height of the scrolled window to the |
1631 | * minimum given by the height parameter. |
1632 | * |
1633 | * This piece of guesswork is a horrid hack based |
1634 | * on looking inside the GTK 1.2 sources |
1635 | * (specifically gtkviewport.c, which appears to be |
1636 | * the widget which provides the border around the |
1637 | * scrolling area). Anyone lets me know how I can |
1638 | * do this in a way which isn't at risk from GTK |
1639 | * upgrades, I'd be grateful. |
1640 | */ |
1641 | { |
1c291fcf |
1642 | int edge = GTK_WIDGET(uc->list)->style->klass->ythickness; |
1d0d4a3b |
1643 | gtk_widget_set_usize(w, 10, |
1644 | 2*edge + (ctrl->listbox.height * |
1645 | listitemheight)); |
1646 | } |
1d0d4a3b |
1647 | |
1648 | if (ctrl->listbox.draglist) { |
1649 | /* |
1650 | * GTK doesn't appear to make it easy to |
1651 | * implement a proper draggable list; so |
1652 | * instead I'm just going to have to put an Up |
1653 | * and a Down button to the right of the actual |
1654 | * list box. Ah well. |
1655 | */ |
1656 | GtkWidget *cols, *button; |
1657 | static const gint percentages[2] = { 80, 20 }; |
1658 | |
1659 | cols = columns_new(4); |
1660 | columns_set_cols(COLUMNS(cols), 2, percentages); |
1661 | columns_add(COLUMNS(cols), w, 0, 1); |
1662 | gtk_widget_show(w); |
1663 | button = gtk_button_new_with_label("Up"); |
1664 | columns_add(COLUMNS(cols), button, 1, 1); |
1665 | gtk_widget_show(button); |
0f5e1f75 |
1666 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1667 | GTK_SIGNAL_FUNC(draglist_up), dp); |
0bd8d76d |
1668 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
1669 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1670 | button = gtk_button_new_with_label("Down"); |
1671 | columns_add(COLUMNS(cols), button, 1, 1); |
1672 | gtk_widget_show(button); |
0f5e1f75 |
1673 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
1674 | GTK_SIGNAL_FUNC(draglist_down), dp); |
0bd8d76d |
1675 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
1676 | GTK_SIGNAL_FUNC(widget_focus), dp); |
1d0d4a3b |
1677 | |
1678 | w = cols; |
1679 | } |
1680 | |
1681 | } |
1682 | if (ctrl->generic.label) { |
1683 | GtkWidget *label, *container; |
1684 | |
1685 | label = gtk_label_new(ctrl->generic.label); |
1686 | |
1687 | container = columns_new(4); |
1688 | if (ctrl->listbox.percentwidth == 100) { |
1689 | columns_add(COLUMNS(container), label, 0, 1); |
1690 | columns_force_left_align(COLUMNS(container), label); |
1691 | columns_add(COLUMNS(container), w, 0, 1); |
1692 | } else { |
1693 | gint percentages[2]; |
1694 | percentages[1] = ctrl->listbox.percentwidth; |
1695 | percentages[0] = 100 - ctrl->listbox.percentwidth; |
1696 | columns_set_cols(COLUMNS(container), 2, percentages); |
1697 | columns_add(COLUMNS(container), label, 0, 1); |
1698 | columns_force_left_align(COLUMNS(container), label); |
1699 | columns_add(COLUMNS(container), w, 1, 1); |
1700 | } |
1701 | gtk_widget_show(label); |
1702 | gtk_widget_show(w); |
1c291fcf |
1703 | shortcut_add(scs, label, ctrl->listbox.shortcut, |
1704 | SHORTCUT_UCTRL, uc); |
1d0d4a3b |
1705 | w = container; |
aef05b78 |
1706 | uc->label = label; |
1d0d4a3b |
1707 | } |
1708 | break; |
d9b15094 |
1709 | case CTRL_TEXT: |
c5d64b49 |
1710 | /* |
1711 | * Wrapping text widgets don't sit well with the GTK |
1712 | * layout model, in which widgets state a minimum size |
1713 | * and the whole window then adjusts to the smallest |
1714 | * size it can sensibly take given its contents. A |
1715 | * wrapping text widget _has_ no clear minimum size; |
1716 | * instead it has a range of possibilities. It can be |
1717 | * one line deep but 2000 wide, or two lines deep and |
1718 | * 1000 pixels, or three by 867, or four by 500 and so |
1719 | * on. It can be as short as you like provided you |
1720 | * don't mind it being wide, or as narrow as you like |
1721 | * provided you don't mind it being tall. |
1722 | * |
1723 | * Therefore, it fits very badly into the layout model. |
1724 | * Hence the only thing to do is pick a width and let |
1725 | * it choose its own number of lines. To do this I'm |
1726 | * going to cheat a little. All new wrapping text |
1727 | * widgets will be created with a minimal text content |
1728 | * "X"; then, after the rest of the dialog box is set |
1729 | * up and its size calculated, the text widgets will be |
1730 | * told their width and given their real text, which |
1731 | * will cause the size to be recomputed in the y |
1732 | * direction (because many of them will expand to more |
1733 | * than one line). |
1734 | */ |
1735 | uc->text = w = gtk_label_new("X"); |
0bd8d76d |
1736 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0); |
d9b15094 |
1737 | gtk_label_set_line_wrap(GTK_LABEL(w), TRUE); |
c5d64b49 |
1738 | uc->textsig = |
1739 | gtk_signal_connect(GTK_OBJECT(w), "size-allocate", |
1740 | GTK_SIGNAL_FUNC(label_sizealloc), dp); |
d9b15094 |
1741 | break; |
1742 | } |
0f5e1f75 |
1743 | |
1c291fcf |
1744 | assert(w != NULL); |
0f5e1f75 |
1745 | |
1c291fcf |
1746 | columns_add(cols, w, |
1747 | COLUMN_START(ctrl->generic.column), |
1748 | COLUMN_SPAN(ctrl->generic.column)); |
1749 | if (left) |
1750 | columns_force_left_align(cols, w); |
1751 | gtk_widget_show(w); |
1752 | |
1753 | uc->toplevel = w; |
1754 | dlg_add_uctrl(dp, uc); |
d9b15094 |
1755 | } |
1756 | |
1757 | return ret; |
1758 | } |
1759 | |
1760 | struct selparam { |
1c291fcf |
1761 | struct dlgparam *dp; |
d5410af8 |
1762 | GtkNotebook *panels; |
d9b15094 |
1763 | GtkWidget *panel, *treeitem; |
1c291fcf |
1764 | struct Shortcuts shortcuts; |
d9b15094 |
1765 | }; |
1766 | |
1767 | static void treeitem_sel(GtkItem *item, gpointer data) |
1768 | { |
1769 | struct selparam *sp = (struct selparam *)data; |
d5410af8 |
1770 | gint page_num; |
d9b15094 |
1771 | |
d5410af8 |
1772 | page_num = gtk_notebook_page_num(sp->panels, sp->panel); |
1773 | gtk_notebook_set_page(sp->panels, page_num); |
1c291fcf |
1774 | |
aef05b78 |
1775 | dlg_refresh(NULL, sp->dp); |
1776 | |
1c291fcf |
1777 | sp->dp->shortcuts = &sp->shortcuts; |
0bd8d76d |
1778 | sp->dp->currtreeitem = sp->treeitem; |
d9b15094 |
1779 | } |
1780 | |
0bd8d76d |
1781 | static void window_destroy(GtkWidget *widget, gpointer data) |
d9b15094 |
1782 | { |
1783 | gtk_main_quit(); |
1784 | } |
1785 | |
c5d64b49 |
1786 | static int tree_grab_focus(struct dlgparam *dp) |
1787 | { |
1788 | int i, f; |
1789 | |
1790 | /* |
1791 | * See if any of the treeitems has the focus. |
1792 | */ |
1793 | f = -1; |
1794 | for (i = 0; i < dp->ntreeitems; i++) |
1795 | if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) { |
1796 | f = i; |
1797 | break; |
1798 | } |
1799 | |
1800 | if (f >= 0) |
1801 | return FALSE; |
1802 | else { |
1803 | gtk_widget_grab_focus(dp->currtreeitem); |
1804 | return TRUE; |
1805 | } |
1806 | } |
1807 | |
1808 | gint tree_focus(GtkContainer *container, GtkDirectionType direction, |
1809 | gpointer data) |
1810 | { |
1811 | struct dlgparam *dp = (struct dlgparam *)data; |
1812 | |
1813 | gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus"); |
1814 | /* |
1815 | * If there's a focused treeitem, we return FALSE to cause the |
1816 | * focus to move on to some totally other control. If not, we |
1817 | * focus the selected one. |
1818 | */ |
1819 | return tree_grab_focus(dp); |
1820 | } |
1821 | |
1c291fcf |
1822 | int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
1823 | { |
1824 | struct dlgparam *dp = (struct dlgparam *)data; |
1825 | |
0bd8d76d |
1826 | if (event->keyval == GDK_Escape && dp->cancelbutton) { |
1827 | gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked"); |
1c291fcf |
1828 | return TRUE; |
1829 | } |
1830 | |
1831 | if ((event->state & GDK_MOD1_MASK) && |
1832 | (unsigned char)event->string[0] > 0 && |
1833 | (unsigned char)event->string[0] <= 127) { |
1834 | int schr = (unsigned char)event->string[0]; |
1835 | struct Shortcut *sc = &dp->shortcuts->sc[schr]; |
1836 | |
1837 | switch (sc->action) { |
c5d64b49 |
1838 | case SHORTCUT_TREE: |
1839 | tree_grab_focus(dp); |
1840 | break; |
1c291fcf |
1841 | case SHORTCUT_FOCUS: |
1842 | gtk_widget_grab_focus(sc->widget); |
1843 | break; |
1844 | case SHORTCUT_UCTRL: |
1845 | /* |
1846 | * We must do something sensible with a uctrl. |
1847 | * Precisely what this is depends on the type of |
1848 | * control. |
1849 | */ |
1850 | switch (sc->uc->ctrl->generic.type) { |
1851 | case CTRL_CHECKBOX: |
1852 | case CTRL_BUTTON: |
1853 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
1854 | gtk_widget_grab_focus(sc->uc->toplevel); |
1855 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel), |
1856 | "clicked"); |
1857 | break; |
1858 | case CTRL_FILESELECT: |
1859 | case CTRL_FONTSELECT: |
1860 | /* File/font selectors have their buttons pressed (ooer), |
1861 | * and focus transferred to the edit box. */ |
1862 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button), |
1863 | "clicked"); |
1864 | gtk_widget_grab_focus(sc->uc->entry); |
1865 | break; |
1866 | case CTRL_RADIO: |
1867 | /* |
1868 | * Radio buttons are fun, because they have |
1869 | * multiple shortcuts. We must find whether the |
1870 | * activated shortcut is the shortcut for the whole |
1871 | * group, or for a particular button. In the former |
1872 | * case, we find the currently selected button and |
1873 | * focus it; in the latter, we focus-and-click the |
1874 | * button whose shortcut was pressed. |
1875 | */ |
1876 | if (schr == sc->uc->ctrl->radio.shortcut) { |
1877 | int i; |
1878 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
1879 | if (gtk_toggle_button_get_active |
1880 | (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) { |
1881 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
1882 | } |
1883 | } else if (sc->uc->ctrl->radio.shortcuts) { |
1884 | int i; |
1885 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
1886 | if (schr == sc->uc->ctrl->radio.shortcuts[i]) { |
1887 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
1888 | gtk_signal_emit_by_name |
1889 | (GTK_OBJECT(sc->uc->buttons[i]), "clicked"); |
1890 | } |
1891 | } |
1892 | break; |
1893 | case CTRL_LISTBOX: |
1894 | /* |
0bd8d76d |
1895 | * If the list is really an option menu, we focus |
1896 | * and click it. Otherwise we tell it to focus one |
1897 | * of its children, which appears to do the Right |
1898 | * Thing. |
1c291fcf |
1899 | */ |
1900 | if (sc->uc->optmenu) { |
1901 | GdkEventButton bev; |
1902 | gint returnval; |
1903 | |
1904 | gtk_widget_grab_focus(sc->uc->optmenu); |
1905 | /* Option menus don't work using the "clicked" signal. |
1906 | * We need to manufacture a button press event :-/ */ |
1907 | bev.type = GDK_BUTTON_PRESS; |
1908 | bev.button = 1; |
1909 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu), |
1910 | "button_press_event", |
1911 | &bev, &returnval); |
1912 | } else { |
0bd8d76d |
1913 | assert(sc->uc->list != NULL); |
1914 | |
1915 | gtk_container_focus(GTK_CONTAINER(sc->uc->list), |
1916 | GTK_DIR_TAB_FORWARD); |
1c291fcf |
1917 | } |
1918 | break; |
1919 | } |
1920 | break; |
1921 | } |
1922 | } |
1923 | |
1924 | return FALSE; |
1925 | } |
1926 | |
0bd8d76d |
1927 | int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
1928 | { |
1929 | struct dlgparam *dp = (struct dlgparam *)data; |
1930 | |
1931 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
1932 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down) { |
c5d64b49 |
1933 | int dir, i, j = -1; |
0bd8d76d |
1934 | for (i = 0; i < dp->ntreeitems; i++) |
c5d64b49 |
1935 | if (widget == dp->treeitems[i]) |
1936 | break; |
1937 | if (i < dp->ntreeitems) { |
1938 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) |
1939 | dir = -1; |
1940 | else |
1941 | dir = +1; |
1942 | |
1943 | while (1) { |
1944 | i += dir; |
1945 | if (i < 0 || i >= dp->ntreeitems) |
1946 | break; /* nothing in that dir to select */ |
1947 | /* |
1948 | * Determine if this tree item is visible. |
1949 | */ |
1950 | { |
1951 | GtkWidget *w = dp->treeitems[i]; |
1952 | int vis = TRUE; |
1d009ae7 |
1953 | while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) { |
c5d64b49 |
1954 | if (!GTK_WIDGET_VISIBLE(w)) { |
1955 | vis = FALSE; |
1956 | break; |
1957 | } |
1958 | w = w->parent; |
1959 | } |
1960 | if (vis) { |
1961 | j = i; /* got one */ |
1962 | break; |
1963 | } |
1964 | } |
1965 | } |
1966 | } |
0bd8d76d |
1967 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1968 | "key_press_event"); |
1969 | if (j >= 0) { |
0bd8d76d |
1970 | gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle"); |
1971 | gtk_widget_grab_focus(dp->treeitems[j]); |
1972 | } |
1973 | return TRUE; |
1974 | } |
1975 | |
0bd8d76d |
1976 | /* |
c5d64b49 |
1977 | * It's nice for Left and Right to expand and collapse tree |
1978 | * branches. |
0bd8d76d |
1979 | */ |
c5d64b49 |
1980 | if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) { |
1981 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1982 | "key_press_event"); |
1983 | gtk_tree_item_collapse(GTK_TREE_ITEM(widget)); |
1984 | return TRUE; |
1985 | } |
1986 | if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) { |
1987 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
1988 | "key_press_event"); |
1989 | gtk_tree_item_expand(GTK_TREE_ITEM(widget)); |
1990 | return TRUE; |
0bd8d76d |
1991 | } |
c5d64b49 |
1992 | |
1993 | return FALSE; |
0bd8d76d |
1994 | } |
1995 | |
aef05b78 |
1996 | static void shortcut_highlight(GtkWidget *labelw, int chr) |
1c291fcf |
1997 | { |
1998 | GtkLabel *label = GTK_LABEL(labelw); |
1999 | gchar *currstr, *pattern; |
2000 | int i; |
2001 | |
aef05b78 |
2002 | gtk_label_get(label, &currstr); |
2003 | for (i = 0; currstr[i]; i++) |
2004 | if (tolower((unsigned char)currstr[i]) == chr) { |
2005 | GtkRequisition req; |
2006 | |
2007 | pattern = dupprintf("%*s_", i, ""); |
2008 | |
2009 | gtk_widget_size_request(GTK_WIDGET(label), &req); |
2010 | gtk_label_set_pattern(label, pattern); |
2011 | gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height); |
2012 | |
2013 | sfree(pattern); |
2014 | break; |
2015 | } |
2016 | } |
2017 | |
2018 | void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
2019 | int chr, int action, void *ptr) |
2020 | { |
1c291fcf |
2021 | if (chr == NO_SHORTCUT) |
2022 | return; |
2023 | |
2024 | chr = tolower((unsigned char)chr); |
2025 | |
2026 | assert(scs->sc[chr].action == SHORTCUT_EMPTY); |
2027 | |
2028 | scs->sc[chr].action = action; |
2029 | |
2030 | if (action == SHORTCUT_FOCUS) { |
2031 | scs->sc[chr].uc = NULL; |
2032 | scs->sc[chr].widget = (GtkWidget *)ptr; |
2033 | } else { |
2034 | scs->sc[chr].widget = NULL; |
2035 | scs->sc[chr].uc = (struct uctrl *)ptr; |
2036 | } |
2037 | |
aef05b78 |
2038 | shortcut_highlight(labelw, chr); |
1c291fcf |
2039 | } |
2040 | |
8eed910d |
2041 | int get_listitemheight(void) |
2042 | { |
2043 | GtkWidget *listitem = gtk_list_item_new_with_label("foo"); |
2044 | GtkRequisition req; |
2045 | gtk_widget_size_request(listitem, &req); |
2046 | gtk_widget_unref(listitem); |
2047 | return req.height; |
2048 | } |
2049 | |
f89c3294 |
2050 | int do_config_box(const char *title, Config *cfg, int midsession, |
2051 | int protcfginfo) |
d9b15094 |
2052 | { |
2053 | GtkWidget *window, *hbox, *vbox, *cols, *label, |
2054 | *tree, *treescroll, *panels, *panelvbox; |
1d0d4a3b |
2055 | int index, level, listitemheight; |
d9b15094 |
2056 | struct controlbox *ctrlbox; |
2057 | char *path; |
2058 | GtkTreeItem *treeitemlevels[8]; |
2059 | GtkTree *treelevels[8]; |
0f5e1f75 |
2060 | struct dlgparam dp; |
1c291fcf |
2061 | struct Shortcuts scs; |
d9b15094 |
2062 | |
2063 | struct selparam *selparams = NULL; |
2064 | int nselparams = 0, selparamsize = 0; |
2065 | |
0f5e1f75 |
2066 | dlg_init(&dp); |
2067 | |
8eed910d |
2068 | listitemheight = get_listitemheight(); |
2069 | |
1c291fcf |
2070 | for (index = 0; index < lenof(scs.sc); index++) { |
2071 | scs.sc[index].action = SHORTCUT_EMPTY; |
2072 | } |
2073 | |
7718480a |
2074 | window = gtk_dialog_new(); |
2075 | |
d9b15094 |
2076 | ctrlbox = ctrl_new_box(); |
12745e35 |
2077 | setup_config_box(ctrlbox, midsession, cfg->protocol, protcfginfo); |
8a7f1392 |
2078 | unix_setup_config_box(ctrlbox, midsession, cfg->protocol); |
8d68f26a |
2079 | gtk_setup_config_box(ctrlbox, midsession, window); |
d9b15094 |
2080 | |
c5d64b49 |
2081 | gtk_window_set_title(GTK_WINDOW(window), title); |
d9b15094 |
2082 | hbox = gtk_hbox_new(FALSE, 4); |
2083 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0); |
1d0d4a3b |
2084 | gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
d9b15094 |
2085 | gtk_widget_show(hbox); |
2086 | vbox = gtk_vbox_new(FALSE, 4); |
2087 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
2088 | gtk_widget_show(vbox); |
2089 | cols = columns_new(4); |
2090 | gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0); |
2091 | gtk_widget_show(cols); |
2092 | label = gtk_label_new("Category:"); |
2093 | columns_add(COLUMNS(cols), label, 0, 1); |
2094 | columns_force_left_align(COLUMNS(cols), label); |
2095 | gtk_widget_show(label); |
2096 | treescroll = gtk_scrolled_window_new(NULL, NULL); |
2097 | tree = gtk_tree_new(); |
0bd8d76d |
2098 | gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event", |
2099 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
c5d64b49 |
2100 | shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree); |
d9b15094 |
2101 | gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM); |
1d0d4a3b |
2102 | gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE); |
0bd8d76d |
2103 | gtk_signal_connect(GTK_OBJECT(tree), "focus", |
2104 | GTK_SIGNAL_FUNC(tree_focus), &dp); |
d9b15094 |
2105 | gtk_widget_show(treescroll); |
2106 | gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0); |
d5410af8 |
2107 | panels = gtk_notebook_new(); |
2108 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(panels), FALSE); |
2109 | gtk_notebook_set_show_border(GTK_NOTEBOOK(panels), FALSE); |
d9b15094 |
2110 | gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0); |
2111 | gtk_widget_show(panels); |
2112 | |
2113 | panelvbox = NULL; |
2114 | path = NULL; |
2115 | level = 0; |
2116 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
2117 | struct controlset *s = ctrlbox->ctrlsets[index]; |
1c291fcf |
2118 | GtkWidget *w; |
d9b15094 |
2119 | |
2120 | if (!*s->pathname) { |
1c291fcf |
2121 | w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window)); |
d9b15094 |
2122 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
2123 | w, TRUE, TRUE, 0); |
2124 | } else { |
2125 | int j = path ? ctrl_path_compare(s->pathname, path) : 0; |
2126 | if (j != INT_MAX) { /* add to treeview, start new panel */ |
2127 | char *c; |
2128 | GtkWidget *treeitem; |
2129 | int first; |
2130 | |
2131 | /* |
2132 | * We expect never to find an implicit path |
2133 | * component. For example, we expect never to see |
2134 | * A/B/C followed by A/D/E, because that would |
2135 | * _implicitly_ create A/D. All our path prefixes |
2136 | * are expected to contain actual controls and be |
2137 | * selectable in the treeview; so we would expect |
2138 | * to see A/D _explicitly_ before encountering |
2139 | * A/D/E. |
2140 | */ |
2141 | assert(j == ctrl_path_elements(s->pathname) - 1); |
2142 | |
2143 | c = strrchr(s->pathname, '/'); |
2144 | if (!c) |
2145 | c = s->pathname; |
2146 | else |
2147 | c++; |
2148 | |
2149 | treeitem = gtk_tree_item_new_with_label(c); |
2150 | assert(j-1 < level); |
2151 | if (j > 0) { |
2152 | if (!treelevels[j-1]) { |
2153 | treelevels[j-1] = GTK_TREE(gtk_tree_new()); |
2154 | gtk_tree_item_set_subtree |
2155 | (treeitemlevels[j-1], |
2156 | GTK_WIDGET(treelevels[j-1])); |
d2c8d274 |
2157 | if (j < 2) |
2158 | gtk_tree_item_expand(treeitemlevels[j-1]); |
2159 | else |
2160 | gtk_tree_item_collapse(treeitemlevels[j-1]); |
d9b15094 |
2161 | } |
2162 | gtk_tree_append(treelevels[j-1], treeitem); |
2163 | } else { |
2164 | gtk_tree_append(GTK_TREE(tree), treeitem); |
2165 | } |
2166 | treeitemlevels[j] = GTK_TREE_ITEM(treeitem); |
2167 | treelevels[j] = NULL; |
2168 | level = j+1; |
2169 | |
0bd8d76d |
2170 | gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event", |
2171 | GTK_SIGNAL_FUNC(tree_key_press), &dp); |
2172 | gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event", |
2173 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
2174 | |
d9b15094 |
2175 | gtk_widget_show(treeitem); |
2176 | |
2177 | path = s->pathname; |
2178 | |
2179 | first = (panelvbox == NULL); |
2180 | |
2181 | panelvbox = gtk_vbox_new(FALSE, 4); |
d5410af8 |
2182 | gtk_widget_show(panelvbox); |
2183 | gtk_notebook_append_page(GTK_NOTEBOOK(panels), panelvbox, |
2184 | NULL); |
d9b15094 |
2185 | if (first) { |
d5410af8 |
2186 | gint page_num; |
2187 | |
2188 | page_num = gtk_notebook_page_num(GTK_NOTEBOOK(panels), |
2189 | panelvbox); |
2190 | gtk_notebook_set_page(GTK_NOTEBOOK(panels), page_num); |
d9b15094 |
2191 | gtk_tree_select_child(GTK_TREE(tree), treeitem); |
2192 | } |
2193 | |
2194 | if (nselparams >= selparamsize) { |
2195 | selparamsize += 16; |
3d88e64d |
2196 | selparams = sresize(selparams, selparamsize, |
2197 | struct selparam); |
d9b15094 |
2198 | } |
1c291fcf |
2199 | selparams[nselparams].dp = &dp; |
d5410af8 |
2200 | selparams[nselparams].panels = GTK_NOTEBOOK(panels); |
d9b15094 |
2201 | selparams[nselparams].panel = panelvbox; |
1c291fcf |
2202 | selparams[nselparams].shortcuts = scs; /* structure copy */ |
d9b15094 |
2203 | selparams[nselparams].treeitem = treeitem; |
2204 | nselparams++; |
2205 | |
2206 | } |
2207 | |
1c291fcf |
2208 | w = layout_ctrls(&dp, |
2209 | &selparams[nselparams-1].shortcuts, |
2210 | s, listitemheight, NULL); |
d9b15094 |
2211 | gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0); |
1d0d4a3b |
2212 | gtk_widget_show(w); |
d9b15094 |
2213 | } |
2214 | } |
2215 | |
0bd8d76d |
2216 | dp.ntreeitems = nselparams; |
3d88e64d |
2217 | dp.treeitems = snewn(dp.ntreeitems, GtkWidget *); |
0bd8d76d |
2218 | |
d9b15094 |
2219 | for (index = 0; index < nselparams; index++) { |
2220 | gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select", |
2221 | GTK_SIGNAL_FUNC(treeitem_sel), |
2222 | &selparams[index]); |
0bd8d76d |
2223 | dp.treeitems[index] = selparams[index].treeitem; |
d9b15094 |
2224 | } |
2225 | |
1d009ae7 |
2226 | dp.data = cfg; |
0f5e1f75 |
2227 | dlg_refresh(NULL, &dp); |
2228 | |
1c291fcf |
2229 | dp.shortcuts = &selparams[0].shortcuts; |
0bd8d76d |
2230 | dp.currtreeitem = dp.treeitems[0]; |
2231 | dp.lastfocus = NULL; |
2232 | dp.retval = 0; |
23897ed0 |
2233 | dp.window = window; |
1c291fcf |
2234 | |
85b1c49f |
2235 | { |
2236 | /* in gtkwin.c */ |
2237 | extern void set_window_icon(GtkWidget *window, |
2238 | const char *const *const *icon, |
2239 | int n_icon); |
2240 | extern const char *const *const cfg_icon[]; |
2241 | extern const int n_cfg_icon; |
2242 | set_window_icon(window, cfg_icon, n_cfg_icon); |
2243 | } |
2244 | |
d2c8d274 |
2245 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll), |
2246 | tree); |
2247 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll), |
2248 | GTK_POLICY_NEVER, |
2249 | GTK_POLICY_AUTOMATIC); |
2250 | gtk_widget_show(tree); |
2251 | |
23897ed0 |
2252 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
d9b15094 |
2253 | gtk_widget_show(window); |
2254 | |
0bd8d76d |
2255 | /* |
2256 | * Set focus into the first available control. |
2257 | */ |
2258 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
2259 | struct controlset *s = ctrlbox->ctrlsets[index]; |
2260 | int done = 0; |
2261 | int j; |
2262 | |
2263 | if (*s->pathname) { |
2264 | for (j = 0; j < s->ncontrols; j++) |
2265 | if (s->ctrls[j]->generic.type != CTRL_TABDELAY && |
2266 | s->ctrls[j]->generic.type != CTRL_COLUMNS && |
2267 | s->ctrls[j]->generic.type != CTRL_TEXT) { |
2268 | dlg_set_focus(s->ctrls[j], &dp); |
2269 | dp.lastfocus = s->ctrls[j]; |
2270 | done = 1; |
2271 | break; |
2272 | } |
2273 | } |
2274 | if (done) |
2275 | break; |
2276 | } |
2277 | |
d9b15094 |
2278 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
0bd8d76d |
2279 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
1c291fcf |
2280 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
2281 | GTK_SIGNAL_FUNC(win_key_press), &dp); |
d9b15094 |
2282 | |
2283 | gtk_main(); |
2284 | |
0f5e1f75 |
2285 | dlg_cleanup(&dp); |
d9b15094 |
2286 | sfree(selparams); |
0bd8d76d |
2287 | |
2288 | return dp.retval; |
d9b15094 |
2289 | } |
2290 | |
5bf9955d |
2291 | static void messagebox_handler(union control *ctrl, void *dlg, |
2292 | void *data, int event) |
2293 | { |
2294 | if (event == EVENT_ACTION) |
2295 | dlg_end(dlg, ctrl->generic.context.i); |
2296 | } |
2297 | int messagebox(GtkWidget *parentwin, char *title, char *msg, int minwid, ...) |
2298 | { |
2299 | GtkWidget *window, *w0, *w1; |
2300 | struct controlbox *ctrlbox; |
2301 | struct controlset *s0, *s1; |
2302 | union control *c; |
2303 | struct dlgparam dp; |
2304 | struct Shortcuts scs; |
2305 | int index, ncols; |
2306 | va_list ap; |
d9b15094 |
2307 | |
5bf9955d |
2308 | dlg_init(&dp); |
d9b15094 |
2309 | |
5bf9955d |
2310 | for (index = 0; index < lenof(scs.sc); index++) { |
2311 | scs.sc[index].action = SHORTCUT_EMPTY; |
2312 | } |
d9b15094 |
2313 | |
5bf9955d |
2314 | ctrlbox = ctrl_new_box(); |
c5d64b49 |
2315 | |
5bf9955d |
2316 | ncols = 0; |
2317 | va_start(ap, minwid); |
2318 | while (va_arg(ap, char *) != NULL) { |
2319 | ncols++; |
2320 | (void) va_arg(ap, int); /* shortcut */ |
2321 | (void) va_arg(ap, int); /* normal/default/cancel */ |
2322 | (void) va_arg(ap, int); /* end value */ |
2323 | } |
2324 | va_end(ap); |
d9b15094 |
2325 | |
5bf9955d |
2326 | s0 = ctrl_getset(ctrlbox, "", "", ""); |
2327 | c = ctrl_columns(s0, 2, 50, 50); |
2328 | c->columns.ncols = s0->ncolumns = ncols; |
2329 | c->columns.percentages = sresize(c->columns.percentages, ncols, int); |
2330 | for (index = 0; index < ncols; index++) |
2331 | c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols; |
2332 | va_start(ap, minwid); |
2333 | index = 0; |
2334 | while (1) { |
2335 | char *title = va_arg(ap, char *); |
2336 | int shortcut, type, value; |
2337 | if (title == NULL) |
2338 | break; |
2339 | shortcut = va_arg(ap, int); |
2340 | type = va_arg(ap, int); |
2341 | value = va_arg(ap, int); |
2342 | c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help), |
2343 | messagebox_handler, I(value)); |
2344 | c->generic.column = index++; |
2345 | if (type > 0) |
2346 | c->button.isdefault = TRUE; |
2347 | else if (type < 0) |
2348 | c->button.iscancel = TRUE; |
2349 | } |
d9b15094 |
2350 | va_end(ap); |
d9b15094 |
2351 | |
5bf9955d |
2352 | s1 = ctrl_getset(ctrlbox, "x", "", ""); |
2353 | ctrl_text(s1, msg, HELPCTX(no_help)); |
d9b15094 |
2354 | |
5bf9955d |
2355 | window = gtk_dialog_new(); |
2356 | gtk_window_set_title(GTK_WINDOW(window), title); |
2357 | w0 = layout_ctrls(&dp, &scs, s0, 0, GTK_WINDOW(window)); |
2358 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
2359 | w0, TRUE, TRUE, 0); |
2360 | gtk_widget_show(w0); |
2361 | w1 = layout_ctrls(&dp, &scs, s1, 0, GTK_WINDOW(window)); |
2362 | gtk_container_set_border_width(GTK_CONTAINER(w1), 10); |
2363 | gtk_widget_set_usize(w1, minwid+20, -1); |
2364 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
2365 | w1, TRUE, TRUE, 0); |
2366 | gtk_widget_show(w1); |
d9b15094 |
2367 | |
5bf9955d |
2368 | dp.shortcuts = &scs; |
2369 | dp.lastfocus = NULL; |
2370 | dp.retval = 0; |
2371 | dp.window = window; |
d9b15094 |
2372 | |
5bf9955d |
2373 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
2374 | if (parentwin) { |
7718480a |
2375 | set_transient_window_pos(parentwin, window); |
5bf9955d |
2376 | gtk_window_set_transient_for(GTK_WINDOW(window), |
2377 | GTK_WINDOW(parentwin)); |
2378 | } else |
2379 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
2380 | gtk_widget_show(window); |
d9b15094 |
2381 | |
5bf9955d |
2382 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
2383 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
2384 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
2385 | GTK_SIGNAL_FUNC(win_key_press), &dp); |
d9b15094 |
2386 | |
5bf9955d |
2387 | gtk_main(); |
2388 | |
2389 | dlg_cleanup(&dp); |
2390 | ctrl_free_box(ctrlbox); |
2391 | |
2392 | return dp.retval; |
d9b15094 |
2393 | } |
2394 | |
5bf9955d |
2395 | static int string_width(char *text) |
2396 | { |
2397 | GtkWidget *label = gtk_label_new(text); |
2398 | GtkRequisition req; |
2399 | gtk_widget_size_request(label, &req); |
2400 | gtk_widget_unref(label); |
2401 | return req.width; |
2402 | } |
2403 | |
abb6895f |
2404 | int reallyclose(void *frontend) |
2405 | { |
2406 | char *title = dupcat(appname, " Exit Confirmation", NULL); |
2407 | int ret = messagebox(GTK_WIDGET(get_window(frontend)), |
2408 | title, "Are you sure you want to close this session?", |
2409 | string_width("Most of the width of the above text"), |
2410 | "Yes", 'y', +1, 1, |
2411 | "No", 'n', -1, 0, |
2412 | NULL); |
2413 | sfree(title); |
2414 | return ret; |
2415 | } |
2416 | |
3d9449a1 |
2417 | int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype, |
2418 | char *keystr, char *fingerprint, |
2419 | void (*callback)(void *ctx, int result), void *ctx) |
5bf9955d |
2420 | { |
2421 | static const char absenttxt[] = |
2422 | "The server's host key is not cached. You have no guarantee " |
2423 | "that the server is the computer you think it is.\n" |
65f66c88 |
2424 | "The server's %s key fingerprint is:\n" |
5bf9955d |
2425 | "%s\n" |
2426 | "If you trust this host, press \"Accept\" to add the key to " |
2427 | "PuTTY's cache and carry on connecting.\n" |
2428 | "If you want to carry on connecting just once, without " |
2429 | "adding the key to the cache, press \"Connect Once\".\n" |
2430 | "If you do not trust this host, press \"Cancel\" to abandon the " |
2431 | "connection."; |
2432 | static const char wrongtxt[] = |
2433 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
2434 | "The server's host key does not match the one PuTTY has " |
2435 | "cached. This means that either the server administrator " |
2436 | "has changed the host key, or you have actually connected " |
2437 | "to another computer pretending to be the server.\n" |
65f66c88 |
2438 | "The new %s key fingerprint is:\n" |
5bf9955d |
2439 | "%s\n" |
2440 | "If you were expecting this change and trust the new key, " |
2441 | "press \"Accept\" to update PuTTY's cache and continue connecting.\n" |
2442 | "If you want to carry on connecting but without updating " |
2443 | "the cache, press \"Connect Once\".\n" |
2444 | "If you want to abandon the connection completely, press " |
2445 | "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed " |
2446 | "safe choice."; |
2447 | char *text; |
2448 | int ret; |
2449 | |
2450 | /* |
2451 | * Verify the key. |
2452 | */ |
2453 | ret = verify_host_key(host, port, keytype, keystr); |
2454 | |
2455 | if (ret == 0) /* success - key matched OK */ |
3d9449a1 |
2456 | return 1; |
5bf9955d |
2457 | |
65f66c88 |
2458 | text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint); |
5bf9955d |
2459 | |
2460 | ret = messagebox(GTK_WIDGET(get_window(frontend)), |
2461 | "PuTTY Security Alert", text, |
2462 | string_width(fingerprint), |
2463 | "Accept", 'a', 0, 2, |
2464 | "Connect Once", 'o', 0, 1, |
2465 | "Cancel", 'c', -1, 0, |
2466 | NULL); |
2467 | |
2468 | sfree(text); |
2469 | |
3686109d |
2470 | if (ret == 2) { |
2471 | store_host_key(host, port, keytype, keystr); |
2472 | return 1; /* continue with connection */ |
2473 | } else if (ret == 1) |
2474 | return 1; /* continue with connection */ |
2475 | return 0; /* do not continue with connection */ |
d9b15094 |
2476 | } |
2477 | |
5bf9955d |
2478 | /* |
83e7d008 |
2479 | * Ask whether the selected algorithm is acceptable (since it was |
5bf9955d |
2480 | * below the configured 'warn' threshold). |
5bf9955d |
2481 | */ |
3d9449a1 |
2482 | int askalg(void *frontend, const char *algtype, const char *algname, |
2483 | void (*callback)(void *ctx, int result), void *ctx) |
5bf9955d |
2484 | { |
2485 | static const char msg[] = |
83e7d008 |
2486 | "The first %s supported by the server is " |
5bf9955d |
2487 | "%s, which is below the configured warning threshold.\n" |
2488 | "Continue with connection?"; |
2489 | char *text; |
2490 | int ret; |
2491 | |
83e7d008 |
2492 | text = dupprintf(msg, algtype, algname); |
5bf9955d |
2493 | ret = messagebox(GTK_WIDGET(get_window(frontend)), |
2494 | "PuTTY Security Alert", text, |
2495 | string_width("Continue with connection?"), |
2496 | "Yes", 'y', 0, 1, |
2497 | "No", 'n', 0, 0, |
2498 | NULL); |
2499 | sfree(text); |
2500 | |
2501 | if (ret) { |
3d9449a1 |
2502 | return 1; |
5bf9955d |
2503 | } else { |
3d9449a1 |
2504 | return 0; |
5bf9955d |
2505 | } |
d9b15094 |
2506 | } |
2507 | |
5bf9955d |
2508 | void old_keyfile_warning(void) |
d9b15094 |
2509 | { |
5bf9955d |
2510 | /* |
2511 | * This should never happen on Unix. We hope. |
2512 | */ |
d9b15094 |
2513 | } |
2514 | |
3f935d5b |
2515 | void fatal_message_box(void *window, char *msg) |
d9b15094 |
2516 | { |
3f935d5b |
2517 | messagebox(window, "PuTTY Fatal Error", msg, |
5bf9955d |
2518 | string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"), |
2519 | "OK", 'o', 1, 1, NULL); |
d9b15094 |
2520 | } |
3f935d5b |
2521 | |
2522 | void fatalbox(char *p, ...) |
d9b15094 |
2523 | { |
5bf9955d |
2524 | va_list ap; |
2525 | char *msg; |
2526 | va_start(ap, p); |
2527 | msg = dupvprintf(p, ap); |
2528 | va_end(ap); |
3f935d5b |
2529 | fatal_message_box(NULL, msg); |
5bf9955d |
2530 | sfree(msg); |
2531 | cleanup_exit(1); |
d9b15094 |
2532 | } |
47e4e735 |
2533 | |
2534 | static GtkWidget *aboutbox = NULL; |
2535 | |
2536 | static void about_close_clicked(GtkButton *button, gpointer data) |
2537 | { |
2538 | gtk_widget_destroy(aboutbox); |
2539 | aboutbox = NULL; |
2540 | } |
2541 | |
2542 | static void licence_clicked(GtkButton *button, gpointer data) |
2543 | { |
2544 | char *title; |
2545 | |
2546 | char *licence = |
24abf4f1 |
2547 | "Copyright 1997-2008 Simon Tatham.\n\n" |
47e4e735 |
2548 | |
2549 | "Portions copyright Robert de Bath, Joris van Rantwijk, Delian " |
2550 | "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas " |
f116042a |
2551 | "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, " |
2552 | "Markus Kuhn, and CORE SDI S.A.\n\n" |
47e4e735 |
2553 | |
2554 | "Permission is hereby granted, free of charge, to any person " |
2555 | "obtaining a copy of this software and associated documentation " |
2556 | "files (the ""Software""), to deal in the Software without restriction, " |
2557 | "including without limitation the rights to use, copy, modify, merge, " |
2558 | "publish, distribute, sublicense, and/or sell copies of the Software, " |
2559 | "and to permit persons to whom the Software is furnished to do so, " |
2560 | "subject to the following conditions:\n\n" |
2561 | |
2562 | "The above copyright notice and this permission notice shall be " |
2563 | "included in all copies or substantial portions of the Software.\n\n" |
2564 | |
2565 | "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT " |
2566 | "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, " |
2567 | "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " |
2568 | "MERCHANTABILITY, FITNESS FOR A PARTICULAR " |
2569 | "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE " |
2570 | "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES " |
2571 | "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, " |
2572 | "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN " |
2573 | "CONNECTION WITH THE SOFTWARE OR THE USE OR " |
2574 | "OTHER DEALINGS IN THE SOFTWARE."; |
2575 | |
2576 | title = dupcat(appname, " Licence", NULL); |
2577 | assert(aboutbox != NULL); |
2578 | messagebox(aboutbox, title, licence, |
2579 | string_width("LONGISH LINE OF TEXT SO THE LICENCE" |
2580 | " BOX ISN'T EXCESSIVELY TALL AND THIN"), |
2581 | "OK", 'o', 1, 1, NULL); |
2582 | sfree(title); |
2583 | } |
2584 | |
7718480a |
2585 | void about_box(void *window) |
47e4e735 |
2586 | { |
2587 | GtkWidget *w; |
2588 | char *title; |
2589 | |
2590 | if (aboutbox) { |
2591 | gtk_widget_grab_focus(aboutbox); |
2592 | return; |
2593 | } |
2594 | |
2595 | aboutbox = gtk_dialog_new(); |
2596 | gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10); |
2597 | title = dupcat("About ", appname, NULL); |
2598 | gtk_window_set_title(GTK_WINDOW(aboutbox), title); |
2599 | sfree(title); |
2600 | |
2601 | w = gtk_button_new_with_label("Close"); |
2602 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
2603 | gtk_window_set_default(GTK_WINDOW(aboutbox), w); |
2604 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area), |
2605 | w, FALSE, FALSE, 0); |
2606 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
2607 | GTK_SIGNAL_FUNC(about_close_clicked), NULL); |
2608 | gtk_widget_show(w); |
2609 | |
2610 | w = gtk_button_new_with_label("View Licence"); |
2611 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
2612 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area), |
2613 | w, FALSE, FALSE, 0); |
2614 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
2615 | GTK_SIGNAL_FUNC(licence_clicked), NULL); |
2616 | gtk_widget_show(w); |
2617 | |
2618 | w = gtk_label_new(appname); |
2619 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox), |
2620 | w, FALSE, FALSE, 0); |
2621 | gtk_widget_show(w); |
2622 | |
2623 | w = gtk_label_new(ver); |
2624 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox), |
2625 | w, FALSE, FALSE, 5); |
2626 | gtk_widget_show(w); |
2627 | |
24abf4f1 |
2628 | w = gtk_label_new("Copyright 1997-2008 Simon Tatham. All rights reserved"); |
47e4e735 |
2629 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox), |
2630 | w, FALSE, FALSE, 5); |
2631 | gtk_widget_show(w); |
2632 | |
7718480a |
2633 | set_transient_window_pos(GTK_WIDGET(window), aboutbox); |
47e4e735 |
2634 | gtk_widget_show(aboutbox); |
2635 | } |
8eed910d |
2636 | |
2637 | struct eventlog_stuff { |
2638 | GtkWidget *parentwin, *window; |
2639 | struct controlbox *eventbox; |
2640 | struct Shortcuts scs; |
2641 | struct dlgparam dp; |
2642 | union control *listctrl; |
2643 | char **events; |
2644 | int nevents, negsize; |
8c161662 |
2645 | char *seldata; |
2646 | int sellen; |
2647 | int ignore_selchange; |
8eed910d |
2648 | }; |
2649 | |
2650 | static void eventlog_destroy(GtkWidget *widget, gpointer data) |
2651 | { |
2652 | struct eventlog_stuff *es = (struct eventlog_stuff *)data; |
2653 | |
2654 | es->window = NULL; |
8c161662 |
2655 | sfree(es->seldata); |
8eed910d |
2656 | dlg_cleanup(&es->dp); |
2657 | ctrl_free_box(es->eventbox); |
2658 | } |
2659 | static void eventlog_ok_handler(union control *ctrl, void *dlg, |
2660 | void *data, int event) |
2661 | { |
2662 | if (event == EVENT_ACTION) |
2663 | dlg_end(dlg, 0); |
2664 | } |
2665 | static void eventlog_list_handler(union control *ctrl, void *dlg, |
2666 | void *data, int event) |
2667 | { |
2668 | struct eventlog_stuff *es = (struct eventlog_stuff *)data; |
2669 | |
2670 | if (event == EVENT_REFRESH) { |
2671 | int i; |
2672 | |
2673 | dlg_update_start(ctrl, dlg); |
2674 | dlg_listbox_clear(ctrl, dlg); |
2675 | for (i = 0; i < es->nevents; i++) { |
2676 | dlg_listbox_add(ctrl, dlg, es->events[i]); |
2677 | } |
2678 | dlg_update_done(ctrl, dlg); |
8c161662 |
2679 | } else if (event == EVENT_SELCHANGE) { |
2680 | int i; |
2681 | int selsize = 0; |
2682 | |
2683 | /* |
2684 | * If this SELCHANGE event is happening as a result of |
2685 | * deliberate deselection because someone else has grabbed |
2686 | * the selection, the last thing we want to do is pre-empt |
2687 | * them. |
2688 | */ |
2689 | if (es->ignore_selchange) |
2690 | return; |
2691 | |
2692 | /* |
2693 | * Construct the data to use as the selection. |
2694 | */ |
2695 | sfree(es->seldata); |
2696 | es->seldata = NULL; |
2697 | es->sellen = 0; |
2698 | for (i = 0; i < es->nevents; i++) { |
2699 | if (dlg_listbox_issel(ctrl, dlg, i)) { |
2700 | int extralen = strlen(es->events[i]); |
2701 | |
2702 | if (es->sellen + extralen + 2 > selsize) { |
2703 | selsize = es->sellen + extralen + 512; |
2704 | es->seldata = sresize(es->seldata, selsize, char); |
2705 | } |
2706 | |
2707 | strcpy(es->seldata + es->sellen, es->events[i]); |
2708 | es->sellen += extralen; |
2709 | es->seldata[es->sellen++] = '\n'; |
2710 | } |
2711 | } |
2712 | |
2713 | if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY, |
2714 | GDK_CURRENT_TIME)) { |
2715 | extern GdkAtom compound_text_atom; |
2716 | |
2717 | gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY, |
2718 | GDK_SELECTION_TYPE_STRING, 1); |
2719 | gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY, |
2720 | compound_text_atom, 1); |
2721 | } |
2722 | |
8eed910d |
2723 | } |
2724 | } |
8c161662 |
2725 | |
2726 | void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata, |
2727 | guint info, guint time_stamp, gpointer data) |
2728 | { |
2729 | struct eventlog_stuff *es = (struct eventlog_stuff *)data; |
2730 | |
2731 | gtk_selection_data_set(seldata, seldata->target, 8, |
8a9977e5 |
2732 | (unsigned char *)es->seldata, es->sellen); |
8c161662 |
2733 | } |
2734 | |
2735 | gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata, |
2736 | gpointer data) |
2737 | { |
2738 | struct eventlog_stuff *es = (struct eventlog_stuff *)data; |
2739 | struct uctrl *uc; |
2740 | |
2741 | /* |
2742 | * Deselect everything in the list box. |
2743 | */ |
2744 | uc = dlg_find_byctrl(&es->dp, es->listctrl); |
2745 | es->ignore_selchange = 1; |
2746 | gtk_list_unselect_all(GTK_LIST(uc->list)); |
2747 | es->ignore_selchange = 0; |
2748 | |
2749 | sfree(es->seldata); |
2750 | es->sellen = 0; |
2751 | es->seldata = NULL; |
2752 | return TRUE; |
2753 | } |
2754 | |
8eed910d |
2755 | void showeventlog(void *estuff, void *parentwin) |
2756 | { |
2757 | struct eventlog_stuff *es = (struct eventlog_stuff *)estuff; |
2758 | GtkWidget *window, *w0, *w1; |
2759 | GtkWidget *parent = GTK_WIDGET(parentwin); |
2760 | struct controlset *s0, *s1; |
2761 | union control *c; |
2762 | int listitemheight, index; |
2763 | char *title; |
2764 | |
2765 | if (es->window) { |
2766 | gtk_widget_grab_focus(es->window); |
2767 | return; |
2768 | } |
2769 | |
2770 | dlg_init(&es->dp); |
2771 | |
2772 | for (index = 0; index < lenof(es->scs.sc); index++) { |
2773 | es->scs.sc[index].action = SHORTCUT_EMPTY; |
2774 | } |
2775 | |
2776 | es->eventbox = ctrl_new_box(); |
2777 | |
2778 | s0 = ctrl_getset(es->eventbox, "", "", ""); |
2779 | ctrl_columns(s0, 3, 33, 34, 33); |
2780 | c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help), |
2781 | eventlog_ok_handler, P(NULL)); |
2782 | c->button.column = 1; |
2783 | c->button.isdefault = TRUE; |
2784 | |
2785 | s1 = ctrl_getset(es->eventbox, "x", "", ""); |
2786 | es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help), |
2787 | eventlog_list_handler, P(es)); |
2788 | c->listbox.height = 10; |
2789 | c->listbox.multisel = 2; |
2790 | c->listbox.ncols = 3; |
2791 | c->listbox.percentages = snewn(3, int); |
2792 | c->listbox.percentages[0] = 25; |
2793 | c->listbox.percentages[1] = 10; |
2794 | c->listbox.percentages[2] = 65; |
2795 | |
2796 | listitemheight = get_listitemheight(); |
2797 | |
2798 | es->window = window = gtk_dialog_new(); |
2799 | title = dupcat(appname, " Event Log", NULL); |
2800 | gtk_window_set_title(GTK_WINDOW(window), title); |
2801 | sfree(title); |
2802 | w0 = layout_ctrls(&es->dp, &es->scs, s0, |
2803 | listitemheight, GTK_WINDOW(window)); |
2804 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
2805 | w0, TRUE, TRUE, 0); |
2806 | gtk_widget_show(w0); |
2807 | w1 = layout_ctrls(&es->dp, &es->scs, s1, |
2808 | listitemheight, GTK_WINDOW(window)); |
2809 | gtk_container_set_border_width(GTK_CONTAINER(w1), 10); |
2810 | gtk_widget_set_usize(w1, 20 + |
2811 | string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG" |
2812 | " IS QUITE LONG 'COS SSH LOG ENTRIES" |
2813 | " ARE WIDE"), -1); |
2814 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
2815 | w1, TRUE, TRUE, 0); |
2816 | gtk_widget_show(w1); |
2817 | |
2818 | es->dp.data = es; |
2819 | es->dp.shortcuts = &es->scs; |
2820 | es->dp.lastfocus = NULL; |
2821 | es->dp.retval = 0; |
2822 | es->dp.window = window; |
2823 | |
2824 | dlg_refresh(NULL, &es->dp); |
2825 | |
2826 | if (parent) { |
7718480a |
2827 | set_transient_window_pos(parent, window); |
8eed910d |
2828 | gtk_window_set_transient_for(GTK_WINDOW(window), |
2829 | GTK_WINDOW(parent)); |
2830 | } else |
2831 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
2832 | gtk_widget_show(window); |
2833 | |
2834 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
2835 | GTK_SIGNAL_FUNC(eventlog_destroy), es); |
2836 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
2837 | GTK_SIGNAL_FUNC(win_key_press), &es->dp); |
8c161662 |
2838 | gtk_signal_connect(GTK_OBJECT(window), "selection_get", |
2839 | GTK_SIGNAL_FUNC(eventlog_selection_get), es); |
2840 | gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event", |
2841 | GTK_SIGNAL_FUNC(eventlog_selection_clear), es); |
8eed910d |
2842 | } |
2843 | |
2844 | void *eventlogstuff_new(void) |
2845 | { |
2846 | struct eventlog_stuff *es; |
2847 | es = snew(struct eventlog_stuff); |
2848 | memset(es, 0, sizeof(*es)); |
2849 | return es; |
2850 | } |
2851 | |
cbe2d68f |
2852 | void logevent_dlg(void *estuff, const char *string) |
8eed910d |
2853 | { |
2854 | struct eventlog_stuff *es = (struct eventlog_stuff *)estuff; |
2855 | |
2856 | char timebuf[40]; |
aca589d9 |
2857 | struct tm tm; |
8eed910d |
2858 | |
2859 | if (es->nevents >= es->negsize) { |
2860 | es->negsize += 64; |
2861 | es->events = sresize(es->events, es->negsize, char *); |
2862 | } |
2863 | |
aca589d9 |
2864 | tm=ltime(); |
2865 | strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm); |
8eed910d |
2866 | |
2867 | es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char); |
2868 | strcpy(es->events[es->nevents], timebuf); |
2869 | strcat(es->events[es->nevents], string); |
2870 | if (es->window) { |
2871 | dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]); |
2872 | } |
2873 | es->nevents++; |
2874 | } |
e3acd29b |
2875 | |
919baedb |
2876 | int askappend(void *frontend, Filename filename, |
2877 | void (*callback)(void *ctx, int result), void *ctx) |
e3acd29b |
2878 | { |
2879 | static const char msgtemplate[] = |
2880 | "The session log file \"%.*s\" already exists. " |
2881 | "You can overwrite it with a new session log, " |
2882 | "append your session log to the end of it, " |
2883 | "or disable session logging for this session."; |
2884 | char *message; |
2885 | char *mbtitle; |
2886 | int mbret; |
2887 | |
2888 | message = dupprintf(msgtemplate, FILENAME_MAX, filename.path); |
2889 | mbtitle = dupprintf("%s Log to File", appname); |
2890 | |
2891 | mbret = messagebox(get_window(frontend), mbtitle, message, |
2892 | string_width("LINE OF TEXT SUITABLE FOR THE" |
2893 | " ASKAPPEND WIDTH"), |
2894 | "Overwrite", 'o', 1, 2, |
2895 | "Append", 'a', 0, 1, |
2896 | "Disable", 'd', -1, 0, |
2897 | NULL); |
2898 | |
2899 | sfree(message); |
2900 | sfree(mbtitle); |
2901 | |
2902 | return mbret; |
2903 | } |