Created
December 19, 2010 15:07
-
-
Save codebrainz/747398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* large portions of this were taken from Geany's source code */ | |
#include "geanyplugin.h" | |
#include <math.h> | |
GeanyPlugin *geany_plugin; | |
GeanyData *geany_data; | |
GeanyFunctions *geany_functions; | |
PLUGIN_VERSION_CHECK(147) | |
PLUGIN_SET_INFO("Color Chooser", | |
"Shows a color dialog and inserts the color into the document.", | |
"1.0", "Matthew Brush <mbrush@leftclick.ca>"); | |
GtkToolItem *toolbutton = NULL; | |
gchar *get_sel_color(void) | |
{ | |
gchar *color; | |
gint pos; | |
GeanyDocument *doc; | |
doc = document_get_current(); | |
g_return_if_fail(doc != NULL); | |
pos = sci_get_current_position(doc->editor->sci); | |
color = editor_get_word_at_pos(doc->editor, pos, GEANY_WORDCHARS"#"); | |
return color; | |
} | |
gdouble scale_round(gdouble val, gdouble factor) | |
{ | |
val = floor(val); | |
val = (val > 0) ? val : 0; | |
val = (val < factor) ? val : factor; | |
return val; | |
} | |
gchar *get_hex_from_color(GdkColor *color) | |
{ | |
gchar *buffer = g_malloc0(9); | |
g_return_val_if_fail(color != NULL, NULL); | |
g_snprintf(buffer, 8, "#%02X%02X%02X", | |
(guint) (scale_round(color->red / 256, 255)), | |
(guint) (scale_round(color->green / 256, 255)), | |
(guint) (scale_round(color->blue / 256, 255))); | |
return buffer; | |
} | |
void insert_color(GeanyEditor *editor, const gchar *color) | |
{ | |
g_return_if_fail(editor != NULL); | |
gint pos; | |
if (sci_has_selection(editor->sci)) | |
{ | |
pos = sci_get_selection_start(editor->sci); | |
/* if selection starts with 0x, replace stuff after 0x */ | |
if (sci_get_char_at(editor->sci, pos) == '0' && | |
sci_get_char_at(editor->sci, pos + 1) == 'x') | |
{ | |
sci_set_selection_start(editor->sci, pos + 2); | |
sci_set_selection_end(editor->sci, pos + 8); | |
color++; /* skip the leading "0x" */ | |
} | |
/* if selection is missing the leading #, skip relacing # */ | |
else if (sci_get_char_at(editor->sci, pos - 1) == '#') | |
color++; | |
sci_replace_sel(editor->sci, color); | |
} | |
else | |
{ | |
pos = sci_get_current_position(editor->sci); | |
sci_insert_text(editor->sci, pos, color); | |
} | |
} | |
void on_color_sel_button_clicked(GtkToolButton *toolbutton, gpointer user_data) | |
{ | |
GtkWidget *color_sel_dialog; | |
GtkWidget *colorsel; | |
GdkColor color; | |
gint result; | |
gchar *sel_color; | |
GeanyDocument *doc = document_get_current(); | |
g_return_if_fail(doc != NULL); | |
sel_color = get_sel_color(); | |
color_sel_dialog = gtk_color_selection_dialog_new("Select a Color"); | |
colorsel = gtk_color_selection_dialog_get_color_selection( | |
GTK_COLOR_SELECTION_DIALOG(color_sel_dialog)); | |
gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(colorsel), TRUE); | |
/* if color is non-NULL set it in the dialog as preselected color */ | |
if (sel_color != NULL && (sel_color[0] == '0' || sel_color[0] == '#')) | |
{ | |
gchar *c = (gchar*)sel_color; /* store ptr to first chr for free() */ | |
if (c[0] == '0' && c[1] == 'x') | |
{ /* we have a string of the format "0x00ff00" and we need it to "#00ff00" */ | |
c[1] = '#'; | |
c++; | |
} | |
if (gdk_color_parse(c, &color)) | |
gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &color); | |
g_free(sel_color); | |
} | |
result = gtk_dialog_run(GTK_DIALOG(color_sel_dialog)); | |
if (result == GTK_RESPONSE_OK) | |
{ | |
gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(colorsel), &color); | |
sel_color = get_hex_from_color(&color); | |
insert_color(doc->editor, sel_color); | |
g_free(sel_color); | |
} | |
gtk_widget_destroy(GTK_WIDGET(color_sel_dialog)); | |
} | |
void plugin_init(GeanyData *data) | |
{ | |
toolbutton = gtk_tool_button_new_from_stock(GTK_STOCK_SELECT_COLOR); | |
gtk_widget_set_tooltip_text(GTK_WIDGET(toolbutton), | |
"Open a color chooser dialog to insert HTML colors into the " | |
"current document."); | |
g_signal_connect(G_OBJECT(toolbutton), "clicked", | |
G_CALLBACK(on_color_sel_button_clicked), NULL); | |
gtk_toolbar_insert(GTK_TOOLBAR(geany->main_widgets->toolbar), | |
toolbutton, 16); | |
gtk_widget_show(GTK_WIDGET(toolbutton)); | |
} | |
void plugin_cleanup(void) | |
{ | |
gtk_widget_destroy(GTK_WIDGET(toolbutton)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment