Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gtk/engine/sugar-style.c
blob: cb33f15477e26ed9c2c035d076cc134ee6b4614e (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
/*
 * Copyright (C) 2007, Red Hat, Inc.
 * Copyright (C) 2007, Benjamin Berg
 *
 * 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 <string.h>
#include <gtk/gtk.h>

#include "sugar-style.h"
#include "sugar-rc-style.h"
#include "sugar-info.h"
#include "sugar-drawing.h"

#define SANITIZE_SIZE g_assert (width >= -1 && height >= -1);    \
    if (width == -1 && height == -1) {                          \
        gdk_drawable_get_size (GDK_DRAWABLE (window), &width, &height);          \
    } else if (width == -1) {                                   \
        gdk_drawable_get_size (GDK_DRAWABLE (window), &width, NULL);             \
    } else if (height == -1) {                                  \
        gdk_drawable_get_size (GDK_DRAWABLE (window), NULL, &height);            \
    }

#define HINT(str) (SUGAR_RC_STYLE (style->rc_style)->hint && g_str_equal (SUGAR_RC_STYLE (style->rc_style)->hint, str))
#define DETAIL(str) (detail && g_str_equal (detail, str))


static void sugar_style_init       (SugarStyle      *style);
static void sugar_style_class_init (SugarStyleClass *klass);

GType sugar_type_style = 0;

static GtkStyleClass *parent_class;

void
sugar_style_register_type (GTypeModule *module)
{
    static const GTypeInfo object_info =
    {
        sizeof (SugarStyleClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) sugar_style_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (SugarStyle),
        0,              /* n_preallocs */
        (GInstanceInitFunc) sugar_style_init,
    };
  
    sugar_type_style = g_type_module_register_type (module,
                                                    GTK_TYPE_STYLE,
                                                    "SugarStyle",
                                                    &object_info, 0);
}

static void
sugar_style_init (SugarStyle *style)
{
}

static cairo_t*
sugar_cairo_create (GdkWindow *window, GdkRectangle *area)
{
    cairo_t *cr;
    
    cr = gdk_cairo_create (GDK_DRAWABLE (window));
    
    if (area) {
        gdk_cairo_rectangle (cr, area);
        cairo_clip (cr);
    }
    return cr;
}


static void
sugar_style_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;
    gint width;
    gint height;

    cr = sugar_cairo_create (window, area);

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

    g_assert (x1 < x2);

    width = x2 - x1; /* + 1  ?? */
    height = SUGAR_RC_STYLE (style->rc_style)->line_width;
    y -= height / 2;

    cairo_rectangle (cr, x1, y, width, height);
    cairo_fill (cr);

    cairo_destroy (cr);
}

static void
sugar_style_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;
    gint width;
    gint height;

    cr = sugar_cairo_create (window, area);

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

    g_assert (y1 < y2);

    height = y2 - y1; /* + 1  ?? */
    width = SUGAR_RC_STYLE (style->rc_style)->line_width;
    x -= width / 2;

    cairo_rectangle (cr, x, y1, width, height);
    cairo_fill (cr);

    cairo_destroy (cr);
}


static void
sugar_style_draw_focus (GtkStyle       *style,
                       GdkWindow      *window,
                       GtkStateType    state_type,
                       GdkRectangle   *area,
                       GtkWidget      *widget,
                       const gchar    *detail,
                       gint            x,
                       gint            y,
                       gint            width,
                       gint            height)
{
    SugarInfo info;
    cairo_t *cr;
    gboolean interior_focus = TRUE;

    sugar_fill_generic_info (&info, style, state_type, GTK_SHADOW_NONE, widget, detail, x, y, width, height);
    sugar_info_get_style_property (&info, "interior-focus", &interior_focus);

    if (!interior_focus) {
        cr = sugar_cairo_create (window, area);

        if (HINT ("comboboxentry")) {
            if (DETAIL ("button")) {
                sugar_remove_corners (&info.corners, info.ltr ? EDGE_LEFT : EDGE_RIGHT);
            } else {
                sugar_remove_corners (&info.corners, info.ltr ? EDGE_RIGHT : EDGE_LEFT);
            }
        } else if (DETAIL ("entry") && HINT ("spinbutton")) {
            /* We need to fake the focus on the button separately. */
            info.cont_edges |= info.ltr ? EDGE_RIGHT : EDGE_LEFT;

            sugar_remove_corners (&info.corners, info.cont_edges);

            info.pos.width += info.rc_style->thick_line_width;
            if (!info.ltr)
                info.pos.x -= info.rc_style->thick_line_width;
        } else if (DETAIL ("spinbutton_up") || DETAIL ("spinbutton_down")) {
            /* spinbutton button focus hack -- this gets called from draw_box */

            gdk_cairo_rectangle (cr, &info.pos);
            cairo_clip (cr);

            /* duplicated from draw_box */
            if (DETAIL ("spinbutton_up"))
                info.cont_edges |= EDGE_BOTTOM;
            else
                info.cont_edges |= EDGE_TOP;

              info.cont_edges |= info.ltr ? EDGE_LEFT : EDGE_RIGHT;

            sugar_remove_corners (&info.corners, info.cont_edges);
        } else if (DETAIL ("trough")) {
            /* Must be scale?!? */
            SugarRangeInfo range_info;

            /* This will decrease the size of the focus as neccessary. */
            range_info.info = info;
            sugar_fill_range_info (&range_info, TRUE);
            info = range_info.info;
        }

        sugar_draw_exterior_focus (cr, &info);
        cairo_destroy (cr);
    } else {
        parent_class->draw_focus (style, window, state_type, area, widget, detail, x, y, width, height);
    }
}

static void
sugar_style_draw_slider (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,
                        GtkOrientation  orientation)
{
    cairo_t *cr;

    SANITIZE_SIZE
    
    cr = sugar_cairo_create (window, area);
    
    if (DETAIL ("hscale") || DETAIL ("vscale")) {
        SugarRangeInfo range_info;
        sugar_fill_generic_info (&range_info.info, style, state_type, shadow_type, widget, detail, x, y, width, height);
        sugar_fill_range_info (&range_info, FALSE);

        sugar_draw_scale_slider (cr, &range_info);
    } else if (HINT ("scrollbar")) {
        SugarRangeInfo range_info;
        sugar_fill_generic_info (&range_info.info, style, state_type, shadow_type, widget, detail, x, y, width, height);
        sugar_fill_range_info (&range_info, FALSE);

        sugar_draw_scrollbar_slider (cr, &range_info);
    } else {
        parent_class->draw_box (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
    }
    cairo_destroy (cr);
}

static void
sugar_style_draw_box (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;

    SANITIZE_SIZE
    
    cr = sugar_cairo_create (window, area);
    
    if (DETAIL ("button") || DETAIL ("optionmenu") || DETAIL ("buttondefault")) {
        SugarInfo info;
        sugar_fill_generic_info (&info, style, state_type, shadow_type, widget, detail, x, y, width, height);

        if (HINT ("comboboxentry")) {
            info.cont_edges = info.ltr ? EDGE_LEFT : EDGE_RIGHT;
            sugar_remove_corners (&info.corners, info.cont_edges);
        }

        if (DETAIL ("buttondefault"))
            sugar_draw_button_default (cr, &info);
        else
            sugar_draw_button (cr, &info);

    } else if (DETAIL ("spinbutton")) {
        SugarInfo info;
        sugar_fill_generic_info (&info, style, state_type, shadow_type, widget, detail, x, y, width, height);

        /* Fill the background with bg_color. */
        sugar_fill_background (cr, &info);

        info.cont_edges = info.ltr ? EDGE_LEFT : EDGE_RIGHT;
        sugar_remove_corners (&info.corners, info.cont_edges);

        sugar_draw_button (cr, &info);
    } else if (DETAIL ("spinbutton_up") || DETAIL ("spinbutton_down")) {
        SugarInfo info;
        sugar_fill_generic_info (&info, style, state_type, shadow_type, widget, detail, x, y, width, height);

        info.cont_edges = info.ltr ? EDGE_LEFT : EDGE_RIGHT;

        if (DETAIL ("spinbutton_up"))
            info.cont_edges |= EDGE_BOTTOM;
        else
            info.cont_edges |= EDGE_TOP;

        sugar_remove_corners (&info.corners, info.cont_edges);
        sugar_draw_button (cr, &info);

        /* Spinbutton focus hack. */
        if (widget && GTK_WIDGET_HAS_FOCUS (widget)) {
            /* Draw a focus for the spinbutton */
            sugar_style_draw_focus (style, window, GTK_STATE_NORMAL, area, widget, detail, x, y, width, height);
        }

    } else if (DETAIL ("trough") || DETAIL ("trough-upper") || DETAIL ("trough-lower")) {
        /* scale or progress bar trough */
        if (HINT ("hscale") || HINT ("vscale")) {
            SugarRangeInfo range_info;
            sugar_fill_generic_info (&range_info.info, style, state_type, shadow_type, widget, detail, x, y, width, height);
            sugar_fill_range_info (&range_info, TRUE);

            sugar_draw_scale_trough (cr, &range_info);
        } else {
            /* just paint a flat box ... */
            gtk_paint_flat_box (style, window, GTK_STATE_NORMAL, shadow_type, area, widget, detail, x, y, width, height);
        }
    } else if (DETAIL ("hseparator") || DETAIL ("vseparator")) {
            /* just fill the separator with bg[state] */
            gdk_cairo_set_source_color (cr, &style->bg[state_type]);
            cairo_rectangle (cr, x, y, width, height);
            cairo_fill (cr);
    } else {
        parent_class->draw_box (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
    }
    cairo_destroy (cr);
}

static void
sugar_style_draw_flat_box (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)
{
    /* Hack to change the entries background when it has the focus. */
    if (DETAIL ("entry_bg")) {
        if (widget && GTK_WIDGET_HAS_FOCUS (widget)) {
            state_type = GTK_STATE_ACTIVE;
        }
    }

    parent_class->draw_flat_box (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
}

static void
sugar_style_draw_shadow (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;

    SANITIZE_SIZE
    
    cr = sugar_cairo_create (window, area);
    
    if (DETAIL ("entry")) {
        SugarInfo info;

        sugar_fill_generic_info (&info, style, state_type, shadow_type, widget, detail, x, y, width, height);

        /* Corner detection. */
        if (HINT ("comboboxentry") || HINT("spinbutton")) {
            info.cont_edges = info.ltr ? EDGE_RIGHT : EDGE_LEFT;
            sugar_remove_corners (&info.corners, info.cont_edges);

            /* Remove the padding on one side. */
            width += info.rc_style->thick_line_width;
            if (!info.ltr) {
                x -= info.rc_style->thick_line_width;
            }
        }
        
        /* XXX: This fakes an ACTIVE state for the focused entry.
         *      Getting this changed in GTK+ with a style property would be cleaner
         *      as that also works for the font colors. (see also draw_flat_box) */
        if (widget && GTK_WIDGET_HAS_FOCUS (widget)) {
            info.state = GTK_STATE_ACTIVE;
        }
        if (widget && !GTK_WIDGET_IS_SENSITIVE (widget)) {
            info.state = GTK_STATE_INSENSITIVE;
        }

        /* Fill the background with bg_color. */
        sugar_fill_background (cr, &info);
        sugar_draw_entry (cr, &info);
    } else {
        parent_class->draw_shadow (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
    }
    
    cairo_destroy (cr);    
}



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

    cr = gdk_cairo_create (window);

    if (DETAIL("tab")) {
        gdk_cairo_set_source_color(cr, &style->bg[state_type]);
        cairo_rectangle(cr, x, y, width, height);
        cairo_fill(cr);
    } else {
        parent_class->draw_extension(style, window, state_type,
                                     shadow_type, area, widget, detail,
                                     x, y, width, height, gap_side);
    }
    
    cairo_destroy(cr);
}

static void
sugar_style_draw_layout(GtkStyle        *style,
                        GdkWindow       *window,
                        GtkStateType     state_type,
                        gboolean         use_text,
                        GdkRectangle    *area,
                        GtkWidget       *widget,
                        const char      *detail,
                        int              x,
                        int              y,
                        PangoLayout     *layout)
{
    GdkGC *gc;

    /* We don't want embossed text. */
    gc = use_text ? style->text_gc[state_type] : style->fg_gc[state_type];

    if (area)
        gdk_gc_set_clip_rectangle (gc, area);

    gdk_draw_layout (window, gc, x, y, layout);

    if (area)
        gdk_gc_set_clip_rectangle (gc, NULL);
}

static void
sugar_style_class_init (SugarStyleClass *klass)
{
    GtkStyleClass *style_class = GTK_STYLE_CLASS(klass);
    
    parent_class = g_type_class_peek_parent(klass);

    style_class->draw_hline = sugar_style_draw_hline;
    style_class->draw_vline = sugar_style_draw_vline;
    style_class->draw_extension = sugar_style_draw_extension;
    style_class->draw_box = sugar_style_draw_box;
    style_class->draw_flat_box = sugar_style_draw_flat_box;
    style_class->draw_shadow = sugar_style_draw_shadow;
    style_class->draw_focus = sugar_style_draw_focus;
    style_class->draw_slider = sugar_style_draw_slider;
    style_class->draw_layout = sugar_style_draw_layout;
}