Persist colour picker palette
[geany-mirror.git] / src / tools.c
blob6365a201a0096ffe72abf3e6361698455e6ddee2
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 "gtkcompat.h"
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <errno.h>
49 enum
51 CC_COLUMN_ID,
52 CC_COLUMN_STATUS,
53 CC_COLUMN_TOOLTIP,
54 CC_COLUMN_CMD,
55 CC_COLUMN_LABEL,
56 CC_COLUMN_COUNT
59 /* custom commands code*/
60 struct cc_dialog
62 guint count;
63 GtkWidget *view;
64 GtkTreeViewColumn *edit_column;
65 GtkListStore *store;
66 GtkTreeSelection *selection;
67 GtkWidget *button_add;
68 GtkWidget *button_remove;
69 GtkWidget *button_up;
70 GtkWidget *button_down;
74 /* update STATUS and TOOLTIP columns according to cmd */
75 static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
77 GError *err = NULL;
78 const gchar *stock_id = GTK_STOCK_NO;
79 gchar *tooltip = NULL;
81 if (EMPTY(cmd) || spawn_check_command(cmd, TRUE, &err))
82 stock_id = GTK_STOCK_YES;
83 else
85 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
86 g_error_free(err);
89 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
90 g_free(tooltip);
94 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
95 static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
97 GtkTreeIter iter;
98 const gchar *cmd = NULL;
99 const gchar *label = NULL;
100 guint id = cc->count;
102 if (idx >= 0)
104 cmd = ui_prefs.custom_commands[idx];
105 label = ui_prefs.custom_commands_labels[idx];
108 cc->count++;
109 gtk_list_store_append(cc->store, &iter);
110 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, id, CC_COLUMN_CMD, cmd, CC_COLUMN_LABEL, label, -1);
111 cc_dialog_update_row_status(cc->store, &iter, cmd);
113 if (start_editing)
115 GtkTreePath *path;
117 gtk_widget_grab_focus(cc->view);
118 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
119 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc->view), path, cc->edit_column, TRUE);
120 gtk_tree_path_free(path);
125 static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
127 cc_dialog_add_command(cc, -1, TRUE);
131 static void scroll_to_cursor(GtkTreeView *view)
133 GtkTreePath *path;
134 GtkTreeViewColumn *column;
136 gtk_tree_view_get_cursor(view, &path, &column);
137 if (path)
139 gtk_tree_view_scroll_to_cell(view, path, column, FALSE, 1.0, 1.0);
140 gtk_tree_path_free(path);
144 static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
146 GtkTreeIter iter;
148 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
150 gtk_list_store_remove(cc->store, &iter);
151 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
156 static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
158 GtkTreeIter iter;
160 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
162 GtkTreePath *path;
163 GtkTreeIter prev;
165 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
166 if (gtk_tree_path_prev(path) &&
167 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &prev, path))
169 gtk_list_store_move_before(cc->store, &iter, &prev);
170 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
172 gtk_tree_path_free(path);
177 static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
179 GtkTreeIter iter;
181 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
183 GtkTreeIter next = iter;
185 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
187 gtk_list_store_move_after(cc->store, &iter, &next);
188 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
194 /* Executes command (which should include all necessary command line args) and passes the current
195 * selection through the standard input of command. The whole output of command replaces the
196 * current selection. */
197 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
199 GError *error = NULL;
200 gchar *sel;
201 SpawnWriteData input;
202 GString *output;
203 GString *errors;
204 gint status;
206 g_return_if_fail(doc != NULL && command != NULL);
208 if (! sci_has_selection(doc->editor->sci))
209 editor_select_lines(doc->editor, FALSE);
211 sel = sci_get_selection_contents(doc->editor->sci);
212 input.ptr = sel;
213 input.size = strlen(sel);
214 output = g_string_sized_new(256);
215 errors = g_string_new(NULL);
216 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
218 if (spawn_sync(NULL, command, NULL, NULL, &input, output, errors, &status, &error))
220 if (errors->len > 0)
222 g_warning("%s: %s\n", command, errors->str);
223 ui_set_statusbar(TRUE,
224 _("The executed custom command returned an error. "
225 "Your selection was not changed. Error message: %s"),
226 errors->str);
228 else if (!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS)
230 /* TODO maybe include the exit code in the error message */
231 ui_set_statusbar(TRUE,
232 _("The executed custom command exited with an unsuccessful exit code."));
234 else
235 { /* Command completed successfully */
236 sci_replace_sel(doc->editor->sci, output->str);
239 else
241 ui_set_statusbar(TRUE, _("Cannot execute custom command \"%s\": %s. "
242 "Check the path setting in Custom Commands."), command, error->message);
243 g_error_free(error);
246 g_string_free(output, TRUE);
247 g_string_free(errors, TRUE);
248 g_free(sel);
252 static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
253 struct cc_dialog *cc)
255 GtkTreeIter iter;
257 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
258 gtk_list_store_set(cc->store, &iter, CC_COLUMN_CMD, text, -1);
259 cc_dialog_update_row_status(cc->store, &iter, text);
263 static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
264 struct cc_dialog *cc)
266 GtkTreeIter iter;
268 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc->store), &iter, path);
269 gtk_list_store_set(cc->store, &iter, CC_COLUMN_LABEL, text, -1);
273 /* re-compute IDs to reflect the current store state */
274 static void cc_dialog_update_ids(struct cc_dialog *cc)
276 GtkTreeIter iter;
278 cc->count = 1;
279 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
280 return;
284 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
285 cc->count++;
287 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
291 /* update sensitiveness of the buttons according to the selection */
292 static void cc_dialog_update_sensitive(struct cc_dialog *cc)
294 GtkTreeIter iter;
295 gboolean has_selection = FALSE;
296 gboolean first_selected = FALSE;
297 gboolean last_selected = FALSE;
299 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
301 GtkTreePath *path;
302 GtkTreePath *copy;
304 path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc->store), &iter);
305 copy = gtk_tree_path_copy(path);
306 first_selected = ! gtk_tree_path_prev(copy);
307 gtk_tree_path_free(copy);
308 gtk_tree_path_next(path);
309 last_selected = ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc->store), &iter, path);
310 gtk_tree_path_free(path);
313 gtk_widget_set_sensitive(cc->button_remove, has_selection);
314 gtk_widget_set_sensitive(cc->button_up, has_selection && ! first_selected);
315 gtk_widget_set_sensitive(cc->button_down, has_selection && ! last_selected);
319 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
321 cc_dialog_update_sensitive(cc);
325 static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
326 struct cc_dialog *cc)
328 cc_dialog_update_ids(cc);
329 cc_dialog_update_sensitive(cc);
333 static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
335 cc_dialog_update_ids(cc);
336 cc_dialog_update_sensitive(cc);
340 static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
341 gpointer new_order, struct cc_dialog *cc)
343 cc_dialog_update_ids(cc);
344 cc_dialog_update_sensitive(cc);
348 static void cc_show_dialog_custom_commands(void)
350 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
351 GtkCellRenderer *renderer;
352 GtkTreeViewColumn *column;
353 guint i;
354 struct cc_dialog cc;
356 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
357 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
358 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
359 gtk_window_set_default_size(GTK_WINDOW(dialog), 300, 300); /* give a reasonable minimal default size */
360 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
361 gtk_box_set_spacing(GTK_BOX(vbox), 6);
362 gtk_widget_set_name(dialog, "GeanyDialog");
364 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."));
365 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
366 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
367 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
369 cc.count = 1;
370 cc.store = gtk_list_store_new(CC_COLUMN_COUNT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
371 G_TYPE_STRING, G_TYPE_STRING);
372 cc.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc.store));
373 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(cc.view), CC_COLUMN_TOOLTIP);
374 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc.view), TRUE);
375 cc.selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cc.view));
376 /* ID column */
377 renderer = gtk_cell_renderer_text_new();
378 column = gtk_tree_view_column_new_with_attributes(_("ID"), renderer, "text", CC_COLUMN_ID, NULL);
379 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
380 /* command column, holding status and command display */
381 column = g_object_new(GTK_TYPE_TREE_VIEW_COLUMN, "title", _("Command"), "expand", TRUE, "resizable", TRUE, NULL);
382 renderer = gtk_cell_renderer_pixbuf_new();
383 gtk_tree_view_column_pack_start(column, renderer, FALSE);
384 gtk_tree_view_column_set_attributes(column, renderer, "stock-id", CC_COLUMN_STATUS, NULL);
385 renderer = gtk_cell_renderer_text_new();
386 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
387 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_command_edited), &cc);
388 gtk_tree_view_column_pack_start(column, renderer, TRUE);
389 gtk_tree_view_column_set_attributes(column, renderer, "text", CC_COLUMN_CMD, NULL);
390 cc.edit_column = column;
391 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
392 /* label column */
393 renderer = gtk_cell_renderer_text_new();
394 g_object_set(renderer, "editable", TRUE, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
395 g_signal_connect(renderer, "edited", G_CALLBACK(cc_dialog_on_label_edited), &cc);
396 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", CC_COLUMN_LABEL, NULL);
397 g_object_set(column, "expand", TRUE, "resizable", TRUE, NULL);
398 gtk_tree_view_append_column(GTK_TREE_VIEW(cc.view), column);
400 scroll = gtk_scrolled_window_new(NULL, NULL);
401 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC,
402 GTK_POLICY_AUTOMATIC);
403 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
404 gtk_container_add(GTK_CONTAINER(scroll), cc.view);
405 gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
407 if (ui_prefs.custom_commands != NULL)
409 GtkTreeIter iter;
410 guint len = g_strv_length(ui_prefs.custom_commands);
412 for (i = 0; i < len; i++)
414 if (EMPTY(ui_prefs.custom_commands[i]))
415 continue; /* skip empty fields */
417 cc_dialog_add_command(&cc, i, FALSE);
420 /* focus the first row if any */
421 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
423 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
425 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
426 gtk_tree_path_free(path);
430 buttonbox = gtk_hbutton_box_new();
431 gtk_box_set_spacing(GTK_BOX(buttonbox), 6);
432 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 0);
433 cc.button_add = gtk_button_new_from_stock(GTK_STOCK_ADD);
434 g_signal_connect(cc.button_add, "clicked", G_CALLBACK(cc_on_dialog_add_clicked), &cc);
435 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_add);
436 cc.button_remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
437 g_signal_connect(cc.button_remove, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked), &cc);
438 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_remove);
439 cc.button_up = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
440 g_signal_connect(cc.button_up, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked), &cc);
441 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_up);
442 cc.button_down = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
443 g_signal_connect(cc.button_down, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked), &cc);
444 gtk_container_add(GTK_CONTAINER(buttonbox), cc.button_down);
446 cc_dialog_update_sensitive(&cc);
448 /* only connect the selection signal when all other cc_dialog fields are set */
449 g_signal_connect(cc.selection, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed), &cc);
450 g_signal_connect(cc.store, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted), &cc);
451 g_signal_connect(cc.store, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted), &cc);
452 g_signal_connect(cc.store, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered), &cc);
454 gtk_widget_show_all(vbox);
456 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
458 GSList *cmd_list = NULL;
459 GSList *lbl_list = NULL;
460 gint len = 0;
461 gchar **commands = NULL;
462 gchar **labels = NULL;
463 GtkTreeIter iter;
465 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
469 gchar *cmd;
470 gchar *lbl;
472 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
473 if (!EMPTY(cmd))
475 cmd_list = g_slist_prepend(cmd_list, cmd);
476 lbl_list = g_slist_prepend(lbl_list, lbl);
477 len++;
479 else
481 g_free(cmd);
482 g_free(lbl);
485 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
487 cmd_list = g_slist_reverse(cmd_list);
488 lbl_list = g_slist_reverse(lbl_list);
489 /* create a new null-terminated array but only if there is any commands defined */
490 if (len > 0)
492 gint j = 0;
493 GSList *cmd_node, *lbl_node;
495 commands = g_new(gchar*, len + 1);
496 labels = g_new(gchar*, len + 1);
497 /* walk commands and labels lists */
498 for (cmd_node = cmd_list, lbl_node = lbl_list; cmd_node != NULL; cmd_node = cmd_node->next, lbl_node = lbl_node->next)
500 commands[j] = (gchar*) cmd_node->data;
501 labels[j] = (gchar*) lbl_node->data;
502 j++;
504 /* null-terminate the arrays */
505 commands[j] = NULL;
506 labels[j] = NULL;
508 /* set the new arrays */
509 g_strfreev(ui_prefs.custom_commands);
510 ui_prefs.custom_commands = commands;
511 g_strfreev(ui_prefs.custom_commands_labels);
512 ui_prefs.custom_commands_labels = labels;
513 /* rebuild the menu items */
514 tools_create_insert_custom_command_menu_items();
516 g_slist_free(cmd_list);
517 g_slist_free(lbl_list);
519 gtk_widget_destroy(dialog);
523 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
525 GeanyDocument *doc = document_get_current();
526 gint command_idx;
528 g_return_if_fail(DOC_VALID(doc));
530 command_idx = GPOINTER_TO_INT(user_data);
532 if (ui_prefs.custom_commands == NULL ||
533 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
535 cc_show_dialog_custom_commands();
536 return;
539 /* send it through the command and when the command returned the output the current selection
540 * will be replaced */
541 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
545 static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
547 GtkWidget *item;
548 gint key_idx = -1;
550 switch (idx)
552 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
553 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
554 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
555 case 3: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD4; break;
556 case 4: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD5; break;
557 case 5: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD6; break;
558 case 6: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD7; break;
559 case 7: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD8; break;
560 case 8: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD9; break;
563 item = gtk_menu_item_new_with_label(label);
564 gtk_widget_set_tooltip_text(item, tooltip);
565 if (key_idx != -1)
567 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
569 if (kb->key > 0)
571 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
572 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
575 gtk_container_add(GTK_CONTAINER(me), item);
576 gtk_widget_show(item);
577 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
578 GINT_TO_POINTER(idx));
582 void tools_create_insert_custom_command_menu_items(void)
584 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
585 GtkWidget *item;
586 GList *me_children, *node;
588 /* first clean the menus to be able to rebuild them */
589 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
590 foreach_list(node, me_children)
591 gtk_widget_destroy(GTK_WIDGET(node->data));
592 g_list_free(me_children);
594 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
596 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
597 gtk_container_add(GTK_CONTAINER(menu_edit), item);
598 gtk_widget_set_sensitive(item, FALSE);
599 gtk_widget_show(item);
601 else
603 guint i, len;
604 gint idx = 0;
605 len = g_strv_length(ui_prefs.custom_commands);
606 for (i = 0; i < len; i++)
608 const gchar *label = ui_prefs.custom_commands_labels[i];
610 if (EMPTY(label))
611 label = ui_prefs.custom_commands[i];
612 if (!EMPTY(label)) /* skip empty items */
614 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
615 idx++;
620 /* separator and Set menu item */
621 item = gtk_separator_menu_item_new();
622 gtk_container_add(GTK_CONTAINER(menu_edit), item);
623 gtk_widget_show(item);
625 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
629 /* (stolen from bluefish, thanks)
630 * Returns number of characters, lines and words in the supplied gchar*.
631 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
632 * Words are defined as any characters grouped, separated with spaces. */
633 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
635 guint in_word = 0;
636 gunichar utext;
638 if (! text)
639 return; /* politely refuse to operate on NULL */
641 *chars = *words = *lines = 0;
642 while (*text != '\0')
644 (*chars)++;
646 switch (*text)
648 case '\n':
649 (*lines)++;
650 /* fall through */
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 static void on_color_selection_change_palette_with_screen(GdkScreen *screen, const GdkColor *colors, gint n_colors)
817 GtkSettings *settings;
819 /* Get the updated palette */
820 g_free(ui_prefs.color_picker_palette);
821 ui_prefs.color_picker_palette = gtk_color_selection_palette_to_string(colors, n_colors);
823 /* Update the gtk-color-palette setting so all GtkColorSelection widgets will be modified */
824 settings = gtk_settings_get_for_screen(screen);
825 g_object_set(G_OBJECT(settings), "gtk-color-palette", ui_prefs.color_picker_palette, NULL);
829 /* This shows the color selection dialog to choose a color. */
830 void tools_color_chooser(const gchar *color)
832 GdkColor gc;
833 GtkWidget *colorsel;
835 #ifdef G_OS_WIN32
836 if (interface_prefs.use_native_windows_dialogs)
838 win32_show_color_dialog(color);
839 return;
841 #endif
843 if (ui_widgets.open_colorsel == NULL)
845 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
846 gtk_dialog_add_button(GTK_DIALOG(ui_widgets.open_colorsel), GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
847 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets.open_colorsel),
848 GTK_RESPONSE_APPLY, GTK_RESPONSE_CANCEL, GTK_RESPONSE_OK, -1);
849 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
850 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
851 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
852 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE);
853 gtk_color_selection_set_change_palette_with_screen_hook(on_color_selection_change_palette_with_screen);
855 g_signal_connect(ui_widgets.open_colorsel, "response",
856 G_CALLBACK(on_color_dialog_response), NULL);
857 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
858 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
860 else
861 colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
862 /* if color is non-NULL set it in the dialog as preselected color */
863 if (color != NULL && utils_parse_color(color, &gc))
865 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
866 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
869 /* We make sure the dialog is visible. */
870 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));