1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) Widget Image
--
-- Author : Axel Simon
--
-- Created: 23 May 2001
--
-- Copyright (C) 2001-2005 Axel Simon
--
-- 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.
--
-- TODO
--
-- Figure out what other functions are useful within Haskell. Maybe we should
-- support loading Pixmaps without exposing them.
--
-- Because Haskell is not the best language to modify large images directly
-- only functions are bound that allow loading images from disc or by stock
-- names.
--
-- Another function for extracting the 'Pixbuf' is added for
-- 'CellRenderer'.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- A widget displaying an image
--
module Graphics.UI.Gtk.Display.Image (
-- * Detail
--
-- | The 'Image' widget displays an image. Various kinds of object can be
-- displayed as an image; most typically, you would load a 'Pixbuf' (\"pixel
-- buffer\") from a file, and then display that. There's a convenience function
-- to do this, 'imageNewFromFile', used as follows: If the file isn't loaded
-- successfully, the image will contain a \"broken image\" icon similar to that
-- used in many web browsers. If you want to handle errors in loading the file
-- yourself, for example by displaying an error message, then load the image
-- with 'Graphics.UI.Gtk.Gdk.Pixbuf.pixbufNewFromFile', then create the
-- 'Image' with 'imageNewFromPixbuf'.
--
-- > image <- imageNewFromFile "myfile.png"
--
-- The image file may contain an animation, if so the 'Image' will display
-- an animation ('PixbufAnimation') instead of a static image.
--
-- 'Image' is a subclass of 'Misc', which implies that you can align it
-- (center, left, right) and add padding to it, using 'Misc' methods.
--
-- 'Image' is a \"no window\" widget (has no 'DrawWindow' of its own), so by
-- default does not receive events. If you want to receive events on the image,
-- such as button clicks, place the image inside a 'EventBox', then connect to
-- the event signals on the event box.
--
-- When handling events on the event box, keep in mind that coordinates in
-- the image may be different from event box coordinates due to the alignment
-- and padding settings on the image (see 'Misc'). The simplest way to solve
-- this is to set the alignment to 0.0 (left\/top), and set the padding to
-- zero. Then the origin of the image will be the same as the origin of the
-- event box.
--
-- Sometimes an application will want to avoid depending on external data
-- files, such as image files. Gtk+ comes with a program to avoid this, called
-- gdk-pixbuf-csource. This program allows you to convert an image into a C
-- variable declaration, which can then be loaded into a 'Pixbuf' using
-- 'Graphics.UI.Gtk.Gdk.Pixbuf.pixbufNewFromInline'.
-- * Class Hierarchy
-- |
-- @
-- | 'GObject'
-- | +----'Object'
-- | +----'Widget'
-- | +----'Misc'
-- | +----Image
-- @
-- * Types
Image,
ImageClass,
castToImage, gTypeImage,
toImage,
ImageType(..),
-- * Constructors
imageNewFromFile,
imageNewFromPixbuf,
imageNewFromAnimation,
imageNewFromStock,
imageNew,
#if GTK_CHECK_VERSION(2,6,0)
imageNewFromIconName,
#endif
-- * Methods
imageGetPixbuf,
imageSetFromPixbuf,
imageSetFromAnimation,
imageSetFromFile,
imageSetFromStock,
#if GTK_CHECK_VERSION(2,6,0)
imageSetFromIconName,
imageSetPixelSize,
imageGetPixelSize,
#endif
#if GTK_CHECK_VERSION(2,8,0)
imageClear,
#endif
-- * Icon Sizes
IconSize(..),
-- * Attributes
imagePixbuf,
imagePixmap,
imageAnimation,
imageImage,
imageMask,
imageFile,
imageStock,
imageIconSize,
#if GTK_CHECK_VERSION(2,6,0)
imagePixelSize,
#endif
#if GTK_CHECK_VERSION(2,6,0)
imageIconName,
#endif
imageStorageType,
) where
import Control.Monad (liftM)
import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.Attributes
import System.Glib.Properties
import Graphics.UI.Gtk.Abstract.Object (makeNewObject)
import System.Glib.GObject (makeNewGObject)
{#import Graphics.UI.Gtk.Types#}
import Graphics.UI.Gtk.General.StockItems
import Graphics.UI.Gtk.General.Structs (IconSize(..))
{# context lib="gtk" prefix="gtk" #}
--------------------
-- Types
-- | Describes the image data representation used by a 'Image'. If you want to
-- get the image from the widget, you can only get the currently-stored
-- representation. e.g. if the 'imageStorageType' is 'ImagePixbuf',
-- then you can call 'imageGetPixbuf' but not 'imageGetStock'. For empty
-- images, you can request any storage type (call any of the "get" functions),
-- but they will all return @Nothing@.
--
{# enum ImageType {underscoreToCase} deriving (Show, Eq) #}
--------------------
-- Constructors
-- | Creates a new 'Image' displaying the file @filename@. If the file isn't
-- found or can't be loaded, the resulting 'Image' will display a \"broken
-- image\" icon.
--
-- If the file contains an animation, the image will contain an animation.
--
-- If you need to detect failures to load the file, use
-- 'Graphics.UI.Gtk.Gdk.Pixbuf.pixbufNewFromFile'
-- to load the file yourself, then create the 'Image' from the pixbuf. (Or for
-- animations, use
-- 'Graphics.UI.Gtk.Gdk.Pixbuf.pixbufAnimationNewFromFile').
--
-- The storage type ('imageGetStorageType') of the returned image is not
-- defined, it will be whatever is appropriate for displaying the file.
--
imageNewFromFile :: FilePath -> IO Image
imageNewFromFile filename =
makeNewObject mkImage $
liftM (castPtr :: Ptr Widget -> Ptr Image) $
withUTFString filename $ \filenamePtr ->
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
{# call unsafe gtk_image_new_from_file_utf8 #}
#else
{# call unsafe gtk_image_new_from_file #}
#endif
filenamePtr
-- | Creates a new 'Image' displaying a 'Pixbuf'.
--
-- Note that this function just creates an 'Image' from the pixbuf. The
-- 'Image' created will not react to state changes. Should you want that, you
-- should use 'imageNewFromIconSet'.
--
imageNewFromPixbuf :: Pixbuf -> IO Image
imageNewFromPixbuf pixbuf =
makeNewObject mkImage $
liftM (castPtr :: Ptr Widget -> Ptr Image) $
{# call unsafe image_new_from_pixbuf #}
pixbuf
imageNewFromAnimation :: (PixbufAnimationClass animation) => animation -> IO Image
imageNewFromAnimation pba = makeNewObject mkImage $
liftM (castPtr :: Ptr Widget -> Ptr Image) $
{# call unsafe image_new_from_animation #} (toPixbufAnimation pba)
-- | Creates a 'Image' displaying a stock icon. If the stock icon name isn't
-- known, the image will be empty.
--
imageNewFromStock ::
StockId -- ^ @stockId@ - a stock icon name
-> IconSize -- ^ @size@ - a stock icon size
-> IO Image
imageNewFromStock stockId size =
makeNewObject mkImage $
liftM (castPtr :: Ptr Widget -> Ptr Image) $
withUTFString stockId $ \stockIdPtr ->
{# call unsafe image_new_from_stock #}
stockIdPtr
((fromIntegral . fromEnum) size)
-- | Creates a new empty 'Image' widget.
--
imageNew :: IO Image
imageNew =
makeNewObject mkImage $
liftM (castPtr :: Ptr Widget -> Ptr Image) $
{# call gtk_image_new #}
#if GTK_CHECK_VERSION(2,6,0)
-- | Creates a 'Image' displaying an icon from the current icon theme. If the
-- icon name isn't known, a \"broken image\" icon will be displayed instead. If
-- the current icon theme is changed, the icon will be updated appropriately.
--
-- * Available since Gtk+ version 2.6
--
imageNewFromIconName ::
String -- ^ @iconName@ - an icon name
-> IconSize -- ^ @size@ - a stock icon size
-> IO Image
imageNewFromIconName iconName size =
makeNewObject mkImage $
liftM (castPtr :: Ptr Widget -> Ptr Image) $
withUTFString iconName $ \iconNamePtr ->
{# call gtk_image_new_from_icon_name #}
iconNamePtr
((fromIntegral . fromEnum) size)
#endif
--------------------
-- Methods
-- | Gets the 'Pixbuf' being displayed by the 'Image'. The storage type of the
-- image must be 'ImageEmpty' or 'ImagePixbuf' (see 'imageGetStorageType').
--
imageGetPixbuf :: Image -> IO Pixbuf
imageGetPixbuf self =
makeNewGObject mkPixbuf $ liftM castPtr $
throwIfNull "Image.imageGetPixbuf: The image contains no Pixbuf object." $
{# call unsafe image_get_pixbuf #}
self
-- | Overwrite the current content of the 'Image' with a new 'Pixbuf'.
--
imageSetFromPixbuf :: Image -> Pixbuf -> IO ()
imageSetFromPixbuf self pixbuf =
{# call unsafe gtk_image_set_from_pixbuf #}
self
pixbuf
imageSetFromAnimation :: (PixbufAnimationClass animation) => Image -> animation -> IO ()
imageSetFromAnimation self pba =
{# call unsafe gtk_image_set_from_animation #}
self
(toPixbufAnimation pba)
-- | See 'imageNewFromFile' for details.
--
imageSetFromFile :: Image -> FilePath -> IO ()
imageSetFromFile self filename =
withUTFString filename $ \filenamePtr ->
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
{# call gtk_image_set_from_file_utf8 #}
#else
{# call gtk_image_set_from_file #}
#endif
self
filenamePtr
-- | See 'imageNewFromStock' for details.
--
imageSetFromStock :: Image
-> StockId -- ^ @stockId@ - a stock icon name
-> IconSize -- ^ @size@ - a stock icon size
-> IO ()
imageSetFromStock self stockId size =
withUTFString stockId $ \stockIdPtr ->
{# call gtk_image_set_from_stock #}
self
stockIdPtr
((fromIntegral . fromEnum) size)
#if GTK_CHECK_VERSION(2,6,0)
-- | See 'imageNewFromIconName' for details.
--
-- * Available since Gtk+ version 2.6
--
imageSetFromIconName :: Image
-> String -- ^ @iconName@ - an icon name
-> IconSize -- ^ @size@ - an icon size
-> IO ()
imageSetFromIconName self iconName size =
withUTFString iconName $ \iconNamePtr ->
{# call gtk_image_set_from_icon_name #}
self
iconNamePtr
((fromIntegral . fromEnum) size)
-- | Sets the pixel size to use for named icons. If the pixel size is set to a
-- @value \/= -1@, it is used instead of the icon size set by
-- 'imageSetFromIconName'.
--
-- * Available since Gtk+ version 2.6
--
imageSetPixelSize :: Image
-> Int -- ^ @pixelSize@ - the new pixel size
-> IO ()
imageSetPixelSize self pixelSize =
{# call gtk_image_set_pixel_size #}
self
(fromIntegral pixelSize)
-- | Gets the pixel size used for named icons.
--
-- * Available since Gtk+ version 2.6
--
imageGetPixelSize :: Image -> IO Int
imageGetPixelSize self =
liftM fromIntegral $
{# call gtk_image_get_pixel_size #}
self
#endif
#if GTK_CHECK_VERSION(2,8,0)
-- | Resets the image to be empty.
--
-- * Available since Gtk+ version 2.8
--
imageClear :: Image -> IO ()
imageClear self =
{# call gtk_image_clear #}
self
#endif
--------------------
-- Attributes
-- | A 'Pixbuf' to display.
--
imagePixbuf :: PixbufClass pixbuf => ReadWriteAttr Image Pixbuf pixbuf
imagePixbuf = newAttrFromObjectProperty "pixbuf"
{# call pure unsafe gdk_pixbuf_get_type #}
imageAnimation :: (PixbufClass pixbuf, PixbufAnimationClass animation) => ReadWriteAttr Image animation pixbuf
imageAnimation = newAttrFromObjectProperty "pixbuf-animation"
{# call pure unsafe gdk_pixbuf_get_type #}
-- | A 'Pixmap' to display.
--
imagePixmap :: PixmapClass pixmap => ReadWriteAttr Image Pixmap pixmap
imagePixmap = newAttrFromObjectProperty "pixmap"
{# call pure unsafe gdk_pixmap_get_type #}
-- | A 'Image' to display.
--
imageImage :: ImageClass image => ReadWriteAttr Image Image image
imageImage = newAttrFromObjectProperty "image"
{# call pure unsafe gtk_image_get_type #}
-- | Mask bitmap to use with 'Image' or 'Pixmap'.
--
imageMask :: PixmapClass pixmap => ReadWriteAttr Image Pixmap pixmap
imageMask = newAttrFromObjectProperty "mask"
{# call pure unsafe gdk_pixmap_get_type #}
-- | Filename to load and display.
--
-- Default value: \"\"
--
imageFile :: Attr Image String
imageFile = newAttrFromStringProperty "file"
-- | Stock ID for a stock image to display.
--
-- Default value: \"\"
--
imageStock :: Attr Image String
imageStock = newAttrFromStringProperty "stock"
-- | Symbolic size to use for stock icon, icon set or named icon.
--
-- Allowed values: >= 0
--
-- Default value: 4
--
imageIconSize :: Attr Image Int
imageIconSize = newAttrFromIntProperty "icon-size"
#if GTK_CHECK_VERSION(2,6,0)
-- | The pixel-size property can be used to specify a fixed size overriding
-- the icon-size property for images of type 'ImageIconName'.
--
-- Allowed values: >= -1
--
-- Default value: -1
--
imagePixelSize :: Attr Image Int
imagePixelSize = newAttr
imageGetPixelSize
imageSetPixelSize
#endif
#if GTK_CHECK_VERSION(2,6,0)
-- | The name of the icon in the icon theme. If the icon theme is changed, the
-- image will be updated automatically.
--
-- Default value: \"\"
--
imageIconName :: Attr Image String
imageIconName = newAttrFromStringProperty "icon-name"
#endif
-- | The representation being used for image data.
--
-- Default value: 'ImageEmpty'
--
imageStorageType :: ReadAttr Image ImageType
imageStorageType = readAttrFromEnumProperty "storage-type"
{# call pure unsafe gtk_image_type_get_type #}
|