dialog-view.c 5.54 KB
Newer Older
1
/*
2 3 4 5 6
 * dialog-view.c:  New view dialog.
 *
 * Author:
 *        Morten Welinder <terra@gnome.org>
 *
7 8 9 10
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) version 3.
11 12 13 14 15 16 17 18
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
19 20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
21 22 23 24 25
 */

#include <gnumeric-config.h>
#include <gnumeric.h>
#include "dialogs.h"
26
#include "help.h"
27
#include "application.h"
28
#include <gui-util.h>
29 30 31
#include <wbc-gtk.h>

#include <glib/gi18n-lib.h>
32 33 34 35

#define VIEW_DIALOG_KEY "view-dialog"

typedef struct {
36
	WBCGtk *wbcg;
37
	GtkWidget          *dialog;
38
	GtkBuilder         *gui;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	GtkRadioButton     *location_elsewhere;
	GtkEntry           *location_display_name;
} ViewState;

static void
cb_view_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
			ViewState *state)
{
	gtk_widget_destroy (state->dialog);
}

static void
cb_view_ok_clicked (G_GNUC_UNUSED GtkWidget *button,
		    ViewState *state)
{
54
	WBCGtk *wbcg = state->wbcg;
Morten Welinder's avatar
Morten Welinder committed
55
	WorkbookControl *wbc = GNM_WBC (wbcg);
56
	WorkbookControl *new_wbc;
57
	gboolean shared;
58
	GdkScreen *screen;
59 60
	GSList *buttons = gtk_radio_button_get_group (state->location_elsewhere);

Morten Welinder's avatar
Morten Welinder committed
61
	shared = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget (state->gui, "view_shared")));
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

	while (buttons)
		if (gtk_toggle_button_get_active (buttons->data))
			break;
		else
			buttons = buttons->next;

	if (!buttons) {
		g_assert_not_reached ();
		return;
	} else if (buttons->data == state->location_elsewhere) {
		const char *name = gtk_entry_get_text (state->location_display_name);
		GdkDisplay *display;
		if (!name)
			return;  /* Just ignore */

		display = gdk_display_open (name);
Morten Welinder's avatar
Morten Welinder committed
79
		if (!display) {
80 81
			char *error_str =
				g_strdup_printf (_("Display \"%s\" could not be opened."),
Morten Welinder's avatar
Morten Welinder committed
82
						 name);
83
			gtk_widget_destroy (state->dialog);
Jody Goldberg's avatar
Jody Goldberg committed
84
			go_gtk_notice_dialog (wbcg_toplevel (wbcg),
85 86
					      GTK_MESSAGE_ERROR,
					      "%s", error_str);
87 88 89 90 91 92 93 94 95 96 97
			g_free (error_str);
			return;
		}

		screen = gdk_display_get_default_screen (display);
	} else {
		screen = g_object_get_data (buttons->data, "screen");
	}

	gtk_widget_destroy (state->dialog);

98
	new_wbc = workbook_control_new_wrapper
99 100
		(wbc,
		 shared ? wb_control_view (wbc) : NULL,
101
		 wb_control_get_workbook (wbc),
102
		 screen);
103

Morten Welinder's avatar
Morten Welinder committed
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 ();
109
	}
110 111 112 113 114 115
}

static void
cb_view_destroy (ViewState *state)
{
	if (state->gui != NULL) {
116
		g_object_unref (state->gui);
117 118 119 120 121 122 123 124
		state->gui = NULL;
	}

	state->dialog = NULL;
	g_free (state);
}

void
125
dialog_new_view (WBCGtk *wbcg)
126 127
{
	ViewState *state;
128
	GtkBuilder *gui;
129

Morten Welinder's avatar
Morten Welinder committed
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 134 135 136 137 138
	if (gui == NULL)
		return;

	state = g_new (ViewState, 1);
	state->wbcg = wbcg;
	state->gui = gui;
Morten Welinder's avatar
Morten Welinder committed
139 140 141
	state->dialog = go_gtk_builder_get_widget (gui, "View");
	state->location_elsewhere = GTK_RADIO_BUTTON (go_gtk_builder_get_widget (gui, "location_elsewhere"));
	state->location_display_name = GTK_ENTRY (go_gtk_builder_get_widget (gui, "location_display_name"));
142 143 144 145 146 147
	g_return_if_fail (state->dialog != NULL);

	{
		GdkScreen *this_screen = gtk_window_get_screen (wbcg_toplevel (wbcg));
		GdkDisplay *this_display = gdk_screen_get_display (this_screen);
		int n_screens = gdk_display_get_n_screens (this_display);
Morten Welinder's avatar
Morten Welinder committed
148
		GtkBox *box = GTK_BOX (go_gtk_builder_get_widget (gui, "location_screens_vbox"));
149 150 151 152 153 154 155
		int i;

		for (i = 0; i < n_screens; i++) {
			/* Get this for every screen -- it might change.  */
			GSList *group =
				gtk_radio_button_get_group (state->location_elsewhere);
			GdkScreen *screen = gdk_display_get_screen (this_display, i);
Morten Welinder's avatar
Morten Welinder committed
156
			char *label =
157 158 159 160 161 162 163 164 165 166 167
				screen == this_screen
				? (n_screens == 1
				   ? g_strdup (_("This screen"))
				   : g_strdup_printf (_("Screen %d [This screen]"), i))
				: g_strdup_printf (_("Screen %d"), i);
			GtkWidget *button =
				gtk_radio_button_new_with_label (group, label);
			g_free (label);

			if (screen == this_screen)
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
168 169 170

			g_object_set_data (G_OBJECT (button),
					   "screen", screen);
171 172 173 174 175

			gtk_box_pack_start (box, button, TRUE, TRUE, 0);
		}
	}

Morten Welinder's avatar
Morten Welinder committed
176
	g_signal_connect (G_OBJECT (go_gtk_builder_get_widget (gui, "ok_button")),
177 178
			  "clicked",
			  G_CALLBACK (cb_view_ok_clicked), state);
Morten Welinder's avatar
Morten Welinder committed
179
	g_signal_connect (G_OBJECT (go_gtk_builder_get_widget (gui, "cancel_button")),
180 181 182
			  "clicked",
			  G_CALLBACK (cb_view_cancel_clicked), state);

Morten Welinder's avatar
Morten Welinder committed
183 184 185
	gnm_link_button_and_entry (GTK_WIDGET (state->location_elsewhere),
				   GTK_WIDGET (state->location_display_name));

Morten Welinder's avatar
Morten Welinder committed
186
	gnm_editable_enters (GTK_WINDOW (state->dialog),
187
				  GTK_WIDGET (state->location_display_name));
Morten Welinder's avatar
Morten Welinder committed
188

Morten Welinder's avatar
Morten Welinder committed
189
	gnm_init_help_button (
Morten Welinder's avatar
Morten Welinder committed
190
		go_gtk_builder_get_widget (gui, "help_button"),
191
		GNUMERIC_HELP_LINK_VIEW);
Morten Welinder's avatar
Morten Welinder committed
192
	gnm_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
193 194 195 196 197 198
			       VIEW_DIALOG_KEY);

	g_object_set_data_full (G_OBJECT (state->dialog),
				"state", state, (GDestroyNotify) cb_view_destroy);
	gtk_widget_show_all (state->dialog);
}