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