1.12.42
[gnumeric.git] / src / dialogs / dialog-view.c
blob15e1710de74009ea20a56c841705d2e37f39b5be
1 /*
2 * dialog-view.c: New view dialog.
4 * Author:
5 * Morten Welinder <terra@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
23 #include <gnumeric-config.h>
24 #include <gnumeric.h>
25 #include <dialogs/dialogs.h>
26 #include <dialogs/help.h>
27 #include <application.h>
28 #include <gui-util.h>
29 #include <wbc-gtk.h>
31 #include <glib/gi18n-lib.h>
33 #define VIEW_DIALOG_KEY "view-dialog"
35 typedef struct {
36 WBCGtk *wbcg;
37 GtkWidget *dialog;
38 GtkBuilder *gui;
39 GtkRadioButton *location_elsewhere;
40 GtkEntry *location_display_name;
41 } ViewState;
43 static void
44 cb_view_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
45 ViewState *state)
47 gtk_widget_destroy (state->dialog);
50 static void
51 cb_view_ok_clicked (G_GNUC_UNUSED GtkWidget *button,
52 ViewState *state)
54 WBCGtk *wbcg = state->wbcg;
55 WorkbookControl *wbc = GNM_WBC (wbcg);
56 WorkbookControl *new_wbc;
57 gboolean shared;
58 GdkScreen *screen;
59 GSList *buttons = gtk_radio_button_get_group (state->location_elsewhere);
61 shared = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget (state->gui, "view_shared")));
63 while (buttons)
64 if (gtk_toggle_button_get_active (buttons->data))
65 break;
66 else
67 buttons = buttons->next;
69 if (!buttons) {
70 g_assert_not_reached ();
71 return;
72 } else if (buttons->data == state->location_elsewhere) {
73 const char *name = gtk_entry_get_text (state->location_display_name);
74 GdkDisplay *display;
75 if (!name)
76 return; /* Just ignore */
78 display = gdk_display_open (name);
79 if (!display) {
80 char *error_str =
81 g_strdup_printf (_("Display \"%s\" could not be opened."),
82 name);
83 gtk_widget_destroy (state->dialog);
84 go_gtk_notice_dialog (wbcg_toplevel (wbcg),
85 GTK_MESSAGE_ERROR,
86 "%s", error_str);
87 g_free (error_str);
88 return;
91 screen = gdk_display_get_default_screen (display);
92 } else {
93 screen = g_object_get_data (buttons->data, "screen");
96 gtk_widget_destroy (state->dialog);
98 new_wbc = workbook_control_new_wrapper
99 (wbc,
100 shared ? wb_control_view (wbc) : NULL,
101 wb_control_get_workbook (wbc),
102 screen);
104 if (GNM_IS_WBC_GTK (new_wbc)) {
105 /* What else would it be? */
106 WBCGtk *new_wbcg = WBC_GTK (new_wbc);
107 wbcg_copy_toolbar_visibility (new_wbcg, wbcg);
108 gnm_app_flag_windows_changed_ ();
112 static void
113 cb_view_destroy (ViewState *state)
115 if (state->gui != NULL) {
116 g_object_unref (state->gui);
117 state->gui = NULL;
120 state->dialog = NULL;
121 g_free (state);
124 void
125 dialog_new_view (WBCGtk *wbcg)
127 ViewState *state;
128 GtkBuilder *gui;
130 if (gnm_dialog_raise_if_exists (wbcg, VIEW_DIALOG_KEY))
131 return;
132 gui = gnm_gtk_builder_load ("res:ui/view.ui", NULL, GO_CMD_CONTEXT (wbcg));
133 if (gui == NULL)
134 return;
136 state = g_new (ViewState, 1);
137 state->wbcg = wbcg;
138 state->gui = gui;
139 state->dialog = go_gtk_builder_get_widget (gui, "View");
140 state->location_elsewhere = GTK_RADIO_BUTTON (go_gtk_builder_get_widget (gui, "location_elsewhere"));
141 state->location_display_name = GTK_ENTRY (go_gtk_builder_get_widget (gui, "location_display_name"));
142 g_return_if_fail (state->dialog != NULL);
145 GdkScreen *this_screen = gtk_window_get_screen (wbcg_toplevel (wbcg));
146 GdkDisplay *this_display = gdk_screen_get_display (this_screen);
147 int n_screens = gdk_display_get_n_screens (this_display);
148 GtkBox *box = GTK_BOX (go_gtk_builder_get_widget (gui, "location_screens_vbox"));
149 int i;
151 for (i = 0; i < n_screens; i++) {
152 /* Get this for every screen -- it might change. */
153 GSList *group =
154 gtk_radio_button_get_group (state->location_elsewhere);
155 GdkScreen *screen = gdk_display_get_screen (this_display, i);
156 char *label =
157 screen == this_screen
158 ? (n_screens == 1
159 ? g_strdup (_("This screen"))
160 : g_strdup_printf (_("Screen %d [This screen]"), i))
161 : g_strdup_printf (_("Screen %d"), i);
162 GtkWidget *button =
163 gtk_radio_button_new_with_label (group, label);
164 g_free (label);
166 if (screen == this_screen)
167 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
169 g_object_set_data (G_OBJECT (button),
170 "screen", screen);
172 gtk_box_pack_start (box, button, TRUE, TRUE, 0);
176 g_signal_connect (G_OBJECT (go_gtk_builder_get_widget (gui, "ok_button")),
177 "clicked",
178 G_CALLBACK (cb_view_ok_clicked), state);
179 g_signal_connect (G_OBJECT (go_gtk_builder_get_widget (gui, "cancel_button")),
180 "clicked",
181 G_CALLBACK (cb_view_cancel_clicked), state);
183 gnm_link_button_and_entry (GTK_WIDGET (state->location_elsewhere),
184 GTK_WIDGET (state->location_display_name));
186 gnm_editable_enters (GTK_WINDOW (state->dialog),
187 GTK_WIDGET (state->location_display_name));
189 gnm_init_help_button (
190 go_gtk_builder_get_widget (gui, "help_button"),
191 GNUMERIC_HELP_LINK_VIEW);
192 gnm_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
193 VIEW_DIALOG_KEY);
195 g_object_set_data_full (G_OBJECT (state->dialog),
196 "state", state, (GDestroyNotify) cb_view_destroy);
197 gtk_widget_show_all (state->dialog);