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