color editor: Add a color picker button

The button is shown if we have a GtkColorPicker implementation.
Currently, there are none, so the button is never shown.
This commit is contained in:
Matthias Clasen 2018-07-26 18:46:37 -04:00
parent 543fb6d7b6
commit 6b3272e4b0
2 changed files with 63 additions and 0 deletions

View File

@ -24,7 +24,9 @@
#include "gtkcolorscaleprivate.h"
#include "gtkcolorswatchprivate.h"
#include "gtkcolorutils.h"
#include "gtkcolorpickerprivate.h"
#include "gtkgrid.h"
#include "gtkbutton.h"
#include "gtkintl.h"
#include "gtkorientable.h"
#include "gtkentry.h"
@ -61,6 +63,9 @@ struct _GtkColorEditorPrivate
GtkAdjustment *v_adj;
GtkAdjustment *a_adj;
GtkWidget *picker_button;
GtkColorPicker *picker;
gint popup_position;
guint text_changed : 1;
@ -353,6 +358,36 @@ popup_draw (GtkWidget *popup,
return FALSE;
}
static void
color_picked (GObject *source,
GAsyncResult *res,
gpointer data)
{
GtkColorPicker *picker = GTK_COLOR_PICKER (source);
GtkColorEditor *editor = data;
GError *error = NULL;
GdkRGBA *color;
color = gtk_color_picker_pick_finish (picker, res, &error);
if (color == NULL)
{
g_warning ("Picking color failed: %s", error->message);
g_error_free (error);
}
else
{
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (editor), color);
gdk_rgba_free (color);
}
}
static void
pick_color (GtkButton *button,
GtkColorEditor *editor)
{
gtk_color_picker_pick (editor->priv->picker, color_picked, editor);
}
static void
gtk_color_editor_init (GtkColorEditor *editor)
{
@ -394,6 +429,10 @@ gtk_color_editor_init (GtkColorEditor *editor)
gtk_overlay_add_overlay (GTK_OVERLAY (editor->priv->overlay), editor->priv->a_popup);
gtk_style_context_remove_class (gtk_widget_get_style_context (editor->priv->swatch), "activatable");
editor->priv->picker = gtk_color_picker_new ();
if (editor->priv->picker == NULL)
gtk_widget_hide (editor->priv->picker_button);
}
static void
@ -402,6 +441,7 @@ gtk_color_editor_dispose (GObject *object)
GtkColorEditor *editor = GTK_COLOR_EDITOR (object);
dismiss_current_popup (editor);
g_clear_object (&editor->priv->picker);
G_OBJECT_CLASS (gtk_color_editor_parent_class)->dispose (object);
}
@ -504,6 +544,7 @@ gtk_color_editor_class_init (GtkColorEditorClass *class)
gtk_widget_class_bind_template_child_private (widget_class, GtkColorEditor, s_adj);
gtk_widget_class_bind_template_child_private (widget_class, GtkColorEditor, v_adj);
gtk_widget_class_bind_template_child_private (widget_class, GtkColorEditor, a_adj);
gtk_widget_class_bind_template_child_private (widget_class, GtkColorEditor, picker_button);
gtk_widget_class_bind_template_callback (widget_class, hsv_changed);
gtk_widget_class_bind_template_callback (widget_class, popup_draw);
@ -514,6 +555,7 @@ gtk_color_editor_class_init (GtkColorEditorClass *class)
gtk_widget_class_bind_template_callback (widget_class, entry_apply);
gtk_widget_class_bind_template_callback (widget_class, entry_focus_out);
gtk_widget_class_bind_template_callback (widget_class, popup_edit);
gtk_widget_class_bind_template_callback (widget_class, pick_color);
}
static void

View File

@ -40,6 +40,27 @@
<property name="margin-end">30</property>
<property name="row-spacing">12</property>
<property name="column-spacing">12</property>
<child>
<object class="GtkButton" id="picker_button">
<property name="visible">1</property>
<property name="no-show-all">1</property>
<child>
<object class="GtkImage">
<property name="visible">1</property>
<property name="icon-name">color-select-symbolic</property>
</object>
</child>
<property name="tooltip-text" translatable="yes">Pick a color from the screen</property>
<signal name="clicked" handler="pick_color"/>
<style>
<class name="circular"/>
</style>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkColorSwatch" id="swatch">
<property name="visible">True</property>