1/* GTK - The GIMP Toolkit
2 * testcellrenderertext.c: Tests for the various properties of GtkCellRendererText
3 * Copyright (C) 2005, Novell, Inc.
4 *
5 * Authors:
6 * Federico Mena-Quintero <federico@novell.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <gtk/gtk.h>
23
24#define COL_BACKGROUND 15
25#define COL_LINE_NUM 16
26#define NUM_COLS 17 /* change this when adding columns */
27
28struct cell_params {
29 const char *description; /* 0 */
30 const char *test; /* 1 */
31 int xpad; /* 2 */
32 int ypad; /* 3 */
33 double xalign; /* 4 */
34 double yalign; /* 5 */
35 gboolean sensitive; /* 6 */
36 int width; /* 7 */
37 int height; /* 8 */
38 int width_chars; /* 9 */
39 int wrap_width; /* 10 */
40 PangoWrapMode wrap_mode; /* 11 */
41 gboolean single_paragraph_mode; /* 12 */
42 PangoEllipsizeMode ellipsize; /* 13 */
43 PangoAlignment alignment; /* 14 */
44 /* COL_BACKGROUND */ /* 15 */
45 /* COL_LINE_NUM */ /* 16 */
46};
47
48#define WO PANGO_WRAP_WORD
49#define CH PANGO_WRAP_CHAR
50#define WC PANGO_WRAP_WORD_CHAR
51
52#define NO PANGO_ELLIPSIZE_NONE
53#define ST PANGO_ELLIPSIZE_START
54#define MI PANGO_ELLIPSIZE_MIDDLE
55#define EN PANGO_ELLIPSIZE_END
56
57#define AL PANGO_ALIGN_LEFT
58#define AC PANGO_ALIGN_CENTER
59#define AR PANGO_ALIGN_RIGHT
60
61#define TESTL "LEFT JUSTIFIED This is really truly verily some very long text\n\330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205 \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205 \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205"
62
63#define TESTC "CENTERED This is really truly verily some very long text\n\330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205 \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205 \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205"
64
65#define TESTR "RIGHT JUSTIFIED This is really truly verily some very long text\n\330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205 \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205 \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205"
66
67
68/* DO NOT CHANGE THE ROWS! They are numbered so that we can refer to
69 * problematic rows in bug reports. If you need a different test, just add a
70 * new row at the bottom. Also, please add your new row numbers to this column -------------------------------+
71 * to keep things tidy. v
72 */
73static const struct cell_params cell_params[] = {
74 { "xp yp xa ya se wi he wc ww wm sp el", "", 0, 0, 0.0, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO }, /* 0 */
75
76 /* Test alignment */
77
78 { "0 0 0 0 T -1 -1 -1 -1 CH F NO", TESTL, 0, 0, 0.0, 0.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 1 */
79 { "0 0 .5 0 T -1 -1 -1 -1 CH F NO", TESTC, 0, 0, 0.5, 0.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 2 */
80 { "0 0 1 0 T -1 -1 -1 -1 CH F NO", TESTR, 0, 0, 1.0, 0.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 3 */
81 { "0 0 0 .5 T -1 -1 -1 -1 CH F NO", TESTL, 0, 0, 0.0, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 4 */
82 { "0 0 .5 .5 T -1 -1 -1 -1 CH F NO", TESTC, 0, 0, 0.5, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 5 */
83 { "0 0 1 .5 T -1 -1 -1 -1 CH F NO", TESTR, 0, 0, 1.0, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 6 */
84 { "0 0 0 1 T -1 -1 -1 -1 CH F NO", TESTL, 0, 0, 0.0, 1.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 7 */
85 { "0 0 .5 1 T -1 -1 -1 -1 CH F NO", TESTC, 0, 0, 0.5, 1.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 8 */
86 { "0 0 1 1 T -1 -1 -1 -1 CH F NO", TESTR, 0, 0, 1.0, 1.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 9 */
87
88 /* Test padding */
89
90 { "10 10 0 0 T -1 -1 -1 -1 CH F NO", TESTL, 10, 10, 0.0, 0.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 10 */
91 { "10 10 .5 0 T -1 -1 -1 -1 CH F NO", TESTC, 10, 10, 0.5, 0.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 11 */
92 { "10 10 1 0 T -1 -1 -1 -1 CH F NO", TESTR, 10, 10, 1.0, 0.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 12 */
93 { "10 10 0 .5 T -1 -1 -1 -1 CH F NO", TESTL, 10, 10, 0.0, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 13 */
94 { "10 10 .5 .5 T -1 -1 -1 -1 CH F NO", TESTC, 10, 10, 0.5, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 14 */
95 { "10 10 1 .5 T -1 -1 -1 -1 CH F NO", TESTR, 10, 10, 1.0, 0.5, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 15 */
96 { "10 10 0 1 T -1 -1 -1 -1 CH F NO", TESTL, 10, 10, 0.0, 1.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 16 */
97 { "10 10 .5 1 T -1 -1 -1 -1 CH F NO", TESTC, 10, 10, 0.5, 1.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 17 */
98 { "10 10 1 1 T -1 -1 -1 -1 CH F NO", TESTR, 10, 10, 1.0, 1.0, TRUE, -1, -1, -1, -1, CH, FALSE, NO , AL }, /* 18 */
99
100 /* Test Pango alignment (not xalign) */
101 { "0 0 0 0 T -1 -1 -1 -1 CH F NO AL", TESTL, 0, 0, 0.0, 0.0, TRUE, -1, -1, -1, 20, WO, FALSE, NO , AL }, /* 19 */
102 { "0 0 0 0 T -1 -1 -1 -1 CH F NO AC", TESTC, 0, 0, 0.0, 0.0, TRUE, -1, -1, -1, 20, WO, FALSE, NO , AC }, /* 20 */
103 { "0 0 0 0 T -1 -1 -1 -1 CH F NO AR", TESTR, 0, 0, 0.0, 0.0, TRUE, -1, -1, -1, 20, WO, FALSE, NO , AR }, /* 21 */
104};
105
106static GtkListStore *
107create_list_store (void)
108{
109 GtkListStore *list_store;
110 int i;
111
112 list_store = gtk_list_store_new (NUM_COLS,
113 G_TYPE_STRING, /* 0 */
114 G_TYPE_STRING, /* 1 */
115 G_TYPE_INT, /* 2 */
116 G_TYPE_INT, /* 3 */
117 G_TYPE_DOUBLE, /* 4 */
118 G_TYPE_DOUBLE, /* 5 */
119 G_TYPE_BOOLEAN, /* 6 */
120 G_TYPE_INT, /* 7 */
121 G_TYPE_INT, /* 8 */
122 G_TYPE_INT, /* 9 */
123 G_TYPE_INT, /* 10 */
124 PANGO_TYPE_WRAP_MODE, /* 11 */
125 G_TYPE_BOOLEAN, /* 12 */
126 PANGO_TYPE_ELLIPSIZE_MODE, /* 13 */
127 PANGO_TYPE_ALIGNMENT, /* 14 */
128 G_TYPE_STRING, /* 15 */
129 G_TYPE_STRING); /* 16 */
130
131 for (i = 0; i < G_N_ELEMENTS (cell_params); i++)
132 {
133 const struct cell_params *p;
134 GtkTreeIter iter;
135 char buf[50];
136
137 p = cell_params + i;
138
139 g_snprintf (string: buf, n: sizeof (buf), format: "%d", i);
140
141 gtk_list_store_append (list_store, iter: &iter);
142 gtk_list_store_set (list_store, iter: &iter,
143 0, p->description,
144 1, p->test,
145 2, p->xpad,
146 3, p->ypad,
147 4, p->xalign,
148 5, p->yalign,
149 6, p->sensitive,
150 7, p->width,
151 8, p->height,
152 9, p->width_chars,
153 10, p->wrap_width,
154 11, p->wrap_mode,
155 12, p->single_paragraph_mode,
156 13, p->ellipsize,
157 14, p->alignment,
158 15, (i % 2 == 0) ? "gray50" : "gray80",
159 16, buf,
160 -1);
161 }
162
163 return list_store;
164}
165
166static GtkWidget *
167create_tree (gboolean rtl)
168{
169 GtkWidget *sw;
170 GtkWidget *treeview;
171 GtkListStore *list_store;
172 GtkTreeViewColumn *column;
173 GtkCellRenderer *renderer;
174 GdkPixbuf *pixbuf;
175
176 sw = gtk_scrolled_window_new ();
177 gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
178 gtk_widget_set_direction (widget: sw, dir: rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
179
180 list_store = create_list_store ();
181
182 treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
183 gtk_widget_set_direction (widget: treeview, dir: rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
184 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child: treeview);
185
186 /* Line number */
187
188 renderer = gtk_cell_renderer_text_new ();
189 column = gtk_tree_view_column_new_with_attributes (title: "#",
190 cell: renderer,
191 "text", COL_LINE_NUM,
192 NULL);
193 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
194
195 /* Description */
196
197 renderer = gtk_cell_renderer_text_new ();
198 g_object_set (object: renderer,
199 first_property_name: "font", "monospace",
200 NULL);
201 column = gtk_tree_view_column_new_with_attributes (title: "Description",
202 cell: renderer,
203 "text", 0,
204 NULL);
205 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
206
207 /* Test text */
208
209 renderer = gtk_cell_renderer_text_new ();
210 column = gtk_tree_view_column_new_with_attributes (title: "Test",
211 cell: renderer,
212 "text", 1,
213 "xpad", 2,
214 "ypad", 3,
215 "xalign", 4,
216 "yalign", 5,
217 "sensitive", 6,
218 "width", 7,
219 "height", 8,
220 "width_chars", 9,
221 "wrap_width", 10,
222 "wrap_mode", 11,
223 "single_paragraph_mode", 12,
224 "ellipsize", 13,
225 "alignment", 14,
226 "cell_background", 15,
227 NULL);
228 gtk_tree_view_column_set_resizable (tree_column: column, TRUE);
229 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
230
231 /* Empty column */
232
233 pixbuf = gdk_pixbuf_new_from_file (filename: "apple-red.png", NULL);
234
235 renderer = gtk_cell_renderer_pixbuf_new ();
236 g_object_set (object: renderer,
237 first_property_name: "pixbuf", pixbuf,
238 "xpad", 10,
239 "ypad", 10,
240 NULL);
241 column = gtk_tree_view_column_new_with_attributes (title: "Empty",
242 cell: renderer,
243 NULL);
244 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
245
246 return sw;
247}
248
249static void
250quit_cb (GtkWidget *widget,
251 gpointer data)
252{
253 gboolean *done = data;
254
255 *done = TRUE;
256
257 g_main_context_wakeup (NULL);
258}
259
260int
261main (int argc, char **argv)
262{
263 GtkWidget *window;
264 GtkWidget *vbox;
265 GtkWidget *label;
266 GtkWidget *tree;
267 gboolean done = FALSE;
268
269 gtk_init ();
270
271 window = gtk_window_new ();
272 g_signal_connect (window, "destroy",
273 G_CALLBACK (quit_cb), &done);
274
275 vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 12);
276 gtk_window_set_child (GTK_WINDOW (window), child: vbox);
277
278 /* LTR */
279
280 label = gtk_label_new (str: "Left to right");
281 gtk_box_append (GTK_BOX (vbox), child: label);
282
283 tree = create_tree (FALSE);
284 gtk_widget_set_vexpand (widget: tree, TRUE);
285 gtk_box_append (GTK_BOX (vbox), child: tree);
286
287 /* RTL */
288
289 label = gtk_label_new (str: "Right to left");
290 gtk_box_append (GTK_BOX (vbox), child: label);
291
292 tree = create_tree (TRUE);
293 gtk_widget_set_vexpand (widget: tree, TRUE);
294 gtk_box_append (GTK_BOX (vbox), child: tree);
295
296 gtk_widget_show (widget: window);
297
298 while (!done)
299 g_main_context_iteration (NULL, TRUE);
300
301 return 0;
302}
303

source code of gtk/tests/testcellrenderertext.c