gtkthemingengine.c 91.1 KB
Newer Older
Carlos Garnacho's avatar
Carlos Garnacho committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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"

22
#include <math.h>
Carlos Garnacho's avatar
Carlos Garnacho committed
23 24 25 26 27 28
#include <gtk/gtk.h>

#include <gtk/gtkthemingengine.h>
#include <gtk/gtkstylecontext.h>
#include <gtk/gtkintl.h>

29
#include "gtkprivate.h"
30
#include "gtkborderimageprivate.h"
31
#include "gtkpango.h"
32
#include "gtkshadowprivate.h"
33
#include "gtkcsstypesprivate.h"
34
#include "gtkthemingengineprivate.h"
35
#include "gtkroundedboxprivate.h"
36

Carlos Garnacho's avatar
Carlos Garnacho committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/**
 * SECTION:gtkthemingengine
 * @Short_description: Theming renderers
 * @Title: GtkThemingEngine
 * @See_also: #GtkStyleContext
 *
 * #GtkThemingEngine is the object used for rendering themed content
 * in GTK+ widgets. Even though GTK+ has a default implementation,
 * it can be overridden in CSS files by enforcing a #GtkThemingEngine
 * object to be loaded as a module.
 *
 * In order to implement a theming engine, a #GtkThemingEngine subclass
 * must be created, alongside the CSS file that will reference it, the
 * theming engine would be created as an .so library, and installed in
 * $(gtk-modules-dir)/theming-engines/.
52 53 54 55
 *
 * #GtkThemingEngine<!-- -->s have limited access to the object they are
 * rendering, the #GtkThemingEngine API has read-only accessors to the
 * style information contained in the rendered object's #GtkStyleContext.
Carlos Garnacho's avatar
Carlos Garnacho committed
56 57
 */

58 59 60 61
enum {
  SIDE_LEFT   = 1,
  SIDE_BOTTOM = 1 << 1,
  SIDE_RIGHT  = 1 << 2,
62 63
  SIDE_TOP    = 1 << 3,
  SIDE_ALL    = 0xF
64 65
};

66 67 68 69 70
enum {
  PROP_0,
  PROP_NAME
};

Carlos Garnacho's avatar
Carlos Garnacho committed
71 72 73
struct GtkThemingEnginePrivate
{
  GtkStyleContext *context;
74
  gchar *name;
Carlos Garnacho's avatar
Carlos Garnacho committed
75 76 77 78
};

#define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))

79 80 81 82 83 84 85 86 87 88
static void gtk_theming_engine_finalize          (GObject      *object);
static void gtk_theming_engine_impl_set_property (GObject      *object,
                                                  guint         prop_id,
                                                  const GValue *value,
                                                  GParamSpec   *pspec);
static void gtk_theming_engine_impl_get_property (GObject      *object,
                                                  guint         prop_id,
                                                  GValue       *value,
                                                  GParamSpec   *pspec);

89 90 91 92 93 94
static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
                                             cairo_t          *cr,
                                             gdouble           x,
                                             gdouble           y,
                                             gdouble           width,
                                             gdouble           height);
95 96 97 98 99 100
static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
                                              cairo_t          *cr,
                                              gdouble           x,
                                              gdouble           y,
                                              gdouble           width,
                                              gdouble           height);
101 102 103 104 105 106
static void gtk_theming_engine_render_arrow  (GtkThemingEngine *engine,
                                              cairo_t          *cr,
                                              gdouble           angle,
                                              gdouble           x,
                                              gdouble           y,
                                              gdouble           size);
107 108 109 110 111 112
static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
                                                  cairo_t          *cr,
                                                  gdouble           x,
                                                  gdouble           y,
                                                  gdouble           width,
                                                  gdouble           height);
113 114 115 116 117 118
static void gtk_theming_engine_render_frame  (GtkThemingEngine *engine,
                                              cairo_t          *cr,
                                              gdouble           x,
                                              gdouble           y,
                                              gdouble           width,
                                              gdouble           height);
119 120 121 122 123 124
static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
                                                cairo_t          *cr,
                                                gdouble           x,
                                                gdouble           y,
                                                gdouble           width,
                                                gdouble           height);
125 126 127 128 129 130
static void gtk_theming_engine_render_focus    (GtkThemingEngine *engine,
                                                cairo_t          *cr,
                                                gdouble           x,
                                                gdouble           y,
                                                gdouble           width,
                                                gdouble           height);
131 132 133 134 135
static void gtk_theming_engine_render_layout   (GtkThemingEngine *engine,
                                                cairo_t          *cr,
                                                gdouble           x,
                                                gdouble           y,
                                                PangoLayout      *layout);
136 137 138 139 140 141
static void gtk_theming_engine_render_line     (GtkThemingEngine *engine,
                                                cairo_t          *cr,
                                                gdouble           x0,
                                                gdouble           y0,
                                                gdouble           x1,
                                                gdouble           y1);
142 143 144 145 146 147 148
static void gtk_theming_engine_render_slider   (GtkThemingEngine *engine,
                                                cairo_t          *cr,
                                                gdouble           x,
                                                gdouble           y,
                                                gdouble           width,
                                                gdouble           height,
                                                GtkOrientation    orientation);
149 150 151 152 153 154 155 156 157
static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
                                                 cairo_t          *cr,
                                                 gdouble           x,
                                                 gdouble           y,
                                                 gdouble           width,
                                                 gdouble           height,
                                                 GtkPositionType   gap_side,
                                                 gdouble           xy0_gap,
                                                 gdouble           xy1_gap);
158 159 160 161 162 163 164
static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
                                                 cairo_t          *cr,
                                                 gdouble           x,
                                                 gdouble           y,
                                                 gdouble           width,
                                                 gdouble           height,
                                                 GtkPositionType   gap_side);
165 166 167 168 169
static void gtk_theming_engine_render_handle    (GtkThemingEngine *engine,
                                                 cairo_t          *cr,
                                                 gdouble           x,
                                                 gdouble           y,
                                                 gdouble           width,
170
                                                 gdouble           height);
Carlos Garnacho's avatar
Carlos Garnacho committed
171 172 173 174 175 176
static void gtk_theming_engine_render_activity  (GtkThemingEngine *engine,
                                                 cairo_t          *cr,
                                                 gdouble           x,
                                                 gdouble           y,
                                                 gdouble           width,
                                                 gdouble           height);
177 178 179
static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine    *engine,
                                                          const GtkIconSource *source,
                                                          GtkIconSize          size);
180 181 182 183 184
static void gtk_theming_engine_render_icon (GtkThemingEngine *engine,
                                            cairo_t *cr,
					    GdkPixbuf *pixbuf,
                                            gdouble x,
                                            gdouble y);
185

Carlos Garnacho's avatar
Carlos Garnacho committed
186 187
G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)

188 189 190 191 192 193 194

typedef struct GtkThemingModule GtkThemingModule;
typedef struct GtkThemingModuleClass GtkThemingModuleClass;

struct GtkThemingModule
{
  GTypeModule parent_instance;
195
  GModule *module;
196 197
  gchar *name;

198 199
  void (*init) (GTypeModule *module);
  void (*exit) (void);
200 201 202 203 204 205 206 207 208 209 210 211 212 213
  GtkThemingEngine * (*create_engine) (void);
};

struct GtkThemingModuleClass
{
  GTypeModuleClass parent_class;
};

#define GTK_TYPE_THEMING_MODULE  (gtk_theming_module_get_type ())
#define GTK_THEMING_MODULE(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
#define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))

G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);

Carlos Garnacho's avatar
Carlos Garnacho committed
214 215 216 217 218
static void
gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

219 220 221 222
  object_class->finalize = gtk_theming_engine_finalize;
  object_class->set_property = gtk_theming_engine_impl_set_property;
  object_class->get_property = gtk_theming_engine_impl_get_property;

223
  klass->render_icon = gtk_theming_engine_render_icon;
224
  klass->render_check = gtk_theming_engine_render_check;
225
  klass->render_option = gtk_theming_engine_render_option;
226
  klass->render_arrow = gtk_theming_engine_render_arrow;
227
  klass->render_background = gtk_theming_engine_render_background;
228
  klass->render_frame = gtk_theming_engine_render_frame;
229
  klass->render_expander = gtk_theming_engine_render_expander;
230
  klass->render_focus = gtk_theming_engine_render_focus;
231
  klass->render_layout = gtk_theming_engine_render_layout;
232
  klass->render_line = gtk_theming_engine_render_line;
233
  klass->render_slider = gtk_theming_engine_render_slider;
234
  klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
235
  klass->render_extension = gtk_theming_engine_render_extension;
236
  klass->render_handle = gtk_theming_engine_render_handle;
Carlos Garnacho's avatar
Carlos Garnacho committed
237
  klass->render_activity = gtk_theming_engine_render_activity;
238
  klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  /**
   * GtkThemingEngine:name:
   *
   * The theming engine name, this name will be used when registering
   * custom properties, for a theming engine named "Clearlooks" registering
   * a "glossy" custom property, it could be referenced in the CSS file as
   *
   * <programlisting>
   * -Clearlooks-glossy: true;
   * </programlisting>
   *
   * Since: 3.0
   */
  g_object_class_install_property (object_class,
				   PROP_NAME,
				   g_param_spec_string ("name",
                                                        P_("Name"),
                                                        P_("Theming engine name"),
                                                        NULL,
                                                        G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));

Carlos Garnacho's avatar
Carlos Garnacho committed
261 262 263 264 265 266 267 268 269
  g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
}

static void
gtk_theming_engine_init (GtkThemingEngine *engine)
{
  engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
static void
gtk_theming_engine_finalize (GObject *object)
{
  GtkThemingEnginePrivate *priv;

  priv = GTK_THEMING_ENGINE (object)->priv;
  g_free (priv->name);

  G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
}

static void
gtk_theming_engine_impl_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  GtkThemingEnginePrivate *priv;

  priv = GTK_THEMING_ENGINE (object)->priv;

  switch (prop_id)
    {
    case PROP_NAME:
      if (priv->name)
        g_free (priv->name);

      priv->name = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_theming_engine_impl_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  GtkThemingEnginePrivate *priv;

  priv = GTK_THEMING_ENGINE (object)->priv;

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Carlos Garnacho's avatar
Carlos Garnacho committed
326 327 328 329 330 331 332 333 334 335 336 337 338
void
_gtk_theming_engine_set_context (GtkThemingEngine *engine,
                                 GtkStyleContext  *context)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));

  priv = engine->priv;
  priv->context = context;
}

Carlos Garnacho's avatar
Carlos Garnacho committed
339
/**
340
 * gtk_theming_engine_register_property: (skip)
341
 * @name_space: namespace for the property name
Carlos Garnacho's avatar
Carlos Garnacho committed
342
 * @parse_func: parsing function to use, or %NULL
343
 * @pspec: the #GParamSpec for the new property
Carlos Garnacho's avatar
Carlos Garnacho committed
344 345 346
 *
 * Registers a property so it can be used in the CSS file format,
 * on the CSS file the property will look like
347 348
 * "-${@name_space}-${property_name}". being
 * ${property_name} the given to @pspec. @name_space will usually
349
 * be the theme engine name.
Carlos Garnacho's avatar
Carlos Garnacho committed
350 351 352 353 354 355 356 357
 *
 * For any type a @parse_func may be provided, being this function
 * used for turning any property value (between ':' and ';') in
 * CSS to the #GValue needed. For basic types there is already
 * builtin parsing support, so %NULL may be provided for these
 * cases.
 *
 * <note>
358 359 360 361
 * Engines must ensure property registration happens exactly once,
 * usually GTK+ deals with theming engines as singletons, so this
 * should be guaranteed to happen once, but bear this in mind
 * when creating #GtkThemeEngine<!-- -->s yourself.
Carlos Garnacho's avatar
Carlos Garnacho committed
362 363 364 365
 * </note>
 *
 * <note>
 * In order to make use of the custom registered properties in
366 367 368
 * the CSS file, make sure the engine is loaded first by specifying
 * the engine property, either in a previous rule or within the same
 * one.
Carlos Garnacho's avatar
Carlos Garnacho committed
369 370 371
 * <programlisting>
 * &ast; {
 *     engine: someengine;
372
 *     -SomeEngine-custom-property: 2;
Carlos Garnacho's avatar
Carlos Garnacho committed
373 374 375 376 377 378
 * }
 * </programlisting>
 * </note>
 *
 * Since: 3.0
 **/
379
void
380
gtk_theming_engine_register_property (const gchar            *name_space,
381 382
                                      GtkStylePropertyParser  parse_func,
                                      GParamSpec             *pspec)
383
{
384
  gchar *name;
385

386 387
  g_return_if_fail (name_space != NULL);
  g_return_if_fail (strchr (name_space, ' ') == NULL);
388
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
389

390
  /* FIXME: hack hack hack, replacing pspec->name to include namespace */
391
  name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
392 393
  pspec->name = (char *)g_intern_string (name);
  g_free (name);
394

395
  gtk_style_properties_register_property (parse_func, pspec);
396 397
}

Carlos Garnacho's avatar
Carlos Garnacho committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411
/**
 * gtk_theming_engine_get_property:
 * @engine: a #GtkThemingEngine
 * @property: the property name
 * @state: state to retrieve the value for
 * @value: (out) (transfer full): return location for the property value,
 *         you must free this memory using g_value_unset() once you are
 *         done with it.
 *
 * Gets a property value as retrieved from the style settings that apply
 * to the currently rendered element.
 *
 * Since: 3.0
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
412 413 414
void
gtk_theming_engine_get_property (GtkThemingEngine *engine,
                                 const gchar      *property,
415
                                 GtkStateFlags     state,
Carlos Garnacho's avatar
Carlos Garnacho committed
416 417 418 419 420 421 422 423 424 425 426 427
                                 GValue           *value)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
  g_return_if_fail (property != NULL);
  g_return_if_fail (value != NULL);

  priv = engine->priv;
  gtk_style_context_get_property (priv->context, property, state, value);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
428 429 430 431 432 433 434 435 436 437 438
/**
 * gtk_theming_engine_get_valist:
 * @engine: a #GtkThemingEngine
 * @state: state to retrieve values for
 * @args: va_list of property name/return location pairs, followed by %NULL
 *
 * Retrieves several style property values that apply to the currently
 * rendered element.
 *
 * Since: 3.0
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
439 440
void
gtk_theming_engine_get_valist (GtkThemingEngine *engine,
441
                               GtkStateFlags     state,
Carlos Garnacho's avatar
Carlos Garnacho committed
442 443 444 445 446 447 448 449 450 451
                               va_list           args)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_valist (priv->context, state, args);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
452 453 454 455 456 457 458 459 460 461 462
/**
 * gtk_theming_engine_get:
 * @engine: a #GtkThemingEngine
 * @state: state to retrieve values for
 * @...: property name /return value pairs, followed by %NULL
 *
 * Retrieves several style property values that apply to the currently
 * rendered element.
 *
 * Since: 3.0
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
463 464
void
gtk_theming_engine_get (GtkThemingEngine *engine,
465
                        GtkStateFlags     state,
Carlos Garnacho's avatar
Carlos Garnacho committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479
                        ...)
{
  GtkThemingEnginePrivate *priv;
  va_list args;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;

  va_start (args, state);
  gtk_style_context_get_valist (priv->context, state, args);
  va_end (args);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
480 481 482 483
/**
 * gtk_theming_engine_get_style_property:
 * @engine: a #GtkThemingEngine
 * @property_name: the name of the widget style property
484
 * @value: Return location for the property value, free with
Carlos Garnacho's avatar
Carlos Garnacho committed
485 486 487 488 489 490
 *         g_value_unset() after use.
 *
 * Gets the value for a widget style property.
 *
 * Since: 3.0
 **/
491 492 493 494 495 496 497 498 499 500 501 502 503 504
void
gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
                                       const gchar      *property_name,
                                       GValue           *value)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
  g_return_if_fail (property_name != NULL);

  priv = engine->priv;
  gtk_style_context_get_style_property (priv->context, property_name, value);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
505 506 507 508 509 510 511 512 513 514
/**
 * gtk_theming_engine_get_style_valist:
 * @engine: a #GtkThemingEngine
 * @args: va_list of property name/return location pairs, followed by %NULL
 *
 * Retrieves several widget style properties from @engine according to the
 * currently rendered content's style.
 *
 * Since: 3.0
 **/
515 516 517 518 519 520 521 522 523 524 525 526
void
gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
                                     va_list           args)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_style_valist (priv->context, args);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
527 528 529 530 531 532 533 534 535 536
/**
 * gtk_theming_engine_get_style:
 * @engine: a #GtkThemingEngine
 * @...: property name /return value pairs, followed by %NULL
 *
 * Retrieves several widget style properties from @engine according
 * to the currently rendered content's style.
 *
 * Since: 3.0
 **/
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
void
gtk_theming_engine_get_style (GtkThemingEngine *engine,
                              ...)
{
  GtkThemingEnginePrivate *priv;
  va_list args;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;

  va_start (args, engine);
  gtk_style_context_get_style_valist (priv->context, args);
  va_end (args);
}

553 554 555 556
/**
 * gtk_theming_engine_lookup_color:
 * @engine: a #GtkThemingEngine
 * @color_name: color name to lookup
557
 * @color: (out): Return location for the looked up color
558 559
 *
 * Looks up and resolves a color name in the current style's color map.
560 561
 *
 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
562
 **/
563
gboolean
564 565 566
gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
                                 const gchar      *color_name,
                                 GdkRGBA          *color)
567 568 569
{
  GtkThemingEnginePrivate *priv;

570 571
  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
  g_return_val_if_fail (color_name != NULL, FALSE);
572 573

  priv = engine->priv;
574
  return gtk_style_context_lookup_color (priv->context, color_name, color);
575 576
}

Carlos Garnacho's avatar
Carlos Garnacho committed
577 578 579 580 581 582 583 584 585 586
/**
 * gtk_theming_engine_get_state:
 * @engine: a #GtkThemingEngine
 *
 * returns the state used when rendering.
 *
 * Returns: the state flags
 *
 * Since: 3.0
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
587 588 589 590 591 592 593 594 595 596 597
GtkStateFlags
gtk_theming_engine_get_state (GtkThemingEngine *engine)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);

  priv = engine->priv;
  return gtk_style_context_get_state (priv->context);
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
/**
 * gtk_theming_engine_state_is_running:
 * @engine: a #GtkThemingEngine
 * @state: a widget state
 * @progress: (out): return location for the transition progress
 *
 * Returns %TRUE if there is a transition animation running for the
 * current region (see gtk_style_context_push_animatable_region()).
 *
 * If @progress is not %NULL, the animation progress will be returned
 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
 * it's closest to being %TRUE. This means transition animations will
 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
 * it's being set to %FALSE.
 *
 * Returns: %TRUE if there is a running transition animation for @state.
 *
 * Since: 3.0
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
617
gboolean
618 619 620
gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
                                     GtkStateType      state,
                                     gdouble          *progress)
Carlos Garnacho's avatar
Carlos Garnacho committed
621 622 623
{
  GtkThemingEnginePrivate *priv;

624
  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
Carlos Garnacho's avatar
Carlos Garnacho committed
625 626

  priv = engine->priv;
627
  return gtk_style_context_state_is_running (priv->context, state, progress);
Carlos Garnacho's avatar
Carlos Garnacho committed
628 629
}

Carlos Garnacho's avatar
Carlos Garnacho committed
630 631 632 633 634 635 636 637 638 639
/**
 * gtk_theming_engine_get_path:
 * @engine: a #GtkThemingEngine
 *
 * Returns the widget path used for style matching.
 *
 * Returns: (transfer none): A #GtkWidgetPath
 *
 * Since: 3.0
 **/
640
const GtkWidgetPath *
Carlos Garnacho's avatar
Carlos Garnacho committed
641 642 643 644 645 646 647 648 649 650
gtk_theming_engine_get_path (GtkThemingEngine *engine)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);

  priv = engine->priv;
  return gtk_style_context_get_path (priv->context);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
651 652 653 654 655 656 657 658 659 660 661 662
/**
 * gtk_theming_engine_has_class:
 * @engine: a #GtkThemingEngine
 * @style_class: class name to look up
 *
 * Returns %TRUE if the currently rendered contents have
 * defined the given class name.
 *
 * Returns: %TRUE if @engine has @class_name defined
 *
 * Since: 3.0
 **/
663 664 665 666 667 668 669 670 671 672 673 674
gboolean
gtk_theming_engine_has_class (GtkThemingEngine *engine,
                              const gchar      *style_class)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);

  priv = engine->priv;
  return gtk_style_context_has_class (priv->context, style_class);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688
/**
 * gtk_theming_engine_has_region:
 * @engine: a #GtkThemingEngine
 * @style_region: a region name
 * @flags: (out) (allow-none): return location for region flags
 *
 * Returns %TRUE if the currently rendered contents have the
 * region defined. If @flags_return is not %NULL, it is set
 * to the flags affecting the region.
 *
 * Returns: %TRUE if region is defined
 *
 * Since: 3.0
 **/
689
gboolean
690
gtk_theming_engine_has_region (GtkThemingEngine *engine,
Carlos Garnacho's avatar
Carlos Garnacho committed
691
                               const gchar      *style_region,
692
                               GtkRegionFlags   *flags)
693 694 695 696 697 698 699 700 701
{
  GtkThemingEnginePrivate *priv;

  if (flags)
    *flags = 0;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);

  priv = engine->priv;
Carlos Garnacho's avatar
Carlos Garnacho committed
702
  return gtk_style_context_has_region (priv->context, style_region, flags);
703 704
}

Carlos Garnacho's avatar
Carlos Garnacho committed
705 706 707 708 709 710 711 712 713 714
/**
 * gtk_theming_engine_get_direction:
 * @engine: a #GtkThemingEngine
 *
 * Returns the widget direction used for rendering.
 *
 * Returns: the widget direction
 *
 * Since: 3.0
 **/
715 716 717 718 719 720 721 722 723 724 725
GtkTextDirection
gtk_theming_engine_get_direction (GtkThemingEngine *engine)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);

  priv = engine->priv;
  return gtk_style_context_get_direction (priv->context);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
726 727 728 729 730 731 732 733 734 735
/**
 * gtk_theming_engine_get_junction_sides:
 * @engine: a #GtkThemingEngine
 *
 * Returns the widget direction used for rendering.
 *
 * Returns: the widget direction
 *
 * Since: 3.0
 **/
736 737 738 739 740 741 742 743 744 745 746
GtkJunctionSides
gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);

  priv = engine->priv;
  return gtk_style_context_get_junction_sides (priv->context);
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
/**
 * gtk_theming_engine_get_color:
 * @engine: a #GtkThemingEngine
 * @state: state to retrieve the color for
 * @color: (out): return value for the foreground color
 *
 * Gets the foreground color for a given state.
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_get_color (GtkThemingEngine *engine,
                              GtkStateFlags     state,
                              GdkRGBA          *color)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_color (priv->context, state, color);
}

/**
 * gtk_theming_engine_get_background_color:
 * @engine: a #GtkThemingEngine
 * @state: state to retrieve the color for
 * @color: (out): return value for the background color
 *
 * Gets the background color for a given state.
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_get_background_color (GtkThemingEngine *engine,
                                         GtkStateFlags     state,
                                         GdkRGBA          *color)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_background_color (priv->context, state, color);
}

/**
 * gtk_theming_engine_get_border_color:
 * @engine: a #GtkThemingEngine
 * @state: state to retrieve the color for
 * @color: (out): return value for the border color
 *
 * Gets the border color for a given state.
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_get_border_color (GtkThemingEngine *engine,
                                     GtkStateFlags     state,
                                     GdkRGBA          *color)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_border_color (priv->context, state, color);
}

/**
 * gtk_theming_engine_get_border:
 * @engine: a #GtkthemingEngine
 * @state: state to retrieve the border for
Matthias Clasen's avatar
Matthias Clasen committed
820
 * @border: (out): return value for the border settings
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
 *
 * Gets the border for a given state as a #GtkBorder.
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_get_border (GtkThemingEngine *engine,
                               GtkStateFlags     state,
                               GtkBorder        *border)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_border (priv->context, state, border);
}

/**
 * gtk_theming_engine_get_padding:
 * @engine: a #GtkthemingEngine
 * @state: state to retrieve the padding for
843
 * @padding: (out): return value for the padding settings
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
 *
 * Gets the padding for a given state as a #GtkBorder.
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_get_padding (GtkThemingEngine *engine,
                                GtkStateFlags     state,
                                GtkBorder        *padding)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_padding (priv->context, state, padding);
}

/**
 * gtk_theming_engine_get_margin:
864
 * @engine: a #GtkThemingEngine
865
 * @state: state to retrieve the border for
866
 * @margin: (out): return value for the margin settings
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
 *
 * Gets the margin for a given state as a #GtkBorder.
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_get_margin (GtkThemingEngine *engine,
                               GtkStateFlags     state,
                               GtkBorder        *margin)
{
  GtkThemingEnginePrivate *priv;

  g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));

  priv = engine->priv;
  gtk_style_context_get_margin (priv->context, state, margin);
}

885 886 887 888 889 890 891
/**
 * gtk_theming_engine_get_font:
 * @engine: a #GtkThemingEngine
 * @state: state to retrieve the font for
 *
 * Returns the font description for a given state.
 *
892 893 894
 * Returns: (transfer none): the #PangoFontDescription for the given
 *          state. This object is owned by GTK+ and should not be
 *          freed.
895 896 897 898 899 900 901 902 903 904 905 906 907 908
 *
 * Since: 3.0
 **/
const PangoFontDescription *
gtk_theming_engine_get_font (GtkThemingEngine *engine,
                             GtkStateFlags     state)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);

  priv = engine->priv;
  return gtk_style_context_get_font (priv->context, state);
}
909

910 911 912 913 914 915 916 917 918 919 920 921 922 923
/* GtkThemingModule */

static gboolean
gtk_theming_module_load (GTypeModule *type_module)
{
  GtkThemingModule *theming_module;
  GModule *module;
  gchar *name, *module_path;

  theming_module = GTK_THEMING_MODULE (type_module);
  name = theming_module->name;
  module_path = _gtk_find_module (name, "theming-engines");

  if (!module_path)
924
    return FALSE;
925 926 927 928 929

  module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
  g_free (module_path);

  if (!module)
930
    return FALSE;
931

932 933 934 935 936
  if (!g_module_symbol (module, "theme_init",
                        (gpointer *) &theming_module->init) ||
      !g_module_symbol (module, "theme_exit",
                        (gpointer *) &theming_module->exit) ||
      !g_module_symbol (module, "create_engine",
937 938 939 940 941 942 943
                        (gpointer *) &theming_module->create_engine))
    {
      g_module_close (module);

      return FALSE;
    }

944 945
  theming_module->module = module;

946
  theming_module->init (G_TYPE_MODULE (theming_module));
947 948 949 950

  return TRUE;
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static void
gtk_theming_module_unload (GTypeModule *type_module)
{
  GtkThemingModule *theming_module;

  theming_module = GTK_THEMING_MODULE (type_module);

  theming_module->exit ();

  g_module_close (theming_module->module);

  theming_module->module = NULL;
  theming_module->init = NULL;
  theming_module->exit = NULL;
  theming_module->create_engine = NULL;
}

968 969 970 971 972 973
static void
gtk_theming_module_class_init (GtkThemingModuleClass *klass)
{
  GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);

  module_class->load = gtk_theming_module_load;
974
  module_class->unload = gtk_theming_module_unload;
975 976 977 978 979 980 981
}

static void
gtk_theming_module_init (GtkThemingModule *module)
{
}

Carlos Garnacho's avatar
Carlos Garnacho committed
982 983 984 985 986 987 988 989 990 991
/**
 * gtk_theming_engine_load:
 * @name: Theme engine name to load
 *
 * Loads and initializes a theming engine module from the
 * standard directories.
 *
 * Returns: (transfer none): A theming engine, or %NULL if
 * the engine @name doesn't exist.
 **/
992
GtkThemingEngine *
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
gtk_theming_engine_load (const gchar *name)
{
  static GHashTable *engines = NULL;
  static GtkThemingEngine *default_engine;
  GtkThemingEngine *engine = NULL;

  if (name)
    {
      if (!engines)
        engines = g_hash_table_new (g_str_hash, g_str_equal);

      engine = g_hash_table_lookup (engines, name);

      if (!engine)
        {
          GtkThemingModule *module;

          module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
          g_type_module_set_name (G_TYPE_MODULE (module), name);
          module->name = g_strdup (name);

          if (module && g_type_module_use (G_TYPE_MODULE (module)))
            {
              engine = (module->create_engine) ();

              if (engine)
                g_hash_table_insert (engines, module->name, engine);
            }
        }
    }
1023
  else
1024
    {
1025
      if (G_UNLIKELY (!default_engine))
1026 1027 1028 1029 1030 1031 1032 1033
        default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);

      engine = default_engine;
    }

  return engine;
}

Carlos Garnacho's avatar
Carlos Garnacho committed
1034 1035 1036 1037 1038 1039
/**
 * gtk_theming_engine_get_screen:
 * @engine: a #GtkThemingEngine
 *
 * Returns the #GdkScreen to which @engine currently rendering to.
 *
1040
 * Returns: (transfer none): a #GdkScreen, or %NULL.
Carlos Garnacho's avatar
Carlos Garnacho committed
1041
 **/
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
GdkScreen *
gtk_theming_engine_get_screen (GtkThemingEngine *engine)
{
  GtkThemingEnginePrivate *priv;

  g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);

  priv = engine->priv;
  return gtk_style_context_get_screen (priv->context);
}

1053 1054 1055 1056 1057 1058 1059 1060 1061
/* Paint method implementations */
static void
gtk_theming_engine_render_check (GtkThemingEngine *engine,
                                 cairo_t          *cr,
                                 gdouble           x,
                                 gdouble           y,
                                 gdouble           width,
                                 gdouble           height)
{
1062
  GdkRGBA fg_color, bg_color;
1063
  GtkStateFlags flags;
1064
  gint exterior_size, interior_size, thickness, pad;
1065
  GtkBorderStyle border_style;
1066
  GtkBorder border;
1067
  gint border_width;
1068 1069 1070 1071

  flags = gtk_theming_engine_get_state (engine);
  cairo_save (cr);

1072 1073 1074 1075
  gtk_theming_engine_get_color (engine, flags, &fg_color);
  gtk_theming_engine_get_background_color (engine, flags, &bg_color);
  gtk_theming_engine_get_border (engine, flags, &border);

1076
  gtk_theming_engine_get (engine, flags,
1077
                          "border-style", &border_style,
1078 1079
                          NULL);

1080 1081
  border_width = MIN (MIN (border.top, border.bottom),
                      MIN (border.left, border.right));
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  exterior_size = MIN (width, height);

  if (exterior_size % 2 == 0) /* Ensure odd */
    exterior_size -= 1;

  /* FIXME: thickness */
  thickness = 1;
  pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
  interior_size = MAX (1, exterior_size - 2 * pad);

  if (interior_size < 7)
1093
    {
1094 1095 1096
      interior_size = 7;
      pad = MAX (0, (exterior_size - interior_size) / 2);
    }
1097

1098 1099
  x -= (1 + exterior_size - (gint) width) / 2;
  y -= (1 + exterior_size - (gint) height) / 2;
1100

1101
  if (border_style == GTK_BORDER_STYLE_SOLID)
1102
    {
1103 1104
      GdkRGBA border_color;

1105
      cairo_set_line_width (cr, border_width);
1106
      gtk_theming_engine_get_border_color (engine, flags, &border_color);
1107 1108

      cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
1109
      gdk_cairo_set_source_rgba (cr, &bg_color);
1110 1111
      cairo_fill_preserve (cr);

1112
      gdk_cairo_set_source_rgba (cr, &border_color);
1113 1114 1115
      cairo_stroke (cr);
    }

1116
  gdk_cairo_set_source_rgba (cr, &fg_color);
1117

1118
  if (flags & GTK_STATE_FLAG_INCONSISTENT)
1119
    {
1120 1121 1122 1123 1124 1125 1126 1127
      int line_thickness = MAX (1, (3 + interior_size * 2) / 7);

      cairo_rectangle (cr,
		       x + pad,
		       y + pad + (1 + interior_size - line_thickness) / 2,
		       interior_size,
		       line_thickness);
      cairo_fill (cr);
1128
    }
1129
  else
1130
    {
1131
      gdouble progress;
1132
      gboolean running;
1133

1134
      running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1135

1136
      if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
1137
        {
1138 1139 1140
          if (!running)
            progress = 1;

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
          cairo_translate (cr,
                           x + pad, y + pad);

          cairo_scale (cr, interior_size / 7., interior_size / 7.);

          cairo_rectangle (cr, 0, 0, 7 * progress, 7);
          cairo_clip (cr);

          cairo_move_to  (cr, 7.0, 0.0);
          cairo_line_to  (cr, 7.5, 1.0);
          cairo_curve_to (cr, 5.3, 2.0,
                          4.3, 4.0,
                          3.5, 7.0);
          cairo_curve_to (cr, 3.0, 5.7,
                          1.3, 4.7,
                          0.0, 4.7);
          cairo_line_to  (cr, 0.2, 3.5);
          cairo_curve_to (cr, 1.1, 3.5,
                          2.3, 4.3,
                          3.0, 5.0);
          cairo_curve_to (cr, 1.0, 3.9,
                          2.4, 4.1,
                          3.2, 4.9);
          cairo_curve_to (cr, 3.5, 3.1,
                          5.2, 2.0,
                          7.0, 0.0);

          cairo_fill (cr);
        }
1170 1171 1172 1173 1174
    }

  cairo_restore (cr);
}

1175 1176 1177 1178 1179 1180 1181 1182 1183
static void
gtk_theming_engine_render_option (GtkThemingEngine *engine,
                                  cairo_t          *cr,
                                  gdouble           x,
                                  gdouble           y,
                                  gdouble           width,
                                  gdouble           height)
{
  GtkStateFlags flags;
1184
  GdkRGBA fg_color, bg_color;
1185 1186
  gint exterior_size, interior_size, pad, thickness, border_width;
  GtkBorderStyle border_style;
1187
  GtkBorder border;
1188 1189 1190 1191 1192

  flags = gtk_theming_engine_get_state (engine);

  cairo_save (cr);

1193 1194 1195 1196
  gtk_theming_engine_get_color (engine, flags, &fg_color);
  gtk_theming_engine_get_background_color (engine, flags, &bg_color);
  gtk_theming_engine_get_border (engine, flags, &border);

1197
  gtk_theming_engine_get (engine, flags,
1198
                          "border-style", &border_style,
1199 1200
                          NULL);

1201
  exterior_size = MIN (width, height);
1202 1203
  border_width = MIN (MIN (border.top, border.bottom),
                      MIN (border.left, border.right));
1204 1205 1206 1207 1208 1209 1210

  if (exterior_size % 2 == 0) /* Ensure odd */
    exterior_size -= 1;

  x -= (1 + exterior_size - width) / 2;
  y -= (1 + exterior_size - height) / 2;

1211
  if (border_style == GTK_BORDER_STYLE_SOLID)
1212
    {
1213 1214
      GdkRGBA border_color;

1215
      cairo_set_line_width (cr, border_width);
1216
      gtk_theming_engine_get_border_color (engine, flags, &border_color);
1217 1218

      cairo_new_sub_path (cr);
1219
      cairo_arc (cr,
Matthias Clasen's avatar
Matthias Clasen committed
1220 1221 1222 1223
                 x + exterior_size / 2.,
                 y + exterior_size / 2.,
                 (exterior_size - 1) / 2.,
                 0, 2 * G_PI);
1224

1225
      gdk_cairo_set_source_rgba (cr, &bg_color);
1226 1227
      cairo_fill_preserve (cr);

1228
      gdk_cairo_set_source_rgba (cr, &border_color);
1229 1230 1231
      cairo_stroke (cr);
    }

1232
  gdk_cairo_set_source_rgba (cr, &fg_color);
1233

1234 1235 1236
  /* FIXME: thickness */
  thickness = 1;

1237
  if (flags & GTK_STATE_FLAG_INCONSISTENT)
1238
    {
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
      gint line_thickness;

      pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
      interior_size = MAX (1, exterior_size - 2 * pad);

      if (interior_size < 7)
        {
          interior_size = 7;
          pad = MAX (0, (exterior_size - interior_size) / 2);
        }

      line_thickness = MAX (1, (3 + interior_size * 2) / 7);

      cairo_rectangle (cr,
Matthias Clasen's avatar
Matthias Clasen committed
1253 1254 1255 1256
                       x + pad,
                       y + pad + (interior_size - line_thickness) / 2.,
                       interior_size,
                       line_thickness);
1257
      cairo_fill (cr);
1258
    }
1259
  if (flags & GTK_STATE_FLAG_ACTIVE)
1260
    {
1261 1262
      pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
      interior_size = MAX (1, exterior_size - 2 * pad);
1263

1264
      if (interior_size < 5)
Matthias Clasen's avatar
Matthias Clasen committed
1265 1266 1267 1268
        {
          interior_size = 7;
          pad = MAX (0, (exterior_size - interior_size) / 2);
        }
1269

1270
      cairo_new_sub_path (cr);
1271
      cairo_arc (cr,
Matthias Clasen's avatar
Matthias Clasen committed
1272 1273 1274 1275
                 x + pad + interior_size / 2.,
                 y + pad + interior_size / 2.,
                 interior_size / 2.,
                 0, 2 * G_PI);
1276 1277 1278 1279 1280 1281
      cairo_fill (cr);
    }

  cairo_restore (cr);
}

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
static void
add_path_arrow (cairo_t *cr,
                gdouble  angle,
                gdouble  x,
                gdouble  y,
                gdouble  size)
{
  cairo_save (cr);

  cairo_translate (cr, x + (size / 2), y + (size / 2));
  cairo_rotate (cr, angle);

1294 1295 1296
  cairo_move_to (cr, 0, - (size / 4));
  cairo_line_to (cr, - (size / 2), (size / 4));
  cairo_line_to (cr, (size / 2), (size / 4));
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
  cairo_close_path (cr);

  cairo_restore (cr);
}

static void
gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
                                 cairo_t          *cr,
                                 gdouble           angle,
                                 gdouble           x,
                                 gdouble           y,
                                 gdouble           size)
{
  GtkStateFlags flags;
1311
  GdkRGBA fg_color;
1312 1313 1314 1315

  cairo_save (cr);

  flags = gtk_theming_engine_get_state (engine);
1316
  gtk_theming_engine_get_color (engine, flags, &fg_color);
1317 1318 1319 1320 1321 1322 1323 1324 1325

  if (flags & GTK_STATE_FLAG_INSENSITIVE)
    {
      add_path_arrow (cr, angle, x + 1, y + 1, size);
      cairo_set_source_rgb (cr, 1, 1, 1);
      cairo_fill (cr);
    }

  add_path_arrow (cr, angle, x, y, size);
1326
  gdk_cairo_set_source_rgba (cr, &fg_color);
1327 1328 1329 1330 1331
  cairo_fill (cr);

  cairo_restore (cr);
}

1332
static void
1333 1334 1335 1336 1337
add_path_line (cairo_t        *cr,
               gdouble         x1,
               gdouble         y1,
               gdouble         x2,
               gdouble         y2)
1338
{
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  /* Adjust endpoints */
  if (y1 == y2)
    {
      y1 += 0.5;
      y2 += 0.5;
      x2 += 1;
    }
  else if (x1 == x2)
    {
      x1 += 0.5;
      x2 += 0.5;
      y2 += 1;
    }
1352

1353 1354 1355 1356
  cairo_move_to (cr, x1, y1);
  cairo_line_to (cr, x2, y2);
}

1357
static void
1358 1359 1360
color_shade (const GdkRGBA *color,
             gdouble        factor,
             GdkRGBA       *color_return)
1361
{
1362 1363 1364 1365 1366 1367 1368 1369
  GtkSymbolicColor *literal, *shade;

  literal = gtk_symbolic_color_new_literal (color);
  shade = gtk_symbolic_color_new_shade (literal, factor);
  gtk_symbolic_color_unref (literal);

  gtk_symbolic_color_resolve (shade, NULL, color_return);
  gtk_symbolic_color_unref (shade);
1370 1371
}

1372
static void
1373 1374 1375 1376 1377 1378 1379
render_background_internal (GtkThemingEngine *engine,
                            cairo_t          *cr,
                            gdouble           x,
                            gdouble           y,
                            gdouble           width,
                            gdouble           height,
                            GtkJunctionSides  junction)
1380
{
1381
  GdkRGBA bg_color;
1382
  cairo_pattern_t *pattern;
1383
  GtkStateFlags flags;
1384
  gboolean running;
1385
  gdouble progress;
1386
  GtkRoundedBox border_box;
1387
  GtkShadow *box_shadow;
1388
  GtkBorder border;
1389

1390
  flags = gtk_theming_engine_get_state (engine);
1391

1392 1393
  gtk_theming_engine_get_background_color (engine, flags, &bg_color);

1394 1395
  gtk_theming_engine_get (engine, flags,
                          "background-image", &pattern,
1396
                          "box-shadow", &box_shadow,
1397 1398
                          NULL);

1399 1400 1401 1402
  cairo_save (cr);
  cairo_translate (cr, x, y);

  running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1403

1404 1405 1406 1407 1408 1409 1410
  if (gtk_theming_engine_has_class (engine, "background"))
    {
      cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
      cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
      cairo_paint (cr);
    }

1411
  if (running)
1412 1413 1414
    {
      cairo_pattern_t *other_pattern;
      GtkStateFlags other_flags;
1415
      GdkRGBA other_bg;
1416 1417
      cairo_pattern_t *new_pattern = NULL;

1418
      if (flags & GTK_STATE_FLAG_PRELIGHT)
1419 1420 1421 1422 1423 1424 1425
        {
          other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
          progress = 1 - progress;
        }
      else
        other_flags = flags | GTK_STATE_FLAG_PRELIGHT;

1426
      gtk_theming_engine_get_background_color (engine, other_flags, &other_bg);
1427 1428 1429 1430 1431 1432
      gtk_theming_engine_get (engine, other_flags,
                              "background-image", &other_pattern,
                              NULL);

      if (pattern && other_pattern)
        {
1433
          cairo_pattern_type_t type, other_type;
1434 1435 1436 1437
          gint n0, n1;

          cairo_pattern_get_color_stop_count (pattern, &n0);
          cairo_pattern_get_color_stop_count (other_pattern, &n1);
1438 1439
          type = cairo_pattern_get_type (pattern);
          other_type = cairo_pattern_get_type (other_pattern);
1440

1441
          if (type == other_type && n0 == n1)
1442
            {
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
              gdouble offset0, red0, green0, blue0, alpha0;
              gdouble offset1, red1, green1, blue1, alpha1;
              gdouble x00, x01, y00, y01, x10, x11, y10, y11;
              gdouble r00, r01, r10, r11;
              guint i;

              if (type == CAIRO_PATTERN_TYPE_LINEAR)
                {
                  cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
                  cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);

                  new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
                                                             y00 + (y10 - y00) * progress,
                                                             x01 + (x11 - x01) * progress,
                                                             y01 + (y11 - y01) * progress);
                }
              else
                {
                  cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
                  cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);

                  new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
                                                             y00 + (y10 - y00) * progress,
                                                             r00 + (r10 - r00) * progress,
                                                             x01 + (x11 - x01) * progress,
                                                             y01 + (y11 - y01) * progress,
                                                             r01 + (r11 - r01) * progress);
                }

              cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
              i = 0;

              /* Blend both gradients into one */
              while (i < n0 && i < n1)
                {
                  cairo_pattern_get_color_stop_rgba (pattern, i,
                                                     &offset0,
                                                     &red0, &green0, &blue0,
                                                     &alpha0);
                  cairo_pattern_get_color_stop_rgba (other_pattern, i,
                                                     &offset1,
                                                     &red1, &green1, &blue1,
                                                     &alpha1);

                  cairo_pattern_add_color_stop_rgba (new_pattern,
                                                     offset0 + ((offset1 - offset0) * progress),
                                                     red0 + ((red1 - red0) * progress),
                                                     green0 + ((green1 - green0) * progress),
                                                     blue0 + ((blue1 - blue0) * progress),
                                                     alpha0 + ((alpha1 - alpha0) * progress));
                  i++;
                }
            }
          else
            {
1498 1499 1500 1501 1502 1503
              cairo_save (cr);

              cairo_rectangle (cr, 0, 0, width, height);
              cairo_clip (cr);

              cairo_push_group (cr);
1504

1505
              cairo_scale (cr, width, height);
1506
              cairo_set_source (cr, other_pattern);
1507 1508 1509 1510 1511
              cairo_paint_with_alpha (cr, progress);
              cairo_set_source (cr, pattern);
              cairo_paint_with_alpha (cr, 1.0 - progress);

              new_pattern = cairo_pop_group (cr);
1512

1513
              cairo_restore (cr);
1514 1515 1516 1517 1518
            }
        }
      else if (pattern || other_pattern)
        {
          cairo_pattern_t *p;
Carlos Garnacho's avatar
Carlos Garnacho committed
1519
          const GdkRGBA *c;
1520
          gdouble x0, y0, x1, y1, r0, r1;
1521 1522 1523 1524 1525 1526
          gint n, i;

          /* Blend a pattern with a color */
          if (pattern)
            {
              p = pattern;
1527
              c = &other_bg;
1528 1529 1530 1531 1532
              progress = 1 - progress;
            }
          else
            {
              p = other_pattern;
1533
              c = &bg_color;
1534 1535
            }

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
          if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
            {
              cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
              new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
            }
          else
            {
              cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
              new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
            }

1547 1548 1549 1550
          cairo_pattern_get_color_stop_count (p, &n);

          for (i = 0; i < n; i++)
            {
1551 1552
              gdouble red1, green1, blue1, alpha1;
              gdouble offset;
1553 1554 1555 1556

              cairo_pattern_get_color_stop_rgba (p, i,
                                                 &offset,
                                                 &red1, &green1, &blue1,
1557
                                                 &alpha1);
1558
              cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1559 1560 1561 1562
                                                 c->red + ((red1 - c->red) * progress),
                                                 c->green + ((green1 - c->green) * progress),
                                                 c->blue + ((blue1 - c->blue) * progress),
                                                 c->alpha + ((alpha1 - c->alpha) * progress));
1563 1564 1565 1566 1567
            }
        }
      else
        {
          /* Merge just colors */
1568 1569 1570 1571
          new_pattern = cairo_pattern_create_rgba (CLAMP (bg_color.red + ((other_bg.red - bg_color.red) * progress), 0, 1),
                                                   CLAMP (bg_color.green + ((other_bg.green - bg_color.green) * progress), 0, 1),
                                                   CLAMP (bg_color.blue + ((other_bg.blue - bg_color.blue) * progress), 0, 1),
                                                   CLAMP (bg_color.alpha + ((other_bg.alpha - bg_color.alpha) * progress), 0, 1));
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
        }

      if (new_pattern)
        {
          /* Replace pattern to use */
          cairo_pattern_destroy (pattern);
          pattern = new_pattern;
        }

      if (other_pattern)
        cairo_pattern_destroy (other_pattern);
    }

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  gtk_theming_engine_get_border (engine, flags, &border);

  /* In the CSS box model, by default the background positioning area is
   * the padding-box, i.e. all the border-box minus the borders themselves,
   * which determines also its default size, see
   * http://dev.w3.org/csswg/css3-background/#background-origin
   *
   * In the future we might want to support different origins or clips, but
   * right now we just shrink to the default.
   */
1595 1596
  _gtk_rounded_box_init_rect (&border_box, 0, 0, width, height);
  _gtk_rounded_box_apply_border_radius (&border_box, engine, flags, junction);
Cosimo Cecchi's avatar