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