gtkshadow.c 8.8 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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * Author: Cosimo Cecchi <cosimoc@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"

#include "gtkshadowprivate.h"
#include "gtkstylecontext.h"
26 27
#include "gtkthemingengineprivate.h"
#include "gtkthemingengine.h"
28
#include "gtkpango.h"
29
#include "gtkthemingengineprivate.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

typedef struct _GtkShadowElement GtkShadowElement;

struct _GtkShadowElement {
  gint16 hoffset;
  gint16 voffset;
  gint16 radius;
  gint16 spread;

  gboolean inset;

  GdkRGBA color;
  GtkSymbolicColor *symbolic_color;
};

45 46 47
static void
shadow_element_print (GtkShadowElement *element,
                      GString          *str)
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
{
  gchar *color_str;

  if (element->inset)
    g_string_append (str, "inset ");

  g_string_append_printf (str, "%d %d ",
                          (gint) element->hoffset,
                          (gint) element->voffset);

  if (element->radius != 0)
    g_string_append_printf (str, "%d ", (gint) element->radius);

  if (element->spread != 0)
    g_string_append_printf (str, "%d ", (gint) element->spread);

  if (element->symbolic_color != NULL)
    color_str = gtk_symbolic_color_to_string (element->symbolic_color);
  else
    color_str = gdk_rgba_to_string (&element->color);

  g_string_append (str, color_str);
  g_free (color_str);
}

static void
shadow_element_free (GtkShadowElement *element)
{
  if (element->symbolic_color != NULL)
    gtk_symbolic_color_unref (element->symbolic_color);

  g_slice_free (GtkShadowElement, element);
}

static GtkShadowElement *
shadow_element_new (gdouble hoffset,
                    gdouble voffset,
                    gdouble radius,
                    gdouble spread,
                    gboolean inset,
                    GdkRGBA *color,
                    GtkSymbolicColor *symbolic_color)
{
  GtkShadowElement *retval;

  retval = g_slice_new0 (GtkShadowElement);

  retval->hoffset = hoffset;
  retval->voffset = voffset;
  retval->radius = radius;
  retval->spread = spread;
  retval->inset = inset;

  if (symbolic_color != NULL)
    retval->symbolic_color = gtk_symbolic_color_ref (symbolic_color);

  if (color != NULL)
    retval->color = *color;

  return retval;
}                  

/****************
 * GtkShadow *
 ****************/

G_DEFINE_BOXED_TYPE (GtkShadow, _gtk_shadow,
                     _gtk_shadow_ref, _gtk_shadow_unref)

struct _GtkShadow {
  GList *elements;

  guint ref_count;
  gboolean resolved;
};

GtkShadow *
_gtk_shadow_new (void)
{
  GtkShadow *retval;

  retval = g_slice_new0 (GtkShadow);
  retval->ref_count = 1;
  retval->resolved = FALSE;

  return retval;
}

GtkShadow *
_gtk_shadow_ref (GtkShadow *shadow)
{
  g_return_val_if_fail (shadow != NULL, NULL);

  shadow->ref_count++;

  return shadow;
}

gboolean
_gtk_shadow_get_resolved (GtkShadow *shadow)
{
  return shadow->resolved;
}

void
_gtk_shadow_unref (GtkShadow *shadow)
{
  g_return_if_fail (shadow != NULL);

  shadow->ref_count--;

  if (shadow->ref_count == 0)
    {
      g_list_free_full (shadow->elements,
                        (GDestroyNotify) shadow_element_free);
      g_slice_free (GtkShadow, shadow);
    }
}

void
_gtk_shadow_append (GtkShadow        *shadow,
                    gdouble           hoffset,
                    gdouble           voffset,
                    gdouble           radius,
                    gdouble           spread,
                    gboolean          inset,
                    GtkSymbolicColor *color)
{
  GtkShadowElement *element;

  g_return_if_fail (shadow != NULL);
  g_return_if_fail (color != NULL);

  element = shadow_element_new (hoffset, voffset,
                                radius, spread, inset,
                                NULL, color);

  shadow->elements = g_list_append (shadow->elements, element);
}

GtkShadow *
_gtk_shadow_resolve (GtkShadow          *shadow,
                     GtkStyleProperties *props)
{
  GtkShadow *resolved_shadow;
  GtkShadowElement *element, *resolved_element;
  GdkRGBA color;
  GList *l;

  if (shadow->resolved)
    return _gtk_shadow_ref (shadow);

  resolved_shadow = _gtk_shadow_new ();

  for (l = shadow->elements; l != NULL; l = l->next)
    {
      element = l->data;

      if (!gtk_symbolic_color_resolve (element->symbolic_color,
                                       props,
                                       &color))
        {
          _gtk_shadow_unref (resolved_shadow);
          return NULL;
        }

      resolved_element =
        shadow_element_new (element->hoffset, element->voffset,
                            element->radius, element->spread, element->inset,
                            &color, NULL);

      resolved_shadow->elements =
        g_list_append (resolved_shadow->elements, resolved_element);
    }

  resolved_shadow->resolved = TRUE;

  return resolved_shadow;
}

228 229 230
void
_gtk_shadow_print (GtkShadow *shadow,
                   GString   *str)
231 232 233 234 235 236 237
{
  gint length;
  GList *l;

  length = g_list_length (shadow->elements);

  if (length == 0)
238
    return;
239

240
  shadow_element_print (shadow->elements->data, str);
241 242

  if (length == 1)
243
    return;
244 245 246 247

  for (l = g_list_next (shadow->elements); l != NULL; l = l->next)
    {
      g_string_append (str, ", ");
248
      shadow_element_print (l->data, str);
249 250
    }
}
251 252 253 254 255 256 257 258 259

void
_gtk_text_shadow_paint_layout (GtkShadow       *shadow,
                               cairo_t         *cr,
                               PangoLayout     *layout)
{
  GList *l;
  GtkShadowElement *element;

260 261 262
  if (!cairo_has_current_point (cr))
    cairo_move_to (cr, 0, 0);

263 264 265 266 267 268 269 270 271
  /* render shadows starting from the last one,
   * and the others on top.
   */
  for (l = g_list_last (shadow->elements); l != NULL; l = l->prev)
    {
      element = l->data;

      cairo_save (cr);

272
      cairo_rel_move_to (cr, element->hoffset, element->voffset);
273 274 275
      gdk_cairo_set_source_rgba (cr, &element->color);
      _gtk_pango_fill_layout (cr, layout);

276
      cairo_rel_move_to (cr, -element->hoffset, -element->voffset);
277 278 279 280
      cairo_restore (cr);
  }
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
void
_gtk_icon_shadow_paint (GtkShadow *shadow,
			cairo_t *cr)
{
  GList *l;
  GtkShadowElement *element;
  cairo_pattern_t *pattern;

  for (l = g_list_last (shadow->elements); l != NULL; l = l->prev)
    {
      element = l->data;

      cairo_save (cr);
      pattern = cairo_pattern_reference (cairo_get_source (cr));
      gdk_cairo_set_source_rgba (cr, &element->color);

      cairo_translate (cr, element->hoffset, element->voffset);
      cairo_mask (cr, pattern);

      cairo_restore (cr);
      cairo_pattern_destroy (pattern);
    }
}
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

void
_gtk_icon_shadow_paint_spinner (GtkShadow *shadow,
                                cairo_t   *cr,
                                gdouble    radius,
                                gdouble    progress)
{
  GtkShadowElement *element;
  GList *l;

  for (l = g_list_last (shadow->elements); l != NULL; l = l->prev)
    {
      element = l->data;

      cairo_save (cr);

      cairo_translate (cr, element->hoffset, element->voffset);
      _gtk_theming_engine_paint_spinner (cr,
                                         radius, progress,
                                         &element->color);

      cairo_restore (cr);
    }
}
328 329 330 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

void
_gtk_box_shadow_render (GtkShadow           *shadow,
                        cairo_t             *cr,
                        const GtkRoundedBox *padding_box)
{
  GtkShadowElement *element;
  GtkRoundedBox box;
  GList *l;

  cairo_save (cr);
  cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);

  _gtk_rounded_box_path (padding_box, cr);
  cairo_clip (cr);

  /* render shadows starting from the last one,
   * and the others on top.
   */
  for (l = g_list_last (shadow->elements); l != NULL; l = l->prev)
    {
      element = l->data;

      if (!element->inset)
        continue;

      box = *padding_box;
      _gtk_rounded_box_move (&box, element->hoffset, element->voffset);
      _gtk_rounded_box_shrink (&box,
                               element->spread, element->spread,
                               element->spread, element->spread);

      _gtk_rounded_box_path (&box, cr);
361
      _gtk_rounded_box_clip_path (padding_box, cr);
362 363 364 365 366 367 368

      gdk_cairo_set_source_rgba (cr, &element->color);
      cairo_fill (cr);
  }

  cairo_restore (cr);
}