Return GdkColor via out parameter rather than return value
[geany-mirror.git] / src / tools.c
blob706394812a28841b0a7f147cac83e18c9a08344b
1 /*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Miscellaneous code for the built-in Tools menu items, and custom command code.
24 * For Plugins code see plugins.c.
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "tools.h"
33 #include "document.h"
34 #include "keybindings.h"
35 #include "sciwrappers.h"
36 #include "spawn.h"
37 #include "support.h"
38 #include "ui_utils.h"
39 #include "utils.h"
40 #include "win32.h"
42 #include "gtkcompat.h"
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <errno.h>
50 enum
52 CC_COLUMN_ID,
53 CC_COLUMN_STATUS,
54 CC_COLUMN_TOOLTIP,
55 CC_COLUMN_CMD,
56 CC_COLUMN_LABEL,
57 CC_COLUMN_COUNT
60 /* custom commands code*/
61 struct cc_dialog
63 guint count;
64 GtkWidget *view;
65 GtkTreeViewColumn *edit_column;
66 GtkListStore *store;
67 GtkTreeSelection *selection;
68 GtkWidget *button_add;
69 GtkWidget *button_remove;
70 GtkWidget *button_up;
71 GtkWidget *button_down;
75 /* update STATUS and TOOLTIP columns according to cmd */
76 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
78 GError *err = NULL;
79 const gchar *stock_id = GTK_STOCK_NO;
80 gchar *tooltip = NULL;
82 if (EMPTY(cmd) || spawn_check_command(cmd, TRUE, &err))
83 stock_id = GTK_STOCK_YES;
84 else
86 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
87 g_error_free(err);
90 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
91 g_free(tooltip);
95 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
96 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
98 GtkTreeIter iter;
99 const gchar *cmd = NULL;
100 const gchar *label = NULL;
101 guint id = cc->count;
103 if (idx >= 0)
105 cmd = ui_prefs.custom_commands[idx];
106 label = ui_prefs.custom_commands_labels[idx];
109 cc->count++;
110 gtk_list_store_append(cc->store, &iter);
111 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, CC_COLUMN_LABEL, label, -1);
112 cc_dialog_update_row_status(cc->store, &iter, cmd);
114 if (start_editing)
116 GtkTreePath *path;
118 gtk_widget_grab_focus(cc->view);
119 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
120 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
121 gtk_tree_path_free(path);
126 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
128 cc_dialog_add_command(cc, -1, TRUE);
132 static void scroll_to_cursor(GtkTreeView *view)
134 GtkTreePath *path;
135 GtkTreeViewColumn *column;
137 gtk_tree_view_get_cursor(view, &path, &column);
138 if (path)
140 gtk_tree_view_scroll_to_cell(view, path, column, FALSE, 1.0, 1.0);
141 gtk_tree_path_free(path);
145 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
147 GtkTreeIter iter;
149 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
151 gtk_list_store_remove(cc->store, &iter);
152 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
157 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
159 GtkTreeIter iter;
161 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
163 GtkTreePath *path;
164 GtkTreeIter prev;
166 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
167 if (gtk_tree_path_prev(path) &&
168 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
170 gtk_list_store_move_before(cc->store, &iter, &prev);
171 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
173 gtk_tree_path_free(path);
178 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
180 GtkTreeIter iter;
182 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
184 GtkTreeIter next = iter;
186 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
188 gtk_list_store_move_after(cc->store, &iter, &next);
189 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
195 /* Executes command (which should include all necessary command line args) and passes the current
196 * selection through the standard input of command. The whole output of command replaces the
197 * current selection. */
198 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
200 GError *error = NULL;
201 gchar *sel;
202 SpawnWriteData input;
203 GString *output;
204 GString *errors;
205 gint status;
207 g_return_if_fail(doc != NULL && command != NULL);
209 if (! sci_has_selection(doc->editor->sci))
210 editor_select_lines(doc->editor, FALSE);
212 sel = sci_get_selection_contents(doc->editor->sci);
213 input.ptr = sel;
214 input.size = strlen(sel);
215 output = g_string_sized_new(256);
216 errors = g_string_new(NULL);
217 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
219 if (spawn_sync(NULL, command, NULL, NULL, &input, output, errors, &status, &error))
221 if (errors->len > 0)
223 g_warning("%s: %s\n", command, errors->str);
224 ui_set_statusbar(TRUE,
225 _("The executed custom command returned an error. "
226 "Your selection was not changed. Error message: %s"),
227 errors->str);
229 else if (!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS)
231 /* TODO maybe include the exit code in the error message */
232 ui_set_statusbar(TRUE,
233 _("The executed custom command exited with an unsuccessful exit code."));
235 else
236 { /* Command completed successfully */
237 sci_replace_sel(doc->editor->sci, output->str);
240 else
242 ui_set_statusbar(TRUE, _("Cannot execute custom command \"%s\": %s. "
243 "Check the path setting in Custom Commands."), command, error->message);
244 g_error_free(error);
247 g_string_free(output, TRUE);
248 g_string_free(errors, TRUE);
249 g_free(sel);
253 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
254 struct cc_dialog *cc)
256 GtkTreeIter iter;
258 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
259 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
260 cc_dialog_update_row_status(cc->store, &iter, text);
264 static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
265 struct cc_dialog *cc)
267 GtkTreeIter iter;
269 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
270 gtk_list_store_set(cc->store, &iter, CC_COLUMN_LABEL, text, -1);
274 /* re-compute IDs to reflect the current store state */
275 static void cc_dialog_update_ids(struct cc_dialog *cc)
277 GtkTreeIter iter;
279 cc->count = 1;
280 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
281 return;
285 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
286 cc->count++;
288 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
292 /* update sensitiveness of the buttons according to the selection */
293 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
295 GtkTreeIter iter;
296 gboolean has_selection = FALSE;
297 gboolean first_selected = FALSE;
298 gboolean last_selected = FALSE;
300 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
302 GtkTreePath *path;
303 GtkTreePath *copy;
305 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
306 copy = gtk_tree_path_copy(path);
307 first_selected = ! gtk_tree_path_prev(copy);
308 gtk_tree_path_free(copy);
309 gtk_tree_path_next(path);
310 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
311 gtk_tree_path_free(path);
314 gtk_widget_set_sensitive(cc->button_remove, has_selection);
315 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
316 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
320 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
322 cc_dialog_update_sensitive(cc);
326 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
327 struct cc_dialog *cc)
329 cc_dialog_update_ids(cc);
330 cc_dialog_update_sensitive(cc);
334 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
336 cc_dialog_update_ids(cc);
337 cc_dialog_update_sensitive(cc);
341 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
342 gpointer new_order, struct cc_dialog *cc)
344 cc_dialog_update_ids(cc);
345 cc_dialog_update_sensitive(cc);
349 static void cc_show_dialog_custom_commands(void)
351 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
352 GtkCellRenderer *renderer;
353 GtkTreeViewColumn *column;
354 guint i;
355 struct cc_dialog cc;
357 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
358 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
359 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
360 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
361 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
362 gtk_box_set_spacing(GTK_BOX(vbox), 6);
363 gtk_widget_set_name(dialog, "GeanyDialog");
365 label = gtk_label_new(_("You can send the current selection to any of these commands and the output of the command replaces the current selection."));
366 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
367 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
368 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
370 cc.count = 1;
371 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
372 G_TYPE_STRING, G_TYPE_STRING);
373 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
374 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
375 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
376 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
377 /* ID column */
378 renderer = gtk_cell_renderer_text_new();
379 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
380 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
381 /* command column, holding status and command display */
382 column = g_object_new(GTK_TYPE_TREE_VIEW_COLUMN, "title", _("Command"), "expand", TRUE, "resizable", TRUE, NULL);
383 renderer = gtk_cell_renderer_pixbuf_new();
384 gtk_tree_view_column_pack_start(column, renderer, FALSE);
385 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
386 renderer = gtk_cell_renderer_text_new();
387 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
388 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
389 gtk_tree_view_column_pack_start(column, renderer, TRUE);
390 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
391 cc.edit_column = column;
392 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
393 /* label column */
394 renderer = gtk_cell_renderer_text_new();
395 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
396 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_label_edited), &cc);
397 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", CC_COLUMN_LABEL, NULL);
398 g_object_set(column, "expand", TRUE, "resizable", TRUE, NULL);
399 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
401 scroll = gtk_scrolled_window_new(NULL, NULL);
402 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
403 GTK_POLICY_AUTOMATIC);
404 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
405 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
406 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
408 if (ui_prefs.custom_commands != NULL)
410 GtkTreeIter iter;
411 guint len = g_strv_length(ui_prefs.custom_commands);
413 for (i = 0; i < len; i++)
415 if (EMPTY(ui_prefs.custom_commands[i]))
416 continue; /* skip empty fields */
418 cc_dialog_add_command(&cc, i, FALSE);
421 /* focus the first row if any */
422 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
424 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
426 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
427 gtk_tree_path_free(path);
431 buttonbox = gtk_hbutton_box_new();
432 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
433 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
434 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
435 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
436 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
437 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
438 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
439 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
440 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
441 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
442 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
443 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
444 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
445 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
447 cc_dialog_update_sensitive(&cc);
449 /* only connect the selection signal when all other cc_dialog fields are set */
450 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
451 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
452 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
453 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
455 gtk_widget_show_all(vbox);
457 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
459 GSList *cmd_list = NULL;
460 GSList *lbl_list = NULL;
461 gint len = 0;
462 gchar **commands = NULL;
463 gchar **labels = NULL;
464 GtkTreeIter iter;
466 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
470 gchar *cmd;
471 gchar *lbl;
473 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
474 if (!EMPTY(cmd))
476 cmd_list = g_slist_prepend(cmd_list, cmd);
477 lbl_list = g_slist_prepend(lbl_list, lbl);
478 len++;
480 else
482 g_free(cmd);
483 g_free(lbl);
486 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
488 cmd_list = g_slist_reverse(cmd_list);
489 lbl_list = g_slist_reverse(lbl_list);
490 /* create a new null-terminated array but only if there is any commands defined */
491 if (len > 0)
493 gint j = 0;
494 GSList *cmd_node, *lbl_node;
496 commands = g_new(gchar*, len + 1);
497 labels = g_new(gchar*, len + 1);
498 /* walk commands and labels lists */
499 for (cmd_node = cmd_list, lbl_node = lbl_list; cmd_node != NULL; cmd_node = cmd_node->next, lbl_node = lbl_node->next)
501 commands[j] = (gchar*) cmd_node->data;
502 labels[j] = (gchar*) lbl_node->data;
503 j++;
505 /* null-terminate the arrays */
506 commands[j] = NULL;
507 labels[j] = NULL;
509 /* set the new arrays */
510 g_strfreev(ui_prefs.custom_commands);
511 ui_prefs.custom_commands = commands;
512 g_strfreev(ui_prefs.custom_commands_labels);
513 ui_prefs.custom_commands_labels = labels;
514 /* rebuild the menu items */
515 tools_create_insert_custom_command_menu_items();
517 g_slist_free(cmd_list);
518 g_slist_free(lbl_list);
520 gtk_widget_destroy(dialog);
524 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
526 GeanyDocument *doc = document_get_current();
527 gint command_idx;
529 g_return_if_fail(DOC_VALID(doc));
531 command_idx = GPOINTER_TO_INT(user_data);
533 if (ui_prefs.custom_commands == NULL ||
534 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
536 cc_show_dialog_custom_commands();
537 return;
540 /* send it through the command and when the command returned the output the current selection
541 * will be replaced */
542 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
546 static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
548 GtkWidget *item;
549 gint key_idx = -1;
551 switch (idx)
553 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
554 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
555 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
556 case 3: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD4; break;
557 case 4: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD5; break;
558 case 5: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD6; break;
559 case 6: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD7; break;
560 case 7: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD8; break;
561 case 8: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD9; break;
564 item = gtk_menu_item_new_with_label(label);
565 gtk_widget_set_tooltip_text(item, tooltip);
566 if (key_idx != -1)
568 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
570 if (kb->key > 0)
572 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
573 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
576 gtk_container_add(GTK_CONTAINER(me), item);
577 gtk_widget_show(item);
578 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
579 GINT_TO_POINTER(idx));
583 void tools_create_insert_custom_command_menu_items(void)
585 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
586 GtkWidget *item;
587 GList *me_children, *node;
589 /* first clean the menus to be able to rebuild them */
590 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
591 foreach_list(node, me_children)
592 gtk_widget_destroy(GTK_WIDGET(node->data));
593 g_list_free(me_children);
595 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
597 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
598 gtk_container_add(GTK_CONTAINER(menu_edit), item);
599 gtk_widget_set_sensitive(item, FALSE);
600 gtk_widget_show(item);
602 else
604 guint i, len;
605 gint idx = 0;
606 len = g_strv_length(ui_prefs.custom_commands);
607 for (i = 0; i < len; i++)
609 const gchar *label = ui_prefs.custom_commands_labels[i];
611 if (EMPTY(label))
612 label = ui_prefs.custom_commands[i];
613 if (!EMPTY(label)) /* skip empty items */
615 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
616 idx++;
621 /* separator and Set menu item */
622 item = gtk_separator_menu_item_new();
623 gtk_container_add(GTK_CONTAINER(menu_edit), item);
624 gtk_widget_show(item);
626 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
630 /* (stolen from bluefish, thanks)
631 * Returns number of characters, lines and words in the supplied gchar*.
632 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
633 * Words are defined as any characters grouped, separated with spaces. */
634 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
636 guint in_word = 0;
637 gunichar utext;
639 if (! text)
640 return; /* politely refuse to operate on NULL */
642 *chars = *words = *lines = 0;
643 while (*text != '\0')
645 (*chars)++;
647 switch (*text)
649 case '\n':
650 (*lines)++;
651 case '\r':
652 case '\f':
653 case '\t':
654 case ' ':
655 case '\v':
656 mb_word_separator:
657 if (in_word)
659 in_word = 0;
660 (*words)++;
662 break;
663 default:
664 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
665 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
666 goto mb_word_separator;
667 if (g_unichar_isgraph(utext)) /* Is this something printable? */
668 in_word = 1;
669 break;
671 /* Even if the current char is 2 bytes, this will iterate correctly. */
672 text = g_utf8_next_char(text);
675 /* Capture last word, if there's no whitespace at the end of the file. */
676 if (in_word)
677 (*words)++;
678 /* We start counting line numbers from 1 */
679 if (*chars > 0)
680 (*lines)++;
684 void tools_word_count(void)
686 GtkWidget *dialog, *label, *vbox, *table;
687 GeanyDocument *doc;
688 guint chars = 0, lines = 0, words = 0;
689 gchar *text;
690 const gchar *range;
692 doc = document_get_current();
693 g_return_if_fail(doc != NULL);
695 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
696 GTK_DIALOG_DESTROY_WITH_PARENT,
697 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
698 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
699 gtk_widget_set_name(dialog, "GeanyDialog");
701 if (sci_has_selection(doc->editor->sci))
703 text = sci_get_selection_contents(doc->editor->sci);
704 range = _("selection");
706 else
708 text = sci_get_contents(doc->editor->sci, -1);
709 range = _("whole document");
711 word_count(text, &chars, &lines, &words);
712 g_free(text);
714 table = gtk_table_new(4, 2, FALSE);
715 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
716 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
718 label = gtk_label_new(_("Range:"));
719 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
720 (GtkAttachOptions) (GTK_FILL),
721 (GtkAttachOptions) (0), 0, 0);
722 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
724 label = gtk_label_new(range);
725 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
726 (GtkAttachOptions) (GTK_FILL),
727 (GtkAttachOptions) (0), 20, 0);
728 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
730 label = gtk_label_new(_("Lines:"));
731 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
732 (GtkAttachOptions) (GTK_FILL),
733 (GtkAttachOptions) (0), 0, 0);
734 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
736 text = g_strdup_printf("%d", lines);
737 label = gtk_label_new(text);
738 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
739 (GtkAttachOptions) (GTK_FILL),
740 (GtkAttachOptions) (0), 20, 0);
741 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
742 g_free(text);
744 label = gtk_label_new(_("Words:"));
745 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
746 (GtkAttachOptions) (GTK_FILL),
747 (GtkAttachOptions) (0), 0, 0);
748 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
750 text = g_strdup_printf("%d", words);
751 label = gtk_label_new(text);
752 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
753 (GtkAttachOptions) (GTK_FILL),
754 (GtkAttachOptions) (0), 20, 0);
755 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
756 g_free(text);
758 label = gtk_label_new(_("Characters:"));
759 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
760 (GtkAttachOptions) (GTK_FILL),
761 (GtkAttachOptions) (0), 0, 0);
762 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
764 text = g_strdup_printf("%d", chars);
765 label = gtk_label_new(text);
766 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
767 (GtkAttachOptions) (GTK_FILL),
768 (GtkAttachOptions) (0), 20, 0);
769 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
770 g_free(text);
772 gtk_container_add(GTK_CONTAINER(vbox), table);
774 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
775 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
777 gtk_widget_show_all(dialog);
782 * color dialog callbacks
784 static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
786 switch (response)
788 case GTK_RESPONSE_OK:
789 gtk_widget_hide(ui_widgets.open_colorsel);
790 /* fall through */
791 case GTK_RESPONSE_APPLY:
793 GdkColor color;
794 GeanyDocument *doc = document_get_current();
795 gchar *hex;
796 GtkWidget *colorsel;
798 g_return_if_fail(doc != NULL);
800 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
801 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color);
803 hex = utils_get_hex_from_color(&color);
804 editor_insert_color(doc->editor, hex);
805 g_free(hex);
806 break;
809 default:
810 gtk_widget_hide(ui_widgets.open_colorsel);
815 /* This shows the color selection dialog to choose a color. */
816 void tools_color_chooser(const gchar *color)
818 GdkColor gc;
819 GtkWidget *colorsel;
821 #ifdef G_OS_WIN32
822 if (interface_prefs.use_native_windows_dialogs)
824 win32_show_color_dialog(color);
825 return;
827 #endif
829 if (ui_widgets.open_colorsel == NULL)
831 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
832 gtk_dialog_add_button(GTK_DIALOG(ui_widgets.open_colorsel), GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
833 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets.open_colorsel),
834 GTK_RESPONSE_APPLY, GTK_RESPONSE_CANCEL, GTK_RESPONSE_OK, -1);
835 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
836 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
837 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
838 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE);
840 g_signal_connect(ui_widgets.open_colorsel, "response",
841 G_CALLBACK(on_color_dialog_response), NULL);
842 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
843 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
845 else
846 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
847 /* if color is non-NULL set it in the dialog as preselected color */
848 if (color != NULL && utils_parse_color(color, &gc))
850 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
851 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
854 /* We make sure the dialog is visible. */
855 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));