summaryrefslogtreecommitdiff
path: root/tests/foreigndrawing.c
blob: 03b1a6aa9bf240b09ce9268e834f4ab781ac499d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
52
53
54
55
56
57
58
59
60
61
62
63
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* foreign-drawing.c
 * Copyright (C) 2015 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 Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>

typedef struct {
  GType type;
  const gchar *name;
  const gchar *class1;
  const gchar *class2;
} PathElt;

static GtkStyleContext *
get_style (PathElt pelt[], gint n_elts)
{
  GtkWidgetPath *path;
  gint i;
  GtkStyleContext *context;

  path = gtk_widget_path_new ();

  for (i = 0; i < n_elts; i++)
    {
      gtk_widget_path_append_type (path, pelt[i].type);
      if (pelt[i].name)
        gtk_widget_path_iter_set_object_name (path, i, pelt[i].name);
      if (pelt[i].class1)
        gtk_widget_path_iter_add_class (path, i, pelt[i].class1);
      if (pelt[i].class2)
        gtk_widget_path_iter_add_class (path, i, pelt[i].class2);
    }

  context = gtk_style_context_new ();
  gtk_style_context_set_path (context, path);
  gtk_widget_path_unref (path);

  return context;
}

static void
draw_horizontal_scrollbar (GtkWidget     *widget,
                           cairo_t       *cr,
                           gint           x,
                           gint           y,
                           gint           width,
                           gint           height,
                           gint           position,
                           GtkStateFlags  state)
{
  GtkStyleContext *scrollbar_context;
  GtkStyleContext *trough_context;
  GtkStyleContext *slider_context;

  PathElt scrollbar_path[1] = {
    { GTK_TYPE_SCROLLBAR, "scrollbar", "horizontal", NULL },
  };

  PathElt trough_path[2] = {
    { GTK_TYPE_SCROLLBAR, "scrollbar", "horizontal", NULL },
    { G_TYPE_NONE, "trough", NULL, NULL }
  };

  PathElt slider_path[3] = {
    { GTK_TYPE_SCROLLBAR, "scrollbar", "horizontal", NULL },
    { G_TYPE_NONE, "trough", NULL, NULL },
    { G_TYPE_NONE, "slider", NULL, NULL }
  };

  scrollbar_context = get_style (scrollbar_path, G_N_ELEMENTS (scrollbar_path));
  trough_context = get_style (trough_path, G_N_ELEMENTS (trough_path));
  slider_context = get_style (slider_path, G_N_ELEMENTS (slider_path));
  gtk_style_context_set_parent (slider_context, trough_context);
  gtk_style_context_set_parent (trough_context, scrollbar_context);

  gtk_style_context_set_state (scrollbar_context, state);
  gtk_style_context_set_state (trough_context, state);
  gtk_style_context_set_state (slider_context, state);

  gtk_render_background (trough_context, cr, x, y, width, height);
  gtk_render_frame (trough_context, cr, x, y, width, height);
  gtk_render_slider (slider_context, cr, x + position, y + 1, 30, height - 2, GTK_ORIENTATION_HORIZONTAL);

  g_object_unref (slider_context);
  g_object_unref (trough_context);
  g_object_unref (scrollbar_context);
}

static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr)
{
  gint width;

  width = gtk_widget_get_allocated_width (widget);

  draw_horizontal_scrollbar (widget, cr, 10, 10, width - 20, 10, 30, GTK_STATE_FLAG_NORMAL);
  draw_horizontal_scrollbar (widget, cr, 10, 30, width - 20, 10, 40, GTK_STATE_FLAG_PRELIGHT);
  draw_horizontal_scrollbar (widget, cr, 10, 50, width - 20, 10, 50, GTK_STATE_FLAG_ACTIVE|GTK_STATE_FLAG_PRELIGHT);

  return FALSE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *da;

  gtk_init (NULL, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_app_paintable (window, TRUE);
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
  gtk_container_add (GTK_CONTAINER (window), box);
  da = gtk_drawing_area_new ();
  gtk_widget_set_size_request (da, 200, 200);
  gtk_widget_set_app_paintable (da, TRUE);
  gtk_container_add (GTK_CONTAINER (box), da);

  g_signal_connect (da, "draw", G_CALLBACK (draw_cb), NULL);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}