gedit-status-menu-button.c 4.76 KB
Newer Older
1
/*
2
 * gedit-status-menu-button.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * This file is part of gedit
 *
 * Copyright (C) 2008 - Jesse van den Kieboom
 *
 * 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
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 20
 */

21
#include "gedit-status-menu-button.h"
22

23
struct _GeditStatusMenuButton
24
{
25 26
	GtkMenuButton parent_instance;

27 28 29
	GtkWidget *label;
};

30
typedef struct
31 32
{
	GtkCssProvider *css;
33
} GeditStatusMenuButtonClassPrivate;
34

35
enum
36 37
{
	PROP_0,
38 39
	PROP_LABEL,
	LAST_PROP
40 41
};

42 43 44 45
G_DEFINE_TYPE_WITH_CODE (GeditStatusMenuButton,
			 gedit_status_menu_button,
			 GTK_TYPE_MENU_BUTTON,
			 g_type_add_class_private (g_define_type_id, sizeof (GeditStatusMenuButtonClassPrivate)))
46 47

static void
48 49 50 51
gedit_status_menu_button_get_property (GObject    *object,
				       guint       prop_id,
				       GValue     *value,
				       GParamSpec *pspec)
52
{
53
	GeditStatusMenuButton *obj = GEDIT_STATUS_MENU_BUTTON (object);
54 55 56 57

	switch (prop_id)
	{
		case PROP_LABEL:
58
			g_value_set_string (value, gedit_status_menu_button_get_label (obj));
59 60 61 62 63 64 65 66
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
67 68 69 70
gedit_status_menu_button_set_property (GObject      *object,
				       guint         prop_id,
				       const GValue *value,
				       GParamSpec   *pspec)
71
{
72
	GeditStatusMenuButton *obj = GEDIT_STATUS_MENU_BUTTON (object);
73 74 75 76

	switch (prop_id)
	{
		case PROP_LABEL:
77
			gedit_status_menu_button_set_label (obj, g_value_get_string (value));
78 79 80 81 82 83 84 85
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
86
gedit_status_menu_button_class_init (GeditStatusMenuButtonClass *klass)
87
{
88 89
	static const gchar style[] =
		"* {\n"
90
		  "padding: 1px 8px 2px 4px;\n"
91 92
		  "border: 0;\n"
		  "outline-width: 0;\n"
93
		"}";
94

95 96 97 98
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
	GeditStatusMenuButtonClassPrivate *class_priv;

99 100
	object_class->get_property = gedit_status_menu_button_get_property;
	object_class->set_property = gedit_status_menu_button_set_property;
101

102
	g_object_class_override_property (object_class, PROP_LABEL, "label");
103

104 105 106
	/* Bind class to template */
	gtk_widget_class_set_template_from_resource (widget_class,
	                                             "/org/gnome/gedit/ui/gedit-status-menu-button.ui");
107
	gtk_widget_class_bind_template_child_internal (widget_class, GeditStatusMenuButton, label);
108

109 110 111 112
	/* Store the CSS provider in the class private data so it is shared among all instances */
	class_priv = G_TYPE_CLASS_GET_PRIVATE (klass, GEDIT_TYPE_STATUS_MENU_BUTTON, GeditStatusMenuButtonClassPrivate);
	class_priv->css = gtk_css_provider_new ();
	gtk_css_provider_load_from_data (class_priv->css, style, -1, NULL);
113 114
}

115
static void
116
gedit_status_menu_button_init (GeditStatusMenuButton *self)
117
{
118
	GtkStyleContext *context;
119
	GeditStatusMenuButtonClassPrivate *class_priv;
120

121
	gtk_widget_init_template (GTK_WIDGET (self));
122

123
	/* make it as small as possible */
124
	context = gtk_widget_get_style_context (GTK_WIDGET (self));
125 126 127
	class_priv = G_TYPE_CLASS_GET_PRIVATE (G_TYPE_INSTANCE_GET_CLASS (self, GEDIT_TYPE_STATUS_MENU_BUTTON, GeditStatusMenuButtonClass),
	                                       GEDIT_TYPE_STATUS_MENU_BUTTON,
	                                       GeditStatusMenuButtonClassPrivate);
128
	gtk_style_context_add_provider (context,
129
	                                GTK_STYLE_PROVIDER (class_priv->css),
130
	                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
131 132
}

133
/**
134
 * gedit_status_menu_button_new:
135 136
 * @label: (allow-none):
 */
137
GtkWidget *
138
gedit_status_menu_button_new (void)
139
{
140
	return g_object_new (GEDIT_TYPE_STATUS_MENU_BUTTON, NULL);
141 142
}

143 144 145 146 147
/* we cannot rely on gtk_button_set_label since it manually replaces
 * the internal child instead of just setting the property :( */

void
gedit_status_menu_button_set_label (GeditStatusMenuButton *button,
148
                                    const gchar           *label)
149
{
150 151
	g_return_if_fail (GEDIT_IS_STATUS_MENU_BUTTON (button));

152
	gtk_label_set_markup (GTK_LABEL (button->label), label);
153 154 155 156 157
}

const gchar *
gedit_status_menu_button_get_label (GeditStatusMenuButton *button)
{
158 159
	g_return_val_if_fail (GEDIT_IS_STATUS_MENU_BUTTON (button), NULL);

160
	return gtk_label_get_label (GTK_LABEL (button->label));
161 162
}

163
/* ex:set ts=8 noet: */