37cd424ac18c6c42a3cbf66f4d3bc93ae27c4506
[blender.git] / source / blender / windowmanager / intern / wm_operators.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <float.h>
30 #define _USE_MATH_DEFINES
31 #include <math.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35
36 #include "DNA_ID.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
40 #include "DNA_windowmanager_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "PIL_time.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_dynstr.h" /*for WM_operator_pystring */
48
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_idprop.h"
52 #include "BKE_library.h"
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55 #include "BKE_scene.h"
56 #include "BKE_utildefines.h"
57
58 #include "BIF_gl.h"
59 #include "BIF_glutil.h" /* for paint cursor */
60
61 #include "IMB_imbuf_types.h"
62
63 #include "ED_screen.h"
64 #include "ED_util.h"
65
66 #include "RNA_access.h"
67 #include "RNA_define.h"
68
69 #include "UI_interface.h"
70 #include "UI_resources.h"
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74
75 #include "wm.h"
76 #include "wm_draw.h"
77 #include "wm_event_system.h"
78 #include "wm_subwindow.h"
79 #include "wm_window.h"
80
81
82
83 static ListBase global_ops= {NULL, NULL};
84
85 /* ************ operator API, exported ********** */
86
87
88 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
89 {
90         wmOperatorType *ot;
91         
92         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
93         WM_operator_bl_idname(idname_bl, idname);
94         
95         if (idname_bl[0]) {
96                 for(ot= global_ops.first; ot; ot= ot->next) {
97                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
98                            return ot;
99                 }
100         }
101         
102         if(!quiet)
103                 printf("search for unknown operator %s, %s\n", idname_bl, idname);
104         
105         return NULL;
106 }
107
108 wmOperatorType *WM_operatortype_exists(const char *idname)
109 {
110         wmOperatorType *ot;
111         
112         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
113         WM_operator_bl_idname(idname_bl, idname);
114         
115         if(idname_bl[0]) {
116                 for(ot= global_ops.first; ot; ot= ot->next) {
117                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
118                            return ot;
119                 }
120         }
121         return NULL;
122 }
123
124 wmOperatorType *WM_operatortype_first(void)
125 {
126         return global_ops.first;
127 }
128
129 /* all ops in 1 list (for time being... needs evaluation later) */
130 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
131 {
132         wmOperatorType *ot;
133         
134         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
135         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
136         opfunc(ot);
137
138         if(ot->name==NULL) {
139                 static char dummy_name[] = "Dummy Name";
140                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
141                 ot->name= dummy_name;
142         }
143
144         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)"); // XXX All ops should have a description but for now allow them not to.
145         RNA_def_struct_identifier(ot->srna, ot->idname);
146         BLI_addtail(&global_ops, ot);
147 }
148
149 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
150 {
151         wmOperatorType *ot;
152
153         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
154         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
155         opfunc(ot, userdata);
156         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
157         RNA_def_struct_identifier(ot->srna, ot->idname);
158         BLI_addtail(&global_ops, ot);
159 }
160
161 /* ********************* macro operator ******************** */
162
163 /* macro exec only runs exec calls */
164 static int wm_macro_exec(bContext *C, wmOperator *op)
165 {
166         wmOperator *opm;
167         int retval= OPERATOR_FINISHED;
168         
169 //      printf("macro exec %s\n", op->type->idname);
170         
171         for(opm= op->macro.first; opm; opm= opm->next) {
172                 
173                 if(opm->type->exec) {
174 //                      printf("macro exec %s\n", opm->type->idname);
175                         retval= opm->type->exec(C, opm);
176                 
177                         if(!(retval & OPERATOR_FINISHED))
178                                 break;
179                 }
180         }
181 //      if(opm)
182 //              printf("macro ended not finished\n");
183 //      else
184 //              printf("macro end\n");
185         
186         return retval;
187 }
188
189 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
190 {
191         wmOperator *opm;
192         int retval= OPERATOR_FINISHED;
193         
194 //      printf("macro invoke %s\n", op->type->idname);
195         
196         for(opm= op->macro.first; opm; opm= opm->next) {
197                 
198                 if(opm->type->invoke)
199                         retval= opm->type->invoke(C, opm, event);
200                 else if(opm->type->exec)
201                         retval= opm->type->exec(C, opm);
202                 
203                 if(!(retval & OPERATOR_FINISHED))
204                         break;
205         }
206         
207 //      if(opm)
208 //              printf("macro ended not finished\n");
209 //      else
210 //              printf("macro end\n");
211         
212         
213         return retval;
214 }
215
216 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
217 {
218 //      printf("macro modal %s\n", op->type->idname);
219         
220         if(op->opm==NULL)
221                 printf("macro error, calling NULL modal()\n");
222         else {
223 //              printf("macro modal %s\n", op->opm->type->idname);
224                 return op->opm->type->modal(C, op->opm, event);
225         }       
226         
227         return OPERATOR_FINISHED;
228 }
229
230 /* Names have to be static for now */
231 wmOperatorType *WM_operatortype_append_macro(char *idname, char *name, int flag)
232 {
233         wmOperatorType *ot;
234         
235         if(WM_operatortype_exists(idname)) {
236                 printf("Macro error: operator %s exists\n", idname);
237                 return NULL;
238         }
239         
240         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
241         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
242         
243         ot->idname= idname;
244         ot->name= name;
245         ot->flag= OPTYPE_MACRO | flag;
246         
247         ot->exec= wm_macro_exec;
248         ot->invoke= wm_macro_invoke;
249         ot->modal= wm_macro_modal;
250         ot->poll= NULL;
251         
252         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)"); // XXX All ops should have a description but for now allow them not to.
253         RNA_def_struct_identifier(ot->srna, ot->idname);
254
255         BLI_addtail(&global_ops, ot);
256
257         return ot;
258 }
259
260 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
261 {
262         wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
263         
264         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
265
266         /* do this on first use, since operatordefinitions might have been not done yet */
267 //      otmacro->ptr= MEM_callocN(sizeof(PointerRNA), "optype macro ItemPtr");
268 //      WM_operator_properties_create(otmacro->ptr, idname);
269         
270         BLI_addtail(&ot->macro, otmacro);
271         
272         return otmacro;
273 }
274
275 static void wm_operatortype_free_macro(wmOperatorType *ot)
276 {
277         wmOperatorTypeMacro *otmacro;
278         
279         for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
280                 if(otmacro->ptr) {
281                         WM_operator_properties_free(otmacro->ptr);
282                         MEM_freeN(otmacro->ptr);
283                 }
284         }
285         BLI_freelistN(&ot->macro);
286 }
287
288
289 int WM_operatortype_remove(const char *idname)
290 {
291         wmOperatorType *ot = WM_operatortype_find(idname, 0);
292
293         if (ot==NULL)
294                 return 0;
295         
296         BLI_remlink(&global_ops, ot);
297         RNA_struct_free(&BLENDER_RNA, ot->srna);
298         
299         if(ot->macro.first)
300                 wm_operatortype_free_macro(ot);
301         
302         MEM_freeN(ot);
303
304         return 1;
305 }
306
307 /* SOME_OT_op -> some.op */
308 void WM_operator_py_idname(char *to, const char *from)
309 {
310         char *sep= strstr(from, "_OT_");
311         if(sep) {
312                 int i, ofs= (sep-from);
313
314                 for(i=0; i<ofs; i++)
315                         to[i]= tolower(from[i]);
316
317                 to[ofs] = '.';
318                 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
319         }
320         else {
321                 /* should not happen but support just incase */
322                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
323         }
324 }
325
326 /* some.op -> SOME_OT_op */
327 void WM_operator_bl_idname(char *to, const char *from)
328 {
329         if (from) {
330                 char *sep= strchr(from, '.');
331
332                 if(sep) {
333                         int i, ofs= (sep-from);
334
335                         for(i=0; i<ofs; i++)
336                                 to[i]= toupper(from[i]);
337
338                         BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
339                         BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
340                 }
341                 else {
342                         /* should not happen but support just incase */
343                         BLI_strncpy(to, from, OP_MAX_TYPENAME);
344                 }
345         }
346         else
347                 to[0]= 0;
348 }
349
350 /* print a string representation of the operator, with the args that it runs 
351  * so python can run it again,
352  *
353  * When calling from an existing wmOperator do.
354  * WM_operator_pystring(op->type, op->ptr);
355  */
356 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
357 {
358         const char *arg_name= NULL;
359         char idname_py[OP_MAX_TYPENAME];
360
361         PropertyRNA *prop, *iterprop;
362
363         /* for building the string */
364         DynStr *dynstr= BLI_dynstr_new();
365         char *cstring, *buf;
366         int first_iter=1, ok= 1;
367
368
369         /* only to get the orginal props for comparisons */
370         PointerRNA opptr_default;
371         PropertyRNA *prop_default;
372         char *buf_default;
373         if(!all_args) {
374                 WM_operator_properties_create(&opptr_default, ot->idname);
375         }
376
377
378         WM_operator_py_idname(idname_py, ot->idname);
379         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
380
381         iterprop= RNA_struct_iterator_property(opptr->type);
382
383         RNA_PROP_BEGIN(opptr, propptr, iterprop) {
384                 prop= propptr.data;
385                 arg_name= RNA_property_identifier(prop);
386
387                 if (strcmp(arg_name, "rna_type")==0) continue;
388
389                 buf= RNA_property_as_string(C, opptr, prop);
390                 
391                 ok= 1;
392
393                 if(!all_args) {
394                         /* not verbose, so only add in attributes that use non-default values
395                          * slow but good for tooltips */
396                         prop_default= RNA_struct_find_property(&opptr_default, arg_name);
397
398                         if(prop_default) {
399                                 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
400
401                                 if(strcmp(buf, buf_default)==0)
402                                         ok= 0; /* values match, dont bother printing */
403
404                                 MEM_freeN(buf_default);
405                         }
406
407                 }
408                 if(ok) {
409                         BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
410                         first_iter = 0;
411                 }
412
413                 MEM_freeN(buf);
414
415         }
416         RNA_PROP_END;
417
418         if(all_args==0)
419                 WM_operator_properties_free(&opptr_default);
420
421         BLI_dynstr_append(dynstr, ")");
422
423         cstring = BLI_dynstr_get_cstring(dynstr);
424         BLI_dynstr_free(dynstr);
425         return cstring;
426 }
427
428 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
429 {
430         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
431
432         if(ot)
433                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
434         else
435                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
436 }
437
438 void WM_operator_properties_free(PointerRNA *ptr)
439 {
440         IDProperty *properties= ptr->data;
441
442         if(properties) {
443                 IDP_FreeProperty(properties);
444                 MEM_freeN(properties);
445         }
446 }
447
448 /* ************ default op callbacks, exported *********** */
449
450 /* invoke callback, uses enum property named "type" */
451 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
452 {
453         PropertyRNA *prop;
454         uiPopupMenu *pup;
455         uiLayout *layout;
456
457         prop= RNA_struct_find_property(op->ptr, "type");
458
459         if(!prop) {
460                 RNA_STRUCT_BEGIN(op->ptr, findprop) {
461                         if(RNA_property_type(findprop) == PROP_ENUM) {
462                                 prop= findprop;
463                                 break;
464                         }
465                 }
466                 RNA_STRUCT_END;
467         }
468
469         if(prop==NULL) {
470                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
471         }
472         else if (RNA_property_type(prop) != PROP_ENUM) {
473                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
474         }
475         else {
476                 pup= uiPupMenuBegin(C, op->type->name, 0);
477                 layout= uiPupMenuLayout(pup);
478                 uiItemsEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop));
479                 uiPupMenuEnd(C, pup);
480         }
481
482         return OPERATOR_CANCELLED;
483 }
484
485 /* op->invoke */
486 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
487 {
488         uiPopupMenu *pup;
489         uiLayout *layout;
490
491         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
492         layout= uiPupMenuLayout(pup);
493         uiItemO(layout, NULL, 0, op->type->idname);
494         uiPupMenuEnd(C, pup);
495         
496         return OPERATOR_CANCELLED;
497 }
498
499 /* op->invoke, opens fileselect if filename property not set, otherwise executes */
500 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
501 {
502         if (RNA_property_is_set(op->ptr, "filename")) {
503                 return WM_operator_call(C, op);
504         } 
505         else {
506                 WM_event_add_fileselect(C, op);
507                 return OPERATOR_RUNNING_MODAL;
508         }
509 }
510
511 /* default properties for fileselect */
512 void WM_operator_properties_filesel(wmOperatorType *ot, int filter)
513 {
514         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "Path to file.");
515
516         RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
517         RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
518         RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
519         RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
520         RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
521         RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
522         RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
523         RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
524 }
525
526 /* op->poll */
527 int WM_operator_winactive(bContext *C)
528 {
529         if(CTX_wm_window(C)==NULL) return 0;
530         return 1;
531 }
532
533 /* op->invoke */
534 static void redo_cb(bContext *C, void *arg_op, void *arg2)
535 {
536         wmOperator *lastop= arg_op;
537         
538         if(lastop) {
539                 ED_undo_pop_op(C, lastop);
540                 WM_operator_repeat(C, lastop);
541         }
542 }
543
544 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
545 {
546         wmWindowManager *wm= CTX_wm_manager(C);
547         wmOperator *op= arg_op;
548         PointerRNA ptr;
549         uiBlock *block;
550         uiLayout *layout;
551         uiStyle *style= U.uistyles.first;
552         
553         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
554         uiBlockClearFlag(block, UI_BLOCK_LOOP);
555         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
556         uiBlockSetFunc(block, redo_cb, arg_op, NULL);
557
558         if(!op->properties) {
559                 IDPropertyTemplate val = {0};
560                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
561         }
562
563         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
564         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
565         uiItemL(layout, op->type->name, 0);
566
567         if(op->type->ui)
568                 op->type->ui((bContext*)C, &ptr, layout);
569         else
570                 uiDefAutoButsRNA(C, layout, &ptr, 2);
571
572         uiPopupBoundsBlock(block, 4.0f, 0, 0);
573         uiEndBlock(C, block);
574
575         return block;
576 }
577
578 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
579 {
580         int retval= OPERATOR_CANCELLED;
581         
582         if(op->type->exec)
583                 retval= op->type->exec(C, op);
584
585         if(retval != OPERATOR_CANCELLED)
586                 uiPupBlock(C, wm_block_create_redo, op);
587
588         return retval;
589 }
590
591 int WM_operator_redo_popup(bContext *C, wmOperator *op)
592 {
593         uiPupBlock(C, wm_block_create_redo, op);
594
595         return OPERATOR_CANCELLED;
596 }
597
598 /* ***************** Debug menu ************************* */
599
600 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
601 {
602         wmOperator *op= arg_op;
603         uiBlock *block;
604         uiLayout *layout;
605         uiStyle *style= U.uistyles.first;
606         
607         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
608         uiBlockClearFlag(block, UI_BLOCK_LOOP);
609         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
610         
611         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
612         uiItemL(layout, op->type->name, 0);
613
614         if(op->type->ui)
615                 op->type->ui(C, op->ptr, layout);
616         else
617                 uiDefAutoButsRNA(C, layout, op->ptr, 2);
618         
619         uiPopupBoundsBlock(block, 4.0f, 0, 0);
620         uiEndBlock(C, block);
621         
622         return block;
623 }
624
625 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
626 {
627         G.rt= RNA_int_get(op->ptr, "debugval");
628         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
629         WM_event_add_notifier(C, NC_WINDOW, NULL);
630         
631         return OPERATOR_FINISHED;       
632 }
633
634 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
635 {
636         
637         RNA_int_set(op->ptr, "debugval", G.rt);
638         
639         /* pass on operator, so return modal */
640         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
641         
642         return OPERATOR_RUNNING_MODAL;
643 }
644
645 static void WM_OT_debug_menu(wmOperatorType *ot)
646 {
647         ot->name= "Debug Menu";
648         ot->idname= "WM_OT_debug_menu";
649         ot->description= "Open a popup to set the debug level.";
650         
651         ot->invoke= wm_debug_menu_invoke;
652         ot->exec= wm_debug_menu_exec;
653         ot->poll= WM_operator_winactive;
654         
655         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
656 }
657
658 /* ***************** Search menu ************************* */
659 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
660 {
661         wmOperatorType *ot= arg2;
662         
663         if(ot)
664                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
665 }
666
667 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
668 {
669         wmOperatorType *ot = WM_operatortype_first();
670         
671         for(; ot; ot= ot->next) {
672                 
673                 if(BLI_strcasestr(ot->name, str)) {
674                         if(ot->poll==NULL || ot->poll((bContext *)C)) {
675                                 char name[256];
676                                 int len= strlen(ot->name);
677                                 
678                                 /* display name for menu, can hold hotkey */
679                                 BLI_strncpy(name, ot->name, 256);
680                                 
681                                 /* check for hotkey */
682                                 if(len < 256-6) {
683                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
684                                                 name[len]= '|';
685                                 }
686                                 
687                                 if(0==uiSearchItemAdd(items, name, ot, 0))
688                                         break;
689                         }
690                 }
691         }
692 }
693
694 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
695 {
696         static char search[256]= "";
697         wmEvent event;
698         wmWindow *win= CTX_wm_window(C);
699         uiBlock *block;
700         uiBut *but;
701         
702         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
703         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
704         
705         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
706         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
707         
708         /* fake button, it holds space for search items */
709         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
710         
711         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
712         uiEndBlock(C, block);
713         
714         event= *(win->eventstate);      /* XXX huh huh? make api call */
715         event.type= EVT_BUT_OPEN;
716         event.val= KM_PRESS;
717         event.customdata= but;
718         event.customdatafree= FALSE;
719         wm_event_add(win, &event);
720         
721         return block;
722 }
723
724 static int wm_search_menu_exec(bContext *C, wmOperator *op)
725 {
726         
727         return OPERATOR_FINISHED;       
728 }
729
730 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
731 {
732         
733         uiPupBlock(C, wm_block_search_menu, op);
734         
735         return OPERATOR_CANCELLED;
736 }
737
738 /* op->poll */
739 int wm_search_menu_poll(bContext *C)
740 {
741         if(CTX_wm_window(C)==NULL) return 0;
742         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
743         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_TEXT) return 0;  // XXX - so we can use the spacebar in the text editor
744         return 1;
745 }
746
747 static void WM_OT_search_menu(wmOperatorType *ot)
748 {
749         ot->name= "Search Menu";
750         ot->idname= "WM_OT_search_menu";
751         
752         ot->invoke= wm_search_menu_invoke;
753         ot->exec= wm_search_menu_exec;
754         ot->poll= wm_search_menu_poll;
755 }
756
757
758 /* ************ window / screen operator definitions ************** */
759
760 static void WM_OT_window_duplicate(wmOperatorType *ot)
761 {
762         ot->name= "Duplicate Window";
763         ot->idname= "WM_OT_window_duplicate";
764         ot->description="Duplicate the current Blender window.";
765                 
766         ot->exec= wm_window_duplicate_op;
767         ot->poll= WM_operator_winactive;
768 }
769
770 static void WM_OT_save_homefile(wmOperatorType *ot)
771 {
772         ot->name= "Save User Settings";
773         ot->idname= "WM_OT_save_homefile";
774         ot->description="Make the current file the default .blend file.";
775                 
776         ot->invoke= WM_operator_confirm;
777         ot->exec= WM_write_homefile;
778         ot->poll= WM_operator_winactive;
779 }
780
781 static void WM_OT_read_homefile(wmOperatorType *ot)
782 {
783         ot->name= "Reload Start-Up File";
784         ot->idname= "WM_OT_read_homefile";
785         ot->description="Open the default file (doesn't save the current file).";
786         
787         ot->invoke= WM_operator_confirm;
788         ot->exec= WM_read_homefile;
789         ot->poll= WM_operator_winactive;
790         
791         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
792 }
793
794
795 /* ********* recent file *********** */
796
797 static int recentfile_exec(bContext *C, wmOperator *op)
798 {
799         int event= RNA_enum_get(op->ptr, "file");
800
801         // XXX wm in context is not set correctly after WM_read_file -> crash
802         // do it before for now, but is this correct with multiple windows?
803
804         if(event>0) {
805                 if (G.sce[0] && (event==1)) {
806                         WM_event_add_notifier(C, NC_WINDOW, NULL);
807                         WM_read_file(C, G.sce, op->reports);
808                 }
809                 else {
810                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-1);
811                         if(recent) {
812                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
813                                 WM_read_file(C, recent->filename, op->reports);
814                         }
815                 }
816         }
817         return 0;
818 }
819
820 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
821 {
822         uiPopupMenu *pup;
823         uiLayout *layout;
824
825         pup= uiPupMenuBegin(C, "Open Recent", 0);
826         layout= uiPupMenuLayout(pup);
827         uiItemsEnumO(layout, op->type->idname, "file");
828         uiPupMenuEnd(C, pup);
829         
830         return OPERATOR_CANCELLED;
831 }
832
833 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
834 {
835         EnumPropertyItem tmp = {0, "", 0, "", ""};
836         EnumPropertyItem *item= NULL;
837         struct RecentFile *recent;
838         int totitem= 0, i;
839
840         /* dynamically construct enum */
841         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
842                 tmp.value= i+1;
843                 tmp.identifier= recent->filename;
844                 tmp.name= BLI_short_filename(recent->filename);
845                 RNA_enum_item_add(&item, &totitem, &tmp);
846         }
847
848         RNA_enum_item_end(&item, &totitem);
849         *free= 1;
850
851         return item;
852 }
853
854 static void WM_OT_open_recentfile(wmOperatorType *ot)
855 {
856         PropertyRNA *prop;
857         static EnumPropertyItem file_items[]= {
858                 {0, NULL, 0, NULL, NULL}};
859
860         ot->name= "Open Recent File";
861         ot->idname= "WM_OT_open_recentfile";
862         ot->description="Open recent files list.";
863         
864         ot->invoke= wm_recentfile_invoke;
865         ot->exec= recentfile_exec;
866         ot->poll= WM_operator_winactive;
867         
868         prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
869         RNA_def_enum_funcs(prop, open_recentfile_itemf);
870 }
871
872 /* ********* main file *********** */
873
874 static void untitled(char *name)
875 {
876         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
877                 char *c= BLI_last_slash(name);
878                 
879                 if (c)
880                         strcpy(&c[1], "untitled.blend");
881                 else
882                         strcpy(name, "untitled.blend");
883         }
884 }
885
886 static void load_set_load_ui(wmOperator *op)
887 {
888         if(!RNA_property_is_set(op->ptr, "load_ui"))
889                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
890 }
891
892 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
893 {
894         RNA_string_set(op->ptr, "filename", G.sce);
895         load_set_load_ui(op);
896
897         WM_event_add_fileselect(C, op);
898
899         return OPERATOR_RUNNING_MODAL;
900 }
901
902 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
903 {
904         char filename[FILE_MAX];
905
906         RNA_string_get(op->ptr, "filename", filename);
907         load_set_load_ui(op);
908
909         if(RNA_boolean_get(op->ptr, "load_ui"))
910                 G.fileflags &= ~G_FILE_NO_UI;
911         else
912                 G.fileflags |= G_FILE_NO_UI;
913         
914         // XXX wm in context is not set correctly after WM_read_file -> crash
915         // do it before for now, but is this correct with multiple windows?
916         WM_event_add_notifier(C, NC_WINDOW, NULL);
917
918         WM_read_file(C, filename, op->reports);
919         
920         return 0;
921 }
922
923 static void WM_OT_open_mainfile(wmOperatorType *ot)
924 {
925         ot->name= "Open Blender File";
926         ot->idname= "WM_OT_open_mainfile";
927         ot->description="Open a Blender file.";
928         
929         ot->invoke= wm_open_mainfile_invoke;
930         ot->exec= wm_open_mainfile_exec;
931         ot->poll= WM_operator_winactive;
932         
933         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
934
935         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file.");
936 }
937
938 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
939 {
940         char scestr[FILE_MAX], filename[FILE_MAX];
941         int save_over;
942
943         /* back up some values */
944         BLI_strncpy(scestr, G.sce, sizeof(scestr));
945         save_over = G.save_over;
946
947         // XXX wm in context is not set correctly after WM_read_file -> crash
948         // do it before for now, but is this correct with multiple windows?
949         WM_event_add_notifier(C, NC_WINDOW, NULL);
950
951         /* load file */
952         BLI_make_file_string("/", filename, btempdir, "quit.blend");
953         WM_read_file(C, filename, op->reports);
954
955         /* restore */
956         G.save_over = save_over;
957         BLI_strncpy(G.sce, scestr, sizeof(G.sce));
958
959         return 0;
960 }
961
962 static void WM_OT_recover_last_session(wmOperatorType *ot)
963 {
964         ot->name= "Recover Last Session";
965         ot->idname= "WM_OT_recover_last_session";
966         ot->description="Open the last closed file (\"quit.blend\").";
967         
968         ot->exec= wm_recover_last_session_exec;
969         ot->poll= WM_operator_winactive;
970 }
971
972 static void save_set_compress(wmOperator *op)
973 {
974         if(!RNA_property_is_set(op->ptr, "compress")) {
975                 if(G.save_over) /* keep flag for existing file */
976                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
977                 else /* use userdef for new file */
978                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
979         }
980 }
981
982 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
983 {
984         char name[FILE_MAX];
985
986         save_set_compress(op);
987         
988         BLI_strncpy(name, G.sce, FILE_MAX);
989         untitled(name);
990         RNA_string_set(op->ptr, "filename", name);
991         
992         WM_event_add_fileselect(C, op);
993
994         return OPERATOR_RUNNING_MODAL;
995 }
996
997 /* function used for WM_OT_save_mainfile too */
998 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
999 {
1000         char filename[FILE_MAX];
1001         int compress;
1002
1003         save_set_compress(op);
1004         compress= RNA_boolean_get(op->ptr, "compress");
1005         
1006         if(RNA_property_is_set(op->ptr, "filename"))
1007                 RNA_string_get(op->ptr, "filename", filename);
1008         else {
1009                 BLI_strncpy(filename, G.sce, FILE_MAX);
1010                 untitled(filename);
1011         }
1012
1013         WM_write_file(C, filename, compress, op->reports);
1014         
1015         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1016
1017         return 0;
1018 }
1019
1020 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1021 {
1022         ot->name= "Save As Blender File";
1023         ot->idname= "WM_OT_save_as_mainfile";
1024         ot->description="Save the current file in the desired location.";
1025         
1026         ot->invoke= wm_save_as_mainfile_invoke;
1027         ot->exec= wm_save_as_mainfile_exec;
1028         ot->poll= WM_operator_winactive;
1029         
1030         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
1031         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1032 }
1033
1034 /* *************** Save file directly ******** */
1035
1036 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1037 {
1038         char name[FILE_MAX];
1039
1040         save_set_compress(op);
1041         
1042         BLI_strncpy(name, G.sce, FILE_MAX);
1043         untitled(name);
1044         RNA_string_set(op->ptr, "filename", name);
1045         uiPupMenuSaveOver(C, op, name);
1046
1047         return OPERATOR_RUNNING_MODAL;
1048 }
1049
1050 static void WM_OT_save_mainfile(wmOperatorType *ot)
1051 {
1052         ot->name= "Save Blender File";
1053         ot->idname= "WM_OT_save_mainfile";
1054         ot->description="Save the current Blender file.";
1055         
1056         ot->invoke= wm_save_mainfile_invoke;
1057         ot->exec= wm_save_as_mainfile_exec;
1058         ot->poll= WM_operator_winactive;
1059         
1060         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
1061         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1062 }
1063
1064
1065 /* *********************** */
1066
1067 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1068 {
1069         ot->name= "Toggle Fullscreen";
1070         ot->idname= "WM_OT_window_fullscreen_toggle";
1071         ot->description="Toggle the current window fullscreen.";
1072
1073         ot->exec= wm_window_fullscreen_toggle_op;
1074         ot->poll= WM_operator_winactive;
1075 }
1076
1077 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1078 {
1079         WM_operator_free(op);
1080         
1081         WM_exit(C);     
1082         
1083         return OPERATOR_FINISHED;
1084 }
1085
1086 static void WM_OT_exit_blender(wmOperatorType *ot)
1087 {
1088         ot->name= "Exit Blender";
1089         ot->idname= "WM_OT_exit_blender";
1090         ot->description= "Quit Blender.";
1091
1092         ot->invoke= WM_operator_confirm;
1093         ot->exec= wm_exit_blender_op;
1094         ot->poll= WM_operator_winactive;
1095 }
1096
1097 /* ************ default paint cursors, draw always around cursor *********** */
1098 /*
1099  - returns handler to free 
1100  - poll(bContext): returns 1 if draw should happen
1101  - draw(bContext): drawing callback for paint cursor
1102 */
1103
1104 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1105                                wmPaintCursorDraw draw, void *customdata)
1106 {
1107         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1108         
1109         BLI_addtail(&wm->paintcursors, pc);
1110         
1111         pc->customdata = customdata;
1112         pc->poll= poll;
1113         pc->draw= draw;
1114         
1115         return pc;
1116 }
1117
1118 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1119 {
1120         wmPaintCursor *pc;
1121         
1122         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1123                 if(pc == (wmPaintCursor *)handle) {
1124                         BLI_remlink(&wm->paintcursors, pc);
1125                         MEM_freeN(pc);
1126                         return;
1127                 }
1128         }
1129 }
1130
1131 /* ************ window gesture operator-callback definitions ************** */
1132 /*
1133  * These are default callbacks for use in operators requiring gesture input
1134  */
1135
1136 /* **************** Border gesture *************** */
1137
1138 /* Border gesture has two types:
1139    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1140    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1141
1142    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1143 */
1144
1145 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1146 {
1147         wmGesture *gesture= op->customdata;
1148         rcti *rect= gesture->customdata;
1149         
1150         if(rect->xmin > rect->xmax)
1151                 SWAP(int, rect->xmin, rect->xmax);
1152         if(rect->ymin > rect->ymax)
1153                 SWAP(int, rect->ymin, rect->ymax);
1154         
1155         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1156                 return 0;
1157                 
1158         /* operator arguments and storage. */
1159         RNA_int_set(op->ptr, "xmin", rect->xmin);
1160         RNA_int_set(op->ptr, "ymin", rect->ymin);
1161         RNA_int_set(op->ptr, "xmax", rect->xmax);
1162         RNA_int_set(op->ptr, "ymax", rect->ymax);
1163         
1164         /* XXX weak; border should be configured for this without reading event types */
1165         if( RNA_struct_find_property(op->ptr, "event_type") ) {
1166                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1167                         event_type= LEFTMOUSE;
1168                 
1169                 RNA_int_set(op->ptr, "event_type", event_type);
1170         }
1171         op->type->exec(C, op);
1172         
1173         return 1;
1174 }
1175
1176 static void wm_gesture_end(bContext *C, wmOperator *op)
1177 {
1178         wmGesture *gesture= op->customdata;
1179         
1180         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1181         op->customdata= NULL;
1182
1183         ED_area_tag_redraw(CTX_wm_area(C));
1184         
1185         if( RNA_struct_find_property(op->ptr, "cursor") )
1186                 WM_cursor_restore(CTX_wm_window(C));
1187 }
1188
1189 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1190 {
1191         if(WM_key_event_is_tweak(event->type))
1192                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1193         else
1194                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1195
1196         /* add modal handler */
1197         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1198         
1199         wm_gesture_tag_redraw(C);
1200
1201         return OPERATOR_RUNNING_MODAL;
1202 }
1203
1204 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1205 {
1206         wmGesture *gesture= op->customdata;
1207         rcti *rect= gesture->customdata;
1208         int sx, sy;
1209         
1210         switch(event->type) {
1211                 case MOUSEMOVE:
1212                         
1213                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1214                         
1215                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1216                                 rect->xmin= rect->xmax= event->x - sx;
1217                                 rect->ymin= rect->ymax= event->y - sy;
1218                         }
1219                         else {
1220                                 rect->xmax= event->x - sx;
1221                                 rect->ymax= event->y - sy;
1222                         }
1223                         
1224                         wm_gesture_tag_redraw(C);
1225
1226                         break;
1227                         
1228                 case LEFTMOUSE:
1229                 case MIDDLEMOUSE:
1230                 case RIGHTMOUSE:
1231                         if(event->val==KM_PRESS) {
1232                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1233                                         gesture->mode= 1;
1234                                         wm_gesture_tag_redraw(C);
1235                                 }
1236                         }
1237                         else {
1238                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1239                                         wm_gesture_end(C, op);
1240                                         return OPERATOR_FINISHED;
1241                                 }
1242                                 wm_gesture_end(C, op);
1243                                 return OPERATOR_CANCELLED;
1244                         }
1245                         break;
1246                 case ESCKEY:
1247                         wm_gesture_end(C, op);
1248                         return OPERATOR_CANCELLED;
1249         }
1250         return OPERATOR_RUNNING_MODAL;
1251 }
1252
1253 /* **************** circle gesture *************** */
1254 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1255
1256 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1257 {
1258         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1259         
1260         /* add modal handler */
1261         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1262         
1263         wm_gesture_tag_redraw(C);
1264         
1265         return OPERATOR_RUNNING_MODAL;
1266 }
1267
1268 static void gesture_circle_apply(bContext *C, wmOperator *op)
1269 {
1270         wmGesture *gesture= op->customdata;
1271         rcti *rect= gesture->customdata;
1272         
1273         /* operator arguments and storage. */
1274         RNA_int_set(op->ptr, "x", rect->xmin);
1275         RNA_int_set(op->ptr, "y", rect->ymin);
1276         RNA_int_set(op->ptr, "radius", rect->xmax);
1277         
1278         if(op->type->exec)
1279                 op->type->exec(C, op);
1280 }
1281
1282 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1283 {
1284         wmGesture *gesture= op->customdata;
1285         rcti *rect= gesture->customdata;
1286         int sx, sy;
1287         
1288         switch(event->type) {
1289                 case MOUSEMOVE:
1290                         
1291                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1292                         
1293                         rect->xmin= event->x - sx;
1294                         rect->ymin= event->y - sy;
1295                         
1296                         wm_gesture_tag_redraw(C);
1297                         
1298                         if(gesture->mode)
1299                                 gesture_circle_apply(C, op);
1300
1301                         break;
1302                 case WHEELUPMOUSE:
1303                         rect->xmax += 2 + rect->xmax/10;
1304                         wm_gesture_tag_redraw(C);
1305                         break;
1306                 case WHEELDOWNMOUSE:
1307                         rect->xmax -= 2 + rect->xmax/10;
1308                         if(rect->xmax < 1) rect->xmax= 1;
1309                         wm_gesture_tag_redraw(C);
1310                         break;
1311                 case LEFTMOUSE:
1312                 case MIDDLEMOUSE:
1313                 case RIGHTMOUSE:
1314                         if(event->val==0) {     /* key release */
1315                                 wm_gesture_end(C, op);
1316                                 return OPERATOR_FINISHED;
1317                         }
1318                         else {
1319                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1320                                         RNA_int_set(op->ptr, "event_type", event->type);
1321                                 
1322                                 /* apply first click */
1323                                 gesture_circle_apply(C, op);
1324                                 gesture->mode= 1;
1325                         }
1326                         break;
1327                 case ESCKEY:
1328                         wm_gesture_end(C, op);
1329                         return OPERATOR_CANCELLED;
1330         }
1331         return OPERATOR_RUNNING_MODAL;
1332 }
1333
1334 #if 0
1335 /* template to copy from */
1336 void WM_OT_circle_gesture(wmOperatorType *ot)
1337 {
1338         ot->name= "Circle Gesture";
1339         ot->idname= "WM_OT_circle_gesture";
1340         ot->description="Enter rotate mode with a circular gesture.";
1341         
1342         ot->invoke= WM_gesture_circle_invoke;
1343         ot->modal= WM_gesture_circle_modal;
1344         
1345         ot->poll= WM_operator_winactive;
1346         
1347         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1348         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1349         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1350
1351 }
1352 #endif
1353
1354 /* **************** Tweak gesture *************** */
1355
1356 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1357 {
1358         wmWindow *window= CTX_wm_window(C);
1359         wmGesture *gesture= window->tweak;
1360         rcti *rect= gesture->customdata;
1361         int sx, sy, val;
1362         
1363         switch(event->type) {
1364                 case MOUSEMOVE:
1365                         
1366                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1367                         
1368                         rect->xmax= event->x - sx;
1369                         rect->ymax= event->y - sy;
1370                         
1371                         if((val= wm_gesture_evaluate(C, gesture))) {
1372                                 wmEvent event;
1373
1374                                 event= *(window->eventstate);
1375                                 if(gesture->event_type==LEFTMOUSE)
1376                                         event.type= EVT_TWEAK_L;
1377                                 else if(gesture->event_type==RIGHTMOUSE)
1378                                         event.type= EVT_TWEAK_R;
1379                                 else
1380                                         event.type= EVT_TWEAK_M;
1381                                 event.val= val;
1382                                 /* mouse coords! */
1383                                 wm_event_add(window, &event);
1384                                 
1385                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1386                                 window->tweak= NULL;
1387                         }
1388                         
1389                         break;
1390                         
1391                 case LEFTMOUSE:
1392                 case RIGHTMOUSE:
1393                 case MIDDLEMOUSE:
1394                         if(gesture->event_type==event->type) {
1395                                 WM_gesture_end(C, gesture);
1396                                 window->tweak= NULL;
1397                                 
1398                                 /* when tweak fails we should give the other keymap entries a chance */
1399                                 event->val= KM_RELEASE;
1400                         }
1401                         break;
1402                 default:
1403                         WM_gesture_end(C, gesture);
1404                         window->tweak= NULL;
1405         }
1406 }
1407
1408 /* standard tweak, called after window handlers passed on event */
1409 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1410 {
1411         wmWindow *win= CTX_wm_window(C);
1412         
1413         if(win->tweak==NULL) {
1414                 if(CTX_wm_region(C)) {
1415                         if(event->val) { // pressed
1416                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1417                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1418                         }
1419                 }
1420         }
1421         else {
1422                 if(action==WM_HANDLER_BREAK) {
1423                         WM_gesture_end(C, win->tweak);
1424                         win->tweak= NULL;
1425                 }
1426                 else
1427                         tweak_gesture_modal(C, event);
1428         }
1429 }
1430
1431 /* *********************** lasso gesture ****************** */
1432
1433 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1434 {
1435         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1436         
1437         /* add modal handler */
1438         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1439         
1440         wm_gesture_tag_redraw(C);
1441         
1442         if( RNA_struct_find_property(op->ptr, "cursor") )
1443                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1444         
1445         return OPERATOR_RUNNING_MODAL;
1446 }
1447
1448 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1449 {
1450         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1451         
1452         /* add modal handler */
1453         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1454         
1455         wm_gesture_tag_redraw(C);
1456         
1457         if( RNA_struct_find_property(op->ptr, "cursor") )
1458                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1459         
1460         return OPERATOR_RUNNING_MODAL;
1461 }
1462
1463
1464 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1465 {
1466         wmGesture *gesture= op->customdata;
1467         PointerRNA itemptr;
1468         float loc[2];
1469         int i;
1470         short *lasso= gesture->customdata;
1471         
1472         /* operator storage as path. */
1473
1474         for(i=0; i<gesture->points; i++, lasso+=2) {
1475                 loc[0]= lasso[0];
1476                 loc[1]= lasso[1];
1477                 RNA_collection_add(op->ptr, "path", &itemptr);
1478                 RNA_float_set_array(&itemptr, "loc", loc);
1479         }
1480         
1481         wm_gesture_end(C, op);
1482                 
1483         if(op->type->exec)
1484                 op->type->exec(C, op);
1485         
1486 }
1487
1488 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1489 {
1490         wmGesture *gesture= op->customdata;
1491         int sx, sy;
1492         
1493         switch(event->type) {
1494                 case MOUSEMOVE:
1495                         
1496                         wm_gesture_tag_redraw(C);
1497                         
1498                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1499                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1500                                 short *lasso= gesture->customdata;
1501                                 lasso += 2 * gesture->points;
1502                                 lasso[0] = event->x - sx;
1503                                 lasso[1] = event->y - sy;
1504                                 gesture->points++;
1505                         }
1506                         else {
1507                                 gesture_lasso_apply(C, op, event->type);
1508                                 return OPERATOR_FINISHED;
1509                         }
1510                         break;
1511                         
1512                 case LEFTMOUSE:
1513                 case MIDDLEMOUSE:
1514                 case RIGHTMOUSE:
1515                         if(event->val==0) {     /* key release */
1516                                 gesture_lasso_apply(C, op, event->type);
1517                                 return OPERATOR_FINISHED;
1518                         }
1519                         break;
1520                 case ESCKEY:
1521                         wm_gesture_end(C, op);
1522                         return OPERATOR_CANCELLED;
1523         }
1524         return OPERATOR_RUNNING_MODAL;
1525 }
1526
1527 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1528 {
1529         return WM_gesture_lasso_modal(C, op, event);
1530 }
1531
1532 #if 0
1533 /* template to copy from */
1534
1535 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1536 {
1537         RNA_BEGIN(op->ptr, itemptr, "path") {
1538                 float loc[2];
1539                 
1540                 RNA_float_get_array(&itemptr, "loc", loc);
1541                 printf("Location: %f %f\n", loc[0], loc[1]);
1542         }
1543         RNA_END;
1544         
1545         return OPERATOR_FINISHED;
1546 }
1547
1548 void WM_OT_lasso_gesture(wmOperatorType *ot)
1549 {
1550         PropertyRNA *prop;
1551         
1552         ot->name= "Lasso Gesture";
1553         ot->idname= "WM_OT_lasso_gesture";
1554         ot->description="Select objects within the lasso as you move the pointer.";
1555         
1556         ot->invoke= WM_gesture_lasso_invoke;
1557         ot->modal= WM_gesture_lasso_modal;
1558         ot->exec= gesture_lasso_exec;
1559         
1560         ot->poll= WM_operator_winactive;
1561         
1562         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1563         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1564 }
1565 #endif
1566
1567 /* *********************** radial control ****************** */
1568
1569 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1570
1571 typedef struct wmRadialControl {
1572         int mode;
1573         float initial_value, value, max_value;
1574         int initial_mouse[2];
1575         void *cursor;
1576         GLuint tex;
1577 } wmRadialControl;
1578
1579 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1580 {
1581         wmRadialControl *rc = (wmRadialControl*)customdata;
1582         ARegion *ar = CTX_wm_region(C);
1583         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1584
1585         /* Keep cursor in the original place */
1586         x = rc->initial_mouse[0] - ar->winrct.xmin;
1587         y = rc->initial_mouse[1] - ar->winrct.ymin;
1588
1589         glPushMatrix();
1590         
1591         glTranslatef((float)x, (float)y, 0.0f);
1592
1593         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1594                 r1= rc->value;
1595                 r2= rc->initial_value;
1596                 r3= r1;
1597         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1598                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1599                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1600                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1601         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1602                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1603                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1604                 angle = rc->value;
1605         }
1606
1607         glColor4ub(255, 255, 255, 128);
1608         glEnable( GL_LINE_SMOOTH );
1609         glEnable(GL_BLEND);
1610
1611         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1612                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1613
1614         if(rc->tex) {
1615                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1616
1617                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1618                         glRotatef(angle, 0, 0, 1);
1619                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1620                 }
1621
1622                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1623
1624                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1625                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1626
1627                 glEnable(GL_TEXTURE_2D);
1628                 glBegin(GL_QUADS);
1629                 glColor4f(0,0,0, str);
1630                 glTexCoord2f(0,0);
1631                 glVertex2f(-r3, -r3);
1632                 glTexCoord2f(1,0);
1633                 glVertex2f(r3, -r3);
1634                 glTexCoord2f(1,1);
1635                 glVertex2f(r3, r3);
1636                 glTexCoord2f(0,1);
1637                 glVertex2f(-r3, r3);
1638                 glEnd();
1639                 glDisable(GL_TEXTURE_2D);
1640         }
1641
1642         glColor4ub(255, 255, 255, 128); 
1643         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1644         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1645         glDisable(GL_BLEND);
1646         glDisable( GL_LINE_SMOOTH );
1647         
1648         glPopMatrix();
1649 }
1650
1651 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1652 {
1653         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1654         int mode, initial_mouse[2], delta[2];
1655         float dist;
1656         double new_value = RNA_float_get(op->ptr, "new_value");
1657         int ret = OPERATOR_RUNNING_MODAL;
1658
1659         mode = RNA_int_get(op->ptr, "mode");
1660         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1661
1662         switch(event->type) {
1663         case MOUSEMOVE:
1664                 delta[0]= initial_mouse[0] - event->x;
1665                 delta[1]= initial_mouse[1] - event->y;
1666                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1667
1668                 if(mode == WM_RADIALCONTROL_SIZE)
1669                         new_value = dist;
1670                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1671                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1672                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1673                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1674                 
1675                 if(event->ctrl) {
1676                         if(mode == WM_RADIALCONTROL_STRENGTH)
1677                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1678                         else
1679                                 new_value = ((int)new_value + 5) / 10*10;
1680                 }
1681                 
1682                 break;
1683         case ESCKEY:
1684         case RIGHTMOUSE:
1685                 ret = OPERATOR_CANCELLED;
1686                 break;
1687         case LEFTMOUSE:
1688         case PADENTER:
1689                 op->type->exec(C, op);
1690                 ret = OPERATOR_FINISHED;
1691                 break;
1692         }
1693
1694         /* Clamp */
1695         if(new_value > rc->max_value)
1696                 new_value = rc->max_value;
1697         else if(new_value < 0)
1698                 new_value = 0;
1699
1700         /* Update paint data */
1701         rc->value = new_value;
1702
1703         RNA_float_set(op->ptr, "new_value", new_value);
1704
1705         if(ret != OPERATOR_RUNNING_MODAL) {
1706                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1707                 MEM_freeN(rc);
1708         }
1709         
1710         ED_region_tag_redraw(CTX_wm_region(C));
1711
1712         return ret;
1713 }
1714
1715 /* Expects the operator customdata to be an ImBuf (or NULL) */
1716 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1717 {
1718         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1719         int mode = RNA_int_get(op->ptr, "mode");
1720         float initial_value = RNA_float_get(op->ptr, "initial_value");
1721         int mouse[2] = {event->x, event->y};
1722
1723         if(mode == WM_RADIALCONTROL_SIZE) {
1724                 rc->max_value = 200;
1725                 mouse[0]-= initial_value;
1726         }
1727         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1728                 rc->max_value = 1;
1729                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1730         }
1731         else if(mode == WM_RADIALCONTROL_ANGLE) {
1732                 rc->max_value = 360;
1733                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1734                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1735                 initial_value *= 180.0f/M_PI;
1736         }
1737
1738         if(op->customdata) {
1739                 ImBuf *im = (ImBuf*)op->customdata;
1740                 /* Build GL texture */
1741                 glGenTextures(1, &rc->tex);
1742                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1743                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1744                 MEM_freeN(im->rect_float);
1745                 MEM_freeN(im);
1746         }
1747
1748         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1749         RNA_float_set(op->ptr, "new_value", initial_value);
1750                 
1751         op->customdata = rc;
1752         rc->mode = mode;
1753         rc->initial_value = initial_value;
1754         rc->initial_mouse[0] = mouse[0];
1755         rc->initial_mouse[1] = mouse[1];
1756         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1757                                               wm_radial_control_paint, op->customdata);
1758
1759         /* add modal handler */
1760         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1761         
1762         WM_radial_control_modal(C, op, event);
1763         
1764         return OPERATOR_RUNNING_MODAL;
1765 }
1766
1767 /* Gets a descriptive string of the operation */
1768 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1769 {
1770         int mode = RNA_int_get(op->ptr, "mode");
1771         float v = RNA_float_get(op->ptr, "new_value");
1772
1773         if(mode == WM_RADIALCONTROL_SIZE)
1774                 sprintf(str, "Size: %d", (int)v);
1775         else if(mode == WM_RADIALCONTROL_STRENGTH)
1776                 sprintf(str, "Strength: %d", (int)v);
1777         else if(mode == WM_RADIALCONTROL_ANGLE)
1778                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1779 }
1780
1781 /** Important: this doesn't define an actual operator, it
1782     just sets up the common parts of the radial control op. **/
1783 void WM_OT_radial_control_partial(wmOperatorType *ot)
1784 {
1785         static EnumPropertyItem prop_mode_items[] = {
1786                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
1787                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
1788                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
1789                 {0, NULL, 0, NULL, NULL}};
1790
1791         /* Should be set in custom invoke() */
1792         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1793
1794         /* Set internally, should be used in custom exec() to get final value */
1795         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1796
1797         /* Should be set before calling operator */
1798         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1799
1800         /* Internal */
1801         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1802 }
1803
1804 /* ************************** timer for testing ***************** */
1805
1806 /* uses no type defines, fully local testing function anyway... ;) */
1807
1808 static int ten_timer_exec(bContext *C, wmOperator *op)
1809 {
1810         ARegion *ar= CTX_wm_region(C);
1811         double stime= PIL_check_seconds_timer();
1812         int type = RNA_int_get(op->ptr, "type");
1813         int a, time;
1814         char tmpstr[128];
1815         
1816         WM_cursor_wait(1);
1817         
1818         for(a=0; a<10; a++) {
1819                 if (type==0) {
1820                         ED_region_do_draw(C, ar);
1821                 } 
1822                 else if (type==1) {
1823                         wmWindow *win= CTX_wm_window(C);
1824                         
1825                         ED_region_tag_redraw(ar);
1826                         wm_draw_update(C);
1827                         
1828                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1829                 }
1830                 else if (type==2) {
1831                         wmWindow *win= CTX_wm_window(C);
1832                         ScrArea *sa;
1833                         
1834                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1835                                 ED_area_tag_redraw(sa);
1836                         wm_draw_update(C);
1837                         
1838                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1839                 }
1840                 else if (type==3) {
1841                         Scene *scene= CTX_data_scene(C);
1842                         
1843                         if(a & 1) scene->r.cfra--;
1844                         else scene->r.cfra++;
1845                         scene_update_for_newframe(scene, scene->lay);
1846                 }
1847                 else {
1848                         ED_undo_pop(C);
1849                         ED_undo_redo(C);
1850                 }
1851         }
1852         
1853         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1854         
1855         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1856         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1857         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1858         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1859         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1860         
1861         WM_cursor_wait(0);
1862         
1863         uiPupMenuNotice(C, tmpstr);
1864         
1865         return OPERATOR_FINISHED;
1866 }
1867
1868 static void WM_OT_ten_timer(wmOperatorType *ot)
1869 {
1870         static EnumPropertyItem prop_type_items[] = {
1871         {0, "DRAW", 0, "Draw Region", ""},
1872         {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
1873         {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
1874         {3, "ANIMSTEP", 0, "Anim Step", ""},
1875         {4, "UNDO", 0, "Undo/Redo", ""},
1876         {0, NULL, 0, NULL, NULL}};
1877         
1878         ot->name= "Ten Timer";
1879         ot->idname= "WM_OT_ten_timer";
1880         ot->description="Ten Timer operator.";
1881         
1882         ot->invoke= WM_menu_invoke;
1883         ot->exec= ten_timer_exec;
1884         ot->poll= WM_operator_winactive;
1885         
1886         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1887
1888 }
1889
1890
1891
1892 /* ******************************************************* */
1893  
1894 /* called on initialize WM_exit() */
1895 void wm_operatortype_free(void)
1896 {
1897         wmOperatorType *ot;
1898         
1899         for(ot= global_ops.first; ot; ot= ot->next)
1900                 if(ot->macro.first)
1901                         wm_operatortype_free_macro(ot);
1902         
1903         BLI_freelistN(&global_ops);
1904 }
1905
1906 /* called on initialize WM_init() */
1907 void wm_operatortype_init(void)
1908 {
1909         WM_operatortype_append(WM_OT_window_duplicate);
1910         WM_operatortype_append(WM_OT_read_homefile);
1911         WM_operatortype_append(WM_OT_save_homefile);
1912         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1913         WM_operatortype_append(WM_OT_exit_blender);
1914         WM_operatortype_append(WM_OT_open_recentfile);
1915         WM_operatortype_append(WM_OT_open_mainfile);
1916         WM_operatortype_append(WM_OT_recover_last_session);
1917         WM_operatortype_append(WM_OT_jobs_timer);
1918         WM_operatortype_append(WM_OT_save_as_mainfile);
1919         WM_operatortype_append(WM_OT_save_mainfile);
1920         WM_operatortype_append(WM_OT_ten_timer);
1921         WM_operatortype_append(WM_OT_debug_menu);
1922         WM_operatortype_append(WM_OT_search_menu);
1923 }
1924
1925 /* default keymap for windows and screens, only call once per WM */
1926 void wm_window_keymap(wmWindowManager *wm)
1927 {
1928         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1929         
1930         /* items to make WM work */
1931         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1932         
1933         /* note, this doesn't replace existing keymap items */
1934         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1935         #ifdef __APPLE__
1936         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
1937         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
1938         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
1939         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
1940         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
1941         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
1942         #endif
1943         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
1944         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
1945         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
1946         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
1947         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
1948         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
1949
1950         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_SHIFT, 0);
1951         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1952
1953         /* debug/testing */
1954         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1955         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1956         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
1957         
1958 }
1959