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
|
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) IconFactory
--
-- Author : Axel Simon
--
-- Created: 24 May 2001
--
-- Copyright (C) 1999-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
--
-- The following functions are not bound:
-- iconFactoryLookup, iconFactoryLookupDefault
-- It is not a good idea to lookup an IconSet directly. If an Icon needs to
-- be displayed it happends always in the context of a widget. The best
-- practice is to get the widgets Style and call styleLookupIconSet.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- Manipulating stock icons
--
module Graphics.UI.Gtk.General.IconFactory (
-- * Detail
--
-- | Browse the available stock icons in the list of stock IDs found here. You
-- can also use the gtk-demo application for this purpose.
--
-- An icon factory manages a collection of 'IconSet'; a 'IconSet' manages a
-- set of variants of a particular icon (i.e. a 'IconSet' contains variants for
-- different sizes and widget states). Icons in an icon factory are named by a
-- stock ID, which is a simple string identifying the icon. Each 'Style' has a
-- list of 'IconFactory' derived from the current theme; those icon factories
-- are consulted first when searching for an icon. If the theme doesn't set a
-- particular icon, Gtk+ looks for the icon in a list of default icon
-- factories, maintained by 'iconFactoryAddDefault' and
-- 'iconFactoryRemoveDefault'. Applications with icons should add a default
-- icon factory with their icons, which will allow themes to override the icons
-- for the application.
--
-- To display an icon, always use
-- 'Graphics.UI.Gtk.General.Style.styleLookupIconSet' on the widget that
-- will display the icon, or the convenience function
-- 'Graphics.UI.Gtk.Abstract.Widget.widgetRenderIcon'. These
-- functions take the theme into account when looking up the icon to use for a
-- given stock ID.
-- * Class Hierarchy
-- |
-- @
-- | 'GObject'
-- | +----IconFactory
-- @
-- * Types
IconFactory,
IconFactoryClass,
castToIconFactory, gTypeIconFactory,
toIconFactory,
-- * Constructors
iconFactoryNew,
-- * Methods
iconFactoryAdd,
iconFactoryAddDefault,
iconFactoryLookup,
iconFactoryLookupDefault,
iconFactoryRemoveDefault,
IconSet,
iconSetNew,
iconSetNewFromPixbuf,
iconSetAddSource,
iconSetRenderIcon,
iconSetGetSizes,
IconSource,
iconSourceNew,
TextDirection(..),
iconSourceGetDirection,
iconSourceSetDirection,
iconSourceResetDirection,
iconSourceGetFilename,
iconSourceSetFilename,
iconSourceGetPixbuf,
iconSourceSetPixbuf,
iconSourceGetSize,
iconSourceSetSize,
iconSourceResetSize,
StateType(..),
iconSourceGetState,
iconSourceSetState,
iconSourceResetState,
IconSize(..),
iconSizeCheck,
iconSizeRegister,
iconSizeRegisterAlias,
iconSizeFromName,
iconSizeGetName
) where
import Control.Monad (liftM)
import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.GObject (constructNewGObject, makeNewGObject)
{#import Graphics.UI.Gtk.Types#}
import Graphics.UI.Gtk.General.Enums (TextDirection(..), StateType(..))
import Graphics.UI.Gtk.General.StockItems
import Graphics.UI.Gtk.General.Structs (IconSize(..))
{# context lib="gtk" prefix="gtk" #}
{#pointer *IconSource foreign newtype#}
{#pointer *IconSet foreign newtype#}
-- The Show instance for IconSize is here since we need c2hs.
instance Show IconSize where
show i = unsafePerformIO (lookupSizeString (fromEnum i))
where
lookupSizeString n = do
ptr <- {#call unsafe icon_size_get_name#} (fromIntegral n)
if ptr==nullPtr then return "" else peekUTFString ptr
--------------------
-- Constructors
-- | Create a new IconFactory.
--
-- * An application should create a new 'IconFactory' and add all
-- needed icons.
-- By calling 'iconFactoryAddDefault' these icons become
-- available as stock objects and can easily be displayed by
-- 'Image'. Furthermore, a theme can override the icons defined by
-- the application.
--
iconFactoryNew :: IO IconFactory
iconFactoryNew =
constructNewGObject mkIconFactory {#call unsafe icon_factory_new#}
--------------------
-- Methods
-- | Add an IconSet to an IconFactory.
--
-- In order to use the new stock object, the factory as to be added to the
-- default factories by 'iconFactoryAddDefault'.
--
iconFactoryAdd :: IconFactory -> StockId -> IconSet -> IO ()
iconFactoryAdd i stockId iconSet = withUTFString stockId $ \strPtr ->
{#call unsafe icon_factory_add#} i strPtr iconSet
-- | Add all entries of the IconFactory to the
-- applications stock object database.
--
iconFactoryAddDefault :: IconFactory -> IO ()
iconFactoryAddDefault = {#call unsafe icon_factory_add_default#}
-- | Looks up the stock id in the icon factory, returning an icon set if found,
-- otherwise Nothing.
--
-- For display to the user, you should use
-- 'Graphics.UI.Gtk.General.Style.styleLookupIconSet' on the
-- 'Graphics.UI.Gtk.General.Style.Style'
-- for the widget that will display the icon, instead of using this function
-- directly, so that themes are taken into account.
--
iconFactoryLookup :: IconFactory -> StockId -> IO (Maybe IconSet)
iconFactoryLookup i stockId =
withUTFString stockId $ \strPtr -> do
iconSetPtr <- {#call unsafe icon_factory_lookup#} i strPtr
if iconSetPtr == nullPtr then return Nothing else liftM (Just . IconSet) $
newForeignPtr iconSetPtr icon_set_unref
-- | Looks for an icon in the list of default icon factories.
--
-- For display to the user, you should use
-- 'Graphics.UI.Gtk.General.Style.styleLookupIconSet' on the
-- 'Graphics.UI.Gtk.General.Style.Style'
-- for the widget that will display the icon, instead of using this function
-- directly, so that themes are taken into account.
--
iconFactoryLookupDefault :: StockId -> IO (Maybe IconSet)
iconFactoryLookupDefault stockId =
withUTFString stockId $ \strPtr -> do
iconSetPtr <- {#call unsafe icon_factory_lookup_default#} strPtr
if iconSetPtr == nullPtr then return Nothing else liftM (Just . IconSet) $
newForeignPtr iconSetPtr icon_set_unref
-- | Remove an IconFactory from the
-- application's stock database.
--
iconFactoryRemoveDefault :: IconFactory -> IO ()
iconFactoryRemoveDefault = {#call unsafe icon_factory_remove_default#}
-- | Add an 'IconSource' (an Icon with
-- attributes) to an 'IconSet'.
--
-- * If an icon is looked up in the IconSet @set@ the best matching
-- IconSource will be taken. It is therefore advisable to add a default
-- (wildcarded) icon, than can be used if no exact match is found.
--
iconSetAddSource :: IconSet -> IconSource -> IO ()
iconSetAddSource set source = {#call unsafe icon_set_add_source#} set source
iconSetRenderIcon :: WidgetClass widget => IconSet
-> TextDirection
-> StateType
-> IconSize
-> widget
-> IO Pixbuf
iconSetRenderIcon set dir state size widget = makeNewGObject mkPixbuf $
{#call icon_set_render_icon#} set (Style nullForeignPtr)
((fromIntegral.fromEnum) dir) ((fromIntegral.fromEnum) state)
((fromIntegral.fromEnum) size) (toWidget widget) nullPtr
-- | Create a new IconSet.
--
-- * Each icon in an application is contained in an 'IconSet'. The
-- 'IconSet' contains several variants ('IconSource's) to
-- accomodate for different sizes and states.
--
iconSetNew :: IO IconSet
iconSetNew = do
isPtr <- {#call unsafe icon_set_new#}
liftM IconSet $ newForeignPtr isPtr icon_set_unref
-- | Creates a new 'IconSet' with the given pixbuf as the default\/fallback
-- source image. If you don't add any additional "IconSource" to the icon set,
-- all variants of the icon will be created from the pixbuf, using scaling,
-- pixelation, etc. as required to adjust the icon size or make the icon look
-- insensitive\/prelighted.
--
iconSetNewFromPixbuf :: Pixbuf -> IO IconSet
iconSetNewFromPixbuf pixbuf = do
isPtr <- {#call unsafe icon_set_new_from_pixbuf#} pixbuf
liftM IconSet $ newForeignPtr isPtr icon_set_unref
-- | Obtains a list of icon sizes this icon set can render.
--
iconSetGetSizes :: IconSet -> IO [IconSize]
iconSetGetSizes set =
alloca $ \sizesArrPtr -> alloca $ \lenPtr -> do
{#call unsafe icon_set_get_sizes#} set sizesArrPtr lenPtr
len <- peek lenPtr
sizesArr <- peek sizesArrPtr
list <- peekArray (fromIntegral len) sizesArr
{#call unsafe g_free#} (castPtr sizesArr)
return $ map (toEnum.fromIntegral) list
foreign import ccall unsafe ">k_icon_set_unref"
icon_set_unref :: FinalizerPtr IconSet
-- | Check if a given IconSize is registered.
--
-- * Useful if your application expects a theme to install a set with a
-- specific size. You can test if this actually happend and use another size
-- if not.
--
iconSizeCheck :: IconSize -> IO Bool
iconSizeCheck size = liftM toBool $
{#call icon_size_lookup#} ((fromIntegral . fromEnum) size) nullPtr nullPtr
-- | Register a new IconSize.
--
iconSizeRegister ::
String -- ^ the new name of the size
-> Int -- ^ the width of the icon
-> Int -- ^ the height of the icon
-> IO IconSize -- ^ the new icon size
iconSizeRegister name width height = liftM (toEnum . fromIntegral) $
withUTFString name $ \strPtr -> {#call unsafe icon_size_register#}
strPtr (fromIntegral width) (fromIntegral height)
-- | Register an additional alias for a name.
--
iconSizeRegisterAlias :: IconSize -> String -> IO ()
iconSizeRegisterAlias target alias = withUTFString alias $ \strPtr ->
{#call unsafe icon_size_register_alias#} strPtr ((fromIntegral . fromEnum) target)
-- | Lookup an IconSize by name.
--
-- * This fixed value 'iconSizeInvalid' is returned if the name was
-- not found.
--
iconSizeFromName :: String -> IO IconSize
iconSizeFromName name = liftM (toEnum . fromIntegral) $
withUTFString name {#call unsafe icon_size_from_name#}
-- | Lookup the name of an IconSize.
--
-- * Returns @Nothing@ if the name was not found.
--
iconSizeGetName :: IconSize -> IO (Maybe String)
iconSizeGetName size = do
strPtr <- {#call unsafe icon_size_get_name#} ((fromIntegral . fromEnum) size)
if strPtr==nullPtr then return Nothing else liftM Just $ peekUTFString strPtr
-- | Retrieve the 'TextDirection' of
-- this IconSource.
--
-- * @Nothing@ is returned if no explicit direction was set.
--
iconSourceGetDirection :: IconSource -> IO (Maybe TextDirection)
iconSourceGetDirection is = do
res <- {#call icon_source_get_direction_wildcarded#} is
if (toBool res) then return Nothing else liftM (Just .toEnum.fromIntegral) $
{#call unsafe icon_source_get_direction#} is
-- | Retrieve the filename this IconSource was
-- based on.
--
-- * Returns @Nothing@ if the IconSource was generated by a Pixbuf.
--
iconSourceGetFilename :: IconSource -> IO (Maybe String)
iconSourceGetFilename is = do
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
strPtr <- {#call unsafe icon_source_get_filename_utf8#} is
#else
strPtr <- {#call unsafe icon_source_get_filename#} is
#endif
if strPtr==nullPtr then return Nothing else liftM Just $ peekUTFString strPtr
-- | Retrieve the 'IconSize' of this
-- IconSource.
--
-- * @Nothing@ is returned if no explicit size was set (i.e. this
-- 'IconSource' matches all sizes).
--
iconSourceGetSize :: IconSource -> IO (Maybe IconSize)
iconSourceGetSize is = do
res <- {#call unsafe icon_source_get_size_wildcarded#} is
if (toBool res) then return Nothing else liftM (Just . toEnum . fromIntegral) $
{#call unsafe icon_source_get_size#} is
-- | Retrieve the 'StateType' of this
-- 'IconSource'.
--
-- * @Nothing@ is returned if the 'IconSource' matches all
-- states.
--
iconSourceGetState :: IconSource -> IO (Maybe StateType)
iconSourceGetState is = do
res <- {#call unsafe icon_source_get_state_wildcarded#} is
if (toBool res) then return Nothing else liftM (Just .toEnum.fromIntegral) $
{#call unsafe icon_source_get_state#} is
-- | Create a new IconSource.
--
-- * An IconSource is a single image that is usually added to an IconSet. Next
-- to the image it contains information about which state, text direction
-- and size it should apply.
--
iconSourceNew :: IO IconSource
iconSourceNew = do
isPtr <- {#call unsafe icon_source_new#}
liftM IconSource $ newForeignPtr isPtr icon_source_free
foreign import ccall unsafe ">k_icon_source_free"
icon_source_free :: FinalizerPtr IconSource
-- | Mark this 'IconSource' that it
-- should only apply to the specified 'TextDirection'.
--
iconSourceSetDirection :: IconSource -> TextDirection -> IO ()
iconSourceSetDirection is td = do
{#call unsafe icon_source_set_direction_wildcarded#} is (fromBool False)
{#call unsafe icon_source_set_direction#} is ((fromIntegral.fromEnum) td)
-- | Reset the specific
-- 'TextDirection' set with 'iconSourceSetDirection'.
--
iconSourceResetDirection :: IconSource -> IO ()
iconSourceResetDirection is =
{#call unsafe icon_source_set_direction_wildcarded#} is (fromBool True)
-- | Load an icon picture from this filename.
--
iconSourceSetFilename :: IconSource -> FilePath -> IO ()
iconSourceSetFilename is name =
#if defined (WIN32) && GTK_CHECK_VERSION(2,6,0)
withUTFString name $ {# call unsafe icon_source_set_filename_utf8 #} is
#else
withUTFString name $ {# call unsafe icon_source_set_filename #} is
#endif
-- | Retrieves the source pixbuf, or Nothing if none is set.
--
iconSourceGetPixbuf :: IconSource -> IO (Maybe Pixbuf)
iconSourceGetPixbuf is = maybeNull (makeNewGObject mkPixbuf) $
{#call unsafe icon_source_get_pixbuf#} is
-- | Sets a pixbuf to use as a base image when creating icon variants for
-- 'IconSet'.
--
iconSourceSetPixbuf :: IconSource -> Pixbuf -> IO ()
iconSourceSetPixbuf is pb = do
{#call icon_source_set_pixbuf#} is pb
-- | Set this 'IconSource' to a specific
-- size.
--
iconSourceSetSize :: IconSource -> IconSize -> IO ()
iconSourceSetSize is size = do
{#call unsafe icon_source_set_size_wildcarded#} is (fromBool False)
{#call unsafe icon_source_set_size#} is ((fromIntegral . fromEnum) size)
-- | Reset the 'IconSize' of this
-- 'IconSource' so that is matches anything.
--
iconSourceResetSize :: IconSource -> IO ()
iconSourceResetSize is =
{#call unsafe icon_source_set_size_wildcarded#} is (fromBool True)
-- | Mark this icon to be used only with this
-- specific state.
--
iconSourceSetState :: IconSource -> StateType -> IO ()
iconSourceSetState is state = do
{#call unsafe icon_source_set_state_wildcarded#} is (fromBool False)
{#call unsafe icon_source_set_state#} is ((fromIntegral.fromEnum) state)
-- | Reset the 'StateType' of this
-- 'IconSource' so that is matches anything.
--
iconSourceResetState :: IconSource -> IO ()
iconSourceResetState is =
{#call unsafe icon_source_set_state_wildcarded#} is (fromBool True)
|