]> Dogcows Code - chaz/tint2/blob - src/tint2conf/theme_view.c
tint2conf : custom tree view
[chaz/tint2] / src / tint2conf / theme_view.c
1
2 #include "theme_view.h"
3
4
5 GtkListStore *g_store;
6
7 // Some boring function declarations: GObject type system stuff
8 static void custom_cell_renderer_theme_init(CustomCellRendererTheme *cellprogress);
9 static void custom_cell_renderer_theme_class_init(CustomCellRendererThemeClass *klass);
10 static void custom_cell_renderer_theme_finalize(GObject *gobject);
11 static void custom_cell_renderer_theme_get_property(GObject *object, guint param_id, GValue *value, GParamSpec *pspec);
12 static void custom_cell_renderer_theme_set_property(GObject *object, guint param_id, const GValue *value, GParamSpec *pspec);
13
14
15 // These functions are the heart of our custom cell renderer:
16 static void custom_cell_renderer_theme_get_size(GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *cell_area, gint *x_offset, gint *y_offset, gint *width, gint *height);
17 static void custom_cell_renderer_theme_render(GtkCellRenderer *cell, GdkWindow *window, GtkWidget *widget, GdkRectangle *background_area, GdkRectangle *cell_area, GdkRectangle *expose_area, guint flags);
18
19
20 static gpointer parent_class;
21
22 enum { PROP_TITLE = 1, };
23
24
25
26
27 GtkWidget *create_view_and_model(void)
28 {
29 GtkTreeViewColumn *col;
30 GtkCellRenderer *renderer;
31 GtkWidget *view;
32 GtkTreeSelection *sel;
33 GtkTreeIter iter;
34
35 g_store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);
36 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(g_store));
37 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
38
39 g_object_unref(g_store); // destroy store automatically with view
40 /*
41 renderer = gtk_cell_renderer_text_new();
42 col = gtk_tree_view_column_new_with_attributes("Deb", renderer, "title", COL_TEXT, NULL);
43 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
44 */
45 renderer = g_object_new(CUSTOM_TYPE_CELL_RENDERER_THEME, NULL);
46 col = gtk_tree_view_column_new();
47 gtk_tree_view_column_pack_start (col, renderer, TRUE);
48 gtk_tree_view_column_add_attribute (col, renderer, "title", COL_TEXT);
49 gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
50
51 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
52 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(sel), GTK_SELECTION_SINGLE);
53 g_signal_connect(sel, "changed", G_CALLBACK(on_changed), NULL);
54
55 return view;
56 }
57
58
59 void on_changed(GtkWidget *widget, gpointer label)
60 {
61 GtkTreeIter iter;
62 GtkTreeModel *model;
63 char *value;
64
65 if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
66 gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1);
67 //gtk_label_set_text(GTK_LABEL(label), value);
68 g_free(value);
69 }
70
71 }
72
73
74 void add_to_list(GtkWidget *list, const gchar *str)
75 {
76 GtkTreeIter iter;
77
78 gtk_list_store_append(g_store, &iter);
79 gtk_list_store_set(g_store, &iter, COL_TEXT, str, -1);
80 //gtk_list_store_set (g_store, &iter, COL_TEXT, buf, -1);
81 }
82
83
84 /***************************************************************************
85 * custom_cell_renderer_theme_get_type: here we register our type with
86 * the GObject type system if we
87 * haven't done so yet. Everything
88 * else is done in the callbacks.
89 ***************************************************************************/
90
91 GType custom_cell_renderer_theme_get_type (void)
92 {
93 static GType cell_type = 0;
94
95 if (cell_type == 0) {
96 static const GTypeInfo cell_info =
97 {
98 sizeof (CustomCellRendererThemeClass),
99 NULL, // base_init
100 NULL, // base_finalize
101 (GClassInitFunc) custom_cell_renderer_theme_class_init,
102 NULL, // class_finalize
103 NULL, // class_data
104 sizeof (CustomCellRendererTheme),
105 0, // n_preallocs
106 (GInstanceInitFunc) custom_cell_renderer_theme_init,
107 };
108
109 // Derive from GtkCellRenderer
110 cell_type = g_type_register_static(GTK_TYPE_CELL_RENDERER, "CustomCellRendererTheme", &cell_info, 0);
111 }
112
113 return cell_type;
114 }
115
116
117 static void custom_cell_renderer_theme_init(CustomCellRendererTheme *celltheme)
118 {
119 // set some default properties
120 GTK_CELL_RENDERER(celltheme)->mode = GTK_CELL_RENDERER_MODE_INERT;
121 GTK_CELL_RENDERER(celltheme)->xpad = 2;
122 GTK_CELL_RENDERER(celltheme)->ypad = 2;
123 celltheme->title = 0;
124 printf("custom_cell_renderer_theme_init\n\n");
125
126 }
127
128
129 /***************************************************************************
130 * custom_cell_renderer_theme_class_init:
131 *
132 * set up our own get_property and set_property functions, and
133 * override the parent's functions that we need to implement.
134 * And make our new "percentage" property known to the type system.
135 * If you want cells that can be activated on their own (ie. not
136 * just the whole row selected) or cells that are editable, you
137 * will need to override 'activate' and 'start_editing' as well.
138 ***************************************************************************/
139
140 static void custom_cell_renderer_theme_class_init(CustomCellRendererThemeClass *klass)
141 {
142 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(klass);
143 GObjectClass *object_class = G_OBJECT_CLASS(klass);
144
145 parent_class = g_type_class_peek_parent (klass);
146 object_class->finalize = custom_cell_renderer_theme_finalize;
147
148 object_class->get_property = custom_cell_renderer_theme_get_property;
149 object_class->set_property = custom_cell_renderer_theme_set_property;
150
151 // Override the two crucial functions that are the heart of a cell renderer in the parent class
152 cell_class->get_size = custom_cell_renderer_theme_get_size;
153 cell_class->render = custom_cell_renderer_theme_render;
154
155 printf("custom_class_init\n\n");
156
157 // Install our very own properties
158 g_object_class_install_property (object_class, PROP_TITLE, g_param_spec_string("title", "Title", "Theme's title", 0, G_PARAM_READWRITE));
159 // g_object_class_install_property (object_class, PROP_PERCENTAGE, g_param_spec_double ("percentage", "Percentage", "The fractional progress to display", 0, 1, 0, G_PARAM_READWRITE));
160 }
161
162
163 static void custom_cell_renderer_theme_finalize(GObject *object)
164 {
165 CustomCellRendererTheme *celltheme = CUSTOM_CELL_RENDERER_THEME(object);
166
167 // free any resources here
168 //if (celltheme->title)
169 //g_free(celltheme->title);
170
171 // Free any dynamically allocated resources here
172 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
173 }
174
175
176 static void custom_cell_renderer_theme_get_property(GObject *object, guint param_id, GValue *value, GParamSpec *psec)
177 {
178 CustomCellRendererTheme *celltheme = CUSTOM_CELL_RENDERER_THEME(object);
179
180 switch (param_id) {
181 case PROP_TITLE:
182 g_value_set_string(value, celltheme->title);
183 break;
184
185 default:
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec);
187 break;
188 }
189 }
190
191
192 static void custom_cell_renderer_theme_set_property(GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
193 {
194 CustomCellRendererTheme *cellprogress = CUSTOM_CELL_RENDERER_THEME(object);
195
196 switch (param_id) {
197 case PROP_TITLE:
198 //printf("set_property**************************************\n");
199 cellprogress->title = g_value_get_string(value);
200 break;
201
202 default:
203 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
204 break;
205 }
206 }
207
208
209 #define FIXED_WIDTH 100
210 #define FIXED_HEIGHT 20
211
212 static void custom_cell_renderer_theme_get_size(GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *cell_area, gint *x_offset, gint *y_offset, gint *width, gint *height)
213 {
214 gint calc_width;
215 gint calc_height;
216
217 // calculate the size of our cell, taking into account
218 // padding and alignment properties of parent.
219 calc_width = (gint) cell->xpad * 2 + FIXED_WIDTH;
220 calc_height = (gint) cell->ypad * 2 + FIXED_HEIGHT;
221
222 if (width)
223 *width = calc_width;
224
225 if (height)
226 *height = calc_height;
227
228 if (cell_area) {
229 if (x_offset) {
230 *x_offset = cell->xalign * (cell_area->width - calc_width);
231 *x_offset = MAX (*x_offset, 0);
232 }
233
234 if (y_offset) {
235 *y_offset = cell->yalign * (cell_area->height - calc_height);
236 *y_offset = MAX (*y_offset, 0);
237 }
238 }
239 }
240
241
242 static void custom_cell_renderer_theme_render(GtkCellRenderer *cell, GdkWindow *window, GtkWidget *widget, GdkRectangle *background_area, GdkRectangle *cell_area, GdkRectangle *expose_area, guint flags)
243 {
244 CustomCellRendererTheme *celltheme = CUSTOM_CELL_RENDERER_THEME(cell);
245 GtkStateType state;
246 gint width, height, x_offset, y_offset;
247 PangoLayout *layout;
248
249 // do the rendering.
250 custom_cell_renderer_theme_get_size(cell, widget, cell_area, &x_offset, &y_offset, &width, &height);
251
252 if (GTK_WIDGET_HAS_FOCUS (widget))
253 state = GTK_STATE_ACTIVE;
254 else
255 state = GTK_STATE_NORMAL;
256
257 width -= cell->xpad*2;
258 height -= cell->ypad*2;
259
260 layout = gtk_widget_create_pango_layout(widget, "");
261 pango_layout_set_text(layout, celltheme->title, strlen(celltheme->title));
262 gtk_paint_layout (widget->style, widget->window, state, FALSE, NULL, widget, NULL, x_offset, y_offset, layout);
263 //pango_layout_get_size (layout, &width, &height);
264 g_object_unref (layout);
265
266 //gtk_paint_layout
267 //gtk_paint_box (widget->style, window, GTK_STATE_NORMAL, GTK_SHADOW_IN, NULL, widget, "title", cell_area->x + x_offset + cell->xpad, cell_area->y + y_offset + cell->ypad, width - 1, height - 1);
268
269 printf("custom_cell_renderer_theme_render\n");
270 //gtk_paint_box (widget->style, window, state, GTK_SHADOW_OUT, NULL, widget, "bar", cell_area->x + x_offset + cell->xpad, cell_area->y + y_offset + cell->ypad, width * cellprogress->progress, height - 1);
271 }
272
This page took 0.046828 seconds and 5 git commands to generate.