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

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995-1999 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 "image_new.h"
  24.  
  25. #include "appenv.h"
  26. #include "apptypes.h"
  27. #include "gimprc.h"
  28. #include "file_new_dialog.h"
  29. #include "tile_manager_pvt.h"
  30. #include "gdisplay.h"
  31. #include "gimpcontext.h"
  32. #include "gimage.h"
  33.  
  34. #include "libgimp/gimpparasite.h"
  35.  
  36. #include "libgimp/gimpintl.h"
  37.  
  38. #include "pixmaps/wilber2.xpm"
  39.  
  40.  
  41. static GList              *image_base_type_names = NULL;
  42. static GList              *fill_type_names = NULL;
  43. static GimpImageNewValues  last_values;
  44. static gboolean            current_cut_buffer = FALSE;
  45.  
  46. extern TileManager        *global_buf;
  47.  
  48. static void
  49. image_new_init (void)
  50. {
  51.   static gboolean image_new_inited = FALSE;
  52.  
  53.   GimpImageBaseTypeName *new_type;
  54.   GimpFillTypeName      *new_fill_type;
  55.  
  56.   if (image_new_inited)
  57.     return;
  58.   else
  59.     image_new_inited = TRUE;
  60.  
  61.   /* Available Image Base Types */
  62.   new_type = g_new (GimpImageBaseTypeName, 1);
  63.   new_type->type = RGB;
  64.   new_type->name = _("RGB");
  65.   image_base_type_names = g_list_append (image_base_type_names, new_type);
  66.  
  67.   new_type = g_new (GimpImageBaseTypeName, 1);
  68.   new_type->type = GRAY;
  69.   new_type->name = _("Grayscale");
  70.   image_base_type_names = g_list_append (image_base_type_names, new_type);
  71.   
  72.   /* Available Fill Types */
  73.   new_fill_type = g_new (GimpFillTypeName, 1);
  74.   new_fill_type->type = FOREGROUND_FILL;
  75.   new_fill_type->name = _("Foreground");
  76.   fill_type_names = g_list_append (fill_type_names, new_fill_type);
  77.  
  78.   new_fill_type = g_new (GimpFillTypeName, 1);
  79.   new_fill_type->type = BACKGROUND_FILL;
  80.   new_fill_type->name = _("Background");
  81.   fill_type_names = g_list_append (fill_type_names, new_fill_type);
  82.  
  83.   new_fill_type = g_new (GimpFillTypeName, 1);
  84.   new_fill_type->type = WHITE_FILL;
  85.   new_fill_type->name = _("White");
  86.   fill_type_names = g_list_append (fill_type_names, new_fill_type);
  87.  
  88.   new_fill_type = g_new (GimpFillTypeName, 1);
  89.   new_fill_type->type = TRANSPARENT_FILL;
  90.   new_fill_type->name = _("Transparent");
  91.   fill_type_names = g_list_append (fill_type_names, new_fill_type);
  92.  
  93.   /* Set the last values used to default values. */
  94.   last_values.width = default_width;
  95.   last_values.height = default_height;
  96.   last_values.unit = default_units;
  97.   last_values.xresolution = default_xresolution;
  98.   last_values.yresolution = default_yresolution;
  99.   last_values.res_unit = default_resolution_units;
  100.   last_values.type = default_type;
  101.   last_values.fill_type = BACKGROUND_FILL;
  102. }
  103.  
  104. GList *
  105. image_new_get_image_base_type_names (void)
  106. {
  107.   image_new_init ();
  108.  
  109.   return image_base_type_names;
  110. }
  111.  
  112. GList *
  113. image_new_get_fill_type_names (void)
  114. {
  115.   image_new_init ();
  116.  
  117.   return fill_type_names;
  118. }
  119.  
  120. void
  121. image_new_create_window (const GimpImageNewValues *create_values,
  122.                          const GimpImage          *image)
  123. {
  124.   GimpImageNewValues *values;
  125.  
  126.   image_new_init ();
  127.  
  128.   values = image_new_values_new (create_values);
  129.   
  130.   if (image)
  131.     {
  132.       values->width = gimp_image_get_width (image);
  133.       values->height = gimp_image_get_height (image);
  134.       values->unit = gimp_image_get_unit (image);
  135.       
  136.       gimp_image_get_resolution (image, 
  137.                  &values->xresolution, 
  138.                  &values->yresolution);
  139.  
  140.       values->type = gimp_image_base_type (image);
  141.  
  142.       if (values->type == INDEXED)
  143.         values->type = RGB; /* no indexed images */
  144.     }
  145.  
  146.   /*  If a cut buffer exists, default to using its size for the new image
  147.    *  also check to see if a new_image has been opened
  148.    */
  149.   if (global_buf && current_cut_buffer)
  150.     {
  151.       values->width = global_buf->width;
  152.       values->height = global_buf->height;
  153.     }
  154.  
  155.   ui_new_image_window_create (values);
  156.  
  157.   image_new_values_free (values);
  158. }
  159.  
  160. void
  161. image_new_set_default_values (const GimpImageNewValues *values)
  162. {
  163.   g_return_if_fail (values != NULL);
  164.  
  165.   image_new_init ();
  166.  
  167.   memcpy(&last_values, values, sizeof (GimpImageNewValues));
  168.  
  169.   current_cut_buffer = FALSE;
  170. }
  171.  
  172. GimpImageNewValues*
  173. image_new_values_new (const GimpImageNewValues *src_values)
  174. {
  175.   GimpImageNewValues *values;
  176.  
  177.   image_new_init ();
  178.  
  179.   values = g_new (GimpImageNewValues, 1);
  180.  
  181.   if (src_values)
  182.     memcpy(values, src_values, sizeof (GimpImageNewValues));
  183.   else
  184.     memcpy(values, &last_values, sizeof (GimpImageNewValues));
  185.  
  186.   return values;
  187. }
  188.  
  189. void
  190. image_new_values_free (GimpImageNewValues *values)
  191. {
  192.   g_return_if_fail (values != NULL);
  193.  
  194.   g_free (values);
  195. }
  196.  
  197. static gint
  198. image_new_rotate_the_shield_harmonics (GtkWidget    *widget,
  199.                        GdkEvent     *eevent,
  200.                        gpointer      data)
  201. {
  202.   GdkPixmap *pixmap = NULL;
  203.   GdkBitmap *mask   = NULL;
  204.   gint width  = 0;
  205.   gint height = 0;
  206.  
  207.   gtk_signal_disconnect_by_func 
  208.     (GTK_OBJECT (widget), 
  209.      GTK_SIGNAL_FUNC (image_new_rotate_the_shield_harmonics), data);
  210.  
  211.   pixmap =
  212.     gdk_pixmap_create_from_xpm_d (widget->window,
  213.                   &mask,
  214.                   NULL,
  215.                   wilber2_xpm);
  216.  
  217.   gdk_window_get_size (pixmap, &width, &height);
  218.  
  219.   if (widget->allocation.width  >= width &&
  220.       widget->allocation.height >= height)
  221.     {
  222.       gint x, y;
  223.  
  224.       x = (widget->allocation.width  - width) / 2;
  225.       y = (widget->allocation.height - height) / 2;
  226.  
  227.       gdk_gc_set_clip_mask (widget->style->black_gc, mask);
  228.       gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
  229.  
  230.       gdk_draw_pixmap (widget->window,
  231.                widget->style->black_gc,
  232.                pixmap, 0, 0,
  233.                x, y,
  234.                width, height);
  235.  
  236.       gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
  237.       gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
  238.     }
  239.  
  240.   gdk_pixmap_unref (pixmap);
  241.   gdk_bitmap_unref (mask);
  242.  
  243.   return FALSE;
  244. }
  245.  
  246. void 
  247. image_new_create_image (const GimpImageNewValues *values)
  248. {
  249.   GimpImage *image;
  250.   GDisplay *display;
  251.   Layer *layer;
  252.   GimpImageType type;
  253.   GimpParasite *comment_parasite;
  254.   gint width, height;
  255.  
  256.   g_return_if_fail (values != NULL);
  257.  
  258.   image_new_set_default_values (values);
  259.  
  260.   switch (values->fill_type)
  261.     {
  262.     case FOREGROUND_FILL:
  263.     case BACKGROUND_FILL:
  264.     case WHITE_FILL:
  265.       type = (values->type == RGB) ? RGB_GIMAGE : GRAY_GIMAGE;
  266.       break;
  267.     case TRANSPARENT_FILL:
  268.       type = (values->type == RGB) ? RGBA_GIMAGE : GRAYA_GIMAGE;
  269.       break;
  270.     default:
  271.       type = RGB_GIMAGE; 
  272.       break;
  273.     }
  274.  
  275.   image = gimage_new (values->width, values->height, values->type);
  276.   
  277.   gimp_image_set_resolution (image, values->xresolution, values->yresolution);
  278.   gimp_image_set_unit (image, values->unit);
  279.  
  280.   if (default_comment)
  281.     {
  282.       comment_parasite = gimp_parasite_new ("gimp-comment",
  283.                         GIMP_PARASITE_PERSISTENT,
  284.                         strlen (default_comment) + 1,
  285.                         (gpointer) default_comment);
  286.       gimp_image_parasite_attach (image, comment_parasite);
  287.       gimp_parasite_free (comment_parasite);
  288.     }
  289.   
  290.   /*  Make the background (or first) layer  */
  291.   width = gimp_image_get_width (image);
  292.   height = gimp_image_get_height (image);
  293.   layer = layer_new (image, width, height,
  294.                      type, _("Background"),
  295.              OPAQUE_OPACITY, NORMAL_MODE);
  296.  
  297.   if (layer)
  298.     {
  299.       /*  add the new layer to the gimage  */
  300.       gimp_image_undo_disable (image);
  301.       gimp_image_add_layer (image, layer, 0);
  302.       gimp_image_undo_enable (image);
  303.  
  304.       drawable_fill (GIMP_DRAWABLE (layer), values->fill_type);
  305.  
  306.       gimp_image_clean_all (image);
  307.  
  308.       display = gdisplay_new (image, 0x0101);
  309.  
  310.       gimp_context_set_display (gimp_context_get_user (), display);
  311.  
  312.       if (double_speed)
  313.     gtk_signal_connect_after
  314.       (GTK_OBJECT (display->canvas), "expose_event",
  315.        GTK_SIGNAL_FUNC (image_new_rotate_the_shield_harmonics),
  316.        NULL);
  317.     }
  318. }
  319.  
  320. gdouble
  321. image_new_calculate_size (GimpImageNewValues *values)
  322. {
  323.   gdouble width, height, size;
  324.  
  325.   width = (gdouble) values->width;
  326.   height = (gdouble) values->height;
  327.  
  328.   size = 
  329.     width * height *
  330.       ((values->type == RGB ? 3 : 1) +                   /* bytes per pixel */
  331.        (values->fill_type == TRANSPARENT_FILL ? 1 : 0)); /* alpha channel */
  332.  
  333.   return size;
  334. }
  335.  
  336. gchar *
  337. image_new_get_size_string (gdouble size)
  338. {
  339.   if (size < 4096)
  340.     return g_strdup_printf (_("%d Bytes"), (gint) size);
  341.   else if (size < 1024 * 10)
  342.     return g_strdup_printf (_("%.2f KB"), size / 1024);
  343.   else if (size < 1024 * 100)
  344.     return g_strdup_printf (_("%.1f KB"), size / 1024);
  345.   else if (size < 1024 * 1024)
  346.     return g_strdup_printf (_("%d KB"), (gint) size / 1024);
  347.   else if (size < 1024 * 1024 * 10)
  348.     return g_strdup_printf (_("%.2f MB"), size / 1024 / 1024);
  349.   else
  350.     return g_strdup_printf (_("%.1f MB"), size / 1024 / 1024);
  351. }
  352.  
  353. void
  354. image_new_reset_current_cut_buffer (void)
  355. {
  356.   /* This function just changes the status of current_cut_buffer
  357.      if there hass been a cut/copy since the last file new */
  358.   current_cut_buffer = TRUE;
  359. }
  360.  
  361.