]> Pileus Git - ~andy/gtk/blob - tests/testcellrenderertext.c
New file with a set of tests for GtkCellRendererText. The idea is to run a
[~andy/gtk] / tests / testcellrenderertext.c
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, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <gtk/gtk.h>
25
26 #define COL_BACKGROUND 14
27 #define COL_LINE_NUM   15
28 #define NUM_COLS       16 /* change this when adding columns */
29
30 struct cell_params {
31   char *description;                    /* 0 */
32   char *test;                           /* 1 */
33   int xpad;                             /* 2 */
34   int ypad;                             /* 3 */
35   double xalign;                        /* 4 */
36   double yalign;                        /* 5 */
37   gboolean sensitive;                   /* 6 */
38   int width;                            /* 7 */
39   int height;                           /* 8 */
40   int width_chars;                      /* 9 */
41   int wrap_width;                       /* 10 */
42   PangoWrapMode wrap_mode;              /* 11 */
43   gboolean single_paragraph_mode;       /* 12 */
44   PangoEllipsizeMode ellipsize;         /* 13 */
45   /* COL_BACKGROUND      */             /* 14 */
46   /* COL_LINE_NUM */                    /* 15 */
47 };
48
49 #define WO PANGO_WRAP_WORD
50 #define CH PANGO_WRAP_CHAR
51 #define WC PANGO_WRAP_WORD_CHAR
52
53 #define NO PANGO_ELLIPSIZE_NONE
54 #define ST PANGO_ELLIPSIZE_START
55 #define MI PANGO_ELLIPSIZE_MIDDLE
56 #define EN PANGO_ELLIPSIZE_END
57
58
59 #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"
60
61 #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"
62
63 #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"
64
65 static const struct cell_params cell_params[] = {
66   { "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 */
67
68   /* Test alignment */
69
70   { "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 }, /* 1 */
71   { "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 }, /* 2 */
72   { "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 }, /* 3 */
73   { "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 }, /* 4 */
74   { "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 }, /* 5 */
75   { "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 }, /* 6 */
76   { "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 }, /* 7 */
77   { "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 }, /* 8 */
78   { "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 }, /* 9 */
79
80   /* Test padding */
81
82   { "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 }, /* 10 */
83   { "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 }, /* 11 */
84   { "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 }, /* 12 */
85   { "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 }, /* 13 */
86   { "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 }, /* 14 */
87   { "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 }, /* 15 */
88   { "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 }, /* 16 */
89   { "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 }, /* 17 */
90   { "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 }, /* 18 */
91
92
93 };
94
95 static GtkListStore *
96 create_list_store (void)
97 {
98   GtkListStore *list_store;
99   int i;
100
101   list_store = gtk_list_store_new (NUM_COLS,
102                                    G_TYPE_STRING,               /* 0 */ 
103                                    G_TYPE_STRING,               /* 1 */ 
104                                    G_TYPE_INT,                  /* 2 */ 
105                                    G_TYPE_INT,                  /* 3 */ 
106                                    G_TYPE_DOUBLE,               /* 4 */ 
107                                    G_TYPE_DOUBLE,               /* 5 */ 
108                                    G_TYPE_BOOLEAN,              /* 6 */ 
109                                    G_TYPE_INT,                  /* 7 */ 
110                                    G_TYPE_INT,                  /* 8 */ 
111                                    G_TYPE_INT,                  /* 9 */ 
112                                    G_TYPE_INT,                  /* 10 */
113                                    PANGO_TYPE_WRAP_MODE,        /* 11 */
114                                    G_TYPE_BOOLEAN,              /* 12 */
115                                    PANGO_TYPE_ELLIPSIZE_MODE,   /* 13 */
116                                    G_TYPE_STRING,               /* 14 */
117                                    G_TYPE_STRING);              /* 15 */
118
119   for (i = 0; i < G_N_ELEMENTS (cell_params); i++)
120     {
121       const struct cell_params *p;
122       GtkTreeIter iter;
123       char buf[50];
124
125       p = cell_params + i;
126
127       snprintf (buf, sizeof (buf), "%d", i);
128
129       gtk_list_store_append (list_store, &iter);
130       gtk_list_store_set (list_store, &iter,
131                           0, p->description,
132                           1, p->test,
133                           2, p->xpad,
134                           3, p->ypad,
135                           4, p->xalign,
136                           5, p->yalign,
137                           6, p->sensitive,
138                           7, p->width,
139                           8, p->height,
140                           9, p->width_chars,
141                           10, p->wrap_width,
142                           11, p->wrap_mode,
143                           12, p->single_paragraph_mode,
144                           13, p->ellipsize,
145                           14, (i % 2 == 0) ? "gray50" : "gray80",
146                           15, buf,
147                           -1);
148     }
149
150   return list_store;
151 }
152
153 static GtkWidget *
154 create_tree (gboolean rtl)
155 {
156   GtkWidget *sw;
157   GtkWidget *treeview;
158   GtkListStore *list_store;
159   GtkTreeViewColumn *column;
160   GtkCellRenderer *renderer;
161   GdkPixbuf *pixbuf;
162
163   sw = gtk_scrolled_window_new (NULL, NULL);
164   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
165   gtk_widget_set_direction (sw, rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
166
167   list_store = create_list_store ();
168
169   treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
170   gtk_widget_set_direction (treeview, rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
171   gtk_container_add (GTK_CONTAINER (sw), treeview);
172
173   /* Line number */
174
175   renderer = gtk_cell_renderer_text_new ();
176   column = gtk_tree_view_column_new_with_attributes ("#",
177                                                      renderer,
178                                                      "text", COL_LINE_NUM,
179                                                      NULL);
180   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
181
182   /* Description */
183
184   renderer = gtk_cell_renderer_text_new ();
185   g_object_set (renderer,
186                 "font", "monospace",
187                 NULL);
188   column = gtk_tree_view_column_new_with_attributes ("Description",
189                                                      renderer,
190                                                      "text", 0,
191                                                      NULL);
192   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
193
194   /* Test text */
195
196   renderer = gtk_cell_renderer_text_new ();
197   column = gtk_tree_view_column_new_with_attributes ("Test",
198                                                      renderer,
199                                                      "text", 1,
200                                                      "xpad", 2,
201                                                      "ypad", 3,
202                                                      "xalign", 4,
203                                                      "yalign", 5,
204                                                      "sensitive", 6,
205                                                      "width", 7,
206                                                      "height", 8,
207                                                      "width_chars", 9,
208                                                      "wrap_width", 10,
209                                                      "wrap_mode", 11,
210                                                      "single_paragraph_mode", 12,
211                                                      "ellipsize", 13,
212                                                      "cell_background", 14,
213                                                      NULL);
214   gtk_tree_view_column_set_resizable (column, TRUE);
215   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
216
217   /* Empty column */
218
219   pixbuf = gdk_pixbuf_new_from_file ("apple-red.png", NULL);
220
221   renderer = gtk_cell_renderer_pixbuf_new ();
222   g_object_set (renderer,
223                 "pixbuf", pixbuf,
224                 "xpad", 10,
225                 "ypad", 10,
226                 NULL);
227   column = gtk_tree_view_column_new_with_attributes ("Empty",
228                                                      renderer,
229                                                      NULL);
230   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
231
232   return sw;
233 }
234
235 int
236 main (int argc, char **argv)
237 {
238   GtkWidget *window;
239   GtkWidget *vbox;
240   GtkWidget *label;
241   GtkWidget *tree;
242
243   gtk_init (&argc, &argv);
244
245   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
246   g_signal_connect (window, "destroy",
247                     G_CALLBACK (gtk_main_quit), NULL);
248   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
249
250   vbox = gtk_vbox_new (FALSE, 12);
251   gtk_container_add (GTK_CONTAINER (window), vbox);
252
253   /* LTR */
254
255   label = gtk_label_new ("Left to right");
256   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
257
258   tree = create_tree (FALSE);
259   gtk_box_pack_start (GTK_BOX (vbox), tree, TRUE, TRUE, 0);
260
261   /* RTL */
262
263   label = gtk_label_new ("Right to left");
264   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
265
266   tree = create_tree (TRUE);
267   gtk_box_pack_start (GTK_BOX (vbox), tree, TRUE, TRUE, 0);
268
269   gtk_widget_show_all (window);
270   gtk_main ();
271
272   return 0;
273 }