Version bump.
[geany-mirror.git] / src / tools.c
blobca9a6614149ca5e6eabb7993abedd4399478d5fc
1 /*
2 * tools.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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 return;
245 argv = g_strsplit(command, " ", -1);
246 ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
248 cc_error_occurred = FALSE;
250 if (g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
251 NULL, NULL, &pid, &stdin_fd, &stdout_fd, &stderr_fd, &error))
253 gchar *sel;
254 gint len, remaining, wrote;
256 if (pid > 0)
257 g_child_watch_add(pid, (GChildWatchFunc) cc_exit_cb, doc);
259 /* use GIOChannel to monitor stdout */
260 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
261 FALSE, cc_iofunc, NULL);
262 /* copy program's stderr to Geany's stdout to help error tracking */
263 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
264 FALSE, cc_iofunc_err, (gpointer)command);
266 /* get selection */
267 len = sci_get_selected_text_length(doc->editor->sci);
268 sel = g_malloc0(len + 1);
269 sci_get_selected_text(doc->editor->sci, sel);
271 /* write data to the command */
272 remaining = len - 1;
275 wrote = write(stdin_fd, sel, remaining);
276 if (G_UNLIKELY(wrote < 0))
278 g_warning("%s: %s: %s\n", G_STRFUNC, "Failed sending data to command",
279 g_strerror(errno));
280 break;
282 remaining -= wrote;
283 } while (remaining > 0);
284 close(stdin_fd);
285 g_free(sel);
287 else
289 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
290 ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
291 g_error_free(error);
294 g_strfreev(argv);
298 static void cc_show_dialog_custom_commands(void)
300 GtkWidget *dialog, *label, *vbox, *button;
301 guint i;
302 struct cc_dialog cc;
304 dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
305 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
306 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
307 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
308 gtk_box_set_spacing(GTK_BOX(vbox), 6);
309 gtk_widget_set_name(dialog, "GeanyDialog");
311 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."));
312 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
313 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
314 gtk_container_add(GTK_CONTAINER(vbox), label);
316 cc.count = 1;
317 cc.box = gtk_vbox_new(FALSE, 0);
318 gtk_container_add(GTK_CONTAINER(vbox), cc.box);
320 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
322 cc_add_command(&cc, -1);
324 else
326 guint len = g_strv_length(ui_prefs.custom_commands);
327 for (i = 0; i < len; i++)
329 if (ui_prefs.custom_commands[i][0] == '\0')
330 continue; /* skip empty fields */
332 cc_add_command(&cc, i);
336 button = gtk_button_new_from_stock("gtk-add");
337 g_signal_connect(button, "clicked", G_CALLBACK(cc_on_custom_commands_dlg_add_clicked), &cc);
338 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
340 gtk_widget_show_all(vbox);
342 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
344 /* get all hboxes which contain a label and an entry element */
345 GList *children = gtk_container_get_children(GTK_CONTAINER(cc.box));
346 GList *node, *list;
347 GSList *result_list = NULL;
348 gint j = 0;
349 gint len = 0;
350 gchar **result = NULL;
351 const gchar *text;
353 foreach_list(node, children)
355 /* get the contents of each hbox */
356 list = gtk_container_get_children(GTK_CONTAINER(node->data));
358 /* first element of the list is the label, so skip it and get the entry element */
359 text = gtk_entry_get_text(GTK_ENTRY(list->next->data));
361 /* if the content of the entry is non-empty, add it to the result array */
362 if (text[0] != '\0')
364 result_list = g_slist_append(result_list, g_strdup(text));
365 len++;
367 g_list_free(list);
369 /* create a new null-terminated array but only if there any commands defined */
370 if (len > 0)
372 result = g_new(gchar*, len + 1);
373 while (result_list != NULL)
375 result[j] = (gchar*) result_list->data;
377 result_list = result_list->next;
378 j++;
380 result[len] = NULL; /* null-terminate the array */
382 /* set the new array */
383 g_strfreev(ui_prefs.custom_commands);
384 ui_prefs.custom_commands = result;
385 /* rebuild the menu items */
386 tools_create_insert_custom_command_menu_items();
388 g_slist_free(result_list);
389 g_list_free(children);
391 gtk_widget_destroy(dialog);
395 /* enable or disable all custom command menu items when the sub menu is opened */
396 static void cc_on_custom_command_menu_activate(GtkMenuItem *menuitem, gpointer user_data)
398 GeanyDocument *doc = document_get_current();
399 gint i, len;
400 gboolean enable;
401 GList *children, *node;
403 g_return_if_fail(doc != NULL);
405 enable = sci_has_selection(doc->editor->sci) && (ui_prefs.custom_commands != NULL);
407 children = gtk_container_get_children(GTK_CONTAINER(user_data));
408 len = g_list_length(children);
409 i = 0;
410 foreach_list(node, children)
412 if (i == (len - 2))
413 break; /* stop before the last two elements (the seperator and the set entry) */
415 gtk_widget_set_sensitive(GTK_WIDGET(node->data), enable);
416 i++;
418 g_list_free(children);
422 static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
424 GeanyDocument *doc = document_get_current();
425 gint command_idx;
427 g_return_if_fail(doc != NULL);
429 command_idx = GPOINTER_TO_INT(user_data);
431 if (ui_prefs.custom_commands == NULL ||
432 command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
434 cc_show_dialog_custom_commands();
435 return;
438 /* send it through the command and when the command returned the output the current selection
439 * will be replaced */
440 tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
444 static void cc_insert_custom_command_items(GtkMenu *me, gchar *label, gint idx)
446 GtkWidget *item;
447 gint key_idx = -1;
448 GeanyKeyBinding *kb = NULL;
450 switch (idx)
452 case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
453 case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
454 case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
457 if (key_idx != -1)
458 kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
460 item = gtk_menu_item_new_with_label(label);
461 if (key_idx != -1)
462 gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
463 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
464 gtk_container_add(GTK_CONTAINER(me), item);
465 gtk_widget_show(item);
466 g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
467 GINT_TO_POINTER(idx));
471 void tools_create_insert_custom_command_menu_items(void)
473 GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
474 GtkWidget *item;
475 GList *me_children, *node;
476 static gboolean signal_set = FALSE;
478 /* first clean the menus to be able to rebuild them */
479 me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
480 foreach_list(node, me_children)
481 gtk_widget_destroy(GTK_WIDGET(node->data));
482 g_list_free(me_children);
484 if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
486 item = gtk_menu_item_new_with_label(_("No custom commands defined."));
487 gtk_container_add(GTK_CONTAINER(menu_edit), item);
488 gtk_widget_set_sensitive(item, FALSE);
489 gtk_widget_show(item);
491 else
493 guint i, len;
494 gint idx = 0;
495 len = g_strv_length(ui_prefs.custom_commands);
496 for (i = 0; i < len; i++)
498 if (ui_prefs.custom_commands[i][0] != '\0') /* skip empty fields */
500 cc_insert_custom_command_items(menu_edit, ui_prefs.custom_commands[i], idx);
501 idx++;
506 /* separator and Set menu item */
507 item = gtk_separator_menu_item_new();
508 gtk_container_add(GTK_CONTAINER(menu_edit), item);
509 gtk_widget_show(item);
511 cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), -1);
513 if (! signal_set)
515 g_signal_connect(ui_lookup_widget(main_widgets.window, "send_selection_to2"),
516 "activate", G_CALLBACK(cc_on_custom_command_menu_activate), menu_edit);
517 signal_set = TRUE;
522 /* (stolen from bluefish, thanks)
523 * Returns number of characters, lines and words in the supplied gchar*.
524 * Handles UTF-8 correctly. Input must be properly encoded UTF-8.
525 * Words are defined as any characters grouped, separated with spaces. */
526 static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
528 guint in_word = 0;
529 gunichar utext;
531 if (! text)
532 return; /* politely refuse to operate on NULL */
534 *chars = *words = *lines = 0;
535 while (*text != '\0')
537 (*chars)++;
539 switch (*text)
541 case '\n':
542 (*lines)++;
543 case '\r':
544 case '\f':
545 case '\t':
546 case ' ':
547 case '\v':
548 mb_word_separator:
549 if (in_word)
551 in_word = 0;
552 (*words)++;
554 break;
555 default:
556 utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
557 if (g_unichar_isspace(utext)) /* Unicode encoded space? */
558 goto mb_word_separator;
559 if (g_unichar_isgraph(utext)) /* Is this something printable? */
560 in_word = 1;
561 break;
563 /* Even if the current char is 2 bytes, this will iterate correctly. */
564 text = g_utf8_next_char(text);
567 /* Capture last word, if there's no whitespace at the end of the file. */
568 if (in_word)
569 (*words)++;
570 /* We start counting line numbers from 1 */
571 if (*chars > 0)
572 (*lines)++;
576 void tools_word_count(void)
578 GtkWidget *dialog, *label, *vbox, *table;
579 GeanyDocument *doc;
580 guint chars = 0, lines = 0, words = 0;
581 gchar *text, *range;
583 doc = document_get_current();
584 g_return_if_fail(doc != NULL);
586 dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
587 GTK_DIALOG_DESTROY_WITH_PARENT,
588 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
589 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
590 gtk_widget_set_name(dialog, "GeanyDialog");
592 if (sci_has_selection(doc->editor->sci))
594 text = g_malloc0(sci_get_selected_text_length(doc->editor->sci) + 1);
595 sci_get_selected_text(doc->editor->sci, text);
596 range = _("selection");
598 else
600 text = g_malloc(sci_get_length(doc->editor->sci) + 1);
601 sci_get_text(doc->editor->sci, sci_get_length(doc->editor->sci) + 1 , text);
602 range = _("whole document");
604 word_count(text, &chars, &lines, &words);
605 g_free(text);
607 table = gtk_table_new(4, 2, FALSE);
608 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
609 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
611 label = gtk_label_new(_("Range:"));
612 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
613 (GtkAttachOptions) (GTK_FILL),
614 (GtkAttachOptions) (0), 0, 0);
615 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
617 label = gtk_label_new(range);
618 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
619 (GtkAttachOptions) (GTK_FILL),
620 (GtkAttachOptions) (0), 20, 0);
621 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
623 label = gtk_label_new(_("Lines:"));
624 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
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", lines);
630 label = gtk_label_new(text);
631 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
632 (GtkAttachOptions) (GTK_FILL),
633 (GtkAttachOptions) (0), 20, 0);
634 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
635 g_free(text);
637 label = gtk_label_new(_("Words:"));
638 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
639 (GtkAttachOptions) (GTK_FILL),
640 (GtkAttachOptions) (0), 0, 0);
641 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
643 text = g_strdup_printf("%d", words);
644 label = gtk_label_new(text);
645 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
646 (GtkAttachOptions) (GTK_FILL),
647 (GtkAttachOptions) (0), 20, 0);
648 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
649 g_free(text);
651 label = gtk_label_new(_("Characters:"));
652 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
653 (GtkAttachOptions) (GTK_FILL),
654 (GtkAttachOptions) (0), 0, 0);
655 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
657 text = g_strdup_printf("%d", chars);
658 label = gtk_label_new(text);
659 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
660 (GtkAttachOptions) (GTK_FILL),
661 (GtkAttachOptions) (0), 20, 0);
662 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
663 g_free(text);
665 gtk_container_add(GTK_CONTAINER(vbox), table);
667 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
668 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
670 gtk_widget_show_all(dialog);
675 * color dialog callbacks
677 #ifndef G_OS_WIN32
678 static void
679 on_color_cancel_button_clicked (GtkButton *button,
680 gpointer user_data)
682 gtk_widget_hide(ui_widgets.open_colorsel);
686 static void
687 on_color_ok_button_clicked (GtkButton *button,
688 gpointer user_data)
690 GdkColor color;
691 GeanyDocument *doc = document_get_current();
692 gchar *hex;
694 gtk_widget_hide(ui_widgets.open_colorsel);
695 g_return_if_fail(doc != NULL);
697 gtk_color_selection_get_current_color(
698 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &color);
700 hex = utils_get_hex_from_color(&color);
701 editor_insert_color(doc->editor, hex);
702 g_free(hex);
704 #endif
707 /* This shows the color selection dialog to choose a color. */
708 void tools_color_chooser(const gchar *color)
710 #ifdef G_OS_WIN32
711 win32_show_color_dialog(color);
712 #else
713 gchar *c = (gchar*) color;
715 if (ui_widgets.open_colorsel == NULL)
717 ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
718 gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
719 gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
720 gtk_color_selection_set_has_palette(
721 GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), TRUE);
723 g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->cancel_button, "clicked",
724 G_CALLBACK(on_color_cancel_button_clicked), NULL);
725 g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->ok_button, "clicked",
726 G_CALLBACK(on_color_ok_button_clicked), NULL);
727 g_signal_connect(ui_widgets.open_colorsel, "delete-event",
728 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
730 /* if color is non-NULL set it in the dialog as preselected color */
731 if (c != NULL && (c[0] == '0' || c[0] == '#'))
733 GdkColor gc;
735 if (c[0] == '0' && c[1] == 'x')
736 { /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
737 c[1] = '#';
738 c++;
740 gdk_color_parse(c, &gc);
741 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(
742 GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
743 gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(
744 GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
747 /* We make sure the dialog is visible. */
748 gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));
749 #endif