dialog-view.c 5.73 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 24
/**
 * 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-i18n.h>
#include <gnumeric.h>
#include "dialogs.h"
25
#include "help.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <glade/glade.h>
#include <gui-util.h>
#include <workbook-control-gui.h>

#define VIEW_DIALOG_KEY "view-dialog"

static const char *shared_group[] = {
	"view_shared",
	"view_unshared",
	0
};


typedef struct {
	WorkbookControlGUI *wbcg;
	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)
{
	WorkbookControl *wbc = WORKBOOK_CONTROL (state->wbcg);
	gboolean shared;
	GdkScreen *screen = NULL;
	GSList *buttons = gtk_radio_button_get_group (state->location_elsewhere);

	shared = gnumeric_glade_group_value (state->gui, shared_group) == 0;

	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 */

80
		if (!g_getenv ("GNUMERIC_MULTIHEAD")) {
81
			WorkbookControlGUI *wbcg = state->wbcg;
82
			gtk_widget_destroy (state->dialog);
83
			gnumeric_notice (wbcg, GTK_MESSAGE_ERROR,
84 85 86 87 88 89 90
					 _("Multi-display support in Gnumeric has been turned "
					   "off due to multiple bugs in GTK+, libgnomeui, "
					   "libgnomeprintui, libbonoboui, and Gnumeric "
					   "at the time of writing, ultimo May 2003."));
			return;
		}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		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);
			gnumeric_notice (state->wbcg, GTK_MESSAGE_ERROR, error_str);
			g_free (error_str);
			return;
		}

		screen = gdk_display_get_default_screen (display);
	} else {
		screen = g_object_get_data (buttons->data, "screen");
		/* screen will be NULL for current screen.  */
	}

	gtk_widget_destroy (state->dialog);

110
	(void) wb_control_wrapper_new
111 112
		(wbc,
		 shared ? wb_control_view (wbc) : NULL,
113 114
		 wb_control_workbook (wbc),
		 screen);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

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
dialog_new_view (WorkbookControlGUI *wbcg)
{
	ViewState *state;
	GladeXML *gui;

	if (gnumeric_dialog_raise_if_exists (wbcg, VIEW_DIALOG_KEY))
		return;
	gui = gnm_glade_xml_new (COMMAND_CONTEXT (wbcg),
				 "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);
			else
				g_object_set_data (G_OBJECT (button),
						   "screen",
						   screen);

			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
190 191 192
	gnm_link_button_and_entry (GTK_WIDGET (state->location_elsewhere),
				   GTK_WIDGET (state->location_display_name));

193 194 195 196 197
	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_WIDGET (state->location_display_name));
	
	gnumeric_init_help_button (
		glade_xml_get_widget (gui, "help_button"),
198
		GNUMERIC_HELP_LINK_VIEW);
199 200 201 202 203 204 205
	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);
}