svn merge -r 30566:30717 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / windowmanager / intern / wm_operators.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <float.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <stdio.h>
33
34 #include "DNA_ID.h"
35 #include "DNA_object_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_userdef_types.h"
39 #include "DNA_windowmanager_types.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLF_api.h"
44
45 #include "PIL_time.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_dynstr.h" /*for WM_operator_pystring */
49 #include "BLI_math.h"
50
51 #include "BLO_readfile.h"
52
53 #include "BKE_blender.h"
54 #include "BKE_context.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_idprop.h"
57 #include "BKE_library.h"
58 #include "BKE_global.h"
59 #include "BKE_main.h"
60 #include "BKE_report.h"
61 #include "BKE_scene.h"
62 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
63 #include "BKE_utildefines.h"
64 #include "BKE_brush.h" // JW
65
66 #include "BIF_gl.h"
67 #include "BIF_glutil.h" /* for paint cursor */
68
69 #include "IMB_imbuf_types.h"
70
71 #include "ED_screen.h"
72 #include "ED_util.h"
73 #include "ED_view3d.h" // JW
74
75 #include "RNA_access.h"
76 #include "RNA_define.h"
77
78 #include "UI_interface.h"
79 #include "UI_resources.h"
80
81 #include "WM_api.h"
82 #include "WM_types.h"
83
84 #include "wm.h"
85 #include "wm_draw.h"
86 #include "wm_event_system.h"
87 #include "wm_event_types.h"
88 #include "wm_subwindow.h"
89 #include "wm_window.h"
90
91
92
93 static ListBase global_ops= {NULL, NULL};
94
95 /* ************ operator API, exported ********** */
96
97
98 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
99 {
100         wmOperatorType *ot;
101         
102         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
103         WM_operator_bl_idname(idname_bl, idname);
104         
105         if (idname_bl[0]) {
106                 for(ot= global_ops.first; ot; ot= ot->next) {
107                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
108                            return ot;
109                 }
110         }
111         
112         if(!quiet)
113                 printf("search for unknown operator %s, %s\n", idname_bl, idname);
114         
115         return NULL;
116 }
117
118 wmOperatorType *WM_operatortype_exists(const char *idname)
119 {
120         wmOperatorType *ot;
121         
122         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
123         WM_operator_bl_idname(idname_bl, idname);
124         
125         if(idname_bl[0]) {
126                 for(ot= global_ops.first; ot; ot= ot->next) {
127                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
128                            return ot;
129                 }
130         }
131         return NULL;
132 }
133
134 wmOperatorType *WM_operatortype_first(void)
135 {
136         return global_ops.first;
137 }
138
139 /* all ops in 1 list (for time being... needs evaluation later) */
140 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
141 {
142         wmOperatorType *ot;
143         
144         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
145         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
146         opfunc(ot);
147
148         if(ot->name==NULL) {
149                 static char dummy_name[] = "Dummy Name";
150                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
151                 ot->name= dummy_name;
152         }
153
154         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)"); // XXX All ops should have a description but for now allow them not to.
155         RNA_def_struct_identifier(ot->srna, ot->idname);
156         BLI_addtail(&global_ops, ot);
157 }
158
159 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
160 {
161         wmOperatorType *ot;
162
163         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
164         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
165         opfunc(ot, userdata);
166         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
167         RNA_def_struct_identifier(ot->srna, ot->idname);
168         BLI_addtail(&global_ops, ot);
169 }
170
171 /* ********************* macro operator ******************** */
172
173 typedef struct {
174         int retval;
175 } MacroData;
176
177 static void wm_macro_start(wmOperator *op)
178 {
179         if (op->customdata == NULL) {
180                 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
181         }
182 }
183
184 static int wm_macro_end(wmOperator *op, int retval)
185 {
186         if (retval & OPERATOR_CANCELLED) {
187                 MacroData *md = op->customdata;
188
189                 if (md->retval & OPERATOR_FINISHED) {
190                         retval |= OPERATOR_FINISHED;
191                         retval &= ~OPERATOR_CANCELLED;
192                 }
193         }
194
195         /* if modal is ending, free custom data */
196         if (retval & (OPERATOR_FINISHED|OPERATOR_CANCELLED)) {
197                 if (op->customdata) {
198                         MEM_freeN(op->customdata);
199                         op->customdata = NULL;
200                 }
201         }
202
203         return retval;
204 }
205
206 /* macro exec only runs exec calls */
207 static int wm_macro_exec(bContext *C, wmOperator *op)
208 {
209         wmOperator *opm;
210         int retval= OPERATOR_FINISHED;
211         
212         wm_macro_start(op);
213
214         for(opm= op->macro.first; opm; opm= opm->next) {
215                 
216                 if(opm->type->exec) {
217                         retval= opm->type->exec(C, opm);
218                 
219                         if (retval & OPERATOR_FINISHED) {
220                                 MacroData *md = op->customdata;
221                                 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
222                         } else {
223                                 break; /* operator didn't finish, end macro */
224                         }
225                 }
226         }
227         
228         return wm_macro_end(op, retval);
229 }
230
231 int wm_macro_invoke_internal(bContext *C, wmOperator *op, wmEvent *event, wmOperator *opm)
232 {
233         int retval= OPERATOR_FINISHED;
234
235         /* start from operator received as argument */
236         for( ; opm; opm= opm->next) {
237                 if(opm->type->invoke)
238                         retval= opm->type->invoke(C, opm, event);
239                 else if(opm->type->exec)
240                         retval= opm->type->exec(C, opm);
241
242                 if (retval & OPERATOR_FINISHED) {
243                         MacroData *md = op->customdata;
244                         md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
245                 } else {
246                         break; /* operator didn't finish, end macro */
247                 }
248         }
249
250         return wm_macro_end(op, retval);
251 }
252
253 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
254 {
255         wm_macro_start(op);
256         return wm_macro_invoke_internal(C, op, event, op->macro.first);
257 }
258
259 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
260 {
261         wmOperator *opm = op->opm;
262         int retval= OPERATOR_FINISHED;
263         
264         if(opm==NULL)
265                 printf("macro error, calling NULL modal()\n");
266         else {
267                 retval = opm->type->modal(C, opm, event);
268
269                 /* if this one is done but it's not the last operator in the macro */
270                 if ((retval & OPERATOR_FINISHED) && opm->next) {
271                         MacroData *md = op->customdata;
272
273                         md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
274
275                         retval = wm_macro_invoke_internal(C, op, event, opm->next);
276
277                         /* if new operator is modal and also added its own handler */
278                         if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
279                                 wmWindow *win = CTX_wm_window(C);
280                                 wmEventHandler *handler = NULL;
281
282                                 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
283                                         /* first handler in list is the new one */
284                                         if (handler->op == op)
285                                                 break;
286                                 }
287
288                                 if (handler) {
289                                         BLI_remlink(&win->modalhandlers, handler);
290                                         wm_event_free_handler(handler);
291                                 }
292
293                                 /* if operator is blocking, grab cursor
294                                  * This may end up grabbing twice, but we don't care.
295                                  * */
296                                 if(op->opm->type->flag & OPTYPE_BLOCKING) {
297                                         int bounds[4] = {-1,-1,-1,-1};
298                                         int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
299
300                                         if(wrap) {
301                                                 ARegion *ar= CTX_wm_region(C);
302                                                 if(ar) {
303                                                         bounds[0]= ar->winrct.xmin;
304                                                         bounds[1]= ar->winrct.ymax;
305                                                         bounds[2]= ar->winrct.xmax;
306                                                         bounds[3]= ar->winrct.ymin;
307                                                 }
308                                         }
309
310                                         WM_cursor_grab(CTX_wm_window(C), wrap, FALSE, bounds);
311                                 }
312                         }
313                 }
314         }
315
316         return wm_macro_end(op, retval);
317 }
318
319 static int wm_macro_cancel(bContext *C, wmOperator *op)
320 {
321         /* call cancel on the current modal operator, if any */
322         if (op->opm && op->opm->type->cancel) {
323                 op->opm->type->cancel(C, op->opm);
324         }
325
326         return wm_macro_end(op, OPERATOR_CANCELLED);
327 }
328
329 /* Names have to be static for now */
330 wmOperatorType *WM_operatortype_append_macro(char *idname, char *name, int flag)
331 {
332         wmOperatorType *ot;
333         
334         if(WM_operatortype_exists(idname)) {
335                 printf("Macro error: operator %s exists\n", idname);
336                 return NULL;
337         }
338         
339         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
340         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
341         
342         ot->idname= idname;
343         ot->name= name;
344         ot->flag= OPTYPE_MACRO|flag;
345         
346         ot->exec= wm_macro_exec;
347         ot->invoke= wm_macro_invoke;
348         ot->modal= wm_macro_modal;
349         ot->cancel= wm_macro_cancel;
350         ot->poll= NULL;
351
352         if(!ot->description)
353                 ot->description= "(undocumented operator)";
354         
355         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description); // XXX All ops should have a description but for now allow them not to.
356         RNA_def_struct_identifier(ot->srna, ot->idname);
357
358         BLI_addtail(&global_ops, ot);
359
360         return ot;
361 }
362
363 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
364 {
365         wmOperatorType *ot;
366
367         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
368         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
369
370         ot->flag= OPTYPE_MACRO;
371         ot->exec= wm_macro_exec;
372         ot->invoke= wm_macro_invoke;
373         ot->modal= wm_macro_modal;
374         ot->cancel= wm_macro_cancel;
375         ot->poll= NULL;
376
377         if(!ot->description)
378                 ot->description= "(undocumented operator)";
379
380         opfunc(ot, userdata);
381
382         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
383         RNA_def_struct_identifier(ot->srna, ot->idname);
384
385         BLI_addtail(&global_ops, ot);
386 }
387
388 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
389 {
390         wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
391         
392         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
393
394         /* do this on first use, since operatordefinitions might have been not done yet */
395         WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
396         WM_operator_properties_sanitize(otmacro->ptr, 1);
397         
398         BLI_addtail(&ot->macro, otmacro);
399
400         {
401                 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
402                 RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
403                 otsub->name, otsub->description);
404         }
405         
406         return otmacro;
407 }
408
409 static void wm_operatortype_free_macro(wmOperatorType *ot)
410 {
411         wmOperatorTypeMacro *otmacro;
412         
413         for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
414                 if(otmacro->ptr) {
415                         WM_operator_properties_free(otmacro->ptr);
416                         MEM_freeN(otmacro->ptr);
417                 }
418         }
419         BLI_freelistN(&ot->macro);
420 }
421
422
423 int WM_operatortype_remove(const char *idname)
424 {
425         wmOperatorType *ot = WM_operatortype_find(idname, 0);
426
427         if (ot==NULL)
428                 return 0;
429         
430         BLI_remlink(&global_ops, ot);
431         RNA_struct_free(&BLENDER_RNA, ot->srna);
432         
433         if(ot->macro.first)
434                 wm_operatortype_free_macro(ot);
435         
436         MEM_freeN(ot);
437
438         return 1;
439 }
440
441 /* SOME_OT_op -> some.op */
442 void WM_operator_py_idname(char *to, const char *from)
443 {
444         char *sep= strstr(from, "_OT_");
445         if(sep) {
446                 int i, ofs= (sep-from);
447
448                 for(i=0; i<ofs; i++)
449                         to[i]= tolower(from[i]);
450
451                 to[ofs] = '.';
452                 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
453         }
454         else {
455                 /* should not happen but support just incase */
456                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
457         }
458 }
459
460 /* some.op -> SOME_OT_op */
461 void WM_operator_bl_idname(char *to, const char *from)
462 {
463         if (from) {
464                 char *sep= strchr(from, '.');
465
466                 if(sep) {
467                         int i, ofs= (sep-from);
468
469                         for(i=0; i<ofs; i++)
470                                 to[i]= toupper(from[i]);
471
472                         BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
473                         BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
474                 }
475                 else {
476                         /* should not happen but support just incase */
477                         BLI_strncpy(to, from, OP_MAX_TYPENAME);
478                 }
479         }
480         else
481                 to[0]= 0;
482 }
483
484 /* print a string representation of the operator, with the args that it runs 
485  * so python can run it again,
486  *
487  * When calling from an existing wmOperator do.
488  * WM_operator_pystring(op->type, op->ptr);
489  */
490 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
491 {
492         const char *arg_name= NULL;
493         char idname_py[OP_MAX_TYPENAME];
494
495         PropertyRNA *prop, *iterprop;
496
497         /* for building the string */
498         DynStr *dynstr= BLI_dynstr_new();
499         char *cstring, *buf;
500         int first_iter=1, ok= 1;
501
502
503         /* only to get the orginal props for comparisons */
504         PointerRNA opptr_default;
505         PropertyRNA *prop_default;
506         char *buf_default;
507         if(all_args==0 || opptr==NULL) {
508                 WM_operator_properties_create_ptr(&opptr_default, ot);
509
510                 if(opptr==NULL)
511                         opptr = &opptr_default;
512         }
513
514
515         WM_operator_py_idname(idname_py, ot->idname);
516         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
517
518         iterprop= RNA_struct_iterator_property(opptr->type);
519
520         RNA_PROP_BEGIN(opptr, propptr, iterprop) {
521                 prop= propptr.data;
522                 arg_name= RNA_property_identifier(prop);
523
524                 if (strcmp(arg_name, "rna_type")==0) continue;
525
526                 buf= RNA_property_as_string(C, opptr, prop);
527                 
528                 ok= 1;
529
530                 if(!all_args) {
531                         /* not verbose, so only add in attributes that use non-default values
532                          * slow but good for tooltips */
533                         prop_default= RNA_struct_find_property(&opptr_default, arg_name);
534
535                         if(prop_default) {
536                                 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
537
538                                 if(strcmp(buf, buf_default)==0)
539                                         ok= 0; /* values match, dont bother printing */
540
541                                 MEM_freeN(buf_default);
542                         }
543
544                 }
545                 if(ok) {
546                         BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
547                         first_iter = 0;
548                 }
549
550                 MEM_freeN(buf);
551
552         }
553         RNA_PROP_END;
554
555         if(all_args==0 || opptr==&opptr_default )
556                 WM_operator_properties_free(&opptr_default);
557
558         BLI_dynstr_append(dynstr, ")");
559
560         cstring = BLI_dynstr_get_cstring(dynstr);
561         BLI_dynstr_free(dynstr);
562         return cstring;
563 }
564
565 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
566 {
567         RNA_pointer_create(NULL, ot->srna, NULL, ptr);
568 }
569
570 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
571 {
572         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
573
574         if(ot)
575                 WM_operator_properties_create_ptr(ptr, ot);
576         else
577                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
578 }
579
580 /* similar to the function above except its uses ID properties
581  * used for keymaps and macros */
582 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
583 {
584         if(*properties==NULL) {
585                 IDPropertyTemplate val = {0};
586                 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
587         }
588
589         if(*ptr==NULL) {
590                 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
591                 WM_operator_properties_create(*ptr, opstring);
592         }
593
594         (*ptr)->data= *properties;
595
596 }
597
598 void WM_operator_properties_sanitize(PointerRNA *ptr, int val)
599 {
600         RNA_STRUCT_BEGIN(ptr, prop) {
601                 switch(RNA_property_type(prop)) {
602                 case PROP_ENUM:
603                         if (val)
604                                 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
605                         else
606                                 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
607                         break;
608                 case PROP_POINTER:
609                         {
610                                 StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
611
612                                 /* recurse into operator properties */
613                                 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
614                                         PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
615                                         WM_operator_properties_sanitize(&opptr, val);
616                                 }
617                                 break;
618                         }
619                 default:
620                         break;
621                 }
622         }
623         RNA_STRUCT_END;
624 }
625
626 void WM_operator_properties_free(PointerRNA *ptr)
627 {
628         IDProperty *properties= ptr->data;
629
630         if(properties) {
631                 IDP_FreeProperty(properties);
632                 MEM_freeN(properties);
633         }
634 }
635
636 /* ************ default op callbacks, exported *********** */
637
638 /* invoke callback, uses enum property named "type" */
639 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
640 {
641         PropertyRNA *prop= op->type->prop;
642         uiPopupMenu *pup;
643         uiLayout *layout;
644
645         if(prop==NULL) {
646                 printf("WM_menu_invoke: %s has no enum property set\n", op->type->idname);
647         }
648         else if (RNA_property_type(prop) != PROP_ENUM) {
649                 printf("WM_menu_invoke: %s \"%s\" is not an enum property\n", op->type->idname, RNA_property_identifier(prop));
650         }
651         else if (RNA_property_is_set(op->ptr, RNA_property_identifier(prop))) {
652                 return op->type->exec(C, op);
653         }
654         else {
655                 pup= uiPupMenuBegin(C, op->type->name, 0);
656                 layout= uiPupMenuLayout(pup);
657                 uiItemsFullEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
658                 uiPupMenuEnd(C, pup);
659         }
660
661         return OPERATOR_CANCELLED;
662 }
663
664
665 /* generic enum search invoke popup */
666 static void operator_enum_search_cb(const struct bContext *C, void *arg_ot, char *str, uiSearchItems *items)
667 {
668         wmOperatorType *ot = (wmOperatorType *)arg_ot;
669         PropertyRNA *prop= ot->prop;
670
671         if(prop==NULL) {
672                 printf("WM_enum_search_invoke: %s has no enum property set\n", ot->idname);
673         }
674         else if (RNA_property_type(prop) != PROP_ENUM) {
675                 printf("WM_enum_search_invoke: %s \"%s\" is not an enum property\n", ot->idname, RNA_property_identifier(prop));
676         }
677         else {
678                 PointerRNA ptr;
679
680                 EnumPropertyItem *item, *item_array;
681                 int free;
682
683                 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
684                 RNA_property_enum_items((bContext *)C, &ptr, prop, &item_array, NULL, &free);
685
686                 for(item= item_array; item->identifier; item++) {
687                         /* note: need to give the intex rather then the dientifier because the enum can be freed */
688                         if(BLI_strcasestr(item->name, str))
689                                 if(0==uiSearchItemAdd(items, item->name, SET_INT_IN_POINTER(item->value), 0))
690                                         break;
691                 }
692
693                 if(free)
694                         MEM_freeN(item_array);
695         }
696 }
697
698 static void operator_enum_call_cb(struct bContext *C, void *arg1, void *arg2)
699 {
700         wmOperatorType *ot= arg1;
701
702         if(ot) {
703                 PointerRNA props_ptr;
704                 WM_operator_properties_create_ptr(&props_ptr, ot);
705                 RNA_property_enum_set(&props_ptr, ot->prop, GET_INT_FROM_POINTER(arg2));
706                 WM_operator_name_call(C, ot->idname, WM_OP_EXEC_DEFAULT, &props_ptr);
707                 WM_operator_properties_free(&props_ptr);
708         }
709 }
710
711 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
712 {
713         static char search[256]= "";
714         wmEvent event;
715         wmWindow *win= CTX_wm_window(C);
716         uiBlock *block;
717         uiBut *but;
718         wmOperator *op= (wmOperator *)arg_op;
719
720         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
721         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
722
723         //uiDefBut(block, LABEL, 0, op->type->name, 10, 10, 180, 19, NULL, 0.0, 0.0, 0, 0, ""); // ok, this isnt so easy...
724         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
725         uiButSetSearchFunc(but, operator_enum_search_cb, op->type, operator_enum_call_cb, NULL);
726
727         /* fake button, it holds space for search items */
728         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
729
730         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
731         uiEndBlock(C, block);
732
733         event= *(win->eventstate);      /* XXX huh huh? make api call */
734         event.type= EVT_BUT_OPEN;
735         event.val= KM_PRESS;
736         event.customdata= but;
737         event.customdatafree= FALSE;
738         wm_event_add(win, &event);
739
740         return block;
741 }
742
743
744 int WM_enum_search_invoke(bContext *C, wmOperator *op, wmEvent *event)
745 {
746         uiPupBlock(C, wm_enum_search_menu, op);
747         return OPERATOR_CANCELLED;
748 }
749
750 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
751 int WM_operator_confirm_message(bContext *C, wmOperator *op, char *message)
752 {
753         uiPopupMenu *pup;
754         uiLayout *layout;
755         IDProperty *properties= op->ptr->data;
756
757         if(properties && properties->len)
758                 properties= IDP_CopyProperty(op->ptr->data);
759         else
760                 properties= NULL;
761
762         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
763         layout= uiPupMenuLayout(pup);
764         uiItemFullO(layout, op->type->idname, message, 0, properties, WM_OP_EXEC_REGION_WIN, 0);
765         uiPupMenuEnd(C, pup);
766         
767         return OPERATOR_CANCELLED;
768 }
769
770
771 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
772 {
773         return WM_operator_confirm_message(C, op, NULL);
774 }
775
776 /* op->invoke, opens fileselect if path property not set, otherwise executes */
777 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
778 {
779         if (RNA_property_is_set(op->ptr, "filepath")) {
780                 return WM_operator_call(C, op);
781         } 
782         else {
783                 WM_event_add_fileselect(C, op);
784                 return OPERATOR_RUNNING_MODAL;
785         }
786 }
787
788 /* default properties for fileselect */
789 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag)
790 {
791         PropertyRNA *prop;
792
793
794         if(flag & WM_FILESEL_FILEPATH)
795                 RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "File Path", "Path to file");
796
797         if(flag & WM_FILESEL_DIRECTORY)
798                 RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file");
799
800         if(flag & WM_FILESEL_FILENAME)
801                 RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file");
802
803         if (action == FILE_SAVE) {
804                 prop= RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
805                 RNA_def_property_flag(prop, PROP_HIDDEN);
806         }
807         
808         prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
809         RNA_def_property_flag(prop, PROP_HIDDEN);
810         prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
811         RNA_def_property_flag(prop, PROP_HIDDEN);
812         prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
813         RNA_def_property_flag(prop, PROP_HIDDEN);
814         prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
815         RNA_def_property_flag(prop, PROP_HIDDEN);
816         prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
817         RNA_def_property_flag(prop, PROP_HIDDEN);
818         prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
819         RNA_def_property_flag(prop, PROP_HIDDEN);
820         prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
821         RNA_def_property_flag(prop, PROP_HIDDEN);
822         prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
823         RNA_def_property_flag(prop, PROP_HIDDEN);
824         prop= RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
825         RNA_def_property_flag(prop, PROP_HIDDEN);
826         prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
827         RNA_def_property_flag(prop, PROP_HIDDEN);
828
829         prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL, 
830                 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
831                 FILE_LOADLIB, FILE_SPECIAL);
832         RNA_def_property_flag(prop, PROP_HIDDEN);
833
834         if(flag & WM_FILESEL_RELPATH)
835                 RNA_def_boolean(ot->srna, "relative_path", (U.flag & USER_RELPATHS) ? 1:0, "Relative Path", "Select the file relative to the blend file");
836 }
837
838 void WM_operator_properties_select_all(wmOperatorType *ot) {
839         static EnumPropertyItem select_all_actions[] = {
840                         {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
841                         {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
842                         {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
843                         {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
844                         {0, NULL, 0, NULL, NULL}
845         };
846
847         RNA_def_enum(ot->srna, "action", select_all_actions, SEL_TOGGLE, "Action", "Selection action to execute");
848 }
849
850 void WM_operator_properties_gesture_border(wmOperatorType *ot, int extend)
851 {
852         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
853         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
854         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
855         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
856         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
857
858         if(extend)
859                 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
860 }
861
862 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
863 {
864         RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
865         RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
866         RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
867         RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
868         
869         if(cursor)
870                 RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX, "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
871 }
872
873
874 /* op->poll */
875 int WM_operator_winactive(bContext *C)
876 {
877         if(CTX_wm_window(C)==NULL) return 0;
878         return 1;
879 }
880
881 /* op->invoke */
882 static void redo_cb(bContext *C, void *arg_op, int event)
883 {
884         wmOperator *lastop= arg_op;
885         
886         if(lastop) {
887                 ED_undo_pop_op(C, lastop);
888                 WM_operator_repeat(C, lastop);
889         }
890 }
891
892 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
893 {
894         wmWindowManager *wm= CTX_wm_manager(C);
895         wmOperator *op= arg_op;
896         PointerRNA ptr;
897         uiBlock *block;
898         uiLayout *layout;
899         uiStyle *style= U.uistyles.first;
900         int columns= 2, width= 300;
901         
902
903         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
904         uiBlockClearFlag(block, UI_BLOCK_LOOP);
905         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
906         uiBlockSetHandleFunc(block, redo_cb, arg_op);
907
908         if(!op->properties) {
909                 IDPropertyTemplate val = {0};
910                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
911         }
912
913         // XXX - hack, only for editing docs
914         if(strcmp(op->type->idname, "WM_OT_doc_edit")==0) {
915                 columns= 1;
916                 width= 500;
917         }
918
919         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
920         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, 20, style);
921         uiItemL(layout, op->type->name, 0);
922
923         if(op->type->ui) {
924                 op->layout= layout;
925                 op->type->ui((bContext*)C, op);
926                 op->layout= NULL;
927         }
928         else
929                 uiDefAutoButsRNA(C, layout, &ptr, columns);
930
931         uiPopupBoundsBlock(block, 4.0f, 0, 0);
932         uiEndBlock(C, block);
933
934         return block;
935 }
936
937 /* Only invoked by OK button in popups created with wm_block_create_dialog() */
938 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
939 {
940         wmOperator *op= arg1;
941         uiBlock *block= arg2;
942
943         WM_operator_call(C, op);
944
945         uiPupBlockClose(C, block);
946 }
947
948 /* Dialogs are popups that require user verification (click OK) before exec */
949 static uiBlock *wm_block_create_dialog(bContext *C, ARegion *ar, void *userData)
950 {
951         struct { wmOperator *op; int width; int height; } * data = userData;
952         wmWindowManager *wm= CTX_wm_manager(C);
953         wmOperator *op= data->op;
954         PointerRNA ptr;
955         uiBlock *block;
956         uiLayout *layout;
957         uiBut *btn;
958         uiStyle *style= U.uistyles.first;
959         int columns= 2;
960
961         block = uiBeginBlock(C, ar, "operator dialog", UI_EMBOSS);
962         uiBlockClearFlag(block, UI_BLOCK_LOOP);
963         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
964
965         if (!op->properties) {
966                 IDPropertyTemplate val = {0};
967                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
968         }
969
970         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
971         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
972         uiItemL(layout, op->type->name, 0);
973
974         if (op->type->ui) {
975                 op->layout= layout;
976                 op->type->ui((bContext*)C, op);
977                 op->layout= NULL;
978         }
979         else
980                 uiDefAutoButsRNA(C, layout, &ptr, columns);
981
982         /* Create OK button, the callback of which will execute op */
983         btn= uiDefBut(block, BUT, 0, "OK", 0, 0, 0, 20, NULL, 0, 0, 0, 0, "");
984         uiButSetFunc(btn, dialog_exec_cb, op, block);
985
986         uiPopupBoundsBlock(block, 4.0f, 0, 0);
987         uiEndBlock(C, block);
988
989         return block;
990 }
991
992 static uiBlock *wm_operator_create_ui(bContext *C, ARegion *ar, void *userData)
993 {
994         struct { wmOperator *op; int width; int height; } * data = userData;
995         wmWindowManager *wm= CTX_wm_manager(C);
996         wmOperator *op= data->op;
997         PointerRNA ptr;
998         uiBlock *block;
999         uiLayout *layout;
1000         uiStyle *style= U.uistyles.first;
1001
1002         block= uiBeginBlock(C, ar, "opui_popup", UI_EMBOSS);
1003         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1004         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1005
1006         if(!op->properties) {
1007                 IDPropertyTemplate val = {0};
1008                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
1009         }
1010
1011         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1012         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
1013
1014         if(op->type->ui) {
1015                 op->layout= layout;
1016                 op->type->ui((bContext*)C, op);
1017                 op->layout= NULL;
1018         }
1019
1020         uiPopupBoundsBlock(block, 4.0f, 0, 0);
1021         uiEndBlock(C, block);
1022
1023         return block;
1024 }
1025
1026 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
1027 {
1028         int retval= OPERATOR_CANCELLED;
1029         
1030         if(op->type->exec) {
1031                 retval= op->type->exec(C, op);
1032
1033                 /* ED_undo_push_op(C, op), called by wm_operator_finished now. */
1034         }
1035
1036         if(retval != OPERATOR_CANCELLED)
1037                 uiPupBlock(C, wm_block_create_redo, op);
1038
1039         return retval;
1040 }
1041
1042 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1043 {
1044         struct { wmOperator *op; int width; int height; } data;
1045         
1046         data.op= op;
1047         data.width= width;
1048         data.height= height;
1049
1050         /* op is not executed until popup OK but is clicked */
1051         uiPupBlock(C, wm_block_create_dialog, &data);
1052
1053         return OPERATOR_RUNNING_MODAL;
1054 }
1055
1056 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1057 {
1058         struct { wmOperator *op; int width; int height; } data;
1059         data.op = op;
1060         data.width = width;
1061         data.height = height;
1062         uiPupBlock(C, wm_operator_create_ui, &data);
1063         return OPERATOR_RUNNING_MODAL;
1064 }
1065
1066 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1067 {
1068         uiPupBlock(C, wm_block_create_redo, op);
1069
1070         return OPERATOR_CANCELLED;
1071 }
1072
1073 /* ***************** Debug menu ************************* */
1074
1075 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
1076 {
1077         wmOperator *op= arg_op;
1078         uiBlock *block;
1079         uiLayout *layout;
1080         uiStyle *style= U.uistyles.first;
1081         
1082         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1083         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1084         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1085         
1086         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
1087         uiItemL(layout, op->type->name, 0);
1088
1089         if(op->type->ui) {
1090                 op->layout= layout;
1091                 op->type->ui(C, op);
1092                 op->layout= NULL;
1093         }
1094         else
1095                 uiDefAutoButsRNA(C, layout, op->ptr, 2);
1096         
1097         uiPopupBoundsBlock(block, 4.0f, 0, 0);
1098         uiEndBlock(C, block);
1099         
1100         return block;
1101 }
1102
1103 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1104 {
1105         G.rt= RNA_int_get(op->ptr, "debugval");
1106         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1107         WM_event_add_notifier(C, NC_WINDOW, NULL);
1108         
1109         return OPERATOR_FINISHED;       
1110 }
1111
1112 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
1113 {
1114         
1115         RNA_int_set(op->ptr, "debugval", G.rt);
1116         
1117         /* pass on operator, so return modal */
1118         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
1119         
1120         return OPERATOR_RUNNING_MODAL;
1121 }
1122
1123 static void WM_OT_debug_menu(wmOperatorType *ot)
1124 {
1125         ot->name= "Debug Menu";
1126         ot->idname= "WM_OT_debug_menu";
1127         ot->description= "Open a popup to set the debug level";
1128         
1129         ot->invoke= wm_debug_menu_invoke;
1130         ot->exec= wm_debug_menu_exec;
1131         ot->poll= WM_operator_winactive;
1132         
1133         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
1134 }
1135
1136
1137 /* ***************** Splash Screen ************************* */
1138
1139 static void wm_block_splash_close(bContext *C, void *arg_block, void *arg_unused)
1140 {
1141         uiPupBlockClose(C, arg_block);
1142 }
1143
1144 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1145
1146 /* XXX: hack to refresh splash screen with updated prest menu name,
1147  * since popup blocks don't get regenerated like panels do */
1148 void wm_block_splash_refreshmenu (bContext *C, void *arg_block, void *unused)
1149 {
1150         /* ugh, causes crashes in other buttons, disabling for now until 
1151          * a better fix
1152         uiPupBlockClose(C, arg_block);
1153         uiPupBlock(C, wm_block_create_splash, NULL);
1154           */
1155 }
1156
1157 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused)
1158 {
1159         uiBlock *block;
1160         uiBut *but;
1161         uiLayout *layout, *split, *col;
1162         uiStyle *style= U.uistyles.first;
1163         struct RecentFile *recent;
1164         int i;
1165         Menu menu= {0};
1166         MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1167         
1168 #ifdef NAN_BUILDINFO
1169         int ver_width, rev_width;
1170         char *version_str = NULL;
1171         char *revision_str = NULL;
1172         char version_buf[128];
1173         char revision_buf[128];
1174         extern char build_rev[];
1175         
1176         version_str = &version_buf[0];
1177         revision_str = &revision_buf[0];
1178         
1179         sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1180         sprintf(revision_str, "r%s", build_rev);
1181         
1182         BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1183         ver_width = BLF_width(style->widgetlabel.uifont_id, version_str)+5;
1184         rev_width = BLF_width(style->widgetlabel.uifont_id, revision_str)+5;
1185 #endif //NAN_BUILDINFO
1186
1187         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1188         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1189         
1190         but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1191         uiButSetFunc(but, wm_block_splash_close, block, NULL);
1192         uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1193         
1194 #ifdef NAN_BUILDINFO    
1195         uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1196         uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1197 #endif //NAN_BUILDINFO
1198         
1199         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1200         
1201         uiBlockSetEmboss(block, UI_EMBOSS);
1202         /* show the splash menu (containing interaction presets), using python */
1203         if (mt) {
1204                 menu.layout= layout;
1205                 menu.type= mt;
1206                 mt->draw(C, &menu);
1207         }
1208         
1209         uiBlockSetEmboss(block, UI_EMBOSSP);
1210         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1211         
1212         split = uiLayoutSplit(layout, 0, 0);
1213         col = uiLayoutColumn(split, 0);
1214         uiItemL(col, "Links", 0);
1215         uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1216         uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-250/");
1217         uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:Manual");
1218         uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1219         uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/");
1220         uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/documentation/250PythonDoc/contents.html");
1221         uiItemL(col, "", 0);
1222         
1223         col = uiLayoutColumn(split, 0);
1224         uiItemL(col, "Recent", 0);
1225         for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1226                 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1227         }
1228         uiItemS(col);
1229         uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1230         uiItemL(col, "", 0);
1231         
1232         uiCenteredBoundsBlock(block, 0.0f);
1233         uiEndBlock(C, block);
1234         
1235         return block;
1236 }
1237
1238 static int wm_splash_invoke(bContext *C, wmOperator *op, wmEvent *event)
1239 {
1240         uiPupBlock(C, wm_block_create_splash, NULL);
1241         
1242         return OPERATOR_FINISHED;
1243 }
1244
1245 static void WM_OT_splash(wmOperatorType *ot)
1246 {
1247         ot->name= "Splash Screen";
1248         ot->idname= "WM_OT_splash";
1249         ot->description= "Opens a blocking popup region with release info";
1250         
1251         ot->invoke= wm_splash_invoke;
1252         ot->poll= WM_operator_winactive;
1253 }
1254
1255
1256 /* ***************** Search menu ************************* */
1257 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
1258 {
1259         wmOperatorType *ot= arg2;
1260         
1261         if(ot)
1262                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1263 }
1264
1265 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
1266 {
1267         wmOperatorType *ot = WM_operatortype_first();
1268         
1269         for(; ot; ot= ot->next) {
1270                 
1271                 if(BLI_strcasestr(ot->name, str)) {
1272                         if(WM_operator_poll((bContext*)C, ot)) {
1273                                 char name[256];
1274                                 int len= strlen(ot->name);
1275                                 
1276                                 /* display name for menu, can hold hotkey */
1277                                 BLI_strncpy(name, ot->name, 256);
1278                                 
1279                                 /* check for hotkey */
1280                                 if(len < 256-6) {
1281                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1282                                                 name[len]= '|';
1283                                 }
1284                                 
1285                                 if(0==uiSearchItemAdd(items, name, ot, 0))
1286                                         break;
1287                         }
1288                 }
1289         }
1290 }
1291
1292 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
1293 {
1294         static char search[256]= "";
1295         wmEvent event;
1296         wmWindow *win= CTX_wm_window(C);
1297         uiBlock *block;
1298         uiBut *but;
1299         
1300         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1301         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1302         
1303         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1304         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1305         
1306         /* fake button, it holds space for search items */
1307         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1308         
1309         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
1310         uiEndBlock(C, block);
1311         
1312         event= *(win->eventstate);      /* XXX huh huh? make api call */
1313         event.type= EVT_BUT_OPEN;
1314         event.val= KM_PRESS;
1315         event.customdata= but;
1316         event.customdatafree= FALSE;
1317         wm_event_add(win, &event);
1318         
1319         return block;
1320 }
1321
1322 static int wm_search_menu_exec(bContext *C, wmOperator *op)
1323 {
1324         
1325         return OPERATOR_FINISHED;       
1326 }
1327
1328 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
1329 {
1330         
1331         uiPupBlock(C, wm_block_search_menu, op);
1332         
1333         return OPERATOR_CANCELLED;
1334 }
1335
1336 /* op->poll */
1337 static int wm_search_menu_poll(bContext *C)
1338 {
1339         if(CTX_wm_window(C)==NULL) return 0;
1340         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
1341         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_TEXT) return 0;  // XXX - so we can use the spacebar in the text editor
1342         if(CTX_data_edit_object(C) && CTX_data_edit_object(C)->type==OB_FONT) return 0; // XXX - so we can use the spacebar for entering text
1343         return 1;
1344 }
1345
1346 static void WM_OT_search_menu(wmOperatorType *ot)
1347 {
1348         ot->name= "Search Menu";
1349         ot->idname= "WM_OT_search_menu";
1350         
1351         ot->invoke= wm_search_menu_invoke;
1352         ot->exec= wm_search_menu_exec;
1353         ot->poll= wm_search_menu_poll;
1354 }
1355
1356 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1357 {
1358         char idname[BKE_ST_MAXNAME];
1359         RNA_string_get(op->ptr, "name", idname);
1360
1361         uiPupMenuInvoke(C, idname);
1362
1363         return OPERATOR_CANCELLED;
1364 }
1365
1366 static void WM_OT_call_menu(wmOperatorType *ot)
1367 {
1368         ot->name= "Call Menu";
1369         ot->idname= "WM_OT_call_menu";
1370
1371         ot->exec= wm_call_menu_exec;
1372
1373         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1374 }
1375
1376 /* ************ window / screen operator definitions ************** */
1377
1378 /* this poll functions is needed in place of WM_operator_winactive
1379  * while it crashes on full screen */
1380 static int wm_operator_winactive_normal(bContext *C)
1381 {
1382         wmWindow *win= CTX_wm_window(C);
1383
1384         if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1385                 return 0;
1386
1387         return 1;
1388 }
1389
1390 static void WM_OT_window_duplicate(wmOperatorType *ot)
1391 {
1392         ot->name= "Duplicate Window";
1393         ot->idname= "WM_OT_window_duplicate";
1394         ot->description="Duplicate the current Blender window";
1395                 
1396         ot->exec= wm_window_duplicate_op;
1397         ot->poll= wm_operator_winactive_normal;
1398 }
1399
1400 static void WM_OT_save_homefile(wmOperatorType *ot)
1401 {
1402         ot->name= "Save User Settings";
1403         ot->idname= "WM_OT_save_homefile";
1404         ot->description="Make the current file the default .blend file";
1405                 
1406         ot->invoke= WM_operator_confirm;
1407         ot->exec= WM_write_homefile;
1408         ot->poll= WM_operator_winactive;
1409 }
1410
1411 static void WM_OT_read_homefile(wmOperatorType *ot)
1412 {
1413         ot->name= "Reload Start-Up File";
1414         ot->idname= "WM_OT_read_homefile";
1415         ot->description="Open the default file (doesn't save the current file)";
1416         
1417         ot->invoke= WM_operator_confirm;
1418         ot->exec= WM_read_homefile;
1419         ot->poll= WM_operator_winactive;
1420         
1421         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
1422 }
1423
1424 /* *************** open file **************** */
1425
1426 static void open_set_load_ui(wmOperator *op)
1427 {
1428         if(!RNA_property_is_set(op->ptr, "load_ui"))
1429                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1430 }
1431
1432 static void open_set_use_scripts(wmOperator *op)
1433 {
1434         if(!RNA_property_is_set(op->ptr, "use_scripts"))
1435                 RNA_boolean_set(op->ptr, "use_scripts", !(U.flag & USER_SCRIPT_AUTOEXEC_DISABLE));
1436 }
1437
1438 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1439 {
1440         RNA_string_set(op->ptr, "filepath", G.sce);
1441         open_set_load_ui(op);
1442         open_set_use_scripts(op);
1443
1444         WM_event_add_fileselect(C, op);
1445
1446         return OPERATOR_RUNNING_MODAL;
1447 }
1448
1449 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1450 {
1451         char path[FILE_MAX];
1452
1453         RNA_string_get(op->ptr, "filepath", path);
1454         open_set_load_ui(op);
1455         open_set_use_scripts(op);
1456
1457         if(RNA_boolean_get(op->ptr, "load_ui"))
1458                 G.fileflags &= ~G_FILE_NO_UI;
1459         else
1460                 G.fileflags |= G_FILE_NO_UI;
1461                 
1462         if(RNA_boolean_get(op->ptr, "use_scripts"))
1463                 G.f |= G_SCRIPT_AUTOEXEC;
1464         else
1465                 G.f &= ~G_SCRIPT_AUTOEXEC;
1466         
1467         // XXX wm in context is not set correctly after WM_read_file -> crash
1468         // do it before for now, but is this correct with multiple windows?
1469         WM_event_add_notifier(C, NC_WINDOW, NULL);
1470
1471         WM_read_file(C, path, op->reports);
1472         
1473         return OPERATOR_FINISHED;
1474 }
1475
1476 static void WM_OT_open_mainfile(wmOperatorType *ot)
1477 {
1478         ot->name= "Open Blender File";
1479         ot->idname= "WM_OT_open_mainfile";
1480         ot->description="Open a Blender file";
1481         
1482         ot->invoke= wm_open_mainfile_invoke;
1483         ot->exec= wm_open_mainfile_exec;
1484         ot->poll= WM_operator_winactive;
1485         
1486         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1487
1488         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1489         RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1490 }
1491
1492 /* **************** link/append *************** */
1493
1494 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *event)
1495 {
1496         if(!RNA_property_is_set(op->ptr, "relative_path"))
1497                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1498
1499         if(RNA_property_is_set(op->ptr, "filepath")) {
1500                 return WM_operator_call(C, op);
1501         } 
1502         else {
1503                 /* XXX TODO solve where to get last linked library from */
1504                 RNA_string_set(op->ptr, "filepath", G.lib);
1505                 WM_event_add_fileselect(C, op);
1506                 return OPERATOR_RUNNING_MODAL;
1507         }
1508 }
1509
1510 static short wm_link_append_flag(wmOperator *op)
1511 {
1512         short flag= 0;
1513
1514         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1515         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1516         if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1517         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1518         if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1519
1520         return flag;
1521 }
1522
1523 static void wm_link_make_library_local(Main *main, const char *libname)
1524 {
1525         Library *lib;
1526
1527         /* and now find the latest append lib file */
1528         for(lib= main->library.first; lib; lib=lib->id.next)
1529                 if(BLI_streq(libname, lib->filepath))
1530                         break;
1531         
1532         /* make local */
1533         if(lib) {
1534                 all_local(lib, 1);
1535         }
1536 }
1537
1538 static int wm_link_append_exec(bContext *C, wmOperator *op)
1539 {
1540         Main *bmain= CTX_data_main(C);
1541         Scene *scene= CTX_data_scene(C);
1542         Main *mainl= 0;
1543         BlendHandle *bh;
1544         PropertyRNA *prop;
1545         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1546         int idcode, totfiles=0;
1547         short flag;
1548
1549         name[0] = '\0';
1550         RNA_string_get(op->ptr, "filename", name);
1551         RNA_string_get(op->ptr, "directory", dir);
1552
1553         /* test if we have a valid data */
1554         if(BLO_is_a_library(dir, libname, group) == 0) {
1555                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1556                 return OPERATOR_CANCELLED;
1557         }
1558         else if(group[0] == 0) {
1559                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1560                 return OPERATOR_CANCELLED;
1561         }
1562         else if(BLI_streq(bmain->name, libname)) {
1563                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1564                 return OPERATOR_CANCELLED;
1565         }
1566
1567         /* check if something is indicated for append/link */
1568         prop = RNA_struct_find_property(op->ptr, "files");
1569         if(prop) {
1570                 totfiles= RNA_property_collection_length(op->ptr, prop);
1571                 if(totfiles == 0) {
1572                         if(name[0] == '\0') {
1573                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1574                                 return OPERATOR_CANCELLED;
1575                         }
1576                 }
1577         }
1578         else if(name[0] == '\0') {
1579                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1580                 return OPERATOR_CANCELLED;
1581         }
1582
1583         /* now we have or selected, or an indicated file */
1584         if(RNA_boolean_get(op->ptr, "autoselect"))
1585                 scene_deselect_all(scene);
1586
1587         bh = BLO_blendhandle_from_file(libname);
1588         idcode = BLO_idcode_from_name(group);
1589         
1590         flag = wm_link_append_flag(op);
1591
1592         /* sanity checks for flag */
1593         if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1594                 /* TODO, user never gets this message */
1595                 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1596                 flag &= ~FILE_GROUP_INSTANCE;
1597         }
1598
1599
1600         /* tag everything, all untagged data can be made local
1601          * its also generally useful to know what is new
1602          *
1603          * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1604         flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1605
1606         /* here appending/linking starts */
1607         mainl = BLO_library_append_begin(C, &bh, libname);
1608         if(totfiles == 0) {
1609                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1610         }
1611         else {
1612                 RNA_BEGIN(op->ptr, itemptr, "files") {
1613                         RNA_string_get(&itemptr, "name", name);
1614                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1615                 }
1616                 RNA_END;
1617         }
1618         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1619         
1620         /* mark all library linked objects to be updated */
1621         recalc_all_library_objects(bmain);
1622
1623         /* append, rather than linking */
1624         if((flag & FILE_LINK)==0)
1625                 wm_link_make_library_local(bmain, libname);
1626
1627         /* important we unset, otherwise these object wont
1628          * link into other scenes from this blend file */
1629         flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1630
1631         /* recreate dependency graph to include new objects */
1632         DAG_scene_sort(scene);
1633         DAG_ids_flush_update(0);
1634
1635         BLO_blendhandle_close(bh);
1636
1637         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1638         BLI_strncpy(G.lib, dir, FILE_MAX);
1639
1640         WM_event_add_notifier(C, NC_WINDOW, NULL);
1641
1642         return OPERATOR_FINISHED;
1643 }
1644
1645 static void WM_OT_link_append(wmOperatorType *ot)
1646 {
1647         ot->name= "Link/Append from Library";
1648         ot->idname= "WM_OT_link_append";
1649         ot->description= "Link or Append from a Library .blend file";
1650         
1651         ot->invoke= wm_link_append_invoke;
1652         ot->exec= wm_link_append_exec;
1653         ot->poll= WM_operator_winactive;
1654         
1655         ot->flag |= OPTYPE_UNDO;
1656
1657         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1658         
1659         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1660         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1661         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1662         RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1663
1664         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1665 }       
1666
1667 /* *************** recover last session **************** */
1668
1669 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1670 {
1671         char filename[FILE_MAX];
1672
1673         G.fileflags |= G_FILE_RECOVER;
1674
1675         // XXX wm in context is not set correctly after WM_read_file -> crash
1676         // do it before for now, but is this correct with multiple windows?
1677         WM_event_add_notifier(C, NC_WINDOW, NULL);
1678
1679         /* load file */
1680         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1681         WM_read_file(C, filename, op->reports);
1682
1683         G.fileflags &= ~G_FILE_RECOVER;
1684         return OPERATOR_FINISHED;
1685 }
1686
1687 static void WM_OT_recover_last_session(wmOperatorType *ot)
1688 {
1689         ot->name= "Recover Last Session";
1690         ot->idname= "WM_OT_recover_last_session";
1691         ot->description="Open the last closed file (\"quit.blend\")";
1692         
1693         ot->exec= wm_recover_last_session_exec;
1694         ot->poll= WM_operator_winactive;
1695 }
1696
1697 /* *************** recover auto save **************** */
1698
1699 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1700 {
1701         char path[FILE_MAX];
1702
1703         RNA_string_get(op->ptr, "filepath", path);
1704
1705         G.fileflags |= G_FILE_RECOVER;
1706
1707         // XXX wm in context is not set correctly after WM_read_file -> crash
1708         // do it before for now, but is this correct with multiple windows?
1709         WM_event_add_notifier(C, NC_WINDOW, NULL);
1710
1711         /* load file */
1712         WM_read_file(C, path, op->reports);
1713
1714         G.fileflags &= ~G_FILE_RECOVER;
1715
1716         return OPERATOR_FINISHED;
1717 }
1718
1719 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
1720 {
1721         char filename[FILE_MAX];
1722
1723         wm_autosave_location(filename);
1724         RNA_string_set(op->ptr, "filepath", filename);
1725         WM_event_add_fileselect(C, op);
1726
1727         return OPERATOR_RUNNING_MODAL;
1728 }
1729
1730 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1731 {
1732         ot->name= "Recover Auto Save";
1733         ot->idname= "WM_OT_recover_auto_save";
1734         ot->description="Open an automatically saved file to recover it";
1735         
1736         ot->exec= wm_recover_auto_save_exec;
1737         ot->invoke= wm_recover_auto_save_invoke;
1738         ot->poll= WM_operator_winactive;
1739
1740         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1741 }
1742
1743 /* *************** save file as **************** */
1744
1745 static void untitled(char *name)
1746 {
1747         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1748                 char *c= BLI_last_slash(name);
1749                 
1750                 if(c)
1751                         strcpy(&c[1], "untitled.blend");
1752                 else
1753                         strcpy(name, "untitled.blend");
1754         }
1755 }
1756
1757 static void save_set_compress(wmOperator *op)
1758 {
1759         if(!RNA_property_is_set(op->ptr, "compress")) {
1760                 if(G.save_over) /* keep flag for existing file */
1761                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1762                 else /* use userdef for new file */
1763                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1764         }
1765 }
1766
1767 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1768 {
1769         char name[FILE_MAX];
1770
1771         save_set_compress(op);
1772         
1773         BLI_strncpy(name, G.sce, FILE_MAX);
1774         untitled(name);
1775         RNA_string_set(op->ptr, "filepath", name);
1776         
1777         WM_event_add_fileselect(C, op);
1778
1779         return OPERATOR_RUNNING_MODAL;
1780 }
1781
1782 /* function used for WM_OT_save_mainfile too */
1783 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1784 {
1785         char path[FILE_MAX];
1786         int fileflags;
1787         int copy=0;
1788
1789         save_set_compress(op);
1790         
1791         if(RNA_property_is_set(op->ptr, "filepath"))
1792                 RNA_string_get(op->ptr, "filepath", path);
1793         else {
1794                 BLI_strncpy(path, G.sce, FILE_MAX);
1795                 untitled(path);
1796         }
1797
1798         if(RNA_property_is_set(op->ptr, "copy"))
1799                 copy = RNA_boolean_get(op->ptr, "copy");
1800         
1801         fileflags= G.fileflags;
1802
1803         /* set compression flag */
1804         if(RNA_boolean_get(op->ptr, "compress"))                fileflags |=  G_FILE_COMPRESS;
1805         else                                                                                    fileflags &= ~G_FILE_COMPRESS;
1806         if(RNA_boolean_get(op->ptr, "relative_remap"))  fileflags |=  G_FILE_RELATIVE_REMAP;
1807         else                                                                                    fileflags &= ~G_FILE_RELATIVE_REMAP;
1808
1809         if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1810                 return OPERATOR_CANCELLED;
1811
1812         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1813
1814         return OPERATOR_FINISHED;
1815 }
1816
1817 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1818 {
1819         ot->name= "Save As Blender File";
1820         ot->idname= "WM_OT_save_as_mainfile";
1821         ot->description="Save the current file in the desired location";
1822         
1823         ot->invoke= wm_save_as_mainfile_invoke;
1824         ot->exec= wm_save_as_mainfile_exec;
1825         ot->poll= WM_operator_winactive;
1826         
1827         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1828         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1829         RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1830         RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1831 }
1832
1833 /* *************** save file directly ******** */
1834
1835 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1836 {
1837         char name[FILE_MAX];
1838         int check_existing=1;
1839         
1840         /* cancel if no active window */
1841         if (CTX_wm_window(C) == NULL)
1842                 return OPERATOR_CANCELLED;
1843
1844         save_set_compress(op);
1845         
1846         BLI_strncpy(name, G.sce, FILE_MAX);
1847         untitled(name);
1848         RNA_string_set(op->ptr, "filepath", name);
1849         
1850         if (RNA_struct_find_property(op->ptr, "check_existing"))
1851                 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1852                         check_existing = 0;
1853         
1854         if (G.save_over) {
1855                 if (check_existing)
1856                         uiPupMenuSaveOver(C, op, name);
1857                 else {
1858                         wm_save_as_mainfile_exec(C, op);
1859                 }
1860         } else {
1861                 WM_event_add_fileselect(C, op);
1862         }
1863         
1864         return OPERATOR_RUNNING_MODAL;
1865 }
1866
1867 static void WM_OT_save_mainfile(wmOperatorType *ot)
1868 {
1869         ot->name= "Save Blender File";
1870         ot->idname= "WM_OT_save_mainfile";
1871         ot->description="Save the current Blender file";
1872         
1873         ot->invoke= wm_save_mainfile_invoke;
1874         ot->exec= wm_save_as_mainfile_exec;
1875         ot->poll= NULL;
1876         
1877         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1878         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1879         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1880 }
1881
1882 /* XXX: move these collada operators to a more appropriate place */
1883 #ifdef WITH_COLLADA
1884
1885 #include "../../collada/collada.h"
1886
1887 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *event)
1888 {       
1889         if(!RNA_property_is_set(op->ptr, "filepath")) {
1890                 char *path = BLI_replacestr(G.sce, ".blend", ".dae");
1891                 RNA_string_set(op->ptr, "filepath", path);
1892                 MEM_freeN(path);
1893         }
1894
1895         WM_event_add_fileselect(C, op);
1896
1897         return OPERATOR_RUNNING_MODAL;
1898 }
1899
1900 /* function used for WM_OT_save_mainfile too */
1901 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1902 {
1903         char filename[FILE_MAX];
1904         
1905         if(!RNA_property_is_set(op->ptr, "filepath")) {
1906                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1907                 return OPERATOR_CANCELLED;
1908         }
1909         
1910         RNA_string_get(op->ptr, "filepath", filename);
1911         collada_export(CTX_data_scene(C), filename);
1912         
1913         return OPERATOR_FINISHED;
1914 }
1915
1916 static void WM_OT_collada_export(wmOperatorType *ot)
1917 {
1918         ot->name= "Export COLLADA";
1919         ot->idname= "WM_OT_collada_export";
1920         
1921         ot->invoke= wm_collada_export_invoke;
1922         ot->exec= wm_collada_export_exec;
1923         ot->poll= WM_operator_winactive;
1924         
1925         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1926 }
1927
1928 /* function used for WM_OT_save_mainfile too */
1929 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1930 {
1931         char filename[FILE_MAX];
1932         
1933         if(!RNA_property_is_set(op->ptr, "filepath")) {
1934                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1935                 return OPERATOR_CANCELLED;
1936         }
1937
1938         RNA_string_get(op->ptr, "filepath", filename);
1939         collada_import(C, filename);
1940         
1941         return OPERATOR_FINISHED;
1942 }
1943
1944 static void WM_OT_collada_import(wmOperatorType *ot)
1945 {
1946         ot->name= "Import COLLADA";
1947         ot->idname= "WM_OT_collada_import";
1948         
1949         ot->invoke= WM_operator_filesel;
1950         ot->exec= wm_collada_import_exec;
1951         ot->poll= WM_operator_winactive;
1952         
1953         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1954 }
1955
1956 #endif
1957
1958
1959
1960 /* *********************** */
1961
1962 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1963 {
1964         ot->name= "Toggle Fullscreen";
1965         ot->idname= "WM_OT_window_fullscreen_toggle";
1966         ot->description="Toggle the current window fullscreen";
1967
1968         ot->exec= wm_window_fullscreen_toggle_op;
1969         ot->poll= WM_operator_winactive;
1970 }
1971
1972 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1973 {
1974         WM_operator_free(op);
1975         
1976         WM_exit(C);     
1977         
1978         return OPERATOR_FINISHED;
1979 }
1980
1981 static void WM_OT_exit_blender(wmOperatorType *ot)
1982 {
1983         ot->name= "Exit Blender";
1984         ot->idname= "WM_OT_exit_blender";
1985         ot->description= "Quit Blender";
1986
1987         ot->invoke= WM_operator_confirm;
1988         ot->exec= wm_exit_blender_op;
1989         ot->poll= WM_operator_winactive;
1990 }
1991
1992 /* ************ default paint cursors, draw always around cursor *********** */
1993 /*
1994  - returns handler to free 
1995  - poll(bContext): returns 1 if draw should happen
1996  - draw(bContext): drawing callback for paint cursor
1997 */
1998
1999 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2000                                    wmPaintCursorDraw draw, void *customdata)
2001 {
2002         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2003         
2004         BLI_addtail(&wm->paintcursors, pc);
2005         
2006         pc->customdata = customdata;
2007         pc->poll= poll;
2008         pc->draw= draw;
2009         
2010         return pc;
2011 }
2012
2013 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2014 {
2015         wmPaintCursor *pc;
2016         
2017         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2018                 if(pc == (wmPaintCursor *)handle) {
2019                         BLI_remlink(&wm->paintcursors, pc);
2020                         MEM_freeN(pc);
2021                         return;
2022                 }
2023         }
2024 }
2025
2026 /* ************ window gesture operator-callback definitions ************** */
2027 /*
2028  * These are default callbacks for use in operators requiring gesture input
2029  */
2030
2031 /* **************** Border gesture *************** */
2032
2033 /* Border gesture has two types:
2034    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
2035    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2036
2037    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2038 */
2039
2040 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2041 {
2042         wmGesture *gesture= op->customdata;
2043         rcti *rect= gesture->customdata;
2044         
2045         if(rect->xmin > rect->xmax)
2046                 SWAP(int, rect->xmin, rect->xmax);
2047         if(rect->ymin > rect->ymax)
2048                 SWAP(int, rect->ymin, rect->ymax);
2049         
2050         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2051                 return 0;
2052                 
2053         /* operator arguments and storage. */
2054         RNA_int_set(op->ptr, "xmin", rect->xmin);
2055         RNA_int_set(op->ptr, "ymin", rect->ymin);
2056         RNA_int_set(op->ptr, "xmax", rect->xmax);
2057         RNA_int_set(op->ptr, "ymax", rect->ymax);
2058         
2059         /* XXX weak; border should be configured for this without reading event types */
2060         if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2061                 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2062
2063         op->type->exec(C, op);
2064         
2065         return 1;
2066 }
2067
2068 static void wm_gesture_end(bContext *C, wmOperator *op)
2069 {
2070         wmGesture *gesture= op->customdata;
2071         
2072         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2073         op->customdata= NULL;
2074
2075         ED_area_tag_redraw(CTX_wm_area(C));
2076         
2077         if( RNA_struct_find_property(op->ptr, "cursor") )
2078                 WM_cursor_restore(CTX_wm_window(C));
2079 }
2080
2081 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2082 {
2083         if(ISTWEAK(event->type))
2084                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2085         else
2086                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2087
2088         /* add modal handler */
2089         WM_event_add_modal_handler(C, op);
2090         
2091         wm_gesture_tag_redraw(C);
2092
2093         return OPERATOR_RUNNING_MODAL;
2094 }
2095
2096 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2097 {
2098         wmGesture *gesture= op->customdata;
2099         rcti *rect= gesture->customdata;
2100         int sx, sy;
2101         
2102         if(event->type== MOUSEMOVE) {
2103                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2104
2105                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2106                         rect->xmin= rect->xmax= event->x - sx;
2107                         rect->ymin= rect->ymax= event->y - sy;
2108                 }
2109                 else {
2110                         rect->xmax= event->x - sx;
2111                         rect->ymax= event->y - sy;
2112                 }
2113
2114                 wm_gesture_tag_redraw(C);
2115         }
2116         else if (event->type==EVT_MODAL_MAP) {
2117                 switch (event->val) {
2118                 case GESTURE_MODAL_BEGIN:
2119                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2120                                 gesture->mode= 1;
2121                                 wm_gesture_tag_redraw(C);
2122                         }
2123                         break;
2124                 case GESTURE_MODAL_SELECT:
2125                 case GESTURE_MODAL_DESELECT:
2126                 case GESTURE_MODAL_IN:
2127                 case GESTURE_MODAL_OUT:
2128                         if(border_apply(C, op, event->val)) {
2129                                 wm_gesture_end(C, op);
2130                                 return OPERATOR_FINISHED;
2131                         }
2132                         wm_gesture_end(C, op);
2133                         return OPERATOR_CANCELLED;
2134                         break;
2135
2136                 case GESTURE_MODAL_CANCEL:
2137                         wm_gesture_end(C, op);
2138                         return OPERATOR_CANCELLED;
2139                 }
2140
2141         }
2142 //      // Allow view navigation???
2143 //      else {
2144 //              return OPERATOR_PASS_THROUGH;
2145 //      }
2146
2147         return OPERATOR_RUNNING_MODAL;
2148 }
2149
2150 /* **************** circle gesture *************** */
2151 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2152
2153 #ifdef GESTURE_MEMORY
2154 int circle_select_size= 25; // XXX - need some operator memory thing\!
2155 #endif
2156
2157 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2158 {
2159         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2160         
2161         /* add modal handler */
2162         WM_event_add_modal_handler(C, op);
2163         
2164         wm_gesture_tag_redraw(C);
2165         
2166         return OPERATOR_RUNNING_MODAL;
2167 }
2168
2169 static void gesture_circle_apply(bContext *C, wmOperator *op)
2170 {
2171         wmGesture *gesture= op->customdata;
2172         rcti *rect= gesture->customdata;
2173         
2174         if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2175                 return;
2176
2177         /* operator arguments and storage. */
2178         RNA_int_set(op->ptr, "x", rect->xmin);
2179         RNA_int_set(op->ptr, "y", rect->ymin);
2180         RNA_int_set(op->ptr, "radius", rect->xmax);
2181         
2182         if(op->type->exec)
2183                 op->type->exec(C, op);
2184
2185 #ifdef GESTURE_MEMORY
2186         circle_select_size= rect->xmax;
2187 #endif
2188 }
2189
2190 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2191 {
2192         wmGesture *gesture= op->customdata;
2193         rcti *rect= gesture->customdata;
2194         int sx, sy;
2195
2196         if(event->type== MOUSEMOVE) {
2197                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2198
2199                 rect->xmin= event->x - sx;
2200                 rect->ymin= event->y - sy;
2201
2202                 wm_gesture_tag_redraw(C);
2203
2204                 if(gesture->mode)
2205                         gesture_circle_apply(C, op);
2206         }
2207         else if (event->type==EVT_MODAL_MAP) {
2208                 switch (event->val) {
2209                 case GESTURE_MODAL_CIRCLE_ADD:
2210                         rect->xmax += 2 + rect->xmax/10;
2211                         wm_gesture_tag_redraw(C);
2212                         break;
2213                 case GESTURE_MODAL_CIRCLE_SUB:
2214                         rect->xmax -= 2 + rect->xmax/10;
2215                         if(rect->xmax < 1) rect->xmax= 1;
2216                         wm_gesture_tag_redraw(C);
2217                         break;
2218                 case GESTURE_MODAL_SELECT:
2219                 case GESTURE_MODAL_DESELECT:
2220                 case GESTURE_MODAL_NOP:
2221                         if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2222                                 RNA_int_set(op->ptr, "gesture_mode", event->val);
2223
2224                         if(event->val != GESTURE_MODAL_NOP) {
2225                                 /* apply first click */
2226                                 gesture_circle_apply(C, op);
2227                                 gesture->mode= 1;
2228                                 wm_gesture_tag_redraw(C);
2229                         }
2230                         break;
2231
2232                 case GESTURE_MODAL_CANCEL:
2233                 case GESTURE_MODAL_CONFIRM:
2234                         wm_gesture_end(C, op);
2235                         return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2236                 }
2237         }
2238 //      // Allow view navigation???
2239 //      else {
2240 //              return OPERATOR_PASS_THROUGH;
2241 //      }
2242
2243         return OPERATOR_RUNNING_MODAL;
2244 }
2245
2246 #if 0
2247 /* template to copy from */
2248 void WM_OT_circle_gesture(wmOperatorType *ot)
2249 {
2250         ot->name= "Circle Gesture";
2251         ot->idname= "WM_OT_circle_gesture";
2252         ot->description="Enter rotate mode with a circular gesture";
2253         
2254         ot->invoke= WM_gesture_circle_invoke;
2255         ot->modal= WM_gesture_circle_modal;
2256         
2257         ot->poll= WM_operator_winactive;
2258         
2259         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2260         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2261         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2262
2263 }
2264 #endif
2265
2266 /* **************** Tweak gesture *************** */
2267
2268 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2269 {
2270         wmWindow *window= CTX_wm_window(C);
2271         wmGesture *gesture= window->tweak;
2272         rcti *rect= gesture->customdata;
2273         int sx, sy, val;
2274         
2275         switch(event->type) {
2276                 case MOUSEMOVE:
2277                 case INBETWEEN_MOUSEMOVE:
2278                         
2279                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2280                         
2281                         rect->xmax= event->x - sx;
2282                         rect->ymax= event->y - sy;
2283                         
2284                         if((val= wm_gesture_evaluate(C, gesture))) {
2285                                 wmEvent event;
2286
2287                                 event= *(window->eventstate);
2288                                 if(gesture->event_type==LEFTMOUSE)
2289                                         event.type= EVT_TWEAK_L;
2290                                 else if(gesture->event_type==RIGHTMOUSE)
2291                                         event.type= EVT_TWEAK_R;
2292                                 else
2293                                         event.type= EVT_TWEAK_M;
2294                                 event.val= val;
2295                                 /* mouse coords! */
2296                                 wm_event_add(window, &event);
2297                                 
2298                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2299                         }
2300                         
2301                         break;
2302                         
2303                 case LEFTMOUSE:
2304                 case RIGHTMOUSE:
2305                 case MIDDLEMOUSE:
2306                         if(gesture->event_type==event->type) {
2307                                 WM_gesture_end(C, gesture);
2308
2309                                 /* when tweak fails we should give the other keymap entries a chance */
2310                                 event->val= KM_RELEASE;
2311                         }
2312                         break;
2313                 default:
2314                         if(!ISTIMER(event->type)) {
2315                                 WM_gesture_end(C, gesture);
2316                         }
2317                         break;
2318         }
2319 }
2320
2321 /* standard tweak, called after window handlers passed on event */
2322 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2323 {
2324         wmWindow *win= CTX_wm_window(C);
2325         
2326         if(win->tweak==NULL) {
2327                 if(CTX_wm_region(C)) {
2328                         if(event->val==KM_PRESS) { 
2329                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2330                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2331                         }
2332                 }
2333         }
2334         else {
2335                 /* no tweaks if event was handled */
2336                 if((action & WM_HANDLER_BREAK)) {
2337                         WM_gesture_end(C, win->tweak);
2338                 }
2339                 else
2340                         tweak_gesture_modal(C, event);
2341         }
2342 }
2343
2344 /* *********************** lasso gesture ****************** */
2345
2346 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2347 {
2348         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2349         
2350         /* add modal handler */
2351         WM_event_add_modal_handler(C, op);
2352         
2353         wm_gesture_tag_redraw(C);
2354         
2355         if( RNA_struct_find_property(op->ptr, "cursor") )
2356                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2357         
2358         return OPERATOR_RUNNING_MODAL;
2359 }
2360
2361 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2362 {
2363         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2364         
2365         /* add modal handler */
2366         WM_event_add_modal_handler(C, op);
2367         
2368         wm_gesture_tag_redraw(C);
2369         
2370         if( RNA_struct_find_property(op->ptr, "cursor") )
2371                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2372         
2373         return OPERATOR_RUNNING_MODAL;
2374 }
2375
2376
2377 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
2378 {
2379         wmGesture *gesture= op->customdata;
2380         PointerRNA itemptr;
2381         float loc[2];
2382         int i;
2383         short *lasso= gesture->customdata;
2384         
2385         /* operator storage as path. */
2386
2387         for(i=0; i<gesture->points; i++, lasso+=2) {
2388                 loc[0]= lasso[0];
2389                 loc[1]= lasso[1];
2390                 RNA_collection_add(op->ptr, "path", &itemptr);
2391                 RNA_float_set_array(&itemptr, "loc", loc);
2392         }
2393         
2394         wm_gesture_end(C, op);
2395                 
2396         if(op->type->exec)
2397                 op->type->exec(C, op);
2398         
2399 }
2400
2401 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2402 {
2403         wmGesture *gesture= op->customdata;
2404         int sx, sy;
2405         
2406         switch(event->type) {
2407                 case MOUSEMOVE:
2408                 case INBETWEEN_MOUSEMOVE:
2409                         
2410                         wm_gesture_tag_redraw(C);
2411                         
2412                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2413
2414                         if(gesture->points == gesture->size) {
2415                                 short *old_lasso = gesture->customdata;
2416                                 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2417                                 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2418                                 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2419                                 MEM_freeN(old_lasso);
2420                                 printf("realloc\n");
2421                         }
2422
2423                         {
2424                                 short *lasso= gesture->customdata;
2425                                 lasso += 2 * gesture->points;
2426                                 lasso[0] = event->x - sx;
2427                                 lasso[1] = event->y - sy;
2428                                 gesture->points++;
2429                         }
2430                         break;
2431                         
2432                 case LEFTMOUSE:
2433                 case MIDDLEMOUSE:
2434                 case RIGHTMOUSE:
2435                         if(event->val==KM_RELEASE) {    /* key release */
2436                                 gesture_lasso_apply(C, op, event->type);
2437                                 return OPERATOR_FINISHED;
2438                         }
2439                         break;
2440                 case ESCKEY:
2441                         wm_gesture_end(C, op);
2442                         return OPERATOR_CANCELLED;
2443         }
2444         return OPERATOR_RUNNING_MODAL;
2445 }
2446
2447 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2448 {
2449         return WM_gesture_lasso_modal(C, op, event);
2450 }
2451
2452 #if 0
2453 /* template to copy from */
2454
2455 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2456 {
2457         RNA_BEGIN(op->ptr, itemptr, "path") {
2458                 float loc[2];
2459                 
2460                 RNA_float_get_array(&itemptr, "loc", loc);
2461                 printf("Location: %f %f\n", loc[0], loc[1]);
2462         }
2463         RNA_END;
2464         
2465         return OPERATOR_FINISHED;
2466 }
2467
2468 void WM_OT_lasso_gesture(wmOperatorType *ot)
2469 {
2470         PropertyRNA *prop;
2471         
2472         ot->name= "Lasso Gesture";
2473         ot->idname= "WM_OT_lasso_gesture";
2474         ot->description="Select objects within the lasso as you move the pointer";
2475         
2476         ot->invoke= WM_gesture_lasso_invoke;
2477         ot->modal= WM_gesture_lasso_modal;
2478         ot->exec= gesture_lasso_exec;
2479         
2480         ot->poll= WM_operator_winactive;
2481         
2482         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2483         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2484 }
2485 #endif
2486
2487 /* *********************** straight line gesture ****************** */
2488
2489 static int straightline_apply(bContext *C, wmOperator *op)
2490 {
2491         wmGesture *gesture= op->customdata;
2492         rcti *rect= gesture->customdata;
2493         
2494         if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2495                 return 0;
2496         
2497         /* operator arguments and storage. */
2498         RNA_int_set(op->ptr, "xstart", rect->xmin);
2499         RNA_int_set(op->ptr, "ystart", rect->ymin);
2500         RNA_int_set(op->ptr, "xend", rect->xmax);
2501         RNA_int_set(op->ptr, "yend", rect->ymax);
2502
2503         if(op->type->exec)
2504                 op->type->exec(C, op);
2505         
2506         return 1;
2507 }
2508
2509
2510 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2511 {
2512         op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2513         
2514         /* add modal handler */
2515         WM_event_add_modal_handler(C, op);
2516         
2517         wm_gesture_tag_redraw(C);
2518         
2519         if( RNA_struct_find_property(op->ptr, "cursor") )
2520                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2521                 
2522         return OPERATOR_RUNNING_MODAL;
2523 }
2524
2525 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2526 {
2527         wmGesture *gesture= op->customdata;
2528         rcti *rect= gesture->customdata;
2529         int sx, sy;
2530         
2531         if(event->type== MOUSEMOVE) {
2532                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2533                 
2534                 if(gesture->mode==0) {
2535                         rect->xmin= rect->xmax= event->x - sx;
2536                         rect->ymin= rect->ymax= event->y - sy;
2537                 }
2538                 else {
2539                         rect->xmax= event->x - sx;
2540                         rect->ymax= event->y - sy;
2541                         straightline_apply(C, op);
2542                 }
2543                 
2544                 wm_gesture_tag_redraw(C);
2545         }
2546         else if (event->type==EVT_MODAL_MAP) {
2547                 switch (event->val) {
2548                         case GESTURE_MODAL_BEGIN:
2549                                 if(gesture->mode==0) {
2550                                         gesture->mode= 1;
2551                                         wm_gesture_tag_redraw(C);
2552                                 }
2553                                 break;
2554                         case GESTURE_MODAL_SELECT:
2555                                 if(straightline_apply(C, op)) {
2556                                         wm_gesture_end(C, op);
2557                                         return OPERATOR_FINISHED;
2558                                 }
2559                                 wm_gesture_end(C, op);
2560                                 return OPERATOR_CANCELLED;
2561                                 break;
2562                                 
2563                         case GESTURE_MODAL_CANCEL:
2564                                 wm_gesture_end(C, op);
2565                                 return OPERATOR_CANCELLED;
2566                 }
2567                 
2568         }
2569
2570         return OPERATOR_RUNNING_MODAL;
2571 }
2572
2573 #if 0
2574 /* template to copy from */
2575 void WM_OT_straightline_gesture(wmOperatorType *ot)
2576 {
2577         PropertyRNA *prop;
2578         
2579         ot->name= "Straight Line Gesture";
2580         ot->idname= "WM_OT_straightline_gesture";
2581         ot->description="Draw a straight line as you move the pointer";
2582         
2583         ot->invoke= WM_gesture_straightline_invoke;
2584         ot->modal= WM_gesture_straightline_modal;
2585         ot->exec= gesture_straightline_exec;
2586         
2587         ot->poll= WM_operator_winactive;
2588         
2589         WM_operator_properties_gesture_straightline(ot, 0);
2590 }
2591 #endif
2592
2593 /* *********************** radial control ****************** */
2594
2595 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2596
2597 typedef struct wmRadialControl {
2598         int mode;
2599         float initial_value, value, max_value;
2600         int initial_mouse[2];
2601         void *cursor;
2602         GLuint tex;
2603 } wmRadialControl;
2604
2605 extern Paint *paint_get_active(Scene *sce);
2606 extern struct Brush *paint_brush(struct Paint *paint);
2607
2608 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2609 {
2610         wmRadialControl *rc = (wmRadialControl*)customdata;
2611         ARegion *ar = CTX_wm_region(C);
2612         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2613
2614         Paint *paint = paint_get_active(CTX_data_scene(C));
2615         Brush *brush = paint_brush(paint);
2616
2617         ViewContext vc;
2618
2619         // int hit = 0;
2620
2621         int flip;
2622         int sign;
2623
2624         float* col;
2625
2626         const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : (brush->texture_overlay_alpha / 100.0f);
2627
2628         if(rc->mode == WM_RADIALCONTROL_SIZE) {
2629                 r1= rc->value;
2630                 r2= rc->initial_value;
2631                 r3= r1;
2632         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2633                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2634                 r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2635         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2636                 r1= r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2637                 angle = rc->value;
2638         }
2639
2640         /* Keep cursor in the original place */
2641         x = rc->initial_mouse[0] - ar->winrct.xmin;
2642         y = rc->initial_mouse[1] - ar->winrct.ymin;
2643
2644         view3d_set_viewcontext(C, &vc);
2645
2646         // XXX: no way currently to know state of pen flip or invert key modifier without starting a stroke
2647         flip = 1;
2648
2649         sign = flip * ((brush->flag & BRUSH_DIR_IN)? -1 : 1);
2650
2651         if (sign < 0 && ELEM4(brush->sculpt_tool, SCULPT_TOOL_DRAW, SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_PINCH))
2652                 col = brush->sub_col;
2653         else
2654                 col = brush->add_col;
2655
2656         glTranslatef((float)x, (float)y, 0.0f);
2657
2658         glEnable(GL_BLEND);
2659
2660         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2661                 glRotatef(angle, 0, 0, 1);
2662         }
2663
2664         if (rc->tex) {
2665                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2666
2667                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2668                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2669
2670                 glEnable(GL_TEXTURE_2D);
2671                 glBegin(GL_QUADS);
2672                 glColor4f(U.sculpt_paint_overlay_col[0],U.sculpt_paint_overlay_col[1],U.sculpt_paint_overlay_col[2], str);
2673                 glTexCoord2f(0,0);
2674                 glVertex2f(-r3, -r3);
2675                 glTexCoord2f(1,0);
2676                 glVertex2f(r3, -r3);
2677                 glTexCoord2f(1,1);
2678                 glVertex2f(r3, r3);
2679                 glTexCoord2f(0,1);
2680                 glVertex2f(-r3, r3);
2681                 glEnd();
2682                 glDisable(GL_TEXTURE_2D);
2683         }
2684
2685         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2686                 glColor4f(col[0], col[1], col[2], 0.5f);
2687                 glEnable(GL_LINE_SMOOTH);
2688                 glRotatef(-angle, 0, 0, 1);
2689                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2690                 glRotatef(angle, 0, 0, 1);
2691                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2692                 glDisable(GL_LINE_SMOOTH);
2693         }
2694
2695         glColor4f(col[0], col[1], col[2], 0.5f);
2696         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2697         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2698         glDisable(GL_BLEND);
2699         
2700         glPopMatrix();
2701 }
2702
2703 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2704 {
2705         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2706         int mode, initial_mouse[2], delta[2];
2707         float dist;
2708         double new_value = RNA_float_get(op->ptr, "new_value");
2709         int ret = OPERATOR_RUNNING_MODAL;
2710         // float initial_value = RNA_float_get(op->ptr, "initial_value");
2711
2712         mode = RNA_int_get(op->ptr, "mode");
2713         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2714
2715         switch(event->type) {
2716         case MOUSEMOVE:
2717                 delta[0]= initial_mouse[0] - event->x;
2718                 delta[1]= initial_mouse[1] - event->y;
2719
2720                 //if (mode == WM_RADIALCONTROL_SIZE) 
2721                 //      delta[0]+= initial_value;
2722                 //else if(mode == WM_RADIALCONTROL_STRENGTH)
2723                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2724                 //else if(mode == WM_RADIALCONTROL_ANGLE) {
2725                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value*M_PI/180.0f);
2726                 //      delta[1]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value*M_PI/180.0f);
2727                 //}
2728
2729                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2730
2731                 if(mode == WM_RADIALCONTROL_SIZE)
2732                         new_value = dist;
2733                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2734                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2735                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2736                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2737                 
2738                 if(event->ctrl) {
2739                         if(mode == WM_RADIALCONTROL_STRENGTH)
2740                                 new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2741                         else
2742                                 new_value = ((int)new_value + 5) / 10*10;
2743                 }
2744                 
2745                 break;
2746         case ESCKEY:
2747         case RIGHTMOUSE:
2748                 ret = OPERATOR_CANCELLED;
2749                 break;
2750         case LEFTMOUSE:
2751         case PADENTER:
2752                 op->type->exec(C, op);
2753                 ret = OPERATOR_FINISHED;
2754                 break;
2755         }
2756
2757         /* Clamp */
2758         if(new_value > rc->max_value)
2759                 new_value = rc->max_value;
2760         else if(new_value < 0)
2761                 new_value = 0;
2762
2763         /* Update paint data */
2764         rc->value = new_value;
2765
2766         RNA_float_set(op->ptr, "new_value", new_value);
2767
2768         if(ret != OPERATOR_RUNNING_MODAL) {
2769                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2770                 MEM_freeN(rc);
2771         }
2772         
2773         ED_region_tag_redraw(CTX_wm_region(C));
2774
2775         //if (ret != OPERATOR_RUNNING_MODAL) {
2776         //      wmWindow *win = CTX_wm_window(C);
2777         //      WM_cursor_restore(win);
2778         //}
2779
2780         return ret;
2781 }
2782
2783 /* Expects the operator customdata to be an ImBuf (or NULL) */
2784 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2785 {
2786         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2787         // wmWindow *win = CTX_wm_window(C);
2788         int mode = RNA_int_get(op->ptr, "mode");
2789         float initial_value = RNA_float_get(op->ptr, "initial_value");
2790         //float initial_size = RNA_float_get(op->ptr, "initial_size");
2791         int mouse[2] = {event->x, event->y};
2792
2793         //if (initial_size == 0)
2794         //      initial_size = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2795
2796         if(mode == WM_RADIALCONTROL_SIZE) {
2797                 rc->max_value = 200;
2798                 mouse[0]-= initial_value;
2799         }
2800         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2801                 rc->max_value = 1;
2802                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2803         }
2804         else if(mode == WM_RADIALCONTROL_ANGLE) {
2805                 rc->max_value = 360;
2806                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2807                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2808                 initial_value *= 180.0f/M_PI;
2809         }
2810
2811         if(op->customdata) {
2812                 ImBuf *im = (ImBuf*)op->customdata;
2813                 /* Build GL texture */
2814                 glGenTextures(1, &rc->tex);
2815                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2816                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2817                 MEM_freeN(im->rect_float);
2818                 MEM_freeN(im);
2819         }
2820
2821         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2822         RNA_float_set(op->ptr, "new_value", initial_value);
2823                 
2824         op->customdata = rc;
2825         rc->mode = mode;
2826         rc->initial_value = initial_value;
2827         rc->initial_mouse[0] = mouse[0];
2828         rc->initial_mouse[1] = mouse[1];
2829         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2830                                                   wm_radial_control_paint, op->customdata);
2831
2832         //WM_cursor_modal(win, CURSOR_NONE);
2833
2834         /* add modal handler */
2835         WM_event_add_modal_handler(C, op);
2836         
2837         WM_radial_control_modal(C, op, event);
2838         
2839         return OPERATOR_RUNNING_MODAL;
2840 }
2841
2842 /* Gets a descriptive string of the operation */
2843 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2844 {
2845         int mode = RNA_int_get(op->ptr, "mode");
2846         float v = RNA_float_get(op->ptr, "new_value");
2847
2848         if(mode == WM_RADIALCONTROL_SIZE)
2849                 sprintf(str, "Size: %d", (int)v);
2850         else if(mode == WM_RADIALCONTROL_STRENGTH)
2851                 sprintf(str, "Strength: %d", (int)v);
2852         else if(mode == WM_RADIALCONTROL_ANGLE)
2853                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
2854 }
2855
2856 /** Important: this doesn't define an actual operator, it
2857         just sets up the common parts of the radial control op. **/
2858 void WM_OT_radial_control_partial(wmOperatorType *ot)
2859 {
2860         static EnumPropertyItem radial_mode_items[] = {
2861                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2862                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2863                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2864                 {0, NULL, 0, NULL, NULL}};
2865
2866         /* Should be set in custom invoke() */
2867         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2868
2869         /* Set internally, should be used in custom exec() to get final value */
2870         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2871
2872         /* Should be set before calling operator */
2873         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2874
2875         /* Internal */
2876         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
2877 }
2878
2879 /* ************************** timer for testing ***************** */
2880
2881 /* uses no type defines, fully local testing function anyway... ;) */
2882
2883 static void redraw_timer_window_swap(bContext *C)
2884 {
2885         wmWindow *win= CTX_wm_window(C);
2886         ScrArea *sa;
2887
2888         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2889                 ED_area_tag_redraw(sa);
2890         wm_draw_update(C);
2891
2892         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2893 }
2894
2895 static EnumPropertyItem redraw_timer_type_items[] = {
2896         {0, "DRAW", 0, "Draw Region", "Draw Region"},
2897         {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2898         {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2899         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2900         {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2901         {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2902         {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2903         {0, NULL, 0, NULL, NULL}};
2904
2905 static int redraw_timer_exec(bContext *C, wmOperator *op)
2906 {
2907         ARegion *ar= CTX_wm_region(C);
2908         double stime= PIL_check_seconds_timer();
2909         int type = RNA_int_get(op->ptr, "type");
2910         int iter = RNA_int_get(op->ptr, "iterations");
2911         int a;
2912         float time;
2913         const char *infostr= "";
2914         
2915         WM_cursor_wait(1);
2916
2917         for(a=0; a<iter; a++) {
2918                 if (type==0) {
2919                         ED_region_do_draw(C, ar);
2920                 } 
2921                 else if (type==1) {
2922                         wmWindow *win= CTX_wm_window(C);
2923                         
2924                         ED_region_tag_redraw(ar);
2925                         wm_draw_update(C);
2926                         
2927                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2928                 }
2929                 else if (type==2) {
2930                         wmWindow *win= CTX_wm_window(C);
2931                         ScrArea *sa;
2932                         
2933                         ScrArea *sa_back= CTX_wm_area(C);
2934                         ARegion *ar_back= CTX_wm_region(C);
2935
2936                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2937                                 ARegion *ar_iter;
2938                                 CTX_wm_area_set(C, sa);
2939
2940                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2941                                         if(ar_iter->swinid) {
2942                                                 CTX_wm_region_set(C, ar_iter);
2943                                                 ED_region_do_draw(C, ar_iter);
2944                                         }
2945                                 }
2946                         }
2947
2948                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2949
2950                         CTX_wm_area_set(C, sa_back);
2951                         CTX_wm_region_set(C, ar_back);
2952                 }
2953                 else if (type==3) {
2954                         redraw_timer_window_swap(C);
2955                 }
2956                 else if (type==4) {
2957                         Scene *scene= CTX_data_scene(C);
2958                         
2959                         if(a & 1) scene->r.cfra--;
2960                         else scene->r.cfra++;
2961                         scene_update_for_newframe(scene, scene->lay);
2962                 }
2963                 else if (type==5) {
2964
2965                         /* play anim, return on same frame as started with */
2966                         Scene *scene= CTX_data_scene(C);
2967                         int tot= (scene->r.efra - scene->r.sfra) + 1;
2968
2969                         while(tot--) {
2970                                 /* todo, ability to escape! */
2971                                 scene->r.cfra++;
2972                                 if(scene->r.cfra > scene->r.efra)
2973                                         scene->r.cfra= scene->r.sfra;
2974
2975                                 scene_update_for_newframe(scene, scene->lay);
2976                                 redraw_timer_window_swap(C);
2977                         }
2978                 }
2979                 else { /* 6 */
2980                         ED_undo_pop(C);
2981                         ED_undo_redo(C);
2982                 }
2983         }
2984         
2985         time= ((PIL_check_seconds_timer()-stime)*1000);
2986
2987         RNA_enum_description(redraw_timer_type_items, type, &infostr);
2988
2989         WM_cursor_wait(0);
2990
2991         BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
2992         
2993         return OPERATOR_FINISHED;
2994 }
2995
2996 static void WM_OT_redraw_timer(wmOperatorType *ot)
2997 {
2998         ot->name= "Redraw Timer";
2999         ot->idname= "WM_OT_redraw_timer";
3000         ot->description="Simple redraw timer to test the speed of updating the interface";
3001
3002         ot->invoke= WM_menu_invoke;
3003         ot->exec= redraw_timer_exec;
3004         ot->poll= WM_operator_winactive;
3005
3006         ot->prop= RNA_def_enum(ot->srna, "type", redraw_timer_type_items, 0, "Type", "");
3007         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
3008
3009 }
3010
3011 /* ************************** memory statistics for testing ***************** */
3012
3013 static int memory_statistics_exec(bContext *C, wmOperator *op)
3014 {
3015         MEM_printmemlist_stats();
3016         return OPERATOR_FINISHED;
3017 }
3018
3019 static void WM_OT_memory_statistics(wmOperatorType *ot)
3020 {
3021         ot->name= "Memory Statistics";
3022         ot->idname= "WM_OT_memory_statistics";
3023         ot->description= "Print memory statistics to the console";
3024         
3025         ot->exec= memory_statistics_exec;
3026 }
3027
3028 /* ******************************************************* */
3029  
3030 /* called on initialize WM_exit() */
3031 void wm_operatortype_free(void)
3032 {
3033         wmOperatorType *ot;
3034         
3035         for(ot= global_ops.first; ot; ot= ot->next) {
3036                 if(ot->macro.first)
3037                         wm_operatortype_free_macro(ot);
3038
3039                 if(ot->ext.srna) /* python operator, allocs own string */
3040                         MEM_freeN(ot->idname);
3041         }
3042         
3043         BLI_freelistN(&global_ops);
3044 }
3045
3046 /* called on initialize WM_init() */
3047 void wm_operatortype_init(void)
3048 {
3049         WM_operatortype_append(WM_OT_window_duplicate);
3050         WM_operatortype_append(WM_OT_read_homefile);
3051         WM_operatortype_append(WM_OT_save_homefile);
3052         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
3053         WM_operatortype_append(WM_OT_exit_blender);
3054         WM_operatortype_append(WM_OT_open_mainfile);
3055         WM_operatortype_append(WM_OT_link_append);
3056         WM_operatortype_append(WM_OT_recover_last_session);
3057         WM_operatortype_append(WM_OT_recover_auto_save);
3058         WM_operatortype_append(WM_OT_save_as_mainfile);
3059         WM_operatortype_append(WM_OT_save_mainfile);
3060         WM_operatortype_append(WM_OT_redraw_timer);
3061         WM_operatortype_append(WM_OT_memory_statistics);
3062         WM_operatortype_append(WM_OT_debug_menu);
3063         WM_operatortype_append(WM_OT_splash);
3064         WM_operatortype_append(WM_OT_search_menu);
3065         WM_operatortype_append(WM_OT_call_menu);
3066
3067 #ifdef WITH_COLLADA
3068         /* XXX: move these */
3069         WM_operatortype_append(WM_OT_collada_export);
3070         WM_operatortype_append(WM_OT_collada_import);
3071 #endif
3072
3073 }
3074
3075 /* circleselect-like modal operators */
3076 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
3077 {
3078         static EnumPropertyItem modal_items[] = {
3079         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3080         {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3081         {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
3082         {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
3083
3084         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3085         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3086         {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
3087
3088
3089         {0, NULL, 0, NULL, NULL}};
3090
3091         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
3092
3093         /* this function is called for each spacetype, only needs to add map once */
3094         if(keymap) return;
3095
3096         keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
3097
3098         /* items for modal map */
3099         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3100         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3101
3102         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
3103         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
3104
3105         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
3106
3107 #if 0 // Durien guys like this :S
3108         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3109         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
3110 #else
3111         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); //  defailt 2.4x
3112         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); //  defailt 2.4x
3113 #endif
3114
3115         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
3116
3117         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3118         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3119         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3120         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3121
3122         /* assign map to operators */
3123         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
3124         WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
3125
3126 }
3127
3128 /* straight line modal operators */
3129 static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
3130 {
3131         static EnumPropertyItem modal_items[] = {
3132                 {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3133                 {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3134                 {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3135                 {0, NULL, 0, NULL, NULL}};
3136         
3137         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Straight Line");
3138         
3139         /* this function is called for each spacetype, only needs to add map once */
3140         if(keymap) return;
3141         
3142         keymap= WM_modalkeymap_add(keyconf, "Gesture Straight Line", modal_items);
3143         
3144         /* items for modal map */
3145         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3146         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3147         
3148         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3149         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_SELECT);
3150         
3151         /* assign map to operators */
3152         WM_modalkeymap_assign(keymap, "IMAGE_OT_sample_line");
3153 }
3154
3155
3156 /* borderselect-like modal operators */
3157 static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
3158 {
3159         static EnumPropertyItem modal_items[] = {
3160         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3161         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3162         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3163         {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3164         {0, NULL, 0, NULL, NULL}};
3165
3166         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Border");
3167
3168         /* this function is called for each spacetype, only needs to add map once */
3169         if(keymap) return;
3170
3171         keymap= WM_modalkeymap_add(keyconf, "Gesture Border", modal_items);
3172
3173         /* items for modal map */
3174         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3175         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3176
3177         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3178         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, GESTURE_MODAL_SELECT);
3179
3180 #if 0 // Durian guys like this
3181         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_BEGIN);
3182         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3183 #else
3184         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3185         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_DESELECT);
3186 #endif
3187
3188         /* assign map to operators */
3189         WM_modalkeymap_assign(keymap, "ACTION_OT_select_border");
3190         WM_modalkeymap_assign(keymap, "ANIM_OT_channels_select_border");
3191         WM_modalkeymap_assign(keymap, "ANIM_OT_previewrange_set");
3192         WM_modalkeymap_assign(keymap, "CONSOLE_OT_select_border");
3193         WM_modalkeymap_assign(keymap, "FILE_OT_select_border");
3194         WM_modalkeymap_assign(keymap, "GRAPH_OT_select_border");
3195         WM_modalkeymap_assign(keymap, "MARKER_OT_select_border");
3196         WM_modalkeymap_assign(keymap, "NLA_OT_select_border");
3197         WM_modalkeymap_assign(keymap, "NODE_OT_select_border");
3198 //      WM_modalkeymap_assign(keymap, "SCREEN_OT_border_select"); // template
3199         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_select_border");
3200         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_view_ghost_border");
3201         WM_modalkeymap_assign(keymap, "UV_OT_select_border");
3202         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3203         WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
3204         WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");
3205         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
3206         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); // XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel
3207 }
3208
3209 /* zoom to border modal operators */
3210 static void gesture_zoom_border_modal_keymap(wmKeyConfig *keyconf)
3211 {
3212         static EnumPropertyItem modal_items[] = {
3213         {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3214         {GESTURE_MODAL_IN,      "IN", 0, "In", ""},
3215         {GESTURE_MODAL_OUT, "OUT", 0, "Out", ""},
3216         {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3217         {0, NULL, 0, NULL, NULL}};
3218
3219         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Zoom Border");
3220
3221         /* this function is called for each spacetype, only needs to add map once */
3222         if(keymap) return;
3223
3224         keymap= WM_modalkeymap_add(keyconf, "Gesture Zoom Border", modal_items);
3225
3226         /* items for modal map */
3227         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3228         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3229
3230         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3231         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_IN); 
3232
3233         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3234         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_OUT);
3235
3236         /* assign map to operators */
3237         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3238         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border");
3239 }
3240
3241 /* default keymap for windows and screens, only call once per WM */
3242 void wm_window_keymap(wmKeyConfig *keyconf)
3243 {
3244         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
3245         wmKeyMapItem *kmi;
3246         
3247         /* note, this doesn't replace existing keymap items */
3248         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3249         #ifdef __APPLE__
3250         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
3251         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3252         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
3253         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
3254         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3255         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
3256         #endif
3257         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
3258         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
3259         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3260         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
3261         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
3262         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3263         kmi= WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
3264         RNA_boolean_set(kmi->ptr, "link", FALSE);
3265         RNA_boolean_set(kmi->ptr, "instance_groups", FALSE);
3266
3267         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
3268         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
3269         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3270         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
3271         kmi= WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3272         RNA_boolean_set(kmi->ptr, "copy", 1);
3273
3274         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_ALT, 0);
3275         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
3276
3277         /* debug/testing */
3278         WM_keymap_verify_item(keymap, "WM_OT_redraw_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3279         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3280         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
3281         
3282         /* Space switching */
3283
3284
3285         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
3286         RNA_string_set(kmi->ptr, "data_path", "area.type");
3287         RNA_string_set(kmi->ptr, "value", "LOGIC_EDITOR");
3288
3289         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F3KEY, KM_PRESS, KM_SHIFT, 0);
3290         RNA_string_set(kmi->ptr, "data_path", "area.type");
3291         RNA_string_set(kmi->ptr, "value", "NODE_EDITOR");
3292
3293         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F4KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was data browser */
3294         RNA_string_set(kmi->ptr, "data_path", "area.type");
3295         RNA_string_set(kmi->ptr, "value", "CONSOLE");
3296
3297         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F5KEY, KM_PRESS, KM_SHIFT, 0);
3298         RNA_string_set(kmi->ptr, "data_path", "area.type");