diff options
author | Richard Hughes <richard@hughsie.com> | 2012-12-09 22:03:44 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2012-12-09 22:03:44 +0000 |
commit | eb3616a758bb16c85febc4c56390ea9f5e210c9f (patch) | |
tree | 48826604c7d185171b4c7ca7739d677f51b5d805 | |
parent | 1dc532c41119c26256f29b03abb15e5ed17fbc13 (diff) | |
download | colord-gtk-eb3616a758bb16c85febc4c56390ea9f5e210c9f.tar.gz |
Don't use a GtkImage for the sample window
This allows us to have _huge_ sample windows in the future.
-rw-r--r-- | libcolord-gtk/Makefile.am | 2 | ||||
-rw-r--r-- | libcolord-gtk/cd-sample-widget.c | 166 | ||||
-rw-r--r-- | libcolord-gtk/cd-sample-widget.h | 62 | ||||
-rw-r--r-- | libcolord-gtk/cd-sample-window.c | 53 | ||||
-rw-r--r-- | libcolord-gtk/colord-gtk.h | 1 |
5 files changed, 239 insertions, 45 deletions
diff --git a/libcolord-gtk/Makefile.am b/libcolord-gtk/Makefile.am index 537a136..4cfd1c2 100644 --- a/libcolord-gtk/Makefile.am +++ b/libcolord-gtk/Makefile.am @@ -29,11 +29,13 @@ libcolord_include_HEADERS = \ libcolordgtkbase_includedir = $(libcolord_includedir)/colord-gtk libcolordgtkbase_include_HEADERS = \ cd-version.h \ + cd-sample-widget.h \ cd-sample-window.h \ cd-window.h \ cd-window-sync.h libcolord_gtk_la_SOURCES = \ + cd-sample-widget.c \ cd-sample-window.c \ cd-window.c \ cd-window-sync.c diff --git a/libcolord-gtk/cd-sample-widget.c b/libcolord-gtk/cd-sample-widget.c new file mode 100644 index 0000000..e91e19a --- /dev/null +++ b/libcolord-gtk/cd-sample-widget.c @@ -0,0 +1,166 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2012 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include <gtk/gtk.h> +#include <colord.h> + +#include "cd-sample-widget.h" + +G_DEFINE_TYPE (CdSampleWidget, cd_sample_widget, GTK_TYPE_DRAWING_AREA); +#define CD_SAMPLE_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CD_TYPE_SAMPLE_WIDGET, CdSampleWidgetPrivate)) + +struct CdSampleWidgetPrivate +{ + cairo_t *cr; + CdColorRGB color; +}; + +enum +{ + PROP_0, + PROP_COLOR, +}; + +/** + * up_sample_get_property: + **/ +static void +up_sample_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + CdSampleWidget *sample = CD_SAMPLE_WIDGET (object); + switch (prop_id) { + case PROP_COLOR: + g_value_set_boxed (value, &sample->priv->color); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +/** + * up_sample_set_property: + **/ +static void +up_sample_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + CdColorRGB *tmp; + CdSampleWidget *sample = CD_SAMPLE_WIDGET (object); + + switch (prop_id) { + case PROP_COLOR: + tmp = g_value_get_boxed (value); + cd_color_copy_rgb (tmp, &sample->priv->color); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + + /* refresh widget */ + gtk_widget_hide (GTK_WIDGET (sample)); + gtk_widget_show (GTK_WIDGET (sample)); +} + +/** + * cd_sample_widget_draw: + **/ +static gboolean +cd_sample_widget_draw (GtkWidget *widget, cairo_t *cr) +{ + CdColorRGB *color; + CdSampleWidget *sample = CD_SAMPLE_WIDGET (widget); + GtkAllocation allocation; + + g_return_val_if_fail (CD_IS_SAMPLE_WIDGET (sample), FALSE); + + color = &sample->priv->color; + gtk_widget_get_allocation (widget, &allocation); + cairo_save (cr); + cairo_set_source_rgb (cr, color->R, color->G, color->B); + cairo_rectangle (cr, 0, 0, allocation.width, allocation.height); + cairo_fill (cr); + cairo_restore (cr); + return FALSE; +} + +/** + * cd_sample_widget_class_init: + **/ +static void +cd_sample_widget_class_init (CdSampleWidgetClass *class) +{ + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + GObjectClass *object_class = G_OBJECT_CLASS (class); + + widget_class->draw = cd_sample_widget_draw; + object_class->get_property = up_sample_get_property; + object_class->set_property = up_sample_set_property; + + g_type_class_add_private (class, sizeof (CdSampleWidgetPrivate)); + + /* properties */ + g_object_class_install_property (object_class, + PROP_COLOR, + g_param_spec_boxed ("color", NULL, NULL, + CD_TYPE_COLOR_RGB, + G_PARAM_READWRITE)); +} + +/** + * cd_sample_widget_init: + **/ +static void +cd_sample_widget_init (CdSampleWidget *sample) +{ + sample->priv = CD_SAMPLE_WIDGET_GET_PRIVATE (sample); + cd_color_set_rgb (&sample->priv->color, 1.0, 1.0, 1.0); +} + +/** + * cd_sample_widget_set_color: + * @sample: This class instance + * @color: A color + * + * Sets the color for the sample widget + **/ +void +cd_sample_widget_set_color (CdSampleWidget *sample, const CdColorRGB *color) +{ + g_return_if_fail (color != NULL); + g_return_if_fail (CD_IS_SAMPLE_WIDGET (sample)); + + /* set new color and refresh */ + cd_color_copy_rgb (color, &sample->priv->color); + gtk_widget_queue_draw (GTK_WIDGET (sample)); +} + +/** + * cd_sample_widget_new: + * Return value: A new #CdSampleWidget object. + **/ +GtkWidget * +cd_sample_widget_new (void) +{ + return g_object_new (CD_TYPE_SAMPLE_WIDGET, NULL); +} diff --git a/libcolord-gtk/cd-sample-widget.h b/libcolord-gtk/cd-sample-widget.h new file mode 100644 index 0000000..1cd2a24 --- /dev/null +++ b/libcolord-gtk/cd-sample-widget.h @@ -0,0 +1,62 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2012 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#if !defined (__COLORD_GTK_H_INSIDE__) && !defined (CD_COMPILATION) +#error "Only <colord-gtk.h> can be included directly." +#endif + +#ifndef __CD_SAMPLE_WIDGET_H__ +#define __CD_SAMPLE_WIDGET_H__ + +#include <gtk/gtk.h> + +G_BEGIN_DECLS + +#define CD_TYPE_SAMPLE_WIDGET (cd_sample_widget_get_type ()) +#define CD_SAMPLE_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CD_TYPE_SAMPLE_WIDGET, CdSampleWidget)) +#define CD_SAMPLE_WIDGET_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST ((obj), CD_SAMPLE_WIDGET, CdSampleWidgetClass)) +#define CD_IS_SAMPLE_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CD_TYPE_SAMPLE_WIDGET)) +#define CD_IS_SAMPLE_WIDGET_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE ((obj), EFF_TYPE_SAMPLE_WIDGET)) +#define CD_SAMPLE_WIDGET_GET_CLASS (G_TYPE_INSTANCE_GET_CLASS ((obj), CD_TYPE_SAMPLE_WIDGET, CdSampleWidgetClass)) + +typedef struct CdSampleWidget CdSampleWidget; +typedef struct CdSampleWidgetClass CdSampleWidgetClass; +typedef struct CdSampleWidgetPrivate CdSampleWidgetPrivate; + +struct CdSampleWidget +{ + GtkDrawingArea parent; + CdSampleWidgetPrivate *priv; +}; + +struct CdSampleWidgetClass +{ + GtkDrawingAreaClass parent_class; +}; + +GType cd_sample_widget_get_type (void); +GtkWidget *cd_sample_widget_new (void); +void cd_sample_widget_set_color (CdSampleWidget *sample_widget, + const CdColorRGB *color); + +G_END_DECLS + +#endif diff --git a/libcolord-gtk/cd-sample-window.c b/libcolord-gtk/cd-sample-window.c index 95eeb17..a30ef1f 100644 --- a/libcolord-gtk/cd-sample-window.c +++ b/libcolord-gtk/cd-sample-window.c @@ -25,6 +25,7 @@ #include <colord.h> +#include "cd-sample-widget.h" #include "cd-sample-window.h" static void cd_sample_window_finalize (GObject *object); @@ -39,7 +40,7 @@ static void cd_sample_window_finalize (GObject *object); **/ struct _CdSampleWindowPrivate { - GtkWidget *image; + GtkWidget *sample_widget; GtkWidget *progress_bar; guint pulse_id; }; @@ -100,50 +101,12 @@ void cd_sample_window_set_color (CdSampleWindow *sample_window, const CdColorRGB *color) { - GdkPixbuf *pixbuf; - gint width; - gint height; - gint i; - guchar *data; - guchar *pixels; - GtkWindow *window = GTK_WINDOW (sample_window); - - /* get the window size */ - gtk_window_get_size (window, &width, &height); - - /* if no pixbuf, create it */ g_debug ("setting RGB: %f, %f, %f", color->R, color->G, color->B); - pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (sample_window->priv->image)); - if (pixbuf == NULL) { - data = g_new0 (guchar, width * height * 3); - pixbuf = gdk_pixbuf_new_from_data (data, - GDK_COLORSPACE_RGB, - FALSE, - 8, - width, - height, - width * 3, - (GdkPixbufDestroyNotify) g_free, - NULL); - gtk_image_set_from_pixbuf (GTK_IMAGE (sample_window->priv->image), - pixbuf); - } - - /* get the pixbuf size */ - height = gdk_pixbuf_get_height (pixbuf); - width = gdk_pixbuf_get_width (pixbuf); - - /* set the pixel array */ - pixels = gdk_pixbuf_get_pixels (pixbuf); - for (i = 0; i < width * height * 3; i += 3) { - pixels[i+0] = (guchar) (color->R * 255.0f); - pixels[i+1] = (guchar) (color->G * 255.0f); - pixels[i+2] = (guchar) (color->B * 255.0f); - } + cd_sample_widget_set_color (CD_SAMPLE_WIDGET (sample_window->priv->sample_widget), color); /* force redraw */ - gtk_widget_set_visible (sample_window->priv->image, FALSE); - gtk_widget_set_visible (sample_window->priv->image, TRUE); + gtk_widget_set_visible (sample_window->priv->sample_widget, FALSE); + gtk_widget_set_visible (sample_window->priv->sample_widget, TRUE); } /** @@ -203,15 +166,15 @@ cd_sample_window_init (CdSampleWindow *sample_window) GtkWindow *window = GTK_WINDOW (sample_window); GtkWidget *vbox; sample_window->priv = CD_SAMPLE_WINDOW_GET_PRIVATE (sample_window); - sample_window->priv->image = gtk_image_new (); + sample_window->priv->sample_widget = cd_sample_widget_new (); sample_window->priv->progress_bar = gtk_progress_bar_new (); /* pack in two widgets into the window */ vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); gtk_container_add (GTK_CONTAINER (sample_window), vbox); - gtk_box_pack_start (GTK_BOX (vbox), sample_window->priv->image, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (vbox), sample_window->priv->sample_widget, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), sample_window->priv->progress_bar, FALSE, FALSE, 0); - gtk_widget_set_size_request (sample_window->priv->image, 400, 400); + gtk_widget_set_size_request (sample_window->priv->sample_widget, 400, 400); gtk_widget_show_all (vbox); /* be clever and allow the colorimeter to do it's job */ diff --git a/libcolord-gtk/colord-gtk.h b/libcolord-gtk/colord-gtk.h index 4080667..b041082 100644 --- a/libcolord-gtk/colord-gtk.h +++ b/libcolord-gtk/colord-gtk.h @@ -36,6 +36,7 @@ #include <gtk/gtk.h> #include <colord.h> +#include <colord-gtk/cd-sample-widget.h> #include <colord-gtk/cd-sample-window.h> #include <colord-gtk/cd-version.h> #include <colord-gtk/cd-window.h> |