]> Pileus Git - ~andy/gtk/blob - tests/testellipsise.c
Don't use GTK_WIDGET_NO_WINDOW in GtkContainer
[~andy/gtk] / tests / testellipsise.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /*
20  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21  * file for a list of people on the GTK+ Team.  See the ChangeLog
22  * files for a list of changes.  These files are distributed with
23  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
24  */
25
26 #include <gtk/gtk.h>
27
28 static void
29 redraw_event_box (GtkWidget *widget)
30 {
31   while (widget)
32     {
33       if (GTK_IS_EVENT_BOX (widget))
34         {
35           gtk_widget_queue_draw (widget);
36           break;
37         }
38
39       widget = gtk_widget_get_parent (widget);
40     }
41 }
42
43 static void
44 combo_changed_cb (GtkWidget *combo,
45                   gpointer   data)
46 {
47   GtkWidget *label = GTK_WIDGET (data);
48   gint active;
49
50   active = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
51   gtk_label_set_ellipsize (GTK_LABEL (label), (PangoEllipsizeMode)active);
52   redraw_event_box (label);
53 }
54
55 static void
56 scale_changed_cb (GtkRange *range,
57                   gpointer   data)
58 {
59   double angle = gtk_range_get_value (range);
60   GtkWidget *label = GTK_WIDGET (data);
61
62   gtk_label_set_angle (GTK_LABEL (label), angle);
63   redraw_event_box (label);
64 }
65
66 static gboolean
67 ebox_expose_event_cb (GtkWidget      *widget,
68                       GdkEventExpose *event,
69                       gpointer        data)
70 {
71   PangoLayout *layout;
72   const double dashes[] = { 6, 18 };
73   GtkRequisition natural_size;
74   GtkWidget *label = data;
75   gint x, y, dx, dy;
76   gchar *markup;
77   cairo_t *cr;
78
79   gtk_widget_translate_coordinates (label, widget, 0, 0, &x, &y);
80
81   cr = gdk_cairo_create (widget->window);
82   cairo_translate (cr, -0.5, -0.5);
83   cairo_set_line_width (cr, 1);
84
85   cairo_rectangle (cr,
86                    x + 0.5 * (label->allocation.width - label->requisition.width),
87                    y + 0.5 * (label->allocation.height - label->requisition.height),
88                    label->requisition.width, label->requisition.height);
89   cairo_set_source_rgb (cr, 0.8, 0.2, 0.2);
90   cairo_set_dash (cr, NULL, 0, 0);
91   cairo_stroke (cr);
92
93   cairo_rectangle (cr, x, y, label->allocation.width, label->allocation.height);
94   cairo_set_source_rgb (cr, 0.2, 0.2, 0.8);
95   cairo_set_dash (cr, dashes, 2, 0.5);
96   cairo_stroke (cr);
97
98   gtk_extended_layout_get_desired_size (GTK_EXTENDED_LAYOUT (label),
99                                         NULL, &natural_size);
100
101   cairo_rectangle (cr,
102                    x + 0.5 * (label->allocation.width - natural_size.width),
103                    y + 0.5 * (label->allocation.height - natural_size.height),
104                    natural_size.width, natural_size.height);
105   cairo_set_source_rgb (cr, 0.2, 0.8, 0.2);
106   cairo_set_dash (cr, dashes, 2, 12.5);
107   cairo_stroke (cr);
108
109   markup = g_strdup_printf (
110     "<span color='#c33'>\342\200\242 requisition:\t%dx%d</span>\n"
111     "<span color='#3c3'>\342\200\242 natural size:\t%dx%d</span>\n"
112     "<span color='#33c'>\342\200\242 allocation:\t%dx%d</span>",
113     label->requisition.width, label->requisition.height,
114     natural_size.width, natural_size.height,
115     label->allocation.width, label->allocation.height);
116
117   layout = gtk_widget_create_pango_layout (widget, NULL);
118   pango_layout_set_markup (layout, markup, -1);
119   pango_layout_get_pixel_size (layout, &dx, &dy);
120
121   g_free (markup);
122
123   cairo_translate (cr, 0, widget->allocation.height - dy - 8);
124
125   cairo_set_source_rgba (cr, 1, 1, 1, 0.8);
126   cairo_rectangle (cr, 0, 0, dx + 12, dy + 8);
127   cairo_fill (cr);
128
129   cairo_translate (cr, 6, 4);
130   pango_cairo_show_layout (cr, layout);
131
132   g_object_unref (layout);
133   cairo_destroy (cr);
134
135   return FALSE;
136 }
137
138 int
139 main (int argc, char *argv[])
140 {
141   GtkWidget *window, *vbox, *label;
142   GtkWidget *combo, *scale, *align, *ebox;
143
144   gtk_init (&argc, &argv);
145
146   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
147   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
148   gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
149   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
150
151   vbox = gtk_vbox_new (FALSE, 6);
152   gtk_container_add (GTK_CONTAINER (window), vbox);
153
154   combo = gtk_combo_box_new_text ();
155   scale = gtk_hscale_new_with_range (0, 360, 1);
156   label = gtk_label_new ("This label may be ellipsized\nto make it fit.");
157
158   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "NONE");
159   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "START");
160   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "MIDDLE");
161   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "END");
162   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
163
164   align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
165   gtk_container_add (GTK_CONTAINER (align), label);
166
167   ebox = gtk_event_box_new ();
168   gtk_widget_set_app_paintable (ebox, TRUE);
169   gtk_container_add (GTK_CONTAINER (ebox), align);
170
171   gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, TRUE, 0);
172   gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, TRUE, 0);
173   gtk_box_pack_start (GTK_BOX (vbox), ebox, TRUE, TRUE, 0);
174
175   g_object_set_data (G_OBJECT (label), "combo", combo);
176
177   g_signal_connect (combo, "changed", G_CALLBACK (combo_changed_cb), label);
178   g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed_cb), label);
179   g_signal_connect_after (ebox, "expose-event", G_CALLBACK (ebox_expose_event_cb), label);
180
181   gtk_widget_show_all (window);
182
183   gtk_main ();
184
185   return 0;
186 }