4 #include <glib/gi18n.h>
6 #include "ui-toolbar.h"
8 #include "comic-new-dialog.h"
9 #include "comic-saveas-dialog.h"
10 #include "comic-open-dialog.h"
11 #include "tbo-window.h"
13 #include "custom-stock.h"
14 #include "ui-drawing.h"
16 #include "frame-tool.h"
17 #include "selector-tool.h"
18 #include "doodle-tool.h"
19 #include "text-tool.h"
22 static int SELECTED_TOOL = NONE;
23 static GtkActionGroup *ACTION_GROUP = NULL;
25 static ToolStruct TOOLS[] =
29 frame_tool_on_unselect,
32 frame_tool_on_release,
37 selector_tool_on_select,
38 selector_tool_on_unselect,
39 selector_tool_on_move,
40 selector_tool_on_click,
41 selector_tool_on_release,
43 selector_tool_drawing},
46 doodle_tool_on_select,
47 doodle_tool_on_unselect,
50 doodle_tool_on_release,
56 text_tool_on_unselect,
71 void unselect (enum Tool tool, TboWindow *tbo);
72 gboolean select_tool (GtkAction *action, TboWindow *tbo);
73 void update_toolbar (TboWindow *tbo);
83 set_current_tab_page (TboWindow *tbo, gboolean setit)
87 nth = tbo_comic_page_index (tbo->comic);
89 gtk_notebook_set_current_page (GTK_NOTEBOOK (tbo->notebook), nth);
90 tbo->dw_scroll = gtk_notebook_get_nth_page (GTK_NOTEBOOK (tbo->notebook), nth);
91 tbo->drawing = gtk_bin_get_child (GTK_BIN (tbo->dw_scroll));
92 set_frame_view (NULL);
93 set_selected_tool (NONE, tbo);
97 notebook_switch_page_cb (GtkNotebook *notebook,
98 GtkNotebookPage *page,
102 tbo_comic_set_current_page_nth (tbo->comic, page_num);
103 set_current_tab_page (tbo, FALSE);
104 update_toolbar (tbo);
105 tbo_window_update_status (tbo, 0, 0);
110 set_selected_tool (enum Tool tool, TboWindow *tbo)
112 unselect (SELECTED_TOOL, tbo);
113 SELECTED_TOOL = tool;
115 tool_signal (tool, TOOL_SELECT, tbo);
116 update_toolbar (tbo);
120 update_toolbar (TboWindow *tbo)
128 GtkAction *new_frame;
134 // Page next, prev and delete button sensitive
135 prev = gtk_action_group_get_action (ACTION_GROUP, "PrevPage");
136 next = gtk_action_group_get_action (ACTION_GROUP, "NextPage");
137 delete = gtk_action_group_get_action (ACTION_GROUP, "DelPage");
139 if (tbo_comic_page_first (tbo->comic))
140 gtk_action_set_sensitive (prev, FALSE);
142 gtk_action_set_sensitive (prev, TRUE);
144 if (tbo_comic_page_last (tbo->comic))
145 gtk_action_set_sensitive (next, FALSE);
147 gtk_action_set_sensitive (next, TRUE);
148 if (tbo_comic_len (tbo->comic) > 1)
149 gtk_action_set_sensitive (delete, TRUE);
151 gtk_action_set_sensitive (delete, FALSE);
153 // Frame view disabled in page view
154 doodle = gtk_action_group_get_action (ACTION_GROUP, "Doodle");
155 text = gtk_action_group_get_action (ACTION_GROUP, "Text");
156 new_frame = gtk_action_group_get_action (ACTION_GROUP, "NewFrame");
157 pix = gtk_action_group_get_action (ACTION_GROUP, "Pix");
159 if (get_frame_view() == NULL)
161 gtk_action_set_sensitive (doodle, FALSE);
162 gtk_action_set_sensitive (text, FALSE);
163 gtk_action_set_sensitive (pix, FALSE);
164 gtk_action_set_sensitive (new_frame, TRUE);
168 gtk_action_set_sensitive (doodle, TRUE);
169 gtk_action_set_sensitive (text, TRUE);
170 gtk_action_set_sensitive (pix, TRUE);
171 gtk_action_set_sensitive (new_frame, FALSE);
176 toolbar_handler (GtkWidget *widget, gpointer data)
182 add_new_page (GtkAction *action, TboWindow *tbo)
184 Page *page = tbo_comic_new_page (tbo->comic);
185 int nth = tbo_comic_page_nth (tbo->comic, page);
186 gtk_notebook_insert_page (GTK_NOTEBOOK (tbo->notebook),
190 tbo_window_update_status (tbo, 0, 0);
191 update_toolbar (tbo);
196 del_current_page (GtkAction *action, TboWindow *tbo)
198 int nth = tbo_comic_page_index (tbo->comic);
199 tbo_comic_del_current_page (tbo->comic);
200 set_current_tab_page (tbo, TRUE);
201 gtk_notebook_remove_page (GTK_NOTEBOOK (tbo->notebook), nth);
202 tbo_window_update_status (tbo, 0, 0);
203 update_toolbar (tbo);
208 next_page (GtkAction *action, TboWindow *tbo)
210 tbo_comic_next_page (tbo->comic);
211 set_current_tab_page (tbo, TRUE);
212 update_toolbar (tbo);
213 tbo_window_update_status (tbo, 0, 0);
219 prev_page (GtkAction *action, TboWindow *tbo)
221 tbo_comic_prev_page (tbo->comic);
222 set_current_tab_page (tbo, TRUE);
223 update_toolbar (tbo);
224 tbo_window_update_status (tbo, 0, 0);
230 zoom_100 (GtkAction *action, TboWindow *tbo)
232 tbo_drawing_zoom_100 (tbo);
237 zoom_in (GtkAction *action, TboWindow *tbo)
239 tbo_drawing_zoom_in (tbo);
244 zoom_out (GtkAction *action, TboWindow *tbo)
246 tbo_drawing_zoom_out (tbo);
251 add_pix (GtkAction *action, TboWindow *tbo)
254 GtkFileFilter *filter;
256 dialog = gtk_file_chooser_dialog_new (_("Add an Image"),
257 GTK_WINDOW (tbo->window),
258 GTK_FILE_CHOOSER_ACTION_OPEN,
259 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
260 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
263 filter = gtk_file_filter_new ();
264 gtk_file_filter_set_name (filter, _("png"));
265 gtk_file_filter_add_pattern (filter, "*.png");
266 gtk_file_filter_add_pattern (filter, "*.PNG");
267 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
268 filter = gtk_file_filter_new ();
269 gtk_file_filter_set_name (filter, _("All files"));
270 gtk_file_filter_add_pattern (filter, "*");
271 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
273 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
276 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
277 PIXImage *piximage = tbo_piximage_new_with_params (0, 0, 0, 0, filename);
278 tbo_frame_add_obj (get_frame_view(), piximage);
279 update_drawing (tbo);
283 gtk_widget_destroy (dialog);
287 static const GtkActionEntry tbo_tools_entries [] = {
288 { "NewFileTool", GTK_STOCK_NEW, N_("_New"), "<control>N",
290 G_CALLBACK (tbo_comic_new_dialog) },
292 { "OpenFileTool", GTK_STOCK_OPEN, N_("_Open"), "<control>O",
294 G_CALLBACK (tbo_comic_open_dialog) },
296 { "SaveFileTool", GTK_STOCK_SAVE, N_("_Save"), "<control>S",
297 N_("Save current document"),
298 G_CALLBACK (tbo_comic_save_dialog) },
301 { "NewPage", GTK_STOCK_ADD, N_("New Page"), "<control>P",
303 G_CALLBACK (add_new_page) },
305 { "DelPage", GTK_STOCK_DELETE, N_("Delete Page"), "",
306 N_("Delete current page"),
307 G_CALLBACK (del_current_page) },
309 { "PrevPage", GTK_STOCK_GO_BACK, N_("Prev Page"), "",
311 G_CALLBACK (prev_page) },
313 { "NextPage", GTK_STOCK_GO_FORWARD, N_("Next Page"), "",
315 G_CALLBACK (next_page) },
318 { "Zoomin", GTK_STOCK_ZOOM_IN, N_("Zoom in"), "",
320 G_CALLBACK (zoom_in) },
321 { "Zoom100", GTK_STOCK_ZOOM_100, N_("Zoom 1:1"), "",
323 G_CALLBACK (zoom_100) },
324 { "Zoomout", GTK_STOCK_ZOOM_OUT, N_("Zoom out"), "",
326 G_CALLBACK (zoom_out) },
329 { "Pix", TBO_STOCK_PIX, N_("Image"), "",
331 G_CALLBACK (add_pix) },
334 static const GtkToggleActionEntry tbo_tools_toogle_entries [] = {
336 { "NewFrame", TBO_STOCK_FRAME, N_("New _Frame"), "<control>F",
338 G_CALLBACK (select_tool), FALSE },
340 { "Selector", TBO_STOCK_SELECTOR, N_("Selector"), "",
342 G_CALLBACK (select_tool), FALSE },
345 { "Doodle", TBO_STOCK_DOODLE, N_("Doodle"), "",
347 G_CALLBACK (select_tool), FALSE },
348 { "Text", TBO_STOCK_TEXT, N_("Text"), "",
350 G_CALLBACK (select_tool), FALSE },
353 static const tool_and_action tools_actions [] = {
355 {SELECTOR, "Selector"},
361 set_selected_tool_and_action (enum Tool tool, TboWindow *tbo)
363 GtkToggleAction *action;
364 enum Tool action_tool;
368 GtkToggleActionEntry entry;
370 for (i=0; i<G_N_ELEMENTS (tools_actions); i++)
372 if (tool == tools_actions[i].tool)
374 name = (gchar *) tools_actions[i].action;
379 action = (GtkToggleAction *) gtk_action_group_get_action (ACTION_GROUP, name);
380 if (gtk_action_is_sensitive (GTK_ACTION (action)))
381 gtk_toggle_action_set_active (action, TRUE);
385 unselect (enum Tool tool, TboWindow *tbo)
388 GtkToggleAction *action;
390 for (i=0; i<G_N_ELEMENTS (tools_actions); i++)
392 if (tools_actions[i].tool == tool)
394 action = (GtkToggleAction *) gtk_action_group_get_action (ACTION_GROUP,
395 tools_actions[i].action);
397 gtk_toggle_action_set_active (action, FALSE);
401 tool_signal (tool, TOOL_UNSELECT, tbo);
405 select_tool (GtkAction *action, TboWindow *tbo)
407 GtkToggleAction *toggle_action;
412 toggle_action = (GtkToggleAction *) action;
413 name = gtk_action_get_name (action);
416 for (i=0; i<G_N_ELEMENTS (tools_actions); i++)
418 if (strcmp (tools_actions[i].action, name) == 0)
420 tool = tools_actions[i].tool;
425 if (gtk_toggle_action_get_active (toggle_action))
426 set_selected_tool (tool, tbo);
428 set_selected_tool (NONE, tbo);
429 tbo_window_update_status (tbo, 0, 0);
433 GtkWidget *generate_toolbar (TboWindow *window){
435 GtkUIManager *manager;
436 GError *error = NULL;
438 manager = gtk_ui_manager_new ();
439 gtk_ui_manager_add_ui_from_file (manager, DATA_DIR "/ui/tbo-toolbar-ui.xml", &error);
442 g_warning ("Could not merge tbo-toolbar-ui.xml: %s", error->message);
443 g_error_free (error);
446 ACTION_GROUP = gtk_action_group_new ("ToolsActions");
447 gtk_action_group_set_translation_domain (ACTION_GROUP, NULL);
448 gtk_action_group_add_actions (ACTION_GROUP, tbo_tools_entries,
449 G_N_ELEMENTS (tbo_tools_entries), window);
450 gtk_action_group_add_toggle_actions (ACTION_GROUP, tbo_tools_toogle_entries,
451 G_N_ELEMENTS (tbo_tools_toogle_entries), window);
453 gtk_ui_manager_insert_action_group (manager, ACTION_GROUP, 0);
455 toolbar = gtk_ui_manager_get_widget (manager, "/toolbar");
457 update_toolbar (window);
463 tool_signal (enum Tool tool, enum ToolSignal signal, gpointer data)
466 ToolStruct *toolstruct = NULL;
469 for (i=0; i<G_N_ELEMENTS (TOOLS); i++)
471 if (tool == TOOLS[i].tool)
473 toolstruct = &TOOLS[i];
483 toolstruct->tool_on_select(data);
486 toolstruct->tool_on_unselect(data);
490 toolstruct->tool_on_move (pdata[0], pdata[1], pdata[2]);
494 toolstruct->tool_on_click (pdata[0], pdata[1], pdata[2]);
498 toolstruct->tool_on_release (pdata[0], pdata[1], pdata[2]);
502 toolstruct->tool_on_key (pdata[0], pdata[1], pdata[2]);
505 toolstruct->tool_drawing (data);