[gimp/wip/simon/palettestuff: 3/3] remove the ->position entry from GimpPaletteEntry




commit edb498f1580d56c809a491f9b4a0bb4439a47ecc
Author: Simon Budig <simon budig de>
Date:   Tue Nov 12 15:32:38 2019 +0100

    remove the ->position entry from GimpPaletteEntry

 app/core/gimppalette-load.c         |  1 -
 app/core/gimppalette.c              | 70 ++++++-------------------------------
 app/core/gimppalette.h              |  5 ++-
 app/pdb/palette-cmds.c              |  5 ++-
 app/widgets/gimpcolormapselection.c | 24 +++++++++++--
 app/widgets/gimppaletteeditor.c     | 27 +++++++++-----
 app/widgets/gimppaletteview.c       | 36 +++++++++++++------
 pdb/groups/palette.pdb              |  5 ++-
 8 files changed, 81 insertions(+), 92 deletions(-)
---
diff --git a/app/core/gimppalette-load.c b/app/core/gimppalette-load.c
index 32a1fc1ea5..6755f0c27f 100644
--- a/app/core/gimppalette-load.c
+++ b/app/core/gimppalette-load.c
@@ -192,7 +192,6 @@ gimp_palette_load (GimpContext   *context,
                                255);
 
           entry->name     = g_strdup (tok ? tok : _("Untitled"));
-          entry->position = gimp_palette_get_n_colors (palette);
 
           palette->colors = g_list_prepend (palette->colors, entry);
           palette->n_colors++;
diff --git a/app/core/gimppalette.c b/app/core/gimppalette.c
index f5b848ca41..9148a19447 100644
--- a/app/core/gimppalette.c
+++ b/app/core/gimppalette.c
@@ -399,49 +399,16 @@ gimp_palette_move_entry (GimpPalette      *palette,
                          GimpPaletteEntry *entry,
                          gint              position)
 {
-  GList *list;
-  gint   pos = 0;
-
   g_return_if_fail (GIMP_IS_PALETTE (palette));
   g_return_if_fail (entry != NULL);
 
   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,
                                        entry);
       palette->colors = g_list_insert (palette->colors,
                                        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));
     }
 }
@@ -464,25 +431,11 @@ gimp_palette_add_entry (GimpPalette   *palette,
 
   if (position < 0 || position >= palette->n_colors)
     {
-      entry->position = palette->n_colors;
       palette->colors = g_list_append (palette->colors, entry);
     }
   else
     {
-      GList *list;
-
-      entry->position = 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;
@@ -496,30 +449,17 @@ void
 gimp_palette_delete_entry (GimpPalette      *palette,
                            GimpPaletteEntry *entry)
 {
-  GList *list;
-  gint   pos = 0;
-
   g_return_if_fail (GIMP_IS_PALETTE (palette));
   g_return_if_fail (entry != NULL);
 
   if (g_list_find (palette->colors, entry))
     {
-      pos = entry->position;
       gimp_palette_entry_free (entry);
 
       palette->colors = g_list_remove (palette->colors, entry);
 
       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));
     }
 }
@@ -607,6 +547,16 @@ gimp_palette_get_entry (GimpPalette *palette,
   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
 gimp_palette_set_columns (GimpPalette *palette,
                           gint         columns)
diff --git a/app/core/gimppalette.h b/app/core/gimppalette.h
index 5bc6d8c823..e621c596d7 100644
--- a/app/core/gimppalette.h
+++ b/app/core/gimppalette.h
@@ -34,9 +34,6 @@ struct _GimpPaletteEntry
 {
   GimpRGB  color;
   gchar   *name;
-
-  /* EEK */
-  gint     position;
 };
 
 
@@ -90,6 +87,8 @@ gboolean           gimp_palette_set_entry_name  (GimpPalette      *palette,
                                                  const gchar      *name);
 GimpPaletteEntry * gimp_palette_get_entry       (GimpPalette      *palette,
                                                  gint              position);
+gint               gimp_palette_get_entry_position (GimpPalette   *palette,
+                                                 GimpPaletteEntry *entry);
 
 void               gimp_palette_set_columns     (GimpPalette      *palette,
                                                  gint              columns);
diff --git a/app/pdb/palette-cmds.c b/app/pdb/palette-cmds.c
index efabd99646..9a40a5e559 100644
--- a/app/pdb/palette-cmds.c
+++ b/app/pdb/palette-cmds.c
@@ -397,10 +397,9 @@ palette_add_entry_invoker (GimpProcedure         *procedure,
 
       if (palette)
         {
-          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_n_colors (palette) - 1;
         }
       else
         success = FALSE;
diff --git a/app/widgets/gimpcolormapselection.c b/app/widgets/gimpcolormapselection.c
index 036e07557d..ba3afc38a3 100644
--- a/app/widgets/gimpcolormapselection.c
+++ b/app/widgets/gimpcolormapselection.c
@@ -590,7 +590,12 @@ gimp_colormap_selection_entry_clicked (GimpPaletteView       *view,
                                        GdkModifierType        state,
                                        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);
 }
@@ -600,7 +605,15 @@ gimp_colormap_selection_entry_selected (GimpPaletteView       *view,
                                         GimpPaletteEntry      *entry,
                                         GimpColormapSelection *selection)
 {
-  gint index = entry ? entry->position : 0;
+  GimpPalette *palette;
+  gint index = -1;
+
+  palette = gimp_image_get_colormap_palette (selection->active_image);
+  if (entry)
+    index = gimp_palette_get_entry_position (palette, entry);
+
+  if (index < 0)
+    index = 0;
 
   gimp_colormap_selection_set_index (selection, index, NULL);
 }
@@ -610,7 +623,12 @@ gimp_colormap_selection_entry_activated (GimpPaletteView       *view,
                                          GimpPaletteEntry      *entry,
                                          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);
 }
diff --git a/app/widgets/gimppaletteeditor.c b/app/widgets/gimppaletteeditor.c
index 2335dad337..250e3595ac 100644
--- a/app/widgets/gimppaletteeditor.c
+++ b/app/widgets/gimppaletteeditor.c
@@ -537,12 +537,14 @@ gimp_palette_editor_pick_color (GimpPaletteEditor  *editor,
       gint              index = -1;
 
       data = gimp_data_editor_get_data (GIMP_DATA_EDITOR (editor));
+      index = gimp_palette_get_entry_position (GIMP_PALETTE (data),
+                                               editor->color);
 
       switch (pick_state)
         {
         case GIMP_COLOR_PICK_STATE_START:
           if (editor->color)
-            index = editor->color->position + 1;
+            index += 1;
 
           entry = gimp_palette_add_entry (GIMP_PALETTE (data), index,
                                           NULL, color);
@@ -553,8 +555,7 @@ gimp_palette_editor_pick_color (GimpPaletteEditor  *editor,
         case GIMP_COLOR_PICK_STATE_UPDATE:
         case GIMP_COLOR_PICK_STATE_END:
           gimp_palette_set_entry_color (GIMP_PALETTE (data),
-                                        editor->color->position,
-                                        color);
+                                        index, color);
           break;
         }
     }
@@ -649,7 +650,7 @@ gimp_palette_editor_get_index (GimpPaletteEditor *editor,
       entry = gimp_palette_find_entry (palette, search, editor->color);
 
       if (entry)
-        return entry->position;
+        return gimp_palette_get_entry_position (palette, entry);
     }
 
   return -1;
@@ -792,9 +793,17 @@ palette_editor_entry_selected (GimpPaletteView   *view,
       editor->color = 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
-        g_snprintf (index, sizeof (index), "####");
+        {
+          g_snprintf (index, sizeof (index), "####");
+        }
 
       gtk_label_set_text (GTK_LABEL (editor->index_label), index);
 
@@ -877,7 +886,7 @@ palette_editor_color_dropped (GimpPaletteView   *view,
       gint         pos     = -1;
 
       if (entry)
-        pos = entry->position;
+        pos = gimp_palette_get_entry_position (palette, entry);
 
       entry = gimp_palette_add_entry (palette, pos, NULL, color);
       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);
       const gchar *name;
+      gint pos;
 
       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);
     }
 }
 
diff --git a/app/widgets/gimppaletteview.c b/app/widgets/gimppaletteview.c
index ee6168c99f..364322fe79 100644
--- a/app/widgets/gimppaletteview.c
+++ b/app/widgets/gimppaletteview.c
@@ -152,6 +152,9 @@ gimp_palette_view_draw (GtkWidget *widget,
 {
   GimpPaletteView *pal_view = GIMP_PALETTE_VIEW (widget);
   GimpView        *view     = GIMP_VIEW (widget);
+  GimpPalette     *palette;
+
+  palette = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
 
   cairo_save (cr);
   GTK_WIDGET_CLASS (parent_class)->draw (widget, cr);
@@ -161,14 +164,16 @@ gimp_palette_view_draw (GtkWidget *widget,
     {
       GimpViewRendererPalette *renderer;
       GtkAllocation            allocation;
-      gint                     row, col;
+      gint                     pos, row, col;
 
       renderer = GIMP_VIEW_RENDERER_PALETTE (view->renderer);
 
       gtk_widget_get_allocation (widget, &allocation);
 
-      row = pal_view->selected->position / renderer->columns;
-      col = pal_view->selected->position % renderer->columns;
+      pos = gimp_palette_get_entry_position (palette, pal_view->selected);
+
+      row = pos / renderer->columns;
+      col = pos % renderer->columns;
 
       cairo_rectangle (cr,
                        col * renderer->cell_width  + 0.5,
@@ -310,9 +315,12 @@ gimp_palette_view_focus (GtkWidget        *widget,
       if (skip != 0)
         {
           GimpPaletteEntry *entry;
+          GimpPalette      *palette;
           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);
 
@@ -410,8 +418,9 @@ gimp_palette_view_get_entry_rect (GimpPaletteView  *view,
                                   GdkRectangle     *rect)
 {
   GimpViewRendererPalette *renderer;
+  GimpPalette             *palette;
   GtkAllocation            allocation;
-  gint                     row, col;
+  gint                     pos, row, col;
 
   g_return_if_fail (GIMP_IS_PALETTE_VIEW (view));
   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);
 
   renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);
-  row = entry->position / renderer->columns;
-  col = entry->position % renderer->columns;
+  palette = GIMP_PALETTE (GIMP_VIEW_RENDERER (renderer)->viewable);
+  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->y = allocation.y + row * renderer->cell_height;
@@ -442,8 +453,8 @@ gimp_palette_view_find_entry (GimpPaletteView *view,
   GimpPaletteEntry        *entry = NULL;
   gint                     col, row;
 
-  palette  = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
   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))
     return NULL;
@@ -466,16 +477,19 @@ gimp_palette_view_expose_entry (GimpPaletteView  *view,
                                 GimpPaletteEntry *entry)
 {
   GimpViewRendererPalette *renderer;
-  gint                     row, col;
+  gint                     pos, row, col;
   GtkWidget               *widget = GTK_WIDGET (view);
   GtkAllocation            allocation;
+  GimpPalette             *palette;
 
   renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);
+  palette = GIMP_PALETTE (GIMP_VIEW_RENDERER (renderer)->viewable);
 
   gtk_widget_get_allocation (widget, &allocation);
 
-  row = entry->position / renderer->columns;
-  col = entry->position % renderer->columns;
+  pos = gimp_palette_get_entry_position (palette, entry);
+  row = pos / renderer->columns;
+  col = pos % renderer->columns;
 
   gtk_widget_queue_draw_area (GTK_WIDGET (view),
                               allocation.x + col * renderer->cell_width,
diff --git a/pdb/groups/palette.pdb b/pdb/groups/palette.pdb
index da3c7fc533..933e0c1a33 100644
--- a/pdb/groups/palette.pdb
+++ b/pdb/groups/palette.pdb
@@ -356,10 +356,9 @@ HELP
 
   if (palette)
     {
-      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_n_colors (palette) - 1;
     }
   else
     success = FALSE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]