summaryrefslogtreecommitdiff
path: root/tests/testboxcss.c
blob: 2ae081b577bef885a25951e9aca13480e6f65767 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* testbox.c
 *
 * Copyright (C) 2010 Benjamin Otte <otte@gnome.ogr>
 *
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"
#include <gtk/gtk.h>
#include "prop-editor.h"

/* This is exactly the style information you've been looking for */
#define GTK_STYLE_PROVIDER_PRIORITY_FORCE G_MAXUINT

#define DEFAULT_CSS \
  ".play {\n" \
  "  engine: none;\n" \
  "  background-image: none;\n" \
  "  background-color: red;\n" \
  "  border-color: black;\n" \
  "  border-radius: 0;\n" \
  "}\n" \
  "\n" \
  ".play:nth-child(even) {\n" \
  "  background-color: yellow;\n" \
  "  color: green;\n" \
  "}\n" \
  "\n" \
  ".play:nth-child(first) {\n" \
  "  border-radius: 5 0 0 5;\n" \
  "}\n" \
  "\n" \
  ".play:nth-child(last) {\n" \
  "  border-radius: 0 5 5 0;\n" \
  "}\n" \
  "\n"

static void
show_parsing_error (GtkCssProvider *provider,
                    const gchar    *path,
                    guint           line,
                    guint           position,
                    const GError   *error,
                    GtkTextBuffer  *buffer)
{
  GtkTextIter start, end;
  const char *tag_name;

  gtk_text_buffer_get_iter_at_line (buffer, &start, line - 1);
  if (gtk_text_buffer_get_line_count (buffer) <= line)
    gtk_text_buffer_get_end_iter (buffer, &end);
  else
    gtk_text_buffer_get_iter_at_line (buffer, &end, line);

  if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
    tag_name = "warning";
  else
    tag_name = "error";

  gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
}
                    
static void
css_text_changed (GtkTextBuffer  *buffer,
                  GtkCssProvider *provider)
{
  GtkTextIter start, end;
  char *text;

  gtk_text_buffer_get_start_iter (buffer, &start);
  gtk_text_buffer_get_end_iter (buffer, &end);
  gtk_text_buffer_remove_all_tags (buffer, &start, &end);

  text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
  gtk_css_provider_load_from_data (provider, text, -1, NULL);
  g_free (text);

  gtk_style_context_reset_widgets (gdk_screen_get_default ());
}

static void
remove_widget (GtkWidget *widget)
{
  gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
}

static int count = 0;

static void
add_button (GtkBox *box)
{
  GtkWidget* button;
  char *text;

  text = g_strdup_printf ("Remove %d", ++count);
  button = (GtkWidget *)gtk_button_new_from_stock (text);
  g_free (text);
  gtk_style_context_add_class (gtk_widget_get_style_context (button), "play");
  g_signal_connect_swapped (button,
                            "clicked",
                            G_CALLBACK (remove_widget),
                            button);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);
}

static void
add_toolbutton (GtkToolbar *toolbar)
{
  GtkWidget* button;
  char *text;

  text = g_strdup_printf ("Remove %d", ++count);
  button = (GtkWidget *)gtk_tool_button_new (NULL, text);
  g_free (text);
  gtk_style_context_add_class (gtk_widget_get_style_context (button), "play");
  g_signal_connect_swapped (button,
                            "clicked",
                            G_CALLBACK (remove_widget),
                            button);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (toolbar), button);
}

static void
set_orientation (GtkSwitch *switch_)
{
  gtk_widget_set_default_direction (gtk_switch_get_active (switch_) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL);
}

gint
main (gint argc, gchar **argv)
{
  GtkWidget *window, *main_box, *container, *child;
  GtkWidget *box, *toolbar;
  GtkStyleProvider *provider;
  GtkTextBuffer *css;
  
  gtk_init (&argc, &argv);

  css = gtk_text_buffer_new (NULL);
  gtk_text_buffer_create_tag (css,
                              "warning",
                              "background", "rgba(255,255,0,0.3)",
                              NULL);
  gtk_text_buffer_create_tag (css,
                              "error",
                              "background", "rgba(255,0,0,0.3)",
                              NULL);

  provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                             provider,
                                             GTK_STYLE_PROVIDER_PRIORITY_FORCE);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);

  main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (window), main_box);

  toolbar = gtk_toolbar_new ();
  gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_TEXT);
  gtk_box_pack_start (GTK_BOX (main_box), toolbar, FALSE, TRUE, 0);

  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_box_pack_start (GTK_BOX (main_box), box, FALSE, TRUE, 0);

  container = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (container), 200);
  gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (container), 200);
  gtk_box_pack_start (GTK_BOX (main_box), container, TRUE, TRUE, 0);
  child = gtk_text_view_new_with_buffer (css);
  gtk_container_add (GTK_CONTAINER (container), child);
  g_signal_connect (css,
                    "changed",
                    G_CALLBACK (css_text_changed),
                    provider);
  gtk_text_buffer_set_text (css,
                            DEFAULT_CSS,
                            -1);
  g_signal_connect (provider,
                    "parsing-error",
                    G_CALLBACK (show_parsing_error),
                    gtk_text_view_get_buffer (GTK_TEXT_VIEW (child)));

  container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_box_pack_start (GTK_BOX (main_box), container, FALSE, TRUE, 0);
  child = gtk_switch_new ();
  gtk_switch_set_active (GTK_SWITCH (child), gtk_widget_get_default_direction () == GTK_TEXT_DIR_LTR);
  g_signal_connect (child,
                    "notify::active",
                    G_CALLBACK (set_orientation),
                    NULL);
  gtk_box_pack_start (GTK_BOX (container), child, FALSE, FALSE, 0);
  child = gtk_label_new ("left-to-right");
  gtk_box_pack_start (GTK_BOX (container), child, FALSE, FALSE, 0);
  child = gtk_button_new_with_label ("Add button");
  g_signal_connect_swapped (child,
                            "clicked",
                            G_CALLBACK (add_button),
                            box);
  gtk_box_pack_end (GTK_BOX (container), child, FALSE, FALSE, 0);
  child = gtk_button_new_with_label ("Add toolbutton");
  g_signal_connect_swapped (child,
                            "clicked",
                            G_CALLBACK (add_toolbutton),
                            toolbar);
  gtk_box_pack_end (GTK_BOX (container), child, FALSE, FALSE, 0);

  add_toolbutton (GTK_TOOLBAR (toolbar));
  add_toolbutton (GTK_TOOLBAR (toolbar));
  add_toolbutton (GTK_TOOLBAR (toolbar));
  add_toolbutton (GTK_TOOLBAR (toolbar));

  add_button (GTK_BOX (box));
  add_button (GTK_BOX (box));
  add_button (GTK_BOX (box));
  add_button (GTK_BOX (box));

  gtk_widget_show_all (window);

  gtk_main ();
  
  return 0;
}