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