Add eyedrop tool. The treeview will scroll to the selection.
[colorize.git] / gui / main.c
index c89da3c..dd75769 100644 (file)
@@ -8,6 +8,8 @@
 #include "../src/opticalflow.h"
 #endif
 #include "main.h"
+#include "menu.h"
+#include "toolbar.h"
 #include "image.h"
 #include "palette.h"
 #include "timeline.h"
@@ -33,23 +35,11 @@ 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)
 
-/* the labels are used to identify menu and button items */
-enum toggle_label {
-       TOGGLE_LABEL_NONE = 0,
-       TOGGLE_LABEL_HIGHLIGHT,
-       TOGGLE_LABEL_PREVIEW,
-       TOGGLE_LABEL_RENDERED,
-       TOGGLE_LABEL_FLOWVIEW,
-       TOGGLE_LABEL_ZOOMFIELDS,
-};
-
-static void set_button_toggel_by_label(enum toggle_label label, gboolean active);
-
 /* error requester */
 void printerror(const char *fmt, ...)
 {
@@ -102,11 +92,6 @@ static void destroy(GtkWidget *widget, gpointer data)
  * keypress
  */
 
-static void undo_event(gpointer priv);
-static void redo_event(gpointer priv);
-static void copy_event(gpointer priv);
-static void paste_event(gpointer priv);
-
 /* event handler for main window keys */
 static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
 {
@@ -298,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;
        }
                                                                                   
@@ -322,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
@@ -333,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;
@@ -407,10 +404,10 @@ static gint timeline_expose_event (GtkWidget *widget, GdkEventExpose *event)
  * colorselection
  */
 
-/* color selection's buttons have been pressed */
 GtkWidget *colorseldlg = NULL;
 GtkColorSelection *colorsel;
-static void color_response(GtkDialog *dialog, gint response_id, gpointer user_data)
+/* color selection's buttons have been pressed */
+void color_response(GtkDialog *dialog, gint response_id, gpointer user_data)
 {
        GdkColor ncolor;
 
@@ -433,359 +430,6 @@ new_color:
 }
 
 /*
- * menus
- */
-
-static void open_event(gpointer priv)
-{
-       GtkWidget *dialog;
-       char *filename = NULL;
-       
-       if (anything_modified) {
-               int ret;
-               GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW(main_window),
-                       GTK_DIALOG_DESTROY_WITH_PARENT,
-                       GTK_MESSAGE_ERROR,
-                       GTK_BUTTONS_OK_CANCEL,
-                       "Image has been changed, really open new image?");
-               ret = gtk_dialog_run(GTK_DIALOG (dialog));
-               gtk_widget_destroy(dialog);
-               if (ret != GTK_RESPONSE_OK)
-                       return;
-       }
-
-       dialog = gtk_file_chooser_dialog_new("Select first grey image",
-               GTK_WINDOW(main_window),
-               GTK_FILE_CHOOSER_ACTION_OPEN,
-               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-               GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-               NULL);
-
-       if (frame_list)
-               gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), frame_list[timeline_selected].filename);
-       if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
-               filename = strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
-       }
-
-       gtk_widget_destroy(dialog);
-
-       if (filename) {
-// do not save         timeline_select_and_save(timeline_selected, timeline_selected);
-               
-               create_timeline(filename);
-               if (frame_list)
-                       create_image(frame_list[timeline_selected].filename, 1);
-               free(filename);
-       }
-
-}
-
-static void save_event(gpointer priv)
-{
-       timeline_select_and_save(timeline_selected, timeline_selected);
-}
-
-static void exit_event(gpointer priv)
-{
-       main_destroy();
-}
-
-static void color_changed(GtkWidget *widget, GtkColorSelection *colorsel)
-{
-}
-
-static void undo_event(gpointer priv)
-{
-       copy_undo_to_mark(0);
-       draw_image(0, 0, -1, -1);
-}
-
-static void redo_event(gpointer priv)
-{
-       copy_undo_to_mark(1);
-       draw_image(0, 0, -1, -1);
-}
-
-static void copy_event(gpointer priv)
-{
-       copy_color(mark_selected + 1);
-}
-
-static void copy_all_event(gpointer priv)
-{
-       copy_color(0);
-}
-
-static void paste_event(gpointer priv)
-{
-       copy_mark_to_undo();
-       paste_color();
-       draw_image(0, 0, -1, -1);
-}
-
-static void color_destroy(GtkWidget *widget, gpointer data)
-{
-       colorseldlg = NULL;
-}
-
-static void palette_event(gpointer priv)
-{
-       GdkColor color;
-
-       if (colorseldlg) {
-               gtk_widget_destroy(colorseldlg);
-               colorseldlg = NULL;
-               return;
-       }
-
-       color.red = mark_palette[mark_selected].r * 256 + 128;
-       color.green = mark_palette[mark_selected].g * 256 + 128;
-       color.blue = mark_palette[mark_selected].b * 256 + 128;
-
-       colorseldlg = gtk_color_selection_dialog_new("Select Color");
-       colorsel = GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (colorseldlg)->colorsel);
-       gtk_color_selection_set_previous_color (colorsel, &color);
-       gtk_color_selection_set_current_color (colorsel, &color);
-       g_signal_connect(G_OBJECT(colorsel), "color_changed", G_CALLBACK(color_changed), (gpointer)colorsel);
-       g_signal_connect(G_OBJECT(colorseldlg), "response", G_CALLBACK(color_response), NULL);
-       g_signal_connect(G_OBJECT(colorseldlg), "destroy", G_CALLBACK(color_destroy), NULL);
-
-
-       gtk_widget_show(colorseldlg);
-}
-
-static void keyframe_event(gpointer priv)
-{
-       toggle_keyframe();
-}
-
-static void erase_event(gpointer priv)
-{
-       copy_mark_to_undo();
-       erase_mark(0);
-       draw_image(0, 0, -1, -1);
-}
-
-static void erase_color_event(gpointer priv)
-{
-       copy_mark_to_undo();
-       erase_mark(mark_selected + 1);
-       draw_image(0, 0, -1, -1);
-}
-
-static void zoom_in_event(gpointer priv)
-{
-       if (img_scale_x < 64) {
-               int aspect = img_scale_y / img_scale_x;
-               img_scale_x += 4;
-               img_scale_y = img_scale_x * aspect;
-               create_or_reset_pixbuf(0);
-       }
-}
-
-static void zoom_out_event(gpointer priv)
-{
-       if (img_scale_x > 4) {
-               int aspect = img_scale_y / img_scale_x;
-               img_scale_x -= 4;
-               img_scale_y = img_scale_x * aspect;
-               create_or_reset_pixbuf(0);
-       }
-}
-
-static void zoom_100_event(gpointer priv)
-{
-       img_scale_x = 16;
-       img_scale_y = 16;
-       create_or_reset_pixbuf(0);
-}
-
-static void zoom_field_event(gpointer priv)
-{
-       if (gtk_check_menu_item_get_active((GtkCheckMenuItem *)priv))
-               img_scale_y = img_scale_x*2;
-       else
-               img_scale_y = img_scale_x;
-       create_or_reset_pixbuf(1);
-}
-
-static void show_highlighted_event(gpointer priv)
-{
-       highlight = gtk_check_menu_item_get_active((GtkCheckMenuItem *)priv);
-       set_button_toggel_by_label(TOGGLE_LABEL_HIGHLIGHT, highlight);
-}
-
-static void show_preview_event(gpointer priv)
-{
-       preview = gtk_check_menu_item_get_active((GtkCheckMenuItem *)priv);
-       set_button_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
-}
-
-static void colorize_event(gpointer priv)
-{
-       if (rendered) {
-               printerror("Disable colorized view first.");
-               return;
-       }
-#ifdef WITH_OPENCV
-       if (flowview) {
-               printerror("Disable flow view first.");
-               return;
-       }
-#endif
-       if (img_mark_buffer)
-               colorize_image();
-}
-
-static void show_colorized_event(gpointer priv)
-{
-       if (timeline_frames > 1)
-               rendered = gtk_check_menu_item_get_active((GtkCheckMenuItem *)priv);
-       else
-               rendered = 0;
-       gtk_check_menu_item_set_active((GtkCheckMenuItem *)priv, rendered);
-       set_button_toggel_by_label(TOGGLE_LABEL_RENDERED, rendered);
-}
-
-#ifdef WITH_OPENCV
-static void show_flow_event(gpointer priv)
-{
-       if (flow_enable && timeline_frames > 1)
-               flowview = gtk_check_menu_item_get_active((GtkCheckMenuItem *)priv);
-       else
-               flowview = 0;
-       gtk_check_menu_item_set_active((GtkCheckMenuItem *)priv, flowview);
-       set_button_toggel_by_label(TOGGLE_LABEL_FLOWVIEW, flowview);
-}
-#endif
-
-static void dummy_event(gpointer priv)
-{
-}
-
-struct my_menu_item {
-       enum toggle_label label;
-       GtkWidget *widget;
-       const char *text;
-       void (*handler)(gpointer priv);
-       gchar *stock;
-};
-
-struct my_menu {
-       const char *text;
-       struct my_menu_item *items;
-};
-
-struct my_menu_item file_menu_items[] = {
-       { TOGGLE_LABEL_NONE, NULL, "Open", open_event, GTK_STOCK_OPEN },
-       { TOGGLE_LABEL_NONE, NULL, "Save", save_event, GTK_STOCK_SAVE },
-       { TOGGLE_LABEL_NONE, NULL, "", dummy_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Exit", exit_event, GTK_STOCK_QUIT },
-       { TOGGLE_LABEL_NONE, NULL, NULL, NULL }
-};
-
-struct my_menu_item edit_menu_items[] = {
-       { TOGGLE_LABEL_NONE, NULL, "Undo", undo_event, GTK_STOCK_UNDO },
-       { TOGGLE_LABEL_NONE, NULL, "Redo", redo_event, GTK_STOCK_REDO },
-       { TOGGLE_LABEL_NONE, NULL, "", dummy_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Copy", copy_event, GTK_STOCK_COPY },
-       { TOGGLE_LABEL_NONE, NULL, "Copy All", copy_all_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Paste", paste_event, GTK_STOCK_PASTE },
-       { TOGGLE_LABEL_NONE, NULL, "", dummy_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Palette", palette_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Bright/Contrast", bc_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Adjust Levels", level_event, NULL },
-#ifdef WITH_OPENCV
-       { TOGGLE_LABEL_NONE, NULL, "Optical Flow", flow_event, NULL },
-#endif
-       { TOGGLE_LABEL_NONE, NULL, "", dummy_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Keyframe", keyframe_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "", dummy_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "Clear", erase_event, GTK_STOCK_CLEAR },
-       { TOGGLE_LABEL_NONE, NULL, "Clear Color", erase_color_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, NULL, NULL }
-};
-
-struct my_menu_item view_menu_items[] = {
-       { TOGGLE_LABEL_NONE, NULL, "Zoom In", zoom_in_event, GTK_STOCK_ZOOM_IN },
-       { TOGGLE_LABEL_NONE, NULL, "Zoom Out", zoom_out_event, GTK_STOCK_ZOOM_OUT },
-       { TOGGLE_LABEL_NONE, NULL, "Zoom 100", zoom_100_event, GTK_STOCK_ZOOM_100 },
-       { TOGGLE_LABEL_ZOOMFIELDS, NULL, "Zoom Field", zoom_field_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, "", dummy_event, NULL },
-       { TOGGLE_LABEL_HIGHLIGHT, NULL, "Highlight color", show_highlighted_event, NULL },
-       { TOGGLE_LABEL_PREVIEW, NULL, "Preview color", show_preview_event, NULL },
-       { TOGGLE_LABEL_RENDERED, NULL, "Show colorized", show_colorized_event, NULL },
-#ifdef WITH_OPENCV
-       { TOGGLE_LABEL_FLOWVIEW, NULL, "Show flow", show_flow_event, NULL },
-#endif
-       { TOGGLE_LABEL_NONE, NULL, NULL, NULL }
-};
-
-struct my_menu_item render_menu_items[] = {
-       { TOGGLE_LABEL_NONE, NULL, "Colorize Image", colorize_event, NULL },
-       { TOGGLE_LABEL_NONE, NULL, NULL, NULL }
-};
-
-struct my_menu menus[] = {
-       { "_File", file_menu_items },
-       { "_Edit", edit_menu_items },
-       { "_View", view_menu_items },
-       { "_Render", render_menu_items },
-       { NULL, NULL }
-};
-
-/* create menu bar */
-static void create_menus(GtkWidget *menu_bar)
-{
-       GtkWidget *root_menu;
-       GtkWidget *menu;
-       GtkWidget *menu_item;
-       struct my_menu_item *items;
-       int i, j;
-
-       /* create vbox and add menu_bar */
-       for (i = 0; menus[i].text; i++) {
-               /* create menu with items */
-               items = menus[i].items;
-               menu = gtk_menu_new();
-               for (j = 0; items[j].text; j++) {
-                       if (!items[j].text[0])
-                               menu_item = gtk_separator_menu_item_new();
-                       else if (items[j].stock)
-                               menu_item = gtk_image_menu_item_new_from_stock(items[j].stock, NULL);
-                       else if (items[j].label != TOGGLE_LABEL_NONE)
-                               menu_item = gtk_check_menu_item_new_with_label(items[j].text);
-                       else
-                               menu_item = gtk_menu_item_new_with_label(items[j].text);
-                       items[j].widget = menu_item;
-                       gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
-                       g_signal_connect_swapped(menu_item, "activate", G_CALLBACK(items[j].handler), menu_item);
-                       gtk_widget_show(menu_item);
-               }
-               /* create root menu and add to menu_bar */
-               root_menu = gtk_menu_item_new_with_mnemonic(menus[i].text);
-               gtk_widget_show(root_menu);
-               gtk_menu_item_set_submenu(GTK_MENU_ITEM(root_menu), menu);
-               gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), root_menu);
-       }
-
-}
-
-static void set_menu_toggel_by_label(enum toggle_label label, gboolean active)
-{
-       struct my_menu_item *items;
-       int i, j;
-
-       for (i = 0; menus[i].text; i++) {
-               items = menus[i].items;
-               for (j = 0; items[j].text; j++) {
-                       if (items[j].label == label)
-                               gtk_check_menu_item_set_active((GtkCheckMenuItem *)items[j].widget, active);
-               }
-       }
-}
-
-/*
  * palette
  */
 
@@ -849,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 },
-};
-
-static 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);
 
@@ -1058,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 ();