gimpwidgetsutils.c 23.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpwidgets.c
 * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24 25 26 27 28
#include <lcms2.h>

#include <gegl.h>
#include <gtk/gtk.h>

29 30 31 32 33 34 35 36 37 38 39
#ifdef G_OS_WIN32
#include <windows.h>
#endif

#ifdef GDK_WINDOWING_QUARTZ
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
#include <CoreServices/CoreServices.h>
#endif

#include "libgimpcolor/gimpcolor.h"
40
#include "libgimpconfig/gimpconfig.h"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#include "gimpwidgetstypes.h"

#include "gimpsizeentry.h"
#include "gimpwidgetsutils.h"

#include "libgimp/libgimp-intl.h"


/**
 * SECTION: gimpwidgetsutils
 * @title: GimpWidgetsUtils
 * @short_description: A collection of helper functions.
 *
 * A collection of helper functions.
 **/


static GtkWidget *
find_mnemonic_widget (GtkWidget *widget,
                      gint       level)
{
  gboolean can_focus;

  g_object_get (widget, "can-focus", &can_focus, NULL);

  if (GTK_WIDGET_GET_CLASS (widget)->activate_signal ||
      can_focus                                      ||
      GTK_WIDGET_GET_CLASS (widget)->mnemonic_activate !=
      GTK_WIDGET_CLASS (g_type_class_peek (GTK_TYPE_WIDGET))->mnemonic_activate)
    {
      return widget;
    }

  if (GIMP_IS_SIZE_ENTRY (widget))
    {
77 78
      GimpSizeEntry *entry    = GIMP_SIZE_ENTRY (widget);
      gint           n_fields = gimp_size_entry_get_n_fields (entry);
79

80
      return gimp_size_entry_get_help_widget (entry, n_fields - 1);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    }
  else if (GTK_IS_CONTAINER (widget))
    {
      GtkWidget *mnemonic_widget = NULL;
      GList     *children;
      GList     *list;

      children = gtk_container_get_children (GTK_CONTAINER (widget));

      for (list = children; list; list = g_list_next (list))
        {
          mnemonic_widget = find_mnemonic_widget (list->data, level + 1);

          if (mnemonic_widget)
            break;
        }

      g_list_free (children);

      return mnemonic_widget;
    }

  return NULL;
}

106 107 108 109 110 111 112 113 114 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 142 143 144 145 146 147 148 149 150 151
/**
 * gimp_grid_attach_aligned:
 * @grid:       The #GtkGrid the widgets will be attached to.
 * @left:       The column to start with.
 * @top:        The row to attach the widgets.
 * @label_text: The text for the #GtkLabel which will be attached left of
 *              the widget.
 * @xalign:     The horizontal alignment of the #GtkLabel.
 * @yalign:     The vertical alignment of the #GtkLabel.
 * @widget:     The #GtkWidget to attach right of the label.
 * @columns:    The number of columns the widget will use.
 *
 * Note that the @label_text can be %NULL and that the widget will be
 * attached starting at (@column + 1) in this case, too.
 *
 * Returns: The created #GtkLabel.
 **/
GtkWidget *
gimp_grid_attach_aligned (GtkGrid     *grid,
                          gint         left,
                          gint         top,
                          const gchar *label_text,
                          gfloat       xalign,
                          gfloat       yalign,
                          GtkWidget   *widget,
                          gint         columns)
{
  GtkWidget *label = NULL;

  if (label_text)
    {
      GtkWidget *mnemonic_widget;

      label = gtk_label_new_with_mnemonic (label_text);
      gtk_label_set_xalign (GTK_LABEL (label), xalign);
      gtk_label_set_yalign (GTK_LABEL (label), yalign);
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
      gtk_grid_attach (grid, label, left, top, 1, 1);
      gtk_widget_show (label);

      mnemonic_widget = find_mnemonic_widget (widget, 0);

      if (mnemonic_widget)
        gtk_label_set_mnemonic_widget (GTK_LABEL (label), mnemonic_widget);
    }

152
  gtk_widget_set_hexpand (widget, TRUE);
153 154 155 156 157 158
  gtk_grid_attach (grid, widget, left + 1, top, columns, 1);
  gtk_widget_show (widget);

  return label;
}

159 160 161 162 163 164 165 166 167 168
/**
 * gimp_label_set_attributes:
 * @label: a #GtkLabel
 * @...:   a list of PangoAttrType and value pairs terminated by -1.
 *
 * Sets Pango attributes on a #GtkLabel in a more convenient way than
 * gtk_label_set_attributes().
 *
 * This function is useful if you want to change the font attributes
 * of a #GtkLabel. This is an alternative to using PangoMarkup which
169
 * is slow to parse and awkward to handle in an i18n-friendly way.
170 171 172 173 174
 *
 * The attributes are set on the complete label, from start to end. If
 * you need to set attributes on part of the label, you will have to
 * use the PangoAttributes API directly.
 *
175
 * Since: 2.2
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
 **/
void
gimp_label_set_attributes (GtkLabel *label,
                           ...)
{
  PangoAttribute *attr  = NULL;
  PangoAttrList  *attrs;
  va_list         args;

  g_return_if_fail (GTK_IS_LABEL (label));

  attrs = pango_attr_list_new ();

  va_start (args, label);

  do
    {
      PangoAttrType attr_type = va_arg (args, PangoAttrType);

      if (attr_type == -1)
        attr_type = PANGO_ATTR_INVALID;

      switch (attr_type)
        {
        case PANGO_ATTR_LANGUAGE:
          attr = pango_attr_language_new (va_arg (args, PangoLanguage *));
          break;

        case PANGO_ATTR_FAMILY:
          attr = pango_attr_family_new (va_arg (args, const gchar *));
          break;

        case PANGO_ATTR_STYLE:
          attr = pango_attr_style_new (va_arg (args, PangoStyle));
          break;

        case PANGO_ATTR_WEIGHT:
          attr = pango_attr_weight_new (va_arg (args, PangoWeight));
          break;

        case PANGO_ATTR_VARIANT:
          attr = pango_attr_variant_new (va_arg (args, PangoVariant));
          break;

        case PANGO_ATTR_STRETCH:
          attr = pango_attr_stretch_new (va_arg (args, PangoStretch));
          break;

        case PANGO_ATTR_SIZE:
          attr = pango_attr_size_new (va_arg (args, gint));
          break;

        case PANGO_ATTR_FONT_DESC:
          attr = pango_attr_font_desc_new (va_arg (args,
                                                   const PangoFontDescription *));
          break;

        case PANGO_ATTR_FOREGROUND:
          {
            const PangoColor *color = va_arg (args, const PangoColor *);

            attr = pango_attr_foreground_new (color->red,
                                              color->green,
                                              color->blue);
          }
          break;

        case PANGO_ATTR_BACKGROUND:
          {
            const PangoColor *color = va_arg (args, const PangoColor *);

            attr = pango_attr_background_new (color->red,
                                              color->green,
                                              color->blue);
          }
          break;

        case PANGO_ATTR_UNDERLINE:
          attr = pango_attr_underline_new (va_arg (args, PangoUnderline));
          break;

        case PANGO_ATTR_STRIKETHROUGH:
          attr = pango_attr_strikethrough_new (va_arg (args, gboolean));
          break;

        case PANGO_ATTR_RISE:
          attr = pango_attr_rise_new (va_arg (args, gint));
          break;

        case PANGO_ATTR_SCALE:
          attr = pango_attr_scale_new (va_arg (args, gdouble));
          break;

        default:
          g_warning ("%s: invalid PangoAttribute type %d",
                     G_STRFUNC, attr_type);
        case PANGO_ATTR_INVALID:
          attr = NULL;
          break;
        }

      if (attr)
        {
          attr->start_index = 0;
          attr->end_index   = -1;
          pango_attr_list_insert (attrs, attr);
        }
    }
  while (attr);

  va_end (args);

  gtk_label_set_attributes (label, attrs);
  pango_attr_list_unref (attrs);
}

292
GdkMonitor *
293 294 295 296 297 298 299 300 301 302 303
gimp_widget_get_monitor (GtkWidget *widget)
{
  GdkWindow     *window;
  GtkAllocation  allocation;
  gint           x, y;

  g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);

  window = gtk_widget_get_window (widget);

  if (! window)
304
    return gimp_get_monitor_at_pointer ();
305 306 307 308 309 310 311 312 313 314 315 316 317

  gdk_window_get_origin (window, &x, &y);
  gtk_widget_get_allocation (widget, &allocation);

  if (! gtk_widget_get_has_window (widget))
    {
      x += allocation.x;
      y += allocation.y;
    }

  x += allocation.width  / 2;
  y += allocation.height / 2;

318
  return gdk_display_get_monitor_at_point (gdk_display_get_default (), x, y);
319 320
}

321 322
GdkMonitor *
gimp_get_monitor_at_pointer (void)
323
{
324 325 326
  GdkDisplay *display;
  GdkSeat    *seat;
  gint        x, y;
327

328 329
  display = gdk_display_get_default ();
  seat = gdk_display_get_default_seat (display);
330

331 332
  gdk_device_get_position (gdk_seat_get_pointer (seat),
                           NULL, &x, &y);
333

334
  return gdk_display_get_monitor_at_point (display, x, y);
335 336
}

337 338 339 340
typedef void (* MonitorChangedCallback) (GtkWidget *, gpointer);

typedef struct
{
341 342
  GtkWidget  *widget;
  GdkMonitor *monitor;
343 344 345 346 347 348 349 350 351 352

  MonitorChangedCallback callback;
  gpointer               user_data;
} TrackMonitorData;

static gboolean
track_monitor_configure_event (GtkWidget        *toplevel,
                               GdkEvent         *event,
                               TrackMonitorData *track_data)
{
353
  GdkMonitor *monitor = gimp_widget_get_monitor (toplevel);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

  if (monitor != track_data->monitor)
    {
      track_data->monitor = monitor;

      track_data->callback (track_data->widget, track_data->user_data);
    }

  return FALSE;
}

static void
track_monitor_hierarchy_changed (GtkWidget        *widget,
                                 GtkWidget        *previous_toplevel,
                                 TrackMonitorData *track_data)
{
  GtkWidget *toplevel;

  if (previous_toplevel)
    {
      g_signal_handlers_disconnect_by_func (previous_toplevel,
                                            track_monitor_configure_event,
                                            track_data);
    }

  toplevel = gtk_widget_get_toplevel (widget);

  if (GTK_IS_WINDOW (toplevel))
    {
383 384
      GClosure   *closure;
      GdkMonitor *monitor;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

      closure = g_cclosure_new (G_CALLBACK (track_monitor_configure_event),
                                track_data, NULL);
      g_object_watch_closure (G_OBJECT (widget), closure);
      g_signal_connect_closure (toplevel, "configure-event", closure, FALSE);

      monitor = gimp_widget_get_monitor (toplevel);

      if (monitor != track_data->monitor)
        {
          track_data->monitor = monitor;

          track_data->callback (track_data->widget, track_data->user_data);
        }
    }
}

/**
 * gimp_widget_track_monitor:
 * @widget:                   a #GtkWidget
 * @monitor_changed_callback: the callback when @widget's monitor changes
 * @user_data:                data passed to @monitor_changed_callback
 *
 * This function behaves as if #GtkWidget had a signal
 *
 * GtkWidget::monitor_changed(GtkWidget *widget, gpointer user_data)
 *
 * That is emitted whenever @widget's toplevel window is moved from
 * one monitor to another. This function automatically connects to
 * the right toplevel #GtkWindow, even across moving @widget between
 * toplevel windows.
 *
 * Note that this function tracks the toplevel, not @widget itself, so
 * all a window's widgets are always considered to be on the same
 * monitor. This is because this function is mainly used for fetching
 * the new monitor's color profile, and it makes little sense to use
 * different profiles for the widgets of one window.
 *
 * Since: 2.10
 **/
void
gimp_widget_track_monitor (GtkWidget *widget,
                           GCallback  monitor_changed_callback,
                           gpointer   user_data)
{
  TrackMonitorData *track_data;
  GtkWidget        *toplevel;

  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (monitor_changed_callback != NULL);

  track_data = g_new0 (TrackMonitorData, 1);

  track_data->widget    = widget;
  track_data->callback  = (MonitorChangedCallback) monitor_changed_callback;
  track_data->user_data = user_data;

  g_object_weak_ref (G_OBJECT (widget), (GWeakNotify) g_free, track_data);

  g_signal_connect (widget, "hierarchy-changed",
                    G_CALLBACK (track_monitor_hierarchy_changed),
                    track_data);

  toplevel = gtk_widget_get_toplevel (widget);

  if (GTK_IS_WINDOW (toplevel))
    track_monitor_hierarchy_changed (widget, NULL, track_data);
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467
static gint
monitor_number (GdkMonitor *monitor)
{
  GdkDisplay *display    = gdk_monitor_get_display (monitor);
  gint        n_monitors = gdk_display_get_n_monitors (display);
  gint        i;

  for (i = 0; i < n_monitors; i++)
    if (gdk_display_get_monitor (display, i) == monitor)
      return i;

  return 0;
}

468
/**
469 470
 * gimp_monitor_get_color_profile:
 * @monitor: a #GdkMonitor
471
 *
472 473
 * This function returns the #GimpColorProfile of @monitor
 * or %NULL if there is no profile configured.
474
 *
475
 * Since: 3.0
476 477 478
 *
 * Return value: the monitor's #GimpColorProfile, or %NULL.
 **/
479
GimpColorProfile *
480
gimp_monitor_get_color_profile (GdkMonitor *monitor)
481
{
482 483
  GdkDisplay       *display;
  GdkScreen        *screen;
484
  GimpColorProfile *profile = NULL;
485

486 487 488
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);

  display = gdk_monitor_get_display (monitor);
489 490 491 492 493 494

#if defined GDK_WINDOWING_X11
  {
    GdkAtom  type    = GDK_NONE;
    gint     format  = 0;
    gint     nitems  = 0;
495
    gint     number;
496 497 498
    gchar   *atom_name;
    guchar  *data    = NULL;

499 500 501 502
    number = monitor_number (monitor);

    if (number > 0)
      atom_name = g_strdup_printf ("_ICC_PROFILE_%d", number);
503 504 505
    else
      atom_name = g_strdup ("_ICC_PROFILE");

506 507
    screen = gdk_display_get_default_screen (display);

508 509 510 511 512 513
    if (gdk_property_get (gdk_screen_get_root_window (screen),
                          gdk_atom_intern (atom_name, FALSE),
                          GDK_NONE,
                          0, 64 * 1024 * 1024, FALSE,
                          &type, &format, &nitems, &data) && nitems > 0)
      {
514 515
        profile = gimp_color_profile_new_from_icc_profile (data, nitems,
                                                           NULL);
516 517 518 519 520 521 522
        g_free (data);
      }

    g_free (atom_name);
  }
#elif defined GDK_WINDOWING_QUARTZ
  {
523
    CGColorSpaceRef space = NULL;
524

525
    space = CGDisplayCopyColorSpace (monitor_number (monitor));
526

527
    if (space)
528 529 530
      {
        CFDataRef data;

531
        data = CGColorSpaceCopyICCData (space);
532 533 534 535 536 537 538 539 540 541 542 543

        if (data)
          {
            UInt8 *buffer = g_malloc (CFDataGetLength (data));

            /* We cannot use CFDataGetBytesPtr(), because that returns
             * a const pointer where cmsOpenProfileFromMem wants a
             * non-const pointer.
             */
            CFDataGetBytes (data, CFRangeMake (0, CFDataGetLength (data)),
                            buffer);

544
            profile = gimp_color_profile_new_from_icc_profile (buffer,
545 546
                                                               CFDataGetLength (data),
                                                               NULL);
547 548 549 550

            g_free (buffer);
            CFRelease (data);
          }
551 552

        CFRelease (space);
553 554 555 556 557 558 559 560 561 562 563
      }
  }
#elif defined G_OS_WIN32
  {
    HDC hdc = GetDC (NULL);

    if (hdc)
      {
        gchar  *path;
        gint32  len = 0;

564
        GetICMProfile (hdc, (LPDWORD) &len, NULL);
565 566
        path = g_new (gchar, len);

567
        if (GetICMProfile (hdc, (LPDWORD) &len, path))
568 569 570
          {
            GFile *file = g_file_new_for_path (path);

571
            profile = gimp_color_profile_new_from_file (file, NULL);
572 573 574 575 576 577 578 579 580 581 582
            g_object_unref (file);
          }

        g_free (path);
        ReleaseDC (NULL, hdc);
      }
  }
#endif

  return profile;
}
583

584 585 586
GimpColorProfile *
gimp_widget_get_color_profile (GtkWidget *widget)
{
587
  GdkMonitor *monitor;
588 589 590 591 592 593 594 595 596

  g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);

  if (widget)
    {
      monitor = gimp_widget_get_monitor (widget);
    }
  else
    {
597
      monitor = gdk_display_get_monitor (gdk_display_get_default (), 0);
598 599
    }

600
  return gimp_monitor_get_color_profile (monitor);
601 602
}

603
static GimpColorProfile *
604 605 606
get_display_profile (GtkWidget       *widget,
                     GimpColorConfig *config)
{
607
  GimpColorProfile *profile = NULL;
608

609
  if (gimp_color_config_get_display_profile_from_gdk (config))
610 611
    /* get the toplevel's profile so all a window's colors look the same */
    profile = gimp_widget_get_color_profile (gtk_widget_get_toplevel (widget));
612 613

  if (! profile)
614
    profile = gimp_color_config_get_display_color_profile (config, NULL);
615

616
  if (! profile)
617
    profile = gimp_color_profile_new_rgb_srgb ();
618

619 620 621
  return profile;
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 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
typedef struct _TransformCache TransformCache;

struct _TransformCache
{
  GimpColorTransform *transform;

  GimpColorConfig    *config;
  GimpColorProfile   *src_profile;
  const Babl         *src_format;
  GimpColorProfile   *dest_profile;
  const Babl         *dest_format;
  GimpColorProfile   *proof_profile;

  gulong              notify_id;
};

static GList    *transform_caches = NULL;
static gboolean  debug_cache      = FALSE;

static gboolean
profiles_equal (GimpColorProfile *profile1,
                GimpColorProfile *profile2)
{
  return ((profile1 == NULL && profile2 == NULL) ||
          (profile1 != NULL && profile2 != NULL &&
           gimp_color_profile_is_equal (profile1, profile2)));
}

static TransformCache *
transform_cache_get (GimpColorConfig  *config,
                     GimpColorProfile *src_profile,
                     const Babl       *src_format,
                     GimpColorProfile *dest_profile,
                     const Babl       *dest_format,
                     GimpColorProfile *proof_profile)
{
  GList *list;

  for (list = transform_caches; list; list = g_list_next (list))
    {
      TransformCache *cache = list->data;

      if (config      == cache->config                        &&
          src_format  == cache->src_format                    &&
          dest_format == cache->dest_format                   &&
          profiles_equal (src_profile,   cache->src_profile)  &&
          profiles_equal (dest_profile,  cache->dest_profile) &&
          profiles_equal (proof_profile, cache->proof_profile))
        {
          if (debug_cache)
            g_printerr ("found cache %p\n", cache);

          return cache;
        }
    }

  return NULL;
}

static void
transform_cache_config_notify (GObject          *config,
                               const GParamSpec *pspec,
                               TransformCache   *cache)
{
  transform_caches = g_list_remove (transform_caches, cache);

  g_signal_handler_disconnect (config, cache->notify_id);

690 691 692
  if (cache->transform)
    g_object_unref (cache->transform);

693 694 695 696 697 698 699 700 701 702 703 704
  g_object_unref (cache->src_profile);
  g_object_unref (cache->dest_profile);

  if (cache->proof_profile)
    g_object_unref (cache->proof_profile);

  g_free (cache);

  if (debug_cache)
    g_printerr ("deleted cache %p\n", cache);
}

705 706 707 708 709 710
GimpColorTransform *
gimp_widget_get_color_transform (GtkWidget        *widget,
                                 GimpColorConfig  *config,
                                 GimpColorProfile *src_profile,
                                 const Babl       *src_format,
                                 const Babl       *dest_format)
711
{
712
  static gboolean     initialized   = FALSE;
713 714
  GimpColorProfile   *dest_profile  = NULL;
  GimpColorProfile   *proof_profile = NULL;
715
  TransformCache     *cache;
716 717 718

  g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (GIMP_IS_COLOR_CONFIG (config), NULL);
719
  g_return_val_if_fail (GIMP_IS_COLOR_PROFILE (src_profile), NULL);
720 721 722
  g_return_val_if_fail (src_format != NULL, NULL);
  g_return_val_if_fail (dest_format != NULL, NULL);

723 724 725 726 727 728 729
  if (G_UNLIKELY (! initialized))
    {
      initialized = TRUE;

      debug_cache = g_getenv ("GIMP_DEBUG_TRANSFORM_CACHE") != NULL;
    }

730
  switch (gimp_color_config_get_mode (config))
731 732 733 734 735
    {
    case GIMP_COLOR_MANAGEMENT_OFF:
      return NULL;

    case GIMP_COLOR_MANAGEMENT_SOFTPROOF:
736 737
      proof_profile = gimp_color_config_get_simulation_color_profile (config,
                                                                      NULL);
738 739 740 741 742 743 744
      /*  fallthru  */

    case GIMP_COLOR_MANAGEMENT_DISPLAY:
      dest_profile = get_display_profile (widget, config);
      break;
    }

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 786 787 788 789 790 791 792
  cache = transform_cache_get (config,
                               src_profile,
                               src_format,
                               dest_profile,
                               dest_format,
                               proof_profile);

  if (cache)
    {
      g_object_unref (dest_profile);

      if (proof_profile)
        g_object_unref (proof_profile);

      if (cache->transform)
        return g_object_ref (cache->transform);

      return NULL;
    }

  if (! proof_profile &&
      gimp_color_profile_is_equal (src_profile, dest_profile))
    {
      g_object_unref (dest_profile);

      return NULL;
    }

  cache = g_new0 (TransformCache, 1);

  if (debug_cache)
    g_printerr ("creating cache %p\n", cache);

  cache->config        = g_object_ref (config);
  cache->src_profile   = g_object_ref (src_profile);
  cache->src_format    = src_format;
  cache->dest_profile  = dest_profile;
  cache->dest_format   = dest_format;
  cache->proof_profile = proof_profile;

  cache->notify_id =
    g_signal_connect (cache->config, "notify",
                      G_CALLBACK (transform_cache_config_notify),
                      cache);

  transform_caches = g_list_prepend (transform_caches, cache);

  if (cache->proof_profile)
793
    {
794
      GimpColorTransformFlags flags = 0;
795

796
      if (gimp_color_config_get_simulation_bpc (config))
797 798
        flags |= GIMP_COLOR_TRANSFORM_FLAGS_BLACK_POINT_COMPENSATION;

799
      if (! gimp_color_config_get_simulation_optimize (config))
800
        flags |= GIMP_COLOR_TRANSFORM_FLAGS_NOOPTIMIZE;
801

802
      if (gimp_color_config_get_simulation_gamut_check (config))
803
        {
804
          GimpRGB         color;
805 806
          cmsUInt16Number alarmCodes[cmsMAXCHANNELS] = { 0, };
          guchar          r, g, b;
807

808
          flags |= GIMP_COLOR_TRANSFORM_FLAGS_GAMUT_CHECK;
809

810 811
          gimp_color_config_get_out_of_gamut_color (config, &color);
          gimp_rgb_get_uchar (&color, &r, &g, &b);
812 813 814 815 816 817 818 819

          alarmCodes[0] = (cmsUInt16Number) r * 256;
          alarmCodes[1] = (cmsUInt16Number) g * 256;
          alarmCodes[2] = (cmsUInt16Number) b * 256;

          cmsSetAlarmCodes (alarmCodes);
        }

820 821 822 823 824 825
      cache->transform =
        gimp_color_transform_new_proofing (cache->src_profile,
                                           cache->src_format,
                                           cache->dest_profile,
                                           cache->dest_format,
                                           cache->proof_profile,
826 827
                                           gimp_color_config_get_simulation_intent (config),
                                           gimp_color_config_get_display_intent (config),
828
                                           flags);
829
    }
830
  else
831
    {
832
      GimpColorTransformFlags flags = 0;
833

834
      if (gimp_color_config_get_display_bpc (config))
835 836
        flags |= GIMP_COLOR_TRANSFORM_FLAGS_BLACK_POINT_COMPENSATION;

837
      if (! gimp_color_config_get_display_optimize (config))
838
        flags |= GIMP_COLOR_TRANSFORM_FLAGS_NOOPTIMIZE;
839

840 841 842 843 844
      cache->transform =
        gimp_color_transform_new (cache->src_profile,
                                  cache->src_format,
                                  cache->dest_profile,
                                  cache->dest_format,
845
                                  gimp_color_config_get_display_intent (config),
846
                                  flags);
847 848
    }

849 850
  if (cache->transform)
    return g_object_ref (cache->transform);
851

852
  return NULL;
853
}