gtkrender.c 13.7 KB
Newer Older
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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtkrender.h"

22
23
24
25
26
#include <math.h>

#include "gtkcsscornervalueprivate.h"
#include "gtkcssimagevalueprivate.h"
#include "gtkcssnumbervalueprivate.h"
Timm Bäder's avatar
Timm Bäder committed
27
#include "gtkcsscolorvalueprivate.h"
Timm Bäder's avatar
Timm Bäder committed
28
#include "gtkcssshadowvalueprivate.h"
29
#include "gtkcsstransformvalueprivate.h"
30
#include "gtkrendericonprivate.h"
31
#include "gtkstylecontextprivate.h"
32

33
#include "gsk/gskroundedrectprivate.h"
34
#include <gdk/gdktextureprivate.h>
35

36
static void
37
38
gtk_do_render_icon (GtkStyleContext        *context,
                    cairo_t                *cr,
39
40
41
42
                    double                  x,
                    double                  y,
                    double                  width,
                    double                  height)
43
{
44
45
  GtkSnapshot *snapshot;
  GskRenderNode *node;
46

47
  snapshot = gtk_snapshot_new ();
48
  gtk_css_style_snapshot_icon (gtk_style_context_lookup_style (context), snapshot, width, height);
49
50
51
  node = gtk_snapshot_free_to_node (snapshot);
  if (node == NULL)
    return;
52

53
54
55
56
57
58
  cairo_save (cr);
  cairo_translate (cr, x, y);
  gsk_render_node_draw (node, cr);
  cairo_restore (cr);

  gsk_render_node_unref (node);
59
}
60
61
62

/**
 * gtk_render_check:
Matthias Clasen's avatar
Matthias Clasen committed
63
64
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
65
66
67
68
69
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
Matthias Clasen's avatar
Matthias Clasen committed
70
 * Renders a checkmark (as in a `GtkCheckButton`).
71
 *
72
 * The %GTK_STATE_FLAG_CHECKED state determines whether the check is
73
74
75
76
77
78
79
80
81
82
 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
 * should be marked as undefined.
 *
 * Typical checkmark rendering:
 *
 * ![](checks.png)
 **/
void
gtk_render_check (GtkStyleContext *context,
                  cairo_t         *cr,
83
84
85
86
                  double           x,
                  double           y,
                  double           width,
                  double           height)
87
88
89
90
91
92
93
{
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

94
  gtk_do_render_icon (context, cr, x, y, width, height);
95
96
}

97
98
/**
 * gtk_render_option:
Matthias Clasen's avatar
Matthias Clasen committed
99
100
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
101
102
103
104
105
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
106
 * Renders an option mark (as in a radio button), the %GTK_STATE_FLAG_CHECKED
107
108
109
110
111
112
113
114
115
116
 * state will determine whether the option is on or off, and
 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
 *
 * Typical option mark rendering:
 *
 * ![](options.png)
 **/
void
gtk_render_option (GtkStyleContext *context,
                   cairo_t         *cr,
117
118
119
120
                   double           x,
                   double           y,
                   double           width,
                   double           height)
121
122
123
124
125
126
127
{
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

128
  gtk_do_render_icon (context, cr, x, y, width, height);
129
130
}

131
132
/**
 * gtk_render_arrow:
Matthias Clasen's avatar
Matthias Clasen committed
133
134
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
135
136
137
138
139
140
141
 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
 * @x: X origin of the render area
 * @y: Y origin of the render area
 * @size: square side for render area
 *
 * Renders an arrow pointing to @angle.
 *
Phillip Wood's avatar
Phillip Wood committed
142
 * Typical arrow rendering at 0, 1⁄2 π;, π; and 3⁄2 π:
143
144
145
146
147
148
 *
 * ![](arrows.png)
 **/
void
gtk_render_arrow (GtkStyleContext *context,
                  cairo_t         *cr,
149
150
151
152
                  double           angle,
                  double           x,
                  double           y,
                  double           size)
153
154
155
156
157
158
159
{
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (size <= 0)
    return;

160
  gtk_do_render_icon (context, cr, x, y, size, size);
161
162
163
164
}

/**
 * gtk_render_background:
Matthias Clasen's avatar
Matthias Clasen committed
165
166
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
 * Renders the background of an element.
 *
 * Typical background rendering, showing the effect of
 * `background-image`, `border-width` and `border-radius`:
 *
 * ![](background.png)
 **/
void
gtk_render_background (GtkStyleContext *context,
                       cairo_t         *cr,
182
183
184
185
                       double           x,
                       double           y,
                       double           width,
                       double           height)
186
{
187
188
189
  GtkSnapshot *snapshot;
  GskRenderNode *node;

190
191
192
193
194
195
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

196
197
198
199
200
201
202
203
204
205
206
  snapshot = gtk_snapshot_new ();
  gtk_snapshot_render_background (snapshot, context, x, y, width, height);
  node = gtk_snapshot_free_to_node (snapshot);
  if (node == NULL)
    return;

  cairo_save (cr);
  gsk_render_node_draw (node, cr);
  cairo_restore (cr);

  gsk_render_node_unref (node);
207
208
209
210
}

/**
 * gtk_render_frame:
Matthias Clasen's avatar
Matthias Clasen committed
211
212
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
 *
 * Examples of frame rendering, showing the effect of `border-image`,
 * `border-color`, `border-width`, `border-radius` and junctions:
 *
 * ![](frames.png)
 **/
void
gtk_render_frame (GtkStyleContext *context,
                  cairo_t         *cr,
228
229
230
231
                  double           x,
                  double           y,
                  double           width,
                  double           height)
232
{
233
234
235
  GtkSnapshot *snapshot;
  GskRenderNode *node;

236
237
238
239
240
241
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

242
243
244
245
246
247
248
249
250
251
252
  snapshot = gtk_snapshot_new ();
  gtk_snapshot_render_frame (snapshot, context, x, y, width, height);
  node = gtk_snapshot_free_to_node (snapshot);
  if (node == NULL)
    return;

  cairo_save (cr);
  gsk_render_node_draw (node, cr);
  cairo_restore (cr);

  gsk_render_node_unref (node);
253
254
255
256
}

/**
 * gtk_render_expander:
Matthias Clasen's avatar
Matthias Clasen committed
257
258
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
259
260
261
262
263
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
Matthias Clasen's avatar
Matthias Clasen committed
264
 * Renders an expander (as used in `GtkTreeView` and `GtkExpander`) in the area
265
 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_CHECKED
266
267
268
269
270
271
272
273
274
 * determines whether the expander is collapsed or expanded.
 *
 * Typical expander rendering:
 *
 * ![](expanders.png)
 **/
void
gtk_render_expander (GtkStyleContext *context,
                     cairo_t         *cr,
275
276
277
278
                     double           x,
                     double           y,
                     double           width,
                     double           height)
279
280
281
282
283
284
285
{
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

286
  gtk_do_render_icon (context, cr, x, y, width, height);
287
288
289
290
}

/**
 * gtk_render_focus:
Matthias Clasen's avatar
Matthias Clasen committed
291
292
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
293
294
295
296
297
298
299
300
301
302
303
304
305
306
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
 *
 * Typical focus rendering:
 *
 * ![](focus.png)
 **/
void
gtk_render_focus (GtkStyleContext *context,
                  cairo_t         *cr,
307
308
309
310
                  double           x,
                  double           y,
                  double           width,
                  double           height)
311
{
312
313
314
  GtkSnapshot *snapshot;
  GskRenderNode *node;

315
316
317
318
319
320
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

321
322
323
324
325
326
327
328
329
330
331
  snapshot = gtk_snapshot_new ();
  gtk_snapshot_render_frame (snapshot, context, x, y, width, height);
  node = gtk_snapshot_free_to_node (snapshot);
  if (node == NULL)
    return;

  cairo_save (cr);
  gsk_render_node_draw (node, cr);
  cairo_restore (cr);

  gsk_render_node_unref (node);
332
333
334
335
}

/**
 * gtk_render_layout:
Matthias Clasen's avatar
Matthias Clasen committed
336
337
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
338
339
 * @x: X origin
 * @y: Y origin
Matthias Clasen's avatar
Matthias Clasen committed
340
 * @layout: the `PangoLayout` to render
341
342
343
344
345
346
 *
 * Renders @layout on the coordinates @x, @y
 **/
void
gtk_render_layout (GtkStyleContext *context,
                   cairo_t         *cr,
347
348
                   double           x,
                   double           y,
349
350
                   PangoLayout     *layout)
{
351
352
353
  GtkSnapshot *snapshot;
  GskRenderNode *node;

354
355
356
357
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (PANGO_IS_LAYOUT (layout));
  g_return_if_fail (cr != NULL);

358
359
360
361
362
363
364
365
366
367
368
  snapshot = gtk_snapshot_new ();
  gtk_snapshot_render_layout (snapshot, context, x, y, layout); 
  node = gtk_snapshot_free_to_node (snapshot);
  if (node == NULL)
    return;

  cairo_save (cr);
  gsk_render_node_draw (node, cr);
  cairo_restore (cr);

  gsk_render_node_unref (node);
369
370
371
372
}

/**
 * gtk_render_line:
Matthias Clasen's avatar
Matthias Clasen committed
373
374
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
375
376
377
378
379
380
381
382
383
384
 * @x0: X coordinate for the origin of the line
 * @y0: Y coordinate for the origin of the line
 * @x1: X coordinate for the end of the line
 * @y1: Y coordinate for the end of the line
 *
 * Renders a line from (x0, y0) to (x1, y1).
 **/
void
gtk_render_line (GtkStyleContext *context,
                 cairo_t         *cr,
385
386
387
388
                 double           x0,
                 double           y0,
                 double           x1,
                 double           y1)
389
{
390
391
  const GdkRGBA *color;

392
393
394
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

395
396
397
398
399
400
401
402
403
404
405
406
407
408
  cairo_save (cr);

  color = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));

  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
  cairo_set_line_width (cr, 1);

  cairo_move_to (cr, x0 + 0.5, y0 + 0.5);
  cairo_line_to (cr, x1 + 0.5, y1 + 0.5);

  gdk_cairo_set_source_rgba (cr, color);
  cairo_stroke (cr);

  cairo_restore (cr);
409
410
411
412
}

/**
 * gtk_render_handle:
Matthias Clasen's avatar
Matthias Clasen committed
413
414
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
415
416
417
418
419
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
Matthias Clasen's avatar
Matthias Clasen committed
420
421
 * Renders a handle (as in `GtkPaned` and `GtkWindow`’s resize grip),
 * in the rectangle determined by @x, @y, @width, @height.
422
423
424
425
426
427
428
429
 *
 * Handles rendered for the paned and grip classes:
 *
 * ![](handles.png)
 **/
void
gtk_render_handle (GtkStyleContext *context,
                   cairo_t         *cr,
430
431
432
433
                   double           x,
                   double           y,
                   double           width,
                   double           height)
434
435
436
437
438
439
440
{
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

441
442
443
  gtk_render_background (context, cr, x, y, width, height);
  gtk_render_frame (context, cr, x, y, width, height);

444
  gtk_do_render_icon (context, cr, x, y, width, height);
445
446
447
448
}

/**
 * gtk_render_activity:
Matthias Clasen's avatar
Matthias Clasen committed
449
450
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
451
452
453
454
455
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
Matthias Clasen's avatar
Matthias Clasen committed
456
 * Renders an activity indicator (such as in `GtkSpinner`).
457
 * The state %GTK_STATE_FLAG_CHECKED determines whether there is
458
459
460
461
462
 * activity going on.
 **/
void
gtk_render_activity (GtkStyleContext *context,
                     cairo_t         *cr,
463
464
465
466
                     double           x,
                     double           y,
                     double           width,
                     double           height)
467
468
469
470
471
472
473
{
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (cr != NULL);

  if (width <= 0 || height <= 0)
    return;

474
  gtk_do_render_icon (context, cr, x, y, width, height);
475
476
477
478
}

/**
 * gtk_render_icon:
Matthias Clasen's avatar
Matthias Clasen committed
479
480
481
 * @context: a `GtkStyleContext`
 * @cr: a `cairo_t`
 * @texture: a `GdkTexture` containing the icon to draw
Matthias Clasen's avatar
Matthias Clasen committed
482
 * @x: X position for the @texture
Matthias Clasen's avatar
Matthias Clasen committed
483
 * @y: Y position for the @texture
484
 *
485
 * Renders the icon in @texture at the specified @x and @y coordinates.
486
 *
487
 * This function will render the icon in @texture at exactly its size,
488
489
490
 * regardless of scaling factors, which may not be appropriate when
 * drawing on displays with high pixel densities.
 *
491
492
493
494
 **/
void
gtk_render_icon (GtkStyleContext *context,
                 cairo_t         *cr,
495
                 GdkTexture      *texture,
496
497
                 double           x,
                 double           y)
498
{
499
500
  GtkSnapshot *snapshot;
  GskRenderNode *node;
501

502
503
504
505
  snapshot = gtk_snapshot_new ();
  gtk_css_style_snapshot_icon_paintable (gtk_style_context_lookup_style (context),
                                         snapshot,
                                         GDK_PAINTABLE (texture),
506
                                         gdk_texture_get_width (texture),
507
                                         gdk_texture_get_height (texture));
508
509
510
  node = gtk_snapshot_free_to_node (snapshot);
  if (node == NULL)
    return;
511

512
513
514
515
  cairo_save (cr);
  cairo_translate (cr, x, y);
  gsk_render_node_draw (node, cr);
  cairo_restore (cr);
516
}