2006-12-13 00:46:14 +00:00
|
|
|
/*
|
|
|
|
* tools.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2009-01-04 18:30:42 +00:00
|
|
|
* Copyright 2006-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2006-2009 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2006-12-13 00:46:14 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2007-02-24 11:41:56 +00:00
|
|
|
/*
|
2007-07-27 11:28:17 +00:00
|
|
|
* Miscellaneous code for the built-in Tools menu items, and custom command code.
|
|
|
|
* For Plugins code see plugins.c.
|
2007-02-24 11:41:56 +00:00
|
|
|
*/
|
|
|
|
|
2006-12-13 00:46:14 +00:00
|
|
|
#include "geany.h"
|
|
|
|
|
2007-02-25 14:26:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2007-03-01 11:42:11 +00:00
|
|
|
#include <errno.h>
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "tools.h"
|
2006-12-13 00:46:14 +00:00
|
|
|
#include "support.h"
|
|
|
|
#include "document.h"
|
2008-05-15 13:43:29 +00:00
|
|
|
#include "editor.h"
|
2006-12-13 00:46:14 +00:00
|
|
|
#include "sciwrappers.h"
|
2007-02-25 14:26:55 +00:00
|
|
|
#include "utils.h"
|
2006-12-13 00:46:14 +00:00
|
|
|
#include "ui_utils.h"
|
2007-02-25 14:26:55 +00:00
|
|
|
#include "msgwindow.h"
|
|
|
|
#include "keybindings.h"
|
2007-06-14 13:10:51 +00:00
|
|
|
#include "templates.h"
|
2007-07-28 15:18:01 +00:00
|
|
|
#include "win32.h"
|
2008-03-14 18:55:30 +00:00
|
|
|
#include "dialogs.h"
|
2006-12-13 00:46:14 +00:00
|
|
|
|
2006-12-18 22:55:27 +00:00
|
|
|
|
2007-02-25 14:26:55 +00:00
|
|
|
/* custom commands code*/
|
|
|
|
struct cc_dialog
|
|
|
|
{
|
|
|
|
gint count;
|
|
|
|
GtkWidget *box;
|
|
|
|
};
|
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
static gboolean cc_error_occurred = FALSE;
|
|
|
|
static gboolean cc_reading_finished = FALSE;
|
|
|
|
static GString *cc_buffer;
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2007-03-09 13:52:26 +00:00
|
|
|
static void cc_add_command(struct cc_dialog *cc, gint idx)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
GtkWidget *label, *entry, *hbox;
|
|
|
|
gchar str[6];
|
|
|
|
|
|
|
|
hbox = gtk_hbox_new(FALSE, 5);
|
|
|
|
g_snprintf(str, 5, "%d:", cc->count);
|
|
|
|
label = gtk_label_new(str);
|
|
|
|
|
|
|
|
entry = gtk_entry_new();
|
2007-03-09 16:40:22 +00:00
|
|
|
if (idx >= 0)
|
2007-08-23 11:34:06 +00:00
|
|
|
gtk_entry_set_text(GTK_ENTRY(entry), ui_prefs.custom_commands[idx]);
|
2009-09-21 16:46:16 +00:00
|
|
|
ui_entry_add_clear_icon(GTK_ENTRY(entry));
|
2007-02-25 14:26:55 +00:00
|
|
|
gtk_entry_set_max_length(GTK_ENTRY(entry), 255);
|
|
|
|
gtk_entry_set_width_chars(GTK_ENTRY(entry), 30);
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
|
|
|
|
gtk_widget_show_all(hbox);
|
|
|
|
gtk_container_add(GTK_CONTAINER(cc->box), hbox);
|
|
|
|
cc->count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void cc_on_custom_commands_dlg_add_clicked(GtkToolButton *toolbutton, struct cc_dialog *cc)
|
|
|
|
{
|
|
|
|
cc_add_command(cc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean cc_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
|
|
|
|
{
|
|
|
|
if (cond & (G_IO_IN | G_IO_PRI))
|
|
|
|
{
|
|
|
|
gchar *msg = NULL;
|
2007-03-09 13:52:26 +00:00
|
|
|
GIOStatus rv;
|
2007-03-01 10:54:37 +00:00
|
|
|
GError *err = NULL;
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
cc_buffer = g_string_sized_new(256);
|
|
|
|
|
2007-03-01 10:54:37 +00:00
|
|
|
do
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2007-03-01 10:54:37 +00:00
|
|
|
rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, &err);
|
|
|
|
if (msg != NULL)
|
|
|
|
{
|
2008-03-14 18:55:30 +00:00
|
|
|
g_string_append(cc_buffer, msg);
|
2007-03-01 10:54:37 +00:00
|
|
|
g_free(msg);
|
|
|
|
}
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(err != NULL))
|
2007-03-01 10:54:37 +00:00
|
|
|
{
|
2009-01-21 22:49:21 +00:00
|
|
|
geany_debug("%s: %s", G_STRFUNC, err->message);
|
2007-03-01 10:54:37 +00:00
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
} while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
|
2007-03-09 13:52:26 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(rv != G_IO_STATUS_EOF))
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* Something went wrong? */
|
2009-01-21 22:49:21 +00:00
|
|
|
g_warning("%s: %s\n", G_STRFUNC, "Incomplete command output");
|
2007-03-01 10:54:37 +00:00
|
|
|
}
|
2007-02-25 14:26:55 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean cc_iofunc_err(GIOChannel *ioc, GIOCondition cond, gpointer data)
|
|
|
|
{
|
|
|
|
if (cond & (G_IO_IN | G_IO_PRI))
|
|
|
|
{
|
|
|
|
gchar *msg = NULL;
|
2008-03-14 18:55:30 +00:00
|
|
|
GString *str = g_string_sized_new(256);
|
|
|
|
GIOStatus rv;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rv = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL);
|
|
|
|
if (msg != NULL)
|
|
|
|
{
|
|
|
|
g_string_append(str, msg);
|
|
|
|
g_free(msg);
|
|
|
|
}
|
|
|
|
} while (rv == G_IO_STATUS_NORMAL || rv == G_IO_STATUS_AGAIN);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
if (NZV(str->str))
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2008-03-14 18:55:30 +00:00
|
|
|
g_warning("%s: %s\n", (const gchar *) data, str->str);
|
|
|
|
ui_set_statusbar(TRUE,
|
|
|
|
_("The executed custom command returned an error. "
|
|
|
|
"Your selection was not changed. Error message: %s"),
|
|
|
|
str->str);
|
|
|
|
cc_error_occurred = TRUE;
|
|
|
|
|
2007-02-25 14:26:55 +00:00
|
|
|
}
|
2008-03-14 18:55:30 +00:00
|
|
|
g_string_free(str, TRUE);
|
|
|
|
}
|
|
|
|
cc_reading_finished = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean cc_replace_sel_cb(gpointer user_data)
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = user_data;
|
2008-03-14 18:55:30 +00:00
|
|
|
|
|
|
|
if (! cc_reading_finished)
|
|
|
|
{ /* keep this function in the main loop until cc_iofunc_err() has finished */
|
2007-02-25 14:26:55 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
if (! cc_error_occurred && cc_buffer != NULL)
|
|
|
|
{ /* Command completed successfully */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_replace_sel(doc->editor->sci, cc_buffer->str);
|
2008-03-14 18:55:30 +00:00
|
|
|
g_string_free(cc_buffer, TRUE);
|
|
|
|
cc_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_error_occurred = FALSE;
|
|
|
|
cc_reading_finished = FALSE;
|
|
|
|
|
2007-02-25 14:26:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
/* check whether the executed command failed and if so do nothing.
|
|
|
|
* If it returned with a sucessful exit code, replace the selection. */
|
|
|
|
static void cc_exit_cb(GPid child_pid, gint status, gpointer user_data)
|
|
|
|
{
|
|
|
|
/* if there was already an error, skip further checks */
|
|
|
|
if (! cc_error_occurred)
|
|
|
|
{
|
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
{
|
|
|
|
if (WEXITSTATUS(status) != EXIT_SUCCESS)
|
|
|
|
cc_error_occurred = TRUE;
|
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
{ /* the terminating signal: WTERMSIG (status)); */
|
|
|
|
cc_error_occurred = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* any other failure occured */
|
|
|
|
cc_error_occurred = TRUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
cc_error_occurred = ! win32_get_exit_status(child_pid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (cc_error_occurred)
|
|
|
|
{ /* here we are sure cc_error_occurred was set due to an unsuccessful exit code
|
|
|
|
* and so we add an error message */
|
|
|
|
/* TODO maybe include the exit code in the error message */
|
|
|
|
ui_set_statusbar(TRUE,
|
|
|
|
_("The executed custom command exited with an unsuccessful exit code."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_idle_add(cc_replace_sel_cb, user_data);
|
|
|
|
g_spawn_close_pid(child_pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-25 14:26:55 +00:00
|
|
|
/* Executes command (which should include all necessary command line args) and passes the current
|
|
|
|
* selection through the standard input of command. The whole output of command replaces the
|
|
|
|
* current selection. */
|
2008-06-15 13:35:48 +00:00
|
|
|
void tools_execute_custom_command(GeanyDocument *doc, const gchar *command)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GPid pid;
|
|
|
|
gchar **argv;
|
|
|
|
gint stdin_fd;
|
|
|
|
gint stdout_fd;
|
|
|
|
gint stderr_fd;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL && command != NULL);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-09-17 18:02:55 +00:00
|
|
|
if (! sci_has_selection(doc->editor->sci))
|
2007-02-25 14:26:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
argv = g_strsplit(command, " ", -1);
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Passing data and executing custom command: %s"), command);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
cc_error_occurred = FALSE;
|
|
|
|
|
|
|
|
if (g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
|
2007-02-25 14:26:55 +00:00
|
|
|
NULL, NULL, &pid, &stdin_fd, &stdout_fd, &stderr_fd, &error))
|
|
|
|
{
|
|
|
|
gchar *sel;
|
2007-03-01 10:54:37 +00:00
|
|
|
gint len, remaining, wrote;
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-03-14 18:55:30 +00:00
|
|
|
if (pid > 0)
|
2008-06-15 13:35:48 +00:00
|
|
|
g_child_watch_add(pid, (GChildWatchFunc) cc_exit_cb, doc);
|
2008-03-14 18:55:30 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* use GIOChannel to monitor stdout */
|
2009-09-16 14:13:38 +00:00
|
|
|
utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
|
2008-03-14 18:55:30 +00:00
|
|
|
FALSE, cc_iofunc, NULL);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* copy program's stderr to Geany's stdout to help error tracking */
|
2009-09-16 14:13:38 +00:00
|
|
|
utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
|
2007-03-01 21:45:43 +00:00
|
|
|
FALSE, cc_iofunc_err, (gpointer)command);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get selection */
|
2008-07-14 11:13:54 +00:00
|
|
|
len = sci_get_selected_text_length(doc->editor->sci);
|
2007-07-30 10:40:09 +00:00
|
|
|
sel = g_malloc0(len + 1);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_get_selected_text(doc->editor->sci, sel);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* write data to the command */
|
2007-03-01 10:54:37 +00:00
|
|
|
remaining = len - 1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
wrote = write(stdin_fd, sel, remaining);
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(wrote < 0))
|
2007-03-01 10:54:37 +00:00
|
|
|
{
|
2009-01-21 22:49:21 +00:00
|
|
|
g_warning("%s: %s: %s\n", G_STRFUNC, "Failed sending data to command",
|
2008-10-06 18:37:10 +00:00
|
|
|
g_strerror(errno));
|
2007-03-01 10:54:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
remaining -= wrote;
|
|
|
|
} while (remaining > 0);
|
2007-02-25 14:26:55 +00:00
|
|
|
close(stdin_fd);
|
|
|
|
g_free(sel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
|
2008-03-14 18:55:30 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Custom command failed: %s"), error->message);
|
2007-02-25 14:26:55 +00:00
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
static void cc_show_dialog_custom_commands(void)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
GtkWidget *dialog, *label, *vbox, *button;
|
2007-02-27 15:06:17 +00:00
|
|
|
guint i;
|
2007-02-25 14:26:55 +00:00
|
|
|
struct cc_dialog cc;
|
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
dialog = gtk_dialog_new_with_buttons(_("Set Custom Commands"), GTK_WINDOW(main_widgets.window),
|
2007-02-25 14:26:55 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
|
|
|
|
vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
|
|
|
|
gtk_box_set_spacing(GTK_BOX(vbox), 6);
|
2007-05-15 15:16:16 +00:00
|
|
|
gtk_widget_set_name(dialog, "GeanyDialog");
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
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."));
|
|
|
|
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), label);
|
|
|
|
|
|
|
|
cc.count = 1;
|
|
|
|
cc.box = gtk_vbox_new(FALSE, 0);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), cc.box);
|
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
cc_add_command(&cc, -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-10 21:11:25 +00:00
|
|
|
guint len = g_strv_length(ui_prefs.custom_commands);
|
|
|
|
for (i = 0; i < len; i++)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
if (ui_prefs.custom_commands[i][0] == '\0')
|
2008-02-27 13:17:29 +00:00
|
|
|
continue; /* skip empty fields */
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
cc_add_command(&cc, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
button = gtk_button_new_from_stock("gtk-add");
|
2008-07-18 13:40:48 +00:00
|
|
|
g_signal_connect(button, "clicked", G_CALLBACK(cc_on_custom_commands_dlg_add_clicked), &cc);
|
2007-02-25 14:26:55 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
gtk_widget_show_all(vbox);
|
|
|
|
|
|
|
|
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get all hboxes which contain a label and an entry element */
|
2007-02-25 14:26:55 +00:00
|
|
|
GList *children = gtk_container_get_children(GTK_CONTAINER(cc.box));
|
2009-10-12 16:31:38 +00:00
|
|
|
GList *node, *list;
|
2007-02-25 14:26:55 +00:00
|
|
|
GSList *result_list = NULL;
|
2007-03-09 13:52:26 +00:00
|
|
|
gint j = 0;
|
2007-02-25 14:26:55 +00:00
|
|
|
gint len = 0;
|
2007-02-26 13:32:34 +00:00
|
|
|
gchar **result = NULL;
|
2007-02-25 14:26:55 +00:00
|
|
|
const gchar *text;
|
|
|
|
|
2009-10-12 16:31:38 +00:00
|
|
|
foreach_list(node, children)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get the contents of each hbox */
|
2009-10-12 16:31:38 +00:00
|
|
|
list = gtk_container_get_children(GTK_CONTAINER(node->data));
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* first element of the list is the label, so skip it and get the entry element */
|
2009-10-12 16:31:38 +00:00
|
|
|
text = gtk_entry_get_text(GTK_ENTRY(list->next->data));
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if the content of the entry is non-empty, add it to the result array */
|
2007-02-25 14:26:55 +00:00
|
|
|
if (text[0] != '\0')
|
|
|
|
{
|
|
|
|
result_list = g_slist_append(result_list, g_strdup(text));
|
|
|
|
len++;
|
|
|
|
}
|
2009-10-12 16:31:38 +00:00
|
|
|
g_list_free(list);
|
2006-12-13 00:46:14 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* create a new null-terminated array but only if there any commands defined */
|
2007-02-26 13:32:34 +00:00
|
|
|
if (len > 0)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2007-02-26 13:32:34 +00:00
|
|
|
result = g_new(gchar*, len + 1);
|
|
|
|
while (result_list != NULL)
|
|
|
|
{
|
2007-03-09 13:52:26 +00:00
|
|
|
result[j] = (gchar*) result_list->data;
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2007-02-26 13:32:34 +00:00
|
|
|
result_list = result_list->next;
|
2007-03-09 13:52:26 +00:00
|
|
|
j++;
|
2007-02-26 13:32:34 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
result[len] = NULL; /* null-terminate the array */
|
2007-02-25 14:26:55 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* set the new array */
|
2007-08-23 11:34:06 +00:00
|
|
|
g_strfreev(ui_prefs.custom_commands);
|
|
|
|
ui_prefs.custom_commands = result;
|
2008-02-27 13:17:29 +00:00
|
|
|
/* rebuild the menu items */
|
2007-02-25 14:26:55 +00:00
|
|
|
tools_create_insert_custom_command_menu_items();
|
2007-02-26 13:32:34 +00:00
|
|
|
|
|
|
|
g_slist_free(result_list);
|
|
|
|
g_list_free(children);
|
2006-12-13 00:46:14 +00:00
|
|
|
}
|
2007-02-25 14:26:55 +00:00
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* enable or disable all custom command menu items when the sub menu is opened */
|
|
|
|
static void cc_on_custom_command_menu_activate(GtkMenuItem *menuitem, gpointer user_data)
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2007-02-25 14:26:55 +00:00
|
|
|
gint i, len;
|
|
|
|
gboolean enable;
|
2009-10-12 16:31:38 +00:00
|
|
|
GList *children, *node;
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2008-09-17 18:02:55 +00:00
|
|
|
enable = sci_has_selection(doc->editor->sci) && (ui_prefs.custom_commands != NULL);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
children = gtk_container_get_children(GTK_CONTAINER(user_data));
|
|
|
|
len = g_list_length(children);
|
|
|
|
i = 0;
|
2009-10-12 16:31:38 +00:00
|
|
|
foreach_list(node, children)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
if (i == (len - 2))
|
2008-02-27 13:17:29 +00:00
|
|
|
break; /* stop before the last two elements (the seperator and the set entry) */
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2009-10-12 16:31:38 +00:00
|
|
|
gtk_widget_set_sensitive(GTK_WIDGET(node->data), enable);
|
2007-02-25 14:26:55 +00:00
|
|
|
i++;
|
|
|
|
}
|
2009-10-12 16:31:38 +00:00
|
|
|
g_list_free(children);
|
2007-02-25 14:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void cc_on_custom_command_activate(GtkMenuItem *menuitem, gpointer user_data)
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2007-02-25 14:26:55 +00:00
|
|
|
gint command_idx;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
command_idx = GPOINTER_TO_INT(user_data);
|
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
if (ui_prefs.custom_commands == NULL ||
|
|
|
|
command_idx < 0 || command_idx > (gint) g_strv_length(ui_prefs.custom_commands))
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
cc_show_dialog_custom_commands();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* send it through the command and when the command returned the output the current selection
|
|
|
|
* will be replaced */
|
2008-06-15 13:35:48 +00:00
|
|
|
tools_execute_custom_command(doc, ui_prefs.custom_commands[command_idx]);
|
2006-12-13 00:46:14 +00:00
|
|
|
}
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
|
2009-10-27 18:10:39 +00:00
|
|
|
static void cc_insert_custom_command_items(GtkMenu *me, gchar *label, gint idx)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
GtkWidget *item;
|
|
|
|
gint key_idx = -1;
|
2008-08-08 18:05:53 +00:00
|
|
|
GeanyKeyBinding *kb = NULL;
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
switch (idx)
|
|
|
|
{
|
2008-03-12 13:37:39 +00:00
|
|
|
case 0: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD1; break;
|
|
|
|
case 1: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD2; break;
|
|
|
|
case 2: key_idx = GEANY_KEYS_FORMAT_SENDTOCMD3; break;
|
2007-02-25 14:26:55 +00:00
|
|
|
}
|
|
|
|
|
2008-02-29 19:30:28 +00:00
|
|
|
if (key_idx != -1)
|
2008-03-12 13:37:39 +00:00
|
|
|
kb = keybindings_lookup_item(GEANY_KEY_GROUP_FORMAT, key_idx);
|
2008-02-29 19:30:28 +00:00
|
|
|
|
2007-02-25 14:26:55 +00:00
|
|
|
item = gtk_menu_item_new_with_label(label);
|
|
|
|
if (key_idx != -1)
|
|
|
|
gtk_widget_add_accelerator(item, "activate", gtk_accel_group_new(),
|
2008-02-29 19:30:28 +00:00
|
|
|
kb->key, kb->mods, GTK_ACCEL_VISIBLE);
|
2007-02-25 14:26:55 +00:00
|
|
|
gtk_container_add(GTK_CONTAINER(me), item);
|
|
|
|
gtk_widget_show(item);
|
2008-07-18 13:40:48 +00:00
|
|
|
g_signal_connect(item, "activate", G_CALLBACK(cc_on_custom_command_activate),
|
2007-02-25 14:26:55 +00:00
|
|
|
GINT_TO_POINTER(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
void tools_create_insert_custom_command_menu_items(void)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2008-12-18 21:21:53 +00:00
|
|
|
GtkMenu *menu_edit = GTK_MENU(ui_lookup_widget(main_widgets.window, "send_selection_to2_menu"));
|
2007-02-25 14:26:55 +00:00
|
|
|
GtkWidget *item;
|
2009-10-12 16:31:38 +00:00
|
|
|
GList *me_children, *node;
|
2007-02-25 14:26:55 +00:00
|
|
|
static gboolean signal_set = FALSE;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* first clean the menus to be able to rebuild them */
|
2007-02-25 14:26:55 +00:00
|
|
|
me_children = gtk_container_get_children(GTK_CONTAINER(menu_edit));
|
2009-10-12 16:31:38 +00:00
|
|
|
foreach_list(node, me_children)
|
|
|
|
gtk_widget_destroy(GTK_WIDGET(node->data));
|
|
|
|
g_list_free(me_children);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
if (ui_prefs.custom_commands == NULL || g_strv_length(ui_prefs.custom_commands) == 0)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
|
|
|
item = gtk_menu_item_new_with_label(_("No custom commands defined."));
|
|
|
|
gtk_container_add(GTK_CONTAINER(menu_edit), item);
|
|
|
|
gtk_widget_set_sensitive(item, FALSE);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-10 21:11:25 +00:00
|
|
|
guint i, len;
|
2007-02-25 14:26:55 +00:00
|
|
|
gint idx = 0;
|
2009-02-10 21:11:25 +00:00
|
|
|
len = g_strv_length(ui_prefs.custom_commands);
|
|
|
|
for (i = 0; i < len; i++)
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
if (ui_prefs.custom_commands[i][0] != '\0') /* skip empty fields */
|
2007-02-25 14:26:55 +00:00
|
|
|
{
|
2009-10-27 18:10:39 +00:00
|
|
|
cc_insert_custom_command_items(menu_edit, ui_prefs.custom_commands[i], idx);
|
2007-02-25 14:26:55 +00:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* separator and Set menu item */
|
2007-02-25 14:26:55 +00:00
|
|
|
item = gtk_separator_menu_item_new();
|
|
|
|
gtk_container_add(GTK_CONTAINER(menu_edit), item);
|
|
|
|
gtk_widget_show(item);
|
|
|
|
|
2009-10-27 18:10:39 +00:00
|
|
|
cc_insert_custom_command_items(menu_edit, _("Set Custom Commands"), -1);
|
2007-02-25 14:26:55 +00:00
|
|
|
|
|
|
|
if (! signal_set)
|
|
|
|
{
|
2008-12-18 21:21:53 +00:00
|
|
|
g_signal_connect(ui_lookup_widget(main_widgets.window, "send_selection_to2"),
|
2007-02-25 14:26:55 +00:00
|
|
|
"activate", G_CALLBACK(cc_on_custom_command_menu_activate), menu_edit);
|
|
|
|
signal_set = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-27 11:28:17 +00:00
|
|
|
|
|
|
|
/* (stolen from bluefish, thanks)
|
|
|
|
* Returns number of characters, lines and words in the supplied gchar*.
|
|
|
|
* Handles UTF-8 correctly. Input must be properly encoded UTF-8.
|
2008-02-27 13:17:29 +00:00
|
|
|
* Words are defined as any characters grouped, separated with spaces. */
|
|
|
|
static void word_count(gchar *text, guint *chars, guint *lines, guint *words)
|
2007-07-27 11:28:17 +00:00
|
|
|
{
|
|
|
|
guint in_word = 0;
|
|
|
|
gunichar utext;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! text)
|
2009-04-05 21:07:40 +00:00
|
|
|
return; /* politely refuse to operate on NULL */
|
2007-07-27 11:28:17 +00:00
|
|
|
|
|
|
|
*chars = *words = *lines = 0;
|
|
|
|
while (*text != '\0')
|
|
|
|
{
|
|
|
|
(*chars)++;
|
|
|
|
|
|
|
|
switch (*text)
|
|
|
|
{
|
|
|
|
case '\n':
|
|
|
|
(*lines)++;
|
|
|
|
case '\r':
|
|
|
|
case '\f':
|
|
|
|
case '\t':
|
|
|
|
case ' ':
|
|
|
|
case '\v':
|
|
|
|
mb_word_separator:
|
|
|
|
if (in_word)
|
|
|
|
{
|
|
|
|
in_word = 0;
|
|
|
|
(*words)++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2008-02-27 13:17:29 +00:00
|
|
|
utext = g_utf8_get_char_validated(text, 2); /* This might be an utf-8 char */
|
|
|
|
if (g_unichar_isspace(utext)) /* Unicode encoded space? */
|
2007-07-27 11:28:17 +00:00
|
|
|
goto mb_word_separator;
|
2008-02-27 13:17:29 +00:00
|
|
|
if (g_unichar_isgraph(utext)) /* Is this something printable? */
|
2007-07-27 11:28:17 +00:00
|
|
|
in_word = 1;
|
|
|
|
break;
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Even if the current char is 2 bytes, this will iterate correctly. */
|
|
|
|
text = g_utf8_next_char(text);
|
2007-07-27 11:28:17 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Capture last word, if there's no whitespace at the end of the file. */
|
2009-04-05 21:07:40 +00:00
|
|
|
if (in_word)
|
|
|
|
(*words)++;
|
2008-02-27 13:17:29 +00:00
|
|
|
/* We start counting line numbers from 1 */
|
2009-04-05 21:07:40 +00:00
|
|
|
if (*chars > 0)
|
|
|
|
(*lines)++;
|
2007-07-27 11:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
void tools_word_count(void)
|
2007-07-27 11:28:17 +00:00
|
|
|
{
|
|
|
|
GtkWidget *dialog, *label, *vbox, *table;
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc;
|
2007-07-27 11:28:17 +00:00
|
|
|
guint chars = 0, lines = 0, words = 0;
|
|
|
|
gchar *text, *range;
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc = document_get_current();
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2007-07-27 11:28:17 +00:00
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
dialog = gtk_dialog_new_with_buttons(_("Word Count"), GTK_WINDOW(main_widgets.window),
|
2007-07-27 11:28:17 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
|
|
|
|
vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
|
|
|
|
gtk_widget_set_name(dialog, "GeanyDialog");
|
|
|
|
|
2008-09-17 18:02:55 +00:00
|
|
|
if (sci_has_selection(doc->editor->sci))
|
2007-07-27 11:28:17 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
text = g_malloc0(sci_get_selected_text_length(doc->editor->sci) + 1);
|
|
|
|
sci_get_selected_text(doc->editor->sci, text);
|
2007-07-27 11:28:17 +00:00
|
|
|
range = _("selection");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
text = g_malloc(sci_get_length(doc->editor->sci) + 1);
|
|
|
|
sci_get_text(doc->editor->sci, sci_get_length(doc->editor->sci) + 1 , text);
|
2007-07-27 11:28:17 +00:00
|
|
|
range = _("whole document");
|
|
|
|
}
|
|
|
|
word_count(text, &chars, &lines, &words);
|
|
|
|
g_free(text);
|
|
|
|
|
|
|
|
table = gtk_table_new(4, 2, FALSE);
|
|
|
|
gtk_table_set_row_spacings(GTK_TABLE(table), 5);
|
|
|
|
gtk_table_set_col_spacings(GTK_TABLE(table), 10);
|
|
|
|
|
|
|
|
label = gtk_label_new(_("Range:"));
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 0, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
|
|
|
|
|
|
|
|
label = gtk_label_new(range);
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 20, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
|
|
|
|
|
|
|
|
label = gtk_label_new(_("Lines:"));
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 0, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
|
|
|
|
|
|
|
|
text = g_strdup_printf("%d", lines);
|
|
|
|
label = gtk_label_new(text);
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 1, 2, 1, 2,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 20, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
|
|
|
|
g_free(text);
|
|
|
|
|
|
|
|
label = gtk_label_new(_("Words:"));
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 0, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
|
|
|
|
|
|
|
|
text = g_strdup_printf("%d", words);
|
|
|
|
label = gtk_label_new(text);
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 1, 2, 2, 3,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 20, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
|
|
|
|
g_free(text);
|
|
|
|
|
|
|
|
label = gtk_label_new(_("Characters:"));
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 0, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
|
|
|
|
|
|
|
|
text = g_strdup_printf("%d", chars);
|
|
|
|
label = gtk_label_new(text);
|
|
|
|
gtk_table_attach(GTK_TABLE(table), label, 1, 2, 3, 4,
|
|
|
|
(GtkAttachOptions) (GTK_FILL),
|
|
|
|
(GtkAttachOptions) (0), 20, 0);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
|
|
|
|
g_free(text);
|
|
|
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), table);
|
|
|
|
|
|
|
|
g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
|
|
|
|
g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_destroy), dialog);
|
|
|
|
|
|
|
|
gtk_widget_show_all(dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* color dialog callbacks
|
|
|
|
*/
|
2007-07-28 15:18:01 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2007-07-27 11:28:17 +00:00
|
|
|
static void
|
|
|
|
on_color_cancel_button_clicked (GtkButton *button,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2007-08-23 11:34:06 +00:00
|
|
|
gtk_widget_hide(ui_widgets.open_colorsel);
|
2007-07-27 11:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_color_ok_button_clicked (GtkButton *button,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GdkColor color;
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2007-07-27 11:28:17 +00:00
|
|
|
gchar *hex;
|
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
gtk_widget_hide(ui_widgets.open_colorsel);
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2007-07-27 11:28:17 +00:00
|
|
|
|
|
|
|
gtk_color_selection_get_current_color(
|
2007-08-23 11:34:06 +00:00
|
|
|
GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &color);
|
2007-07-27 11:28:17 +00:00
|
|
|
|
|
|
|
hex = utils_get_hex_from_color(&color);
|
2008-09-25 18:28:37 +00:00
|
|
|
editor_insert_color(doc->editor, hex);
|
2007-07-27 11:28:17 +00:00
|
|
|
g_free(hex);
|
|
|
|
}
|
2007-07-28 15:18:01 +00:00
|
|
|
#endif
|
2007-07-27 11:28:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* This shows the color selection dialog to choose a color. */
|
2009-04-05 21:07:40 +00:00
|
|
|
void tools_color_chooser(const gchar *color)
|
2007-07-27 11:28:17 +00:00
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
win32_show_color_dialog(color);
|
|
|
|
#else
|
2009-04-05 21:07:40 +00:00
|
|
|
gchar *c = (gchar*) color;
|
2007-07-27 11:28:17 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (ui_widgets.open_colorsel == NULL)
|
2007-07-27 11:28:17 +00:00
|
|
|
{
|
2007-08-23 11:34:06 +00:00
|
|
|
ui_widgets.open_colorsel = gtk_color_selection_dialog_new(_("Color Chooser"));
|
|
|
|
gtk_widget_set_name(ui_widgets.open_colorsel, "GeanyDialog");
|
2008-05-22 14:41:28 +00:00
|
|
|
gtk_window_set_transient_for(GTK_WINDOW(ui_widgets.open_colorsel), GTK_WINDOW(main_widgets.window));
|
2007-07-27 11:28:17 +00:00
|
|
|
gtk_color_selection_set_has_palette(
|
2007-08-23 11:34:06 +00:00
|
|
|
GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), TRUE);
|
2007-07-27 11:28:17 +00:00
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->cancel_button, "clicked",
|
2007-07-27 11:28:17 +00:00
|
|
|
G_CALLBACK(on_color_cancel_button_clicked), NULL);
|
2007-08-23 11:34:06 +00:00
|
|
|
g_signal_connect(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->ok_button, "clicked",
|
2007-07-27 11:28:17 +00:00
|
|
|
G_CALLBACK(on_color_ok_button_clicked), NULL);
|
2008-07-18 13:40:48 +00:00
|
|
|
g_signal_connect(ui_widgets.open_colorsel, "delete-event",
|
2007-07-27 11:28:17 +00:00
|
|
|
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if color is non-NULL set it in the dialog as preselected color */
|
2009-04-05 21:07:40 +00:00
|
|
|
if (c != NULL && (c[0] == '0' || c[0] == '#'))
|
2007-07-27 11:28:17 +00:00
|
|
|
{
|
|
|
|
GdkColor gc;
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (c[0] == '0' && c[1] == 'x')
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
|
2009-04-05 21:07:40 +00:00
|
|
|
c[1] = '#';
|
|
|
|
c++;
|
2007-07-27 11:28:17 +00:00
|
|
|
}
|
2009-05-02 17:04:35 +00:00
|
|
|
gdk_color_parse(c, &gc);
|
2007-07-27 11:28:17 +00:00
|
|
|
gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(
|
2007-08-23 11:34:06 +00:00
|
|
|
GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
|
2007-07-27 11:28:17 +00:00
|
|
|
gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(
|
2007-08-23 11:34:06 +00:00
|
|
|
GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel)->colorsel), &gc);
|
2007-07-27 11:28:17 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* We make sure the dialog is visible. */
|
2007-08-23 11:34:06 +00:00
|
|
|
gtk_window_present(GTK_WINDOW(ui_widgets.open_colorsel));
|
2007-07-27 11:28:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|