- missing return values
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <float.h>
30 #define _USE_MATH_DEFINES
31 #include <math.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35
36 #include "DNA_ID.h"
37 #include "DNA_object_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_userdef_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "PIL_time.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_dynstr.h" /*for WM_operator_pystring */
49
50 #include "BLO_readfile.h"
51
52 #include "BKE_blender.h"
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_idprop.h"
56 #include "BKE_library.h"
57 #include "BKE_global.h"
58 #include "BKE_main.h"
59 #include "BKE_report.h"
60 #include "BKE_scene.h"
61 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
62 #include "BKE_utildefines.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h" /* for paint cursor */
66
67 #include "IMB_imbuf_types.h"
68
69 #include "ED_screen.h"
70 #include "ED_util.h"
71
72 #include "RNA_access.h"
73 #include "RNA_define.h"
74
75 #include "UI_interface.h"
76 #include "UI_resources.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "wm.h"
82 #include "wm_draw.h"
83 #include "wm_event_system.h"
84 #include "wm_subwindow.h"
85 #include "wm_window.h"
86
87
88
89 static ListBase global_ops= {NULL, NULL};
90
91 /* ************ operator API, exported ********** */
92
93
94 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
95 {
96         wmOperatorType *ot;
97         
98         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
99         WM_operator_bl_idname(idname_bl, idname);
100         
101         if (idname_bl[0]) {
102                 for(ot= global_ops.first; ot; ot= ot->next) {
103                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
104                            return ot;
105                 }
106         }
107         
108         if(!quiet)
109                 printf("search for unknown operator %s, %s\n", idname_bl, idname);
110         
111         return NULL;
112 }
113
114 wmOperatorType *WM_operatortype_exists(const char *idname)
115 {
116         wmOperatorType *ot;
117         
118         char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
119         WM_operator_bl_idname(idname_bl, idname);
120         
121         if(idname_bl[0]) {
122                 for(ot= global_ops.first; ot; ot= ot->next) {
123                         if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
124                            return ot;
125                 }
126         }
127         return NULL;
128 }
129
130 wmOperatorType *WM_operatortype_first(void)
131 {
132         return global_ops.first;
133 }
134
135 /* all ops in 1 list (for time being... needs evaluation later) */
136 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
137 {
138         wmOperatorType *ot;
139         
140         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
141         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
142         opfunc(ot);
143
144         if(ot->name==NULL) {
145                 static char dummy_name[] = "Dummy Name";
146                 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
147                 ot->name= dummy_name;
148         }
149
150         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.
151         RNA_def_struct_identifier(ot->srna, ot->idname);
152         BLI_addtail(&global_ops, ot);
153 }
154
155 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
156 {
157         wmOperatorType *ot;
158
159         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
160         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
161         opfunc(ot, userdata);
162         RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
163         RNA_def_struct_identifier(ot->srna, ot->idname);
164         BLI_addtail(&global_ops, ot);
165 }
166
167 /* ********************* macro operator ******************** */
168
169 /* macro exec only runs exec calls */
170 static int wm_macro_exec(bContext *C, wmOperator *op)
171 {
172         wmOperator *opm;
173         int retval= OPERATOR_FINISHED;
174         
175 //      printf("macro exec %s\n", op->type->idname);
176         
177         for(opm= op->macro.first; opm; opm= opm->next) {
178                 
179                 if(opm->type->exec) {
180 //                      printf("macro exec %s\n", opm->type->idname);
181                         retval= opm->type->exec(C, opm);
182                 
183                         if(!(retval & OPERATOR_FINISHED))
184                                 break;
185                 }
186         }
187 //      if(opm)
188 //              printf("macro ended not finished\n");
189 //      else
190 //              printf("macro end\n");
191         
192         return retval;
193 }
194
195 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
196 {
197         wmOperator *opm;
198         int retval= OPERATOR_FINISHED;
199         
200 //      printf("macro invoke %s\n", op->type->idname);
201         
202         for(opm= op->macro.first; opm; opm= opm->next) {
203                 
204                 if(opm->type->invoke)
205                         retval= opm->type->invoke(C, opm, event);
206                 else if(opm->type->exec)
207                         retval= opm->type->exec(C, opm);
208                 
209                 if(!(retval & OPERATOR_FINISHED))
210                         break;
211         }
212         
213 //      if(opm)
214 //              printf("macro ended not finished\n");
215 //      else
216 //              printf("macro end\n");
217         
218         
219         return retval;
220 }
221
222 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
223 {
224 //      printf("macro modal %s\n", op->type->idname);
225         
226         if(op->opm==NULL)
227                 printf("macro error, calling NULL modal()\n");
228         else {
229 //              printf("macro modal %s\n", op->opm->type->idname);
230                 return op->opm->type->modal(C, op->opm, event);
231         }       
232         
233         return OPERATOR_FINISHED;
234 }
235
236 /* Names have to be static for now */
237 wmOperatorType *WM_operatortype_append_macro(char *idname, char *name, int flag)
238 {
239         wmOperatorType *ot;
240         
241         if(WM_operatortype_exists(idname)) {
242                 printf("Macro error: operator %s exists\n", idname);
243                 return NULL;
244         }
245         
246         ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
247         ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
248         
249         ot->idname= idname;
250         ot->name= name;
251         ot->flag= OPTYPE_MACRO|flag;
252         
253         ot->exec= wm_macro_exec;
254         ot->invoke= wm_macro_invoke;
255         ot->modal= wm_macro_modal;
256         ot->poll= NULL;
257         
258         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.
259         RNA_def_struct_identifier(ot->srna, ot->idname);
260
261         BLI_addtail(&global_ops, ot);
262
263         return ot;
264 }
265
266 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
267 {
268         wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
269         
270         BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
271
272         /* do this on first use, since operatordefinitions might have been not done yet */
273         WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
274         
275         BLI_addtail(&ot->macro, otmacro);
276         
277         return otmacro;
278 }
279
280 static void wm_operatortype_free_macro(wmOperatorType *ot)
281 {
282         wmOperatorTypeMacro *otmacro;
283         
284         for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
285                 if(otmacro->ptr) {
286                         WM_operator_properties_free(otmacro->ptr);
287                         MEM_freeN(otmacro->ptr);
288                 }
289         }
290         BLI_freelistN(&ot->macro);
291 }
292
293
294 int WM_operatortype_remove(const char *idname)
295 {
296         wmOperatorType *ot = WM_operatortype_find(idname, 0);
297
298         if (ot==NULL)
299                 return 0;
300         
301         BLI_remlink(&global_ops, ot);
302         RNA_struct_free(&BLENDER_RNA, ot->srna);
303         
304         if(ot->macro.first)
305                 wm_operatortype_free_macro(ot);
306         
307         MEM_freeN(ot);
308
309         return 1;
310 }
311
312 /* SOME_OT_op -> some.op */
313 void WM_operator_py_idname(char *to, const char *from)
314 {
315         char *sep= strstr(from, "_OT_");
316         if(sep) {
317                 int i, ofs= (sep-from);
318
319                 for(i=0; i<ofs; i++)
320                         to[i]= tolower(from[i]);
321
322                 to[ofs] = '.';
323                 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
324         }
325         else {
326                 /* should not happen but support just incase */
327                 BLI_strncpy(to, from, OP_MAX_TYPENAME);
328         }
329 }
330
331 /* some.op -> SOME_OT_op */
332 void WM_operator_bl_idname(char *to, const char *from)
333 {
334         if (from) {
335                 char *sep= strchr(from, '.');
336
337                 if(sep) {
338                         int i, ofs= (sep-from);
339
340                         for(i=0; i<ofs; i++)
341                                 to[i]= toupper(from[i]);
342
343                         BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
344                         BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
345                 }
346                 else {
347                         /* should not happen but support just incase */
348                         BLI_strncpy(to, from, OP_MAX_TYPENAME);
349                 }
350         }
351         else
352                 to[0]= 0;
353 }
354
355 /* print a string representation of the operator, with the args that it runs 
356  * so python can run it again,
357  *
358  * When calling from an existing wmOperator do.
359  * WM_operator_pystring(op->type, op->ptr);
360  */
361 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
362 {
363         const char *arg_name= NULL;
364         char idname_py[OP_MAX_TYPENAME];
365
366         PropertyRNA *prop, *iterprop;
367
368         /* for building the string */
369         DynStr *dynstr= BLI_dynstr_new();
370         char *cstring, *buf;
371         int first_iter=1, ok= 1;
372
373
374         /* only to get the orginal props for comparisons */
375         PointerRNA opptr_default;
376         PropertyRNA *prop_default;
377         char *buf_default;
378         if(!all_args) {
379                 WM_operator_properties_create(&opptr_default, ot->idname);
380         }
381
382
383         WM_operator_py_idname(idname_py, ot->idname);
384         BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
385
386         iterprop= RNA_struct_iterator_property(opptr->type);
387
388         RNA_PROP_BEGIN(opptr, propptr, iterprop) {
389                 prop= propptr.data;
390                 arg_name= RNA_property_identifier(prop);
391
392                 if (strcmp(arg_name, "rna_type")==0) continue;
393
394                 buf= RNA_property_as_string(C, opptr, prop);
395                 
396                 ok= 1;
397
398                 if(!all_args) {
399                         /* not verbose, so only add in attributes that use non-default values
400                          * slow but good for tooltips */
401                         prop_default= RNA_struct_find_property(&opptr_default, arg_name);
402
403                         if(prop_default) {
404                                 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
405
406                                 if(strcmp(buf, buf_default)==0)
407                                         ok= 0; /* values match, dont bother printing */
408
409                                 MEM_freeN(buf_default);
410                         }
411
412                 }
413                 if(ok) {
414                         BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
415                         first_iter = 0;
416                 }
417
418                 MEM_freeN(buf);
419
420         }
421         RNA_PROP_END;
422
423         if(all_args==0)
424                 WM_operator_properties_free(&opptr_default);
425
426         BLI_dynstr_append(dynstr, ")");
427
428         cstring = BLI_dynstr_get_cstring(dynstr);
429         BLI_dynstr_free(dynstr);
430         return cstring;
431 }
432
433 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
434 {
435         wmOperatorType *ot= WM_operatortype_find(opstring, 0);
436
437         if(ot)
438                 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
439         else
440                 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
441 }
442
443 /* similar to the function above except its uses ID properties
444  * used for keymaps and macros */
445 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
446 {
447         if(*properties==NULL) {
448                 IDPropertyTemplate val = {0};
449                 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
450         }
451
452         if(*ptr==NULL) {
453                 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
454                 WM_operator_properties_create(*ptr, opstring);
455         }
456
457         (*ptr)->data= *properties;
458
459 }
460
461
462 void WM_operator_properties_free(PointerRNA *ptr)
463 {
464         IDProperty *properties= ptr->data;
465
466         if(properties) {
467                 IDP_FreeProperty(properties);
468                 MEM_freeN(properties);
469         }
470 }
471
472 /* ************ default op callbacks, exported *********** */
473
474 /* invoke callback, uses enum property named "type" */
475 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
476 {
477         PropertyRNA *prop;
478         uiPopupMenu *pup;
479         uiLayout *layout;
480
481         prop= RNA_struct_find_property(op->ptr, "type");
482
483         if(!prop) {
484                 RNA_STRUCT_BEGIN(op->ptr, findprop) {
485                         if(RNA_property_type(findprop) == PROP_ENUM) {
486                                 prop= findprop;
487                                 break;
488                         }
489                 }
490                 RNA_STRUCT_END;
491         }
492
493         if(prop==NULL) {
494                 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
495         }
496         else if (RNA_property_type(prop) != PROP_ENUM) {
497                 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
498         }
499         else {
500                 pup= uiPupMenuBegin(C, op->type->name, 0);
501                 layout= uiPupMenuLayout(pup);
502                 uiItemsEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop));
503                 uiPupMenuEnd(C, pup);
504         }
505
506         return OPERATOR_CANCELLED;
507 }
508
509 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
510 int WM_operator_confirm_message(bContext *C, wmOperator *op, char *message)
511 {
512         uiPopupMenu *pup;
513         uiLayout *layout;
514
515         pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
516         layout= uiPupMenuLayout(pup);
517         uiItemO(layout, message, 0, op->type->idname);
518         uiPupMenuEnd(C, pup);
519         
520         return OPERATOR_CANCELLED;
521 }
522
523
524 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
525 {
526         return WM_operator_confirm_message(C, op, NULL);
527 }
528
529 /* op->invoke, opens fileselect if path property not set, otherwise executes */
530 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
531 {
532         if (RNA_property_is_set(op->ptr, "path")) {
533                 return WM_operator_call(C, op);
534         } 
535         else {
536                 WM_event_add_fileselect(C, op);
537                 return OPERATOR_RUNNING_MODAL;
538         }
539 }
540
541 /* default properties for fileselect */
542 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type)
543 {
544         PropertyRNA *prop;
545
546         RNA_def_string_file_path(ot->srna, "path", "", FILE_MAX, "File Path", "Path to file.");
547         RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file.");
548         RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file.");
549
550         prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
551         RNA_def_property_flag(prop, PROP_HIDDEN);
552         prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
553         RNA_def_property_flag(prop, PROP_HIDDEN);
554         prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
555         RNA_def_property_flag(prop, PROP_HIDDEN);
556         prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
557         RNA_def_property_flag(prop, PROP_HIDDEN);
558         prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
559         RNA_def_property_flag(prop, PROP_HIDDEN);
560         prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
561         RNA_def_property_flag(prop, PROP_HIDDEN);
562         prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
563         RNA_def_property_flag(prop, PROP_HIDDEN);
564         prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
565         RNA_def_property_flag(prop, PROP_HIDDEN);
566
567         prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL, 
568                 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file.",
569                 FILE_LOADLIB, FILE_SPECIAL);
570         RNA_def_property_flag(prop, PROP_HIDDEN);
571 }
572
573 /* op->poll */
574 int WM_operator_winactive(bContext *C)
575 {
576         if(CTX_wm_window(C)==NULL) return 0;
577         return 1;
578 }
579
580 /* op->invoke */
581 static void redo_cb(bContext *C, void *arg_op, int event)
582 {
583         wmOperator *lastop= arg_op;
584         
585         if(lastop) {
586                 ED_undo_pop_op(C, lastop);
587                 WM_operator_repeat(C, lastop);
588         }
589 }
590
591 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
592 {
593         wmWindowManager *wm= CTX_wm_manager(C);
594         wmOperator *op= arg_op;
595         PointerRNA ptr;
596         uiBlock *block;
597         uiLayout *layout;
598         uiStyle *style= U.uistyles.first;
599         
600         block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
601         uiBlockClearFlag(block, UI_BLOCK_LOOP);
602         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
603         uiBlockSetHandleFunc(block, redo_cb, arg_op);
604
605         if(!op->properties) {
606                 IDPropertyTemplate val = {0};
607                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
608         }
609
610         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
611         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
612         uiItemL(layout, op->type->name, 0);
613
614         if(op->type->ui)
615                 op->type->ui((bContext*)C, &ptr, layout);
616         else
617                 uiDefAutoButsRNA(C, layout, &ptr, 2);
618
619         uiPopupBoundsBlock(block, 4.0f, 0, 0);
620         uiEndBlock(C, block);
621
622         return block;
623 }
624
625 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
626 {
627         int retval= OPERATOR_CANCELLED;
628         
629         if(op->type->exec)
630                 retval= op->type->exec(C, op);
631
632         if(retval != OPERATOR_CANCELLED)
633                 uiPupBlock(C, wm_block_create_redo, op);
634
635         return retval;
636 }
637
638 int WM_operator_redo_popup(bContext *C, wmOperator *op)
639 {
640         uiPupBlock(C, wm_block_create_redo, op);
641
642         return OPERATOR_CANCELLED;
643 }
644
645 /* ***************** Debug menu ************************* */
646
647 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
648 {
649         wmOperator *op= arg_op;
650         uiBlock *block;
651         uiLayout *layout;
652         uiStyle *style= U.uistyles.first;
653         
654         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
655         uiBlockClearFlag(block, UI_BLOCK_LOOP);
656         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
657         
658         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
659         uiItemL(layout, op->type->name, 0);
660
661         if(op->type->ui)
662                 op->type->ui(C, op->ptr, layout);
663         else
664                 uiDefAutoButsRNA(C, layout, op->ptr, 2);
665         
666         uiPopupBoundsBlock(block, 4.0f, 0, 0);
667         uiEndBlock(C, block);
668         
669         return block;
670 }
671
672 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
673 {
674         G.rt= RNA_int_get(op->ptr, "debugval");
675         ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
676         WM_event_add_notifier(C, NC_WINDOW, NULL);
677         
678         return OPERATOR_FINISHED;       
679 }
680
681 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
682 {
683         
684         RNA_int_set(op->ptr, "debugval", G.rt);
685         
686         /* pass on operator, so return modal */
687         uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
688         
689         return OPERATOR_RUNNING_MODAL;
690 }
691
692 static void WM_OT_debug_menu(wmOperatorType *ot)
693 {
694         ot->name= "Debug Menu";
695         ot->idname= "WM_OT_debug_menu";
696         ot->description= "Open a popup to set the debug level.";
697         
698         ot->invoke= wm_debug_menu_invoke;
699         ot->exec= wm_debug_menu_exec;
700         ot->poll= WM_operator_winactive;
701         
702         RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
703 }
704
705 /* ***************** Search menu ************************* */
706 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
707 {
708         wmOperatorType *ot= arg2;
709         
710         if(ot)
711                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
712 }
713
714 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
715 {
716         wmOperatorType *ot = WM_operatortype_first();
717         
718         for(; ot; ot= ot->next) {
719                 
720                 if(BLI_strcasestr(ot->name, str)) {
721                         if(WM_operator_poll((bContext*)C, ot)) {
722                                 char name[256];
723                                 int len= strlen(ot->name);
724                                 
725                                 /* display name for menu, can hold hotkey */
726                                 BLI_strncpy(name, ot->name, 256);
727                                 
728                                 /* check for hotkey */
729                                 if(len < 256-6) {
730                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
731                                                 name[len]= '|';
732                                 }
733                                 
734                                 if(0==uiSearchItemAdd(items, name, ot, 0))
735                                         break;
736                         }
737                 }
738         }
739 }
740
741 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
742 {
743         static char search[256]= "";
744         wmEvent event;
745         wmWindow *win= CTX_wm_window(C);
746         uiBlock *block;
747         uiBut *but;
748         
749         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
750         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
751         
752         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
753         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
754         
755         /* fake button, it holds space for search items */
756         uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
757         
758         uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
759         uiEndBlock(C, block);
760         
761         event= *(win->eventstate);      /* XXX huh huh? make api call */
762         event.type= EVT_BUT_OPEN;
763         event.val= KM_PRESS;
764         event.customdata= but;
765         event.customdatafree= FALSE;
766         wm_event_add(win, &event);
767         
768         return block;
769 }
770
771 static int wm_search_menu_exec(bContext *C, wmOperator *op)
772 {
773         
774         return OPERATOR_FINISHED;       
775 }
776
777 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
778 {
779         
780         uiPupBlock(C, wm_block_search_menu, op);
781         
782         return OPERATOR_CANCELLED;
783 }
784
785 /* op->poll */
786 static int wm_search_menu_poll(bContext *C)
787 {
788         if(CTX_wm_window(C)==NULL) return 0;
789         if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0;  // XXX - so we can use the shortcut in the console
790         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
791         if(CTX_data_edit_object(C) && CTX_data_edit_object(C)->type==OB_CURVE) return 0; // XXX - so we can use the spacebar for entering text
792         return 1;
793 }
794
795 static void WM_OT_search_menu(wmOperatorType *ot)
796 {
797         ot->name= "Search Menu";
798         ot->idname= "WM_OT_search_menu";
799         
800         ot->invoke= wm_search_menu_invoke;
801         ot->exec= wm_search_menu_exec;
802         ot->poll= wm_search_menu_poll;
803 }
804
805 static int wm_call_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
806 {
807         char idname[BKE_ST_MAXNAME];
808         RNA_string_get(op->ptr, "name", idname);
809
810         uiPupMenuInvoke(C, idname);
811
812         return OPERATOR_CANCELLED;
813 }
814
815 static void WM_OT_call_menu(wmOperatorType *ot)
816 {
817         ot->name= "Call Menu";
818         ot->idname= "WM_OT_call_menu";
819
820         ot->invoke= wm_call_menu_invoke;
821
822         RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the new sequence strip");
823 }
824
825 /* ************ window / screen operator definitions ************** */
826
827 static void WM_OT_window_duplicate(wmOperatorType *ot)
828 {
829         ot->name= "Duplicate Window";
830         ot->idname= "WM_OT_window_duplicate";
831         ot->description="Duplicate the current Blender window.";
832                 
833         ot->exec= wm_window_duplicate_op;
834         ot->poll= WM_operator_winactive;
835 }
836
837 static void WM_OT_save_homefile(wmOperatorType *ot)
838 {
839         ot->name= "Save User Settings";
840         ot->idname= "WM_OT_save_homefile";
841         ot->description="Make the current file the default .blend file.";
842                 
843         ot->invoke= WM_operator_confirm;
844         ot->exec= WM_write_homefile;
845         ot->poll= WM_operator_winactive;
846 }
847
848 static void WM_OT_read_homefile(wmOperatorType *ot)
849 {
850         ot->name= "Reload Start-Up File";
851         ot->idname= "WM_OT_read_homefile";
852         ot->description="Open the default file (doesn't save the current file).";
853         
854         ot->invoke= WM_operator_confirm;
855         ot->exec= WM_read_homefile;
856         ot->poll= WM_operator_winactive;
857         
858         RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
859 }
860
861
862 /* ********* recent file *********** */
863
864 static int recentfile_exec(bContext *C, wmOperator *op)
865 {
866         int event= RNA_enum_get(op->ptr, "file");
867
868         // XXX wm in context is not set correctly after WM_read_file -> crash
869         // do it before for now, but is this correct with multiple windows?
870
871         if(event>0) {
872                 if (G.sce[0] && (event==1)) {
873                         WM_event_add_notifier(C, NC_WINDOW, NULL);
874                         WM_read_file(C, G.sce, op->reports);
875                 }
876                 else {
877                         struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-1);
878                         if(recent) {
879                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
880                                 WM_read_file(C, recent->filename, op->reports);
881                         }
882                 }
883         }
884         return 0;
885 }
886
887 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
888 {
889         uiPopupMenu *pup;
890         uiLayout *layout;
891
892         pup= uiPupMenuBegin(C, "Open Recent", 0);
893         layout= uiPupMenuLayout(pup);
894         uiItemsEnumO(layout, op->type->idname, "file");
895         uiPupMenuEnd(C, pup);
896         
897         return OPERATOR_CANCELLED;
898 }
899
900 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
901 {
902         EnumPropertyItem tmp = {0, "", 0, "", ""};
903         EnumPropertyItem *item= NULL;
904         struct RecentFile *recent;
905         int totitem= 0, i;
906
907         /* dynamically construct enum */
908         for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
909                 tmp.value= i+1;
910                 tmp.identifier= recent->filename;
911                 tmp.name= BLI_short_filename(recent->filename);
912                 RNA_enum_item_add(&item, &totitem, &tmp);
913         }
914
915         RNA_enum_item_end(&item, &totitem);
916         *free= 1;
917
918         return item;
919 }
920
921 static void WM_OT_open_recentfile(wmOperatorType *ot)
922 {
923         PropertyRNA *prop;
924         static EnumPropertyItem file_items[]= {
925                 {0, NULL, 0, NULL, NULL}};
926
927         ot->name= "Open Recent File";
928         ot->idname= "WM_OT_open_recentfile";
929         ot->description="Open recent files list.";
930         
931         ot->invoke= wm_recentfile_invoke;
932         ot->exec= recentfile_exec;
933         ot->poll= WM_operator_winactive;
934         
935         prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
936         RNA_def_enum_funcs(prop, open_recentfile_itemf);
937 }
938
939 /* *************** open file **************** */
940
941 static void open_set_load_ui(wmOperator *op)
942 {
943         if(!RNA_property_is_set(op->ptr, "load_ui"))
944                 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
945 }
946
947 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
948 {
949         RNA_string_set(op->ptr, "path", G.sce);
950         open_set_load_ui(op);
951
952         WM_event_add_fileselect(C, op);
953
954         return OPERATOR_RUNNING_MODAL;
955 }
956
957 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
958 {
959         char path[FILE_MAX];
960
961         RNA_string_get(op->ptr, "path", path);
962         open_set_load_ui(op);
963
964         if(RNA_boolean_get(op->ptr, "load_ui"))
965                 G.fileflags &= ~G_FILE_NO_UI;
966         else
967                 G.fileflags |= G_FILE_NO_UI;
968         
969         // XXX wm in context is not set correctly after WM_read_file -> crash
970         // do it before for now, but is this correct with multiple windows?
971         WM_event_add_notifier(C, NC_WINDOW, NULL);
972
973         WM_read_file(C, path, op->reports);
974         
975         return OPERATOR_FINISHED;
976 }
977
978 static void WM_OT_open_mainfile(wmOperatorType *ot)
979 {
980         ot->name= "Open Blender File";
981         ot->idname= "WM_OT_open_mainfile";
982         ot->description="Open a Blender file.";
983         
984         ot->invoke= wm_open_mainfile_invoke;
985         ot->exec= wm_open_mainfile_exec;
986         ot->poll= WM_operator_winactive;
987         
988         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
989
990         RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file.");
991 }
992
993 /* **************** link/append *************** */
994
995 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *event)
996 {
997         if(RNA_property_is_set(op->ptr, "path")) {
998                 return WM_operator_call(C, op);
999         } 
1000         else {
1001                 /* XXX TODO solve where to get last linked library from */
1002                 RNA_string_set(op->ptr, "path", G.lib);
1003                 WM_event_add_fileselect(C, op);
1004                 return OPERATOR_RUNNING_MODAL;
1005         }
1006 }
1007
1008 static short wm_link_append_flag(wmOperator *op)
1009 {
1010         short flag= 0;
1011
1012         if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1013         if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1014         if(RNA_boolean_get(op->ptr, "relative_paths")) flag |= FILE_STRINGCODE;
1015         if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1016
1017         return flag;
1018 }
1019
1020 static void wm_link_make_library_local(Main *main, const char *libname)
1021 {
1022         Library *lib;
1023
1024         /* and now find the latest append lib file */
1025         for(lib= main->library.first; lib; lib=lib->id.next)
1026                 if(BLI_streq(libname, lib->filename))
1027                         break;
1028         
1029         /* make local */
1030         if(lib) {
1031                 all_local(lib, 1);
1032                 /* important we unset, otherwise these object wont
1033                  * link into other scenes from this blend file */
1034                 flag_all_listbases_ids(LIB_APPEND_TAG, 0);
1035         }
1036 }
1037
1038 static int wm_link_append_exec(bContext *C, wmOperator *op)
1039 {
1040         Main *bmain= CTX_data_main(C);
1041         Scene *scene= CTX_data_scene(C);
1042         Main *mainl= 0;
1043         BlendHandle *bh;
1044         PropertyRNA *prop;
1045         char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1046         int idcode, totfiles=0;
1047         short flag;
1048
1049         name[0] = '\0';
1050         RNA_string_get(op->ptr, "filename", name);
1051         RNA_string_get(op->ptr, "directory", dir);
1052
1053         /* test if we have a valid data */
1054         if(BLO_is_a_library(dir, libname, group) == 0) {
1055                 BKE_report(op->reports, RPT_ERROR, "Not a library");
1056                 return OPERATOR_CANCELLED;
1057         }
1058         else if(group[0] == 0) {
1059                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1060                 return OPERATOR_CANCELLED;
1061         }
1062         else if(BLI_streq(bmain->name, libname)) {
1063                 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1064                 return OPERATOR_CANCELLED;
1065         }
1066
1067         /* check if something is indicated for append/link */
1068         prop = RNA_struct_find_property(op->ptr, "files");
1069         if(prop) {
1070                 totfiles= RNA_property_collection_length(op->ptr, prop);
1071                 if(totfiles == 0) {
1072                         if(name[0] == '\0') {
1073                                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1074                                 return OPERATOR_CANCELLED;
1075                         }
1076                 }
1077         }
1078         else if(name[0] == '\0') {
1079                 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1080                 return OPERATOR_CANCELLED;
1081         }
1082
1083         /* now we have or selected, or an indicated file */
1084         if(RNA_boolean_get(op->ptr, "autoselect"))
1085                 scene_deselect_all(scene);
1086
1087         bh = BLO_blendhandle_from_file(libname);
1088         idcode = BLO_idcode_from_name(group);
1089         
1090         flag = wm_link_append_flag(op);
1091
1092         /* tag everything, all untagged data can be made local */
1093         if((flag & FILE_LINK)==0)
1094                 flag_all_listbases_ids(LIB_APPEND_TAG, 1);
1095
1096         /* here appending/linking starts */
1097         mainl = BLO_library_append_begin(C, &bh, libname);
1098         if(totfiles == 0) {
1099                 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1100         }
1101         else {
1102                 RNA_BEGIN(op->ptr, itemptr, "files") {
1103                         RNA_string_get(&itemptr, "name", name);
1104                         BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1105                 }
1106                 RNA_END;
1107         }
1108         BLO_library_append_end(C, mainl, &bh, idcode, flag);
1109         
1110         /* mark all library linked objects to be updated */
1111         recalc_all_library_objects(bmain);
1112
1113         /* append, rather than linking */
1114         if((flag & FILE_LINK)==0)
1115                 wm_link_make_library_local(bmain, libname);
1116
1117         /* recreate dependency graph to include new objects */
1118         DAG_scene_sort(scene);
1119         DAG_ids_flush_update(0);
1120
1121         BLO_blendhandle_close(bh);
1122
1123         /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1124         BLI_strncpy(G.lib, dir, FILE_MAX);
1125
1126         WM_event_add_notifier(C, NC_WINDOW, NULL);
1127
1128         return OPERATOR_FINISHED;
1129 }
1130
1131 static void WM_OT_link_append(wmOperatorType *ot)
1132 {
1133         ot->name= "Link/Append from Library";
1134         ot->idname= "WM_OT_link_append";
1135         ot->description= "Link or Append from a Library .blend file";
1136         
1137         ot->invoke= wm_link_append_invoke;
1138         ot->exec= wm_link_append_exec;
1139         ot->poll= WM_operator_winactive;
1140         
1141         ot->flag |= OPTYPE_UNDO;
1142
1143         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB);
1144         
1145         RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending.");
1146         RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects.");
1147         RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer.");
1148         RNA_def_boolean(ot->srna, "relative_paths", 1, "Relative Paths", "Store the library path as a relative path to current .blend file.");
1149
1150         RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1151 }       
1152
1153 /* *************** recover last session **************** */
1154
1155 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1156 {
1157         char filename[FILE_MAX];
1158
1159         G.fileflags |= G_FILE_RECOVER;
1160
1161         // XXX wm in context is not set correctly after WM_read_file -> crash
1162         // do it before for now, but is this correct with multiple windows?
1163         WM_event_add_notifier(C, NC_WINDOW, NULL);
1164
1165         /* load file */
1166         BLI_make_file_string("/", filename, btempdir, "quit.blend");
1167         WM_read_file(C, filename, op->reports);
1168
1169         G.fileflags &= ~G_FILE_RECOVER;
1170
1171         return OPERATOR_FINISHED;
1172 }
1173
1174 static void WM_OT_recover_last_session(wmOperatorType *ot)
1175 {
1176         ot->name= "Recover Last Session";
1177         ot->idname= "WM_OT_recover_last_session";
1178         ot->description="Open the last closed file (\"quit.blend\").";
1179         
1180         ot->exec= wm_recover_last_session_exec;
1181         ot->poll= WM_operator_winactive;
1182 }
1183
1184 /* *************** recover auto save **************** */
1185
1186 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1187 {
1188         char path[FILE_MAX];
1189
1190         RNA_string_get(op->ptr, "path", path);
1191
1192         G.fileflags |= G_FILE_RECOVER;
1193
1194         // XXX wm in context is not set correctly after WM_read_file -> crash
1195         // do it before for now, but is this correct with multiple windows?
1196         WM_event_add_notifier(C, NC_WINDOW, NULL);
1197
1198         /* load file */
1199         WM_read_file(C, path, op->reports);
1200
1201         G.fileflags &= ~G_FILE_RECOVER;
1202
1203         return OPERATOR_FINISHED;
1204 }
1205
1206 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
1207 {
1208         char filename[FILE_MAX];
1209
1210         wm_autosave_location(filename);
1211         RNA_string_set(op->ptr, "path", filename);
1212         WM_event_add_fileselect(C, op);
1213
1214         return OPERATOR_RUNNING_MODAL;
1215 }
1216
1217 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1218 {
1219         ot->name= "Recover Auto Save";
1220         ot->idname= "WM_OT_recover_auto_save";
1221         ot->description="Open an automatically saved file to recover it.";
1222         
1223         ot->exec= wm_recover_auto_save_exec;
1224         ot->invoke= wm_recover_auto_save_invoke;
1225         ot->poll= WM_operator_winactive;
1226
1227         WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER);
1228 }
1229
1230 /* *************** save file as **************** */
1231
1232 static void untitled(char *name)
1233 {
1234         if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1235                 char *c= BLI_last_slash(name);
1236                 
1237                 if(c)
1238                         strcpy(&c[1], "untitled.blend");
1239                 else
1240                         strcpy(name, "untitled.blend");
1241         }
1242 }
1243
1244 static void save_set_compress(wmOperator *op)
1245 {
1246         if(!RNA_property_is_set(op->ptr, "compress")) {
1247                 if(G.save_over) /* keep flag for existing file */
1248                         RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1249                 else /* use userdef for new file */
1250                         RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1251         }
1252 }
1253
1254 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1255 {
1256         char name[FILE_MAX];
1257
1258         save_set_compress(op);
1259         
1260         BLI_strncpy(name, G.sce, FILE_MAX);
1261         untitled(name);
1262         RNA_string_set(op->ptr, "path", name);
1263         
1264         WM_event_add_fileselect(C, op);
1265
1266         return OPERATOR_RUNNING_MODAL;
1267 }
1268
1269 /* function used for WM_OT_save_mainfile too */
1270 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1271 {
1272         char path[FILE_MAX];
1273         int fileflags;
1274
1275         save_set_compress(op);
1276         
1277         if(RNA_property_is_set(op->ptr, "path"))
1278                 RNA_string_get(op->ptr, "path", path);
1279         else {
1280                 BLI_strncpy(path, G.sce, FILE_MAX);
1281                 untitled(path);
1282         }
1283
1284         fileflags= G.fileflags;
1285
1286         /* set compression flag */
1287         if(RNA_boolean_get(op->ptr, "compress"))
1288                 fileflags |= G_FILE_COMPRESS;
1289         else
1290                 fileflags &= ~G_FILE_COMPRESS;
1291
1292         WM_write_file(C, path, fileflags, op->reports);
1293         
1294         WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1295
1296         return 0;
1297 }
1298
1299 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1300 {
1301         ot->name= "Save As Blender File";
1302         ot->idname= "WM_OT_save_as_mainfile";
1303         ot->description="Save the current file in the desired location.";
1304         
1305         ot->invoke= wm_save_as_mainfile_invoke;
1306         ot->exec= wm_save_as_mainfile_exec;
1307         ot->poll= WM_operator_winactive;
1308         
1309         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1310         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1311 }
1312
1313 /* *************** save file directly ******** */
1314
1315 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1316 {
1317         char name[FILE_MAX];
1318
1319         save_set_compress(op);
1320         
1321         BLI_strncpy(name, G.sce, FILE_MAX);
1322         untitled(name);
1323         RNA_string_set(op->ptr, "path", name);
1324         
1325         if (G.save_over)
1326                 uiPupMenuSaveOver(C, op, name);
1327         else
1328                 WM_event_add_fileselect(C, op);
1329         
1330         return OPERATOR_RUNNING_MODAL;
1331 }
1332
1333 static void WM_OT_save_mainfile(wmOperatorType *ot)
1334 {
1335         ot->name= "Save Blender File";
1336         ot->idname= "WM_OT_save_mainfile";
1337         ot->description="Save the current Blender file.";
1338         
1339         ot->invoke= wm_save_mainfile_invoke;
1340         ot->exec= wm_save_as_mainfile_exec;
1341         ot->poll= WM_operator_winactive;
1342         
1343         WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER);
1344         RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1345 }
1346
1347
1348 /* XXX: move these collada operators to a more appropriate place */
1349 #ifdef WITH_COLLADA
1350
1351 #include "../../collada/collada.h"
1352
1353 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *event)
1354 {
1355         //char name[FILE_MAX];
1356         //BLI_strncpy(name, G.sce, FILE_MAX);
1357         //untitled(name);
1358
1359         /* RNA_string_set(op->ptr, "path", "/tmp/test.dae"); */
1360         
1361         WM_event_add_fileselect(C, op);
1362
1363         return OPERATOR_RUNNING_MODAL;
1364 }
1365
1366 /* function used for WM_OT_save_mainfile too */
1367 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1368 {
1369         char filename[FILE_MAX];
1370         
1371         if(RNA_property_is_set(op->ptr, "path"))
1372                 RNA_string_get(op->ptr, "path", filename);
1373         else {
1374                 BLI_strncpy(filename, G.sce, FILE_MAX);
1375                 untitled(filename);
1376         }
1377         
1378         //WM_write_file(C, filename, op->reports);
1379         collada_export(CTX_data_scene(C), filename);
1380         
1381         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
1382
1383         return OPERATOR_FINISHED;
1384 }
1385
1386 static void WM_OT_collada_export(wmOperatorType *ot)
1387 {
1388         ot->name= "Export COLLADA";
1389         ot->idname= "WM_OT_collada_export";
1390         
1391         ot->invoke= wm_collada_export_invoke;
1392         ot->exec= wm_collada_export_exec;
1393         ot->poll= WM_operator_winactive;
1394         
1395         ot->flag= 0;
1396         
1397         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
1398 }
1399
1400 static int wm_collada_import_invoke(bContext *C, wmOperator *op, wmEvent *event)
1401 {
1402         /* RNA_string_set(op->ptr, "path", "/tmp/test.dae"); */
1403         
1404         WM_event_add_fileselect(C, op);
1405
1406         return OPERATOR_RUNNING_MODAL;
1407 }
1408
1409 /* function used for WM_OT_save_mainfile too */
1410 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1411 {
1412         char filename[FILE_MAX];
1413         
1414         if(RNA_property_is_set(op->ptr, "path"))
1415                 RNA_string_get(op->ptr, "path", filename);
1416         else {
1417                 BLI_strncpy(filename, G.sce, FILE_MAX);
1418                 untitled(filename);
1419         }
1420         
1421         //WM_write_file(C, filename, op->reports);
1422         collada_import(C, filename);
1423         
1424         /* WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL); */
1425
1426         return OPERATOR_FINISHED;
1427 }
1428
1429 static void WM_OT_collada_import(wmOperatorType *ot)
1430 {
1431         ot->name= "Import COLLADA";
1432         ot->idname= "WM_OT_collada_import";
1433         
1434         ot->invoke= wm_collada_import_invoke;
1435         ot->exec= wm_collada_import_exec;
1436         ot->poll= WM_operator_winactive;
1437         
1438         ot->flag= 0;
1439         
1440         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
1441 }
1442
1443 #endif
1444
1445
1446
1447 /* *********************** */
1448
1449 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1450 {
1451         ot->name= "Toggle Fullscreen";
1452         ot->idname= "WM_OT_window_fullscreen_toggle";
1453         ot->description="Toggle the current window fullscreen.";
1454
1455         ot->exec= wm_window_fullscreen_toggle_op;
1456         ot->poll= WM_operator_winactive;
1457 }
1458
1459 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1460 {
1461         WM_operator_free(op);
1462         
1463         WM_exit(C);     
1464         
1465         return OPERATOR_FINISHED;
1466 }
1467
1468 static void WM_OT_exit_blender(wmOperatorType *ot)
1469 {
1470         ot->name= "Exit Blender";
1471         ot->idname= "WM_OT_exit_blender";
1472         ot->description= "Quit Blender.";
1473
1474         ot->invoke= WM_operator_confirm;
1475         ot->exec= wm_exit_blender_op;
1476         ot->poll= WM_operator_winactive;
1477 }
1478
1479 /* ************ default paint cursors, draw always around cursor *********** */
1480 /*
1481  - returns handler to free 
1482  - poll(bContext): returns 1 if draw should happen
1483  - draw(bContext): drawing callback for paint cursor
1484 */
1485
1486 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1487                                wmPaintCursorDraw draw, void *customdata)
1488 {
1489         wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1490         
1491         BLI_addtail(&wm->paintcursors, pc);
1492         
1493         pc->customdata = customdata;
1494         pc->poll= poll;
1495         pc->draw= draw;
1496         
1497         return pc;
1498 }
1499
1500 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1501 {
1502         wmPaintCursor *pc;
1503         
1504         for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1505                 if(pc == (wmPaintCursor *)handle) {
1506                         BLI_remlink(&wm->paintcursors, pc);
1507                         MEM_freeN(pc);
1508                         return;
1509                 }
1510         }
1511 }
1512
1513 /* ************ window gesture operator-callback definitions ************** */
1514 /*
1515  * These are default callbacks for use in operators requiring gesture input
1516  */
1517
1518 /* **************** Border gesture *************** */
1519
1520 /* Border gesture has two types:
1521    1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border 
1522    2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1523
1524    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1525 */
1526
1527 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1528 {
1529         wmGesture *gesture= op->customdata;
1530         rcti *rect= gesture->customdata;
1531         
1532         if(rect->xmin > rect->xmax)
1533                 SWAP(int, rect->xmin, rect->xmax);
1534         if(rect->ymin > rect->ymax)
1535                 SWAP(int, rect->ymin, rect->ymax);
1536         
1537         if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1538                 return 0;
1539                 
1540         /* operator arguments and storage. */
1541         RNA_int_set(op->ptr, "xmin", rect->xmin);
1542         RNA_int_set(op->ptr, "ymin", rect->ymin);
1543         RNA_int_set(op->ptr, "xmax", rect->xmax);
1544         RNA_int_set(op->ptr, "ymax", rect->ymax);
1545         
1546         /* XXX weak; border should be configured for this without reading event types */
1547         if( RNA_struct_find_property(op->ptr, "event_type") ) {
1548                 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1549                         event_type= LEFTMOUSE;
1550                 
1551                 RNA_int_set(op->ptr, "event_type", event_type);
1552         }
1553         op->type->exec(C, op);
1554         
1555         return 1;
1556 }
1557
1558 static void wm_gesture_end(bContext *C, wmOperator *op)
1559 {
1560         wmGesture *gesture= op->customdata;
1561         
1562         WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1563         op->customdata= NULL;
1564
1565         ED_area_tag_redraw(CTX_wm_area(C));
1566         
1567         if( RNA_struct_find_property(op->ptr, "cursor") )
1568                 WM_cursor_restore(CTX_wm_window(C));
1569 }
1570
1571 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1572 {
1573         if(ISTWEAK(event->type))
1574                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1575         else
1576                 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1577
1578         /* add modal handler */
1579         WM_event_add_modal_handler(C, op);
1580         
1581         wm_gesture_tag_redraw(C);
1582
1583         return OPERATOR_RUNNING_MODAL;
1584 }
1585
1586 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1587 {
1588         wmGesture *gesture= op->customdata;
1589         rcti *rect= gesture->customdata;
1590         int sx, sy;
1591         
1592         switch(event->type) {
1593                 case MOUSEMOVE:
1594                         
1595                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1596                         
1597                         if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1598                                 rect->xmin= rect->xmax= event->x - sx;
1599                                 rect->ymin= rect->ymax= event->y - sy;
1600                         }
1601                         else {
1602                                 rect->xmax= event->x - sx;
1603                                 rect->ymax= event->y - sy;
1604                         }
1605                         
1606                         wm_gesture_tag_redraw(C);
1607
1608                         break;
1609                         
1610                 case LEFTMOUSE:
1611                 case MIDDLEMOUSE:
1612                 case RIGHTMOUSE:
1613                         if(event->val==KM_PRESS) {
1614                                 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1615                                         gesture->mode= 1;
1616                                         wm_gesture_tag_redraw(C);
1617                                 }
1618                         }
1619                         else {
1620                                 if(border_apply(C, op, event->type, gesture->event_type)) {
1621                                         wm_gesture_end(C, op);
1622                                         return OPERATOR_FINISHED;
1623                                 }
1624                                 wm_gesture_end(C, op);
1625                                 return OPERATOR_CANCELLED;
1626                         }
1627                         break;
1628                 case ESCKEY:
1629                         wm_gesture_end(C, op);
1630                         return OPERATOR_CANCELLED;
1631         }
1632         return OPERATOR_RUNNING_MODAL;
1633 }
1634
1635 /* **************** circle gesture *************** */
1636 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1637
1638 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1639 {
1640         op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1641         
1642         /* add modal handler */
1643         WM_event_add_modal_handler(C, op);
1644         
1645         wm_gesture_tag_redraw(C);
1646         
1647         return OPERATOR_RUNNING_MODAL;
1648 }
1649
1650 static void gesture_circle_apply(bContext *C, wmOperator *op)
1651 {
1652         wmGesture *gesture= op->customdata;
1653         rcti *rect= gesture->customdata;
1654         
1655         /* operator arguments and storage. */
1656         RNA_int_set(op->ptr, "x", rect->xmin);
1657         RNA_int_set(op->ptr, "y", rect->ymin);
1658         RNA_int_set(op->ptr, "radius", rect->xmax);
1659         
1660         if(op->type->exec)
1661                 op->type->exec(C, op);
1662 }
1663
1664 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1665 {
1666         wmGesture *gesture= op->customdata;
1667         rcti *rect= gesture->customdata;
1668         int sx, sy;
1669         
1670         switch(event->type) {
1671                 case MOUSEMOVE:
1672                         
1673                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1674                         
1675                         rect->xmin= event->x - sx;
1676                         rect->ymin= event->y - sy;
1677                         
1678                         wm_gesture_tag_redraw(C);
1679                         
1680                         if(gesture->mode)
1681                                 gesture_circle_apply(C, op);
1682
1683                         break;
1684                 case WHEELUPMOUSE:
1685                         rect->xmax += 2 + rect->xmax/10;
1686                         wm_gesture_tag_redraw(C);
1687                         break;
1688                 case WHEELDOWNMOUSE:
1689                         rect->xmax -= 2 + rect->xmax/10;
1690                         if(rect->xmax < 1) rect->xmax= 1;
1691                         wm_gesture_tag_redraw(C);
1692                         break;
1693                 case LEFTMOUSE:
1694                 case MIDDLEMOUSE:
1695                 case RIGHTMOUSE:
1696                         if(event->val==KM_RELEASE) {    /* key release */
1697                                 wm_gesture_end(C, op);
1698                                 return OPERATOR_FINISHED;
1699                         }
1700                         else {
1701                                 if( RNA_struct_find_property(op->ptr, "event_type") )
1702                                         RNA_int_set(op->ptr, "event_type", event->type);
1703                                 
1704                                 /* apply first click */
1705                                 gesture_circle_apply(C, op);
1706                                 gesture->mode= 1;
1707                         }
1708                         break;
1709                 case ESCKEY:
1710                         wm_gesture_end(C, op);
1711                         return OPERATOR_CANCELLED;
1712         }
1713         return OPERATOR_RUNNING_MODAL;
1714 }
1715
1716 #if 0
1717 /* template to copy from */
1718 void WM_OT_circle_gesture(wmOperatorType *ot)
1719 {
1720         ot->name= "Circle Gesture";
1721         ot->idname= "WM_OT_circle_gesture";
1722         ot->description="Enter rotate mode with a circular gesture.";
1723         
1724         ot->invoke= WM_gesture_circle_invoke;
1725         ot->modal= WM_gesture_circle_modal;
1726         
1727         ot->poll= WM_operator_winactive;
1728         
1729         RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1730         RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1731         RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1732
1733 }
1734 #endif
1735
1736 /* **************** Tweak gesture *************** */
1737
1738 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1739 {
1740         wmWindow *window= CTX_wm_window(C);
1741         wmGesture *gesture= window->tweak;
1742         rcti *rect= gesture->customdata;
1743         int sx, sy, val;
1744         
1745         switch(event->type) {
1746                 case MOUSEMOVE:
1747                         
1748                         wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1749                         
1750                         rect->xmax= event->x - sx;
1751                         rect->ymax= event->y - sy;
1752                         
1753                         if((val= wm_gesture_evaluate(C, gesture))) {
1754                                 wmEvent event;
1755
1756                                 event= *(window->eventstate);
1757                                 if(gesture->event_type==LEFTMOUSE)
1758                                         event.type= EVT_TWEAK_L;
1759                                 else if(gesture->event_type==RIGHTMOUSE)
1760                                         event.type= EVT_TWEAK_R;
1761                                 else
1762                                         event.type= EVT_TWEAK_M;
1763                                 event.val= val;
1764                                 /* mouse coords! */
1765                                 wm_event_add(window, &event);
1766                                 
1767                                 WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
1768                                 window->tweak= NULL;
1769                         }
1770                         
1771                         break;
1772                         
1773                 case LEFTMOUSE:
1774                 case RIGHTMOUSE:
1775                 case MIDDLEMOUSE:
1776                         if(gesture->event_type==event->type) {
1777                                 WM_gesture_end(C, gesture);
1778                                 window->tweak= NULL;
1779
1780                                 /* when tweak fails we should give the other keymap entries a chance */
1781                                 event->val= KM_RELEASE;
1782                         }
1783                         break;
1784                 default:
1785                         WM_gesture_end(C, gesture);
1786                         window->tweak= NULL;
1787         }
1788 }
1789
1790 /* standard tweak, called after window handlers passed on event */
1791 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1792 {
1793         wmWindow *win= CTX_wm_window(C);
1794         
1795         if(win->tweak==NULL) {
1796                 if(CTX_wm_region(C)) {
1797                         if(event->val==KM_PRESS) { // pressed
1798                                 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1799                                         win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1800                         }
1801                 }
1802         }
1803         else {
1804                 if(action==WM_HANDLER_BREAK) {
1805                         WM_gesture_end(C, win->tweak);
1806                         win->tweak= NULL;
1807                 }
1808                 else
1809                         tweak_gesture_modal(C, event);
1810         }
1811 }
1812
1813 /* *********************** lasso gesture ****************** */
1814
1815 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1816 {
1817         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1818         
1819         /* add modal handler */
1820         WM_event_add_modal_handler(C, op);
1821         
1822         wm_gesture_tag_redraw(C);
1823         
1824         if( RNA_struct_find_property(op->ptr, "cursor") )
1825                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1826         
1827         return OPERATOR_RUNNING_MODAL;
1828 }
1829
1830 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1831 {
1832         op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1833         
1834         /* add modal handler */
1835         WM_event_add_modal_handler(C, op);
1836         
1837         wm_gesture_tag_redraw(C);
1838         
1839         if( RNA_struct_find_property(op->ptr, "cursor") )
1840                 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1841         
1842         return OPERATOR_RUNNING_MODAL;
1843 }
1844
1845
1846 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1847 {
1848         wmGesture *gesture= op->customdata;
1849         PointerRNA itemptr;
1850         float loc[2];
1851         int i;
1852         short *lasso= gesture->customdata;
1853         
1854         /* operator storage as path. */
1855
1856         for(i=0; i<gesture->points; i++, lasso+=2) {
1857                 loc[0]= lasso[0];
1858                 loc[1]= lasso[1];
1859                 RNA_collection_add(op->ptr, "path", &itemptr);
1860                 RNA_float_set_array(&itemptr, "loc", loc);
1861         }
1862         
1863         wm_gesture_end(C, op);
1864                 
1865         if(op->type->exec)
1866                 op->type->exec(C, op);
1867         
1868 }
1869
1870 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1871 {
1872         wmGesture *gesture= op->customdata;
1873         int sx, sy;
1874         
1875         switch(event->type) {
1876                 case MOUSEMOVE:
1877                         
1878                         wm_gesture_tag_redraw(C);
1879                         
1880                         wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1881                         if(gesture->points < WM_LASSO_MAX_POINTS) {
1882                                 short *lasso= gesture->customdata;
1883                                 lasso += 2 * gesture->points;
1884                                 lasso[0] = event->x - sx;
1885                                 lasso[1] = event->y - sy;
1886                                 gesture->points++;
1887                         }
1888                         else {
1889                                 gesture_lasso_apply(C, op, event->type);
1890                                 return OPERATOR_FINISHED;
1891                         }
1892                         break;
1893                         
1894                 case LEFTMOUSE:
1895                 case MIDDLEMOUSE:
1896                 case RIGHTMOUSE:
1897                         if(event->val==KM_RELEASE) {    /* key release */
1898                                 gesture_lasso_apply(C, op, event->type);
1899                                 return OPERATOR_FINISHED;
1900                         }
1901                         break;
1902                 case ESCKEY:
1903                         wm_gesture_end(C, op);
1904                         return OPERATOR_CANCELLED;
1905         }
1906         return OPERATOR_RUNNING_MODAL;
1907 }
1908
1909 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1910 {
1911         return WM_gesture_lasso_modal(C, op, event);
1912 }
1913
1914 #if 0
1915 /* template to copy from */
1916
1917 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1918 {
1919         RNA_BEGIN(op->ptr, itemptr, "path") {
1920                 float loc[2];
1921                 
1922                 RNA_float_get_array(&itemptr, "loc", loc);
1923                 printf("Location: %f %f\n", loc[0], loc[1]);
1924         }
1925         RNA_END;
1926         
1927         return OPERATOR_FINISHED;
1928 }
1929
1930 void WM_OT_lasso_gesture(wmOperatorType *ot)
1931 {
1932         PropertyRNA *prop;
1933         
1934         ot->name= "Lasso Gesture";
1935         ot->idname= "WM_OT_lasso_gesture";
1936         ot->description="Select objects within the lasso as you move the pointer.";
1937         
1938         ot->invoke= WM_gesture_lasso_invoke;
1939         ot->modal= WM_gesture_lasso_modal;
1940         ot->exec= gesture_lasso_exec;
1941         
1942         ot->poll= WM_operator_winactive;
1943         
1944         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1945         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1946 }
1947 #endif
1948
1949 /* *********************** radial control ****************** */
1950
1951 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1952
1953 typedef struct wmRadialControl {
1954         int mode;
1955         float initial_value, value, max_value;
1956         int initial_mouse[2];
1957         void *cursor;
1958         GLuint tex;
1959 } wmRadialControl;
1960
1961 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1962 {
1963         wmRadialControl *rc = (wmRadialControl*)customdata;
1964         ARegion *ar = CTX_wm_region(C);
1965         float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1966
1967         /* Keep cursor in the original place */
1968         x = rc->initial_mouse[0] - ar->winrct.xmin;
1969         y = rc->initial_mouse[1] - ar->winrct.ymin;
1970
1971         glPushMatrix();
1972         
1973         glTranslatef((float)x, (float)y, 0.0f);
1974
1975         if(rc->mode == WM_RADIALCONTROL_SIZE) {
1976                 r1= rc->value;
1977                 r2= rc->initial_value;
1978                 r3= r1;
1979         } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1980                 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1981                 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1982                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1983         } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1984                 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1985                 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1986                 angle = rc->value;
1987         }
1988
1989         glColor4ub(255, 255, 255, 128);
1990         glEnable( GL_LINE_SMOOTH );
1991         glEnable(GL_BLEND);
1992
1993         if(rc->mode == WM_RADIALCONTROL_ANGLE)
1994                 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1995
1996         if(rc->tex) {
1997                 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1998
1999                 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2000                         glRotatef(angle, 0, 0, 1);
2001                         fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2002                 }
2003
2004                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2005
2006                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2007                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2008
2009                 glEnable(GL_TEXTURE_2D);
2010                 glBegin(GL_QUADS);
2011                 glColor4f(0,0,0, str);
2012                 glTexCoord2f(0,0);
2013                 glVertex2f(-r3, -r3);
2014                 glTexCoord2f(1,0);
2015                 glVertex2f(r3, -r3);
2016                 glTexCoord2f(1,1);
2017                 glVertex2f(r3, r3);
2018                 glTexCoord2f(0,1);
2019                 glVertex2f(-r3, r3);
2020                 glEnd();
2021                 glDisable(GL_TEXTURE_2D);
2022         }
2023
2024         glColor4ub(255, 255, 255, 128); 
2025         glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2026         glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2027         glDisable(GL_BLEND);
2028         glDisable( GL_LINE_SMOOTH );
2029         
2030         glPopMatrix();
2031 }
2032
2033 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2034 {
2035         wmRadialControl *rc = (wmRadialControl*)op->customdata;
2036         int mode, initial_mouse[2], delta[2];
2037         float dist;
2038         double new_value = RNA_float_get(op->ptr, "new_value");
2039         int ret = OPERATOR_RUNNING_MODAL;
2040
2041         mode = RNA_int_get(op->ptr, "mode");
2042         RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2043
2044         switch(event->type) {
2045         case MOUSEMOVE:
2046                 delta[0]= initial_mouse[0] - event->x;
2047                 delta[1]= initial_mouse[1] - event->y;
2048                 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2049
2050                 if(mode == WM_RADIALCONTROL_SIZE)
2051                         new_value = dist;
2052                 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2053                         new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2054                 } else if(mode == WM_RADIALCONTROL_ANGLE)
2055                         new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2056                 
2057                 if(event->ctrl) {
2058                         if(mode == WM_RADIALCONTROL_STRENGTH)
2059                                 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
2060                         else
2061                                 new_value = ((int)new_value + 5) / 10*10;
2062                 }
2063                 
2064                 break;
2065         case ESCKEY:
2066         case RIGHTMOUSE:
2067                 ret = OPERATOR_CANCELLED;
2068                 break;
2069         case LEFTMOUSE:
2070         case PADENTER:
2071                 op->type->exec(C, op);
2072                 ret = OPERATOR_FINISHED;
2073                 break;
2074         }
2075
2076         /* Clamp */
2077         if(new_value > rc->max_value)
2078                 new_value = rc->max_value;
2079         else if(new_value < 0)
2080                 new_value = 0;
2081
2082         /* Update paint data */
2083         rc->value = new_value;
2084
2085         RNA_float_set(op->ptr, "new_value", new_value);
2086
2087         if(ret != OPERATOR_RUNNING_MODAL) {
2088                 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2089                 MEM_freeN(rc);
2090         }
2091         
2092         ED_region_tag_redraw(CTX_wm_region(C));
2093
2094         return ret;
2095 }
2096
2097 /* Expects the operator customdata to be an ImBuf (or NULL) */
2098 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2099 {
2100         wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2101         int mode = RNA_int_get(op->ptr, "mode");
2102         float initial_value = RNA_float_get(op->ptr, "initial_value");
2103         int mouse[2] = {event->x, event->y};
2104
2105         if(mode == WM_RADIALCONTROL_SIZE) {
2106                 rc->max_value = 200;
2107                 mouse[0]-= initial_value;
2108         }
2109         else if(mode == WM_RADIALCONTROL_STRENGTH) {
2110                 rc->max_value = 1;
2111                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2112         }
2113         else if(mode == WM_RADIALCONTROL_ANGLE) {
2114                 rc->max_value = 360;
2115                 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2116                 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2117                 initial_value *= 180.0f/M_PI;
2118         }
2119
2120         if(op->customdata) {
2121                 ImBuf *im = (ImBuf*)op->customdata;
2122                 /* Build GL texture */
2123                 glGenTextures(1, &rc->tex);
2124                 glBindTexture(GL_TEXTURE_2D, rc->tex);
2125                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2126                 MEM_freeN(im->rect_float);
2127                 MEM_freeN(im);
2128         }
2129
2130         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2131         RNA_float_set(op->ptr, "new_value", initial_value);
2132                 
2133         op->customdata = rc;
2134         rc->mode = mode;
2135         rc->initial_value = initial_value;
2136         rc->initial_mouse[0] = mouse[0];
2137         rc->initial_mouse[1] = mouse[1];
2138         rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2139                                               wm_radial_control_paint, op->customdata);
2140
2141         /* add modal handler */
2142         WM_event_add_modal_handler(C, op);
2143         
2144         WM_radial_control_modal(C, op, event);
2145         
2146         return OPERATOR_RUNNING_MODAL;
2147 }
2148
2149 /* Gets a descriptive string of the operation */
2150 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2151 {
2152         int mode = RNA_int_get(op->ptr, "mode");
2153         float v = RNA_float_get(op->ptr, "new_value");
2154
2155         if(mode == WM_RADIALCONTROL_SIZE)
2156                 sprintf(str, "Size: %d", (int)v);
2157         else if(mode == WM_RADIALCONTROL_STRENGTH)
2158                 sprintf(str, "Strength: %d", (int)v);
2159         else if(mode == WM_RADIALCONTROL_ANGLE)
2160                 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
2161 }
2162
2163 /** Important: this doesn't define an actual operator, it
2164     just sets up the common parts of the radial control op. **/
2165 void WM_OT_radial_control_partial(wmOperatorType *ot)
2166 {
2167         static EnumPropertyItem radial_mode_items[] = {
2168                 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2169                 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2170                 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2171                 {0, NULL, 0, NULL, NULL}};
2172
2173         /* Should be set in custom invoke() */
2174         RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2175
2176         /* Set internally, should be used in custom exec() to get final value */
2177         RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2178
2179         /* Should be set before calling operator */
2180         RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2181
2182         /* Internal */
2183         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
2184 }
2185
2186 /* ************************** timer for testing ***************** */
2187
2188 /* uses no type defines, fully local testing function anyway... ;) */
2189
2190 static int redraw_timer_exec(bContext *C, wmOperator *op)
2191 {
2192         ARegion *ar= CTX_wm_region(C);
2193         double stime= PIL_check_seconds_timer();
2194         int type = RNA_int_get(op->ptr, "type");
2195         int iter = RNA_int_get(op->ptr, "iterations");
2196         int a;
2197         float time;
2198         char *infostr= "";
2199         
2200         WM_cursor_wait(1);
2201
2202         for(a=0; a<iter; a++) {
2203                 if (type==0) {
2204                         ED_region_do_draw(C, ar);
2205                 } 
2206                 else if (type==1) {
2207                         wmWindow *win= CTX_wm_window(C);
2208                         
2209                         ED_region_tag_redraw(ar);
2210                         wm_draw_update(C);
2211                         
2212                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2213                 }
2214                 else if (type==2) {
2215                         wmWindow *win= CTX_wm_window(C);
2216                         ScrArea *sa;
2217                         
2218                         ScrArea *sa_back= CTX_wm_area(C);
2219                         ARegion *ar_back= CTX_wm_region(C);
2220
2221                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2222                                 ARegion *ar_iter;
2223                                 CTX_wm_area_set(C, sa);
2224
2225                                 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2226                                         CTX_wm_region_set(C, ar_iter);
2227                                         ED_region_do_draw(C, ar_iter);
2228                                 }
2229                         }
2230
2231                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2232
2233                         CTX_wm_area_set(C, sa_back);
2234                         CTX_wm_region_set(C, ar_back);
2235                 }
2236                 else if (type==3) {
2237                         wmWindow *win= CTX_wm_window(C);
2238                         ScrArea *sa;
2239
2240                         for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2241                                 ED_area_tag_redraw(sa);
2242                         wm_draw_update(C);
2243                         
2244                         CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
2245                 }
2246                 else if (type==4) {
2247                         Scene *scene= CTX_data_scene(C);
2248                         
2249                         if(a & 1) scene->r.cfra--;
2250                         else scene->r.cfra++;
2251                         scene_update_for_newframe(scene, scene->lay);
2252                 }
2253                 else {
2254                         ED_undo_pop(C);
2255                         ED_undo_redo(C);
2256                 }
2257         }
2258         
2259         time= ((PIL_check_seconds_timer()-stime)*1000);
2260         
2261         if(type==0) infostr= "Draw Region";
2262         if(type==1) infostr= "Draw Region and Swap";
2263         if(type==2) infostr= "Draw Window";
2264         if(type==3) infostr= "Draw Window and Swap";
2265         if(type==4) infostr= "Animation Steps";
2266         if(type==5) infostr= "Undo/Redo";
2267         
2268         WM_cursor_wait(0);
2269         
2270         BKE_reportf(op->reports, RPT_INFO, "%d x %s: %.2f ms,  average: %.4f", iter, infostr, time, time/iter);
2271         
2272         return OPERATOR_FINISHED;
2273 }
2274
2275 static void WM_OT_redraw_timer(wmOperatorType *ot)
2276 {
2277         static EnumPropertyItem prop_type_items[] = {
2278         {0, "DRAW", 0, "Draw Region", ""},
2279         {1, "DRAW_SWAP", 0, "Draw Region + Swap", ""},
2280         {2, "DRAW_WIN", 0, "Draw Window", ""},
2281         {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", ""},
2282         {4, "ANIM_STEP", 0, "Anim Step", ""},
2283         {5, "UNDO", 0, "Undo/Redo", ""},
2284         {0, NULL, 0, NULL, NULL}};
2285         
2286         ot->name= "Redraw Timer";
2287         ot->idname= "WM_OT_redraw_timer";
2288         ot->description="Simple redraw timer to test the speed of updating the interface.";
2289         
2290         ot->invoke= WM_menu_invoke;
2291         ot->exec= redraw_timer_exec;
2292         ot->poll= WM_operator_winactive;
2293         
2294         RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
2295         RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
2296
2297 }
2298
2299 /* ************************** memory statistics for testing ***************** */
2300
2301 static int memory_statistics_exec(bContext *C, wmOperator *op)
2302 {
2303         MEM_printmemlist_stats();
2304         return OPERATOR_FINISHED;
2305 }
2306
2307 static void WM_OT_memory_statistics(wmOperatorType *ot)
2308 {
2309         ot->name= "Memory Statistics";
2310         ot->idname= "WM_OT_memory_statistics";
2311         ot->description= "Print memory statistics to the console.";
2312         
2313         ot->exec= memory_statistics_exec;
2314 }
2315
2316 /* ******************************************************* */
2317  
2318 /* called on initialize WM_exit() */
2319 void wm_operatortype_free(void)
2320 {
2321         wmOperatorType *ot;
2322         
2323         for(ot= global_ops.first; ot; ot= ot->next)
2324                 if(ot->macro.first)
2325                         wm_operatortype_free_macro(ot);
2326         
2327         BLI_freelistN(&global_ops);
2328 }
2329
2330 /* called on initialize WM_init() */
2331 void wm_operatortype_init(void)
2332 {
2333         WM_operatortype_append(WM_OT_window_duplicate);
2334         WM_operatortype_append(WM_OT_read_homefile);
2335         WM_operatortype_append(WM_OT_save_homefile);
2336         WM_operatortype_append(WM_OT_window_fullscreen_toggle);
2337         WM_operatortype_append(WM_OT_exit_blender);
2338         WM_operatortype_append(WM_OT_open_recentfile);
2339         WM_operatortype_append(WM_OT_open_mainfile);
2340         WM_operatortype_append(WM_OT_link_append);
2341         WM_operatortype_append(WM_OT_recover_last_session);
2342         WM_operatortype_append(WM_OT_recover_auto_save);
2343         WM_operatortype_append(WM_OT_save_as_mainfile);
2344         WM_operatortype_append(WM_OT_save_mainfile);
2345         WM_operatortype_append(WM_OT_redraw_timer);
2346         WM_operatortype_append(WM_OT_memory_statistics);
2347         WM_operatortype_append(WM_OT_debug_menu);
2348         WM_operatortype_append(WM_OT_search_menu);
2349         WM_operatortype_append(WM_OT_call_menu);
2350
2351 #ifdef WITH_COLLADA
2352         /* XXX: move these */
2353         WM_operatortype_append(WM_OT_collada_export);
2354         WM_operatortype_append(WM_OT_collada_import);
2355 #endif
2356
2357 }
2358
2359 /* default keymap for windows and screens, only call once per WM */
2360 void wm_window_keymap(wmKeyConfig *keyconf)
2361 {
2362         wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
2363         wmKeyMapItem *km;
2364         
2365         /* note, this doesn't replace existing keymap items */
2366         WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2367         #ifdef __APPLE__
2368         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
2369         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2370         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
2371         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
2372         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
2373         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
2374         #endif
2375         WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
2376         WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0); 
2377         WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2378         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
2379         WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
2380         WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
2381         km= WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
2382         RNA_boolean_set(km->ptr, "link", FALSE);
2383
2384         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
2385         WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
2386         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
2387         WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
2388
2389         WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_ALT, 0);
2390         WM_keymap_add_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
2391
2392         /* debug/testing */
2393         WM_keymap_verify_item(keymap, "WM_OT_redraw_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2394         WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
2395         WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);
2396         
2397         /* Space switching */
2398
2399
2400         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F2KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was DXF export */
2401         RNA_string_set(km->ptr, "path", "area.type");
2402         RNA_string_set(km->ptr, "value", "LOGIC_EDITOR");
2403
2404         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F3KEY, KM_PRESS, KM_SHIFT, 0);
2405         RNA_string_set(km->ptr, "path", "area.type");
2406         RNA_string_set(km->ptr, "value", "NODE_EDITOR");
2407
2408         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F4KEY, KM_PRESS, KM_SHIFT, 0); /* new in 2.5x, was data browser */
2409         RNA_string_set(km->ptr, "path", "area.type");
2410         RNA_string_set(km->ptr, "value", "CONSOLE");
2411
2412         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F5KEY, KM_PRESS, KM_SHIFT, 0);
2413         RNA_string_set(km->ptr, "path", "area.type");
2414         RNA_string_set(km->ptr, "value", "VIEW_3D");
2415
2416         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F6KEY, KM_PRESS, KM_SHIFT, 0);
2417         RNA_string_set(km->ptr, "path", "area.type");
2418         RNA_string_set(km->ptr, "value", "GRAPH_EDITOR");
2419
2420         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F7KEY, KM_PRESS, KM_SHIFT, 0);
2421         RNA_string_set(km->ptr, "path", "area.type");
2422         RNA_string_set(km->ptr, "value", "PROPERTIES");
2423
2424         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F8KEY, KM_PRESS, KM_SHIFT, 0);
2425         RNA_string_set(km->ptr, "path", "area.type");
2426         RNA_string_set(km->ptr, "value", "SEQUENCE_EDITOR");
2427
2428         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F9KEY, KM_PRESS, KM_SHIFT, 0);
2429         RNA_string_set(km->ptr, "path", "area.type");
2430         RNA_string_set(km->ptr, "value", "OUTLINER");
2431
2432         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F9KEY, KM_PRESS, KM_SHIFT, 0);
2433         RNA_string_set(km->ptr, "path", "area.type");
2434         RNA_string_set(km->ptr, "value", "OUTLINER");
2435
2436         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F10KEY, KM_PRESS, KM_SHIFT, 0);
2437         RNA_string_set(km->ptr, "path", "area.type");
2438         RNA_string_set(km->ptr, "value", "IMAGE_EDITOR");
2439
2440         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F11KEY, KM_PRESS, KM_SHIFT, 0);
2441         RNA_string_set(km->ptr, "path", "area.type");
2442         RNA_string_set(km->ptr, "value", "TEXT_EDITOR");
2443
2444         km = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", F12KEY, KM_PRESS, KM_SHIFT, 0);
2445         RNA_string_set(km->ptr, "path", "area.type");
2446         RNA_string_set(km->ptr, "value", "DOPESHEET_EDITOR");
2447 }
2448
2449 /* Generic itemf's for operators that take library args */
2450 static EnumPropertyItem *rna_id_itemf(bContext *C, PointerRNA *ptr, int *free, ID *id)
2451 {
2452         EnumPropertyItem *item= NULL, item_tmp;
2453         int totitem= 0;
2454         int i= 0;
2455
2456         memset(&item_tmp, 0, sizeof(item_tmp));
2457
2458         for( ; id; id= id->next) {
2459                 item_tmp.identifier= item_tmp.name= id->name+2;
2460                 item_tmp.value= i++;
2461                 RNA_enum_item_add(&item, &totitem, &item_tmp);
2462         }
2463
2464         RNA_enum_item_end(&item, &totitem);
2465         *free= 1;
2466
2467         return item;
2468 }
2469
2470 /* can add more */
2471 EnumPropertyItem *RNA_group_itemf(bContext *C, PointerRNA *ptr, int *free)
2472 {
2473         return rna_id_itemf(C, ptr, free, (ID *)CTX_data_main(C)->group.first);
2474 }
2475 EnumPropertyItem *RNA_scene_itemf(bContext *C, PointerRNA *ptr, int *free)
2476 {
2477         return rna_id_itemf(C, ptr, free, (ID *)CTX_data_main(C)->scene.first);
2478 }