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