geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

tools.c
Go to the documentation of this file.
1/*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2006 The Geany contributors
5 *
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.
10 *
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.
15 *
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.
19 */
20
21/*
22 * Miscellaneous code for the built-in Tools menu items, and custom command code.
23 * For Plugins code see plugins.c.
24 */
25
26#ifdef HAVE_CONFIG_H
27# include "config.h"
28#endif
29
30#include "tools.h"
31
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"
40
41#include <stdlib.h>
42#include <unistd.h>
43#include <string.h>
44#include <errno.h>
45#include <gtk/gtk.h>
46
47
48enum
49{
56};
57
58/* custom commands code*/
60{
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;
70};
71
72
73/* update STATUS and TOOLTIP columns according to cmd */
74static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
75{
76 GError *err = NULL;
77 const gchar *stock_id = GTK_STOCK_NO;
78 gchar *tooltip = NULL;
79
80 if (EMPTY(cmd) || spawn_check_command(cmd, TRUE, &err))
81 stock_id = GTK_STOCK_YES;
82 else
83 {
84 tooltip = g_strdup_printf(_("Invalid command: %s"), err->message);
85 g_error_free(err);
86 }
87
88 gtk_list_store_set(store, iter, CC_COLUMN_STATUS, stock_id, CC_COLUMN_TOOLTIP, tooltip, -1);
89 g_free(tooltip);
90}
91
92
93/* adds a new row for custom command @p idx, or an new empty one if < 0 */
94static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
95{
96 GtkTreeIter iter;
97 const gchar *cmd = NULL;
98 const gchar *label = NULL;
99 guint id = cc->count;
100
101 if (idx >= 0)
102 {
103 cmd = ui_prefs.custom_commands[idx];
104 label = ui_prefs.custom_commands_labels[idx];
105 }
106
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);
111
112 if (start_editing)
113 {
114 GtkTreePath *path;
115
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);
120 }
121}
122
123
124static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
125{
126 cc_dialog_add_command(cc, -1, TRUE);
127}
128
129
130static void scroll_to_cursor(GtkTreeView *view)
131{
132 GtkTreePath *path;
133 GtkTreeViewColumn *column;
134
135 gtk_tree_view_get_cursor(view, &path, &column);
136 if (path)
137 {
138 gtk_tree_view_scroll_to_cell(view, path, column, FALSE, 1.0, 1.0);
139 gtk_tree_path_free(path);
140 }
141}
142
143static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
144{
145 GtkTreeIter iter;
146
147 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
148 {
149 gtk_list_store_remove(cc->store, &iter);
150 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
151 }
152}
153
154
155static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
156{
157 GtkTreeIter iter;
158
159 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
160 {
161 GtkTreePath *path;
162 GtkTreeIter prev;
163
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))
167 {
168 gtk_list_store_move_before(cc->store, &iter, &prev);
169 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
170 }
171 gtk_tree_path_free(path);
172 }
173}
174
175
176static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
177{
178 GtkTreeIter iter;
179
180 if (gtk_tree_selection_get_selected(cc->selection, NULL, &iter))
181 {
182 GtkTreeIter next = iter;
183
184 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &next))
185 {
186 gtk_list_store_move_after(cc->store, &iter, &next);
187 scroll_to_cursor(GTK_TREE_VIEW(cc->view));
188 }
189 }
190}
191
192
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. */
197{
198 GError *error = NULL;
199 gchar *sel;
200 SpawnWriteData input;
201 GString *output;
202 GString *errors;
203 gint status;
204
205 g_return_if_fail(doc != NULL && command != NULL);
206
207 if (! sci_has_selection(doc->editor->sci))
208 editor_select_lines(doc->editor, FALSE);
209
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);
216
217 if (spawn_sync(NULL, command, NULL, NULL, &input, output, errors, &status, &error))
218 {
219 if (errors->len > 0)
220 {
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);
226 }
227 else if (!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS)
228 {
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."));
232 }
233 else
234 { /* Command completed successfully */
235 sci_replace_sel(doc->editor->sci, output->str);
236 }
237 }
238 else
239 {
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);
243 }
244
245 g_string_free(output, TRUE);
246 g_string_free(errors, TRUE);
247 g_free(sel);
248}
249
250
251static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
252 struct cc_dialog *cc)
253{
254 GtkTreeIter iter;
255
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);
259}
260
261
262static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text,
263 struct cc_dialog *cc)
264{
265 GtkTreeIter iter;
266
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);
269}
270
271
272/* re-compute IDs to reflect the current store state */
273static void cc_dialog_update_ids(struct cc_dialog *cc)
274{
275 GtkTreeIter iter;
276
277 cc->count = 1;
278 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc->store), &iter))
279 return;
280
281 do
282 {
283 gtk_list_store_set(cc->store, &iter, CC_COLUMN_ID, cc->count, -1);
284 cc->count++;
285 }
286 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc->store), &iter));
287}
288
289
290/* update sensitiveness of the buttons according to the selection */
292{
293 GtkTreeIter iter;
294 gboolean has_selection = FALSE;
295 gboolean first_selected = FALSE;
296 gboolean last_selected = FALSE;
297
298 if ((has_selection = gtk_tree_selection_get_selected(cc->selection, NULL, &iter)))
299 {
300 GtkTreePath *path;
301 GtkTreePath *copy;
302
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);
310 }
311
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);
315}
316
317
318static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
319{
321}
322
323
324static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
325 struct cc_dialog *cc)
326{
329}
330
331
332static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
333{
336}
337
338
339static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
340 gpointer new_order, struct cc_dialog *cc)
341{
344}
345
346
348{
349 GtkWidget *dialog, *label, *vbox, *scroll, *buttonbox;
350 GtkCellRenderer *renderer;
351 GtkTreeViewColumn *column;
352 guint i;
353 struct cc_dialog cc;
354
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");
362
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);
367
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));
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);
398
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);
405
406 if (ui_prefs.custom_commands != NULL)
407 {
408 GtkTreeIter iter;
409 guint len = g_strv_length(ui_prefs.custom_commands);
410
411 for (i = 0; i < len; i++)
412 {
413 if (EMPTY(ui_prefs.custom_commands[i]))
414 continue; /* skip empty fields */
415
416 cc_dialog_add_command(&cc, i, FALSE);
417 }
418
419 /* focus the first row if any */
420 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
421 {
422 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(cc.store), &iter);
423
424 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc.view), path, cc.edit_column, FALSE);
425 gtk_tree_path_free(path);
426 }
427 }
428
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);
444
446
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);
452
454
455 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
456 {
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;
463
464 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc.store), &iter))
465 {
466 do
467 {
468 gchar *cmd;
469 gchar *lbl;
470
471 gtk_tree_model_get(GTK_TREE_MODEL(cc.store), &iter, CC_COLUMN_CMD, &cmd, CC_COLUMN_LABEL, &lbl, -1);
472 if (!EMPTY(cmd))
473 {
474 cmd_list = g_slist_prepend(cmd_list, cmd);
475 lbl_list = g_slist_prepend(lbl_list, lbl);
476 len++;
477 }
478 else
479 {
480 g_free(cmd);
481 g_free(lbl);
482 }
483 }
484 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc.store), &iter));
485 }
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)
490 {
491 gint j = 0;
492 GSList *cmd_node, *lbl_node;
493
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)
498 {
499 commands[j] = (gchar*) cmd_node->data;
500 labels[j] = (gchar*) lbl_node->data;
501 j++;
502 }
503 /* null-terminate the arrays */
504 commands[j] = NULL;
505 labels[j] = NULL;
506 }
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 */
514
515 g_slist_free(cmd_list);
516 g_slist_free(lbl_list);
517 }
518 gtk_widget_destroy(dialog);
519}
520
521
522static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
523{
525 gint command_idx;
526
527 g_return_if_fail(DOC_VALID(doc));
528
529 command_idx = GPOINTER_TO_INT(user_data);
530
531 if (ui_prefs.custom_commands == NULL ||
532 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
533 {
535 return;
536 }
537
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]);
541}
542
543
544static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
545{
546 GtkWidget *item;
547 gint key_idx = -1;
548
549 switch (idx)
550 {
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;
560 }
561
562 item = gtk_menu_item_new_with_label(label);
563 gtk_widget_set_tooltip_text(item, tooltip);
564 if (key_idx != -1)
565 {
567
568 if (kb->key > 0)
569 {
570 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
571 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
572 }
573 }
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));
578}
579
580
582{
583 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
584 GtkWidget *item;
585 GList *me_children, *node;
586
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);
592
593 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
594 {
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);
599 }
600 else
601 {
602 guint i, len;
603 gint idx = 0;
604 len = g_strv_length(ui_prefs.custom_commands);
605 for (i = 0; i < len; i++)
606 {
607 const gchar *label = ui_prefs.custom_commands_labels[i];
608
609 if (EMPTY(label))
610 label = ui_prefs.custom_commands[i];
611 if (!EMPTY(label)) /* skip empty items */
612 {
613 cc_insert_custom_command_items(menu_edit, label, ui_prefs.custom_commands[i], idx);
614 idx++;
615 }
616 }
617 }
618
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);
623
624 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), NULL, -1);
625}
626
627
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. */
632static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
633{
634 guint in_word = 0;
635 gunichar utext;
636
637 if (! text)
638 return; /* politely refuse to operate on NULL */
639
640 *chars = *words = *lines = 0;
641 while (*text != '\0')
642 {
643 (*chars)++;
644
645 switch (*text)
646 {
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)
657 {
658 in_word = 0;
659 (*words)++;
660 }
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;
669 }
670 /* Even if the current char is 2 bytes, this will iterate correctly. */
671 text = g_utf8_next_char(text);
672 }
673
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)++;
680}
681
682
684{
685 GtkWidget *dialog, *label, *vbox, *table;
686 GeanyDocument *doc;
687 guint chars = 0, lines = 0, words = 0;
688 gchar *text;
689 const gchar *range;
690
691 doc = document_get_current();
692 g_return_if_fail(doc != NULL);
693
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");
699
700 if (sci_has_selection(doc->editor->sci))
701 {
703 range = _("selection");
704 }
705 else
706 {
707 text = sci_get_contents(doc->editor->sci, -1);
708 range = _("whole document");
709 }
710 word_count(text, &chars, &lines, &words);
711 g_free(text);
712
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);
716
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);
722
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);
728
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);
734
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);
742
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);
748
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);
756
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);
762
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);
770
771 gtk_container_add(GTK_CONTAINER(vbox), table);
772
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);
775
777}
778
779
780/*
781 * color dialog callbacks
782 */
783static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
784{
785 switch (response)
786 {
787 case GTK_RESPONSE_OK:
788 gtk_widget_hide(ui_widgets.open_colorsel);
789 /* fall through */
790 case GTK_RESPONSE_APPLY:
791 {
792 GdkColor color;
794 gchar *hex;
795 GtkWidget *colorsel;
796
797 g_return_if_fail(doc != NULL);
798
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);
801
803 editor_insert_color(doc->editor, hex);
804 g_free(hex);
805 break;
806 }
807
808 default:
809 gtk_widget_hide(ui_widgets.open_colorsel);
810 }
811}
812
813
814static void on_color_selection_change_palette_with_screen(GdkScreen *screen, const GdkColor *colors, gint n_colors)
815{
816 GtkSettings *settings;
817
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);
821
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);
825}
826
827
828/* This shows the color selection dialog to choose a color. */
829void tools_color_chooser(const gchar *color)
830{
831 GdkColor gc;
832 GtkWidget *colorsel;
833
834#ifdef G_OS_WIN32
836 {
837 win32_show_color_dialog(color);
838 return;
839 }
840#endif
841
842 if (ui_widgets.open_colorsel == NULL)
843 {
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);
853
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);
858 }
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))
863 {
864 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
865 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
866 }
867
868 /* We make sure the dialog is visible. */
869 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));
870}
const gchar * command
Definition: build.c:2677
const gchar * label
Definition: build.c:2676
GeanyDocument * document_get_current(void)
Finds the current document.
Definition: document.c:371
GdkColor color
Definition: document.c:3220
Document related actions: new, save, open, etc.
#define DOC_VALID(doc_ptr)
Null-safe way to check GeanyDocument::is_valid.
Definition: document.h:162
void editor_insert_color(GeanyEditor *editor, const gchar *colour)
Definition: editor.c:4238
void editor_select_lines(GeanyEditor *editor, gboolean extra_line)
Definition: editor.c:3752
gchar * text
Definition: editor.c:83
void error(const errorSelection selection, const char *const format,...)
Definition: error.c:53
const gchar * chars[][2]
Definition: htmlchars.c:72
GeanyKeyBinding * keybindings_lookup_item(guint group_id, guint key_id)
Definition: keybindings.c:1408
Configurable keyboard shortcuts.
@ GEANY_KEY_GROUP_FORMAT
Group.
Definition: keybindings.h:107
@ GEANY_KEYS_FORMAT_SENDTOCMD5
Keybinding.
Definition: keybindings.h:270
@ GEANY_KEYS_FORMAT_SENDTOCMD7
Keybinding.
Definition: keybindings.h:272
@ GEANY_KEYS_FORMAT_SENDTOCMD3
Keybinding.
Definition: keybindings.h:166
@ GEANY_KEYS_FORMAT_SENDTOCMD4
Keybinding.
Definition: keybindings.h:269
@ GEANY_KEYS_FORMAT_SENDTOCMD9
Keybinding.
Definition: keybindings.h:274
@ GEANY_KEYS_FORMAT_SENDTOCMD8
Keybinding.
Definition: keybindings.h:273
@ GEANY_KEYS_FORMAT_SENDTOCMD6
Keybinding.
Definition: keybindings.h:271
@ GEANY_KEYS_FORMAT_SENDTOCMD2
Keybinding.
Definition: keybindings.h:206
@ GEANY_KEYS_FORMAT_SENDTOCMD1
Keybinding.
Definition: keybindings.h:220
static GtkPrintSettings * settings
Definition: printing.c:82
#define NULL
Definition: rbtree.h:150
gchar * sci_get_selection_contents(ScintillaObject *sci)
Gets selected text.
Definition: sciwrappers.c:778
void sci_replace_sel(ScintillaObject *sci, const gchar *text)
Replaces selection.
Definition: sciwrappers.c:646
gboolean sci_has_selection(ScintillaObject *sci)
Checks if there's a selection.
Definition: sciwrappers.c:920
gchar * sci_get_contents(ScintillaObject *sci, gint buffer_len)
Allocates and fills a buffer with text from the start of the document.
Definition: sciwrappers.c:743
Wrapper functions for the Scintilla editor widget SCI_* messages.
gboolean spawn_check_command(const gchar *command_line, gboolean execute, GError **error)
Checks whether a command line is valid.
Definition: spawn.c:246
gboolean spawn_sync(const gchar *working_directory, const gchar *command_line, gchar **argv, gchar **envp, SpawnWriteData *stdin_data, GString *stdout_data, GString *stderr_data, gint *exit_status, GError **error)
Executes a child synchronously.
Definition: spawn.c:1357
Portable and convenient process spawning and communication.
#define SPAWN_WIFEXITED(status)
non-zero if the child exited normally
Definition: spawn.h:34
#define SPAWN_WEXITSTATUS(status)
exit status of a child if exited normally
Definition: spawn.h:35
GtkWidget * dialog
gtk_container_add(GTK_CONTAINER(dialog->vbox), check_button)
gtk_widget_show_all(dialog)
long lines
Definition: stats.c:32
Structure for representing an open tab with all its properties.
Definition: document.h:81
GeanyEditor * editor
The editor associated with the document.
Definition: document.h:98
ScintillaObject * sci
The Scintilla editor GtkWidget.
Definition: editor.h:152
gboolean use_native_windows_dialogs
whether to use native Windows' dialogs (only used on Windows)
Definition: ui_utils.h:67
Represents a single keybinding action.
Definition: keybindings.h:75
GdkModifierType mods
Modifier keys, such as GDK_CONTROL_MASK or 0.
Definition: keybindings.h:77
guint key
Key value in lower-case, such as GDK_KEY_a or 0.
Definition: keybindings.h:76
GtkWidget * window
Main window.
Definition: ui_utils.h:80
A simple structure used by spawn_write_data() to write data to a channel.
Definition: spawn.h:93
gsize size
Size of the data.
Definition: spawn.h:95
const gchar * ptr
Pointer to the data.
Definition: spawn.h:94
GtkWidget * button_add
Definition: tools.c:66
GtkWidget * view
Definition: tools.c:62
guint count
Definition: tools.c:61
GtkTreeSelection * selection
Definition: tools.c:65
GtkTreeViewColumn * edit_column
Definition: tools.c:63
GtkWidget * button_remove
Definition: tools.c:67
GtkWidget * button_up
Definition: tools.c:68
GtkWidget * button_down
Definition: tools.c:69
GtkListStore * store
Definition: tools.c:64
Defines internationalization macros.
#define _(String)
Definition: support.h:42
static void cc_dialog_on_tree_selection_changed(GtkTreeSelection *selection, struct cc_dialog *cc)
Definition: tools.c:318
static void cc_dialog_update_row_status(GtkListStore *store, GtkTreeIter *iter, const gchar *cmd)
Definition: tools.c:74
static void cc_dialog_on_row_deleted(GtkTreeModel *model, GtkTreePath *path, struct cc_dialog *cc)
Definition: tools.c:332
static void cc_on_dialog_remove_clicked(GtkButton *button, struct cc_dialog *cc)
Definition: tools.c:143
static void cc_dialog_add_command(struct cc_dialog *cc, gint idx, gboolean start_editing)
Definition: tools.c:94
void tools_word_count(void)
Definition: tools.c:683
static void cc_on_dialog_move_down_clicked(GtkButton *button, struct cc_dialog *cc)
Definition: tools.c:176
static void cc_dialog_update_sensitive(struct cc_dialog *cc)
Definition: tools.c:291
void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
Definition: tools.c:196
void tools_create_insert_custom_command_menu_items(void)
Definition: tools.c:581
static void cc_dialog_on_rows_reordered(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer new_order, struct cc_dialog *cc)
Definition: tools.c:339
void tools_color_chooser(const gchar *color)
Definition: tools.c:829
static void cc_dialog_on_row_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, struct cc_dialog *cc)
Definition: tools.c:324
static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
Definition: tools.c:632
static void cc_on_dialog_add_clicked(GtkButton *button, struct cc_dialog *cc)
Definition: tools.c:124
static void cc_on_dialog_move_up_clicked(GtkButton *button, struct cc_dialog *cc)
Definition: tools.c:155
static void on_color_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
Definition: tools.c:783
static void on_color_selection_change_palette_with_screen(GdkScreen *screen, const GdkColor *colors, gint n_colors)
Definition: tools.c:814
static void cc_insert_custom_command_items(GtkMenu *me, const gchar *label, const gchar *tooltip, gint idx)
Definition: tools.c:544
static void cc_dialog_on_command_edited(GtkCellRendererText *renderer, gchar *path, gchar *text, struct cc_dialog *cc)
Definition: tools.c:251
static void cc_dialog_update_ids(struct cc_dialog *cc)
Definition: tools.c:273
static void scroll_to_cursor(GtkTreeView *view)
Definition: tools.c:130
@ CC_COLUMN_STATUS
Definition: tools.c:51
@ CC_COLUMN_LABEL
Definition: tools.c:54
@ CC_COLUMN_TOOLTIP
Definition: tools.c:52
@ CC_COLUMN_CMD
Definition: tools.c:53
@ CC_COLUMN_ID
Definition: tools.c:50
@ CC_COLUMN_COUNT
Definition: tools.c:55
static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
Definition: tools.c:522
static void cc_dialog_on_label_edited(GtkCellRendererText *renderer, gchar *path, gchar *text, struct cc_dialog *cc)
Definition: tools.c:262
static void cc_show_dialog_custom_commands(void)
Definition: tools.c:347
void ui_dialog_set_primary_button_order(GtkDialog *dialog, gint response,...)
Definition: ui_utils.c:1523
GtkWidget * ui_dialog_vbox_new(GtkDialog *dialog)
Makes a fixed border for dialogs without increasing the button box border.
Definition: ui_utils.c:1499
GeanyMainWidgets main_widgets
Definition: ui_utils.c:72
void ui_set_statusbar(gboolean log, const gchar *format,...)
Displays text on the statusbar.
Definition: ui_utils.c:168
UIPrefs ui_prefs
Definition: ui_utils.c:74
GtkWidget * ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
Returns a widget from a name in a component, usually created by Glade.
Definition: ui_utils.c:2743
void ui_tree_view_set_tooltip_text_column(GtkTreeView *tree_view, gint column)
Adds text tooltips to a tree view.
Definition: ui_utils.c:1876
UIWidgets ui_widgets
Definition: ui_utils.c:75
GeanyInterfacePrefs interface_prefs
Definition: ui_utils.c:71
User Interface general utility functions.
gboolean utils_parse_color(const gchar *spec, GdkColor *color)
Definition: utils.c:976
gchar * utils_get_hex_from_color(GdkColor *color)
Definition: utils.c:883
General utility functions, non-GTK related.
#define foreach_list(node, list)
Iterates all the nodes in list.
Definition: utils.h:115
#define EMPTY(ptr)
Returns TRUE if ptr is NULL or *ptr is FALSE.
Definition: utils.h:38