Store project files in base directory by default
[geany-mirror.git] / src / tools.c
blob5f9e5c74d3f3bacdeba9079e573fe5e1d117eb0d
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"
39 #include "win32.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <gtk/gtk.h>
48 enum
50 CC_COLUMN_ID,
51 CC_COLUMN_STATUS,
52 CC_COLUMN_TOOLTIP,
53 CC_COLUMN_CMD,
54 CC_COLUMN_LABEL,
55 CC_COLUMN_COUNT
58 /* custom commands code*/
59 struct cc_dialog
61 guint count;
62 GtkWidget *view;
63 GtkTreeViewColumn *edit_column;
64 GtkListStore *store;
65 GtkTreeSelection *selection;
66 GtkWidget *button_add;
67 GtkWidget *button_remove;
68 GtkWidget *button_up;
69 GtkWidget *button_down;
73 /* update STATUS and TOOLTIP columns according to cmd */
74 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
76 GError *err = NULL;
77 const gchar *stock_id = GTK_STOCK_NO;
78 gchar *tooltip = NULL;
80 if (EMPTY(cmd) || spawn_check_command(cmd, TRUE, &err))
81 stock_id = GTK_STOCK_YES;
82 else
84 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
85 g_error_free(err);
88 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
89 g_free(tooltip);
93 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
94 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
96 GtkTreeIter iter;
97 const gchar *cmd = NULL;
98 const gchar *label = NULL;
99 guint id = cc->count;
101 if (idx >= 0)
103 cmd = ui_prefs.custom_commands[idx];
104 label = ui_prefs.custom_commands_labels[idx];
107 cc->count++;
108 gtk_list_store_append(cc->store, &iter);
109 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, CC_COLUMN_LABEL, label, -1);
110 cc_dialog_update_row_status(cc->store, &iter, cmd);
112 if (start_editing)
114 GtkTreePath *path;
116 gtk_widget_grab_focus(cc->view);
117 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
118 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
119 gtk_tree_path_free(path);
124 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
126 cc_dialog_add_command(cc, -1, TRUE);
130 static void scroll_to_cursor(GtkTreeView *view)
132 GtkTreePath *path;
133 GtkTreeViewColumn *column;
135 gtk_tree_view_get_cursor(view, &path, &column);
136 if (path)
138 gtk_tree_view_scroll_to_cell(view, path, column, FALSE, 1.0, 1.0);
139 gtk_tree_path_free(path);
143 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
145 GtkTreeIter iter;
147 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
149 gtk_list_store_remove(cc->store, &iter);
150 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
155 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
157 GtkTreeIter iter;
159 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
161 GtkTreePath *path;
162 GtkTreeIter prev;
164 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
165 if (gtk_tree_path_prev(path) &&
166 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
168 gtk_list_store_move_before(cc->store, &iter, &prev);
169 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
171 gtk_tree_path_free(path);
176 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
178 GtkTreeIter iter;
180 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
182 GtkTreeIter next = iter;
184 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
186 gtk_list_store_move_after(cc->store, &iter, &next);
187 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
193 /* Executes command (which should include all necessary command line args) and passes the current
194 * selection through the standard input of command. The whole output of command replaces the
195 * current selection. */
196 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
198 GError *error = NULL;
199 gchar *sel;
200 SpawnWriteData input;
201 GString *output;
202 GString *errors;
203 gint status;
205 g_return_if_fail(doc != NULL && command != NULL);
207 if (! sci_has_selection(doc->editor->sci))
208 editor_select_lines(doc->editor, FALSE);
210 sel = sci_get_selection_contents(doc->editor->sci);
211 input.ptr = sel;
212 input.size = strlen(sel);
213 output = g_string_sized_new(256);
214 errors = g_string_new(NULL);
215 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
217 if (spawn_sync(NULL, command, NULL, NULL, &input, output, errors, &status, &error))
219 if (errors->len > 0)
221 g_warning("%s: %s\n", command, errors->str);
222 ui_set_statusbar(TRUE,
223 _("The executed custom command returned an error. "
224 "Your selection was not changed. Error message: %s"),
225 errors->str);
227 else if (!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS)
229 /* TODO maybe include the exit code in the error message */
230 ui_set_statusbar(TRUE,
231 _("The executed custom command exited with an unsuccessful exit code."));
233 else
234 { /* Command completed successfully */
235 sci_replace_sel(doc->editor->sci, output->str);
238 else
240 ui_set_statusbar(TRUE, _("Cannot execute custom command \"%s\": %s. "
241 "Check the path setting in Custom Commands."), command, error->message);
242 g_error_free(error);
245 g_string_free(output, TRUE);
246 g_string_free(errors, TRUE);
247 g_free(sel);
251 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
252 struct cc_dialog *cc)
254 GtkTreeIter iter;
256 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
257 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
258 cc_dialog_update_row_status(cc->store, &iter, text);
262 static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
263 struct cc_dialog *cc)
265 GtkTreeIter iter;
267 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
268 gtk_list_store_set(cc->store, &iter, CC_COLUMN_LABEL, text, -1);
272 /* re-compute IDs to reflect the current store state */
273 static void cc_dialog_update_ids(struct cc_dialog *cc)
275 GtkTreeIter iter;
277 cc->count = 1;
278 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
279 return;
283 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
284 cc->count++;
286 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
290 /* update sensitiveness of the buttons according to the selection */
291 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
293 GtkTreeIter iter;
294 gboolean has_selection = FALSE;
295 gboolean first_selected = FALSE;
296 gboolean last_selected = FALSE;
298 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
300 GtkTreePath *path;
301 GtkTreePath *copy;
303 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
304 copy = gtk_tree_path_copy(path);
305 first_selected = ! gtk_tree_path_prev(copy);
306 gtk_tree_path_free(copy);
307 gtk_tree_path_next(path);
308 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
309 gtk_tree_path_free(path);
312 gtk_widget_set_sensitive(cc->button_remove, has_selection);
313 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
314 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
318 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
320 cc_dialog_update_sensitive(cc);
324 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
325 struct cc_dialog *cc)
327 cc_dialog_update_ids(cc);
328 cc_dialog_update_sensitive(cc);
332 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
334 cc_dialog_update_ids(cc);
335 cc_dialog_update_sensitive(cc);
339 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
340 gpointer new_order, struct cc_dialog *cc)
342 cc_dialog_update_ids(cc);
343 cc_dialog_update_sensitive(cc);
347 static void cc_show_dialog_custom_commands(void)
349 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
350 GtkCellRenderer *renderer;
351 GtkTreeViewColumn *column;
352 guint i;
353 struct cc_dialog cc;
355 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
356 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
357 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
358 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
359 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
360 gtk_box_set_spacing(GTK_BOX(vbox), 6);
361 gtk_widget_set_name(dialog, "GeanyDialog");
363 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."));
364 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
365 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
366 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
368 cc.count = 1;
369 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
370 G_TYPE_STRING, G_TYPE_STRING);
371 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
372 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
373 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
374 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
375 /* ID column */
376 renderer = gtk_cell_renderer_text_new();
377 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
378 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
379 /* command column, holding status and command display */
380 column = g_object_new(GTK_TYPE_TREE_VIEW_COLUMN, "title", _("Command"), "expand", TRUE, "resizable", TRUE, NULL);
381 renderer = gtk_cell_renderer_pixbuf_new();
382 gtk_tree_view_column_pack_start(column, renderer, FALSE);
383 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
384 renderer = gtk_cell_renderer_text_new();
385 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
386 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
387 gtk_tree_view_column_pack_start(column, renderer, TRUE);
388 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
389 cc.edit_column = column;
390 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
391 /* label column */
392 renderer = gtk_cell_renderer_text_new();
393 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
394 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_label_edited), &cc);
395 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", CC_COLUMN_LABEL, NULL);
396 g_object_set(column, "expand", TRUE, "resizable", TRUE, NULL);
397 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
399 scroll = gtk_scrolled_window_new(NULL, NULL);
400 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
401 GTK_POLICY_AUTOMATIC);
402 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
403 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
404 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
406 if (ui_prefs.custom_commands != NULL)
408 GtkTreeIter iter;
409 guint len = g_strv_length(ui_prefs.custom_commands);
411 for (i = 0; i < len; i++)
413 if (EMPTY(ui_prefs.custom_commands[i]))
414 continue; /* skip empty fields */
416 cc_dialog_add_command(&cc, i, FALSE);
419 /* focus the first row if any */
420 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
422 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
424 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
425 gtk_tree_path_free(path);
429 buttonbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
430 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
431 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
432 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
433 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
434 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
435 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
436 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
437 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
438 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
439 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
440 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
441 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
442 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
443 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
445 cc_dialog_update_sensitive(&cc);
447 /* only connect the selection signal when all other cc_dialog fields are set */
448 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
449 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
450 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
451 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
453 gtk_widget_show_all(vbox);
455 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
457 GSList *cmd_list = NULL;
458 GSList *lbl_list = NULL;
459 gint len = 0;
460 gchar **commands = NULL;
461 gchar **labels = NULL;
462 GtkTreeIter iter;
464 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
468 gchar *cmd;
469 gchar *lbl;
471 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
472 if (!EMPTY(cmd))
474 cmd_list = g_slist_prepend(cmd_list, cmd);
475 lbl_list = g_slist_prepend(lbl_list, lbl);
476 len++;
478 else
480 g_free(cmd);
481 g_free(lbl);
484 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
486 cmd_list = g_slist_reverse(cmd_list);
487 lbl_list = g_slist_reverse(lbl_list);
488 /* create a new null-terminated array but only if there is any commands defined */
489 if (len > 0)
491 gint j = 0;
492 GSList *cmd_node, *lbl_node;
494 commands = g_new(gchar*, len + 1);
495 labels = g_new(gchar*, len + 1);
496 /* walk commands and labels lists */
497 for (cmd_node = cmd_list, lbl_node = lbl_list; cmd_node != NULL; cmd_node = cmd_node->next, lbl_node = lbl_node->next)
499 commands[j] = (gchar*) cmd_node->data;
500 labels[j] = (gchar*) lbl_node->data;
501 j++;
503 /* null-terminate the arrays */
504 commands[j] = NULL;
505 labels[j] = NULL;
507 /* set the new arrays */
508 g_strfreev(ui_prefs.custom_commands);
509 ui_prefs.custom_commands = commands;
510 g_strfreev(ui_prefs.custom_commands_labels);
511 ui_prefs.custom_commands_labels = labels;
512 /* rebuild the menu items */
513 tools_create_insert_custom_command_menu_items();
515 g_slist_free(cmd_list);
516 g_slist_free(lbl_list);
518 gtk_widget_destroy(dialog);
522 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
524 GeanyDocument *doc = document_get_current();
525 gint command_idx;
527 g_return_if_fail(DOC_VALID(doc));
529 command_idx = GPOINTER_TO_INT(user_data);
531 if (ui_prefs.custom_commands == NULL ||
532 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
534 cc_show_dialog_custom_commands();
535 return;
538 /* send it through the command and when the command returned the output the current selection
539 * will be replaced */
540 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
544 static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
546 GtkWidget *item;
547 gint key_idx = -1;
549 switch (idx)
551 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
552 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
553 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
554 case 3: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD4; break;
555 case 4: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD5; break;
556 case 5: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD6; break;
557 case 6: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD7; break;
558 case 7: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD8; break;
559 case 8: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD9; break;
562 item = gtk_menu_item_new_with_label(label);
563 gtk_widget_set_tooltip_text(item, tooltip);
564 if (key_idx != -1)
566 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
568 if (kb->key > 0)
570 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
571 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
574 gtk_container_add(GTK_CONTAINER(me), item);
575 gtk_widget_show(item);
576 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
577 GINT_TO_POINTER(idx));
581 void tools_create_insert_custom_command_menu_items(void)
583 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
584 GtkWidget *item;
585 GList *me_children, *node;
587 /* first clean the menus to be able to rebuild them */
588 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
589 foreach_list(node, me_children)
590 gtk_widget_destroy(GTK_WIDGET(node->data));
591 g_list_free(me_children);
593 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
595 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
596 gtk_container_add(GTK_CONTAINER(menu_edit), item);
597 gtk_widget_set_sensitive(item, FALSE);
598 gtk_widget_show(item);
600 else
602 guint i, len;
603 gint idx = 0;
604 len = g_strv_length(ui_prefs.custom_commands);
605 for (i = 0; i < len; i++)
607 const gchar *label = ui_prefs.custom_commands_labels[i];
609 if (EMPTY(label))
610 label = ui_prefs.custom_commands[i];
611 if (!EMPTY(label)) /* skip empty items */
613 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
614 idx++;
619 /* separator and Set menu item */
620 item = gtk_separator_menu_item_new();
621 gtk_container_add(GTK_CONTAINER(menu_edit), item);
622 gtk_widget_show(item);
624 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
628 /* (stolen from bluefish, thanks)
629 * Returns number of characters, lines and words in the supplied gchar*.
630 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
631 * Words are defined as any characters grouped, separated with spaces. */
632 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
634 guint in_word = 0;
635 gunichar utext;
637 if (! text)
638 return; /* politely refuse to operate on NULL */
640 *chars = *words = *lines = 0;
641 while (*text != '\0')
643 (*chars)++;
645 switch (*text)
647 case '\n':
648 (*lines)++;
649 /* fall through */
650 case '\r':
651 case '\f':
652 case '\t':
653 case ' ':
654 case '\v':
655 mb_word_separator:
656 if (in_word)
658 in_word = 0;
659 (*words)++;
661 break;
662 default:
663 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
664 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
665 goto mb_word_separator;
666 if (g_unichar_isgraph(utext)) /* Is this something printable? */
667 in_word = 1;
668 break;
670 /* Even if the current char is 2 bytes, this will iterate correctly. */
671 text = g_utf8_next_char(text);
674 /* Capture last word, if there's no whitespace at the end of the file. */
675 if (in_word)
676 (*words)++;
677 /* We start counting line numbers from 1 */
678 if (*chars > 0)
679 (*lines)++;
683 void tools_word_count(void)
685 GtkWidget *dialog, *label, *vbox, *table;
686 GeanyDocument *doc;
687 guint chars = 0, lines = 0, words = 0;
688 gchar *text;
689 const gchar *range;
691 doc = document_get_current();
692 g_return_if_fail(doc != NULL);
694 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
695 GTK_DIALOG_DESTROY_WITH_PARENT,
696 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
697 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
698 gtk_widget_set_name(dialog, "GeanyDialog");
700 if (sci_has_selection(doc->editor->sci))
702 text = sci_get_selection_contents(doc->editor->sci);
703 range = _("selection");
705 else
707 text = sci_get_contents(doc->editor->sci, -1);
708 range = _("whole document");
710 word_count(text, &chars, &lines, &words);
711 g_free(text);
713 table = gtk_table_new(4, 2, FALSE);
714 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
715 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
717 label = gtk_label_new(_("Range:"));
718 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
719 (GtkAttachOptions) (GTK_FILL),
720 (GtkAttachOptions) (0), 0, 0);
721 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
723 label = gtk_label_new(range);
724 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
725 (GtkAttachOptions) (GTK_FILL),
726 (GtkAttachOptions) (0), 20, 0);
727 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
729 label = gtk_label_new(_("Lines:"));
730 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
731 (GtkAttachOptions) (GTK_FILL),
732 (GtkAttachOptions) (0), 0, 0);
733 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
735 text = g_strdup_printf("%d", lines);
736 label = gtk_label_new(text);
737 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
738 (GtkAttachOptions) (GTK_FILL),
739 (GtkAttachOptions) (0), 20, 0);
740 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
741 g_free(text);
743 label = gtk_label_new(_("Words:"));
744 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
745 (GtkAttachOptions) (GTK_FILL),
746 (GtkAttachOptions) (0), 0, 0);
747 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
749 text = g_strdup_printf("%d", words);
750 label = gtk_label_new(text);
751 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
752 (GtkAttachOptions) (GTK_FILL),
753 (GtkAttachOptions) (0), 20, 0);
754 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
755 g_free(text);
757 label = gtk_label_new(_("Characters:"));
758 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
759 (GtkAttachOptions) (GTK_FILL),
760 (GtkAttachOptions) (0), 0, 0);
761 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
763 text = g_strdup_printf("%d", chars);
764 label = gtk_label_new(text);
765 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
766 (GtkAttachOptions) (GTK_FILL),
767 (GtkAttachOptions) (0), 20, 0);
768 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
769 g_free(text);
771 gtk_container_add(GTK_CONTAINER(vbox), table);
773 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
774 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
776 gtk_widget_show_all(dialog);
781 * color dialog callbacks
783 static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
785 switch (response)
787 case GTK_RESPONSE_OK:
788 gtk_widget_hide(ui_widgets.open_colorsel);
789 /* fall through */
790 case GTK_RESPONSE_APPLY:
792 GdkColor color;
793 GeanyDocument *doc = document_get_current();
794 gchar *hex;
795 GtkWidget *colorsel;
797 g_return_if_fail(doc != NULL);
799 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
800 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color);
802 hex = utils_get_hex_from_color(&color);
803 editor_insert_color(doc->editor, hex);
804 g_free(hex);
805 break;
808 default:
809 gtk_widget_hide(ui_widgets.open_colorsel);
814 static void on_color_selection_change_palette_with_screen(GdkScreen *screen, const GdkColor *colors, gint n_colors)
816 GtkSettings *settings;
818 /* Get the updated palette */
819 g_free(ui_prefs.color_picker_palette);
820 ui_prefs.color_picker_palette = gtk_color_selection_palette_to_string(colors, n_colors);
822 /* Update the gtk-color-palette setting so all GtkColorSelection widgets will be modified */
823 settings = gtk_settings_get_for_screen(screen);
824 g_object_set(G_OBJECT(settings), "gtk-color-palette", ui_prefs.color_picker_palette, NULL);
828 /* This shows the color selection dialog to choose a color. */
829 void tools_color_chooser(const gchar *color)
831 GdkColor gc;
832 GtkWidget *colorsel;
834 #ifdef G_OS_WIN32
835 if (interface_prefs.use_native_windows_dialogs)
837 win32_show_color_dialog(color);
838 return;
840 #endif
842 if (ui_widgets.open_colorsel == NULL)
844 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
845 gtk_dialog_add_button(GTK_DIALOG(ui_widgets.open_colorsel), GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
846 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets.open_colorsel),
847 GTK_RESPONSE_APPLY, GTK_RESPONSE_CANCEL, GTK_RESPONSE_OK, -1);
848 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
849 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
850 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
851 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE);
852 gtk_color_selection_set_change_palette_with_screen_hook(on_color_selection_change_palette_with_screen);
854 g_signal_connect(ui_widgets.open_colorsel, "response",
855 G_CALLBACK(on_color_dialog_response), NULL);
856 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
857 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
859 else
860 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
861 /* if color is non-NULL set it in the dialog as preselected color */
862 if (color != NULL && utils_parse_color(color, &gc))
864 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
865 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
868 /* We make sure the dialog is visible. */
869 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));