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, NULL, ot->srna, NULL, ptr);
176         else
177                 memset(ptr, 0, sizeof(*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) + 5;
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) + 5;
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         wmWindow *newwin, *win;
332         bScreen *newsc, *sc;
333         ScrArea *sa, *oldsa;
334         SpaceFile *sfile;
335
336         rcti rect;
337         
338         win= CTX_wm_window(C);
339         sc= CTX_wm_screen(C);
340         oldsa= CTX_wm_area(C);
341
342 #if 0 /* XXX experimental code for opening filebrowser in new window */
343         /*  poll() checks area context, but we don't accept full-area windows */
344         if(sc->full != SCREENNORMAL) 
345                 return OPERATOR_CANCELLED;
346         
347         /* adds window to WM */
348         rect.xmin = 0;
349         rect.ymin = 0;
350         rect.xmax = win->sizex;
351         rect.ymax = win->sizey;
352         BLI_translate_rcti(&rect, win->posx, win->posy);
353         newwin= WM_window_open(C, &rect);
354         
355         /* allocs new screen and adds to newly created window, using window size */
356         newsc= screen_add(newwin, CTX_data_scene(C), sc->id.name+2);
357         newwin->screen= newsc;
358
359         /* create filebrowser */
360         CTX_wm_window_set(C, newwin);
361         CTX_wm_screen_set(C, newsc);
362         sa= (ScrArea*)newsc->areabase.first;
363         CTX_wm_area_set(C, sa);
364         ED_area_newspace(C, sa, SPACE_FILE);
365 #else 
366         ED_area_newspace(C, oldsa, SPACE_FILE);
367 #endif
368
369         /* settings for filebrowser */
370         sfile= (SpaceFile*)CTX_wm_space_data(C);
371         sfile->op = op;
372
373         ED_fileselect_set_params(C, FILE_BLENDER, "Load", "C:\\", 0, 0, 0);
374
375         /* screen, areas init */
376         WM_event_add_notifier(C, NC_SCREEN|NA_EDITED, NULL);
377         
378         return OPERATOR_RUNNING_MODAL;
379 }
380
381 static int wm_mainfile_exec(bContext *C, wmOperator *op)
382 {
383         char filename[FILE_MAX];
384         RNA_string_get(op->ptr, "filename", filename);
385         
386         WM_read_file(C, filename, op->reports);
387         
388         WM_event_add_notifier(C, NC_WINDOW, NULL);
389
390         return 0;
391 }
392
393 static void WM_OT_open_mainfile(wmOperatorType *ot)
394 {
395         ot->name= "Open Blender File";
396         ot->idname= "WM_OT_open_mainfile";
397         
398         ot->invoke= wm_mainfile_invoke;
399         ot->exec= wm_mainfile_exec;
400         ot->poll= WM_operator_winactive;
401         
402         ot->flag= 0;
403         
404         RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
405
406 }
407
408 /* *********************** */
409
410 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
411 {
412     ot->name= "Toggle Fullscreen";
413     ot->idname= "WM_OT_window_fullscreen_toggle";
414
415     ot->invoke= WM_operator_confirm;
416     ot->exec= wm_window_fullscreen_toggle_op;
417     ot->poll= WM_operator_winactive;
418 }
419
420 static void WM_OT_exit_blender(wmOperatorType *ot)
421 {
422         ot->name= "Exit Blender";
423         ot->idname= "WM_OT_exit_blender";
424
425         ot->invoke= WM_operator_confirm;
426         ot->exec= wm_exit_blender_op;
427         ot->poll= WM_operator_winactive;
428 }
429
430 /* ************ window gesture operator-callback definitions ************** */
431 /*
432  * These are default callbacks for use in operators requiring gesture input
433  */
434
435 /* **************** Border gesture *************** */
436
437 /* Border gesture has two types:
438    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
439    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
440
441    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
442 */
443
444 static void border_apply(bContext *C, wmOperator *op, int event_type)
445 {
446         wmGesture *gesture= op->customdata;
447         rcti *rect= gesture->customdata;
448         
449         if(rect->xmin > rect->xmax)
450                 SWAP(int, rect->xmin, rect->xmax);
451         if(rect->ymin > rect->ymax)
452                 SWAP(int, rect->ymin, rect->ymax);
453         
454         /* operator arguments and storage. */
455         RNA_int_set(op->ptr, "xmin", rect->xmin);
456         RNA_int_set(op->ptr, "ymin", rect->ymin);
457         RNA_int_set(op->ptr, "xmax", rect->xmax);
458         RNA_int_set(op->ptr, "ymax", rect->ymax);
459         if( RNA_struct_find_property(op->ptr, "event_type") )
460                 RNA_int_set(op->ptr, "event_type", event_type);
461         
462         op->type->exec(C, op);
463 }
464
465 static void wm_gesture_end(bContext *C, wmOperator *op)
466 {
467         wmGesture *gesture= op->customdata;
468         
469         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
470         op->customdata= NULL;
471
472         ED_area_tag_redraw(CTX_wm_area(C));
473         
474 }
475
476 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
477 {
478         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
479
480         /* add modal handler */
481         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
482         
483         wm_gesture_tag_redraw(C);
484
485         return OPERATOR_RUNNING_MODAL;
486 }
487
488 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
489 {
490         wmGesture *gesture= op->customdata;
491         rcti *rect= gesture->customdata;
492         int sx, sy;
493         
494         switch(event->type) {
495                 case MOUSEMOVE:
496                         
497                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
498                         
499                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
500                                 rect->xmin= rect->xmax= event->x - sx;
501                                 rect->ymin= rect->ymax= event->y - sy;
502                         }
503                         else {
504                                 rect->xmax= event->x - sx;
505                                 rect->ymax= event->y - sy;
506                         }
507                         
508                         wm_gesture_tag_redraw(C);
509
510                         break;
511                         
512                 case LEFTMOUSE:
513                 case MIDDLEMOUSE:
514                 case RIGHTMOUSE:
515                         if(event->val==1) {
516                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
517                                         gesture->mode= 1;
518                                         wm_gesture_tag_redraw(C);
519                                 }
520                         }
521                         else {
522                                 border_apply(C, op, event->type);
523                                 wm_gesture_end(C, op);
524                                 return OPERATOR_FINISHED;
525                         }
526                         break;
527                 case ESCKEY:
528                         wm_gesture_end(C, op);
529                         return OPERATOR_CANCELLED;
530         }
531         return OPERATOR_RUNNING_MODAL;
532 }
533
534 /* **************** circle gesture *************** */
535 /* works now only for selection or modal paint stuff, calls exec while hold mouse */
536
537 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
538 {
539         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
540         
541         /* add modal handler */
542         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
543         
544         wm_gesture_tag_redraw(C);
545         
546         return OPERATOR_RUNNING_MODAL;
547 }
548
549 static void gesture_circle_apply(bContext *C, wmOperator *op, int event_type)
550 {
551         wmGesture *gesture= op->customdata;
552         rcti *rect= gesture->customdata;
553         
554         /* operator arguments and storage. */
555         RNA_int_set(op->ptr, "x", rect->xmin);
556         RNA_int_set(op->ptr, "y", rect->ymin);
557         RNA_int_set(op->ptr, "radius", rect->xmax);
558         if( RNA_struct_find_property(op->ptr, "event_type") )
559                 RNA_int_set(op->ptr, "event_type", event_type);
560         
561         if(op->type->exec)
562                 op->type->exec(C, op);
563 }
564
565 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
566 {
567         wmGesture *gesture= op->customdata;
568         rcti *rect= gesture->customdata;
569         int sx, sy;
570         
571         switch(event->type) {
572                 case MOUSEMOVE:
573                         
574                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
575                         
576                         rect->xmin= event->x - sx;
577                         rect->ymin= event->y - sy;
578                         
579                         wm_gesture_tag_redraw(C);
580                         
581                         if(gesture->mode)
582                                 gesture_circle_apply(C, op, event->type);
583
584                         break;
585                 case WHEELUPMOUSE:
586                         rect->xmax += 2 + rect->xmax/10;
587                         wm_gesture_tag_redraw(C);
588                         break;
589                 case WHEELDOWNMOUSE:
590                         rect->xmax -= 2 + rect->xmax/10;
591                         if(rect->xmax < 1) rect->xmax= 1;
592                         wm_gesture_tag_redraw(C);
593                         break;
594                 case LEFTMOUSE:
595                 case MIDDLEMOUSE:
596                 case RIGHTMOUSE:
597                         if(event->val==0) {     /* key release */
598                                 wm_gesture_end(C, op);
599                                 return OPERATOR_FINISHED;
600                         }
601                         else
602                                 gesture->mode= 1;
603
604                         break;
605                 case ESCKEY:
606                         wm_gesture_end(C, op);
607                         return OPERATOR_CANCELLED;
608         }
609         return OPERATOR_RUNNING_MODAL;
610 }
611
612 #if 0
613 /* template to copy from */
614 void WM_OT_circle_gesture(wmOperatorType *ot)
615 {
616         ot->name= "Circle Gesture";
617         ot->idname= "WM_OT_circle_gesture";
618         
619         ot->invoke= WM_gesture_circle_invoke;
620         ot->modal= WM_gesture_circle_modal;
621         
622         ot->poll= WM_operator_winactive;
623         
624         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
625         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
626         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
627
628 }
629 #endif
630
631 /* **************** Tweak gesture *************** */
632
633 static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
634 {
635         op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
636         
637         /* add modal handler */
638         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
639         
640         wm_gesture_tag_redraw(C);
641         
642         return OPERATOR_RUNNING_MODAL;
643 }
644
645 static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
646 {
647         wmWindow *window= CTX_wm_window(C);
648         wmGesture *gesture= op->customdata;
649         rcti *rect= gesture->customdata;
650         int sx, sy, val;
651         
652         switch(event->type) {
653                 case MOUSEMOVE:
654                         
655                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
656                         
657                         rect->xmax= event->x - sx;
658                         rect->ymax= event->y - sy;
659                         
660                         if((val= wm_gesture_evaluate(C, gesture))) {
661                                 wmEvent event;
662                                         
663                                 event= *(window->eventstate);
664                                 if(gesture->event_type==LEFTMOUSE)
665                                         event.type= EVT_TWEAK_L;
666                                 else if(gesture->event_type==RIGHTMOUSE)
667                                         event.type= EVT_TWEAK_R;
668                                 else
669                                         event.type= EVT_TWEAK_M;
670                                 event.val= val;
671                                 /* mouse coords! */
672                                 wm_event_add(window, &event);
673                                 
674                                 wm_gesture_end(C, op);
675                                 return OPERATOR_FINISHED;
676                         }
677                         else
678                                 wm_gesture_tag_redraw(C);
679                         
680                         break;
681                         
682                 case LEFTMOUSE:
683                 case RIGHTMOUSE:
684                 case MIDDLEMOUSE:
685                         if(gesture->event_type==event->type) {
686                                 wm_gesture_evaluate(C, gesture);
687                                 wm_gesture_end(C, op);
688                                 return OPERATOR_FINISHED;
689                         }
690                         break;
691         }
692         return OPERATOR_RUNNING_MODAL;
693 }
694
695 void WM_OT_tweak_gesture(wmOperatorType *ot)
696 {
697         ot->name= "Tweak Gesture";
698         ot->idname= "WM_OT_tweak_gesture";
699         
700         ot->invoke= tweak_gesture_invoke;
701         ot->modal= tweak_gesture_modal;
702
703         ot->poll= WM_operator_winactive;
704 }
705
706 /* *********************** lasso gesture ****************** */
707
708 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
709 {
710         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
711         
712         /* add modal handler */
713         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
714         
715         wm_gesture_tag_redraw(C);
716         
717         return OPERATOR_RUNNING_MODAL;
718 }
719
720
721 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
722 {
723         wmGesture *gesture= op->customdata;
724         PointerRNA itemptr;
725         float loc[2];
726         int i;
727         short *lasso= gesture->customdata;
728         
729         /* operator storage as path. */
730
731         for(i=0; i<gesture->points; i++, lasso+=2) {
732                 loc[0]= lasso[0];
733                 loc[1]= lasso[1];
734                 RNA_collection_add(op->ptr, "path", &itemptr);
735                 RNA_float_set_array(&itemptr, "loc", loc);
736         }
737         
738         wm_gesture_end(C, op);
739                 
740         if(op->type->exec)
741                 op->type->exec(C, op);
742         
743 }
744
745 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
746 {
747         wmGesture *gesture= op->customdata;
748         int sx, sy;
749         
750         switch(event->type) {
751                 case MOUSEMOVE:
752                         
753                         wm_gesture_tag_redraw(C);
754                         
755                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
756                         if(gesture->points < WM_LASSO_MAX_POINTS) {
757                                 short *lasso= gesture->customdata;
758                                 lasso += 2 * gesture->points;
759                                 lasso[0] = event->x - sx;
760                                 lasso[1] = event->y - sy;
761                                 gesture->points++;
762                         }
763                         else {
764                                 gesture_lasso_apply(C, op, event->type);
765                                 return OPERATOR_FINISHED;
766                         }
767                         break;
768                         
769                 case LEFTMOUSE:
770                 case MIDDLEMOUSE:
771                 case RIGHTMOUSE:
772                         if(event->val==0) {     /* key release */
773                                 gesture_lasso_apply(C, op, event->type);
774                                 return OPERATOR_FINISHED;
775                         }
776                         break;
777                 case ESCKEY:
778                         wm_gesture_end(C, op);
779                         return OPERATOR_CANCELLED;
780         }
781         return OPERATOR_RUNNING_MODAL;
782 }
783
784 #if 0
785 /* template to copy from */
786
787 static int gesture_lasso_exec(bContext *C, wmOperator *op)
788 {
789         RNA_BEGIN(op->ptr, itemptr, "path") {
790                 float loc[2];
791                 
792                 RNA_float_get_array(&itemptr, "loc", loc);
793                 printf("Location: %f %f\n", loc[0], loc[1]);
794         }
795         RNA_END;
796         
797         return OPERATOR_FINISHED;
798 }
799
800 void WM_OT_lasso_gesture(wmOperatorType *ot)
801 {
802         PropertyRNA *prop;
803         
804         ot->name= "Lasso Gesture";
805         ot->idname= "WM_OT_lasso_gesture";
806         
807         ot->invoke= WM_gesture_lasso_invoke;
808         ot->modal= WM_gesture_lasso_modal;
809         ot->exec= gesture_lasso_exec;
810         
811         ot->poll= WM_operator_winactive;
812         
813         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
814         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
815 }
816 #endif
817
818 /* ******************************************************* */
819  
820 /* called on initialize WM_exit() */
821 void wm_operatortype_free(void)
822 {
823         BLI_freelistN(&global_ops);
824 }
825
826 /* called on initialize WM_init() */
827 void wm_operatortype_init(void)
828 {
829         WM_operatortype_append(WM_OT_window_duplicate);
830         WM_operatortype_append(WM_OT_save_homefile);
831         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
832         WM_operatortype_append(WM_OT_exit_blender);
833         WM_operatortype_append(WM_OT_tweak_gesture);
834         WM_operatortype_append(WM_OT_open_recentfile);
835         WM_operatortype_append(WM_OT_open_mainfile);
836 }
837
838 /* default keymap for windows and screens, only call once per WM */
839 void wm_window_keymap(wmWindowManager *wm)
840 {
841         ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
842         
843         /* note, this doesn't replace existing keymap items */
844         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
845         WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
846         WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
847         WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
848         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", FKEY, KM_PRESS, 0, 0);
849         WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
850 }
851