forked from mhammond/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32gui.i
More file actions
7598 lines (6910 loc) · 269 KB
/
win32gui.i
File metadata and controls
7598 lines (6910 loc) · 269 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* File : win32gui.i */
// @doc
%ifdef WINXPGUI
%module winxpgui
%else
%module win32gui // A module which provides an interface to the native win32
// GUI API.<nl>Note that a module <o winxpgui> also exists,
// which has the same methods as win32gui, but has an XP
// manifest and is setup for side-by-side sharing support for
// certain system DLLs, notably commctl32.
%endif
%{
#define _WIN32_IE 0x0501 // to enable balloon notifications in Shell_NotifyIcon
#define _WIN32_WINNT 0x0501
#ifdef WINXPGUI
// This changes the entire world for XP!
#define ISOLATION_AWARE_ENABLED 1
#endif
%}
%include "typemaps.i"
%include "pywintypes.i"
%{
#undef PyHANDLE
#include "pywinobjects.h"
#include "winuser.h"
#include "commctrl.h"
#include "windowsx.h" // For edit control hacks.
#include "Dbt.h" // device notification
#include "malloc.h"
#ifdef MS_WINCE
#include "winbase.h"
#define IS_INTRESOURCE(res) (((DWORD)(res) & 0xffff0000) == 0)
#endif
#define CHECK_PFN(fname)if (pfn##fname==NULL) return PyErr_Format(PyExc_NotImplementedError,"%s is not available on this platform", #fname);
typedef BOOL (WINAPI *SetLayeredWindowAttributesfunc)(HWND, COLORREF, BYTE,DWORD);
static SetLayeredWindowAttributesfunc pfnSetLayeredWindowAttributes=NULL;
typedef BOOL (WINAPI *GetLayeredWindowAttributesfunc)(HWND, COLORREF *, BYTE *, DWORD *);
static GetLayeredWindowAttributesfunc pfnGetLayeredWindowAttributes=NULL;
typedef BOOL (WINAPI *UpdateLayeredWindowfunc)(HWND,HDC,POINT *,SIZE *,HDC,POINT *,COLORREF,BLENDFUNCTION *,DWORD);
static UpdateLayeredWindowfunc pfnUpdateLayeredWindow=NULL;
typedef BOOL (WINAPI *AngleArcfunc)(HDC, int, int, DWORD, FLOAT, FLOAT);
static AngleArcfunc pfnAngleArc=NULL;
typedef BOOL (WINAPI *PlgBltfunc)(HDC,CONST POINT *,HDC,int,int,int,int,HBITMAP,int,int);
static PlgBltfunc pfnPlgBlt=NULL;
typedef BOOL (WINAPI *GetWorldTransformfunc)(HDC,XFORM *);
static GetWorldTransformfunc pfnGetWorldTransform=NULL;
typedef BOOL (WINAPI *SetWorldTransformfunc)(HDC,XFORM *);
static SetWorldTransformfunc pfnSetWorldTransform=NULL;
typedef BOOL (WINAPI *ModifyWorldTransformfunc)(HDC,XFORM *,DWORD);
static ModifyWorldTransformfunc pfnModifyWorldTransform=NULL;
typedef BOOL (WINAPI *CombineTransformfunc)(LPXFORM,CONST XFORM *,CONST XFORM *);
static CombineTransformfunc pfnCombineTransform=NULL;
typedef BOOL (WINAPI *GradientFillfunc)(HDC,PTRIVERTEX,ULONG,PVOID,ULONG,ULONG);
static GradientFillfunc pfnGradientFill=NULL;
typedef BOOL (WINAPI *TransparentBltfunc)(HDC,int,int,int,int,HDC,int,int,int,int,UINT);
static TransparentBltfunc pfnTransparentBlt=NULL;
typedef BOOL (WINAPI *MaskBltfunc)(HDC,int,int,int,int,HDC,int,int,HBITMAP,int,int,DWORD);
static MaskBltfunc pfnMaskBlt=NULL;
typedef BOOL (WINAPI *AlphaBlendfunc)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
static AlphaBlendfunc pfnAlphaBlend=NULL;
typedef BOOL (WINAPI *AnimateWindowfunc)(HWND,DWORD,DWORD);
static AnimateWindowfunc pfnAnimateWindow=NULL;
typedef BOOL (WINAPI *GetMenuInfofunc)(HMENU, LPCMENUINFO);
static GetMenuInfofunc pfnGetMenuInfo=NULL;
typedef BOOL (WINAPI *SetMenuInfofunc)(HMENU, LPCMENUINFO);
static GetMenuInfofunc pfnSetMenuInfo=NULL;
typedef DWORD (WINAPI *GetLayoutfunc)(HDC);
static GetLayoutfunc pfnGetLayout=NULL;
typedef DWORD (WINAPI *SetLayoutfunc)(HDC, DWORD);
static SetLayoutfunc pfnSetLayout=NULL;
typedef int (WINAPI *DrawTextWfunc)(HDC,LPWSTR,int,LPRECT,UINT);
static DrawTextWfunc pfnDrawTextW = NULL;
static PyObject *g_AtomMap = NULL; // Mapping class atoms to Python WNDPROC
static PyObject *g_HWNDMap = NULL; // Mapping HWND to Python WNDPROC
static PyObject *g_DLGMap = NULL; // Mapping Dialog HWND to Python WNDPROC
static HWND hDialogCurrent = NULL; // see MS TID Q71450 and PumpMessages for this
extern HGLOBAL MakeResourceFromDlgList(PyObject *tmpl);
extern PyObject *MakeDlgListFromResource(HGLOBAL res);
HINSTANCE g_dllhandle;
static PyObject *logger = NULL;
void HandleError(char *prefix)
{
BOOL do_stderr = TRUE;
if (logger) {
PyObject *exc_typ = NULL, *exc_val = NULL, *exc_tb = NULL;
PyErr_Fetch( &exc_typ, &exc_val, &exc_tb);
PyObject *kw = PyDict_New();
PyObject *exc_info = Py_BuildValue("OOO", exc_typ, exc_val, exc_tb);
if (kw)
PyDict_SetItemString(kw, "exc_info", exc_info);
Py_XDECREF(exc_info);
PyObject *args = Py_BuildValue("(s)", prefix);
PyObject *method = PyObject_GetAttrString(logger, "error");
PyObject *result = NULL;
if (method && kw && args)
result = PyObject_Call(method, args, kw);
Py_XDECREF(method);
Py_XDECREF(kw);
Py_XDECREF(args);
if (result) {
do_stderr = FALSE;
Py_DECREF(result);
}
}
if (do_stderr) {
PySys_WriteStderr(prefix);
PySys_WriteStderr("\n");
PyErr_Print();
}
}
// @object PyBLENDFUNCTION|Tuple of four small ints used to fill a BLENDFUNCTION struct
// Each int must fit in a byte (0-255).
// @pyseeapi BLENDFUNCTION
BOOL PyWinObject_AsBLENDFUNCTION(PyObject *obbl, BLENDFUNCTION *pbl)
{
if (!PyTuple_Check(obbl)){
PyErr_SetString(PyExc_TypeError, "BLENDFUNCTION must be a tuple of four small ints (0-255)");
return FALSE;
}
return PyArg_ParseTuple(obbl, "BBBB:BLENDFUNCTION",
&pbl->BlendOp, // @tupleitem 0|int|BlendOp|Only defined value is AC_SRC_OVER (0)
&pbl->BlendFlags, // @tupleitem 1|int|BlendFlags|None currently defined, must be 0
&pbl->SourceConstantAlpha, // @tupleitem 2|int|SourceConstantAlpha|Transparency to be applied to entire source. (255 is opaque)
&pbl->AlphaFormat); // @tupleitem 3|int|AlphaFormat|Only defined flag is AC_SRC_ALPHA, used when src bitmap contains per-pixel alpha
}
// @object PySIZE|Tuple of two ints (cx,cy) representing a SIZE struct
BOOL PyWinObject_AsSIZE(PyObject *obsize, SIZE *psize)
{
if (!PyTuple_Check(obsize)){
PyErr_SetString(PyExc_TypeError, "SIZE must be a tuple of 2 ints (x,y)");
return FALSE;
}
return PyArg_ParseTuple(obsize, "ll;SIZE must be a tuple of 2 ints (x,y)",
&psize->cx, &psize->cy);
}
// @object PyGdiHANDLE|Gdi objects such as brush (HBRUSH), pen (HPEN), font (HFONT), region (HRGN), bitmap (HBITMAP)
// On destruction, the handle is closed using DeleteObject. The object's Close() method also calls DeleteObject.
// The gdi object should be deselected from any DC that it is selected into before it's closed.
// Inherits the methods and properties of <o PyHANDLE>.
class PyGdiHANDLE: public PyHANDLE
{
public:
PyGdiHANDLE(HANDLE hInit) : PyHANDLE(hInit) {}
virtual BOOL Close(void){
BOOL ret=DeleteObject(m_handle);
if (!ret)
PyWin_SetAPIError("DeleteObject");
m_handle = 0;
return ret;
}
virtual const char *GetTypeName(){
return "PyGdiHANDLE";
}
};
PyObject *PyWinObject_FromGdiHANDLE(HGDIOBJ h)
{
PyObject *ret=new PyGdiHANDLE(h);
if (ret==NULL) {
DeleteObject(h);
PyErr_NoMemory();
}
return ret;
}
%}
// SWIG support for GDI handles.
%typemap(python,except) HPEN, HBRUSH, HFONT, HRGN, HBITMAP {
Py_BEGIN_ALLOW_THREADS
$function
Py_END_ALLOW_THREADS
if ($source==NULL){
$cleanup
return PyWin_SetAPIError("$name");
}
}
/* ??? If you don't map these to a known type, swig obstinately ignores the input and output typemaps and tries to treat them as pointers.
However, it doesn't seem to matter what you typedef them to as long as they have in and out typemaps. ??? */
typedef float HPEN, HBRUSH, HFONT, HRGN, HBITMAP;
%typemap(python,out) HPEN, HBRUSH, HFONT, HRGN, HBITMAP{
$target = PyWinObject_FromGdiHANDLE($source);
}
%typemap(python,in) HPEN, HBRUSH, HFONT, HRGN, HBITMAP{
if (!PyWinObject_AsHANDLE($source, (HANDLE *)&$target))
return NULL;
}
%typemap(python,in) HRGN INPUT_NULLOK, HBRUSH INPUT_NULLOK, HBITMAP INPUT_NULLOK{
if (!PyWinObject_AsHANDLE($source, (HANDLE *)&$target))
return NULL;
}
%typedef int int_regiontype;
// Several functions return an int containg a region type (NULLREGION,SIMPLEREGION,COMPLEXREGION) or ERROR on failure
%typemap(python,except) int_regiontype{
Py_BEGIN_ALLOW_THREADS
$function
Py_END_ALLOW_THREADS
if ($source==ERROR){
$cleanup
return PyWin_SetAPIError("$name");
}
}
%{
// @object PyHDEVNOTIFY|A handle returned by RegisterDeviceNotifications which
// automatically calls UnregisterDeviceNotification on destruction.
// Inherits the methods and properties of <o PyHANDLE>.
class PyHDEVNOTIFY: public PyHANDLE
{
public:
PyHDEVNOTIFY(HANDLE hInit) : PyHANDLE(hInit) {}
virtual BOOL Close(void){
BOOL ret=UnregisterDeviceNotification(m_handle);
if (!ret)
PyWin_SetAPIError("UnregisterDeviceNotification");
m_handle = 0;
return ret;
}
virtual const char *GetTypeName(){
return "PyHDEVNOTIFY";
}
};
PyObject *PyWinObject_FromHDEVNOTIFY(HGDIOBJ h)
{
PyObject *ret=new PyHDEVNOTIFY(h);
if (ret==NULL) {
UnregisterDeviceNotification(h);
PyErr_NoMemory();
}
return ret;
}
%}
// TODO: SWIG support for PyHDEVNOTIFY - but SWIG currently doesn't use it.
// Written to the module init function.
%init %{
PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
PyDict_SetItemString(d, "dllhandle", PyWinLong_FromVoidPtr(g_dllhandle));
PyDict_SetItemString(d, "error", PyWinExc_ApiError);
if (PyType_Ready(&PyWNDCLASSType) == -1 ||
PyType_Ready(&PyBITMAPType) == -1 ||
PyType_Ready(&PyLOGFONTType) == -1)
PYWIN_MODULE_INIT_RETURN_ERROR;
// Expose the window procedure and window class dicts to aid debugging
g_AtomMap = PyDict_New();
g_HWNDMap = PyDict_New();
g_DLGMap = PyDict_New();
#ifdef Py_DEBUG
PyDict_SetItemString(d, "g_AtomMap", g_AtomMap);
PyDict_SetItemString(d, "g_HWNDMap", g_HWNDMap);
PyDict_SetItemString(d, "g_DLGMap", g_DLGMap);
#endif
PyDict_SetItemString(d, "UNICODE", Py_True);
// hack borrowed from win32security since version of SWIG we use doesn't do keyword arguments
#ifdef WINXPGUI
for (PyMethodDef *pmd = winxpguiMethods; pmd->ml_name; pmd++)
#else
for (PyMethodDef *pmd = win32guiMethods; pmd->ml_name; pmd++)
#endif
if (strcmp(pmd->ml_name, "SetLayeredWindowAttributes")==0
||strcmp(pmd->ml_name, "GetLayeredWindowAttributes")==0
||strcmp(pmd->ml_name, "UpdateLayeredWindow")==0
||strcmp(pmd->ml_name, "AnimateWindow")==0
||strcmp(pmd->ml_name, "GetOpenFileNameW")==0
||strcmp(pmd->ml_name, "GetSaveFileNameW")==0
||strcmp(pmd->ml_name, "SystemParametersInfo")==0
||strcmp(pmd->ml_name, "DrawTextW")==0
)
pmd->ml_flags = METH_VARARGS | METH_KEYWORDS;
HMODULE hmodule=GetModuleHandle(TEXT("user32.dll"));
if (hmodule==NULL)
hmodule=LoadLibrary(TEXT("user32.dll"));
if (hmodule){
pfnSetLayeredWindowAttributes=(SetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"SetLayeredWindowAttributes");
pfnGetLayeredWindowAttributes=(GetLayeredWindowAttributesfunc)GetProcAddress(hmodule,"GetLayeredWindowAttributes");
pfnUpdateLayeredWindow=(UpdateLayeredWindowfunc)GetProcAddress(hmodule,"UpdateLayeredWindow");
pfnAnimateWindow=(AnimateWindowfunc)GetProcAddress(hmodule,"AnimateWindow");
pfnGetMenuInfo=(GetMenuInfofunc)GetProcAddress(hmodule,"GetMenuInfo");
pfnSetMenuInfo=(SetMenuInfofunc)GetProcAddress(hmodule,"SetMenuInfo");
pfnDrawTextW=(DrawTextWfunc)GetProcAddress(hmodule, "DrawTextW");
}
hmodule=GetModuleHandle(TEXT("gdi32.dll"));
if (hmodule==NULL)
hmodule=LoadLibrary(TEXT("gdi32.dll"));
if (hmodule){
pfnAngleArc=(AngleArcfunc)GetProcAddress(hmodule,"AngleArc");
pfnPlgBlt=(PlgBltfunc)GetProcAddress(hmodule,"PlgBlt");
pfnGetWorldTransform=(GetWorldTransformfunc)GetProcAddress(hmodule,"GetWorldTransform");
pfnSetWorldTransform=(SetWorldTransformfunc)GetProcAddress(hmodule,"SetWorldTransform");
pfnModifyWorldTransform=(ModifyWorldTransformfunc)GetProcAddress(hmodule,"ModifyWorldTransform");
pfnCombineTransform=(CombineTransformfunc)GetProcAddress(hmodule,"CombineTransform");
pfnMaskBlt=(MaskBltfunc)GetProcAddress(hmodule,"MaskBlt");
pfnGetLayout=(GetLayoutfunc)GetProcAddress(hmodule,"GetLayout");
pfnSetLayout=(SetLayoutfunc)GetProcAddress(hmodule,"SetLayout");
}
hmodule=GetModuleHandle(TEXT("msimg32.dll"));
if (hmodule==NULL)
hmodule=LoadLibrary(TEXT("msimg32.dll"));
if (hmodule){
pfnGradientFill=(GradientFillfunc)GetProcAddress(hmodule,"GradientFill");
pfnTransparentBlt=(TransparentBltfunc)GetProcAddress(hmodule,"TransparentBlt");
pfnAlphaBlend=(AlphaBlendfunc)GetProcAddress(hmodule,"AlphaBlend");
}
%}
%{
#ifdef MS_WINCE
typedef HANDLE HINST_ARG;
// WinCE gives a compile error this with dllexport
#define DECLSPEC_DLLMAIN
#else
typedef HINSTANCE HINST_ARG;
#define DECLSPEC_DLLMAIN __declspec(dllexport)
#endif
extern "C" DECLSPEC_DLLMAIN BOOL WINAPI DllMain(HINST_ARG hInstance, DWORD dwReason, LPVOID lpReserved)
{
if ( dwReason == DLL_PROCESS_ATTACH )
g_dllhandle = (HINSTANCE)hInstance;
return TRUE;
}
%}
// Custom 'exception handlers' for simple types that exist only to
// manage the thread-lock.
%typemap(python,except) int {
Py_BEGIN_ALLOW_THREADS
$function
Py_END_ALLOW_THREADS
}
// Handles types with no specific PyHANDLE subclass, returned to Python as plain ints or longs
typedef float HDC, HCURSOR, HINSTANCE, HMENU, HICON, HGDIOBJ, HIMAGELIST, HACCEL;
%typemap(python, in) HDC, HCURSOR, HINSTANCE, HMENU, HICON, HGDIOBJ, HIMAGELIST, HACCEL{
if (!PyWinObject_AsHANDLE($source, (HANDLE *)&$target))
return NULL;
}
%typemap(python, out) HDC, HCURSOR, HINSTANCE, HMENU, HICON, HGDIOBJ, HIMAGELIST, HACCEL{
$target=PyWinLong_FromHANDLE($source);
}
%apply COLORREF {long};
typedef long COLORREF
typedef HANDLE WPARAM;
typedef HANDLE LPARAM;
typedef HANDLE LRESULT;
typedef int UINT;
%typedef void *NULL_ONLY
%typemap(python,in) NULL_ONLY {
if ($source != Py_None) {
PyErr_SetString(PyExc_TypeError, "This param must be None");
return NULL;
}
$target = NULL;
}
%typemap(python,ignore) MSG *OUTPUT(MSG temp)
{
$target = &temp;
memset($target, 0, sizeof(MSG));
}
%typemap(python,argout) MSG *OUTPUT{
PyObject *o = PyWinObject_FromMSG($source);
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,in) MSG *INPUT {
$target = (MSG *)_alloca(sizeof(MSG));
if (!PyWinObject_AsMSG($source, $target))
return NULL;
}
%typemap(python,ignore) RECT *OUTPUT(RECT rect_output)
{
$target = &rect_output;
}
%typemap(python,in) RECT *INPUT(RECT rect_input)
{
if (PyTuple_Check($source)) {
if (PyArg_ParseTuple($source, "llll", &rect_input.left, &rect_input.top, &rect_input.right, &rect_input.bottom) == 0) {
return PyErr_Format(PyExc_TypeError, "%s: This param must be a tuple of four integers", "$name");
}
$target = &rect_input;
} else {
return PyErr_Format(PyExc_TypeError, "%s: This param must be a tuple of four integers", "$name");
}
}
%typemap(python,in) RECT *INPUT_NULLOK(RECT rect_input_nullok)
{
if (PyTuple_Check($source)) {
if (PyArg_ParseTuple($source, "llll", &rect_input_nullok.left, &rect_input_nullok.top, &rect_input_nullok.right, &rect_input_nullok.bottom) == 0) {
return PyErr_Format(PyExc_TypeError, "%s: This param must be a tuple of four integers or None", "$name");
}
$target = &rect_input_nullok;
} else {
if ($source == Py_None) {
$target = NULL;
} else {
PyErr_SetString(PyExc_TypeError, "This param must be a tuple of four integers or None");
return NULL;
}
}
}
%typemap(python,argout) RECT *OUTPUT {
PyObject *o;
o = Py_BuildValue("llll", $source->left, $source->top, $source->right, $source->bottom);
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,in) RECT *BOTH = RECT *INPUT;
%typemap(python,argout) RECT *BOTH = RECT *OUTPUT;
%typemap(python,argout) POINT *OUTPUT {
PyObject *o;
o = Py_BuildValue("ll", $source->x, $source->y);
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,ignore) POINT *OUTPUT(POINT point_output)
{
$target = &point_output;
}
%typemap(python,in) POINT *INPUT(POINT point_input) {
if (!PyWinObject_AsPOINT($source, &point_input))
return NULL;
$target = &point_input;
}
%typemap(python,in) POINT INPUT {
if (!PyWinObject_AsPOINT($source, &$target))
return NULL;
}
%typemap(python,in) POINT *BOTH = POINT *INPUT;
%typemap(python,argout) POINT *BOTH = POINT *OUTPUT;
%typemap(python,in) SIZE *INPUT(SIZE size_input){
if (!PyWinObject_AsSIZE($source, &size_input))
return NULL;
$target = &size_input;
}
// @object PyICONINFO|Tuple describing an icon or cursor
// @pyseeapi ICONINFO
%typemap(python,in) ICONINFO *INPUT(ICONINFO iconinfo_input) {
PyObject *obmask, *obcolor;
if (PyTuple_Check($source)) {
if (!PyArg_ParseTuple($source, "lllOO",
&iconinfo_input.fIcon, // @tupleitem 0|boolean|Icon|True indicates an icon, False for a cursor
&iconinfo_input.xHotspot, // @tupleitem 1|int|xHotSpot|For a cursor, X coord of hotspot. Ignored for icons
&iconinfo_input.yHotspot, // @tupleitem 2|int|yHotSpot|For a cursor, Y coord of hotspot. Ignored for icons
&obmask, // @tupleitem 3|<o PyGdiHANDLE>|hbmMask|Monochrome mask bitmap
&obcolor)) // @tupleitem 4|<o PyGdiHANDLE>|hbmColor|Color bitmap, may be None for black and white icon
return PyErr_Format(PyExc_TypeError, "%s: an ICONINFO must be a tuple of (int,int,int,HANDLE,HANDLE)", "$name");
if (!PyWinObject_AsHANDLE(obmask, (HANDLE *)&iconinfo_input.hbmMask))
return NULL;
if (!PyWinObject_AsHANDLE(obcolor, (HANDLE *)&iconinfo_input.hbmColor))
return NULL;
$target = &iconinfo_input;
} else {
return PyErr_Format(PyExc_TypeError, "%s: an ICONINFO must be a tuple of (int,int,int,HANDLE,HANDLE)", "$name");
}
}
%typemap(python,argout) ICONINFO *OUTPUT {
PyObject *o;
o = Py_BuildValue("lllNN", $source->fIcon, $source->xHotspot, $source->yHotspot,
PyWinObject_FromGdiHANDLE($source->hbmMask), PyWinObject_FromGdiHANDLE($source->hbmColor));
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,ignore) ICONINFO *OUTPUT(ICONINFO temp)
{
$target = &temp;
}
%typemap(python,in) BLENDFUNCTION *INPUT(BLENDFUNCTION bf_input) {
if (!PyWinObject_AsBLENDFUNCTION($source, &bf_input))
return NULL;
$target = &bf_input;
}
%typemap(python,argout) PAINTSTRUCT *OUTPUT {
PyObject *o;
o = Py_BuildValue("(Nl(iiii)llN)",
PyWinLong_FromHANDLE($source->hdc),
$source->fErase,
$source->rcPaint.left, $source->rcPaint.top, $source->rcPaint.right, $source->rcPaint.bottom,
$source->fRestore,
$source->fIncUpdate,
PyBytes_FromStringAndSize((char *)$source->rgbReserved,sizeof($source->rgbReserved)));
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,ignore) PAINTSTRUCT *OUTPUT(PAINTSTRUCT ps_output)
{
$target = &ps_output;
}
%typemap(python,in) PAINTSTRUCT *INPUT(PAINTSTRUCT ps_input) {
char *szReserved;
Py_ssize_t lenReserved;
PyObject *obdc, *obReserved;
if (PyTuple_Check($source)) {
if (!PyArg_ParseTuple($source,
"Ol(iiii)llO",
&obdc,
&ps_input.fErase,
&ps_input.rcPaint.left, &ps_input.rcPaint.top, &ps_input.rcPaint.right, &ps_input.rcPaint.bottom,
&ps_input.fRestore,
&ps_input.fIncUpdate,
&obReserved))
return NULL;
if (!PyWinObject_AsHANDLE(obdc, (HANDLE *)&ps_input.hdc))
return NULL;
if (PyBytes_AsStringAndSize(obReserved, &szReserved, &lenReserved)==-1)
return NULL;
if (lenReserved != sizeof(ps_input.rgbReserved))
return PyErr_Format(PyExc_ValueError, "%s: last element must be string of %d bytes",
"$name", sizeof(ps_input.rgbReserved));
memcpy(&ps_input.rgbReserved, szReserved, sizeof(ps_input.rgbReserved));
$target = &ps_input;
} else {
return PyErr_Format(PyExc_TypeError, "%s: a PAINTSTRUCT must be a tuple", "$name");
}
}
// @object TRACKMOUSEEVENT|A tuple of (dwFlags, hwndTrack, dwHoverTime)
%typemap(python,in) TRACKMOUSEEVENT *INPUT(TRACKMOUSEEVENT e){
PyObject *obhwnd;
e.cbSize = sizeof e;
if (PyTuple_Check($source)) {
if (PyArg_ParseTuple($source, "lOl", &e.dwFlags, &obhwnd, &e.dwHoverTime) == 0) {
return PyErr_Format(PyExc_TypeError, "%s: a TRACKMOUSEEVENT must be a tuple of 3 integers", "$name");
}
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&e.hwndTrack))
return NULL;
$target = &e;
} else {
return PyErr_Format(PyExc_TypeError, "%s: a TRACKMOUSEEVENT must be a tuple of 3 integers", "$name");
}
}
%typemap(python,except) LRESULT {
Py_BEGIN_ALLOW_THREADS
$function
Py_END_ALLOW_THREADS
}
%typemap(python,except) BOOL {
Py_BEGIN_ALLOW_THREADS
$function
Py_END_ALLOW_THREADS
}
%typemap(python,except) HWND, HDC, HMENU, HICON, HBITMAP, HIMAGELIST {
Py_BEGIN_ALLOW_THREADS
SetLastError(0);
$function
Py_END_ALLOW_THREADS
DWORD le;
if ($source==0 && (le=GetLastError())) {
$cleanup
return PyWin_SetAPIError("$name", le);
}
}
%{
#ifdef STRICT
#define MYWNDPROC WNDPROC
#else
#define MYWNDPROC FARPROC
#endif
// Returns TRUE if a call was made (and the rc is in the param)
// Returns FALSE if nothing could be done (so the caller should probably
// call its default)
// NOTE: assumes thread state already acquired.
BOOL PyWndProc_Call(PyObject *obFuncOrMap, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *prc)
{
// oldWndProc may be:
// NULL : Call DefWindowProc
// -1 : Assumed a dialog proc, and returns FALSE
// else : A valid WndProc to call.
PyObject *obFunc = NULL;
if (obFuncOrMap!=NULL) {
if (PyDict_Check(obFuncOrMap)) {
PyObject *key = PyLong_FromLong(uMsg);
if (key==NULL){
HandleError("Internal error converting Msg param of window procedure");
return FALSE;
}
obFunc = PyDict_GetItem(obFuncOrMap, key);
Py_DECREF(key);
} else {
obFunc = obFuncOrMap;
}
}
if (obFunc==NULL)
return FALSE;
// We are dispatching to Python...
PyObject *args = Py_BuildValue("NlNN", PyWinLong_FromHANDLE(hWnd), uMsg,
PyWinObject_FromPARAM(wParam), PyWinObject_FromPARAM(lParam));
if (args==NULL){
HandleError("Error building argument tuple for python callback");
return FALSE;
}
PyObject *ret = PyObject_CallObject(obFunc, args);
Py_DECREF(args);
LRESULT rc = 0;
if (ret){
if (!PyWinObject_AsPARAM(ret, (LPARAM *)&rc))
HandleError("WNDPROC return value cannot be converted to LRESULT");
Py_DECREF(ret);
}
else
HandleError("Python WNDPROC handler failed");
*prc = rc;
return TRUE;
}
LRESULT CALLBACK PyWndProcClass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PyObject *obFunc = (PyObject *)GetClassLongPtr( hWnd, 0);
LRESULT rc = 0;
CEnterLeavePython _celp;
if (!PyWndProc_Call(obFunc, hWnd, uMsg, wParam, lParam, &rc)) {
_celp.release();
rc = DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return rc;
}
LRESULT CALLBACK PyDlgProcClass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PyObject *obFunc = (PyObject *)GetClassLongPtr( hWnd, 0);
LRESULT rc = 0;
CEnterLeavePython _celp;
if (!PyWndProc_Call(obFunc, hWnd, uMsg, wParam, lParam, &rc)) {
_celp.release();
rc = DefDlgProc(hWnd, uMsg, wParam, lParam);
}
return rc;
}
LRESULT CALLBACK PyWndProcHWND(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CEnterLeavePython _celp;
PyObject *key = PyWinLong_FromHANDLE(hWnd);
PyObject *obInfo = PyDict_GetItem(g_HWNDMap, key);
Py_DECREF(key);
MYWNDPROC oldWndProc = NULL;
PyObject *obFunc = NULL;
if (obInfo!=NULL) { // Is one of ours!
obFunc = PyTuple_GET_ITEM(obInfo, 0);
PyObject *obOldWndProc = PyTuple_GET_ITEM(obInfo, 1);
PyWinLong_AsVoidPtr(obOldWndProc, (void **)&oldWndProc);
}
LRESULT rc = 0;
if (!PyWndProc_Call(obFunc, hWnd, uMsg, wParam, lParam, &rc))
if (oldWndProc) {
_celp.release();
rc = CallWindowProc(oldWndProc, hWnd, uMsg, wParam, lParam);
}
#ifdef WM_NCDESTROY
if (uMsg==WM_NCDESTROY) {
#else // CE doesnt have this message!
if (uMsg==WM_DESTROY) {
#endif
_celp.acquire(); // in case we released above - safe if already acquired.
PyObject *key = PyWinLong_FromHANDLE(hWnd);
if (PyDict_DelItem(g_HWNDMap, key) != 0)
PyErr_Clear();
Py_DECREF(key);
}
return rc;
}
INT_PTR CALLBACK PyDlgProcHDLG(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BOOL rc = FALSE;
CEnterLeavePython _celp;
if (uMsg==WM_INITDIALOG) {
// The lparam is our PyObject.
// Put our HWND in the map.
PyObject *obTuple = (PyObject *)lParam;
PyObject *obWndProc = PyTuple_GET_ITEM(obTuple, 0);
// Replace the lParam with the one the user specified.
lParam = 0;
if (PyTuple_GET_ITEM(obTuple, 1) != Py_None)
PyWinObject_AsPARAM( PyTuple_GET_ITEM(obTuple, 1), &lParam );
PyObject *key = PyWinLong_FromHANDLE(hWnd);
PyDict_SetItem(g_DLGMap, key, obWndProc);
Py_DECREF(key);
// obWndProc has no reference.
rc = TRUE;
} else if(uMsg == WM_ACTIVATE) { // see MS TID Q71450 and PumpMessages
if(0 == wParam)
hDialogCurrent = NULL;
else
hDialogCurrent = hWnd;
}
// If our HWND is in the map, then call it.
PyObject *obFunc = NULL;
PyObject *key = PyWinLong_FromHANDLE(hWnd);
obFunc = PyDict_GetItem(g_DLGMap, key);
Py_XDECREF(key);
if (!obFunc)
PyErr_Clear();
if (obFunc) {
LRESULT lrc;
if (PyWndProc_Call(obFunc, hWnd, uMsg, wParam, lParam, &lrc))
rc = (BOOL)lrc;
}
#ifdef WM_NCDESTROY
if (uMsg==WM_NCDESTROY) {
#else // CE doesnt have this message!
if (uMsg==WM_DESTROY) {
#endif
PyObject *key = PyWinLong_FromHANDLE(hWnd);
if (g_DLGMap != NULL)
if (PyDict_DelItem(g_DLGMap, key) != 0)
PyErr_Clear();
Py_DECREF(key);
}
return rc;
}
#include "structmember.h"
// Support for a WNDCLASS object.
class PyWNDCLASS : public PyObject
{
public:
WNDCLASS *GetWC() {return &m_WNDCLASS;}
PyWNDCLASS(void);
~PyWNDCLASS();
/* Python support */
static PyObject *meth_SetDialogProc(PyObject *self, PyObject *args);
static void deallocFunc(PyObject *ob);
static PyObject *getattro(PyObject *self, PyObject *obname);
static int setattro(PyObject *self, PyObject *obname, PyObject *v);
static struct PyMemberDef members[];
static struct PyMethodDef methods[];
static PyObject *PySetDialogProc(PyObject *self, PyObject *args);
WNDCLASS m_WNDCLASS;
PyObject *m_obMenuName, *m_obClassName, *m_obWndProc;
};
#define PyWNDCLASS_Check(ob) ((ob)->ob_type == &PyWNDCLASSType)
// @object PyWNDCLASS|A Python object, representing an WNDCLASS structure
// @comm Typically you create a PyWNDCLASS object, and set its properties.
// The object can then be passed to any function which takes an WNDCLASS object
PyTypeObject PyWNDCLASSType =
{
PYWIN_OBJECT_HEAD
"PyWNDCLASS",
sizeof(PyWNDCLASS),
0,
PyWNDCLASS::deallocFunc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyWNDCLASS::getattro, /* tp_getattro */
PyWNDCLASS::setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PyWNDCLASS::methods, /* tp_methods */
PyWNDCLASS::members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
#define OFF(e) offsetof(PyWNDCLASS, e)
/*static*/ struct PyMemberDef PyWNDCLASS::members[] = {
{"style", T_INT, OFF(m_WNDCLASS.style)}, // @prop integer|style|
// {"cbClsExtra", T_INT, OFF(m_WNDCLASS.cbClsExtra)}, // @prop integer|cbClsExtra|
{"cbWndExtra", T_INT, OFF(m_WNDCLASS.cbWndExtra)}, // @prop integer|cbWndExtra|
{NULL}
// ack - these are also handled now explicitly, as T_LONGLONG is too
// stupid to handle ints :(
// @prop integer|hInstance|
// @prop integer|hIcon|
// @prop integer|hCursor|
// @prop integer|hbrBackground|
// These 3 handled manually in PyWNDCLASS::getattro/setattro. The pymeth below is used as an
// end tag, so these props will be lost if below it
// @prop string|lpszMenuName|
// @prop string|lpszClassName|
// @prop function|lpfnWndProc|
};
PyObject *PyWNDCLASS::PySetDialogProc(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":SetDialogProc"))
return NULL;
PyWNDCLASS *pW = (PyWNDCLASS *)self;
pW->m_WNDCLASS.lpfnWndProc = (WNDPROC)PyDlgProcClass;
Py_INCREF(Py_None);
return Py_None;
}
struct PyMethodDef PyWNDCLASS::methods[] = {
{"SetDialogProc", PyWNDCLASS::PySetDialogProc, 1}, // @pymeth SetDialogProc|Sets the WNDCLASS to be for a dialog box.
// @pymethod |PyWNDCLASS|SetDialogProc|Sets the WNDCLASS to be for a dialog box
{NULL}
};
PyWNDCLASS::PyWNDCLASS()
{
ob_type = &PyWNDCLASSType;
_Py_NewReference(this);
memset(&m_WNDCLASS, 0, sizeof(m_WNDCLASS));
m_WNDCLASS.cbClsExtra = sizeof(PyObject *);
m_WNDCLASS.lpfnWndProc = PyWndProcClass;
m_obMenuName = m_obClassName = m_obWndProc = NULL;
}
PyWNDCLASS::~PyWNDCLASS(void)
{
Py_XDECREF(m_obMenuName);
Py_XDECREF(m_obClassName);
Py_XDECREF(m_obWndProc);
}
PyObject *PyWNDCLASS::getattro(PyObject *self, PyObject *obname)
{
char *name=PYWIN_ATTR_CONVERT(obname);
if (name==NULL)
return NULL;
PyWNDCLASS *pW = (PyWNDCLASS *)self;
if (strcmp("lpszMenuName", name)==0) {
PyObject *ret = pW->m_obMenuName ? pW->m_obMenuName : Py_None;
Py_INCREF(ret);
return ret;
}
if (strcmp("lpszClassName", name)==0) {
PyObject *ret = pW->m_obClassName ? pW->m_obClassName : Py_None;
Py_INCREF(ret);
return ret;
}
if (strcmp("lpfnWndProc", name)==0) {
PyObject *ret = pW->m_obWndProc ? pW->m_obWndProc : Py_None;
Py_INCREF(ret);
return ret;
}
if (strcmp("hInstance", name)==0)
return PyWinLong_FromVoidPtr(pW->m_WNDCLASS.hInstance);
if (strcmp("hIcon", name)==0)
return PyWinLong_FromVoidPtr(pW->m_WNDCLASS.hIcon);
if (strcmp("hCursor", name)==0)
return PyWinLong_FromVoidPtr(pW->m_WNDCLASS.hCursor);
if (strcmp("hbrBackground", name)==0)
return PyWinLong_FromVoidPtr(pW->m_WNDCLASS.hbrBackground);