2.5
[blender-staging.git] / source / blender / windowmanager / intern / wm_operators.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30
31 #include "DNA_ID.h"
32 #include "DNA_screen_types.h"
33 #include "DNA_userdef_types.h"
34 #include "DNA_windowmanager_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_dynstr.h" /*for WM_operator_pystring */
40
41 #include "BKE_blender.h"
42 #include "BKE_context.h"
43 #include "BKE_idprop.h"
44 #include "BKE_library.h"
45 #include "BKE_global.h"
46 #include "BKE_main.h"
47 #include "BKE_utildefines.h"
48
49 #include "ED_fileselect.h"
50 #include "ED_screen.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "wm.h"
62 #include "wm_window.h"
63 #include "wm_subwindow.h"
64 #include "wm_event_system.h"
65
66
67
68 static ListBase global_ops= {NULL, NULL};
69
70 /* ************ operator API, exported ********** */
71
72 wmOperatorType *WM_operatortype_find(const char *idname)
73 {
74         wmOperatorType *ot;
75         
76         for(ot= global_ops.first; ot; ot= ot->next) {
77                 if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
78                    return ot;
79         }
80         printf("search for unknown operator %s\n", idname);
81         return NULL;
82 }
83
84 wmOperatorType *WM_operatortype_first(void)
85 {
86         return global_ops.first;
87 }
88
89 /* all ops in 1 list (for time being... needs evaluation later) */
90 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
91 {
92         wmOperatorType *ot;
93         
94         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
95         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
96         opfunc(ot);
97         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
98         RNA_def_struct_identifier(ot->srna, ot->idname);
99         BLI_addtail(&global_ops, ot);
100 }
101
102 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
103 {
104         wmOperatorType *ot;
105
106         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
107         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
108         opfunc(ot, userdata);
109         RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
110         RNA_def_struct_identifier(ot->srna, ot->idname);
111         BLI_addtail(&global_ops, ot);
112 }
113
114 int WM_operatortype_remove(const char *idname)
115 {
116         wmOperatorType *ot = WM_operatortype_find(idname);
117
118         if (ot==NULL)
119                 return 0;
120         
121         BLI_remlink(&global_ops, ot);
122         RNA_struct_free(&BLENDER_RNA, ot->srna);
123         MEM_freeN(ot);
124
125         return 1;
126 }
127
128 /* print a string representation of the operator, with the args that it runs 
129  * so python can run it again */
130 char *WM_operator_pystring(wmOperator *op)
131 {
132         const char *arg_name= NULL;
133
134         PropertyRNA *prop, *iterprop;
135         CollectionPropertyIterator iter;
136
137         /* for building the string */
138         DynStr *dynstr= BLI_dynstr_new();
139         char *cstring, *buf;
140         int first_iter=1;
141
142         BLI_dynstr_appendf(dynstr, "%s(", op->idname);
143
144         iterprop= RNA_struct_iterator_property(op->ptr);
145         RNA_property_collection_begin(op->ptr, iterprop, &iter);
146
147         for(; iter.valid; RNA_property_collection_next(&iter)) {
148                 prop= iter.ptr.data;
149                 arg_name= RNA_property_identifier(&iter.ptr, prop);
150
151                 if (strcmp(arg_name, "rna_type")==0) continue;
152
153                 buf= RNA_property_as_string(op->ptr, prop);
154                 
155                 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
156
157                 MEM_freeN(buf);
158                 first_iter = 0;
159         }
160
161         RNA_property_collection_end(&iter);
162
163         BLI_dynstr_append(dynstr, ")");
164
165         cstring = BLI_dynstr_get_cstring(dynstr);
166         BLI_dynstr_free(dynstr);
167         return cstring;
168 }
169
170 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
171 {
172         wmOperatorType *ot= WM_operatortype_find(opstring);
173
174         if(ot)
175                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
176         else
177                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
178 }
179
180 void WM_operator_properties_free(PointerRNA *ptr)
181 {
182         IDProperty *properties= ptr->data;
183
184         if(properties) {
185                 IDP_FreeProperty(properties);
186                 MEM_freeN(properties);
187         }
188 }
189
190 /* ************ default op callbacks, exported *********** */
191
192 /* invoke callback, uses enum property named "type" */
193 /* only weak thing is the fixed property name... */
194 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
195 {
196         PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
197         const EnumPropertyItem *item;
198         int totitem, i, len= strlen(op->type->name) + 8;
199         char *menu, *p;
200         
201         if(prop) {
202                 RNA_property_enum_items(op->ptr, prop, &item, &totitem);
203                 
204                 for (i=0; i<totitem; i++)
205                         len+= strlen(item[i].name) + 8;
206                 
207                 menu= MEM_callocN(len, "string");
208                 
209                 p= menu + sprintf(menu, "%s %%t", op->type->name);
210                 for (i=0; i<totitem; i++)
211                         p+= sprintf(p, "|%s %%x%d", item[i].name, item[i].value);
212                 
213                 uiPupmenuOperator(C, totitem/30, op, "type", menu);
214                 MEM_freeN(menu);
215                 
216                 return OPERATOR_RUNNING_MODAL;
217         }
218         return OPERATOR_CANCELLED;
219 }
220
221 /* op->invoke */
222 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
223 {
224         char buf[512];
225         
226         sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
227         uiPupmenuOperator(C, 0, op, NULL, buf);
228         
229         return OPERATOR_RUNNING_MODAL;
230 }
231
232 /* op->poll */
233 int WM_operator_winactive(bContext *C)
234 {
235         if(CTX_wm_window(C)==NULL) return 0;
236         return 1;
237 }
238
239 /* ************ window / screen operator definitions ************** */
240
241 static void WM_OT_window_duplicate(wmOperatorType *ot)
242 {
243         ot->name= "Duplicate Window";
244         ot->idname= "WM_OT_window_duplicate";
245         
246         ot->invoke= WM_operator_confirm;
247         ot->exec= wm_window_duplicate_op;
248         ot->poll= WM_operator_winactive;
249 }
250
251 static void WM_OT_save_homefile(wmOperatorType *ot)
252 {
253         ot->name= "Save User Settings";
254         ot->idname= "WM_OT_save_homefile";
255         
256         ot->invoke= WM_operator_confirm;
257         ot->exec= WM_write_homefile;
258         ot->poll= WM_operator_winactive;
259         
260         ot->flag= OPTYPE_REGISTER;
261 }
262
263 /* ********* recent file *********** */
264
265 static void recent_filelist(char *pup)
266 {
267         struct RecentFile *recent;
268         int i, ofs= 0;
269         char *p;
270         
271         p= pup + sprintf(pup, "Open Recent%%t");
272         
273         if (G.sce[0]) {
274                 p+= sprintf(p, "|%s %%x%d", G.sce, 1);
275                 ofs = 1;
276         }
277         
278         for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
279                 if (strcmp(recent->filename, G.sce)) {
280                         p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
281                 }
282         }
283 }
284
285 static int recentfile_exec(bContext *C, wmOperator *op)
286 {
287         int event= RNA_enum_get(op->ptr, "nr");
288         
289         if(event>0) {
290                 if (G.sce[0] && (event==1))
291                         WM_read_file(C, G.sce, op->reports);
292                 else {
293                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
294                         if(recent) {
295                                 WM_read_file(C, recent->filename, op->reports);
296                         }
297                 }
298         }
299         return 0;
300 }
301
302 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
303 {
304         char pup[2048];
305         
306         recent_filelist(pup);
307         uiPupmenuOperator(C, 0, op, "nr", pup);
308         
309         return OPERATOR_RUNNING_MODAL;
310 }
311
312 static void WM_OT_open_recentfile(wmOperatorType *ot)
313 {
314         ot->name= "Open Recent File";
315         ot->idname= "WM_OT_open_recentfile";
316         
317         ot->invoke= wm_recentfile_invoke;
318         ot->exec= recentfile_exec;
319         ot->poll= WM_operator_winactive;
320         
321         ot->flag= OPTYPE_REGISTER;
322         
323         RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
324
325 }
326
327 /* ********* main file *********** */
328
329 static int wm_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
330 {
331         SpaceFile *sfile;
332         
333         ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
334
335         /* settings for filebrowser */
336         sfile= (SpaceFile*)CTX_wm_space_data(C);
337         sfile->op = op;
338         ED_fileselect_set_params(sfile, FILE_BLENDER, "Load", "C:\\", 0, 0, 0);
339
340         /* screen and area have been reset already in ED_screen_full_newspace */
341
342         return OPERATOR_RUNNING_MODAL;
343 }
344
345 static int wm_mainfile_exec(bContext *C, wmOperator *op)
346 {
347         char filename[FILE_MAX];
348         RNA_string_get(op->ptr, "filename", filename);
349         
350         WM_read_file(C, filename, op->reports);
351         
352         // XXX wm in context is not set correctly after WM_read_file -> crash
353         // WM_event_add_notifier(C, NC_WINDOW, NULL);
354
355         return 0;
356 }
357
358 static void WM_OT_open_mainfile(wmOperatorType *ot)
359 {
360         ot->name= "Open Blender File";
361         ot->idname= "WM_OT_open_mainfile";
362         
363         ot->invoke= wm_mainfile_invoke;
364         ot->exec= wm_mainfile_exec;
365         ot->poll= WM_operator_winactive;
366         
367         ot->flag= 0;
368         
369         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
370
371 }
372
373 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
374 {
375         SpaceFile *sfile;
376         
377         ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
378
379         /* settings for filebrowser */
380         sfile= (SpaceFile*)CTX_wm_space_data(C);
381         sfile->op = op;
382         // XXX replace G.sce
383         ED_fileselect_set_params(sfile, FILE_BLENDER, "Save As", G.sce, 0, 0, 0);
384
385         /* screen and area have been reset already in ED_screen_full_newspace */
386
387         return OPERATOR_RUNNING_MODAL;
388 }
389
390 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
391 {
392         char filename[FILE_MAX];
393         RNA_string_get(op->ptr, "filename", filename);
394         
395         WM_write_file(C, filename, op->reports);
396         
397         WM_event_add_notifier(C, NC_WINDOW, NULL);
398
399         return 0;
400 }
401
402 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
403 {
404         ot->name= "Open Blender File";
405         ot->idname= "WM_OT_save_as_mainfile";
406         
407         ot->invoke= wm_save_as_mainfile_invoke;
408         ot->exec= wm_save_as_mainfile_exec;
409         ot->poll= WM_operator_winactive;
410         
411         ot->flag= 0;
412         
413         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
414
415 }
416
417 /* *********************** */
418
419 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
420 {
421     ot->name= "Toggle Fullscreen";
422     ot->idname= "WM_OT_window_fullscreen_toggle";
423
424     ot->invoke= WM_operator_confirm;
425     ot->exec= wm_window_fullscreen_toggle_op;
426     ot->poll= WM_operator_winactive;
427 }
428
429 static void WM_OT_exit_blender(wmOperatorType *ot)
430 {
431         ot->name= "Exit Blender";
432         ot->idname= "WM_OT_exit_blender";
433
434         ot->invoke= WM_operator_confirm;
435         ot->exec= wm_exit_blender_op;
436         ot->poll= WM_operator_winactive;
437 }
438
439 /* ************ default paint cursors, draw always around cursor *********** */
440 /*
441  - returns handler to free 
442  - poll(bContext): returns 1 if draw should happen
443  - draw(bContext): drawing callback for paint cursor
444 */
445
446 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C), void (*draw)(bContext *C, int, int))
447 {
448         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
449         
450         BLI_addtail(&wm->paintcursors, pc);
451         
452         pc->poll= poll;
453         pc->draw= draw;
454         
455         return pc;
456 }
457
458 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
459 {
460         wmPaintCursor *pc;
461         
462         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
463                 if(pc == (wmPaintCursor *)handle) {
464                         BLI_remlink(&wm->paintcursors, pc);
465                         MEM_freeN(pc);
466                         return;
467                 }
468         }
469 }
470
471 /* ************ window gesture operator-callback definitions ************** */
472 /*
473  * These are default callbacks for use in operators requiring gesture input
474  */
475
476 /* **************** Border gesture *************** */
477
478 /* Border gesture has two types:
479    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
480    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
481
482    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
483 */
484
485 static void border_apply(bContext *C, wmOperator *op, int event_type)
486 {
487         wmGesture *gesture= op->customdata;
488         rcti *rect= gesture->customdata;
489         
490         if(rect->xmin > rect->xmax)
491                 SWAP(int, rect->xmin, rect->xmax);
492         if(rect->ymin > rect->ymax)
493                 SWAP(int, rect->ymin, rect->ymax);
494         
495         /* operator arguments and storage. */
496         RNA_int_set(op->ptr, "xmin", rect->xmin);
497         RNA_int_set(op->ptr, "ymin", rect->ymin);
498         RNA_int_set(op->ptr, "xmax", rect->xmax);
499         RNA_int_set(op->ptr, "ymax", rect->ymax);
500         if( RNA_struct_find_property(op->ptr, "event_type") )
501                 RNA_int_set(op->ptr, "event_type", event_type);
502         
503         op->type->exec(C, op);
504 }
505
506 static void wm_gesture_end(bContext *C, wmOperator *op)
507 {
508         wmGesture *gesture= op->customdata;
509         
510         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
511         op->customdata= NULL;
512
513         ED_area_tag_redraw(CTX_wm_area(C));
514         
515 }
516
517 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
518 {
519         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
520
521         /* add modal handler */
522         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
523         
524         wm_gesture_tag_redraw(C);
525
526         return OPERATOR_RUNNING_MODAL;
527 }
528
529 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
530 {
531         wmGesture *gesture= op->customdata;
532         rcti *rect= gesture->customdata;
533         int sx, sy;
534         
535         switch(event->type) {
536                 case MOUSEMOVE:
537                         
538                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
539                         
540                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
541                                 rect->xmin= rect->xmax= event->x - sx;
542                                 rect->ymin= rect->ymax= event->y - sy;
543                         }
544                         else {
545                                 rect->xmax= event->x - sx;
546                                 rect->ymax= event->y - sy;
547                         }
548                         
549                         wm_gesture_tag_redraw(C);
550
551                         break;
552                         
553                 case LEFTMOUSE:
554                 case MIDDLEMOUSE:
555                 case RIGHTMOUSE:
556                         if(event->val==1) {
557                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
558                                         gesture->mode= 1;
559                                         wm_gesture_tag_redraw(C);
560                                 }
561                         }
562                         else {
563                                 border_apply(C, op, event->type);
564                                 wm_gesture_end(C, op);
565                                 return OPERATOR_FINISHED;
566                         }
567                         break;
568                 case ESCKEY:
569                         wm_gesture_end(C, op);
570                         return OPERATOR_CANCELLED;
571         }
572         return OPERATOR_RUNNING_MODAL;
573 }
574
575 /* **************** circle gesture *************** */
576 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
577
578 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
579 {
580         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
581         
582         /* add modal handler */
583         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
584         
585         wm_gesture_tag_redraw(C);
586         
587         return OPERATOR_RUNNING_MODAL;
588 }
589
590 static void gesture_circle_apply(bContext *C, wmOperator *op, int event_type)
591 {
592         wmGesture *gesture= op->customdata;
593         rcti *rect= gesture->customdata;
594         
595         /* operator arguments and storage. */
596         RNA_int_set(op->ptr, "x", rect->xmin);
597         RNA_int_set(op->ptr, "y", rect->ymin);
598         RNA_int_set(op->ptr, "radius", rect->xmax);
599         if( RNA_struct_find_property(op->ptr, "event_type") )
600                 RNA_int_set(op->ptr, "event_type", event_type);
601         
602         if(op->type->exec)
603                 op->type->exec(C, op);
604 }
605
606 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
607 {
608         wmGesture *gesture= op->customdata;
609         rcti *rect= gesture->customdata;
610         int sx, sy;
611         
612         switch(event->type) {
613                 case MOUSEMOVE:
614                         
615                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
616                         
617                         rect->xmin= event->x - sx;
618                         rect->ymin= event->y - sy;
619                         
620                         wm_gesture_tag_redraw(C);
621                         
622                         if(gesture->mode)
623                                 gesture_circle_apply(C, op, event->type);
624
625                         break;
626                 case WHEELUPMOUSE:
627                         rect->xmax += 2 + rect->xmax/10;
628                         wm_gesture_tag_redraw(C);
629                         break;
630                 case WHEELDOWNMOUSE:
631                         rect->xmax -= 2 + rect->xmax/10;
632                         if(rect->xmax < 1) rect->xmax= 1;
633                         wm_gesture_tag_redraw(C);
634                         break;
635                 case LEFTMOUSE:
636                 case MIDDLEMOUSE:
637                 case RIGHTMOUSE:
638                         if(event->val==0) {     /* key release */
639                                 wm_gesture_end(C, op);
640                                 return OPERATOR_FINISHED;
641                         }
642                         else
643                                 gesture->mode= 1;
644
645                         break;
646                 case ESCKEY:
647                         wm_gesture_end(C, op);
648                         return OPERATOR_CANCELLED;
649         }
650         return OPERATOR_RUNNING_MODAL;
651 }
652
653 #if 0
654 /* template to copy from */
655 void WM_OT_circle_gesture(wmOperatorType *ot)
656 {
657         ot->name= "Circle Gesture";
658         ot->idname= "WM_OT_circle_gesture";
659         
660         ot->invoke= WM_gesture_circle_invoke;
661         ot->modal= WM_gesture_circle_modal;
662         
663         ot->poll= WM_operator_winactive;
664         
665         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
666         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
667         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
668
669 }
670 #endif
671
672 /* **************** Tweak gesture *************** */
673
674 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
675 {
676         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
677         
678         /* add modal handler */
679         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
680         
681         wm_gesture_tag_redraw(C);
682         
683         return OPERATOR_RUNNING_MODAL;
684 }
685
686 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
687 {
688         wmWindow *window= CTX_wm_window(C);
689         wmGesture *gesture= op->customdata;
690         rcti *rect= gesture->customdata;
691         int sx, sy, val;
692         
693         switch(event->type) {
694                 case MOUSEMOVE:
695                         
696                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
697                         
698                         rect->xmax= event->x - sx;
699                         rect->ymax= event->y - sy;
700                         
701                         if((val= wm_gesture_evaluate(C, gesture))) {
702                                 wmEvent event;
703                                         
704                                 event= *(window->eventstate);
705                                 if(gesture->event_type==LEFTMOUSE)
706                                         event.type= EVT_TWEAK_L;
707                                 else if(gesture->event_type==RIGHTMOUSE)
708                                         event.type= EVT_TWEAK_R;
709                                 else
710                                         event.type= EVT_TWEAK_M;
711                                 event.val= val;
712                                 /* mouse coords! */
713                                 wm_event_add(window, &event);
714                                 
715                                 wm_gesture_end(C, op);
716                                 return OPERATOR_FINISHED;
717                         }
718                         else
719                                 wm_gesture_tag_redraw(C);
720                         
721                         break;
722                         
723                 case LEFTMOUSE:
724                 case RIGHTMOUSE:
725                 case MIDDLEMOUSE:
726                         if(gesture->event_type==event->type) {
727                                 wm_gesture_evaluate(C, gesture);
728                                 wm_gesture_end(C, op);
729                                 return OPERATOR_FINISHED;
730                         }
731                         break;
732         }
733         return OPERATOR_RUNNING_MODAL;
734 }
735
736 void WM_OT_tweak_gesture(wmOperatorType *ot)
737 {
738         ot->name= "Tweak Gesture";
739         ot->idname= "WM_OT_tweak_gesture";
740         
741         ot->invoke= tweak_gesture_invoke;
742         ot->modal= tweak_gesture_modal;
743
744         ot->poll= WM_operator_winactive;
745 }
746
747 /* *********************** lasso gesture ****************** */
748
749 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
750 {
751         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
752         
753         /* add modal handler */
754         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
755         
756         wm_gesture_tag_redraw(C);
757         
758         return OPERATOR_RUNNING_MODAL;
759 }
760
761
762 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
763 {
764         wmGesture *gesture= op->customdata;
765         PointerRNA itemptr;
766         float loc[2];
767         int i;
768         short *lasso= gesture->customdata;
769         
770         /* operator storage as path. */
771
772         for(i=0; i<gesture->points; i++, lasso+=2) {
773                 loc[0]= lasso[0];
774                 loc[1]= lasso[1];
775                 RNA_collection_add(op->ptr, "path", &itemptr);
776                 RNA_float_set_array(&itemptr, "loc", loc);
777         }
778         
779         wm_gesture_end(C, op);
780                 
781         if(op->type->exec)
782                 op->type->exec(C, op);
783         
784 }
785
786 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
787 {
788         wmGesture *gesture= op->customdata;
789         int sx, sy;
790         
791         switch(event->type) {
792                 case MOUSEMOVE:
793                         
794                         wm_gesture_tag_redraw(C);
795                         
796                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
797                         if(gesture->points < WM_LASSO_MAX_POINTS) {
798                                 short *lasso= gesture->customdata;
799                                 lasso += 2 * gesture->points;
800                                 lasso[0] = event->x - sx;
801                                 lasso[1] = event->y - sy;
802                                 gesture->points++;
803                         }
804                         else {
805                                 gesture_lasso_apply(C, op, event->type);
806                                 return OPERATOR_FINISHED;
807                         }
808                         break;
809                         
810                 case LEFTMOUSE:
811                 case MIDDLEMOUSE:
812                 case RIGHTMOUSE:
813                         if(event->val==0) {     /* key release */
814                                 gesture_lasso_apply(C, op, event->type);
815                                 return OPERATOR_FINISHED;
816                         }
817                         break;
818                 case ESCKEY:
819                         wm_gesture_end(C, op);
820                         return OPERATOR_CANCELLED;
821         }
822         return OPERATOR_RUNNING_MODAL;
823 }
824
825 #if 0
826 /* template to copy from */
827
828 static int gesture_lasso_exec(bContext *C, wmOperator *op)
829 {
830         RNA_BEGIN(op->ptr, itemptr, "path") {
831                 float loc[2];
832                 
833                 RNA_float_get_array(&itemptr, "loc", loc);
834                 printf("Location: %f %f\n", loc[0], loc[1]);
835         }
836         RNA_END;
837         
838         return OPERATOR_FINISHED;
839 }
840
841 void WM_OT_lasso_gesture(wmOperatorType *ot)
842 {
843         PropertyRNA *prop;
844         
845         ot->name= "Lasso Gesture";
846         ot->idname= "WM_OT_lasso_gesture";
847         
848         ot->invoke= WM_gesture_lasso_invoke;
849         ot->modal= WM_gesture_lasso_modal;
850         ot->exec= gesture_lasso_exec;
851         
852         ot->poll= WM_operator_winactive;
853         
854         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
855         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
856 }
857 #endif
858
859 /* ******************************************************* */
860  
861 /* called on initialize WM_exit() */
862 void wm_operatortype_free(void)
863 {
864         BLI_freelistN(&global_ops);
865 }
866
867 /* called on initialize WM_init() */
868 void wm_operatortype_init(void)
869 {
870         WM_operatortype_append(WM_OT_window_duplicate);
871         WM_operatortype_append(WM_OT_save_homefile);
872         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
873         WM_operatortype_append(WM_OT_exit_blender);
874         WM_operatortype_append(WM_OT_tweak_gesture);
875         WM_operatortype_append(WM_OT_open_recentfile);
876         WM_operatortype_append(WM_OT_open_mainfile);
877         WM_operatortype_append(WM_OT_jobs_timer);
878         WM_operatortype_append(WM_OT_save_as_mainfile);
879 }
880
881 /* default keymap for windows and screens, only call once per WM */
882 void wm_window_keymap(wmWindowManager *wm)
883 {
884         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
885         
886         /* items to make WM work */
887         WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
888         
889         /* note, this doesn't replace existing keymap items */
890         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
891         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
892         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
893         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
894         WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
895         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
896         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
897 }
898