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