0.3.6
by Jordi Mallach
Import upstream version 2.8.0 |
1 |
/* LIBGIMP - The GIMP Library
|
2 |
* Copyright (C) 1995-1999 Peter Mattis and Spencer Kimball
|
|
3 |
*
|
|
4 |
* gimpunitcombobox.c
|
|
5 |
* Copyright (C) 2004, 2008 Sven Neumann <sven@gimp.org>
|
|
6 |
*
|
|
7 |
* This library is free software: you can redistribute it and/or
|
|
8 |
* modify it under the terms of the GNU Lesser General Public
|
|
9 |
* License as published by the Free Software Foundation; either
|
|
10 |
* version 3 of the License, or (at your option) any later version.
|
|
11 |
*
|
|
12 |
* This library is distributed in the hope that it will be useful,
|
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 |
* Library General Public License for more details.
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU Lesser General Public
|
|
18 |
* License along with this library. If not, see
|
|
19 |
* <http://www.gnu.org/licenses/>.
|
|
20 |
*/
|
|
21 |
||
22 |
#include "config.h" |
|
23 |
||
24 |
#include <gtk/gtk.h> |
|
25 |
||
26 |
#include "gimpwidgetstypes.h" |
|
27 |
||
28 |
#include "gimpunitcombobox.h" |
|
29 |
#include "gimpunitstore.h" |
|
30 |
||
31 |
||
32 |
/**
|
|
33 |
* SECTION: gimpunitcombobox
|
|
34 |
* @title: GimpUnitComboBox
|
|
35 |
* @short_description: A #GtkComboBox to select a #GimpUnit.
|
|
36 |
* @see_also: #GimpUnit, #GimpUnitStore
|
|
37 |
*
|
|
38 |
* #GimpUnitComboBox allows to select units stored in a #GimpUnitStore.
|
|
39 |
* It replaces the deprecated #GimpUnitMenu.
|
|
40 |
**/
|
|
41 |
||
42 |
||
43 |
static void gimp_unit_combo_box_style_set (GtkWidget *widget, |
|
44 |
GtkStyle *prev_style); |
|
45 |
||
46 |
||
47 |
G_DEFINE_TYPE (GimpUnitComboBox, gimp_unit_combo_box, GTK_TYPE_COMBO_BOX) |
|
48 |
||
49 |
#define parent_class gimp_unit_combo_box_parent_class
|
|
50 |
||
51 |
||
52 |
static void |
|
53 |
gimp_unit_combo_box_class_init (GimpUnitComboBoxClass *klass) |
|
54 |
{
|
|
55 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); |
|
56 |
||
57 |
widget_class->style_set = gimp_unit_combo_box_style_set; |
|
58 |
||
59 |
gtk_widget_class_install_style_property (widget_class, |
|
60 |
g_param_spec_double ("label-scale", |
|
61 |
NULL, NULL, |
|
62 |
0.0, |
|
63 |
G_MAXDOUBLE, |
|
64 |
1.0, |
|
65 |
GIMP_PARAM_READABLE)); |
|
66 |
}
|
|
67 |
||
68 |
static void |
|
69 |
gimp_unit_combo_box_init (GimpUnitComboBox *combo) |
|
70 |
{
|
|
71 |
GtkCellLayout *layout = GTK_CELL_LAYOUT (combo); |
|
72 |
GtkCellRenderer *cell; |
|
73 |
||
74 |
cell = gtk_cell_renderer_text_new (); |
|
75 |
gtk_cell_layout_pack_start (layout, cell, TRUE); |
|
76 |
gtk_cell_layout_set_attributes (layout, cell, |
|
77 |
"text", GIMP_UNIT_STORE_UNIT_LONG_FORMAT, |
|
78 |
NULL); |
|
79 |
}
|
|
80 |
||
81 |
static void |
|
82 |
gimp_unit_combo_box_style_set (GtkWidget *widget, |
|
83 |
GtkStyle *prev_style) |
|
84 |
{
|
|
85 |
GtkCellLayout *layout; |
|
86 |
GtkCellRenderer *cell; |
|
87 |
gdouble scale; |
|
88 |
||
89 |
GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style); |
|
90 |
||
91 |
gtk_widget_style_get (widget, "label-scale", &scale, NULL); |
|
92 |
||
93 |
/* hackedehack ... */
|
|
94 |
layout = GTK_CELL_LAYOUT (gtk_bin_get_child (GTK_BIN (widget))); |
|
95 |
gtk_cell_layout_clear (layout); |
|
96 |
||
97 |
cell = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, |
|
98 |
"scale", scale, |
|
99 |
NULL); |
|
100 |
gtk_cell_layout_pack_start (layout, cell, TRUE); |
|
101 |
gtk_cell_layout_set_attributes (layout, cell, |
|
102 |
"text", GIMP_UNIT_STORE_UNIT_SHORT_FORMAT, |
|
103 |
NULL); |
|
104 |
}
|
|
105 |
||
106 |
/**
|
|
107 |
* gimp_unit_combo_box_new:
|
|
108 |
*
|
|
109 |
* Return value: a new #GimpUnitComboBox.
|
|
110 |
**/
|
|
111 |
GtkWidget * |
|
112 |
gimp_unit_combo_box_new (void) |
|
113 |
{
|
|
114 |
GtkWidget *combo_box; |
|
115 |
GimpUnitStore *store; |
|
116 |
||
117 |
store = gimp_unit_store_new (0); |
|
118 |
||
119 |
combo_box = g_object_new (GIMP_TYPE_UNIT_COMBO_BOX, |
|
120 |
"model", store, |
|
121 |
NULL); |
|
122 |
||
123 |
g_object_unref (store); |
|
124 |
||
125 |
return combo_box; |
|
126 |
}
|
|
127 |
||
128 |
/**
|
|
129 |
* gimp_unit_combo_box_new_with_model:
|
|
130 |
* @model: a GimpUnitStore
|
|
131 |
*
|
|
132 |
* Return value: a new #GimpUnitComboBox.
|
|
133 |
**/
|
|
134 |
GtkWidget * |
|
135 |
gimp_unit_combo_box_new_with_model (GimpUnitStore *model) |
|
136 |
{
|
|
137 |
return g_object_new (GIMP_TYPE_UNIT_COMBO_BOX, |
|
138 |
"model", model, |
|
139 |
NULL); |
|
140 |
}
|
|
141 |
||
142 |
GimpUnit
|
|
143 |
gimp_unit_combo_box_get_active (GimpUnitComboBox *combo) |
|
144 |
{
|
|
145 |
GtkTreeIter iter; |
|
146 |
gint unit; |
|
147 |
||
148 |
g_return_val_if_fail (GIMP_IS_UNIT_COMBO_BOX (combo), -1); |
|
149 |
||
150 |
gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter); |
|
151 |
||
152 |
gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo)), &iter, |
|
153 |
GIMP_UNIT_STORE_UNIT, &unit, |
|
154 |
-1); |
|
155 |
||
156 |
return (GimpUnit) unit; |
|
157 |
}
|
|
158 |
||
159 |
void
|
|
160 |
gimp_unit_combo_box_set_active (GimpUnitComboBox *combo, |
|
161 |
GimpUnit unit) |
|
162 |
{
|
|
163 |
GtkTreeModel *model; |
|
164 |
GtkTreeIter iter; |
|
165 |
gboolean iter_valid; |
|
166 |
||
167 |
g_return_if_fail (GIMP_IS_UNIT_COMBO_BOX (combo)); |
|
168 |
||
169 |
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo)); |
|
170 |
||
171 |
for (iter_valid = gtk_tree_model_get_iter_first (model, &iter); |
|
172 |
iter_valid; |
|
173 |
iter_valid = gtk_tree_model_iter_next (model, &iter)) |
|
174 |
{
|
|
175 |
gint iter_unit; |
|
176 |
||
177 |
gtk_tree_model_get (model, &iter, |
|
178 |
GIMP_UNIT_STORE_UNIT, &iter_unit, |
|
179 |
-1); |
|
180 |
||
181 |
if (unit == (GimpUnit) iter_unit) |
|
182 |
{
|
|
183 |
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter); |
|
184 |
break; |
|
185 |
}
|
|
186 |
}
|
|
187 |
||
188 |
}
|