aboutsummaryrefslogtreecommitdiff
path: root/patches/treeview-zebra-stripes.c
blob: 8dafa1625ca1ae0d82f85dcd59d9685283ab8350 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Treeview Zebra Stripes:
 * (based on gtk3-classic patch 'treeview__alternating_row_colours.patch'):
 *
 * Adds appropriate style classes to each cell in a GtkTreeView, so that themes
 * can draw the rows of a treeview in alternating colours, also known as
 * 'zebra stripes'.
 */


#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <cairo/cairo.h>

#include <gtk3-classic.h>


#undef  G_LOG_DOMAIN
#define G_LOG_DOMAIN	"Gtk3-Classic::Treeview-Zebra-Stripes"


static gboolean	find_row_index	(GtkTreeView *	treeview,
				 GtkTreeModel *	model,
				 GtkTreeIter *	start_iter,
				 GtkTreePath *	target_path,
				 int *		index);


INTERCEPTED_CLASS_METHOD (gtk_cell_area, render,
			  (GtkCellArea *	area,
			   GtkCellAreaContext *	context,
			   GtkWidget *		widget,
			   cairo_t *		cr,
			   const GdkRectangle *	background_area,
			   const GdkRectangle *	cell_area,
			   GtkCellRendererState	flags,
			   gboolean		paint_focus),
			  void)


void treeview_zebra_stripes_init ()
{
	GtkCellAreaClass * gtk_cell_area_class =
	 g_type_class_ref (GTK_TYPE_CELL_AREA);

	INTERCEPT_CLASS_METHOD (gtk_cell_area, GTK_CELL_AREA_CLASS,
				render)

	g_type_class_unref (gtk_cell_area_class);
}


static void
new_gtk_cell_area_render
(GtkCellArea *		area,
 GtkCellAreaContext *	context,
 GtkWidget *		widget,
 cairo_t *		cr,
 const GdkRectangle *	background_area,
 const GdkRectangle *	cell_area,
 GtkCellRendererState	flags,
 gboolean		paint_focus)
{
	GtkTreeView *		treeview;
	GtkTreeModel *		model;
	GtkStyleContext *	style;
	gint			bin_x, bin_y;
	GtkTreePath *		path;
	int			index;
	GtkTreeViewColumn *	column;
	GList *			columns;


	if (GTK_IS_TREE_VIEW (widget))
	{
		treeview = GTK_TREE_VIEW (widget);
		model = gtk_tree_view_get_model (treeview);
		style = gtk_widget_get_style_context (widget);
		columns = gtk_tree_view_get_columns (treeview);

		bin_x = cell_area->x + cell_area->width - 1;
		bin_y = cell_area->y + cell_area->height - 1;

		gtk_tree_view_get_path_at_pos (treeview, bin_x, bin_y,
					       &path, &column, NULL, NULL);
		if (path != NULL)
		{
			index = 0;
			find_row_index (treeview, model, NULL, path, &index);
			gtk_tree_path_free (path);
			gtk_style_context_add_class (style,
			  ((index % 2) ? "even" : "odd"));
		}

		if (column == columns->data)
			gtk_style_context_add_class (style, "first");
		if (column == g_list_last (columns)->data)
			gtk_style_context_add_class (style, "last");
		if ((flags & GTK_CELL_RENDERER_SORTED))
			gtk_style_context_add_class (style, "sorted");

		gtk_render_background (style, cr,
				       background_area->x, background_area->y,
				       background_area->width,
				       background_area->height);
	}


	CALL_ORIGINAL_CLASS_METHOD (gtk_cell_area, render,
				    (area, context, widget, cr,
				     background_area, cell_area,
				     flags, paint_focus));
}


/* Helper functions */

static gboolean
find_row_index
(GtkTreeView *	treeview,
 GtkTreeModel *	model,
 GtkTreeIter *	start_iter,
 GtkTreePath *	target_path,
 int *		index)
{
	GtkTreeIter	iter;
	GtkTreeIter	child_iter;
	GtkTreePath *	path;


	if (start_iter == NULL)
	{
		if (! gtk_tree_model_get_iter_first (model, &iter))
			return TRUE;
	}
	else
		iter = *start_iter;

	while (1)
	{
		path = gtk_tree_model_get_path (model, &iter);

		if (gtk_tree_path_compare (path, target_path) == 0)
		{
			gtk_tree_path_free (path);
			return TRUE;
		}

		if (gtk_tree_view_row_expanded (treeview, path))
		{
			(*index)++;
			gtk_tree_model_iter_children (model, &child_iter, &iter);
			if (find_row_index (treeview, model, &child_iter,
					    target_path, index))
			{
				gtk_tree_path_free (path);
				return TRUE;
			}
		}

		gtk_tree_path_free (path);
		if (! gtk_tree_model_iter_next (model, &iter))
			break;
		else
			(*index)++;
	}


	return FALSE;
}
bgstack15