slab-section.c 5.23 KB
Newer Older
Jim Krehl's avatar
Jim Krehl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of libslab.
 *
 * Copyright (c) 2006 Novell, Inc.
 *
 * Libslab is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Libslab 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 Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libslab; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "slab-section.h"

#include <gtk/gtklabel.h>
#include <gtk/gtkalignment.h>

26
G_DEFINE_TYPE (SlabSection, slab_section, GTK_TYPE_VBOX)
Jim Krehl's avatar
Jim Krehl committed
27

28 29
static void slab_section_finalize (GObject *);
gboolean slab_section_expose_event (GtkWidget * widget, GdkEventExpose * event, gpointer data);
Jim Krehl's avatar
Jim Krehl committed
30

31
static void slab_section_class_init (SlabSectionClass * slab_section_class)
Jim Krehl's avatar
Jim Krehl committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
	GObjectClass *g_obj_class = G_OBJECT_CLASS (slab_section_class);

	g_obj_class->finalize = slab_section_finalize;
}

static void
slab_section_init (SlabSection * section)
{
	section->title = NULL;
	section->contents = NULL;
}

static void
slab_section_finalize (GObject * obj)
{
	g_assert (IS_SLAB_SECTION (obj));
49
	(*G_OBJECT_CLASS (slab_section_parent_class)->finalize) (obj);
Jim Krehl's avatar
Jim Krehl committed
50 51 52 53 54
}

static void
slab_section_set_title_color (GtkWidget * widget)
{
55 56
	switch (SLAB_SECTION (widget)->style)
	{
Jim Krehl's avatar
Jim Krehl committed
57
	case Style1:
58 59
		gtk_widget_modify_fg (SLAB_SECTION (widget)->title, GTK_STATE_NORMAL,
			&widget->style->bg[GTK_STATE_SELECTED]);
Jim Krehl's avatar
Jim Krehl committed
60 61 62
		break;
	case Style2:
		if (SLAB_SECTION (widget)->selected)
63 64
			gtk_widget_modify_fg (SLAB_SECTION (widget)->title, GTK_STATE_NORMAL,
				&widget->style->dark[GTK_STATE_SELECTED]);
Jim Krehl's avatar
Jim Krehl committed
65
		else
66 67
			gtk_widget_modify_fg (SLAB_SECTION (widget)->title, GTK_STATE_NORMAL,
				&widget->style->text[GTK_STATE_INSENSITIVE]);
Jim Krehl's avatar
Jim Krehl committed
68 69 70 71 72 73 74
		break;
	default:
		g_assert_not_reached ();
	}
}

static void
75
slab_section_style_set (GtkWidget * widget, GtkStyle * prev_style, gpointer user_data)
Jim Krehl's avatar
Jim Krehl committed
76 77
{
	static gboolean recursively_entered = FALSE;
78 79
	if (!recursively_entered)
	{
Jim Krehl's avatar
Jim Krehl committed
80 81 82 83 84 85 86 87 88
		recursively_entered = TRUE;

		slab_section_set_title_color (widget);

		recursively_entered = FALSE;
	}
}

gboolean
89
slab_section_expose_event (GtkWidget * widget, GdkEventExpose * event, gpointer data)
Jim Krehl's avatar
Jim Krehl committed
90
{
91 92 93
	gdk_draw_rectangle (widget->window, widget->style->light_gc[GTK_STATE_SELECTED], TRUE,
		widget->allocation.x, widget->allocation.y,
		widget->allocation.width + 40, widget->allocation.height);
Jim Krehl's avatar
Jim Krehl committed
94 95 96

	GList *child = gtk_container_get_children (GTK_CONTAINER (widget));
	for (; child; child = child->next)
97 98
		gtk_container_propagate_expose (GTK_CONTAINER (widget), GTK_WIDGET (child->data),
			event);
Jim Krehl's avatar
Jim Krehl committed
99 100 101 102 103 104 105 106 107 108 109

	return FALSE;
}

void
slab_section_set_selected (SlabSection * section, gboolean selected)
{
	if (selected == section->selected)
		return;
	section->selected = selected;

110 111 112 113 114 115 116 117
	/*
	   if(selected)
	   {
	   section->expose_handler_id = g_signal_connect(G_OBJECT(section),
	   "expose-event", G_CALLBACK(slab_section_expose_event), NULL);
	   }
	   else
	   {
Jim Krehl's avatar
Jim Krehl committed
118
	   g_signal_handler_disconnect(section, section->expose_handler_id);
119 120
	   }
	 */
Jim Krehl's avatar
Jim Krehl committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	slab_section_set_title_color (GTK_WIDGET (section));
}

GtkWidget *
slab_section_new_with_markup (const gchar * title_markup, SlabStyle style)
{
	SlabSection *section;

	section = g_object_new (SLAB_SECTION_TYPE, NULL);
	gtk_box_set_homogeneous (GTK_BOX (section), FALSE);
	gtk_box_set_spacing (GTK_BOX (section), 0);
	section->style = style;
	section->selected = FALSE;

	GtkWidget *align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
137 138
	switch (style)
	{
Jim Krehl's avatar
Jim Krehl committed
139 140 141 142
	case Style1:
		gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, 0, 0);
		break;
	case Style2:
143 144
		gtk_alignment_set_padding (GTK_ALIGNMENT (align), SLAB_TOP_PADDING,
			SLAB_BOTTOM_PADDING, SLAB_LEFT_PADDING, 0);
Jim Krehl's avatar
Jim Krehl committed
145 146 147 148 149 150 151
		break;
	default:
		g_assert_not_reached ();
	}
	gtk_box_pack_start (GTK_BOX (section), align, TRUE, TRUE, 0);

	section->childbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
152
	gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (section->childbox));
Jim Krehl's avatar
Jim Krehl committed
153 154 155 156 157

	section->title = gtk_label_new (title_markup);
	gtk_label_set_use_markup (GTK_LABEL (section->title), TRUE);
	gtk_misc_set_alignment (GTK_MISC (section->title), 0.0, 0.5);

158 159
	g_signal_connect (G_OBJECT (section), "style-set", G_CALLBACK (slab_section_style_set),
		NULL);
Jim Krehl's avatar
Jim Krehl committed
160

161
	gtk_box_pack_start (section->childbox, section->title, FALSE, FALSE, 0);
Jim Krehl's avatar
Jim Krehl committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	return GTK_WIDGET (section);
}

GtkWidget *
slab_section_new (const gchar * title, SlabStyle style)
{
	GtkWidget *section;
	gchar *markup;

	markup = g_strdup_printf ("<span size=\"large\">%s</span>", title);
	section = slab_section_new_with_markup (markup, style);

	g_free (markup);

	return section;
}

void
slab_section_set_title (SlabSection * section, const gchar * title)
{
183
	gchar *markup = g_strdup_printf ("<span size=\"large\">%s</span>", title);
Jim Krehl's avatar
Jim Krehl committed
184 185 186 187 188 189 190 191 192 193 194 195 196

	gtk_label_set_markup (GTK_LABEL (section->title), markup);

	g_free (markup);
}

void
slab_section_set_contents (SlabSection * section, GtkWidget * contents)
{
	section->contents = contents;

	gtk_box_pack_start (section->childbox, contents, FALSE, FALSE, 0);
}