aboutsummaryrefslogtreecommitdiffstats
path: root/libowl/owlcolourswatch.c
blob: b2eb651b963a09e6703d600316a0412171a62607 (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
/*
 * OWL (OpenedHand Widget Library) Colour Swatch.
 *
 * Author: Rob Bradford  <rob@openedhand.com>
 *
 * Copyright (C) 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 "owlcolourswatch.h"

#include <gtk/gtk.h>

G_DEFINE_TYPE (OwlColourSwatch, owl_colour_swatch, GTK_TYPE_FRAME);

#define COLOUR_SWATCH_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), OWL_TYPE_COLOUR_SWATCH, OwlColourSwatchPrivate))

typedef struct _OwlColourSwatchPrivate OwlColourSwatchPrivate;

struct _OwlColourSwatchPrivate
{
  GtkWidget *draw_area;
  GdkPixbuf *pixbuf;
  GdkGC *gc;
  guint32 colour;
};

static void
owl_colour_swatch_get_property (GObject *object, guint property_id,
                              GValue *value, GParamSpec *pspec)
{
  switch (property_id) {
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
owl_colour_swatch_set_property (GObject *object, guint property_id,
                              const GValue *value, GParamSpec *pspec)
{
  switch (property_id) {
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
owl_colour_swatch_dispose (GObject *object)
{
  if (G_OBJECT_CLASS (owl_colour_swatch_parent_class)->dispose)
    G_OBJECT_CLASS (owl_colour_swatch_parent_class)->dispose (object);
}

static void
owl_colour_swatch_finalize (GObject *object)
{
  G_OBJECT_CLASS (owl_colour_swatch_parent_class)->finalize (object);
}

static void
update_pixbuf (OwlColourSwatch *swatch)
{
  OwlColourSwatchPrivate *priv = COLOUR_SWATCH_PRIVATE (swatch);

  gint width = priv->draw_area->allocation.width;
  gint height = priv->draw_area->allocation.height;

  if (priv->pixbuf != NULL)
    g_object_unref (priv->pixbuf);

  priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
  gdk_pixbuf_fill (priv->pixbuf, priv->colour);
}

static gint
expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  OwlColourSwatch *swatch = OWL_COLOUR_SWATCH (data);
  OwlColourSwatchPrivate *priv = COLOUR_SWATCH_PRIVATE (data);

  gint width = priv->draw_area->allocation.width;
  gint height = priv->draw_area->allocation.height;

  if (priv->pixbuf == NULL ||
      width != gdk_pixbuf_get_width (priv->pixbuf) ||
      height != gdk_pixbuf_get_height (priv->pixbuf))
    {
      update_pixbuf (swatch);
    }

  if (priv->gc == NULL)
    gdk_gc_new (widget->window);

  gdk_draw_pixbuf (widget->window,
                   priv->gc,
                   priv->pixbuf,
                   event->area.x - widget->allocation.x,
                   event->area.y - widget->allocation.y,
                   event->area.x,
                   event->area.y,
                   event->area.width,
                   event->area.height,
                   GDK_RGB_DITHER_MAX,
                   event->area.x - widget->allocation.x,
                   event->area.y - widget->allocation.y);


  return FALSE;
}


static void
owl_colour_swatch_class_init (OwlColourSwatchClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (OwlColourSwatchPrivate));

  object_class->get_property = owl_colour_swatch_get_property;
  object_class->set_property = owl_colour_swatch_set_property;
  object_class->dispose = owl_colour_swatch_dispose;
  object_class->finalize = owl_colour_swatch_finalize;
}

static void
owl_colour_swatch_init (OwlColourSwatch *self)
{
  OwlColourSwatchPrivate *priv = COLOUR_SWATCH_PRIVATE (self);

  priv->draw_area = gtk_alignment_new (0.5,0.5,0,0);

  priv->gc = NULL;
  priv->pixbuf = NULL;

  g_signal_connect (priv->draw_area, "expose-event",
                    G_CALLBACK (expose_event), self);

  priv->colour = 0xffffffff;

  gtk_widget_set_size_request (GTK_WIDGET (self), 24, 24);

  gtk_container_add (GTK_CONTAINER(self), priv->draw_area);

}

GtkWidget*
owl_colour_swatch_new (void)
{
  return GTK_WIDGET(g_object_new (OWL_TYPE_COLOUR_SWATCH, NULL));
}

void
owl_colour_swatch_set_colour (OwlColourSwatch *self, guint32 colour)
{
  OwlColourSwatchPrivate *priv = COLOUR_SWATCH_PRIVATE (self);

  priv->colour = colour;
  update_pixbuf (self);

  gtk_widget_queue_draw (priv->draw_area);
}

guint32
owl_colour_swatch_get_colour (OwlColourSwatch *self)
{
  OwlColourSwatchPrivate *priv = COLOUR_SWATCH_PRIVATE (self);

  return priv->colour;
}