gnm-notebook.c 4.77 KB
Newer Older
1
/**
2
 * gnm-notebook.c: Implements a button-only notebook.
3 4
 *
 * Copyright (c) 2008 Morten Welinder <terra@gnome.org>
5
 * Copyright notices for included gtknotebook.c, see below.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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) any later version.
 *
 * 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 "gnm-notebook.h"
#include <gsf/gsf-impl-utils.h>

struct _GnmNotebook {
	GtkNotebook parent;

	/*
	 * This is the number of pixels from a regular notebook that
	 * we are not drawing.  It is caused by the empty widgets
	 * that we have to use.
Morten Welinder's avatar
Morten Welinder committed
33
	 */
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	int dummy_height;
};

typedef struct {
	GtkNotebookClass parent_class;
} GnmNotebookClass;

static GtkNotebookClass *gnm_notebook_parent_class;

#define DUMMY_KEY "GNM-NOTEBOOK-DUMMY-WIDGET"

static void
gnm_notebook_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
	int i, h = 0;
	GnmNotebook *gnb = (GnmNotebook *)widget;
	GtkAllocation alc = *allocation;

	for (i = 0; TRUE; i++) {
		GtkWidget *page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (widget), i);
55
		GtkAllocation a;
56 57
		if (!page)
			break;
58
		if (!gtk_widget_get_visible (page))
59
			continue;
60 61
		gtk_widget_get_allocation (page, &a);
		h = MAX (h, a.height);
62 63 64 65 66 67 68 69 70 71
	}

	gnb->dummy_height = h;

	alc.y -= h;
	((GtkWidgetClass *)gnm_notebook_parent_class)->size_allocate
		(widget, &alc);
}

static void
72
gnm_notebook_class_init (GtkWidgetClass *klass)
73 74
{
	gnm_notebook_parent_class = g_type_class_peek (GTK_TYPE_NOTEBOOK);
75
	klass->size_allocate = gnm_notebook_size_allocate;
76 77 78
}

static void
79
gnm_notebook_init (GnmNotebook *notebook)
80
{
81 82 83
	GtkCssProvider *css;
	GtkStyleContext *context;

84
	gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

	context = gtk_widget_get_style_context (GTK_WIDGET (notebook));
	css = gtk_css_provider_new ();
	gtk_css_provider_load_from_data (css,
					 "GnmNotebook {\n"
					 "  padding: 0;\n"
					 "/*  background-color: #dd0000; */\n"
					 "}\n"
					 "GnmNotebook tab {\n"
					 "}",
					 -1, NULL);
	gtk_style_context_add_provider (context,
					GTK_STYLE_PROVIDER (css),
					GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
	g_object_unref (css);
100 101 102 103 104 105 106 107 108 109 110 111 112
}

GSF_CLASS (GnmNotebook, gnm_notebook,
	   gnm_notebook_class_init, gnm_notebook_init, GTK_TYPE_NOTEBOOK)

int
gnm_notebook_get_n_visible (GnmNotebook *nb)
{
	int count = 0;
	GList *l, *children = gtk_container_get_children (GTK_CONTAINER (nb));

	for (l = children; l; l = l->next) {
		GtkWidget *child = l->data;
113
		if (gtk_widget_get_visible (child))
114 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
			count++;
	}

	g_list_free (children);

	return count;
}

GtkWidget *
gnm_notebook_get_nth_label (GnmNotebook *nb, int n)
{
	GtkWidget *page;

	g_return_val_if_fail (IS_GNM_NOTEBOOK (nb), NULL);

	page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (nb), n);
	if (!page)
		return NULL;

	return gtk_notebook_get_tab_label (GTK_NOTEBOOK (nb), page);
}

static void
cb_label_destroyed (G_GNUC_UNUSED GtkWidget *label, GtkWidget *dummy)
{
	gtk_widget_destroy (dummy);
}

static void
cb_label_visibility (GtkWidget *label,
		     G_GNUC_UNUSED GParamSpec *pspec,
		     GtkWidget *dummy)
{
147
	gtk_widget_set_visible (dummy, gtk_widget_get_visible (label));
148 149 150 151 152
}

void
gnm_notebook_insert_tab (GnmNotebook *nb, GtkWidget *label, int pos)
{
153
	GtkWidget *dummy_page = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
154
	gtk_widget_set_size_request (dummy_page, 1, 1);
155

156 157 158 159 160
	g_object_set_data (G_OBJECT (label), DUMMY_KEY, dummy_page);

	g_signal_connect_object (G_OBJECT (label), "destroy",
				 G_CALLBACK (cb_label_destroyed), dummy_page,
				 0);
161 162

	cb_label_visibility (label, NULL, dummy_page);
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 190 191 192 193
	g_signal_connect_object (G_OBJECT (label), "notify::visible",
				 G_CALLBACK (cb_label_visibility), dummy_page,
				 0);

	gtk_notebook_insert_page (GTK_NOTEBOOK (nb), dummy_page, label, pos);
}

void
gnm_notebook_move_tab (GnmNotebook *nb, GtkWidget *label, int newpos)
{
	GtkWidget *child = g_object_get_data (G_OBJECT (label), DUMMY_KEY);
	gtk_notebook_reorder_child (GTK_NOTEBOOK (nb), child, newpos);
}

void
gnm_notebook_set_current_page (GnmNotebook *nb, int page)
{
	gtk_notebook_set_current_page (GTK_NOTEBOOK (nb), page);
}

void
gnm_notebook_prev_page (GnmNotebook *nb)
{
	gtk_notebook_prev_page (GTK_NOTEBOOK (nb));
}

void
gnm_notebook_next_page (GnmNotebook *nb)
{
	gtk_notebook_next_page (GTK_NOTEBOOK (nb));
}