summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimphighlightablebutton.c
blob: 41c5a44118eb426f370687b19502dac14442ebb6 (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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimphighlightablebutton.c
 * Copyright (C) 2018 Ell
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimp-cairo.h"

#include "gimphighlightablebutton.h"


#define DEFAULT_HIGHLIGHT_COLOR {0.20, 0.70, 0.20, 0.65}

#define PADDING       1
#define BORDER_WIDTH  1
#define CORNER_RADIUS 2

#define REV (2.0 * G_PI)


enum
{
  PROP_0,
  PROP_HIGHLIGHT,
  PROP_HIGHLIGHT_COLOR
};


struct _GimpHighlightableButtonPrivate
{
  gboolean highlight;
  GimpRGB  highlight_color;
};


static void       gimp_highlightable_button_set_property (GObject        *object,
                                                          guint           property_id,
                                                          const GValue   *value,
                                                          GParamSpec     *pspec);
static void       gimp_highlightable_button_get_property (GObject        *object,
                                                          guint           property_id,
                                                          GValue         *value,
                                                          GParamSpec     *pspec);

static gboolean   gimp_highlightable_button_expose_event (GtkWidget      *widget,
                                                          GdkEventExpose *event);


G_DEFINE_TYPE_WITH_PRIVATE (GimpHighlightableButton, gimp_highlightable_button,
                            GIMP_TYPE_BUTTON)

#define parent_class gimp_highlightable_button_parent_class


static void
gimp_highlightable_button_class_init (GimpHighlightableButtonClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->get_property = gimp_highlightable_button_get_property;
  object_class->set_property = gimp_highlightable_button_set_property;

  widget_class->expose_event = gimp_highlightable_button_expose_event;

  g_object_class_install_property (object_class, PROP_HIGHLIGHT,
                                   g_param_spec_boolean ("highlight",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_HIGHLIGHT_COLOR,
                                   gimp_param_spec_rgb ("highlight-color",
                                                        NULL, NULL,
                                                        TRUE,
                                                        &(GimpRGB) DEFAULT_HIGHLIGHT_COLOR,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));
}

static void
gimp_highlightable_button_init (GimpHighlightableButton *button)
{
  button->priv = gimp_highlightable_button_get_instance_private (button);
}

static void
gimp_highlightable_button_set_property (GObject      *object,
                                        guint         property_id,
                                        const GValue *value,
                                        GParamSpec   *pspec)
{
  GimpHighlightableButton *button = GIMP_HIGHLIGHTABLE_BUTTON (object);

  switch (property_id)
    {
    case PROP_HIGHLIGHT:
      gimp_highlightable_button_set_highlight (button,
                                               g_value_get_boolean (value));
      break;

    case PROP_HIGHLIGHT_COLOR:
      gimp_highlightable_button_set_highlight_color (button,
                                                     g_value_get_boxed (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_highlightable_button_get_property (GObject    *object,
                                        guint       property_id,
                                        GValue     *value,
                                        GParamSpec *pspec)
{
  GimpHighlightableButton *button = GIMP_HIGHLIGHTABLE_BUTTON (object);

  switch (property_id)
    {
    case PROP_HIGHLIGHT:
      g_value_set_boolean (value, button->priv->highlight);
      break;

    case PROP_HIGHLIGHT_COLOR:
      g_value_set_boxed (value, &button->priv->highlight_color);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static gboolean
gimp_highlightable_button_expose_event (GtkWidget      *widget,
                                        GdkEventExpose *event)
{
  GimpHighlightableButton *button = GIMP_HIGHLIGHTABLE_BUTTON (widget);

  if (button->priv->highlight)
    {
      if (gtk_widget_is_drawable (widget))
        {
          GtkStyle      *style = gtk_widget_get_style (widget);
          GtkStateType   state = gtk_widget_get_state (widget);
          GtkAllocation  allocation;
          gboolean       border;
          gdouble        lightness;
          gdouble        opacity;
          gdouble        x;
          gdouble        y;
          gdouble        width;
          gdouble        height;
          cairo_t       *cr;

          gtk_widget_get_allocation (widget, &allocation);

          border =
            (state                                       == GTK_STATE_ACTIVE   ||
             state                                       == GTK_STATE_PRELIGHT ||
             gtk_button_get_relief (GTK_BUTTON (button)) == GTK_RELIEF_NORMAL);

          lightness = 1.00;
          opacity   = 1.00;

          switch (state)
            {
            case GTK_STATE_ACTIVE:      lightness = 0.80; break;
            case GTK_STATE_PRELIGHT:    lightness = 1.25; break;
            case GTK_STATE_INSENSITIVE: opacity   = 0.50; break;
            default:                                      break;
            }

          x      = allocation.x      +       PADDING;
          y      = allocation.y      +       PADDING;
          width  = allocation.width  - 2.0 * PADDING;
          height = allocation.height - 2.0 * PADDING;

          if (border)
            {
              x      += BORDER_WIDTH / 2.0;
              y      += BORDER_WIDTH / 2.0;
              width  -= BORDER_WIDTH;
              height -= BORDER_WIDTH;
            }

          cr = gdk_cairo_create (event->window);
          gdk_cairo_region (cr, event->region);
          cairo_clip (cr);

          cairo_set_source_rgba (cr,
                                 button->priv->highlight_color.r * lightness,
                                 button->priv->highlight_color.g * lightness,
                                 button->priv->highlight_color.b * lightness,
                                 button->priv->highlight_color.a * opacity);

          gimp_cairo_rounded_rectangle (cr,
                                        x, y, width, height, CORNER_RADIUS);

          cairo_fill_preserve (cr);

          if (border)
            {
              gdk_cairo_set_source_color (cr, &style->fg[state]);

              cairo_set_line_width (cr, BORDER_WIDTH);

              cairo_stroke (cr);
            }

          cairo_destroy (cr);

          if (gtk_widget_has_focus (widget))
            {
              gboolean interior_focus;
              gint     focus_width;
              gint     focus_pad;
              gint     child_displacement_x;
              gint     child_displacement_y;
              gboolean displace_focus;
              gint     x;
              gint     y;
              gint     width;
              gint     height;

              gtk_widget_style_get (widget,
                                    "interior-focus",       &interior_focus,
                                    "focus-line-width",     &focus_width,
                                    "focus-padding",        &focus_pad,
                                    "child-displacement-y", &child_displacement_y,
                                    "child-displacement-x", &child_displacement_x,
                                    "displace-focus",       &displace_focus,
                                    NULL);

              x      = allocation.x      +     PADDING;
              y      = allocation.y      +     PADDING;
              width  = allocation.width  - 2 * PADDING;
              height = allocation.height - 2 * PADDING;

              if (interior_focus)
                {
                  x      += style->xthickness + focus_pad;
                  y      += style->ythickness + focus_pad;
                  width  -= 2 * (style->xthickness + focus_pad);
                  height -= 2 * (style->ythickness + focus_pad);
                }
              else
                {
                  x      -= focus_width + focus_pad;
                  y      -= focus_width + focus_pad;
                  width  += 2 * (focus_width + focus_pad);
                  height += 2 * (focus_width + focus_pad);
                }

              if (state == GTK_STATE_ACTIVE && displace_focus)
                {
                  x += child_displacement_x;
                  y += child_displacement_y;
                }

              gtk_paint_focus (style, gtk_widget_get_window (widget), state,
                               &event->area, widget, "button",
                               x, y, width, height);
            }

          gtk_container_propagate_expose (GTK_CONTAINER (button),
                                          gtk_bin_get_child (GTK_BIN (button)),
                                          event);
        }

      return FALSE;
    }
  else
    {
      return GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
    }
}


/*  public functions  */


GtkWidget *
gimp_highlightable_button_new (void)
{
  return g_object_new (GIMP_TYPE_HIGHLIGHTABLE_BUTTON, NULL);
}

void
gimp_highlightable_button_set_highlight (GimpHighlightableButton *button,
                                         gboolean                 highlight)
{
  g_return_if_fail (GIMP_IS_HIGHLIGHTABLE_BUTTON (button));

  if (button->priv->highlight != highlight)
    {
      button->priv->highlight = highlight;

      gtk_widget_queue_draw (GTK_WIDGET (button));

      g_object_notify (G_OBJECT (button), "highlight");
    }
}

gboolean
gimp_highlightable_button_get_highlight (GimpHighlightableButton *button)
{
  g_return_val_if_fail (GIMP_IS_HIGHLIGHTABLE_BUTTON (button), FALSE);

  return button->priv->highlight;
}

void
gimp_highlightable_button_set_highlight_color (GimpHighlightableButton *button,
                                               const GimpRGB           *color)
{
  g_return_if_fail (GIMP_IS_HIGHLIGHTABLE_BUTTON (button));
  g_return_if_fail (color != NULL);

  if (memcmp (&button->priv->highlight_color, color, sizeof (GimpRGB)))
    {
      button->priv->highlight_color = *color;

      if (button->priv->highlight)
        gtk_widget_queue_draw (GTK_WIDGET (button));

      g_object_notify (G_OBJECT (button), "highlight-color");
    }
}

void
gimp_highlightable_button_get_highlight_color (GimpHighlightableButton *button,
                                               GimpRGB                 *color)
{
  g_return_if_fail (GIMP_IS_HIGHLIGHTABLE_BUTTON (button));
  g_return_if_fail (color != NULL);

  *color = button->priv->highlight_color;
}