Add eyedrop tool. The treeview will scroll to the selection.
[colorize.git] / gui / palette.c
index 8d55689..667214f 100644 (file)
@@ -6,6 +6,7 @@
 #include "../src/yuv.h"
 #include "palette.h"
 #include "timeline.h"
+#include "image.h"
 
 int mark_selected = 0;
 
@@ -154,8 +155,12 @@ void create_palette(void)
                g_object_unref(color);
                if (i == mark_selected) {
                        GtkTreeSelection *selection;
+                       GtkTreePath *path;
                        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(palette_treeview));
                        gtk_tree_selection_select_iter(selection, &iter);
+                       path = gtk_tree_model_get_path(GTK_TREE_MODEL(palette_store), &iter);
+                       gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(palette_treeview), path, NULL, FALSE, 0,0);
+                       gtk_tree_path_free(path);
                }
        }
 
@@ -163,20 +168,65 @@ void create_palette(void)
 
 }
 
-void update_color(int entry)
+int get_iter_by_path_name(GtkTreeIter *iter, GtkTreeModel *model, int entry)
 {
-       GdkPixbuf *color;
+       char path_name[10];
 
-       color = draw_pixpuf(entry);
+       sprintf(path_name, "%d", entry);
+        if (gtk_tree_model_get_iter_from_string(model, iter, path_name))
+               return 1;
+       return 0;
+}
 
+void update_color(int entry)
+{
+       GdkPixbuf *color;
        GtkTreeIter iter;
        GtkTreeModel *model;
-       char path[10];
 
-       sprintf(path, "%d", entry);
+       color = draw_pixpuf(entry);
+
        model = gtk_tree_view_get_model(GTK_TREE_VIEW(palette_treeview));
-        if (gtk_tree_model_get_iter_from_string(model, &iter, path)) {
+       if (get_iter_by_path_name(&iter, model, entry))
                gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, color, 2, mark_palette[entry].name, -1);
+}
+
+void pick_color(int x, int y)
+{
+       GdkDrawable *draw = gtk_widget_get_window(img_drawing_area);
+       int window_width, window_height;
+       unsigned char c;
+       gdk_drawable_get_size (draw, &window_width, &window_height);
+
+       if (!img_mark_buffer)
+               return;
+
+       /* we need to calculate an offset, since the drawing area is now larger */
+       x -= (window_width - img_width*img_scale_x/16) / 2;
+       y -= (window_height - img_height*img_scale_y/16) / 2;
+
+       x = x*16/img_scale_x;
+       y = y*16/img_scale_y;
+
+       if (x < 0 || x >= img_width || y < 0 || y >= img_height)
+               return;
+
+       c = img_mark_buffer[img_width*y+x];
+       if (c > 0) {
+               GtkTreeIter iter;
+               GtkTreeModel *model;
+               GtkTreeSelection *selection;
+               GtkTreePath *path;
+
+               mark_selected = c - 1;
+               model = gtk_tree_view_get_model(GTK_TREE_VIEW(palette_treeview));
+               selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(palette_treeview));
+               if (get_iter_by_path_name(&iter, model, mark_selected)) {
+                       gtk_tree_selection_select_iter(selection, &iter);
+                       path = gtk_tree_model_get_path(model, &iter);
+                       gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(palette_treeview), path, NULL, FALSE, 0,0);
+                       gtk_tree_path_free(path);
+               }
        }
 }