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