Add eyedrop tool. The treeview will scroll to the selection.
[colorize.git] / gui / main.c
index b828b14..dd75769 100644 (file)
@@ -9,6 +9,7 @@
 #endif
 #include "main.h"
 #include "menu.h"
+#include "toolbar.h"
 #include "image.h"
 #include "palette.h"
 #include "timeline.h"
@@ -34,7 +35,7 @@ GtkToggleButton *show_flow_button;
 #endif
 int button_down = 0, button_down_x = -1000, button_down_y = -1000, shift_pressed = 0, button_num = 1;
 int brush_size;
-int highlight = 0, preview = 0, rendered = 0, move_mode = 0, fill_mode = 0, flowview = 0;
+int highlight = 0, preview = 0, rendered = 0, draw_mode = 1, move_mode = 0, fill_mode = 0, pick_mode = 0, flowview = 0;
 int mouse_over_palette_area = 0, mouse_over_drawing_area = 0, mouse_over_timeline_area = 0;
 #define min(x,y) ((x < y) ? x : y)
 #define abs(x,y) ((x < y) ? y - x : x - y)
@@ -282,15 +283,21 @@ static gint motion_notify_event( GtkWidget *widget,
        }
 
        if ((state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK))) {
-               if (button_down) {
+               if (button_down && pick_mode) {
+                       pick_color(x, y);
+               }
+               if (button_down && !pick_mode) {
                        if (move_mode) {
                                move_mark((x-button_down_x)*16/img_scale_x, (y-button_down_y)*16/img_scale_y);
                                draw_image(0, 0, -1, -1);
-                       } else {
+                       } else
+                       if (draw_mode) {
                                draw_line(x, y, button_num == 1);
                        }
                }
        } else {
+               if (button_down && pick_mode)
+                       release_pick_mode();
                button_down = 0;
        }
                                                                                   
@@ -306,9 +313,14 @@ static gint button_press_event (GtkWidget *widget, GdkEventButton *event)
        y = event->y;
        button_num = event->button;
 
-       if (!button_down)
+       if (!button_down && !pick_mode)
                copy_mark_to_undo();
+       if (pick_mode) {
+               pick_color(x, y);
+       } else
        if (fill_mode) {
+               if (button_down)
+                       return TRUE;
                fill(x, y, event->button != 1);
                draw_image(0, 0, -1, -1);
        } else
@@ -317,7 +329,8 @@ static gint button_press_event (GtkWidget *widget, GdkEventButton *event)
                        return TRUE;
                button_down_x = x;
                button_down_y = y;
-       } else {
+       } else
+       if (draw_mode) {
                /* if not shift, draw a dot and not a line */
                if (!shift_pressed || button_down_x == -1000) {
                        button_down_x = x;
@@ -480,187 +493,18 @@ void palette_edited(GtkCellRendererText *renderer, gchar *path, gchar *new_text,
 }
 
 /*
- * tool bar
- */
-
-extern const guint8 img_size_1[];
-extern const guint8 img_size_3[];
-extern const guint8 img_size_5[];
-extern const guint8 img_size_9[];
-extern const guint8 img_size_11[];
-extern const guint8 img_size_19[];
-extern const guint8 img_fill[];
-extern const guint8 img_move[];
-
-struct paint_buttons {
-       const guint8 *data;
-       GtkToggleButton *button;
-       int toggle_state;
-       int size;
-       int move;
-       int fill;
-       const char *tooltip;
-} paint_buttons[] = {
-       { img_size_1, NULL, FALSE, 1, 0, 0, "Set pen size to 1" },
-       { img_size_3, NULL, TRUE, 2, 0, 0, "Set pen size to 3" },
-       { img_size_5, NULL, FALSE, 3, 0, 0, "Set pen size to 5" },
-       { img_size_9, NULL, FALSE, 5, 0, 0, "Set pen size to 9" },
-       { img_size_11, NULL, FALSE, 6, 0, 0, "Set pen size to 11" },
-       { img_size_19, NULL, FALSE, 10, 0, 0, "Set pen size to 19" },
-       { img_fill, NULL, FALSE, 0, 0, 1, "FILL marked area" },
-       { img_move, NULL, FALSE, 0, 1, 0, "Move marked pixles" },
-       { NULL, NULL, 0, 0, 0, 0, NULL },
-};
-
-void paint_button_toggled(GtkToggleButton *togglebutton, gpointer index)
-{
-       int i;
-
-       if (paint_buttons[(long)index].toggle_state == gtk_toggle_button_get_active(togglebutton)) {
-               return;
-       }
-
-       for (i = 0; paint_buttons[i].button; i++) {
-               if ((long)index == i) {
-                       paint_buttons[i].toggle_state = TRUE;
-                       gtk_toggle_button_set_active(paint_buttons[i].button, TRUE);
-                       brush_size = paint_buttons[i].size;
-                       move_mode = paint_buttons[i].move;
-                       fill_mode = paint_buttons[i].fill;
-               } else {
-                       paint_buttons[i].toggle_state = FALSE;
-                       gtk_toggle_button_set_active(paint_buttons[i].button, FALSE);
-               }
-       }
-
-}
-
-void zoomin_button_clicked(GtkButton *button, gpointer index)
-{
-       zoom_in_event(NULL);
-}
-
-void zoomout_button_clicked(GtkButton *button, gpointer index)
-{
-       zoom_out_event(NULL);
-}
-
-void highlight_button_toggled(GtkButton *button, gpointer index)
-{
-       highlight = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
-       set_menu_toggel_by_label(TOGGLE_LABEL_HIGHLIGHT, highlight);
-       draw_image(0, 0, -1, -1);
-}
-
-void preview_button_toggled(GtkButton *button, gpointer index)
-{
-       preview = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
-       set_menu_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
-       draw_image(0, 0, -1, -1);
-}
-
-void palette_button_clicked(GtkButton *button, gpointer index)
-{
-       palette_event(NULL);
-}
-
-void bc_button_clicked(GtkButton *button, gpointer index)
-{
-       bc_event(NULL);
-}
-
-void colorize_button_clicked(GtkButton *button, gpointer index)
-{
-       colorize_event(NULL);
-}
-
-void view_colorized_button_toggled(GtkButton *button, gpointer index)
-{
-       if (timeline_frames > 1)
-               rendered = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
-       else
-               rendered = 0;
-       gtk_toggle_button_set_active(show_colorized_button, rendered);
-       set_menu_toggel_by_label(TOGGLE_LABEL_RENDERED, rendered);
-       timeline_select_and_save(timeline_selected, timeline_selected);
-}
-
-#ifdef WITH_OPENCV
-void view_flow_button_toggled(GtkButton *button, gpointer index)
-{
-       if (flow_enable && timeline_frames > 1)
-               flowview = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
-       else
-               flowview = 0;
-       gtk_toggle_button_set_active(show_flow_button, flowview);
-       set_menu_toggel_by_label(TOGGLE_LABEL_FLOWVIEW, flowview);
-       timeline_select_and_save(timeline_selected, timeline_selected);
-}
-#endif
-
-extern const guint8 img_zoomin[];
-extern const guint8 img_zoomout[];
-extern const guint8 img_pal[];
-extern const guint8 img_bc[];
-extern const guint8 img_highlight[];
-extern const guint8 img_preview[];
-extern const guint8 img_eye[];
-extern const guint8 img_col[];
-extern const guint8 img_flow[];
-
-struct tool_buttons {
-       enum toggle_label label;
-       GtkWidget *widget;
-       const guint8 *data;
-       int toggle;
-       void (*handler)(GtkButton *togglebutton, gpointer priv);
-       GtkToggleButton **button;
-       const char *tooltip;
-} tool_buttons[] = {
-       { TOGGLE_LABEL_NONE, NULL, img_zoomin, 0, zoomin_button_clicked, NULL, "Zoom in" },
-       { TOGGLE_LABEL_NONE, NULL, img_zoomout, 0, zoomout_button_clicked, NULL, "Zoom out" },
-       { TOGGLE_LABEL_NONE, NULL, img_pal, 0, palette_button_clicked, NULL, "Palette dialog" },
-       { TOGGLE_LABEL_NONE, NULL, img_bc, 0, bc_button_clicked, NULL, "Brightness+Contrast dialog" },
-       { TOGGLE_LABEL_HIGHLIGHT, NULL, img_highlight, 1, highlight_button_toggled, &show_highlighted_button, "Highlight selected mark color" },
-       { TOGGLE_LABEL_PREVIEW, NULL, img_preview, 1, preview_button_toggled, &show_preview_button, "Show preview of selected mark color" },
-       { TOGGLE_LABEL_RENDERED, NULL, img_eye, 1, view_colorized_button_toggled, &show_colorized_button, "Show result of a rendered sequence" },
-#ifdef WITH_OPENCV
-       { TOGGLE_LABEL_FLOWVIEW, NULL, img_flow, 1, view_flow_button_toggled, &show_flow_button, "Show optical flow" },
-#endif
-       { TOGGLE_LABEL_NONE, NULL, img_col, 0, colorize_button_clicked, NULL, "Colorize current image" },
-       { TOGGLE_LABEL_NONE, NULL, NULL, 0, NULL, NULL, NULL },
-};
-
-void set_button_toggel_by_label(enum toggle_label label, gboolean active)
-{
-       int i;
-
-       for (i = 0; tool_buttons[i].data; i++) {
-               if (tool_buttons[i].label == label)
-                       gtk_toggle_button_set_active((GtkToggleButton *)tool_buttons[i].widget, active);
-       }
-}
-
-/*
  * creation of main window
  */
 
 int main(int argc, char *argv[])
 {
-       GtkWidget *vbox, *tool_box;
+       GtkWidget *vbox, *tool_bar;
        GtkWidget *paned;
        GtkWidget *pal_scroll;
        GtkWidget *menu_bar;
-       GtkWidget *separator;
        GtkTreeSelection *selection;
        GtkTreeViewColumn *palette_column;
        GtkCellRenderer *palette_renderer;
-       GtkToggleButton *button;
-       GtkWidget *image;
-       GdkPixbuf *pixbuf;
-       GtkTooltips *tooltips;
-
-       int i;
 
        gtk_init(&argc, &argv);
 
@@ -689,54 +533,11 @@ int main(int argc, char *argv[])
        gtk_widget_show(menu_bar);
        create_menus(menu_bar);
 
-       /* add tool_box to vbox (bottom part of palette box) */
-       tool_box = gtk_hbox_new(FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(vbox), tool_box, FALSE, FALSE, 2);
-       gtk_widget_show(tool_box);
-
-       for (i = 0; paint_buttons[i].data; i++) {
-               pixbuf = gdk_pixbuf_new_from_inline(-1, paint_buttons[i].data, FALSE, NULL);
-               image = gtk_image_new_from_pixbuf(pixbuf);
-               gtk_widget_show(GTK_WIDGET(image));
-               paint_buttons[i].button = button = (GtkToggleButton *) gtk_toggle_button_new();
-               if (paint_buttons[i].toggle_state) {
-                       gtk_toggle_button_set_active(button, TRUE);
-                       brush_size = paint_buttons[i].size;
-               }
-               g_signal_connect(button, "toggled", G_CALLBACK(paint_button_toggled), (void *)((long)i));
-               tooltips = gtk_tooltips_new();
-               gtk_tooltips_set_tip(tooltips, GTK_WIDGET(button), paint_buttons[i].tooltip, NULL);
-               gtk_container_add (GTK_CONTAINER (button), image);
-               gtk_widget_show(GTK_WIDGET(button));
-               gtk_box_pack_start(GTK_BOX(tool_box), GTK_WIDGET(button), FALSE, FALSE, 2);
-       }
-
-       for (i = 0; tool_buttons[i].data; i++) {
-               if (i == 0 || i == 2) {
-                       /* add vertical seperation to hbox */
-                       separator = gtk_vseparator_new();
-                       gtk_widget_show(separator);
-                       gtk_box_pack_start(GTK_BOX(tool_box), separator, FALSE, FALSE, 3);
-               }
-               pixbuf = gdk_pixbuf_new_from_inline(-1, tool_buttons[i].data, FALSE, NULL);
-               image = gtk_image_new_from_pixbuf(pixbuf);
-               gtk_widget_show(GTK_WIDGET(image));
-               if (tool_buttons[i].toggle) {
-                       button = (GtkToggleButton *) gtk_toggle_button_new();
-                       g_signal_connect(button, "toggled", G_CALLBACK(tool_buttons[i].handler), NULL);
-               } else {
-                       button = (GtkToggleButton *) gtk_button_new();
-                       g_signal_connect(button, "clicked", G_CALLBACK(tool_buttons[i].handler), NULL);
-               }
-               tool_buttons[i].widget = (GtkWidget *)button;
-               tooltips = gtk_tooltips_new();
-               gtk_tooltips_set_tip(tooltips, GTK_WIDGET(button), tool_buttons[i].tooltip, NULL);
-               if (tool_buttons[i].button)
-                       *(tool_buttons[i].button) = button;
-               gtk_container_add (GTK_CONTAINER (button), image);
-               gtk_widget_show(GTK_WIDGET(button));
-               gtk_box_pack_start(GTK_BOX(tool_box), GTK_WIDGET(button), FALSE, FALSE, 2);
-       }
+       /* add tool_bar to vbox (bottom part of palette box) */
+       tool_bar = gtk_hbox_new(FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), tool_bar, FALSE, FALSE, 2);
+       gtk_widget_show(tool_bar);
+       create_toolbar(tool_bar);
 
        /* add paned view to vbox (middle part of vbox) */
        paned = gtk_hpaned_new ();