| 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 <ctype.h> |
| 16 | #include <gtk/gtk.h> |
| 17 | #include <gdk/gdkkeysyms.h> |
| 18 | #include <gdk/gdkx.h> |
| 19 | #include <X11/Xlib.h> |
| 20 | #include <X11/Xutil.h> |
| 21 | |
| 22 | #include "gtkcols.h" |
| 23 | #include "gtkpanel.h" |
| 24 | |
| 25 | #ifdef TESTMODE |
| 26 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
| 27 | #endif |
| 28 | |
| 29 | #include "putty.h" |
| 30 | #include "dialog.h" |
| 31 | #include "tree234.h" |
| 32 | |
| 33 | struct Shortcut { |
| 34 | GtkWidget *widget; |
| 35 | struct uctrl *uc; |
| 36 | int action; |
| 37 | }; |
| 38 | |
| 39 | struct Shortcuts { |
| 40 | struct Shortcut sc[128]; |
| 41 | }; |
| 42 | |
| 43 | struct uctrl { |
| 44 | union control *ctrl; |
| 45 | GtkWidget *toplevel; |
| 46 | void *privdata; |
| 47 | int privdata_needs_free; |
| 48 | GtkWidget **buttons; int nbuttons; /* for radio buttons */ |
| 49 | GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */ |
| 50 | GtkWidget *button; /* for filesel, fontsel */ |
| 51 | GtkWidget *list; /* for combobox, listbox */ |
| 52 | GtkWidget *menu; /* for optionmenu (==droplist) */ |
| 53 | GtkWidget *optmenu; /* also for optionmenu */ |
| 54 | GtkWidget *text; /* for text */ |
| 55 | GtkAdjustment *adj; /* for the scrollbar in a list box */ |
| 56 | guint textsig; |
| 57 | }; |
| 58 | |
| 59 | struct dlgparam { |
| 60 | tree234 *byctrl, *bywidget; |
| 61 | void *data; |
| 62 | struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */ |
| 63 | /* `flags' are set to indicate when a GTK signal handler is being called |
| 64 | * due to automatic processing and should not flag a user event. */ |
| 65 | int flags; |
| 66 | struct Shortcuts *shortcuts; |
| 67 | GtkWidget *window, *cancelbutton, *currtreeitem, **treeitems; |
| 68 | union control *currfocus, *lastfocus; |
| 69 | int ntreeitems; |
| 70 | int retval; |
| 71 | }; |
| 72 | #define FLAG_UPDATING_COMBO_LIST 1 |
| 73 | |
| 74 | enum { /* values for Shortcut.action */ |
| 75 | SHORTCUT_EMPTY, /* no shortcut on this key */ |
| 76 | SHORTCUT_TREE, /* focus a tree item */ |
| 77 | SHORTCUT_FOCUS, /* focus the supplied widget */ |
| 78 | SHORTCUT_UCTRL, /* do something sane with uctrl */ |
| 79 | SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */ |
| 80 | SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */ |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * Forward references. |
| 85 | */ |
| 86 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
| 87 | gpointer data); |
| 88 | static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
| 89 | int chr, int action, void *ptr); |
| 90 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
| 91 | gpointer data); |
| 92 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
| 93 | gpointer data); |
| 94 | static int listitem_button(GtkWidget *item, GdkEventButton *event, |
| 95 | gpointer data); |
| 96 | static void menuitem_activate(GtkMenuItem *item, gpointer data); |
| 97 | static void coloursel_ok(GtkButton *button, gpointer data); |
| 98 | static void coloursel_cancel(GtkButton *button, gpointer data); |
| 99 | static void window_destroy(GtkWidget *widget, gpointer data); |
| 100 | |
| 101 | static int uctrl_cmp_byctrl(void *av, void *bv) |
| 102 | { |
| 103 | struct uctrl *a = (struct uctrl *)av; |
| 104 | struct uctrl *b = (struct uctrl *)bv; |
| 105 | if (a->ctrl < b->ctrl) |
| 106 | return -1; |
| 107 | else if (a->ctrl > b->ctrl) |
| 108 | return +1; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int uctrl_cmp_byctrl_find(void *av, void *bv) |
| 113 | { |
| 114 | union control *a = (union control *)av; |
| 115 | struct uctrl *b = (struct uctrl *)bv; |
| 116 | if (a < b->ctrl) |
| 117 | return -1; |
| 118 | else if (a > b->ctrl) |
| 119 | return +1; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int uctrl_cmp_bywidget(void *av, void *bv) |
| 124 | { |
| 125 | struct uctrl *a = (struct uctrl *)av; |
| 126 | struct uctrl *b = (struct uctrl *)bv; |
| 127 | if (a->toplevel < b->toplevel) |
| 128 | return -1; |
| 129 | else if (a->toplevel > b->toplevel) |
| 130 | return +1; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int uctrl_cmp_bywidget_find(void *av, void *bv) |
| 135 | { |
| 136 | GtkWidget *a = (GtkWidget *)av; |
| 137 | struct uctrl *b = (struct uctrl *)bv; |
| 138 | if (a < b->toplevel) |
| 139 | return -1; |
| 140 | else if (a > b->toplevel) |
| 141 | return +1; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void dlg_init(struct dlgparam *dp) |
| 146 | { |
| 147 | dp->byctrl = newtree234(uctrl_cmp_byctrl); |
| 148 | dp->bywidget = newtree234(uctrl_cmp_bywidget); |
| 149 | dp->coloursel_result.ok = FALSE; |
| 150 | } |
| 151 | |
| 152 | static void dlg_cleanup(struct dlgparam *dp) |
| 153 | { |
| 154 | struct uctrl *uc; |
| 155 | |
| 156 | freetree234(dp->byctrl); /* doesn't free the uctrls inside */ |
| 157 | while ( (uc = index234(dp->bywidget, 0)) != NULL) { |
| 158 | del234(dp->bywidget, uc); |
| 159 | if (uc->privdata_needs_free) |
| 160 | sfree(uc->privdata); |
| 161 | sfree(uc->buttons); |
| 162 | sfree(uc); |
| 163 | } |
| 164 | freetree234(dp->bywidget); |
| 165 | sfree(dp->treeitems); |
| 166 | } |
| 167 | |
| 168 | static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc) |
| 169 | { |
| 170 | add234(dp->byctrl, uc); |
| 171 | add234(dp->bywidget, uc); |
| 172 | } |
| 173 | |
| 174 | static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl) |
| 175 | { |
| 176 | return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find); |
| 177 | } |
| 178 | |
| 179 | static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w) |
| 180 | { |
| 181 | struct uctrl *ret = NULL; |
| 182 | do { |
| 183 | ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find); |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | w = w->parent; |
| 187 | } while (w); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
| 192 | { |
| 193 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 194 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 195 | return uc->privdata; |
| 196 | } |
| 197 | |
| 198 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
| 199 | { |
| 200 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 201 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 202 | uc->privdata = ptr; |
| 203 | uc->privdata_needs_free = FALSE; |
| 204 | } |
| 205 | |
| 206 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
| 207 | { |
| 208 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 209 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 210 | uc->privdata = smalloc(size); |
| 211 | uc->privdata_needs_free = FALSE; |
| 212 | return uc->privdata; |
| 213 | } |
| 214 | |
| 215 | union control *dlg_last_focused(union control *ctrl, void *dlg) |
| 216 | { |
| 217 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 218 | if (dp->currfocus != ctrl) |
| 219 | return dp->currfocus; |
| 220 | else |
| 221 | return dp->lastfocus; |
| 222 | } |
| 223 | |
| 224 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int which) |
| 225 | { |
| 226 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 227 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 228 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
| 229 | assert(uc->buttons != NULL); |
| 230 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE); |
| 231 | } |
| 232 | |
| 233 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
| 234 | { |
| 235 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 236 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 237 | int i; |
| 238 | |
| 239 | assert(uc->ctrl->generic.type == CTRL_RADIO); |
| 240 | assert(uc->buttons != NULL); |
| 241 | for (i = 0; i < uc->nbuttons; i++) |
| 242 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i]))) |
| 243 | return i; |
| 244 | return 0; /* got to return something */ |
| 245 | } |
| 246 | |
| 247 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
| 248 | { |
| 249 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 250 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 251 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
| 252 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked); |
| 253 | } |
| 254 | |
| 255 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
| 256 | { |
| 257 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 258 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 259 | assert(uc->ctrl->generic.type == CTRL_CHECKBOX); |
| 260 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel)); |
| 261 | } |
| 262 | |
| 263 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
| 264 | { |
| 265 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 266 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 267 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
| 268 | assert(uc->entry != NULL); |
| 269 | gtk_entry_set_text(GTK_ENTRY(uc->entry), text); |
| 270 | } |
| 271 | |
| 272 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
| 273 | { |
| 274 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 275 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 276 | assert(uc->ctrl->generic.type == CTRL_EDITBOX); |
| 277 | assert(uc->entry != NULL); |
| 278 | strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
| 279 | length); |
| 280 | buffer[length-1] = '\0'; |
| 281 | } |
| 282 | |
| 283 | static void container_remove_and_destroy(GtkWidget *w, gpointer data) |
| 284 | { |
| 285 | GtkContainer *cont = GTK_CONTAINER(data); |
| 286 | /* gtk_container_remove will unref the widget for us; we need not. */ |
| 287 | gtk_container_remove(cont, w); |
| 288 | } |
| 289 | |
| 290 | /* The `listbox' functions can also apply to combo boxes. */ |
| 291 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
| 292 | { |
| 293 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 294 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 295 | GtkContainer *cont; |
| 296 | |
| 297 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 298 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 299 | assert(uc->menu != NULL || uc->list != NULL); |
| 300 | |
| 301 | cont = (uc->menu ? GTK_CONTAINER(uc->menu) : GTK_CONTAINER(uc->list)); |
| 302 | |
| 303 | gtk_container_foreach(cont, container_remove_and_destroy, cont); |
| 304 | } |
| 305 | |
| 306 | void dlg_listbox_del(union control *ctrl, void *dlg, int index) |
| 307 | { |
| 308 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 309 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 310 | |
| 311 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 312 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 313 | assert(uc->menu != NULL || uc->list != NULL); |
| 314 | |
| 315 | if (uc->menu) { |
| 316 | gtk_container_remove |
| 317 | (GTK_CONTAINER(uc->menu), |
| 318 | g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index)); |
| 319 | } else { |
| 320 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) |
| 325 | { |
| 326 | dlg_listbox_addwithindex(ctrl, dlg, text, 0); |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Each listbox entry may have a numeric id associated with it. |
| 331 | * Note that some front ends only permit a string to be stored at |
| 332 | * each position, which means that _if_ you put two identical |
| 333 | * strings in any listbox then you MUST not assign them different |
| 334 | * IDs and expect to get meaningful results back. |
| 335 | */ |
| 336 | void dlg_listbox_addwithindex(union control *ctrl, void *dlg, |
| 337 | char const *text, int id) |
| 338 | { |
| 339 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 340 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 341 | |
| 342 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 343 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 344 | assert(uc->menu != NULL || uc->list != NULL); |
| 345 | |
| 346 | dp->flags |= FLAG_UPDATING_COMBO_LIST; |
| 347 | |
| 348 | if (uc->menu) { |
| 349 | /* |
| 350 | * List item in a drop-down (but non-combo) list. Tabs are |
| 351 | * ignored; we just provide a standard menu item with the |
| 352 | * text. |
| 353 | */ |
| 354 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
| 355 | |
| 356 | gtk_container_add(GTK_CONTAINER(uc->menu), menuitem); |
| 357 | gtk_widget_show(menuitem); |
| 358 | |
| 359 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", (gpointer)id); |
| 360 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
| 361 | GTK_SIGNAL_FUNC(menuitem_activate), dp); |
| 362 | } else if (!uc->entry) { |
| 363 | /* |
| 364 | * List item in a non-combo-box list box. We make all of |
| 365 | * these Columns containing GtkLabels. This allows us to do |
| 366 | * the nasty force_left hack irrespective of whether there |
| 367 | * are tabs in the thing. |
| 368 | */ |
| 369 | GtkWidget *listitem = gtk_list_item_new(); |
| 370 | GtkWidget *cols = columns_new(10); |
| 371 | gint *percents; |
| 372 | int i, ncols; |
| 373 | |
| 374 | /* Count the tabs in the text, and hence determine # of columns. */ |
| 375 | ncols = 1; |
| 376 | for (i = 0; text[i]; i++) |
| 377 | if (text[i] == '\t') |
| 378 | ncols++; |
| 379 | |
| 380 | assert(ncols <= |
| 381 | (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1)); |
| 382 | percents = smalloc(ncols * sizeof(gint)); |
| 383 | percents[ncols-1] = 100; |
| 384 | for (i = 0; i < ncols-1; i++) { |
| 385 | percents[i] = uc->ctrl->listbox.percentages[i]; |
| 386 | percents[ncols-1] -= percents[i]; |
| 387 | } |
| 388 | columns_set_cols(COLUMNS(cols), ncols, percents); |
| 389 | sfree(percents); |
| 390 | |
| 391 | for (i = 0; i < ncols; i++) { |
| 392 | int len = strcspn(text, "\t"); |
| 393 | char *dup = dupprintf("%.*s", len, text); |
| 394 | GtkWidget *label; |
| 395 | |
| 396 | text += len; |
| 397 | if (*text) text++; |
| 398 | label = gtk_label_new(dup); |
| 399 | sfree(dup); |
| 400 | |
| 401 | columns_add(COLUMNS(cols), label, i, 1); |
| 402 | columns_force_left_align(COLUMNS(cols), label); |
| 403 | gtk_widget_show(label); |
| 404 | } |
| 405 | gtk_container_add(GTK_CONTAINER(listitem), cols); |
| 406 | gtk_widget_show(cols); |
| 407 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
| 408 | gtk_widget_show(listitem); |
| 409 | |
| 410 | if (ctrl->listbox.multisel) { |
| 411 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
| 412 | GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj); |
| 413 | } else { |
| 414 | gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event", |
| 415 | GTK_SIGNAL_FUNC(listitem_single_key), uc->adj); |
| 416 | } |
| 417 | gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event", |
| 418 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 419 | gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event", |
| 420 | GTK_SIGNAL_FUNC(listitem_button), dp); |
| 421 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
| 422 | } else { |
| 423 | /* |
| 424 | * List item in a combo-box list, which means the sensible |
| 425 | * thing to do is make it a perfectly normal label. Hence |
| 426 | * tabs are disregarded. |
| 427 | */ |
| 428 | GtkWidget *listitem = gtk_list_item_new_with_label(text); |
| 429 | |
| 430 | gtk_container_add(GTK_CONTAINER(uc->list), listitem); |
| 431 | gtk_widget_show(listitem); |
| 432 | |
| 433 | gtk_object_set_data(GTK_OBJECT(listitem), "user-data", (gpointer)id); |
| 434 | } |
| 435 | |
| 436 | dp->flags &= ~FLAG_UPDATING_COMBO_LIST; |
| 437 | } |
| 438 | |
| 439 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
| 440 | { |
| 441 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 442 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 443 | GList *children; |
| 444 | GtkObject *item; |
| 445 | |
| 446 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 447 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 448 | assert(uc->menu != NULL || uc->list != NULL); |
| 449 | |
| 450 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
| 451 | uc->list)); |
| 452 | item = GTK_OBJECT(g_list_nth_data(children, index)); |
| 453 | g_list_free(children); |
| 454 | |
| 455 | return (int)gtk_object_get_data(GTK_OBJECT(item), "user-data"); |
| 456 | } |
| 457 | |
| 458 | /* dlg_listbox_index returns <0 if no single element is selected. */ |
| 459 | int dlg_listbox_index(union control *ctrl, void *dlg) |
| 460 | { |
| 461 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 462 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 463 | GList *children; |
| 464 | GtkWidget *item, *activeitem; |
| 465 | int i; |
| 466 | int selected = -1; |
| 467 | |
| 468 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 469 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 470 | assert(uc->menu != NULL || uc->list != NULL); |
| 471 | |
| 472 | if (uc->menu) |
| 473 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
| 474 | |
| 475 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
| 476 | uc->list)); |
| 477 | for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL; |
| 478 | i++, children = children->next) { |
| 479 | if (uc->menu ? activeitem == item : |
| 480 | GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) { |
| 481 | if (selected == -1) |
| 482 | selected = i; |
| 483 | else |
| 484 | selected = -2; |
| 485 | } |
| 486 | } |
| 487 | g_list_free(children); |
| 488 | return selected < 0 ? -1 : selected; |
| 489 | } |
| 490 | |
| 491 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
| 492 | { |
| 493 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 494 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 495 | GList *children; |
| 496 | GtkWidget *item, *activeitem; |
| 497 | |
| 498 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 499 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 500 | assert(uc->menu != NULL || uc->list != NULL); |
| 501 | |
| 502 | children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu : |
| 503 | uc->list)); |
| 504 | item = GTK_WIDGET(g_list_nth_data(children, index)); |
| 505 | g_list_free(children); |
| 506 | |
| 507 | if (uc->menu) { |
| 508 | activeitem = gtk_menu_get_active(GTK_MENU(uc->menu)); |
| 509 | return item == activeitem; |
| 510 | } else { |
| 511 | return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
| 516 | { |
| 517 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 518 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 519 | |
| 520 | assert(uc->ctrl->generic.type == CTRL_EDITBOX || |
| 521 | uc->ctrl->generic.type == CTRL_LISTBOX); |
| 522 | assert(uc->optmenu != NULL || uc->list != NULL); |
| 523 | |
| 524 | if (uc->optmenu) { |
| 525 | gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index); |
| 526 | } else { |
| 527 | gtk_list_select_item(GTK_LIST(uc->list), index); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
| 532 | { |
| 533 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 534 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 535 | |
| 536 | assert(uc->ctrl->generic.type == CTRL_TEXT); |
| 537 | assert(uc->text != NULL); |
| 538 | |
| 539 | gtk_label_set_text(GTK_LABEL(uc->text), text); |
| 540 | } |
| 541 | |
| 542 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
| 543 | { |
| 544 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 545 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 546 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
| 547 | assert(uc->entry != NULL); |
| 548 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path); |
| 549 | } |
| 550 | |
| 551 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
| 552 | { |
| 553 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 554 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 555 | assert(uc->ctrl->generic.type == CTRL_FILESELECT); |
| 556 | assert(uc->entry != NULL); |
| 557 | strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
| 558 | lenof(fn->path)); |
| 559 | fn->path[lenof(fn->path)-1] = '\0'; |
| 560 | } |
| 561 | |
| 562 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs) |
| 563 | { |
| 564 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 565 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 566 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
| 567 | assert(uc->entry != NULL); |
| 568 | gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name); |
| 569 | } |
| 570 | |
| 571 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs) |
| 572 | { |
| 573 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 574 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 575 | assert(uc->ctrl->generic.type == CTRL_FONTSELECT); |
| 576 | assert(uc->entry != NULL); |
| 577 | strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)), |
| 578 | lenof(fs->name)); |
| 579 | fs->name[lenof(fs->name)-1] = '\0'; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Bracketing a large set of updates in these two functions will |
| 584 | * cause the front end (if possible) to delay updating the screen |
| 585 | * until it's all complete, thus avoiding flicker. |
| 586 | */ |
| 587 | void dlg_update_start(union control *ctrl, void *dlg) |
| 588 | { |
| 589 | /* |
| 590 | * Apparently we can't do this at all in GTK. GtkCList supports |
| 591 | * freeze and thaw, but not GtkList. Bah. |
| 592 | */ |
| 593 | } |
| 594 | |
| 595 | void dlg_update_done(union control *ctrl, void *dlg) |
| 596 | { |
| 597 | /* |
| 598 | * Apparently we can't do this at all in GTK. GtkCList supports |
| 599 | * freeze and thaw, but not GtkList. Bah. |
| 600 | */ |
| 601 | } |
| 602 | |
| 603 | void dlg_set_focus(union control *ctrl, void *dlg) |
| 604 | { |
| 605 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 606 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 607 | |
| 608 | switch (ctrl->generic.type) { |
| 609 | case CTRL_CHECKBOX: |
| 610 | case CTRL_BUTTON: |
| 611 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
| 612 | gtk_widget_grab_focus(uc->toplevel); |
| 613 | break; |
| 614 | case CTRL_FILESELECT: |
| 615 | case CTRL_FONTSELECT: |
| 616 | case CTRL_EDITBOX: |
| 617 | /* Anything containing an edit box gets that focused. */ |
| 618 | gtk_widget_grab_focus(uc->entry); |
| 619 | break; |
| 620 | case CTRL_RADIO: |
| 621 | /* |
| 622 | * Radio buttons: we find the currently selected button and |
| 623 | * focus it. |
| 624 | */ |
| 625 | { |
| 626 | int i; |
| 627 | for (i = 0; i < ctrl->radio.nbuttons; i++) |
| 628 | if (gtk_toggle_button_get_active |
| 629 | (GTK_TOGGLE_BUTTON(uc->buttons[i]))) { |
| 630 | gtk_widget_grab_focus(uc->buttons[i]); |
| 631 | } |
| 632 | } |
| 633 | break; |
| 634 | case CTRL_LISTBOX: |
| 635 | /* |
| 636 | * If the list is really an option menu, we focus it. |
| 637 | * Otherwise we tell it to focus one of its children, which |
| 638 | * appears to do the Right Thing. |
| 639 | */ |
| 640 | if (uc->optmenu) { |
| 641 | gtk_widget_grab_focus(uc->optmenu); |
| 642 | } else { |
| 643 | assert(uc->list != NULL); |
| 644 | gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD); |
| 645 | } |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * During event processing, you might well want to give an error |
| 652 | * indication to the user. dlg_beep() is a quick and easy generic |
| 653 | * error; dlg_error() puts up a message-box or equivalent. |
| 654 | */ |
| 655 | void dlg_beep(void *dlg) |
| 656 | { |
| 657 | gdk_beep(); |
| 658 | } |
| 659 | |
| 660 | static void errmsg_button_clicked(GtkButton *button, gpointer data) |
| 661 | { |
| 662 | gtk_widget_destroy(GTK_WIDGET(data)); |
| 663 | } |
| 664 | |
| 665 | void dlg_error_msg(void *dlg, char *msg) |
| 666 | { |
| 667 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 668 | GtkWidget *window, *hbox, *text, *ok; |
| 669 | |
| 670 | window = gtk_dialog_new(); |
| 671 | text = gtk_label_new(msg); |
| 672 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
| 673 | hbox = gtk_hbox_new(FALSE, 0); |
| 674 | gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20); |
| 675 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
| 676 | hbox, FALSE, FALSE, 20); |
| 677 | gtk_widget_show(text); |
| 678 | gtk_widget_show(hbox); |
| 679 | gtk_window_set_title(GTK_WINDOW(window), "Error"); |
| 680 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
| 681 | ok = gtk_button_new_with_label("OK"); |
| 682 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
| 683 | ok, FALSE, FALSE, 0); |
| 684 | gtk_widget_show(ok); |
| 685 | GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT); |
| 686 | gtk_window_set_default(GTK_WINDOW(window), ok); |
| 687 | gtk_signal_connect(GTK_OBJECT(ok), "clicked", |
| 688 | GTK_SIGNAL_FUNC(errmsg_button_clicked), window); |
| 689 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
| 690 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
| 691 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
| 692 | gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window)); |
| 693 | { |
| 694 | gint x, y, w, h, dx, dy; |
| 695 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE); |
| 696 | gdk_window_get_origin(GTK_WIDGET(dp->window)->window, &x, &y); |
| 697 | gdk_window_get_size(GTK_WIDGET(dp->window)->window, &w, &h); |
| 698 | dx = x + w/4; |
| 699 | dy = y + h/4; |
| 700 | gtk_widget_set_uposition(GTK_WIDGET(window), dx, dy); |
| 701 | } |
| 702 | gtk_widget_show(window); |
| 703 | gtk_main(); |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * This function signals to the front end that the dialog's |
| 708 | * processing is completed, and passes an integer value (typically |
| 709 | * a success status). |
| 710 | */ |
| 711 | void dlg_end(void *dlg, int value) |
| 712 | { |
| 713 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 714 | dp->retval = value; |
| 715 | gtk_main_quit(); |
| 716 | } |
| 717 | |
| 718 | void dlg_refresh(union control *ctrl, void *dlg) |
| 719 | { |
| 720 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 721 | struct uctrl *uc; |
| 722 | |
| 723 | if (ctrl) { |
| 724 | if (ctrl->generic.handler != NULL) |
| 725 | ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH); |
| 726 | } else { |
| 727 | int i; |
| 728 | |
| 729 | for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) { |
| 730 | assert(uc->ctrl != NULL); |
| 731 | if (uc->ctrl->generic.handler != NULL) |
| 732 | uc->ctrl->generic.handler(uc->ctrl, dp, |
| 733 | dp->data, EVENT_REFRESH); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b) |
| 739 | { |
| 740 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 741 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl); |
| 742 | gdouble cvals[4]; |
| 743 | |
| 744 | GtkWidget *coloursel = |
| 745 | gtk_color_selection_dialog_new("Select a colour"); |
| 746 | GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel); |
| 747 | |
| 748 | dp->coloursel_result.ok = FALSE; |
| 749 | |
| 750 | gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE); |
| 751 | gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE); |
| 752 | cvals[0] = r / 255.0; |
| 753 | cvals[1] = g / 255.0; |
| 754 | cvals[2] = b / 255.0; |
| 755 | cvals[3] = 1.0; /* fully opaque! */ |
| 756 | gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals); |
| 757 | |
| 758 | gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data", |
| 759 | (gpointer)coloursel); |
| 760 | gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data", |
| 761 | (gpointer)coloursel); |
| 762 | gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc); |
| 763 | gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked", |
| 764 | GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp); |
| 765 | gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked", |
| 766 | GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp); |
| 767 | gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked", |
| 768 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
| 769 | (gpointer)coloursel); |
| 770 | gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked", |
| 771 | GTK_SIGNAL_FUNC(gtk_widget_destroy), |
| 772 | (gpointer)coloursel); |
| 773 | gtk_widget_show(coloursel); |
| 774 | } |
| 775 | |
| 776 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
| 777 | int *r, int *g, int *b) |
| 778 | { |
| 779 | struct dlgparam *dp = (struct dlgparam *)dlg; |
| 780 | if (dp->coloursel_result.ok) { |
| 781 | *r = dp->coloursel_result.r; |
| 782 | *g = dp->coloursel_result.g; |
| 783 | *b = dp->coloursel_result.b; |
| 784 | return 1; |
| 785 | } else |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | /* ---------------------------------------------------------------------- |
| 790 | * Signal handlers while the dialog box is active. |
| 791 | */ |
| 792 | |
| 793 | static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event, |
| 794 | gpointer data) |
| 795 | { |
| 796 | struct dlgparam *dp = (struct dlgparam *)data; |
| 797 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
| 798 | union control *focus; |
| 799 | |
| 800 | if (uc && uc->ctrl) |
| 801 | focus = uc->ctrl; |
| 802 | else |
| 803 | focus = NULL; |
| 804 | |
| 805 | if (focus != dp->currfocus) { |
| 806 | dp->lastfocus = dp->currfocus; |
| 807 | dp->currfocus = focus; |
| 808 | } |
| 809 | |
| 810 | return FALSE; |
| 811 | } |
| 812 | |
| 813 | static void button_clicked(GtkButton *button, gpointer data) |
| 814 | { |
| 815 | struct dlgparam *dp = (struct dlgparam *)data; |
| 816 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
| 817 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
| 818 | } |
| 819 | |
| 820 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
| 821 | { |
| 822 | struct dlgparam *dp = (struct dlgparam *)data; |
| 823 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb)); |
| 824 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
| 825 | } |
| 826 | |
| 827 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
| 828 | { |
| 829 | /* |
| 830 | * GtkEntry has a nasty habit of eating the Return key, which |
| 831 | * is unhelpful since it doesn't actually _do_ anything with it |
| 832 | * (it calls gtk_widget_activate, but our edit boxes never need |
| 833 | * activating). So I catch Return before GtkEntry sees it, and |
| 834 | * pass it straight on to the parent widget. Effect: hitting |
| 835 | * Return in an edit box will now activate the default button |
| 836 | * in the dialog just like it will everywhere else. |
| 837 | */ |
| 838 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
| 839 | gint return_val; |
| 840 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
| 841 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
| 842 | event, &return_val); |
| 843 | return return_val; |
| 844 | } |
| 845 | return FALSE; |
| 846 | } |
| 847 | |
| 848 | static void editbox_changed(GtkEditable *ed, gpointer data) |
| 849 | { |
| 850 | struct dlgparam *dp = (struct dlgparam *)data; |
| 851 | if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) { |
| 852 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
| 853 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event, |
| 858 | gpointer data) |
| 859 | { |
| 860 | struct dlgparam *dp = (struct dlgparam *)data; |
| 861 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed)); |
| 862 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH); |
| 863 | } |
| 864 | |
| 865 | static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data, |
| 866 | int multiple) |
| 867 | { |
| 868 | GtkAdjustment *adj = GTK_ADJUSTMENT(data); |
| 869 | |
| 870 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
| 871 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down || |
| 872 | event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up || |
| 873 | event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) { |
| 874 | /* |
| 875 | * Up, Down, PgUp or PgDn have been pressed on a ListItem |
| 876 | * in a list box. So, if the list box is single-selection: |
| 877 | * |
| 878 | * - if the list item in question isn't already selected, |
| 879 | * we simply select it. |
| 880 | * - otherwise, we find the next one (or next |
| 881 | * however-far-away) in whichever direction we're going, |
| 882 | * and select that. |
| 883 | * + in this case, we must also fiddle with the |
| 884 | * scrollbar to ensure the newly selected item is |
| 885 | * actually visible. |
| 886 | * |
| 887 | * If it's multiple-selection, we do all of the above |
| 888 | * except actually selecting anything, so we move the focus |
| 889 | * and fiddle the scrollbar to follow it. |
| 890 | */ |
| 891 | GtkWidget *list = item->parent; |
| 892 | |
| 893 | gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event"); |
| 894 | |
| 895 | if (!multiple && |
| 896 | GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) { |
| 897 | gtk_list_select_child(GTK_LIST(list), item); |
| 898 | } else { |
| 899 | int direction = |
| 900 | (event->keyval==GDK_Up || event->keyval==GDK_KP_Up || |
| 901 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
| 902 | ? -1 : +1; |
| 903 | int step = |
| 904 | (event->keyval==GDK_Page_Down || |
| 905 | event->keyval==GDK_KP_Page_Down || |
| 906 | event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up) |
| 907 | ? 2 : 1; |
| 908 | int i, n; |
| 909 | GtkWidget *thisitem; |
| 910 | GList *children, *chead; |
| 911 | |
| 912 | chead = children = gtk_container_children(GTK_CONTAINER(list)); |
| 913 | |
| 914 | n = g_list_length(children); |
| 915 | |
| 916 | if (step == 2) { |
| 917 | /* |
| 918 | * Figure out how many list items to a screenful, |
| 919 | * and adjust the step appropriately. |
| 920 | */ |
| 921 | step = 0.5 + adj->page_size * n / (adj->upper - adj->lower); |
| 922 | step--; /* go by one less than that */ |
| 923 | } |
| 924 | |
| 925 | i = 0; |
| 926 | while (children != NULL) { |
| 927 | if (item == children->data) |
| 928 | break; |
| 929 | children = children->next; |
| 930 | i++; |
| 931 | } |
| 932 | |
| 933 | while (step > 0) { |
| 934 | if (direction < 0 && i > 0) |
| 935 | children = children->prev, i--; |
| 936 | else if (direction > 0 && i < n-1) |
| 937 | children = children->next, i++; |
| 938 | step--; |
| 939 | } |
| 940 | |
| 941 | if (children && children->data) { |
| 942 | if (!multiple) |
| 943 | gtk_list_select_child(GTK_LIST(list), |
| 944 | GTK_WIDGET(children->data)); |
| 945 | gtk_widget_grab_focus(GTK_WIDGET(children->data)); |
| 946 | gtk_adjustment_clamp_page |
| 947 | (adj, |
| 948 | adj->lower + (adj->upper-adj->lower) * i / n, |
| 949 | adj->lower + (adj->upper-adj->lower) * (i+1) / n); |
| 950 | } |
| 951 | |
| 952 | g_list_free(chead); |
| 953 | } |
| 954 | return TRUE; |
| 955 | } |
| 956 | |
| 957 | return FALSE; |
| 958 | } |
| 959 | |
| 960 | static int listitem_single_key(GtkWidget *item, GdkEventKey *event, |
| 961 | gpointer data) |
| 962 | { |
| 963 | listitem_key(item, event, data, FALSE); |
| 964 | } |
| 965 | |
| 966 | static int listitem_multi_key(GtkWidget *item, GdkEventKey *event, |
| 967 | gpointer data) |
| 968 | { |
| 969 | listitem_key(item, event, data, TRUE); |
| 970 | } |
| 971 | |
| 972 | static int listitem_button(GtkWidget *item, GdkEventButton *event, |
| 973 | gpointer data) |
| 974 | { |
| 975 | struct dlgparam *dp = (struct dlgparam *)data; |
| 976 | if (event->type == GDK_2BUTTON_PRESS || |
| 977 | event->type == GDK_3BUTTON_PRESS) { |
| 978 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item)); |
| 979 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION); |
| 980 | return TRUE; |
| 981 | } |
| 982 | return FALSE; |
| 983 | } |
| 984 | |
| 985 | static void list_selchange(GtkList *list, gpointer data) |
| 986 | { |
| 987 | struct dlgparam *dp = (struct dlgparam *)data; |
| 988 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list)); |
| 989 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
| 990 | } |
| 991 | |
| 992 | static void menuitem_activate(GtkMenuItem *item, gpointer data) |
| 993 | { |
| 994 | struct dlgparam *dp = (struct dlgparam *)data; |
| 995 | GtkWidget *menushell = GTK_WIDGET(item)->parent; |
| 996 | gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data"); |
| 997 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu)); |
| 998 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE); |
| 999 | } |
| 1000 | |
| 1001 | static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction) |
| 1002 | { |
| 1003 | int index = dlg_listbox_index(uc->ctrl, dp); |
| 1004 | GList *children = gtk_container_children(GTK_CONTAINER(uc->list)); |
| 1005 | GtkWidget *child; |
| 1006 | |
| 1007 | if ((index < 0) || |
| 1008 | (index == 0 && direction < 0) || |
| 1009 | (index == g_list_length(children)-1 && direction > 0)) { |
| 1010 | gdk_beep(); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | child = g_list_nth_data(children, index); |
| 1015 | gtk_widget_ref(child); |
| 1016 | gtk_list_clear_items(GTK_LIST(uc->list), index, index+1); |
| 1017 | g_list_free(children); |
| 1018 | |
| 1019 | children = NULL; |
| 1020 | children = g_list_append(children, child); |
| 1021 | gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction); |
| 1022 | gtk_list_select_item(GTK_LIST(uc->list), index + direction); |
| 1023 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE); |
| 1024 | } |
| 1025 | |
| 1026 | static void draglist_up(GtkButton *button, gpointer data) |
| 1027 | { |
| 1028 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1029 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
| 1030 | draglist_move(dp, uc, -1); |
| 1031 | } |
| 1032 | |
| 1033 | static void draglist_down(GtkButton *button, gpointer data) |
| 1034 | { |
| 1035 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1036 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
| 1037 | draglist_move(dp, uc, +1); |
| 1038 | } |
| 1039 | |
| 1040 | static void filesel_ok(GtkButton *button, gpointer data) |
| 1041 | { |
| 1042 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1043 | gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
| 1044 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data"); |
| 1045 | char *name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); |
| 1046 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
| 1047 | } |
| 1048 | |
| 1049 | static void fontsel_ok(GtkButton *button, gpointer data) |
| 1050 | { |
| 1051 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1052 | gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
| 1053 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data"); |
| 1054 | char *name = gtk_font_selection_dialog_get_font_name |
| 1055 | (GTK_FONT_SELECTION_DIALOG(fontsel)); |
| 1056 | gtk_entry_set_text(GTK_ENTRY(uc->entry), name); |
| 1057 | } |
| 1058 | |
| 1059 | static void coloursel_ok(GtkButton *button, gpointer data) |
| 1060 | { |
| 1061 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1062 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
| 1063 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
| 1064 | gdouble cvals[4]; |
| 1065 | gtk_color_selection_get_color |
| 1066 | (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel), |
| 1067 | cvals); |
| 1068 | dp->coloursel_result.r = (int) (255 * cvals[0]); |
| 1069 | dp->coloursel_result.g = (int) (255 * cvals[1]); |
| 1070 | dp->coloursel_result.b = (int) (255 * cvals[2]); |
| 1071 | dp->coloursel_result.ok = TRUE; |
| 1072 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
| 1073 | } |
| 1074 | |
| 1075 | static void coloursel_cancel(GtkButton *button, gpointer data) |
| 1076 | { |
| 1077 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1078 | gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
| 1079 | struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data"); |
| 1080 | dp->coloursel_result.ok = FALSE; |
| 1081 | uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK); |
| 1082 | } |
| 1083 | |
| 1084 | static void filefont_clicked(GtkButton *button, gpointer data) |
| 1085 | { |
| 1086 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1087 | struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button)); |
| 1088 | |
| 1089 | if (uc->ctrl->generic.type == CTRL_FILESELECT) { |
| 1090 | GtkWidget *filesel = |
| 1091 | gtk_file_selection_new(uc->ctrl->fileselect.title); |
| 1092 | gtk_window_set_modal(GTK_WINDOW(filesel), TRUE); |
| 1093 | gtk_object_set_data |
| 1094 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data", |
| 1095 | (gpointer)filesel); |
| 1096 | gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc); |
| 1097 | gtk_signal_connect |
| 1098 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
| 1099 | GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp); |
| 1100 | gtk_signal_connect_object |
| 1101 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
| 1102 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
| 1103 | gtk_signal_connect_object |
| 1104 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", |
| 1105 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
| 1106 | gtk_widget_show(filesel); |
| 1107 | } |
| 1108 | |
| 1109 | if (uc->ctrl->generic.type == CTRL_FONTSELECT) { |
| 1110 | gchar *spacings[] = { "c", "m", NULL }; |
| 1111 | gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry)); |
| 1112 | GtkWidget *fontsel = |
| 1113 | gtk_font_selection_dialog_new("Select a font"); |
| 1114 | gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE); |
| 1115 | gtk_font_selection_dialog_set_filter |
| 1116 | (GTK_FONT_SELECTION_DIALOG(fontsel), |
| 1117 | GTK_FONT_FILTER_BASE, GTK_FONT_ALL, |
| 1118 | NULL, NULL, NULL, NULL, spacings, NULL); |
| 1119 | if (!gtk_font_selection_dialog_set_font_name |
| 1120 | (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) { |
| 1121 | /* |
| 1122 | * If the font name wasn't found as it was, try opening |
| 1123 | * it and extracting its FONT property. This should |
| 1124 | * have the effect of mapping short aliases into true |
| 1125 | * XLFDs. |
| 1126 | */ |
| 1127 | GdkFont *font = gdk_font_load(fontname); |
| 1128 | if (font) { |
| 1129 | XFontStruct *xfs = GDK_FONT_XFONT(font); |
| 1130 | Display *disp = GDK_FONT_XDISPLAY(font); |
| 1131 | Atom fontprop = XInternAtom(disp, "FONT", False); |
| 1132 | unsigned long ret; |
| 1133 | if (XGetFontProperty(xfs, fontprop, &ret)) { |
| 1134 | char *name = XGetAtomName(disp, (Atom)ret); |
| 1135 | if (name) |
| 1136 | gtk_font_selection_dialog_set_font_name |
| 1137 | (GTK_FONT_SELECTION_DIALOG(fontsel), name); |
| 1138 | } |
| 1139 | gdk_font_unref(font); |
| 1140 | } |
| 1141 | } |
| 1142 | gtk_object_set_data |
| 1143 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
| 1144 | "user-data", (gpointer)fontsel); |
| 1145 | gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc); |
| 1146 | gtk_signal_connect |
| 1147 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
| 1148 | "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp); |
| 1149 | gtk_signal_connect_object |
| 1150 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button), |
| 1151 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
| 1152 | (gpointer)fontsel); |
| 1153 | gtk_signal_connect_object |
| 1154 | (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button), |
| 1155 | "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), |
| 1156 | (gpointer)fontsel); |
| 1157 | gtk_widget_show(fontsel); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc, |
| 1162 | gpointer data) |
| 1163 | { |
| 1164 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1165 | struct uctrl *uc = dlg_find_bywidget(dp, widget); |
| 1166 | |
| 1167 | gtk_widget_set_usize(uc->text, alloc->width, -1); |
| 1168 | gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label); |
| 1169 | gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig); |
| 1170 | } |
| 1171 | |
| 1172 | /* ---------------------------------------------------------------------- |
| 1173 | * This function does the main layout work: it reads a controlset, |
| 1174 | * it creates the relevant GTK controls, and returns a GtkWidget |
| 1175 | * containing the result. (This widget might be a title of some |
| 1176 | * sort, it might be a Columns containing many controls, or it |
| 1177 | * might be a GtkFrame containing a Columns; whatever it is, it's |
| 1178 | * definitely a GtkWidget and should probably be added to a |
| 1179 | * GtkVbox.) |
| 1180 | * |
| 1181 | * `listitemheight' is used to calculate a usize for list boxes: it |
| 1182 | * should be the height from the size request of a GtkListItem. |
| 1183 | * |
| 1184 | * `win' is required for setting the default button. If it is |
| 1185 | * non-NULL, all buttons created will be default-capable (so they |
| 1186 | * have extra space round them for the default highlight). |
| 1187 | */ |
| 1188 | GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs, |
| 1189 | struct controlset *s, int listitemheight, |
| 1190 | GtkWindow *win) |
| 1191 | { |
| 1192 | Columns *cols; |
| 1193 | GtkWidget *ret; |
| 1194 | int i; |
| 1195 | |
| 1196 | if (!s->boxname && s->boxtitle) { |
| 1197 | /* This controlset is a panel title. */ |
| 1198 | return gtk_label_new(s->boxtitle); |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * Otherwise, we expect to be laying out actual controls, so |
| 1203 | * we'll start by creating a Columns for the purpose. |
| 1204 | */ |
| 1205 | cols = COLUMNS(columns_new(4)); |
| 1206 | ret = GTK_WIDGET(cols); |
| 1207 | gtk_widget_show(ret); |
| 1208 | |
| 1209 | /* |
| 1210 | * Create a containing frame if we have a box name. |
| 1211 | */ |
| 1212 | if (*s->boxname) { |
| 1213 | ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */ |
| 1214 | gtk_container_set_border_width(GTK_CONTAINER(cols), 4); |
| 1215 | gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols)); |
| 1216 | gtk_widget_show(ret); |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Now iterate through the controls themselves, create them, |
| 1221 | * and add them to the Columns. |
| 1222 | */ |
| 1223 | for (i = 0; i < s->ncontrols; i++) { |
| 1224 | union control *ctrl = s->ctrls[i]; |
| 1225 | struct uctrl *uc; |
| 1226 | int left = FALSE; |
| 1227 | GtkWidget *w = NULL; |
| 1228 | |
| 1229 | switch (ctrl->generic.type) { |
| 1230 | case CTRL_COLUMNS: |
| 1231 | { |
| 1232 | static const int simplecols[1] = { 100 }; |
| 1233 | columns_set_cols(cols, ctrl->columns.ncols, |
| 1234 | (ctrl->columns.percentages ? |
| 1235 | ctrl->columns.percentages : simplecols)); |
| 1236 | } |
| 1237 | continue; /* no actual control created */ |
| 1238 | case CTRL_TABDELAY: |
| 1239 | { |
| 1240 | struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl); |
| 1241 | if (uc) |
| 1242 | columns_taborder_last(cols, uc->toplevel); |
| 1243 | } |
| 1244 | continue; /* no actual control created */ |
| 1245 | } |
| 1246 | |
| 1247 | uc = smalloc(sizeof(struct uctrl)); |
| 1248 | uc->ctrl = ctrl; |
| 1249 | uc->privdata = NULL; |
| 1250 | uc->privdata_needs_free = FALSE; |
| 1251 | uc->buttons = NULL; |
| 1252 | uc->entry = uc->list = uc->menu = NULL; |
| 1253 | uc->button = uc->optmenu = uc->text = NULL; |
| 1254 | |
| 1255 | switch (ctrl->generic.type) { |
| 1256 | case CTRL_BUTTON: |
| 1257 | w = gtk_button_new_with_label(ctrl->generic.label); |
| 1258 | if (win) { |
| 1259 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
| 1260 | if (ctrl->button.isdefault) |
| 1261 | gtk_window_set_default(win, w); |
| 1262 | if (ctrl->button.iscancel) |
| 1263 | dp->cancelbutton = w; |
| 1264 | } |
| 1265 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
| 1266 | GTK_SIGNAL_FUNC(button_clicked), dp); |
| 1267 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
| 1268 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1269 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut, |
| 1270 | SHORTCUT_UCTRL, uc); |
| 1271 | break; |
| 1272 | case CTRL_CHECKBOX: |
| 1273 | w = gtk_check_button_new_with_label(ctrl->generic.label); |
| 1274 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
| 1275 | GTK_SIGNAL_FUNC(button_toggled), dp); |
| 1276 | gtk_signal_connect(GTK_OBJECT(w), "focus_in_event", |
| 1277 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1278 | shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut, |
| 1279 | SHORTCUT_UCTRL, uc); |
| 1280 | left = TRUE; |
| 1281 | break; |
| 1282 | case CTRL_RADIO: |
| 1283 | /* |
| 1284 | * Radio buttons get to go inside their own Columns, no |
| 1285 | * matter what. |
| 1286 | */ |
| 1287 | { |
| 1288 | gint i, *percentages; |
| 1289 | GSList *group; |
| 1290 | |
| 1291 | w = columns_new(0); |
| 1292 | if (ctrl->generic.label) { |
| 1293 | GtkWidget *label = gtk_label_new(ctrl->generic.label); |
| 1294 | columns_add(COLUMNS(w), label, 0, 1); |
| 1295 | columns_force_left_align(COLUMNS(w), label); |
| 1296 | gtk_widget_show(label); |
| 1297 | shortcut_add(scs, label, ctrl->radio.shortcut, |
| 1298 | SHORTCUT_UCTRL, uc); |
| 1299 | } |
| 1300 | percentages = g_new(gint, ctrl->radio.ncolumns); |
| 1301 | for (i = 0; i < ctrl->radio.ncolumns; i++) { |
| 1302 | percentages[i] = |
| 1303 | ((100 * (i+1) / ctrl->radio.ncolumns) - |
| 1304 | 100 * i / ctrl->radio.ncolumns); |
| 1305 | } |
| 1306 | columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns, |
| 1307 | percentages); |
| 1308 | g_free(percentages); |
| 1309 | group = NULL; |
| 1310 | |
| 1311 | uc->nbuttons = ctrl->radio.nbuttons; |
| 1312 | uc->buttons = smalloc(uc->nbuttons * sizeof(GtkWidget *)); |
| 1313 | |
| 1314 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
| 1315 | GtkWidget *b; |
| 1316 | gint colstart; |
| 1317 | |
| 1318 | b = (gtk_radio_button_new_with_label |
| 1319 | (group, ctrl->radio.buttons[i])); |
| 1320 | uc->buttons[i] = b; |
| 1321 | group = gtk_radio_button_group(GTK_RADIO_BUTTON(b)); |
| 1322 | colstart = i % ctrl->radio.ncolumns; |
| 1323 | columns_add(COLUMNS(w), b, colstart, |
| 1324 | (i == ctrl->radio.nbuttons-1 ? |
| 1325 | ctrl->radio.ncolumns - colstart : 1)); |
| 1326 | columns_force_left_align(COLUMNS(w), b); |
| 1327 | gtk_widget_show(b); |
| 1328 | gtk_signal_connect(GTK_OBJECT(b), "toggled", |
| 1329 | GTK_SIGNAL_FUNC(button_toggled), dp); |
| 1330 | gtk_signal_connect(GTK_OBJECT(b), "focus_in_event", |
| 1331 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1332 | if (ctrl->radio.shortcuts) { |
| 1333 | shortcut_add(scs, GTK_BIN(b)->child, |
| 1334 | ctrl->radio.shortcuts[i], |
| 1335 | SHORTCUT_UCTRL, uc); |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | break; |
| 1340 | case CTRL_EDITBOX: |
| 1341 | { |
| 1342 | GtkRequisition req; |
| 1343 | |
| 1344 | if (ctrl->editbox.has_list) { |
| 1345 | w = gtk_combo_new(); |
| 1346 | gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE); |
| 1347 | uc->entry = GTK_COMBO(w)->entry; |
| 1348 | uc->list = GTK_COMBO(w)->list; |
| 1349 | } else { |
| 1350 | w = gtk_entry_new(); |
| 1351 | if (ctrl->editbox.password) |
| 1352 | gtk_entry_set_visibility(GTK_ENTRY(w), FALSE); |
| 1353 | uc->entry = w; |
| 1354 | } |
| 1355 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
| 1356 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
| 1357 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
| 1358 | GTK_SIGNAL_FUNC(editbox_key), dp); |
| 1359 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
| 1360 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1361 | /* |
| 1362 | * Edit boxes, for some strange reason, have a minimum |
| 1363 | * width of 150 in GTK 1.2. We don't want this - we'd |
| 1364 | * rather the edit boxes acquired their natural width |
| 1365 | * from the column layout of the rest of the box. |
| 1366 | * |
| 1367 | * Also, while we're here, we'll squirrel away the |
| 1368 | * edit box height so we can use that to centre its |
| 1369 | * label vertically beside it. |
| 1370 | */ |
| 1371 | gtk_widget_size_request(w, &req); |
| 1372 | gtk_widget_set_usize(w, 10, req.height); |
| 1373 | |
| 1374 | if (ctrl->generic.label) { |
| 1375 | GtkWidget *label, *container; |
| 1376 | |
| 1377 | label = gtk_label_new(ctrl->generic.label); |
| 1378 | |
| 1379 | shortcut_add(scs, label, ctrl->editbox.shortcut, |
| 1380 | SHORTCUT_FOCUS, uc->entry); |
| 1381 | |
| 1382 | container = columns_new(4); |
| 1383 | if (ctrl->editbox.percentwidth == 100) { |
| 1384 | columns_add(COLUMNS(container), label, 0, 1); |
| 1385 | columns_force_left_align(COLUMNS(container), label); |
| 1386 | columns_add(COLUMNS(container), w, 0, 1); |
| 1387 | } else { |
| 1388 | gint percentages[2]; |
| 1389 | percentages[1] = ctrl->editbox.percentwidth; |
| 1390 | percentages[0] = 100 - ctrl->editbox.percentwidth; |
| 1391 | columns_set_cols(COLUMNS(container), 2, percentages); |
| 1392 | columns_add(COLUMNS(container), label, 0, 1); |
| 1393 | columns_force_left_align(COLUMNS(container), label); |
| 1394 | columns_add(COLUMNS(container), w, 1, 1); |
| 1395 | /* Centre the label vertically. */ |
| 1396 | gtk_widget_set_usize(label, -1, req.height); |
| 1397 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); |
| 1398 | } |
| 1399 | gtk_widget_show(label); |
| 1400 | gtk_widget_show(w); |
| 1401 | |
| 1402 | w = container; |
| 1403 | } |
| 1404 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event", |
| 1405 | GTK_SIGNAL_FUNC(editbox_lostfocus), dp); |
| 1406 | } |
| 1407 | break; |
| 1408 | case CTRL_FILESELECT: |
| 1409 | case CTRL_FONTSELECT: |
| 1410 | { |
| 1411 | GtkWidget *ww; |
| 1412 | GtkRequisition req; |
| 1413 | char *browsebtn = |
| 1414 | (ctrl->generic.type == CTRL_FILESELECT ? |
| 1415 | "Browse..." : "Change..."); |
| 1416 | |
| 1417 | gint percentages[] = { 75, 25 }; |
| 1418 | w = columns_new(4); |
| 1419 | columns_set_cols(COLUMNS(w), 2, percentages); |
| 1420 | |
| 1421 | if (ctrl->generic.label) { |
| 1422 | ww = gtk_label_new(ctrl->generic.label); |
| 1423 | columns_add(COLUMNS(w), ww, 0, 2); |
| 1424 | columns_force_left_align(COLUMNS(w), ww); |
| 1425 | gtk_widget_show(ww); |
| 1426 | shortcut_add(scs, ww, |
| 1427 | (ctrl->generic.type == CTRL_FILESELECT ? |
| 1428 | ctrl->fileselect.shortcut : |
| 1429 | ctrl->fontselect.shortcut), |
| 1430 | SHORTCUT_UCTRL, uc); |
| 1431 | } |
| 1432 | |
| 1433 | uc->entry = ww = gtk_entry_new(); |
| 1434 | gtk_widget_size_request(ww, &req); |
| 1435 | gtk_widget_set_usize(ww, 10, req.height); |
| 1436 | columns_add(COLUMNS(w), ww, 0, 1); |
| 1437 | gtk_widget_show(ww); |
| 1438 | |
| 1439 | uc->button = ww = gtk_button_new_with_label(browsebtn); |
| 1440 | columns_add(COLUMNS(w), ww, 1, 1); |
| 1441 | gtk_widget_show(ww); |
| 1442 | |
| 1443 | gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event", |
| 1444 | GTK_SIGNAL_FUNC(editbox_key), dp); |
| 1445 | gtk_signal_connect(GTK_OBJECT(uc->entry), "changed", |
| 1446 | GTK_SIGNAL_FUNC(editbox_changed), dp); |
| 1447 | gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event", |
| 1448 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1449 | gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event", |
| 1450 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1451 | gtk_signal_connect(GTK_OBJECT(ww), "clicked", |
| 1452 | GTK_SIGNAL_FUNC(filefont_clicked), dp); |
| 1453 | } |
| 1454 | break; |
| 1455 | case CTRL_LISTBOX: |
| 1456 | if (ctrl->listbox.height == 0) { |
| 1457 | uc->optmenu = w = gtk_option_menu_new(); |
| 1458 | uc->menu = gtk_menu_new(); |
| 1459 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu); |
| 1460 | gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data", |
| 1461 | (gpointer)uc->optmenu); |
| 1462 | gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event", |
| 1463 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1464 | } else { |
| 1465 | uc->list = gtk_list_new(); |
| 1466 | if (ctrl->listbox.multisel) { |
| 1467 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
| 1468 | GTK_SELECTION_MULTIPLE); |
| 1469 | } else { |
| 1470 | gtk_list_set_selection_mode(GTK_LIST(uc->list), |
| 1471 | GTK_SELECTION_SINGLE); |
| 1472 | } |
| 1473 | w = gtk_scrolled_window_new(NULL, NULL); |
| 1474 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w), |
| 1475 | uc->list); |
| 1476 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w), |
| 1477 | GTK_POLICY_NEVER, |
| 1478 | GTK_POLICY_AUTOMATIC); |
| 1479 | uc->adj = gtk_scrolled_window_get_vadjustment |
| 1480 | (GTK_SCROLLED_WINDOW(w)); |
| 1481 | |
| 1482 | gtk_widget_show(uc->list); |
| 1483 | gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed", |
| 1484 | GTK_SIGNAL_FUNC(list_selchange), dp); |
| 1485 | gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event", |
| 1486 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1487 | |
| 1488 | /* |
| 1489 | * Adjust the height of the scrolled window to the |
| 1490 | * minimum given by the height parameter. |
| 1491 | * |
| 1492 | * This piece of guesswork is a horrid hack based |
| 1493 | * on looking inside the GTK 1.2 sources |
| 1494 | * (specifically gtkviewport.c, which appears to be |
| 1495 | * the widget which provides the border around the |
| 1496 | * scrolling area). Anyone lets me know how I can |
| 1497 | * do this in a way which isn't at risk from GTK |
| 1498 | * upgrades, I'd be grateful. |
| 1499 | */ |
| 1500 | { |
| 1501 | int edge = GTK_WIDGET(uc->list)->style->klass->ythickness; |
| 1502 | gtk_widget_set_usize(w, 10, |
| 1503 | 2*edge + (ctrl->listbox.height * |
| 1504 | listitemheight)); |
| 1505 | } |
| 1506 | |
| 1507 | if (ctrl->listbox.draglist) { |
| 1508 | /* |
| 1509 | * GTK doesn't appear to make it easy to |
| 1510 | * implement a proper draggable list; so |
| 1511 | * instead I'm just going to have to put an Up |
| 1512 | * and a Down button to the right of the actual |
| 1513 | * list box. Ah well. |
| 1514 | */ |
| 1515 | GtkWidget *cols, *button; |
| 1516 | static const gint percentages[2] = { 80, 20 }; |
| 1517 | |
| 1518 | cols = columns_new(4); |
| 1519 | columns_set_cols(COLUMNS(cols), 2, percentages); |
| 1520 | columns_add(COLUMNS(cols), w, 0, 1); |
| 1521 | gtk_widget_show(w); |
| 1522 | button = gtk_button_new_with_label("Up"); |
| 1523 | columns_add(COLUMNS(cols), button, 1, 1); |
| 1524 | gtk_widget_show(button); |
| 1525 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
| 1526 | GTK_SIGNAL_FUNC(draglist_up), dp); |
| 1527 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
| 1528 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1529 | button = gtk_button_new_with_label("Down"); |
| 1530 | columns_add(COLUMNS(cols), button, 1, 1); |
| 1531 | gtk_widget_show(button); |
| 1532 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
| 1533 | GTK_SIGNAL_FUNC(draglist_down), dp); |
| 1534 | gtk_signal_connect(GTK_OBJECT(button), "focus_in_event", |
| 1535 | GTK_SIGNAL_FUNC(widget_focus), dp); |
| 1536 | |
| 1537 | w = cols; |
| 1538 | } |
| 1539 | |
| 1540 | } |
| 1541 | if (ctrl->generic.label) { |
| 1542 | GtkWidget *label, *container; |
| 1543 | |
| 1544 | label = gtk_label_new(ctrl->generic.label); |
| 1545 | |
| 1546 | container = columns_new(4); |
| 1547 | if (ctrl->listbox.percentwidth == 100) { |
| 1548 | columns_add(COLUMNS(container), label, 0, 1); |
| 1549 | columns_force_left_align(COLUMNS(container), label); |
| 1550 | columns_add(COLUMNS(container), w, 0, 1); |
| 1551 | } else { |
| 1552 | gint percentages[2]; |
| 1553 | percentages[1] = ctrl->listbox.percentwidth; |
| 1554 | percentages[0] = 100 - ctrl->listbox.percentwidth; |
| 1555 | columns_set_cols(COLUMNS(container), 2, percentages); |
| 1556 | columns_add(COLUMNS(container), label, 0, 1); |
| 1557 | columns_force_left_align(COLUMNS(container), label); |
| 1558 | columns_add(COLUMNS(container), w, 1, 1); |
| 1559 | } |
| 1560 | gtk_widget_show(label); |
| 1561 | gtk_widget_show(w); |
| 1562 | shortcut_add(scs, label, ctrl->listbox.shortcut, |
| 1563 | SHORTCUT_UCTRL, uc); |
| 1564 | w = container; |
| 1565 | } |
| 1566 | break; |
| 1567 | case CTRL_TEXT: |
| 1568 | /* |
| 1569 | * Wrapping text widgets don't sit well with the GTK |
| 1570 | * layout model, in which widgets state a minimum size |
| 1571 | * and the whole window then adjusts to the smallest |
| 1572 | * size it can sensibly take given its contents. A |
| 1573 | * wrapping text widget _has_ no clear minimum size; |
| 1574 | * instead it has a range of possibilities. It can be |
| 1575 | * one line deep but 2000 wide, or two lines deep and |
| 1576 | * 1000 pixels, or three by 867, or four by 500 and so |
| 1577 | * on. It can be as short as you like provided you |
| 1578 | * don't mind it being wide, or as narrow as you like |
| 1579 | * provided you don't mind it being tall. |
| 1580 | * |
| 1581 | * Therefore, it fits very badly into the layout model. |
| 1582 | * Hence the only thing to do is pick a width and let |
| 1583 | * it choose its own number of lines. To do this I'm |
| 1584 | * going to cheat a little. All new wrapping text |
| 1585 | * widgets will be created with a minimal text content |
| 1586 | * "X"; then, after the rest of the dialog box is set |
| 1587 | * up and its size calculated, the text widgets will be |
| 1588 | * told their width and given their real text, which |
| 1589 | * will cause the size to be recomputed in the y |
| 1590 | * direction (because many of them will expand to more |
| 1591 | * than one line). |
| 1592 | */ |
| 1593 | uc->text = w = gtk_label_new("X"); |
| 1594 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0); |
| 1595 | gtk_label_set_line_wrap(GTK_LABEL(w), TRUE); |
| 1596 | uc->textsig = |
| 1597 | gtk_signal_connect(GTK_OBJECT(w), "size-allocate", |
| 1598 | GTK_SIGNAL_FUNC(label_sizealloc), dp); |
| 1599 | break; |
| 1600 | } |
| 1601 | |
| 1602 | assert(w != NULL); |
| 1603 | |
| 1604 | columns_add(cols, w, |
| 1605 | COLUMN_START(ctrl->generic.column), |
| 1606 | COLUMN_SPAN(ctrl->generic.column)); |
| 1607 | if (left) |
| 1608 | columns_force_left_align(cols, w); |
| 1609 | gtk_widget_show(w); |
| 1610 | |
| 1611 | uc->toplevel = w; |
| 1612 | dlg_add_uctrl(dp, uc); |
| 1613 | } |
| 1614 | |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | struct selparam { |
| 1619 | struct dlgparam *dp; |
| 1620 | Panels *panels; |
| 1621 | GtkWidget *panel, *treeitem; |
| 1622 | struct Shortcuts shortcuts; |
| 1623 | }; |
| 1624 | |
| 1625 | static void treeitem_sel(GtkItem *item, gpointer data) |
| 1626 | { |
| 1627 | struct selparam *sp = (struct selparam *)data; |
| 1628 | |
| 1629 | panels_switch_to(sp->panels, sp->panel); |
| 1630 | |
| 1631 | sp->dp->shortcuts = &sp->shortcuts; |
| 1632 | sp->dp->currtreeitem = sp->treeitem; |
| 1633 | } |
| 1634 | |
| 1635 | static void window_destroy(GtkWidget *widget, gpointer data) |
| 1636 | { |
| 1637 | gtk_main_quit(); |
| 1638 | } |
| 1639 | |
| 1640 | static int tree_grab_focus(struct dlgparam *dp) |
| 1641 | { |
| 1642 | int i, f; |
| 1643 | |
| 1644 | /* |
| 1645 | * See if any of the treeitems has the focus. |
| 1646 | */ |
| 1647 | f = -1; |
| 1648 | for (i = 0; i < dp->ntreeitems; i++) |
| 1649 | if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) { |
| 1650 | f = i; |
| 1651 | break; |
| 1652 | } |
| 1653 | |
| 1654 | if (f >= 0) |
| 1655 | return FALSE; |
| 1656 | else { |
| 1657 | gtk_widget_grab_focus(dp->currtreeitem); |
| 1658 | return TRUE; |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | gint tree_focus(GtkContainer *container, GtkDirectionType direction, |
| 1663 | gpointer data) |
| 1664 | { |
| 1665 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1666 | |
| 1667 | gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus"); |
| 1668 | /* |
| 1669 | * If there's a focused treeitem, we return FALSE to cause the |
| 1670 | * focus to move on to some totally other control. If not, we |
| 1671 | * focus the selected one. |
| 1672 | */ |
| 1673 | return tree_grab_focus(dp); |
| 1674 | } |
| 1675 | |
| 1676 | int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
| 1677 | { |
| 1678 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1679 | |
| 1680 | if (event->keyval == GDK_Escape && dp->cancelbutton) { |
| 1681 | gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked"); |
| 1682 | return TRUE; |
| 1683 | } |
| 1684 | |
| 1685 | if ((event->state & GDK_MOD1_MASK) && |
| 1686 | (unsigned char)event->string[0] > 0 && |
| 1687 | (unsigned char)event->string[0] <= 127) { |
| 1688 | int schr = (unsigned char)event->string[0]; |
| 1689 | struct Shortcut *sc = &dp->shortcuts->sc[schr]; |
| 1690 | |
| 1691 | switch (sc->action) { |
| 1692 | case SHORTCUT_TREE: |
| 1693 | tree_grab_focus(dp); |
| 1694 | break; |
| 1695 | case SHORTCUT_FOCUS: |
| 1696 | gtk_widget_grab_focus(sc->widget); |
| 1697 | break; |
| 1698 | case SHORTCUT_UCTRL: |
| 1699 | /* |
| 1700 | * We must do something sensible with a uctrl. |
| 1701 | * Precisely what this is depends on the type of |
| 1702 | * control. |
| 1703 | */ |
| 1704 | switch (sc->uc->ctrl->generic.type) { |
| 1705 | case CTRL_CHECKBOX: |
| 1706 | case CTRL_BUTTON: |
| 1707 | /* Check boxes and buttons get the focus _and_ get toggled. */ |
| 1708 | gtk_widget_grab_focus(sc->uc->toplevel); |
| 1709 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel), |
| 1710 | "clicked"); |
| 1711 | break; |
| 1712 | case CTRL_FILESELECT: |
| 1713 | case CTRL_FONTSELECT: |
| 1714 | /* File/font selectors have their buttons pressed (ooer), |
| 1715 | * and focus transferred to the edit box. */ |
| 1716 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button), |
| 1717 | "clicked"); |
| 1718 | gtk_widget_grab_focus(sc->uc->entry); |
| 1719 | break; |
| 1720 | case CTRL_RADIO: |
| 1721 | /* |
| 1722 | * Radio buttons are fun, because they have |
| 1723 | * multiple shortcuts. We must find whether the |
| 1724 | * activated shortcut is the shortcut for the whole |
| 1725 | * group, or for a particular button. In the former |
| 1726 | * case, we find the currently selected button and |
| 1727 | * focus it; in the latter, we focus-and-click the |
| 1728 | * button whose shortcut was pressed. |
| 1729 | */ |
| 1730 | if (schr == sc->uc->ctrl->radio.shortcut) { |
| 1731 | int i; |
| 1732 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
| 1733 | if (gtk_toggle_button_get_active |
| 1734 | (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) { |
| 1735 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
| 1736 | } |
| 1737 | } else if (sc->uc->ctrl->radio.shortcuts) { |
| 1738 | int i; |
| 1739 | for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++) |
| 1740 | if (schr == sc->uc->ctrl->radio.shortcuts[i]) { |
| 1741 | gtk_widget_grab_focus(sc->uc->buttons[i]); |
| 1742 | gtk_signal_emit_by_name |
| 1743 | (GTK_OBJECT(sc->uc->buttons[i]), "clicked"); |
| 1744 | } |
| 1745 | } |
| 1746 | break; |
| 1747 | case CTRL_LISTBOX: |
| 1748 | /* |
| 1749 | * If the list is really an option menu, we focus |
| 1750 | * and click it. Otherwise we tell it to focus one |
| 1751 | * of its children, which appears to do the Right |
| 1752 | * Thing. |
| 1753 | */ |
| 1754 | if (sc->uc->optmenu) { |
| 1755 | GdkEventButton bev; |
| 1756 | gint returnval; |
| 1757 | |
| 1758 | gtk_widget_grab_focus(sc->uc->optmenu); |
| 1759 | /* Option menus don't work using the "clicked" signal. |
| 1760 | * We need to manufacture a button press event :-/ */ |
| 1761 | bev.type = GDK_BUTTON_PRESS; |
| 1762 | bev.button = 1; |
| 1763 | gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu), |
| 1764 | "button_press_event", |
| 1765 | &bev, &returnval); |
| 1766 | } else { |
| 1767 | assert(sc->uc->list != NULL); |
| 1768 | |
| 1769 | gtk_container_focus(GTK_CONTAINER(sc->uc->list), |
| 1770 | GTK_DIR_TAB_FORWARD); |
| 1771 | } |
| 1772 | break; |
| 1773 | } |
| 1774 | break; |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | return FALSE; |
| 1779 | } |
| 1780 | |
| 1781 | int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
| 1782 | { |
| 1783 | struct dlgparam *dp = (struct dlgparam *)data; |
| 1784 | |
| 1785 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
| 1786 | event->keyval == GDK_Down || event->keyval == GDK_KP_Down) { |
| 1787 | int dir, i, j = -1; |
| 1788 | for (i = 0; i < dp->ntreeitems; i++) |
| 1789 | if (widget == dp->treeitems[i]) |
| 1790 | break; |
| 1791 | if (i < dp->ntreeitems) { |
| 1792 | if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up) |
| 1793 | dir = -1; |
| 1794 | else |
| 1795 | dir = +1; |
| 1796 | |
| 1797 | while (1) { |
| 1798 | i += dir; |
| 1799 | if (i < 0 || i >= dp->ntreeitems) |
| 1800 | break; /* nothing in that dir to select */ |
| 1801 | /* |
| 1802 | * Determine if this tree item is visible. |
| 1803 | */ |
| 1804 | { |
| 1805 | GtkWidget *w = dp->treeitems[i]; |
| 1806 | int vis = TRUE; |
| 1807 | while (w && GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w)) { |
| 1808 | if (!GTK_WIDGET_VISIBLE(w)) { |
| 1809 | vis = FALSE; |
| 1810 | break; |
| 1811 | } |
| 1812 | w = w->parent; |
| 1813 | } |
| 1814 | if (vis) { |
| 1815 | j = i; /* got one */ |
| 1816 | break; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
| 1822 | "key_press_event"); |
| 1823 | if (j >= 0) { |
| 1824 | gint return_val; |
| 1825 | gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle"); |
| 1826 | gtk_widget_grab_focus(dp->treeitems[j]); |
| 1827 | } |
| 1828 | return TRUE; |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | * It's nice for Left and Right to expand and collapse tree |
| 1833 | * branches. |
| 1834 | */ |
| 1835 | if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) { |
| 1836 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
| 1837 | "key_press_event"); |
| 1838 | gtk_tree_item_collapse(GTK_TREE_ITEM(widget)); |
| 1839 | return TRUE; |
| 1840 | } |
| 1841 | if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) { |
| 1842 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), |
| 1843 | "key_press_event"); |
| 1844 | gtk_tree_item_expand(GTK_TREE_ITEM(widget)); |
| 1845 | return TRUE; |
| 1846 | } |
| 1847 | |
| 1848 | return FALSE; |
| 1849 | } |
| 1850 | |
| 1851 | void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw, |
| 1852 | int chr, int action, void *ptr) |
| 1853 | { |
| 1854 | GtkLabel *label = GTK_LABEL(labelw); |
| 1855 | gchar *currstr, *pattern; |
| 1856 | int i; |
| 1857 | |
| 1858 | if (chr == NO_SHORTCUT) |
| 1859 | return; |
| 1860 | |
| 1861 | chr = tolower((unsigned char)chr); |
| 1862 | |
| 1863 | assert(scs->sc[chr].action == SHORTCUT_EMPTY); |
| 1864 | |
| 1865 | scs->sc[chr].action = action; |
| 1866 | |
| 1867 | if (action == SHORTCUT_FOCUS) { |
| 1868 | scs->sc[chr].uc = NULL; |
| 1869 | scs->sc[chr].widget = (GtkWidget *)ptr; |
| 1870 | } else { |
| 1871 | scs->sc[chr].widget = NULL; |
| 1872 | scs->sc[chr].uc = (struct uctrl *)ptr; |
| 1873 | } |
| 1874 | |
| 1875 | gtk_label_get(label, &currstr); |
| 1876 | for (i = 0; currstr[i]; i++) |
| 1877 | if (tolower((unsigned char)currstr[i]) == chr) { |
| 1878 | GtkRequisition req; |
| 1879 | |
| 1880 | pattern = dupprintf("%*s_", i, ""); |
| 1881 | |
| 1882 | gtk_widget_size_request(GTK_WIDGET(label), &req); |
| 1883 | gtk_label_set_pattern(label, pattern); |
| 1884 | gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height); |
| 1885 | |
| 1886 | sfree(pattern); |
| 1887 | break; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | int do_config_box(const char *title) |
| 1892 | { |
| 1893 | GtkWidget *window, *hbox, *vbox, *cols, *label, |
| 1894 | *tree, *treescroll, *panels, *panelvbox; |
| 1895 | int index, level, listitemheight; |
| 1896 | struct controlbox *ctrlbox; |
| 1897 | char *path; |
| 1898 | GtkTreeItem *treeitemlevels[8]; |
| 1899 | GtkTree *treelevels[8]; |
| 1900 | Config cfg; |
| 1901 | struct dlgparam dp; |
| 1902 | struct sesslist sl; |
| 1903 | struct Shortcuts scs; |
| 1904 | |
| 1905 | struct selparam *selparams = NULL; |
| 1906 | int nselparams = 0, selparamsize = 0; |
| 1907 | |
| 1908 | do_defaults(NULL, &cfg); |
| 1909 | |
| 1910 | dlg_init(&dp); |
| 1911 | |
| 1912 | { |
| 1913 | GtkWidget *listitem = gtk_list_item_new_with_label("foo"); |
| 1914 | GtkRequisition req; |
| 1915 | gtk_widget_size_request(listitem, &req); |
| 1916 | listitemheight = req.height; |
| 1917 | gtk_widget_unref(listitem); |
| 1918 | } |
| 1919 | |
| 1920 | sl.nsessions = 0; |
| 1921 | |
| 1922 | for (index = 0; index < lenof(scs.sc); index++) { |
| 1923 | scs.sc[index].action = SHORTCUT_EMPTY; |
| 1924 | } |
| 1925 | |
| 1926 | ctrlbox = ctrl_new_box(); |
| 1927 | setup_config_box(ctrlbox, &sl, FALSE, 0); |
| 1928 | unix_setup_config_box(ctrlbox, FALSE); |
| 1929 | |
| 1930 | window = gtk_dialog_new(); |
| 1931 | gtk_window_set_title(GTK_WINDOW(window), title); |
| 1932 | hbox = gtk_hbox_new(FALSE, 4); |
| 1933 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0); |
| 1934 | gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
| 1935 | gtk_widget_show(hbox); |
| 1936 | vbox = gtk_vbox_new(FALSE, 4); |
| 1937 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
| 1938 | gtk_widget_show(vbox); |
| 1939 | cols = columns_new(4); |
| 1940 | gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0); |
| 1941 | gtk_widget_show(cols); |
| 1942 | label = gtk_label_new("Category:"); |
| 1943 | columns_add(COLUMNS(cols), label, 0, 1); |
| 1944 | columns_force_left_align(COLUMNS(cols), label); |
| 1945 | gtk_widget_show(label); |
| 1946 | treescroll = gtk_scrolled_window_new(NULL, NULL); |
| 1947 | tree = gtk_tree_new(); |
| 1948 | gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event", |
| 1949 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
| 1950 | shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree); |
| 1951 | gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM); |
| 1952 | gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE); |
| 1953 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll), |
| 1954 | tree); |
| 1955 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll), |
| 1956 | GTK_POLICY_NEVER, |
| 1957 | GTK_POLICY_AUTOMATIC); |
| 1958 | gtk_signal_connect(GTK_OBJECT(tree), "focus", |
| 1959 | GTK_SIGNAL_FUNC(tree_focus), &dp); |
| 1960 | gtk_widget_show(tree); |
| 1961 | gtk_widget_show(treescroll); |
| 1962 | gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0); |
| 1963 | panels = panels_new(); |
| 1964 | gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0); |
| 1965 | gtk_widget_show(panels); |
| 1966 | |
| 1967 | panelvbox = NULL; |
| 1968 | path = NULL; |
| 1969 | level = 0; |
| 1970 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
| 1971 | struct controlset *s = ctrlbox->ctrlsets[index]; |
| 1972 | GtkWidget *w; |
| 1973 | |
| 1974 | if (!*s->pathname) { |
| 1975 | w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window)); |
| 1976 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), |
| 1977 | w, TRUE, TRUE, 0); |
| 1978 | } else { |
| 1979 | int j = path ? ctrl_path_compare(s->pathname, path) : 0; |
| 1980 | if (j != INT_MAX) { /* add to treeview, start new panel */ |
| 1981 | char *c; |
| 1982 | GtkWidget *treeitem; |
| 1983 | int first; |
| 1984 | |
| 1985 | /* |
| 1986 | * We expect never to find an implicit path |
| 1987 | * component. For example, we expect never to see |
| 1988 | * A/B/C followed by A/D/E, because that would |
| 1989 | * _implicitly_ create A/D. All our path prefixes |
| 1990 | * are expected to contain actual controls and be |
| 1991 | * selectable in the treeview; so we would expect |
| 1992 | * to see A/D _explicitly_ before encountering |
| 1993 | * A/D/E. |
| 1994 | */ |
| 1995 | assert(j == ctrl_path_elements(s->pathname) - 1); |
| 1996 | |
| 1997 | c = strrchr(s->pathname, '/'); |
| 1998 | if (!c) |
| 1999 | c = s->pathname; |
| 2000 | else |
| 2001 | c++; |
| 2002 | |
| 2003 | treeitem = gtk_tree_item_new_with_label(c); |
| 2004 | assert(j-1 < level); |
| 2005 | if (j > 0) { |
| 2006 | if (!treelevels[j-1]) { |
| 2007 | treelevels[j-1] = GTK_TREE(gtk_tree_new()); |
| 2008 | gtk_tree_item_set_subtree |
| 2009 | (treeitemlevels[j-1], |
| 2010 | GTK_WIDGET(treelevels[j-1])); |
| 2011 | gtk_tree_item_expand(treeitemlevels[j-1]); |
| 2012 | } |
| 2013 | gtk_tree_append(treelevels[j-1], treeitem); |
| 2014 | } else { |
| 2015 | gtk_tree_append(GTK_TREE(tree), treeitem); |
| 2016 | } |
| 2017 | treeitemlevels[j] = GTK_TREE_ITEM(treeitem); |
| 2018 | treelevels[j] = NULL; |
| 2019 | level = j+1; |
| 2020 | |
| 2021 | gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event", |
| 2022 | GTK_SIGNAL_FUNC(tree_key_press), &dp); |
| 2023 | gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event", |
| 2024 | GTK_SIGNAL_FUNC(widget_focus), &dp); |
| 2025 | |
| 2026 | gtk_widget_show(treeitem); |
| 2027 | |
| 2028 | path = s->pathname; |
| 2029 | |
| 2030 | first = (panelvbox == NULL); |
| 2031 | |
| 2032 | panelvbox = gtk_vbox_new(FALSE, 4); |
| 2033 | gtk_container_add(GTK_CONTAINER(panels), panelvbox); |
| 2034 | if (first) { |
| 2035 | panels_switch_to(PANELS(panels), panelvbox); |
| 2036 | gtk_tree_select_child(GTK_TREE(tree), treeitem); |
| 2037 | } |
| 2038 | |
| 2039 | if (nselparams >= selparamsize) { |
| 2040 | selparamsize += 16; |
| 2041 | selparams = srealloc(selparams, |
| 2042 | selparamsize * sizeof(*selparams)); |
| 2043 | } |
| 2044 | selparams[nselparams].dp = &dp; |
| 2045 | selparams[nselparams].panels = PANELS(panels); |
| 2046 | selparams[nselparams].panel = panelvbox; |
| 2047 | selparams[nselparams].shortcuts = scs; /* structure copy */ |
| 2048 | selparams[nselparams].treeitem = treeitem; |
| 2049 | nselparams++; |
| 2050 | |
| 2051 | } |
| 2052 | |
| 2053 | w = layout_ctrls(&dp, |
| 2054 | &selparams[nselparams-1].shortcuts, |
| 2055 | s, listitemheight, NULL); |
| 2056 | gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0); |
| 2057 | gtk_widget_show(w); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | dp.ntreeitems = nselparams; |
| 2062 | dp.treeitems = smalloc(dp.ntreeitems * sizeof(GtkWidget *)); |
| 2063 | |
| 2064 | for (index = 0; index < nselparams; index++) { |
| 2065 | gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select", |
| 2066 | GTK_SIGNAL_FUNC(treeitem_sel), |
| 2067 | &selparams[index]); |
| 2068 | dp.treeitems[index] = selparams[index].treeitem; |
| 2069 | } |
| 2070 | |
| 2071 | dp.data = &cfg; |
| 2072 | dlg_refresh(NULL, &dp); |
| 2073 | |
| 2074 | dp.shortcuts = &selparams[0].shortcuts; |
| 2075 | dp.currtreeitem = dp.treeitems[0]; |
| 2076 | dp.lastfocus = NULL; |
| 2077 | dp.retval = 0; |
| 2078 | dp.window = window; |
| 2079 | |
| 2080 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
| 2081 | gtk_widget_show(window); |
| 2082 | |
| 2083 | /* |
| 2084 | * Set focus into the first available control. |
| 2085 | */ |
| 2086 | for (index = 0; index < ctrlbox->nctrlsets; index++) { |
| 2087 | struct controlset *s = ctrlbox->ctrlsets[index]; |
| 2088 | int done = 0; |
| 2089 | int j; |
| 2090 | |
| 2091 | if (*s->pathname) { |
| 2092 | for (j = 0; j < s->ncontrols; j++) |
| 2093 | if (s->ctrls[j]->generic.type != CTRL_TABDELAY && |
| 2094 | s->ctrls[j]->generic.type != CTRL_COLUMNS && |
| 2095 | s->ctrls[j]->generic.type != CTRL_TEXT) { |
| 2096 | dlg_set_focus(s->ctrls[j], &dp); |
| 2097 | dp.lastfocus = s->ctrls[j]; |
| 2098 | done = 1; |
| 2099 | break; |
| 2100 | } |
| 2101 | } |
| 2102 | if (done) |
| 2103 | break; |
| 2104 | } |
| 2105 | |
| 2106 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
| 2107 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
| 2108 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
| 2109 | GTK_SIGNAL_FUNC(win_key_press), &dp); |
| 2110 | |
| 2111 | gtk_main(); |
| 2112 | |
| 2113 | dlg_cleanup(&dp); |
| 2114 | sfree(selparams); |
| 2115 | |
| 2116 | return dp.retval; |
| 2117 | } |
| 2118 | |
| 2119 | /* ====================================================================== |
| 2120 | * Below here is a stub main program which allows the dialog box |
| 2121 | * code to be compiled and tested with a minimal amount of the rest |
| 2122 | * of PuTTY. |
| 2123 | */ |
| 2124 | |
| 2125 | #ifdef TESTMODE |
| 2126 | |
| 2127 | /* Compile command for testing: |
| 2128 | |
| 2129 | gcc -g -o gtkdlg gtk{dlg,cols,panel}.c ../{config,dialog,settings}.c \ |
| 2130 | ../{misc,tree234,be_none}.c ux{store,misc,print,cfg}.c \ |
| 2131 | -I. -I.. -I../charset -DTESTMODE `gtk-config --cflags --libs` |
| 2132 | |
| 2133 | */ |
| 2134 | |
| 2135 | void modalfatalbox(char *p, ...) |
| 2136 | { |
| 2137 | va_list ap; |
| 2138 | fprintf(stderr, "FATAL ERROR: "); |
| 2139 | va_start(ap, p); |
| 2140 | vfprintf(stderr, p, ap); |
| 2141 | va_end(ap); |
| 2142 | fputc('\n', stderr); |
| 2143 | exit(1); |
| 2144 | } |
| 2145 | |
| 2146 | char *cp_name(int codepage) |
| 2147 | { |
| 2148 | return (codepage == 123 ? "testing123" : |
| 2149 | codepage == 234 ? "testing234" : |
| 2150 | codepage == 345 ? "testing345" : |
| 2151 | "unknown"); |
| 2152 | } |
| 2153 | |
| 2154 | char *cp_enumerate(int index) |
| 2155 | { |
| 2156 | return (index == 0 ? "testing123" : |
| 2157 | index == 1 ? "testing234" : |
| 2158 | NULL); |
| 2159 | } |
| 2160 | |
| 2161 | int decode_codepage(char *cp_name) |
| 2162 | { |
| 2163 | return (!strcmp(cp_name, "testing123") ? 123 : |
| 2164 | !strcmp(cp_name, "testing234") ? 234 : |
| 2165 | !strcmp(cp_name, "testing345") ? 345 : |
| 2166 | -2); |
| 2167 | } |
| 2168 | |
| 2169 | struct printer_enum_tag { int dummy; } printer_test; |
| 2170 | |
| 2171 | printer_enum *printer_start_enum(int *nprinters_ptr) { |
| 2172 | *nprinters_ptr = 2; |
| 2173 | return &printer_test; |
| 2174 | } |
| 2175 | char *printer_get_name(printer_enum *pe, int i) { |
| 2176 | return (i==0 ? "lpr" : i==1 ? "lpr -Pfoobar" : NULL); |
| 2177 | } |
| 2178 | void printer_finish_enum(printer_enum *pe) { } |
| 2179 | |
| 2180 | char *platform_default_s(const char *name) |
| 2181 | { |
| 2182 | return NULL; |
| 2183 | } |
| 2184 | |
| 2185 | int platform_default_i(const char *name, int def) |
| 2186 | { |
| 2187 | return def; |
| 2188 | } |
| 2189 | |
| 2190 | FontSpec platform_default_fontspec(const char *name) |
| 2191 | { |
| 2192 | FontSpec ret; |
| 2193 | if (!strcmp(name, "Font")) |
| 2194 | strcpy(ret.name, "fixed"); |
| 2195 | else |
| 2196 | *ret.name = '\0'; |
| 2197 | return ret; |
| 2198 | } |
| 2199 | |
| 2200 | Filename platform_default_filename(const char *name) |
| 2201 | { |
| 2202 | Filename ret; |
| 2203 | if (!strcmp(name, "LogFileName")) |
| 2204 | strcpy(ret.path, "putty.log"); |
| 2205 | else |
| 2206 | *ret.path = '\0'; |
| 2207 | return ret; |
| 2208 | } |
| 2209 | |
| 2210 | char *x_get_default(const char *key) |
| 2211 | { |
| 2212 | return NULL; |
| 2213 | } |
| 2214 | |
| 2215 | int main(int argc, char **argv) |
| 2216 | { |
| 2217 | gtk_init(&argc, &argv); |
| 2218 | printf("returned %d\n", do_config_box("PuTTY Configuration")); |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| 2222 | #endif |