testboxcss.c 7.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
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 21 22 23 24 25 26 27 28 29 30 31 32 33
 */
#include "config.h"
#include <gtk/gtk.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" \
34 35
  "  background-color: yellow;\n" \
  "  color: green;\n" \
36 37 38 39 40 41 42 43 44 45 46 47 48
  "}\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,
49
                    GtkCssSection  *section,
50 51 52 53 54 55
                    const GError   *error,
                    GtkTextBuffer  *buffer)
{
  GtkTextIter start, end;
  const char *tag_name;

56 57 58 59 60 61 62 63
  gtk_text_buffer_get_iter_at_line_index (buffer,
                                          &start,
                                          gtk_css_section_get_start_line (section),
                                          gtk_css_section_get_start_position (section));
  gtk_text_buffer_get_iter_at_line_index (buffer,
                                          &end,
                                          gtk_css_section_get_end_line (section),
                                          gtk_css_section_get_end_position (section));
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

  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);
}

97 98
static int count = 0;

99
static void
100
add_button (GtkBox *box)
101 102 103 104 105
{
  GtkWidget* button;
  char *text;

  text = g_strdup_printf ("Remove %d", ++count);
106
  button = (GtkWidget *)gtk_button_new_with_label (text);
107 108 109 110 111 112 113 114
  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);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
}

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);
133 134 135 136 137 138 139 140 141 142 143
}

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)
{
144 145
  GtkWidget *window, *main_box, *container, *child;
  GtkWidget *box, *toolbar;
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
  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);

174 175 176 177 178
  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);
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
  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);
210
  child = gtk_button_new_with_label ("Add button");
211 212
  g_signal_connect_swapped (child,
                            "clicked",
213
                            G_CALLBACK (add_button),
214 215
                            box);
  gtk_box_pack_end (GTK_BOX (container), child, FALSE, FALSE, 0);
216 217 218 219 220 221 222 223 224 225 226
  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));
227

228 229 230 231
  add_button (GTK_BOX (box));
  add_button (GTK_BOX (box));
  add_button (GTK_BOX (box));
  add_button (GTK_BOX (box));
232 233 234 235 236 237 238

  gtk_widget_show_all (window);

  gtk_main ();
  
  return 0;
}