1
-----------------------------------------------------------------------------
3
-- Module : Graphics.Win32.Misc
4
-- Copyright : (c) Alastair Reid, 1997-2003
5
-- License : BSD-style (see the file libraries/base/LICENSE)
7
-- Maintainer : Esa Ilari Vuokko <ei@vuokko.info>
8
-- Stability : provisional
9
-- Portability : portable
11
-- A collection of FFI declarations for interfacing with Win32.
13
-----------------------------------------------------------------------------
15
module Graphics.Win32.Misc where
17
import Graphics.Win32.GDI.Types
18
import System.Win32.Types
26
----------------------------------------------------------------
28
-- (should probably be distributed between
29
-- Graphics.Win32.{Icon,Cursor,Accelerator,Menu,...})
30
----------------------------------------------------------------
32
type Accelerator = LPCTSTR
33
-- intToAccelerator :: Int -> Accelerator
34
-- intToAccelerator i = makeIntResource (toWord i)
36
-- cursor and icon should not be const pointer; GSL ???
38
-- intToCursor :: Int -> Cursor
39
-- intToCursor i = makeIntResource (toWord i)
42
-- intToIcon :: Int -> Icon
43
-- intToIcon i = makeIntResource (toWord i)
45
loadAccelerators :: Maybe HINSTANCE -> Accelerator -> IO HACCEL
46
loadAccelerators mb_inst accel =
47
failIfNull "LoadAccelerators" $ c_LoadAccelerators (maybePtr mb_inst) accel
48
foreign import stdcall unsafe "windows.h LoadAcceleratorsW"
49
c_LoadAccelerators :: HINSTANCE -> Accelerator -> IO HACCEL
51
loadCursor :: Maybe HINSTANCE -> Cursor -> IO HCURSOR
52
loadCursor mb_inst cursor =
53
failIfNull "LoadCursor" $ c_LoadCursor (maybePtr mb_inst) cursor
54
foreign import stdcall unsafe "windows.h LoadCursorW"
55
c_LoadCursor :: HINSTANCE -> Cursor -> IO HCURSOR
57
loadIcon :: Maybe HINSTANCE -> Icon -> IO HICON
58
loadIcon mb_inst icon =
59
failIfNull "LoadIcon" $ c_LoadIcon (maybePtr mb_inst) icon
60
foreign import stdcall unsafe "windows.h LoadIconW"
61
c_LoadIcon :: HINSTANCE -> Icon -> IO HICON
63
#{enum Cursor, castUINTToPtr
64
, iDC_ARROW = (UINT)IDC_ARROW
65
, iDC_IBEAM = (UINT)IDC_IBEAM
66
, iDC_WAIT = (UINT)IDC_WAIT
67
, iDC_CROSS = (UINT)IDC_CROSS
68
, iDC_UPARROW = (UINT)IDC_UPARROW
69
, iDC_SIZENWSE = (UINT)IDC_SIZENWSE
70
, iDC_SIZENESW = (UINT)IDC_SIZENESW
71
, iDC_SIZEWE = (UINT)IDC_SIZEWE
72
, iDC_SIZENS = (UINT)IDC_SIZENS
75
#{enum Icon, castUINTToPtr
76
, iDI_APPLICATION = (UINT)IDI_APPLICATION
77
, iDI_HAND = (UINT)IDI_HAND
78
, iDI_QUESTION = (UINT)IDI_QUESTION
79
, iDI_EXCLAMATION = (UINT)IDI_EXCLAMATION
80
, iDI_ASTERISK = (UINT)IDI_ASTERISK
83
----------------------------------------------------------------
85
----------------------------------------------------------------
91
, mB_OKCANCEL = MB_OKCANCEL
92
, mB_ABORTRETRYIGNORE = MB_ABORTRETRYIGNORE
93
, mB_YESNOCANCEL = MB_YESNOCANCEL
95
, mB_RETRYCANCEL = MB_RETRYCANCEL
96
, mB_ICONHAND = MB_ICONHAND
97
, mB_ICONQUESTION = MB_ICONQUESTION
98
, mB_ICONEXCLAMATION = MB_ICONEXCLAMATION
99
, mB_ICONASTERISK = MB_ICONASTERISK
100
, mB_ICONINFORMATION = MB_ICONINFORMATION
101
, mB_ICONSTOP = MB_ICONSTOP
102
, mB_DEFBUTTON1 = MB_DEFBUTTON1
103
, mB_DEFBUTTON2 = MB_DEFBUTTON2
104
, mB_DEFBUTTON3 = MB_DEFBUTTON3
105
, mB_APPLMODAL = MB_APPLMODAL
106
, mB_SYSTEMMODAL = MB_SYSTEMMODAL
107
, mB_TASKMODAL = MB_TASKMODAL
108
, mB_SETFOREGROUND = MB_SETFOREGROUND
115
, iDCANCEL = IDCANCEL
116
, iDIGNORE = IDIGNORE
123
-- Note: if the error is ever raised, we're in a very sad way!
125
messageBox :: HWND -> String -> String -> MBStyle -> IO MBStatus
126
messageBox wnd text caption style =
127
withTString text $ \ c_text ->
128
withTString caption $ \ c_caption ->
129
failIfZero "MessageBox" $ c_MessageBox wnd c_text c_caption style
130
foreign import stdcall unsafe "windows.h MessageBoxW"
131
c_MessageBox :: HWND -> LPCTSTR -> LPCTSTR -> MBStyle -> IO MBStatus
133
----------------------------------------------------------------
135
----------------------------------------------------------------
137
type StdHandleId = DWORD
140
, sTD_INPUT_HANDLE = STD_INPUT_HANDLE
141
, sTD_OUTPUT_HANDLE = STD_OUTPUT_HANDLE
142
, sTD_ERROR_HANDLE = STD_ERROR_HANDLE
145
getStdHandle :: StdHandleId -> IO HANDLE
147
failIf (== iNVALID_HANDLE_VALUE) "GetStdHandle" $ c_GetStdHandle hid
148
foreign import stdcall unsafe "windows.h GetStdHandle"
149
c_GetStdHandle :: StdHandleId -> IO HANDLE
151
----------------------------------------------------------------
152
-- Rotatable Ellipse hack
154
-- Win95 (Win32?) doesn't support rotating ellipses - so we
155
-- implement them with polygons.
157
-- We use a fixed number of edges rather than varying the number
158
-- according to the radius of the ellipse.
159
-- If anyone feels like improving the code (to vary the number),
160
-- they should place a fixed upper bound on the number of edges
161
-- since it takes a relatively long time to draw 1000 edges.
162
----------------------------------------------------------------
164
transformedEllipse :: HDC -> POINT -> POINT -> POINT -> IO ()
165
transformedEllipse dc (x0,y0) (x1,y1) (x2,y2) =
166
failIfFalse_ "transformedEllipse" $ c_transformedEllipse dc x0 y0 x1 y1 x2 y2
167
foreign import ccall unsafe "ellipse.h transformedEllipse"
168
c_transformedEllipse :: HDC -> LONG -> LONG -> LONG -> LONG -> LONG -> LONG -> IO Bool
170
{-# CFILES cbits/ellipse.c #-}
172
----------------------------------------------------------------
174
----------------------------------------------------------------
176
getCursorPos :: IO POINT
178
allocaPOINT $ \ p_pt -> do
179
failIfFalse_ "GetCursorPos" $ c_GetCursorPos p_pt
181
foreign import stdcall unsafe "windows.h GetCursorPos"
182
c_GetCursorPos :: Ptr POINT -> IO Bool
184
setCursorPos :: POINT -> IO ()
186
failIfFalse_ "setCursorPos" $ c_SetCursorPos x y
187
foreign import stdcall unsafe "windows.h SetCursorPos"
188
c_SetCursorPos :: LONG -> LONG -> IO Bool
190
clipCursor :: RECT -> IO ()
192
withRECT rect $ \ p_rect ->
193
failIfFalse_ "ClipCursor" $ c_ClipCursor p_rect
194
foreign import stdcall unsafe "windows.h ClipCursor"
195
c_ClipCursor :: Ptr RECT -> IO Bool
197
getClipCursor :: IO RECT
199
allocaRECT $ \ p_rect -> do
200
failIfFalse_ "GetClipCursor" $ c_GetClipCursor p_rect
202
foreign import stdcall unsafe "windows.h GetClipCursor"
203
c_GetClipCursor :: Ptr RECT -> IO Bool
205
----------------------------------------------------------------
207
----------------------------------------------------------------
209
type ExitOption = UINT
212
, eWX_FORCE = EWX_FORCE
213
, eWX_LOGOFF = EWX_LOGOFF
214
, eWX_POWEROFF = EWX_POWEROFF
215
, eWX_REBOOT = EWX_REBOOT
216
, eWX_SHUTDOWN = EWX_SHUTDOWN
219
exitWindowsEx :: ExitOption -> IO ()
221
failIfFalse_ "ExitWindowsEx" $ c_ExitWindowsEx opt 0
222
foreign import stdcall unsafe "windows.h ExitWindowsEx"
223
c_ExitWindowsEx :: ExitOption -> DWORD -> IO Bool
226
exitWindows = exitWindowsEx 0
228
----------------------------------------------------------------
230
----------------------------------------------------------------
233
type MbBeep = Maybe Beep
235
maybeBeep :: Maybe Beep -> Beep
236
maybeBeep = fromMaybe 0xffffffff
240
type MbDuration = Maybe Duration
242
maybeDuration :: Maybe Duration -> Duration
243
maybeDuration = fromMaybe (-1)
245
messageBeep :: Maybe Beep -> IO ()
246
messageBeep mb_beep =
247
c_MessageBeep (maybeBeep mb_beep)
248
foreign import stdcall unsafe "windows.h MessageBeep"
249
c_MessageBeep :: Beep -> IO ()
251
beep :: WORD -> MbDuration -> IO ()
253
failIfFalse_ "Beep" $ c_Beep freq (maybeDuration mb_dur)
254
foreign import stdcall unsafe "windows.h Beep"
255
c_Beep :: WORD -> Duration -> IO Bool
257
----------------------------------------------------------------
259
----------------------------------------------------------------
263
type TIMERPROC = FunPtr (HWND -> UINT -> TimerId -> DWORD -> IO ())
265
-- ToDo: support the other two forms of timer initialisation
267
-- Cause WM_TIMER events to be sent to window callback
269
setWinTimer :: HWND -> TimerId -> UINT -> IO TimerId
270
setWinTimer wnd timer elapse =
271
failIfZero "SetTimer" $ c_SetTimer wnd timer elapse nullFunPtr
272
foreign import stdcall unsafe "windows.h SetTimer"
273
c_SetTimer :: HWND -> TimerId -> UINT -> TIMERPROC -> IO TimerId
275
killTimer :: Maybe HWND -> TimerId -> IO ()
276
killTimer mb_wnd timer =
277
failIfFalse_ "KillTimer" $ c_KillTimer (maybePtr mb_wnd) timer
278
foreign import stdcall unsafe "windows.h KillTimer"
279
c_KillTimer :: HWND -> TimerId -> IO Bool
281
-- For documentation purposes:
282
type MilliSeconds = DWORD
284
foreign import stdcall unsafe "windows.h timeGetTime"
285
timeGetTime :: IO MilliSeconds
287
----------------------------------------------------------------
289
-- %fun ezCreateFont :: Unknown
290
-- %result BITMAP({ getBitmapInfo(x) })
292
----------------------------------------------------------------
294
----------------------------------------------------------------