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