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