simplify thumbnail reading and remove some warnings
[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, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-250/");
1216         uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:Manual");
1217         uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1218         uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/");
1219         uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/documentation/250PythonDoc/contents.html");
1220         uiItemL(col, "", 0);
1221         
1222         col = uiLayoutColumn(split, 0);
1223         uiItemL(col, "Recent", 0);
1224         for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1225                 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1226         }
1227         uiItemS(col);
1228         uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1229         uiItemL(col, "", 0);
1230         
1231         uiCenteredBoundsBlock(block, 0.0f);
1232         uiEndBlock(C, block);
1233         
1234         return block;
1235 }
1236
1237 static int wm_splash_invoke(bContext *C, wmOperator *op, wmEvent *event)
1238 {
1239         uiPupBlock(C, wm_block_create_splash, NULL);
1240         
1241         return OPERATOR_FINISHED;
1242 }
1243
1244 static void WM_OT_splash(wmOperatorType *ot)
1245 {
1246         ot->name= "Splash Screen";
1247         ot->idname= "WM_OT_splash";
1248         ot->description= "Opens a blocking popup region with release info";
1249         
1250         ot->invoke= wm_splash_invoke;
1251         ot->poll= WM_operator_winactive;
1252 }
1253
1254
1255 /* ***************** Search menu ************************* */
1256 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
1257 {
1258         wmOperatorType *ot= arg2;
1259         
1260         if(ot)
1261                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1262 }
1263
1264 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
1265 {
1266         wmOperatorType *ot = WM_operatortype_first();
1267         
1268         for(; ot; ot= ot->next) {
1269                 
1270                 if(BLI_strcasestr(ot->name, str)) {
1271                         if(WM_operator_poll((bContext*)C, ot)) {
1272                                 char name[256];
1273                                 int len= strlen(ot->name);
1274                                 
1275                                 /* display name for menu, can hold hotkey */
1276                                 BLI_strncpy(name, ot->name, 256);
1277                                 
1278                                 /* check for hotkey */
1279                                 if(len < 256-6) {
1280                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1281                                                 name[len]= '|';
1282                                 }
1283                                 
1284                                 if(0==uiSearchItemAdd(items, name, ot, 0))
1285                                         break;
1286                         }
1287                 }
1288         }
1289 }
1290
1291 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
1292 {
1293         static char search[256]= "";
1294         wmEvent event;
1295         wmWindow *win= CTX_wm_window(C);
1296         uiBlock *block;
1297         uiBut *but;
1298         
1299         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1300         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1301         
1302         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1303         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1304         
1305         /* fake button, it holds space for search items */
1306         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1307         
1308         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
1309         uiEndBlock(C, block);
1310         
1311         event= *(win->eventstate);      /* XXX huh huh? make api call */
1312         event.type= EVT_BUT_OPEN;
1313         event.val= KM_PRESS;
1314         event.customdata= but;
1315         event.customdatafree= FALSE;
1316         wm_event_add(win, &event);
1317         
1318         return block;
1319 }
1320
1321 static int wm_search_menu_exec(bContext *C, wmOperator *op)
1322 {
1323         
1324         return OPERATOR_FINISHED;       
1325 }
1326
1327 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
1328 {
1329         
1330         uiPupBlock(C, wm_block_search_menu, op);
1331         
1332         return OPERATOR_CANCELLED;
1333 }
1334
1335 /* op->poll */
1336 static int wm_search_menu_poll(bContext *C)
1337 {
1338         if(CTX_wm_window(C)==NULL) return 0;
1339         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
1340         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
1341         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
1342         return 1;
1343 }
1344
1345 static void WM_OT_search_menu(wmOperatorType *ot)
1346 {
1347         ot->name= "Search Menu";
1348         ot->idname= "WM_OT_search_menu";
1349         
1350         ot->invoke= wm_search_menu_invoke;
1351         ot->exec= wm_search_menu_exec;
1352         ot->poll= wm_search_menu_poll;
1353 }
1354
1355 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1356 {
1357         char idname[BKE_ST_MAXNAME];
1358         RNA_string_get(op->ptr, "name", idname);
1359
1360         uiPupMenuInvoke(C, idname);
1361
1362         return OPERATOR_CANCELLED;
1363 }
1364
1365 static void WM_OT_call_menu(wmOperatorType *ot)
1366 {
1367         ot->name= "Call Menu";
1368         ot->idname= "WM_OT_call_menu";
1369
1370         ot->exec= wm_call_menu_exec;
1371
1372         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1373 }
1374
1375 /* ************ window / screen operator definitions ************** */
1376
1377 /* this poll functions is needed in place of WM_operator_winactive
1378  * while it crashes on full screen */
1379 static int wm_operator_winactive_normal(bContext *C)
1380 {
1381         wmWindow *win= CTX_wm_window(C);
1382
1383         if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1384                 return 0;
1385
1386         return 1;
1387 }
1388
1389 static void WM_OT_window_duplicate(wmOperatorType *ot)
1390 {
1391         ot->name= "Duplicate Window";
1392         ot->idname= "WM_OT_window_duplicate";
1393         ot->description="Duplicate the current Blender window";
1394                 
1395         ot->exec= wm_window_duplicate_op;
1396         ot->poll= wm_operator_winactive_normal;
1397 }
1398
1399 static void WM_OT_save_homefile(wmOperatorType *ot)
1400 {
1401         ot->name= "Save User Settings";
1402         ot->idname= "WM_OT_save_homefile";
1403         ot->description="Make the current file the default .blend file";
1404                 
1405         ot->invoke= WM_operator_confirm;
1406         ot->exec= WM_write_homefile;
1407         ot->poll= WM_operator_winactive;
1408 }
1409
1410 static void WM_OT_read_homefile(wmOperatorType *ot)
1411 {
1412         ot->name= "Reload Start-Up File";
1413         ot->idname= "WM_OT_read_homefile";
1414         ot->description="Open the default file (doesn't save the current file)";
1415         
1416         ot->invoke= WM_operator_confirm;
1417         ot->exec= WM_read_homefile;
1418         ot->poll= WM_operator_winactive;
1419         
1420         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
1421 }
1422
1423 /* *************** open file **************** */
1424
1425 static void open_set_load_ui(wmOperator *op)
1426 {
1427         if(!RNA_property_is_set(op->ptr, "load_ui"))
1428                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1429 }
1430
1431 static void open_set_use_scripts(wmOperator *op)
1432 {
1433         if(!RNA_property_is_set(op->ptr, "use_scripts"))
1434                 RNA_boolean_set(op->ptr, "use_scripts", !(U.flag & USER_SCRIPT_AUTOEXEC_DISABLE));
1435 }
1436
1437 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1438 {
1439         RNA_string_set(op->ptr, "filepath", G.sce);
1440         open_set_load_ui(op);
1441         open_set_use_scripts(op);
1442
1443         WM_event_add_fileselect(C, op);
1444
1445         return OPERATOR_RUNNING_MODAL;
1446 }
1447
1448 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1449 {
1450         char path[FILE_MAX];
1451
1452         RNA_string_get(op->ptr, "filepath", path);
1453         open_set_load_ui(op);
1454         open_set_use_scripts(op);
1455
1456         if(RNA_boolean_get(op->ptr, "load_ui"))
1457                 G.fileflags &= ~G_FILE_NO_UI;
1458         else
1459                 G.fileflags |= G_FILE_NO_UI;
1460                 
1461         if(RNA_boolean_get(op->ptr, "use_scripts"))
1462                 G.f |= G_SCRIPT_AUTOEXEC;
1463         else
1464                 G.f &= ~G_SCRIPT_AUTOEXEC;
1465         
1466         // XXX wm in context is not set correctly after WM_read_file -> crash
1467         // do it before for now, but is this correct with multiple windows?
1468         WM_event_add_notifier(C, NC_WINDOW, NULL);
1469
1470         WM_read_file(C, path, op->reports);
1471         
1472         return OPERATOR_FINISHED;
1473 }
1474
1475 static void WM_OT_open_mainfile(wmOperatorType *ot)
1476 {
1477         ot->name= "Open Blender File";
1478         ot->idname= "WM_OT_open_mainfile";
1479         ot->description="Open a Blender file";
1480         
1481         ot->invoke= wm_open_mainfile_invoke;
1482         ot->exec= wm_open_mainfile_exec;
1483         ot->poll= WM_operator_winactive;
1484         
1485         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1486
1487         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1488         RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1489 }
1490
1491 /* **************** link/append *************** */
1492
1493 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *event)
1494 {
1495         if(!RNA_property_is_set(op->ptr, "relative_path"))
1496                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1497
1498         if(RNA_property_is_set(op->ptr, "filepath")) {
1499                 return WM_operator_call(C, op);
1500         } 
1501         else {
1502                 /* XXX TODO solve where to get last linked library from */
1503                 RNA_string_set(op->ptr, "filepath", G.lib);
1504                 WM_event_add_fileselect(C, op);
1505                 return OPERATOR_RUNNING_MODAL;
1506         }
1507 }
1508
1509 static short wm_link_append_flag(wmOperator *op)
1510 {
1511         short flag= 0;
1512
1513         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1514         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1515         if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1516         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1517         if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1518
1519         return flag;
1520 }
1521
1522 static void wm_link_make_library_local(Main *main, const char *libname)
1523 {
1524         Library *lib;
1525
1526         /* and now find the latest append lib file */
1527         for(lib= main->library.first; lib; lib=lib->id.next)
1528                 if(BLI_streq(libname, lib->filepath))
1529                         break;
1530         
1531         /* make local */
1532         if(lib) {
1533                 all_local(lib, 1);
1534         }
1535 }
1536
1537 static int wm_link_append_exec(bContext *C, wmOperator *op)
1538 {
1539         Main *bmain= CTX_data_main(C);
1540         Scene *scene= CTX_data_scene(C);
1541         Main *mainl= 0;
1542         BlendHandle *bh;
1543         PropertyRNA *prop;
1544         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1545         int idcode, totfiles=0;
1546         short flag;
1547
1548         name[0] = '\0';
1549         RNA_string_get(op->ptr, "filename", name);
1550         RNA_string_get(op->ptr, "directory", dir);
1551
1552         /* test if we have a valid data */
1553         if(BLO_is_a_library(dir, libname, group) == 0) {
1554                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1555                 return OPERATOR_CANCELLED;
1556         }
1557         else if(group[0] == 0) {
1558                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1559                 return OPERATOR_CANCELLED;
1560         }
1561         else if(BLI_streq(bmain->name, libname)) {
1562                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1563                 return OPERATOR_CANCELLED;
1564         }
1565
1566         /* check if something is indicated for append/link */
1567         prop = RNA_struct_find_property(op->ptr, "files");
1568         if(prop) {
1569                 totfiles= RNA_property_collection_length(op->ptr, prop);
1570                 if(totfiles == 0) {
1571                         if(name[0] == '\0') {
1572                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1573                                 return OPERATOR_CANCELLED;
1574                         }
1575                 }
1576         }
1577         else if(name[0] == '\0') {
1578                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1579                 return OPERATOR_CANCELLED;
1580         }
1581
1582         /* now we have or selected, or an indicated file */
1583         if(RNA_boolean_get(op->ptr, "autoselect"))
1584                 scene_deselect_all(scene);
1585
1586         bh = BLO_blendhandle_from_file(libname);
1587         idcode = BLO_idcode_from_name(group);
1588         
1589         flag = wm_link_append_flag(op);
1590
1591         /* sanity checks for flag */
1592         if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1593                 /* TODO, user never gets this message */
1594                 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1595                 flag &= ~FILE_GROUP_INSTANCE;
1596         }
1597
1598
1599         /* tag everything, all untagged data can be made local
1600          * its also generally useful to know what is new
1601          *
1602          * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1603         flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1604
1605         /* here appending/linking starts */
1606         mainl = BLO_library_append_begin(C, &bh, libname);
1607         if(totfiles == 0) {
1608                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1609         }
1610         else {
1611                 RNA_BEGIN(op->ptr, itemptr, "files") {
1612                         RNA_string_get(&itemptr, "name", name);
1613                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1614                 }
1615                 RNA_END;
1616         }
1617         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1618         
1619         /* mark all library linked objects to be updated */
1620         recalc_all_library_objects(bmain);
1621
1622         /* append, rather than linking */
1623         if((flag & FILE_LINK)==0)
1624                 wm_link_make_library_local(bmain, libname);
1625
1626         /* important we unset, otherwise these object wont
1627          * link into other scenes from this blend file */
1628         flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1629
1630         /* recreate dependency graph to include new objects */
1631         DAG_scene_sort(scene);
1632         DAG_ids_flush_update(0);
1633
1634         BLO_blendhandle_close(bh);
1635
1636         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1637         BLI_strncpy(G.lib, dir, FILE_MAX);
1638
1639         WM_event_add_notifier(C, NC_WINDOW, NULL);
1640
1641         return OPERATOR_FINISHED;
1642 }
1643
1644 static void WM_OT_link_append(wmOperatorType *ot)
1645 {
1646         ot->name= "Link/Append from Library";
1647         ot->idname= "WM_OT_link_append";
1648         ot->description= "Link or Append from a Library .blend file";
1649         
1650         ot->invoke= wm_link_append_invoke;
1651         ot->exec= wm_link_append_exec;
1652         ot->poll= WM_operator_winactive;
1653         
1654         ot->flag |= OPTYPE_UNDO;
1655
1656         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1657         
1658         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1659         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1660         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1661         RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1662
1663         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1664 }       
1665
1666 /* *************** recover last session **************** */
1667
1668 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1669 {
1670         char filename[FILE_MAX];
1671
1672         G.fileflags |= G_FILE_RECOVER;
1673
1674         // XXX wm in context is not set correctly after WM_read_file -> crash
1675         // do it before for now, but is this correct with multiple windows?
1676         WM_event_add_notifier(C, NC_WINDOW, NULL);
1677
1678         /* load file */
1679         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1680         WM_read_file(C, filename, op->reports);
1681
1682         G.fileflags &= ~G_FILE_RECOVER;
1683         return OPERATOR_FINISHED;
1684 }
1685
1686 static void WM_OT_recover_last_session(wmOperatorType *ot)
1687 {
1688         ot->name= "Recover Last Session";
1689         ot->idname= "WM_OT_recover_last_session";
1690         ot->description="Open the last closed file (\"quit.blend\")";
1691         
1692         ot->exec= wm_recover_last_session_exec;
1693         ot->poll= WM_operator_winactive;
1694 }
1695
1696 /* *************** recover auto save **************** */
1697
1698 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1699 {
1700         char path[FILE_MAX];
1701
1702         RNA_string_get(op->ptr, "filepath", path);
1703
1704         G.fileflags |= G_FILE_RECOVER;
1705
1706         // XXX wm in context is not set correctly after WM_read_file -> crash
1707         // do it before for now, but is this correct with multiple windows?
1708         WM_event_add_notifier(C, NC_WINDOW, NULL);
1709
1710         /* load file */
1711         WM_read_file(C, path, op->reports);
1712
1713         G.fileflags &= ~G_FILE_RECOVER;
1714
1715         return OPERATOR_FINISHED;
1716 }
1717
1718 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
1719 {
1720         char filename[FILE_MAX];
1721
1722         wm_autosave_location(filename);
1723         RNA_string_set(op->ptr, "filepath", filename);
1724         WM_event_add_fileselect(C, op);
1725
1726         return OPERATOR_RUNNING_MODAL;
1727 }
1728
1729 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1730 {
1731         ot->name= "Recover Auto Save";
1732         ot->idname= "WM_OT_recover_auto_save";
1733         ot->description="Open an automatically saved file to recover it";
1734         
1735         ot->exec= wm_recover_auto_save_exec;
1736         ot->invoke= wm_recover_auto_save_invoke;
1737         ot->poll= WM_operator_winactive;
1738
1739         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1740 }
1741
1742 /* *************** save file as **************** */
1743
1744 static void untitled(char *name)
1745 {
1746         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1747                 char *c= BLI_last_slash(name);
1748                 
1749                 if(c)
1750                         strcpy(&c[1], "untitled.blend");
1751                 else
1752                         strcpy(name, "untitled.blend");
1753         }
1754 }
1755
1756 static void save_set_compress(wmOperator *op)
1757 {
1758         if(!RNA_property_is_set(op->ptr, "compress")) {
1759                 if(G.save_over) /* keep flag for existing file */
1760                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1761                 else /* use userdef for new file */
1762                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1763         }
1764 }
1765
1766 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1767 {
1768         char name[FILE_MAX];
1769
1770         save_set_compress(op);
1771         
1772         BLI_strncpy(name, G.sce, FILE_MAX);
1773         untitled(name);
1774         RNA_string_set(op->ptr, "filepath", name);
1775         
1776         WM_event_add_fileselect(C, op);
1777
1778         return OPERATOR_RUNNING_MODAL;
1779 }
1780
1781 /* function used for WM_OT_save_mainfile too */
1782 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1783 {
1784         char path[FILE_MAX];
1785         int fileflags;
1786
1787         save_set_compress(op);
1788         
1789         if(RNA_property_is_set(op->ptr, "filepath"))
1790                 RNA_string_get(op->ptr, "filepath", path);
1791         else {
1792                 BLI_strncpy(path, G.sce, FILE_MAX);
1793                 untitled(path);
1794         }
1795
1796         fileflags= G.fileflags;
1797
1798         /* set compression flag */
1799         if(RNA_boolean_get(op->ptr, "compress"))                fileflags |=  G_FILE_COMPRESS;
1800         else                                                                                    fileflags &= ~G_FILE_COMPRESS;
1801         if(RNA_boolean_get(op->ptr, "relative_remap"))  fileflags |=  G_FILE_RELATIVE_REMAP;
1802         else                                                                                    fileflags &= ~G_FILE_RELATIVE_REMAP;
1803
1804         if ( WM_write_file(C, path, fileflags, op->reports) != 0)
1805                 return OPERATOR_CANCELLED;
1806
1807         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1808
1809         return OPERATOR_FINISHED;
1810 }
1811
1812 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1813 {
1814         ot->name= "Save As Blender File";
1815         ot->idname= "WM_OT_save_as_mainfile";
1816         ot->description="Save the current file in the desired location";
1817         
1818         ot->invoke= wm_save_as_mainfile_invoke;
1819         ot->exec= wm_save_as_mainfile_exec;
1820         ot->poll= WM_operator_winactive;
1821         
1822         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1823         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1824         RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1825 }
1826
1827 /* *************** save file directly ******** */
1828
1829 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1830 {
1831         char name[FILE_MAX];
1832         int check_existing=1;
1833         
1834         /* cancel if no active window */
1835         if (CTX_wm_window(C) == NULL)
1836                 return OPERATOR_CANCELLED;
1837
1838         save_set_compress(op);
1839         
1840         BLI_strncpy(name, G.sce, FILE_MAX);
1841         untitled(name);
1842         RNA_string_set(op->ptr, "filepath", name);
1843         
1844         if (RNA_struct_find_property(op->ptr, "check_existing"))
1845                 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1846                         check_existing = 0;
1847         
1848         if (G.save_over) {
1849                 if (check_existing)
1850                         uiPupMenuSaveOver(C, op, name);
1851                 else {
1852                         wm_save_as_mainfile_exec(C, op);
1853                 }
1854         } else {
1855                 WM_event_add_fileselect(C, op);
1856         }
1857         
1858         return OPERATOR_RUNNING_MODAL;
1859 }
1860
1861 static void WM_OT_save_mainfile(wmOperatorType *ot)
1862 {
1863         ot->name= "Save Blender File";
1864         ot->idname= "WM_OT_save_mainfile";
1865         ot->description="Save the current Blender file";
1866         
1867         ot->invoke= wm_save_mainfile_invoke;
1868         ot->exec= wm_save_as_mainfile_exec;
1869         ot->poll= NULL;
1870         
1871         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1872         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1873         RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1874 }
1875
1876
1877 /* XXX: move these collada operators to a more appropriate place */
1878 #ifdef WITH_COLLADA
1879
1880 #include "../../collada/collada.h"
1881
1882 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *event)
1883 {       
1884         if(!RNA_property_is_set(op->ptr, "filepath")) {
1885                 char *path = BLI_replacestr(G.sce, ".blend", ".dae");
1886                 RNA_string_set(op->ptr, "filepath", path);
1887                 MEM_freeN(path);
1888         }
1889
1890         WM_event_add_fileselect(C, op);
1891
1892         return OPERATOR_RUNNING_MODAL;
1893 }
1894
1895 /* function used for WM_OT_save_mainfile too */
1896 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1897 {
1898         char filename[FILE_MAX];
1899         
1900         if(!RNA_property_is_set(op->ptr, "filepath")) {
1901                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1902                 return OPERATOR_CANCELLED;
1903         }
1904         
1905         RNA_string_get(op->ptr, "filepath", filename);
1906         collada_export(CTX_data_scene(C), filename);
1907         
1908         return OPERATOR_FINISHED;
1909 }
1910
1911 static void WM_OT_collada_export(wmOperatorType *ot)
1912 {
1913         ot->name= "Export COLLADA";
1914         ot->idname= "WM_OT_collada_export";
1915         
1916         ot->invoke= wm_collada_export_invoke;
1917         ot->exec= wm_collada_export_exec;
1918         ot->poll= WM_operator_winactive;
1919         
1920         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1921 }
1922
1923 /* function used for WM_OT_save_mainfile too */
1924 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1925 {
1926         char filename[FILE_MAX];
1927         
1928         if(!RNA_property_is_set(op->ptr, "filepath")) {
1929                 BKE_report(op->reports, RPT_ERROR, "No filename given");
1930                 return OPERATOR_CANCELLED;
1931         }
1932
1933         RNA_string_get(op->ptr, "filepath", filename);
1934         collada_import(C, filename);
1935         
1936         return OPERATOR_FINISHED;
1937 }
1938
1939 static void WM_OT_collada_import(wmOperatorType *ot)
1940 {
1941         ot->name= "Import COLLADA";
1942         ot->idname= "WM_OT_collada_import";
1943         
1944         ot->invoke= WM_operator_filesel;
1945         ot->exec= wm_collada_import_exec;
1946         ot->poll= WM_operator_winactive;
1947         
1948         WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1949 }
1950
1951 #endif
1952
1953
1954
1955 /* *********************** */
1956
1957 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1958 {
1959         ot->name= "Toggle Fullscreen";
1960         ot->idname= "WM_OT_window_fullscreen_toggle";
1961         ot->description="Toggle the current window fullscreen";
1962
1963         ot->exec= wm_window_fullscreen_toggle_op;
1964         ot->poll= WM_operator_winactive;
1965 }
1966
1967 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1968 {
1969         WM_operator_free(op);
1970         
1971         WM_exit(C);     
1972         
1973         return OPERATOR_FINISHED;
1974 }
1975
1976 static void WM_OT_exit_blender(wmOperatorType *ot)
1977 {
1978         ot->name= "Exit Blender";
1979         ot->idname= "WM_OT_exit_blender";
1980         ot->description= "Quit Blender";
1981
1982         ot->invoke= WM_operator_confirm;
1983         ot->exec= wm_exit_blender_op;
1984         ot->poll= WM_operator_winactive;
1985 }
1986
1987 /* ************ default paint cursors, draw always around cursor *********** */
1988 /*
1989  - returns handler to free 
1990  - poll(bContext): returns 1 if draw should happen
1991  - draw(bContext): drawing callback for paint cursor
1992 */
1993
1994 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1995                                    wmPaintCursorDraw draw, void *customdata)
1996 {
1997         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1998         
1999         BLI_addtail(&wm->paintcursors, pc);
2000         
2001         pc->customdata = customdata;
2002         pc->poll= poll;
2003         pc->draw= draw;
2004         
2005         return pc;
2006 }
2007
2008 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2009 {
2010         wmPaintCursor *pc;
2011         
2012         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2013                 if(pc == (wmPaintCursor *)handle) {
2014                         BLI_remlink(&wm->paintcursors, pc);
2015                         MEM_freeN(pc);
2016                         return;
2017                 }
2018         }
2019 }
2020
2021 /* ************ window gesture operator-callback definitions ************** */
2022 /*
2023  * These are default callbacks for use in operators requiring gesture input
2024  */
2025
2026 /* **************** Border gesture *************** */
2027
2028 /* Border gesture has two types:
2029    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
2030    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2031
2032    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2033 */
2034
2035 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2036 {
2037         wmGesture *gesture= op->customdata;
2038         rcti *rect= gesture->customdata;
2039         
2040         if(rect->xmin > rect->xmax)
2041                 SWAP(int, rect->xmin, rect->xmax);
2042         if(rect->ymin > rect->ymax)
2043                 SWAP(int, rect->ymin, rect->ymax);
2044         
2045         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2046                 return 0;
2047                 
2048         /* operator arguments and storage. */
2049         RNA_int_set(op->ptr, "xmin", rect->xmin);
2050         RNA_int_set(op->ptr, "ymin", rect->ymin);
2051         RNA_int_set(op->ptr, "xmax", rect->xmax);
2052         RNA_int_set(op->ptr, "ymax", rect->ymax);
2053         
2054         /* XXX weak; border should be configured for this without reading event types */
2055         if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2056                 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2057
2058         op->type->exec(C, op);
2059         
2060         return 1;
2061 }
2062
2063 static void wm_gesture_end(bContext *C, wmOperator *op)
2064 {
2065         wmGesture *gesture= op->customdata;
2066         
2067         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2068         op->customdata= NULL;
2069
2070         ED_area_tag_redraw(CTX_wm_area(C));
2071         
2072         if( RNA_struct_find_property(op->ptr, "cursor") )
2073                 WM_cursor_restore(CTX_wm_window(C));
2074 }
2075
2076 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2077 {
2078         if(ISTWEAK(event->type))
2079                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2080         else
2081                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2082
2083         /* add modal handler */
2084         WM_event_add_modal_handler(C, op);
2085         
2086         wm_gesture_tag_redraw(C);
2087
2088         return OPERATOR_RUNNING_MODAL;
2089 }
2090
2091 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2092 {
2093         wmGesture *gesture= op->customdata;
2094         rcti *rect= gesture->customdata;
2095         int sx, sy;
2096         
2097         if(event->type== MOUSEMOVE) {
2098                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2099
2100                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2101                         rect->xmin= rect->xmax= event->x - sx;
2102                         rect->ymin= rect->ymax= event->y - sy;
2103                 }
2104                 else {
2105                         rect->xmax= event->x - sx;
2106                         rect->ymax= event->y - sy;
2107                 }
2108
2109                 wm_gesture_tag_redraw(C);
2110         }
2111         else if (event->type==EVT_MODAL_MAP) {
2112                 switch (event->val) {
2113                 case GESTURE_MODAL_BEGIN:
2114                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2115                                 gesture->mode= 1;
2116                                 wm_gesture_tag_redraw(C);
2117                         }
2118                         break;
2119                 case GESTURE_MODAL_SELECT:
2120                 case GESTURE_MODAL_DESELECT:
2121                 case GESTURE_MODAL_IN:
2122                 case GESTURE_MODAL_OUT:
2123                         if(border_apply(C, op, event->val)) {
2124                                 wm_gesture_end(C, op);
2125                                 return OPERATOR_FINISHED;
2126                         }
2127                         wm_gesture_end(C, op);
2128                         return OPERATOR_CANCELLED;
2129                         break;
2130
2131                 case GESTURE_MODAL_CANCEL:
2132                         wm_gesture_end(C, op);
2133                         return OPERATOR_CANCELLED;
2134                 }
2135
2136         }
2137 //      // Allow view navigation???
2138 //      else {
2139 //              return OPERATOR_PASS_THROUGH;
2140 //      }
2141
2142         return OPERATOR_RUNNING_MODAL;
2143 }
2144
2145 /* **************** circle gesture *************** */
2146 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2147
2148 #ifdef GESTURE_MEMORY
2149 int circle_select_size= 25; // XXX - need some operator memory thing\!
2150 #endif
2151
2152 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2153 {
2154         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2155         
2156         /* add modal handler */
2157         WM_event_add_modal_handler(C, op);
2158         
2159         wm_gesture_tag_redraw(C);
2160         
2161         return OPERATOR_RUNNING_MODAL;
2162 }
2163
2164 static void gesture_circle_apply(bContext *C, wmOperator *op)
2165 {
2166         wmGesture *gesture= op->customdata;
2167         rcti *rect= gesture->customdata;
2168         
2169         if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2170                 return;
2171
2172         /* operator arguments and storage. */
2173         RNA_int_set(op->ptr, "x", rect->xmin);
2174         RNA_int_set(op->ptr, "y", rect->ymin);
2175         RNA_int_set(op->ptr, "radius", rect->xmax);
2176         
2177         if(op->type->exec)
2178                 op->type->exec(C, op);
2179
2180 #ifdef GESTURE_MEMORY
2181         circle_select_size= rect->xmax;
2182 #endif
2183 }
2184
2185 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2186 {
2187         wmGesture *gesture= op->customdata;
2188         rcti *rect= gesture->customdata;
2189         int sx, sy;
2190
2191         if(event->type== MOUSEMOVE) {
2192                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2193
2194                 rect->xmin= event->x - sx;
2195                 rect->ymin= event->y - sy;
2196
2197                 wm_gesture_tag_redraw(C);
2198
2199                 if(gesture->mode)
2200                         gesture_circle_apply(C, op);
2201         }
2202         else if (event->type==EVT_MODAL_MAP) {
2203                 switch (event->val) {
2204                 case GESTURE_MODAL_CIRCLE_ADD:
2205                         rect->xmax += 2 + rect->xmax/10;
2206                         wm_gesture_tag_redraw(C);
2207                         break;
2208                 case GESTURE_MODAL_CIRCLE_SUB:
2209                         rect->xmax -= 2 + rect->xmax/10;
2210                         if(rect->xmax < 1) rect->xmax= 1;
2211                         wm_gesture_tag_redraw(C);
2212                         break;
2213                 case GESTURE_MODAL_SELECT:
2214                 case GESTURE_MODAL_DESELECT:
2215                 case GESTURE_MODAL_NOP:
2216                         if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2217                                 RNA_int_set(op->ptr, "gesture_mode", event->val);
2218
2219                         if(event->val != GESTURE_MODAL_NOP) {
2220                                 /* apply first click */
2221                                 gesture_circle_apply(C, op);
2222                                 gesture->mode= 1;
2223                                 wm_gesture_tag_redraw(C);
2224                         }
2225                         break;
2226
2227                 case GESTURE_MODAL_CANCEL:
2228                 case GESTURE_MODAL_CONFIRM:
2229                         wm_gesture_end(C, op);
2230                         return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2231                 }
2232         }
2233 //      // Allow view navigation???
2234 //      else {
2235 //              return OPERATOR_PASS_THROUGH;
2236 //      }
2237
2238         return OPERATOR_RUNNING_MODAL;
2239 }
2240
2241 #if 0
2242 /* template to copy from */
2243 void WM_OT_circle_gesture(wmOperatorType *ot)
2244 {
2245         ot->name= "Circle Gesture";
2246         ot->idname= "WM_OT_circle_gesture";
2247         ot->description="Enter rotate mode with a circular gesture";
2248         
2249         ot->invoke= WM_gesture_circle_invoke;
2250         ot->modal= WM_gesture_circle_modal;
2251         
2252         ot->poll= WM_operator_winactive;
2253         
2254         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2255         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2256         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2257
2258 }
2259 #endif
2260
2261 /* **************** Tweak gesture *************** */
2262
2263 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2264 {
2265         wmWindow *window= CTX_wm_window(C);
2266         wmGesture *gesture= window->tweak;
2267         rcti *rect= gesture->customdata;
2268         int sx, sy, val;
2269         
2270         switch(event->type) {
2271                 case MOUSEMOVE:
2272                 case INBETWEEN_MOUSEMOVE:
2273                         
2274                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2275                         
2276                         rect->xmax= event->x - sx;
2277                         rect->ymax= event->y - sy;
2278                         
2279                         if((val= wm_gesture_evaluate(C, gesture))) {
2280                                 wmEvent event;
2281
2282                                 event= *(window->eventstate);
2283                                 if(gesture->event_type==LEFTMOUSE)
2284                                         event.type= EVT_TWEAK_L;
2285                                 else if(gesture->event_type==RIGHTMOUSE)
2286                                         event.type= EVT_TWEAK_R;
2287                                 else
2288                                         event.type= EVT_TWEAK_M;
2289                                 event.val= val;
2290                                 /* mouse coords! */
2291                                 wm_event_add(window, &event);
2292                                 
2293                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
2294                         }
2295                         
2296                         break;
2297                         
2298                 case LEFTMOUSE:
2299                 case RIGHTMOUSE:
2300                 case MIDDLEMOUSE:
2301                         if(gesture->event_type==event->type) {
2302                                 WM_gesture_end(C, gesture);
2303
2304                                 /* when tweak fails we should give the other keymap entries a chance */
2305                                 event->val= KM_RELEASE;
2306                         }
2307                         break;
2308                 default:
2309                         if(!ISTIMER(event->type)) {
2310                                 WM_gesture_end(C, gesture);
2311                         }
2312                         break;
2313         }
2314 }
2315
2316 /* standard tweak, called after window handlers passed on event */
2317 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2318 {
2319         wmWindow *win= CTX_wm_window(C);
2320         
2321         if(win->tweak==NULL) {
2322                 if(CTX_wm_region(C)) {
2323                         if(event->val==KM_PRESS) { 
2324                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2325                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2326                         }
2327                 }
2328         }
2329         else {
2330                 /* no tweaks if event was handled */
2331                 if((action & WM_HANDLER_BREAK)) {
2332                         WM_gesture_end(C, win->tweak);
2333                 }
2334                 else
2335                         tweak_gesture_modal(C, event);
2336         }
2337 }
2338
2339 /* *********************** lasso gesture ****************** */
2340
2341 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2342 {
2343         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2344         
2345         /* add modal handler */
2346         WM_event_add_modal_handler(C, op);
2347         
2348         wm_gesture_tag_redraw(C);
2349         
2350         if( RNA_struct_find_property(op->ptr, "cursor") )
2351                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2352         
2353         return OPERATOR_RUNNING_MODAL;
2354 }
2355
2356 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2357 {
2358         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2359         
2360         /* add modal handler */
2361         WM_event_add_modal_handler(C, op);
2362         
2363         wm_gesture_tag_redraw(C);
2364         
2365         if( RNA_struct_find_property(op->ptr, "cursor") )
2366                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2367         
2368         return OPERATOR_RUNNING_MODAL;
2369 }
2370
2371
2372 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
2373 {
2374         wmGesture *gesture= op->customdata;
2375         PointerRNA itemptr;
2376         float loc[2];
2377         int i;
2378         short *lasso= gesture->customdata;
2379         
2380         /* operator storage as path. */
2381
2382         for(i=0; i<gesture->points; i++, lasso+=2) {
2383                 loc[0]= lasso[0];
2384                 loc[1]= lasso[1];
2385                 RNA_collection_add(op->ptr, "path", &itemptr);
2386                 RNA_float_set_array(&itemptr, "loc", loc);
2387         }
2388         
2389         wm_gesture_end(C, op);
2390                 
2391         if(op->type->exec)
2392                 op->type->exec(C, op);
2393         
2394 }
2395
2396 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2397 {
2398         wmGesture *gesture= op->customdata;
2399         int sx, sy;
2400         
2401         switch(event->type) {
2402                 case MOUSEMOVE:
2403                 case INBETWEEN_MOUSEMOVE:
2404                         
2405                         wm_gesture_tag_redraw(C);
2406                         
2407                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2408
2409                         if(gesture->points == gesture->size) {
2410                                 short *old_lasso = gesture->customdata;
2411                                 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2412                                 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2413                                 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2414                                 MEM_freeN(old_lasso);
2415                                 printf("realloc\n");
2416                         }
2417
2418                         {
2419                                 short *lasso= gesture->customdata;
2420                                 lasso += 2 * gesture->points;
2421                                 lasso[0] = event->x - sx;
2422                                 lasso[1] = event->y - sy;
2423                                 gesture->points++;
2424                         }
2425                         break;
2426                         
2427                 case LEFTMOUSE:
2428                 case MIDDLEMOUSE:
2429                 case RIGHTMOUSE:
2430                         if(event->val==KM_RELEASE) {    /* key release */
2431                                 gesture_lasso_apply(C, op, event->type);
2432                                 return OPERATOR_FINISHED;
2433                         }
2434                         break;
2435                 case ESCKEY:
2436                         wm_gesture_end(C, op);
2437                         return OPERATOR_CANCELLED;
2438         }
2439         return OPERATOR_RUNNING_MODAL;
2440 }
2441
2442 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2443 {
2444         return WM_gesture_lasso_modal(C, op, event);
2445 }
2446
2447 #if 0
2448 /* template to copy from */
2449
2450 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2451 {
2452         RNA_BEGIN(op->ptr, itemptr, "path") {
2453                 float loc[2];
2454                 
2455                 RNA_float_get_array(&itemptr, "loc", loc);
2456                 printf("Location: %f %f\n", loc[0], loc[1]);
2457         }
2458         RNA_END;
2459         
2460         return OPERATOR_FINISHED;
2461 }
2462
2463 void WM_OT_lasso_gesture(wmOperatorType *ot)
2464 {
2465         PropertyRNA *prop;
2466         
2467         ot->name= "Lasso Gesture";
2468         ot->idname= "WM_OT_lasso_gesture";
2469         ot->description="Select objects within the lasso as you move the pointer";
2470         
2471         ot->invoke= WM_gesture_lasso_invoke;
2472         ot->modal= WM_gesture_lasso_modal;
2473         ot->exec= gesture_lasso_exec;
2474         
2475         ot->poll= WM_operator_winactive;
2476         
2477         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2478         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2479 }
2480 #endif
2481
2482 /* *********************** straight line gesture ****************** */
2483
2484 static int straightline_apply(bContext *C, wmOperator *op)
2485 {
2486         wmGesture *gesture= op->customdata;
2487         rcti *rect= gesture->customdata;
2488         
2489         if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2490                 return 0;
2491         
2492         /* operator arguments and storage. */
2493         RNA_int_set(op->ptr, "xstart", rect->xmin);
2494         RNA_int_set(op->ptr, "ystart", rect->ymin);
2495         RNA_int_set(op->ptr, "xend", rect->xmax);
2496         RNA_int_set(op->ptr, "yend", rect->ymax);
2497
2498         if(op->type->exec)
2499                 op->type->exec(C, op);
2500         
2501         return 1;
2502 }
2503
2504
2505 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2506 {
2507         op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2508         
2509         /* add modal handler */
2510         WM_event_add_modal_handler(C, op);
2511         
2512         wm_gesture_tag_redraw(C);
2513         
2514         if( RNA_struct_find_property(op->ptr, "cursor") )
2515                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2516                 
2517         return OPERATOR_RUNNING_MODAL;
2518 }
2519
2520 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2521 {
2522         wmGesture *gesture= op->customdata;
2523         rcti *rect= gesture->customdata;
2524         int sx, sy;
2525         
2526         if(event->type== MOUSEMOVE) {
2527                 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2528                 
2529                 if(gesture->mode==0) {
2530                         rect->xmin= rect->xmax= event->x - sx;
2531                         rect->ymin= rect->ymax= event->y - sy;
2532                 }
2533                 else {
2534                         rect->xmax= event->x - sx;
2535                         rect->ymax= event->y - sy;
2536                         straightline_apply(C, op);
2537                 }
2538                 
2539                 wm_gesture_tag_redraw(C);
2540         }
2541         else if (event->type==EVT_MODAL_MAP) {
2542                 switch (event->val) {
2543                         case GESTURE_MODAL_BEGIN:
2544                                 if(gesture->mode==0) {
2545                                         gesture->mode= 1;
2546                                         wm_gesture_tag_redraw(C);
2547                                 }
2548                                 break;
2549                         case GESTURE_MODAL_SELECT:
2550                                 if(straightline_apply(C, op)) {
2551                                         wm_gesture_end(C, op);
2552                                         return OPERATOR_FINISHED;
2553                                 }
2554                                 wm_gesture_end(C, op);
2555                                 return OPERATOR_CANCELLED;
2556                                 break;
2557                                 
2558                         case GESTURE_MODAL_CANCEL:
2559                                 wm_gesture_end(C, op);
2560                                 return OPERATOR_CANCELLED;
2561                 }
2562                 
2563         }
2564
2565         return OPERATOR_RUNNING_MODAL;
2566 }
2567
2568 #if 0
2569 /* template to copy from */
2570 void WM_OT_straightline_gesture(wmOperatorType *ot)
2571 {
2572         PropertyRNA *prop;
2573         
2574         ot->name= "Straight Line Gesture";
2575         ot->idname= "WM_OT_straightline_gesture";
2576         ot->description="Draw a straight line as you move the pointer";
2577         
2578         ot->invoke= WM_gesture_straightline_invoke;
2579         ot->modal= WM_gesture_straightline_modal;
2580         ot->exec= gesture_straightline_exec;
2581         
2582         ot->poll= WM_operator_winactive;
2583         
2584         WM_operator_properties_gesture_straightline(ot, 0);
2585 }
2586 #endif
2587
2588 /* *********************** radial control ****************** */
2589
2590 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2591
2592 typedef struct wmRadialControl {
2593         int mode;
2594         float initial_value, value, max_value;
2595         int initial_mouse[2];
2596         void *cursor;
2597         GLuint tex;
2598 } wmRadialControl;
2599
2600 extern Paint *paint_get_active(Scene *sce);
2601 extern struct Brush *paint_brush(struct Paint *paint);
2602
2603 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2604 {
2605         wmRadialControl *rc = (wmRadialControl*)customdata;
2606         ARegion *ar = CTX_wm_region(C);
2607         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2608
2609         Paint *paint = paint_get_active(CTX_data_scene(C));
2610         Brush *brush = paint_brush(paint);
2611
2612         ViewContext vc;
2613
2614         // int hit = 0;
2615
2616         int flip;
2617         int sign;
2618
2619         float* col;
2620
2621         const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : (brush->texture_overlay_alpha / 100.0f);
2622
2623         if(rc->mode == WM_RADIALCONTROL_SIZE) {
2624                 r1= rc->value;
2625                 r2= rc->initial_value;
2626                 r3= r1;
2627         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2628                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2629                 r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2630         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2631                 r1= r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2632                 angle = rc->value;
2633         }
2634
2635         /* Keep cursor in the original place */
2636         x = rc->initial_mouse[0] - ar->winrct.xmin;
2637         y = rc->initial_mouse[1] - ar->winrct.ymin;
2638
2639         view3d_set_viewcontext(C, &vc);
2640
2641         // XXX: no way currently to know state of pen flip or invert key modifier without starting a stroke
2642         flip = 1;
2643
2644         sign = flip * ((brush->flag & BRUSH_DIR_IN)? -1 : 1);
2645
2646         if (sign < 0 && ELEM4(brush->sculpt_tool, SCULPT_TOOL_DRAW, SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_PINCH))
2647                 col = brush->sub_col;
2648         else
2649                 col = brush->add_col;
2650
2651         glTranslatef((float)x, (float)y, 0.0f);
2652
2653         glEnable(GL_BLEND);
2654
2655         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2656                 glRotatef(angle, 0, 0, 1);
2657         }
2658
2659         if (rc->tex) {
2660                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2661
2662                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2663                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2664
2665                 glEnable(GL_TEXTURE_2D);
2666                 glBegin(GL_QUADS);
2667                 glColor4f(U.sculpt_paint_overlay_col[0],U.sculpt_paint_overlay_col[1],U.sculpt_paint_overlay_col[2], str);
2668                 glTexCoord2f(0,0);
2669                 glVertex2f(-r3, -r3);
2670                 glTexCoord2f(1,0);
2671                 glVertex2f(r3, -r3);
2672                 glTexCoord2f(1,1);
2673                 glVertex2f(r3, r3);
2674                 glTexCoord2f(0,1);
2675                 glVertex2f(-r3, r3);
2676                 glEnd();
2677                 glDisable(GL_TEXTURE_2D);
2678         }
2679
2680         if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2681                 glColor4f(col[0], col[1], col[2], 0.5f);
2682                 glEnable(GL_LINE_SMOOTH);
2683                 glRotatef(-angle, 0, 0, 1);
2684                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2685                 glRotatef(angle, 0, 0, 1);
2686                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2687                 glDisable(GL_LINE_SMOOTH);
2688         }
2689
2690         glColor4f(col[0], col[1], col[2], 0.5f);
2691         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2692         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2693         glDisable(GL_BLEND);
2694         
2695         glPopMatrix();
2696 }
2697
2698 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2699 {
2700         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2701         int mode, initial_mouse[2], delta[2];
2702         float dist;
2703         double new_value = RNA_float_get(op->ptr, "new_value");
2704         int ret = OPERATOR_RUNNING_MODAL;
2705         // float initial_value = RNA_float_get(op->ptr, "initial_value");
2706
2707         mode = RNA_int_get(op->ptr, "mode");
2708         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2709
2710         switch(event->type) {
2711         case MOUSEMOVE:
2712                 delta[0]= initial_mouse[0] - event->x;
2713                 delta[1]= initial_mouse[1] - event->y;
2714
2715                 //if (mode == WM_RADIALCONTROL_SIZE) 
2716                 //      delta[0]+= initial_value;
2717                 //else if(mode == WM_RADIALCONTROL_STRENGTH)
2718                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2719                 //else if(mode == WM_RADIALCONTROL_ANGLE) {
2720                 //      delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value*M_PI/180.0f);
2721                 //      delta[1]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value*M_PI/180.0f);
2722                 //}
2723
2724                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2725
2726                 if(mode == WM_RADIALCONTROL_SIZE)
2727                         new_value = dist;
2728                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2729                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2730                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2731                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2732                 
2733                 if(event->ctrl) {
2734                         if(mode == WM_RADIALCONTROL_STRENGTH)
2735                                 new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2736                         else
2737                                 new_value = ((int)new_value + 5) / 10*10;
2738                 }
2739                 
2740                 break;
2741         case ESCKEY:
2742         case RIGHTMOUSE:
2743                 ret = OPERATOR_CANCELLED;
2744                 break;
2745         case LEFTMOUSE:
2746         case PADENTER:
2747                 op->type->exec(C, op);
2748                 ret = OPERATOR_FINISHED;
2749                 break;
2750         }
2751
2752         /* Clamp */
2753         if(new_value > rc->max_value)
2754                 new_value = rc->max_value;
2755         else if(new_value < 0)
2756                 new_value = 0;
2757
2758         /* Update paint data */
2759         rc->value = new_value;
2760
2761         RNA_float_set(op->ptr, "new_value", new_value);
2762
2763         if(ret != OPERATOR_RUNNING_MODAL) {
2764                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2765                 MEM_freeN(rc);
2766         }
2767         
2768         ED_region_tag_redraw(CTX_wm_region(C));
2769
2770         //if (ret != OPERATOR_RUNNING_MODAL) {
2771         //      wmWindow *win = CTX_wm_window(C);
2772         //      WM_cursor_restore(win);
2773         //}
2774
2775         return ret;
2776 }
2777
2778 /* Expects the operator customdata to be an ImBuf (or NULL) */
2779 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2780 {
2781         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2782         // wmWindow *win = CTX_wm_window(C);
2783         int mode = RNA_int_get(op->ptr, "mode");
2784         float initial_value = RNA_float_get(op->ptr, "initial_value");
2785         //float initial_size = RNA_float_get(op->ptr, "initial_size");
2786         int mouse[2] = {event->x, event->y};
2787
2788         //if (initial_size == 0)
2789         //      initial_size = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2790
2791         if(mode == WM_RADIALCONTROL_SIZE) {
2792                 rc->max_value = 200;
2793                 mouse[0]-= initial_value;
2794         }
2795         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2796                 rc->max_value = 1;
2797                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2798         }
2799         else if(mode == WM_RADIALCONTROL_ANGLE) {
2800                 rc->max_value = 360;
2801                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2802                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2803                 initial_value *= 180.0f/M_PI;
2804         }
2805
2806         if(op->customdata) {
2807                 ImBuf *im = (ImBuf*)op->customdata;
2808                 /* Build GL texture */
2809                 glGenTextures(1, &rc->tex);
2810                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2811                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2812                 MEM_freeN(im->rect_float);
2813                 MEM_freeN(im);
2814         }
2815
2816         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2817         RNA_float_set(op->ptr, "new_value", initial_value);
2818                 
2819         op->customdata = rc;
2820         rc->mode = mode;
2821         rc->initial_value = initial_value;
2822         rc->initial_mouse[0] = mouse[0];
2823         rc->initial_mouse[1] = mouse[1];
2824         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2825                                                   wm_radial_control_paint, op->customdata);
2826
2827         //WM_cursor_modal(win, CURSOR_NONE);
2828
2829         /* add modal handler */
2830         WM_event_add_modal_handler(C, op);
2831         
2832         WM_radial_control_modal(C, op, event);
2833         
2834         return OPERATOR_RUNNING_MODAL;
2835 }
2836
2837 /* Gets a descriptive string of the operation */
2838 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2839 {
2840         int mode = RNA_int_get(op->ptr, "mode");
2841         float v = RNA_float_get(op->ptr, "new_value");
2842
2843         if(mode == WM_RADIALCONTROL_SIZE)
2844                 sprintf(str, "Size: %d", (int)v);
2845         else if(mode == WM_RADIALCONTROL_STRENGTH)
2846                 sprintf(str, "Strength: %d", (int)v);
2847         else if(mode == WM_RADIALCONTROL_ANGLE)
2848                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
2849 }
2850
2851 /** Important: this doesn't define an actual operator, it
2852         just sets up the common parts of the radial control op. **/
2853 void WM_OT_radial_control_partial(wmOperatorType *ot)
2854 {
2855         static EnumPropertyItem radial_mode_items[] = {
2856                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2857                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2858                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2859                 {0, NULL, 0, NULL, NULL}};
2860
2861         /* Should be set in custom invoke() */
2862         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2863
2864         /* Set internally, should be used in custom exec() to get final value */
2865         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2866
2867         /* Should be set before calling operator */
2868         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2869
2870         /* Internal */
2871         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
2872 }
2873
2874 /* ************************** timer for testing ***************** */
2875
2876 /* uses no type defines, fully local testing function anyway... ;) */
2877
2878 static void redraw_timer_window_swap(bContext *C)
2879 {
2880         wmWindow *win= CTX_wm_window(C);
2881         ScrArea *sa;
2882
2883         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2884                 ED_area_tag_redraw(sa);
2885         wm_draw_update(C);
2886
2887         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2888 }
2889
2890 static EnumPropertyItem redraw_timer_type_items[] = {
2891         {0, "DRAW", 0, "Draw Region", "Draw Region"},
2892         {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2893         {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2894         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2895         {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2896         {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2897         {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2898         {0, NULL, 0, NULL, NULL}};
2899
2900 static int redraw_timer_exec(bContext *C, wmOperator *op)
2901 {
2902         ARegion *ar= CTX_wm_region(C);
2903         double stime= PIL_check_seconds_timer();
2904         int type = RNA_int_get(op->ptr, "type");
2905         int iter = RNA_int_get(op->ptr, "iterations");
2906         int a;
2907         float time;
2908         const char *infostr= "";
2909         
2910         WM_cursor_wait(1);
2911
2912         for(a=0; a<iter; a++) {
2913                 if (type==0) {
2914                         ED_region_do_draw(C, ar);
2915                 } 
2916                 else if (type==1) {
2917                         wmWindow *win= CTX_wm_window(C);
2918                         
2919                         ED_region_tag_redraw(ar);
2920                         wm_draw_update(C);
2921                         
2922                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2923                 }
2924                 else if (type==2) {
2925                         wmWindow *win= CTX_wm_window(C);
2926                         ScrArea *sa;
2927                         
2928                         ScrArea *sa_back= CTX_wm_area(C);
2929                         ARegion *ar_back= CTX_wm_region(C);
2930
2931                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2932                                 ARegion *ar_iter;
2933                                 CTX_wm_area_set(C, sa);
2934
2935                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2936                                         if(ar_iter->swinid) {
2937                                                 CTX_wm_region_set(C, ar_iter);
2938                                                 ED_region_do_draw(C, ar_iter);
2939                                         }
2940                                 }
2941                         }
2942
2943                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2944
2945                         CTX_wm_area_set(C, sa_back);
2946                         CTX_wm_region_set(C, ar_back);
2947                 }
2948                 else if (type==3) {
2949                         redraw_timer_window_swap(C);
2950                 }
2951                 else if (type==4) {
2952                         Scene *scene= CTX_data_scene(C);
2953                         
2954                         if(a & 1) scene->r.cfra--;
2955                         else scene->r.cfra++;
2956                         scene_update_for_newframe(scene, scene->lay);
2957                 }
2958                 else if (type==5) {
2959
2960                         /* play anim, return on same frame as started with */
2961                         Scene *scene= CTX_data_scene(C);
2962                         int tot= (scene->r.efra - scene->r.sfra) + 1;
2963
2964                         while(tot--) {
2965                                 /* todo, ability to escape! */
2966                                 scene->r.cfra++;
2967                                 if(scene->r.cfra > scene->r.efra)
2968                                         scene->r.cfra= scene->r.sfra;
2969
2970                                 scene_update_for_newframe(scene, scene->lay);
2971                                 redraw_timer_window_swap(C);
2972                         }
2973                 }
2974                 else { /* 6 */
2975                         ED_undo_pop(C);
2976                         ED_undo_redo(C);
2977                 }
2978         }
2979         
2980         time= ((PIL_check_seconds_timer()-stime)*1000);
2981
2982         RNA_enum_description(redraw_timer_type_items, type, &infostr);
2983
2984         WM_cursor_wait(0);
2985
2986         BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
2987         
2988         return OPERATOR_FINISHED;
2989 }
2990
2991 static void WM_OT_redraw_timer(wmOperatorType *ot)
2992 {
2993         ot->name= "Redraw Timer";
2994         ot->idname= "WM_OT_redraw_timer";
2995         ot->description="Simple redraw timer to test the speed of updating the interface";
2996
2997         ot->invoke= WM_menu_invoke;
2998         ot->exec= redraw_timer_exec;
2999         ot->poll= WM_operator_winactive;
3000
3001         ot->prop= RNA_def_enum(ot->srna, "type", redraw_timer_type_items, 0, "Type", "");
3002         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
3003
3004 }
3005
3006 /* ************************** memory statistics for testing ***************** */
3007
3008 static int memory_statistics_exec(bContext *C, wmOperator *op)
3009 {
3010         MEM_printmemlist_stats();
3011         return OPERATOR_FINISHED;
3012 }
3013
3014 static void WM_OT_memory_statistics(wmOperatorType *ot)
3015 {
3016         ot->name= "Memory Statistics";
3017         ot->idname= "WM_OT_memory_statistics";
3018         ot->description= "Print memory statistics to the console";
3019         
3020         ot->exec= memory_statistics_exec;
3021 }
3022
3023 /* ******************************************************* */
3024  
3025 /* called on initialize WM_exit() */
3026 void wm_operatortype_free(void)
3027 {
3028         wmOperatorType *ot;
3029         
3030         for(ot= global_ops.first; ot; ot= ot->next) {
3031                 if(ot->macro.first)
3032                         wm_operatortype_free_macro(ot);
3033
3034                 if(ot->ext.srna) /* python operator, allocs own string */
3035                         MEM_freeN(ot->idname);
3036         }
3037         
3038         BLI_freelistN(&global_ops);
3039 }
3040
3041 /* called on initialize WM_init() */
3042 void wm_operatortype_init(void)
3043 {
3044         WM_operatortype_append(WM_OT_window_duplicate);
3045         WM_operatortype_append(WM_OT_read_homefile);
3046         WM_operatortype_append(WM_OT_save_homefile);
3047         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
3048         WM_operatortype_append(WM_OT_exit_blender);
3049         WM_operatortype_append(WM_OT_open_mainfile);
3050         WM_operatortype_append(WM_OT_link_append);
3051         WM_operatortype_append(WM_OT_recover_last_session);
3052         WM_operatortype_append(WM_OT_recover_auto_save);
3053         WM_operatortype_append(WM_OT_save_as_mainfile);
3054         WM_operatortype_append(WM_OT_save_mainfile);
3055         WM_operatortype_append(WM_OT_redraw_timer);
3056         WM_operatortype_append(WM_OT_memory_statistics);
3057         WM_operatortype_append(WM_OT_debug_menu);
3058         WM_operatortype_append(WM_OT_splash);
3059         WM_operatortype_append(WM_OT_search_menu);
3060         WM_operatortype_append(WM_OT_call_menu);
3061
3062 #ifdef WITH_COLLADA
3063         /* XXX: move these */
3064         WM_operatortype_append(WM_OT_collada_export);
3065         WM_operatortype_append(WM_OT_collada_import);
3066 #endif
3067
3068 }
3069
3070 /* circleselect-like modal operators */
3071 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
3072 {
3073         static EnumPropertyItem modal_items[] = {
3074         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3075         {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3076         {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
3077         {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
3078
3079         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3080         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3081         {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
3082
3083
3084         {0, NULL, 0, NULL, NULL}};
3085
3086         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
3087
3088         /* this function is called for each spacetype, only needs to add map once */
3089         if(keymap) return;
3090
3091         keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
3092
3093         /* items for modal map */
3094         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3095         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3096
3097         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
3098         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
3099
3100         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
3101
3102 #if 0 // Durien guys like this :S
3103         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3104         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
3105 #else
3106         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); //  defailt 2.4x
3107         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); //  defailt 2.4x
3108 #endif
3109
3110         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
3111
3112         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3113         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3114         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3115         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3116
3117         /* assign map to operators */
3118         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
3119         WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
3120
3121 }
3122
3123 /* straight line modal operators */
3124 static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
3125 {
3126         static EnumPropertyItem modal_items[] = {
3127                 {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3128                 {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3129                 {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3130                 {0, NULL, 0, NULL, NULL}};
3131         
3132         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Straight Line");
3133         
3134         /* this function is called for each spacetype, only needs to add map once */
3135         if(keymap) return;
3136         
3137         keymap= WM_modalkeymap_add(keyconf, "Gesture Straight Line", modal_items);
3138         
3139         /* items for modal map */
3140         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3141         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3142         
3143         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3144         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_SELECT);
3145         
3146         /* assign map to operators */
3147         WM_modalkeymap_assign(keymap, "IMAGE_OT_sample_line");
3148 }
3149
3150
3151 /* borderselect-like modal operators */
3152 static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
3153 {
3154         static EnumPropertyItem modal_items[] = {
3155         {GESTURE_MODAL_CANCEL,  "CANCEL", 0, "Cancel", ""},
3156         {GESTURE_MODAL_SELECT,  "SELECT", 0, "Select", ""},
3157         {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3158         {GESTURE_MODAL_BEGIN,   "BEGIN", 0, "Begin", ""},
3159         {0, NULL, 0, NULL, NULL}};
3160
3161         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Border");
3162
3163         /* this function is called for each spacetype, only needs to add map once */
3164         if(keymap) return;
3165
3166         keymap= WM_modalkeymap_add(keyconf, "Gesture Border", modal_items);
3167
3168         /* items for modal map */
3169         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3170         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3171
3172         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3173         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, GESTURE_MODAL_SELECT);
3174
3175 #if 0 // Durian guys like this
3176         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_BEGIN);
3177         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3178 #else
3179         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3180         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_DESELECT);
3181 #endif
3182
3183         /* assign map to operators */
3184         WM_modalkeymap_assign(keymap, "ACTION_OT_select_border");
3185         WM_modalkeymap_assign(keymap, "ANIM_OT_channels_select_border");
3186         WM_modalkeymap_assign(keymap, "ANIM_OT_previewrange_set");
3187         WM_modalkeymap_assign(keymap, "CONSOLE_OT_select_border");
3188         WM_modalkeymap_assign(keymap, "FILE_OT_select_border");
3189         WM_modalkeymap_assign(keymap, "GRAPH_OT_select_border");
3190         WM_modalkeymap_assign(keymap, "MARKER_OT_select_border");
3191         WM_modalkeymap_assign(keymap, "NLA_OT_select_border");
3192         WM_modalkeymap_assign(keymap, "NODE_OT_select_border");
3193 //      WM_modalkeymap_assign(keymap, "SCREEN_OT_border_select"); // template
3194         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_select_border");
3195         WM_modalkeymap_assign(keymap, "SEQUENCER_OT_view_ghost_border");
3196         WM_modalkeymap_assign(keymap, "UV_OT_select_border");
3197         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3198         WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
3199         WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");
3200         WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
3201         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); // XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel
3202 }
3203
3204 /* zoom to border modal operators */
3205 static void gesture_zoom_border_modal_keymap(wmKeyConfig *keyconf)
3206 {
3207         static EnumPropertyItem modal_items[] = {
3208         {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3209         {GESTURE_MODAL_IN,      "IN", 0, "In", ""},
3210         {GESTURE_MODAL_OUT, "OUT", 0, "Out", ""},
3211         {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3212         {0, NULL, 0, NULL, NULL}};
3213
3214         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Zoom Border");
3215
3216         /* this function is called for each spacetype, only needs to add map once */
3217         if(keymap) return;
3218
3219         keymap= WM_modalkeymap_add(keyconf, "Gesture Zoom Border", modal_items);
3220
3221         /* items for modal map */
3222         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3223         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3224
3225         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3226         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_IN); 
3227
3228         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3229         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_OUT);
3230
3231         /* assign map to operators */
3232         WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3233         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border");
3234 }
3235
3236 /* default keymap for windows and screens, only call once per WM */
3237 void wm_window_keymap(wmKeyConfig *keyconf)
3238 {
3239         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
3240         wmKeyMapItem *kmi;
3241         
3242         /* note, this doesn't replace existing keymap items */
3243         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3244         #ifdef __APPLE__
3245         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
3246         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3247         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
3248         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
3249         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3250         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
3251         #endif
3252         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
3253         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
3254         WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3255         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
3256         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
3257         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3258         kmi= WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
3259         RNA_boolean_set(kmi->ptr, "link", FALSE);
3260         RNA_boolean_set(kmi->ptr, "instance_groups", FALSE);
3261
3262         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
3263         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
3264         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3265         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
3266
3267         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_ALT, 0);
3268         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
3269
3270         /* debug/testing */
3271         WM_keymap_verify_item(keymap, "WM_OT_redraw_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3272         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
3273         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
3274         
3275         /* Space switching */
3276
3277
3278         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
3279         RNA_string_set(kmi->ptr, "data_path", "area.type");
3280         RNA_string_set(kmi->ptr, "value", "LOGIC_EDITOR");
3281
3282         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F3KEY, KM_PRESS, KM_SHIFT, 0);
3283         RNA_string_set(kmi->ptr, "data_path", "area.type");
3284         RNA_string_set(kmi->ptr, "value", "NODE_EDITOR");
3285
3286         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F4KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was data browser */
3287         RNA_string_set(kmi->ptr, "data_path", "area.type");
3288         RNA_string_set(kmi->ptr, "value", "CONSOLE");
3289
3290         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F5KEY, KM_PRESS, KM_SHIFT, 0);
3291         RNA_string_set(kmi->ptr, "data_path", "area.type");
3292         RNA_string_set(kmi->ptr, "value", "VIEW_3D");
3293
3294         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F6KEY, KM_PRESS, KM_SHIFT, 0);
3295         RNA_string_set(kmi->ptr, "data_path", "area.type");
3296         RNA_string_set(kmi->ptr, "value", "GRAPH_EDITOR");
3297
3298         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F7KEY, KM_PRESS, KM_SHIFT, 0);
3299         RNA_string_set(kmi->ptr, "data_path", "area.type");