dialog-view.c 5.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * dialog-view.c:  New view dialog.
 *
 * Author:
 *        Morten Welinder <terra@gnome.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as published
 * by the Free Software Foundation.
 *
 * 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
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gnumeric-config.h>
#include <gnumeric.h>
#include "dialogs.h"
24
#include "help.h"
25
#include "application.h"
26 27
#include <glade/glade.h>
#include <gui-util.h>
28 29
#include <wbc-gtk.h>

30
#include <gtk/gtkradiobutton.h>
31
#include <gtk/gtkbox.h>
32
#include <glib/gi18n-lib.h>
33 34 35 36

#define VIEW_DIALOG_KEY "view-dialog"

typedef struct {
37
	WBCGtk *wbcg;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	GtkWidget          *dialog;
	GladeXML           *gui;
	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)
{
55
	WBCGtk *wbcg = state->wbcg;
56 57
	WorkbookControl *wbc = WORKBOOK_CONTROL (wbcg);
	WorkbookControl *new_wbc;
58
	gboolean shared;
59
	GdkScreen *screen;
60 61
	GSList *buttons = gtk_radio_button_get_group (state->location_elsewhere);

62
	shared = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (glade_xml_get_widget (state->gui, "view_shared")));
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	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);
		if (!display) {		
			char *error_str =
				g_strdup_printf (_("Display \"%s\" could not be opened."),
						 name);				
			gtk_widget_destroy (state->dialog);
Jody Goldberg's avatar
Jody Goldberg committed
85
			go_gtk_notice_dialog (wbcg_toplevel (wbcg),
86
					 GTK_MESSAGE_ERROR, 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 = wb_control_wrapper_new
99 100
		(wbc,
		 shared ? wb_control_view (wbc) : NULL,
101
		 wb_control_get_workbook (wbc),
102
		 screen);
103

104
	if (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 116 117 118 119 120 121 122 123 124
}

static void
cb_view_destroy (ViewState *state)
{
	if (state->gui != NULL) {
		g_object_unref (G_OBJECT (state->gui));
		state->gui = NULL;
	}

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

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

	if (gnumeric_dialog_raise_if_exists (wbcg, VIEW_DIALOG_KEY))
		return;
132
	gui = gnm_glade_xml_new (GO_CMD_CONTEXT (wbcg),
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
				 "view.glade", NULL, NULL);
	if (gui == NULL)
		return;

	state = g_new (ViewState, 1);
	state->wbcg = wbcg;
	state->gui = gui;
	state->dialog = glade_xml_get_widget (gui, "View");
	state->location_elsewhere = GTK_RADIO_BUTTON (glade_xml_get_widget (gui, "location_elsewhere"));
	state->location_display_name = GTK_ENTRY (glade_xml_get_widget (gui, "location_display_name"));
	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);
		GtkBox *box = GTK_BOX (glade_xml_get_widget (gui, "location_screens_vbox"));
		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);
			char *label = 
				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);
169 170 171

			g_object_set_data (G_OBJECT (button),
					   "screen", screen);
172 173 174 175 176 177 178 179 180 181 182 183

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

	g_signal_connect (G_OBJECT (glade_xml_get_widget (gui, "ok_button")),
			  "clicked",
			  G_CALLBACK (cb_view_ok_clicked), state);
	g_signal_connect (G_OBJECT (glade_xml_get_widget (gui, "cancel_button")),
			  "clicked",
			  G_CALLBACK (cb_view_cancel_clicked), state);

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

187 188 189 190 191
	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_WIDGET (state->location_display_name));
	
	gnumeric_init_help_button (
		glade_xml_get_widget (gui, "help_button"),
192
		GNUMERIC_HELP_LINK_VIEW);
193 194 195 196 197 198 199
	gnumeric_keyed_dialog (wbcg, GTK_WINDOW (state->dialog),
			       VIEW_DIALOG_KEY);

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