Moved color model settings and optical flow settings to a single *_settings 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 "diag_bc.h"
18 #include "diag_level.h"
19 #include "fill.h"
20 #ifdef WITH_OPENCV
21 #include "diag_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 extern const guint8 img_pick[];
37
38 struct paint_buttons {
39         const guint8 *data;
40         GtkToggleButton *button;
41         int toggle_state;
42         int size;
43         int draw;
44         int move;
45         int fill;
46         int pick;
47         const char *tooltip;
48 } paint_buttons[] = {
49         { img_size_1,   NULL, FALSE, 1,  1, 0, 0, 0, "Set pen size to 1" },
50         { img_size_3,   NULL, TRUE,  2,  1, 0, 0, 0, "Set pen size to 3" },
51         { img_size_5,   NULL, FALSE, 3,  1, 0, 0, 0, "Set pen size to 5" },
52         { img_size_9,   NULL, FALSE, 5,  1, 0, 0, 0, "Set pen size to 9" },
53         { img_size_11,  NULL, FALSE, 6,  1, 0, 0, 0, "Set pen size to 11" },
54         { img_size_19,  NULL, FALSE, 10, 1, 0, 0, 0, "Set pen size to 19" },
55         { img_fill,     NULL, FALSE, 0,  0, 0, 1, 0, "FILL marked area" },
56         { img_move,     NULL, FALSE, 0,  0, 1, 0, 0, "Move marked pixles" },
57         { img_pick,     NULL, FALSE, 0,  0, 0, 0, 1, "Pick color from marked area" },
58         { NULL,         NULL, FALSE, 0,  0, 0, 0, 0, NULL },
59 };
60
61 void paint_button_toggled(GtkToggleButton *togglebutton, gpointer index)
62 {
63         int i;
64
65         if (paint_buttons[(long)index].toggle_state == gtk_toggle_button_get_active(togglebutton)) {
66                 return;
67         }
68
69         /* picker just toggles */
70         if (paint_buttons[(long)index].pick) {
71                 i = (long)index;
72                 if (paint_buttons[i].toggle_state == FALSE) {
73                         paint_buttons[i].toggle_state = TRUE;
74                         gtk_toggle_button_set_active(paint_buttons[i].button, TRUE);
75                         pick_mode = 1;
76                 } else
77                         release_pick_mode();
78                 return;
79         }
80
81         for (i = 0; paint_buttons[i].button; i++) {
82                 if ((long)index == i) {
83                         paint_buttons[i].toggle_state = TRUE;
84                         gtk_toggle_button_set_active(paint_buttons[i].button, TRUE);
85                         brush_size = paint_buttons[i].size;
86                         draw_mode = paint_buttons[i].draw;
87                         move_mode = paint_buttons[i].move;
88                         fill_mode = paint_buttons[i].fill;
89                         pick_mode = 0;
90                 } else {
91                         paint_buttons[i].toggle_state = FALSE;
92                         gtk_toggle_button_set_active(paint_buttons[i].button, FALSE);
93                 }
94         }
95
96 }
97
98 void release_pick_mode(void)
99 {
100         int i;
101
102         pick_mode = 0;
103         for (i = 0; paint_buttons[i].button; i++) {
104                 if (paint_buttons[i].pick) {
105                         paint_buttons[i].toggle_state = FALSE;
106                         gtk_toggle_button_set_active(paint_buttons[i].button, FALSE);
107                 }
108         }
109 }
110
111 void zoomin_button_clicked(GtkButton *button, gpointer index)
112 {
113         zoom_in_event(NULL);
114 }
115
116 void zoomout_button_clicked(GtkButton *button, gpointer index)
117 {
118         zoom_out_event(NULL);
119 }
120
121 void mark_button_toggled(GtkButton *button, gpointer index)
122 {
123         mark = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
124         set_menu_toggel_by_label(TOGGLE_LABEL_MARK, mark);
125         if (mark == 1 && highlight == 1) {
126                 highlight = 0;
127                 set_menu_toggel_by_label(TOGGLE_LABEL_HIGHLIGHT, highlight);
128                 set_button_toggel_by_label(TOGGLE_LABEL_HIGHLIGHT, highlight);
129         }
130         draw_image(0, 0, -1, -1);
131 }
132
133 void highlight_button_toggled(GtkButton *button, gpointer index)
134 {
135         highlight = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
136         set_menu_toggel_by_label(TOGGLE_LABEL_HIGHLIGHT, highlight);
137         if (mark == 1 && highlight == 1) {
138                 mark = 0;
139                 set_menu_toggel_by_label(TOGGLE_LABEL_MARK, mark);
140                 set_button_toggel_by_label(TOGGLE_LABEL_MARK, mark);
141         }
142         draw_image(0, 0, -1, -1);
143 }
144
145 void preview_button_toggled(GtkButton *button, gpointer index)
146 {
147         preview = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
148         if (preview) {
149                 create_preview();
150                 if (!img_preview_buffer)
151                         preview = 0;
152         } else if (!rendered)
153                 destroy_preview();
154         set_button_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
155         set_menu_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
156         draw_image(0, 0, -1, -1);
157
158         if (preview && rendered) {
159                 rendered = 0;
160                 set_button_toggel_by_label(TOGGLE_LABEL_RENDERED, rendered);
161                 set_menu_toggel_by_label(TOGGLE_LABEL_RENDERED, rendered);
162         }
163 }
164
165 void palette_button_clicked(GtkButton *button, gpointer index)
166 {
167         palette_event(NULL);
168 }
169
170 void bc_button_clicked(GtkButton *button, gpointer index)
171 {
172         bc_event(NULL);
173 }
174
175 void colorize_button_clicked(GtkButton *button, gpointer index)
176 {
177         colorize_event(NULL);
178 }
179
180 void view_colorized_button_toggled(GtkButton *button, gpointer index)
181 {
182         if (timeline_frames > 1)
183                 rendered = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
184         else
185                 rendered = 0;
186         if (rendered) {
187                 create_rendered(img_name);
188                 if (!img_preview_buffer)
189                         rendered = 0;
190         } else if (!preview)
191                 destroy_preview();
192         gtk_toggle_button_set_active(show_colorized_button, rendered);
193         set_menu_toggel_by_label(TOGGLE_LABEL_RENDERED, rendered);
194         draw_image(0, 0, -1, -1);
195
196         if (rendered && preview) {
197                 preview = 0;
198                 set_button_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
199                 set_menu_toggel_by_label(TOGGLE_LABEL_PREVIEW, preview);
200         }
201 }
202
203 #ifdef WITH_OPENCV
204 void view_flow_button_toggled(GtkButton *button, gpointer index)
205 {
206         if (flow_enable && timeline_frames > 1)
207                 flowview = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
208         else
209                 flowview = 0;
210         gtk_toggle_button_set_active(show_flow_button, flowview);
211         set_menu_toggel_by_label(TOGGLE_LABEL_FLOWVIEW, flowview);
212         timeline_select_and_save(timeline_selected, timeline_selected);
213 }
214 #endif
215
216 extern const guint8 img_zoomin[];
217 extern const guint8 img_zoomout[];
218 extern const guint8 img_pal[];
219 extern const guint8 img_bc[];
220 extern const guint8 img_mark[];
221 extern const guint8 img_highlight[];
222 extern const guint8 img_preview[];
223 extern const guint8 img_eye[];
224 extern const guint8 img_col[];
225 extern const guint8 img_flow[];
226
227 struct tool_buttons {
228         enum toggle_label label;
229         GtkWidget *widget;
230         const guint8 *data;
231         int toggle;
232         void (*handler)(GtkButton *togglebutton, gpointer priv);
233         GtkToggleButton **button;
234         const char *tooltip;
235 } tool_buttons[] = {
236         { TOGGLE_LABEL_NONE, NULL, img_zoomin, 0, zoomin_button_clicked, NULL, "Zoom in" },
237         { TOGGLE_LABEL_NONE, NULL, img_zoomout, 0, zoomout_button_clicked, NULL, "Zoom out" },
238         { TOGGLE_LABEL_NONE, NULL, img_pal, 0, palette_button_clicked, NULL, "Palette dialog" },
239         { TOGGLE_LABEL_NONE, NULL, img_bc, 0, bc_button_clicked, NULL, "Brightness+Contrast dialog" },
240         { TOGGLE_LABEL_MARK, NULL, img_mark, 1, mark_button_toggled, &show_marked_button, "Show marked pixles on view" },
241         { TOGGLE_LABEL_HIGHLIGHT, NULL, img_highlight, 1, highlight_button_toggled, &show_highlighted_button, "Highlight selected mark color" },
242         { TOGGLE_LABEL_PREVIEW, NULL, img_preview, 1, preview_button_toggled, &show_preview_button, "Show preview of selected mark color" },
243         { TOGGLE_LABEL_RENDERED, NULL, img_eye, 1, view_colorized_button_toggled, &show_colorized_button, "Show result of a rendered sequence" },
244 #ifdef WITH_OPENCV
245         { TOGGLE_LABEL_FLOWVIEW, NULL, img_flow, 1, view_flow_button_toggled, &show_flow_button, "Show optical flow" },
246 #endif
247         { TOGGLE_LABEL_NONE, NULL, img_col, 0, colorize_button_clicked, NULL, "Colorize current image" },
248         { TOGGLE_LABEL_NONE, NULL, NULL, 0, NULL, NULL, NULL },
249 };
250
251 void create_toolbar(GtkWidget *tool_bar)
252 {
253         int i;
254         GdkPixbuf *pixbuf;
255         GtkWidget *image;
256         GtkToggleButton *button;
257         GtkTooltips *tooltips;
258         GtkWidget *separator;
259
260         for (i = 0; paint_buttons[i].data; i++) {
261                 pixbuf = gdk_pixbuf_new_from_inline(-1, paint_buttons[i].data, FALSE, NULL);
262                 image = gtk_image_new_from_pixbuf(pixbuf);
263                 gtk_widget_show(GTK_WIDGET(image));
264                 paint_buttons[i].button = button = (GtkToggleButton *) gtk_toggle_button_new();
265                 if (paint_buttons[i].toggle_state) {
266                         gtk_toggle_button_set_active(button, TRUE);
267                         brush_size = paint_buttons[i].size;
268                 }
269                 g_signal_connect(button, "toggled", G_CALLBACK(paint_button_toggled), (void *)((long)i));
270                 tooltips = gtk_tooltips_new();
271                 gtk_tooltips_set_tip(tooltips, GTK_WIDGET(button), paint_buttons[i].tooltip, NULL);
272                 gtk_container_add (GTK_CONTAINER (button), image);
273                 gtk_widget_show(GTK_WIDGET(button));
274                 gtk_box_pack_start(GTK_BOX(tool_bar), GTK_WIDGET(button), FALSE, FALSE, 2);
275         }
276
277         for (i = 0; tool_buttons[i].data; i++) {
278                 if (i == 0 || i == 2) {
279                         /* add vertical seperation to hbox */
280                         separator = gtk_vseparator_new();
281                         gtk_widget_show(separator);
282                         gtk_box_pack_start(GTK_BOX(tool_bar), separator, FALSE, FALSE, 3);
283                 }
284                 pixbuf = gdk_pixbuf_new_from_inline(-1, tool_buttons[i].data, FALSE, NULL);
285                 image = gtk_image_new_from_pixbuf(pixbuf);
286                 gtk_widget_show(GTK_WIDGET(image));
287                 if (tool_buttons[i].toggle) {
288                         button = (GtkToggleButton *) gtk_toggle_button_new();
289                         g_signal_connect(button, "toggled", G_CALLBACK(tool_buttons[i].handler), NULL);
290                 } else {
291                         button = (GtkToggleButton *) gtk_button_new();
292                         g_signal_connect(button, "clicked", G_CALLBACK(tool_buttons[i].handler), NULL);
293                 }
294                 tool_buttons[i].widget = (GtkWidget *)button;
295                 tooltips = gtk_tooltips_new();
296                 gtk_tooltips_set_tip(tooltips, GTK_WIDGET(button), tool_buttons[i].tooltip, NULL);
297                 if (tool_buttons[i].button)
298                         *(tool_buttons[i].button) = button;
299                 gtk_container_add (GTK_CONTAINER (button), image);
300                 gtk_widget_show(GTK_WIDGET(button));
301                 gtk_box_pack_start(GTK_BOX(tool_bar), GTK_WIDGET(button), FALSE, FALSE, 2);
302         }
303 }
304
305 void set_button_toggel_by_label(enum toggle_label label, gboolean active)
306 {
307         int i;
308
309         for (i = 0; tool_buttons[i].data; i++) {
310                 if (tool_buttons[i].label == label)
311                         gtk_toggle_button_set_active((GtkToggleButton *)tool_buttons[i].widget, active);
312         }
313 }
314