77
#define HAVE_COLORMAP(gimage) \
79
gimp_image_base_type (gimage) == GIMP_INDEXED && \
80
gimp_image_get_colormap (gimage) != NULL)
83
static void gimp_colormap_editor_class_init (GimpColormapEditorClass *klass);
84
static void gimp_colormap_editor_init (GimpColormapEditor *colormap_editor);
68
#define HAVE_COLORMAP(image) \
70
gimp_image_base_type (image) == GIMP_INDEXED && \
71
gimp_image_get_colormap (image) != NULL)
86
74
static GObject * gimp_colormap_editor_constructor (GType type,
115
101
static void gimp_colormap_preview_drop_color (GtkWidget *widget,
116
104
const GimpRGB *color,
119
107
static void gimp_colormap_adjustment_changed (GtkAdjustment *adjustment,
120
108
GimpColormapEditor *editor);
121
static void gimp_colormap_hex_entry_activate (GtkEntry *entry,
122
GimpColormapEditor *editor);
123
static gboolean gimp_colormap_hex_entry_focus_out (GtkEntry *entry,
109
static void gimp_colormap_hex_entry_changed (GimpColorHexEntry *entry,
125
110
GimpColormapEditor *editor);
127
static void gimp_colormap_image_mode_changed (GimpImage *gimage,
112
static void gimp_colormap_image_mode_changed (GimpImage *image,
128
113
GimpColormapEditor *editor);
129
static void gimp_colormap_image_colormap_changed (GimpImage *gimage,
114
static void gimp_colormap_image_colormap_changed (GimpImage *image,
131
116
GimpColormapEditor *editor);
119
G_DEFINE_TYPE (GimpColormapEditor, gimp_colormap_editor,
120
GIMP_TYPE_IMAGE_EDITOR)
122
#define parent_class gimp_colormap_editor_parent_class
134
124
static guint editor_signals[LAST_SIGNAL] = { 0 };
136
static GimpImageEditorClass *parent_class = NULL;
140
gimp_colormap_editor_get_type (void)
142
static GType type = 0;
146
static const GTypeInfo info =
148
sizeof (GimpColormapEditorClass),
149
(GBaseInitFunc) NULL,
150
(GBaseFinalizeFunc) NULL,
151
(GClassInitFunc) gimp_colormap_editor_class_init,
152
NULL, /* class_finalize */
153
NULL, /* class_data */
154
sizeof (GimpColormapEditor),
156
(GInstanceInitFunc) gimp_colormap_editor_init,
159
type = g_type_register_static (GIMP_TYPE_IMAGE_EDITOR,
160
"GimpColormapEditor",
168
128
gimp_colormap_editor_class_init (GimpColormapEditorClass* klass)
213
172
gtk_box_pack_start (GTK_BOX (editor), frame, TRUE, TRUE, 0);
214
173
gtk_widget_show (frame);
216
editor->palette = gtk_preview_new (GTK_PREVIEW_COLOR);
217
gtk_widget_set_size_request (editor->palette, -1, 60);
218
gtk_preview_set_expand (GTK_PREVIEW (editor->palette), TRUE);
219
gtk_widget_add_events (editor->palette, GDK_BUTTON_PRESS_MASK);
220
gtk_container_add (GTK_CONTAINER (frame), editor->palette);
221
gtk_widget_show (editor->palette);
175
editor->preview = gtk_preview_new (GTK_PREVIEW_COLOR);
176
gtk_widget_set_size_request (editor->preview, -1, 60);
177
gtk_preview_set_expand (GTK_PREVIEW (editor->preview), TRUE);
178
gtk_widget_add_events (editor->preview, GDK_BUTTON_PRESS_MASK);
179
gtk_container_add (GTK_CONTAINER (frame), editor->preview);
180
gtk_widget_show (editor->preview);
223
g_signal_connect_after (editor->palette, "size_allocate",
182
g_signal_connect_after (editor->preview, "size-allocate",
224
183
G_CALLBACK (gimp_colormap_preview_size_allocate),
227
g_signal_connect (editor->palette, "button_press_event",
186
g_signal_connect (editor->preview, "button-press-event",
228
187
G_CALLBACK (gimp_colormap_preview_button_press),
231
gimp_dnd_color_source_add (editor->palette, gimp_colormap_preview_drag_color,
190
gimp_dnd_color_source_add (editor->preview, gimp_colormap_preview_drag_color,
233
gimp_dnd_color_dest_add (editor->palette, gimp_colormap_preview_drop_color,
192
gimp_dnd_color_dest_add (editor->preview, gimp_colormap_preview_drop_color,
236
195
/* Some helpful hints */
240
199
gtk_box_pack_end (GTK_BOX (editor), table, FALSE, FALSE, 0);
241
200
gtk_widget_show (table);
243
editor->index_spinbutton =
244
gimp_spin_button_new ((GtkObject **) &editor->index_adjustment,
245
0, 0, 0, 1, 10, 10, 1.0, 0);
202
editor->index_spinbutton = gimp_spin_button_new (&adj,
203
0, 0, 0, 1, 10, 10, 1.0, 0);
204
editor->index_adjustment = GTK_ADJUSTMENT (adj);
246
205
gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
247
206
_("Color index:"), 0.0, 0.5,
248
207
editor->index_spinbutton, 1, TRUE);
250
g_signal_connect (editor->index_adjustment, "value_changed",
209
g_signal_connect (editor->index_adjustment, "value-changed",
251
210
G_CALLBACK (gimp_colormap_adjustment_changed),
254
editor->color_entry = gtk_entry_new ();
255
gtk_entry_set_width_chars (GTK_ENTRY (editor->color_entry), 8);
256
gtk_entry_set_max_length (GTK_ENTRY (editor->color_entry), 6);
213
editor->color_entry = gimp_color_hex_entry_new ();
214
gtk_entry_set_width_chars (GTK_ENTRY (editor->color_entry), 12);
257
215
gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
258
216
_("HTML notation:"), 0.0, 0.5,
259
217
editor->color_entry, 1, TRUE);
261
g_signal_connect (editor->color_entry, "activate",
262
G_CALLBACK (gimp_colormap_hex_entry_activate),
264
g_signal_connect (editor->color_entry, "focus_out_event",
265
G_CALLBACK (gimp_colormap_hex_entry_focus_out),
219
g_signal_connect (editor->color_entry, "color-changed",
220
G_CALLBACK (gimp_colormap_hex_entry_changed),
279
234
editor = GIMP_COLORMAP_EDITOR (object);
281
236
editor->edit_button =
282
gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap-editor",
283
"colormap-editor-edit-color",
237
gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap",
238
"colormap-edit-color",
286
241
editor->add_button =
287
gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap-editor",
288
"colormap-editor-add-color-from-fg",
289
"colormap-editor-add-color-from-bg",
242
gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap",
243
"colormap-add-color-from-fg",
244
"colormap-add-color-from-bg",
290
245
GDK_CONTROL_MASK,
322
277
gimp_colormap_editor_set_image (GimpImageEditor *image_editor,
325
280
GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (image_editor);
327
if (image_editor->gimage)
282
if (image_editor->image)
329
g_signal_handlers_disconnect_by_func (image_editor->gimage,
284
g_signal_handlers_disconnect_by_func (image_editor->image,
330
285
gimp_colormap_image_mode_changed,
332
g_signal_handlers_disconnect_by_func (image_editor->gimage,
287
g_signal_handlers_disconnect_by_func (image_editor->image,
333
288
gimp_colormap_image_colormap_changed,
336
291
if (editor->color_dialog)
337
292
gtk_widget_hide (editor->color_dialog);
339
if (! HAVE_COLORMAP (gimage))
294
if (! HAVE_COLORMAP (image))
341
296
editor->index_adjustment->upper = 0;
342
297
gtk_adjustment_changed (editor->index_adjustment);
349
GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, gimage);
304
GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image);
351
306
editor->col_index = 0;
352
307
editor->dnd_col_index = 0;
356
g_signal_connect (gimage, "mode_changed",
311
g_signal_connect (image, "mode-changed",
357
312
G_CALLBACK (gimp_colormap_image_mode_changed),
359
g_signal_connect (gimage, "colormap_changed",
314
g_signal_connect (image, "colormap-changed",
360
315
G_CALLBACK (gimp_colormap_image_colormap_changed),
363
if (HAVE_COLORMAP (gimage))
318
if (HAVE_COLORMAP (image))
365
320
gimp_colormap_editor_draw (editor);
367
322
editor->index_adjustment->upper =
368
gimp_image_get_colormap_size (gimage) - 1;
323
gimp_image_get_colormap_size (image) - 1;
370
325
gtk_adjustment_changed (editor->index_adjustment);
385
340
return g_object_new (GIMP_TYPE_COLORMAP_EDITOR,
386
341
"menu-factory", menu_factory,
387
"menu-identifier", "<ColormapEditor>",
388
"ui-path", "/colormap-editor-popup",
342
"menu-identifier", "<Colormap>",
343
"ui-path", "/colormap-popup",
393
gimp_colormap_editor_selected (GimpColormapEditor *editor,
394
GdkModifierType state)
396
g_return_if_fail (GIMP_IS_COLORMAP_EDITOR (editor));
398
g_signal_emit (editor, editor_signals[SELECTED], 0, state);
402
gimp_colormap_editor_col_index (GimpColormapEditor *editor)
404
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), 0);
406
return editor->col_index;
348
gimp_colormap_editor_get_index (GimpColormapEditor *editor,
349
const GimpRGB *search)
354
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), 01);
356
image = GIMP_IMAGE_EDITOR (editor)->image;
358
if (! HAVE_COLORMAP (image))
361
index = editor->col_index;
367
gimp_image_get_colormap_entry (image, index, &temp);
369
if (gimp_rgb_distance (&temp, search) > EPSILON)
371
gint n_colors = gimp_image_get_colormap_size (image);
374
for (i = 0; i < n_colors; i++)
376
gimp_image_get_colormap_entry (image, i, &temp);
378
if (gimp_rgb_distance (&temp, search) < EPSILON)
391
gimp_colormap_editor_set_index (GimpColormapEditor *editor,
397
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), FALSE);
399
image = GIMP_IMAGE_EDITOR (editor)->image;
401
if (! HAVE_COLORMAP (image))
404
index = CLAMP (index, 0, gimp_image_get_colormap_size (image) - 1);
406
if (index != editor->col_index)
408
gint old = editor->col_index;
410
editor->col_index = index;
411
editor->dnd_col_index = index;
413
gimp_colormap_editor_draw_cell (editor, old);
414
gimp_colormap_editor_draw_cell (editor, index);
416
gimp_colormap_editor_update_entries (editor);
420
gimp_image_get_colormap_entry (GIMP_IMAGE_EDITOR (editor)->image,
427
gimp_colormap_editor_max_index (GimpColormapEditor *editor)
431
g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), -1);
433
image = GIMP_IMAGE_EDITOR (editor)->image;
435
if (! HAVE_COLORMAP (image))
438
return MAX (0, gimp_image_get_colormap_size (image) - 1);
415
447
gimp_colormap_editor_draw (GimpColormapEditor *editor)
449
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
418
450
gint i, j, k, l, b;
421
453
gint cellsize, ncol, xn, yn, width, height;
424
gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
426
palette = editor->palette;
427
width = palette->allocation.width;
428
height = palette->allocation.height;
429
ncol = gimp_image_get_colormap_size (gimage);
455
width = editor->preview->allocation.width;
456
height = editor->preview->allocation.height;
457
ncol = gimp_image_get_colormap_size (image);
479
507
row[l * 3 + b] = (((((i * cellsize + k) & 0x4) ?
481
509
(l + 0x4)) & 0x4) ?
482
render_blend_light_check[0] :
483
render_blend_dark_check[0]);
510
gimp_render_blend_light_check[0] :
511
gimp_render_blend_dark_check[0]);
485
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row, 0,
513
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row, 0,
486
514
i * cellsize + k,
493
521
gimp_colormap_editor_draw_cell (editor, editor->col_index);
496
gtk_widget_queue_draw (palette);
524
gtk_widget_queue_draw (editor->preview);
500
528
gimp_colormap_editor_draw_cell (GimpColormapEditor *editor,
505
gint cellsize, x, y, k;
507
gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
509
cellsize = editor->cellsize;
510
row = g_alloca (cellsize * 3);
531
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
532
gint cellsize = editor->cellsize;
533
guchar *row = g_alloca (cellsize * 3);
511
536
x = (col % editor->xn) * cellsize;
512
537
y = (col / editor->xn) * cellsize;
516
541
for(k = 0; k < cellsize; k++)
517
542
row[k*3] = row[k*3+1] = row[k*3+2] = (k & 1) * 255;
518
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row, x, y, cellsize);
543
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row, x, y, cellsize);
520
545
if (!(cellsize & 1))
521
546
for (k = 0; k < cellsize; k++)
522
547
row[k*3] = row[k*3+1] = row[k*3+2] = ((x+y+1) & 1) * 255;
523
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row,
548
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
524
549
x, y+cellsize-1, cellsize);
526
551
row[0]=row[1]=row[2]=255;
528
553
= 255 * (cellsize & 1);
529
554
for (k = 1; k < cellsize - 1; k++)
531
row[k*3] = gimage->cmap[col * 3];
532
row[k*3+1] = gimage->cmap[col * 3 + 1];
533
row[k*3+2] = gimage->cmap[col * 3 + 2];
556
row[k*3] = image->cmap[col * 3];
557
row[k*3+1] = image->cmap[col * 3 + 1];
558
row[k*3+2] = image->cmap[col * 3 + 2];
535
560
for (k = 1; k < cellsize - 1; k+=2)
536
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row,
561
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
537
562
x, y+k, cellsize);
539
564
row[0] = row[1] = row[2] = 0;
540
565
row[cellsize*3-3] = row[cellsize*3-2] = row[cellsize*3-1]
541
566
= 255 * ((cellsize+1) & 1);
542
567
for (k = 2; k < cellsize - 1; k += 2)
543
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row,
568
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
544
569
x, y+k, cellsize);
548
573
for (k = 0; k < cellsize; k++)
550
row[k*3] = gimage->cmap[col * 3];
551
row[k*3+1] = gimage->cmap[col * 3 + 1];
552
row[k*3+2] = gimage->cmap[col * 3 + 2];
575
row[k*3] = image->cmap[col * 3];
576
row[k*3+1] = image->cmap[col * 3 + 1];
577
row[k*3+2] = image->cmap[col * 3 + 2];
554
579
for (k = 0; k < cellsize; k++)
555
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row,
580
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
556
581
x, y+k, cellsize);
559
gtk_widget_queue_draw_area (editor->palette,
584
gtk_widget_queue_draw_area (editor->preview,
561
586
cellsize, cellsize);
594
615
row[j * 3 + 0] = row[j * 3 + 1] = row[j * 3 + 2] =
595
616
((j + offset) & 0x4) ?
596
render_blend_dark_check[0] : render_blend_light_check[0];
617
gimp_render_blend_dark_check[0] : gimp_render_blend_light_check[0];
599
620
for (j = 0; j < (4 - (start_row & 0x3)) && start_row + j < height; j++)
600
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row,
621
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
601
622
0, start_row + j, width);
603
624
start_row += (4 - (start_row & 0x3));
612
633
row[j * 3 + 0] = row[j * 3 + 1] = row[j * 3 + 2] =
613
634
((j + offset) & 0x4) ?
614
render_blend_dark_check[0] : render_blend_light_check[0];
635
gimp_render_blend_dark_check[0] : gimp_render_blend_light_check[0];
617
638
for (j = 0; j < 4 && i + j < height; j++)
618
gtk_preview_draw_row (GTK_PREVIEW (editor->palette), row,
639
gtk_preview_draw_row (GTK_PREVIEW (editor->preview), row,
619
640
0, i + j, width);
625
gtk_widget_queue_draw (palette);
643
gtk_widget_queue_draw (editor->preview);
629
647
gimp_colormap_editor_update_entries (GimpColormapEditor *editor)
631
GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
649
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
633
if (! HAVE_COLORMAP (gimage))
651
if (! HAVE_COLORMAP (image))
635
653
gtk_widget_set_sensitive (editor->index_spinbutton, FALSE);
636
654
gtk_widget_set_sensitive (editor->color_entry, FALSE);
660
gimp_colormap_editor_set_index (GimpColormapEditor *editor,
663
if (i != editor->col_index)
665
gint old = editor->col_index;
667
editor->col_index = i;
668
editor->dnd_col_index = i;
670
gimp_colormap_editor_draw_cell (editor, old);
671
gimp_colormap_editor_draw_cell (editor, i);
673
gimp_colormap_editor_update_entries (editor);
678
678
gimp_colormap_preview_size_allocate (GtkWidget *widget,
679
679
GtkAllocation *alloc,
680
680
GimpColormapEditor *editor)
682
GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
682
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
684
if (HAVE_COLORMAP (gimage))
684
if (HAVE_COLORMAP (image))
685
685
gimp_colormap_editor_draw (editor);
687
687
gimp_colormap_editor_clear (editor, -1);
708
706
col = (editor->xn * ((gint) bevent->y / editor->cellsize) +
709
707
((gint) bevent->x / editor->cellsize));
711
if (col >= gimp_image_get_colormap_size (gimage))
709
if (col >= gimp_image_get_colormap_size (image))
714
712
switch (bevent->button)
717
gimp_colormap_editor_set_index (editor, col);
718
gimp_colormap_editor_selected (editor, bevent->state);
715
gimp_colormap_editor_set_index (editor, col, NULL);
716
g_signal_emit (editor, editor_signals[SELECTED], 0, bevent->state);
720
718
if (bevent->type == GDK_2BUTTON_PRESS)
724
action = gimp_ui_manager_find_action (GIMP_EDITOR (editor)->ui_manager,
726
"colormap-editor-edit-color");
729
gtk_action_activate (action);
719
gimp_ui_manager_activate_action (GIMP_EDITOR (editor)->ui_manager,
721
"colormap-edit-color");
754
GimpColormapEditor *editor;
757
editor = GIMP_COLORMAP_EDITOR (data);
758
gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
760
if (HAVE_COLORMAP (gimage))
761
gimp_image_get_colormap_entry (gimage, editor->dnd_col_index, color);
745
GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (data);
746
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
748
if (HAVE_COLORMAP (image))
749
gimp_image_get_colormap_entry (image, editor->dnd_col_index, color);
765
753
gimp_colormap_preview_drop_color (GtkWidget *widget,
766
756
const GimpRGB *color,
769
GimpColormapEditor *editor;
772
editor = GIMP_COLORMAP_EDITOR (data);
773
gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
775
if (HAVE_COLORMAP (gimage) && gimp_image_get_colormap_size (gimage) < 256)
759
GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (data);
760
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
762
if (HAVE_COLORMAP (image) && gimp_image_get_colormap_size (image) < 256)
777
gimp_image_add_colormap_entry (gimage, color);
764
gimp_image_add_colormap_entry (image, color);
782
769
gimp_colormap_adjustment_changed (GtkAdjustment *adjustment,
783
770
GimpColormapEditor *editor)
785
GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
772
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
787
if (HAVE_COLORMAP (gimage))
774
if (HAVE_COLORMAP (image))
789
gimp_colormap_editor_set_index (editor, adjustment->value + 0.5);
776
gimp_colormap_editor_set_index (editor, adjustment->value + 0.5, NULL);
791
778
gimp_colormap_editor_update_entries (editor);
796
gimp_colormap_hex_entry_activate (GtkEntry *entry,
797
GimpColormapEditor *editor)
783
gimp_colormap_hex_entry_changed (GimpColorHexEntry *entry,
784
GimpColormapEditor *editor)
799
GimpImage *gimage = GIMP_IMAGE_EDITOR (editor)->gimage;
786
GimpImage *image = GIMP_IMAGE_EDITOR (editor)->image;
806
s = gtk_entry_get_text (entry);
808
if (sscanf (s, "%lx", &i))
813
r = (i & 0xFF0000) >> 16;
814
g = (i & 0x00FF00) >> 8;
817
gimp_rgb_set_uchar (&color, r, g, b);
819
gimp_image_set_colormap_entry (gimage, editor->col_index, &color,
821
gimp_image_flush (gimage);
792
gimp_color_hex_entry_get_color (entry, &color);
794
gimp_image_set_colormap_entry (image, editor->col_index, &color, TRUE);
795
gimp_image_flush (image);
827
gimp_colormap_hex_entry_focus_out (GtkEntry *entry,
829
GimpColormapEditor *editor)
831
gimp_colormap_hex_entry_activate (entry, editor);
837
gimp_colormap_image_mode_changed (GimpImage *gimage,
800
gimp_colormap_image_mode_changed (GimpImage *image,
838
801
GimpColormapEditor *editor)
840
803
if (editor->color_dialog)
841
804
gtk_widget_hide (editor->color_dialog);
843
gimp_colormap_image_colormap_changed (gimage, -1, editor);
806
gimp_colormap_image_colormap_changed (image, -1, editor);
847
gimp_colormap_image_colormap_changed (GimpImage *gimage,
810
gimp_colormap_image_colormap_changed (GimpImage *image,
849
812
GimpColormapEditor *editor)
851
if (HAVE_COLORMAP (gimage))
814
if (HAVE_COLORMAP (image))