~ubuntu-branches/ubuntu/natty/bluefish/natty-proposed

« back to all changes in this revision

Viewing changes to src/coloursel.c

  • Committer: Bazaar Package Importer
  • Author(s): Davide Puricelli (evo)
  • Date: 2005-04-23 17:05:18 UTC
  • mto: (1.1.5 upstream) (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050423170518-izh2k25xve7ui1jx
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Bluefish HTML Editor
2
 
 * coloursel2.c - Colour Picker
3
 
 *
4
 
 * Copyright (C) 2000 Olivier Sessink
5
 
 *
6
 
 * this is a rewrite from scratch, the original colour picker
7
 
 * was Copyright (C) 1998 Neil Millar (neil@millar.u-net.com)
8
 
 *
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.
13
 
 * 
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.
18
 
 * 
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. 
22
 
 *
23
 
 */
24
 
#include "default_include.h"
25
 
 
26
 
#include <string.h>     /* strncpy */
27
 
#include <stdlib.h>  /* strtol() */
28
 
 
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() */
34
 
 
35
 
typedef struct {
36
 
        GtkWidget *win;
37
 
        GtkWidget *hexentry;
38
 
        GtkWidget *websafe;
39
 
        GtkWidget *csel;
40
 
        gint is_modal;
41
 
        gint startpos;
42
 
        gint endpos;
43
 
        gint hex_changed_id;
44
 
        gint csel_changed_id;
45
 
} Tcolsel;
46
 
 
47
 
static gint string_is_color(gchar *color) {
48
 
        gint i;
49
 
 
50
 
        if (!color) {
51
 
                DEBUG_MSG("string_is_color, pointer NULL\n");
52
 
                return 0;
53
 
        }
54
 
        if (strlen(color) != 7) {
55
 
                DEBUG_MSG("string_is_color, strlen(%s) != 7\n", color);
56
 
                return 0;
57
 
        }
58
 
        if (color[0] != '#') {
59
 
                DEBUG_MSG("string_is_color, 0 in %s is not #\n", color);
60
 
                return 0;
61
 
        }
62
 
        for (i = 1; i <7 ; i++) {
63
 
                if ((color[i] > 102) 
64
 
                                || (color[i] < 48) 
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]);
68
 
                        return 0;
69
 
                }
70
 
        }
71
 
        DEBUG_MSG("string_is_color, %s is color\n", color);
72
 
        return 1;
73
 
 
74
 
}
75
 
 
76
 
static gchar *gdouble_arr_to_hex(gdouble *color, gint websafe)
77
 
{
78
 
        gchar *tmpstr;
79
 
        unsigned int red_int;
80
 
        unsigned int green_int;
81
 
        unsigned int blue_int;
82
 
        gdouble red;
83
 
        gdouble green;
84
 
        gdouble blue;
85
 
        
86
 
        red = color[0];
87
 
        green = color[1];
88
 
        blue = color[2];
89
 
 
90
 
        if (websafe) {
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));
94
 
        } else {
95
 
                red_int = (unsigned int) (red * 255);
96
 
                green_int = (unsigned int) (green * 255);
97
 
                blue_int = (unsigned int) (blue * 255);
98
 
        }
99
 
        tmpstr = g_malloc(8*sizeof(char));
100
 
        g_snprintf (tmpstr, 8,"#%.2X%.2X%.2X", red_int, green_int, blue_int);
101
 
        return tmpstr;
102
 
}
103
 
 
104
 
static gdouble *hex_to_gdouble_arr(gchar *color)
105
 
{
106
 
        static gdouble tmpcol[4];
107
 
        gchar tmpstr[8];
108
 
        long tmpl;
109
 
 
110
 
        if (!string_is_color(color)) {
111
 
                DEBUG_MSG("hex_to_gdouble_arr, color is not a valid #...... color\n");
112
 
                return NULL;
113
 
        }
114
 
        strncpy(tmpstr, &color[1], 2);
115
 
        tmpl = strtol(tmpstr, NULL, 16);
116
 
        tmpcol[0] = (gdouble) tmpl;
117
 
        
118
 
        strncpy(tmpstr, &color[3], 2);
119
 
        tmpl = strtol(tmpstr, NULL, 16);
120
 
        tmpcol[1] = (gdouble) tmpl;
121
 
        
122
 
        strncpy(tmpstr, &color[5], 2);
123
 
        tmpl = strtol(tmpstr, NULL, 16);
124
 
        tmpcol[2] = (gdouble) tmpl;
125
 
 
126
 
        DEBUG_MSG("hex_to_gdouble_arr, R=%d, G=%d, B=%d\n", color[0], color[1], color[2]);
127
 
 
128
 
        tmpcol[3] = 0;
129
 
        return tmpcol;
130
 
}
131
 
 
132
 
static void colsel_destroy_lcb(GtkWidget *widget, GdkEvent *event, Tcolsel *csd) {
133
 
        if (csd->is_modal) {
134
 
                gtk_main_quit();
135
 
        } else {
136
 
                gchar *tmpstr;
137
 
                
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);
142
 
                        } else {
143
 
                                insert_dbl_text(tmpstr, NULL);
144
 
                        }
145
 
                }
146
 
                g_free(tmpstr);
147
 
                
148
 
                window_destroy(csd->win);
149
 
                g_free(csd);
150
 
        }
151
 
}
152
 
 
153
 
static void colsel_ok_clicked_lcb(GtkWidget *widget, Tcolsel *csd) { 
154
 
        colsel_destroy_lcb(NULL, NULL, csd);
155
 
}
156
 
 
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);
160
 
}
161
 
/* declaration needed to connect/disconnect callback */
162
 
static void colsel_color_changed(GtkWidget *widget, Tcolsel *csd);
163
 
 
164
 
static void hexentry_color_changed(GtkWidget *widget, Tcolsel *csd) {
165
 
        gdouble *color;
166
 
        gchar *tmpstr;
167
 
        tmpstr=gtk_editable_get_chars(GTK_EDITABLE(csd->hexentry), 0, -1);
168
 
        if (strlen(tmpstr) == 7){
169
 
                color = hex_to_gdouble_arr(tmpstr);
170
 
                if (color) {
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);
174
 
                } else {
175
 
                        tmpstr=NULL;
176
 
                }
177
 
        }
178
 
        g_free(tmpstr);
179
 
}
180
 
 
181
 
 
182
 
static void colsel_color_changed(GtkWidget *widget, Tcolsel *csd) {
183
 
        gdouble color[4];
184
 
        gchar *tmpstr;
185
 
 
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)));
188
 
 
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);
192
 
        g_free(tmpstr);
193
 
}
194
 
 
195
 
static Tcolsel *colsel_dialog(gchar *setcolor, gint modal, gint startpos, gint endpos) {
196
 
 
197
 
        Tcolsel *csd;
198
 
        GtkWidget *vbox, *hbox, *but;
199
 
        gdouble *color;
200
 
        gchar *this_color=setcolor;
201
 
 
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();
212
 
        if (this_color) {
213
 
                color = hex_to_gdouble_arr(this_color);
214
 
                if (color) {
215
 
                        gtk_color_selection_set_color(GTK_COLOR_SELECTION(csd->csel), color);
216
 
                } else {
217
 
                        this_color=NULL;
218
 
                }
219
 
        }
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);
222
 
 
223
 
        hbox = gtk_hbox_new(FALSE, 0);
224
 
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
225
 
        
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);
229
 
 
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);
234
 
 
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");
242
 
        return csd;
243
 
}
244
 
 
245
 
void sel_colour_cb(GtkWidget *widget, gpointer data) {
246
 
        Tcolsel *csd;
247
 
        gchar *tmpstr=NULL;
248
 
        gint startpos=0;
249
 
        gint endpos=0;
250
 
 
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) {
256
 
                        gint tmpint;
257
 
                        tmpint = startpos;
258
 
                        startpos = endpos;              
259
 
                        endpos = tmpint;
260
 
                }
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;
265
 
                        }
266
 
                        DEBUG_MSG("sel_colour_cb, tmpstr = %s (startpos=%d, endpos=%d)\n", tmpstr, startpos, endpos);
267
 
                } else {
268
 
                        DEBUG_MSG("sel_colour_cb, startpos=%d, endpos=%d\n", startpos, endpos);
269
 
                        startpos = endpos = 0;
270
 
                }
271
 
        } else {
272
 
                DEBUG_MSG("sel_colour_cb, NO selection found\n");
273
 
        }
274
 
 
275
 
        csd = colsel_dialog(tmpstr, 0, startpos, endpos);
276
 
        if (tmpstr) {
277
 
                g_free(tmpstr);
278
 
        }
279
 
}
280
 
 
281
 
gchar *return_color(gchar *start_value) {
282
 
 
283
 
        Tcolsel *csd;
284
 
        gchar *return_text;
285
 
 
286
 
        csd = colsel_dialog(start_value, 1, 0, 0);
287
 
        DEBUG_MSG("return color, started\n");
288
 
        gtk_grab_add(csd->win);
289
 
        gtk_main();
290
 
 
291
 
        return_text = gtk_editable_get_chars(GTK_EDITABLE(csd->hexentry), 0, -1);
292
 
        window_destroy(csd->win);
293
 
        g_free(csd);
294
 
        DEBUG_MSG("return_color, the new gtk_main stopped, return_text=%s, %p\n", return_text, return_text);
295
 
        return return_text;
296
 
 
297
 
}
298
 
 
299
 
static void color_but_clicked(GtkWidget * widget, GtkWidget * entry)
300
 
{
301
 
        gchar *tmpstring, *tmpstr2;
302
 
 
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);
308
 
        g_free(tmpstring);
309
 
        g_free(tmpstr2);
310
 
 
311
 
}
312
 
 
313
 
GtkWidget *color_but_new(GtkWidget * which_entry, GtkWidget * win)
314
 
{
315
 
 
316
 
        GtkWidget *pixmap, *color_but;
317
 
 
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);
324
 
        return color_but;
325
 
}