Move GTK tool bar and functions into seperate file
[colorize.git] / gui / toolbar.c
1 #include <gtk/gtk.h>
2 #include <gdk/gdkkeysyms.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include "../src/mark.h"
7 #ifdef WITH_OPENCV
8 #include "../src/opticalflow.h"
9 #endif
10 #include "main.h"
11 #include "menu.h"
12 #include "toolbar.h"
13 #include "image.h"
14 #include "palette.h"
15 #include "timeline.h"
16 #include "colorize.h"
17 #include "brightcontrast.h"
18 #include "level.h"
19 #include "fill.h"
20 #ifdef WITH_OPENCV
21 #include "flow.h"
22 #endif
23
24 /*
25  * tool bar
26  */
27
28 extern const guint8 img_size_1[];
29 extern const guint8 img_size_3[];
30 extern const guint8 img_size_5[];
31 extern const guint8 img_size_9[];
32 extern const guint8 img_size_11[];
33 extern const guint8 img_size_19[];
34 extern const guint8 img_fill[];
35 extern const guint8 img_move[];
36
37 struct paint_buttons {
38         const guint8 *data;
39         GtkToggleButton *button;
40         int toggle_state;
41         int size;
42         int move;
43         int fill;
44         const char *tooltip;
45 } paint_buttons[] = {
46         { img_size_1, NULL, FALSE, 1, 0, 0, "Set pen size to 1" },
47         { img_size_3, NULL, TRUE, 2, 0, 0, "Set pen size to 3" },
48         { img_size_5, NULL, FALSE, 3, 0, 0, "Set pen size to 5" },
49         { img_size_9, NULL, FALSE, 5, 0, 0, "Set pen size to 9" },
50         { img_size_11, NULL, FALSE, 6, 0, 0, "Set pen size to 11" },
51         { img_size_19, NULL, FALSE, 10, 0, 0, "Set pen size to 19" },
52         { img_fill, NULL, FALSE, 0, 0, 1, "FILL marked area" },
53         { img_move, NULL, FALSE, 0, 1, 0, "Move marked pixles" },
54         { NULL, NULL, 0, 0, 0, 0, NULL },
55 };
56
57 void paint_button_toggled(GtkToggleButton *togglebutton, gpointer index)
58 {
59         int i;
60
61         if (paint_buttons[(long)index].toggle_state == gtk_toggle_button_get_active(togglebutton)) {
62                 return;
63         }
64
65         for (i = 0; paint_buttons[i].button; i++) {
66                 if ((long)index == i) {
67                         paint_buttons[i].toggle_state = TRUE;
68                         gtk_toggle_button_set_active(paint_buttons[i].button, TRUE);
69                         brush_size = paint_buttons[i].size;
70                         move_mode = paint_buttons[i].move;
71                         fill_mode = paint_buttons[i].fill;
72                 } else {
73                         paint_buttons[i].toggle_state = FALSE;
74                         gtk_toggle_button_set_active(paint_buttons[i].button, FALSE);
75                 }
76         }
77
78 }
79
80 void zoomin_button_clicked(GtkButton *button, gpointer index)
81 {
82         zoom_in_event(NULL);
83 }
84
85 void zoomout_button_clicked(GtkButton *button, gpointer index)
86 {
87         zoom_out_event(NULL);
88 }
89
90 void highlight_button_toggled(GtkButton *button, gpointer index)
91 {
92         highlight = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
93         set_menu_toggel_by_label(TOGGLE_LABEL_HIGHLIGHT, highlight);
94         draw_image(0, 0, -1, -1);
95 }
96
97 void preview_button_toggled(GtkButton *button, gpointer index)
98 {
99         preview = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
100         set_menu_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
101         draw_image(0, 0, -1, -1);
102 }
103
104 void palette_button_clicked(GtkButton *button, gpointer index)
105 {
106         palette_event(NULL);
107 }
108
109 void bc_button_clicked(GtkButton *button, gpointer index)
110 {
111         bc_event(NULL);
112 }
113
114 void colorize_button_clicked(GtkButton *button, gpointer index)
115 {
116         colorize_event(NULL);
117 }
118
119 void view_colorized_button_toggled(GtkButton *button, gpointer index)
120 {
121         if (timeline_frames > 1)
122                 rendered = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
123         else
124                 rendered = 0;
125         gtk_toggle_button_set_active(show_colorized_button, rendered);
126         set_menu_toggel_by_label(TOGGLE_LABEL_RENDERED, rendered);
127         timeline_select_and_save(timeline_selected, timeline_selected);
128 }
129
130 #ifdef WITH_OPENCV
131 void view_flow_button_toggled(GtkButton *button, gpointer index)
132 {
133         if (flow_enable && timeline_frames > 1)
134                 flowview = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
135         else
136                 flowview = 0;
137         gtk_toggle_button_set_active(show_flow_button, flowview);
138         set_menu_toggel_by_label(TOGGLE_LABEL_FLOWVIEW, flowview);
139         timeline_select_and_save(timeline_selected, timeline_selected);
140 }
141 #endif
142
143 extern const guint8 img_zoomin[];
144 extern const guint8 img_zoomout[];
145 extern const guint8 img_pal[];
146 extern const guint8 img_bc[];
147 extern const guint8 img_highlight[];
148 extern const guint8 img_preview[];
149 extern const guint8 img_eye[];
150 extern const guint8 img_col[];
151 extern const guint8 img_flow[];
152
153 struct tool_buttons {
154         enum toggle_label label;
155         GtkWidget *widget;
156         const guint8 *data;
157         int toggle;
158         void (*handler)(GtkButton *togglebutton, gpointer priv);
159         GtkToggleButton **button;
160         const char *tooltip;
161 } tool_buttons[] = {
162         { TOGGLE_LABEL_NONE, NULL, img_zoomin, 0, zoomin_button_clicked, NULL, "Zoom in" },
163         { TOGGLE_LABEL_NONE, NULL, img_zoomout, 0, zoomout_button_clicked, NULL, "Zoom out" },
164         { TOGGLE_LABEL_NONE, NULL, img_pal, 0, palette_button_clicked, NULL, "Palette dialog" },
165         { TOGGLE_LABEL_NONE, NULL, img_bc, 0, bc_button_clicked, NULL, "Brightness+Contrast dialog" },
166         { TOGGLE_LABEL_HIGHLIGHT, NULL, img_highlight, 1, highlight_button_toggled, &show_highlighted_button, "Highlight selected mark color" },
167         { TOGGLE_LABEL_PREVIEW, NULL, img_preview, 1, preview_button_toggled, &show_preview_button, "Show preview of selected mark color" },
168         { TOGGLE_LABEL_RENDERED, NULL, img_eye, 1, view_colorized_button_toggled, &show_colorized_button, "Show result of a rendered sequence" },
169 #ifdef WITH_OPENCV
170         { TOGGLE_LABEL_FLOWVIEW, NULL, img_flow, 1, view_flow_button_toggled, &show_flow_button, "Show optical flow" },
171 #endif
172         { TOGGLE_LABEL_NONE, NULL, img_col, 0, colorize_button_clicked, NULL, "Colorize current image" },
173         { TOGGLE_LABEL_NONE, NULL, NULL, 0, NULL, NULL, NULL },
174 };
175
176 void create_toolbar(GtkWidget *tool_bar)
177 {
178         int i;
179         GdkPixbuf *pixbuf;
180         GtkWidget *image;
181         GtkToggleButton *button;
182         GtkTooltips *tooltips;
183         GtkWidget *separator;
184
185         for (i = 0; paint_buttons[i].data; i++) {
186                 pixbuf = gdk_pixbuf_new_from_inline(-1, paint_buttons[i].data, FALSE, NULL);
187                 image = gtk_image_new_from_pixbuf(pixbuf);
188                 gtk_widget_show(GTK_WIDGET(image));
189                 paint_buttons[i].button = button = (GtkToggleButton *) gtk_toggle_button_new();
190                 if (paint_buttons[i].toggle_state) {
191                         gtk_toggle_button_set_active(button, TRUE);
192                         brush_size = paint_buttons[i].size;
193                 }
194                 g_signal_connect(button, "toggled", G_CALLBACK(paint_button_toggled), (void *)((long)i));
195                 tooltips = gtk_tooltips_new();
196                 gtk_tooltips_set_tip(tooltips, GTK_WIDGET(button), paint_buttons[i].tooltip, NULL);
197                 gtk_container_add (GTK_CONTAINER (button), image);
198                 gtk_widget_show(GTK_WIDGET(button));
199                 gtk_box_pack_start(GTK_BOX(tool_bar), GTK_WIDGET(button), FALSE, FALSE, 2);
200         }
201
202         for (i = 0; tool_buttons[i].data; i++) {
203                 if (i == 0 || i == 2) {
204                         /* add vertical seperation to hbox */
205                         separator = gtk_vseparator_new();
206                         gtk_widget_show(separator);
207                         gtk_box_pack_start(GTK_BOX(tool_bar), separator, FALSE, FALSE, 3);
208                 }
209                 pixbuf = gdk_pixbuf_new_from_inline(-1, tool_buttons[i].data, FALSE, NULL);
210                 image = gtk_image_new_from_pixbuf(pixbuf);
211                 gtk_widget_show(GTK_WIDGET(image));
212                 if (tool_buttons[i].toggle) {
213                         button = (GtkToggleButton *) gtk_toggle_button_new();
214                         g_signal_connect(button, "toggled", G_CALLBACK(tool_buttons[i].handler), NULL);
215                 } else {
216                         button = (GtkToggleButton *) gtk_button_new();
217                         g_signal_connect(button, "clicked", G_CALLBACK(tool_buttons[i].handler), NULL);
218                 }
219                 tool_buttons[i].widget = (GtkWidget *)button;
220                 tooltips = gtk_tooltips_new();
221                 gtk_tooltips_set_tip(tooltips, GTK_WIDGET(button), tool_buttons[i].tooltip, NULL);
222                 if (tool_buttons[i].button)
223                         *(tool_buttons[i].button) = button;
224                 gtk_container_add (GTK_CONTAINER (button), image);
225                 gtk_widget_show(GTK_WIDGET(button));
226                 gtk_box_pack_start(GTK_BOX(tool_bar), GTK_WIDGET(button), FALSE, FALSE, 2);
227         }
228 }
229
230 void set_button_toggel_by_label(enum toggle_label label, gboolean active)
231 {
232         int i;
233
234         for (i = 0; tool_buttons[i].data; i++) {
235                 if (tool_buttons[i].label == label)
236                         gtk_toggle_button_set_active((GtkToggleButton *)tool_buttons[i].widget, active);
237         }
238 }
239