testscale.c 8.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* testscale.c - scale mark demo
 * Copyright (C) 2009 Red Hat, Inc.
 * Author: Matthias Clasen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 */

#include <gtk/gtk.h>

21 22 23

GSList *scales;
GtkWidget *flipbox;
24
GtkWidget *extra_scale;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

static void
invert (GtkButton *button)
{
  GSList *l;

  for (l = scales; l; l = l->next)
    {
      GtkRange *range = l->data;
      gtk_range_set_inverted (range, !gtk_range_get_inverted (range));
    }
}

static void
flip (GtkButton *button)
{
  GSList *l;

  gtk_orientable_set_orientation (GTK_ORIENTABLE (flipbox), 1 - gtk_orientable_get_orientation (GTK_ORIENTABLE (flipbox)));

  for (l = scales; l; l = l->next)
    {
      GtkOrientable *o = l->data;
      gtk_orientable_set_orientation (o, 1 - gtk_orientable_get_orientation (o));
    }
}

52
static void
53
trough (GtkToggleButton *button)
54
{
55
  GSList *l;
56 57 58
  gboolean value;

  value = gtk_toggle_button_get_active (button);
59 60 61 62 63 64

  for (l = scales; l; l = l->next)
    {
      GtkRange *range = l->data;
      gtk_range_set_range (range, 0., value ? 100.0 : 0.);
    }
65 66
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
gdouble marks[3] = { 0.0, 50.0, 100.0 };
gdouble extra_marks[2] = { 20.0, 40.0 };

static void
extra (GtkToggleButton *button)
{
  gboolean value;

  value = gtk_toggle_button_get_active (button);

  if (value)
    {
      gtk_scale_add_mark (GTK_SCALE (extra_scale), extra_marks[0], GTK_POS_TOP, NULL);
      gtk_scale_add_mark (GTK_SCALE (extra_scale), extra_marks[1], GTK_POS_TOP, NULL);
    }
  else
    {
      gtk_scale_clear_marks (GTK_SCALE (extra_scale));
      gtk_scale_add_mark (GTK_SCALE (extra_scale), marks[0], GTK_POS_BOTTOM, NULL);
      gtk_scale_add_mark (GTK_SCALE (extra_scale), marks[1], GTK_POS_BOTTOM, NULL);
      gtk_scale_add_mark (GTK_SCALE (extra_scale), marks[2], GTK_POS_BOTTOM, NULL);
    }
}

91 92 93 94
int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *box;
95
  GtkWidget *box1;
96
  GtkWidget *box2;
97
  GtkWidget *button;
98 99
  GtkWidget *frame;
  GtkWidget *scale;
100 101 102 103
  const gchar *labels[3] = {
    "<small>Left</small>",
    "<small>Middle</small>",
    "<small>Right</small>"
104 105 106
  };

  gdouble bath_marks[4] = { 0.0, 33.3, 66.6, 100.0 };
107 108 109 110 111
  const gchar *bath_labels[4] = {
    "<span color='blue' size='small'>Cold</span>",
    "<span size='small'>Baby bath</span>",
    "<span size='small'>Hot tub</span>",
    "<span color='Red' size='small'>Hot</span>"
112 113
  };

114 115 116
  gdouble pos_marks[4] = { 0.0, 33.3, 66.6, 100.0 };
  const gchar *pos_labels[4] = { "Left", "Right", "Top", "Bottom" };

117 118 119 120
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Ranges with marks");
121 122 123 124 125 126
  box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  flipbox = box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  gtk_widget_set_hexpand (flipbox, TRUE);
  gtk_widget_set_vexpand (flipbox, TRUE);
  gtk_container_add (GTK_CONTAINER (box1), box);
  gtk_container_add (GTK_CONTAINER (window), box1);
127 128

  frame = gtk_frame_new ("No marks");
129 130
  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
131 132 133 134
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);

135 136 137 138 139 140 141 142 143
  frame = gtk_frame_new ("With fill level");
  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_range_set_show_fill_level (GTK_RANGE (scale), TRUE);
  gtk_range_set_fill_level (GTK_RANGE (scale), 50);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);

144
  frame = gtk_frame_new ("Simple marks");
145
  extra_scale = scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
146
  scales = g_slist_prepend (scales, scale);
147 148 149 150 151 152
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[0], GTK_POS_BOTTOM, NULL);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[1], GTK_POS_BOTTOM, NULL);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[2], GTK_POS_BOTTOM, NULL);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);
153

154 155 156 157 158 159 160 161 162 163
  frame = gtk_frame_new ("Simple marks up");
  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[0], GTK_POS_TOP, NULL);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[1], GTK_POS_TOP, NULL);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[2], GTK_POS_TOP, NULL);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);

164
  frame = gtk_frame_new ("Labeled marks");
165 166 167 168
  box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);

  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
169 170 171 172 173 174
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[0], GTK_POS_BOTTOM, labels[0]);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[1], GTK_POS_BOTTOM, labels[1]);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[2], GTK_POS_BOTTOM, labels[2]);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);
175

176
  frame = gtk_frame_new ("Some labels");
177 178
  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
179
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
180 181 182
  gtk_scale_add_mark (GTK_SCALE (scale), marks[0], GTK_POS_TOP, labels[0]);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[1], GTK_POS_TOP, NULL);
  gtk_scale_add_mark (GTK_SCALE (scale), marks[2], GTK_POS_TOP, labels[2]);
183 184
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);
185

186
  frame = gtk_frame_new ("Above and below");
187 188
  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
189 190 191 192 193 194 195 196
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_scale_add_mark (GTK_SCALE (scale), bath_marks[0], GTK_POS_TOP, bath_labels[0]);
  gtk_scale_add_mark (GTK_SCALE (scale), bath_marks[1], GTK_POS_BOTTOM, bath_labels[1]);
  gtk_scale_add_mark (GTK_SCALE (scale), bath_marks[2], GTK_POS_BOTTOM, bath_labels[2]);
  gtk_scale_add_mark (GTK_SCALE (scale), bath_marks[3], GTK_POS_TOP, bath_labels[3]);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);

197
  frame = gtk_frame_new ("Positions");
198 199
  scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  scales = g_slist_prepend (scales, scale);
200 201 202 203 204 205 206 207
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_scale_add_mark (GTK_SCALE (scale), pos_marks[0], GTK_POS_LEFT, pos_labels[0]);
  gtk_scale_add_mark (GTK_SCALE (scale), pos_marks[1], GTK_POS_RIGHT, pos_labels[1]);
  gtk_scale_add_mark (GTK_SCALE (scale), pos_marks[2], GTK_POS_TOP, pos_labels[2]);
  gtk_scale_add_mark (GTK_SCALE (scale), pos_marks[3], GTK_POS_BOTTOM, pos_labels[3]);
  gtk_container_add (GTK_CONTAINER (frame), scale);
  gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);

208 209 210 211 212
  box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_container_add (GTK_CONTAINER (box1), box2);
  button = gtk_button_new_with_label ("Flip");
  g_signal_connect (button, "clicked", G_CALLBACK (flip), NULL);
  gtk_container_add (GTK_CONTAINER (box2), button);
213

214 215 216
  button = gtk_button_new_with_label ("Invert");
  g_signal_connect (button, "clicked", G_CALLBACK (invert), NULL);
  gtk_container_add (GTK_CONTAINER (box2), button);
217

218 219 220 221
  button = gtk_toggle_button_new_with_label ("Trough");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  g_signal_connect (button, "toggled", G_CALLBACK (trough), NULL);
  gtk_container_add (GTK_CONTAINER (box2), button);
222 223
  gtk_widget_show_all (window);

224 225 226 227 228 229
  button = gtk_toggle_button_new_with_label ("Extra");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
  g_signal_connect (button, "toggled", G_CALLBACK (extra), NULL);
  gtk_container_add (GTK_CONTAINER (box2), button);
  gtk_widget_show_all (window);

230 231 232 233 234 235
  gtk_main ();

  return 0;
}