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