aboutsummaryrefslogtreecommitdiffstats
path: root/notes/e-bevel-button.c
blob: 480a43051680ef315afffdbd76fd70b1898d31e6 (plain) (blame)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

#include <config.h>
#include <gtk/gtkbutton.h>

#include "e-util/e-util.h"

#include "e-bevel-button.h"
#include "e-bevel-button-util.h"

#define PARENT_TYPE GTK_TYPE_BUTTON

static GtkButtonClass *parent_class = NULL;

struct _EBevelButtonPrivate {
    GdkColor base_color;
    GdkColor dark_color;
    GdkColor light_color;
    GdkGC *gc;
};

static void
e_bevel_button_paint (GtkWidget *widget, GdkRectangle *area)
{
    EBevelButton *button;

    button = E_BEVEL_BUTTON (widget);
    
    if (GTK_WIDGET_DRAWABLE (widget)) {
        gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
        gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);

        gdk_gc_set_foreground (button->priv->gc, &button->priv->base_color);
        gdk_draw_rectangle (widget->window,
                    button->priv->gc,
                    TRUE,
                    0, 0,
                    widget->allocation.width, widget->allocation.height);

        if (GTK_BUTTON (button)->in_button) {
            gdk_gc_set_foreground (button->priv->gc,
                           GTK_BUTTON (button)->button_down ?
                           &button->priv->dark_color :
                           &button->priv->light_color);
            gdk_draw_line (widget->window, button->priv->gc,
                       0, 0, 0, widget->allocation.height - 2);
            gdk_draw_line (widget->window, button->priv->gc,
                       0, 0, widget->allocation.width - 2, 0);

            gdk_gc_set_foreground (button->priv->gc,
                           GTK_BUTTON (button)->button_down ?
                           &button->priv->light_color :
                           &button->priv->dark_color);
            gdk_draw_line (widget->window, button->priv->gc,
                       widget->allocation.width - 1 , 1,
                       widget->allocation.width - 1, widget->allocation.height - 1);
            gdk_draw_line (widget->window, button->priv->gc,
                       1, widget->allocation.height - 1,
                       widget->allocation.width - 1, widget->allocation.height - 1);
        }
    }
}

static gint
e_bevel_button_expose (GtkWidget *widget, GdkEventExpose *event)
{
      GtkBin *bin;
      GdkEventExpose child_event;
      
      if (GTK_WIDGET_DRAWABLE (widget)) {
          bin = GTK_BIN (widget);
          
          e_bevel_button_paint (widget, &event->area);
          
          child_event = *event;
          if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) &&
              gtk_widget_intersect (bin->child, &event->area, &child_event.area))
              gtk_widget_event (bin->child, (GdkEvent*) &child_event);
      }

      return FALSE;
}

static void
e_bevel_button_draw (GtkWidget *widget, GdkRectangle *area)
{
    GdkRectangle child_area;
    GdkRectangle tmp_area;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (E_IS_BEVEL_BUTTON (widget));
    g_return_if_fail (area != NULL);

    if (GTK_WIDGET_DRAWABLE (widget)) {
        tmp_area = *area;
        tmp_area.x -= GTK_CONTAINER (widget)->border_width;
        tmp_area.y -= GTK_CONTAINER (widget)->border_width;

        e_bevel_button_paint (widget, &tmp_area);

        if (GTK_BIN (widget)->child && gtk_widget_intersect (GTK_BIN (widget)->child, &tmp_area, &child_area))
            gtk_widget_draw (GTK_BIN (widget)->child, &child_area);
    }
}

static void
e_bevel_button_realize (GtkWidget *widget)
{
    EBevelButton *button = E_BEVEL_BUTTON (widget);

    GTK_WIDGET_CLASS (parent_class)->realize (widget);

    button->priv->gc = gdk_gc_new (widget->window);

    gdk_color_parse ("#d0d888", &button->priv->base_color);
    e_bevel_button_util_shade (&button->priv->base_color,
                   &button->priv->light_color,
                   LIGHTNESS_MULT);
    e_bevel_button_util_shade (&button->priv->base_color,
                   &button->priv->dark_color,
                   DARKNESS_MULT);
    gdk_colormap_alloc_color (gdk_rgb_get_cmap (), &button->priv->base_color, FALSE, TRUE);
    gdk_colormap_alloc_color (gdk_rgb_get_cmap (), &button->priv->light_color, FALSE, TRUE);
    gdk_colormap_alloc_color (gdk_rgb_get_cmap (), &button->priv->dark_color, FALSE, TRUE);
}

static void
e_bevel_button_class_init (EBevelButtonClass *klass)
{
    GtkWidgetClass *widget_class;

    widget_class = (GtkWidgetClass *)klass;

    parent_class = gtk_type_class (PARENT_TYPE);
    
    widget_class->draw = e_bevel_button_draw;
    widget_class->expose_event = e_bevel_button_expose;
    widget_class->realize = e_bevel_button_realize;
}

static void
e_bevel_button_init (EBevelButton *button)
{
    EBevelButtonPrivate *priv;

    GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_FOCUS);

    priv = g_new (EBevelButtonPrivate, 1);

    button->priv = priv;
}

GtkWidget *
e_bevel_button_new (void)
{
    EBevelButton *button;

    button = gtk_type_new (E_TYPE_BEVEL_BUTTON);

    return GTK_WIDGET (button);
}

E_MAKE_TYPE (e_bevel_button, "EBevelButton", EBevelButton, e_bevel_button_class_init, e_bevel_button_init, PARENT_TYPE);