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