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