Commit f2aaffaf authored by Matthias Clasen's avatar Matthias Clasen

Finishing touches

Implement popups in the editor, fix window sizing, fix RTL flipping.
GtkColorPlane is now using adjustments, and GtkColorEditor is using
adjustments as its model as well.
parent cd300835
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include "gtkorientable.h" #include "gtkorientable.h"
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkintl.h" #include "gtkintl.h"
#include "gtksizegroup.h"
#include "gtkalignment.h"
struct _GtkColorChooserWidgetPrivate struct _GtkColorChooserWidgetPrivate
{ {
...@@ -45,6 +47,8 @@ struct _GtkColorChooserWidgetPrivate ...@@ -45,6 +47,8 @@ struct _GtkColorChooserWidgetPrivate
GtkColorSwatch *current; GtkColorSwatch *current;
gboolean show_alpha; gboolean show_alpha;
GtkSizeGroup *size_group;
GSettings *settings; GSettings *settings;
}; };
...@@ -184,9 +188,11 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc) ...@@ -184,9 +188,11 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
{ {
GtkWidget *grid; GtkWidget *grid;
GtkWidget *p; GtkWidget *p;
GtkWidget *alignment;
GtkWidget *button; GtkWidget *button;
GtkWidget *label; GtkWidget *label;
gint i, j; gint i, j;
gint left;
GdkRGBA color; GdkRGBA color;
GVariant *variant; GVariant *variant;
GVariantIter iter; GVariantIter iter;
...@@ -252,6 +258,8 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc) ...@@ -252,6 +258,8 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
gtk_grid_set_column_spacing (GTK_GRID (grid), 4); gtk_grid_set_column_spacing (GTK_GRID (grid), 4);
gtk_container_add (GTK_CONTAINER (cc->priv->palette), grid); gtk_container_add (GTK_CONTAINER (cc->priv->palette), grid);
left = (gtk_widget_get_direction (GTK_WIDGET (cc)) == GTK_TEXT_DIR_LTR) ? 0 : 8;
for (i = 0; i < 9; i++) for (i = 0; i < 9; i++)
{ {
gdk_rgba_parse (&color, default_grayscale[i]); gdk_rgba_parse (&color, default_grayscale[i]);
...@@ -259,9 +267,9 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc) ...@@ -259,9 +267,9 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
p = gtk_color_swatch_new (); p = gtk_color_swatch_new ();
connect_swatch_signals (p, cc); connect_swatch_signals (p, cc);
if (i == 0) if (i == left)
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 10, 1, 1, 10); gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 10, 1, 1, 10);
else if (i == 8) else if (i == (8 - left))
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 10, 10, 1); gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 10, 10, 1);
else else
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 1, 1, 1); gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 1, 1, 1);
...@@ -307,12 +315,22 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc) ...@@ -307,12 +315,22 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
if (i > 0) if (i > 0)
{ {
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 10, 10, 1); if (gtk_widget_get_direction (GTK_WIDGET (cc)) == GTK_TEXT_DIR_LTR)
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (button), 10, 1, 1, 10); {
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 1, 10, 10, 1);
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (button), 10, 1, 1, 10);
}
else
{
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (button), 1, 10, 10, 1);
gtk_color_swatch_set_corner_radii (GTK_COLOR_SWATCH (p), 10, 1, 1, 10);
}
} }
cc->priv->editor = gtk_color_editor_new (); cc->priv->editor = gtk_color_editor_new ();
gtk_container_add (GTK_CONTAINER (cc), cc->priv->editor); alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
gtk_container_add (GTK_CONTAINER (cc), alignment);
gtk_container_add (GTK_CONTAINER (alignment), cc->priv->editor);
g_settings_get (cc->priv->settings, "selected-color", "(bdddd)", g_settings_get (cc->priv->settings, "selected-color", "(bdddd)",
&selected, &selected,
...@@ -326,6 +344,10 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc) ...@@ -326,6 +344,10 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
gtk_widget_set_no_show_all (cc->priv->palette, TRUE); gtk_widget_set_no_show_all (cc->priv->palette, TRUE);
gtk_widget_set_no_show_all (cc->priv->editor, TRUE); gtk_widget_set_no_show_all (cc->priv->editor, TRUE);
cc->priv->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
gtk_size_group_add_widget (cc->priv->size_group, cc->priv->palette);
gtk_size_group_add_widget (cc->priv->size_group, alignment);
} }
static void static void
...@@ -430,6 +452,7 @@ gtk_color_chooser_widget_finalize (GObject *object) ...@@ -430,6 +452,7 @@ gtk_color_chooser_widget_finalize (GObject *object)
{ {
GtkColorChooserWidget *cc = GTK_COLOR_CHOOSER_WIDGET (object); GtkColorChooserWidget *cc = GTK_COLOR_CHOOSER_WIDGET (object);
g_object_unref (cc->priv->size_group);
g_object_unref (cc->priv->settings); g_object_unref (cc->priv->settings);
G_OBJECT_CLASS (gtk_color_chooser_widget_parent_class)->finalize (object); G_OBJECT_CLASS (gtk_color_chooser_widget_parent_class)->finalize (object);
......
This diff is collapsed.
...@@ -21,23 +21,21 @@ ...@@ -21,23 +21,21 @@
#include "gtkhsv.h" #include "gtkhsv.h"
#include "gtkcolorplane.h" #include "gtkcolorplane.h"
#include "gtkcontainer.h"
#include "gtkwindow.h"
#include "gtkbutton.h"
struct _GtkColorPlanePrivate struct _GtkColorPlanePrivate
{ {
GtkAdjustment *h_adj;
GtkAdjustment *s_adj;
GtkAdjustment *v_adj;
cairo_surface_t *surface; cairo_surface_t *surface;
gdouble h, s, v;
gint x, y; gint x, y;
gboolean in_drag; gboolean in_drag;
}; };
enum
{
CHANGED,
LAST_SIGNAL
};
guint signals[LAST_SIGNAL];
G_DEFINE_TYPE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA) G_DEFINE_TYPE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA)
static gboolean static gboolean
...@@ -118,7 +116,7 @@ create_sv_surface (GtkColorPlane *plane) ...@@ -118,7 +116,7 @@ create_sv_surface (GtkColorPlane *plane)
data = g_malloc (height * stride); data = g_malloc (height * stride);
h = plane->priv->h; h = gtk_adjustment_get_value (plane->priv->h_adj);
sf = 1.0 / (height - 1); sf = 1.0 / (height - 1);
vf = 1.0 / (width - 1); vf = 1.0 / (width - 1);
for (y = 0; y < height; y++) for (y = 0; y < height; y++)
...@@ -151,13 +149,16 @@ create_sv_surface (GtkColorPlane *plane) ...@@ -151,13 +149,16 @@ create_sv_surface (GtkColorPlane *plane)
static void static void
hsv_to_xy (GtkColorPlane *plane) hsv_to_xy (GtkColorPlane *plane)
{ {
gdouble s, v;
gint width, height; gint width, height;
width = gtk_widget_get_allocated_width (GTK_WIDGET (plane)); width = gtk_widget_get_allocated_width (GTK_WIDGET (plane));
height = gtk_widget_get_allocated_height (GTK_WIDGET (plane)); height = gtk_widget_get_allocated_height (GTK_WIDGET (plane));
plane->priv->x = CLAMP (width * plane->priv->v, 0, width - 1); s = gtk_adjustment_get_value (plane->priv->s_adj);
plane->priv->y = CLAMP (height * (1 - plane->priv->s), 0, height - 1); v = gtk_adjustment_get_value (plane->priv->v_adj);
plane->priv->x = CLAMP (width * v, 0, width - 1);
plane->priv->y = CLAMP (height * (1 - s), 0, height - 1);
} }
static gboolean static gboolean
...@@ -168,6 +169,7 @@ sv_configure (GtkWidget *widget, ...@@ -168,6 +169,7 @@ sv_configure (GtkWidget *widget,
create_sv_surface (plane); create_sv_surface (plane);
hsv_to_xy (plane); hsv_to_xy (plane);
return TRUE; return TRUE;
} }
...@@ -209,13 +211,15 @@ sv_update_color (GtkColorPlane *plane, ...@@ -209,13 +211,15 @@ sv_update_color (GtkColorPlane *plane,
gint y) gint y)
{ {
GtkWidget *widget = GTK_WIDGET (plane); GtkWidget *widget = GTK_WIDGET (plane);
gdouble s, v;
plane->priv->x = x; plane->priv->x = x;
plane->priv->y = y; plane->priv->y = y;
plane->priv->s = CLAMP (1 - y * (1.0 / gtk_widget_get_allocated_height (widget)), 0, 1); s = CLAMP (1 - y * (1.0 / gtk_widget_get_allocated_height (widget)), 0, 1);
plane->priv->v = CLAMP (x * (1.0 / gtk_widget_get_allocated_width (widget)), 0, 1); v = CLAMP (x * (1.0 / gtk_widget_get_allocated_width (widget)), 0, 1);
g_signal_emit (plane, signals[CHANGED], 0); gtk_adjustment_set_value (plane->priv->s_adj, s);
gtk_adjustment_set_value (plane->priv->v_adj, v);
gtk_widget_queue_draw (widget); gtk_widget_queue_draw (widget);
} }
...@@ -225,6 +229,15 @@ sv_button_press (GtkWidget *widget, ...@@ -225,6 +229,15 @@ sv_button_press (GtkWidget *widget,
{ {
GtkColorPlane *plane = GTK_COLOR_PLANE (widget); GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
if (event->button == GDK_BUTTON_SECONDARY)
{
gboolean handled;
g_signal_emit_by_name (widget, "popup-menu", &handled);
return TRUE;
}
if (plane->priv->in_drag || event->button != GDK_BUTTON_PRIMARY) if (plane->priv->in_drag || event->button != GDK_BUTTON_PRIMARY)
return FALSE; return FALSE;
...@@ -268,53 +281,70 @@ sv_motion (GtkWidget *widget, ...@@ -268,53 +281,70 @@ sv_motion (GtkWidget *widget,
return TRUE; return TRUE;
} }
static void
h_changed (GtkColorPlane *plane)
{
create_sv_surface (plane);
gtk_widget_queue_draw (GTK_WIDGET (plane));
}
static void
sv_changed (GtkColorPlane *plane)
{
hsv_to_xy (plane);
gtk_widget_queue_draw (GTK_WIDGET (plane));
}
static void static void
sv_move (GtkColorPlane *plane, sv_move (GtkColorPlane *plane,
gdouble ds, gdouble ds,
gdouble dv) gdouble dv)
{ {
if (plane->priv->s + ds > 1) gdouble s, v;
s = gtk_adjustment_get_value (plane->priv->s_adj);
v = gtk_adjustment_get_value (plane->priv->v_adj);
if (s + ds > 1)
{ {
if (plane->priv->s < 1) if (s < 1)
plane->priv->s = 1; s = 1;
else else
goto error; goto error;
} }
else if (plane->priv->s + ds < 0) else if (s + ds < 0)
{ {
if (plane->priv->s > 0) if (s > 0)
plane->priv->s = 0; s = 0;
else else
goto error; goto error;
} }
else else
{ {
plane->priv->s += ds; s += ds;
} }
if (plane->priv->v + dv > 1) if (v + dv > 1)
{ {
if (plane->priv->v < 1) if (v < 1)
plane->priv->v = 1; v = 1;
else else
goto error; goto error;
} }
else if (plane->priv->v + dv < 0) else if (v + dv < 0)
{ {
if (plane->priv->v > 0) if (v > 0)
plane->priv->v = 0; v = 0;
else else
goto error; goto error;
} }
else else
{ {
plane->priv->v += dv; v += dv;
} }
hsv_to_xy (plane); gtk_adjustment_set_value (plane->priv->s_adj, s);
g_signal_emit (plane, signals[CHANGED], 0); gtk_adjustment_set_value (plane->priv->v_adj, v);
gtk_widget_queue_draw (GTK_WIDGET (plane));
return; return;
error: error:
...@@ -347,7 +377,7 @@ sv_key_press (GtkWidget *widget, ...@@ -347,7 +377,7 @@ sv_key_press (GtkWidget *widget,
event->keyval == GDK_KEY_KP_Right) event->keyval == GDK_KEY_KP_Right)
sv_move (plane, 0, step); sv_move (plane, 0, step);
else else
return FALSE; return GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->key_press_event (widget, event);
return TRUE; return TRUE;
} }
...@@ -371,6 +401,9 @@ sv_finalize (GObject *object) ...@@ -371,6 +401,9 @@ sv_finalize (GObject *object)
GtkColorPlane *plane = GTK_COLOR_PLANE (object); GtkColorPlane *plane = GTK_COLOR_PLANE (object);
cairo_surface_destroy (plane->priv->surface); cairo_surface_destroy (plane->priv->surface);
g_clear_object (&plane->priv->h_adj);
g_clear_object (&plane->priv->s_adj);
g_clear_object (&plane->priv->v_adj);
G_OBJECT_CLASS (gtk_color_plane_parent_class)->finalize (object); G_OBJECT_CLASS (gtk_color_plane_parent_class)->finalize (object);
} }
...@@ -391,65 +424,28 @@ gtk_color_plane_class_init (GtkColorPlaneClass *class) ...@@ -391,65 +424,28 @@ gtk_color_plane_class_init (GtkColorPlaneClass *class)
widget_class->grab_broken_event = sv_grab_broken; widget_class->grab_broken_event = sv_grab_broken;
widget_class->key_press_event = sv_key_press; widget_class->key_press_event = sv_key_press;
signals[CHANGED] =
g_signal_new ("changed",
GTK_TYPE_COLOR_PLANE,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkColorPlaneClass, changed),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
g_type_class_add_private (class, sizeof (GtkColorPlanePrivate)); g_type_class_add_private (class, sizeof (GtkColorPlanePrivate));
} }
gdouble GtkWidget *
gtk_color_plane_get_h (GtkColorPlane *plane) gtk_color_plane_new (GtkAdjustment *h_adj,
{ GtkAdjustment *s_adj,
return plane->priv->h; GtkAdjustment *v_adj)
}
gdouble
gtk_color_plane_get_s (GtkColorPlane *plane)
{
return plane->priv->s;
}
gdouble
gtk_color_plane_get_v (GtkColorPlane *plane)
{ {
return plane->priv->v; GtkColorPlane *plane;
}
void plane = (GtkColorPlane *) g_object_new (GTK_TYPE_COLOR_PLANE, NULL);
gtk_color_plane_set_h (GtkColorPlane *plane,
gdouble h)
{
plane->priv->h = h;
create_sv_surface (plane);
gtk_widget_queue_draw (GTK_WIDGET (plane));
}
void
gtk_color_plane_set_s (GtkColorPlane *plane,
gdouble s)
{
plane->priv->s = s;
hsv_to_xy (plane);
gtk_widget_queue_draw (GTK_WIDGET (plane));
}
void plane->priv->h_adj = g_object_ref_sink (h_adj);
gtk_color_plane_set_v (GtkColorPlane *plane, plane->priv->s_adj = g_object_ref_sink (s_adj);
gdouble v) plane->priv->v_adj = g_object_ref_sink (v_adj);
{ g_signal_connect_swapped (plane->priv->h_adj, "value-changed",
plane->priv->v = v; G_CALLBACK (h_changed), plane);
hsv_to_xy (plane); g_signal_connect_swapped (plane->priv->s_adj, "value-changed",
gtk_widget_queue_draw (GTK_WIDGET (plane)); G_CALLBACK (sv_changed), plane);
} g_signal_connect_swapped (plane->priv->v_adj, "value-changed",
G_CALLBACK (sv_changed), plane);
GtkWidget * return (GtkWidget *)plane;
gtk_color_plane_new (void)
{
return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_PLANE, NULL);
} }
...@@ -51,8 +51,6 @@ struct _GtkColorPlaneClass ...@@ -51,8 +51,6 @@ struct _GtkColorPlaneClass
{ {
GtkDrawingAreaClass parent_class; GtkDrawingAreaClass parent_class;
void (* changed) (GtkColorPlane *plane);
/* Padding for future expansion */ /* Padding for future expansion */
void (*_gtk_reserved1) (void); void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void); void (*_gtk_reserved2) (void);
...@@ -63,17 +61,9 @@ struct _GtkColorPlaneClass ...@@ -63,17 +61,9 @@ struct _GtkColorPlaneClass
GType gtk_color_plane_get_type (void) G_GNUC_CONST; GType gtk_color_plane_get_type (void) G_GNUC_CONST;
GtkWidget * gtk_color_plane_new (void); GtkWidget * gtk_color_plane_new (GtkAdjustment *h_adj,
GtkAdjustment *s_adj,
gdouble gtk_color_plane_get_h (GtkColorPlane *plane); GtkAdjustment *v_adj);
void gtk_color_plane_set_h (GtkColorPlane *plane,
gdouble h);
gdouble gtk_color_plane_get_s (GtkColorPlane *plane);
void gtk_color_plane_set_s (GtkColorPlane *plane,
gdouble s);
gdouble gtk_color_plane_get_v (GtkColorPlane *plane);
void gtk_color_plane_set_v (GtkColorPlane *plane,
gdouble v);
G_END_DECLS G_END_DECLS
......
...@@ -232,9 +232,16 @@ scale_draw (GtkWidget *widget, ...@@ -232,9 +232,16 @@ scale_draw (GtkWidget *widget,
if (scale_has_asymmetric_thumb (widget)) if (scale_has_asymmetric_thumb (widget))
{ {
if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL) if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
cairo_rectangle (cr, width / 2, 1, width / 2 - 1, height - 2); {
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
cairo_rectangle (cr, 1, 1, width / 2 - 1, height - 2);
else
cairo_rectangle (cr, width / 2, 1, width / 2 - 1, height - 2);
}
else else
cairo_rectangle (cr, 1, 1, width - 2, height / 2); {
cairo_rectangle (cr, 1, 1, width - 2, height / 2);
}
} }
else else
cairo_rectangle (cr, 1, 1, width - 2, height - 2); cairo_rectangle (cr, 1, 1, width - 2, height - 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment