From: Jim Evins Date: Tue, 1 Sep 2009 02:25:51 +0000 (+0000) Subject: 2009-08-31 Jim Evins X-Git-Tag: glabels-2_3_0~207 X-Git-Url: https://git.sur5r.net/?p=glabels;a=commitdiff_plain;h=d701d4f532af82224b880a18ab89cb8d6c1f65fa 2009-08-31 Jim Evins * data/builder/property-bar.builder: Make sure event box does not obscure tool bar background. * src/Makefile.am: * src/color-swatch.c: * src/color-swatch.h: New color swatch widget. * src/color-combo-color-menu-item.c: (gl_color_combo_color_menu_item_init), (gl_color_combo_color_menu_item_new), (gl_color_combo_color_menu_item_set_color): * src/color-combo.c: (gl_color_combo_init), (gl_color_combo_finalize), (gl_color_combo_new), (gl_color_combo_set_color), (menu_color_changed_cb): Updated to use new color swatch widget. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@863 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- diff --git a/glabels2/ChangeLog b/glabels2/ChangeLog index 42267793..2f3fb912 100644 --- a/glabels2/ChangeLog +++ b/glabels2/ChangeLog @@ -1,3 +1,20 @@ +2009-08-31 Jim Evins + + * data/builder/property-bar.builder: + Make sure event box does not obscure tool bar background. + * src/Makefile.am: + * src/color-swatch.c: + * src/color-swatch.h: + New color swatch widget. + * src/color-combo-color-menu-item.c: + (gl_color_combo_color_menu_item_init), + (gl_color_combo_color_menu_item_new), + (gl_color_combo_color_menu_item_set_color): + * src/color-combo.c: (gl_color_combo_init), + (gl_color_combo_finalize), (gl_color_combo_new), + (gl_color_combo_set_color), (menu_color_changed_cb): + Updated to use new color swatch widget. + 2009-08-26 Jim Evins * po/POTFILES.in: diff --git a/glabels2/data/builder/property-bar.builder b/glabels2/data/builder/property-bar.builder index 74a2f8f3..3fd4f0ac 100644 --- a/glabels2/data/builder/property-bar.builder +++ b/glabels2/data/builder/property-bar.builder @@ -146,6 +146,7 @@ True Text color + False @@ -163,6 +164,7 @@ True Fill color + False @@ -180,6 +182,7 @@ True Line color + False diff --git a/glabels2/src/Makefile.am b/glabels2/src/Makefile.am index c848a5f0..da5c4de8 100644 --- a/glabels2/src/Makefile.am +++ b/glabels2/src/Makefile.am @@ -187,6 +187,8 @@ glabels_SOURCES = \ color-combo-color-menu-item.h \ color-combo-history.c \ color-combo-history.h \ + color-swatch.c \ + color-swatch.h \ cairo-label-path.c \ cairo-label-path.h \ cairo-markup-path.c \ diff --git a/glabels2/src/color-combo-color-menu-item.c b/glabels2/src/color-combo-color-menu-item.c index 742b3de5..9fc6c79d 100644 --- a/glabels2/src/color-combo-color-menu-item.c +++ b/glabels2/src/color-combo-color-menu-item.c @@ -27,9 +27,9 @@ #include #include #include -#include +#include "color-swatch.h" #include "color.h" -#include "util.h" + /*===========================================*/ @@ -37,7 +37,6 @@ /*===========================================*/ #define SIZE 20 -#define PAD 5 /*===========================================*/ @@ -46,7 +45,9 @@ struct _glColorComboColorMenuItemPrivate { - gint id; + gint id; + + GtkWidget *swatch; }; @@ -95,8 +96,6 @@ static void gl_color_combo_color_menu_item_init (glColorComboColorMenuItem *this) { this->priv = g_new0 (glColorComboColorMenuItemPrivate, 1); - - gtk_widget_set_size_request (GTK_MENU_ITEM (this), SIZE + 2*PAD, SIZE + 2*PAD); } @@ -126,15 +125,13 @@ gl_color_combo_color_menu_item_new (gint id, const gchar *tip) { glColorComboColorMenuItem *this; - GdkPixbuf *pixbuf; this = g_object_new (GL_TYPE_COLOR_COMBO_COLOR_MENU_ITEM, NULL); this->priv->id = id; - pixbuf = create_color_pixbuf (SIZE, SIZE, color); - gtk_container_add (GTK_CONTAINER (this), - gtk_image_new_from_pixbuf (pixbuf)); + this->priv->swatch = gl_color_swatch_new (SIZE, SIZE, color); + gtk_container_add (GTK_CONTAINER (this), this->priv->swatch); gtk_widget_set_tooltip_text (GTK_WIDGET (this), tip); @@ -151,60 +148,11 @@ gl_color_combo_color_menu_item_set_color(glColorComboColorMenuItem *this, guint color, const gchar *tip) { - GdkPixbuf *pixbuf; - this->priv->id = id; - pixbuf = create_color_pixbuf (SIZE, SIZE, color); - gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (this))), - pixbuf); + gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color); gtk_widget_set_tooltip_text (GTK_WIDGET (this), tip); - } - - -/*****************************************************************************/ -/* Create new pixbuf with color preview. */ -/*****************************************************************************/ -static GdkPixbuf * -create_color_pixbuf (gdouble w, - gdouble h, - guint color) -{ - cairo_surface_t *surface; - cairo_t *cr; - GdkPixbuf *pixbuf; - - surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h); - - cr = cairo_create (surface); - - cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); - - cairo_save (cr); - cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); - cairo_paint (cr); - cairo_restore (cr); - - cairo_rectangle( cr, 2, 2, w-3, h-3 ); - - cairo_set_source_rgba (cr, - GL_COLOR_F_RED (color), - GL_COLOR_F_GREEN (color), - GL_COLOR_F_BLUE (color), - GL_COLOR_F_ALPHA (color)); - cairo_fill_preserve( cr ); - - cairo_set_line_width (cr, 1.0); - cairo_set_source_rgb (cr, 0.5, 0.5, 0.5); - cairo_stroke (cr); - - cairo_destroy( cr ); - - pixbuf = gl_util_cairo_convert_to_pixbuf (surface); - cairo_surface_destroy (surface); - - return pixbuf; } diff --git a/glabels2/src/color-combo.c b/glabels2/src/color-combo.c index c98b8b61..f296f51e 100644 --- a/glabels2/src/color-combo.c +++ b/glabels2/src/color-combo.c @@ -1,6 +1,6 @@ /* * color-combo.c - * Copyright (C) 2008 Jim Evins . + * Copyright (C) 2008-2009 Jim Evins . * * This file is part of gLabels. * @@ -26,16 +26,18 @@ #include #include #include -#include +#include "color-swatch.h" #include #include "marshal.h" -#include "util.h" #include "color.h" #define IMAGE_W 24 #define IMAGE_H 24 +#define SWATCH_H 5 + + /*========================================================*/ /* Private types. */ /*========================================================*/ @@ -48,10 +50,9 @@ struct _glColorComboPrivate { guint default_color; - GdkPixbuf *icon; - GtkWidget *button; - GtkWidget *button_image; + GtkWidget *button_vbox; + GtkWidget *swatch; GtkWidget *dropdown_button; GtkWidget *menu; @@ -135,17 +136,16 @@ gl_color_combo_class_init (glColorComboClass *class) static void gl_color_combo_init (glColorCombo *this) { - GtkWidget *button_vbox; GtkWidget *arrow; gtk_box_set_spacing (GTK_BOX (this), 0); this->priv = g_new0 (glColorComboPrivate, 1); + this->priv->button_vbox = gtk_vbox_new (FALSE, 0); + this->priv->button = gtk_button_new (); - this->priv->button_image = gtk_image_new (); - gtk_button_set_image (GTK_BUTTON (this->priv->button), - this->priv->button_image); + gtk_container_add (GTK_CONTAINER (this->priv->button), this->priv->button_vbox); gtk_button_set_focus_on_click (GTK_BUTTON (this->priv->button), FALSE); g_signal_connect_swapped (this->priv->button, "clicked", G_CALLBACK(button_clicked_cb), this); @@ -175,11 +175,6 @@ gl_color_combo_finalize (GObject *object) g_return_if_fail (object && IS_GL_COLOR_COMBO (object)); this = GL_COLOR_COMBO (object); - if (this->priv->icon) - { - g_object_unref (this->priv->icon); - } - g_free (this->priv); G_OBJECT_CLASS (gl_color_combo_parent_class)->finalize (object); @@ -196,15 +191,11 @@ gl_color_combo_new (GdkPixbuf *icon, guint color) { glColorCombo *this; - GtkWidget *separator_menuitem; + GdkPixbuf *pixbuf; + GtkWidget *wimage; this = g_object_new (TYPE_GL_COLOR_COMBO, NULL); - if (icon) - { - this->priv->icon = g_object_ref (icon); - } - if (!default_label) { default_label = _("Default Color"); @@ -213,13 +204,25 @@ gl_color_combo_new (GdkPixbuf *icon, this->priv->default_color = default_color; this->priv->color = color; + if (icon) + { + pixbuf = gdk_pixbuf_new_subpixbuf (icon, 0, 0, IMAGE_W, IMAGE_H-SWATCH_H); + wimage = gtk_image_new_from_pixbuf (pixbuf); + g_object_unref (G_OBJECT (pixbuf)); + gtk_box_pack_start (GTK_BOX (this->priv->button_vbox), wimage, FALSE, FALSE, 0); + + this->priv->swatch = gl_color_swatch_new (IMAGE_W, SWATCH_H, color); + } + else + { + this->priv->swatch = gl_color_swatch_new (IMAGE_W, IMAGE_H, color); + } + gtk_box_pack_start (GTK_BOX (this->priv->button_vbox), this->priv->swatch, FALSE, FALSE, 0); + this->priv->menu = gl_color_combo_menu_new (default_label, color); gtk_widget_show_all (this->priv->menu); - gtk_image_set_from_pixbuf (GTK_IMAGE (this->priv->button_image), - create_pixbuf (this, IMAGE_W, IMAGE_H)); - g_signal_connect (this->priv->menu, "color_changed", G_CALLBACK (menu_color_changed_cb), this); @@ -236,8 +239,7 @@ gl_color_combo_set_color (glColorCombo *this, { this->priv->color = color; - gtk_image_set_from_pixbuf (GTK_IMAGE (this->priv->button_image), - create_pixbuf (this, IMAGE_W, IMAGE_H)); + gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color); } @@ -289,74 +291,6 @@ button_clicked_cb( glColorCombo *this ) } -/*****************************************************************************/ -/* Create new pixbuf with color preview. */ -/*****************************************************************************/ -static GdkPixbuf * -create_pixbuf (glColorCombo *this, - gdouble w, - gdouble h) -{ - cairo_surface_t *surface; - cairo_t *cr; - gdouble image_w, image_h; - gdouble swatch_h; - GdkPixbuf *pixbuf; - - surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h); - - cr = cairo_create (surface); - - cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); - - cairo_save (cr); - cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); - cairo_paint (cr); - cairo_restore (cr); - - if (this->priv->icon) - { - image_w = gdk_pixbuf_get_width (this->priv->icon); - image_h = gdk_pixbuf_get_height (this->priv->icon); - - cairo_save (cr); - - cairo_rectangle (cr, 0, 0, w, h); - cairo_scale (cr, w/image_w, h/image_h); - gdk_cairo_set_source_pixbuf (cr, this->priv->icon, 0, 0); - cairo_fill (cr); - - cairo_restore (cr); - - swatch_h = h/5 + 1; - } - else - { - swatch_h = h; - } - - cairo_rectangle( cr, 1, h-swatch_h+1, w-2, swatch_h-2 ); - - cairo_set_source_rgba (cr, - GL_COLOR_F_RED (this->priv->color), - GL_COLOR_F_GREEN (this->priv->color), - GL_COLOR_F_BLUE (this->priv->color), - GL_COLOR_F_ALPHA (this->priv->color)); - cairo_fill_preserve( cr ); - - cairo_set_line_width (cr, 1.0); - cairo_set_source_rgb (cr, 0.5, 0.5, 0.5); - cairo_stroke (cr); - - cairo_destroy( cr ); - - pixbuf = gl_util_cairo_convert_to_pixbuf (surface); - cairo_surface_destroy (surface); - - return pixbuf; -} - - /*****************************************************************************/ /* Menu positioning function. */ /*****************************************************************************/ @@ -443,8 +377,7 @@ menu_color_changed_cb (glColorComboMenu *object, } this->priv->is_default_flag = is_default; - gtk_image_set_from_pixbuf (GTK_IMAGE (this->priv->button_image), - create_pixbuf (this, IMAGE_W, IMAGE_H)); + gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color); g_signal_emit (this, signals[COLOR_CHANGED], 0, this->priv->color, diff --git a/glabels2/src/color-swatch.c b/glabels2/src/color-swatch.c new file mode 100644 index 00000000..b0d453e1 --- /dev/null +++ b/glabels2/src/color-swatch.c @@ -0,0 +1,242 @@ +/* + * color-swatch.c + * Copyright (C) 2009 Jim Evins . + * + * This file is part of gLabels. + * + * gLabels 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. + * + * gLabels 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 gLabels. If not, see . + */ + +#include + +#include "color-swatch.h" + +#include "marshal.h" +#include "color.h" + + +/*===========================================*/ +/* Private macros and constants. */ +/*===========================================*/ + + +/*===========================================*/ +/* Private types */ +/*===========================================*/ + +struct _glColorSwatchPrivate { + + guint color; + +}; + + +/*===========================================*/ +/* Private globals */ +/*===========================================*/ + + +/*===========================================*/ +/* Local function prototypes */ +/*===========================================*/ + +static void gl_color_swatch_finalize (GObject *object); + +static void redraw (glColorSwatch *this); + +static gboolean expose_event_cb (GtkWidget *widget, + GdkEventExpose *event); + +static void draw_swatch (glColorSwatch *this, + cairo_t *cr); + + + +/****************************************************************************/ +/* Boilerplate Object stuff. */ +/****************************************************************************/ +G_DEFINE_TYPE (glColorSwatch, gl_color_swatch, GTK_TYPE_DRAWING_AREA); + + +/*****************************************************************************/ +/* Class Init Function. */ +/*****************************************************************************/ +static void +gl_color_swatch_class_init (glColorSwatchClass *class) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + + gl_color_swatch_parent_class = g_type_class_peek_parent (class); + + gobject_class->finalize = gl_color_swatch_finalize; + + widget_class->expose_event = expose_event_cb; +} + + +/*****************************************************************************/ +/* Object Instance Init Function. */ +/*****************************************************************************/ +static void +gl_color_swatch_init (glColorSwatch *this) +{ + this->priv = g_new0 (glColorSwatchPrivate, 1); +} + + +/*****************************************************************************/ +/* Finalize Method. */ +/*****************************************************************************/ +static void +gl_color_swatch_finalize (GObject *object) +{ + glColorSwatch *this = GL_COLOR_SWATCH (object); + + g_return_if_fail (object != NULL); + g_return_if_fail (GL_IS_COLOR_SWATCH (object)); + + g_free (this->priv); + + G_OBJECT_CLASS (gl_color_swatch_parent_class)->finalize (object); +} + + +/*****************************************************************************/ +/** New Object Generator. */ +/*****************************************************************************/ +GtkWidget * +gl_color_swatch_new (gint w, + gint h, + guint color) +{ + glColorSwatch *this; + + this = g_object_new (GL_TYPE_COLOR_SWATCH, NULL); + + this->priv->color = color; + + gtk_widget_set_size_request (GTK_WIDGET (this), w, h); + + return GTK_WIDGET (this); +} + + +/*****************************************************************************/ +/* Set color. */ +/*****************************************************************************/ +void +gl_color_swatch_set_color (glColorSwatch *this, + guint color) +{ + this->priv->color = color; + + redraw (this); +} + + +/*****************************************************************************/ +/* Request redraw. */ +/*****************************************************************************/ +static void +redraw (glColorSwatch *this) +{ + GdkRegion *region; + + if (GTK_WIDGET_REALIZED (GTK_WIDGET (this))) + { + /* redraw the cairo canvas forcing an expose event */ + region = gdk_drawable_get_clip_region (GTK_WIDGET (this)->window); + gdk_window_invalidate_region (GTK_WIDGET (this)->window, region, TRUE); + gdk_region_destroy (region); + } +} + + +/*****************************************************************************/ +/* "Expose event" callback. */ +/*****************************************************************************/ +static gboolean +expose_event_cb (GtkWidget *widget, + GdkEventExpose *event) +{ + cairo_t *cr; + + cr = gdk_cairo_create (widget->window); + + cairo_rectangle (cr, + event->area.x, event->area.y, + event->area.width, event->area.height); + cairo_clip (cr); + + draw_swatch (GL_COLOR_SWATCH (widget), cr); + + cairo_destroy (cr); + + return FALSE; +} + + +/*****************************************************************************/ +/* Draw swatch. */ +/*****************************************************************************/ +static void +draw_swatch (glColorSwatch *this, + cairo_t *cr) +{ + GdkPixbuf *pixbuf; + GtkStyle *style; + gdouble w, h; + guint fill_color, line_color; + + + cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); + + + w = GTK_WIDGET (this)->allocation.width; + h = GTK_WIDGET (this)->allocation.height; + + + style = gtk_widget_get_style (GTK_WIDGET (this)); + if ( GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (this)) ) + { + fill_color = this->priv->color; + line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]); + } + else + { + fill_color = GL_COLOR_NONE; + line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]); + } + + cairo_rectangle( cr, 0, 0, w-1, h-1 ); + + cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color)); + cairo_fill_preserve( cr ); + + cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color)); + cairo_set_line_width (cr, 1.0); + cairo_stroke (cr); +} + + + +/* + * Local Variables: -- emacs + * mode: C -- emacs + * c-basic-offset: 8 -- emacs + * tab-width: 8 -- emacs + * indent-tabs-mode: nil -- emacs + * End: -- emacs + */ diff --git a/glabels2/src/color-swatch.h b/glabels2/src/color-swatch.h new file mode 100644 index 00000000..94c09a9c --- /dev/null +++ b/glabels2/src/color-swatch.h @@ -0,0 +1,78 @@ +/* + * color-swatch.h + * Copyright (C) 2009 Jim Evins . + * + * This file is part of gLabels. + * + * gLabels 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. + * + * gLabels 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 gLabels. If not, see . + */ + +#ifndef __COLOR_SWATCH_H__ +#define __COLOR_SWATCH_H__ + + +#include + + +G_BEGIN_DECLS + +#define GL_TYPE_COLOR_SWATCH (gl_color_swatch_get_type ()) +#define GL_COLOR_SWATCH(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GL_TYPE_COLOR_SWATCH, glColorSwatch )) +#define GL_COLOR_SWATCH_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), GL_TYPE_COLOR_SWATCH, glColorSwatchClass)) +#define GL_IS_COLOR_SWATCH(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GL_TYPE_COLOR_SWATCH)) +#define GL_IS_COLOR_SWATCH_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), GL_TYPE_COLOR_SWATCH)) + +typedef struct _glColorSwatch glColorSwatch; +typedef struct _glColorSwatchPrivate glColorSwatchPrivate; +typedef struct _glColorSwatchClass glColorSwatchClass; + +struct _glColorSwatch { + GtkDrawingArea parent_widget; + + glColorSwatchPrivate *priv; +}; + +struct _glColorSwatchClass { + GtkDrawingAreaClass parent_class; +}; + + +GType gl_color_swatch_get_type (void) G_GNUC_CONST; + +GtkWidget *gl_color_swatch_new (gint w, + gint h, + guint color); + +void gl_color_swatch_set_color(glColorSwatch *this, + guint color); + + +G_END_DECLS + +#endif /* __COLOR_SWATCH_H__ */ + + + +/* + * Local Variables: -- emacs + * mode: C -- emacs + * c-basic-offset: 8 -- emacs + * tab-width: 8 -- emacs + * indent-tabs-mode: nil -- emacs + * End: -- emacs + */