Debian Codegraph @

contrib/win32/win32.lisp

raw
1 ;;; Copyright (c) 2005, Michael Goffioul (michael dot goffioul at swing dot be)
2 ;;;
3 ;;; This program is free software; you can redistribute it and/or
4 ;;; modify it under the terms of the GNU Library General Public
5 ;;; License as published by the Free Software Foundation; either
6 ;;; version 2 of the License, or (at your option) any later version.
7 ;;;
8 ;;; See file '../../Copyright' for full details.
9 ;;;
10 ;;; FOREIGN FUNCTION INTERFACE TO MICROSOFT WINDOWS API
11 ;;;
12
13 (defpackage "WIN32"
14 (:use "COMMON-LISP" "FFI")
15 (:export))
16
17 (in-package "WIN32")
18
19 (clines
20 "#include <windows.h>"
21 "#include <commctrl.h>"
22 )
23
24 ;; Windows types
25
26 (def-foreign-type HANDLE :pointer-void)
27 (def-foreign-type LPCSTR :cstring)
28 (def-foreign-type WNDPROC :pointer-void)
29 (def-foreign-type DWORD :unsigned-int)
30 (def-foreign-type WORD :unsigned-short)
31
32 ;; Windows constants
33
34 (defmacro define-win-constant (name value &optional (c-type :int))
35 `(defconstant ,name ,value))
36
37 (define-win-constant *TRUE* 1)
38 (define-win-constant *FALSE* 0)
39
40 (define-win-constant *WM_CLOSE* #x0010)
41 (define-win-constant *WM_COMMAND* #x0111)
42 (define-win-constant *WM_CONTEXTMENU* #x007b)
43 (define-win-constant *WM_COPY* #x0301)
44 (define-win-constant *WM_CREATE* #x0001)
45 (define-win-constant *WM_CUT* #x0300)
46 (define-win-constant *WM_DESTROY* #x0002)
47 (define-win-constant *WM_GETFONT* #x0031)
48 (define-win-constant *WM_GETMINMAXINFO* #x0024)
49 (define-win-constant *WM_INITMENU* #x0116)
50 (define-win-constant *WM_INITMENUPOPUP* #x0117)
51 (define-win-constant *WM_NCPAINT* #x0085)
52 (define-win-constant *WM_NOTIFY* #x004e)
53 (define-win-constant *WM_PAINT* #x000f)
54 (define-win-constant *WM_PASTE* #x0302)
55 (define-win-constant *WM_QUIT* #x0012)
56 (define-win-constant *WM_SETFOCUS* #x0007)
57 (define-win-constant *WM_SETFONT* #x0030)
58 (define-win-constant *WM_SIZE* #x0005)
59 (define-win-constant *WM_UNDO* #x0304)
60 (define-win-constant *WM_USER* #x0400)
61
62 (define-win-constant *WS_BORDER* #x00800000)
63 (define-win-constant *WS_CHILD* #x40000000)
64 (define-win-constant *WS_CLIPCHILDREN* #x02000000)
65 (define-win-constant *WS_CLIPSIBLINGS* #x04000000)
66 (define-win-constant *WS_DLGFRAME* #x00400000)
67 (define-win-constant *WS_DISABLED* #x08000000)
68 (define-win-constant *WS_HSCROLL* #x00100000)
69 (define-win-constant *WS_OVERLAPPEDWINDOW* #x00CF0000)
70 (define-win-constant *WS_VISIBLE* #x10000000)
71 (define-win-constant *WS_VSCROLL* #x00200000)
72
73 (define-win-constant *WS_EX_CLIENTEDGE* #x00000200)
74
75 (define-win-constant *RICHEDIT_CLASS* "RichEdit20A")
76 (define-win-constant *WC_LISTVIEW* "SysListView32")
77 (define-win-constant *WC_TABCONTROL* "SysTabControl32")
78
79 (define-win-constant *HWND_BOTTOM* (make-pointer 1 'HANDLE))
80 (define-win-constant *HWND_NOTOPMOST* (make-pointer -2 'HANDLE))
81 (define-win-constant *HWND_TOP* (make-pointer 0 'HANDLE))
82 (define-win-constant *HWND_TOPMOST* (make-pointer -1 'HANDLE))
83
84 (define-win-constant *SWP_DRAWFRAME* #x0020)
85 (define-win-constant *SWP_HIDEWINDOW* #x0080)
86 (define-win-constant *SWP_NOMOVE* #x0002)
87 (define-win-constant *SWP_NOOWNERZORDER* #x0200)
88 (define-win-constant *SWP_NOREDRAW* #x0008)
89 (define-win-constant *SWP_NOREPOSITION* #x0200)
90 (define-win-constant *SWP_NOSIZE* #x0001)
91 (define-win-constant *SWP_NOZORDER* #x0004)
92 (define-win-constant *SWP_SHOWWINDOW* #x0040)
93
94 (define-win-constant *BS_DEFPUSHBUTTON* #x00000000)
95 (define-win-constant *BS_PUSHBUTTON* #x00000001)
96
97 (define-win-constant *BN_CLICKED* 0)
98
99 (define-win-constant *ES_AUTOHSCROLL* #x0080)
100 (define-win-constant *ES_AUTOVSCROLL* #x0040)
101 (define-win-constant *ES_LEFT* #x0000)
102 (define-win-constant *ES_MULTILINE* #x0004)
103
104 (define-win-constant *EM_CANUNDO* #x00c6)
105 (define-win-constant *EM_SETEVENTMASK* (+ *WM_USER* 69))
106 (define-win-constant *EM_SETSEL* #x00b1)
107 (define-win-constant *EM_UNDO* #x00c7)
108 (define-win-constant *EN_CHANGE* #x0300)
109 (define-win-constant *ENM_CHANGE* #x00000001)
110
111 (define-win-constant *TCIF_IMAGE* #x0002)
112 (define-win-constant *TCIF_PARAM* #x0008)
113 (define-win-constant *TCIF_RTLREADING* #x0004)
114 (define-win-constant *TCIF_STATE* #x0010)
115 (define-win-constant *TCIF_TEXT* #x0001)
116
117 (define-win-constant *TCHT_NOWHERE* #x0001)
118 (define-win-constant *TCHT_ONITEM* #x0006)
119 (define-win-constant *TCHT_ONITEMICON* #x0002)
120 (define-win-constant *TCHT_ONITEMLABEL* #x0004)
121
122 (define-win-constant *TCM_FIRST* #x1300)
123 (define-win-constant *TCN_FIRST* #xfffffdda)
124 (define-win-constant *TCM_ADJUSTRECT* (+ *TCM_FIRST* 40))
125 (define-win-constant *TCM_DELETEITEM* (+ *TCM_FIRST* 8))
126 (define-win-constant *TCM_GETCURSEL* (+ *TCM_FIRST* 11))
127 (define-win-constant *TCM_HITTEST* (+ *TCM_FIRST* 13))
128 (define-win-constant *TCM_INSERTITEM* (+ *TCM_FIRST* 7))
129 (define-win-constant *TCM_SETCURSEL* (+ *TCM_FIRST* 12))
130 (define-win-constant *TCM_SETITEM* (+ *TCM_FIRST* 6))
131 (define-win-constant *TCN_SELCHANGE* (- *TCN_FIRST* 1))
132
133 (define-win-constant *NM_FIRST* #x100000000)
134 (define-win-constant *NM_CLICK* (- *NM_FIRST* 1))
135 (define-win-constant *NM_RCLICK* (- *NM_FIRST* 5))
136
137 (define-win-constant *SW_HIDE* 0)
138 (define-win-constant *SW_SHOW* 5)
139 (define-win-constant *SW_SHOWNORMAL* 1)
140
141 (define-win-constant *RDW_ERASE* #x0004)
142 (define-win-constant *RDW_FRAME* #x0400)
143 (define-win-constant *RDW_INTERNALPAINT* #x0002)
144 (define-win-constant *RDW_INVALIDATE* #x0001)
145 (define-win-constant *RDW_NOERASE* #x0020)
146 (define-win-constant *RDW_NOFRAME* #x0800)
147 (define-win-constant *RDW_NOINTERNALPAINT* #x0010)
148 (define-win-constant *RDW_VALIDATE* #x0008)
149 (define-win-constant *RDW_ERASENOW* #x0200)
150 (define-win-constant *RDW_UPDATENOW* #x0100)
151 (define-win-constant *RDW_ALLCHILDREN* #x0080)
152 (define-win-constant *RDW_NOCHILDREN* #x0040)
153
154 (define-win-constant *CW_USEDEFAULT* (- #x80000000))
155
156 (define-win-constant *IDC_ARROW* 32512)
157 (define-win-constant *IDI_APPLICATION* 32512)
158
159 (define-win-constant *COLOR_BACKGROUND* 1)
160 (define-win-constant *DEFAULT_GUI_FONT* 17)
161 (define-win-constant *OEM_FIXED_FONT* 10)
162 (define-win-constant *SYSTEM_FONT* 13)
163 (define-win-constant *SYSTEM_FIXED_FONT* 16)
164
165 (define-win-constant *MB_HELP* #x00004000)
166 (define-win-constant *MB_OK* #x00000000)
167 (define-win-constant *MB_OKCANCEL* #x00000001)
168 (define-win-constant *MB_YESNO* #x00000004)
169 (define-win-constant *MB_YESNOCANCEL* #x00000003)
170 (define-win-constant *MB_ICONEXCLAMATION* #x00000030)
171 (define-win-constant *MB_ICONWARNING* #x00000020)
172 (define-win-constant *MB_ICONERROR* #x00000010)
173 (define-win-constant *MB_ICONINFORMATION* #x00000040)
174 (define-win-constant *MB_ICONQUESTION* #x00000020)
175
176 (define-win-constant *IDCANCEL* 2)
177 (define-win-constant *IDNO* 7)
178 (define-win-constant *IDOK* 1)
179 (define-win-constant *IDYES* 6)
180
181 (define-win-constant *MF_BYCOMMAND* #x00000000)
182 (define-win-constant *MF_BYPOSITION* #x00000400)
183 (define-win-constant *MF_CHECKED* #x00000008)
184 (define-win-constant *MF_DISABLED* #x00000002)
185 (define-win-constant *MF_ENABLED* #x00000000)
186 (define-win-constant *MF_GRAYED* #x00000001)
187 (define-win-constant *MF_MENUBREAK* #x00000040)
188 (define-win-constant *MF_POPUP* #x00000010)
189 (define-win-constant *MF_SEPARATOR* #x00000800)
190 (define-win-constant *MF_STRING* #x00000000)
191 (define-win-constant *MF_UNCHECKED* #x00000000)
192
193 (define-win-constant *TPM_CENTERALIGN* #x0004)
194 (define-win-constant *TPM_LEFTALIGN* #x0000)
195 (define-win-constant *TPM_RIGHTALIGN* #x0008)
196 (define-win-constant *TPM_BOTTOMALIGN* #x0020)
197 (define-win-constant *TPM_TOPALIGN* #x0000)
198 (define-win-constant *TPM_VCENTERALIGN* #x0010)
199 (define-win-constant *TPM_NONOTIFY* #x0080)
200 (define-win-constant *TPM_RETURNCMD* #x0100)
201 (define-win-constant *TPM_LEFTBUTTON* #x0000)
202 (define-win-constant *TPM_RIGHTBUTTON* #x0002)
203
204 (define-win-constant *OFN_FILEMUSTEXIST* #x00001000)
205 (define-win-constant *OFN_OVERWRITEPROMPT* #x00000002)
206 (define-win-constant *OFN_PATHMUSTEXIST* #x00000800)
207 (define-win-constant *OFN_READONLY* #x00000001)
208
209 (define-win-constant *FVIRTKEY* *TRUE*)
210 (define-win-constant *FNOINVERT* #x02)
211 (define-win-constant *FSHIFT* #x04)
212 (define-win-constant *FCONTROL* #x08)
213 (define-win-constant *FALT* #x10)
214
215 (define-win-constant *VK_F1* #x70)
216 (define-win-constant *VK_LEFT* #x25)
217 (define-win-constant *VK_RIGHT* #x27)
218
219 (define-win-constant *GWL_EXSTYLE* -20)
220 (define-win-constant *GWL_HINSTANCE* -6)
221 (define-win-constant *GWL_HWNDPARENT* -8)
222 (define-win-constant *GWL_ID* -12)
223 (define-win-constant *GWL_STYLE* -16)
224 (define-win-constant *GWL_WNDPROC* -4)
225
226 (define-win-constant *FINDMSGSTRING* "commdlg_FindReplace")
227 (define-win-constant *HELPMSGSTRING* "commdlg_help")
228
229 (define-win-constant *FR_DIALOGTERM* #x00000040)
230 (define-win-constant *FR_DOWN* #x00000001)
231 (define-win-constant *FR_FINDNEXT* #x00000008)
232 (define-win-constant *FR_HIDEUPDOWN* #x00004000)
233 (define-win-constant *FR_HIDEMATCHCASE* #x00008000)
234 (define-win-constant *FR_HIDEWHOLEWORD* #x00010000)
235 (define-win-constant *FR_MATCHCASE* #x00000004)
236 (define-win-constant *FR_NOMATCHCASE* #x00000800)
237 (define-win-constant *FR_NOUPDOWN* #x00000400)
238 (define-win-constant *FR_NOWHOLEWORD* #x00001000)
239 (define-win-constant *FR_REPLACE* #x00000010)
240 (define-win-constant *FR_REPLACEALL* #x00000020)
241 (define-win-constant *FR_SHOWHELP* #x00000080)
242 (define-win-constant *FR_WHOLEWORD* #x00000002)
243
244 (defconstant *NULL* (make-null-pointer :void))
245
246 ;; Windows structures
247
248 (def-struct WNDCLASS
249 (style :unsigned-int)
250 (lpfnWndProc WNDPROC)
251 (cbClsExtra :int)
252 (cbWndExtra :int)
253 (hInstance HANDLE)
254 (hIcon HANDLE)
255 (hCursor HANDLE)
256 (hbrBackground HANDLE)
257 (lpszMenuName :cstring)
258 (lpszClassName :cstring))
259 (defun make-wndclass (name &key (style 0) (lpfnWndProc nil) (cbClsExtra 0) (cbWndExtra 0) (hInstance *NULL*)
260 (hIcon (default-icon)) (hCursor (default-cursor)) (hbrBackground (default-background))
261 (lpszMenuName ""))
262 (with-foreign-object (cls 'WNDCLASS)
263 (setf (get-slot-value cls 'WNDCLASS 'style) style
264 (get-slot-value cls 'WNDCLASS 'lpfnWndProc) (callback 'wndproc-proxy)
265 (get-slot-value cls 'WNDCLASS 'cbClsExtra) cbClsExtra
266 (get-slot-value cls 'WNDCLASS 'cbWndExtra) cbWndExtra
267 (get-slot-value cls 'WNDCLASS 'hInstance) hInstance
268 (get-slot-value cls 'WNDCLASS 'hIcon) hIcon
269 (get-slot-value cls 'WNDCLASS 'hCursor) hCursor
270 (get-slot-value cls 'WNDCLASS 'hbrBackground) hbrBackground
271 (get-slot-value cls 'WNDCLASS 'lpszMenuName) lpszMenuName
272 (get-slot-value cls 'WNDCLASS 'lpszClassName) (string name))
273 (register-wndproc (string name) lpfnWndProc)
274 (registerclass cls)))
275 (def-struct POINT
276 (x :int)
277 (y :int))
278 (def-struct MSG
279 (hwnd HANDLE)
280 (message :unsigned-int)
281 (wParam :unsigned-int)
282 (lParam :int)
283 (time :unsigned-int)
284 (pt POINT))
285 (def-struct CREATESTRUCT
286 (lpCreateParams :pointer-void)
287 (hInstance HANDLE)
288 (hMenu HANDLE)
289 (hwndParent HANDLE)
290 (cx :int)
291 (cy :int)
292 (x :int)
293 (y :int)
294 (style :long)
295 (lpszName :cstring)
296 (lpszClass :cstring)
297 (dwExStyle :unsigned-int))
298 (def-struct MINMAXINFO
299 (ptReserved POINT)
300 (ptMaxSize POINT)
301 (ptMaxPosition POINT)
302 (ptMinTrackSize POINT)
303 (ptMaxTrackSize POINT))
304 (def-struct TEXTMETRIC (tmHeight :long) (tmAscent :long) (tmDescent :long) (tmInternalLeading :long) (tmExternalLeading :long)
305 (tmAveCharWidth :long) (tmMaxCharWidth :long) (tmWeight :long) (tmOverhang :long) (tmDigitizedAspectX :long)
306 (tmDigitizedAspectY :long) (tmFirstChar :char) (tmLastChar :char) (tmDefaultChar :char) (tmBreakChar :char)
307 (tmItalic :byte) (tmUnderlined :byte) (tmStruckOut :byte) (tmPitchAndFamily :byte) (tmCharSet :byte))
308 (def-struct SIZE (cx :long) (cy :long))
309 (def-struct RECT (left :long) (top :long) (right :long) (bottom :long))
310 (def-struct TITLEBARINFO (cbSize :unsigned-int) (rcTitlebar RECT) (rgstate (:array :unsigned-int 6)))
311 (def-struct OPENFILENAME (lStructSize :unsigned-int) (hwndOwner HANDLE) (hInstance HANDLE) (lpstrFilter LPCSTR) (lpstrCustomFilter LPCSTR)
312 (nMaxFilter :unsigned-int) (nFilterIndex :unsigned-int) (lpstrFile LPCSTR) (nMaxFile :unsigned-int) (lpstrFileTitle LPCSTR)
313 (nMaxFileTitle :unsigned-int) (lpstrInitialDir LPCSTR) (lpstrTitle LPCSTR) (Flags :unsigned-int) (nFileOffset :unsigned-short)
314 (nFileExtension :unsigned-short) (lpstrDefExt LPCSTR) (lCustData :int) (lpfnHook HANDLE) (lpTemplateName LPCSTR)
315 #|(pvReserved :pointer-void) (dwReserved :unsigned-int) (FlagsEx :unsigned-int)|#)
316 (def-struct ACCEL (fVirt :byte) (key :unsigned-short) (cmd :unsigned-short))
317 (def-struct TCITEM (mask :unsigned-int) (dwState :unsigned-int) (dwStateMask :unsigned-int)
318 (pszText :cstring) (cchTextMax :int) (iImage :int) (lParam :long))
319 (def-struct NMHDR (hwndFrom HANDLE) (idFrom :unsigned-int) (code :unsigned-int))
320 (def-struct TCHITTESTINFO (pt POINT) (flag :unsigned-int))
321 (def-struct TPMPARAMS (cbSize :unsigned-int) (rcExclude RECT))
322 (def-struct FINDREPLACE (lStructSize :unsigned-int) (hwndOwner HANDLE) (hInstance HANDLE) (Flags DWORD)
323 (lpstrFindWhat LPCSTR) (lpstrReplaceWith LPCSTR) (wFindWhatLen WORD) (wReplaceWithLen WORD)
324 (lpCustData :int) (lpfnHook HANDLE) (lpTemplateName LPCSTR))
325
326 ;; Windows functions
327
328 (defvar *wndproc-db* nil)
329 (defun register-wndproc (class-or-obj wndproc)
330 (let ((entry (assoc class-or-obj *wndproc-db* :test #'equal)))
331 (if entry
332 (rplacd entry wndproc)
333 (push (cons class-or-obj wndproc) *wndproc-db*)))
334 (unless (stringp class-or-obj)
335 (let ((old-proc (make-pointer (getwindowlong class-or-obj *GWL_WNDPROC*) 'HANDLE)))
336 (setwindowlong class-or-obj *GWL_WNDPROC* (make-lparam (callback 'wndproc-proxy)))
337 old-proc)))
338 (defun get-wndproc (obj)
339 (let ((entry (or (assoc obj *wndproc-db* :test #'equal)
340 (assoc (getclassname obj) *wndproc-db* :test #'equal))))
341 (and entry
342 (cdr entry))))
343 (defcallback (wndproc-proxy :stdcall) :int ((hnd :pointer-void) (umsg :unsigned-int) (wparam :unsigned-int) (lparam :int))
344 (let* ((wndproc (get-wndproc hnd)))
345 (unless wndproc
346 (error "Cannot find a registered Windows prodecure for object ~S" hnd))
347 (funcall wndproc hnd umsg wparam lparam)))
348
349 (defun make-ID (id) (make-pointer id :pointer-void))
350 (setf (symbol-function 'make-handle) #'make-ID)
351 (defun make-wparam (hnd) (pointer-address hnd))
352 (defun make-lparam (hnd) (pointer-address hnd))
353 (defmacro with-cast-int-pointer ((var type &optional ptr) &body body)
354 (unless ptr (setq ptr var))
355 `(let ((,var (make-pointer ,ptr ',type))) ,@body))
356
357 (defmacro def-win32-function (name args &key (returning :void) module)
358 `(def-function ,name ,args :returning ,returning :module ,module :call :stdcall))
359 (eval-when (:compile-toplevel)
360 (define-compiler-macro def-win32-function (name args &key (returning :void) module)
361 `(def-function ,name ,args :returning ,returning)))
362 (load-foreign-library "kernel32")
363 (load-foreign-library "comdlg32")
364 (load-foreign-library "gdi32")
365 (load-foreign-library "comctl32")
366
367 (def-win32-function ("RtlZeroMemory" zeromemory) ((Destination :pointer-void) (Length :unsigned-int)) :returning :void :module "kernel32")
368 (def-win32-function ("LoadLibraryA" loadlibrary) ((lpLibFileName LPCSTR)) :returning HANDLE :module "kernel32")
369 (def-win32-function ("FreeLibrary" freelibrary) ((hLibModule HANDLE)) :returning :int :module "kernel32")
370 (def-win32-function ("GetModuleHandleA" getmodulehandle) ((lpModuleName LPCSTR)) :returning HANDLE :module "kernel32")
371 (def-win32-function ("GetStockObject" getstockobject) ((fnObject :int)) :returning HANDLE :module "gdi32")
372 (def-win32-function ("GetTextMetricsA" gettextmetrics) ((hdc HANDLE) (lptm (* TEXTMETRIC))) :returning :int :module "gdi32")
373 (def-win32-function ("GetDC" getdc) ((hWnd HANDLE)) :returning HANDLE :module "user32")
374 (def-win32-function ("ReleaseDC" releasedc) ((hWnd HANDLE) (hdc HANDLE)) :returning :int :module "user32")
375 (def-win32-function ("SelectObject" selectobject) ((hdc HANDLE) (hgdiobj HANDLE)) :returning HANDLE :module "gdi32")
376 (def-win32-function ("GetTextExtentPoint32A" gettextextentpoint32) ((hdc HANDLE) (lpString :cstring) (cbString :int) (lpSize (* SIZE))) :returning :int :module "gdi32")
377 (def-win32-function ("LoadCursorA" loadcursor-string) ((hnd HANDLE) (lpCursorName LPCSTR)) :returning HANDLE :module "user32")
378 (def-win32-function ("LoadCursorA" loadcursor-int) ((hnd HANDLE) (lpCursorName :unsigned-int)) :returning HANDLE :module "user32")
379 (defun loadcursor (hnd cur-name)
380 (etypecase cur-name
381 (fixnum (loadcursor-int hnd cur-name))
382 (string (loadcursor-string hnd cur-name))))
383 (defun default-cursor () (loadcursor *NULL* *IDC_ARROW*))
384 (def-win32-function ("LoadIconA" loadicon-int) ((hnd HANDLE) (lpIconName :unsigned-int)) :returning HANDLE :module "user32")
385 (def-win32-function ("LoadIconA" loadicon-string) ((hnd HANDLE) (lpIconName LPCSTR)) :returning HANDLE :module "user32")
386 (defun loadicon (hnd cur-name)
387 (etypecase cur-name
388 (fixnum (loadicon-int hnd cur-name))
389 (string (loadicon-string hnd cur-name))))
390 (defun default-icon () (loadicon *NULL* *IDI_APPLICATION*))
391 (defun default-background () (getstockobject *COLOR_BACKGROUND*))
392 (def-win32-function ("GetLastError" getlasterror) () :returning :unsigned-int :module "kernel32")
393 (def-win32-function ("GetClassNameA" getclassname-i) ((hnd HANDLE) (lpClassName (* :char)) (maxCount :int)) :returning :int :module "user32")
394 (defun getclassname (hnd &aux (max-length 64))
395 (with-foreign-object (s `(:array :char ,max-length))
396 (let ((n (getclassname-i hnd s max-length)))
397 (when (= n 0)
398 (error "Unable to get class name for ~A" hnd))
399 (convert-from-foreign-string s :length n))))
400 (def-win32-function ("RegisterClassA" registerclass) ((lpWndClass (* WNDCLASS))) :returning :int :module "user32")
401 (def-win32-function ("UnregisterClassA" unregisterclass) ((lpClassName :cstring) (hInstance HANDLE)) :returning :int :module "user32")
402 (def-win32-function ("GetWindowLongA" getwindowlong) ((hWnd HANDLE) (nIndex :int)) :returning :long :module "user32")
403 (def-win32-function ("SetWindowLongA" setwindowlong) ((hWnd HANDLE) (nIndex :int) (dwNewLong :long)) :returning :long :module "user32")
404 (def-win32-function ("CreateWindowExA" createwindowex) ((dwExStyle :unsigned-int) (lpClassName :cstring) (lpWindowName :cstring) (dwStyle :unsigned-int)
405 (x :int) (y :int) (nWidth :int) (nHeight :int) (hWndParent HANDLE) (hMenu HANDLE) (hInstance HANDLE)
406 (lpParam :pointer-void))
407 :returning HANDLE :module "user32")
408 (defun createwindow (&rest args)
409 (apply #'createwindowex 0 args))
410 (def-win32-function ("DestroyWindow" destroywindow) ((hWnd HANDLE)) :returning :int :module "user32")
411 (def-win32-function ("ShowWindow" showwindow) ((hWnd HANDLE) (nCmdShow :int)) :returning :int :module "user32")
412 (def-win32-function ("UpdateWindow" updatewindow) ((hWnd HANDLE)) :returning :void :module "user32")
413 (def-win32-function ("RedrawWindow" redrawwindow) ((hWnd HANDLE) (lprcUpdate (* RECT)) (hrgnUpdate HANDLE) (flags :unsigned-int)) :returning :int :module "user32")
414 (def-win32-function ("MoveWindow" movewindow) ((hWnd HANDLE) (x :int) (y :int) (nWidth :int) (nHeight :int) (bRepaint :int)) :returning :int :module "user32")
415 (def-win32-function ("SetWindowPos" setwindowpos) ((hWnd HANDLE) (hWndInsertAfter HANDLE) (x :int)
416 (y :int) (cx :int) (cy :int) (uFlags :unsigned-int)) :returning :int :module "user32")
417 (def-win32-function ("BringWindowToTop" bringwindowtotop) ((hWnd HANDLE)) :returning :int :module "user32")
418 (def-win32-function ("GetWindowTextA" getwindowtext-i) ((hWnd HANDLE) (lpString LPCSTR) (nMaxCount :int)) :returning :int :module "user32")
419 (defun getwindowtext (hnd)
420 (let ((len (1+ (getwindowtextlength hnd))))
421 (with-cstring (s (make-string len))
422 (getwindowtext-i hnd s len)
423 (subseq s 0 (1- len)))))
424 (def-win32-function ("GetWindowTextLengthA" getwindowtextlength) ((hWnd HANDLE)) :returning :int :module "user32")
425 (def-win32-function ("SetWindowTextA" setwindowtext) ((hWnd HANDLE) (lpString LPCSTR)) :returning :int :module "user32")
426 (def-win32-function ("GetParent" getparent) ((hWnd HANDLE)) :returning HANDLE :module "user32")
427 (def-win32-function ("GetClientRect" getclientrect) ((hWnd HANDLE) (lpRect (* RECT))) :returning :int :module "user32")
428 (def-win32-function ("GetWindowRect" getwindowrect) ((hWnd HANDLE) (lpRect (* RECT))) :returning :int :module "user32")
429 (def-win32-function ("InvalidateRect" invalidaterect) ((hWnd HANDLE) (lpRect (* RECT)) (bErase :int)) :returning :int :module "user32")
430 (def-win32-function ("SetRect" setrect) ((lpRect (* RECT)) (xLeft :int) (yTop :int) (xRight :int) (yBottom :int)) :returning :int :module "user32")
431 ;(def-win32-function ("GetTitleBarInfo" gettitlebarinfo) ((hWnd HANDLE) (pti (* TITLEBARINFO))) :returning :int)
432 (def-win32-function ("SetFocus" setfocus) ((hWnd HANDLE)) :returning HANDLE :module "user32")
433 (def-win32-function ("PostQuitMessage" postquitmessage) ((nExitCode :int)) :returning :void :module "user32")
434 (def-win32-function ("SendMessageA" sendmessage) ((hWnd HANDLE) (uMsg :unsigned-int) (wParam :unsigned-int) (lParam :int)) :returning :int :module "user32")
435 (def-win32-function ("PostMessageA" postmessage) ((hWnd HANDLE) (uMsg :unsigned-int) (wParam :unsigned-int) (lParam :int)) :returning :int :module "user32")
436 (def-win32-function ("RegisterWindowMessageA" registerwindowmessage) ((lpString LPCSTR)) :returning :unsigned-int :module "user32")
437 (def-win32-function ("IsDialogMessageA" isdialogmessage) ((hDlg HANDLE) (lpMsg (* MSG))) :returning :int :module "user32")
438 (def-win32-function ("DefWindowProcA" defwindowproc) ((hWnd HANDLE) (uMsg :unsigned-int) (wParam :unsigned-int) (lParam :int)) :returning :int :module "user32")
439 (def-win32-function ("CallWindowProcA" callwindowproc) ((wndProc HANDLE) (hWnd HANDLE) (uMsg :unsigned-int) (wParam :unsigned-int) (lParam :int)) :returning :int :module "user32")
440 (defun loword (x) (logand x #xffff))
441 (defun hiword (x) (logand (floor x 65536) #xffff))
442 (defun get-x-lparam (x) (loword x))
443 (defun get-y-lparam (x) (hiword x))
444 (def-win32-function ("ScreenToClient" screentoclient) ((hWnd HANDLE) (pt (* POINT))) :returning :int :module "user32")
445 (def-win32-function ("MessageBoxA" messagebox) ((hWnd HANDLE) (lpText LPCSTR) (lpCaption LPCSTR) (uType :unsigned-int)) :returning :int :module "user32")
446 (def-win32-function ("GetOpenFileNameA" getopenfilename) ((lpofn (* OPENFILENAME))) :returning :int :module "comdlg32")
447 (def-win32-function ("GetSaveFileNameA" getsavefilename) ((lpofn (* OPENFILENAME))) :returning :int :module "comdlg32")
448 (def-win32-function ("FindTextA" findtext) ((lpfr (* FINDREPLACE))) :returning HANDLE :module "comdlg32")
449 (def-win32-function ("ReplaceTextA" replacetext) ((lpfr (* FINDREPLACE))) :returning HANDLE :module "comdlg32")
450 (def-win32-function ("GetMessageA" getmessage) ((lpMsg (* MSG)) (hWnd HANDLE) (wMsgFitlerMin :unsigned-int) (wMsgFilterMax :unsigned-int)) :returning :int :module "user32")
451 (def-win32-function ("TranslateMessage" translatemessage) ((lpMsg (* MSG))) :returning :int :module "user32")
452 (def-win32-function ("DispatchMessageA" dispatchmessage) ((lpMsg (* MSG))) :returning :int :module "user32")
453 (def-win32-function ("CreateMenu" createmenu) nil :returning HANDLE :module "user32")
454 (def-win32-function ("CreatePopupMenu" createpopupmenu) nil :returning HANDLE :module "user32")
455 (def-win32-function ("DestroyMenu" destroymenu) ((hMenu HANDLE)) :returning :int :module "user32")
456 (def-win32-function ("AppendMenuA" appendmenu) ((hMenu HANDLE) (uFlags :unsigned-int) (uIDNewItem :unsigned-int) (lpNewItem LPCSTR)) :returning :int :module "user32")
457 (def-win32-function ("GetSubMenu" getsubmenu) ((hMenu HANDLE) (nPos :int)) :returning HANDLE :module "user32")
458 (def-win32-function ("DeleteMenu" deletemenu) ((hMenu HANDLE) (uPosition :unsigned-int) (uFlags :unsigned-int)) :returning :int :module "user32")
459 (def-win32-function ("RemoveMenu" removemenu) ((hMenu HANDLE) (uPosition :unsigned-int) (uFlags :unsigned-int)) :returning :int :module "user32")
460 (def-win32-function ("GetMenuItemCount" getmenuitemcount) ((hMenu HANDLE)) :returning :int :module "user32")
461 (def-win32-function ("CheckMenuItem" checkmenuitem) ((hMenu HANDLE) (uIDCheckItem :unsigned-int) (uCheck :unsigned-int)) :returning :int :module "user32")
462 (def-win32-function ("EnableMenuItem" enablemenuitem) ((hMenu HANDLE) (uIDCheckItem :unsigned-int) (uCheck :unsigned-int)) :returning :int :module "user32")
463 (def-win32-function ("TrackPopupMenu" trackpopupmenu) ((hMenu HANDLE) (uFlags :unsigned-int) (x :int) (y :int)
464 (nReserved :int) (hWnd HANDLE) (prcRect HANDLE)) :returning :int :module "user32")
465 (def-win32-function ("TrackPopupMenuEx" trackpopupmenuex) ((hMenu HANDLE) (fuFlags :unsigned-int) (x :int) (y :int)
466 (hWnd HANDLE) (lptpl (* TPMPARAMS))) :returning :int :module "user32")
467 (def-win32-function ("CreateAcceleratorTableA" createacceleratortable) ((lpaccl (* ACCEL)) (cEntries :int)) :returning HANDLE :module "user32")
468 (def-win32-function ("TranslateAcceleratorA" translateaccelerator) ((hWnd HANDLE) (hAccTable HANDLE) (lpMsg (* MSG))) :returning :int :module "user32")
469 (def-win32-function ("DestroyAcceleratorTable" destroyacceleratortable) ((hAccTable HANDLE)) :returning :int :module "user32")
470 (def-win32-function ("InitCommonControls" initcommoncontrols) () :returning :void :module "comctl32")
471
472 (defun event-loop (&key (accelTable *NULL*) (accelMain *NULL*) (dlgSym nil))
473 (with-foreign-object (msg 'MSG)
474 (loop for bRet = (getmessage msg *NULL* 0 0)
475 when (= bRet 0) return bRet
476 if (= bRet -1)
477 do (error "GetMessage failed!!!")
478 else
479 do (or (and (not (null-pointer-p accelTable))
480 (not (null-pointer-p accelMain))
481 (/= (translateaccelerator accelMain accelTable msg) 0))
482 (and dlgSym
483 (not (null-pointer-p (symbol-value dlgSym)))
484 (/= (isdialogmessage (symbol-value dlgSym) msg) 0))
485 (progn
486 (translatemessage msg)
487 (dispatchmessage msg))))))
488
489 (defun y-or-no-p (&optional control &rest args)
490 (let ((s (coerce (apply #'format nil control args) 'simple-string)))
491 (= (messagebox *NULL* s "ECL Dialog" (logior *MB_YESNO* *MB_ICONQUESTION*))
492 *IDYES*)))
493
494 (defun get-open-filename (&key (owner *NULL*) initial-dir filter (dlgfn #'getopenfilename)
495 (flags 0) &aux (max-fn-size 1024))
496 (flet ((null-concat (x &optional y &aux (xx (if y x (car x))) (yy (if y y (cdr x))))
497 (concatenate 'string xx (string #\Null) yy)))
498 (when filter
499 (setq filter (format nil "~A~C~C" (reduce #'null-concat (mapcar #'null-concat filter)) #\Null #\Null)))
500 (with-foreign-object (ofn 'OPENFILENAME)
501 (with-cstrings ((fn (make-string max-fn-size :initial-element #\Null))
502 (filter filter))
503 (zeromemory ofn (size-of-foreign-type 'OPENFILENAME))
504 (setf (get-slot-value ofn 'OPENFILENAME 'lStructSize) (size-of-foreign-type 'OPENFILENAME))
505 (setf (get-slot-value ofn 'OPENFILENAME 'hwndOwner) owner)
506 (setf (get-slot-value ofn 'OPENFILENAME 'lpstrFile) fn)
507 (setf (get-slot-value ofn 'OPENFILENAME 'nMaxFile) max-fn-size)
508 (setf (get-slot-value ofn 'OPENFILENAME 'Flags) flags)
509 (when filter
510 (setf (get-slot-value ofn 'OPENFILENAME 'lpstrFilter) filter))
511 (unless (= (funcall dlgfn ofn) 0)
512 (pathname (string-trim (string #\Null) fn)))))))
513
514 (defun find-text (&key (owner *NULL*) &aux (max-txt-size 1024))
515 (with-foreign-object (fr 'FINDREPLACE)
516 (with-cstring (txt (make-string max-txt-size :initial-element #\Null))
517 (zeromemory fr (size-of-foreign-type 'FINDREPLACE))
518 (setf (get-slot-value fr 'FINDREPLACE 'lStructSize) (size-of-foreign-type 'FINDREPLACE))
519 (setf (get-slot-value fr 'FINDREPLACE 'hwndOwner) owner)
520 (setf (get-slot-value fr 'FINDREPLACE 'wFindWhatLen) max-txt-size)
521 ;(setf (get-slot-value fr 'FINDREPLACE 'Flags) 1)
522 (let ((result (findtext fr)))
523 (print result)
524 txt))))
525
526 #|
527 (defun set-wndproc (obj fun)
528 (let ((cb (si:make-dynamic-callback fun (read-from-string (format nil "~A-WNDPROC" (gensym))) :int '(:pointer-void :unsigned-int :unsigned-int :int)))
529 (old-wndproc (make-pointer (getwindowlong obj *GWL_WNDPROC*) 'HANDLE)))
530 (setwindowlong obj *GWL_WNDPROC* (make-lparam cb))
531 old-wndproc))
532 |#
533
534 (provide "WIN32")
535
536 ;;; Test code
537
538 (defconstant *HELLO_ID* 100)
539 (defconstant *OK_ID* 101)
540
541 (defparameter hBtn nil)
542 (defparameter hOk nil)
543
544 (defun button-min-size (hnd)
545 (let ((fnt (make-pointer (sendmessage hnd *WM_GETFONT* 0 0) :pointer-void))
546 (hdc (getdc hnd))
547 (txt (getwindowtext hnd)))
548 (unless (null-pointer-p fnt)
549 (selectobject hdc fnt))
550 (with-foreign-objects ((sz 'SIZE)
551 (tm 'TEXTMETRIC))
552 (gettextextentpoint32 hdc txt (length txt) sz)
553 (gettextmetrics hdc tm)
554 (releasedc hnd hdc)
555 (list (+ (get-slot-value sz 'SIZE 'cx) 20)
556 (+ (get-slot-value tm 'TEXTMETRIC 'tmHeight) 10)))))
557
558 (defun get-titlebar-rect (hnd)
559 (with-foreign-object (ti 'TITLEBARINFO)
560 (setf (get-slot-value ti 'TITLEBARINFO 'cbSize) (size-of-foreign-type 'TITLEBARINFO))
561 (gettitlebarinfo hnd ti)
562 (let ((rc (get-slot-value ti 'TITLEBARINFO 'rcTitlebar)))
563 (list (get-slot-value rc 'RECT 'left)
564 (get-slot-value rc 'RECT 'top)
565 (get-slot-value rc 'RECT 'right)
566 (get-slot-value rc 'RECT 'bottom)))))
567
568 (defun test-wndproc (hwnd umsg wparam lparam)
569 (cond ((= umsg *WM_DESTROY*)
570 (setq hBtn nil hOk nil)
571 (postquitmessage 0)
572 0)
573 ((= umsg *WM_CREATE*)
574 (setq hBtn (createwindowex 0 "BUTTON" "Hello World!" (logior *WS_VISIBLE* *WS_CHILD* *BS_PUSHBUTTON*)
575 0 0 50 20 hwnd (make-ID *HELLO_ID*) *NULL* *NULL*))
576 (setq hOk (createwindowex 0 "BUTTON" "Close" (logior *WS_VISIBLE* *WS_CHILD* *BS_PUSHBUTTON*)
577 0 0 50 20 hwnd (make-ID *OK_ID*) *NULL* *NULL*))
578 (sendmessage hBtn *WM_SETFONT* (make-wparam (getstockobject *DEFAULT_GUI_FONT*)) 0)
579 (sendmessage hOk *WM_SETFONT* (make-wparam (getstockobject *DEFAULT_GUI_FONT*)) 0)
580 0)
581 ((= umsg *WM_SIZE*)
582 (let* ((new-w (loword lparam))
583 (new-h (hiword lparam))
584 (wb (- new-w 20))
585 (hb (/ (- new-h 30) 2)))
586 (movewindow hBtn 10 10 wb hb *TRUE*)
587 (movewindow hOk 10 (+ 20 hb) wb hb *TRUE*))
588 0)
589 ((= umsg *WM_GETMINMAXINFO*)
590 (let* ((btn1-sz (and hBtn (button-min-size hBtn)))
591 (btn2-sz (and hOk (button-min-size hOk)))
592 #|(rc (get-titlebar-rect hWnd))|#
593 (titleH #|(1+ (- (fourth rc) (second rc)))|# 30))
594 (when (and btn1-sz btn2-sz (> titleH 0))
595 (with-foreign-object (minSz 'POINT)
596 (setf (get-slot-value minSz 'POINT 'x) (+ (max (first btn1-sz) (first btn2-sz)) 20))
597 (setf (get-slot-value minSz 'POINT 'y) (+ (second btn1-sz) (second btn2-sz) 30 titleH))
598 (with-cast-int-pointer (lparam MINMAXINFO)
599 (setf (get-slot-value lparam 'MINMAXINFO 'ptMinTrackSize) minSz)))))
600 0)
601 ((= umsg *WM_COMMAND*)
602 (let ((n (hiword wparam))
603 (id (loword wparam)))
604 (cond ((= n *BN_CLICKED*)
605 (cond ((= id *HELLO_ID*)
606 (format t "~&Hellow World!~%")
607 (get-open-filename :owner hwnd))
608 ((= id *OK_ID*)
609 (destroywindow hwnd))))
610 (t
611 (format t "~&Un-handled notification: ~D~%" n))))
612 0)
613 (t
614 (defwindowproc hwnd umsg wparam lparam))))
615
616 (defun do-test ()
617 (make-wndclass "MyClass"
618 :lpfnWndProc #'test-wndproc)
619 (let* ((hwnd (createwindowex
620 0
621 "MyClass"
622 "ECL/Win32 test"
623 *WS_OVERLAPPEDWINDOW*
624 *CW_USEDEFAULT*
625 *CW_USEDEFAULT*
626 130
627 120
628 *NULL*
629 *NULL*
630 *NULL*
631 *NULL*)))
632 (when (si::null-pointer-p hwnd)
633 (error "Unable to create window"))
634 (showwindow hwnd *SW_SHOWNORMAL*)
635 (updatewindow hwnd)
636 (event-loop)
637 (unregisterclass "MyClass" *NULL*)))
638