testlevelbar.c 2.92 KB
Newer Older
1 2 3 4 5
#include <gtk/gtk.h>

static GtkWidget *
create_level_bar (void)
{
Matthias Clasen's avatar
Matthias Clasen committed
6 7 8 9 10
  GtkWidget *bar;

  bar = gtk_level_bar_new ();
  gtk_level_bar_set_min_value (GTK_LEVEL_BAR (bar), 0.0);
  gtk_level_bar_set_max_value (GTK_LEVEL_BAR (bar), 10.0);
11

Matthias Clasen's avatar
Matthias Clasen committed
12 13
  gtk_level_bar_add_offset_value (GTK_LEVEL_BAR (bar),
                                  GTK_LEVEL_BAR_OFFSET_LOW, 1.0);
14

Matthias Clasen's avatar
Matthias Clasen committed
15 16
  gtk_level_bar_add_offset_value (GTK_LEVEL_BAR (bar),
                                  GTK_LEVEL_BAR_OFFSET_HIGH, 9.0);
17

Matthias Clasen's avatar
Matthias Clasen committed
18 19 20
  gtk_level_bar_add_offset_value (GTK_LEVEL_BAR (bar),
                                  "full", 10.0);

Matthias Clasen's avatar
Matthias Clasen committed
21 22
  gtk_level_bar_add_offset_value (GTK_LEVEL_BAR (bar),
                                  "my-offset", 5.0);
23

Matthias Clasen's avatar
Matthias Clasen committed
24
  return bar;
25 26 27 28 29 30 31
}

static void
add_custom_css (void)
{
  GtkCssProvider *provider;
  const gchar data[] =
Matthias Clasen's avatar
Matthias Clasen committed
32
  "levelbar block.my-offset {"
Matthias Clasen's avatar
Matthias Clasen committed
33
  "   background: magenta;"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  "}";

  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider, data, -1, NULL);
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                             GTK_STYLE_PROVIDER (provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}

static gboolean
increase_level (gpointer data)
{
  GtkLevelBar *bar = data;
  gdouble value;

  value = gtk_level_bar_get_value (bar);
Matthias Clasen's avatar
Matthias Clasen committed
50 51 52
  value += 0.1;
  if (value >= gtk_level_bar_get_max_value (bar))
    value = gtk_level_bar_get_min_value (bar);
53 54 55 56 57
  gtk_level_bar_set_value (bar, value);

  return G_SOURCE_CONTINUE;
}

58 59 60 61 62 63 64 65 66
static gboolean
window_delete_event (GtkWidget *widget,
                     GdkEvent *event,
                     gpointer _data)
{
  gtk_main_quit ();
  return FALSE;
}

Matthias Clasen's avatar
Matthias Clasen committed
67 68 69 70 71 72 73 74 75
static void
toggle (GtkSwitch *sw, GParamSpec *pspec, GtkLevelBar *bar)
{
  if (gtk_switch_get_active (sw))
    gtk_level_bar_set_mode (bar, GTK_LEVEL_BAR_MODE_DISCRETE);
  else
    gtk_level_bar_set_mode (bar, GTK_LEVEL_BAR_MODE_CONTINUOUS);
}

76 77 78 79
int
main (int argc, char *argv[])
{
  GtkWidget *window;
Matthias Clasen's avatar
Matthias Clasen committed
80
  GtkWidget *box;
81
  GtkWidget *bar;
Matthias Clasen's avatar
Matthias Clasen committed
82 83
  GtkWidget *box2;
  GtkWidget *sw;
84 85 86 87 88 89 90

  gtk_init (&argc, &argv);

  add_custom_css ();

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 500, 100);
Matthias Clasen's avatar
Matthias Clasen committed
91 92
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
  g_object_set (box, "margin", 20, NULL);
93
  bar = create_level_bar ();
Matthias Clasen's avatar
Matthias Clasen committed
94 95 96 97 98 99 100 101 102
  gtk_container_add (GTK_CONTAINER (window), box);
  gtk_container_add (GTK_CONTAINER (box), bar);
  box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
  gtk_container_add (GTK_CONTAINER (box), box2);
  gtk_container_add (GTK_CONTAINER (box2), gtk_label_new ("Discrete"));
  sw = gtk_switch_new ();
  gtk_container_add (GTK_CONTAINER (box2), sw);
  g_signal_connect (sw, "notify::active", G_CALLBACK (toggle), bar);

103 104
  gtk_widget_show_all (window);

105 106 107
  g_signal_connect (window, "delete-event",
                    G_CALLBACK (window_delete_event), NULL);

108 109 110 111 112 113
  g_timeout_add (100, increase_level, bar);
  gtk_main ();

  return 0;
}