2 #include "theme_view.h"
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
);
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
);
20 static gpointer parent_class
;
22 enum { PROP_TITLE
= 1, };
27 GtkWidget
*create_view_and_model(void)
29 GtkTreeViewColumn
*col
;
30 GtkCellRenderer
*renderer
;
32 GtkTreeSelection
*sel
;
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
);
39 g_object_unref(g_store
); // destroy store automatically with view
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);
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
);
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
);
59 void on_changed(GtkWidget
*widget
, gpointer label
)
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);
74 void add_to_list(GtkWidget
*list
, const gchar
*str
)
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);
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 ***************************************************************************/
91 GType
custom_cell_renderer_theme_get_type (void)
93 static GType cell_type
= 0;
96 static const GTypeInfo cell_info
=
98 sizeof (CustomCellRendererThemeClass
),
100 NULL
, // base_finalize
101 (GClassInitFunc
) custom_cell_renderer_theme_class_init
,
102 NULL
, // class_finalize
104 sizeof (CustomCellRendererTheme
),
106 (GInstanceInitFunc
) custom_cell_renderer_theme_init
,
109 // Derive from GtkCellRenderer
110 cell_type
= g_type_register_static(GTK_TYPE_CELL_RENDERER
, "CustomCellRendererTheme", &cell_info
, 0);
117 static void custom_cell_renderer_theme_init(CustomCellRendererTheme
*celltheme
)
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");
129 /***************************************************************************
130 * custom_cell_renderer_theme_class_init:
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 ***************************************************************************/
140 static void custom_cell_renderer_theme_class_init(CustomCellRendererThemeClass
*klass
)
142 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS(klass
);
143 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
145 parent_class
= g_type_class_peek_parent (klass
);
146 object_class
->finalize
= custom_cell_renderer_theme_finalize
;
148 object_class
->get_property
= custom_cell_renderer_theme_get_property
;
149 object_class
->set_property
= custom_cell_renderer_theme_set_property
;
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
;
155 printf("custom_class_init\n\n");
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));
163 static void custom_cell_renderer_theme_finalize(GObject
*object
)
165 CustomCellRendererTheme
*celltheme
= CUSTOM_CELL_RENDERER_THEME(object
);
167 // free any resources here
168 //if (celltheme->title)
169 //g_free(celltheme->title);
171 // Free any dynamically allocated resources here
172 (* G_OBJECT_CLASS (parent_class
)->finalize
) (object
);
176 static void custom_cell_renderer_theme_get_property(GObject
*object
, guint param_id
, GValue
*value
, GParamSpec
*psec
)
178 CustomCellRendererTheme
*celltheme
= CUSTOM_CELL_RENDERER_THEME(object
);
182 g_value_set_string(value
, celltheme
->title
);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, psec
);
192 static void custom_cell_renderer_theme_set_property(GObject
*object
, guint param_id
, const GValue
*value
, GParamSpec
*pspec
)
194 CustomCellRendererTheme
*cellprogress
= CUSTOM_CELL_RENDERER_THEME(object
);
198 //printf("set_property**************************************\n");
199 cellprogress
->title
= g_value_get_string(value
);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, param_id
, pspec
);
209 #define FIXED_WIDTH 100
210 #define FIXED_HEIGHT 20
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
)
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
;
226 *height
= calc_height
;
230 *x_offset
= cell
->xalign
* (cell_area
->width
- calc_width
);
231 *x_offset
= MAX (*x_offset
, 0);
235 *y_offset
= cell
->yalign
* (cell_area
->height
- calc_height
);
236 *y_offset
= MAX (*y_offset
, 0);
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
)
244 CustomCellRendererTheme
*celltheme
= CUSTOM_CELL_RENDERER_THEME(cell
);
246 gint width
, height
, x_offset
, y_offset
;
250 custom_cell_renderer_theme_get_size(cell
, widget
, cell_area
, &x_offset
, &y_offset
, &width
, &height
);
252 if (GTK_WIDGET_HAS_FOCUS (widget
))
253 state
= GTK_STATE_ACTIVE
;
255 state
= GTK_STATE_NORMAL
;
257 width
-= cell
->xpad
*2;
258 height
-= cell
->ypad
*2;
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
);
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);
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);