1 | /* testscale.c - scale mark demo |
2 | * Copyright (C) 2009 Red Hat, Inc. |
3 | * Author: Matthias Clasen |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include <gtk/gtk.h> |
20 | |
21 | |
22 | GSList *scales; |
23 | GtkWidget *flipbox; |
24 | GtkWidget *; |
25 | |
26 | static void |
27 | invert (GtkButton *button) |
28 | { |
29 | GSList *l; |
30 | |
31 | for (l = scales; l; l = l->next) |
32 | { |
33 | GtkRange *range = l->data; |
34 | gtk_range_set_inverted (range, setting: !gtk_range_get_inverted (range)); |
35 | } |
36 | } |
37 | |
38 | static void |
39 | flip (GtkButton *button) |
40 | { |
41 | GSList *l; |
42 | |
43 | gtk_orientable_set_orientation (GTK_ORIENTABLE (flipbox), orientation: 1 - gtk_orientable_get_orientation (GTK_ORIENTABLE (flipbox))); |
44 | |
45 | for (l = scales; l; l = l->next) |
46 | { |
47 | GtkOrientable *o = l->data; |
48 | gtk_orientable_set_orientation (orientable: o, orientation: 1 - gtk_orientable_get_orientation (orientable: o)); |
49 | } |
50 | } |
51 | |
52 | static void |
53 | trough (GtkToggleButton *button) |
54 | { |
55 | GSList *l; |
56 | gboolean value; |
57 | |
58 | value = gtk_toggle_button_get_active (toggle_button: button); |
59 | |
60 | for (l = scales; l; l = l->next) |
61 | { |
62 | GtkRange *range = l->data; |
63 | gtk_range_set_range (range, min: 0., max: value ? 100.0 : 0.); |
64 | } |
65 | } |
66 | |
67 | double marks[3] = { 0.0, 50.0, 100.0 }; |
68 | double [2] = { 20.0, 40.0 }; |
69 | |
70 | static void |
71 | (GtkToggleButton *button) |
72 | { |
73 | gboolean value; |
74 | |
75 | value = gtk_toggle_button_get_active (toggle_button: button); |
76 | |
77 | if (value) |
78 | { |
79 | gtk_scale_add_mark (GTK_SCALE (extra_scale), value: extra_marks[0], position: GTK_POS_TOP, NULL); |
80 | gtk_scale_add_mark (GTK_SCALE (extra_scale), value: extra_marks[1], position: GTK_POS_TOP, NULL); |
81 | } |
82 | else |
83 | { |
84 | gtk_scale_clear_marks (GTK_SCALE (extra_scale)); |
85 | gtk_scale_add_mark (GTK_SCALE (extra_scale), value: marks[0], position: GTK_POS_BOTTOM, NULL); |
86 | gtk_scale_add_mark (GTK_SCALE (extra_scale), value: marks[1], position: GTK_POS_BOTTOM, NULL); |
87 | gtk_scale_add_mark (GTK_SCALE (extra_scale), value: marks[2], position: GTK_POS_BOTTOM, NULL); |
88 | } |
89 | } |
90 | |
91 | static void |
92 | quit_cb (GtkWidget *widget, |
93 | gpointer data) |
94 | { |
95 | gboolean *done = data; |
96 | |
97 | *done = TRUE; |
98 | |
99 | g_main_context_wakeup (NULL); |
100 | } |
101 | |
102 | int main (int argc, char *argv[]) |
103 | { |
104 | GtkWidget *window; |
105 | GtkWidget *box; |
106 | GtkWidget *box1; |
107 | GtkWidget *box2; |
108 | GtkWidget *button; |
109 | GtkWidget *frame; |
110 | GtkWidget *scale; |
111 | const char *labels[3] = { |
112 | "<small>Left</small>" , |
113 | "<small>Middle</small>" , |
114 | "<small>Right</small>" |
115 | }; |
116 | |
117 | double bath_marks[4] = { 0.0, 33.3, 66.6, 100.0 }; |
118 | const char *bath_labels[4] = { |
119 | "<span color='blue' size='small'>Cold</span>" , |
120 | "<span size='small'>Baby bath</span>" , |
121 | "<span size='small'>Hot tub</span>" , |
122 | "<span color='Red' size='small'>Hot</span>" |
123 | }; |
124 | |
125 | double pos_marks[4] = { 0.0, 33.3, 66.6, 100.0 }; |
126 | const char *pos_labels[4] = { "Left" , "Right" , "Top" , "Bottom" }; |
127 | gboolean done = FALSE; |
128 | |
129 | gtk_init (); |
130 | |
131 | window = gtk_window_new (); |
132 | gtk_window_set_title (GTK_WINDOW (window), title: "Ranges with marks" ); |
133 | g_signal_connect (window, "destroy" , G_CALLBACK (quit_cb), &done); |
134 | box1 = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 5); |
135 | flipbox = box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 5); |
136 | gtk_widget_set_hexpand (widget: flipbox, TRUE); |
137 | gtk_widget_set_vexpand (widget: flipbox, TRUE); |
138 | gtk_box_append (GTK_BOX (box1), child: box); |
139 | gtk_window_set_child (GTK_WINDOW (window), child: box1); |
140 | |
141 | frame = gtk_frame_new (label: "No marks" ); |
142 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
143 | scales = g_slist_prepend (list: scales, data: scale); |
144 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
145 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
146 | gtk_box_append (GTK_BOX (box), child: frame); |
147 | |
148 | frame = gtk_frame_new (label: "With fill level" ); |
149 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
150 | scales = g_slist_prepend (list: scales, data: scale); |
151 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
152 | gtk_range_set_show_fill_level (GTK_RANGE (scale), TRUE); |
153 | gtk_range_set_fill_level (GTK_RANGE (scale), fill_level: 50); |
154 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
155 | gtk_box_append (GTK_BOX (box), child: frame); |
156 | |
157 | frame = gtk_frame_new (label: "Simple marks" ); |
158 | extra_scale = scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
159 | scales = g_slist_prepend (list: scales, data: scale); |
160 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
161 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[0], position: GTK_POS_BOTTOM, NULL); |
162 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[1], position: GTK_POS_BOTTOM, NULL); |
163 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[2], position: GTK_POS_BOTTOM, NULL); |
164 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
165 | gtk_box_append (GTK_BOX (box), child: frame); |
166 | |
167 | frame = gtk_frame_new (label: "Simple marks up" ); |
168 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
169 | scales = g_slist_prepend (list: scales, data: scale); |
170 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
171 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[0], position: GTK_POS_TOP, NULL); |
172 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[1], position: GTK_POS_TOP, NULL); |
173 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[2], position: GTK_POS_TOP, NULL); |
174 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
175 | gtk_box_append (GTK_BOX (box), child: frame); |
176 | |
177 | frame = gtk_frame_new (label: "Labeled marks" ); |
178 | box2 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 6); |
179 | |
180 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
181 | scales = g_slist_prepend (list: scales, data: scale); |
182 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
183 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[0], position: GTK_POS_BOTTOM, markup: labels[0]); |
184 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[1], position: GTK_POS_BOTTOM, markup: labels[1]); |
185 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[2], position: GTK_POS_BOTTOM, markup: labels[2]); |
186 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
187 | gtk_box_append (GTK_BOX (box), child: frame); |
188 | |
189 | frame = gtk_frame_new (label: "Some labels" ); |
190 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
191 | scales = g_slist_prepend (list: scales, data: scale); |
192 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
193 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[0], position: GTK_POS_TOP, markup: labels[0]); |
194 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[1], position: GTK_POS_TOP, NULL); |
195 | gtk_scale_add_mark (GTK_SCALE (scale), value: marks[2], position: GTK_POS_TOP, markup: labels[2]); |
196 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
197 | gtk_box_append (GTK_BOX (box), child: frame); |
198 | |
199 | frame = gtk_frame_new (label: "Above and below" ); |
200 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
201 | scales = g_slist_prepend (list: scales, data: scale); |
202 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
203 | gtk_scale_add_mark (GTK_SCALE (scale), value: bath_marks[0], position: GTK_POS_TOP, markup: bath_labels[0]); |
204 | gtk_scale_add_mark (GTK_SCALE (scale), value: bath_marks[1], position: GTK_POS_BOTTOM, markup: bath_labels[1]); |
205 | gtk_scale_add_mark (GTK_SCALE (scale), value: bath_marks[2], position: GTK_POS_BOTTOM, markup: bath_labels[2]); |
206 | gtk_scale_add_mark (GTK_SCALE (scale), value: bath_marks[3], position: GTK_POS_TOP, markup: bath_labels[3]); |
207 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
208 | gtk_box_append (GTK_BOX (box), child: frame); |
209 | |
210 | frame = gtk_frame_new (label: "Positions" ); |
211 | scale = gtk_scale_new_with_range (orientation: GTK_ORIENTATION_HORIZONTAL, min: 0, max: 100, step: 1); |
212 | scales = g_slist_prepend (list: scales, data: scale); |
213 | gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); |
214 | gtk_scale_add_mark (GTK_SCALE (scale), value: pos_marks[0], position: GTK_POS_LEFT, markup: pos_labels[0]); |
215 | gtk_scale_add_mark (GTK_SCALE (scale), value: pos_marks[1], position: GTK_POS_RIGHT, markup: pos_labels[1]); |
216 | gtk_scale_add_mark (GTK_SCALE (scale), value: pos_marks[2], position: GTK_POS_TOP, markup: pos_labels[2]); |
217 | gtk_scale_add_mark (GTK_SCALE (scale), value: pos_marks[3], position: GTK_POS_BOTTOM, markup: pos_labels[3]); |
218 | gtk_frame_set_child (GTK_FRAME (frame), child: scale); |
219 | gtk_box_append (GTK_BOX (box), child: frame); |
220 | |
221 | box2 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 6); |
222 | gtk_box_append (GTK_BOX (box1), child: box2); |
223 | button = gtk_button_new_with_label (label: "Flip" ); |
224 | g_signal_connect (button, "clicked" , G_CALLBACK (flip), NULL); |
225 | gtk_box_append (GTK_BOX (box2), child: button); |
226 | |
227 | button = gtk_button_new_with_label (label: "Invert" ); |
228 | g_signal_connect (button, "clicked" , G_CALLBACK (invert), NULL); |
229 | gtk_box_append (GTK_BOX (box2), child: button); |
230 | |
231 | button = gtk_toggle_button_new_with_label (label: "Trough" ); |
232 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); |
233 | g_signal_connect (button, "toggled" , G_CALLBACK (trough), NULL); |
234 | gtk_box_append (GTK_BOX (box2), child: button); |
235 | gtk_widget_show (widget: window); |
236 | |
237 | button = gtk_toggle_button_new_with_label (label: "Extra" ); |
238 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE); |
239 | g_signal_connect (button, "toggled" , G_CALLBACK (extra), NULL); |
240 | gtk_box_append (GTK_BOX (box2), child: button); |
241 | gtk_widget_show (widget: window); |
242 | |
243 | while (!done) |
244 | g_main_context_iteration (NULL, TRUE); |
245 | |
246 | return 0; |
247 | } |
248 | |
249 | |
250 | |