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