2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Miscellaneous code for the built-in Tools menu items, and custom command code.
24 * For Plugins code see plugins.c.
34 #include "keybindings.h"
35 #include "sciwrappers.h"
42 #include "gtkcompat.h"
60 /* custom commands code*/
65 GtkTreeViewColumn
*edit_column
;
67 GtkTreeSelection
*selection
;
68 GtkWidget
*button_add
;
69 GtkWidget
*button_remove
;
71 GtkWidget
*button_down
;
75 /* update STATUS and TOOLTIP columns according to cmd */
76 static void cc_dialog_update_row_status(GtkListStore
*store
, GtkTreeIter
*iter
, const gchar
*cmd
)
79 const gchar
*stock_id
= GTK_STOCK_NO
;
80 gchar
*tooltip
= NULL
;
82 if (EMPTY(cmd
) || spawn_check_command(cmd
, TRUE
, &err
))
83 stock_id
= GTK_STOCK_YES
;
86 tooltip
= g_strdup_printf(_("Invalid command: %s"), err
->message
);
90 gtk_list_store_set(store
, iter
, CC_COLUMN_STATUS
, stock_id
, CC_COLUMN_TOOLTIP
, tooltip
, -1);
95 /* adds a new row for custom command @p idx, or an new empty one if < 0 */
96 static void cc_dialog_add_command(struct cc_dialog
*cc
, gint idx
, gboolean start_editing
)
99 const gchar
*cmd
= NULL
;
100 const gchar
*label
= NULL
;
101 guint id
= cc
->count
;
105 cmd
= ui_prefs
.custom_commands
[idx
];
106 label
= ui_prefs
.custom_commands_labels
[idx
];
110 gtk_list_store_append(cc
->store
, &iter
);
111 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_ID
, id
, CC_COLUMN_CMD
, cmd
, CC_COLUMN_LABEL
, label
, -1);
112 cc_dialog_update_row_status(cc
->store
, &iter
, cmd
);
118 gtk_widget_grab_focus(cc
->view
);
119 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
->store
), &iter
);
120 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc
->view
), path
, cc
->edit_column
, TRUE
);
121 gtk_tree_path_free(path
);
126 static void cc_on_dialog_add_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
128 cc_dialog_add_command(cc
, -1, TRUE
);
132 static void scroll_to_cursor(GtkTreeView
*view
)
135 GtkTreeViewColumn
*column
;
137 gtk_tree_view_get_cursor(view
, &path
, &column
);
140 gtk_tree_view_scroll_to_cell(view
, path
, column
, FALSE
, 1.0, 1.0);
141 gtk_tree_path_free(path
);
145 static void cc_on_dialog_remove_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
149 if (gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
))
151 gtk_list_store_remove(cc
->store
, &iter
);
152 scroll_to_cursor(GTK_TREE_VIEW(cc
->view
));
157 static void cc_on_dialog_move_up_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
161 if (gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
))
166 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
->store
), &iter
);
167 if (gtk_tree_path_prev(path
) &&
168 gtk_tree_model_get_iter(GTK_TREE_MODEL(cc
->store
), &prev
, path
))
170 gtk_list_store_move_before(cc
->store
, &iter
, &prev
);
171 scroll_to_cursor(GTK_TREE_VIEW(cc
->view
));
173 gtk_tree_path_free(path
);
178 static void cc_on_dialog_move_down_clicked(GtkButton
*button
, struct cc_dialog
*cc
)
182 if (gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
))
184 GtkTreeIter next
= iter
;
186 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc
->store
), &next
))
188 gtk_list_store_move_after(cc
->store
, &iter
, &next
);
189 scroll_to_cursor(GTK_TREE_VIEW(cc
->view
));
195 /* Executes command (which should include all necessary command line args) and passes the current
196 * selection through the standard input of command. The whole output of command replaces the
197 * current selection. */
198 void tools_execute_custom_command(GeanyDocument
*doc
, const gchar
*command
)
200 GError
*error
= NULL
;
202 SpawnWriteData input
;
207 g_return_if_fail(doc
!= NULL
&& command
!= NULL
);
209 if (! sci_has_selection(doc
->editor
->sci
))
210 editor_select_lines(doc
->editor
, FALSE
);
212 sel
= sci_get_selection_contents(doc
->editor
->sci
);
214 input
.size
= strlen(sel
);
215 output
= g_string_sized_new(256);
216 errors
= g_string_new(NULL
);
217 ui_set_statusbar(TRUE
, _("Passing data and executing custom command: %s"), command
);
219 if (spawn_sync(NULL
, command
, NULL
, NULL
, &input
, output
, errors
, &status
, &error
))
223 g_warning("%s: %s\n", command
, errors
->str
);
224 ui_set_statusbar(TRUE
,
225 _("The executed custom command returned an error. "
226 "Your selection was not changed. Error message: %s"),
229 else if (!SPAWN_WIFEXITED(status
) || SPAWN_WEXITSTATUS(status
) != EXIT_SUCCESS
)
231 /* TODO maybe include the exit code in the error message */
232 ui_set_statusbar(TRUE
,
233 _("The executed custom command exited with an unsuccessful exit code."));
236 { /* Command completed successfully */
237 sci_replace_sel(doc
->editor
->sci
, output
->str
);
242 geany_debug("spawn_sync() failed: %s", error
->message
);
243 ui_set_statusbar(TRUE
, _("Custom command failed: %s"), error
->message
);
247 g_string_free(output
, TRUE
);
248 g_string_free(errors
, TRUE
);
253 static void cc_dialog_on_command_edited(GtkCellRendererText
*renderer
, gchar
*path
, gchar
*text
,
254 struct cc_dialog
*cc
)
258 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc
->store
), &iter
, path
);
259 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_CMD
, text
, -1);
260 cc_dialog_update_row_status(cc
->store
, &iter
, text
);
264 static void cc_dialog_on_label_edited(GtkCellRendererText
*renderer
, gchar
*path
, gchar
*text
,
265 struct cc_dialog
*cc
)
269 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(cc
->store
), &iter
, path
);
270 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_LABEL
, text
, -1);
274 /* re-compute IDs to reflect the current store state */
275 static void cc_dialog_update_ids(struct cc_dialog
*cc
)
280 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc
->store
), &iter
))
285 gtk_list_store_set(cc
->store
, &iter
, CC_COLUMN_ID
, cc
->count
, -1);
288 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc
->store
), &iter
));
292 /* update sensitiveness of the buttons according to the selection */
293 static void cc_dialog_update_sensitive(struct cc_dialog
*cc
)
296 gboolean has_selection
= FALSE
;
297 gboolean first_selected
= FALSE
;
298 gboolean last_selected
= FALSE
;
300 if ((has_selection
= gtk_tree_selection_get_selected(cc
->selection
, NULL
, &iter
)))
305 path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
->store
), &iter
);
306 copy
= gtk_tree_path_copy(path
);
307 first_selected
= ! gtk_tree_path_prev(copy
);
308 gtk_tree_path_free(copy
);
309 gtk_tree_path_next(path
);
310 last_selected
= ! gtk_tree_model_get_iter(GTK_TREE_MODEL(cc
->store
), &iter
, path
);
311 gtk_tree_path_free(path
);
314 gtk_widget_set_sensitive(cc
->button_remove
, has_selection
);
315 gtk_widget_set_sensitive(cc
->button_up
, has_selection
&& ! first_selected
);
316 gtk_widget_set_sensitive(cc
->button_down
, has_selection
&& ! last_selected
);
320 static void cc_dialog_on_tree_selection_changed(GtkTreeSelection
*selection
, struct cc_dialog
*cc
)
322 cc_dialog_update_sensitive(cc
);
326 static void cc_dialog_on_row_inserted(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
,
327 struct cc_dialog
*cc
)
329 cc_dialog_update_ids(cc
);
330 cc_dialog_update_sensitive(cc
);
334 static void cc_dialog_on_row_deleted(GtkTreeModel
*model
, GtkTreePath
*path
, struct cc_dialog
*cc
)
336 cc_dialog_update_ids(cc
);
337 cc_dialog_update_sensitive(cc
);
341 static void cc_dialog_on_rows_reordered(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
,
342 gpointer new_order
, struct cc_dialog
*cc
)
344 cc_dialog_update_ids(cc
);
345 cc_dialog_update_sensitive(cc
);
349 static void cc_show_dialog_custom_commands(void)
351 GtkWidget
*dialog
, *label
, *vbox
, *scroll
, *buttonbox
;
352 GtkCellRenderer
*renderer
;
353 GtkTreeViewColumn
*column
;
357 dialog
= gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets
.window
),
358 GTK_DIALOG_DESTROY_WITH_PARENT
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
359 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
, NULL
);
360 gtk_window_set_default_size(GTK_WINDOW(dialog
), 300, 300); /* give a reasonable minimal default size */
361 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
362 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
363 gtk_widget_set_name(dialog
, "GeanyDialog");
365 label
= gtk_label_new(_("You can send the current selection to any of these commands and the output of the command replaces the current selection."));
366 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
367 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
368 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, FALSE
, 0);
371 cc
.store
= gtk_list_store_new(CC_COLUMN_COUNT
, G_TYPE_UINT
, G_TYPE_STRING
, G_TYPE_STRING
,
372 G_TYPE_STRING
, G_TYPE_STRING
);
373 cc
.view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc
.store
));
374 ui_tree_view_set_tooltip_text_column(GTK_TREE_VIEW(cc
.view
), CC_COLUMN_TOOLTIP
);
375 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(cc
.view
), TRUE
);
376 cc
.selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(cc
.view
));
378 renderer
= gtk_cell_renderer_text_new();
379 column
= gtk_tree_view_column_new_with_attributes(_("ID"), renderer
, "text", CC_COLUMN_ID
, NULL
);
380 gtk_tree_view_append_column(GTK_TREE_VIEW(cc
.view
), column
);
381 /* command column, holding status and command display */
382 column
= g_object_new(GTK_TYPE_TREE_VIEW_COLUMN
, "title", _("Command"), "expand", TRUE
, "resizable", TRUE
, NULL
);
383 renderer
= gtk_cell_renderer_pixbuf_new();
384 gtk_tree_view_column_pack_start(column
, renderer
, FALSE
);
385 gtk_tree_view_column_set_attributes(column
, renderer
, "stock-id", CC_COLUMN_STATUS
, NULL
);
386 renderer
= gtk_cell_renderer_text_new();
387 g_object_set(renderer
, "editable", TRUE
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
388 g_signal_connect(renderer
, "edited", G_CALLBACK(cc_dialog_on_command_edited
), &cc
);
389 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
390 gtk_tree_view_column_set_attributes(column
, renderer
, "text", CC_COLUMN_CMD
, NULL
);
391 cc
.edit_column
= column
;
392 gtk_tree_view_append_column(GTK_TREE_VIEW(cc
.view
), column
);
394 renderer
= gtk_cell_renderer_text_new();
395 g_object_set(renderer
, "editable", TRUE
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
396 g_signal_connect(renderer
, "edited", G_CALLBACK(cc_dialog_on_label_edited
), &cc
);
397 column
= gtk_tree_view_column_new_with_attributes(_("Label"), renderer
, "text", CC_COLUMN_LABEL
, NULL
);
398 g_object_set(column
, "expand", TRUE
, "resizable", TRUE
, NULL
);
399 gtk_tree_view_append_column(GTK_TREE_VIEW(cc
.view
), column
);
401 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
402 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
), GTK_POLICY_AUTOMATIC
,
403 GTK_POLICY_AUTOMATIC
);
404 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
), GTK_SHADOW_IN
);
405 gtk_container_add(GTK_CONTAINER(scroll
), cc
.view
);
406 gtk_box_pack_start(GTK_BOX(vbox
), scroll
, TRUE
, TRUE
, 0);
408 if (ui_prefs
.custom_commands
!= NULL
)
411 guint len
= g_strv_length(ui_prefs
.custom_commands
);
413 for (i
= 0; i
< len
; i
++)
415 if (EMPTY(ui_prefs
.custom_commands
[i
]))
416 continue; /* skip empty fields */
418 cc_dialog_add_command(&cc
, i
, FALSE
);
421 /* focus the first row if any */
422 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc
.store
), &iter
))
424 GtkTreePath
*path
= gtk_tree_model_get_path(GTK_TREE_MODEL(cc
.store
), &iter
);
426 gtk_tree_view_set_cursor(GTK_TREE_VIEW(cc
.view
), path
, cc
.edit_column
, FALSE
);
427 gtk_tree_path_free(path
);
431 buttonbox
= gtk_hbutton_box_new();
432 gtk_box_set_spacing(GTK_BOX(buttonbox
), 6);
433 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 0);
434 cc
.button_add
= gtk_button_new_from_stock(GTK_STOCK_ADD
);
435 g_signal_connect(cc
.button_add
, "clicked", G_CALLBACK(cc_on_dialog_add_clicked
), &cc
);
436 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_add
);
437 cc
.button_remove
= gtk_button_new_from_stock(GTK_STOCK_REMOVE
);
438 g_signal_connect(cc
.button_remove
, "clicked", G_CALLBACK(cc_on_dialog_remove_clicked
), &cc
);
439 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_remove
);
440 cc
.button_up
= gtk_button_new_from_stock(GTK_STOCK_GO_UP
);
441 g_signal_connect(cc
.button_up
, "clicked", G_CALLBACK(cc_on_dialog_move_up_clicked
), &cc
);
442 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_up
);
443 cc
.button_down
= gtk_button_new_from_stock(GTK_STOCK_GO_DOWN
);
444 g_signal_connect(cc
.button_down
, "clicked", G_CALLBACK(cc_on_dialog_move_down_clicked
), &cc
);
445 gtk_container_add(GTK_CONTAINER(buttonbox
), cc
.button_down
);
447 cc_dialog_update_sensitive(&cc
);
449 /* only connect the selection signal when all other cc_dialog fields are set */
450 g_signal_connect(cc
.selection
, "changed", G_CALLBACK(cc_dialog_on_tree_selection_changed
), &cc
);
451 g_signal_connect(cc
.store
, "row-inserted", G_CALLBACK(cc_dialog_on_row_inserted
), &cc
);
452 g_signal_connect(cc
.store
, "row-deleted", G_CALLBACK(cc_dialog_on_row_deleted
), &cc
);
453 g_signal_connect(cc
.store
, "rows-reordered", G_CALLBACK(cc_dialog_on_rows_reordered
), &cc
);
455 gtk_widget_show_all(vbox
);
457 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
459 GSList
*cmd_list
= NULL
;
460 GSList
*lbl_list
= NULL
;
462 gchar
**commands
= NULL
;
463 gchar
**labels
= NULL
;
466 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(cc
.store
), &iter
))
473 gtk_tree_model_get(GTK_TREE_MODEL(cc
.store
), &iter
, CC_COLUMN_CMD
, &cmd
, CC_COLUMN_LABEL
, &lbl
, -1);
476 cmd_list
= g_slist_prepend(cmd_list
, cmd
);
477 lbl_list
= g_slist_prepend(lbl_list
, lbl
);
486 while (gtk_tree_model_iter_next(GTK_TREE_MODEL(cc
.store
), &iter
));
488 cmd_list
= g_slist_reverse(cmd_list
);
489 lbl_list
= g_slist_reverse(lbl_list
);
490 /* create a new null-terminated array but only if there is any commands defined */
494 GSList
*cmd_node
, *lbl_node
;
496 commands
= g_new(gchar
*, len
+ 1);
497 labels
= g_new(gchar
*, len
+ 1);
498 /* walk commands and labels lists */
499 for (cmd_node
= cmd_list
, lbl_node
= lbl_list
; cmd_node
!= NULL
; cmd_node
= cmd_node
->next
, lbl_node
= lbl_node
->next
)
501 commands
[j
] = (gchar
*) cmd_node
->data
;
502 labels
[j
] = (gchar
*) lbl_node
->data
;
505 /* null-terminate the arrays */
509 /* set the new arrays */
510 g_strfreev(ui_prefs
.custom_commands
);
511 ui_prefs
.custom_commands
= commands
;
512 g_strfreev(ui_prefs
.custom_commands_labels
);
513 ui_prefs
.custom_commands_labels
= labels
;
514 /* rebuild the menu items */
515 tools_create_insert_custom_command_menu_items();
517 g_slist_free(cmd_list
);
518 g_slist_free(lbl_list
);
520 gtk_widget_destroy(dialog
);
524 static void cc_on_custom_command_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
526 GeanyDocument
*doc
= document_get_current();
529 g_return_if_fail(DOC_VALID(doc
));
531 command_idx
= GPOINTER_TO_INT(user_data
);
533 if (ui_prefs
.custom_commands
== NULL
||
534 command_idx
< 0 || command_idx
> (gint
) g_strv_length(ui_prefs
.custom_commands
))
536 cc_show_dialog_custom_commands();
540 /* send it through the command and when the command returned the output the current selection
541 * will be replaced */
542 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[command_idx
]);
546 static void cc_insert_custom_command_items(GtkMenu
*me
, const gchar
*label
, const gchar
*tooltip
, gint idx
)
550 GeanyKeyBinding
*kb
= NULL
;
554 case 0: key_idx
= GEANY_KEYS_FORMAT_SENDTOCMD1
; break;
555 case 1: key_idx
= GEANY_KEYS_FORMAT_SENDTOCMD2
; break;
556 case 2: key_idx
= GEANY_KEYS_FORMAT_SENDTOCMD3
; break;
559 item
= gtk_menu_item_new_with_label(label
);
560 gtk_widget_set_tooltip_text(item
, tooltip
);
563 kb
= keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT
, key_idx
);
566 gtk_widget_add_accelerator(item
, "activate", gtk_accel_group_new(),
567 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
570 gtk_container_add(GTK_CONTAINER(me
), item
);
571 gtk_widget_show(item
);
572 g_signal_connect(item
, "activate", G_CALLBACK(cc_on_custom_command_activate
),
573 GINT_TO_POINTER(idx
));
577 void tools_create_insert_custom_command_menu_items(void)
579 GtkMenu
*menu_edit
= GTK_MENU(ui_lookup_widget(main_widgets
.window
, "send_selection_to2_menu"));
581 GList
*me_children
, *node
;
583 /* first clean the menus to be able to rebuild them */
584 me_children
= gtk_container_get_children(GTK_CONTAINER(menu_edit
));
585 foreach_list(node
, me_children
)
586 gtk_widget_destroy(GTK_WIDGET(node
->data
));
587 g_list_free(me_children
);
589 if (ui_prefs
.custom_commands
== NULL
|| g_strv_length(ui_prefs
.custom_commands
) == 0)
591 item
= gtk_menu_item_new_with_label(_("No custom commands defined."));
592 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
593 gtk_widget_set_sensitive(item
, FALSE
);
594 gtk_widget_show(item
);
600 len
= g_strv_length(ui_prefs
.custom_commands
);
601 for (i
= 0; i
< len
; i
++)
603 const gchar
*label
= ui_prefs
.custom_commands_labels
[i
];
606 label
= ui_prefs
.custom_commands
[i
];
607 if (!EMPTY(label
)) /* skip empty items */
609 cc_insert_custom_command_items(menu_edit
, label
, ui_prefs
.custom_commands
[i
], idx
);
615 /* separator and Set menu item */
616 item
= gtk_separator_menu_item_new();
617 gtk_container_add(GTK_CONTAINER(menu_edit
), item
);
618 gtk_widget_show(item
);
620 cc_insert_custom_command_items(menu_edit
, _("Set Custom Commands"), NULL
, -1);
624 /* (stolen from bluefish, thanks)
625 * Returns number of characters, lines and words in the supplied gchar*.
626 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
627 * Words are defined as any characters grouped, separated with spaces. */
628 static void word_count(gchar
*text
, guint
*chars
, guint
*lines
, guint
*words
)
634 return; /* politely refuse to operate on NULL */
636 *chars
= *words
= *lines
= 0;
637 while (*text
!= '\0')
658 utext
= g_utf8_get_char_validated(text
, 2); /* This might be an utf-8 char */
659 if (g_unichar_isspace(utext
)) /* Unicode encoded space? */
660 goto mb_word_separator
;
661 if (g_unichar_isgraph(utext
)) /* Is this something printable? */
665 /* Even if the current char is 2 bytes, this will iterate correctly. */
666 text
= g_utf8_next_char(text
);
669 /* Capture last word, if there's no whitespace at the end of the file. */
672 /* We start counting line numbers from 1 */
678 void tools_word_count(void)
680 GtkWidget
*dialog
, *label
, *vbox
, *table
;
682 guint chars
= 0, lines
= 0, words
= 0;
686 doc
= document_get_current();
687 g_return_if_fail(doc
!= NULL
);
689 dialog
= gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets
.window
),
690 GTK_DIALOG_DESTROY_WITH_PARENT
,
691 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
, NULL
);
692 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
693 gtk_widget_set_name(dialog
, "GeanyDialog");
695 if (sci_has_selection(doc
->editor
->sci
))
697 text
= sci_get_selection_contents(doc
->editor
->sci
);
698 range
= _("selection");
702 text
= sci_get_contents(doc
->editor
->sci
, -1);
703 range
= _("whole document");
705 word_count(text
, &chars
, &lines
, &words
);
708 table
= gtk_table_new(4, 2, FALSE
);
709 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
710 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
712 label
= gtk_label_new(_("Range:"));
713 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1,
714 (GtkAttachOptions
) (GTK_FILL
),
715 (GtkAttachOptions
) (0), 0, 0);
716 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
718 label
= gtk_label_new(range
);
719 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 0, 1,
720 (GtkAttachOptions
) (GTK_FILL
),
721 (GtkAttachOptions
) (0), 20, 0);
722 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
724 label
= gtk_label_new(_("Lines:"));
725 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 1, 2,
726 (GtkAttachOptions
) (GTK_FILL
),
727 (GtkAttachOptions
) (0), 0, 0);
728 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
730 text
= g_strdup_printf("%d", lines
);
731 label
= gtk_label_new(text
);
732 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 1, 2,
733 (GtkAttachOptions
) (GTK_FILL
),
734 (GtkAttachOptions
) (0), 20, 0);
735 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
738 label
= gtk_label_new(_("Words:"));
739 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 2, 3,
740 (GtkAttachOptions
) (GTK_FILL
),
741 (GtkAttachOptions
) (0), 0, 0);
742 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
744 text
= g_strdup_printf("%d", words
);
745 label
= gtk_label_new(text
);
746 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 2, 3,
747 (GtkAttachOptions
) (GTK_FILL
),
748 (GtkAttachOptions
) (0), 20, 0);
749 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
752 label
= gtk_label_new(_("Characters:"));
753 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 3, 4,
754 (GtkAttachOptions
) (GTK_FILL
),
755 (GtkAttachOptions
) (0), 0, 0);
756 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
758 text
= g_strdup_printf("%d", chars
);
759 label
= gtk_label_new(text
);
760 gtk_table_attach(GTK_TABLE(table
), label
, 1, 2, 3, 4,
761 (GtkAttachOptions
) (GTK_FILL
),
762 (GtkAttachOptions
) (0), 20, 0);
763 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
766 gtk_container_add(GTK_CONTAINER(vbox
), table
);
768 g_signal_connect(dialog
, "response", G_CALLBACK(gtk_widget_destroy
), dialog
);
769 g_signal_connect(dialog
, "delete-event", G_CALLBACK(gtk_widget_destroy
), dialog
);
771 gtk_widget_show_all(dialog
);
776 * color dialog callbacks
778 static void on_color_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
782 case GTK_RESPONSE_OK
:
783 gtk_widget_hide(ui_widgets
.open_colorsel
);
785 case GTK_RESPONSE_APPLY
:
788 GeanyDocument
*doc
= document_get_current();
792 g_return_if_fail(doc
!= NULL
);
794 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
795 gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel
), &color
);
797 hex
= utils_get_hex_from_color(&color
);
798 editor_insert_color(doc
->editor
, hex
);
804 gtk_widget_hide(ui_widgets
.open_colorsel
);
809 /* This shows the color selection dialog to choose a color. */
810 void tools_color_chooser(const gchar
*color
)
816 if (interface_prefs
.use_native_windows_dialogs
)
818 win32_show_color_dialog(color
);
823 if (ui_widgets
.open_colorsel
== NULL
)
825 ui_widgets
.open_colorsel
= gtk_color_selection_dialog_new(_("Color Chooser"));
826 gtk_dialog_add_button(GTK_DIALOG(ui_widgets
.open_colorsel
), GTK_STOCK_APPLY
, GTK_RESPONSE_APPLY
);
827 ui_dialog_set_primary_button_order(GTK_DIALOG(ui_widgets
.open_colorsel
),
828 GTK_RESPONSE_APPLY
, GTK_RESPONSE_CANCEL
, GTK_RESPONSE_OK
, -1);
829 gtk_widget_set_name(ui_widgets
.open_colorsel
, "GeanyDialog");
830 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets
.open_colorsel
), GTK_WINDOW(main_widgets
.window
));
831 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
832 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel
), TRUE
);
834 g_signal_connect(ui_widgets
.open_colorsel
, "response",
835 G_CALLBACK(on_color_dialog_response
), NULL
);
836 g_signal_connect(ui_widgets
.open_colorsel
, "delete-event",
837 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
840 colorsel
= gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets
.open_colorsel
));
841 /* if color is non-NULL set it in the dialog as preselected color */
842 if (color
!= NULL
&& utils_parse_color(color
, &gc
))
844 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel
), &gc
);
845 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel
), &gc
);
848 /* We make sure the dialog is visible. */
849 gtk_window_present(GTK_WINDOW(ui_widgets
.open_colorsel
));