+ case C_CHOICES:
+ /*
+ * Drop-down list (GtkOptionMenu).
+ */
+
+ w = gtk_label_new(i->name);
+ gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
+ gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
+ GTK_SHRINK | GTK_FILL,
+ GTK_EXPAND | GTK_SHRINK | GTK_FILL ,
+ 3, 3);
+ gtk_widget_show(w);
+
+ w = gtk_option_menu_new();
+ gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
+ GTK_EXPAND | GTK_SHRINK | GTK_FILL,
+ GTK_EXPAND | GTK_SHRINK | GTK_FILL,
+ 3, 3);
+ gtk_widget_show(w);
+
+ {
+ int c, val;
+ char *p, *q, *name;
+ GtkWidget *menuitem;
+ GtkWidget *menu = gtk_menu_new();
+
+ gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
+
+ c = *i->sval;
+ p = i->sval+1;
+ val = 0;
+
+ while (*p) {
+ q = p;
+ while (*q && *q != c)
+ q++;
+
+ name = snewn(q-p+1, char);
+ strncpy(name, p, q-p);
+ name[q-p] = '\0';
+
+ if (*q) q++; /* eat delimiter */
+
+ menuitem = gtk_menu_item_new_with_label(name);
+ gtk_container_add(GTK_CONTAINER(menu), menuitem);
+ gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
+ GINT_TO_POINTER(val));
+ gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
+ GTK_SIGNAL_FUNC(droplist_sel), i);
+ gtk_widget_show(menuitem);
+
+ val++;
+
+ p = q;
+ }
+
+ gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
+ }
+
+ break;
+ }
+
+ y++;
+ }
+
+ gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
+ GTK_SIGNAL_FUNC(window_destroy), NULL);
+ gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
+ GTK_SIGNAL_FUNC(win_key_press), cancel);
+ gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
+ gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
+ GTK_WINDOW(fe->window));
+ /* set_transient_window_pos(fe->window, fe->cfgbox); */
+ gtk_widget_show(fe->cfgbox);
+ gtk_main();
+
+ free_cfg(fe->cfg);
+
+ return fe->cfgret;
+}
+
+static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+ int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
+ "user-data"));
+ if (!midend_process_key(fe->me, 0, 0, key))
+ gtk_widget_destroy(fe->window);
+}
+
+static void get_size(frontend *fe, int *px, int *py)
+{
+ int x, y;
+
+ /*
+ * Currently I don't want to make the GTK port scale large
+ * puzzles to fit on the screen. This is because X does permit
+ * extremely large windows and many window managers provide a
+ * means of navigating round them, and the users I consulted
+ * before deciding said that they'd rather have enormous puzzle
+ * windows spanning multiple screen pages than have them
+ * shrunk. I could change my mind later or introduce
+ * configurability; this would be the place to do so, by
+ * replacing the initial values of x and y with the screen
+ * dimensions.
+ */
+ x = INT_MAX;
+ y = INT_MAX;
+ midend_size(fe->me, &x, &y, FALSE);
+ *px = x;
+ *py = y;
+}
+
+#if !GTK_CHECK_VERSION(2,0,0)
+#define gtk_window_resize(win, x, y) \
+ gdk_window_resize(GTK_WIDGET(win)->window, x, y)
+#endif
+
+/*
+ * Called when any other code in this file has changed the
+ * selected game parameters.
+ */
+static void changed_preset(frontend *fe)
+{
+ int n = midend_which_preset(fe->me);
+
+ fe->preset_threaded = TRUE;
+ if (n < 0 && fe->preset_custom) {
+ gtk_check_menu_item_set_active(
+ GTK_CHECK_MENU_ITEM(fe->preset_custom),
+ TRUE);
+ } else {
+ GSList *gs = fe->preset_radio;
+ int i = fe->n_preset_menu_items - 1 - n;
+ if (fe->preset_custom)
+ gs = gs->next;
+ while (i && gs) {
+ i--;
+ gs = gs->next;
+ }
+ if (gs) {
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs->data),
+ TRUE);
+ } else for (gs = fe->preset_radio; gs; gs = gs->next) {
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs->data),
+ FALSE);
+ }
+ }
+ fe->preset_threaded = FALSE;
+
+ /*
+ * Update the greying on the Copy menu option.
+ */
+ if (fe->copy_menu_item) {
+ int enabled = midend_can_format_as_text_now(fe->me);
+ gtk_widget_set_sensitive(fe->copy_menu_item, enabled);
+ }
+}
+
+static gboolean not_size_allocated_yet(GtkWidget *w)
+{
+ /*
+ * This function tests whether a widget has not yet taken up space
+ * on the screen which it will occupy in future. (Therefore, it
+ * returns true only if the widget does exist but does not have a
+ * size allocation. A null widget is already taking up all the
+ * space it ever will.)
+ */
+ if (!w)
+ return FALSE; /* nonexistent widgets aren't a problem */
+
+#if GTK_CHECK_VERSION(2,18,0) /* skip if no gtk_widget_get_allocation */
+ {
+ GtkAllocation a;
+ gtk_widget_get_allocation(w, &a);
+ if (a.height == 0 || a.width == 0)
+ return TRUE; /* widget exists but has no size yet */
+ }
+#endif
+
+ return FALSE;
+}
+
+static void try_shrink_drawing_area(frontend *fe)
+{
+ if (fe->drawing_area_shrink_pending &&
+ !not_size_allocated_yet(fe->menubar) &&
+ !not_size_allocated_yet(fe->statusbar)) {
+ /*
+ * In order to permit the user to resize the window smaller as
+ * well as bigger, we call this function after the window size
+ * has ended up where we want it. This shouldn't shrink the
+ * window immediately; it just arranges that the next time the
+ * user tries to shrink it, they can.
+ *
+ * However, at puzzle creation time, we defer the first of
+ * these operations until after the menu bar and status bar
+ * are actually visible. On Ubuntu 12.04 I've found that these
+ * can take a while to be displayed, and that it's a mistake
+ * to reduce the drawing area's size allocation before they've
+ * turned up or else the drawing area makes room for them by
+ * shrinking to less than the size we intended.
+ */
+ gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1);
+ fe->drawing_area_shrink_pending = FALSE;
+ }
+}
+
+static gint configure_window(GtkWidget *widget,
+ GdkEventConfigure *event, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+ /*
+ * When the main puzzle window changes size, it might be because
+ * the menu bar or status bar has turned up after starting off
+ * absent, in which case we should have another go at enacting a
+ * pending shrink of the drawing area.
+ */
+ try_shrink_drawing_area(fe);
+ return FALSE;
+}
+
+static void resize_fe(frontend *fe)
+{
+ int x, y;
+
+ get_size(fe, &x, &y);
+ fe->w = x;
+ fe->h = y;
+ fe->drawing_area_shrink_pending = FALSE;
+ gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
+ {
+ GtkRequisition req;
+ gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
+ gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
+ }
+ fe->drawing_area_shrink_pending = TRUE;
+ try_shrink_drawing_area(fe);
+}
+
+static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+ game_params *params =
+ (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
+
+ if (fe->preset_threaded ||
+ (GTK_IS_CHECK_MENU_ITEM(menuitem) &&
+ !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem))))
+ return;
+ midend_set_params(fe->me, params);
+ midend_new_game(fe->me);
+ changed_preset(fe);
+ resize_fe(fe);
+}
+
+GdkAtom compound_text_atom, utf8_string_atom;
+int paste_initialised = FALSE;
+
+static void set_selection(frontend *fe, GdkAtom selection)
+{
+ if (!paste_initialised) {
+ compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
+ utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
+ paste_initialised = TRUE;
+ }
+
+ /*
+ * For this simple application we can safely assume that the
+ * data passed to this function is pure ASCII, which means we
+ * can return precisely the same stuff for types STRING,
+ * COMPOUND_TEXT or UTF8_STRING.
+ */
+
+ if (gtk_selection_owner_set(fe->area, selection, CurrentTime)) {
+ gtk_selection_clear_targets(fe->area, selection);
+ gtk_selection_add_target(fe->area, selection,
+ GDK_SELECTION_TYPE_STRING, 1);
+ gtk_selection_add_target(fe->area, selection, compound_text_atom, 1);
+ gtk_selection_add_target(fe->area, selection, utf8_string_atom, 1);
+ }
+}
+
+void write_clip(frontend *fe, char *data)
+{
+ if (fe->paste_data)
+ sfree(fe->paste_data);
+
+ fe->paste_data = data;
+ fe->paste_data_len = strlen(data);
+
+ set_selection(fe, GDK_SELECTION_PRIMARY);
+ set_selection(fe, GDK_SELECTION_CLIPBOARD);
+}
+
+void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
+ guint info, guint time_stamp, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+ gtk_selection_data_set(seldata, seldata->target, 8,
+ fe->paste_data, fe->paste_data_len);
+}
+
+gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
+ gpointer data)
+{
+ frontend *fe = (frontend *)data;
+
+ if (fe->paste_data)
+ sfree(fe->paste_data);
+ fe->paste_data = NULL;
+ fe->paste_data_len = 0;
+ return TRUE;
+}
+
+static void menu_copy_event(GtkMenuItem *menuitem, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+ char *text;
+
+ text = midend_text_format(fe->me);
+
+ if (text) {
+ write_clip(fe, text);
+ } else {
+ gdk_beep();
+ }
+}
+
+#ifdef OLD_FILESEL
+
+static void filesel_ok(GtkButton *button, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+
+ gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
+
+ const char *name =
+ gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
+
+ fe->filesel_name = dupstr(name);
+}
+
+static char *file_selector(frontend *fe, char *title, int save)
+{
+ GtkWidget *filesel =
+ gtk_file_selection_new(title);
+
+ fe->filesel_name = NULL;
+
+ gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
+ gtk_object_set_data
+ (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
+ (gpointer)filesel);
+ gtk_signal_connect
+ (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
+ GTK_SIGNAL_FUNC(filesel_ok), fe);
+ gtk_signal_connect_object
+ (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
+ GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
+ gtk_signal_connect_object
+ (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
+ GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
+ gtk_signal_connect(GTK_OBJECT(filesel), "destroy",
+ GTK_SIGNAL_FUNC(window_destroy), NULL);
+ gtk_widget_show(filesel);
+ gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window));
+ gtk_main();
+
+ return fe->filesel_name;
+}
+
+#else
+
+static char *file_selector(frontend *fe, char *title, int save)
+{
+ char *filesel_name = NULL;
+
+ GtkWidget *filesel =
+ gtk_file_chooser_dialog_new(title,
+ GTK_WINDOW(fe->window),
+ save ? GTK_FILE_CHOOSER_ACTION_SAVE :
+ GTK_FILE_CHOOSER_ACTION_OPEN,
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+ save ? GTK_STOCK_SAVE : GTK_STOCK_OPEN,
+ GTK_RESPONSE_ACCEPT,
+ NULL);
+
+ if (gtk_dialog_run(GTK_DIALOG(filesel)) == GTK_RESPONSE_ACCEPT) {
+ const char *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filesel));
+ filesel_name = dupstr(name);
+ }
+
+ gtk_widget_destroy(filesel);