1.12.42
[gnumeric.git] / src / dialogs / dialog-so-list.c
blobf3e4b170066f97fd54d9804fb9b52454159d754e
2 /*
3 * dialog-so-list.c: A property dialog for lists and combos
5 * Copyright (C) 2006 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
22 #include <gnumeric-config.h>
23 #include <gnumeric.h>
24 #include <dialogs/dialogs.h>
25 #include <dialogs/help.h>
27 #include <expr.h>
28 #include <selection.h>
29 #include <sheet.h>
30 #include <sheet-view.h>
31 #include <sheet-object-widget.h>
32 #include <workbook.h>
33 #include <workbook-control.h>
34 #include <wbc-gtk.h>
35 #include <gui-util.h>
36 #include <parse-util.h>
37 #include <commands.h>
38 #include <widgets/gnm-expr-entry.h>
39 #include <glib/gi18n.h>
41 #define DIALOG_SO_LIST_KEY "so-list"
43 typedef struct {
44 GtkWidget *dialog;
45 GtkWidget *as_index_radio;
46 GnmExprEntry *content_entry, *link_entry;
48 WBCGtk *wbcg;
49 SheetObject *so;
50 } GnmDialogSOList;
52 static GnmExprEntry *
53 init_entry (GnmDialogSOList *state, GtkBuilder *gui, int col, int row,
54 GnmExprTop const *texpr)
56 GnmExprEntry *gee = gnm_expr_entry_new (state->wbcg, TRUE);
57 GtkWidget *w = GTK_WIDGET (gee);
58 GtkGrid *grid = GTK_GRID (gtk_builder_get_object (gui, "main-grid"));
59 Sheet *sheet = sheet_object_get_sheet (state->so);
60 GnmParsePos pp;
62 g_return_val_if_fail (w != NULL, NULL);
64 gtk_grid_attach (grid, w, col, row, 1, 1);
65 gnm_expr_entry_set_flags (gee, GNM_EE_FORCE_ABS_REF |
66 GNM_EE_SHEET_OPTIONAL |
67 GNM_EE_SINGLE_RANGE, GNM_EE_MASK);
69 parse_pos_init_sheet (&pp, sheet);
70 gnm_expr_entry_load_from_expr (gee, texpr, &pp);
71 return gee;
74 static void
75 cb_so_list_response (GtkWidget *dialog, gint response_id, GnmDialogSOList *state)
77 if (response_id == GTK_RESPONSE_HELP)
78 return;
79 if (response_id == GTK_RESPONSE_OK) {
80 GnmParsePos pp;
81 Sheet *sheet = sheet_object_get_sheet (state->so);
82 GnmExprTop const *output;
83 GnmExprTop const *content;
85 parse_pos_init (&pp, sheet->workbook, sheet, 0, 0);
86 output = gnm_expr_entry_parse (state->link_entry,
87 &pp, NULL, FALSE, GNM_EXPR_PARSE_FORCE_ABSOLUTE_REFERENCES);
88 content = gnm_expr_entry_parse (state->content_entry,
89 &pp, NULL, FALSE, GNM_EXPR_PARSE_FORCE_ABSOLUTE_REFERENCES);
90 cmd_so_set_links (GNM_WBC (state->wbcg), state->so, output, content,
91 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
92 (state->as_index_radio)));
95 gtk_widget_destroy (dialog);
98 static gboolean
99 so_list_init (GnmDialogSOList *state, WBCGtk *wbcg, SheetObject *so)
101 GnmExprTop const *texpr;
102 GtkBuilder *gui;
104 gui = gnm_gtk_builder_load ("res:ui/so-list.ui", NULL, GO_CMD_CONTEXT (wbcg));
105 if (gui == NULL)
106 return TRUE;
108 state->wbcg = wbcg;
109 state->so = so;
110 state->dialog = go_gtk_builder_get_widget (gui, "SOList");
112 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog),
113 state->wbcg,
114 GNM_DIALOG_DESTROY_CURRENT_SHEET_REMOVED);
116 texpr = sheet_widget_list_base_get_content_link (so);
117 state->content_entry = init_entry (state, gui, 1, 4, texpr);
118 if (texpr) gnm_expr_top_unref (texpr);
120 texpr = sheet_widget_list_base_get_result_link (so);
121 state->link_entry = init_entry (state, gui, 1, 0, texpr);
122 if (texpr) gnm_expr_top_unref (texpr);
124 state->as_index_radio = go_gtk_builder_get_widget (gui, "as-index-radio");
125 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (state->as_index_radio),
126 sheet_widget_list_base_result_type_is_index (so));
128 g_signal_connect (G_OBJECT (state->dialog), "response",
129 G_CALLBACK (cb_so_list_response), state);
130 gnm_init_help_button (
131 go_gtk_builder_get_widget (gui, "help"),
132 GNUMERIC_HELP_LINK_SO_LIST);
134 /* a candidate for merging into attach guru */
135 gnm_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
136 DIALOG_SO_LIST_KEY);
137 g_object_set_data_full (G_OBJECT (state->dialog),
138 "state", state, g_free);
139 go_gtk_nonmodal_dialog (wbcg_toplevel (state->wbcg),
140 GTK_WINDOW (state->dialog));
141 wbc_gtk_attach_guru (state->wbcg, state->dialog);
142 gtk_widget_show_all (GTK_WIDGET (state->dialog));
143 g_object_unref (gui);
145 return FALSE;
148 void
149 dialog_so_list (WBCGtk *wbcg, GObject *so)
151 GnmDialogSOList *state;
153 g_return_if_fail (wbcg != NULL);
155 if (wbc_gtk_get_guru (wbcg) ||
156 gnm_dialog_raise_if_exists (wbcg, DIALOG_SO_LIST_KEY))
157 return;
159 state = g_new0 (GnmDialogSOList, 1);
160 if (so_list_init (state, wbcg, GNM_SO (so))) {
161 go_gtk_notice_dialog (wbcg_toplevel (wbcg), GTK_MESSAGE_ERROR,
162 _("Could not create the List Property dialog."));
163 g_free (state);