ad490bb599adc50c3fedfd99810e3bde83202b8d
[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(ot->poll==NULL || ot->poll((bContext *)C)) {
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 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 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         BLI_strncpy(G.lib, dir, FILE_MAX);
1068
1069         WM_event_add_notifier(C, NC_WINDOW, NULL);
1070
1071         return OPERATOR_FINISHED;
1072 }
1073
1074 static void WM_OT_link_append(wmOperatorType *ot)
1075 {
1076         ot->name= "Link/Append from Library";
1077         ot->idname= "WM_OT_link_append";
1078         ot->description= "Link or Append from a Library .blend file";
1079         
1080         ot->invoke= wm_link_append_invoke;
1081         ot->exec= wm_link_append_exec;
1082         ot->poll= WM_operator_winactive;
1083         
1084         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB);
1085         
1086         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending.");
1087         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects.");
1088         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer.");
1089         RNA_def_boolean(ot->srna, "relative_paths", 1, "Relative Paths", "Store the library path as a relative path to current .blend file.");
1090
1091         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1092 }       
1093
1094 /* *************** recover last session **************** */
1095
1096 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1097 {
1098         char scestr[FILE_MAX], filename[FILE_MAX];
1099         int save_over;
1100
1101         /* back up some values */
1102         BLI_strncpy(scestr, G.sce, sizeof(scestr));
1103         save_over = G.save_over;
1104
1105         // XXX wm in context is not set correctly after WM_read_file -> crash
1106         // do it before for now, but is this correct with multiple windows?
1107         WM_event_add_notifier(C, NC_WINDOW, NULL);
1108
1109         /* load file */
1110         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1111         WM_read_file(C, filename, op->reports);
1112
1113         /* restore */
1114         G.save_over = save_over;
1115         BLI_strncpy(G.sce, scestr, sizeof(G.sce));
1116
1117         return 0;
1118 }
1119
1120 static void WM_OT_recover_last_session(wmOperatorType *ot)
1121 {
1122         ot->name= "Recover Last Session";
1123         ot->idname= "WM_OT_recover_last_session";
1124         ot->description="Open the last closed file (\"quit.blend\").";
1125         
1126         ot->exec= wm_recover_last_session_exec;
1127         ot->poll= WM_operator_winactive;
1128 }
1129
1130 /* *************** save file as **************** */
1131
1132 static void untitled(char *name)
1133 {
1134         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1135                 char *c= BLI_last_slash(name);
1136                 
1137                 if(c)
1138                         strcpy(&c[1], "untitled.blend");
1139                 else
1140                         strcpy(name, "untitled.blend");
1141         }
1142 }
1143
1144 static void save_set_compress(wmOperator *op)
1145 {
1146         if(!RNA_property_is_set(op->ptr, "compress")) {
1147                 if(G.save_over) /* keep flag for existing file */
1148                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1149                 else /* use userdef for new file */
1150                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1151         }
1152 }
1153
1154 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1155 {
1156         char name[FILE_MAX];
1157
1158         save_set_compress(op);
1159         
1160         BLI_strncpy(name, G.sce, FILE_MAX);
1161         untitled(name);
1162         RNA_string_set(op->ptr, "path", name);
1163         
1164         WM_event_add_fileselect(C, op);
1165
1166         return OPERATOR_RUNNING_MODAL;
1167 }
1168
1169 /* function used for WM_OT_save_mainfile too */
1170 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1171 {
1172         char path[FILE_MAX];
1173         int compress;
1174
1175         save_set_compress(op);
1176         compress= RNA_boolean_get(op->ptr, "compress");
1177         
1178         if(RNA_property_is_set(op->ptr, "path"))
1179                 RNA_string_get(op->ptr, "path", path);
1180         else {
1181                 BLI_strncpy(path, G.sce, FILE_MAX);
1182                 untitled(path);
1183         }
1184
1185         WM_write_file(C, path, compress, op->reports);
1186         
1187         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1188
1189         return 0;
1190 }
1191
1192 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1193 {
1194         ot->name= "Save As Blender File";
1195         ot->idname= "WM_OT_save_as_mainfile";
1196         ot->description="Save the current file in the desired location.";
1197         
1198         ot->invoke= wm_save_as_mainfile_invoke;
1199         ot->exec= wm_save_as_mainfile_exec;
1200         ot->poll= WM_operator_winactive;
1201         
1202         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1203         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1204 }
1205
1206 /* *************** save file directly ******** */
1207
1208 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1209 {
1210         char name[FILE_MAX];
1211
1212         save_set_compress(op);
1213         
1214         BLI_strncpy(name, G.sce, FILE_MAX);
1215         untitled(name);
1216         RNA_string_set(op->ptr, "path", name);
1217         uiPupMenuSaveOver(C, op, name);
1218
1219         return OPERATOR_RUNNING_MODAL;
1220 }
1221
1222 static void WM_OT_save_mainfile(wmOperatorType *ot)
1223 {
1224         ot->name= "Save Blender File";
1225         ot->idname= "WM_OT_save_mainfile";
1226         ot->description="Save the current Blender file.";
1227         
1228         ot->invoke= wm_save_mainfile_invoke;
1229         ot->exec= wm_save_as_mainfile_exec;
1230         ot->poll= WM_operator_winactive;
1231         
1232         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1233         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1234 }
1235
1236
1237 /* *********************** */
1238
1239 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1240 {
1241         ot->name= "Toggle Fullscreen";
1242         ot->idname= "WM_OT_window_fullscreen_toggle";
1243         ot->description="Toggle the current window fullscreen.";
1244
1245         ot->exec= wm_window_fullscreen_toggle_op;
1246         ot->poll= WM_operator_winactive;
1247 }
1248
1249 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1250 {
1251         WM_operator_free(op);
1252         
1253         WM_exit(C);     
1254         
1255         return OPERATOR_FINISHED;
1256 }
1257
1258 static void WM_OT_exit_blender(wmOperatorType *ot)
1259 {
1260         ot->name= "Exit Blender";
1261         ot->idname= "WM_OT_exit_blender";
1262         ot->description= "Quit Blender.";
1263
1264         ot->invoke= WM_operator_confirm;
1265         ot->exec= wm_exit_blender_op;
1266         ot->poll= WM_operator_winactive;
1267 }
1268
1269 /* ************ default paint cursors, draw always around cursor *********** */
1270 /*
1271  - returns handler to free 
1272  - poll(bContext): returns 1 if draw should happen
1273  - draw(bContext): drawing callback for paint cursor
1274 */
1275
1276 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1277                                wmPaintCursorDraw draw, void *customdata)
1278 {
1279         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1280         
1281         BLI_addtail(&wm->paintcursors, pc);
1282         
1283         pc->customdata = customdata;
1284         pc->poll= poll;
1285         pc->draw= draw;
1286         
1287         return pc;
1288 }
1289
1290 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1291 {
1292         wmPaintCursor *pc;
1293         
1294         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1295                 if(pc == (wmPaintCursor *)handle) {
1296                         BLI_remlink(&wm->paintcursors, pc);
1297                         MEM_freeN(pc);
1298                         return;
1299                 }
1300         }
1301 }
1302
1303 /* ************ window gesture operator-callback definitions ************** */
1304 /*
1305  * These are default callbacks for use in operators requiring gesture input
1306  */
1307
1308 /* **************** Border gesture *************** */
1309
1310 /* Border gesture has two types:
1311    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1312    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1313
1314    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1315 */
1316
1317 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1318 {
1319         wmGesture *gesture= op->customdata;
1320         rcti *rect= gesture->customdata;
1321         
1322         if(rect->xmin > rect->xmax)
1323                 SWAP(int, rect->xmin, rect->xmax);
1324         if(rect->ymin > rect->ymax)
1325                 SWAP(int, rect->ymin, rect->ymax);
1326         
1327         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1328                 return 0;
1329                 
1330         /* operator arguments and storage. */
1331         RNA_int_set(op->ptr, "xmin", rect->xmin);
1332         RNA_int_set(op->ptr, "ymin", rect->ymin);
1333         RNA_int_set(op->ptr, "xmax", rect->xmax);
1334         RNA_int_set(op->ptr, "ymax", rect->ymax);
1335         
1336         /* XXX weak; border should be configured for this without reading event types */
1337         if( RNA_struct_find_property(op->ptr, "event_type") ) {
1338                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1339                         event_type= LEFTMOUSE;
1340                 
1341                 RNA_int_set(op->ptr, "event_type", event_type);
1342         }
1343         op->type->exec(C, op);
1344         
1345         return 1;
1346 }
1347
1348 static void wm_gesture_end(bContext *C, wmOperator *op)
1349 {
1350         wmGesture *gesture= op->customdata;
1351         
1352         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1353         op->customdata= NULL;
1354
1355         ED_area_tag_redraw(CTX_wm_area(C));
1356         
1357         if( RNA_struct_find_property(op->ptr, "cursor") )
1358                 WM_cursor_restore(CTX_wm_window(C));
1359 }
1360
1361 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1362 {
1363         if(WM_key_event_is_tweak(event->type))
1364                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1365         else
1366                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1367
1368         /* add modal handler */
1369         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1370         
1371         wm_gesture_tag_redraw(C);
1372
1373         return OPERATOR_RUNNING_MODAL;
1374 }
1375
1376 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1377 {
1378         wmGesture *gesture= op->customdata;
1379         rcti *rect= gesture->customdata;
1380         int sx, sy;
1381         
1382         switch(event->type) {
1383                 case MOUSEMOVE:
1384                         
1385                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1386                         
1387                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1388                                 rect->xmin= rect->xmax= event->x - sx;
1389                                 rect->ymin= rect->ymax= event->y - sy;
1390                         }
1391                         else {
1392                                 rect->xmax= event->x - sx;
1393                                 rect->ymax= event->y - sy;
1394                         }
1395                         
1396                         wm_gesture_tag_redraw(C);
1397
1398                         break;
1399                         
1400                 case LEFTMOUSE:
1401                 case MIDDLEMOUSE:
1402                 case RIGHTMOUSE:
1403                         if(event->val==KM_PRESS) {
1404                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1405                                         gesture->mode= 1;
1406                                         wm_gesture_tag_redraw(C);
1407                                 }
1408                         }
1409                         else {
1410                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1411                                         wm_gesture_end(C, op);
1412                                         return OPERATOR_FINISHED;
1413                                 }
1414                                 wm_gesture_end(C, op);
1415                                 return OPERATOR_CANCELLED;
1416                         }
1417                         break;
1418                 case ESCKEY:
1419                         wm_gesture_end(C, op);
1420                         return OPERATOR_CANCELLED;
1421         }
1422         return OPERATOR_RUNNING_MODAL;
1423 }
1424
1425 /* **************** circle gesture *************** */
1426 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1427
1428 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1429 {
1430         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1431         
1432         /* add modal handler */
1433         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1434         
1435         wm_gesture_tag_redraw(C);
1436         
1437         return OPERATOR_RUNNING_MODAL;
1438 }
1439
1440 static void gesture_circle_apply(bContext *C, wmOperator *op)
1441 {
1442         wmGesture *gesture= op->customdata;
1443         rcti *rect= gesture->customdata;
1444         
1445         /* operator arguments and storage. */
1446         RNA_int_set(op->ptr, "x", rect->xmin);
1447         RNA_int_set(op->ptr, "y", rect->ymin);
1448         RNA_int_set(op->ptr, "radius", rect->xmax);
1449         
1450         if(op->type->exec)
1451                 op->type->exec(C, op);
1452 }
1453
1454 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1455 {
1456         wmGesture *gesture= op->customdata;
1457         rcti *rect= gesture->customdata;
1458         int sx, sy;
1459         
1460         switch(event->type) {
1461                 case MOUSEMOVE:
1462                         
1463                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1464                         
1465                         rect->xmin= event->x - sx;
1466                         rect->ymin= event->y - sy;
1467                         
1468                         wm_gesture_tag_redraw(C);
1469                         
1470                         if(gesture->mode)
1471                                 gesture_circle_apply(C, op);
1472
1473                         break;
1474                 case WHEELUPMOUSE:
1475                         rect->xmax += 2 + rect->xmax/10;
1476                         wm_gesture_tag_redraw(C);
1477                         break;
1478                 case WHEELDOWNMOUSE:
1479                         rect->xmax -= 2 + rect->xmax/10;
1480                         if(rect->xmax < 1) rect->xmax= 1;
1481                         wm_gesture_tag_redraw(C);
1482                         break;
1483                 case LEFTMOUSE:
1484                 case MIDDLEMOUSE:
1485                 case RIGHTMOUSE:
1486                         if(event->val==0) {     /* key release */
1487                                 wm_gesture_end(C, op);
1488                                 return OPERATOR_FINISHED;
1489                         }
1490                         else {
1491                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1492                                         RNA_int_set(op->ptr, "event_type", event->type);
1493                                 
1494                                 /* apply first click */
1495                                 gesture_circle_apply(C, op);
1496                                 gesture->mode= 1;
1497                         }
1498                         break;
1499                 case ESCKEY:
1500                         wm_gesture_end(C, op);
1501                         return OPERATOR_CANCELLED;
1502         }
1503         return OPERATOR_RUNNING_MODAL;
1504 }
1505
1506 #if 0
1507 /* template to copy from */
1508 void WM_OT_circle_gesture(wmOperatorType *ot)
1509 {
1510         ot->name= "Circle Gesture";
1511         ot->idname= "WM_OT_circle_gesture";
1512         ot->description="Enter rotate mode with a circular gesture.";
1513         
1514         ot->invoke= WM_gesture_circle_invoke;
1515         ot->modal= WM_gesture_circle_modal;
1516         
1517         ot->poll= WM_operator_winactive;
1518         
1519         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1520         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1521         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1522
1523 }
1524 #endif
1525
1526 /* **************** Tweak gesture *************** */
1527
1528 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1529 {
1530         wmWindow *window= CTX_wm_window(C);
1531         wmGesture *gesture= window->tweak;
1532         rcti *rect= gesture->customdata;
1533         int sx, sy, val;
1534         
1535         switch(event->type) {
1536                 case MOUSEMOVE:
1537                         
1538                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1539                         
1540                         rect->xmax= event->x - sx;
1541                         rect->ymax= event->y - sy;
1542                         
1543                         if((val= wm_gesture_evaluate(C, gesture))) {
1544                                 wmEvent event;
1545
1546                                 event= *(window->eventstate);
1547                                 if(gesture->event_type==LEFTMOUSE)
1548                                         event.type= EVT_TWEAK_L;
1549                                 else if(gesture->event_type==RIGHTMOUSE)
1550                                         event.type= EVT_TWEAK_R;
1551                                 else
1552                                         event.type= EVT_TWEAK_M;
1553                                 event.val= val;
1554                                 /* mouse coords! */
1555                                 wm_event_add(window, &event);
1556                                 
1557                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1558                                 window->tweak= NULL;
1559                         }
1560                         
1561                         break;
1562                         
1563                 case LEFTMOUSE:
1564                 case RIGHTMOUSE:
1565                 case MIDDLEMOUSE:
1566                         if(gesture->event_type==event->type) {
1567                                 WM_gesture_end(C, gesture);
1568                                 window->tweak= NULL;
1569                                 
1570                                 /* when tweak fails we should give the other keymap entries a chance */
1571                                 event->val= KM_RELEASE;
1572                         }
1573                         break;
1574                 default:
1575                         WM_gesture_end(C, gesture);
1576                         window->tweak= NULL;
1577         }
1578 }
1579
1580 /* standard tweak, called after window handlers passed on event */
1581 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1582 {
1583         wmWindow *win= CTX_wm_window(C);
1584         
1585         if(win->tweak==NULL) {
1586                 if(CTX_wm_region(C)) {
1587                         if(event->val) { // pressed
1588                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1589                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1590                         }
1591                 }
1592         }
1593         else {
1594                 if(action==WM_HANDLER_BREAK) {
1595                         WM_gesture_end(C, win->tweak);
1596                         win->tweak= NULL;
1597                 }
1598                 else
1599                         tweak_gesture_modal(C, event);
1600         }
1601 }
1602
1603 /* *********************** lasso gesture ****************** */
1604
1605 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1606 {
1607         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1608         
1609         /* add modal handler */
1610         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1611         
1612         wm_gesture_tag_redraw(C);
1613         
1614         if( RNA_struct_find_property(op->ptr, "cursor") )
1615                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1616         
1617         return OPERATOR_RUNNING_MODAL;
1618 }
1619
1620 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1621 {
1622         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1623         
1624         /* add modal handler */
1625         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1626         
1627         wm_gesture_tag_redraw(C);
1628         
1629         if( RNA_struct_find_property(op->ptr, "cursor") )
1630                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1631         
1632         return OPERATOR_RUNNING_MODAL;
1633 }
1634
1635
1636 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1637 {
1638         wmGesture *gesture= op->customdata;
1639         PointerRNA itemptr;
1640         float loc[2];
1641         int i;
1642         short *lasso= gesture->customdata;
1643         
1644         /* operator storage as path. */
1645
1646         for(i=0; i<gesture->points; i++, lasso+=2) {
1647                 loc[0]= lasso[0];
1648                 loc[1]= lasso[1];
1649                 RNA_collection_add(op->ptr, "path", &itemptr);
1650                 RNA_float_set_array(&itemptr, "loc", loc);
1651         }
1652         
1653         wm_gesture_end(C, op);
1654                 
1655         if(op->type->exec)
1656                 op->type->exec(C, op);
1657         
1658 }
1659
1660 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1661 {
1662         wmGesture *gesture= op->customdata;
1663         int sx, sy;
1664         
1665         switch(event->type) {
1666                 case MOUSEMOVE:
1667                         
1668                         wm_gesture_tag_redraw(C);
1669                         
1670                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1671                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1672                                 short *lasso= gesture->customdata;
1673                                 lasso += 2 * gesture->points;
1674                                 lasso[0] = event->x - sx;
1675                                 lasso[1] = event->y - sy;
1676                                 gesture->points++;
1677                         }
1678                         else {
1679                                 gesture_lasso_apply(C, op, event->type);
1680                                 return OPERATOR_FINISHED;
1681                         }
1682                         break;
1683                         
1684                 case LEFTMOUSE:
1685                 case MIDDLEMOUSE:
1686                 case RIGHTMOUSE:
1687                         if(event->val==0) {     /* key release */
1688                                 gesture_lasso_apply(C, op, event->type);
1689                                 return OPERATOR_FINISHED;
1690                         }
1691                         break;
1692                 case ESCKEY:
1693                         wm_gesture_end(C, op);
1694                         return OPERATOR_CANCELLED;
1695         }
1696         return OPERATOR_RUNNING_MODAL;
1697 }
1698
1699 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1700 {
1701         return WM_gesture_lasso_modal(C, op, event);
1702 }
1703
1704 #if 0
1705 /* template to copy from */
1706
1707 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1708 {
1709         RNA_BEGIN(op->ptr, itemptr, "path") {
1710                 float loc[2];
1711                 
1712                 RNA_float_get_array(&itemptr, "loc", loc);
1713                 printf("Location: %f %f\n", loc[0], loc[1]);
1714         }
1715         RNA_END;
1716         
1717         return OPERATOR_FINISHED;
1718 }
1719
1720 void WM_OT_lasso_gesture(wmOperatorType *ot)
1721 {
1722         PropertyRNA *prop;
1723         
1724         ot->name= "Lasso Gesture";
1725         ot->idname= "WM_OT_lasso_gesture";
1726         ot->description="Select objects within the lasso as you move the pointer.";
1727         
1728         ot->invoke= WM_gesture_lasso_invoke;
1729         ot->modal= WM_gesture_lasso_modal;
1730         ot->exec= gesture_lasso_exec;
1731         
1732         ot->poll= WM_operator_winactive;
1733         
1734         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1735         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1736 }
1737 #endif
1738
1739 /* *********************** radial control ****************** */
1740
1741 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1742
1743 typedef struct wmRadialControl {
1744         int mode;
1745         float initial_value, value, max_value;
1746         int initial_mouse[2];
1747         void *cursor;
1748         GLuint tex;
1749 } wmRadialControl;
1750
1751 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1752 {
1753         wmRadialControl *rc = (wmRadialControl*)customdata;
1754         ARegion *ar = CTX_wm_region(C);
1755         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1756
1757         /* Keep cursor in the original place */
1758         x = rc->initial_mouse[0] - ar->winrct.xmin;
1759         y = rc->initial_mouse[1] - ar->winrct.ymin;
1760
1761         glPushMatrix();
1762         
1763         glTranslatef((float)x, (float)y, 0.0f);
1764
1765         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1766                 r1= rc->value;
1767                 r2= rc->initial_value;
1768                 r3= r1;
1769         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1770                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1771                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1772                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1773         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1774                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1775                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1776                 angle = rc->value;
1777         }
1778
1779         glColor4ub(255, 255, 255, 128);
1780         glEnable( GL_LINE_SMOOTH );
1781         glEnable(GL_BLEND);
1782
1783         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1784                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1785
1786         if(rc->tex) {
1787                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1788
1789                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1790                         glRotatef(angle, 0, 0, 1);
1791                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1792                 }
1793
1794                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1795
1796                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1797                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1798
1799                 glEnable(GL_TEXTURE_2D);
1800                 glBegin(GL_QUADS);
1801                 glColor4f(0,0,0, str);
1802                 glTexCoord2f(0,0);
1803                 glVertex2f(-r3, -r3);
1804                 glTexCoord2f(1,0);
1805                 glVertex2f(r3, -r3);
1806                 glTexCoord2f(1,1);
1807                 glVertex2f(r3, r3);
1808                 glTexCoord2f(0,1);
1809                 glVertex2f(-r3, r3);
1810                 glEnd();
1811                 glDisable(GL_TEXTURE_2D);
1812         }
1813
1814         glColor4ub(255, 255, 255, 128); 
1815         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1816         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1817         glDisable(GL_BLEND);
1818         glDisable( GL_LINE_SMOOTH );
1819         
1820         glPopMatrix();
1821 }
1822
1823 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1824 {
1825         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1826         int mode, initial_mouse[2], delta[2];
1827         float dist;
1828         double new_value = RNA_float_get(op->ptr, "new_value");
1829         int ret = OPERATOR_RUNNING_MODAL;
1830
1831         mode = RNA_int_get(op->ptr, "mode");
1832         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1833
1834         switch(event->type) {
1835         case MOUSEMOVE:
1836                 delta[0]= initial_mouse[0] - event->x;
1837                 delta[1]= initial_mouse[1] - event->y;
1838                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1839
1840                 if(mode == WM_RADIALCONTROL_SIZE)
1841                         new_value = dist;
1842                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1843                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1844                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1845                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1846                 
1847                 if(event->ctrl) {
1848                         if(mode == WM_RADIALCONTROL_STRENGTH)
1849                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1850                         else
1851                                 new_value = ((int)new_value + 5) / 10*10;
1852                 }
1853                 
1854                 break;
1855         case ESCKEY:
1856         case RIGHTMOUSE:
1857                 ret = OPERATOR_CANCELLED;
1858                 break;
1859         case LEFTMOUSE:
1860         case PADENTER:
1861                 op->type->exec(C, op);
1862                 ret = OPERATOR_FINISHED;
1863                 break;
1864         }
1865
1866         /* Clamp */
1867         if(new_value > rc->max_value)
1868                 new_value = rc->max_value;
1869         else if(new_value < 0)
1870                 new_value = 0;
1871
1872         /* Update paint data */
1873         rc->value = new_value;
1874
1875         RNA_float_set(op->ptr, "new_value", new_value);
1876
1877         if(ret != OPERATOR_RUNNING_MODAL) {
1878                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1879                 MEM_freeN(rc);
1880         }
1881         
1882         ED_region_tag_redraw(CTX_wm_region(C));
1883
1884         return ret;
1885 }
1886
1887 /* Expects the operator customdata to be an ImBuf (or NULL) */
1888 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1889 {
1890         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1891         int mode = RNA_int_get(op->ptr, "mode");
1892         float initial_value = RNA_float_get(op->ptr, "initial_value");
1893         int mouse[2] = {event->x, event->y};
1894
1895         if(mode == WM_RADIALCONTROL_SIZE) {
1896                 rc->max_value = 200;
1897                 mouse[0]-= initial_value;
1898         }
1899         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1900                 rc->max_value = 1;
1901                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1902         }
1903         else if(mode == WM_RADIALCONTROL_ANGLE) {
1904                 rc->max_value = 360;
1905                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1906                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1907                 initial_value *= 180.0f/M_PI;
1908         }
1909
1910         if(op->customdata) {
1911                 ImBuf *im = (ImBuf*)op->customdata;
1912                 /* Build GL texture */
1913                 glGenTextures(1, &rc->tex);
1914                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1915                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1916                 MEM_freeN(im->rect_float);
1917                 MEM_freeN(im);
1918         }
1919
1920         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1921         RNA_float_set(op->ptr, "new_value", initial_value);
1922                 
1923         op->customdata = rc;
1924         rc->mode = mode;
1925         rc->initial_value = initial_value;
1926         rc->initial_mouse[0] = mouse[0];
1927         rc->initial_mouse[1] = mouse[1];
1928         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1929                                               wm_radial_control_paint, op->customdata);
1930
1931         /* add modal handler */
1932         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1933         
1934         WM_radial_control_modal(C, op, event);
1935         
1936         return OPERATOR_RUNNING_MODAL;
1937 }
1938
1939 /* Gets a descriptive string of the operation */
1940 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1941 {
1942         int mode = RNA_int_get(op->ptr, "mode");
1943         float v = RNA_float_get(op->ptr, "new_value");
1944
1945         if(mode == WM_RADIALCONTROL_SIZE)
1946                 sprintf(str, "Size: %d", (int)v);
1947         else if(mode == WM_RADIALCONTROL_STRENGTH)
1948                 sprintf(str, "Strength: %d", (int)v);
1949         else if(mode == WM_RADIALCONTROL_ANGLE)
1950                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1951 }
1952
1953 /** Important: this doesn't define an actual operator, it
1954     just sets up the common parts of the radial control op. **/
1955 void WM_OT_radial_control_partial(wmOperatorType *ot)
1956 {
1957         static EnumPropertyItem prop_mode_items[] = {
1958                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
1959                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
1960                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
1961                 {0, NULL, 0, NULL, NULL}};
1962
1963         /* Should be set in custom invoke() */
1964         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1965
1966         /* Set internally, should be used in custom exec() to get final value */
1967         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1968
1969         /* Should be set before calling operator */
1970         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1971
1972         /* Internal */
1973         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1974 }
1975
1976 /* ************************** timer for testing ***************** */
1977
1978 /* uses no type defines, fully local testing function anyway... ;) */
1979
1980 static int ten_timer_exec(bContext *C, wmOperator *op)
1981 {
1982         ARegion *ar= CTX_wm_region(C);
1983         double stime= PIL_check_seconds_timer();
1984         int type = RNA_int_get(op->ptr, "type");
1985         int a, time;
1986         char tmpstr[128];
1987         
1988         WM_cursor_wait(1);
1989         
1990         for(a=0; a<10; a++) {
1991                 if (type==0) {
1992                         ED_region_do_draw(C, ar);
1993                 } 
1994                 else if (type==1) {
1995                         wmWindow *win= CTX_wm_window(C);
1996                         
1997                         ED_region_tag_redraw(ar);
1998                         wm_draw_update(C);
1999                         
2000                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2001                 }
2002                 else if (type==2) {
2003                         wmWindow *win= CTX_wm_window(C);
2004                         ScrArea *sa;
2005                         
2006                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2007                                 ED_area_tag_redraw(sa);
2008                         wm_draw_update(C);
2009                         
2010                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2011                 }
2012                 else if (type==3) {
2013                         Scene *scene= CTX_data_scene(C);
2014                         
2015                         if(a & 1) scene->r.cfra--;
2016                         else scene->r.cfra++;
2017                         scene_update_for_newframe(scene, scene->lay);
2018                 }
2019                 else {
2020                         ED_undo_pop(C);
2021                         ED_undo_redo(C);
2022                 }
2023         }
2024         
2025         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
2026         
2027         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
2028         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
2029         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
2030         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
2031         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
2032         
2033         WM_cursor_wait(0);
2034         
2035         uiPupMenuNotice(C, tmpstr);
2036         
2037         return OPERATOR_FINISHED;
2038 }
2039
2040 static void WM_OT_ten_timer(wmOperatorType *ot)
2041 {
2042         static EnumPropertyItem prop_type_items[] = {
2043         {0, "DRAW", 0, "Draw Region", ""},
2044         {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
2045         {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
2046         {3, "ANIMSTEP", 0, "Anim Step", ""},
2047         {4, "UNDO", 0, "Undo/Redo", ""},
2048         {0, NULL, 0, NULL, NULL}};
2049         
2050         ot->name= "Ten Timer";
2051         ot->idname= "WM_OT_ten_timer";
2052         ot->description="Ten Timer operator.";
2053         
2054         ot->invoke= WM_menu_invoke;
2055         ot->exec= ten_timer_exec;
2056         ot->poll= WM_operator_winactive;
2057         
2058         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
2059
2060 }
2061
2062
2063
2064 /* ******************************************************* */
2065  
2066 /* called on initialize WM_exit() */
2067 void wm_operatortype_free(void)
2068 {
2069         wmOperatorType *ot;
2070         
2071         for(ot= global_ops.first; ot; ot= ot->next)
2072                 if(ot->macro.first)
2073                         wm_operatortype_free_macro(ot);
2074         
2075         BLI_freelistN(&global_ops);
2076 }
2077
2078 /* called on initialize WM_init() */
2079 void wm_operatortype_init(void)
2080 {
2081         WM_operatortype_append(WM_OT_window_duplicate);
2082         WM_operatortype_append(WM_OT_read_homefile);
2083         WM_operatortype_append(WM_OT_save_homefile);
2084         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
2085         WM_operatortype_append(WM_OT_exit_blender);
2086         WM_operatortype_append(WM_OT_open_recentfile);
2087         WM_operatortype_append(WM_OT_open_mainfile);
2088         WM_operatortype_append(WM_OT_link_append);
2089         WM_operatortype_append(WM_OT_recover_last_session);
2090         WM_operatortype_append(WM_OT_jobs_timer);
2091         WM_operatortype_append(WM_OT_save_as_mainfile);
2092         WM_operatortype_append(WM_OT_save_mainfile);
2093         WM_operatortype_append(WM_OT_ten_timer);
2094         WM_operatortype_append(WM_OT_debug_menu);
2095         WM_operatortype_append(WM_OT_search_menu);
2096 }
2097
2098 /* default keymap for windows and screens, only call once per WM */
2099 void wm_window_keymap(wmWindowManager *wm)
2100 {
2101         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
2102         
2103         /* items to make WM work */
2104         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
2105         
2106         /* note, this doesn't replace existing keymap items */
2107         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2108         #ifdef __APPLE__
2109         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
2110         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2111         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
2112         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
2113         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2114         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
2115         #endif
2116         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
2117         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
2118         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2119         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
2120         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2121         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
2122         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2123
2124         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_SHIFT, 0);
2125         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
2126
2127         /* debug/testing */
2128         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2129         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2130         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
2131         
2132 }
2133