Merge pull request #3812 from Biswa96/meson-add-geany-icon
[geany-mirror.git] / src / tools.c
blobb380e94c3b26616e16f079a6ab645dcb76731abc
1 /*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Miscellaneous code for the built-in Tools menu items, and custom command code.
23 * For Plugins code see plugins.c.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "tools.h"
32 #include "document.h"
33 #include "keybindings.h"
34 #include "sciwrappers.h"
35 #include "spawn.h"
36 #include "support.h"
37 #include "ui_utils.h"
38 #include "utils.h"
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <gtk/gtk.h>
47 enum
49 CC_COLUMN_ID,
50 CC_COLUMN_STATUS,
51 CC_COLUMN_TOOLTIP,
52 CC_COLUMN_CMD,
53 CC_COLUMN_LABEL,
54 CC_COLUMN_COUNT
57 /* custom commands code*/
58 struct cc_dialog
60 guint count;
61 GtkWidget *view;
62 GtkTreeViewColumn *edit_column;
63 GtkListStore *store;
64 GtkTreeSelection *selection;
65 GtkWidget *button_add;
66 GtkWidget *button_remove;
67 GtkWidget *button_up;
68 GtkWidget *button_down;
72 /* update STATUS and TOOLTIP columns according to cmd */
73 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
75 GError *err = NULL;
76 const gchar *stock_id = GTK_STOCK_NO;
77 gchar *tooltip = NULL;
79 if (EMPTY(cmd) || spawn_check_command(cmd, TRUE, &err))
80 stock_id = GTK_STOCK_YES;
81 else
83 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
84 g_error_free(err);
87 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
88 g_free(tooltip);
92 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
93 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
95 GtkTreeIter iter;
96 const gchar *cmd = NULL;
97 const gchar *label = NULL;
98 guint id = cc->count;
100 if (idx >= 0)
102 cmd = ui_prefs.custom_commands[idx];
103 label = ui_prefs.custom_commands_labels[idx];
106 cc->count++;
107 gtk_list_store_append(cc->store, &iter);
108 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, CC_COLUMN_LABEL, label, -1);
109 cc_dialog_update_row_status(cc->store, &iter, cmd);
111 if (start_editing)
113 GtkTreePath *path;
115 gtk_widget_grab_focus(cc->view);
116 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
117 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
118 gtk_tree_path_free(path);
123 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
125 cc_dialog_add_command(cc, -1, TRUE);
129 static void scroll_to_cursor(GtkTreeView *view)
131 GtkTreePath *path;
132 GtkTreeViewColumn *column;
134 gtk_tree_view_get_cursor(view, &path, &column);
135 if (path)
137 gtk_tree_view_scroll_to_cell(view, path, column, FALSE, 1.0, 1.0);
138 gtk_tree_path_free(path);
142 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
144 GtkTreeIter iter;
146 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
148 gtk_list_store_remove(cc->store, &iter);
149 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
154 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
156 GtkTreeIter iter;
158 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
160 GtkTreePath *path;
161 GtkTreeIter prev;
163 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
164 if (gtk_tree_path_prev(path) &&
165 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
167 gtk_list_store_move_before(cc->store, &iter, &prev);
168 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
170 gtk_tree_path_free(path);
175 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
177 GtkTreeIter iter;
179 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
181 GtkTreeIter next = iter;
183 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
185 gtk_list_store_move_after(cc->store, &iter, &next);
186 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
192 /* Executes command (which should include all necessary command line args) and passes the current
193 * selection through the standard input of command. The whole output of command replaces the
194 * current selection. */
195 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
197 GError *error = NULL;
198 gchar *sel;
199 SpawnWriteData input;
200 GString *output;
201 GString *errors;
202 gint status;
204 g_return_if_fail(doc != NULL && command != NULL);
206 if (! sci_has_selection(doc->editor->sci))
207 editor_select_lines(doc->editor, FALSE);
209 sel = sci_get_selection_contents(doc->editor->sci);
210 input.ptr = sel;
211 input.size = strlen(sel);
212 output = g_string_sized_new(256);
213 errors = g_string_new(NULL);
214 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
216 if (spawn_sync(NULL, command, NULL, NULL, &input, output, errors, &status, &error))
218 if (errors->len > 0)
220 g_warning("%s: %s\n", command, errors->str);
221 ui_set_statusbar(TRUE,
222 _("The executed custom command returned an error. "
223 "Your selection was not changed. Error message: %s"),
224 errors->str);
226 else if (!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS)
228 /* TODO maybe include the exit code in the error message */
229 ui_set_statusbar(TRUE,
230 _("The executed custom command exited with an unsuccessful exit code."));
232 else
233 { /* Command completed successfully */
234 sci_replace_sel(doc->editor->sci, output->str);
237 else
239 ui_set_statusbar(TRUE, _("Cannot execute custom command \"%s\": %s. "
240 "Check the path setting in Custom Commands."), command, error->message);
241 g_error_free(error);
244 g_string_free(output, TRUE);
245 g_string_free(errors, TRUE);
246 g_free(sel);
250 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
251 struct cc_dialog *cc)
253 GtkTreeIter iter;
255 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
256 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
257 cc_dialog_update_row_status(cc->store, &iter, text);
261 static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
262 struct cc_dialog *cc)
264 GtkTreeIter iter;
266 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
267 gtk_list_store_set(cc->store, &iter, CC_COLUMN_LABEL, text, -1);
271 /* re-compute IDs to reflect the current store state */
272 static void cc_dialog_update_ids(struct cc_dialog *cc)
274 GtkTreeIter iter;
276 cc->count = 1;
277 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
278 return;
282 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
283 cc->count++;
285 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
289 /* update sensitiveness of the buttons according to the selection */
290 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
292 GtkTreeIter iter;
293 gboolean has_selection = FALSE;
294 gboolean first_selected = FALSE;
295 gboolean last_selected = FALSE;
297 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
299 GtkTreePath *path;
300 GtkTreePath *copy;
302 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
303 copy = gtk_tree_path_copy(path);
304 first_selected = ! gtk_tree_path_prev(copy);
305 gtk_tree_path_free(copy);
306 gtk_tree_path_next(path);
307 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
308 gtk_tree_path_free(path);
311 gtk_widget_set_sensitive(cc->button_remove, has_selection);
312 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
313 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
317 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
319 cc_dialog_update_sensitive(cc);
323 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
324 struct cc_dialog *cc)
326 cc_dialog_update_ids(cc);
327 cc_dialog_update_sensitive(cc);
331 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
333 cc_dialog_update_ids(cc);
334 cc_dialog_update_sensitive(cc);
338 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
339 gpointer new_order, struct cc_dialog *cc)
341 cc_dialog_update_ids(cc);
342 cc_dialog_update_sensitive(cc);
346 static void cc_show_dialog_custom_commands(void)
348 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
349 GtkCellRenderer *renderer;
350 GtkTreeViewColumn *column;
351 guint i;
352 struct cc_dialog cc;
354 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
355 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
356 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
357 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
358 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
359 gtk_box_set_spacing(GTK_BOX(vbox), 6);
360 gtk_widget_set_name(dialog, "GeanyDialog");
362 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."));
363 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
364 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
365 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
367 cc.count = 1;
368 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
369 G_TYPE_STRING, G_TYPE_STRING);
370 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
371 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
372 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
373 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
374 /* ID column */
375 renderer = gtk_cell_renderer_text_new();
376 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
377 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
378 /* command column, holding status and command display */
379 column = g_object_new(GTK_TYPE_TREE_VIEW_COLUMN, "title", _("Command"), "expand", TRUE, "resizable", TRUE, NULL);
380 renderer = gtk_cell_renderer_pixbuf_new();
381 gtk_tree_view_column_pack_start(column, renderer, FALSE);
382 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
383 renderer = gtk_cell_renderer_text_new();
384 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
385 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
386 gtk_tree_view_column_pack_start(column, renderer, TRUE);
387 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
388 cc.edit_column = column;
389 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
390 /* label column */
391 renderer = gtk_cell_renderer_text_new();
392 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
393 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_label_edited), &cc);
394 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", CC_COLUMN_LABEL, NULL);
395 g_object_set(column, "expand", TRUE, "resizable", TRUE, NULL);
396 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
398 scroll = gtk_scrolled_window_new(NULL, NULL);
399 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
400 GTK_POLICY_AUTOMATIC);
401 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
402 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
403 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
405 if (ui_prefs.custom_commands != NULL)
407 GtkTreeIter iter;
408 guint len = g_strv_length(ui_prefs.custom_commands);
410 for (i = 0; i < len; i++)
412 if (EMPTY(ui_prefs.custom_commands[i]))
413 continue; /* skip empty fields */
415 cc_dialog_add_command(&cc, i, FALSE);
418 /* focus the first row if any */
419 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
421 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
423 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
424 gtk_tree_path_free(path);
428 buttonbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
429 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
430 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
431 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
432 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
433 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
434 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
435 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
436 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
437 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
438 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
439 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
440 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
441 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
442 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
444 cc_dialog_update_sensitive(&cc);
446 /* only connect the selection signal when all other cc_dialog fields are set */
447 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
448 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
449 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
450 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
452 gtk_widget_show_all(vbox);
454 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
456 GSList *cmd_list = NULL;
457 GSList *lbl_list = NULL;
458 gint len = 0;
459 gchar **commands = NULL;
460 gchar **labels = NULL;
461 GtkTreeIter iter;
463 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
467 gchar *cmd;
468 gchar *lbl;
470 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
471 if (!EMPTY(cmd))
473 cmd_list = g_slist_prepend(cmd_list, cmd);
474 lbl_list = g_slist_prepend(lbl_list, lbl);
475 len++;
477 else
479 g_free(cmd);
480 g_free(lbl);
483 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
485 cmd_list = g_slist_reverse(cmd_list);
486 lbl_list = g_slist_reverse(lbl_list);
487 /* create a new null-terminated array but only if there is any commands defined */
488 if (len > 0)
490 gint j = 0;
491 GSList *cmd_node, *lbl_node;
493 commands = g_new(gchar*, len + 1);
494 labels = g_new(gchar*, len + 1);
495 /* walk commands and labels lists */
496 for (cmd_node = cmd_list, lbl_node = lbl_list; cmd_node != NULL; cmd_node = cmd_node->next, lbl_node = lbl_node->next)
498 commands[j] = (gchar*) cmd_node->data;
499 labels[j] = (gchar*) lbl_node->data;
500 j++;
502 /* null-terminate the arrays */
503 commands[j] = NULL;
504 labels[j] = NULL;
506 /* set the new arrays */
507 g_strfreev(ui_prefs.custom_commands);
508 ui_prefs.custom_commands = commands;
509 g_strfreev(ui_prefs.custom_commands_labels);
510 ui_prefs.custom_commands_labels = labels;
511 /* rebuild the menu items */
512 tools_create_insert_custom_command_menu_items();
514 g_slist_free(cmd_list);
515 g_slist_free(lbl_list);
517 gtk_widget_destroy(dialog);
521 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
523 GeanyDocument *doc = document_get_current();
524 gint command_idx;
526 g_return_if_fail(DOC_VALID(doc));
528 command_idx = GPOINTER_TO_INT(user_data);
530 if (ui_prefs.custom_commands == NULL ||
531 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
533 cc_show_dialog_custom_commands();
534 return;
537 /* send it through the command and when the command returned the output the current selection
538 * will be replaced */
539 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
543 static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
545 GtkWidget *item;
546 gint key_idx = -1;
548 switch (idx)
550 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
551 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
552 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
553 case 3: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD4; break;
554 case 4: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD5; break;
555 case 5: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD6; break;
556 case 6: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD7; break;
557 case 7: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD8; break;
558 case 8: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD9; break;
561 item = gtk_menu_item_new_with_label(label);
562 gtk_widget_set_tooltip_text(item, tooltip);
563 if (key_idx != -1)
565 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
567 if (kb->key > 0)
569 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
570 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
573 gtk_container_add(GTK_CONTAINER(me), item);
574 gtk_widget_show(item);
575 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
576 GINT_TO_POINTER(idx));
580 void tools_create_insert_custom_command_menu_items(void)
582 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
583 GtkWidget *item;
584 GList *me_children, *node;
586 /* first clean the menus to be able to rebuild them */
587 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
588 foreach_list(node, me_children)
589 gtk_widget_destroy(GTK_WIDGET(node->data));
590 g_list_free(me_children);
592 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
594 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
595 gtk_container_add(GTK_CONTAINER(menu_edit), item);
596 gtk_widget_set_sensitive(item, FALSE);
597 gtk_widget_show(item);
599 else
601 guint i, len;
602 gint idx = 0;
603 len = g_strv_length(ui_prefs.custom_commands);
604 for (i = 0; i < len; i++)
606 const gchar *label = ui_prefs.custom_commands_labels[i];
608 if (EMPTY(label))
609 label = ui_prefs.custom_commands[i];
610 if (!EMPTY(label)) /* skip empty items */
612 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
613 idx++;
618 /* separator and Set menu item */
619 item = gtk_separator_menu_item_new();
620 gtk_container_add(GTK_CONTAINER(menu_edit), item);
621 gtk_widget_show(item);
623 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
627 /* (stolen from bluefish, thanks)
628 * Returns number of characters, lines and words in the supplied gchar*.
629 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
630 * Words are defined as any characters grouped, separated with spaces. */
631 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
633 guint in_word = 0;
634 gunichar utext;
636 if (! text)
637 return; /* politely refuse to operate on NULL */
639 *chars = *words = *lines = 0;
640 while (*text != '\0')
642 (*chars)++;
644 switch (*text)
646 case '\n':
647 (*lines)++;
648 /* fall through */
649 case '\r':
650 case '\f':
651 case '\t':
652 case ' ':
653 case '\v':
654 mb_word_separator:
655 if (in_word)
657 in_word = 0;
658 (*words)++;
660 break;
661 default:
662 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
663 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
664 goto mb_word_separator;
665 if (g_unichar_isgraph(utext)) /* Is this something printable? */
666 in_word = 1;
667 break;
669 /* Even if the current char is 2 bytes, this will iterate correctly. */
670 text = g_utf8_next_char(text);
673 /* Capture last word, if there's no whitespace at the end of the file. */
674 if (in_word)
675 (*words)++;
676 /* We start counting line numbers from 1 */
677 if (*chars > 0)
678 (*lines)++;
682 void tools_word_count(void)
684 GtkWidget *dialog, *label, *vbox, *table;
685 GeanyDocument *doc;
686 guint chars = 0, lines = 0, words = 0;
687 gchar *text;
688 const gchar *range;
690 doc = document_get_current();
691 g_return_if_fail(doc != NULL);
693 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
694 GTK_DIALOG_DESTROY_WITH_PARENT,
695 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
696 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
697 gtk_widget_set_name(dialog, "GeanyDialog");
699 if (sci_has_selection(doc->editor->sci))
701 text = sci_get_selection_contents(doc->editor->sci);
702 range = _("selection");
704 else
706 text = sci_get_contents(doc->editor->sci, -1);
707 range = _("whole document");
709 word_count(text, &chars, &lines, &words);
710 g_free(text);
712 table = gtk_table_new(4, 2, FALSE);
713 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
714 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
716 label = gtk_label_new(_("Range:"));
717 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
718 (GtkAttachOptions) (GTK_FILL),
719 (GtkAttachOptions) (0), 0, 0);
720 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
722 label = gtk_label_new(range);
723 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
724 (GtkAttachOptions) (GTK_FILL),
725 (GtkAttachOptions) (0), 20, 0);
726 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
728 label = gtk_label_new(_("Lines:"));
729 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
730 (GtkAttachOptions) (GTK_FILL),
731 (GtkAttachOptions) (0), 0, 0);
732 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
734 text = g_strdup_printf("%d", lines);
735 label = gtk_label_new(text);
736 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
737 (GtkAttachOptions) (GTK_FILL),
738 (GtkAttachOptions) (0), 20, 0);
739 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
740 g_free(text);
742 label = gtk_label_new(_("Words:"));
743 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
744 (GtkAttachOptions) (GTK_FILL),
745 (GtkAttachOptions) (0), 0, 0);
746 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
748 text = g_strdup_printf("%d", words);
749 label = gtk_label_new(text);
750 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
751 (GtkAttachOptions) (GTK_FILL),
752 (GtkAttachOptions) (0), 20, 0);
753 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
754 g_free(text);
756 label = gtk_label_new(_("Characters:"));
757 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
758 (GtkAttachOptions) (GTK_FILL),
759 (GtkAttachOptions) (0), 0, 0);
760 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
762 text = g_strdup_printf("%d", chars);
763 label = gtk_label_new(text);
764 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
765 (GtkAttachOptions) (GTK_FILL),
766 (GtkAttachOptions) (0), 20, 0);
767 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
768 g_free(text);
770 gtk_container_add(GTK_CONTAINER(vbox), table);
772 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
773 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
775 gtk_widget_show_all(dialog);
780 * color dialog callbacks
782 static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
784 switch (response)
786 case GTK_RESPONSE_OK:
787 gtk_widget_hide(ui_widgets.open_colorsel);
788 /* fall through */
789 case GTK_RESPONSE_APPLY:
791 GdkColor color;
792 GeanyDocument *doc = document_get_current();
793 gchar *hex;
794 GtkWidget *colorsel;
796 g_return_if_fail(doc != NULL);
798 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
799 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color);
801 hex = utils_get_hex_from_color(&color);
802 editor_insert_color(doc->editor, hex);
803 g_free(hex);
804 break;
807 default:
808 gtk_widget_hide(ui_widgets.open_colorsel);
813 static void on_color_selection_change_palette_with_screen(GdkScreen *screen, const GdkColor *colors, gint n_colors)
815 GtkSettings *settings;
817 /* Get the updated palette */
818 g_free(ui_prefs.color_picker_palette);
819 ui_prefs.color_picker_palette = gtk_color_selection_palette_to_string(colors, n_colors);
821 /* Update the gtk-color-palette setting so all GtkColorSelection widgets will be modified */
822 settings = gtk_settings_get_for_screen(screen);
823 g_object_set(G_OBJECT(settings), "gtk-color-palette", ui_prefs.color_picker_palette, NULL);
827 /* This shows the color selection dialog to choose a color. */
828 void tools_color_chooser(const gchar *color)
830 GdkColor gc;
831 GtkWidget *colorsel;
833 if (ui_widgets.open_colorsel == NULL)
835 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
836 gtk_dialog_add_button(GTK_DIALOG(ui_widgets.open_colorsel), GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
837 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets.open_colorsel),
838 GTK_RESPONSE_APPLY, GTK_RESPONSE_CANCEL, GTK_RESPONSE_OK, -1);
839 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
840 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
841 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
842 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE);
843 gtk_color_selection_set_change_palette_with_screen_hook(on_color_selection_change_palette_with_screen);
845 g_signal_connect(ui_widgets.open_colorsel, "response",
846 G_CALLBACK(on_color_dialog_response), NULL);
847 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
848 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
850 else
851 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
852 /* if color is non-NULL set it in the dialog as preselected color */
853 if (color != NULL && utils_parse_color(color, &gc))
855 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
856 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
859 /* We make sure the dialog is visible. */
860 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));