1
/* Bluefish HTML Editor
2
* coloursel2.c - Colour Picker
4
* Copyright (C) 2000 Olivier Sessink
6
* this is a rewrite from scratch, the original colour picker
7
* was Copyright (C) 1998 Neil Millar (neil@millar.u-net.com)
9
* This program is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU General Public License
11
* as published by the Free Software Foundation; either version 2
12
* of the License, or (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
#include "default_include.h"
26
#include <string.h> /* strncpy */
27
#include <stdlib.h> /* strtol() */
29
#include "coloursel.h" /* myself */
30
#include "gtk_easy.h" /* window_destroy() */
31
#include "bluefish.h" /* main_v */
32
#include "document.h" /* insert_dbl_text */
33
#include "pixmaps.h" /* new_pixmap() */
47
static gint string_is_color(gchar *color) {
51
DEBUG_MSG("string_is_color, pointer NULL\n");
54
if (strlen(color) != 7) {
55
DEBUG_MSG("string_is_color, strlen(%s) != 7\n", color);
58
if (color[0] != '#') {
59
DEBUG_MSG("string_is_color, 0 in %s is not #\n", color);
62
for (i = 1; i <7 ; i++) {
65
|| ((color[i] > 57) && (color[i] < 65))
66
|| ((color[i] > 70) && (color[i] < 97))) {
67
DEBUG_MSG("string_is_color, %d in %s is not from a color, it is %d\n", i, color, color[i]);
71
DEBUG_MSG("string_is_color, %s is color\n", color);
76
static gchar *gdouble_arr_to_hex(gdouble *color, gint websafe)
80
unsigned int green_int;
81
unsigned int blue_int;
91
red_int = 0x33*((unsigned int) (red * 255 / 0x33));
92
green_int = 0x33*((unsigned int) (green * 255/0x33));
93
blue_int = 0x33*((unsigned int) (blue * 255/0x33));
95
red_int = (unsigned int) (red * 255);
96
green_int = (unsigned int) (green * 255);
97
blue_int = (unsigned int) (blue * 255);
99
tmpstr = g_malloc(8*sizeof(char));
100
g_snprintf (tmpstr, 8,"#%.2X%.2X%.2X", red_int, green_int, blue_int);
104
static gdouble *hex_to_gdouble_arr(gchar *color)
106
static gdouble tmpcol[4];
110
if (!string_is_color(color)) {
111
DEBUG_MSG("hex_to_gdouble_arr, color is not a valid #...... color\n");
114
strncpy(tmpstr, &color[1], 2);
115
tmpl = strtol(tmpstr, NULL, 16);
116
tmpcol[0] = (gdouble) tmpl;
118
strncpy(tmpstr, &color[3], 2);
119
tmpl = strtol(tmpstr, NULL, 16);
120
tmpcol[1] = (gdouble) tmpl;
122
strncpy(tmpstr, &color[5], 2);
123
tmpl = strtol(tmpstr, NULL, 16);
124
tmpcol[2] = (gdouble) tmpl;
126
DEBUG_MSG("hex_to_gdouble_arr, R=%d, G=%d, B=%d\n", color[0], color[1], color[2]);
132
static void colsel_destroy_lcb(GtkWidget *widget, GdkEvent *event, Tcolsel *csd) {
138
tmpstr = gtk_editable_get_chars(GTK_EDITABLE(csd->hexentry), 0, -1);
139
if (strlen(tmpstr) == 7) {
140
if (csd->startpos || csd->endpos) {
141
replace_text(tmpstr, csd->startpos, csd->endpos);
143
insert_dbl_text(tmpstr, NULL);
148
window_destroy(csd->win);
153
static void colsel_ok_clicked_lcb(GtkWidget *widget, Tcolsel *csd) {
154
colsel_destroy_lcb(NULL, NULL, csd);
157
static void colsel_cancel_clicked_lcb(GtkWidget *widget, Tcolsel *csd) {
158
gtk_entry_set_text(GTK_ENTRY(csd->hexentry), "");
159
colsel_destroy_lcb(NULL, NULL, csd);
161
/* declaration needed to connect/disconnect callback */
162
static void colsel_color_changed(GtkWidget *widget, Tcolsel *csd);
164
static void hexentry_color_changed(GtkWidget *widget, Tcolsel *csd) {
167
tmpstr=gtk_editable_get_chars(GTK_EDITABLE(csd->hexentry), 0, -1);
168
if (strlen(tmpstr) == 7){
169
color = hex_to_gdouble_arr(tmpstr);
171
gtk_signal_disconnect(GTK_OBJECT(csd->csel), csd->csel_changed_id);
172
gtk_color_selection_set_color(GTK_COLOR_SELECTION(csd->csel), color);
173
csd->csel_changed_id = gtk_signal_connect(GTK_OBJECT(csd->csel), "color-changed", colsel_color_changed, csd);
182
static void colsel_color_changed(GtkWidget *widget, Tcolsel *csd) {
186
gtk_color_selection_get_color(GTK_COLOR_SELECTION(csd->csel), color);
187
tmpstr = gdouble_arr_to_hex(color, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(csd->websafe)));
189
gtk_signal_disconnect(GTK_OBJECT(csd->hexentry), csd->hex_changed_id);
190
gtk_entry_set_text(GTK_ENTRY(csd->hexentry), tmpstr);
191
csd->hex_changed_id = gtk_signal_connect(GTK_OBJECT(csd->hexentry), "changed", hexentry_color_changed, csd);
195
static Tcolsel *colsel_dialog(gchar *setcolor, gint modal, gint startpos, gint endpos) {
198
GtkWidget *vbox, *hbox, *but;
200
gchar *this_color=setcolor;
202
csd = g_malloc(sizeof(Tcolsel));
203
csd->is_modal = modal;
204
csd->startpos = startpos;
205
csd->endpos = endpos;
206
DEBUG_MSG("colsel_dialog, malloced at %p\n", csd);
207
csd->win = window_full(_("Bluefish: Select color"), GTK_WIN_POS_MOUSE, GTK_WINDOW_DIALOG
208
, 5, colsel_destroy_lcb, csd);
209
vbox = gtk_vbox_new(FALSE, 0);
210
gtk_container_add(GTK_CONTAINER(csd->win), vbox);
211
csd->csel = gtk_color_selection_new();
213
color = hex_to_gdouble_arr(this_color);
215
gtk_color_selection_set_color(GTK_COLOR_SELECTION(csd->csel), color);
220
csd->csel_changed_id = gtk_signal_connect(GTK_OBJECT(csd->csel), "color-changed", colsel_color_changed, csd);
221
gtk_box_pack_start(GTK_BOX(vbox), csd->csel, TRUE, TRUE, 0);
223
hbox = gtk_hbox_new(FALSE, 0);
224
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
226
csd->hexentry = boxed_entry_with_text(this_color, 7, hbox);
227
csd->hex_changed_id = gtk_signal_connect(GTK_OBJECT(csd->hexentry), "changed", hexentry_color_changed, csd);
228
csd->websafe = boxed_checkbut_with_value(_("websafe"), 0, hbox);
230
hbox = gtk_hbutton_box_new();
231
gtk_hbutton_box_set_layout_default(GTK_BUTTONBOX_END);
232
gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbox), 1);
233
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
235
but = bf_stock_ok_button(colsel_ok_clicked_lcb, csd);
236
gtk_window_set_default(GTK_WINDOW(csd->win), but);
237
gtk_box_pack_start(GTK_BOX(hbox), but, TRUE, TRUE, 0);
238
but = bf_stock_cancel_button(colsel_cancel_clicked_lcb, csd);
239
gtk_box_pack_start(GTK_BOX(hbox), but, TRUE, TRUE, 0);
240
gtk_widget_show_all(csd->win);
241
DEBUG_MSG("colsel_dialog, finished\n");
245
void sel_colour_cb(GtkWidget *widget, gpointer data) {
251
if (GTK_EDITABLE(main_v->current_document->textbox)->has_selection) {
252
DEBUG_MSG("sel_colour_cb, selection found\n");
253
startpos = GTK_EDITABLE(main_v->current_document->textbox)->selection_start_pos;
254
endpos = GTK_EDITABLE(main_v->current_document->textbox)->selection_end_pos;
255
if (startpos > endpos) {
261
if ((endpos - startpos) == 7) {
262
tmpstr = gtk_editable_get_chars(GTK_EDITABLE(main_v->current_document->textbox) ,startpos, endpos);
263
if (!string_is_color(tmpstr)) {
264
startpos = endpos = 0;
266
DEBUG_MSG("sel_colour_cb, tmpstr = %s (startpos=%d, endpos=%d)\n", tmpstr, startpos, endpos);
268
DEBUG_MSG("sel_colour_cb, startpos=%d, endpos=%d\n", startpos, endpos);
269
startpos = endpos = 0;
272
DEBUG_MSG("sel_colour_cb, NO selection found\n");
275
csd = colsel_dialog(tmpstr, 0, startpos, endpos);
281
gchar *return_color(gchar *start_value) {
286
csd = colsel_dialog(start_value, 1, 0, 0);
287
DEBUG_MSG("return color, started\n");
288
gtk_grab_add(csd->win);
291
return_text = gtk_editable_get_chars(GTK_EDITABLE(csd->hexentry), 0, -1);
292
window_destroy(csd->win);
294
DEBUG_MSG("return_color, the new gtk_main stopped, return_text=%s, %p\n", return_text, return_text);
299
static void color_but_clicked(GtkWidget * widget, GtkWidget * entry)
301
gchar *tmpstring, *tmpstr2;
303
DEBUG_MSG("color_but_clicked, before return_color\n");
304
tmpstr2 = gtk_editable_get_chars(GTK_EDITABLE(entry),0 ,-1);
305
tmpstring = return_color(tmpstr2);
306
DEBUG_MSG("color_but_clicked, return_color=%s\n", tmpstring);
307
gtk_entry_set_text(GTK_ENTRY(entry), tmpstring);
313
GtkWidget *color_but_new(GtkWidget * which_entry, GtkWidget * win)
316
GtkWidget *pixmap, *color_but;
318
color_but = gtk_button_new();
319
pixmap = new_pixmap(176, win->window, NULL);
320
gtk_widget_show(pixmap);
321
gtk_container_add(GTK_CONTAINER(color_but), pixmap);
322
gtk_signal_connect(GTK_OBJECT(color_but), "clicked", color_but_clicked, which_entry);
323
gtk_widget_show(color_but);