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

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <gtk/gtk.h>
  22.  
  23. #include "apptypes.h"
  24.  
  25. #include "appenv.h"
  26. #include "cursorutil.h"
  27. #include "context_manager.h"
  28. #include "gdisplay.h"
  29. #include "gimprc.h"
  30. #include "paint_options.h"
  31. #include "tools.h"
  32.  
  33. static GimpContext * global_tool_context;
  34.  
  35. #define PAINT_OPTIONS_MASK GIMP_CONTEXT_OPACITY_MASK | \
  36.                            GIMP_CONTEXT_PAINT_MODE_MASK
  37.  
  38. static void
  39. context_manager_display_changed (GimpContext *context,
  40.                  GDisplay    *display,
  41.                  gpointer     data)
  42. {
  43.   gdisplay_set_menu_sensitivity (display);
  44. }
  45.  
  46. static void
  47. context_manager_tool_changed (GimpContext *user_context,
  48.                   ToolType     tool_type,
  49.                   gpointer     data)
  50. {
  51.   /* FIXME: gimp_busy HACK */
  52.   if (gimp_busy)
  53.     {
  54.       /*  there may be contexts waiting for the user_context's "tool_changed"
  55.        *  signal, so stop emitting it.
  56.        */
  57.       gtk_signal_emit_stop_by_name (GTK_OBJECT (user_context), "tool_changed");
  58.  
  59.       if (active_tool->type != tool_type)
  60.     {
  61.       gtk_signal_handler_block_by_func (GTK_OBJECT (user_context),
  62.                         context_manager_tool_changed,
  63.                         NULL);
  64.  
  65.       /*  explicitly set the current tool  */
  66.       gimp_context_set_tool (user_context, active_tool->type);
  67.  
  68.       gtk_signal_handler_unblock_by_func (GTK_OBJECT (user_context),
  69.                           context_manager_tool_changed,
  70.                           NULL);
  71.     }
  72.  
  73.       /*  take care that the correct toolbox button gets re-activated  */
  74.       tool_type = active_tool->type;
  75.     }
  76.   else
  77.     {
  78.       GimpContext* tool_context;
  79.  
  80.       if (! global_paint_options)
  81.     {
  82.       if (active_tool &&
  83.           (tool_context = tool_info[active_tool->type].tool_context))
  84.         {
  85.           gimp_context_unset_parent (tool_context);
  86.         }
  87.  
  88.       if ((tool_context = tool_info[tool_type].tool_context))
  89.         {
  90.           gimp_context_copy_args (tool_context, user_context,
  91.                       PAINT_OPTIONS_MASK);
  92.           gimp_context_set_parent (tool_context, user_context);
  93.         }
  94.     }
  95.  
  96.       tools_select (tool_type);
  97.     }
  98.  
  99.   if (tool_type == SCALE ||
  100.       tool_type == SHEAR ||
  101.       tool_type == PERSPECTIVE)
  102.     tool_type = ROTATE;
  103.  
  104.   if (! GTK_TOGGLE_BUTTON (tool_info[tool_type].tool_widget)->active)
  105.     {
  106.       gtk_signal_handler_block_by_data
  107.     (GTK_OBJECT (tool_info[tool_type].tool_widget), (gpointer) tool_type);
  108.  
  109.       gtk_widget_activate (tool_info[tool_type].tool_widget);
  110.  
  111.       gtk_signal_handler_unblock_by_data
  112.     (GTK_OBJECT (tool_info[tool_type].tool_widget), (gpointer) tool_type);
  113.     }
  114. }
  115.  
  116. void
  117. context_manager_init (void)
  118. {
  119.   GimpContext *standard_context;
  120.   GimpContext *default_context;
  121.   GimpContext *user_context;
  122.   gint i;
  123.  
  124.   /*  Implicitly create the standard context  */
  125.   standard_context = gimp_context_get_standard ();
  126.  
  127.   /*  TODO: load from disk  */
  128.   default_context = gimp_context_new ("Default", NULL);
  129.   gimp_context_set_default (default_context);
  130.  
  131.   /*  Initialize the user context will with the default context's values  */
  132.   user_context = gimp_context_new ("User", default_context);
  133.   gimp_context_set_user (user_context);
  134.  
  135.   /*  Update the tear-off menus  */
  136.   gtk_signal_connect (GTK_OBJECT (user_context), "display_changed",
  137.               GTK_SIGNAL_FUNC (context_manager_display_changed),
  138.               NULL);
  139.  
  140.   /*  Update the tool system  */
  141.   gtk_signal_connect (GTK_OBJECT (user_context), "tool_changed",
  142.               GTK_SIGNAL_FUNC (context_manager_tool_changed),
  143.               NULL);
  144.  
  145.   /*  Make the user contect the currently active context  */
  146.   gimp_context_set_current (user_context);
  147.  
  148.   /*  Create a context to store the paint options of the
  149.    *  global paint options mode
  150.    */
  151.   global_tool_context = gimp_context_new ("Global Tool Context", user_context);
  152.  
  153.   /*  TODO: add foreground, background, brush, pattern, gradient  */
  154.   gimp_context_define_args (global_tool_context, PAINT_OPTIONS_MASK, FALSE);
  155.  
  156.   /*  Initialize the paint tools' private contexts  */
  157.   for (i = 0; i < num_tools; i++)
  158.     {
  159.       switch (tool_info[i].tool_id)
  160.     {
  161.     case BUCKET_FILL:
  162.     case BLEND:
  163.     case PENCIL:
  164.     case PAINTBRUSH:
  165.     case ERASER:
  166.     case AIRBRUSH:
  167.     case CLONE:
  168.     case CONVOLVE:
  169.     case INK:
  170.     case DODGEBURN:
  171.     case SMUDGE:
  172. /*      case XINPUT_AIRBRUSH: */
  173.       tool_info[i].tool_context =
  174.         gimp_context_new (tool_info[i].private_tip, global_tool_context);
  175.       break;
  176.  
  177.     default:
  178.       tool_info[i].tool_context = NULL;
  179.       break;
  180.     }
  181.     }
  182.  
  183.   if (! global_paint_options &&
  184.       active_tool &&
  185.       tool_info[active_tool->type].tool_context)
  186.     {
  187.       gimp_context_set_parent (tool_info[active_tool->type].tool_context,
  188.                    user_context);
  189.     }
  190.   else if (global_paint_options)
  191.     {
  192.       gimp_context_set_parent (global_tool_context, user_context);
  193.     }
  194. }
  195.  
  196. void
  197. context_manager_free (void)
  198. {
  199.   gint i;
  200.  
  201.   gtk_object_unref (GTK_OBJECT (global_tool_context));
  202.   global_tool_context = NULL;
  203.  
  204.   for (i = 0; i < num_tools; i++)
  205.     {
  206.       if (tool_info[i].tool_context != NULL)
  207.     {
  208.       gtk_object_unref (GTK_OBJECT (tool_info[i].tool_context));
  209.       tool_info[i].tool_context = NULL;
  210.     }
  211.     }
  212.  
  213.   gtk_object_unref (GTK_OBJECT (gimp_context_get_user ()));
  214.   gimp_context_set_user (NULL);
  215.   gimp_context_set_current (NULL);
  216.  
  217.   /*  TODO: Save to disk before destroying  */
  218.   gtk_object_unref (GTK_OBJECT (gimp_context_get_default ()));
  219.   gimp_context_set_default (NULL);
  220. }
  221.  
  222. void
  223. context_manager_set_global_paint_options (gboolean global)
  224. {
  225.   GimpContext* context;
  226.  
  227.   if (global == global_paint_options) return;
  228.  
  229.   paint_options_set_global (global);
  230.  
  231.   if (global)
  232.     {
  233.       if (active_tool &&
  234.       (context = tool_info[active_tool->type].tool_context))
  235.     {
  236.       gimp_context_unset_parent (context);
  237.     }
  238.  
  239.       gimp_context_copy_args (global_tool_context, gimp_context_get_user (),
  240.                   PAINT_OPTIONS_MASK);
  241.       gimp_context_set_parent (global_tool_context, gimp_context_get_user ());
  242.     }
  243.   else
  244.     {
  245.       gimp_context_unset_parent (global_tool_context);
  246.  
  247.       if (active_tool &&
  248.       (context = tool_info[active_tool->type].tool_context))
  249.     {
  250.       gimp_context_copy_args (context, gimp_context_get_user (),
  251.                   GIMP_CONTEXT_PAINT_ARGS_MASK);
  252.       gimp_context_set_parent (context, gimp_context_get_user ());
  253.     }
  254.     }
  255. }
  256.