home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / gimpcontext.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  45.2 KB  |  1,741 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * gimpcontext.c: Copyright (C) 1999 Michael Natterer <mitch@gimp.org>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  */
  20.  
  21. #include "config.h"
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include "apptypes.h"
  26.  
  27. #include "appenv.h"
  28. #include "gimpbrush.h"
  29. #include "gimpbrushlist.h"
  30. #include "gimpcontext.h"
  31. #include "gimprc.h"
  32. #include "gimpsignal.h"
  33. #include "gradient_header.h"
  34. #include "patterns.h"
  35.  
  36. #define context_return_if_fail(context) \
  37.         g_return_if_fail ((context) != NULL); \
  38.         g_return_if_fail (GIMP_IS_CONTEXT (context))
  39.  
  40. #define context_return_val_if_fail(context,val) \
  41.         g_return_val_if_fail ((context) != NULL, (val)); \
  42.         g_return_val_if_fail (GIMP_IS_CONTEXT (context), (val))
  43.  
  44. #define context_check_current(context) \
  45.         ((context) = (context) ? (context) : current_context)
  46.  
  47. #define context_find_defined(context,arg_mask) \
  48.         while (!(((context)->defined_args) & arg_mask) && (context)->parent) \
  49.           (context) = (context)->parent
  50.  
  51. typedef void (* GimpContextCopyArgFunc) (GimpContext *src, GimpContext *dest);
  52.  
  53. /*  local function prototypes  */
  54.  
  55. /*  image  */
  56. static void gimp_context_real_set_image      (GimpContext      *context,
  57.                           GimpImage        *image);
  58. static void gimp_context_copy_image          (GimpContext      *src,
  59.                           GimpContext      *dest);
  60.  
  61. /*  display  */
  62. static void gimp_context_real_set_display    (GimpContext      *context,
  63.                           GDisplay         *display);
  64. static void gimp_context_copy_display        (GimpContext      *src,
  65.                           GimpContext      *dest);
  66.  
  67. /*  tool  */
  68. static void gimp_context_real_set_tool       (GimpContext      *context,
  69.                           ToolType          tool);
  70. static void gimp_context_copy_tool           (GimpContext      *src,
  71.                           GimpContext      *dest);
  72.  
  73. /*  foreground  */
  74. static void gimp_context_real_set_foreground (GimpContext      *context,
  75.                           gint              r,
  76.                           gint              g,
  77.                           gint              b);
  78. static void gimp_context_copy_foreground     (GimpContext      *src,
  79.                           GimpContext      *dest);
  80.  
  81. /*  background  */
  82. static void gimp_context_real_set_background (GimpContext      *context,
  83.                           gint              r,
  84.                           gint              g,
  85.                           gint              b);
  86. static void gimp_context_copy_background     (GimpContext      *src,
  87.                           GimpContext      *dest);
  88.  
  89. /*  opacity  */
  90. static void gimp_context_real_set_opacity    (GimpContext      *context,
  91.                           gdouble           opacity);
  92. static void gimp_context_copy_opacity        (GimpContext      *src,
  93.                           GimpContext      *dest);
  94.  
  95. /*  paint mode  */
  96. static void gimp_context_real_set_paint_mode (GimpContext      *context,
  97.                           LayerModeEffects  paint_mode);
  98. static void gimp_context_copy_paint_mode     (GimpContext      *src,
  99.                           GimpContext      *dest);
  100.  
  101. /*  brush  */
  102. static void gimp_context_real_set_brush      (GimpContext      *context,
  103.                           GimpBrush        *brush);
  104. static void gimp_context_copy_brush          (GimpContext      *src,
  105.                           GimpContext      *dest);
  106.  
  107. /*  pattern  */
  108. static void gimp_context_real_set_pattern    (GimpContext      *context,
  109.                           GPattern         *pattern);
  110. static void gimp_context_copy_pattern        (GimpContext      *src,
  111.                           GimpContext      *dest);
  112.  
  113. /*  gradient  */
  114. static void gimp_context_real_set_gradient   (GimpContext      *context,
  115.                           gradient_t       *gradient);
  116. static void gimp_context_copy_gradient       (GimpContext      *src,
  117.                           GimpContext      *dest);
  118.  
  119. /*  arguments  */
  120.  
  121. enum
  122. {
  123.   ARG_0,
  124.   ARG_IMAGE,
  125.   ARG_DISPLAY,
  126.   ARG_TOOL,
  127.   ARG_FOREGROUND,
  128.   ARG_BACKGROUND,
  129.   ARG_OPACITY,
  130.   ARG_PAINT_MODE,
  131.   ARG_BRUSH,
  132.   ARG_PATTERN,
  133.   ARG_GRADIENT
  134. };
  135.  
  136. static gchar *gimp_context_arg_names[] =
  137. {
  138.   "GimpContext::image",
  139.   "GimpContext::display",
  140.   "GimpContext::tool",
  141.   "GimpContext::foreground",
  142.   "GimpContext::background",
  143.   "GimpContext::opacity",
  144.   "GimpContext::paint_mode",
  145.   "GimpContext::brush",
  146.   "GimpContext::pattern",
  147.   "GimpContext::gradient"
  148. };
  149.  
  150. static GimpContextCopyArgFunc gimp_context_copy_arg_funcs[] =
  151. {
  152.   gimp_context_copy_image,
  153.   gimp_context_copy_display,
  154.   gimp_context_copy_tool,
  155.   gimp_context_copy_foreground,
  156.   gimp_context_copy_background,
  157.   gimp_context_copy_opacity,
  158.   gimp_context_copy_paint_mode,
  159.   gimp_context_copy_brush,
  160.   gimp_context_copy_pattern,
  161.   gimp_context_copy_gradient
  162. };
  163.  
  164. /*  signals  */
  165.  
  166. enum
  167. {
  168.   IMAGE_CHANGED,
  169.   DISPLAY_CHANGED,
  170.   TOOL_CHANGED,
  171.   FOREGROUND_CHANGED,
  172.   BACKGROUND_CHANGED,
  173.   OPACITY_CHANGED,
  174.   PAINT_MODE_CHANGED,
  175.   BRUSH_CHANGED,
  176.   PATTERN_CHANGED,
  177.   GRADIENT_CHANGED,
  178.   LAST_SIGNAL
  179. };
  180.  
  181. static guint gimp_context_signals[LAST_SIGNAL] = { 0 };
  182.  
  183. static gchar *gimp_context_signal_names[] =
  184. {
  185.   "image_changed",
  186.   "display_changed",
  187.   "tool_changed",
  188.   "foreground_changed",
  189.   "background_changed",
  190.   "opacity_changed",
  191.   "paint_mode_changed",
  192.   "brush_changed",
  193.   "pattern_changed",
  194.   "gradient_changed"
  195. };
  196.  
  197. static GtkSignalFunc gimp_context_signal_handlers[] =
  198. {
  199.   gimp_context_real_set_image,
  200.   gimp_context_real_set_display,
  201.   gimp_context_real_set_tool,
  202.   gimp_context_real_set_foreground,
  203.   gimp_context_real_set_background,
  204.   gimp_context_real_set_opacity,
  205.   gimp_context_real_set_paint_mode,
  206.   gimp_context_real_set_brush,
  207.   gimp_context_real_set_pattern,
  208.   gimp_context_real_set_gradient
  209. };
  210.  
  211. static GimpObjectClass * parent_class = NULL;
  212.  
  213. /*  the currently active context  */
  214. static GimpContext *current_context  = NULL;
  215.  
  216. /*  the context user by the interface  */
  217. static GimpContext *user_context     = NULL;
  218.  
  219. /*  the default context which is initialized from gimprc  */
  220. static GimpContext *default_context  = NULL;
  221.  
  222. /*  the hardcoded standard context  */
  223. static GimpContext *standard_context = NULL;
  224.  
  225. /*  the list of all contexts  */
  226. static GSList      *context_list     = NULL;
  227.  
  228. /*****************************************************************************/
  229. /*  private functions  *******************************************************/
  230.  
  231. static void
  232. gimp_context_set_arg (GtkObject *object,
  233.               GtkArg    *arg,
  234.               guint      arg_id)
  235. {
  236.   GimpContext *context;
  237.  
  238.   context = GIMP_CONTEXT (object);
  239.  
  240.   switch (arg_id)
  241.     {
  242.     case ARG_IMAGE:
  243.       gimp_context_set_image (context, GTK_VALUE_POINTER (*arg));
  244.       break;
  245.     case ARG_DISPLAY:
  246.       gimp_context_set_display (context, GTK_VALUE_POINTER (*arg));
  247.       break;
  248.     case ARG_TOOL:
  249.       gimp_context_set_tool (context, GTK_VALUE_INT (*arg));
  250.       break;
  251.     case ARG_FOREGROUND:
  252.       {
  253.     guchar *col = GTK_VALUE_POINTER (*arg);
  254.     gimp_context_set_foreground (context, col[0], col[1], col[2]);
  255.       }
  256.       break;
  257.     case ARG_BACKGROUND:
  258.       {
  259.     guchar *col = GTK_VALUE_POINTER (*arg);
  260.     gimp_context_set_background (context, col[0], col[1], col[2]);
  261.       }
  262.       break;
  263.     case ARG_OPACITY:
  264.       gimp_context_set_opacity (context, GTK_VALUE_DOUBLE (*arg));
  265.       break;
  266.     case ARG_PAINT_MODE:
  267.       gimp_context_set_paint_mode (context, GTK_VALUE_INT (*arg));
  268.       break;
  269.     case ARG_BRUSH:
  270.       gimp_context_set_brush (context, GTK_VALUE_POINTER (*arg));
  271.       break;
  272.     case ARG_PATTERN:
  273.       gimp_context_set_pattern (context, GTK_VALUE_POINTER (*arg));
  274.       break;
  275.     case ARG_GRADIENT:
  276.       gimp_context_set_gradient (context, GTK_VALUE_POINTER (*arg));
  277.       break;
  278.     default:
  279.       break;
  280.     }
  281. }
  282.  
  283. static void
  284. gimp_context_get_arg (GtkObject *object,
  285.               GtkArg    *arg,
  286.               guint      arg_id)
  287. {
  288.   GimpContext *context;
  289.  
  290.   context = GIMP_CONTEXT (object);
  291.  
  292.   switch (arg_id)
  293.     {
  294.     case ARG_IMAGE:
  295.       GTK_VALUE_POINTER (*arg) = gimp_context_get_image (context);
  296.       break;
  297.     case ARG_DISPLAY:
  298.       GTK_VALUE_POINTER (*arg) = gimp_context_get_display (context);
  299.       break;
  300.     case ARG_TOOL:
  301.       GTK_VALUE_INT (*arg) = gimp_context_get_tool (context);
  302.       break;
  303.     case ARG_FOREGROUND:
  304.       {
  305.     guchar *col = GTK_VALUE_POINTER (*arg);
  306.     gimp_context_get_foreground (context, &col[0], &col[1], &col[2]);
  307.       }
  308.       break;
  309.     case ARG_BACKGROUND:
  310.       {
  311.     guchar *col = GTK_VALUE_POINTER (*arg);
  312.     gimp_context_get_background (context, &col[0], &col[1], &col[2]);
  313.       }
  314.       break;
  315.     case ARG_OPACITY:
  316.       GTK_VALUE_DOUBLE (*arg) = gimp_context_get_opacity (context);
  317.       break;
  318.     case ARG_PAINT_MODE:
  319.       GTK_VALUE_INT (*arg) = gimp_context_get_paint_mode (context);
  320.       break;
  321.     case ARG_BRUSH:
  322.       GTK_VALUE_POINTER (*arg) = gimp_context_get_brush (context);
  323.       break;
  324.     case ARG_PATTERN:
  325.       GTK_VALUE_POINTER (*arg) = gimp_context_get_pattern (context);
  326.       break;
  327.     case ARG_GRADIENT:
  328.       GTK_VALUE_POINTER (*arg) = gimp_context_get_gradient (context);
  329.       break;
  330.     default:
  331.       arg->type = GTK_TYPE_INVALID;
  332.       break;
  333.     }
  334. }
  335.  
  336. static void
  337. gimp_context_destroy (GtkObject *object)
  338. {
  339.   GimpContext *context;
  340.  
  341.   context = GIMP_CONTEXT (object);
  342.  
  343.   if (context->parent)
  344.     gimp_context_unset_parent (context);
  345.  
  346.   if (context->name)
  347.     {
  348.       g_free (context->name);
  349.       context->name = NULL;
  350.     }
  351.  
  352.   if (context->image)
  353.     gtk_signal_disconnect_by_data (GTK_OBJECT (image_context), context);
  354.  
  355.   if (context->display)
  356.     gtk_signal_disconnect_by_data (GTK_OBJECT (context->display->shell),
  357.                    context);
  358.  
  359.   if (context->brush)
  360.     {
  361.       gtk_signal_disconnect_by_data (GTK_OBJECT (context->brush), context);
  362.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), context);
  363.     }
  364.  
  365.   if (context->brush_name)
  366.     {
  367.       g_free (context->brush_name);
  368.       context->brush_name = NULL;
  369.     }
  370.  
  371.   if (context->pattern_name)
  372.     {
  373.       g_free (context->pattern_name);
  374.       context->pattern_name = NULL;
  375.     }
  376.  
  377.   if (context->gradient_name)
  378.     {
  379.       g_free (context->gradient_name);
  380.       context->gradient_name = NULL;
  381.     }
  382.  
  383.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  384.     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
  385.  
  386.   context_list = g_slist_remove (context_list, context);
  387. }
  388.  
  389. static void
  390. gimp_context_class_init (GimpContextClass *klass)
  391. {
  392.   GtkObjectClass *object_class;
  393.  
  394.   object_class = GTK_OBJECT_CLASS (klass);
  395.  
  396.   gtk_object_add_arg_type (gimp_context_arg_names[IMAGE_CHANGED],
  397.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_IMAGE);
  398.   gtk_object_add_arg_type (gimp_context_arg_names[DISPLAY_CHANGED],
  399.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_DISPLAY);
  400.   gtk_object_add_arg_type (gimp_context_arg_names[TOOL_CHANGED],
  401.                GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_TOOL);
  402.   gtk_object_add_arg_type (gimp_context_arg_names[FOREGROUND_CHANGED],
  403.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_FOREGROUND);
  404.   gtk_object_add_arg_type (gimp_context_arg_names[BACKGROUND_CHANGED],
  405.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_BACKGROUND);
  406.   gtk_object_add_arg_type (gimp_context_arg_names[OPACITY_CHANGED],
  407.                GTK_TYPE_DOUBLE, GTK_ARG_READWRITE, ARG_OPACITY);
  408.   gtk_object_add_arg_type (gimp_context_arg_names[PAINT_MODE_CHANGED],
  409.                GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_PAINT_MODE);
  410.   gtk_object_add_arg_type (gimp_context_arg_names[BRUSH_CHANGED],
  411.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_BRUSH);
  412.   gtk_object_add_arg_type (gimp_context_arg_names[PATTERN_CHANGED],
  413.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_PATTERN);
  414.   gtk_object_add_arg_type (gimp_context_arg_names[GRADIENT_CHANGED],
  415.                GTK_TYPE_POINTER, GTK_ARG_READWRITE, ARG_GRADIENT);
  416.  
  417.   parent_class = gtk_type_class (gimp_object_get_type ());
  418.  
  419.   gimp_context_signals[IMAGE_CHANGED] =
  420.     gimp_signal_new (gimp_context_signal_names[IMAGE_CHANGED],
  421.              GTK_RUN_FIRST,
  422.              object_class->type,
  423.              GTK_SIGNAL_OFFSET (GimpContextClass,
  424.                     image_changed),
  425.              gimp_sigtype_pointer);
  426.  
  427.   gimp_context_signals[DISPLAY_CHANGED] =
  428.     gimp_signal_new (gimp_context_signal_names[DISPLAY_CHANGED],
  429.              GTK_RUN_FIRST,
  430.              object_class->type,
  431.              GTK_SIGNAL_OFFSET (GimpContextClass,
  432.                     display_changed),
  433.              gimp_sigtype_pointer);
  434.  
  435.   gimp_context_signals[TOOL_CHANGED] =
  436.     gimp_signal_new (gimp_context_signal_names[TOOL_CHANGED],
  437.              GTK_RUN_FIRST,
  438.              object_class->type,
  439.              GTK_SIGNAL_OFFSET (GimpContextClass,
  440.                     tool_changed),
  441.              gimp_sigtype_int);
  442.  
  443.   gimp_context_signals[FOREGROUND_CHANGED] =
  444.     gimp_signal_new (gimp_context_signal_names[FOREGROUND_CHANGED],
  445.              GTK_RUN_FIRST,
  446.              object_class->type,
  447.              GTK_SIGNAL_OFFSET (GimpContextClass,
  448.                     foreground_changed),
  449.              gimp_sigtype_int_int_int);
  450.  
  451.   gimp_context_signals[BACKGROUND_CHANGED] =
  452.     gimp_signal_new (gimp_context_signal_names[BACKGROUND_CHANGED],
  453.              GTK_RUN_FIRST,
  454.              object_class->type,
  455.              GTK_SIGNAL_OFFSET (GimpContextClass,
  456.                     background_changed),
  457.              gimp_sigtype_int_int_int);
  458.  
  459.   gimp_context_signals[OPACITY_CHANGED] =
  460.     gimp_signal_new (gimp_context_signal_names[OPACITY_CHANGED],
  461.              GTK_RUN_FIRST,
  462.              object_class->type,
  463.              GTK_SIGNAL_OFFSET (GimpContextClass,
  464.                     opacity_changed),
  465.              gimp_sigtype_double);
  466.  
  467.   gimp_context_signals[PAINT_MODE_CHANGED] =
  468.     gimp_signal_new (gimp_context_signal_names[PAINT_MODE_CHANGED],
  469.              GTK_RUN_FIRST,
  470.              object_class->type,
  471.              GTK_SIGNAL_OFFSET (GimpContextClass,
  472.                     paint_mode_changed),
  473.              gimp_sigtype_int);
  474.  
  475.   gimp_context_signals[BRUSH_CHANGED] =
  476.     gimp_signal_new (gimp_context_signal_names[BRUSH_CHANGED],
  477.              GTK_RUN_FIRST,
  478.              object_class->type,
  479.              GTK_SIGNAL_OFFSET (GimpContextClass,
  480.                     brush_changed),
  481.              gimp_sigtype_pointer);
  482.  
  483.   gimp_context_signals[PATTERN_CHANGED] =
  484.     gimp_signal_new (gimp_context_signal_names[PATTERN_CHANGED],
  485.              GTK_RUN_FIRST,
  486.              object_class->type,
  487.              GTK_SIGNAL_OFFSET (GimpContextClass,
  488.                     pattern_changed),
  489.              gimp_sigtype_pointer);
  490.  
  491.   gimp_context_signals[GRADIENT_CHANGED] =
  492.     gimp_signal_new (gimp_context_signal_names[GRADIENT_CHANGED],
  493.              GTK_RUN_FIRST,
  494.              object_class->type,
  495.              GTK_SIGNAL_OFFSET (GimpContextClass,
  496.                     gradient_changed),
  497.              gimp_sigtype_pointer);
  498.  
  499.   gtk_object_class_add_signals (object_class, gimp_context_signals,
  500.                 LAST_SIGNAL);
  501.  
  502.   object_class->set_arg = gimp_context_set_arg;
  503.   object_class->get_arg = gimp_context_get_arg;
  504.   object_class->destroy = gimp_context_destroy;
  505.  
  506.   klass->image_changed      = NULL;
  507.   klass->display_changed    = NULL;
  508.   klass->tool_changed       = NULL;
  509.   klass->foreground_changed = NULL;
  510.   klass->background_changed = NULL;
  511.   klass->opacity_changed    = NULL;
  512.   klass->paint_mode_changed = NULL;
  513.   klass->brush_changed      = NULL;
  514.   klass->pattern_changed    = NULL;
  515.   klass->gradient_changed   = NULL;
  516. }
  517.  
  518. static void
  519. gimp_context_init (GimpContext *context)
  520. {
  521.   context->name   = NULL;
  522.   context->parent = NULL;
  523.  
  524.   context->defined_args = GIMP_CONTEXT_ALL_ARGS_MASK;
  525.  
  526.   context->image   = NULL;
  527.   context->display = NULL;
  528.  
  529.   context->tool = RECT_SELECT;
  530.  
  531.   context->foreground[0] = 0;
  532.   context->foreground[1] = 0;
  533.   context->foreground[2] = 0;
  534.  
  535.   context->background[0] = 255;
  536.   context->background[1] = 255;
  537.   context->background[2] = 255;
  538.  
  539.   context->opacity    = 1.0;
  540.   context->paint_mode = NORMAL_MODE;
  541.  
  542.   context->brush      = NULL;
  543.   context->brush_name = NULL;
  544.  
  545.   context->pattern      = NULL;
  546.   context->pattern_name = NULL;
  547.  
  548.   context->gradient      = NULL;
  549.   context->gradient_name = NULL;
  550.  
  551.   context_list = g_slist_prepend (context_list, context);
  552. }
  553.  
  554. /*****************************************************************************/
  555. /*  public functions  ********************************************************/
  556.  
  557. GtkType
  558. gimp_context_get_type (void)
  559. {
  560.   static GtkType context_type = 0;
  561.  
  562.   if (! context_type)
  563.     {
  564.       GtkTypeInfo context_info =
  565.       {
  566.     "GimpContext",
  567.     sizeof (GimpContext),
  568.     sizeof (GimpContextClass),
  569.     (GtkClassInitFunc) gimp_context_class_init,
  570.     (GtkObjectInitFunc) gimp_context_init,
  571.     /* reserved_1 */ NULL,
  572.     /* reserved_2 */ NULL,
  573.     (GtkClassInitFunc) NULL
  574.       };
  575.  
  576.       context_type = gtk_type_unique (gimp_object_get_type (), &context_info);
  577.     }
  578.  
  579.   return context_type;
  580. }
  581.  
  582. GimpContext *
  583. gimp_context_new (const gchar *name,
  584.           GimpContext *template)
  585. {
  586.   GimpContext *context;
  587.  
  588.   g_return_val_if_fail (!template || GIMP_IS_CONTEXT (template), NULL);
  589.  
  590.   context = gtk_type_new (gimp_context_get_type ());
  591.  
  592.   /*  FIXME: need unique names here  */
  593.   context->name = g_strdup (name ? name : "Unnamed");
  594.  
  595.   if (template)
  596.     {
  597.       context->defined_args = template->defined_args;
  598.  
  599.       gimp_context_copy_args (template, context, GIMP_CONTEXT_ALL_ARGS_MASK);
  600.     }
  601.  
  602.   return context;
  603. }
  604.  
  605. /*****************************************************************************/
  606. /*  getting/setting the special contexts  ************************************/
  607.  
  608. GimpContext *
  609. gimp_context_get_current (void)
  610. {
  611.   return current_context;
  612. }
  613.  
  614. void
  615. gimp_context_set_current (GimpContext *context)
  616. {
  617.   current_context = context;
  618. }
  619.  
  620. GimpContext *
  621. gimp_context_get_user (void)
  622. {
  623.   return user_context;
  624. }
  625.  
  626. void
  627. gimp_context_set_user (GimpContext *context)
  628. {
  629.   user_context = context;
  630. }
  631.  
  632. GimpContext *
  633. gimp_context_get_default (void)
  634. {
  635.   return default_context;
  636. }
  637.  
  638. void
  639. gimp_context_set_default (GimpContext *context)
  640. {
  641.   default_context = context;
  642. }
  643.  
  644. GimpContext *
  645. gimp_context_get_standard (void)
  646. {
  647.   if (! standard_context)
  648.     {
  649.       standard_context = gimp_context_new ("Standard", NULL);
  650.  
  651.       gtk_quit_add_destroy (TRUE, GTK_OBJECT (standard_context));
  652.     }
  653.  
  654.   return standard_context;
  655. }
  656.  
  657. /*****************************************************************************/
  658. /*  functions manipulating a single context  *********************************/
  659.  
  660. gchar *
  661. gimp_context_get_name (GimpContext *context)
  662. {
  663.   context_check_current (context);
  664.   context_return_val_if_fail (context, NULL);
  665.  
  666.   return context->name;
  667. }
  668.  
  669. void
  670. gimp_context_set_name (GimpContext *context,
  671.                const gchar *name)
  672. {
  673.   context_check_current (context);
  674.   context_return_if_fail (context);
  675.  
  676.   if (context->name)
  677.     g_free (context->name);
  678.  
  679.   context->name = g_strdup (name ? name : "Unnamed");
  680. }
  681.  
  682. GimpContext *
  683. gimp_context_get_parent (GimpContext *context)
  684. {
  685.   context_check_current (context);
  686.   context_return_val_if_fail (context, NULL);
  687.  
  688.   return context->parent;
  689. }
  690.  
  691. void
  692. gimp_context_set_parent (GimpContext *context,
  693.              GimpContext *parent)
  694. {
  695.   GimpContextArgType arg;
  696.  
  697.   context_check_current (context);
  698.   context_return_if_fail (context);
  699.   g_return_if_fail (!parent || GIMP_IS_CONTEXT (parent));
  700.  
  701.   if (context == parent)
  702.     return;
  703.  
  704.   for (arg = 0; arg < GIMP_CONTEXT_NUM_ARGS; arg++)
  705.     if (! ((1 << arg) & context->defined_args))
  706.       {
  707.     gimp_context_copy_arg (parent, context, arg);
  708.     gtk_signal_connect_object (GTK_OBJECT (parent),
  709.                    gimp_context_signal_names[arg],
  710.                    gimp_context_signal_handlers[arg],
  711.                    GTK_OBJECT (context));
  712.       }
  713.  
  714.   context->parent = parent;
  715. }
  716.  
  717. void
  718. gimp_context_unset_parent (GimpContext *context)
  719. {
  720.   context_check_current (context);
  721.   context_return_if_fail (context);
  722.  
  723.   if (context->parent)
  724.     {
  725.       if (context->defined_args != GIMP_CONTEXT_ALL_ARGS_MASK)
  726.     gtk_signal_disconnect_by_data (GTK_OBJECT (context->parent), context);
  727.  
  728.       context->parent = NULL;
  729.     }
  730. }
  731.  
  732. /*  define / undefinine context arguments  */
  733.  
  734. void
  735. gimp_context_define_arg (GimpContext        *context,
  736.              GimpContextArgType  arg,
  737.              gboolean            defined)
  738. {
  739.   GimpContextArgMask mask;
  740.  
  741.   context_check_current (context);
  742.   context_return_if_fail (context);
  743.   g_return_if_fail ((arg >= 0) && (arg < GIMP_CONTEXT_NUM_ARGS));
  744.  
  745.   mask = (1 << arg);
  746.  
  747.   if (defined)
  748.     {
  749.       if (! (context->defined_args & mask))
  750.     {
  751.       context->defined_args |= mask;
  752.       if (context->parent)
  753.         gtk_signal_disconnect_by_func (GTK_OBJECT (context->parent),
  754.                        gimp_context_signal_handlers[arg],
  755.                        context);
  756.     }
  757.     }
  758.   else
  759.     {
  760.       if (context->defined_args & mask)
  761.     {
  762.       context->defined_args &= ~mask;
  763.       if (context->parent)
  764.         {
  765.           gimp_context_copy_arg (context->parent, context, arg);
  766.           gtk_signal_connect_object (GTK_OBJECT (context->parent),
  767.                      gimp_context_signal_names[arg],
  768.                      gimp_context_signal_handlers[arg],
  769.                      GTK_OBJECT (context));
  770.         }
  771.     }
  772.     }
  773. }
  774.  
  775. gboolean
  776. gimp_context_arg_defined (GimpContext        *context,
  777.               GimpContextArgType  arg)
  778. {
  779.   context_check_current (context);
  780.   context_return_val_if_fail (context, FALSE);
  781.  
  782.   return (context->defined_args & (1 << arg)) ? TRUE : FALSE;
  783. }
  784.  
  785. void
  786. gimp_context_define_args (GimpContext        *context,
  787.               GimpContextArgMask  args_mask,
  788.               gboolean            defined)
  789. {
  790.   GimpContextArgType arg;
  791.  
  792.   context_check_current (context);
  793.   context_return_if_fail (context);
  794.  
  795.   for (arg = 0; arg < GIMP_CONTEXT_NUM_ARGS; arg++)
  796.     if ((1 << arg) & args_mask)
  797.       gimp_context_define_arg (context, arg, defined);
  798. }
  799.  
  800. /*  copying context arguments  */
  801.  
  802. void
  803. gimp_context_copy_arg (GimpContext        *src,
  804.                GimpContext        *dest,
  805.                GimpContextArgType  arg)
  806. {
  807.   context_check_current (src);
  808.   context_return_if_fail (src);
  809.   context_check_current (dest);
  810.   context_return_if_fail (dest);
  811.   g_return_if_fail ((arg >= 0) && (arg < GIMP_CONTEXT_NUM_ARGS));
  812.  
  813.   (* gimp_context_copy_arg_funcs[arg]) (src, dest);
  814. }
  815.  
  816. void
  817. gimp_context_copy_args (GimpContext        *src,
  818.             GimpContext        *dest,
  819.             GimpContextArgMask  args_mask)
  820. {
  821.   GimpContextArgType arg;
  822.  
  823.   context_check_current (src);
  824.   context_return_if_fail (src);
  825.   context_check_current (dest);
  826.   context_return_if_fail (dest);
  827.  
  828.   for (arg = 0; arg < GIMP_CONTEXT_NUM_ARGS; arg++)
  829.     if ((1 << arg) & args_mask)
  830.       {
  831.     gimp_context_copy_arg (src, dest, arg);
  832.       }
  833. }
  834.  
  835. /*  attribute access functions  */
  836.  
  837. /*****************************************************************************/
  838. /*  image  *******************************************************************/
  839.  
  840. GimpImage *
  841. gimp_context_get_image (GimpContext *context)
  842. {
  843.   context_check_current (context);
  844.   context_return_val_if_fail (context, NULL);
  845.  
  846.   return context->image;
  847. }
  848.  
  849. void
  850. gimp_context_set_image (GimpContext *context,
  851.             GimpImage   *image)
  852. {
  853.   context_check_current (context);
  854.   context_return_if_fail (context);
  855.   context_find_defined (context, GIMP_CONTEXT_IMAGE_MASK);
  856.  
  857.   gimp_context_real_set_image (context, image);
  858. }
  859.  
  860. void
  861. gimp_context_image_changed (GimpContext *context)
  862. {
  863.   context_check_current (context);
  864.   context_return_if_fail (context);
  865.  
  866.   gtk_signal_emit (GTK_OBJECT (context),
  867.            gimp_context_signals[IMAGE_CHANGED],
  868.            context->image);
  869. }
  870.  
  871. /*  handle disappearing images  */
  872. static void
  873. gimp_context_image_removed (GimpSet     *set,
  874.                 GimpImage   *image,
  875.                 GimpContext *context)
  876. {
  877.   if (context->image == image)
  878.     gimp_context_real_set_image (context, NULL);
  879. }
  880.  
  881. static void
  882. gimp_context_real_set_image (GimpContext *context,
  883.                  GimpImage   *image)
  884. {
  885.   if (context->image == image)
  886.     return;
  887.  
  888.   if (image == NULL)
  889.     gtk_signal_disconnect_by_data (GTK_OBJECT (image_context), context);
  890.  
  891.   if (context->image == NULL)
  892.     gtk_signal_connect (GTK_OBJECT (image_context), "remove",
  893.             GTK_SIGNAL_FUNC (gimp_context_image_removed),
  894.             context);
  895.  
  896.   context->image = image;
  897.   gimp_context_image_changed (context);
  898. }
  899.  
  900. static void
  901. gimp_context_copy_image (GimpContext *src,
  902.              GimpContext *dest)
  903. {
  904.   gimp_context_real_set_image (dest, src->image);
  905. }
  906.  
  907. /*****************************************************************************/
  908. /*  display  *****************************************************************/
  909.  
  910. GDisplay *
  911. gimp_context_get_display (GimpContext *context)
  912. {
  913.   context_check_current (context);
  914.   context_return_val_if_fail (context, NULL);
  915.  
  916.   return context->display;
  917. }
  918.  
  919. void
  920. gimp_context_set_display (GimpContext *context,
  921.               GDisplay    *display)
  922. {
  923.   context_check_current (context);
  924.   context_return_if_fail (context);
  925.   context_find_defined (context, GIMP_CONTEXT_DISPLAY_MASK);
  926.  
  927.   gimp_context_real_set_display (context, display);
  928. }
  929.  
  930. void
  931. gimp_context_display_changed (GimpContext *context)
  932. {
  933.   context_check_current (context);
  934.   context_return_if_fail (context);
  935.  
  936.   gtk_signal_emit (GTK_OBJECT (context),
  937.            gimp_context_signals[DISPLAY_CHANGED],
  938.            context->display);
  939. }
  940.  
  941. /*  handle dissapearing displays  */
  942. static void
  943. gimp_context_display_destroy (GtkWidget   *disp_shell,
  944.                   GimpContext *context)
  945. {
  946.   context->display = NULL;
  947.   gimp_context_display_changed (context);
  948. }
  949.  
  950. static void
  951. gimp_context_real_set_display (GimpContext *context,
  952.                    GDisplay    *display)
  953. {
  954.   if (context->display == display)
  955.     return;
  956.  
  957.   if (context->display && GTK_IS_OBJECT (context->display->shell))
  958.     gtk_signal_disconnect_by_data (GTK_OBJECT (context->display->shell),
  959.                    context);
  960.  
  961.   if (display)
  962.     gtk_signal_connect (GTK_OBJECT (display->shell), "destroy",
  963.             GTK_SIGNAL_FUNC (gimp_context_display_destroy),
  964.             context);
  965.  
  966.   context->display = display;
  967.  
  968.   /*  set the image _before_ emitting the display_changed signal  */
  969.   if (display)
  970.     gimp_context_real_set_image (context, display->gimage);
  971.  
  972.   gimp_context_display_changed (context);
  973. }
  974.  
  975. static void
  976. gimp_context_copy_display (GimpContext *src,
  977.                GimpContext *dest)
  978. {
  979.   gimp_context_real_set_display (dest, src->display);
  980. }
  981.  
  982. /*****************************************************************************/
  983. /*  tool  ********************************************************************/
  984.  
  985. ToolType
  986. gimp_context_get_tool (GimpContext *context)
  987. {
  988.   context_check_current (context);
  989.   context_return_val_if_fail (context, 0);
  990.  
  991.   return context->tool;
  992. }
  993.  
  994. void
  995. gimp_context_set_tool (GimpContext *context,
  996.                ToolType     tool)
  997. {
  998.   context_check_current (context);
  999.   context_return_if_fail (context);
  1000.   context_find_defined (context, GIMP_CONTEXT_TOOL_MASK);
  1001.  
  1002.   gimp_context_real_set_tool (context, tool);
  1003. }
  1004.  
  1005. void
  1006. gimp_context_tool_changed (GimpContext *context)
  1007. {
  1008.   context_check_current (context);
  1009.   context_return_if_fail (context);
  1010.  
  1011.   gtk_signal_emit (GTK_OBJECT (context),
  1012.            gimp_context_signals[TOOL_CHANGED],
  1013.            context->tool);
  1014. }
  1015.  
  1016. static void
  1017. gimp_context_real_set_tool (GimpContext *context,
  1018.                 ToolType     tool)
  1019. {
  1020.   if (context->tool == tool)
  1021.     return;
  1022.  
  1023.   context->tool = tool;
  1024.   gimp_context_tool_changed (context);
  1025. }
  1026.  
  1027. static void
  1028. gimp_context_copy_tool (GimpContext *src,
  1029.             GimpContext *dest)
  1030. {
  1031.   gimp_context_real_set_tool (dest, src->tool);
  1032. }
  1033.  
  1034. /*****************************************************************************/
  1035. /*  foreground color  ********************************************************/
  1036.  
  1037. void
  1038. gimp_context_get_foreground (GimpContext *context,
  1039.                  guchar      *r,
  1040.                  guchar      *g,
  1041.                  guchar      *b)
  1042. {
  1043.   context_check_current (context);
  1044.   context_return_if_fail (context);
  1045.  
  1046.   *r = context->foreground[0];
  1047.   *g = context->foreground[1];
  1048.   *b = context->foreground[2];
  1049. }
  1050.  
  1051. void
  1052. gimp_context_set_foreground (GimpContext *context,
  1053.                  gint         r,
  1054.                  gint         g,
  1055.                  gint         b)
  1056. {
  1057.   context_check_current (context);
  1058.   context_return_if_fail (context);
  1059.   context_find_defined (context, GIMP_CONTEXT_FOREGROUND_MASK);
  1060.  
  1061.   gimp_context_real_set_foreground (context, r, g, b);
  1062. }
  1063.  
  1064. void
  1065. gimp_context_foreground_changed (GimpContext *context)
  1066. {
  1067.   context_check_current (context);
  1068.   context_return_if_fail (context);
  1069.  
  1070.   gtk_signal_emit (GTK_OBJECT (context),
  1071.            gimp_context_signals[FOREGROUND_CHANGED],
  1072.            context->foreground[0],
  1073.            context->foreground[1],
  1074.            context->foreground[2]);
  1075. }
  1076.  
  1077. static void
  1078. gimp_context_real_set_foreground (GimpContext *context,
  1079.                   gint         r,
  1080.                   gint         g,
  1081.                   gint         b)
  1082. {
  1083.   if (context->foreground[0] == r &&
  1084.       context->foreground[1] == g &&
  1085.       context->foreground[2] == b)
  1086.     return;
  1087.  
  1088.   context->foreground[0] = r;
  1089.   context->foreground[1] = g;
  1090.   context->foreground[2] = b;
  1091.  
  1092.   gimp_context_foreground_changed (context);
  1093. }
  1094.  
  1095. static void
  1096. gimp_context_copy_foreground (GimpContext *src,
  1097.                   GimpContext *dest)
  1098. {
  1099.   gimp_context_real_set_foreground (dest,
  1100.                     src->foreground[0],
  1101.                     src->foreground[1],
  1102.                     src->foreground[2]);
  1103. }
  1104.  
  1105. /*****************************************************************************/
  1106. /*  background color  ********************************************************/
  1107.  
  1108. void
  1109. gimp_context_get_background (GimpContext *context,
  1110.                  guchar      *r,
  1111.                  guchar      *g,
  1112.                  guchar      *b)
  1113. {
  1114.   context_check_current (context);
  1115.   context_return_if_fail (context);
  1116.  
  1117.   *r = context->background[0];
  1118.   *g = context->background[1];
  1119.   *b = context->background[2];
  1120. }
  1121.  
  1122. void
  1123. gimp_context_set_background (GimpContext *context,
  1124.                  gint         r,
  1125.                  gint         g,
  1126.                  gint         b)
  1127. {
  1128.   context_check_current (context);
  1129.   context_return_if_fail (context);
  1130.   context_find_defined (context, GIMP_CONTEXT_BACKGROUND_MASK);
  1131.  
  1132.   gimp_context_real_set_background (context, r, g, b);
  1133. }
  1134.  
  1135. void
  1136. gimp_context_background_changed (GimpContext *context)
  1137. {
  1138.   context_check_current (context);
  1139.   context_return_if_fail (context);
  1140.  
  1141.   gtk_signal_emit (GTK_OBJECT (context),
  1142.            gimp_context_signals[BACKGROUND_CHANGED],
  1143.            context->background[0],
  1144.            context->background[1],
  1145.            context->background[2]);
  1146. }
  1147.  
  1148. static void
  1149. gimp_context_real_set_background (GimpContext *context,
  1150.                   gint         r,
  1151.                   gint         g,
  1152.                   gint         b)
  1153. {
  1154.   if (context->background[0] == r &&
  1155.       context->background[1] == g &&
  1156.       context->background[2] == b)
  1157.     return;
  1158.  
  1159.   context->background[0] = r;
  1160.   context->background[1] = g;
  1161.   context->background[2] = b;
  1162.  
  1163.   gimp_context_background_changed (context);
  1164. }
  1165.  
  1166. static void
  1167. gimp_context_copy_background (GimpContext *src,
  1168.                   GimpContext *dest)
  1169. {
  1170.   gimp_context_real_set_background (dest,
  1171.                     src->background[0],
  1172.                     src->background[1],
  1173.                     src->background[2]);
  1174. }
  1175.  
  1176. /*****************************************************************************/
  1177. /*  color utility functions  *************************************************/
  1178.  
  1179. void
  1180. gimp_context_set_default_colors (GimpContext *context)
  1181. {
  1182.   GimpContext *bg_context;
  1183.  
  1184.   bg_context = context;
  1185.  
  1186.   context_check_current (context);
  1187.   context_return_if_fail (context);
  1188.   context_find_defined (context, GIMP_CONTEXT_FOREGROUND_MASK);
  1189.   context_find_defined (bg_context, GIMP_CONTEXT_BACKGROUND_MASK);
  1190.  
  1191.   gimp_context_real_set_foreground (context, 0, 0, 0);
  1192.   gimp_context_real_set_background (bg_context, 255, 255, 255);
  1193. }
  1194.  
  1195. void
  1196. gimp_context_swap_colors (GimpContext *context)
  1197. {
  1198.   GimpContext *bg_context;
  1199.   guchar f_r, f_g, f_b;
  1200.   guchar b_r, b_g, b_b;
  1201.  
  1202.   bg_context = context;
  1203.  
  1204.   context_check_current (context);
  1205.   context_return_if_fail (context);
  1206.   context_find_defined (context, GIMP_CONTEXT_FOREGROUND_MASK);
  1207.   context_find_defined (bg_context, GIMP_CONTEXT_BACKGROUND_MASK);
  1208.  
  1209.   gimp_context_get_foreground (context, &f_r, &f_g, &f_b);
  1210.   gimp_context_get_background (bg_context, &b_r, &b_g, &b_b);
  1211.  
  1212.   gimp_context_real_set_foreground (context, b_r, b_g, b_b);
  1213.   gimp_context_real_set_background (bg_context, f_r, f_g, f_b);
  1214. }
  1215.  
  1216. /*****************************************************************************/
  1217. /*  opacity  *****************************************************************/
  1218.  
  1219. gdouble
  1220. gimp_context_get_opacity (GimpContext *context)
  1221. {
  1222.   context_check_current (context);
  1223.   context_return_val_if_fail (context, 1.0);
  1224.  
  1225.   return context->opacity;
  1226. }
  1227.  
  1228. void
  1229. gimp_context_set_opacity (GimpContext *context,
  1230.               gdouble      opacity)
  1231. {
  1232.   context_check_current (context);
  1233.   context_return_if_fail (context);
  1234.   context_find_defined (context, GIMP_CONTEXT_OPACITY_MASK);
  1235.  
  1236.   gimp_context_real_set_opacity (context, opacity);
  1237. }
  1238.  
  1239. void
  1240. gimp_context_opacity_changed (GimpContext *context)
  1241. {
  1242.   context_check_current (context);
  1243.   context_return_if_fail (context);
  1244.  
  1245.   gtk_signal_emit (GTK_OBJECT (context),
  1246.            gimp_context_signals[OPACITY_CHANGED],
  1247.            context->opacity);
  1248. }
  1249.  
  1250. static void
  1251. gimp_context_real_set_opacity (GimpContext *context,
  1252.                    gdouble      opacity)
  1253. {
  1254.   if (context->opacity == opacity)
  1255.     return;
  1256.  
  1257.   context->opacity = opacity;
  1258.   gimp_context_opacity_changed (context);
  1259. }
  1260.  
  1261. static void
  1262. gimp_context_copy_opacity (GimpContext *src,
  1263.                GimpContext *dest)
  1264. {
  1265.   gimp_context_real_set_opacity (dest, src->opacity);
  1266. }
  1267.  
  1268. /*****************************************************************************/
  1269. /*  paint mode  **************************************************************/
  1270.  
  1271. LayerModeEffects
  1272. gimp_context_get_paint_mode (GimpContext *context)
  1273. {
  1274.   context_check_current (context);
  1275.   context_return_val_if_fail (context, 0);
  1276.  
  1277.   return context->paint_mode;
  1278. }
  1279.  
  1280. void
  1281. gimp_context_set_paint_mode (GimpContext     *context,
  1282.                  LayerModeEffects paint_mode)
  1283. {
  1284.   context_check_current (context);
  1285.   context_return_if_fail (context);
  1286.   context_find_defined (context, GIMP_CONTEXT_PAINT_MODE_MASK);
  1287.  
  1288.   gimp_context_real_set_paint_mode (context, paint_mode);
  1289. }
  1290.  
  1291. void
  1292. gimp_context_paint_mode_changed (GimpContext *context)
  1293. {
  1294.   context_check_current (context);
  1295.   context_return_if_fail (context);
  1296.  
  1297.   gtk_signal_emit (GTK_OBJECT (context),
  1298.            gimp_context_signals[PAINT_MODE_CHANGED],
  1299.            context->paint_mode);
  1300. }
  1301.  
  1302. static void
  1303. gimp_context_real_set_paint_mode (GimpContext     *context,
  1304.                   LayerModeEffects paint_mode)
  1305. {
  1306.   if (context->paint_mode == paint_mode)
  1307.     return;
  1308.  
  1309.   context->paint_mode = paint_mode;
  1310.   gimp_context_paint_mode_changed (context);
  1311. }
  1312.  
  1313. static void
  1314. gimp_context_copy_paint_mode (GimpContext *src,
  1315.                   GimpContext *dest)
  1316. {
  1317.   gimp_context_real_set_paint_mode (dest, src->paint_mode);
  1318. }
  1319.  
  1320. /*****************************************************************************/
  1321. /*  brush  *******************************************************************/
  1322.  
  1323. static GimpBrush *standard_brush = NULL;
  1324.  
  1325. GimpBrush *
  1326. gimp_context_get_brush (GimpContext *context)
  1327. {
  1328.   context_check_current (context);
  1329.   context_return_val_if_fail (context, NULL);
  1330.  
  1331.   return context->brush;
  1332. }
  1333.  
  1334. void
  1335. gimp_context_set_brush (GimpContext *context,
  1336.             GimpBrush   *brush)
  1337. {
  1338.   context_check_current (context);
  1339.   context_return_if_fail (context);
  1340.   context_find_defined (context, GIMP_CONTEXT_BRUSH_MASK);
  1341.  
  1342.   gimp_context_real_set_brush (context, brush);
  1343. }
  1344.  
  1345. void
  1346. gimp_context_brush_changed (GimpContext *context)
  1347. {
  1348.   context_check_current (context);
  1349.   context_return_if_fail (context);
  1350.  
  1351.   gtk_signal_emit (GTK_OBJECT (context),
  1352.            gimp_context_signals[BRUSH_CHANGED],
  1353.            context->brush);
  1354. }
  1355.  
  1356. /*  the active brush was modified  */
  1357. static void
  1358. gimp_context_brush_dirty (GimpBrush   *brush,
  1359.               GimpContext *context)
  1360. {
  1361.   g_free (context->brush_name);
  1362.   context->brush_name = g_strdup (brush->name);
  1363.  
  1364.   gimp_context_brush_changed (context);
  1365. }
  1366.  
  1367. /*  the active brush disappeared  */
  1368. static void
  1369. gimp_context_brush_removed (GimpBrushList *brush_list,
  1370.                 GimpBrush     *brush,
  1371.                 GimpContext   *context)
  1372. {
  1373.   if (brush == context->brush)
  1374.     {
  1375.       context->brush = NULL;
  1376.  
  1377.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush), context);
  1378.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), context);
  1379.       gtk_object_unref (GTK_OBJECT (brush));
  1380.     }
  1381. }
  1382.  
  1383. static void
  1384. gimp_context_real_set_brush (GimpContext *context,
  1385.                  GimpBrush   *brush)
  1386. {
  1387.   if (! standard_brush)
  1388.     standard_brush = brushes_get_standard_brush ();
  1389.  
  1390.   if (context->brush == brush)
  1391.     return;
  1392.  
  1393.   if (context->brush_name && brush != standard_brush)
  1394.     {
  1395.       g_free (context->brush_name);
  1396.       context->brush_name = NULL;
  1397.     }
  1398.  
  1399.   /*  make sure the active brush is swapped before we get a new one...  */
  1400.   if (stingy_memory_use &&
  1401.       context->brush && context->brush->mask &&
  1402.       GTK_OBJECT (context->brush)->ref_count == 2)
  1403.     {
  1404.       temp_buf_swap (brush->mask);
  1405.     }
  1406.  
  1407.   /*  disconnect from the old brush's signals  */
  1408.   if (context->brush)
  1409.     {
  1410.       gtk_signal_disconnect_by_data (GTK_OBJECT (context->brush), context);
  1411.       gtk_object_unref (GTK_OBJECT (context->brush));
  1412.  
  1413.       /*  if we don't get a new brush, also disconnect from the brush list  */
  1414.       if (! brush)
  1415.     {
  1416.       gtk_signal_disconnect_by_data (GTK_OBJECT (brush_list), context);
  1417.     }
  1418.     }
  1419.   /*  if we get a new brush but didn't have one before...  */
  1420.   else if (brush)
  1421.     {
  1422.       /*  ...connect to the brush list  */
  1423.       gtk_signal_connect (GTK_OBJECT (brush_list), "remove",
  1424.               GTK_SIGNAL_FUNC (gimp_context_brush_removed),
  1425.               context);
  1426.     }
  1427.  
  1428.   context->brush = brush;
  1429.  
  1430.   if (brush)
  1431.     {
  1432.       gtk_object_ref (GTK_OBJECT (brush));
  1433.       gtk_signal_connect (GTK_OBJECT (brush), "dirty",
  1434.               GTK_SIGNAL_FUNC (gimp_context_brush_dirty),
  1435.               context);
  1436.       gtk_signal_connect (GTK_OBJECT (brush), "rename",
  1437.               GTK_SIGNAL_FUNC (gimp_context_brush_dirty),
  1438.               context);
  1439.  
  1440.       /*  Make sure the active brush is unswapped... */
  1441.       if (stingy_memory_use &&
  1442.       brush->mask &&
  1443.       GTK_OBJECT (brush)->ref_count < 2)
  1444.     {
  1445.       temp_buf_unswap (brush->mask);
  1446.     }
  1447.  
  1448.       if (brush != standard_brush)
  1449.     context->brush_name = g_strdup (brush->name);
  1450.     }
  1451.  
  1452.   gimp_context_brush_changed (context);
  1453. }
  1454.  
  1455. static void
  1456. gimp_context_copy_brush (GimpContext *src,
  1457.              GimpContext *dest)
  1458. {
  1459.   gimp_context_real_set_brush (dest, src->brush);
  1460.  
  1461.   if ((!src->brush || src->brush == standard_brush) && src->brush_name)
  1462.     {
  1463.       g_free (dest->brush_name);
  1464.       dest->brush_name = g_strdup (src->brush_name);
  1465.     }
  1466. }
  1467.  
  1468. static void
  1469. gimp_context_refresh_brush (GimpContext *context,
  1470.                 gpointer     data)
  1471. {
  1472.   GimpBrush *brush;
  1473.  
  1474.   if (! context->brush_name)
  1475.     context->brush_name = g_strdup (default_brush);
  1476.  
  1477.   if ((brush = gimp_brush_list_get_brush (brush_list, context->brush_name)))
  1478.     {
  1479.       gimp_context_real_set_brush (context, brush);
  1480.       return;
  1481.     }
  1482.  
  1483.   if (gimp_brush_list_length (brush_list))
  1484.     gimp_context_real_set_brush 
  1485.       (context, gimp_brush_list_get_brush_by_index (brush_list, 0));
  1486.   else
  1487.     gimp_context_real_set_brush (context, brushes_get_standard_brush ());
  1488. }
  1489.  
  1490. void
  1491. gimp_context_refresh_brushes (void)
  1492. {
  1493.   g_slist_foreach (context_list, (GFunc) gimp_context_refresh_brush, NULL);
  1494. }
  1495.  
  1496. /*****************************************************************************/
  1497. /*  pattern  *****************************************************************/
  1498.  
  1499. static GPattern *standard_pattern = NULL;
  1500.  
  1501. GPattern *
  1502. gimp_context_get_pattern (GimpContext *context)
  1503. {
  1504.   context_check_current (context);
  1505.   context_return_val_if_fail (context, NULL);
  1506.  
  1507.   return context->pattern;
  1508. }
  1509.  
  1510. void
  1511. gimp_context_set_pattern (GimpContext *context,
  1512.               GPattern    *pattern)
  1513. {
  1514.   context_check_current (context);
  1515.   context_return_if_fail (context);
  1516.   context_find_defined (context, GIMP_CONTEXT_PATTERN_MASK);
  1517.  
  1518.   gimp_context_real_set_pattern (context, pattern);
  1519. }
  1520.  
  1521. void
  1522. gimp_context_pattern_changed (GimpContext *context)
  1523. {
  1524.   context_check_current (context);
  1525.   context_return_if_fail (context);
  1526.  
  1527.   gtk_signal_emit (GTK_OBJECT (context),
  1528.            gimp_context_signals[PATTERN_CHANGED],
  1529.            context->pattern);
  1530. }
  1531.  
  1532. static void
  1533. gimp_context_real_set_pattern (GimpContext *context,
  1534.                    GPattern    *pattern)
  1535. {
  1536.   if (! standard_pattern)
  1537.     standard_pattern = patterns_get_standard_pattern ();
  1538.  
  1539.   if (context->pattern == pattern)
  1540.     return;
  1541.  
  1542.   if (context->pattern_name && pattern != standard_pattern)
  1543.     {
  1544.       g_free (context->pattern_name);
  1545.       context->pattern_name = NULL;
  1546.     }
  1547.  
  1548.   context->pattern = pattern;
  1549.  
  1550.   if (pattern && pattern != standard_pattern)
  1551.     context->pattern_name = g_strdup (pattern->name);
  1552.  
  1553.   gimp_context_pattern_changed (context);
  1554. }
  1555.  
  1556. static void
  1557. gimp_context_copy_pattern (GimpContext *src,
  1558.                GimpContext *dest)
  1559. {
  1560.   gimp_context_real_set_pattern (dest, src->pattern);
  1561.  
  1562.   if ((!src->pattern || src->pattern == standard_pattern) && src->pattern_name)
  1563.     {
  1564.       g_free (dest->pattern_name);
  1565.       dest->pattern_name = g_strdup (src->pattern_name);
  1566.     }
  1567. }
  1568.  
  1569. static void
  1570. gimp_context_refresh_pattern (GimpContext *context,
  1571.                   gpointer     data)
  1572. {
  1573.   GPattern *pattern;
  1574.  
  1575.   if (! context->pattern_name)
  1576.     context->pattern_name = g_strdup (default_pattern);
  1577.  
  1578.   if ((pattern = pattern_list_get_pattern (pattern_list,
  1579.                        context->pattern_name)))
  1580.     {
  1581.       gimp_context_real_set_pattern (context, pattern);
  1582.       return;
  1583.     }
  1584.  
  1585.   if ((pattern = pattern_list_get_pattern_by_index (pattern_list, 0)))
  1586.     gimp_context_real_set_pattern (context, pattern);
  1587.   else
  1588.     gimp_context_real_set_pattern (context, patterns_get_standard_pattern ());
  1589. }
  1590.  
  1591. void
  1592. gimp_context_refresh_patterns (void)
  1593. {
  1594.   g_slist_foreach (context_list, (GFunc) gimp_context_refresh_pattern, NULL);
  1595. }
  1596.  
  1597. static void
  1598. gimp_context_update_pattern (GimpContext *context,
  1599.                  GPattern    *pattern)
  1600. {
  1601.   if (context->pattern == pattern)
  1602.     {
  1603.       if (context->pattern_name)
  1604.     g_free (context->pattern_name);
  1605.  
  1606.       context->pattern_name = g_strdup (pattern->name);
  1607.  
  1608.       gimp_context_pattern_changed (context);
  1609.     }
  1610. }
  1611.  
  1612. void
  1613. gimp_context_update_patterns (GPattern *pattern)
  1614. {
  1615.   g_slist_foreach (context_list, (GFunc) gimp_context_update_pattern, pattern);
  1616. }
  1617.  
  1618. /*****************************************************************************/
  1619. /*  gradient  ****************************************************************/
  1620.  
  1621. static gradient_t *standard_gradient = NULL;
  1622.  
  1623. gradient_t *
  1624. gimp_context_get_gradient (GimpContext *context)
  1625. {
  1626.   context_check_current (context);
  1627.   context_return_val_if_fail (context, NULL);
  1628.  
  1629.   return context->gradient;
  1630. }
  1631.  
  1632. void
  1633. gimp_context_set_gradient (GimpContext *context,
  1634.                gradient_t  *gradient)
  1635. {
  1636.   context_check_current (context);
  1637.   context_return_if_fail (context);
  1638.   context_find_defined (context, GIMP_CONTEXT_GRADIENT_MASK);
  1639.  
  1640.   gimp_context_real_set_gradient (context, gradient);
  1641. }
  1642.  
  1643. void
  1644. gimp_context_gradient_changed (GimpContext *context)
  1645. {
  1646.   context_check_current (context);
  1647.   context_return_if_fail (context);
  1648.  
  1649.   gtk_signal_emit (GTK_OBJECT (context),
  1650.            gimp_context_signals[GRADIENT_CHANGED],
  1651.            context->gradient);
  1652. }
  1653.  
  1654. static void
  1655. gimp_context_real_set_gradient (GimpContext *context,
  1656.                 gradient_t  *gradient)
  1657. {
  1658.   if (! standard_gradient)
  1659.     standard_gradient = gradients_get_standard_gradient ();
  1660.  
  1661.   if (context->gradient == gradient)
  1662.     return;
  1663.  
  1664.   if (context->gradient_name && gradient != standard_gradient)
  1665.     {
  1666.       g_free (context->gradient_name);
  1667.       context->gradient_name = NULL;
  1668.     }
  1669.  
  1670.   context->gradient = gradient;
  1671.  
  1672.   if (gradient && gradient != standard_gradient)
  1673.     context->gradient_name = g_strdup (gradient->name);
  1674.  
  1675.   gimp_context_gradient_changed (context);
  1676. }
  1677.  
  1678. static void
  1679. gimp_context_copy_gradient (GimpContext *src,
  1680.                 GimpContext *dest)
  1681. {
  1682.   gimp_context_real_set_gradient (dest, src->gradient);
  1683.  
  1684.   if ((!src->gradient || src->gradient == standard_gradient) &&
  1685.       src->gradient_name)
  1686.     {
  1687.       g_free (dest->gradient_name);
  1688.       dest->gradient_name = g_strdup (src->gradient_name);
  1689.     }
  1690. }
  1691.  
  1692. static void
  1693. gimp_context_refresh_gradient (GimpContext *context,
  1694.                    gpointer     data)
  1695. {
  1696.   gradient_t *gradient;
  1697.  
  1698.   if (! context->gradient_name)
  1699.     context->gradient_name = g_strdup (default_gradient);
  1700.  
  1701.   if ((gradient = gradient_list_get_gradient (gradients_list,
  1702.                           context->gradient_name)))
  1703.     {
  1704.       gimp_context_real_set_gradient (context, gradient);
  1705.       return;
  1706.     }
  1707.  
  1708.   if (gradients_list)
  1709.     gimp_context_real_set_gradient (context,
  1710.                     (gradient_t *) gradients_list->data);
  1711.   else
  1712.     gimp_context_real_set_gradient (context, gradients_get_standard_gradient ());
  1713. }
  1714.  
  1715. void
  1716. gimp_context_refresh_gradients (void)
  1717. {
  1718.   g_slist_foreach (context_list, (GFunc) gimp_context_refresh_gradient, NULL);
  1719. }
  1720.  
  1721. static void
  1722. gimp_context_update_gradient (GimpContext *context,
  1723.                   gradient_t  *gradient)
  1724. {
  1725.   if (context->gradient == gradient)
  1726.     {
  1727.       if (context->gradient_name)
  1728.     g_free (context->gradient_name);
  1729.  
  1730.       context->gradient_name = g_strdup (gradient->name);
  1731.  
  1732.       gimp_context_gradient_changed (context);
  1733.     }
  1734. }
  1735.  
  1736. void
  1737. gimp_context_update_gradients (gradient_t *gradient)
  1738. {
  1739.   g_slist_foreach (context_list, (GFunc) gimp_context_update_gradient, gradient);
  1740. }
  1741.