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
27static void
28combo_changed_cb (GtkWidget *combo,
29 gpointer data)
30{
31 GtkWidget *label = GTK_WIDGET (data);
32 int active;
33
34 active = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
35 gtk_label_set_ellipsize (GTK_LABEL (label), mode: (PangoEllipsizeMode)active);
36}
37
38static void
39overlay_draw (GtkDrawingArea *da,
40 cairo_t *cr,
41 int width,
42 int height,
43 gpointer data)
44{
45 GtkWidget *widget = GTK_WIDGET (da);
46 PangoLayout *layout;
47 const double dashes[] = { 6, 18 };
48 GtkAllocation label_allocation;
49 GtkRequisition minimum_size, natural_size;
50 GtkWidget *label = data;
51 double x, y;
52
53 cairo_translate (cr, tx: -0.5, ty: -0.5);
54 cairo_set_line_width (cr, width: 1);
55
56 cairo_set_source_rgb (cr, red: 1, green: 1, blue: 1);
57 cairo_rectangle (cr, x: 0, y: 0, width, height);
58 cairo_fill (cr);
59
60 gtk_widget_translate_coordinates (src_widget: label, dest_widget: widget, src_x: 0, src_y: 0, dest_x: &x, dest_y: &y);
61 layout = gtk_widget_create_pango_layout (widget, text: "");
62
63 gtk_widget_get_preferred_size (widget: label, minimum_size: &minimum_size, natural_size: &natural_size);
64
65 pango_layout_set_markup (layout,
66 markup: "<span color='#c33'>\342\227\217 requisition</span>\n"
67 "<span color='#3c3'>\342\227\217 natural size</span>\n"
68 "<span color='#33c'>\342\227\217 allocation</span>", length: -1);
69
70 pango_cairo_show_layout (cr, layout);
71 g_object_unref (object: layout);
72
73 gtk_widget_get_allocation (widget: label, allocation: &label_allocation);
74
75 cairo_rectangle (cr,
76 x: x + 0.5 * (label_allocation.width - minimum_size.width),
77 y: y + 0.5 * (label_allocation.height - minimum_size.height),
78 width: minimum_size.width, height: minimum_size.height);
79 cairo_set_source_rgb (cr, red: 0.8, green: 0.2, blue: 0.2);
80 cairo_set_dash (cr, NULL, num_dashes: 0, offset: 0);
81 cairo_stroke (cr);
82
83 cairo_rectangle (cr, x, y, width: label_allocation.width, height: label_allocation.height);
84 cairo_set_source_rgb (cr, red: 0.2, green: 0.2, blue: 0.8);
85 cairo_set_dash (cr, dashes, num_dashes: 2, offset: 0.5);
86 cairo_stroke (cr);
87
88 cairo_rectangle (cr,
89 x: x + 0.5 * (label_allocation.width - natural_size.width),
90 y: y + 0.5 * (label_allocation.height - natural_size.height),
91 width: natural_size.width, height: natural_size.height);
92 cairo_set_source_rgb (cr, red: 0.2, green: 0.8, blue: 0.2);
93 cairo_set_dash (cr, dashes, num_dashes: 2, offset: 12.5);
94 cairo_stroke (cr);
95}
96
97static void
98quit_cb (GtkWidget *widget,
99 gpointer data)
100{
101 gboolean *done = data;
102
103 *done = TRUE;
104
105 g_main_context_wakeup (NULL);
106}
107
108int
109main (int argc, char *argv[])
110{
111 GtkWidget *window, *vbox, *label;
112 GtkWidget *combo, *scale, *overlay, *da;
113 gboolean done = FALSE;
114
115 gtk_init ();
116
117 window = gtk_window_new ();
118 gtk_window_set_default_size (GTK_WINDOW (window), width: 400, height: 300);
119 g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
120
121 vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 6);
122 gtk_window_set_child (GTK_WINDOW (window), child: vbox);
123
124 combo = gtk_combo_box_text_new ();
125 scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL,
126 min: 0, max: 360, step: 1);
127 label = gtk_label_new (str: "This label may be ellipsized\nto make it fit.");
128
129 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "NONE");
130 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "START");
131 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "MIDDLE");
132 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "END");
133 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), index_: 0);
134
135 gtk_widget_set_halign (widget: label, align: GTK_ALIGN_CENTER);
136 gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER);
137
138 da = gtk_drawing_area_new ();
139 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_func: overlay_draw, user_data: label, NULL);
140
141 overlay = gtk_overlay_new ();
142 gtk_overlay_set_child (GTK_OVERLAY (overlay), child: da);
143 gtk_widget_set_vexpand (widget: overlay, TRUE);
144 gtk_overlay_add_overlay (GTK_OVERLAY (overlay), widget: label);
145
146 gtk_box_append (GTK_BOX (vbox), child: combo);
147 gtk_box_append (GTK_BOX (vbox), child: scale);
148 gtk_box_append (GTK_BOX (vbox), child: overlay);
149
150 g_object_set_data (G_OBJECT (label), key: "combo", data: combo);
151
152 g_signal_connect (combo, "changed", G_CALLBACK (combo_changed_cb), label);
153
154 gtk_widget_show (widget: window);
155
156 while (!done)
157 g_main_context_iteration (NULL, TRUE);
158
159 return 0;
160}
161

source code of gtk/tests/testellipsise.c