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