aboutsummaryrefslogtreecommitdiffstats
path: root/src/sato-draw-cairo.c
blob: a1a0cdc5b38395596fa39744f43dc76f0764551b (plain)
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
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
326
327
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* vi: set expandtab sw=2 sts=2:
 * sato-draw.c
 * This file is part of sato-engine
 *
 * Copyright (C) 2006,2007 - OpenedHand Ltd
 *
 * 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"

#ifdef ENABLE_CAIRO

#include "sato-draw.h"
#include "sato-style.h"

#include <stdio.h>
#include <math.h>
#include <string.h>

#define DEBUG(func) // g_printf ("%s: detail = '%s'; state = %d; x:%d; y:%d; w:%d; h:%d;\n", func, detail, state_type, x, y, width, height);
#define DETAIL(foo) (detail && strcmp (foo, detail) == 0)

#define LINE_WIDTH 2
#define RADIUS 6

GtkStyleClass *parent_style_class;

static void
sato_rounded_rectangle (cairo_t *cr, gint x, gint y, gint width, gint height)
{
  int radius = RADIUS;
  if (width < RADIUS * 2)
  {
    radius = width / 2;
  }
  else if (height < RADIUS * 2)
  {
    radius = height / 2;
  }


  cairo_move_to (cr, x, y + height - radius);
  cairo_arc (cr, x + radius, y + radius, radius, M_PI, M_PI * 1.5);
  cairo_arc (cr, x + width - radius, y + radius, radius, M_PI * 1.5, 0);
  cairo_arc (cr, x + width - radius, y + height - radius, radius, 0, M_PI * 0.5);
  cairo_arc (cr, x + radius, y + height - radius, radius, M_PI * 0.5, M_PI);
}

static void
sato_draw_box (DRAW_ARGS)
{
  gboolean has_focus;
  cairo_t *cr;
  GdkColor *border_color = &style->fg[state_type];

  DEBUG ("draw_box");

  if (DETAIL ("paned") || DETAIL ("vscrollbar") || DETAIL ("hscrollbar"))
    return;

  /* hack to remove PRELIGHT state */
  if (!DETAIL ("menuitem") && state_type == GTK_STATE_PRELIGHT)
  {
    if (widget && GTK_IS_TOGGLE_BUTTON (widget) && shadow_type == GTK_SHADOW_IN)
    {
      state_type = GTK_STATE_ACTIVE;
    }
    else
    {
      if (widget && GTK_IS_BUTTON (widget)
          && (gtk_button_get_relief (GTK_BUTTON (widget)) == GTK_RELIEF_NONE))
      {
        /* none relief buttons shouldn't draw anything for "normal" state */
        return;
      }
      else
      {
        state_type == GTK_STATE_NORMAL;
      }
    }
  }

  if (width <= 0 && DETAIL ("menu"))
  {
    /* we're allowed to set a mask on the window here */
    GdkBitmap *bm;
    cairo_t *cm;
    GtkWidget *toplevel;

    SANITIZE_SIZE; /* get the correct sizes */

    bm = (GdkBitmap *) gdk_pixmap_new (NULL, width, height, 1);
    cm = gdk_cairo_create (bm);

    /* fill the pixmap black */
    cairo_save (cm);
    cairo_rectangle (cm, 0, 0, width, height);
    cairo_set_operator (cm, CAIRO_OPERATOR_CLEAR);
    cairo_fill (cm);
    cairo_restore (cm);

    /* create the mask as a white region */
    cairo_set_source_rgb (cm, 1.0, 1.0, 1.0);
    sato_rounded_rectangle (cm, 0, 0, width, height);
    cairo_fill (cm);
    cairo_destroy (cm);

    /* set the mask on the top level widget... */
    toplevel = gtk_widget_get_toplevel (widget);

    gtk_widget_shape_combine_mask (toplevel, bm, 0, 0);
    g_object_unref (bm);
  }
  else
  {
    SANITIZE_SIZE;
  }


  /*** treeview headers ***/
  if (widget && GTK_IS_TREE_VIEW (widget->parent))
  {
    return;
  }

  has_focus = (widget && GTK_WIDGET_HAS_FOCUS (widget));
  if (has_focus && state_type != GTK_STATE_ACTIVE)
      border_color = &style->bg[GTK_STATE_SELECTED];

  /*** spin buttons ***/
  if (DETAIL ("spinbutton_down") || DETAIL ("spinbutton_up"))
    return;

  if (DETAIL ("spinbutton"))
  {
    /* FIXME: for RTL */
    width += 10;
    x -= 10;
  }

  /*** combo boxes ***/
  if (DETAIL ("button") && widget && GTK_IS_COMBO_BOX_ENTRY (widget->parent))
  {
    GtkWidget *entry;

    entry = g_object_get_data (G_OBJECT (widget->parent), "sato-combo-entry");
    if (GTK_IS_ENTRY (entry))
    {
      gtk_widget_queue_draw_area (entry, entry->allocation.x, entry->allocation.y,
                                  entry->allocation.width,entry->allocation.height);

      has_focus = GTK_WIDGET_HAS_FOCUS (entry);
    }

    g_object_set_data (G_OBJECT (widget->parent), "sato-combo-button", widget);
    if (has_focus)
      border_color = &style->bg[GTK_STATE_SELECTED];
    else
      border_color = &style->fg[state_type];

    /* FIXME: RTL */
    width += 10;
    x -= 10;

  }


  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);


  /* menu and toolbars get just a single line at the bottom of the widget */
  if (DETAIL ("menubar") || DETAIL ("toolbar"))
  {
    gdk_cairo_set_source_color (cr, border_color);
    cairo_move_to (cr, x, y + height - LINE_WIDTH / 2);
    cairo_line_to (cr, x + width, y + height - LINE_WIDTH / 2);
    cairo_stroke (cr);
  }
  else
  {
    sato_rounded_rectangle (cr, x + LINE_WIDTH / 2, y + LINE_WIDTH / 2,
        width - LINE_WIDTH, height - LINE_WIDTH);
    gdk_cairo_set_source_color (cr, &style->bg[state_type]);
    cairo_fill_preserve (cr);
    gdk_cairo_set_source_color (cr, border_color);
    cairo_stroke (cr);
  }

  cairo_destroy (cr);

}

static void
sato_draw_shadow (DRAW_ARGS)
{
  cairo_t *cr;

  DEBUG ("draw_shadow");

  if (shadow_type == GTK_SHADOW_NONE)
    return;

  SANITIZE_SIZE;

  /* FIXME: for RTL */
  if (widget && (GTK_IS_SPIN_BUTTON (widget) || GTK_IS_COMBO_BOX_ENTRY (widget->parent)))
      width += 10;

  if (widget && GTK_IS_COMBO_BOX_ENTRY (widget->parent))
  {
    GtkWidget *button;
    g_object_set_data (G_OBJECT (widget->parent), "sato-combo-entry", widget);

    button = g_object_get_data (G_OBJECT (widget->parent), "sato-combo-button");
    if (GTK_IS_BUTTON (button))
      gtk_widget_queue_draw_area (button, button->allocation.x, button->allocation.y,
                                  button->allocation.width,button->allocation.height);
  }

  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);

  x += LINE_WIDTH / 2;
  y += LINE_WIDTH / 2;
  width -= LINE_WIDTH;
  height -= LINE_WIDTH;

  /*** draw the border ***/
  sato_rounded_rectangle (cr, x, y, width, height);

  /* draw a hilight shadow on focused widgets (i.e. entry widgets) */
  if (widget && GTK_WIDGET_HAS_FOCUS (widget))
    gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_SELECTED]);
  else
    gdk_cairo_set_source_color (cr, &style->fg[state_type]);
  cairo_stroke (cr);

  cairo_destroy (cr);
}

static void
sato_draw_focus (GtkStyle *style, GdkWindow *window, GtkStateType state_type,
                 GdkRectangle *area, GtkWidget *widget, const gchar *detail,
                 gint x, gint y, gint width, gint height)
{
  DEBUG ("draw_focus");
  /* TODO */
}

static void
sato_draw_check (GtkStyle * style, GdkWindow * window,
		 GtkStateType state_type, GtkShadowType shadow_type,
		 GdkRectangle * area, GtkWidget * widget,
		 const gchar * detail, gint x, gint y, gint width,
		 gint height)
{
  cairo_t *cr;
  DEBUG ("draw_check");

  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);

/* fill the background */
  if (widget && GTK_WIDGET_HAS_FOCUS (widget) && !GTK_IS_TREE_VIEW (widget))
    gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_SELECTED]);
  else
    gdk_cairo_set_source_color (cr, &style->base[state_type]);
  cairo_rectangle (cr, x + LINE_WIDTH / 2, y + LINE_WIDTH / 2, width - LINE_WIDTH, height - LINE_WIDTH);
  cairo_fill_preserve (cr);

  /* draw the border */
  gdk_cairo_set_source_color (cr, &style->fg[state_type]);
  cairo_stroke (cr);

  /*** draw check mark ***/
  if (shadow_type == GTK_SHADOW_IN)
  {
    x += LINE_WIDTH * 2;
    y += LINE_WIDTH * 2;
    width -= LINE_WIDTH * 4;
    height -= LINE_WIDTH * 4;

    cairo_rectangle (cr, x, y, width, height);
    cairo_clip (cr);
    y += 1;
    height -= 2;

    cairo_set_line_width (cr, LINE_WIDTH * 1.5);
    cairo_set_line_join (cr, CAIRO_LINE_JOIN_BEVEL);

    cairo_move_to (cr, x, y + height * 0.6);
    cairo_line_to (cr, x + width * 0.3, y + height);
    cairo_line_to (cr, x + width, y);

    gdk_cairo_set_source_color (cr, &style->text[state_type]);
    cairo_stroke (cr);
  }
  cairo_destroy (cr);

}

static void
sato_draw_option (GtkStyle * style, GdkWindow * window,
		  GtkStateType state_type, GtkShadowType shadow_type,
		  GdkRectangle * area, GtkWidget * widget,
		  const gchar * detail, gint x, gint y, gint width,
		  gint height)
{
  cairo_t *cr;
  gint cx, cy, radius;

  DEBUG ("draw_option");

  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);

  /* define radius and centre coordinates */
  radius = width / 2;
  cx = x + radius;
  cy = y + radius;

  /* fill the background */
  if (widget && GTK_WIDGET_HAS_FOCUS (widget) && !GTK_IS_TREE_VIEW (widget))
    gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_SELECTED]);
  else
    gdk_cairo_set_source_color (cr, &style->base[state_type]);
  cairo_arc (cr, cx, cy, radius, 0, M_PI * 2);
  cairo_fill_preserve (cr);

  /* draw the border */
  gdk_cairo_set_source_color (cr, &style->fg[state_type]);
  cairo_stroke (cr);

  /*** draw check mark ***/
  if (shadow_type == GTK_SHADOW_IN)
  {
    cairo_arc (cr, cx, cy, radius - LINE_WIDTH * 2,  0, M_PI * 2);
    gdk_cairo_set_source_color (cr, &style->text[state_type]);
    cairo_fill (cr);
  }
  cairo_destroy (cr);
}

static void
sato_draw_box_gap (GtkStyle * style, GdkWindow * window,
		   GtkStateType state_type, GtkShadowType shadow_type,
		   GdkRectangle * area, GtkWidget * widget, const gchar * detail,
		   gint x, gint y, gint width, gint height,
		   GtkPositionType gap_side, gint gap_x, gint gap_width)
{
  cairo_t *cr;
  GdkRectangle rect;

  if (shadow_type == GTK_SHADOW_NONE)
    return;


  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);
  gdk_cairo_set_source_color (cr, &style->fg[state_type]);

  /* start off with a rectangle... */
  cairo_rectangle (cr, x + LINE_WIDTH / 2, y + LINE_WIDTH / 2, width - LINE_WIDTH, height - LINE_WIDTH);
  cairo_stroke (cr);

  switch (gap_side)
  {
    case GTK_POS_TOP:
      rect.x = x + gap_x + LINE_WIDTH;
      rect.y = y;
      rect.width = gap_width - LINE_WIDTH * 2;
      rect.height = LINE_WIDTH;
      break;
    case GTK_POS_BOTTOM:
      rect.x = x + gap_x + LINE_WIDTH;
      rect.y = y + height - 2;
      rect.width = gap_width - LINE_WIDTH * 2;
      rect.height = 2;
      break;
    case GTK_POS_LEFT:
      rect.x = x;
      rect.y = y + gap_x + LINE_WIDTH;
      rect.width = 2;
      rect.height = gap_width - LINE_WIDTH * 2;
      break;
    case GTK_POS_RIGHT:
      rect.x = x + width - 2;
      rect.y = y + gap_x + LINE_WIDTH;
      rect.width = 2;
      rect.height = gap_width - LINE_WIDTH * 2;
      break;
  }

  /* and finally blank out the gap */
  gtk_style_apply_default_background (style, window, TRUE, state_type, area,
				      rect.x, rect.y, rect.width,
				      rect.height);


}


static void
sato_draw_extension (GtkStyle * style, GdkWindow * window,
		     GtkStateType state_type, GtkShadowType shadow_type,
		     GdkRectangle * area, GtkWidget * widget,const gchar * detail,
		     gint x, gint y, gint width, gint height,
		     GtkPositionType gap_side)
{
  cairo_t *cr;

  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);


  x += LINE_WIDTH / 2;
  y += LINE_WIDTH / 2;
  width -= LINE_WIDTH;
  height -= LINE_WIDTH;

  switch (gap_side)
  {
    case GTK_POS_TOP:     /* bottom tab */
      /* this allows for some overlap */
      if (state_type == GTK_STATE_NORMAL)
        y -= LINE_WIDTH / 2;

      cairo_move_to (cr, x, y);
      cairo_arc_negative (cr, x + RADIUS, y + height - RADIUS, RADIUS, M_PI, M_PI * 0.5);
      cairo_arc_negative (cr, x + width - RADIUS, y + height - RADIUS, RADIUS, M_PI * 0.5, 0);
      cairo_line_to (cr, x + width, y);
      break;
    case GTK_POS_BOTTOM: /* top tab */
      if (state_type == GTK_STATE_NORMAL)
        height += LINE_WIDTH;

      cairo_move_to (cr, x, y + height);
      cairo_arc (cr, x + RADIUS, y + RADIUS, RADIUS, M_PI, M_PI * 1.5);
      cairo_arc (cr, x + width - RADIUS, y + RADIUS, RADIUS, M_PI * 1.5, 0);
      cairo_line_to (cr, x + width, y + height);
      break;
    case GTK_POS_LEFT:	/* right tab */
      if (state_type == GTK_STATE_NORMAL)
        x -= LINE_WIDTH / 2;
      cairo_move_to (cr, x, y);
      cairo_arc (cr, x + width - RADIUS, y + RADIUS, RADIUS, M_PI * 1.5, 0);
      cairo_arc (cr, x + width - RADIUS, y + height - RADIUS, RADIUS, 0, M_PI * 0.5);
      cairo_line_to (cr, x, y + height);
      break;
    case GTK_POS_RIGHT:	/* left tab */
      if (state_type == GTK_STATE_NORMAL)
        width += LINE_WIDTH;
      cairo_move_to (cr, x + width, y);
      cairo_arc_negative (cr, x + RADIUS, y + RADIUS, RADIUS, M_PI * 1.5, M_PI);
      cairo_arc_negative (cr, x + RADIUS, y + height - RADIUS, RADIUS, M_PI, M_PI * 0.5);
      cairo_line_to (cr, x + width, y + height);
      break;
  }

  gdk_cairo_set_source_color (cr, &style->bg[state_type]);
  cairo_fill_preserve (cr);

  gdk_cairo_set_source_color (cr, &style->fg[state_type]);
  cairo_stroke (cr);

  cairo_destroy (cr);

}

static void
sato_draw_vline (GtkStyle *style, GdkWindow *window, GtkStateType state_type,
                 GdkRectangle *area, GtkWidget *widget, const gchar *detail,
                 gint y1, gint y2, gint x)
{
  cairo_t *cr;

  if (DETAIL ("vscale") || DETAIL ("hscale"))
    return;

  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

  gdk_cairo_set_source_color (cr, &style->dark[state_type]);
  cairo_move_to (cr, x, y1);
  cairo_line_to (cr, x, y2);
  cairo_stroke (cr);

  cairo_destroy (cr);
}

static void
sato_draw_hline (GtkStyle *style, GdkWindow *window,  GtkStateType state_type,
                 GdkRectangle *area,GtkWidget *widget, const gchar *detail,
                 gint x1, gint x2, gint y)
{
  cairo_t *cr;

  if (DETAIL ("vscale") || DETAIL ("hscale"))
    return;

  cr = gdk_cairo_create (window);
  cairo_set_line_width (cr, LINE_WIDTH);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

  gdk_cairo_set_source_color (cr, &style->dark[state_type]);
  cairo_move_to (cr, x1, y);
  cairo_line_to (cr, x2, y);
  cairo_stroke (cr);

  cairo_destroy (cr);
}

void
sato_draw_style_class_init (GtkStyleClass * style_class)
{

  parent_style_class = g_type_class_peek_parent (style_class);

  style_class->draw_shadow = sato_draw_shadow;
  style_class->draw_box = sato_draw_box;
  style_class->draw_check = sato_draw_check;
  style_class->draw_option = sato_draw_option;
  style_class->draw_box_gap = sato_draw_box_gap;
  style_class->draw_shadow_gap = sato_draw_box_gap;
  style_class->draw_extension = sato_draw_extension;
  style_class->draw_hline = sato_draw_hline;
  style_class->draw_vline = sato_draw_vline;
  style_class->draw_focus = sato_draw_focus;

}

#endif /* ENABLE_CAIRO */