gradient-editor-commands.c 44.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gui-types.h"

#include "core/gimp.h"
#include "core/gimpcontext.h"
#include "core/gimpgradient.h"

31
#include "widgets/gimpgradienteditor.h"
32
#include "widgets/gimpitemfactory.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "widgets/gimpwidgets-utils.h"

#include "color-notebook.h"
#include "gradient-editor-commands.h"

#include "libgimp/gimpintl.h"


/*  local function prototypes  */

static void   gradient_editor_left_color_changed     (ColorNotebook     *cnb,
                                                      const GimpRGB     *color,
                                                      ColorNotebookState state,
                                                      gpointer           data);
static void   gradient_editor_right_color_changed    (ColorNotebook     *cnb,
                                                      const GimpRGB     *color,
                                                      ColorNotebookState state,
                                                      gpointer           data);

static GimpGradientSegment *
53 54
              gradient_editor_save_selection         (GimpGradientEditor    *editor);
static void   gradient_editor_replace_selection      (GimpGradientEditor    *editor,
55 56 57
                                                      GimpGradientSegment *replace_seg);

static void   gradient_editor_dialog_cancel_callback (GtkWidget         *widget,
58
                                                      GimpGradientEditor    *editor);
59
static void   gradient_editor_split_uniform_callback (GtkWidget         *widget,
60
                                                      GimpGradientEditor    *editor);
61
static void   gradient_editor_replicate_callback     (GtkWidget         *widget,
62
                                                      GimpGradientEditor    *editor);
63 64 65 66 67 68 69 70 71


/*  public functionss */

void
gradient_editor_left_color_cmd_callback (GtkWidget *widget,
                                         gpointer   data,
                                         guint      action)
{
72 73
  GimpGradientEditor *editor;
  GimpGradient       *gradient;
74

75
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
76 77 78 79

  if (! editor)
    return;

80
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
81 82 83 84 85 86 87 88 89 90 91

  editor->left_saved_dirty    = GIMP_DATA (gradient)->dirty;
  editor->left_saved_segments = gradient_editor_save_selection (editor);

  color_notebook_new (_("Left Endpoint Color"),
		      &editor->control_sel_l->left_color,
		      gradient_editor_left_color_changed,
		      editor,
		      editor->instant_update,
		      TRUE);

92
  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
93 94 95 96 97 98 99
}

void
gradient_editor_load_left_cmd_callback (GtkWidget *widget,
                                        gpointer   data,
                                        guint      action)
{
100 101 102
  GimpGradientEditor  *editor;
  GimpGradient        *gradient;
  GimpContext         *user_context;
103 104 105 106
  GimpGradientSegment *seg;
  GimpRGB              color;
  gint                 i;

107
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
108 109 110 111

  if (! editor)
    return;

112 113
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);

114
  user_context = gimp_get_user_context (GIMP_DATA_EDITOR (editor)->gimp);
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  i = (gint) action;

  switch (i)
    {
    case 0: /* Fetch from left neighbor's right endpoint */
      if (editor->control_sel_l->prev != NULL)
	seg = editor->control_sel_l->prev;
      else
	seg = gimp_gradient_segment_get_last (editor->control_sel_l);

      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &seg->right_color,
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      break;

    case 1: /* Fetch from right endpoint */
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_r->right_color,
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      break;

    case 2: /* Fetch from FG color */
142 143
      gimp_context_get_foreground (user_context, &color);

144 145 146 147 148 149 150 151
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &color,
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      break;

    case 3: /* Fetch from BG color */
152 153
      gimp_context_get_background (user_context, &color);

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &color,
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      break;

    default: /* Load a color */
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->saved_colors[i - 4],
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      break;
    }

170
  gimp_data_dirty (GIMP_DATA (gradient));
171

172
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
173 174 175 176 177 178 179
}

void
gradient_editor_save_left_cmd_callback (GtkWidget *widget,
                                        gpointer   data,
                                        guint      action)
{
180
  GimpGradientEditor *editor;
181

182
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
183 184 185 186 187 188 189 190 191 192 193 194

  if (! editor)
    return;

  editor->saved_colors[action] = editor->control_sel_l->left_color;
}

void
gradient_editor_right_color_cmd_callback (GtkWidget *widget,
                                          gpointer   data,
                                          guint      action)
{
195 196
  GimpGradientEditor *editor;
  GimpGradient       *gradient;
197

198
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
199 200 201 202

  if (! editor)
    return;

203
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
204 205 206 207 208 209 210 211 212 213 214

  editor->right_saved_dirty    = GIMP_DATA (gradient)->dirty;
  editor->right_saved_segments = gradient_editor_save_selection (editor);

  color_notebook_new (_("Right Endpoint Color"),
		      &editor->control_sel_l->right_color,
		      gradient_editor_right_color_changed,
		      editor,
		      editor->instant_update,
		      TRUE);

215
  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
216 217 218 219 220 221 222
}

void
gradient_editor_load_right_cmd_callback (GtkWidget *widget,
                                         gpointer   data,
                                         guint      action)
{
223 224 225
  GimpGradientEditor  *editor;
  GimpGradient        *gradient;
  GimpContext         *user_context;
226 227 228 229
  GimpGradientSegment *seg;
  GimpRGB              color;
  gint                 i;

230
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
231 232 233 234

  if (! editor)
    return;

235 236
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);

237
  user_context = gimp_get_user_context (GIMP_DATA_EDITOR (editor)->gimp);
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  i = (gint) action;

  switch (i)
    {
    case 0: /* Fetch from right neighbor's left endpoint */
      if (editor->control_sel_r->next != NULL)
	seg = editor->control_sel_r->next;
      else
	seg = gimp_gradient_segment_get_first (editor->control_sel_r);

      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_r->left_color,
                                              &seg->left_color,
                                              TRUE, TRUE);
      break;

    case 1: /* Fetch from left endpoint */
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_l->left_color,
                                              &editor->control_sel_l->left_color,
                                              TRUE, TRUE);
      break;

    case 2: /* Fetch from FG color */
265 266
      gimp_context_get_foreground (user_context, &color);

267 268 269 270 271 272 273 274
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_l->left_color,
                                              &color,
                                              TRUE, TRUE);
      break;

    case 3: /* Fetch from BG color */
275 276
      gimp_context_get_background (user_context, &color);

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_l->left_color,
                                              &color,
                                              TRUE, TRUE);
      break;

    default: /* Load a color */
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_l->left_color,
                                              &editor->saved_colors[i - 4],
                                              TRUE, TRUE);
      break;
    }

293
  gimp_data_dirty (GIMP_DATA (gradient));
294

295
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
296 297 298 299 300 301 302
}

void
gradient_editor_save_right_cmd_callback (GtkWidget *widget,
                                         gpointer   data,
                                         guint      action)
{
303
  GimpGradientEditor *editor;
304

305
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
306 307 308 309 310 311 312 313 314 315 316 317

  if (! editor)
    return;

  editor->saved_colors[action] = editor->control_sel_l->left_color;
}

void
gradient_editor_blending_func_cmd_callback (GtkWidget *widget,
                                            gpointer   data,
                                            guint      action)
{
318
  GimpGradientEditor          *editor;
319 320 321 322 323 324 325
  GimpGradient            *gradient;
  GimpGradientSegmentType  type;
  GimpGradientSegment     *seg, *aseg;

  if (! GTK_CHECK_MENU_ITEM (widget)->active)
    return;

326
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
327 328 329 330

  if (! editor)
    return;

331
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

  type = (GimpGradientSegmentType) action;

  seg = editor->control_sel_l;

  do
    {
      seg->type = type;

      aseg = seg;
      seg  = seg->next;
    }
  while (aseg != editor->control_sel_r);

  gimp_data_dirty (GIMP_DATA (gradient));

348
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
349 350 351 352 353 354 355
}

void
gradient_editor_coloring_type_cmd_callback (GtkWidget *widget,
                                            gpointer   data,
                                            guint      action)
{
356
  GimpGradientEditor           *editor;
357 358 359 360 361 362 363
  GimpGradient             *gradient;
  GimpGradientSegmentColor  color;
  GimpGradientSegment      *seg, *aseg;

  if (! GTK_CHECK_MENU_ITEM (widget)->active)
    return;

364
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
365 366 367 368

  if (! editor)
    return;

369
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

  color = (GimpGradientSegmentColor) action;

  seg = editor->control_sel_l;

  do
    {
      seg->color = color;

      aseg = seg;
      seg  = seg->next;
    }
  while (aseg != editor->control_sel_r);

  gimp_data_dirty (GIMP_DATA (gradient));

386
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
387 388 389 390 391 392 393
}

void
gradient_editor_flip_cmd_callback (GtkWidget *widget,
                                   gpointer   data,
                                   guint      action)
{
394
  GimpGradientEditor      *editor;
395 396 397 398 399 400
  GimpGradient        *gradient;
  GimpGradientSegment *oseg, *oaseg;
  GimpGradientSegment *seg, *prev, *tmp;
  GimpGradientSegment *lseg, *rseg;
  gdouble              left, right;

401
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
402 403 404 405

  if (! editor)
    return;

406
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

  left  = editor->control_sel_l->left;
  right = editor->control_sel_r->right;

  /* Build flipped segments */

  prev = NULL;
  oseg = editor->control_sel_r;
  tmp  = NULL;

  do
    {
      seg = gimp_gradient_segment_new ();

      if (prev == NULL)
	{
	  seg->left = left;
	  tmp = seg; /* Remember first segment */
	}
      else
	seg->left = left + right - oseg->right;

      seg->middle = left + right - oseg->middle;
      seg->right  = left + right - oseg->left;

      seg->left_color = oseg->right_color;

      seg->right_color = oseg->left_color;

      switch (oseg->type)
	{
438 439
	case GIMP_GRAD_SPHERE_INCREASING:
	  seg->type = GIMP_GRAD_SPHERE_DECREASING;
440 441
	  break;

442 443
	case GIMP_GRAD_SPHERE_DECREASING:
	  seg->type = GIMP_GRAD_SPHERE_INCREASING;
444 445 446 447 448 449 450 451
	  break;

	default:
	  seg->type = oseg->type;
	}

      switch (oseg->color)
	{
452 453
	case GIMP_GRAD_HSV_CCW:
	  seg->color = GIMP_GRAD_HSV_CW;
454 455
	  break;

456 457
	case GIMP_GRAD_HSV_CW:
	  seg->color = GIMP_GRAD_HSV_CCW;
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	  break;

	default:
	  seg->color = oseg->color;
	}

      seg->prev = prev;
      seg->next = NULL;

      if (prev)
	prev->next = seg;

      prev = seg;

      oaseg = oseg;
      oseg  = oseg->prev; /* Move backwards! */
    }
  while (oaseg != editor->control_sel_l);

  seg->right = right; /* Squish accumulative error */

  /* Free old segments */

  lseg = editor->control_sel_l->prev;
  rseg = editor->control_sel_r->next;

  oseg = editor->control_sel_l;

  do
    {
      oaseg = oseg->next;
      gimp_gradient_segment_free (oseg);
      oseg = oaseg;
    }
  while (oaseg != rseg);

  /* Link in new segments */

  if (lseg)
    lseg->next = tmp;
  else
    gradient->segments = tmp;

  tmp->prev = lseg;

  seg->next = rseg;

  if (rseg)
    rseg->prev = seg;

  /* Reset selection */

  editor->control_sel_l = tmp;
  editor->control_sel_r = seg;

  /* Done */

  gimp_data_dirty (GIMP_DATA (gradient));

517 518
  gimp_gradient_editor_update (editor,
                               GRAD_UPDATE_GRADIENT | GRAD_UPDATE_CONTROL);
519 520 521 522 523 524 525
}

void
gradient_editor_replicate_cmd_callback (GtkWidget *widget,
                                        gpointer   data,
                                        guint      action)
{
526 527 528 529 530 531
  GimpGradientEditor *editor;
  GtkWidget          *dialog;
  GtkWidget          *vbox;
  GtkWidget          *label;
  GtkWidget          *scale;
  GtkObject          *scale_data;
532

533
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

  if (! editor)
    return;

  /*  Create dialog window  */
  dialog =
    gimp_dialog_new ((editor->control_sel_l == editor->control_sel_r) ?
		     _("Replicate segment") :
		     _("Replicate selection"),
		     "gradient_segment_replicate",
		     gimp_standard_help_func,
		     "dialogs/gradient_editor/replicate_segment.html",
		     GTK_WIN_POS_MOUSE,
		     FALSE, TRUE, FALSE,

		     GTK_STOCK_CANCEL, gradient_editor_dialog_cancel_callback,
		     editor, NULL, NULL, TRUE, TRUE,

		     _("Replicate"), gradient_editor_replicate_callback,
		     editor, NULL, NULL, FALSE, FALSE,

		     NULL);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), vbox);
  gtk_widget_show (vbox);

  /*  Instructions  */
  label = gtk_label_new (_("Select the number of times"));
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  label = gtk_label_new ((editor->control_sel_l == editor->control_sel_r) ?
			 _("to replicate the selected segment") :
			 _("to replicate the selection"));
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  /*  Scale  */
  editor->replicate_times = 2;
  scale_data  = gtk_adjustment_new (2.0, 2.0, 21.0, 1.0, 1.0, 1.0);

  scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  gtk_scale_set_digits (GTK_SCALE (scale), 0);
  gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, TRUE, 4);
  gtk_widget_show (scale);

  g_signal_connect (G_OBJECT (scale_data), "value_changed",
		    G_CALLBACK (gimp_int_adjustment_update),
		    &editor->replicate_times);

  /*  Show!  */
  gtk_widget_show (dialog);
589
  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
590 591 592 593 594 595 596
}

void
gradient_editor_split_midpoint_cmd_callback (GtkWidget *widget,
                                             gpointer   data,
                                             guint      action)
{
597
  GimpGradientEditor      *editor;
598 599 600
  GimpGradient        *gradient;
  GimpGradientSegment *seg, *lseg, *rseg;

601
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
602 603 604 605

  if (! editor)
    return;

606
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
607 608 609 610 611 612 613 614 615 616 617 618 619 620

  seg = editor->control_sel_l;

  do
    {
      gimp_gradient_segment_split_midpoint (gradient, seg, &lseg, &rseg);
      seg = rseg->next;
    }
  while (lseg != editor->control_sel_r);

  editor->control_sel_r = rseg;

  gimp_data_dirty (GIMP_DATA (gradient));

621 622
  gimp_gradient_editor_update (editor,
                               GRAD_UPDATE_GRADIENT | GRAD_UPDATE_CONTROL);
623 624 625 626 627 628 629
}

void
gradient_editor_split_uniformly_cmd_callback (GtkWidget *widget,
                                              gpointer   data,
                                              guint      action)
{
630 631 632 633 634 635
  GimpGradientEditor *editor;
  GtkWidget          *dialog;
  GtkWidget          *vbox;
  GtkWidget          *label;
  GtkWidget          *scale;
  GtkObject          *scale_data;
636

637
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

  if (! editor)
    return;

  /*  Create dialog window  */
  dialog =
    gimp_dialog_new ((editor->control_sel_l == editor->control_sel_r) ?
		     _("Split Segment Uniformly") :
		     _("Split Segments Uniformly"),
		     "gradient_segment_split_uniformly",
		     gimp_standard_help_func,
		     "dialogs/gradient_editor/split_segments_uniformly.html",
		     GTK_WIN_POS_MOUSE,
		     FALSE, TRUE, FALSE,

		     GTK_STOCK_CANCEL, gradient_editor_dialog_cancel_callback,
		     editor, NULL, NULL, FALSE, TRUE,

		     _("Split"), gradient_editor_split_uniform_callback,
		     editor, NULL, NULL, TRUE, FALSE,

		     NULL);

  /*  The main vbox  */
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), vbox);
  gtk_widget_show (vbox);

  /*  Instructions  */
  label = gtk_label_new (_("Please select the number of uniform parts"));
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  label =
    gtk_label_new ((editor->control_sel_l == editor->control_sel_r) ?
		   _("in which to split the selected segment") :
		   _("in which to split the segments in the selection"));
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  /*  Scale  */
  editor->split_parts = 2;
  scale_data = gtk_adjustment_new (2.0, 2.0, 21.0, 1.0, 1.0, 1.0);

  scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  gtk_scale_set_digits (GTK_SCALE (scale), 0);
  gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 4);
  gtk_widget_show (scale);

  g_signal_connect (G_OBJECT (scale_data), "value_changed",
		    G_CALLBACK (gimp_int_adjustment_update),
		    &editor->split_parts);

  /*  Show!  */
  gtk_widget_show (dialog);
695
  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
696 697 698 699 700 701 702
}

void
gradient_editor_delete_cmd_callback (GtkWidget *widget,
                                     gpointer   data,
                                     guint      action)
{
703
  GimpGradientEditor      *editor;
704 705 706 707
  GimpGradient        *gradient;
  GimpGradientSegment *lseg, *rseg, *seg, *aseg, *next;
  gdouble              join;

708
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
709 710 711 712

  if (! editor)
    return;

713
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

  /* Remember segments to the left and to the right of the selection */

  lseg = editor->control_sel_l->prev;
  rseg = editor->control_sel_r->next;

  /* Cannot delete all the segments in the gradient */

  if ((lseg == NULL) && (rseg == NULL))
    return;

  /* Calculate join point */

  join = (editor->control_sel_l->left +
	  editor->control_sel_r->right) / 2.0;

  if (lseg == NULL)
    join = 0.0;
  else if (rseg == NULL)
    join = 1.0;

  /* Move segments */

  if (lseg != NULL)
    gimp_gradient_segments_compress_range (lseg, lseg, lseg->left, join);

  if (rseg != NULL)
    gimp_gradient_segments_compress_range (rseg, rseg, join, rseg->right);

  /* Link */

  if (lseg)
    lseg->next = rseg;

  if (rseg)
    rseg->prev = lseg;

  /* Delete old segments */

  seg = editor->control_sel_l;

  do
    {
      next = seg->next;
      aseg = seg;

      gimp_gradient_segment_free (seg);

      seg = next;
    }
  while (aseg != editor->control_sel_r);

  /* Change selection */

  if (rseg)
    {
      editor->control_sel_l = rseg;
      editor->control_sel_r = rseg;
    }
  else
    {
      editor->control_sel_l = lseg;
      editor->control_sel_r = lseg;
    }

  if (lseg == NULL)
    gradient->segments = rseg;

  /* Done */

  gimp_data_dirty (GIMP_DATA (gradient));

786 787
  gimp_gradient_editor_update (editor,
                               GRAD_UPDATE_GRADIENT | GRAD_UPDATE_CONTROL);
788 789 790 791 792 793 794
}

void
gradient_editor_recenter_cmd_callback (GtkWidget *widget,
                                       gpointer   data,
                                       guint      action)
{
795
  GimpGradientEditor      *editor;
796 797 798
  GimpGradient        *gradient;
  GimpGradientSegment *seg, *aseg;

799
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
800 801 802 803

  if (! editor)
    return;

804
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
805 806 807 808 809 810 811 812 813 814 815 816 817 818

  seg = editor->control_sel_l;

  do
    {
      seg->middle = (seg->left + seg->right) / 2.0;

      aseg = seg;
      seg  = seg->next;
    }
  while (aseg != editor->control_sel_r);

  gimp_data_dirty (GIMP_DATA (gradient));

819 820
  gimp_gradient_editor_update (editor,
                               GRAD_UPDATE_GRADIENT | GRAD_UPDATE_CONTROL);
821 822 823 824 825 826 827
}

void
gradient_editor_redistribute_cmd_callback (GtkWidget *widget,
                                           gpointer   data,
                                           guint      action)
{
828
  GimpGradientEditor      *editor;
829 830 831 832 833 834
  GimpGradient        *gradient;
  GimpGradientSegment *seg, *aseg;
  gdouble              left, right, seg_len;
  gint                 num_segs;
  gint                 i;

835
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
836 837 838 839

  if (! editor)
    return;

840
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

  /* Count number of segments in selection */

  num_segs = 0;
  seg      = editor->control_sel_l;

  do
    {
      num_segs++;
      aseg = seg;
      seg  = seg->next;
    }
  while (aseg != editor->control_sel_r);

  /* Calculate new segment length */

  left    = editor->control_sel_l->left;
  right   = editor->control_sel_r->right;
  seg_len = (right - left) / num_segs;

  /* Redistribute */

  seg = editor->control_sel_l;

  for (i = 0; i < num_segs; i++)
    {
      seg->left   = left + i * seg_len;
      seg->right  = left + (i + 1) * seg_len;
      seg->middle = (seg->left + seg->right) / 2.0;

      seg = seg->next;
    }

  /* Fix endpoints to squish accumulative error */

  editor->control_sel_l->left  = left;
  editor->control_sel_r->right = right;

  /* Done */

  gimp_data_dirty (GIMP_DATA (gradient));

883 884
  gimp_gradient_editor_update (editor,
                               GRAD_UPDATE_GRADIENT | GRAD_UPDATE_CONTROL);
885 886 887 888 889 890 891
}

void
gradient_editor_blend_color_cmd_callback (GtkWidget *widget,
                                          gpointer   data,
                                          guint      action)
{
892
  GimpGradientEditor *editor;
893

894
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
895 896 897 898 899 900 901 902 903 904

  if (! editor)
    return;

  gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                          editor->control_sel_r,
                                          &editor->control_sel_l->left_color,
                                          &editor->control_sel_r->right_color,
                                          TRUE, FALSE);

905
  gimp_data_dirty (GIMP_DATA_EDITOR (editor)->data);
906

907
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
908 909 910 911 912 913 914
}

void
gradient_editor_blend_opacity_cmd_callback (GtkWidget *widget,
                                            gpointer   data,
                                            guint      action)
{
915
  GimpGradientEditor *editor;
916

917
  editor = (GimpGradientEditor *) gimp_widget_get_callback_context (widget);
918 919 920 921 922 923 924 925 926 927

  if (! editor)
    return;

  gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                          editor->control_sel_r,
                                          &editor->control_sel_l->left_color,
                                          &editor->control_sel_r->right_color,
                                          FALSE, TRUE);

928
  gimp_data_dirty (GIMP_DATA_EDITOR (editor)->data);
929

930
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
931 932 933
}

void
934 935
gradient_editor_menu_update (GtkItemFactory *factory,
                             gpointer        data)
936
{
937 938
  GimpGradientEditor  *editor;
  GimpContext         *user_context;
939 940 941 942 943 944 945 946 947
  GimpGradientSegment *left_seg;
  GimpGradientSegment *right_seg;
  GimpRGB              fg;
  GimpRGB              bg;
  gboolean             blending_equal = TRUE;
  gboolean             coloring_equal = TRUE;
  gboolean             selection;
  gboolean             delete;

948
  editor = GIMP_GRADIENT_EDITOR (data);
949

950
  user_context = gimp_get_user_context (GIMP_DATA_EDITOR (editor)->gimp);
951

952 953 954 955 956 957 958 959 960 961
  if (editor->control_sel_l->prev)
    left_seg = editor->control_sel_l->prev;
  else
    left_seg = gimp_gradient_segment_get_last (editor->control_sel_l);

  if (editor->control_sel_r->next)
    right_seg = editor->control_sel_r->next;
  else
    right_seg = gimp_gradient_segment_get_first (editor->control_sel_r);

962 963
  gimp_context_get_foreground (user_context, &fg);
  gimp_context_get_background (user_context, &bg);
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989

  {
    GimpGradientSegmentType  type;
    GimpGradientSegmentColor color;
    GimpGradientSegment     *seg, *aseg;

    type  = editor->control_sel_l->type;
    color = editor->control_sel_l->color;

    seg = editor->control_sel_l;

    do
      {
        blending_equal = blending_equal && (seg->type == type);
        coloring_equal = coloring_equal && (seg->color == color);

        aseg = seg;
        seg  = seg->next;
      }
    while (aseg != editor->control_sel_r);
  }

  selection = (editor->control_sel_l != editor->control_sel_r);
  delete    = (editor->control_sel_l->prev || editor->control_sel_r->next);

#define SET_ACTIVE(menu,active) \
990
        gimp_item_factory_set_active (factory, menu, (active))
991
#define SET_COLOR(menu,color,set_label) \
992
        gimp_item_factory_set_color (factory, menu, (color), (set_label))
993
#define SET_LABEL(menu,label) \
994
        gimp_item_factory_set_label (factory, menu, (label))
995
#define SET_SENSITIVE(menu,condition) \
996
        gimp_item_factory_set_sensitive (factory, menu, (condition) != 0)
997
#define SET_VISIBLE(menu,condition) \
998
        gimp_item_factory_set_visible (factory, menu, (condition) != 0)
999

1000
  SET_COLOR ("/Left Endpoint's Color...",
1001
             &editor->control_sel_l->left_color, FALSE);
1002
  SET_COLOR ("/Load Left Color From/Left Neighbor's Right Endpoint",
1003
             &left_seg->right_color, FALSE);
1004
  SET_COLOR ("/Load Left Color From/Right Endpoint",
1005
             &editor->control_sel_r->right_color, FALSE);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  SET_COLOR ("/Load Left Color From/FG Color", &fg, FALSE);
  SET_COLOR ("/Load Left Color From/BG Color", &bg, FALSE);

  SET_COLOR ("/Load Left Color From/01", &editor->saved_colors[0], TRUE);
  SET_COLOR ("/Load Left Color From/02", &editor->saved_colors[1], TRUE);
  SET_COLOR ("/Load Left Color From/03", &editor->saved_colors[2], TRUE);
  SET_COLOR ("/Load Left Color From/04", &editor->saved_colors[3], TRUE);
  SET_COLOR ("/Load Left Color From/05", &editor->saved_colors[4], TRUE);
  SET_COLOR ("/Load Left Color From/06", &editor->saved_colors[5], TRUE);
  SET_COLOR ("/Load Left Color From/07", &editor->saved_colors[6], TRUE);
  SET_COLOR ("/Load Left Color From/08", &editor->saved_colors[7], TRUE);
  SET_COLOR ("/Load Left Color From/09", &editor->saved_colors[8], TRUE);
  SET_COLOR ("/Load Left Color From/10", &editor->saved_colors[9], TRUE);

  SET_COLOR ("/Save Left Color To/01", &editor->saved_colors[0], TRUE);
  SET_COLOR ("/Save Left Color To/02", &editor->saved_colors[1], TRUE);
  SET_COLOR ("/Save Left Color To/03", &editor->saved_colors[2], TRUE);
  SET_COLOR ("/Save Left Color To/04", &editor->saved_colors[3], TRUE);
  SET_COLOR ("/Save Left Color To/05", &editor->saved_colors[4], TRUE);
  SET_COLOR ("/Save Left Color To/06", &editor->saved_colors[5], TRUE);
  SET_COLOR ("/Save Left Color To/07", &editor->saved_colors[6], TRUE);
  SET_COLOR ("/Save Left Color To/08", &editor->saved_colors[7], TRUE);
  SET_COLOR ("/Save Left Color To/09", &editor->saved_colors[8], TRUE);
  SET_COLOR ("/Save Left Color To/10", &editor->saved_colors[9], TRUE);

  SET_COLOR ("/Right Endpoint's Color...",
1032
             &editor->control_sel_r->right_color, FALSE);
1033
  SET_COLOR ("/Load Right Color From/Right Neighbor's Left Endpoint",
1034
             &right_seg->left_color, FALSE);
1035
  SET_COLOR ("/Load Right Color From/Left Endpoint",
1036
             &editor->control_sel_l->left_color, FALSE);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  SET_COLOR ("/Load Right Color From/FG Color", &fg, FALSE);
  SET_COLOR ("/Load Right Color From/BG Color", &bg, FALSE);

  SET_COLOR ("/Load Right Color From/01", &editor->saved_colors[0], TRUE);
  SET_COLOR ("/Load Right Color From/02", &editor->saved_colors[1], TRUE);
  SET_COLOR ("/Load Right Color From/03", &editor->saved_colors[2], TRUE);
  SET_COLOR ("/Load Right Color From/04", &editor->saved_colors[3], TRUE);
  SET_COLOR ("/Load Right Color From/05", &editor->saved_colors[4], TRUE);
  SET_COLOR ("/Load Right Color From/06", &editor->saved_colors[5], TRUE);
  SET_COLOR ("/Load Right Color From/07", &editor->saved_colors[6], TRUE);
  SET_COLOR ("/Load Right Color From/08", &editor->saved_colors[7], TRUE);
  SET_COLOR ("/Load Right Color From/09", &editor->saved_colors[8], TRUE);
  SET_COLOR ("/Load Right Color From/10", &editor->saved_colors[9], TRUE);

  SET_COLOR ("/Save Right Color To/01", &editor->saved_colors[0], TRUE);
  SET_COLOR ("/Save Right Color To/02", &editor->saved_colors[1], TRUE);
  SET_COLOR ("/Save Right Color To/03", &editor->saved_colors[2], TRUE);
  SET_COLOR ("/Save Right Color To/04", &editor->saved_colors[3], TRUE);
  SET_COLOR ("/Save Right Color To/05", &editor->saved_colors[4], TRUE);
  SET_COLOR ("/Save Right Color To/06", &editor->saved_colors[5], TRUE);
  SET_COLOR ("/Save Right Color To/07", &editor->saved_colors[6], TRUE);
  SET_COLOR ("/Save Right Color To/08", &editor->saved_colors[7], TRUE);
  SET_COLOR ("/Save Right Color To/09", &editor->saved_colors[8], TRUE);
  SET_COLOR ("/Save Right Color To/10", &editor->saved_colors[9], TRUE);
1061 1062 1063

  if (! selection)
    {
1064 1065 1066 1067 1068 1069 1070 1071 1072
      SET_LABEL ("/flip",             _("Flip Segment"));
      SET_LABEL ("/replicate",        _("Replicate Segment"));
      SET_LABEL ("/blendingfunction", _("Blending Function for Segment"));
      SET_LABEL ("/coloringtype",     _("Coloring Type for Segment"));
      SET_LABEL ("/splitmidpoint",    _("Split Segment at Midpoint"));
      SET_LABEL ("/splituniformly",   _("Split Segment Uniformly"));
      SET_LABEL ("/delete",           _("Delete Segment"));
      SET_LABEL ("/recenter",         _("Re-center Segment's Midpoint"));
      SET_LABEL ("/redistribute",     _("Re-distribute Handles in Segment"));
1073 1074 1075
    }
  else
    {
1076 1077 1078 1079 1080 1081 1082 1083 1084
      SET_LABEL ("/flip",             _("Flip Selection"));
      SET_LABEL ("/replicate",        _("Replicate Selection"));
      SET_LABEL ("/blendingfunction", _("Blending Function for Selection"));
      SET_LABEL ("/coloringtype",     _("Coloring Type for Selection"));
      SET_LABEL ("/splitmidpoint",    _("Split Segments at Midpoints"));
      SET_LABEL ("/splituniformly",   _("Split Segments Uniformly"));
      SET_LABEL ("/delete",           _("Delete Selection"));
      SET_LABEL ("/recenter",         _("Re-center Midpoints in Selection"));
      SET_LABEL ("/redistribute",     _("Re-distribute Handles in Selection"));
1085 1086
    }

1087 1088
  SET_SENSITIVE ("/blendingfunction/(Varies)", FALSE);
  SET_SENSITIVE ("/coloringtype/(Varies)",     FALSE);
1089 1090 1091

  if (blending_equal)
    {
1092
      SET_VISIBLE ("/blendingfunction/(Varies)", FALSE);
1093 1094 1095

      switch (editor->control_sel_l->type)
        {
1096
        case GIMP_GRAD_LINEAR:
1097
          SET_ACTIVE ("/blendingfunction/Linear", TRUE);
1098
          break;
1099
        case GIMP_GRAD_CURVED:
1100
          SET_ACTIVE ("/blendingfunction/Curved", TRUE);
1101
          break;
1102
        case GIMP_GRAD_SINE:
1103
          SET_ACTIVE ("/blendingfunction/Sinusodial", TRUE);
1104
          break;
1105
        case GIMP_GRAD_SPHERE_INCREASING:
1106
          SET_ACTIVE ("/blendingfunction/Spherical (increasing)", TRUE);
1107
          break;
1108
        case GIMP_GRAD_SPHERE_DECREASING:
1109
          SET_ACTIVE ("/blendingfunction/Spherical (decreasing)", TRUE);
1110 1111 1112 1113 1114
          break;
        }
    }
  else
    {
1115 1116
      SET_VISIBLE ("/blendingfunction/(Varies)", TRUE);
      SET_ACTIVE ("/blendingfunction/(Varies)", TRUE);
1117 1118 1119 1120
    }

  if (coloring_equal)
    {
1121
      SET_VISIBLE ("/coloringtype/(Varies)", FALSE);
1122 1123 1124

      switch (editor->control_sel_l->color)
        {
1125
        case GIMP_GRAD_RGB:
1126
          SET_ACTIVE ("/coloringtype/RGB", TRUE);
1127
          break;
1128
        case GIMP_GRAD_HSV_CCW:
1129
          SET_ACTIVE ("/coloringtype/HSV (counter-clockwise hue)", TRUE);
1130
          break;
1131
        case GIMP_GRAD_HSV_CW:
1132
          SET_ACTIVE ("/coloringtype/HSV (clockwise hue)", TRUE);
1133 1134 1135 1136 1137
          break;
        }
    }
  else
    {
1138 1139
      SET_VISIBLE ("/coloringtype/(Varies)", TRUE);
      SET_ACTIVE ("/coloringtype/(Varies)", TRUE);
1140 1141
    }

1142 1143 1144
  SET_SENSITIVE ("/Blend Endpoints' Colors",  selection);
  SET_SENSITIVE ("/Blend Endpoints' Opacity", selection);
  SET_SENSITIVE ("/delete", delete);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

#undef SET_ACTIVE
#undef SET_COLOR
#undef SET_LABEL
#undef SET_SENSITIVE
#undef SET_VISIBLE
}

static void
gradient_editor_left_color_changed (ColorNotebook      *cnb,
                                    const GimpRGB      *color,
                                    ColorNotebookState  state,
                                    gpointer            data)
{
1159 1160
  GimpGradientEditor *editor;
  GimpGradient       *gradient;
1161

1162
  editor = (GimpGradientEditor *) data;
1163

1164
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

  switch (state)
    {
    case COLOR_NOTEBOOK_OK:
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              (GimpRGB *) color,
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      gimp_gradient_segments_free (editor->left_saved_segments);
      gimp_data_dirty (GIMP_DATA (gradient));
      color_notebook_free (cnb);
1177
      gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
      break;

    case COLOR_NOTEBOOK_UPDATE:
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              (GimpRGB *) color,
                                              &editor->control_sel_r->right_color,
                                              TRUE, TRUE);
      gimp_data_dirty (GIMP_DATA (gradient));
      break;

    case COLOR_NOTEBOOK_CANCEL:
      gradient_editor_replace_selection (editor, editor->left_saved_segments);
      GIMP_DATA (gradient)->dirty = editor->left_saved_dirty;
1192
      gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
1193
      color_notebook_free (cnb);
1194
      gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
1195 1196 1197
      break;
    }

1198
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
1199 1200 1201 1202 1203 1204 1205 1206
}

static void
gradient_editor_right_color_changed (ColorNotebook      *cnb,
                                     const GimpRGB      *color,
                                     ColorNotebookState  state,
                                     gpointer            data)
{
1207 1208
  GimpGradientEditor *editor;
  GimpGradient       *gradient;
1209

1210
  editor = (GimpGradientEditor *) data;
1211

1212
  gradient = GIMP_GRADIENT (GIMP_DATA_EDITOR (editor)->data);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

  switch (state)
    {
    case COLOR_NOTEBOOK_UPDATE:
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_r->left_color,
                                              (GimpRGB *) color,
                                              TRUE, TRUE);
      gimp_data_dirty (GIMP_DATA (gradient));
      break;

    case COLOR_NOTEBOOK_OK:
      gimp_gradient_segments_blend_endpoints (editor->control_sel_l,
                                              editor->control_sel_r,
                                              &editor->control_sel_r->left_color,
                                              (GimpRGB *) color,
                                              TRUE, TRUE);
      gimp_gradient_segments_free (editor->right_saved_segments);
      gimp_data_dirty (GIMP_DATA (gradient));
      color_notebook_free (cnb);
1234
      gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
1235 1236 1237 1238 1239 1240
      break;

    case COLOR_NOTEBOOK_CANCEL:
      gradient_editor_replace_selection (editor, editor->right_saved_segments);
      GIMP_DATA (gradient)->dirty = editor->right_saved_dirty;
      color_notebook_free (cnb);
1241
      gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
1242 1243 1244
      break;
    }

1245
  gimp_gradient_editor_update (editor, GRAD_UPDATE_GRADIENT);
1246 1247 1248
}

static GimpGradientSegment *
1249
gradient_editor_save_selection (GimpGradientEditor *editor)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
{
  GimpGradientSegment *seg, *prev, *tmp;
  GimpGradientSegment *oseg, *oaseg;

  prev = NULL;
  oseg = editor->control_sel_l;
  tmp  = NULL;

  do
    {
      seg = gimp_gradient_segment_new ();

      *seg = *oseg; /* Copy everything */

      if (prev == NULL)
	tmp = seg; /* Remember first segment */
      else
	prev->next = seg;

      seg->prev = prev;
      seg->next = NULL;

      prev  = seg;
      oaseg = oseg;
      oseg  = oseg->next;
    }
  while (oaseg != editor->control_sel_r);

  return tmp;
}

static void
1282
gradient_editor_replace_selection (GimpGradientEditor      *editor,
1283 1284 1285 1286 1287 1288
                                   GimpGradientSegment *replace_seg)
{
  GimpGradient        *gradient;
  GimpGradientSegment *lseg, *rseg;
  GimpGradientSegment *replace_last;