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, see <http://www.gnu.org/licenses/>.Free
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include <gtk/gtk.h>
26
27 static void
redraw_event_box(GtkWidget * widget)28 redraw_event_box (GtkWidget *widget)
29 {
30 while (widget)
31 {
32 if (GTK_IS_EVENT_BOX (widget))
33 {
34 gtk_widget_queue_draw (widget);
35 break;
36 }
37
38 widget = gtk_widget_get_parent (widget);
39 }
40 }
41
42 static void
combo_changed_cb(GtkWidget * combo,gpointer data)43 combo_changed_cb (GtkWidget *combo,
44 gpointer data)
45 {
46 GtkWidget *label = GTK_WIDGET (data);
47 gint active;
48
49 active = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
50 gtk_label_set_ellipsize (GTK_LABEL (label), (PangoEllipsizeMode)active);
51 redraw_event_box (label);
52 }
53
54 static void
scale_changed_cb(GtkRange * range,gpointer data)55 scale_changed_cb (GtkRange *range,
56 gpointer data)
57 {
58 double angle = gtk_range_get_value (range);
59 GtkWidget *label = GTK_WIDGET (data);
60
61 gtk_label_set_angle (GTK_LABEL (label), angle);
62 redraw_event_box (label);
63 }
64
65 static gboolean
ebox_draw_cb(GtkWidget * widget,cairo_t * cr,gpointer data)66 ebox_draw_cb (GtkWidget *widget,
67 cairo_t *cr,
68 gpointer data)
69 {
70 PangoLayout *layout;
71 const double dashes[] = { 6, 18 };
72 GtkAllocation label_allocation;
73 GtkRequisition minimum_size, natural_size;
74 GtkWidget *label = data;
75 gint x, y;
76
77 cairo_translate (cr, -0.5, -0.5);
78 cairo_set_line_width (cr, 1);
79
80 cairo_set_source_rgb (cr, 1, 1, 1);
81 cairo_paint (cr);
82
83 gtk_widget_translate_coordinates (label, widget, 0, 0, &x, &y);
84 layout = gtk_widget_create_pango_layout (widget, "");
85
86 gtk_widget_get_preferred_size (label, &minimum_size, &natural_size);
87
88 pango_layout_set_markup (layout,
89 "<span color='#c33'>\342\227\217 requisition</span>\n"
90 "<span color='#3c3'>\342\227\217 natural size</span>\n"
91 "<span color='#33c'>\342\227\217 allocation</span>", -1);
92
93 pango_cairo_show_layout (cr, layout);
94 g_object_unref (layout);
95
96 gtk_widget_get_allocation (label, &label_allocation);
97
98 cairo_rectangle (cr,
99 x + 0.5 * (label_allocation.width - minimum_size.width),
100 y + 0.5 * (label_allocation.height - minimum_size.height),
101 minimum_size.width, minimum_size.height);
102 cairo_set_source_rgb (cr, 0.8, 0.2, 0.2);
103 cairo_set_dash (cr, NULL, 0, 0);
104 cairo_stroke (cr);
105
106 cairo_rectangle (cr, x, y, label_allocation.width, label_allocation.height);
107 cairo_set_source_rgb (cr, 0.2, 0.2, 0.8);
108 cairo_set_dash (cr, dashes, 2, 0.5);
109 cairo_stroke (cr);
110
111 cairo_rectangle (cr,
112 x + 0.5 * (label_allocation.width - natural_size.width),
113 y + 0.5 * (label_allocation.height - natural_size.height),
114 natural_size.width, natural_size.height);
115 cairo_set_source_rgb (cr, 0.2, 0.8, 0.2);
116 cairo_set_dash (cr, dashes, 2, 12.5);
117 cairo_stroke (cr);
118
119 return FALSE;
120 }
121
122 int
main(int argc,char * argv[])123 main (int argc, char *argv[])
124 {
125 GtkWidget *window, *vbox, *label;
126 GtkWidget *combo, *scale, *ebox;
127
128 gtk_init (&argc, &argv);
129
130 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
131 gtk_container_set_border_width (GTK_CONTAINER (window), 12);
132 gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
133 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
134
135 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
136 gtk_container_add (GTK_CONTAINER (window), vbox);
137
138 combo = gtk_combo_box_text_new ();
139 scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
140 0, 360, 1);
141 label = gtk_label_new ("This label may be ellipsized\nto make it fit.");
142
143 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "NONE");
144 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "START");
145 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "MIDDLE");
146 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "END");
147 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
148
149 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
150 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
151
152 ebox = gtk_event_box_new ();
153 gtk_widget_set_app_paintable (ebox, TRUE);
154 gtk_container_add (GTK_CONTAINER (ebox), label);
155
156 gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, TRUE, 0);
157 gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, TRUE, 0);
158 gtk_box_pack_start (GTK_BOX (vbox), ebox, TRUE, TRUE, 0);
159
160 g_object_set_data (G_OBJECT (label), "combo", combo);
161
162 g_signal_connect (combo, "changed", G_CALLBACK (combo_changed_cb), label);
163 g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed_cb), label);
164 g_signal_connect (ebox, "draw", G_CALLBACK (ebox_draw_cb), label);
165
166 gtk_widget_show_all (window);
167
168 gtk_main ();
169
170 return 0;
171 }
172