Copyright | (c) Daan Leijen 2003 2004 |
---|---|
License | wxWindows |
Maintainer | wxhaskell-devel@lists.sourceforge.net |
Stability | provisional |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Basic types and marshalling code for the wxWidgets C library.
- data Object a
- objectNull :: Object a
- objectIsNull :: Object a -> Bool
- objectCast :: Object a -> Object b
- objectIsManaged :: Object a -> Bool
- objectDelete :: WxObject a -> IO ()
- objectFromPtr :: Ptr a -> Object a
- managedObjectFromPtr :: Ptr (TWxObject a) -> IO (WxObject a)
- withObjectPtr :: Object a -> (Ptr a -> IO b) -> IO b
- withObjectRef :: String -> Object a -> (Ptr a -> IO b) -> IO b
- withObjectResult :: IO (Ptr a) -> IO (Object a)
- withManagedObjectResult :: IO (Ptr (TWxObject a)) -> IO (WxObject a)
- objectFinalize :: Object a -> IO ()
- objectNoFinalize :: Object a -> IO ()
- type Id = Int
- type Style = Int
- type EventId = Int
- fromBool :: Num a => Bool -> a
- toBool :: (Eq a, Num a) => a -> Bool
- data Num a => Point2 a = Point {}
- point :: Num a => a -> a -> Point2 a
- pt :: Num a => a -> a -> Point2 a
- pointZero :: Num a => Point2 a
- pointNull :: Num a => Point2 a
- data Num a => Size2D a = Size {}
- sz :: Num a => a -> a -> Size2D a
- sizeNull :: Num a => Size2D a
- data Num a => Vector2 a = Vector {}
- vector :: Num a => a -> a -> Vector2 a
- vec :: Num a => a -> a -> Vector2 a
- vecZero :: Num a => Vector2 a
- vecNull :: Num a => Vector2 a
- data Num a => Rect2D a = Rect {
- rectLeft :: !a
- rectTop :: !a
- rectWidth :: !a
- rectHeight :: !a
- rect :: Num a => Point2 a -> Size2D a -> Rect2D a
- rectBetween :: (Num a, Ord a) => Point2 a -> Point2 a -> Rect2D a
- rectFromSize :: Num a => Size2D a -> Rect2D a
- rectZero :: Num a => Rect2D a
- rectNull :: Num a => Rect2D a
- rectSize :: Num a => Rect2D a -> Size2D a
- newtype Color = Color Word
- rgb :: Integral a => a -> a -> a -> Color
- colorRGB :: Integral a => a -> a -> a -> Color
- rgba :: Integral a => a -> a -> a -> a -> Color
- colorRGBA :: Integral a => a -> a -> a -> a -> Color
- colorRed :: Num a => Color -> a
- colorGreen :: Num a => Color -> a
- colorBlue :: Num a => Color -> a
- colorAlpha :: Num a => Color -> a
- intFromColor :: Color -> Int
- colorFromInt :: Int -> Color
- fromColor :: Num a => Color -> a
- toColor :: Integral a => a -> Color
- colorOk :: Color -> Bool
- colorIsOk :: Color -> Bool
- withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
- data TreeItem
- treeItemInvalid :: TreeItem
- treeItemIsOk :: TreeItem -> Bool
- type CString = Ptr CChar
- withCString :: String -> (CString -> IO a) -> IO a
- type CWString = Ptr CWchar
- withCWString :: String -> (CWString -> IO a) -> IO a
- newtype CInt :: * = CInt Int32
- data IntPtr :: *
- data Word :: *
- data Word8 :: *
- data Int64 :: *
- newtype CDouble :: * = CDouble Double
- data CChar :: *
- newtype CWchar :: * = CWchar Int32
- data Ptr a :: * -> *
- ptrNull :: Ptr a
- ptrIsNull :: Ptr a -> Bool
- ptrCast :: Ptr a -> Ptr b
- data ForeignPtr a :: * -> *
- data FunPtr a :: * -> *
Object types
An Object a
is a pointer to an object of type a
. The a
parameter is used
to encode the inheritance relation. When the type parameter is unit ()
, it denotes
an object of exactly that class, when the parameter is a type variable a
, it
specifies an object that is at least an instance of that class. For example in
wxWidgets, we have the following class hierarchy:
EvtHandler |- Window |- Frame |- Control |- Button |- Radiobox
In wxHaskell, all the creation functions will return objects of exactly that
class and use the ()
type:
frameCreate :: Window a -> ... -> IO (Frame ()) buttonCreate :: Window a -> ... -> IO (Button ()) ...
In contrast, all the this (or self) pointers of methods can take objects of any instance of that class and have a type variable, for example:
windowSetClientSize :: Window a -> Size -> IO () controlSetLabel :: Control a -> String -> IO () buttonSetDefault :: Button a -> IO ()
This means that we can use windowSetClientSize
on any window, including
buttons and frames, but we can only use controlSetLabel
on controls, not
including frames.
In wxHaskell, this works since a Frame ()
is actually a type synonym for
Window (CFrame ())
(where CFrame
is an abstract data type). We can thus
pass a value of type Frame ()
to anything that expects some Window a
.
For a button this works too, as it is a synonym for Control (CButton ())
which is in turn a synonym for Window (CControl (CButton ()))
. Note that
we can't pass a frame to something that expects a value of type Control a
.
Of course, a Window a
is actually a type synonym for EvtHandler (CWindow a)
.
If you study the documentation in Graphics.UI.WX.Classes closely, you
can discover where this chain ends :-).
Objects are not automatically deleted. Normally you can use a delete function
like windowDelete
to delete an object. However, almost all objects in the
wxWidgets library are automatically deleted by the library. The only objects
that should be used with care are resources as bitmaps, fonts and brushes.
objectNull :: Object a Source #
A null object. Use with care.
objectIsNull :: Object a -> Bool Source #
Test for null object.
objectCast :: Object a -> Object b Source #
Cast an object to another type. Use with care.
objectIsManaged :: Object a -> Bool Source #
Is this a managed object?
objectDelete :: WxObject a -> IO () Source #
Delete a wxObject, works for managed and unmanaged objects.
objectFromPtr :: Ptr a -> Object a Source #
Create an unmanaged object.
managedObjectFromPtr :: Ptr (TWxObject a) -> IO (WxObject a) Source #
Create a managed object that will be deleted using |wxObject_SafeDelete|.
withObjectRef :: String -> Object a -> (Ptr a -> IO b) -> IO b Source #
Extract the object pointer and raise an exception if NULL
.
Otherwise continue with the valid pointer.
withManagedObjectResult :: IO (Ptr (TWxObject a)) -> IO (WxObject a) Source #
Create a managed object that will be deleted using |wxObject_SafeDelete|.
objectFinalize :: Object a -> IO () Source #
Finalize a managed object manually. (No effect on unmanaged objects.)
objectNoFinalize :: Object a -> IO () Source #
Remove the finalizer on a managed object. (No effect on unmanaged objects.)
Type synonyms
Basic types
toBool :: (Eq a, Num a) => a -> Bool #
Convert a Boolean in numeric representation to a Haskell value
Point
data Num a => Point2 a Source #
A point has an x and y coordinate. Coordinates are normally relative to the upper-left corner of their view frame, where a positive x goes to the right and a positive y to the bottom of the view.
pointNull :: Num a => Point2 a Source #
A null
point is not a legal point (x and y are -1) and can be used for some
wxWidgets functions to select a default point.
Size
sizeNull :: Num a => Size2D a Source #
A null
size is not a legal size (width and height are -1) and can be used for some
wxWidgets functions to select a default size.
Vector
vecNull :: Num a => Vector2 a Source #
A null
vector has a delta x and y of -1 and can be used for some
wxWidgets functions to select a default vector.
Rectangle
data Num a => Rect2D a Source #
A rectangle is defined by the left x coordinate, the top y coordinate, the width and the height.
Rect | |
|
rect :: Num a => Point2 a -> Size2D a -> Rect2D a Source #
Create a rectangle at a certain (upper-left) point with a certain size.
rectBetween :: (Num a, Ord a) => Point2 a -> Point2 a -> Rect2D a Source #
Construct a (positive) rectangle between two (arbitrary) points.
rectFromSize :: Num a => Size2D a -> Rect2D a Source #
Create a rectangle of a certain size with the upper-left corner at (pt
0 0).
rectNull :: Num a => Rect2D a Source #
An null
rectangle is not a valid rectangle (Rect -1 -1 -1 -1
) but can
used for some wxWidgets functions to select a default rectangle. (i.e. frameCreate
).
Color
An abstract data type to define colors.
rgba :: Integral a => a -> a -> a -> a -> Color Source #
Create a color from a red/green/blue/alpha quadruple.
colorRGBA :: Integral a => a -> a -> a -> a -> Color Source #
Create a color from a red/green/blue/alpha quadruple.
colorGreen :: Num a => Color -> a Source #
Returns a green color component
colorAlpha :: Num a => Color -> a Source #
Returns a alpha channel component
intFromColor :: Color -> Int Source #
Return an Int
where the three least significant bytes contain
the red, green, and blue component of a color.
colorFromInt :: Int -> Color Source #
Set the color according to an rgb integer. (see rgbIntFromColor
).
fromColor :: Num a => Color -> a Source #
Return an Num
class's numeric representation where the three
least significant the red, green, and blue component of a color.
toColor :: Integral a => a -> Color Source #
Set the color according to Integral
class's numeric representation.
(see rgbaIntFromColor
).
colorOk :: Color -> Bool Source #
Deprecated: Use colorIsOk instead
deprecated: use colorIsOk
instead.
Marshalling
Basic types
withArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b #
Temporarily store a list of storable values in memory
(like with
, but for multiple elements).
Managed object types
Identifies tree items. Note: Replaces the TreeItemId
object and takes automatically
care of allocation issues.
treeItemInvalid :: TreeItem Source #
Invalid tree item.
treeItemIsOk :: TreeItem -> Bool Source #
Is a tree item ok? (i.e. not invalid).
Primitive types
CString
withCString :: String -> (CString -> IO a) -> IO a #
Marshal a Haskell string into a NUL terminated C string using temporary storage.
- the Haskell string may not contain any NUL characters
- the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.
A C wide string is a reference to an array of C wide characters terminated by NUL.
withCWString :: String -> (CWString -> IO a) -> IO a #
Marshal a Haskell string into a NUL terminated C wide string using temporary storage.
- the Haskell string may not contain any NUL characters
- the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.
ByteString
CInt
Haskell type representing the C int
type.
IntPtr
A signed integral type that can be losslessly converted to and from
Ptr
. This type is also compatible with the C99 type intptr_t
, and
can be marshalled to and from that type safely.
CIntPtr
Word
Bounded Word | |
Enum Word | |
Eq Word | |
Integral Word | |
Num Word | |
Ord Word | |
Read Word | |
Real Word | |
Show Word | |
Ix Word | |
Storable Word | |
Bits Word | |
FiniteBits Word | |
IArray UArray Word | |
Functor (URec Word) | |
Foldable (URec Word) | |
Traversable (URec Word) | |
Generic1 (URec Word) | |
MArray (STUArray s) Word (ST s) | |
Eq (URec Word p) | |
Ord (URec Word p) | |
Show (URec Word p) | |
Generic (URec Word p) | |
data URec Word | Used for marking occurrences of |
type Rep1 (URec Word) | |
type Rep (URec Word p) | |
8 bit Word
8-bit unsigned integer type
64 bit Integer
64-bit signed integer type
CDouble
Haskell type representing the C double
type.
CChar
Haskell type representing the C char
type.
Haskell type representing the C wchar_t
type.
CBool
Pointers
A value of type
represents a pointer to an object, or an
array of objects, which may be marshalled to or from Haskell values
of type Ptr
aa
.
The type a
will often be an instance of class
Storable
which provides the marshalling operations.
However this is not essential, and you can provide your own operations
to access the pointer. For example you might write small foreign
functions to get or set the fields of a C struct
.
IArray UArray (Ptr a) | |
Eq (Ptr a) | |
Functor (URec (Ptr ())) | |
Ord (Ptr a) | |
Show (Ptr a) | |
Foldable (URec (Ptr ())) | |
Traversable (URec (Ptr ())) | |
Generic1 (URec (Ptr ())) | |
Storable (Ptr a) | |
MArray (STUArray s) (Ptr a) (ST s) | |
Eq (URec (Ptr ()) p) | |
Ord (URec (Ptr ()) p) | |
Generic (URec (Ptr ()) p) | |
type Rep1 (URec (Ptr ())) | |
data URec (Ptr ()) | Used for marking occurrences of |
type Rep (URec (Ptr ()) p) | |
data ForeignPtr a :: * -> * #
The type ForeignPtr
represents references to objects that are
maintained in a foreign language, i.e., that are not part of the
data structures usually managed by the Haskell storage manager.
The essential difference between ForeignPtr
s and vanilla memory
references of type Ptr a
is that the former may be associated
with finalizers. A finalizer is a routine that is invoked when
the Haskell storage manager detects that - within the Haskell heap
and stack - there are no more references left that are pointing to
the ForeignPtr
. Typically, the finalizer will, then, invoke
routines in the foreign language that free the resources bound by
the foreign object.
The ForeignPtr
is parameterised in the same way as Ptr
. The
type argument of ForeignPtr
should normally be an instance of
class Storable
.
Eq (ForeignPtr a) | |
Ord (ForeignPtr a) | |
Show (ForeignPtr a) | |
A value of type
is a pointer to a function callable
from foreign code. The type FunPtr
aa
will normally be a foreign type,
a function type with zero or more arguments where
- the argument types are marshallable foreign types,
i.e.
Char
,Int
,Double
,Float
,Bool
,Int8
,Int16
,Int32
,Int64
,Word8
,Word16
,Word32
,Word64
,
,Ptr
a
,FunPtr
a
or a renaming of any of these usingStablePtr
anewtype
. - the return type is either a marshallable foreign type or has the form
whereIO
tt
is a marshallable foreign type or()
.
A value of type
may be a pointer to a foreign function,
either returned by another foreign function or imported with a
a static address import likeFunPtr
a
foreign import ccall "stdlib.h &free" p_free :: FunPtr (Ptr a -> IO ())
or a pointer to a Haskell function created using a wrapper stub
declared to produce a FunPtr
of the correct type. For example:
type Compare = Int -> Int -> Bool foreign import ccall "wrapper" mkCompare :: Compare -> IO (FunPtr Compare)
Calls to wrapper stubs like mkCompare
allocate storage, which
should be released with freeHaskellFunPtr
when no
longer required.
To convert FunPtr
values to corresponding Haskell functions, one
can define a dynamic stub for the specific foreign type, e.g.
type IntFunction = CInt -> IO () foreign import ccall "dynamic" mkFun :: FunPtr IntFunction -> IntFunction