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