Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Window.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5cfe796add8b648db290ccd7a9a01e7820994ee0 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
/* 
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA	02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * This is a new part of Blender.
 *
 * Contributor(s): Willian P. Germano, Tom Musgrove, Michael Reimpell,
 * Yann Vernier, Ken Hughes
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
*/

#include <Python.h>

#include "BDR_editobject.h"	/* enter / leave editmode */
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_object.h"		/* for during_script() and during_scriptlink() */
#include "BKE_scene.h"		/* scene_find_camera() */
#include "BIF_mywindow.h"
#include "BIF_imasel.h"
#include "BSE_headerbuttons.h"
#include "BSE_filesel.h"
#include "BIF_editmesh.h"	/* for undo_push_mesh() */
#include "BIF_screen.h"
#include "BIF_space.h"
#include "BIF_drawtext.h"
#include "BIF_poseobject.h"
#include "DNA_view3d_types.h"
#include "DNA_space_types.h"
#include "DNA_scene_types.h"
#include "DNA_text_types.h"
#include "DNA_object_types.h"
#include "mydevice.h"
#include "blendef.h"		/* OBACT */
#include "windowTheme.h"
#include "Mathutils.h"
#include "constant.h"
#include "gen_utils.h"
#include "Armature.h"

/* Pivot Types 
-0 for Bounding Box Center; \n\
-1 for 3D Cursor\n\
-2 for Individual Centers\n\
-3 for Median Point\n\
-4 for Active Object"; */

#define		PIVOT_BOUNDBOX		0
#define		PIVOT_CURSOR		1
#define		PIVOT_INDIVIDUAL	2
#define		PIVOT_MEDIAN		3
#define		PIVOT_ACTIVE		4

/* See Draw.c */
extern int EXPP_disable_force_draw;
extern void setcameratoview3d(void);

/*****************************************************************************/
/* Python API function prototypes for the Window module.		*/
/*****************************************************************************/
PyObject *M_Window_Redraw( PyObject * self, PyObject * args );
static PyObject *M_Window_RedrawAll( PyObject * self, PyObject * args );
static PyObject *M_Window_QRedrawAll( PyObject * self, PyObject * args );
static PyObject *M_Window_DrawProgressBar( PyObject * self, PyObject * args );
static PyObject *M_Window_GetCursorPos( PyObject * self );
static PyObject *M_Window_SetCursorPos( PyObject * self, PyObject * args );
static PyObject *M_Window_WaitCursor( PyObject * self, PyObject * args );
static PyObject *M_Window_GetViewVector( PyObject * self );
static PyObject *M_Window_GetActiveLayer( PyObject * self );
static PyObject *M_Window_SetActiveLayer( PyObject * self, PyObject * args );
static PyObject *M_Window_GetViewQuat( PyObject * self );
static PyObject *M_Window_SetViewQuat( PyObject * self, PyObject * args );
static PyObject *M_Window_GetViewOffset( PyObject * self );
static PyObject *M_Window_SetViewOffset( PyObject * self, PyObject * args );
static PyObject *M_Window_GetViewMatrix( PyObject * self );
static PyObject *M_Window_GetPerspMatrix( PyObject * self );
static PyObject *M_Window_FileSelector( PyObject * self, PyObject * args );
static PyObject *M_Window_ImageSelector( PyObject * self, PyObject * args );
static PyObject *M_Window_EditMode( PyObject * self, PyObject * args );
static PyObject *M_Window_PoseMode( PyObject * self, PyObject * args );
static PyObject *M_Window_ViewLayers( PyObject * self, PyObject * args );
static PyObject *M_Window_CameraView( PyObject * self, PyObject * args );
static PyObject *M_Window_QTest( PyObject * self );
static PyObject *M_Window_QRead( PyObject * self );
static PyObject *M_Window_QAdd( PyObject * self, PyObject * args );
static PyObject *M_Window_QHandle( PyObject * self, PyObject * args );
static PyObject *M_Window_TestBreak( PyObject * self );
static PyObject *M_Window_GetMouseCoords( PyObject * self );
static PyObject *M_Window_SetMouseCoords( PyObject * self, PyObject * args );
static PyObject *M_Window_GetMouseButtons( PyObject * self );
static PyObject *M_Window_GetKeyQualifiers( PyObject * self );
static PyObject *M_Window_SetKeyQualifiers( PyObject * self, PyObject * args );
static PyObject *M_Window_GetAreaSize( PyObject * self );
static PyObject *M_Window_GetAreaID( PyObject * self );
static PyObject *M_Window_GetScreenSize( PyObject * self );
static PyObject *M_Window_GetScreens( PyObject * self );
static PyObject *M_Window_SetScreen( PyObject * self, PyObject * value );
static PyObject *M_Window_GetScreenInfo( PyObject * self, PyObject * args,
					 PyObject * kwords );
static PyObject *M_Window_GetPivot( PyObject * self );
static PyObject *M_Window_SetPivot( PyObject * self, PyObject * value );

PyObject *Window_Init( void );


/*****************************************************************************/
/* The following string definitions are used for documentation strings.	    */
/* In Python these will be written to the console when doing a		    */
/* Blender.Window.__doc__						    */
/*****************************************************************************/
static char M_Window_doc[] = "The Blender Window module\n\n";

static char M_Window_Redraw_doc[] =
	"() - Force a redraw of a specific Window Type (see Window.Types)";

static char M_Window_RedrawAll_doc[] = "() - Redraw all windows";

static char M_Window_QRedrawAll_doc[] =
	"() - Redraw all windows by queue event";

static char M_Window_FileSelector_doc[] =
	"(callback [, title, filename]) - Open a file selector window.\n\
The selected file name is used as argument to a function callback f(name)\n\
that you must provide. 'title' is optional and defaults to 'SELECT FILE'.\n\
'filename' is optional and defaults to Blender.Get('filename').\n\n\
Example:\n\n\
import Blender\n\n\
def my_function(filename):\n\
	print 'The selected file was: ', filename\n\n\
Blender.Window.FileSelector(my_function, 'SAVE FILE')\n";

static char M_Window_ImageSelector_doc[] =
	"(callback [, title, filename]) - Open an image selector window.\n\
The selected file name is used as argument to a function callback f(name)\n\
that you must provide. 'title' is optional and defaults to 'SELECT IMAGE'.\n\
'filename' is optional and defaults to Blender.Get('filename').\n\n\
Example:\n\n\
import Blender\n\n\
def my_function(filename):\n\
	print 'The selected image file was: ', filename\n\n\
Blender.Window.ImageSelector(my_function, 'LOAD IMAGE')\n";

static char M_Window_DrawProgressBar_doc[] =
	"(done, text) - Draw a progress bar.\n\
'done' is a float value <= 1.0, 'text' contains info about what is\n\
currently being done.";

static char M_Window_GetCursorPos_doc[] =
	"() - Get the current 3d cursor position as a list of three floats.";

static char M_Window_SetCursorPos_doc[] =
	"([f,f,f]) - Set the current 3d cursor position from a list of three floats.";

static char M_Window_WaitCursor_doc[] =
	"(bool) - Set cursor to wait mode (nonzero bool) or normal mode (0).";

static char M_Window_GetViewVector_doc[] =
	"() - Get the current 3d view vector as a list of three floats [x,y,z].";

static char M_Window_GetActiveLayer_doc[] =
	"() - Get the current 3d views active layer where new objects are created.";

static char M_Window_SetActiveLayer_doc[] =
	"(int) - Set the current 3d views active layer where new objects are created.";

static char M_Window_GetViewMatrix_doc[] =
	"() - Get the current 3d view matrix.";

static char M_Window_GetPerspMatrix_doc[] =
	"() - Get the current 3d Persp matrix.";

static char M_Window_EditMode_doc[] =
	"() - Get the current status -- 0: not in edit mode; 1: in edit mode.\n\
(status) - if 1: enter edit mode; if 0: leave edit mode.\n\
Returns the current status.  This function is mostly useful to leave\n\
edit mode before applying changes to a mesh (otherwise the changes will\n\
be lost) and then returning to it upon leaving.";
static char M_Window_PoseMode_doc[] =
		"() - Get the current status -- 0: not in pose mode; 1: in edit mode";

static char M_Window_ViewLayers_doc[] =
	"(layers = [], winid = None) - Get/set active layers in all 3d View windows.\n\
() - Make no changes, only return currently visible layers.\n\
(layers = []) - a list of integers, each in the range [1, 20].\n\
(layers = [], winid = int) - layers as above, winid is an optional.\n\
arg that makes the function only set layers for that view.\n\
This function returns the currently visible layers as a list of ints.";

static char M_Window_GetViewQuat_doc[] =
	"() - Get the current VIEW3D view quaternion values.";

static char M_Window_SetViewQuat_doc[] =
	"(quat) - Set the current VIEW3D view quaternion values.\n\
(quat) - [f,f,f,f] or f,f,f,f: the new float values.";

static char M_Window_GetViewOffset_doc[] =
	"() - Get the current VIEW3D view offset values.";

static char M_Window_SetViewOffset_doc[] =
	"(ofs) - Set the current VIEW3D view offset values.\n\
(ofs) - [f,f,f] or f,f,f: the new float values.";

static char M_Window_CameraView_doc[] =
	"(camtov3d = 0) - Set the current VIEW3D view to the active camera's view.\n\
(camtov3d = 0) - bool: if nonzero it's the camera that gets positioned at the\n\
current view, instead of the view being changed to that of the camera.\n\n\
If no camera is the active object, the active camera for the current scene\n\
is used.";

static char M_Window_QTest_doc[] =
	"() - Check if there are pending events in the event queue.";

static char M_Window_QRead_doc[] =
	"() - Get the next pending event from the event queue.\n\
This function returns a list [event, val], where:\n\
event - int: the key or mouse event (see Blender.Draw module);\n\
val - int: if 1 it's a key or mouse button press, if 0 a release.  For\n\
	mouse movement events 'val' returns the new coordinates in x or y.";

static char M_Window_QAdd_doc[] =
	"(win, evt, val, after = 0) - Add an event to some window's event queue.\n\
(win) - int: the win id, see Blender.Window.GetScreenInfo();\n\
(evt) - int: the event number, see events in Blender.Draw;\n\
(val) - bool: 1 for a key press, 0 for a release;\n\
(after) - bool: if 1 the event is put after the current queue and added later.";

static char M_Window_QHandle_doc[] =
	"(win) - Process all events for the given window (area) now.\n\
(win) - int: the window id, see Blender.Window.GetScreenInfo().\n\n\
See Blender.Window.QAdd() for how to send events to a particular window.";

static char M_Window_TestBreak_doc[] =
	"() - Returns true if the user has pressed escape.";

static char M_Window_GetMouseCoords_doc[] =
	"() - Get mouse pointer's current screen coordinates.";

static char M_Window_SetMouseCoords_doc[] =
	"(x, y) - Set mouse pointer's current screen coordinates.\n\
(x,y) - ints ([x, y] also accepted): the new x, y coordinates.";

static char M_Window_GetMouseButtons_doc[] =
	"() - Get the current mouse button state (see Blender.Window.MButs dict).";

static char M_Window_GetKeyQualifiers_doc[] =
	"() - Get the current qualifier keys state.\n\
An int is returned: or'ed combination of values in Blender.Window.Qual's dict.";

static char M_Window_SetKeyQualifiers_doc[] =
	"(qual) - Fake qualifier keys state.\n\
(qual) - int: an or'ed combination of the values in Blender.Window.Qual dict.\n\
Note: remember to reset to 0 after handling the related event (see QAdd()).";

static char M_Window_GetAreaID_doc[] =
	"() - Get the current window's (area) ID.";

static char M_Window_GetAreaSize_doc[] =
	"() - Get the current window's (area) size as [width, height].";

static char M_Window_GetScreenSize_doc[] =
	"() - Get the screen's size as [width, height].";

static char M_Window_GetScreens_doc[] =
	"() - Get a list with the names of all available screens.";

static char M_Window_SetScreen_doc[] =
	"(name) - Set current screen to the one with the given 'name'.";

static char M_Window_GetScreenInfo_doc[] =
	"(type = -1, rect = 'win', screen = None)\n\
- Get info about the the areas in the current screen setup.\n\
(type = -1) - int: the space type (Blender.Window.Types) to restrict the\n\
	results to, all if -1;\n\
(rect = 'win') - str: the rectangle of interest.  This defines if the corner\n\
	coordinates returned will refer to:\n\
	- the whole area: 'total';\n\
	- only the header: 'header';\n\
	- only the window content (default): 'win'.\n\
(screen = None) - str: the screen name, current if not given.\n\n\
A list of dictionaries (one for each area) is returned.\n\
Each dictionary has keys:\n\
'vertices': [xmin, ymin, xmax, ymax] area corners;\n\
'win': window type, see Blender.Window.Types dict;\n\
'id': area's id.";

static char M_Window_SetPivot_doc[] = 
	"(Pivot) - Set Pivot Mode for 3D Viewport:\n\
Options are: \n\
-PivotTypes.BOUNDBOX for Bounding Box Center; \n\
-PivotTypes.CURSOR for 3D Cursor\n\
-PivotTypes.INDIVIDUAL for Individual Centers\n\
-PivotTypes.MEDIAN for Median Point\n\
-PivotTypes.ACTIVE for Active Object";

static char M_Window_GetPivot_doc[] = 
	"Return the pivot for the active 3d window";

/*****************************************************************************/
/* Python method structure definition for Blender.Window module:	*/
/*****************************************************************************/
struct PyMethodDef M_Window_methods[] = {
	{"Redraw", M_Window_Redraw, METH_VARARGS, M_Window_Redraw_doc},
	{"RedrawAll", M_Window_RedrawAll, METH_VARARGS,
	 M_Window_RedrawAll_doc},
	{"QRedrawAll", M_Window_QRedrawAll, METH_VARARGS,
	 M_Window_QRedrawAll_doc},
	{"FileSelector", M_Window_FileSelector, METH_VARARGS,
	 M_Window_FileSelector_doc},
	{"ImageSelector", ( PyCFunction ) M_Window_ImageSelector, METH_VARARGS,
	 M_Window_ImageSelector_doc},
	{"DrawProgressBar", M_Window_DrawProgressBar, METH_VARARGS,
	 M_Window_DrawProgressBar_doc},
	{"drawProgressBar", M_Window_DrawProgressBar, METH_VARARGS,
	 M_Window_DrawProgressBar_doc},
	{"GetCursorPos", ( PyCFunction ) M_Window_GetCursorPos, METH_NOARGS,
	 M_Window_GetCursorPos_doc},
	{"SetCursorPos", M_Window_SetCursorPos, METH_VARARGS,
	 M_Window_SetCursorPos_doc},
	{"WaitCursor", M_Window_WaitCursor, METH_VARARGS,
	 M_Window_WaitCursor_doc},
	{"GetViewVector", ( PyCFunction ) M_Window_GetViewVector, METH_NOARGS,
	 M_Window_GetViewVector_doc},
	{"GetActiveLayer", ( PyCFunction ) M_Window_GetActiveLayer, METH_NOARGS,
	 M_Window_GetActiveLayer_doc},
	{"SetActiveLayer", ( PyCFunction ) M_Window_SetActiveLayer, METH_VARARGS,
	 M_Window_SetActiveLayer_doc},
	{"GetViewQuat", ( PyCFunction ) M_Window_GetViewQuat, METH_NOARGS,
	 M_Window_GetViewQuat_doc},
	{"SetViewQuat", ( PyCFunction ) M_Window_SetViewQuat, METH_VARARGS,
	 M_Window_SetViewQuat_doc},
	{"GetViewOffset", ( PyCFunction ) M_Window_GetViewOffset, METH_NOARGS,
	 M_Window_GetViewOffset_doc},
	{"SetViewOffset", ( PyCFunction ) M_Window_SetViewOffset, METH_VARARGS,
	 M_Window_SetViewOffset_doc},
	{"GetViewMatrix", ( PyCFunction ) M_Window_GetViewMatrix, METH_NOARGS,
	 M_Window_GetViewMatrix_doc},
	{"GetPerspMatrix", ( PyCFunction ) M_Window_GetPerspMatrix, METH_NOARGS,
	 M_Window_GetPerspMatrix_doc},
	{"EditMode", ( PyCFunction ) M_Window_EditMode, METH_VARARGS,
	 M_Window_EditMode_doc},
	{"PoseMode", ( PyCFunction ) M_Window_PoseMode, METH_VARARGS,
	 M_Window_PoseMode_doc},
	{"ViewLayers", ( PyCFunction ) M_Window_ViewLayers, METH_VARARGS,
	 M_Window_ViewLayers_doc},
	 /* typo, deprecate someday: */
	{"ViewLayer", ( PyCFunction ) M_Window_ViewLayers, METH_VARARGS,
	 M_Window_ViewLayers_doc},
	{"CameraView", ( PyCFunction ) M_Window_CameraView, METH_VARARGS,
	 M_Window_CameraView_doc},
	{"QTest", ( PyCFunction ) M_Window_QTest, METH_NOARGS,
	 M_Window_QTest_doc},
	{"QRead", ( PyCFunction ) M_Window_QRead, METH_NOARGS,
	 M_Window_QRead_doc},
	{"QAdd", ( PyCFunction ) M_Window_QAdd, METH_VARARGS,
	 M_Window_QAdd_doc},
	{"QHandle", ( PyCFunction ) M_Window_QHandle, METH_VARARGS,
	 M_Window_QHandle_doc},
	{"TestBreak", ( PyCFunction ) M_Window_TestBreak, METH_NOARGS,
	 M_Window_TestBreak_doc},
	{"GetMouseCoords", ( PyCFunction ) M_Window_GetMouseCoords,
	 METH_NOARGS,
	 M_Window_GetMouseCoords_doc},
	{"SetMouseCoords", ( PyCFunction ) M_Window_SetMouseCoords,
	 METH_VARARGS,
	 M_Window_SetMouseCoords_doc},
	{"GetMouseButtons", ( PyCFunction ) M_Window_GetMouseButtons,
	 METH_NOARGS,
	 M_Window_GetMouseButtons_doc},
	{"GetKeyQualifiers", ( PyCFunction ) M_Window_GetKeyQualifiers,
	 METH_NOARGS,
	 M_Window_GetKeyQualifiers_doc},
	{"SetKeyQualifiers", ( PyCFunction ) M_Window_SetKeyQualifiers,
	 METH_VARARGS,
	 M_Window_SetKeyQualifiers_doc},
	{"GetAreaSize", ( PyCFunction ) M_Window_GetAreaSize, METH_NOARGS,
	 M_Window_GetAreaSize_doc},
	{"GetAreaID", ( PyCFunction ) M_Window_GetAreaID, METH_NOARGS,
	 M_Window_GetAreaID_doc},
	{"GetScreenSize", ( PyCFunction ) M_Window_GetScreenSize, METH_NOARGS,
	 M_Window_GetScreenSize_doc},
	{"GetScreens", ( PyCFunction ) M_Window_GetScreens, METH_NOARGS,
	 M_Window_GetScreens_doc},
	{"SetScreen", ( PyCFunction ) M_Window_SetScreen, METH_O,
	 M_Window_SetScreen_doc},
	{"GetScreenInfo", ( PyCFunction ) M_Window_GetScreenInfo,
	 METH_VARARGS | METH_KEYWORDS, M_Window_GetScreenInfo_doc},
	{"GetPivot", ( PyCFunction ) M_Window_GetPivot, METH_NOARGS, 
	 M_Window_GetPivot_doc},
	{"SetPivot", ( PyCFunction ) M_Window_SetPivot, METH_O, 
	 M_Window_SetPivot_doc},
	{NULL, NULL, 0, NULL}
};

/*****************************************************************************/
/* Function:	M_Window_Redraw						*/
/* Python equivalent:	Blender.Window.Redraw				*/
/*****************************************************************************/
/* not static so py_slider_update in Draw.[ch] can use it */
PyObject *M_Window_Redraw( PyObject * self, PyObject * args )
{
	ScrArea *tempsa, *sa;
	int wintype = SPACE_VIEW3D;
	short redraw_all = 0;

	if( !PyArg_ParseTuple( args, "|i", &wintype ) )
		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
						"expected int argument (or nothing)" ) );

	if( wintype < 0 )
		redraw_all = 1;

	if( !during_script( ) && !G.background ) {
		tempsa = curarea;
		sa = G.curscreen->areabase.first;

		while( sa ) {

			if( sa->spacetype == wintype || redraw_all ) {
				if (sa->spacetype == SPACE_SCRIPT && EXPP_disable_force_draw) {
						scrarea_queue_redraw(sa);
				}
				else {
					/* do not call fancy hacks here like pop_space_text(st); (ton) */
					scrarea_do_windraw( sa );
					if( sa->headwin ) scrarea_do_headdraw( sa );
				}
			}
			sa = sa->next;
		}

		if( curarea != tempsa )
			areawinset( tempsa->win );

		if( curarea ) {	/* is null if Blender is in bg mode */
			if( curarea->headwin )
				scrarea_do_headdraw( curarea );
			screen_swapbuffers(  );
		}
	}

	Py_RETURN_NONE;
}

/*****************************************************************************/
/* Function: M_Window_RedrawAll						*/
/* Python equivalent:	Blender.Window.RedrawAll			*/
/*****************************************************************************/
static PyObject *M_Window_RedrawAll( PyObject * self, PyObject * args )
{
	PyObject *arg = Py_BuildValue( "(i)", -1 );
	PyObject *ret = M_Window_Redraw( self, arg );
	Py_DECREF(arg);
	return ret;
}

/*****************************************************************************/
/* Function:	M_Window_QRedrawAll					*/
/* Python equivalent:			Blender.Window.QRedrawAll	*/
/*****************************************************************************/
static PyObject *M_Window_QRedrawAll( PyObject * self, PyObject * args )
{
	EXPP_allqueue( REDRAWALL, 0 );
	Py_RETURN_NONE;
}

/*****************************************************************************/
/* Function:	M_Window_FileSelector					*/
/* Python equivalent:	Blender.Window.FileSelector			*/
/*****************************************************************************/

/* This is the callback to "activate_fileselect" below.  It receives the
 * selected filename and (using it as argument) calls the Python callback
 * provided by the script writer and stored in EXPP_FS_PyCallback. */

static void getSelectedFile( char *name )
{
	PyObject *pycallback;
	PyObject *result;
	Script *script;

	/* let's find the script that owns this callback */
	script = G.main->script.first;
	while (script) {
		if (script->flags & SCRIPT_RUNNING) break;
		script = script->id.next;
	}

	if (!script) {
		if (curarea->spacetype == SPACE_SCRIPT) {
			SpaceScript *sc = curarea->spacedata.first;
			script = sc->script;
		}
	}

	pycallback = script->py_browsercallback;

	if (pycallback) {
		PyGILState_STATE gilstate = PyGILState_Ensure();

		result = PyObject_CallFunction( pycallback, "s", name );

		if (!result) {
			if (G.f & G_DEBUG)
				fprintf(stderr, "BPy error: Callback call failed!\n");
		}
		else Py_DECREF(result);

		if (script->py_browsercallback == pycallback) {
			SCRIPT_SET_NULL(script);
		}
		/* else another call to selector was made inside pycallback */

		Py_DECREF(pycallback);

		PyGILState_Release(gilstate);
	}

	return;
}

static PyObject *M_Window_FileSelector( PyObject * self, PyObject * args )
{
	char *title = "SELECT FILE";
	char *filename = G.sce;
	SpaceScript *sc;
	Script *script = NULL;
	PyObject *pycallback = NULL;
	int startspace = 0;

	if (during_scriptlink())
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"script links can't call the file selector");

	if (G.background)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"the file selector is not available in background mode");

	if((!PyArg_ParseTuple( args, "O|ss", &pycallback, &title, &filename))
		|| (!PyCallable_Check(pycallback)))
		return EXPP_ReturnPyObjError( PyExc_AttributeError,
			"\nexpected a callback function (and optionally one or two strings) "
			"as argument(s)" );

	Py_INCREF(pycallback);

/* trick: we move to a spacescript because then the fileselector will properly
 * unset our SCRIPT_FILESEL flag when the user chooses a file or cancels the
 * selection.  This is necessary because when a user cancels, the
 * getSelectedFile function above doesn't get called and so couldn't unset the
 * flag. */
	startspace = curarea->spacetype;
	if( startspace != SPACE_SCRIPT )
		newspace( curarea, SPACE_SCRIPT );

	sc = curarea->spacedata.first;

	/* let's find the script that called us */
	script = G.main->script.first;
	while (script) {
		if (script->flags & SCRIPT_RUNNING) break;
		script = script->id.next;
	}

	if( !script ) {
		/* if not running, then we were already on a SpaceScript space, executing
		 * a registered callback -- aka: this script has a gui */
		script = sc->script;	/* this is the right script */
	} else {		/* still running, use the trick */
		script->lastspace = startspace;
		sc->script = script;
	}

	script->flags |= SCRIPT_FILESEL;

	/* clear any previous callback (nested calls to selector) */
	if (script->py_browsercallback) {
		Py_DECREF((PyObject *)script->py_browsercallback);
	}
	script->py_browsercallback = pycallback;

	/* if were not running a script GUI here alredy, then dont make this script persistant */
	if ((script->flags & SCRIPT_GUI)==0) {
		script->scriptname[0] = '\0';
		script->scriptarg[0] = '\0';
	}
	activate_fileselect( FILE_BLENDER, title, filename, getSelectedFile );
	Py_RETURN_NONE;
}

static PyObject *M_Window_ImageSelector( PyObject * self, PyObject * args )
{
	char *title = "SELECT IMAGE";
	char *filename = G.sce;
	SpaceScript *sc;
	Script *script = NULL;
	PyObject *pycallback = NULL;
	int startspace = 0;

	if (during_scriptlink())
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"script links can't call the image selector");

	if (G.background)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"the image selector is not available in background mode");

	if( !PyArg_ParseTuple( args, "O|ss", &pycallback, &title, &filename ) 
		|| (!PyCallable_Check(pycallback)))
		return EXPP_ReturnPyObjError ( PyExc_AttributeError,
			"\nexpected a callback function (and optionally one or two strings) "
			"as argument(s)" );

	Py_INCREF(pycallback);

/* trick: we move to a spacescript because then the fileselector will properly
 * unset our SCRIPT_FILESEL flag when the user chooses a file or cancels the
 * selection.  This is necessary because when a user cancels, the
 * getSelectedFile function above doesn't get called and so couldn't unset the
 * flag. */
	startspace = curarea->spacetype;
	if( startspace != SPACE_SCRIPT )
		newspace( curarea, SPACE_SCRIPT );

	sc = curarea->spacedata.first;

	/* let's find the script that called us */
	script = G.main->script.first;
	while (script) {
		if (script->flags & SCRIPT_RUNNING) break;
		script = script->id.next;
	}

	if( !script ) {
		/* if not running, then we were already on a SpaceScript space, executing
		 * a registered callback -- aka: this script has a gui */
		script = sc->script;	/* this is the right script */
	} else {		/* still running, use the trick */
		script->lastspace = startspace;
		sc->script = script;
	}

	script->flags |= SCRIPT_FILESEL;	/* same flag as filesel */
	/* clear any previous callback (nested calls to selector) */
	if (script->py_browsercallback) {
		Py_DECREF((PyObject *)script->py_browsercallback);
	}
	script->py_browsercallback = pycallback;

	activate_imageselect( FILE_BLENDER, title, filename, getSelectedFile );

	Py_RETURN_NONE;
}

/*****************************************************************************/
/* Function:	M_Window_DrawProgressBar		          	*/
/* Python equivalent:	Blender.Window.DrawProgressBar			*/
/*****************************************************************************/
static PyObject *M_Window_DrawProgressBar( PyObject * self, PyObject * args )
{
	float done;
	char *info = NULL;
	int retval = 0;
	ScrArea *sa = curarea;

	if (G.background)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"the progress bar is not available in background mode");

	if( !PyArg_ParseTuple( args, "fs", &done, &info ) )
		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
			"expected a float and a string as arguments" ) );

	retval = progress_bar( done, info );

	areawinset(sa->win);

	return Py_BuildValue( "i", retval );
}

/*****************************************************************************/
/* Function:   M_Window_GetCursorPos					*/
/* Python equivalent:	Blender.Window.GetCursorPos			*/
/*****************************************************************************/
static PyObject *M_Window_GetCursorPos( PyObject * self )
{
	float *cursor = NULL;
	PyObject *pylist;

	if( G.vd && G.vd->localview )
		cursor = G.vd->cursor;
	else
		cursor = G.scene->cursor;

	pylist = Py_BuildValue( "[fff]", cursor[0], cursor[1], cursor[2] );

	if( !pylist )
		return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
						"GetCursorPos: couldn't create pylist" ) );

	return pylist;
}

/*****************************************************************************/
/* Function:	M_Window_SetCursorPos					*/
/* Python equivalent:	Blender.Window.SetCursorPos			*/
/*****************************************************************************/
static PyObject *M_Window_SetCursorPos( PyObject * self, PyObject * args )
{
	int ok = 0;
	float val[3];

	if( PyObject_Length( args ) == 3 )
		ok = PyArg_ParseTuple( args, "fff", &val[0], &val[1],
				       &val[2] );
	else
		ok = PyArg_ParseTuple( args, "(fff)", &val[0], &val[1],
				       &val[2] );

	if( !ok )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected [f,f,f] or f,f,f as arguments" );

	if( G.vd && G.vd->localview ) {
		G.vd->cursor[0] = val[0];
		G.vd->cursor[1] = val[1];
		G.vd->cursor[2] = val[2];
	} else {
		G.scene->cursor[0] = val[0];
		G.scene->cursor[1] = val[1];
		G.scene->cursor[2] = val[2];
	}

	Py_RETURN_NONE;
}

static PyObject *M_Window_WaitCursor( PyObject * self, PyObject * args )
{
	int bool;

	if( !PyArg_ParseTuple( args, "i", &bool ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected bool (0 or 1) or nothing as argument" );

	waitcursor( bool );	/* nonzero bool sets, zero unsets */

	Py_RETURN_NONE;
}

/*****************************************************************************/
/* Function:	M_Window_GetViewVector					*/
/* Python equivalent:	Blender.Window.GetViewVector			*/
/*****************************************************************************/
static PyObject *M_Window_GetViewVector( PyObject * self )
{
	float *vec = NULL;

	if( !G.vd )
		Py_RETURN_NONE;

	vec = G.vd->viewinv[2];

	return Py_BuildValue( "[fff]", vec[0], vec[1], vec[2] );
}

/*****************************************************************************/
/* Function:	M_Window_GetActiveLayer					*/
/* Python equivalent:	Blender.Window.GetActiveLayer			*/
/*****************************************************************************/
static PyObject *M_Window_GetActiveLayer( PyObject * self )
{
	if (!G.vd) {
		return PyInt_FromLong(0);
	} else {
		return PyInt_FromLong( G.vd->layact );
	}
}

static PyObject *M_Window_SetActiveLayer( PyObject * self, PyObject * args )
{
	int layer, bit=1;
	if( !PyArg_ParseTuple( args, "i", &layer ) )
		return ( EXPP_ReturnPyObjError( PyExc_TypeError,
						"expected an int" ) );
	
	if (!G.vd)
		Py_RETURN_FALSE;
	
	bit= 0;
	while(bit<32) {
		if(layer & (1<<bit)) {
			G.vd->layact= 1<<bit;
			G.vd->lay |= G.vd->layact;
			
			if (G.vd->scenelock) {
				G.scene->lay |= G.vd->layact; 
			}
			bit = -1; /* no error */
			break;
		}
		bit++;
	}
	
	if (bit != -1)
		return ( EXPP_ReturnPyObjError( PyExc_ValueError,
					"The flag could not be used for the active layer" ) );
	
	Py_RETURN_NONE;
}

static PyObject *M_Window_GetViewQuat( PyObject * self )
{
	float *vec = NULL;

	if( !G.vd )
		Py_RETURN_NONE;

	vec = G.vd->viewquat;

	return Py_BuildValue( "[ffff]", vec[0], vec[1], vec[2], vec[3] );
}

static PyObject *M_Window_SetViewQuat( PyObject * self, PyObject * args )
{
	int ok = 0;
	float val[4];

	if( !G.vd )
		Py_RETURN_NONE;

	if( PyObject_Length( args ) == 4 )
		ok = PyArg_ParseTuple( args, "ffff", &val[0], &val[1], &val[2],
				       &val[3] );
	else
		ok = PyArg_ParseTuple( args, "(ffff)", &val[0], &val[1],
				       &val[2], &val[3] );

	if( !ok )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected [f,f,f,f] or f,f,f,f as arguments" );

	G.vd->viewquat[0] = val[0];
	G.vd->viewquat[1] = val[1];
	G.vd->viewquat[2] = val[2];
	G.vd->viewquat[3] = val[3];

	Py_RETURN_NONE;
}

static PyObject *M_Window_GetViewOffset( PyObject * self )
{
	if( !G.vd )
		Py_RETURN_NONE;
	return Py_BuildValue( "[fff]", G.vd->ofs[0], G.vd->ofs[1], G.vd->ofs[2] );
}

static PyObject *M_Window_SetViewOffset( PyObject * self, PyObject * args )
{
	int ok = 0;
	float val[3];

	if( !G.vd )
		Py_RETURN_NONE;

	if( PyObject_Length( args ) == 3 )
		ok = PyArg_ParseTuple( args, "fff", &val[0], &val[1],
				       &val[2] );
	else
		ok = PyArg_ParseTuple( args, "(fff)", &val[0], &val[1],
				       &val[2] );

	if( !ok )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected [f,f,f] or f,f,f as arguments" );

	G.vd->ofs[0] = val[0];
	G.vd->ofs[1] = val[1];
	G.vd->ofs[2] = val[2];

	Py_RETURN_NONE;
}


/*****************************************************************************/
/* Function:	M_Window_GetViewMatrix				*/
/* Python equivalent:	Blender.Window.GetViewMatrix		*/
/*****************************************************************************/
static PyObject *M_Window_GetViewMatrix( PyObject * self )
{
	if( !G.vd )
		Py_RETURN_NONE;

	return newMatrixObject( ( float * ) G.vd->viewmat, 4, 4, Py_WRAP );
}

/*****************************************************************************/
/* Function:	M_Window_GetPerspMatrix				*/
/* Python equivalent:	Blender.Window.GetPerspMatrix		*/
/*****************************************************************************/
static PyObject *M_Window_GetPerspMatrix( PyObject * self )
{
	if( !G.vd )
		Py_RETURN_NONE;

	return newMatrixObject( ( float * ) G.vd->persmat, 4, 4, Py_WRAP );
}


/* update_armature_weakrefs()
 * helper function used in M_Window_EditMode.
 * rebuilds list of Armature weakrefs in __main__
 */

static int update_armature_weakrefs()
{
	/* stuff for armature weak refs */
	char *list_name = ARM_WEAKREF_LIST_NAME;
	PyObject *maindict = NULL, *armlist = NULL;
	PyObject *pyarmature = NULL;
	int x;

	maindict= PyModule_GetDict(PyImport_AddModule(	"__main__"));
	armlist = PyDict_GetItemString(maindict, list_name);
	if( !armlist){
		printf("Oops - update_armature_weakrefs()\n");
		return 0;
	}

	for (x = 0; x < PySequence_Size(armlist); x++){
		pyarmature = PyWeakref_GetObject(PySequence_GetItem(	armlist, x));
		if (pyarmature != Py_None)
			Armature_RebuildEditbones(pyarmature);
	}
	return 1;
}


static PyObject *M_Window_EditMode( PyObject * self, PyObject * args )
{
	short status = -1;
	char *undo_str = "From script";
	int undo_str_len = 11;
	int do_undo = 1;

	if( !PyArg_ParseTuple( args,
				"|hs#i", &status, &undo_str, &undo_str_len, &do_undo ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
				"expected optional int (bool), string and int (bool) as arguments" );

	if( status >= 0 ) {
		if( status ) {
			if( !G.obedit ){

				//update armatures
				if(! update_armature_weakrefs()){
					return EXPP_ReturnPyObjError( 
						PyExc_RuntimeError,
						"internal error -  update_armature_weakrefs");
				}

				//enter editmode
				enter_editmode(0);
			}
		} else if( G.obedit ) {
			if( undo_str_len > 63 )
				undo_str[63] = '\0';	/* 64 is max */
			BIF_undo_push( undo_str ); /* This checks user undo settings */
			exit_editmode( EM_FREEDATA );

			//update armatures
			if(! update_armature_weakrefs()){
				return EXPP_ReturnPyObjError( 
					PyExc_RuntimeError,
					"internal error -  update_armature_weakrefs");
			}

		}
	}

	return Py_BuildValue( "h", G.obedit ? 1 : 0 );
}

static PyObject *M_Window_PoseMode( PyObject * self, PyObject * args )
{
	short status = -1;
	short is_posemode = 0;
	Base *base;
	
	if( !PyArg_ParseTuple( args, "|h", &status ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
									  "expected optional int (bool) as argument" );

	if( status >= 0 ) {
		if( status ) {
			enter_posemode();
		} else if( G.obedit ) {
			exit_posemode();
		}
	}

	base= BASACT;
	if (base && base->object->flag & OB_POSEMODE) {
		is_posemode = 1;
	}
	
	return Py_BuildValue( "h", is_posemode );
}

static PyObject *M_Window_ViewLayers( PyObject * self, PyObject * args )
{
	PyObject *item = NULL;
	PyObject *list = NULL, *resl = NULL;
	int val, i, bit = 0, layer = 0, len_list;
	short winid = -1;

	if( !G.scene ) {
		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
			"can't get pointer to global scene" );
	}

	/* Pase Args, Nothing, One list, Or a list and an int */
	if (PyTuple_GET_SIZE(args)!=0) {
		if( !PyArg_ParseTuple ( args, "O!|h", &PyList_Type, &list, &winid) ) {
			return EXPP_ReturnPyObjError( PyExc_TypeError,
					"nothing or a list and optionaly a window ID argument" );	
		}
	}
	
	if( list ) {
		len_list = PyList_Size(list);

		if (len_list == 0)
			return EXPP_ReturnPyObjError( PyExc_AttributeError,
		  	"list can't be empty, at list one layer must be set" );

		for( i = 0; i < len_list; i++ ) {
			item = PyList_GetItem( list, i );
			if( !PyInt_Check( item ) )
				return EXPP_ReturnPyObjError
					( PyExc_AttributeError,
					  "list must contain only integer numbers" );

			val = ( int ) PyInt_AsLong( item );
			if( val < 1 || val > 20 )
				return EXPP_ReturnPyObjError
					( PyExc_AttributeError,
					  "layer values must be in the range [1, 20]" );

			layer |= 1 << ( val - 1 );
		}
		
		if (winid==-1) {
			/* set scene and viewport */
			G.scene->lay = layer;
			if (G.vd) {
				G.vd->lay = layer;
	
				while( bit < 20 ) {
					val = 1 << bit;
					if( layer & val ) {
						G.vd->layact = val;
						break;
					}
					bit++;
				}
			}
		} else {
			/* only set the windows layer */
			ScrArea *sa;
			SpaceLink *sl;
			View3D *vd;
			 
			if (G.curscreen) { /* can never be too careful */
	            for (sa=G.curscreen->areabase.first; sa; sa= sa->next) {
	            	if (winid == sa->win) {
	            		
	            		for (sl= sa->spacedata.first; sl; sl= sl->next)
	            			if(sl->spacetype==SPACE_VIEW3D)
	            				break;
	            		
	            		if (!sl)
	            			return EXPP_ReturnPyObjError( PyExc_ValueError,
	            				"The window matching the winid has no 3d viewport" );
	            		
	            		vd= (View3D *) sl;
	            		vd->lay = layer;
	            		
	            		for(bit= 0; bit < 20; bit++) {
	            			val = 1 << bit;
	            			if( layer & val ) {
	            				vd->layact = val;
	            				 break;
	            			}
	            		}
	            		
	            		winid = -1; /* so we know its done */
	            		break;
	            	}
	            }
				if (winid!=-1)
					return EXPP_ReturnPyObjError( PyExc_TypeError,
							"The winid argument did not match any window" );
			}
		}
	}

	resl = PyList_New( 0 );
	if( !resl )
		return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
						"couldn't create pylist!" ) );

	layer = G.scene->lay;

	bit = 0;
	while( bit < 20 ) {
		val = 1 << bit;
		if( layer & val ) {
			item = Py_BuildValue( "i", bit + 1 );
			PyList_Append( resl, item );
			Py_DECREF( item );
		}
		bit++;
	}

	return resl;
}

static PyObject *M_Window_CameraView( PyObject * self, PyObject * args )
{
	short camtov3d = 0;

	if( !PyArg_ParseTuple( args, "|i", &camtov3d ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected an int (from Window.Views) as argument" );

	if( !G.vd )
		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
			"this function can only be used after a 3d View has been initialized" );

	if( !G.vd->camera ) {
		if( BASACT && OBACT->type == OB_CAMERA )
			G.vd->camera = OBACT;
		else
			G.vd->camera = scene_find_camera( G.scene );
		handle_view3d_lock(  );
	}

	G.vd->persp = 2;
	G.vd->view = 0;

	if( camtov3d )
		setcameratoview3d(  );

	Py_RETURN_NONE;
}

static PyObject *M_Window_QTest( PyObject * self )
{
	return Py_BuildValue( "h", qtest(  ) );
}

static PyObject *M_Window_QRead( PyObject * self )
{
	short val = 0;
	unsigned short event;

	if (G.background)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"QRead is not available in background mode");

	event = extern_qread( &val );

	return Py_BuildValue( "ii", event, val );
}

static PyObject *M_Window_QAdd( PyObject * self, PyObject * args )
{
	short win;
	short evt;		/* unsigned, we check below */
	short val;
	short after = 0;

	if (G.background)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"QAdd is not available in background mode");

	if( !PyArg_ParseTuple( args, "hhh|h", &win, &evt, &val, &after ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected three or four ints as arguments" );

	if( evt < 0 )		/* evt is unsigned short */
		return EXPP_ReturnPyObjError( PyExc_AttributeError,
					      "event value must be a positive int, check events in Blender.Draw" );

	if( after )
		addafterqueue( win, evt, val );
	else
		addqueue( win, evt, val );

	Py_RETURN_NONE;
}

static PyObject *M_Window_QHandle( PyObject * self, PyObject * args )
{
	short win;
	ScrArea *sa;
	ScrArea *oldsa = NULL;

	if (G.background)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"QHandle is not available in background mode");

	if (!G.curscreen)
		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
			"No screens available");
	
	if( !PyArg_ParseTuple( args, "h", &win ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected an int as argument" );
	
	for (sa= G.curscreen->areabase.first; sa; sa= sa->next)
		if( sa->win == win )
			break;

	if( sa ) {
		BWinEvent evt;
		short do_redraw = 0, do_change = 0;

		if( sa != curarea || sa->win != mywinget(  ) ) {
			oldsa = curarea;
			areawinset( sa->win );
			set_g_activearea( sa );
		}
		while( bwin_qread( sa->win, &evt ) ) {
			if( evt.event == REDRAW ) {
				do_redraw = 1;
			} else if( evt.event == CHANGED ) {
				sa->win_swap = 0;
				do_change = 1;
				do_redraw = 1;
			} else {
				scrarea_do_winhandle( sa, &evt );
			}
		}
	}

	if( oldsa ) {
		areawinset( oldsa->win );
		set_g_activearea( oldsa );
	}

	Py_RETURN_NONE;
}

static PyObject *M_Window_TestBreak( PyObject * self )
{
	if (blender_test_break()) {
		Py_RETURN_TRUE;
	} else {
		Py_RETURN_FALSE;
	}
}
		
static PyObject *M_Window_GetMouseCoords( PyObject * self )
{
	short mval[2];

	getmouse( mval );

	return Py_BuildValue( "hh", mval[0], mval[1] );
}

static PyObject *M_Window_SetMouseCoords( PyObject * self, PyObject * args )
{
	int ok, x, y;

	if( !G.curscreen )
		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
					      "no current screen to retrieve info from!" );

	x = G.curscreen->sizex / 2;
	y = G.curscreen->sizey / 2;

	if( PyObject_Length( args ) == 2 )
		ok = PyArg_ParseTuple( args, "hh", &x, &y );
	else
		ok = PyArg_ParseTuple( args, "|(hh)", &x, &y );

	if( !ok )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected [i, i] or i,i as arguments (or nothing)." );

	warp_pointer( x, y );

	Py_RETURN_NONE;
}

static PyObject *M_Window_GetMouseButtons( PyObject * self )
{
	short mbut = get_mbut(  );

	return Py_BuildValue( "h", mbut );
}

static PyObject *M_Window_GetKeyQualifiers( PyObject * self )
{
	short qual = get_qual(  );

	return Py_BuildValue( "h", qual );
}

static PyObject *M_Window_SetKeyQualifiers( PyObject * self, PyObject * args )
{
	short qual = 0;

	if( !PyArg_ParseTuple( args, "|h", &qual ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected nothing or an int (or'ed flags) as argument" );

	if( qual < 0 )
		return EXPP_ReturnPyObjError( PyExc_AttributeError,
					      "value must be a positive int, check Blender.Window.Qual" );

	G.qual = qual;

	return Py_BuildValue( "h", qual );
}

static PyObject *M_Window_GetAreaSize( PyObject * self )
{
	ScrArea *sa = curarea;

	if( !sa )
		Py_RETURN_NONE;

	return Py_BuildValue( "hh", sa->winx, sa->winy );
}

static PyObject *M_Window_GetAreaID( PyObject * self )
{
	ScrArea *sa = curarea;

	if( !sa )
		Py_RETURN_NONE;

	return Py_BuildValue( "h", sa->win );
}

static PyObject *M_Window_GetScreenSize( PyObject * self )
{
	bScreen *scr = G.curscreen;

	if( !scr )
		Py_RETURN_NONE;

	return Py_BuildValue( "hh", scr->sizex, scr->sizey );
}


static PyObject *M_Window_SetScreen( PyObject * self, PyObject * value )
{
	bScreen *scr = G.main->screen.first;
	char *name = PyString_AsString(value);

	if( !name )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected string as argument" );

	while( scr ) {
		if( !strcmp( scr->id.name + 2, name ) ) {
			setscreen( scr );
			break;
		}
		scr = scr->id.next;
	}

	if( !scr )
		return EXPP_ReturnPyObjError( PyExc_AttributeError,
					      "no such screen, check Window.GetScreens() for valid names" );

	Py_RETURN_NONE;
}

static PyObject *M_Window_GetScreens( PyObject * self )
{
	bScreen *scr = G.main->screen.first;
	PyObject *list = PyList_New( 0 );
	PyObject *str = NULL;

	if( !list )
		return EXPP_ReturnPyObjError( PyExc_MemoryError,
					      "couldn't create py list!" );

	while( scr ) {
		str = PyString_FromString( scr->id.name + 2 );

		if( !str ) {
			Py_DECREF( list );
			return EXPP_ReturnPyObjError( PyExc_MemoryError,
						      "couldn't create py string!" );
		}

		PyList_Append( list, str );	/* incref's str */
		Py_DECREF( str );

		scr = scr->id.next;
	}

	return list;
}

static PyObject *M_Window_GetScreenInfo( PyObject * self, PyObject * args,
					 PyObject * kwords )
{
	ScrArea *sa = G.curscreen->areabase.first;
	bScreen *scr = G.main->screen.first;
	PyObject *item, *list;
	rcti *rct;
	int type = -1;
	char *rect = "win";
	char *screen = "";
	static char *kwlist[] = { "type", "rect", "screen", NULL };
	int rctype = 0;

	if( !PyArg_ParseTupleAndKeywords( args, kwords, "|iss", kwlist, &type,
					  &rect, &screen ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
					      "expected nothing or an int and two strings as arguments" );

	if( !strcmp( rect, "win" ) )
		rctype = 0;
	else if( !strcmp( rect, "total" ) )
		rctype = 1;
	else if( !strcmp( rect, "header" ) )
		rctype = 2;
	else
		return EXPP_ReturnPyObjError( PyExc_AttributeError,
					      "requested invalid type for area rectangle coordinates." );

	list = PyList_New( 0 );

	if( screen && screen[0] != '\0' ) {
		while( scr ) {
			if( !strcmp( scr->id.name + 2, screen ) ) {
				sa = scr->areabase.first;
				break;
			}
			scr = scr->id.next;
		}
	}

	if( !scr ) {
		Py_DECREF( list );
		return EXPP_ReturnPyObjError( PyExc_AttributeError,
					      "no such screen, see existing ones with Window.GetScreens." );
	}

	while( sa ) {
		if( type != -1 && sa->spacetype != type ) {
			sa = sa->next;
			continue;
		}

		switch ( rctype ) {
		case 0:
			rct = &sa->winrct;
			break;
		case 1:
			rct = &sa->totrct;
			break;
		case 2:
		default:
			rct = &sa->headrct;
		}

		item = Py_BuildValue( "{s:[h,h,h,h],s:h,s:h}",
				      "vertices", rct->xmin, rct->ymin,
				      rct->xmax, rct->ymax, "type",
				      ( short ) sa->spacetype, "id",
				      ( short ) sa->win );
		PyList_Append( list, item );
		Py_DECREF( item );

		sa = sa->next;
	}

	return list;
}

static PyObject *M_Window_GetPivot( PyObject * self )
{
	if (G.vd) {
		return PyInt_FromLong( G.vd->around );
	}
	Py_RETURN_NONE;
}

static PyObject *M_Window_SetPivot( PyObject * self, PyObject * value)

{
	short pivot;
	if (G.vd) {
		pivot = (short)PyInt_AsLong( value );

		if ( pivot > 4 || pivot < 0 )
			return EXPP_ReturnPyObjError( PyExc_AttributeError,
							  "Expected a constant from Window.PivotTypes" );
		
		G.vd->around = pivot;
	}
	Py_RETURN_NONE;
}	


/*****************************************************************************/
/* Function:	Window_Init						*/
/*****************************************************************************/
PyObject *Window_Init( void )
{
	PyObject *submodule, *Types, *Qual, *MButs, *PivotTypes, *dict;

	submodule =
		Py_InitModule3( "Blender.Window", M_Window_methods,
				M_Window_doc );

	dict = PyModule_GetDict( submodule );
	if( dict )
		PyDict_SetItemString( dict, "Theme", Theme_Init(  ) );

	Types = PyConstant_New(  );
	Qual = PyConstant_New(  );
	MButs = PyConstant_New(  );
	PivotTypes = PyConstant_New(  );

	if( Types ) {
		BPy_constant *d = ( BPy_constant * ) Types;

		PyConstant_Insert( d, "VIEW3D", PyInt_FromLong( SPACE_VIEW3D ) );
		PyConstant_Insert( d, "IPO", PyInt_FromLong( SPACE_IPO ) );
		PyConstant_Insert( d, "OOPS", PyInt_FromLong( SPACE_OOPS ) );
		PyConstant_Insert( d, "BUTS", PyInt_FromLong( SPACE_BUTS ) );
		PyConstant_Insert( d, "FILE", PyInt_FromLong( SPACE_FILE ) );
		PyConstant_Insert( d, "IMAGE", PyInt_FromLong( SPACE_IMAGE ) );
		PyConstant_Insert( d, "INFO", PyInt_FromLong( SPACE_INFO ) );
		PyConstant_Insert( d, "SEQ", PyInt_FromLong( SPACE_SEQ ) );
		PyConstant_Insert( d, "IMASEL", PyInt_FromLong( SPACE_IMASEL ) );
		PyConstant_Insert( d, "SOUND", PyInt_FromLong( SPACE_SOUND ) );
		PyConstant_Insert( d, "ACTION", PyInt_FromLong( SPACE_ACTION ) );
		PyConstant_Insert( d, "TEXT", PyInt_FromLong( SPACE_TEXT ) );
		PyConstant_Insert( d, "NLA", PyInt_FromLong( SPACE_NLA ) );
		PyConstant_Insert( d, "SCRIPT", PyInt_FromLong( SPACE_SCRIPT ) );
		PyConstant_Insert( d, "TIME", PyInt_FromLong( SPACE_TIME ) );
		PyConstant_Insert( d, "NODE", PyInt_FromLong( SPACE_NODE ) );

		PyModule_AddObject( submodule, "Types", Types );
	}

	if( Qual ) {
		BPy_constant *d = ( BPy_constant * ) Qual;

		PyConstant_Insert( d, "LALT", PyInt_FromLong( L_ALTKEY ) );
		PyConstant_Insert( d, "RALT", PyInt_FromLong( R_ALTKEY ) );
		PyConstant_Insert( d, "ALT", PyInt_FromLong( LR_ALTKEY ) );
		PyConstant_Insert( d, "LCTRL", PyInt_FromLong( L_CTRLKEY ) );
		PyConstant_Insert( d, "RCTRL", PyInt_FromLong( R_CTRLKEY ) );
		PyConstant_Insert( d, "CTRL", PyInt_FromLong( LR_CTRLKEY ) );
		PyConstant_Insert( d, "LSHIFT", PyInt_FromLong( L_SHIFTKEY ) );
		PyConstant_Insert( d, "RSHIFT", PyInt_FromLong( R_SHIFTKEY ) );
		PyConstant_Insert( d, "SHIFT", PyInt_FromLong( LR_SHIFTKEY ) );

		PyModule_AddObject( submodule, "Qual", Qual );
	}

	if( MButs ) {
		BPy_constant *d = ( BPy_constant * ) MButs;

		PyConstant_Insert( d, "L", PyInt_FromLong( L_MOUSE ) );
		PyConstant_Insert( d, "M", PyInt_FromLong( M_MOUSE ) );
		PyConstant_Insert( d, "R", PyInt_FromLong( R_MOUSE ) );

		PyModule_AddObject( submodule, "MButs", MButs );
	}

	if( PivotTypes ) {
		BPy_constant *d = ( BPy_constant * ) PivotTypes;

		PyConstant_Insert(d, "BOUNDBOX", PyInt_FromLong( PIVOT_BOUNDBOX ) );
		PyConstant_Insert(d, "CURSOR", PyInt_FromLong( PIVOT_CURSOR ) );
		PyConstant_Insert(d, "MEDIAN", PyInt_FromLong( PIVOT_MEDIAN ) );
		PyConstant_Insert(d, "ACTIVE", PyInt_FromLong( PIVOT_ACTIVE ) );
		PyConstant_Insert(d, "INDIVIDUAL", PyInt_FromLong( PIVOT_INDIVIDUAL ) );

		PyModule_AddObject( submodule, "PivotTypes", PivotTypes );
	}
	return submodule;
}