styleexamples.c 11.1 KB
Newer Older
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
#include <string.h>
#include <gtk/gtk.h>

static gboolean
draw_cb_checks (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

  gtk_style_context_add_class (context, "check");
  gtk_style_context_set_state (context, 0);
  gtk_render_check (context, cr, 12, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
  gtk_render_check (context, cr, 36, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_INCONSISTENT);
  gtk_render_check (context, cr, 60, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
  gtk_render_check (context, cr, 84, 12, 12, 12);

  gtk_style_context_restore (context);

  return TRUE;
}


static gboolean
draw_cb_options (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

  gtk_style_context_add_class (context, "radio");
  gtk_style_context_set_state (context, 0);
  gtk_render_option (context, cr, 12, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
  gtk_render_option (context, cr, 36, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_INCONSISTENT);
  gtk_render_option (context, cr, 60, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
  gtk_render_option (context, cr, 84, 12, 12, 12);

  gtk_style_context_restore (context);

  return TRUE;
}

static gboolean
draw_cb_arrows (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

  gtk_style_context_set_state (context, 0);
  gtk_render_arrow (context, cr, 0,        12, 12, 12);
  gtk_render_arrow (context, cr, G_PI/2,   36, 12, 12);
  gtk_render_arrow (context, cr, G_PI,     60, 12, 12);
  gtk_render_arrow (context, cr, G_PI*3/2, 84, 12, 12);

  gtk_style_context_restore (context);

  return TRUE;
}

static gboolean
draw_cb_expanders (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

  gtk_style_context_add_class (context, "expander");
  gtk_style_context_set_state (context, 0);
  gtk_render_expander (context, cr, 12, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_PRELIGHT);
  gtk_render_expander (context, cr, 36, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
  gtk_render_expander (context, cr, 60, 12, 12, 12);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
  gtk_render_expander (context, cr, 84, 12, 12, 12);

  gtk_style_context_restore (context);

  return TRUE;
}

97 98 99 100 101 102 103 104 105
static gboolean
draw_cb_background (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

106
  gtk_style_context_add_class (context, "background");
107 108
  gtk_style_context_set_junction_sides (context, 0);
  gtk_render_background (context, cr, 12, 12, 100, 100);
109 110
  gtk_style_context_remove_class (context, "background");

111 112 113 114 115 116 117 118 119 120 121 122 123 124
  gtk_style_context_restore (context);

  return TRUE;
}

static gboolean
draw_cb_frame (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

125
  gtk_style_context_add_class (context, "frame1");
126
  gtk_style_context_set_junction_sides (context, 0);
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  gtk_render_frame (context, cr, 12, 12, 50, 50);
  gtk_style_context_remove_class (context, "frame1");

  gtk_style_context_add_class (context, "frame2");
  gtk_render_frame (context, cr, 74, 12, 50, 50);
  gtk_style_context_remove_class (context, "frame2");

  gtk_style_context_add_class (context, "frame3");
  gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
  gtk_render_frame (context, cr, 12, 74, 56, 50);
  gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
  gtk_render_frame (context, cr, 68, 74, 56, 50);
  gtk_style_context_remove_class (context, "frame3");

141 142 143 144 145
  gtk_style_context_restore (context);

  return TRUE;
}

146 147 148 149 150
/* FIXME: this doesn't work */
static gboolean
draw_cb_activity (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;
151
  GtkWidgetPath *path;
152 153 154 155

  context = gtk_widget_get_style_context (widget);
  gtk_style_context_save (context);

156 157 158 159 160 161 162
  path = gtk_widget_path_new ();
  gtk_widget_path_append_type (path, GTK_TYPE_SPINNER);
  gtk_widget_path_iter_add_class (path, 0, "spinner");
  gtk_style_context_set_path (context, path);
  gtk_widget_path_free (path);

  gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
163 164 165 166 167 168 169
  gtk_render_activity (context, cr, 12, 12, 12, 12);

  gtk_style_context_restore (context);

  return TRUE;
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static gboolean
draw_cb_slider (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;
  GtkWidgetPath *path;

  context = gtk_widget_get_style_context (widget);
  gtk_style_context_save (context);

  path = gtk_widget_path_new ();
  gtk_widget_path_append_type (path, GTK_TYPE_SCALE);
  gtk_widget_path_iter_add_class (path, 0, "slider");
  gtk_widget_path_iter_add_class (path, 0, "scale");
  gtk_style_context_set_path (context, path);
  gtk_widget_path_free (path);

  gtk_render_slider (context, cr, 12, 22, 30, 10, GTK_ORIENTATION_HORIZONTAL);
  gtk_render_slider (context, cr, 54, 12, 10, 30, GTK_ORIENTATION_VERTICAL);

  gtk_style_context_restore (context);

  return TRUE;
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static gboolean
draw_cb_focus (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

  gtk_render_focus (context, cr, 12, 12, 50, 50);

  gtk_style_context_restore (context);

  return TRUE;
}

static gboolean
draw_cb_extension (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);
218

219
  gtk_style_context_add_class (context, "notebook");
220
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
221
  gtk_style_context_add_region (context, GTK_STYLE_REGION_TAB, 0);
222
G_GNUC_END_IGNORE_DEPRECATIONS
223 224 225 226 227 228 229 230 231 232 233

  gtk_style_context_set_state (context, 0);
  gtk_render_extension (context, cr, 26, 12, 24, 12, GTK_POS_BOTTOM);
  gtk_render_extension (context, cr, 12, 26, 12, 24, GTK_POS_RIGHT);
  gtk_render_extension (context, cr, 26, 52, 24, 12, GTK_POS_TOP);
  gtk_render_extension (context, cr, 52, 26, 12, 24, GTK_POS_LEFT);

  gtk_style_context_restore (context);

  return TRUE;
}
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static gboolean
draw_cb_frame_gap (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);

  gtk_style_context_add_class (context, "frame");
  gtk_style_context_set_junction_sides (context, 0);
  gtk_render_frame_gap (context, cr, 12, 12, 50, 50, GTK_POS_TOP, 15, 35);
  gtk_style_context_remove_class (context, "frame");

  gtk_style_context_restore (context);

  return TRUE;
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static gboolean
draw_cb_handles (GtkWidget *widget, cairo_t *cr)
{
  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);
  gtk_style_context_save (context);

  gtk_style_context_add_class (context, "paned");
  gtk_render_handle (context, cr, 12, 22, 20, 10);
  gtk_render_handle (context, cr, 44, 12, 10, 20);
  gtk_style_context_remove_class (context, "paned");

  gtk_style_context_add_class (context, "grip");
  gtk_style_context_set_junction_sides (context, GTK_JUNCTION_CORNER_BOTTOMLEFT);
  gtk_render_handle (context, cr, 12, 48, 12, 12);

  gtk_style_context_set_junction_sides (context, GTK_JUNCTION_CORNER_BOTTOMRIGHT);
  gtk_render_handle (context, cr, 40, 48, 12, 12);

  gtk_style_context_restore (context);

  return TRUE;
}

279 280 281 282 283 284 285 286 287 288 289 290
static char *what;

static gboolean
draw_cb (GtkWidget *widget, cairo_t *cr)
{
  if (strcmp (what, "check") == 0)
    return draw_cb_checks (widget, cr);
  else if (strcmp (what, "option") == 0)
    return draw_cb_options (widget, cr);
  else if (strcmp (what, "arrow") == 0)
    return draw_cb_arrows (widget, cr);
  else if (strcmp (what, "expander") == 0)
291 292 293 294 295
    return draw_cb_expanders (widget, cr);
  else if (strcmp (what, "background") == 0)
    return draw_cb_background (widget, cr);
  else if (strcmp (what, "frame") == 0)
    return draw_cb_frame (widget, cr);
296 297
  else if (strcmp (what, "activity") == 0)
    return draw_cb_activity (widget, cr);
298 299
  else if (strcmp (what, "slider") == 0)
    return draw_cb_slider (widget, cr);
300 301 302 303
  else if (strcmp (what, "focus") == 0)
    return draw_cb_focus (widget, cr);
  else if (strcmp (what, "extension") == 0)
    return draw_cb_extension (widget, cr);
304 305
  else if (strcmp (what, "frame-gap") == 0)
    return draw_cb_frame_gap (widget, cr);
306 307
  else if (strcmp (what, "handle") == 0)
    return draw_cb_handles (widget, cr);
308 309 310 311 312 313 314 315

  return FALSE;
}

int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *ebox;
316 317
  GtkStyleContext *context;
  GtkStyleProvider *provider;
318 319 320 321 322 323 324 325 326 327 328 329 330

  gtk_init (&argc, &argv);

  if (argc > 1)
    what = argv[1];
  else
    what = "check";

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  ebox = gtk_event_box_new ();
  gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), TRUE);
  gtk_container_add (GTK_CONTAINER (window), ebox);
  gtk_widget_set_name (ebox, "ebox");
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

  context = gtk_widget_get_style_context (ebox);
  provider = (GtkStyleProvider *)gtk_css_provider_new ();
  gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider),
                                   ".frame1 {\n"
                                   "   border-image: url('gradient1.png') 10 10 10 10 stretch;\n"
                                   "}\n"
                                   ".frame2 {\n"
                                   "   border-style: solid;\n"
                                   "   border-color: rgb(255,0,0);\n"
                                   "   border-width: 10;\n"
                                   "   border-radius: 10;\n"
                                   "}\n"
                                   ".frame3 {\n"
                                   "   border-style: solid;\n"
                                   "   border-color: rgb(0,0,0);\n"
                                   "   border-width: 2;\n"
                                   "   border-radius: 10;\n"
                                   "}\n"
                                   ".background {\n"
                                   "   border-radius: 10;\n"
                                   "   border-width: 0;\n"
                                   "   background-image: -gtk-gradient (linear, left top, right bottom, from(#ff00ff), to(#aabbcc));\n"
                                   "}\n"
                                   ".frame {\n"
                                   "   border-style: solid;\n"
                                   "   border-width: 1;\n"
                                   "   border-radius: 0;\n"
                                   "}\n", -1, NULL);
  gtk_style_context_add_provider (context, provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

362 363 364 365 366 367
  g_signal_connect_after (ebox, "draw", G_CALLBACK (draw_cb), NULL);

  gtk_widget_show_all (window);

  gtk_main ();

368 369
  gtk_style_context_remove_provider (context, provider);

370 371 372
  return 0;
}