libgimpwidgets: ports GimpColorSelect marker drawing to cairo

This commit is contained in:
Michael Natterer 2010-09-11 22:24:08 +02:00
parent 251177f18d
commit 1770615125
1 changed files with 53 additions and 130 deletions

View File

@ -116,7 +116,6 @@ struct _GimpColorSelect
ColorSelectFillType z_color_fill; ColorSelectFillType z_color_fill;
ColorSelectFillType xy_color_fill; ColorSelectFillType xy_color_fill;
GdkGC *gc;
ColorSelectDragMode drag_mode; ColorSelectDragMode drag_mode;
}; };
@ -144,8 +143,6 @@ struct _ColorSelectFill
}; };
static void gimp_color_select_unrealize (GtkWidget *widget);
static void gimp_color_select_togg_visible (GimpColorSelector *selector, static void gimp_color_select_togg_visible (GimpColorSelector *selector,
gboolean visible); gboolean visible);
static void gimp_color_select_togg_sensitive (GimpColorSelector *selector, static void gimp_color_select_togg_sensitive (GimpColorSelector *selector,
@ -196,11 +193,6 @@ static void gimp_color_select_image_fill (GtkWidget *widget,
const GimpHSV *hsv, const GimpHSV *hsv,
const GimpRGB *rgb); const GimpRGB *rgb);
static void gimp_color_select_draw_z_marker (GimpColorSelect *select,
GdkRectangle *clip);
static void gimp_color_select_draw_xy_marker (GimpColorSelect *select,
GdkRectangle *clip);
static void color_select_update_red (ColorSelectFill *csf); static void color_select_update_red (ColorSelectFill *csf);
static void color_select_update_green (ColorSelectFill *csf); static void color_select_update_green (ColorSelectFill *csf);
static void color_select_update_blue (ColorSelectFill *csf); static void color_select_update_blue (ColorSelectFill *csf);
@ -240,11 +232,8 @@ static const ColorSelectFillUpdateProc update_procs[] =
static void static void
gimp_color_select_class_init (GimpColorSelectClass *klass) gimp_color_select_class_init (GimpColorSelectClass *klass)
{ {
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass); GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
widget_class->unrealize = gimp_color_select_unrealize;
selector_class->name = "GIMP"; selector_class->name = "GIMP";
selector_class->help_id = "gimp-colorselector-gimp"; selector_class->help_id = "gimp-colorselector-gimp";
selector_class->stock_id = GIMP_STOCK_WILBER; selector_class->stock_id = GIMP_STOCK_WILBER;
@ -262,7 +251,6 @@ gimp_color_select_init (GimpColorSelect *select)
select->z_color_fill = COLOR_SELECT_HUE; select->z_color_fill = COLOR_SELECT_HUE;
select->xy_color_fill = COLOR_SELECT_SATURATION_VALUE; select->xy_color_fill = COLOR_SELECT_SATURATION_VALUE;
select->gc = NULL;
select->drag_mode = DRAG_NONE; select->drag_mode = DRAG_NONE;
hbox = gtk_hbox_new (FALSE, 4); hbox = gtk_hbox_new (FALSE, 4);
@ -367,20 +355,6 @@ gimp_color_select_init (GimpColorSelect *select)
} }
} }
static void
gimp_color_select_unrealize (GtkWidget *widget)
{
GimpColorSelect *select = GIMP_COLOR_SELECT (widget);
if (select->gc)
{
g_object_unref (select->gc);
select->gc = NULL;
}
GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}
static void static void
gimp_color_select_togg_visible (GimpColorSelector *selector, gimp_color_select_togg_visible (GimpColorSelector *selector,
gboolean visible) gboolean visible)
@ -644,10 +618,34 @@ gimp_color_select_xy_expose (GtkWidget *widget,
GdkEventExpose *event, GdkEventExpose *event,
GimpColorSelect *select) GimpColorSelect *select)
{ {
if (! select->gc) GtkAllocation allocation;
select->gc = gdk_gc_new (gtk_widget_get_window (widget)); cairo_t *cr;
gint x, y;
gimp_color_select_draw_xy_marker (select, &event->area); gtk_widget_get_allocation (select->xy_color, &allocation);
cr = gdk_cairo_create (gtk_widget_get_window (widget));
gdk_cairo_region (cr, event->region);
cairo_clip (cr);
x = ((allocation.width - 1) * select->pos[0]) / 255;
y = (allocation.height - 1) - ((allocation.height - 1) * select->pos[1]) / 255;
cairo_move_to (cr, 0, y + 0.5);
cairo_line_to (cr, allocation.width, y + 0.5);
cairo_move_to (cr, x + 0.5, 0);
cairo_line_to (cr, x + 0.5, allocation.height);
cairo_set_line_width (cr, 3.0);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.6);
cairo_stroke_preserve (cr);
cairo_set_line_width (cr, 1.0);
cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.8);
cairo_stroke (cr);
cairo_destroy (cr);
return TRUE; return TRUE;
} }
@ -713,8 +711,6 @@ gimp_color_select_xy_events (GtkWidget *widget,
return FALSE; return FALSE;
} }
gimp_color_select_draw_xy_marker (select, NULL);
gtk_widget_get_allocation (select->xy_color, &allocation); gtk_widget_get_allocation (select->xy_color, &allocation);
if (allocation.width > 1 && allocation.height > 1) if (allocation.width > 1 && allocation.height > 1)
@ -726,7 +722,7 @@ gimp_color_select_xy_events (GtkWidget *widget,
select->pos[0] = CLAMP (select->pos[0], 0, 255); select->pos[0] = CLAMP (select->pos[0], 0, 255);
select->pos[1] = CLAMP (select->pos[1], 0, 255); select->pos[1] = CLAMP (select->pos[1], 0, 255);
gimp_color_select_draw_xy_marker (select, NULL); gtk_widget_queue_draw (select->xy_color);
gimp_color_select_update (select, UPDATE_VALUES | UPDATE_CALLER); gimp_color_select_update (select, UPDATE_VALUES | UPDATE_CALLER);
@ -749,10 +745,30 @@ gimp_color_select_z_expose (GtkWidget *widget,
GdkEventExpose *event, GdkEventExpose *event,
GimpColorSelect *select) GimpColorSelect *select)
{ {
if (! select->gc) GtkAllocation allocation;
select->gc = gdk_gc_new (gtk_widget_get_window (widget)); cairo_t *cr;
gint y;
gimp_color_select_draw_z_marker (select, &event->area); gtk_widget_get_allocation (widget, &allocation);
cr = gdk_cairo_create (gtk_widget_get_window (widget));
gdk_cairo_region (cr, event->region);
cairo_clip (cr);
y = (allocation.height - 1) - ((allocation.height - 1) * select->pos[2]) / 255;
cairo_move_to (cr, 0, y + 0.5);
cairo_line_to (cr, allocation.width, y + 0.5);
cairo_set_line_width (cr, 3.0);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.6);
cairo_stroke_preserve (cr);
cairo_set_line_width (cr, 1.0);
cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.8);
cairo_stroke (cr);
cairo_destroy (cr);
return TRUE; return TRUE;
} }
@ -815,8 +831,6 @@ gimp_color_select_z_events (GtkWidget *widget,
return FALSE; return FALSE;
} }
gimp_color_select_draw_z_marker (select, NULL);
gtk_widget_get_allocation (select->z_color, &allocation); gtk_widget_get_allocation (select->z_color, &allocation);
if (allocation.height > 1) if (allocation.height > 1)
@ -824,7 +838,8 @@ gimp_color_select_z_events (GtkWidget *widget,
select->pos[2] = CLAMP (select->pos[2], 0, 255); select->pos[2] = CLAMP (select->pos[2], 0, 255);
gimp_color_select_draw_z_marker (select, NULL); gtk_widget_queue_draw (select->z_color);
gimp_color_select_update (select, gimp_color_select_update (select,
UPDATE_VALUES | UPDATE_XY_COLOR | UPDATE_CALLER); UPDATE_VALUES | UPDATE_XY_COLOR | UPDATE_CALLER);
@ -868,98 +883,6 @@ gimp_color_select_image_fill (GtkWidget *preview,
} }
} }
static void
gimp_color_select_draw_z_marker (GimpColorSelect *select,
GdkRectangle *clip)
{
GtkAllocation allocation;
gint width;
gint height;
gint y;
gint minx;
gint miny;
if (! select->gc)
return;
gtk_widget_get_allocation (select->z_color, &allocation);
width = allocation.width;
height = allocation.height;
if (width < 1 || height < 1)
return;
y = (height - 1) - ((height - 1) * select->pos[2]) / 255;
minx = 0;
miny = 0;
if (clip)
{
width = MIN (width, clip->x + clip->width);
height = MIN (height, clip->y + clip->height);
minx = MAX (0, clip->x);
miny = MAX (0, clip->y);
}
if (y >= miny && y < height)
{
gdk_gc_set_function (select->gc, GDK_INVERT);
gdk_draw_line (gtk_widget_get_window (select->z_color),
select->gc, minx, y, width - 1, y);
gdk_gc_set_function (select->gc, GDK_COPY);
}
}
static void
gimp_color_select_draw_xy_marker (GimpColorSelect *select,
GdkRectangle *clip)
{
GtkAllocation allocation;
gint width;
gint height;
gint x, y;
gint minx, miny;
if (! select->gc)
return;
gtk_widget_get_allocation (select->xy_color, &allocation);
width = allocation.width;
height = allocation.height;
if (width < 1 || height < 1)
return;
x = ((width - 1) * select->pos[0]) / 255;
y = (height - 1) - ((height - 1) * select->pos[1]) / 255;
minx = 0;
miny = 0;
gdk_gc_set_function (select->gc, GDK_INVERT);
if (clip)
{
width = MIN (width, clip->x + clip->width);
height = MIN (height, clip->y + clip->height);
minx = MAX (0, clip->x);
miny = MAX (0, clip->y);
}
if (y >= miny && y < height)
gdk_draw_line (gtk_widget_get_window (select->xy_color),
select->gc, minx, y, width - 1, y);
if (x >= minx && x < width)
gdk_draw_line (gtk_widget_get_window (select->xy_color),
select->gc, x, miny, x, height - 1);
gdk_gc_set_function (select->gc, GDK_COPY);
}
static void static void
color_select_update_red (ColorSelectFill *csf) color_select_update_red (ColorSelectFill *csf)
{ {