Make `Send selection to` send the current line if there is no selection
[geany-mirror.git] / src / tools.c
blobe5fe119354495b42d0365047e2f42e25d220ee16
1 /*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
25 * Miscellaneous code for the built-in Tools menu items, and custom command code.
26 * For Plugins code see plugins.c.
29 #include "geany.h"
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <errno.h>
36 #ifdef G_OS_UNIX
37 # include <sys/types.h>
38 # include <sys/wait.h>
39 # include <signal.h>
40 #endif
42 #include "tools.h"
43 #include "support.h"
44 #include "document.h"
45 #include "editor.h"
46 #include "sciwrappers.h"
47 #include "utils.h"
48 #include "ui_utils.h"
49 #include "msgwindow.h"
50 #include "keybindings.h"
51 #include "templates.h"
52 #include "win32.h"
53 #include "dialogs.h"
56 /* custom commands code*/
57 struct cc_dialog
59 gint count;
60 GtkWidget *box;
63 static gboolean cc_error_occurred = FALSE;
64 static gboolean cc_reading_finished = FALSE;
65 static GString *cc_buffer;
67 static void cc_add_command(struct cc_dialog *cc, gint idx)
69 GtkWidget *label, *entry, *hbox;
70 gchar str[6];
72 hbox = gtk_hbox_new(FALSE, 5);
73 g_snprintf(str, 5, "%d:", cc->count);
74 label = gtk_label_new(str);
76 entry = gtk_entry_new();
77 if (idx >= 0)
78 gtk_entry_set_text(GTK_ENTRY(entry), ui_prefs.custom_commands[idx]);
79 ui_entry_add_clear_icon(GTK_ENTRY(entry));
80 gtk_entry_set_max_length(GTK_ENTRY(entry), 255);
81 gtk_entry_set_width_chars(GTK_ENTRY(entry), 30);
82 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
83 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
84 gtk_widget_show_all(hbox);
85 gtk_container_add(GTK_CONTAINER(cc->box), hbox);
86 cc->count++;
90 static void cc_on_custom_commands_dlg_add_clicked(GtkToolButton *toolbutton, struct cc_dialog *cc)
92 cc_add_command(cc, -1);
96 static gboolean cc_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
98 if (cond & (G_IO_IN | G_IO_PRI))
100 gchar *msg = NULL;
101 GIOStatus rv;
102 GError *err = NULL;
104 cc_buffer = g_string_sized_new(256);
108 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, &err);
109 if (msg != NULL)
111 g_string_append(cc_buffer, msg);
112 g_free(msg);
114 if (G_UNLIKELY(err != NULL))
116 geany_debug("%s: %s", G_STRFUNC, err->message);
117 g_error_free(err);
118 err = NULL;
120 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
122 if (G_UNLIKELY(rv != G_IO_STATUS_EOF))
123 { /* Something went wrong? */
124 g_warning("%s: %s\n", G_STRFUNC, "Incomplete command output");
127 return FALSE;
131 static gboolean cc_iofunc_err(GIOChannel *ioc, GIOCondition cond, gpointer data)
133 if (cond & (G_IO_IN | G_IO_PRI))
135 gchar *msg = NULL;
136 GString *str = g_string_sized_new(256);
137 GIOStatus rv;
141 rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL);
142 if (msg != NULL)
144 g_string_append(str, msg);
145 g_free(msg);
147 } while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
149 if (NZV(str->str))
151 g_warning("%s: %s\n", (const gchar *) data, str->str);
152 ui_set_statusbar(TRUE,
153 _("The executed custom command returned an error. "
154 "Your selection was not changed. Error message: %s"),
155 str->str);
156 cc_error_occurred = TRUE;
159 g_string_free(str, TRUE);
161 cc_reading_finished = TRUE;
162 return FALSE;
166 static gboolean cc_replace_sel_cb(gpointer user_data)
168 GeanyDocument *doc = user_data;
170 if (! cc_reading_finished)
171 { /* keep this function in the main loop until cc_iofunc_err() has finished */
172 return TRUE;
175 if (! cc_error_occurred && cc_buffer != NULL)
176 { /* Command completed successfully */
177 sci_replace_sel(doc->editor->sci, cc_buffer->str);
178 g_string_free(cc_buffer, TRUE);
179 cc_buffer = NULL;
182 cc_error_occurred = FALSE;
183 cc_reading_finished = FALSE;
185 return FALSE;
189 /* check whether the executed command failed and if so do nothing.
190 * If it returned with a sucessful exit code, replace the selection. */
191 static void cc_exit_cb(GPid child_pid, gint status, gpointer user_data)
193 /* if there was already an error, skip further checks */
194 if (! cc_error_occurred)
196 #ifdef G_OS_UNIX
197 if (WIFEXITED(status))
199 if (WEXITSTATUS(status) != EXIT_SUCCESS)
200 cc_error_occurred = TRUE;
202 else if (WIFSIGNALED(status))
203 { /* the terminating signal: WTERMSIG (status)); */
204 cc_error_occurred = TRUE;
206 else
207 { /* any other failure occured */
208 cc_error_occurred = TRUE;
210 #else
211 cc_error_occurred = ! win32_get_exit_status(child_pid);
212 #endif
214 if (cc_error_occurred)
215 { /* here we are sure cc_error_occurred was set due to an unsuccessful exit code
216 * and so we add an error message */
217 /* TODO maybe include the exit code in the error message */
218 ui_set_statusbar(TRUE,
219 _("The executed custom command exited with an unsuccessful exit code."));
223 g_idle_add(cc_replace_sel_cb, user_data);
224 g_spawn_close_pid(child_pid);
228 /* Executes command (which should include all necessary command line args) and passes the current
229 * selection through the standard input of command. The whole output of command replaces the
230 * current selection. */
231 void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
233 GError *error = NULL;
234 GPid pid;
235 gchar **argv;
236 gint stdin_fd;
237 gint stdout_fd;
238 gint stderr_fd;
240 g_return_if_fail(doc != NULL && command != NULL);
242 if (! sci_has_selection(doc->editor->sci))
243 editor_select_lines(doc->editor, FALSE);
245 if (!g_shell_parse_argv(command, NULL, &argv, &error))
247 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
248 g_error_free(error);
249 return;
251 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
253 cc_error_occurred = FALSE;
255 if (g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
256 NULL, NULL, &pid, &stdin_fd, &stdout_fd, &stderr_fd, &error))
258 gchar *sel;
259 gint len, remaining, wrote;
261 if (pid > 0)
262 g_child_watch_add(pid, (GChildWatchFunc) cc_exit_cb, doc);
264 /* use GIOChannel to monitor stdout */
265 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
266 FALSE, cc_iofunc, NULL);
267 /* copy program's stderr to Geany's stdout to help error tracking */
268 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
269 FALSE, cc_iofunc_err, (gpointer)command);
271 /* get selection */
272 len = sci_get_selected_text_length(doc->editor->sci);
273 sel = g_malloc0(len + 1);
274 sci_get_selected_text(doc->editor->sci, sel);
276 /* write data to the command */
277 remaining = len - 1;
280 wrote = write(stdin_fd, sel, remaining);
281 if (G_UNLIKELY(wrote < 0))
283 g_warning("%s: %s: %s\n", G_STRFUNC, "Failed sending data to command",
284 g_strerror(errno));
285 break;
287 remaining -= wrote;
288 } while (remaining > 0);
289 close(stdin_fd);
290 g_free(sel);
292 else
294 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
295 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
296 g_error_free(error);
299 g_strfreev(argv);
303 static void cc_show_dialog_custom_commands(void)
305 GtkWidget *dialog, *label, *vbox, *button;
306 guint i;
307 struct cc_dialog cc;
309 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
310 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
311 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
312 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
313 gtk_box_set_spacing(GTK_BOX(vbox), 6);
314 gtk_widget_set_name(dialog, "GeanyDialog");
316 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."));
317 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
318 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
319 gtk_container_add(GTK_CONTAINER(vbox), label);
321 cc.count = 1;
322 cc.box = gtk_vbox_new(FALSE, 0);
323 gtk_container_add(GTK_CONTAINER(vbox), cc.box);
325 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
327 cc_add_command(&cc, -1);
329 else
331 guint len = g_strv_length(ui_prefs.custom_commands);
332 for (i = 0; i < len; i++)
334 if (ui_prefs.custom_commands[i][0] == '\0')
335 continue; /* skip empty fields */
337 cc_add_command(&cc, i);
341 button = gtk_button_new_from_stock("gtk-add");
342 g_signal_connect(button, "clicked", G_CALLBACK(cc_on_custom_commands_dlg_add_clicked), &cc);
343 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
345 gtk_widget_show_all(vbox);
347 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
349 /* get all hboxes which contain a label and an entry element */
350 GList *children = gtk_container_get_children(GTK_CONTAINER(cc.box));
351 GList *node, *list;
352 GSList *result_list = NULL;
353 gint j = 0;
354 gint len = 0;
355 gchar **result = NULL;
356 const gchar *text;
358 foreach_list(node, children)
360 /* get the contents of each hbox */
361 list = gtk_container_get_children(GTK_CONTAINER(node->data));
363 /* first element of the list is the label, so skip it and get the entry element */
364 text = gtk_entry_get_text(GTK_ENTRY(list->next->data));
366 /* if the content of the entry is non-empty, add it to the result array */
367 if (text[0] != '\0')
369 result_list = g_slist_prepend(result_list, g_strdup(text));
370 len++;
372 g_list_free(list);
374 result_list = g_slist_reverse(result_list);
375 /* create a new null-terminated array but only if there any commands defined */
376 if (len > 0)
378 result = g_new(gchar*, len + 1);
379 while (result_list != NULL)
381 result[j] = (gchar*) result_list->data;
383 result_list = result_list->next;
384 j++;
386 result[len] = NULL; /* null-terminate the array */
388 /* set the new array */
389 g_strfreev(ui_prefs.custom_commands);
390 ui_prefs.custom_commands = result;
391 /* rebuild the menu items */
392 tools_create_insert_custom_command_menu_items();
394 g_slist_free(result_list);
395 g_list_free(children);
397 gtk_widget_destroy(dialog);
401 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
403 GeanyDocument *doc = document_get_current();
404 gint command_idx;
406 g_return_if_fail(doc != NULL);
408 command_idx = GPOINTER_TO_INT(user_data);
410 if (ui_prefs.custom_commands == NULL ||
411 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
413 cc_show_dialog_custom_commands();
414 return;
417 /* send it through the command and when the command returned the output the current selection
418 * will be replaced */
419 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
423 static void cc_insert_custom_command_items(GtkMenu *me, gchar *label, gint idx)
425 GtkWidget *item;
426 gint key_idx = -1;
427 GeanyKeyBinding *kb = NULL;
429 switch (idx)
431 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
432 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
433 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
436 if (key_idx != -1)
437 kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
439 item = gtk_menu_item_new_with_label(label);
440 if (key_idx != -1)
441 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
442 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
443 gtk_container_add(GTK_CONTAINER(me), item);
444 gtk_widget_show(item);
445 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
446 GINT_TO_POINTER(idx));
450 void tools_create_insert_custom_command_menu_items(void)
452 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
453 GtkWidget *item;
454 GList *me_children, *node;
456 /* first clean the menus to be able to rebuild them */
457 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
458 foreach_list(node, me_children)
459 gtk_widget_destroy(GTK_WIDGET(node->data));
460 g_list_free(me_children);
462 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
464 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
465 gtk_container_add(GTK_CONTAINER(menu_edit), item);
466 gtk_widget_set_sensitive(item, FALSE);
467 gtk_widget_show(item);
469 else
471 guint i, len;
472 gint idx = 0;
473 len = g_strv_length(ui_prefs.custom_commands);
474 for (i = 0; i < len; i++)
476 if (ui_prefs.custom_commands[i][0] != '\0') /* skip empty fields */
478 cc_insert_custom_command_items(menu_edit, ui_prefs.custom_commands[i], idx);
479 idx++;
484 /* separator and Set menu item */
485 item = gtk_separator_menu_item_new();
486 gtk_container_add(GTK_CONTAINER(menu_edit), item);
487 gtk_widget_show(item);
489 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), -1);
493 /* (stolen from bluefish, thanks)
494 * Returns number of characters, lines and words in the supplied gchar*.
495 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
496 * Words are defined as any characters grouped, separated with spaces. */
497 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
499 guint in_word = 0;
500 gunichar utext;
502 if (! text)
503 return; /* politely refuse to operate on NULL */
505 *chars = *words = *lines = 0;
506 while (*text != '\0')
508 (*chars)++;
510 switch (*text)
512 case '\n':
513 (*lines)++;
514 case '\r':
515 case '\f':
516 case '\t':
517 case ' ':
518 case '\v':
519 mb_word_separator:
520 if (in_word)
522 in_word = 0;
523 (*words)++;
525 break;
526 default:
527 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
528 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
529 goto mb_word_separator;
530 if (g_unichar_isgraph(utext)) /* Is this something printable? */
531 in_word = 1;
532 break;
534 /* Even if the current char is 2 bytes, this will iterate correctly. */
535 text = g_utf8_next_char(text);
538 /* Capture last word, if there's no whitespace at the end of the file. */
539 if (in_word)
540 (*words)++;
541 /* We start counting line numbers from 1 */
542 if (*chars > 0)
543 (*lines)++;
547 void tools_word_count(void)
549 GtkWidget *dialog, *label, *vbox, *table;
550 GeanyDocument *doc;
551 guint chars = 0, lines = 0, words = 0;
552 gchar *text;
553 const gchar *range;
555 doc = document_get_current();
556 g_return_if_fail(doc != NULL);
558 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
559 GTK_DIALOG_DESTROY_WITH_PARENT,
560 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
561 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
562 gtk_widget_set_name(dialog, "GeanyDialog");
564 if (sci_has_selection(doc->editor->sci))
566 text = g_malloc0(sci_get_selected_text_length(doc->editor->sci) + 1);
567 sci_get_selected_text(doc->editor->sci, text);
568 range = _("selection");
570 else
572 text = g_malloc(sci_get_length(doc->editor->sci) + 1);
573 sci_get_text(doc->editor->sci, sci_get_length(doc->editor->sci) + 1 , text);
574 range = _("whole document");
576 word_count(text, &chars, &lines, &words);
577 g_free(text);
579 table = gtk_table_new(4, 2, FALSE);
580 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
581 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
583 label = gtk_label_new(_("Range:"));
584 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
585 (GtkAttachOptions) (GTK_FILL),
586 (GtkAttachOptions) (0), 0, 0);
587 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
589 label = gtk_label_new(range);
590 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
591 (GtkAttachOptions) (GTK_FILL),
592 (GtkAttachOptions) (0), 20, 0);
593 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
595 label = gtk_label_new(_("Lines:"));
596 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
597 (GtkAttachOptions) (GTK_FILL),
598 (GtkAttachOptions) (0), 0, 0);
599 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
601 text = g_strdup_printf("%d", lines);
602 label = gtk_label_new(text);
603 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
604 (GtkAttachOptions) (GTK_FILL),
605 (GtkAttachOptions) (0), 20, 0);
606 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
607 g_free(text);
609 label = gtk_label_new(_("Words:"));
610 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
611 (GtkAttachOptions) (GTK_FILL),
612 (GtkAttachOptions) (0), 0, 0);
613 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
615 text = g_strdup_printf("%d", words);
616 label = gtk_label_new(text);
617 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
618 (GtkAttachOptions) (GTK_FILL),
619 (GtkAttachOptions) (0), 20, 0);
620 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
621 g_free(text);
623 label = gtk_label_new(_("Characters:"));
624 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
625 (GtkAttachOptions) (GTK_FILL),
626 (GtkAttachOptions) (0), 0, 0);
627 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
629 text = g_strdup_printf("%d", chars);
630 label = gtk_label_new(text);
631 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
632 (GtkAttachOptions) (GTK_FILL),
633 (GtkAttachOptions) (0), 20, 0);
634 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
635 g_free(text);
637 gtk_container_add(GTK_CONTAINER(vbox), table);
639 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
640 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
642 gtk_widget_show_all(dialog);
647 * color dialog callbacks
649 #ifndef G_OS_WIN32
650 static void
651 on_color_cancel_button_clicked (GtkButton *button,
652 gpointer user_data)
654 gtk_widget_hide(ui_widgets.open_colorsel);
658 static void
659 on_color_ok_button_clicked (GtkButton *button,
660 gpointer user_data)
662 GdkColor color;
663 GeanyDocument *doc = document_get_current();
664 gchar *hex;
666 gtk_widget_hide(ui_widgets.open_colorsel);
667 g_return_if_fail(doc != NULL);
669 gtk_color_selection_get_current_color(
670 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &color);
672 hex = utils_get_hex_from_color(&color);
673 editor_insert_color(doc->editor, hex);
674 g_free(hex);
676 #endif
679 /* This shows the color selection dialog to choose a color. */
680 void tools_color_chooser(const gchar *color)
682 #ifdef G_OS_WIN32
683 win32_show_color_dialog(color);
684 #else
685 gchar *c = (gchar*) color;
687 if (ui_widgets.open_colorsel == NULL)
689 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
690 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
691 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
692 gtk_color_selection_set_has_palette(
693 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), TRUE);
695 g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->cancel_button, "clicked",
696 G_CALLBACK(on_color_cancel_button_clicked), NULL);
697 g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->ok_button, "clicked",
698 G_CALLBACK(on_color_ok_button_clicked), NULL);
699 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
700 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
702 /* if color is non-NULL set it in the dialog as preselected color */
703 if (c != NULL && (c[0] == '0' || c[0] == '#'))
705 GdkColor gc;
707 if (c[0] == '0' && c[1] == 'x')
708 { /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
709 c[1] = '#';
710 c++;
712 gdk_color_parse(c, &gc);
713 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(
714 GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
715 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(
716 GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
719 /* We make sure the dialog is visible. */
720 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));
721 #endif