/* Copyright (C) 1998-2002 The gtkmm Development Team
 *
 * 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 2.1 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
 * Lesser 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 <https://www.gnu.org/licenses/>.
 */

#include <glibmm/object.h>
#include <gdkmm/pixbuf.h>
#ifndef GDKMM_DISABLE_DEPRECATED
#include <gdkmm/pixbufanimation.h>
#endif
#include <gdkmm/pixbufformat.h>

_DEFS(gdkmm,gdk)
_PINCLUDE(glibmm/private/object_p.h)

namespace Gdk
{
/** Incremental image loader.
 *
 * `%Gdk::PixbufLoader` provides a way for applications to drive the
 * process of loading an image, by letting them send the image data
 * directly to the loader instead of having the loader read the data
 * from a file. Applications can use this functionality instead of
 * `Gdk::Pixbuf::create_from_file()` or `Gdk::PixbufAnimation::create_from_file()`
 * when they need to parse image data in small chunks. For example,
 * it should be used when reading an image from a (potentially) slow
 * network connection, or when loading an extremely large file.
 *
 * To use `GdkPixbufLoader` to load an image, create a new instance,
 * and call write() to send the data to it. When done, close() should be
 * called to end the stream and finalize everything.
 *
 * The loader will emit three important signals throughout the process:
 *
 *  - signal_size_prepared() will be emitted as
 *    soon as the image has enough information to determine the size of
 *    the image to be used. If you want to scale the image while loading
 *    it, you can call set_size() in response to this signal.
 *  - signal_area_prepared() will be emitted as
 *    soon as the pixbuf of the desired size has been allocated.
 *    You can obtain the Gdk::Pixbuf instance by calling get_pixbuf().
 *    You can also call get_pixbuf() later to get the same pixbuf.
 *  - signal_area_updated() will be emitted every
 *    time a region is updated. This way you can update a partially
 *    completed image. Note that you do not know anything about the
 *    completeness of an image from the updated area. For example, in an
 *    interlaced image you will need to make several passes before the
 *    image is done loading.
 *
 * ## Loading an animation (deprecated from gtkmm 4.20)
 *
 * Loading an animation is almost as easy as loading an image. Once the
 * first signal_area_prepared() has been emitted, you can call get_animation() to
 * get the Gdk::PixbufAnimation instance, and then call
 * and Gdk::PixbufAnimation::get_iter() to get a Gdk::PixbufAnimationIter
 * to retrieve the pixbuf for the desired time stamp.
 */
class GDKMM_API PixbufLoader : public Glib::Object
{
  _CLASS_GOBJECT(PixbufLoader, GdkPixbufLoader, GDK_PIXBUF_LOADER, Glib::Object, GObject, , , GDKMM_API)
protected:

  /** Constructs a pixbuf loader object.
   */
  _CTOR_DEFAULT()

  /** Constructs a pixbuf loader object for a specific image type.
   * Constructs a new pixbuf loader object that always attempts to parse image
   * data as if it were an image of type @a image_type, instead of identifying
   * the type automatically. Useful if you want an error if the image isn't the
   * expected type, for loading image formats that can't be reliably identified
   * by looking at the data, or if the user manually forces a specific type.
   *
   * The image_type can be one of the types returned by Gdk::Pixbuf::get_formats(), or a MIME type.
   *
   * Common MIME Types are listed here
   * http://www.iana.org/assignments/media-types/
   * and here:
   * http://www.freedesktop.org/Software/shared-mime-info
   *
   * The list of non-mime-type image formats depends on what image loaders
   * are installed, but typically "png", "jpeg", "gif", "tiff" and
   * "xpm" are among the supported formats. To obtain the full list of
   * supported image formats, call PixbufFormat::get_name() on each
   * of the PixbufFormats returned by Gdk::Pixbuf::get_formats().
   *
   * @param image_type Name of the image format to be loaded with the image.
   * @param mime_type If this is true then the image_type is a MIME type.
   * @throw Gdk::PixbufError
   */
  explicit PixbufLoader(const Glib::ustring& image_type, bool mime_type = false);

public:

  /** Creates a new pixbuf loader object.
   * @return A newly-created pixbuf loader.
   */
  _WRAP_CREATE()

   /** Constructs a pixbuf loader object for a specific image type.
   * Constructs a new pixbuf loader object that always attempts to parse image
   * data as if it were an image of type @a image_type, instead of identifying
   * the type automatically. Useful if you want an error if the image isn't the
   * expected type, for loading image formats that can't be reliably identified
   * by looking at the data, or if the user manually forces a specific type.
   *
   * The image_type can be one of the types returned by Gdk::Pixbuf::get_formats(), or a MIME type.
   *
   * Common MIME Types are listed here
   * http://www.iana.org/assignments/media-types/
   * and here:
   * http://www.freedesktop.org/Software/shared-mime-info
   *
   * The list of non-mime-type image formats depends on what image loaders
   * are installed, but typically "png", "jpeg", "gif", "tiff" and
   * "xpm" are among the supported formats. To obtain the full list of
   * supported image formats, call PixbufFormat::get_name() on each
   * of the PixbufFormats returned by Gdk::Pixbuf::get_formats().
   *
   * @param image_type Name of the image format to be loaded with the image.
   * @param mime_type If this is true then the image_type is a MIME type.
   * @throw Gdk::PixbufError
   */
  _WRAP_CREATE(const Glib::ustring& image_type, bool mime_type = false)

  _WRAP_METHOD(void set_size(int width, int height), gdk_pixbuf_loader_set_size)

#m4 _CONVERSION(`const guint8*',`const guchar*',`($3)')

  /** Causes the pixbuf loader to parse the next @a count bytes of an image.
   * It will return if the data was loaded successfully, and throw an exception
   * if an error occurred. In the latter case, the loader will be closed, and
   * will not accept further writes.
   * @param buf Pointer to image data.
   * @param count Length of the @a buf buffer in bytes.
   * @throw Gdk::PixbufError
   * @throw Glib::FileError
   */
  _WRAP_METHOD(void write(const guint8* buf, gsize count), gdk_pixbuf_loader_write, errthrow)

  /** Informs a pixbuf loader that no further writes with PixbufLoader::write()
   * will occur, so that it can free its internal loading structures.  Also,
   * tries to parse any data that hasn't yet been parsed; if the remaining data
   * is partial or corrupt, an exception will be thrown. If you're just
   * cancelling a load rather than expecting it to be finished, writing an
   * empty <tt>catch()</tt> block to ignore errors is reasonable.
   * @throw Gdk::PixbufError
   * @throw Glib::FileError
   */
  _WRAP_METHOD(void close(), gdk_pixbuf_loader_close, errthrow)

  _WRAP_METHOD(Glib::RefPtr<Gdk::Pixbuf> get_pixbuf(), gdk_pixbuf_loader_get_pixbuf, refreturn)
  _WRAP_METHOD(Glib::RefPtr<Gdk::PixbufAnimation> get_animation(), gdk_pixbuf_loader_get_animation,
    refreturn, deprecated "4.20: Pixbuf animation is deprecated. There is no replacement.")

  _WRAP_METHOD(PixbufFormat get_format() const, gdk_pixbuf_loader_get_format)

  _WRAP_SIGNAL(void area_prepared(), "area_prepared")
  _WRAP_SIGNAL(void area_updated(int x, int y, int width, int height), "area_updated")
  _WRAP_SIGNAL(void closed(), "closed")
  _WRAP_SIGNAL(void size_prepared(int width, int height), "size_prepared")
};

} // namespace Gdk
