app: use the new GimpColormapSelection in GimpColormapEditor.

Code from GimpColormapSelection is mostly coming from GimpColormapEditor
anyway, except that it is used elsewhere as well.
This commit is contained in:
Jehan 2019-03-17 23:13:34 +01:00
parent 94043529c8
commit 8ab04a2a3a
3 changed files with 78 additions and 501 deletions

View file

@ -28,6 +28,7 @@
#include "core/gimpimage-colormap.h"
#include "widgets/gimpcolormapeditor.h"
#include "widgets/gimpcolormapselection.h"
#include "actions.h"
#include "colormap-commands.h"
@ -73,19 +74,23 @@ colormap_to_selection_cmd_callback (GtkAction *action,
gint value,
gpointer data)
{
GimpColormapEditor *editor;
GimpImage *image;
GimpChannelOps op;
GimpColormapSelection *selection;
GimpColormapEditor *editor;
GimpImage *image;
GimpChannelOps op;
gint col_index;
return_if_no_image (image, data);
editor = GIMP_COLORMAP_EDITOR (data);
editor = GIMP_COLORMAP_EDITOR (data);
selection = GIMP_COLORMAP_SELECTION (editor->selection);
col_index = gimp_colormap_selection_get_index (selection, NULL);
op = (GimpChannelOps) value;
gimp_channel_select_by_index (gimp_image_get_mask (image),
gimp_image_get_active_drawable (image),
editor->col_index,
op,
col_index, op,
FALSE, 0.0, 0.0);
gimp_image_flush (image);

View file

@ -37,6 +37,7 @@
#include "gimpcolordialog.h"
#include "gimpcolormapeditor.h"
#include "gimpcolormapselection.h"
#include "gimpdialogfactory.h"
#include "gimpdnd.h"
#include "gimpdocked.h"
@ -62,60 +63,23 @@ static void gimp_colormap_editor_docked_iface_init (GimpDockedInterface *face);
static void gimp_colormap_editor_constructed (GObject *object);
static void gimp_colormap_editor_dispose (GObject *object);
static void gimp_colormap_editor_finalize (GObject *object);
static void gimp_colormap_editor_unmap (GtkWidget *widget);
static void gimp_colormap_editor_set_image (GimpImageEditor *editor,
GimpImage *image);
static void gimp_colormap_editor_set_context (GimpDocked *docked,
GimpContext *context);
static PangoLayout *
gimp_colormap_editor_create_layout (GtkWidget *widget);
static void gimp_colormap_editor_update_entries (GimpColormapEditor *editor);
static gboolean gimp_colormap_preview_draw (GtkWidget *widget,
cairo_t *cr,
GimpColormapEditor *editor);
static void gimp_colormap_editor_entry_clicked (GimpPaletteView *view,
GimpPaletteEntry *entry,
GdkModifierType state,
GimpColormapEditor *editor);
static void gimp_colormap_editor_entry_selected (GimpPaletteView *view,
GimpPaletteEntry *entry,
GimpColormapEditor *editor);
static void gimp_colormap_editor_entry_activated (GimpPaletteView *view,
GimpPaletteEntry *entry,
GimpColormapEditor *editor);
static void gimp_colormap_editor_entry_context (GimpPaletteView *view,
GimpPaletteEntry *entry,
GimpColormapEditor *editor);
static void gimp_colormap_editor_color_dropped (GimpPaletteView *view,
GimpPaletteEntry *entry,
const GimpRGB *color,
GimpColormapEditor *editor);
static void gimp_colormap_adjustment_changed (GtkAdjustment *adjustment,
GimpColormapEditor *editor);
static void gimp_colormap_hex_entry_changed (GimpColorHexEntry *entry,
GimpColormapEditor *editor);
static void gimp_colormap_image_mode_changed (GimpImage *image,
GimpColormapEditor *editor);
static void gimp_colormap_image_colormap_changed (GimpImage *image,
gint ncol,
GimpColormapEditor *editor);
static void gimp_colormap_editor_edit_color_update
(GimpColorDialog *dialog,
const GimpRGB *color,
GimpColorDialogState state,
GimpColormapEditor *editor);
(GimpColorDialog *dialog,
const GimpRGB *color,
GimpColorDialogState state,
GimpColormapEditor *editor);
static void gimp_colormap_editor_entry_popup (GimpEditor *editor);
static void
gimp_colormap_editor_color_clicked (GimpColormapEditor *editor,
GimpPaletteEntry *entry,
GdkModifierType state);
G_DEFINE_TYPE_WITH_CODE (GimpColormapEditor, gimp_colormap_editor,
GIMP_TYPE_IMAGE_EDITOR,
@ -130,17 +94,13 @@ static GimpDockedInterface *parent_docked_iface = NULL;
static void
gimp_colormap_editor_class_init (GimpColormapEditorClass* klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GimpImageEditorClass *image_editor_class = GIMP_IMAGE_EDITOR_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->constructed = gimp_colormap_editor_constructed;
object_class->dispose = gimp_colormap_editor_dispose;
object_class->finalize = gimp_colormap_editor_finalize;
object_class->constructed = gimp_colormap_editor_constructed;
object_class->dispose = gimp_colormap_editor_dispose;
widget_class->unmap = gimp_colormap_editor_unmap;
image_editor_class->set_image = gimp_colormap_editor_set_image;
widget_class->unmap = gimp_colormap_editor_unmap;
}
static void
@ -157,73 +117,6 @@ gimp_colormap_editor_docked_iface_init (GimpDockedInterface *iface)
static void
gimp_colormap_editor_init (GimpColormapEditor *editor)
{
GtkWidget *frame;
GtkWidget *grid;
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (editor), frame, TRUE, TRUE, 0);
gtk_widget_show (frame);
editor->view = gimp_view_new_full_by_types (NULL,
GIMP_TYPE_PALETTE_VIEW,
GIMP_TYPE_PALETTE,
1, 1, 0,
FALSE, TRUE, FALSE);
gimp_view_set_expand (GIMP_VIEW (editor->view), TRUE);
gtk_container_add (GTK_CONTAINER (frame), editor->view);
gtk_widget_show (editor->view);
g_signal_connect (editor->view, "draw",
G_CALLBACK (gimp_colormap_preview_draw),
editor);
g_signal_connect (editor->view, "entry-clicked",
G_CALLBACK (gimp_colormap_editor_entry_clicked),
editor);
g_signal_connect (editor->view, "entry-selected",
G_CALLBACK (gimp_colormap_editor_entry_selected),
editor);
g_signal_connect (editor->view, "entry-activated",
G_CALLBACK (gimp_colormap_editor_entry_activated),
editor);
g_signal_connect (editor->view, "entry-context",
G_CALLBACK (gimp_colormap_editor_entry_context),
editor);
g_signal_connect (editor->view, "color-dropped",
G_CALLBACK (gimp_colormap_editor_color_dropped),
editor);
/* Some helpful hints */
grid = gtk_grid_new ();
gtk_grid_set_column_spacing (GTK_GRID (grid), 4);
gtk_grid_set_row_spacing (GTK_GRID (grid), 2);
gtk_box_pack_end (GTK_BOX (editor), grid, FALSE, FALSE, 0);
gtk_widget_show (grid);
editor->index_adjustment = gtk_adjustment_new (0, 0, 0, 1, 10, 0);
editor->index_spinbutton = gimp_spin_button_new (editor->index_adjustment,
1.0, 0);
gtk_widget_set_halign (editor->index_spinbutton, GTK_ALIGN_START);
gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (editor->index_spinbutton),
TRUE);
gimp_grid_attach_aligned (GTK_GRID (grid), 0, 0,
_("Color index:"), 0.0, 0.5,
editor->index_spinbutton, 1);
g_signal_connect (editor->index_adjustment, "value-changed",
G_CALLBACK (gimp_colormap_adjustment_changed),
editor);
editor->color_entry = gimp_color_hex_entry_new ();
gtk_widget_set_halign (editor->color_entry, GTK_ALIGN_START);
gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
_("HTML notation:"), 0.0, 0.5,
editor->color_entry, 1);
g_signal_connect (editor->color_entry, "color-changed",
G_CALLBACK (gimp_colormap_hex_entry_changed),
editor);
}
static void
@ -235,6 +128,7 @@ gimp_colormap_editor_constructed (GObject *object)
G_OBJECT_CLASS (parent_class)->constructed (object);
/* Editor buttons. */
extend_mask = gtk_widget_get_modifier_mask (GTK_WIDGET (object),
GDK_MODIFIER_INTENT_EXTEND_SELECTION);
modify_mask = gtk_widget_get_modifier_mask (GTK_WIDGET (object),
@ -271,16 +165,6 @@ gimp_colormap_editor_dispose (GObject *object)
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gimp_colormap_editor_finalize (GObject *object)
{
GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (object);
g_clear_object (&editor->layout);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_colormap_editor_unmap (GtkWidget *widget)
{
@ -292,59 +176,6 @@ gimp_colormap_editor_unmap (GtkWidget *widget)
GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}
static void
gimp_colormap_editor_set_image (GimpImageEditor *image_editor,
GimpImage *image)
{
GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (image_editor);
if (image_editor->image)
{
g_signal_handlers_disconnect_by_func (image_editor->image,
gimp_colormap_image_mode_changed,
editor);
g_signal_handlers_disconnect_by_func (image_editor->image,
gimp_colormap_image_colormap_changed,
editor);
if (editor->color_dialog)
gtk_widget_hide (editor->color_dialog);
if (! HAVE_COLORMAP (image))
{
gtk_adjustment_set_upper (editor->index_adjustment, 0);
if (gtk_widget_get_mapped (GTK_WIDGET (editor)))
gimp_view_set_viewable (GIMP_VIEW (editor->view), NULL);
}
}
GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image);
editor->col_index = 0;
if (image)
{
g_signal_connect (image, "mode-changed",
G_CALLBACK (gimp_colormap_image_mode_changed),
editor);
g_signal_connect (image, "colormap-changed",
G_CALLBACK (gimp_colormap_image_colormap_changed),
editor);
if (HAVE_COLORMAP (image))
{
gimp_view_set_viewable (GIMP_VIEW (editor->view),
GIMP_VIEWABLE (gimp_image_get_colormap_palette (image)));
gtk_adjustment_set_upper (editor->index_adjustment,
gimp_image_get_colormap_size (image) - 1);
}
}
gimp_colormap_editor_update_entries (editor);
}
static void
gimp_colormap_editor_set_context (GimpDocked *docked,
GimpContext *context)
@ -353,8 +184,26 @@ gimp_colormap_editor_set_context (GimpDocked *docked,
parent_docked_iface->set_context (docked, context);
gimp_view_renderer_set_context (GIMP_VIEW (editor->view)->renderer,
context);
if (editor->selection)
gtk_widget_destroy (editor->selection);
/* Main selection widget. */
if (context)
{
editor->selection = gimp_colormap_selection_new (context);
gtk_box_pack_start (GTK_BOX (editor), editor->selection, TRUE, TRUE, 0);
gtk_widget_show (editor->selection);
g_signal_connect_swapped (editor->selection, "color-context",
G_CALLBACK (gimp_colormap_editor_entry_popup),
editor);
g_signal_connect_swapped (editor->selection, "color-clicked",
G_CALLBACK (gimp_colormap_editor_color_clicked),
editor);
g_signal_connect_swapped (editor->selection, "color-activated",
G_CALLBACK (gimp_colormap_editor_edit_color),
editor);
}
}
@ -372,6 +221,19 @@ gimp_colormap_editor_new (GimpMenuFactory *menu_factory)
NULL);
}
static void
gimp_colormap_editor_color_clicked (GimpColormapEditor *editor,
GimpPaletteEntry *entry,
GdkModifierType state)
{
GimpImageEditor *image_editor = GIMP_IMAGE_EDITOR (editor);
if (state & gimp_get_toggle_behavior_mask ())
gimp_context_set_background (image_editor->context, &entry->color);
else
gimp_context_set_foreground (image_editor->context, &entry->color);
}
void
gimp_colormap_editor_edit_color (GimpColormapEditor *editor)
{
@ -388,7 +250,8 @@ gimp_colormap_editor_edit_color (GimpColormapEditor *editor)
if (! HAVE_COLORMAP (image))
return;
index = editor->col_index;
index = gimp_colormap_selection_get_index (GIMP_COLORMAP_SELECTION (editor->selection),
NULL);
colormap = gimp_image_get_colormap (image);
@ -442,43 +305,9 @@ gint
gimp_colormap_editor_get_index (GimpColormapEditor *editor,
const GimpRGB *search)
{
GimpImage *image;
gint index;
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), 0);
image = GIMP_IMAGE_EDITOR (editor)->image;
if (! HAVE_COLORMAP (image))
return -1;
index = editor->col_index;
if (search)
{
GimpRGB temp;
gimp_image_get_colormap_entry (image, index, &temp);
if (gimp_rgb_distance (&temp, search) > RGB_EPSILON)
{
gint n_colors = gimp_image_get_colormap_size (image);
gint i;
for (i = 0; i < n_colors; i++)
{
gimp_image_get_colormap_entry (image, i, &temp);
if (gimp_rgb_distance (&temp, search) < RGB_EPSILON)
{
index = i;
break;
}
}
}
}
return index;
return gimp_colormap_selection_get_index (GIMP_COLORMAP_SELECTION (editor->selection), search);
}
gboolean
@ -486,276 +315,17 @@ gimp_colormap_editor_set_index (GimpColormapEditor *editor,
gint index,
GimpRGB *color)
{
GimpImage *image;
gint size;
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), FALSE);
image = GIMP_IMAGE_EDITOR (editor)->image;
if (! HAVE_COLORMAP (image))
return FALSE;
size = gimp_image_get_colormap_size (image);
if (size < 1)
return FALSE;
index = CLAMP (index, 0, size - 1);
if (index != editor->col_index)
{
GimpPalette *palette = gimp_image_get_colormap_palette (image);
editor->col_index = index;
gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view),
gimp_palette_get_entry (palette, index));
gimp_colormap_editor_update_entries (editor);
}
if (color)
gimp_image_get_colormap_entry (GIMP_IMAGE_EDITOR (editor)->image,
index, color);
return TRUE;
return gimp_colormap_selection_set_index (GIMP_COLORMAP_SELECTION (editor->selection), index, color);
}
gint
gimp_colormap_editor_max_index (GimpColormapEditor *editor)
{
GimpImage *image;
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), -1);
image = GIMP_IMAGE_EDITOR (editor)->image;
if (! HAVE_COLORMAP (image))
return -1;
return MAX (0, gimp_image_get_colormap_size (image) - 1);
}
/* private functions */
static PangoLayout *
gimp_colormap_editor_create_layout (GtkWidget *widget)
{
PangoLayout *layout;
PangoAttrList *attrs;
PangoAttribute *attr;
layout = gtk_widget_create_pango_layout (widget,
_("Only indexed images have "
"a colormap."));
pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
attrs = pango_attr_list_new ();
attr = pango_attr_style_new (PANGO_STYLE_ITALIC);
attr->start_index = 0;
attr->end_index = -1;
pango_attr_list_insert (attrs, attr);
pango_layout_set_attributes (layout, attrs);
pango_attr_list_unref (attrs);
return layout;
}
static gboolean
gimp_colormap_preview_draw (GtkWidget *widget,
cairo_t *cr,
GimpColormapEditor *editor)
{
GimpImageEditor *image_editor = GIMP_IMAGE_EDITOR (editor);
GtkStyleContext *style = gtk_widget_get_style_context (widget);
GtkAllocation allocation;
GdkRGBA color;
gint width, height;
gint y;
if (image_editor->image == NULL ||
gimp_image_get_base_type (image_editor->image) == GIMP_INDEXED)
return FALSE;
gtk_style_context_get_color (style, gtk_widget_get_state_flags (widget),
&color);
gdk_cairo_set_source_rgba (cr, &color);
gtk_widget_get_allocation (widget, &allocation);
if (! editor->layout)
editor->layout = gimp_colormap_editor_create_layout (editor->view);
pango_layout_set_width (editor->layout,
PANGO_SCALE * (allocation.width - 2 * BORDER));
pango_layout_get_pixel_size (editor->layout, &width, &height);
y = (allocation.height - height) / 2;
cairo_move_to (cr, BORDER, MAX (y, 0));
pango_cairo_show_layout (cr, editor->layout);
return TRUE;
}
static void
gimp_colormap_editor_update_entries (GimpColormapEditor *editor)
{
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
if (! HAVE_COLORMAP (image) ||
! gimp_image_get_colormap_size (image))
{
gtk_widget_set_sensitive (editor->index_spinbutton, FALSE);
gtk_widget_set_sensitive (editor->color_entry, FALSE);
gtk_adjustment_set_value (editor->index_adjustment, 0);
gtk_entry_set_text (GTK_ENTRY (editor->color_entry), "");
}
else
{
const guchar *colormap = gimp_image_get_colormap (image);
const guchar *col;
gchar *string;
gtk_adjustment_set_value (editor->index_adjustment, editor->col_index);
col = colormap + editor->col_index * 3;
string = g_strdup_printf ("%02x%02x%02x", col[0], col[1], col[2]);
gtk_entry_set_text (GTK_ENTRY (editor->color_entry), string);
g_free (string);
gtk_widget_set_sensitive (editor->index_spinbutton, TRUE);
gtk_widget_set_sensitive (editor->color_entry, TRUE);
}
}
static void
gimp_colormap_editor_entry_clicked (GimpPaletteView *view,
GimpPaletteEntry *entry,
GdkModifierType state,
GimpColormapEditor *editor)
{
GimpImageEditor *image_editor = GIMP_IMAGE_EDITOR (editor);
gimp_colormap_editor_set_index (editor, entry->position, NULL);
if (state & gimp_get_toggle_behavior_mask ())
gimp_context_set_background (image_editor->context, &entry->color);
else
gimp_context_set_foreground (image_editor->context, &entry->color);
}
static void
gimp_colormap_editor_entry_selected (GimpPaletteView *view,
GimpPaletteEntry *entry,
GimpColormapEditor *editor)
{
gint index = entry ? entry->position : 0;
gimp_colormap_editor_set_index (editor, index, NULL);
}
static void
gimp_colormap_editor_entry_activated (GimpPaletteView *view,
GimpPaletteEntry *entry,
GimpColormapEditor *editor)
{
gimp_colormap_editor_set_index (editor, entry->position, NULL);
gimp_ui_manager_activate_action (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)),
"colormap",
"colormap-edit-color");
}
static void
gimp_colormap_editor_entry_context (GimpPaletteView *view,
GimpPaletteEntry *entry,
GimpColormapEditor *editor)
{
gimp_colormap_editor_set_index (editor, entry->position, NULL);
gimp_editor_popup_menu (GIMP_EDITOR (editor), NULL, NULL);
}
static void
gimp_colormap_editor_color_dropped (GimpPaletteView *view,
GimpPaletteEntry *entry,
const GimpRGB *color,
GimpColormapEditor *editor)
{
}
static void
gimp_colormap_adjustment_changed (GtkAdjustment *adjustment,
GimpColormapEditor *editor)
{
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
if (HAVE_COLORMAP (image))
{
gint index = ROUND (gtk_adjustment_get_value (adjustment));
gimp_colormap_editor_set_index (editor, index, NULL);
gimp_colormap_editor_update_entries (editor);
}
}
static void
gimp_colormap_hex_entry_changed (GimpColorHexEntry *entry,
GimpColormapEditor *editor)
{
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
if (image)
{
GimpRGB color;
gimp_color_hex_entry_get_color (entry, &color);
gimp_image_set_colormap_entry (image, editor->col_index, &color, TRUE);
gimp_image_flush (image);
}
}
static void
gimp_colormap_image_mode_changed (GimpImage *image,
GimpColormapEditor *editor)
{
if (editor->color_dialog)
gtk_widget_hide (editor->color_dialog);
gimp_colormap_image_colormap_changed (image, -1, editor);
}
static void
gimp_colormap_image_colormap_changed (GimpImage *image,
gint ncol,
GimpColormapEditor *editor)
{
if (HAVE_COLORMAP (image))
{
gimp_view_set_viewable (GIMP_VIEW (editor->view),
GIMP_VIEWABLE (gimp_image_get_colormap_palette (image)));
gtk_adjustment_set_upper (editor->index_adjustment,
gimp_image_get_colormap_size (image) - 1);
}
else
{
gimp_view_set_viewable (GIMP_VIEW (editor->view), NULL);
}
if (ncol == editor->col_index || ncol == -1)
gimp_colormap_editor_update_entries (editor);
return gimp_colormap_selection_max_index (GIMP_COLORMAP_SELECTION (editor->selection));
}
static void
@ -789,6 +359,10 @@ gimp_colormap_editor_edit_color_update (GimpColorDialog *dialog,
if (image)
{
gint col_index;
col_index = gimp_colormap_selection_get_index (GIMP_COLORMAP_SELECTION (editor->selection),
NULL);
if (push_undo)
{
GimpRGB old_color;
@ -797,11 +371,11 @@ gimp_colormap_editor_edit_color_update (GimpColorDialog *dialog,
GIMP_COLOR_SELECTION (dialog->selection), &old_color);
/* Restore old color for undo */
gimp_image_set_colormap_entry (image, editor->col_index, &old_color,
gimp_image_set_colormap_entry (image, col_index, &old_color,
FALSE);
}
gimp_image_set_colormap_entry (image, editor->col_index, color,
gimp_image_set_colormap_entry (image, col_index, color,
push_undo);
if (push_undo)
@ -810,3 +384,9 @@ gimp_colormap_editor_edit_color_update (GimpColorDialog *dialog,
gimp_projection_flush (gimp_image_get_projection (image));
}
}
static void
gimp_colormap_editor_entry_popup (GimpEditor *editor)
{
gimp_editor_popup_menu (editor, NULL, NULL);
}

View file

@ -36,15 +36,7 @@ struct _GimpColormapEditor
{
GimpImageEditor parent_instance;
GtkWidget *view;
gint col_index;
PangoLayout *layout;
GtkAdjustment *index_adjustment;
GtkWidget *index_spinbutton;
GtkWidget *color_entry;
GtkWidget *selection;
GtkWidget *color_dialog;
};