remove the ->position entry from GimpPaletteEntry

This commit is contained in:
Simon Budig 2019-11-12 15:32:38 +01:00 committed by Jehan
parent 4cf38d784f
commit 9c96e34be2
8 changed files with 76 additions and 88 deletions

View File

@ -192,7 +192,6 @@ gimp_palette_load (GimpContext *context,
255); 255);
entry->name = g_strdup (tok ? tok : _("Untitled")); entry->name = g_strdup (tok ? tok : _("Untitled"));
entry->position = gimp_palette_get_n_colors (palette);
palette->colors = g_list_prepend (palette->colors, entry); palette->colors = g_list_prepend (palette->colors, entry);
palette->n_colors++; palette->n_colors++;

View File

@ -399,49 +399,16 @@ gimp_palette_move_entry (GimpPalette *palette,
GimpPaletteEntry *entry, GimpPaletteEntry *entry,
gint position) gint position)
{ {
GList *list;
gint pos = 0;
g_return_if_fail (GIMP_IS_PALETTE (palette)); g_return_if_fail (GIMP_IS_PALETTE (palette));
g_return_if_fail (entry != NULL); g_return_if_fail (entry != NULL);
if (g_list_find (palette->colors, entry)) if (g_list_find (palette->colors, entry))
{ {
pos = entry->position;
if (entry->position == position)
return;
entry->position = position;
palette->colors = g_list_remove (palette->colors, palette->colors = g_list_remove (palette->colors,
entry); entry);
palette->colors = g_list_insert (palette->colors, palette->colors = g_list_insert (palette->colors,
entry, position); entry, position);
if (pos < position)
{
for (list = g_list_nth (palette->colors, pos);
list && pos < position;
list = g_list_next (list))
{
entry = (GimpPaletteEntry *) list->data;
entry->position = pos++;
}
}
else
{
for (list = g_list_nth (palette->colors, position + 1);
list && position < pos;
list = g_list_next (list))
{
entry = (GimpPaletteEntry *) list->data;
entry->position += 1;
pos--;
}
}
gimp_data_dirty (GIMP_DATA (palette)); gimp_data_dirty (GIMP_DATA (palette));
} }
} }
@ -464,25 +431,11 @@ gimp_palette_add_entry (GimpPalette *palette,
if (position < 0 || position >= palette->n_colors) if (position < 0 || position >= palette->n_colors)
{ {
entry->position = palette->n_colors;
palette->colors = g_list_append (palette->colors, entry); palette->colors = g_list_append (palette->colors, entry);
} }
else else
{ {
GList *list;
entry->position = position;
palette->colors = g_list_insert (palette->colors, entry, position); palette->colors = g_list_insert (palette->colors, entry, position);
/* renumber the displaced entries */
for (list = g_list_nth (palette->colors, position + 1);
list;
list = g_list_next (list))
{
GimpPaletteEntry *entry2 = list->data;
entry2->position += 1;
}
} }
palette->n_colors += 1; palette->n_colors += 1;
@ -496,30 +449,17 @@ void
gimp_palette_delete_entry (GimpPalette *palette, gimp_palette_delete_entry (GimpPalette *palette,
GimpPaletteEntry *entry) GimpPaletteEntry *entry)
{ {
GList *list;
gint pos = 0;
g_return_if_fail (GIMP_IS_PALETTE (palette)); g_return_if_fail (GIMP_IS_PALETTE (palette));
g_return_if_fail (entry != NULL); g_return_if_fail (entry != NULL);
if (g_list_find (palette->colors, entry)) if (g_list_find (palette->colors, entry))
{ {
pos = entry->position;
gimp_palette_entry_free (entry); gimp_palette_entry_free (entry);
palette->colors = g_list_remove (palette->colors, entry); palette->colors = g_list_remove (palette->colors, entry);
palette->n_colors--; palette->n_colors--;
for (list = g_list_nth (palette->colors, pos);
list;
list = g_list_next (list))
{
entry = (GimpPaletteEntry *) list->data;
entry->position = pos++;
}
gimp_data_dirty (GIMP_DATA (palette)); gimp_data_dirty (GIMP_DATA (palette));
} }
} }
@ -607,6 +547,16 @@ gimp_palette_get_entry (GimpPalette *palette,
return g_list_nth_data (palette->colors, position); return g_list_nth_data (palette->colors, position);
} }
gint
gimp_palette_get_entry_position (GimpPalette *palette,
GimpPaletteEntry *entry)
{
g_return_val_if_fail (GIMP_IS_PALETTE (palette), -1);
g_return_val_if_fail (entry != NULL, -1);
return g_list_index (palette->colors, entry);
}
void void
gimp_palette_set_columns (GimpPalette *palette, gimp_palette_set_columns (GimpPalette *palette,
gint columns) gint columns)

View File

@ -34,9 +34,6 @@ struct _GimpPaletteEntry
{ {
GimpRGB color; GimpRGB color;
gchar *name; gchar *name;
/* EEK */
gint position;
}; };
@ -90,6 +87,8 @@ gboolean gimp_palette_set_entry_name (GimpPalette *palette,
const gchar *name); const gchar *name);
GimpPaletteEntry * gimp_palette_get_entry (GimpPalette *palette, GimpPaletteEntry * gimp_palette_get_entry (GimpPalette *palette,
gint position); gint position);
gint gimp_palette_get_entry_position (GimpPalette *palette,
GimpPaletteEntry *entry);
void gimp_palette_set_columns (GimpPalette *palette, void gimp_palette_set_columns (GimpPalette *palette,
gint columns); gint columns);

View File

@ -400,7 +400,7 @@ palette_add_entry_invoker (GimpProcedure *procedure,
GimpPaletteEntry *entry = GimpPaletteEntry *entry =
gimp_palette_add_entry (palette, -1, entry_name, &color); gimp_palette_add_entry (palette, -1, entry_name, &color);
entry_num = entry->position; entry_num = gimp_palette_get_entry_position (palette, entry);
} }
else else
success = FALSE; success = FALSE;

View File

@ -590,7 +590,12 @@ gimp_colormap_selection_entry_clicked (GimpPaletteView *view,
GdkModifierType state, GdkModifierType state,
GimpColormapSelection *selection) GimpColormapSelection *selection)
{ {
gimp_colormap_selection_set_index (selection, entry->position, NULL); GimpPalette *palette;
gint index;
palette = gimp_image_get_colormap_palette (selection->active_image);
index = gimp_palette_get_entry_position (palette, entry);
gimp_colormap_selection_set_index (selection, index, NULL);
g_signal_emit (selection, signals[COLOR_CLICKED], 0, entry, state); g_signal_emit (selection, signals[COLOR_CLICKED], 0, entry, state);
} }
@ -600,7 +605,12 @@ gimp_colormap_selection_entry_selected (GimpPaletteView *view,
GimpPaletteEntry *entry, GimpPaletteEntry *entry,
GimpColormapSelection *selection) GimpColormapSelection *selection)
{ {
gint index = entry ? entry->position : 0; GimpPalette *palette;
gint index = 0;
palette = gimp_image_get_colormap_palette (selection->active_image);
if (entry)
index = gimp_palette_get_entry_position (palette, entry);
gimp_colormap_selection_set_index (selection, index, NULL); gimp_colormap_selection_set_index (selection, index, NULL);
} }
@ -610,7 +620,12 @@ gimp_colormap_selection_entry_activated (GimpPaletteView *view,
GimpPaletteEntry *entry, GimpPaletteEntry *entry,
GimpColormapSelection *selection) GimpColormapSelection *selection)
{ {
gimp_colormap_selection_set_index (selection, entry->position, NULL); GimpPalette *palette;
gint index;
palette = gimp_image_get_colormap_palette (selection->active_image);
index = gimp_palette_get_entry_position (palette, entry);
gimp_colormap_selection_set_index (selection, index, NULL);
g_signal_emit (selection, signals[COLOR_ACTIVATED], 0, entry); g_signal_emit (selection, signals[COLOR_ACTIVATED], 0, entry);
} }

View File

@ -537,12 +537,14 @@ gimp_palette_editor_pick_color (GimpPaletteEditor *editor,
gint index = -1; gint index = -1;
data = gimp_data_editor_get_data (GIMP_DATA_EDITOR (editor)); data = gimp_data_editor_get_data (GIMP_DATA_EDITOR (editor));
index = gimp_palette_get_entry_position (GIMP_PALETTE (data),
editor->color);
switch (pick_state) switch (pick_state)
{ {
case GIMP_COLOR_PICK_STATE_START: case GIMP_COLOR_PICK_STATE_START:
if (editor->color) if (editor->color)
index = editor->color->position + 1; index += 1;
entry = gimp_palette_add_entry (GIMP_PALETTE (data), index, entry = gimp_palette_add_entry (GIMP_PALETTE (data), index,
NULL, color); NULL, color);
@ -553,8 +555,7 @@ gimp_palette_editor_pick_color (GimpPaletteEditor *editor,
case GIMP_COLOR_PICK_STATE_UPDATE: case GIMP_COLOR_PICK_STATE_UPDATE:
case GIMP_COLOR_PICK_STATE_END: case GIMP_COLOR_PICK_STATE_END:
gimp_palette_set_entry_color (GIMP_PALETTE (data), gimp_palette_set_entry_color (GIMP_PALETTE (data),
editor->color->position, index, color);
color);
break; break;
} }
} }
@ -649,7 +650,7 @@ gimp_palette_editor_get_index (GimpPaletteEditor *editor,
entry = gimp_palette_find_entry (palette, search, editor->color); entry = gimp_palette_find_entry (palette, search, editor->color);
if (entry) if (entry)
return entry->position; return gimp_palette_get_entry_position (palette, entry);
} }
return -1; return -1;
@ -792,9 +793,17 @@ palette_editor_entry_selected (GimpPaletteView *view,
editor->color = entry; editor->color = entry;
if (entry) if (entry)
g_snprintf (index, sizeof (index), "%04i", entry->position); {
GimpPalette *palette = GIMP_PALETTE (data_editor->data);
gint pos;
pos = gimp_palette_get_entry_position (palette, entry);
g_snprintf (index, sizeof (index), "%04i", pos);
}
else else
{
g_snprintf (index, sizeof (index), "####"); g_snprintf (index, sizeof (index), "####");
}
gtk_label_set_text (GTK_LABEL (editor->index_label), index); gtk_label_set_text (GTK_LABEL (editor->index_label), index);
@ -877,7 +886,7 @@ palette_editor_color_dropped (GimpPaletteView *view,
gint pos = -1; gint pos = -1;
if (entry) if (entry)
pos = entry->position; pos = gimp_palette_get_entry_position (palette, entry);
entry = gimp_palette_add_entry (palette, pos, NULL, color); entry = gimp_palette_add_entry (palette, pos, NULL, color);
gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view), entry); gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view), entry);
@ -895,10 +904,12 @@ palette_editor_color_name_changed (GtkWidget *widget,
{ {
GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data); GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
const gchar *name; const gchar *name;
gint pos;
name = gtk_entry_get_text (GTK_ENTRY (editor->color_name)); name = gtk_entry_get_text (GTK_ENTRY (editor->color_name));
gimp_palette_set_entry_name (palette, editor->color->position, name); pos = gimp_palette_get_entry_position (palette, editor->color);
gimp_palette_set_entry_name (palette, pos, name);
} }
} }

View File

@ -152,6 +152,9 @@ gimp_palette_view_draw (GtkWidget *widget,
{ {
GimpPaletteView *pal_view = GIMP_PALETTE_VIEW (widget); GimpPaletteView *pal_view = GIMP_PALETTE_VIEW (widget);
GimpView *view = GIMP_VIEW (widget); GimpView *view = GIMP_VIEW (widget);
GimpPalette *palette;
palette = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
cairo_save (cr); cairo_save (cr);
GTK_WIDGET_CLASS (parent_class)->draw (widget, cr); GTK_WIDGET_CLASS (parent_class)->draw (widget, cr);
@ -161,14 +164,16 @@ gimp_palette_view_draw (GtkWidget *widget,
{ {
GimpViewRendererPalette *renderer; GimpViewRendererPalette *renderer;
GtkAllocation allocation; GtkAllocation allocation;
gint row, col; gint pos, row, col;
renderer = GIMP_VIEW_RENDERER_PALETTE (view->renderer); renderer = GIMP_VIEW_RENDERER_PALETTE (view->renderer);
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
row = pal_view->selected->position / renderer->columns; pos = gimp_palette_get_entry_position (palette, pal_view->selected);
col = pal_view->selected->position % renderer->columns;
row = pos / renderer->columns;
col = pos % renderer->columns;
cairo_rectangle (cr, cairo_rectangle (cr,
col * renderer->cell_width + 0.5, col * renderer->cell_width + 0.5,
@ -310,9 +315,12 @@ gimp_palette_view_focus (GtkWidget *widget,
if (skip != 0) if (skip != 0)
{ {
GimpPaletteEntry *entry; GimpPaletteEntry *entry;
GimpPalette *palette;
gint position; gint position;
position = view->selected->position + skip; palette = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
position = gimp_palette_get_entry_position (palette, view->selected);
position += skip;
entry = gimp_palette_get_entry (palette, position); entry = gimp_palette_get_entry (palette, position);
@ -410,8 +418,9 @@ gimp_palette_view_get_entry_rect (GimpPaletteView *view,
GdkRectangle *rect) GdkRectangle *rect)
{ {
GimpViewRendererPalette *renderer; GimpViewRendererPalette *renderer;
GimpPalette *palette;
GtkAllocation allocation; GtkAllocation allocation;
gint row, col; gint pos, row, col;
g_return_if_fail (GIMP_IS_PALETTE_VIEW (view)); g_return_if_fail (GIMP_IS_PALETTE_VIEW (view));
g_return_if_fail (entry); g_return_if_fail (entry);
@ -420,8 +429,10 @@ gimp_palette_view_get_entry_rect (GimpPaletteView *view,
gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); gtk_widget_get_allocation (GTK_WIDGET (view), &allocation);
renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer); renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);
row = entry->position / renderer->columns; palette = GIMP_PALETTE (GIMP_VIEW_RENDERER (renderer)->viewable);
col = entry->position % renderer->columns; pos = gimp_palette_get_entry_position (palette, entry);
row = pos / renderer->columns;
col = pos % renderer->columns;
rect->x = allocation.x + col * renderer->cell_width; rect->x = allocation.x + col * renderer->cell_width;
rect->y = allocation.y + row * renderer->cell_height; rect->y = allocation.y + row * renderer->cell_height;
@ -442,8 +453,8 @@ gimp_palette_view_find_entry (GimpPaletteView *view,
GimpPaletteEntry *entry = NULL; GimpPaletteEntry *entry = NULL;
gint col, row; gint col, row;
palette = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer); renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);
palette = GIMP_PALETTE (GIMP_VIEW_RENDERER (renderer)->viewable);
if (! palette || ! gimp_palette_get_n_colors (palette)) if (! palette || ! gimp_palette_get_n_colors (palette))
return NULL; return NULL;
@ -466,16 +477,19 @@ gimp_palette_view_expose_entry (GimpPaletteView *view,
GimpPaletteEntry *entry) GimpPaletteEntry *entry)
{ {
GimpViewRendererPalette *renderer; GimpViewRendererPalette *renderer;
gint row, col; gint pos, row, col;
GtkWidget *widget = GTK_WIDGET (view); GtkWidget *widget = GTK_WIDGET (view);
GtkAllocation allocation; GtkAllocation allocation;
GimpPalette *palette;
renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer); renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);
palette = GIMP_PALETTE (GIMP_VIEW_RENDERER (renderer)->viewable);
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
row = entry->position / renderer->columns; pos = gimp_palette_get_entry_position (palette, entry);
col = entry->position % renderer->columns; row = pos / renderer->columns;
col = pos % renderer->columns;
gtk_widget_queue_draw_area (GTK_WIDGET (view), gtk_widget_queue_draw_area (GTK_WIDGET (view),
allocation.x + col * renderer->cell_width, allocation.x + col * renderer->cell_width,

View File

@ -359,7 +359,7 @@ HELP
GimpPaletteEntry *entry = GimpPaletteEntry *entry =
gimp_palette_add_entry (palette, -1, entry_name, &color); gimp_palette_add_entry (palette, -1, entry_name, &color);
entry_num = entry->position; entry_num = gimp_palette_get_entry_position (palette, entry);
} }
else else
success = FALSE; success = FALSE;