home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / gimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  7.3 KB  |  297 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 "gimpimageP.h"
  26. #include "gimage.h"
  27. #include "gimpimage.h"
  28. #include "lc_dialog.h"
  29.  
  30. #include "drawable.h"
  31. #include "gdisplay.h"
  32. #include "procedural_db.h"
  33.  
  34. #include "paletteP.h"
  35. #include "undo.h"
  36.  
  37. #include "layer.h"
  38. #include "layer_pvt.h"
  39. #include "channel.h"
  40. #include "tools.h"
  41. #include "appenv.h"
  42. #include "gimpset.h"
  43. #include "dialog_handler.h"
  44.  
  45.  
  46. /* gimage.c: Junk (ugly dependencies) from gimpimage.c on its way
  47.    to proper places. That is, the handlers should be moved to
  48.    layers_dialog, gdisplay, tools, etc.. */
  49.  
  50. static void gimage_dirty_handler        (GimpImage* gimage);
  51. static void gimage_destroy_handler      (GimpImage* gimage);
  52. static void gimage_cmap_change_handler  (GimpImage* gimage,
  53.                      gint       ncol,
  54.                      gpointer   user_data);
  55. static void gimage_rename_handler       (GimpImage* gimage);
  56. static void gimage_resize_handler       (GimpImage* gimage);
  57. static void gimage_restructure_handler  (GimpImage* gimage);
  58. static void gimage_repaint_handler      (GimpImage* gimage,
  59.                      gint, gint, gint, gint);
  60.  
  61.  
  62. GImage*
  63. gimage_new (int               width, 
  64.         int               height, 
  65.         GimpImageBaseType base_type)
  66. {
  67.   GimpImage* gimage = gimp_image_new (width, height, base_type);
  68.  
  69.   gtk_signal_connect (GTK_OBJECT (gimage), "dirty",
  70.               GTK_SIGNAL_FUNC(gimage_dirty_handler), NULL);
  71.   gtk_signal_connect (GTK_OBJECT (gimage), "destroy",
  72.               GTK_SIGNAL_FUNC(gimage_destroy_handler), NULL);
  73.   gtk_signal_connect (GTK_OBJECT (gimage), "rename",
  74.               GTK_SIGNAL_FUNC(gimage_rename_handler), NULL);
  75.   gtk_signal_connect (GTK_OBJECT (gimage), "resize",
  76.               GTK_SIGNAL_FUNC(gimage_resize_handler), NULL);
  77.   gtk_signal_connect (GTK_OBJECT (gimage), "restructure",
  78.               GTK_SIGNAL_FUNC(gimage_restructure_handler), NULL);
  79.   gtk_signal_connect (GTK_OBJECT (gimage), "repaint",
  80.               GTK_SIGNAL_FUNC(gimage_repaint_handler), NULL);
  81.   gtk_signal_connect (GTK_OBJECT (gimage), "colormap_changed",
  82.               GTK_SIGNAL_FUNC(gimage_cmap_change_handler), NULL);
  83.  
  84.   gimp_set_add (image_context, gimage);
  85.  
  86.   return gimage;
  87. }
  88.  
  89. GImage*
  90. gimage_get_ID (gint ID)
  91. {
  92.   return pdb_id_to_image (ID);
  93. }
  94.  
  95.  
  96. /* Ack, GImages have their own ref counts! This is going to cause
  97.    trouble.. It should be pretty easy to convert to proper GtkObject
  98.    ref counting, though. */
  99.  
  100. /* This caused trouble indeed. The ref_count was only used by the
  101.    displays showing the image, so I renamed it to disp_count to 
  102.    make clear that it should only be used for display references.
  103.                                                (Sven, 23.01.2000) */ 
  104.  
  105. void
  106. gimage_delete (GImage *gimage)
  107. {
  108.   if (gimage->disp_count <= 0)
  109.     gtk_object_unref (GTK_OBJECT (gimage));
  110. }
  111.  
  112. static void
  113. invalidate_cb (gpointer image, 
  114.            gpointer user_data)
  115. {
  116.   gimp_image_invalidate_preview (GIMP_IMAGE(image));
  117. }
  118.  
  119. void
  120. gimage_invalidate_previews (void)
  121. {
  122.   gimp_set_foreach (image_context, invalidate_cb, NULL);
  123. }
  124.  
  125. static void
  126. gimage_dirty_handler (GimpImage *gimage)
  127. {
  128.   if (active_tool && !active_tool->preserve)
  129.     {
  130.       GDisplay* gdisp = active_tool->gdisp_ptr;
  131.  
  132.       if (gdisp)
  133.     {
  134.       if (gdisp->gimage == gimage)
  135.         tools_initialize (active_tool->type, gdisp);
  136.       else
  137.         tools_initialize (active_tool->type, NULL);
  138.     }
  139.     }
  140. }
  141.  
  142. static void
  143. gimlist_cb (gpointer im, 
  144.         gpointer data)
  145. {
  146.   GSList** l=(GSList**)data;
  147.   *l=g_slist_prepend(*l, im);
  148. }
  149.  
  150. gint
  151. gimage_image_count (void)
  152. {
  153.   GSList *list=NULL;
  154.   gint num_images = 0;
  155.  
  156.   gimage_foreach (gimlist_cb, &list);
  157.   num_images = g_slist_length (list);
  158.  
  159.   g_slist_free (list);
  160.  
  161.   return (num_images);
  162. }
  163.  
  164. static void
  165. gimage_destroy_handler (GimpImage *gimage)
  166. {
  167.   GList *list;
  168.   
  169.   /*  free the undo list  */
  170.   undo_free (gimage);
  171.  
  172.   /*  free all guides  */
  173.   list = gimage->guides;
  174.   while (list)
  175.     {
  176.       g_free ((Guide*) list->data);
  177.       list = g_list_next (list);
  178.     }
  179.   g_list_free (gimage->guides);
  180.  
  181.   if (gimage_image_count () == 1)  /*  This is the last image  */
  182.     {
  183.       dialog_show_toolbox ();
  184.     }
  185. }
  186.  
  187. static void 
  188. gimage_cmap_change_handler (GimpImage *gimage, 
  189.                 gint       ncol,
  190.                 gpointer   user_data)
  191. {
  192.   gdisplays_update_full (gimage);
  193.  
  194.   if (gimp_image_base_type (gimage) == INDEXED)
  195.     paint_funcs_invalidate_color_hash_table (gimage, ncol);
  196. }
  197.  
  198. static void
  199. gimage_rename_handler (GimpImage *gimage)
  200. {
  201.   gdisplays_update_title (gimage);
  202.   lc_dialog_update_image_list ();
  203.  
  204.   palette_import_image_renamed (gimage);
  205. }
  206.  
  207. static void
  208. gimage_resize_handler (GimpImage *gimage)
  209. {
  210.   undo_push_group_end (gimage);
  211.  
  212.   /*  shrink wrap and update all views  */
  213.   channel_invalidate_previews (gimage);
  214.   layer_invalidate_previews (gimage);
  215.   gimp_image_invalidate_preview (gimage);
  216.   gdisplays_resize_cursor_label (gimage);
  217.   gdisplays_update_full (gimage);
  218.   gdisplays_shrink_wrap (gimage);
  219. }
  220.  
  221. static void
  222. gimage_restructure_handler (GimpImage* gimage)
  223. {
  224.   gdisplays_update_title (gimage);
  225. }
  226.  
  227. static void
  228. gimage_repaint_handler (GimpImage *gimage, 
  229.             gint       x, 
  230.             gint       y, 
  231.             gint       w, 
  232.             gint       h)
  233. {
  234.   gdisplays_update_area (gimage, x, y, w, h);
  235. }
  236.  
  237.   
  238. /* These really belong in the layer class */
  239.  
  240. void
  241. gimage_set_layer_mask_apply (GImage    *gimage, 
  242.                  GimpLayer *layer)
  243. {
  244.   int off_x, off_y;
  245.  
  246.   g_return_if_fail (gimage);
  247.   g_return_if_fail (layer);
  248.   
  249.   if (! layer->mask)
  250.     return;
  251.  
  252.   layer->apply_mask = ! layer->apply_mask;
  253.   drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  254.   gdisplays_update_area (gimage, off_x, off_y,
  255.              drawable_width (GIMP_DRAWABLE (layer)), 
  256.              drawable_height (GIMP_DRAWABLE (layer)));
  257. }
  258.  
  259. void
  260. gimage_set_layer_mask_edit (GImage   *gimage, 
  261.                 Layer    *layer, 
  262.                 gboolean  edit)
  263. {
  264.   /*  find the layer  */
  265.   if (!layer)
  266.     return;
  267.  
  268.   if (layer->mask)
  269.     layer->edit_mask = edit;
  270. }
  271.  
  272. void
  273. gimage_set_layer_mask_show (GImage    *gimage, 
  274.                 GimpLayer *layer)
  275. {
  276.   int off_x, off_y;
  277.  
  278.   g_return_if_fail (gimage);
  279.   g_return_if_fail (layer);
  280.   
  281.   if (! layer->mask)
  282.     return;
  283.  
  284.   layer->show_mask = ! layer->show_mask;
  285.   drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  286.   gdisplays_update_area (gimage, off_x, off_y,
  287.              drawable_width (GIMP_DRAWABLE (layer)), 
  288.              drawable_height (GIMP_DRAWABLE (layer)));
  289. }
  290.  
  291. void
  292. gimage_foreach (GFunc    func, 
  293.         gpointer user_data)
  294. {
  295.   gimp_set_foreach (image_context, func, user_data);
  296. }
  297.