9c0fc4d8e9e34a0078b264bf5d93d7f8997efd8a
[blender-staging.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
34 #include "DNA_ID.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_userdef_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "PIL_time.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_dynstr.h" /*for WM_operator_pystring */
46
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_scene.h"
54 #include "BKE_utildefines.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_glutil.h" /* for paint cursor */
58
59 #include "IMB_imbuf_types.h"
60
61 #include "ED_screen.h"
62 #include "ED_util.h"
63
64 #include "RNA_access.h"
65 #include "RNA_define.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "wm.h"
74 #include "wm_draw.h"
75 #include "wm_event_system.h"
76 #include "wm_subwindow.h"
77 #include "wm_window.h"
78
79
80
81 static ListBase global_ops= {NULL, NULL};
82
83 /* ************ operator API, exported ********** */
84
85 wmOperatorType *WM_operatortype_find(const char *idname)
86 {
87         wmOperatorType *ot;
88         
89         for(ot= global_ops.first; ot; ot= ot->next) {
90                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
91                    return ot;
92         }
93         printf("search for unknown operator %s\n", idname);
94         return NULL;
95 }
96
97 wmOperatorType *WM_operatortype_first(void)
98 {
99         return global_ops.first;
100 }
101
102 /* all ops in 1 list (for time being... needs evaluation later) */
103 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
104 {
105         wmOperatorType *ot;
106         
107         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
108         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
109         opfunc(ot);
110         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:""); // XXX All ops should have a description but for now allow them not to.
111         RNA_def_struct_identifier(ot->srna, ot->idname);
112         BLI_addtail(&global_ops, ot);
113 }
114
115 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
116 {
117         wmOperatorType *ot;
118
119         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
120         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
121         opfunc(ot, userdata);
122         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
123         RNA_def_struct_identifier(ot->srna, ot->idname);
124         BLI_addtail(&global_ops, ot);
125 }
126
127 int WM_operatortype_remove(const char *idname)
128 {
129         wmOperatorType *ot = WM_operatortype_find(idname);
130
131         if (ot==NULL)
132                 return 0;
133         
134         BLI_remlink(&global_ops, ot);
135         RNA_struct_free(&BLENDER_RNA, ot->srna);
136         MEM_freeN(ot);
137
138         return 1;
139 }
140
141 /* print a string representation of the operator, with the args that it runs 
142  * so python can run it again */
143 char *WM_operator_pystring(wmOperator *op)
144 {
145         const char *arg_name= NULL;
146
147         PropertyRNA *prop, *iterprop;
148         CollectionPropertyIterator iter;
149
150         /* for building the string */
151         DynStr *dynstr= BLI_dynstr_new();
152         char *cstring, *buf;
153         int first_iter=1;
154
155         BLI_dynstr_appendf(dynstr, "%s(", op->idname);
156
157         iterprop= RNA_struct_iterator_property(op->ptr->type);
158         RNA_property_collection_begin(op->ptr, iterprop, &iter);
159
160         for(; iter.valid; RNA_property_collection_next(&iter)) {
161                 prop= iter.ptr.data;
162                 arg_name= RNA_property_identifier(prop);
163
164                 if (strcmp(arg_name, "rna_type")==0) continue;
165
166                 buf= RNA_property_as_string(op->ptr, prop);
167                 
168                 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
169
170                 MEM_freeN(buf);
171                 first_iter = 0;
172         }
173
174         RNA_property_collection_end(&iter);
175
176         BLI_dynstr_append(dynstr, ")");
177
178         cstring = BLI_dynstr_get_cstring(dynstr);
179         BLI_dynstr_free(dynstr);
180         return cstring;
181 }
182
183 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
184 {
185         wmOperatorType *ot= WM_operatortype_find(opstring);
186
187         if(ot)
188                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
189         else
190                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
191 }
192
193 void WM_operator_properties_free(PointerRNA *ptr)
194 {
195         IDProperty *properties= ptr->data;
196
197         if(properties) {
198                 IDP_FreeProperty(properties);
199                 MEM_freeN(properties);
200         }
201 }
202
203 /* ************ default op callbacks, exported *********** */
204
205 /* invoke callback, uses enum property named "type" */
206 /* only weak thing is the fixed property name... */
207 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
208 {
209         PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
210         uiPopupMenu *pup;
211         uiLayout *layout;
212
213         if(prop==NULL) {
214                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
215         }
216         else if (RNA_property_type(prop) != PROP_ENUM) {
217                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
218         }
219         else {
220                 pup= uiPupMenuBegin(C, op->type->name, 0);
221                 layout= uiPupMenuLayout(pup);
222                 uiItemsEnumO(layout, op->type->idname, "type");
223                 uiPupMenuEnd(C, pup);
224         }
225
226         return OPERATOR_CANCELLED;
227 }
228
229 /* op->invoke */
230 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
231 {
232         uiPopupMenu *pup;
233         uiLayout *layout;
234
235         pup= uiPupMenuBegin(C, "OK?", ICON_HELP);
236         layout= uiPupMenuLayout(pup);
237         uiItemO(layout, NULL, 0, op->type->idname);
238         uiPupMenuEnd(C, pup);
239         
240         return OPERATOR_CANCELLED;
241 }
242
243 /* op->invoke, opens fileselect if filename property not set, otherwise executes */
244 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
245 {
246         if (RNA_property_is_set(op->ptr, "filename")) {
247                 return WM_operator_call(C, op);
248         } 
249         else {
250                 WM_event_add_fileselect(C, op);
251                 return OPERATOR_RUNNING_MODAL;
252         }
253 }
254
255 /* op->poll */
256 int WM_operator_winactive(bContext *C)
257 {
258         if(CTX_wm_window(C)==NULL) return 0;
259         return 1;
260 }
261
262 /* op->invoke */
263 static void redo_cb(bContext *C, void *arg_op, void *arg2)
264 {
265         wmOperator *lastop= arg_op;
266         
267         if(lastop) {
268                 ED_undo_pop(C);
269                 WM_operator_repeat(C, lastop);
270         }
271 }
272
273 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
274 {
275         wmWindowManager *wm= CTX_wm_manager(C);
276         wmOperator *op= arg_op;
277         PointerRNA ptr;
278         uiBlock *block;
279         uiLayout *layout;
280         uiStyle *style= U.uistyles.first;
281         
282         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
283         uiBlockClearFlag(block, UI_BLOCK_LOOP);
284         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
285         uiBlockSetFunc(block, redo_cb, arg_op, NULL);
286
287         if(!op->properties) {
288                 IDPropertyTemplate val = {0};
289                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
290         }
291
292         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
293         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
294         uiDefAutoButsRNA(C, layout, &ptr);
295
296         uiPopupBoundsBlock(block, 4.0f, 0, 0);
297         uiEndBlock(C, block);
298
299         return block;
300 }
301
302 int WM_operator_redo(bContext *C, wmOperator *op, wmEvent *event)
303 {
304         int retval= OPERATOR_CANCELLED;
305         
306         if(op->type->exec)
307                 retval= op->type->exec(C, op);
308
309         if(retval != OPERATOR_CANCELLED)
310                 uiPupBlock(C, wm_block_create_redo, op);
311
312         return retval;
313 }
314
315 int WM_operator_redo_popup(bContext *C, wmOperator *op)
316 {
317         uiPupBlock(C, wm_block_create_redo, op);
318
319         return OPERATOR_CANCELLED;
320 }
321
322 /* ************ window / screen operator definitions ************** */
323
324 static void WM_OT_window_duplicate(wmOperatorType *ot)
325 {
326         ot->name= "Duplicate Window";
327         ot->idname= "WM_OT_window_duplicate";
328         
329         ot->invoke= WM_operator_confirm;
330         ot->exec= wm_window_duplicate_op;
331         ot->poll= WM_operator_winactive;
332 }
333
334 static void WM_OT_save_homefile(wmOperatorType *ot)
335 {
336         ot->name= "Save User Settings";
337         ot->idname= "WM_OT_save_homefile";
338         
339         ot->invoke= WM_operator_confirm;
340         ot->exec= WM_write_homefile;
341         ot->poll= WM_operator_winactive;
342 }
343
344 static void WM_OT_read_homefile(wmOperatorType *ot)
345 {
346         ot->name= "Reload Start-Up File";
347         ot->idname= "WM_OT_read_homefile";
348         
349         ot->invoke= WM_operator_confirm;
350         ot->exec= WM_read_homefile;
351         ot->poll= WM_operator_winactive;
352         
353         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
354 }
355
356
357 /* ********* recent file *********** */
358
359 static int recentfile_exec(bContext *C, wmOperator *op)
360 {
361         int event= RNA_enum_get(op->ptr, "nr");
362
363         // XXX wm in context is not set correctly after WM_read_file -> crash
364         // do it before for now, but is this correct with multiple windows?
365
366         if(event>0) {
367                 if (G.sce[0] && (event==1)) {
368                         WM_event_add_notifier(C, NC_WINDOW, NULL);
369                         WM_read_file(C, G.sce, op->reports);
370                 }
371                 else {
372                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
373                         if(recent) {
374                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
375                                 WM_read_file(C, recent->filename, op->reports);
376                         }
377                 }
378         }
379         return 0;
380 }
381
382 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
383 {
384         struct RecentFile *recent;
385         uiPopupMenu *pup;
386         uiLayout *layout;
387         int i, ofs= 0;
388
389         pup= uiPupMenuBegin(C, "Open Recent", 0);
390         layout= uiPupMenuLayout(pup);
391
392         if(G.sce[0]) {
393                 uiItemIntO(layout, G.sce, 0, op->type->idname, "nr", 1);
394                 ofs = 1;
395         }
396         
397         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++)
398                 if(strcmp(recent->filename, G.sce))
399                         uiItemIntO(layout, recent->filename, 0, op->type->idname, "nr", i+ofs+1);
400
401         uiPupMenuEnd(C, pup);
402         
403         return OPERATOR_CANCELLED;
404 }
405
406 static void WM_OT_open_recentfile(wmOperatorType *ot)
407 {
408         ot->name= "Open Recent File";
409         ot->idname= "WM_OT_open_recentfile";
410         
411         ot->invoke= wm_recentfile_invoke;
412         ot->exec= recentfile_exec;
413         ot->poll= WM_operator_winactive;
414         
415         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
416 }
417
418 /* ********* main file *********** */
419
420 static void untitled(char *name)
421 {
422         if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
423                 char *c= BLI_last_slash(name);
424                 
425                 if (c)
426                         strcpy(&c[1], "untitled.blend");
427                 else
428                         strcpy(name, "untitled.blend");
429         }
430 }
431
432
433 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
434 {
435
436         RNA_string_set(op->ptr, "filename", G.sce);
437         WM_event_add_fileselect(C, op);
438
439         return OPERATOR_RUNNING_MODAL;
440 }
441
442 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
443 {
444         char filename[FILE_MAX];
445         RNA_string_get(op->ptr, "filename", filename);
446         
447         // XXX wm in context is not set correctly after WM_read_file -> crash
448         // do it before for now, but is this correct with multiple windows?
449         WM_event_add_notifier(C, NC_WINDOW, NULL);
450
451         WM_read_file(C, filename, op->reports);
452         
453         return 0;
454 }
455
456 static void WM_OT_open_mainfile(wmOperatorType *ot)
457 {
458         ot->name= "Open Blender File";
459         ot->idname= "WM_OT_open_mainfile";
460         
461         ot->invoke= wm_open_mainfile_invoke;
462         ot->exec= wm_open_mainfile_exec;
463         ot->poll= WM_operator_winactive;
464         
465         ot->flag= 0;
466         
467         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
468
469 }
470
471 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
472 {
473         char name[FILE_MAX];
474         
475         BLI_strncpy(name, G.sce, FILE_MAX);
476         untitled(name);
477         RNA_string_set(op->ptr, "filename", name);
478         
479         WM_event_add_fileselect(C, op);
480
481         return OPERATOR_RUNNING_MODAL;
482 }
483
484 /* function used for WM_OT_save_mainfile too */
485 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
486 {
487         char filename[FILE_MAX];
488         
489         if(RNA_property_is_set(op->ptr, "filename"))
490                 RNA_string_get(op->ptr, "filename", filename);
491         else {
492                 BLI_strncpy(filename, G.sce, FILE_MAX);
493                 untitled(filename);
494         }
495         WM_write_file(C, filename, op->reports);
496         
497         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
498
499         return 0;
500 }
501
502 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
503 {
504         ot->name= "Save As Blender File";
505         ot->idname= "WM_OT_save_as_mainfile";
506         
507         ot->invoke= wm_save_as_mainfile_invoke;
508         ot->exec= wm_save_as_mainfile_exec;
509         ot->poll= WM_operator_winactive;
510         
511         ot->flag= 0;
512         
513         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
514
515 }
516
517 /* *************** Save file directly ******** */
518
519 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
520 {
521         char name[FILE_MAX];
522         
523         BLI_strncpy(name, G.sce, FILE_MAX);
524         untitled(name);
525         RNA_string_set(op->ptr, "filename", name);
526         uiPupMenuSaveOver(C, op, name);
527
528         return OPERATOR_RUNNING_MODAL;
529 }
530
531 static void WM_OT_save_mainfile(wmOperatorType *ot)
532 {
533         ot->name= "Save Blender File";
534         ot->idname= "WM_OT_save_mainfile";
535         
536         ot->invoke= wm_save_mainfile_invoke;
537         ot->exec= wm_save_as_mainfile_exec;
538         ot->poll= WM_operator_winactive;
539         
540         ot->flag= 0;
541         
542         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
543         
544 }
545
546
547 /* *********************** */
548
549 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
550 {
551     ot->name= "Toggle Fullscreen";
552     ot->idname= "WM_OT_window_fullscreen_toggle";
553
554     ot->invoke= WM_operator_confirm;
555     ot->exec= wm_window_fullscreen_toggle_op;
556     ot->poll= WM_operator_winactive;
557 }
558
559 static int wm_exit_blender_op(bContext *C, wmOperator *op)
560 {
561         WM_operator_free(op);
562         
563         WM_exit(C);     
564         
565         return OPERATOR_FINISHED;
566 }
567
568 static void WM_OT_exit_blender(wmOperatorType *ot)
569 {
570         ot->name= "Exit Blender";
571         ot->idname= "WM_OT_exit_blender";
572
573         ot->invoke= WM_operator_confirm;
574         ot->exec= wm_exit_blender_op;
575         ot->poll= WM_operator_winactive;
576 }
577
578 /* ************ default paint cursors, draw always around cursor *********** */
579 /*
580  - returns handler to free 
581  - poll(bContext): returns 1 if draw should happen
582  - draw(bContext): drawing callback for paint cursor
583 */
584
585 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
586                                void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
587 {
588         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
589         
590         BLI_addtail(&wm->paintcursors, pc);
591         
592         pc->customdata = customdata;
593         pc->poll= poll;
594         pc->draw= draw;
595         
596         return pc;
597 }
598
599 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
600 {
601         wmPaintCursor *pc;
602         
603         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
604                 if(pc == (wmPaintCursor *)handle) {
605                         BLI_remlink(&wm->paintcursors, pc);
606                         MEM_freeN(pc);
607                         return;
608                 }
609         }
610 }
611
612 /* ************ window gesture operator-callback definitions ************** */
613 /*
614  * These are default callbacks for use in operators requiring gesture input
615  */
616
617 /* **************** Border gesture *************** */
618
619 /* Border gesture has two types:
620    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
621    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
622
623    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
624 */
625
626 static int border_apply(bContext *C, wmOperator *op, int event_type)
627 {
628         wmGesture *gesture= op->customdata;
629         rcti *rect= gesture->customdata;
630         
631         if(rect->xmin > rect->xmax)
632                 SWAP(int, rect->xmin, rect->xmax);
633         if(rect->ymin > rect->ymax)
634                 SWAP(int, rect->ymin, rect->ymax);
635         
636         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
637                 return 0;
638                 
639         /* operator arguments and storage. */
640         RNA_int_set(op->ptr, "xmin", rect->xmin);
641         RNA_int_set(op->ptr, "ymin", rect->ymin);
642         RNA_int_set(op->ptr, "xmax", rect->xmax);
643         RNA_int_set(op->ptr, "ymax", rect->ymax);
644         if( RNA_struct_find_property(op->ptr, "event_type") )
645                 RNA_int_set(op->ptr, "event_type", event_type);
646         
647         op->type->exec(C, op);
648         
649         return 1;
650 }
651
652 static void wm_gesture_end(bContext *C, wmOperator *op)
653 {
654         wmGesture *gesture= op->customdata;
655         
656         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
657         op->customdata= NULL;
658
659         ED_area_tag_redraw(CTX_wm_area(C));
660         
661         if( RNA_struct_find_property(op->ptr, "cursor") )
662                 WM_cursor_restore(CTX_wm_window(C));
663 }
664
665 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
666 {
667         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
668
669         /* add modal handler */
670         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
671         
672         wm_gesture_tag_redraw(C);
673
674         return OPERATOR_RUNNING_MODAL;
675 }
676
677 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
678 {
679         wmGesture *gesture= op->customdata;
680         rcti *rect= gesture->customdata;
681         int sx, sy;
682         
683         switch(event->type) {
684                 case MOUSEMOVE:
685                         
686                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
687                         
688                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
689                                 rect->xmin= rect->xmax= event->x - sx;
690                                 rect->ymin= rect->ymax= event->y - sy;
691                         }
692                         else {
693                                 rect->xmax= event->x - sx;
694                                 rect->ymax= event->y - sy;
695                         }
696                         
697                         wm_gesture_tag_redraw(C);
698
699                         break;
700                         
701                 case LEFTMOUSE:
702                 case MIDDLEMOUSE:
703                 case RIGHTMOUSE:
704                         if(event->val==1) {
705                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
706                                         gesture->mode= 1;
707                                         wm_gesture_tag_redraw(C);
708                                 }
709                         }
710                         else {
711                                 if(border_apply(C, op, event->type)) {
712                                         wm_gesture_end(C, op);
713                                         return OPERATOR_FINISHED;
714                                 }
715                                 wm_gesture_end(C, op);
716                                 return OPERATOR_CANCELLED;
717                         }
718                         break;
719                 case ESCKEY:
720                         wm_gesture_end(C, op);
721                         return OPERATOR_CANCELLED;
722         }
723         return OPERATOR_RUNNING_MODAL;
724 }
725
726 /* **************** circle gesture *************** */
727 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
728
729 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
730 {
731         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
732         
733         /* add modal handler */
734         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
735         
736         wm_gesture_tag_redraw(C);
737         
738         return OPERATOR_RUNNING_MODAL;
739 }
740
741 static void gesture_circle_apply(bContext *C, wmOperator *op)
742 {
743         wmGesture *gesture= op->customdata;
744         rcti *rect= gesture->customdata;
745         
746         /* operator arguments and storage. */
747         RNA_int_set(op->ptr, "x", rect->xmin);
748         RNA_int_set(op->ptr, "y", rect->ymin);
749         RNA_int_set(op->ptr, "radius", rect->xmax);
750         
751         if(op->type->exec)
752                 op->type->exec(C, op);
753 }
754
755 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
756 {
757         wmGesture *gesture= op->customdata;
758         rcti *rect= gesture->customdata;
759         int sx, sy;
760         
761         switch(event->type) {
762                 case MOUSEMOVE:
763                         
764                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
765                         
766                         rect->xmin= event->x - sx;
767                         rect->ymin= event->y - sy;
768                         
769                         wm_gesture_tag_redraw(C);
770                         
771                         if(gesture->mode)
772                                 gesture_circle_apply(C, op);
773
774                         break;
775                 case WHEELUPMOUSE:
776                         rect->xmax += 2 + rect->xmax/10;
777                         wm_gesture_tag_redraw(C);
778                         break;
779                 case WHEELDOWNMOUSE:
780                         rect->xmax -= 2 + rect->xmax/10;
781                         if(rect->xmax < 1) rect->xmax= 1;
782                         wm_gesture_tag_redraw(C);
783                         break;
784                 case LEFTMOUSE:
785                 case MIDDLEMOUSE:
786                 case RIGHTMOUSE:
787                         if(event->val==0) {     /* key release */
788                                 wm_gesture_end(C, op);
789                                 return OPERATOR_FINISHED;
790                         }
791                         else {
792                                 if( RNA_struct_find_property(op->ptr, "event_type") )
793                                         RNA_int_set(op->ptr, "event_type", event->type);
794                                 
795                                 /* apply first click */
796                                 gesture_circle_apply(C, op);
797                                 gesture->mode= 1;
798                         }
799                         break;
800                 case ESCKEY:
801                         wm_gesture_end(C, op);
802                         return OPERATOR_CANCELLED;
803         }
804         return OPERATOR_RUNNING_MODAL;
805 }
806
807 #if 0
808 /* template to copy from */
809 void WM_OT_circle_gesture(wmOperatorType *ot)
810 {
811         ot->name= "Circle Gesture";
812         ot->idname= "WM_OT_circle_gesture";
813         
814         ot->invoke= WM_gesture_circle_invoke;
815         ot->modal= WM_gesture_circle_modal;
816         
817         ot->poll= WM_operator_winactive;
818         
819         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
820         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
821         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
822
823 }
824 #endif
825
826 /* **************** Tweak gesture *************** */
827
828 static void tweak_gesture_modal(bContext *C, wmEvent *event)
829 {
830         wmWindow *window= CTX_wm_window(C);
831         wmGesture *gesture= window->tweak;
832         rcti *rect= gesture->customdata;
833         int sx, sy, val;
834         
835         switch(event->type) {
836                 case MOUSEMOVE:
837                         
838                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
839                         
840                         rect->xmax= event->x - sx;
841                         rect->ymax= event->y - sy;
842                         
843                         if((val= wm_gesture_evaluate(C, gesture))) {
844                                 wmEvent event;
845
846                                 event= *(window->eventstate);
847                                 if(gesture->event_type==LEFTMOUSE)
848                                         event.type= EVT_TWEAK_L;
849                                 else if(gesture->event_type==RIGHTMOUSE)
850                                         event.type= EVT_TWEAK_R;
851                                 else
852                                         event.type= EVT_TWEAK_M;
853                                 event.val= val;
854                                 /* mouse coords! */
855                                 wm_event_add(window, &event);
856                                 
857                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
858                                 window->tweak= NULL;
859                         }
860                         
861                         break;
862                         
863                 case LEFTMOUSE:
864                 case RIGHTMOUSE:
865                 case MIDDLEMOUSE:
866                         if(gesture->event_type==event->type) {
867                                 WM_gesture_end(C, gesture);
868                                 window->tweak= NULL;
869                                 
870                                 /* when tweak fails we should give the other keymap entries a chance */
871                                 event->val= KM_RELEASE;
872                         }
873                         break;
874                 default:
875                         WM_gesture_end(C, gesture);
876                         window->tweak= NULL;
877         }
878 }
879
880 /* standard tweak, called after window handlers passed on event */
881 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
882 {
883         wmWindow *win= CTX_wm_window(C);
884         
885         if(win->tweak==NULL) {
886                 if(CTX_wm_region(C)) {
887                         if(event->val) { // pressed
888                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
889                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
890                         }
891                 }
892         }
893         else {
894                 if(action==WM_HANDLER_BREAK) {
895                         WM_gesture_end(C, win->tweak);
896                         win->tweak= NULL;
897                 }
898                 else
899                         tweak_gesture_modal(C, event);
900         }
901 }
902
903 /* *********************** lasso gesture ****************** */
904
905 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
906 {
907         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
908         
909         /* add modal handler */
910         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
911         
912         wm_gesture_tag_redraw(C);
913         
914         if( RNA_struct_find_property(op->ptr, "cursor") )
915                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
916         
917         return OPERATOR_RUNNING_MODAL;
918 }
919
920 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
921 {
922         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
923         
924         /* add modal handler */
925         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
926         
927         wm_gesture_tag_redraw(C);
928         
929         if( RNA_struct_find_property(op->ptr, "cursor") )
930                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
931         
932         return OPERATOR_RUNNING_MODAL;
933 }
934
935
936 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
937 {
938         wmGesture *gesture= op->customdata;
939         PointerRNA itemptr;
940         float loc[2];
941         int i;
942         short *lasso= gesture->customdata;
943         
944         /* operator storage as path. */
945
946         for(i=0; i<gesture->points; i++, lasso+=2) {
947                 loc[0]= lasso[0];
948                 loc[1]= lasso[1];
949                 RNA_collection_add(op->ptr, "path", &itemptr);
950                 RNA_float_set_array(&itemptr, "loc", loc);
951         }
952         
953         wm_gesture_end(C, op);
954                 
955         if(op->type->exec)
956                 op->type->exec(C, op);
957         
958 }
959
960 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
961 {
962         wmGesture *gesture= op->customdata;
963         int sx, sy;
964         
965         switch(event->type) {
966                 case MOUSEMOVE:
967                         
968                         wm_gesture_tag_redraw(C);
969                         
970                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
971                         if(gesture->points < WM_LASSO_MAX_POINTS) {
972                                 short *lasso= gesture->customdata;
973                                 lasso += 2 * gesture->points;
974                                 lasso[0] = event->x - sx;
975                                 lasso[1] = event->y - sy;
976                                 gesture->points++;
977                         }
978                         else {
979                                 gesture_lasso_apply(C, op, event->type);
980                                 return OPERATOR_FINISHED;
981                         }
982                         break;
983                         
984                 case LEFTMOUSE:
985                 case MIDDLEMOUSE:
986                 case RIGHTMOUSE:
987                         if(event->val==0) {     /* key release */
988                                 gesture_lasso_apply(C, op, event->type);
989                                 return OPERATOR_FINISHED;
990                         }
991                         break;
992                 case ESCKEY:
993                         wm_gesture_end(C, op);
994                         return OPERATOR_CANCELLED;
995         }
996         return OPERATOR_RUNNING_MODAL;
997 }
998
999 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1000 {
1001         return WM_gesture_lasso_modal(C, op, event);
1002 }
1003
1004 #if 0
1005 /* template to copy from */
1006
1007 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1008 {
1009         RNA_BEGIN(op->ptr, itemptr, "path") {
1010                 float loc[2];
1011                 
1012                 RNA_float_get_array(&itemptr, "loc", loc);
1013                 printf("Location: %f %f\n", loc[0], loc[1]);
1014         }
1015         RNA_END;
1016         
1017         return OPERATOR_FINISHED;
1018 }
1019
1020 void WM_OT_lasso_gesture(wmOperatorType *ot)
1021 {
1022         PropertyRNA *prop;
1023         
1024         ot->name= "Lasso Gesture";
1025         ot->idname= "WM_OT_lasso_gesture";
1026         
1027         ot->invoke= WM_gesture_lasso_invoke;
1028         ot->modal= WM_gesture_lasso_modal;
1029         ot->exec= gesture_lasso_exec;
1030         
1031         ot->poll= WM_operator_winactive;
1032         
1033         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1034         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1035 }
1036 #endif
1037
1038 /* *********************** radial control ****************** */
1039
1040 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1041
1042 typedef struct wmRadialControl {
1043         int mode;
1044         float initial_value, value, max_value;
1045         int initial_mouse[2];
1046         void *cursor;
1047         GLuint tex;
1048 } wmRadialControl;
1049
1050 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1051 {
1052         wmRadialControl *rc = (wmRadialControl*)customdata;
1053         ARegion *ar = CTX_wm_region(C);
1054         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1055
1056         /* Keep cursor in the original place */
1057         x = rc->initial_mouse[0] - ar->winrct.xmin;
1058         y = rc->initial_mouse[1] - ar->winrct.ymin;
1059
1060         glPushMatrix();
1061         
1062         glTranslatef((float)x, (float)y, 0.0f);
1063
1064         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1065                 r1= rc->value;
1066                 r2= rc->initial_value;
1067                 r3= r1;
1068         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1069                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1070                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1071                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1072         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1073                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1074                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1075                 angle = rc->value;
1076         }
1077
1078         glColor4ub(255, 255, 255, 128);
1079         glEnable( GL_LINE_SMOOTH );
1080         glEnable(GL_BLEND);
1081
1082         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1083                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1084
1085         if(rc->tex) {
1086                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1087
1088                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1089                         glRotatef(angle, 0, 0, 1);
1090                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1091                 }
1092
1093                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1094
1095                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1096                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1097
1098                 glEnable(GL_TEXTURE_2D);
1099                 glBegin(GL_QUADS);
1100                 glColor4f(0,0,0, str);
1101                 glTexCoord2f(0,0);
1102                 glVertex2f(-r3, -r3);
1103                 glTexCoord2f(1,0);
1104                 glVertex2f(r3, -r3);
1105                 glTexCoord2f(1,1);
1106                 glVertex2f(r3, r3);
1107                 glTexCoord2f(0,1);
1108                 glVertex2f(-r3, r3);
1109                 glEnd();
1110                 glDisable(GL_TEXTURE_2D);
1111         }
1112
1113         glColor4ub(255, 255, 255, 128); 
1114         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1115         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1116         glDisable(GL_BLEND);
1117         glDisable( GL_LINE_SMOOTH );
1118         
1119         glPopMatrix();
1120 }
1121
1122 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1123 {
1124         wmRadialControl *rc = (wmRadialControl*)op->customdata;
1125         int mode, initial_mouse[2], delta[2];
1126         float dist;
1127         double new_value = RNA_float_get(op->ptr, "new_value");
1128         int ret = OPERATOR_RUNNING_MODAL;
1129
1130         mode = RNA_int_get(op->ptr, "mode");
1131         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1132
1133         switch(event->type) {
1134         case MOUSEMOVE:
1135                 delta[0]= initial_mouse[0] - event->x;
1136                 delta[1]= initial_mouse[1] - event->y;
1137                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1138
1139                 if(mode == WM_RADIALCONTROL_SIZE)
1140                         new_value = dist;
1141                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1142                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1143                 } else if(mode == WM_RADIALCONTROL_ANGLE)
1144                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1145                 
1146                 if(event->ctrl) {
1147                         if(mode == WM_RADIALCONTROL_STRENGTH)
1148                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1149                         else
1150                                 new_value = ((int)new_value + 5) / 10*10;
1151                 }
1152                 
1153                 break;
1154         case ESCKEY:
1155         case RIGHTMOUSE:
1156                 ret = OPERATOR_CANCELLED;
1157                 break;
1158         case LEFTMOUSE:
1159         case PADENTER:
1160                 op->type->exec(C, op);
1161                 ret = OPERATOR_FINISHED;
1162                 break;
1163         }
1164
1165         /* Clamp */
1166         if(new_value > rc->max_value)
1167                 new_value = rc->max_value;
1168         else if(new_value < 0)
1169                 new_value = 0;
1170
1171         /* Update paint data */
1172         rc->value = new_value;
1173
1174         RNA_float_set(op->ptr, "new_value", new_value);
1175
1176         if(ret != OPERATOR_RUNNING_MODAL) {
1177                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1178                 MEM_freeN(rc);
1179         }
1180         
1181         ED_region_tag_redraw(CTX_wm_region(C));
1182
1183         return ret;
1184 }
1185
1186 /* Expects the operator customdata to be an ImBuf (or NULL) */
1187 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1188 {
1189         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1190         int mode = RNA_int_get(op->ptr, "mode");
1191         float initial_value = RNA_float_get(op->ptr, "initial_value");
1192         int mouse[2] = {event->x, event->y};
1193
1194         if(mode == WM_RADIALCONTROL_SIZE) {
1195                 rc->max_value = 200;
1196                 mouse[0]-= initial_value;
1197         }
1198         else if(mode == WM_RADIALCONTROL_STRENGTH) {
1199                 rc->max_value = 1;
1200                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1201         }
1202         else if(mode == WM_RADIALCONTROL_ANGLE) {
1203                 rc->max_value = 360;
1204                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1205                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1206                 initial_value *= 180.0f/M_PI;
1207         }
1208
1209         if(op->customdata) {
1210                 ImBuf *im = (ImBuf*)op->customdata;
1211                 /* Build GL texture */
1212                 glGenTextures(1, &rc->tex);
1213                 glBindTexture(GL_TEXTURE_2D, rc->tex);
1214                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1215                 MEM_freeN(im->rect_float);
1216                 MEM_freeN(im);
1217         }
1218
1219         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1220         RNA_float_set(op->ptr, "new_value", initial_value);
1221                 
1222         op->customdata = rc;
1223         rc->mode = mode;
1224         rc->initial_value = initial_value;
1225         rc->initial_mouse[0] = mouse[0];
1226         rc->initial_mouse[1] = mouse[1];
1227         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1228                                               wm_radial_control_paint, op->customdata);
1229
1230         /* add modal handler */
1231         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1232         
1233         WM_radial_control_modal(C, op, event);
1234         
1235         return OPERATOR_RUNNING_MODAL;
1236 }
1237
1238 /* Gets a descriptive string of the operation */
1239 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1240 {
1241         int mode = RNA_int_get(op->ptr, "mode");
1242         float v = RNA_float_get(op->ptr, "new_value");
1243
1244         if(mode == WM_RADIALCONTROL_SIZE)
1245                 sprintf(str, "Size: %d", (int)v);
1246         else if(mode == WM_RADIALCONTROL_STRENGTH)
1247                 sprintf(str, "Strength: %d", (int)v);
1248         else if(mode == WM_RADIALCONTROL_ANGLE)
1249                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1250 }
1251
1252 /** Important: this doesn't define an actual operator, it
1253     just sets up the common parts of the radial control op. **/
1254 void WM_OT_radial_control_partial(wmOperatorType *ot)
1255 {
1256         static EnumPropertyItem prop_mode_items[] = {
1257                 {WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
1258                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
1259                 {WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
1260                 {0, NULL, NULL, NULL}};
1261
1262         /* Should be set in custom invoke() */
1263         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1264
1265         /* Set internally, should be used in custom exec() to get final value */
1266         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1267
1268         /* Should be set before calling operator */
1269         RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1270
1271         /* Internal */
1272         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1273 }
1274
1275 /* ************************** timer for testing ***************** */
1276
1277 /* uses no type defines, fully local testing function anyway... ;) */
1278
1279 static int ten_timer_exec(bContext *C, wmOperator *op)
1280 {
1281         ARegion *ar= CTX_wm_region(C);
1282         double stime= PIL_check_seconds_timer();
1283         int type = RNA_int_get(op->ptr, "type");
1284         int a, time;
1285         char tmpstr[128];
1286         
1287         WM_cursor_wait(1);
1288         
1289         for(a=0; a<10; a++) {
1290                 if (type==0) {
1291                         ED_region_do_draw(C, ar);
1292                 } 
1293                 else if (type==1) {
1294                         wmWindow *win= CTX_wm_window(C);
1295                         
1296                         ED_region_tag_redraw(ar);
1297                         wm_draw_update(C);
1298                         
1299                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1300                 }
1301                 else if (type==2) {
1302                         wmWindow *win= CTX_wm_window(C);
1303                         ScrArea *sa;
1304                         
1305                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1306                                 ED_area_tag_redraw(sa);
1307                         wm_draw_update(C);
1308                         
1309                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
1310                 }
1311                 else if (type==3) {
1312                         Scene *scene= CTX_data_scene(C);
1313                         
1314                         if(a & 1) scene->r.cfra--;
1315                         else scene->r.cfra++;
1316                         scene_update_for_newframe(scene, scene->lay);
1317                 }
1318                 else {
1319                         ED_undo_pop(C);
1320                         ED_undo_redo(C);
1321                 }
1322         }
1323         
1324         time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1325         
1326         if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1327         if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1328         if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1329         if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1330         if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1331         
1332         WM_cursor_wait(0);
1333         
1334         uiPupMenuNotice(C, tmpstr);
1335         
1336         return OPERATOR_FINISHED;
1337 }
1338
1339 static void WM_OT_ten_timer(wmOperatorType *ot)
1340 {
1341         static EnumPropertyItem prop_type_items[] = {
1342         {0, "DRAW", "Draw Region", ""},
1343         {1, "DRAWSWAP", "Draw Region + Swap", ""},
1344         {2, "DRAWWINSWAP", "Draw Window + Swap", ""},
1345         {3, "ANIMSTEP", "Anim Step", ""},
1346         {4, "UNDO", "Undo/Redo", ""},
1347         {0, NULL, NULL, NULL}};
1348         
1349         ot->name= "Ten Timer";
1350         ot->idname= "WM_OT_ten_timer";
1351         
1352         ot->invoke= WM_menu_invoke;
1353         ot->exec= ten_timer_exec;
1354         ot->poll= WM_operator_winactive;
1355         
1356         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1357
1358 }
1359
1360
1361
1362 /* ******************************************************* */
1363  
1364 /* called on initialize WM_exit() */
1365 void wm_operatortype_free(void)
1366 {
1367         BLI_freelistN(&global_ops);
1368 }
1369
1370 /* called on initialize WM_init() */
1371 void wm_operatortype_init(void)
1372 {
1373         WM_operatortype_append(WM_OT_window_duplicate);
1374         WM_operatortype_append(WM_OT_read_homefile);
1375         WM_operatortype_append(WM_OT_save_homefile);
1376         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1377         WM_operatortype_append(WM_OT_exit_blender);
1378         WM_operatortype_append(WM_OT_open_recentfile);
1379         WM_operatortype_append(WM_OT_open_mainfile);
1380         WM_operatortype_append(WM_OT_jobs_timer);
1381         WM_operatortype_append(WM_OT_save_as_mainfile);
1382         WM_operatortype_append(WM_OT_save_mainfile);
1383         WM_operatortype_append(WM_OT_ten_timer);
1384 }
1385
1386 /* default keymap for windows and screens, only call once per WM */
1387 void wm_window_keymap(wmWindowManager *wm)
1388 {
1389         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1390         
1391         /* items to make WM work */
1392         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1393         
1394         /* note, this doesn't replace existing keymap items */
1395         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1396         WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1397         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1398         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
1399         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1400         WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1401         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1402         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
1403         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1404
1405         WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1406 }
1407