]> Pileus Git - ~andy/gtk/blob - tests/testcellrenderertext.c
Add a comment - Federico
[~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
66 /* DO NOT CHANGE THE ROWS!  They are numbered so that we can refer to
67  * problematic rows in bug reports.  If you need a different test, just add a
68  * new row at the bottom.  Also, please add your new row numbers to this column -------------------------------+
69  * to keep things tidy.                                                                                        v
70  */
71 static const struct cell_params cell_params[] = {
72   { "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 */
73
74   /* Test alignment */
75
76   { "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 */
77   { "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 */
78   { "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 */
79   { "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 */
80   { "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 */
81   { "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 */
82   { "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 */
83   { "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 */
84   { "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 */
85
86   /* Test padding */
87
88   { "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 */
89   { "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 */
90   { "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 */
91   { "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 */
92   { "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 */
93   { "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 */
94   { "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 */
95   { "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 */
96   { "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 */
97
98
99 };
100
101 static GtkListStore *
102 create_list_store (void)
103 {
104   GtkListStore *list_store;
105   int i;
106
107   list_store = gtk_list_store_new (NUM_COLS,
108                                    G_TYPE_STRING,               /* 0 */ 
109                                    G_TYPE_STRING,               /* 1 */ 
110                                    G_TYPE_INT,                  /* 2 */ 
111                                    G_TYPE_INT,                  /* 3 */ 
112                                    G_TYPE_DOUBLE,               /* 4 */ 
113                                    G_TYPE_DOUBLE,               /* 5 */ 
114                                    G_TYPE_BOOLEAN,              /* 6 */ 
115                                    G_TYPE_INT,                  /* 7 */ 
116                                    G_TYPE_INT,                  /* 8 */ 
117                                    G_TYPE_INT,                  /* 9 */ 
118                                    G_TYPE_INT,                  /* 10 */
119                                    PANGO_TYPE_WRAP_MODE,        /* 11 */
120                                    G_TYPE_BOOLEAN,              /* 12 */
121                                    PANGO_TYPE_ELLIPSIZE_MODE,   /* 13 */
122                                    G_TYPE_STRING,               /* 14 */
123                                    G_TYPE_STRING);              /* 15 */
124
125   for (i = 0; i < G_N_ELEMENTS (cell_params); i++)
126     {
127       const struct cell_params *p;
128       GtkTreeIter iter;
129       char buf[50];
130
131       p = cell_params + i;
132
133       snprintf (buf, sizeof (buf), "%d", i);
134
135       gtk_list_store_append (list_store, &iter);
136       gtk_list_store_set (list_store, &iter,
137                           0, p->description,
138                           1, p->test,
139                           2, p->xpad,
140                           3, p->ypad,
141                           4, p->xalign,
142                           5, p->yalign,
143                           6, p->sensitive,
144                           7, p->width,
145                           8, p->height,
146                           9, p->width_chars,
147                           10, p->wrap_width,
148                           11, p->wrap_mode,
149                           12, p->single_paragraph_mode,
150                           13, p->ellipsize,
151                           14, (i % 2 == 0) ? "gray50" : "gray80",
152                           15, buf,
153                           -1);
154     }
155
156   return list_store;
157 }
158
159 static GtkWidget *
160 create_tree (gboolean rtl)
161 {
162   GtkWidget *sw;
163   GtkWidget *treeview;
164   GtkListStore *list_store;
165   GtkTreeViewColumn *column;
166   GtkCellRenderer *renderer;
167   GdkPixbuf *pixbuf;
168
169   sw = gtk_scrolled_window_new (NULL, NULL);
170   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
171   gtk_widget_set_direction (sw, rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
172
173   list_store = create_list_store ();
174
175   treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
176   gtk_widget_set_direction (treeview, rtl ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR);
177   gtk_container_add (GTK_CONTAINER (sw), treeview);
178
179   /* Line number */
180
181   renderer = gtk_cell_renderer_text_new ();
182   column = gtk_tree_view_column_new_with_attributes ("#",
183                                                      renderer,
184                                                      "text", COL_LINE_NUM,
185                                                      NULL);
186   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
187
188   /* Description */
189
190   renderer = gtk_cell_renderer_text_new ();
191   g_object_set (renderer,
192                 "font", "monospace",
193                 NULL);
194   column = gtk_tree_view_column_new_with_attributes ("Description",
195                                                      renderer,
196                                                      "text", 0,
197                                                      NULL);
198   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
199
200   /* Test text */
201
202   renderer = gtk_cell_renderer_text_new ();
203   column = gtk_tree_view_column_new_with_attributes ("Test",
204                                                      renderer,
205                                                      "text", 1,
206                                                      "xpad", 2,
207                                                      "ypad", 3,
208                                                      "xalign", 4,
209                                                      "yalign", 5,
210                                                      "sensitive", 6,
211                                                      "width", 7,
212                                                      "height", 8,
213                                                      "width_chars", 9,
214                                                      "wrap_width", 10,
215                                                      "wrap_mode", 11,
216                                                      "single_paragraph_mode", 12,
217                                                      "ellipsize", 13,
218                                                      "cell_background", 14,
219                                                      NULL);
220   gtk_tree_view_column_set_resizable (column, TRUE);
221   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
222
223   /* Empty column */
224
225   pixbuf = gdk_pixbuf_new_from_file ("apple-red.png", NULL);
226
227   renderer = gtk_cell_renderer_pixbuf_new ();
228   g_object_set (renderer,
229                 "pixbuf", pixbuf,
230                 "xpad", 10,
231                 "ypad", 10,
232                 NULL);
233   column = gtk_tree_view_column_new_with_attributes ("Empty",
234                                                      renderer,
235                                                      NULL);
236   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
237
238   return sw;
239 }
240
241 int
242 main (int argc, char **argv)
243 {
244   GtkWidget *window;
245   GtkWidget *vbox;
246   GtkWidget *label;
247   GtkWidget *tree;
248
249   gtk_init (&argc, &argv);
250
251   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
252   g_signal_connect (window, "destroy",
253                     G_CALLBACK (gtk_main_quit), NULL);
254   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
255
256   vbox = gtk_vbox_new (FALSE, 12);
257   gtk_container_add (GTK_CONTAINER (window), vbox);
258
259   /* LTR */
260
261   label = gtk_label_new ("Left to right");
262   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
263
264   tree = create_tree (FALSE);
265   gtk_box_pack_start (GTK_BOX (vbox), tree, TRUE, TRUE, 0);
266
267   /* RTL */
268
269   label = gtk_label_new ("Right to left");
270   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
271
272   tree = create_tree (TRUE);
273   gtk_box_pack_start (GTK_BOX (vbox), tree, TRUE, TRUE, 0);
274
275   gtk_widget_show_all (window);
276   gtk_main ();
277
278   return 0;
279 }