4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
37 #include "BLI_winstuff.h"
43 #include "MEM_guardedalloc.h"
46 #include "DNA_object_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_userdef_types.h"
50 #include "DNA_windowmanager_types.h"
56 #include "BLI_blenlib.h"
57 #include "BLI_dynstr.h" /*for WM_operator_pystring */
59 #include "BLI_utildefines.h"
61 #include "BLO_readfile.h"
63 #include "BKE_blender.h"
64 #include "BKE_context.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_idprop.h"
67 #include "BKE_library.h"
68 #include "BKE_global.h"
70 #include "BKE_report.h"
71 #include "BKE_scene.h"
72 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
74 #include "BKE_idcode.h"
77 #include "BIF_glutil.h" /* for paint cursor */
79 #include "IMB_imbuf_types.h"
81 #include "ED_screen.h"
84 #include "RNA_access.h"
85 #include "RNA_define.h"
86 #include "RNA_enum_types.h"
88 #include "UI_interface.h"
89 #include "UI_resources.h"
96 #include "wm_event_system.h"
97 #include "wm_event_types.h"
98 #include "wm_subwindow.h"
99 #include "wm_window.h"
101 static ListBase global_ops= {NULL, NULL};
103 /* ************ operator API, exported ********** */
106 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
110 char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
111 WM_operator_bl_idname(idname_bl, idname);
114 ot= (wmOperatorType *)BLI_findstring_ptr(&global_ops, idname_bl, offsetof(wmOperatorType, idname));
121 printf("search for unknown operator %s, %s\n", idname_bl, idname);
126 wmOperatorType *WM_operatortype_first(void)
128 return global_ops.first;
131 /* all ops in 1 list (for time being... needs evaluation later) */
132 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
136 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
137 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
141 static char dummy_name[] = "Dummy Name";
142 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
143 ot->name= dummy_name;
146 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.
147 RNA_def_struct_identifier(ot->srna, ot->idname);
148 BLI_addtail(&global_ops, ot);
151 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
155 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
156 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
157 opfunc(ot, userdata);
158 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
159 RNA_def_struct_identifier(ot->srna, ot->idname);
160 BLI_addtail(&global_ops, ot);
163 /* ********************* macro operator ******************** */
169 static void wm_macro_start(wmOperator *op)
171 if (op->customdata == NULL) {
172 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
176 static int wm_macro_end(wmOperator *op, int retval)
178 if (retval & OPERATOR_CANCELLED) {
179 MacroData *md = op->customdata;
181 if (md->retval & OPERATOR_FINISHED) {
182 retval |= OPERATOR_FINISHED;
183 retval &= ~OPERATOR_CANCELLED;
187 /* if modal is ending, free custom data */
188 if (retval & (OPERATOR_FINISHED|OPERATOR_CANCELLED)) {
189 if (op->customdata) {
190 MEM_freeN(op->customdata);
191 op->customdata = NULL;
198 /* macro exec only runs exec calls */
199 static int wm_macro_exec(bContext *C, wmOperator *op)
202 int retval= OPERATOR_FINISHED;
206 for(opm= op->macro.first; opm; opm= opm->next) {
208 if(opm->type->exec) {
209 retval= opm->type->exec(C, opm);
211 if (retval & OPERATOR_FINISHED) {
212 MacroData *md = op->customdata;
213 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
215 break; /* operator didn't finish, end macro */
220 return wm_macro_end(op, retval);
223 static int wm_macro_invoke_internal(bContext *C, wmOperator *op, wmEvent *event, wmOperator *opm)
225 int retval= OPERATOR_FINISHED;
227 /* start from operator received as argument */
228 for( ; opm; opm= opm->next) {
229 if(opm->type->invoke)
230 retval= opm->type->invoke(C, opm, event);
231 else if(opm->type->exec)
232 retval= opm->type->exec(C, opm);
234 BLI_movelisttolist(&op->reports->list, &opm->reports->list);
236 if (retval & OPERATOR_FINISHED) {
237 MacroData *md = op->customdata;
238 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
240 break; /* operator didn't finish, end macro */
244 return wm_macro_end(op, retval);
247 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
250 return wm_macro_invoke_internal(C, op, event, op->macro.first);
253 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
255 wmOperator *opm = op->opm;
256 int retval= OPERATOR_FINISHED;
259 printf("macro error, calling NULL modal()\n");
261 retval = opm->type->modal(C, opm, event);
263 /* if this one is done but it's not the last operator in the macro */
264 if ((retval & OPERATOR_FINISHED) && opm->next) {
265 MacroData *md = op->customdata;
267 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
269 retval = wm_macro_invoke_internal(C, op, event, opm->next);
271 /* if new operator is modal and also added its own handler */
272 if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
273 wmWindow *win = CTX_wm_window(C);
274 wmEventHandler *handler = NULL;
276 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
277 /* first handler in list is the new one */
278 if (handler->op == op)
283 BLI_remlink(&win->modalhandlers, handler);
284 wm_event_free_handler(handler);
287 /* if operator is blocking, grab cursor
288 * This may end up grabbing twice, but we don't care.
290 if(op->opm->type->flag & OPTYPE_BLOCKING) {
291 int bounds[4] = {-1,-1,-1,-1};
292 int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
295 ARegion *ar= CTX_wm_region(C);
297 bounds[0]= ar->winrct.xmin;
298 bounds[1]= ar->winrct.ymax;
299 bounds[2]= ar->winrct.xmax;
300 bounds[3]= ar->winrct.ymin;
304 WM_cursor_grab(CTX_wm_window(C), wrap, FALSE, bounds);
310 return wm_macro_end(op, retval);
313 static int wm_macro_cancel(bContext *C, wmOperator *op)
315 /* call cancel on the current modal operator, if any */
316 if (op->opm && op->opm->type->cancel) {
317 op->opm->type->cancel(C, op->opm);
320 return wm_macro_end(op, OPERATOR_CANCELLED);
323 /* Names have to be static for now */
324 wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, int flag)
328 if(WM_operatortype_find(idname, TRUE)) {
329 printf("Macro error: operator %s exists\n", idname);
333 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
334 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
338 ot->flag= OPTYPE_MACRO|flag;
340 ot->exec= wm_macro_exec;
341 ot->invoke= wm_macro_invoke;
342 ot->modal= wm_macro_modal;
343 ot->cancel= wm_macro_cancel;
347 ot->description= "(undocumented operator)";
349 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description); // XXX All ops should have a description but for now allow them not to.
350 RNA_def_struct_identifier(ot->srna, ot->idname);
352 BLI_addtail(&global_ops, ot);
357 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
361 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
362 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
364 ot->flag= OPTYPE_MACRO;
365 ot->exec= wm_macro_exec;
366 ot->invoke= wm_macro_invoke;
367 ot->modal= wm_macro_modal;
368 ot->cancel= wm_macro_cancel;
372 ot->description= "(undocumented operator)";
374 opfunc(ot, userdata);
376 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
377 RNA_def_struct_identifier(ot->srna, ot->idname);
379 BLI_addtail(&global_ops, ot);
382 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
384 wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
386 BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
388 /* do this on first use, since operatordefinitions might have been not done yet */
389 WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
390 WM_operator_properties_sanitize(otmacro->ptr, 1);
392 BLI_addtail(&ot->macro, otmacro);
395 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
396 RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
397 otsub->name, otsub->description);
403 static void wm_operatortype_free_macro(wmOperatorType *ot)
405 wmOperatorTypeMacro *otmacro;
407 for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
409 WM_operator_properties_free(otmacro->ptr);
410 MEM_freeN(otmacro->ptr);
413 BLI_freelistN(&ot->macro);
417 int WM_operatortype_remove(const char *idname)
419 wmOperatorType *ot = WM_operatortype_find(idname, 0);
424 BLI_remlink(&global_ops, ot);
425 RNA_struct_free(&BLENDER_RNA, ot->srna);
428 wm_operatortype_free_macro(ot);
435 /* SOME_OT_op -> some.op */
436 void WM_operator_py_idname(char *to, const char *from)
438 char *sep= strstr(from, "_OT_");
440 int i, ofs= (sep-from);
443 to[i]= tolower(from[i]);
446 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
449 /* should not happen but support just incase */
450 BLI_strncpy(to, from, OP_MAX_TYPENAME);
454 /* some.op -> SOME_OT_op */
455 void WM_operator_bl_idname(char *to, const char *from)
458 char *sep= strchr(from, '.');
461 int i, ofs= (sep-from);
464 to[i]= toupper(from[i]);
466 BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
467 BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
470 /* should not happen but support just incase */
471 BLI_strncpy(to, from, OP_MAX_TYPENAME);
478 /* print a string representation of the operator, with the args that it runs
479 * so python can run it again,
481 * When calling from an existing wmOperator do.
482 * WM_operator_pystring(op->type, op->ptr);
484 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
486 const char *arg_name= NULL;
487 char idname_py[OP_MAX_TYPENAME];
489 PropertyRNA *prop, *iterprop;
491 /* for building the string */
492 DynStr *dynstr= BLI_dynstr_new();
494 int first_iter=1, ok= 1;
497 /* only to get the orginal props for comparisons */
498 PointerRNA opptr_default;
499 PropertyRNA *prop_default;
501 if(all_args==0 || opptr==NULL) {
502 WM_operator_properties_create_ptr(&opptr_default, ot);
505 opptr = &opptr_default;
509 WM_operator_py_idname(idname_py, ot->idname);
510 BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
512 iterprop= RNA_struct_iterator_property(opptr->type);
514 RNA_PROP_BEGIN(opptr, propptr, iterprop) {
516 arg_name= RNA_property_identifier(prop);
518 if (strcmp(arg_name, "rna_type")==0) continue;
520 buf= RNA_property_as_string(C, opptr, prop);
525 /* not verbose, so only add in attributes that use non-default values
526 * slow but good for tooltips */
527 prop_default= RNA_struct_find_property(&opptr_default, arg_name);
530 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
532 if(strcmp(buf, buf_default)==0)
533 ok= 0; /* values match, dont bother printing */
535 MEM_freeN(buf_default);
540 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
549 if(all_args==0 || opptr==&opptr_default )
550 WM_operator_properties_free(&opptr_default);
552 BLI_dynstr_append(dynstr, ")");
554 cstring = BLI_dynstr_get_cstring(dynstr);
555 BLI_dynstr_free(dynstr);
559 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
561 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
564 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
566 wmOperatorType *ot= WM_operatortype_find(opstring, 0);
569 WM_operator_properties_create_ptr(ptr, ot);
571 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
574 /* similar to the function above except its uses ID properties
575 * used for keymaps and macros */
576 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
578 if(*properties==NULL) {
579 IDPropertyTemplate val = {0};
580 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
584 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
585 WM_operator_properties_create(*ptr, opstring);
588 (*ptr)->data= *properties;
592 void WM_operator_properties_sanitize(PointerRNA *ptr, const short no_context)
594 RNA_STRUCT_BEGIN(ptr, prop) {
595 switch(RNA_property_type(prop)) {
598 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
600 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
604 StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
606 /* recurse into operator properties */
607 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
608 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
609 WM_operator_properties_sanitize(&opptr, no_context);
620 void WM_operator_properties_free(PointerRNA *ptr)
622 IDProperty *properties= ptr->data;
625 IDP_FreeProperty(properties);
626 MEM_freeN(properties);
627 ptr->data= NULL; /* just incase */
631 /* ************ default op callbacks, exported *********** */
633 /* invoke callback, uses enum property named "type" */
634 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
636 PropertyRNA *prop= op->type->prop;
641 printf("WM_menu_invoke: %s has no enum property set\n", op->type->idname);
643 else if (RNA_property_type(prop) != PROP_ENUM) {
644 printf("WM_menu_invoke: %s \"%s\" is not an enum property\n", op->type->idname, RNA_property_identifier(prop));
646 else if (RNA_property_is_set(op->ptr, RNA_property_identifier(prop))) {
647 return op->type->exec(C, op);
650 pup= uiPupMenuBegin(C, op->type->name, ICON_NULL);
651 layout= uiPupMenuLayout(pup);
652 uiItemsFullEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
653 uiPupMenuEnd(C, pup);
656 return OPERATOR_CANCELLED;
660 /* generic enum search invoke popup */
661 static void operator_enum_search_cb(const struct bContext *C, void *arg_ot, const char *str, uiSearchItems *items)
663 wmOperatorType *ot = (wmOperatorType *)arg_ot;
664 PropertyRNA *prop= ot->prop;
667 printf("WM_enum_search_invoke: %s has no enum property set\n", ot->idname);
669 else if (RNA_property_type(prop) != PROP_ENUM) {
670 printf("WM_enum_search_invoke: %s \"%s\" is not an enum property\n", ot->idname, RNA_property_identifier(prop));
675 EnumPropertyItem *item, *item_array;
678 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
679 RNA_property_enum_items((bContext *)C, &ptr, prop, &item_array, NULL, &do_free);
681 for(item= item_array; item->identifier; item++) {
682 /* note: need to give the intex rather then the dientifier because the enum can be freed */
683 if(BLI_strcasestr(item->name, str))
684 if(0==uiSearchItemAdd(items, item->name, SET_INT_IN_POINTER(item->value), 0))
689 MEM_freeN(item_array);
693 static void operator_enum_call_cb(struct bContext *C, void *arg1, void *arg2)
695 wmOperatorType *ot= arg1;
698 PointerRNA props_ptr;
699 WM_operator_properties_create_ptr(&props_ptr, ot);
700 RNA_property_enum_set(&props_ptr, ot->prop, GET_INT_FROM_POINTER(arg2));
701 WM_operator_name_call(C, ot->idname, WM_OP_EXEC_DEFAULT, &props_ptr);
702 WM_operator_properties_free(&props_ptr);
706 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
708 static char search[256]= "";
710 wmWindow *win= CTX_wm_window(C);
713 wmOperator *op= (wmOperator *)arg_op;
715 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
716 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
718 //uiDefBut(block, LABEL, 0, op->type->name, 10, 10, 180, 19, NULL, 0.0, 0.0, 0, 0, ""); // ok, this isnt so easy...
719 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
720 uiButSetSearchFunc(but, operator_enum_search_cb, op->type, operator_enum_call_cb, NULL);
722 /* fake button, it holds space for search items */
723 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
725 uiPopupBoundsBlock(block, 6, 0, -20); /* move it downwards, mouse over button */
726 uiEndBlock(C, block);
728 event= *(win->eventstate); /* XXX huh huh? make api call */
729 event.type= EVT_BUT_OPEN;
731 event.customdata= but;
732 event.customdatafree= FALSE;
733 wm_event_add(win, &event);
739 int WM_enum_search_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
741 uiPupBlock(C, wm_enum_search_menu, op);
742 return OPERATOR_CANCELLED;
745 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
746 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
750 IDProperty *properties= op->ptr->data;
752 if(properties && properties->len)
753 properties= IDP_CopyProperty(op->ptr->data);
757 pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
758 layout= uiPupMenuLayout(pup);
759 uiItemFullO(layout, op->type->idname, message, ICON_NULL, properties, WM_OP_EXEC_REGION_WIN, 0);
760 uiPupMenuEnd(C, pup);
762 return OPERATOR_CANCELLED;
766 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
768 return WM_operator_confirm_message(C, op, NULL);
771 /* op->invoke, opens fileselect if path property not set, otherwise executes */
772 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
774 if (RNA_property_is_set(op->ptr, "filepath")) {
775 return WM_operator_call(C, op);
778 WM_event_add_fileselect(C, op);
779 return OPERATOR_RUNNING_MODAL;
783 /* default properties for fileselect */
784 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag)
789 if(flag & WM_FILESEL_FILEPATH)
790 RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "File Path", "Path to file");
792 if(flag & WM_FILESEL_DIRECTORY)
793 RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file");
795 if(flag & WM_FILESEL_FILENAME)
796 RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file");
798 if (action == FILE_SAVE) {
799 prop= RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
800 RNA_def_property_flag(prop, PROP_HIDDEN);
803 prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
804 RNA_def_property_flag(prop, PROP_HIDDEN);
805 prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
806 RNA_def_property_flag(prop, PROP_HIDDEN);
807 prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
808 RNA_def_property_flag(prop, PROP_HIDDEN);
809 prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
810 RNA_def_property_flag(prop, PROP_HIDDEN);
811 prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
812 RNA_def_property_flag(prop, PROP_HIDDEN);
813 prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
814 RNA_def_property_flag(prop, PROP_HIDDEN);
815 prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
816 RNA_def_property_flag(prop, PROP_HIDDEN);
817 prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
818 RNA_def_property_flag(prop, PROP_HIDDEN);
819 prop= RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
820 RNA_def_property_flag(prop, PROP_HIDDEN);
821 prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
822 RNA_def_property_flag(prop, PROP_HIDDEN);
824 prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL,
825 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
826 FILE_LOADLIB, FILE_SPECIAL);
827 RNA_def_property_flag(prop, PROP_HIDDEN);
829 if(flag & WM_FILESEL_RELPATH)
830 RNA_def_boolean(ot->srna, "relative_path", (U.flag & USER_RELPATHS) ? 1:0, "Relative Path", "Select the file relative to the blend file");
833 void WM_operator_properties_select_all(wmOperatorType *ot) {
834 static EnumPropertyItem select_all_actions[] = {
835 {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
836 {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
837 {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
838 {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
839 {0, NULL, 0, NULL, NULL}
842 RNA_def_enum(ot->srna, "action", select_all_actions, SEL_TOGGLE, "Action", "Selection action to execute");
845 void WM_operator_properties_gesture_border(wmOperatorType *ot, int extend)
847 RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
848 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
849 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
850 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
851 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
854 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
857 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
859 RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
860 RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
861 RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
862 RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
865 RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX, "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
870 int WM_operator_winactive(bContext *C)
872 if(CTX_wm_window(C)==NULL) return 0;
876 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
878 wmOperator *op= arg_op;
881 uiStyle *style= U.uistyles.first;
885 block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
886 uiBlockClearFlag(block, UI_BLOCK_LOOP);
887 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
889 /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
890 * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
891 assert(op->type->flag & OPTYPE_REGISTER);
893 uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, arg_op);
894 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, 20, style);
896 if(ED_undo_valid(C, op->type->name)==0)
897 uiLayoutSetEnabled(layout, 0);
899 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
901 uiPopupBoundsBlock(block, 4.0f, 0, 0);
902 uiEndBlock(C, block);
907 /* Only invoked by OK button in popups created with wm_block_create_dialog() */
908 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
910 wmOperator *op= arg1;
911 uiBlock *block= arg2;
913 WM_operator_call(C, op);
915 uiPupBlockClose(C, block);
918 static void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
920 wmOperator *op= op_ptr;
921 if(op->type->check) {
922 if(op->type->check(C, op)) {
928 /* Dialogs are popups that require user verification (click OK) before exec */
929 static uiBlock *wm_block_create_dialog(bContext *C, ARegion *ar, void *userData)
931 struct { wmOperator *op; int width; int height; } * data = userData;
932 wmOperator *op= data->op;
936 uiStyle *style= U.uistyles.first;
938 block = uiBeginBlock(C, ar, "operator dialog", UI_EMBOSS);
939 uiBlockClearFlag(block, UI_BLOCK_LOOP);
940 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
942 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
944 uiBlockSetFunc(block, dialog_check_cb, op, NULL);
946 uiLayoutOperatorButs(C, layout, op, NULL, 'H', UI_LAYOUT_OP_SHOW_TITLE);
948 /* clear so the OK button is left alone */
949 uiBlockSetFunc(block, NULL, NULL, NULL);
951 /* Create OK button, the callback of which will execute op */
952 btn= uiDefBut(block, BUT, 0, "OK", 0, 0, 0, 20, NULL, 0, 0, 0, 0, "");
953 uiButSetFunc(btn, dialog_exec_cb, op, block);
955 /* center around the mouse */
956 uiPopupBoundsBlock(block, 4.0f, data->width/-2, data->height/2);
957 uiEndBlock(C, block);
962 static uiBlock *wm_operator_create_ui(bContext *C, ARegion *ar, void *userData)
964 struct { wmOperator *op; int width; int height; } * data = userData;
965 wmOperator *op= data->op;
968 uiStyle *style= U.uistyles.first;
970 block= uiBeginBlock(C, ar, "opui_popup", UI_EMBOSS);
971 uiBlockClearFlag(block, UI_BLOCK_LOOP);
972 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
974 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
976 /* since ui is defined the auto-layout args are not used */
977 uiLayoutOperatorButs(C, layout, op, NULL, 'V', 0);
979 uiPopupBoundsBlock(block, 4.0f, 0, 0);
980 uiEndBlock(C, block);
985 /* operator menu needs undo, for redo callback */
986 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
989 if((op->type->flag & OPTYPE_REGISTER)==0) {
990 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
991 return OPERATOR_CANCELLED;
994 ED_undo_push_op(C, op);
995 wm_operator_register(C, op);
997 uiPupBlock(C, wm_block_create_redo, op);
999 return OPERATOR_RUNNING_MODAL;
1002 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1004 struct { wmOperator *op; int width; int height; } data;
1008 data.height= height;
1010 /* op is not executed until popup OK but is clicked */
1011 uiPupBlock(C, wm_block_create_dialog, &data);
1013 return OPERATOR_RUNNING_MODAL;
1016 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1018 struct { wmOperator *op; int width; int height; } data;
1021 data.height = height;
1022 uiPupBlock(C, wm_operator_create_ui, &data);
1023 return OPERATOR_RUNNING_MODAL;
1026 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1028 if((op->type->flag & OPTYPE_REGISTER)==0) {
1029 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1030 return OPERATOR_CANCELLED;
1033 uiPupBlock(C, wm_block_create_redo, op);
1035 return OPERATOR_CANCELLED;
1038 /* ***************** Debug menu ************************* */
1040 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1042 G.rt= RNA_int_get(op->ptr, "debug_value");
1043 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1044 WM_event_add_notifier(C, NC_WINDOW, NULL);
1046 return OPERATOR_FINISHED;
1049 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1051 RNA_int_set(op->ptr, "debug_value", G.rt);
1052 return WM_operator_props_dialog_popup(C, op, 180, 20);
1055 static void WM_OT_debug_menu(wmOperatorType *ot)
1057 ot->name= "Debug Menu";
1058 ot->idname= "WM_OT_debug_menu";
1059 ot->description= "Open a popup to set the debug level";
1061 ot->invoke= wm_debug_menu_invoke;
1062 ot->exec= wm_debug_menu_exec;
1063 ot->poll= WM_operator_winactive;
1065 RNA_def_int(ot->srna, "debug_value", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
1069 /* ***************** Splash Screen ************************* */
1071 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1073 uiPupBlockClose(C, arg_block);
1076 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1078 /* XXX: hack to refresh splash screen with updated prest menu name,
1079 * since popup blocks don't get regenerated like panels do */
1080 static void wm_block_splash_refreshmenu (bContext *UNUSED(C), void *UNUSED(arg_block), void *UNUSED(arg))
1082 /* ugh, causes crashes in other buttons, disabling for now until
1084 uiPupBlockClose(C, arg_block);
1085 uiPupBlock(C, wm_block_create_splash, NULL);
1089 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1093 uiLayout *layout, *split, *col;
1094 uiStyle *style= U.uistyles.first;
1095 struct RecentFile *recent;
1097 MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1100 #ifdef NAN_BUILDINFO
1101 int ver_width, rev_width;
1102 char *version_str = NULL;
1103 char *revision_str = NULL;
1104 char version_buf[128];
1105 char revision_buf[128];
1106 extern char build_rev[];
1108 version_str = &version_buf[0];
1109 revision_str = &revision_buf[0];
1111 sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1112 sprintf(revision_str, "r%s", build_rev);
1114 BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1115 ver_width = BLF_width(style->widgetlabel.uifont_id, version_str)+5;
1116 rev_width = BLF_width(style->widgetlabel.uifont_id, revision_str)+5;
1117 #endif //NAN_BUILDINFO
1119 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1120 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1122 but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1123 uiButSetFunc(but, wm_block_splash_close, block, NULL);
1124 uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1126 #ifdef NAN_BUILDINFO
1127 uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1128 uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1129 #endif //NAN_BUILDINFO
1131 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1133 uiBlockSetEmboss(block, UI_EMBOSS);
1134 /* show the splash menu (containing interaction presets), using python */
1137 menu.layout= layout;
1141 // wmWindowManager *wm= CTX_wm_manager(C);
1142 // uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, ICON_NULL);
1145 uiBlockSetEmboss(block, UI_EMBOSSP);
1146 uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1148 split = uiLayoutSplit(layout, 0, 0);
1149 col = uiLayoutColumn(split, 0);
1150 uiItemL(col, "Links", ICON_NULL);
1151 uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1152 uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-256-beta/");
1153 uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:Manual");
1154 uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1155 uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/"); //
1156 BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1157 uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", url);
1158 uiItemL(col, "", ICON_NULL);
1160 col = uiLayoutColumn(split, 0);
1161 uiItemL(col, "Recent", ICON_NULL);
1162 for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1163 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1166 uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1167 uiItemL(col, "", ICON_NULL);
1169 uiCenteredBoundsBlock(block, 0.0f);
1170 uiEndBlock(C, block);
1175 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
1177 uiPupBlock(C, wm_block_create_splash, NULL);
1179 return OPERATOR_FINISHED;
1182 static void WM_OT_splash(wmOperatorType *ot)
1184 ot->name= "Splash Screen";
1185 ot->idname= "WM_OT_splash";
1186 ot->description= "Opens a blocking popup region with release info";
1188 ot->invoke= wm_splash_invoke;
1189 ot->poll= WM_operator_winactive;
1193 /* ***************** Search menu ************************* */
1194 static void operator_call_cb(struct bContext *C, void *UNUSED(arg1), void *arg2)
1196 wmOperatorType *ot= arg2;
1199 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1202 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
1204 wmOperatorType *ot = WM_operatortype_first();
1206 for(; ot; ot= ot->next) {
1208 if(BLI_strcasestr(ot->name, str)) {
1209 if(WM_operator_poll((bContext*)C, ot)) {
1211 int len= strlen(ot->name);
1213 /* display name for menu, can hold hotkey */
1214 BLI_strncpy(name, ot->name, 256);
1216 /* check for hotkey */
1218 if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1222 if(0==uiSearchItemAdd(items, name, ot, 0))
1229 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1231 static char search[256]= "";
1233 wmWindow *win= CTX_wm_window(C);
1237 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1238 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1240 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1241 uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1243 /* fake button, it holds space for search items */
1244 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1246 uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
1247 uiEndBlock(C, block);
1249 event= *(win->eventstate); /* XXX huh huh? make api call */
1250 event.type= EVT_BUT_OPEN;
1251 event.val= KM_PRESS;
1252 event.customdata= but;
1253 event.customdatafree= FALSE;
1254 wm_event_add(win, &event);
1259 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1261 return OPERATOR_FINISHED;
1264 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1266 uiPupBlock(C, wm_block_search_menu, op);
1268 return OPERATOR_CANCELLED;
1272 static int wm_search_menu_poll(bContext *C)
1274 if(CTX_wm_window(C)==NULL) return 0;
1275 if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0; // XXX - so we can use the shortcut in the console
1276 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
1277 if(CTX_data_edit_object(C) && CTX_data_edit_object(C)->type==OB_FONT) return 0; // XXX - so we can use the spacebar for entering text
1281 static void WM_OT_search_menu(wmOperatorType *ot)
1283 ot->name= "Search Menu";
1284 ot->idname= "WM_OT_search_menu";
1286 ot->invoke= wm_search_menu_invoke;
1287 ot->exec= wm_search_menu_exec;
1288 ot->poll= wm_search_menu_poll;
1291 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1293 char idname[BKE_ST_MAXNAME];
1294 RNA_string_get(op->ptr, "name", idname);
1296 uiPupMenuInvoke(C, idname);
1298 return OPERATOR_CANCELLED;
1301 static void WM_OT_call_menu(wmOperatorType *ot)
1303 ot->name= "Call Menu";
1304 ot->idname= "WM_OT_call_menu";
1306 ot->exec= wm_call_menu_exec;
1308 RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1311 /* ************ window / screen operator definitions ************** */
1313 /* this poll functions is needed in place of WM_operator_winactive
1314 * while it crashes on full screen */
1315 static int wm_operator_winactive_normal(bContext *C)
1317 wmWindow *win= CTX_wm_window(C);
1319 if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1325 static void WM_OT_window_duplicate(wmOperatorType *ot)
1327 ot->name= "Duplicate Window";
1328 ot->idname= "WM_OT_window_duplicate";
1329 ot->description="Duplicate the current Blender window";
1331 ot->exec= wm_window_duplicate_exec;
1332 ot->poll= wm_operator_winactive_normal;
1335 static void WM_OT_save_homefile(wmOperatorType *ot)
1337 ot->name= "Save User Settings";
1338 ot->idname= "WM_OT_save_homefile";
1339 ot->description="Make the current file the default .blend file";
1341 ot->invoke= WM_operator_confirm;
1342 ot->exec= WM_write_homefile;
1343 ot->poll= WM_operator_winactive;
1346 static void WM_OT_read_homefile(wmOperatorType *ot)
1348 ot->name= "Reload Start-Up File";
1349 ot->idname= "WM_OT_read_homefile";
1350 ot->description="Open the default file (doesn't save the current file)";
1352 ot->invoke= WM_operator_confirm;
1353 ot->exec= WM_read_homefile_exec;
1354 /* ommit poll to run in background mode */
1357 static void WM_OT_read_factory_settings(wmOperatorType *ot)
1359 ot->name= "Load Factory Settings";
1360 ot->idname= "WM_OT_read_factory_settings";
1361 ot->description="Load default file and user preferences";
1363 ot->invoke= WM_operator_confirm;
1364 ot->exec= WM_read_homefile_exec;
1365 /* ommit poll to run in background mode */
1368 /* *************** open file **************** */
1370 static void open_set_load_ui(wmOperator *op)
1372 if(!RNA_property_is_set(op->ptr, "load_ui"))
1373 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1376 static void open_set_use_scripts(wmOperator *op)
1378 if(!RNA_property_is_set(op->ptr, "use_scripts"))
1379 RNA_boolean_set(op->ptr, "use_scripts", !(U.flag & USER_SCRIPT_AUTOEXEC_DISABLE));
1382 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1384 const char *openname= G.main->name;
1386 /* if possible, get the name of the most recently used .blend file */
1387 if (G.recent_files.first) {
1388 struct RecentFile *recent = G.recent_files.first;
1389 openname = recent->filepath;
1392 RNA_string_set(op->ptr, "filepath", openname);
1393 open_set_load_ui(op);
1394 open_set_use_scripts(op);
1396 WM_event_add_fileselect(C, op);
1398 return OPERATOR_RUNNING_MODAL;
1401 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1403 char path[FILE_MAX];
1405 RNA_string_get(op->ptr, "filepath", path);
1406 open_set_load_ui(op);
1407 open_set_use_scripts(op);
1409 if(RNA_boolean_get(op->ptr, "load_ui"))
1410 G.fileflags &= ~G_FILE_NO_UI;
1412 G.fileflags |= G_FILE_NO_UI;
1414 if(RNA_boolean_get(op->ptr, "use_scripts"))
1415 G.f |= G_SCRIPT_AUTOEXEC;
1417 G.f &= ~G_SCRIPT_AUTOEXEC;
1419 // XXX wm in context is not set correctly after WM_read_file -> crash
1420 // do it before for now, but is this correct with multiple windows?
1421 WM_event_add_notifier(C, NC_WINDOW, NULL);
1423 WM_read_file(C, path, op->reports);
1425 return OPERATOR_FINISHED;
1428 static void WM_OT_open_mainfile(wmOperatorType *ot)
1430 ot->name= "Open Blender File";
1431 ot->idname= "WM_OT_open_mainfile";
1432 ot->description="Open a Blender file";
1434 ot->invoke= wm_open_mainfile_invoke;
1435 ot->exec= wm_open_mainfile_exec;
1436 ot->poll= WM_operator_winactive;
1438 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1440 RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1441 RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1444 /* **************** link/append *************** */
1446 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1448 if(!RNA_property_is_set(op->ptr, "relative_path"))
1449 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1451 if(RNA_property_is_set(op->ptr, "filepath")) {
1452 return WM_operator_call(C, op);
1455 /* XXX TODO solve where to get last linked library from */
1456 RNA_string_set(op->ptr, "filepath", G.lib);
1457 WM_event_add_fileselect(C, op);
1458 return OPERATOR_RUNNING_MODAL;
1462 static short wm_link_append_flag(wmOperator *op)
1466 if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1467 if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1468 if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1469 if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1470 if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1475 static void wm_link_make_library_local(Main *main, const char *libname)
1479 /* and now find the latest append lib file */
1480 for(lib= main->library.first; lib; lib=lib->id.next)
1481 if(BLI_streq(libname, lib->filepath))
1490 static int wm_link_append_exec(bContext *C, wmOperator *op)
1492 Main *bmain= CTX_data_main(C);
1493 Scene *scene= CTX_data_scene(C);
1497 char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1498 int idcode, totfiles=0;
1502 RNA_string_get(op->ptr, "filename", name);
1503 RNA_string_get(op->ptr, "directory", dir);
1505 /* test if we have a valid data */
1506 if(BLO_is_a_library(dir, libname, group) == 0) {
1507 BKE_report(op->reports, RPT_ERROR, "Not a library");
1508 return OPERATOR_CANCELLED;
1510 else if(group[0] == 0) {
1511 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1512 return OPERATOR_CANCELLED;
1514 else if(BLI_streq(bmain->name, libname)) {
1515 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1516 return OPERATOR_CANCELLED;
1519 /* check if something is indicated for append/link */
1520 prop = RNA_struct_find_property(op->ptr, "files");
1522 totfiles= RNA_property_collection_length(op->ptr, prop);
1524 if(name[0] == '\0') {
1525 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1526 return OPERATOR_CANCELLED;
1530 else if(name[0] == '\0') {
1531 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1532 return OPERATOR_CANCELLED;
1535 /* now we have or selected, or an indicated file */
1536 if(RNA_boolean_get(op->ptr, "autoselect"))
1537 scene_deselect_all(scene);
1539 bh = BLO_blendhandle_from_file(libname);
1540 idcode = BKE_idcode_from_name(group);
1542 flag = wm_link_append_flag(op);
1544 /* sanity checks for flag */
1545 if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1546 /* TODO, user never gets this message */
1547 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1548 flag &= ~FILE_GROUP_INSTANCE;
1552 /* tag everything, all untagged data can be made local
1553 * its also generally useful to know what is new
1555 * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1556 flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1558 /* here appending/linking starts */
1559 mainl = BLO_library_append_begin(C, &bh, libname);
1561 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1564 RNA_BEGIN(op->ptr, itemptr, "files") {
1565 RNA_string_get(&itemptr, "name", name);
1566 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1570 BLO_library_append_end(C, mainl, &bh, idcode, flag);
1572 /* mark all library linked objects to be updated */
1573 recalc_all_library_objects(bmain);
1575 /* append, rather than linking */
1576 if((flag & FILE_LINK)==0)
1577 wm_link_make_library_local(bmain, libname);
1579 /* important we unset, otherwise these object wont
1580 * link into other scenes from this blend file */
1581 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1583 /* recreate dependency graph to include new objects */
1584 DAG_scene_sort(bmain, scene);
1585 DAG_ids_flush_update(bmain, 0);
1587 BLO_blendhandle_close(bh);
1589 /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1590 BLI_strncpy(G.lib, dir, FILE_MAX);
1592 WM_event_add_notifier(C, NC_WINDOW, NULL);
1594 return OPERATOR_FINISHED;
1597 static void WM_OT_link_append(wmOperatorType *ot)
1599 ot->name= "Link/Append from Library";
1600 ot->idname= "WM_OT_link_append";
1601 ot->description= "Link or Append from a Library .blend file";
1603 ot->invoke= wm_link_append_invoke;
1604 ot->exec= wm_link_append_exec;
1605 ot->poll= WM_operator_winactive;
1607 ot->flag |= OPTYPE_UNDO;
1609 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1611 RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1612 RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1613 RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1614 RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1616 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1619 /* *************** recover last session **************** */
1621 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1623 char filename[FILE_MAX];
1625 G.fileflags |= G_FILE_RECOVER;
1627 // XXX wm in context is not set correctly after WM_read_file -> crash
1628 // do it before for now, but is this correct with multiple windows?
1629 WM_event_add_notifier(C, NC_WINDOW, NULL);
1632 BLI_make_file_string("/", filename, btempdir, "quit.blend");
1633 WM_read_file(C, filename, op->reports);
1635 G.fileflags &= ~G_FILE_RECOVER;
1636 return OPERATOR_FINISHED;
1639 static void WM_OT_recover_last_session(wmOperatorType *ot)
1641 ot->name= "Recover Last Session";
1642 ot->idname= "WM_OT_recover_last_session";
1643 ot->description="Open the last closed file (\"quit.blend\")";
1645 ot->exec= wm_recover_last_session_exec;
1646 ot->poll= WM_operator_winactive;
1649 /* *************** recover auto save **************** */
1651 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1653 char path[FILE_MAX];
1655 RNA_string_get(op->ptr, "filepath", path);
1657 G.fileflags |= G_FILE_RECOVER;
1659 // XXX wm in context is not set correctly after WM_read_file -> crash
1660 // do it before for now, but is this correct with multiple windows?
1661 WM_event_add_notifier(C, NC_WINDOW, NULL);
1664 WM_read_file(C, path, op->reports);
1666 G.fileflags &= ~G_FILE_RECOVER;
1668 return OPERATOR_FINISHED;
1671 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1673 char filename[FILE_MAX];
1675 wm_autosave_location(filename);
1676 RNA_string_set(op->ptr, "filepath", filename);
1677 WM_event_add_fileselect(C, op);
1679 return OPERATOR_RUNNING_MODAL;
1682 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1684 ot->name= "Recover Auto Save";
1685 ot->idname= "WM_OT_recover_auto_save";
1686 ot->description="Open an automatically saved file to recover it";
1688 ot->exec= wm_recover_auto_save_exec;
1689 ot->invoke= wm_recover_auto_save_invoke;
1690 ot->poll= WM_operator_winactive;
1692 WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1695 /* *************** save file as **************** */
1697 static void untitled(char *name)
1699 if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1700 char *c= BLI_last_slash(name);
1703 strcpy(&c[1], "untitled.blend");
1705 strcpy(name, "untitled.blend");
1709 static void save_set_compress(wmOperator *op)
1711 if(!RNA_property_is_set(op->ptr, "compress")) {
1712 if(G.save_over) /* keep flag for existing file */
1713 RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1714 else /* use userdef for new file */
1715 RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1719 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1721 char name[FILE_MAX];
1723 save_set_compress(op);
1725 /* if not saved before, get the name of the most recently used .blend file */
1726 if(G.main->name[0]==0 && G.recent_files.first) {
1727 struct RecentFile *recent = G.recent_files.first;
1728 BLI_strncpy(name, recent->filepath, FILE_MAX);
1731 BLI_strncpy(name, G.main->name, FILE_MAX);
1734 RNA_string_set(op->ptr, "filepath", name);
1736 WM_event_add_fileselect(C, op);
1738 return OPERATOR_RUNNING_MODAL;
1741 /* function used for WM_OT_save_mainfile too */
1742 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1744 char path[FILE_MAX];
1748 save_set_compress(op);
1750 if(RNA_property_is_set(op->ptr, "filepath"))
1751 RNA_string_get(op->ptr, "filepath", path);
1753 BLI_strncpy(path, G.main->name, FILE_MAX);
1757 if(RNA_property_is_set(op->ptr, "copy"))
1758 copy = RNA_boolean_get(op->ptr, "copy");
1760 fileflags= G.fileflags;
1762 /* set compression flag */
1763 if(RNA_boolean_get(op->ptr, "compress")) fileflags |= G_FILE_COMPRESS;
1764 else fileflags &= ~G_FILE_COMPRESS;
1765 if(RNA_boolean_get(op->ptr, "relative_remap")) fileflags |= G_FILE_RELATIVE_REMAP;
1766 else fileflags &= ~G_FILE_RELATIVE_REMAP;
1768 if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1769 return OPERATOR_CANCELLED;
1771 WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1773 return OPERATOR_FINISHED;
1776 /* function used for WM_OT_save_mainfile too */
1777 static int blend_save_check(bContext *UNUSED(C), wmOperator *op)
1779 char filepath[FILE_MAX];
1780 RNA_string_get(op->ptr, "filepath", filepath);
1781 if(BLI_replace_extension(filepath, sizeof(filepath), ".blend")) {
1782 RNA_string_set(op->ptr, "filepath", filepath);
1788 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1790 ot->name= "Save As Blender File";
1791 ot->idname= "WM_OT_save_as_mainfile";
1792 ot->description="Save the current file in the desired location";
1794 ot->invoke= wm_save_as_mainfile_invoke;
1795 ot->exec= wm_save_as_mainfile_exec;
1796 ot->check= blend_save_check;
1797 /* ommit window poll so this can work in background mode */
1799 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1800 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1801 RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1802 RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1805 /* *************** save file directly ******** */
1807 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1809 char name[FILE_MAX];
1810 int check_existing=1;
1812 /* cancel if no active window */
1813 if (CTX_wm_window(C) == NULL)
1814 return OPERATOR_CANCELLED;
1816 save_set_compress(op);
1818 /* if not saved before, get the name of the most recently used .blend file */
1819 if(G.main->name[0]==0 && G.recent_files.first) {
1820 struct RecentFile *recent = G.recent_files.first;
1821 BLI_strncpy(name, recent->filepath, FILE_MAX);
1824 BLI_strncpy(name, G.main->name, FILE_MAX);
1828 RNA_string_set(op->ptr, "filepath", name);
1830 if (RNA_struct_find_property(op->ptr, "check_existing"))
1831 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1836 uiPupMenuSaveOver(C, op, name);
1838 wm_save_as_mainfile_exec(C, op);
1841 WM_event_add_fileselect(C, op);
1844 return OPERATOR_RUNNING_MODAL;
1847 static void WM_OT_save_mainfile(wmOperatorType *ot)
1849 ot->name= "Save Blender File";
1850 ot->idname= "WM_OT_save_mainfile";
1851 ot->description="Save the current Blender file";
1853 ot->invoke= wm_save_mainfile_invoke;
1854 ot->exec= wm_save_as_mainfile_exec;
1855 ot->check= blend_save_check;
1858 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1859 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1860 RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1863 /* XXX: move these collada operators to a more appropriate place */
1866 #include "../../collada/collada.h"
1868 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1870 if(!RNA_property_is_set(op->ptr, "filepath")) {
1871 char filepath[FILE_MAX];
1872 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
1873 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
1874 RNA_string_set(op->ptr, "filepath", filepath);
1877 WM_event_add_fileselect(C, op);
1879 return OPERATOR_RUNNING_MODAL;
1882 /* function used for WM_OT_save_mainfile too */
1883 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1885 char filename[FILE_MAX];
1887 if(!RNA_property_is_set(op->ptr, "filepath")) {
1888 BKE_report(op->reports, RPT_ERROR, "No filename given");
1889 return OPERATOR_CANCELLED;
1892 RNA_string_get(op->ptr, "filepath", filename);
1893 collada_export(CTX_data_scene(C), filename);
1895 return OPERATOR_FINISHED;
1898 static void WM_OT_collada_export(wmOperatorType *ot)
1900 ot->name= "Export COLLADA";
1901 ot->idname= "WM_OT_collada_export";
1903 ot->invoke= wm_collada_export_invoke;
1904 ot->exec= wm_collada_export_exec;
1905 ot->poll= WM_operator_winactive;
1907 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1910 /* function used for WM_OT_save_mainfile too */
1911 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1913 char filename[FILE_MAX];
1915 if(!RNA_property_is_set(op->ptr, "filepath")) {
1916 BKE_report(op->reports, RPT_ERROR, "No filename given");
1917 return OPERATOR_CANCELLED;
1920 RNA_string_get(op->ptr, "filepath", filename);
1921 collada_import(C, filename);
1923 return OPERATOR_FINISHED;
1926 static void WM_OT_collada_import(wmOperatorType *ot)
1928 ot->name= "Import COLLADA";
1929 ot->idname= "WM_OT_collada_import";
1931 ot->invoke= WM_operator_filesel;
1932 ot->exec= wm_collada_import_exec;
1933 ot->poll= WM_operator_winactive;
1935 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1942 /* *********************** */
1944 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1946 ot->name= "Toggle Fullscreen";
1947 ot->idname= "WM_OT_window_fullscreen_toggle";
1948 ot->description="Toggle the current window fullscreen";
1950 ot->exec= wm_window_fullscreen_toggle_exec;
1951 ot->poll= WM_operator_winactive;
1954 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1956 WM_operator_free(op);
1960 return OPERATOR_FINISHED;
1963 static void WM_OT_quit_blender(wmOperatorType *ot)
1965 ot->name= "Quit Blender";
1966 ot->idname= "WM_OT_quit_blender";
1967 ot->description= "Quit Blender";
1969 ot->invoke= WM_operator_confirm;
1970 ot->exec= wm_exit_blender_op;
1971 ot->poll= WM_operator_winactive;
1974 /* *********************** */
1976 static int console= 1;
1977 void WM_toggle_console(bContext *UNUSED(C), short show)
1980 ShowWindow(GetConsoleWindow(),SW_SHOW);
1984 ShowWindow(GetConsoleWindow(),SW_HIDE);
1989 static int wm_toggle_console_op(bContext *C, wmOperator *UNUSED(op))
1992 WM_toggle_console(C, 0);
1995 WM_toggle_console(C, 1);
1997 return OPERATOR_FINISHED;
2000 static void WM_OT_toggle_console(wmOperatorType *ot)
2002 ot->name= "Toggle System Console";
2003 ot->idname= "WM_OT_toggle_console";
2004 ot->description= "Toggle System Console";
2006 ot->exec= wm_toggle_console_op;
2007 ot->poll= WM_operator_winactive;
2011 /* ************ default paint cursors, draw always around cursor *********** */
2013 - returns handler to free
2014 - poll(bContext): returns 1 if draw should happen
2015 - draw(bContext): drawing callback for paint cursor
2018 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2019 wmPaintCursorDraw draw, void *customdata)
2021 wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2023 BLI_addtail(&wm->paintcursors, pc);
2025 pc->customdata = customdata;
2032 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2036 for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2037 if(pc == (wmPaintCursor *)handle) {
2038 BLI_remlink(&wm->paintcursors, pc);
2045 /* ************ window gesture operator-callback definitions ************** */
2047 * These are default callbacks for use in operators requiring gesture input
2050 /* **************** Border gesture *************** */
2052 /* Border gesture has two types:
2053 1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
2054 2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2056 It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2059 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2061 wmGesture *gesture= op->customdata;
2062 rcti *rect= gesture->customdata;
2064 if(rect->xmin > rect->xmax)
2065 SWAP(int, rect->xmin, rect->xmax);
2066 if(rect->ymin > rect->ymax)
2067 SWAP(int, rect->ymin, rect->ymax);
2069 if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2072 /* operator arguments and storage. */
2073 RNA_int_set(op->ptr, "xmin", rect->xmin);
2074 RNA_int_set(op->ptr, "ymin", rect->ymin);
2075 RNA_int_set(op->ptr, "xmax", rect->xmax);
2076 RNA_int_set(op->ptr, "ymax", rect->ymax);
2078 /* XXX weak; border should be configured for this without reading event types */
2079 if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2080 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2082 op->type->exec(C, op);
2087 static void wm_gesture_end(bContext *C, wmOperator *op)
2089 wmGesture *gesture= op->customdata;
2091 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2092 op->customdata= NULL;
2094 ED_area_tag_redraw(CTX_wm_area(C));
2096 if( RNA_struct_find_property(op->ptr, "cursor") )
2097 WM_cursor_restore(CTX_wm_window(C));
2100 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2102 if(ISTWEAK(event->type))
2103 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2105 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2107 /* add modal handler */
2108 WM_event_add_modal_handler(C, op);
2110 wm_gesture_tag_redraw(C);
2112 return OPERATOR_RUNNING_MODAL;
2115 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2117 wmGesture *gesture= op->customdata;
2118 rcti *rect= gesture->customdata;
2121 if(event->type== MOUSEMOVE) {
2122 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2124 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2125 rect->xmin= rect->xmax= event->x - sx;
2126 rect->ymin= rect->ymax= event->y - sy;
2129 rect->xmax= event->x - sx;
2130 rect->ymax= event->y - sy;
2133 wm_gesture_tag_redraw(C);
2135 else if (event->type==EVT_MODAL_MAP) {
2136 switch (event->val) {
2137 case GESTURE_MODAL_BEGIN:
2138 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2140 wm_gesture_tag_redraw(C);
2143 case GESTURE_MODAL_SELECT:
2144 case GESTURE_MODAL_DESELECT:
2145 case GESTURE_MODAL_IN:
2146 case GESTURE_MODAL_OUT:
2147 if(border_apply(C, op, event->val)) {
2148 wm_gesture_end(C, op);
2149 return OPERATOR_FINISHED;
2151 wm_gesture_end(C, op);
2152 return OPERATOR_CANCELLED;
2155 case GESTURE_MODAL_CANCEL:
2156 wm_gesture_end(C, op);
2157 return OPERATOR_CANCELLED;
2161 // // Allow view navigation???
2163 // return OPERATOR_PASS_THROUGH;
2166 return OPERATOR_RUNNING_MODAL;
2169 /* **************** circle gesture *************** */
2170 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2172 #ifdef GESTURE_MEMORY
2173 int circle_select_size= 25; // XXX - need some operator memory thing\!
2176 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2178 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2180 /* add modal handler */
2181 WM_event_add_modal_handler(C, op);
2183 wm_gesture_tag_redraw(C);
2185 return OPERATOR_RUNNING_MODAL;
2188 static void gesture_circle_apply(bContext *C, wmOperator *op)
2190 wmGesture *gesture= op->customdata;
2191 rcti *rect= gesture->customdata;
2193 if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2196 /* operator arguments and storage. */
2197 RNA_int_set(op->ptr, "x", rect->xmin);
2198 RNA_int_set(op->ptr, "y", rect->ymin);
2199 RNA_int_set(op->ptr, "radius", rect->xmax);
2202 op->type->exec(C, op);
2204 #ifdef GESTURE_MEMORY
2205 circle_select_size= rect->xmax;
2209 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2211 wmGesture *gesture= op->customdata;
2212 rcti *rect= gesture->customdata;
2215 if(event->type== MOUSEMOVE) {
2216 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2218 rect->xmin= event->x - sx;
2219 rect->ymin= event->y - sy;
2221 wm_gesture_tag_redraw(C);
2224 gesture_circle_apply(C, op);
2226 else if (event->type==EVT_MODAL_MAP) {
2227 switch (event->val) {
2228 case GESTURE_MODAL_CIRCLE_ADD:
2229 rect->xmax += 2 + rect->xmax/10;
2230 wm_gesture_tag_redraw(C);
2232 case GESTURE_MODAL_CIRCLE_SUB:
2233 rect->xmax -= 2 + rect->xmax/10;
2234 if(rect->xmax < 1) rect->xmax= 1;
2235 wm_gesture_tag_redraw(C);
2237 case GESTURE_MODAL_SELECT:
2238 case GESTURE_MODAL_DESELECT:
2239 case GESTURE_MODAL_NOP:
2240 if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2241 RNA_int_set(op->ptr, "gesture_mode", event->val);
2243 if(event->val != GESTURE_MODAL_NOP) {
2244 /* apply first click */
2245 gesture_circle_apply(C, op);
2247 wm_gesture_tag_redraw(C);
2251 case GESTURE_MODAL_CANCEL:
2252 case GESTURE_MODAL_CONFIRM:
2253 wm_gesture_end(C, op);
2254 return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2257 // // Allow view navigation???
2259 // return OPERATOR_PASS_THROUGH;
2262 return OPERATOR_RUNNING_MODAL;
2266 /* template to copy from */
2267 void WM_OT_circle_gesture(wmOperatorType *ot)
2269 ot->name= "Circle Gesture";
2270 ot->idname= "WM_OT_circle_gesture";
2271 ot->description="Enter rotate mode with a circular gesture";
2273 ot->invoke= WM_gesture_circle_invoke;
2274 ot->modal= WM_gesture_circle_modal;
2276 ot->poll= WM_operator_winactive;
2278 RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2279 RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2280 RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2285 /* **************** Tweak gesture *************** */
2287 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2289 wmWindow *window= CTX_wm_window(C);
2290 wmGesture *gesture= window->tweak;
2291 rcti *rect= gesture->customdata;
2294 switch(event->type) {
2296 case INBETWEEN_MOUSEMOVE:
2298 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2300 rect->xmax= event->x - sx;
2301 rect->ymax= event->y - sy;
2303 if((val= wm_gesture_evaluate(gesture))) {
2306 tevent= *(window->eventstate);
2307 if(gesture->event_type==LEFTMOUSE)
2308 tevent.type= EVT_TWEAK_L;
2309 else if(gesture->event_type==RIGHTMOUSE)
2310 tevent.type= EVT_TWEAK_R;
2312 tevent.type= EVT_TWEAK_M;
2315 wm_event_add(window, &tevent);
2317 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2325 if(gesture->event_type==event->type) {
2326 WM_gesture_end(C, gesture);
2328 /* when tweak fails we should give the other keymap entries a chance */
2329 event->val= KM_RELEASE;
2333 if(!ISTIMER(event->type)) {
2334 WM_gesture_end(C, gesture);
2340 /* standard tweak, called after window handlers passed on event */
2341 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2343 wmWindow *win= CTX_wm_window(C);
2345 if(win->tweak==NULL) {
2346 if(CTX_wm_region(C)) {
2347 if(event->val==KM_PRESS) {
2348 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2349 win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2354 /* no tweaks if event was handled */
2355 if((action & WM_HANDLER_BREAK)) {
2356 WM_gesture_end(C, win->tweak);
2359 tweak_gesture_modal(C, event);
2363 /* *********************** lasso gesture ****************** */
2365 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2367 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2369 /* add modal handler */
2370 WM_event_add_modal_handler(C, op);
2372 wm_gesture_tag_redraw(C);
2374 if( RNA_struct_find_property(op->ptr, "cursor") )
2375 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2377 return OPERATOR_RUNNING_MODAL;
2380 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2382 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2384 /* add modal handler */
2385 WM_event_add_modal_handler(C, op);
2387 wm_gesture_tag_redraw(C);
2389 if( RNA_struct_find_property(op->ptr, "cursor") )
2390 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2392 return OPERATOR_RUNNING_MODAL;
2396 static void gesture_lasso_apply(bContext *C, wmOperator *op)
2398 wmGesture *gesture= op->customdata;
2402 short *lasso= gesture->customdata;
2404 /* operator storage as path. */
2406 for(i=0; i<gesture->points; i++, lasso+=2) {
2409 RNA_collection_add(op->ptr, "path", &itemptr);
2410 RNA_float_set_array(&itemptr, "loc", loc);
2413 wm_gesture_end(C, op);
2416 op->type->exec(C, op);
2420 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2422 wmGesture *gesture= op->customdata;
2425 switch(event->type) {
2427 case INBETWEEN_MOUSEMOVE:
2429 wm_gesture_tag_redraw(C);
2431 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2433 if(gesture->points == gesture->size) {
2434 short *old_lasso = gesture->customdata;
2435 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2436 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2437 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2438 MEM_freeN(old_lasso);
2439 printf("realloc\n");
2444 short *lasso= gesture->customdata;
2446 lasso += (2 * gesture->points - 2);
2447 x = (event->x - sx - lasso[0]);
2448 y = (event->y - sy - lasso[1]);
2450 /* make a simple distance check to get a smoother lasso
2451 add only when at least 2 pixels between this and previous location */
2454 lasso[0] = event->x - sx;
2455 lasso[1] = event->y - sy;
2464 if(event->val==KM_RELEASE) { /* key release */
2465 gesture_lasso_apply(C, op);
2466 return OPERATOR_FINISHED;
2470 wm_gesture_end(C, op);
2471 return OPERATOR_CANCELLED;
2473 return OPERATOR_RUNNING_MODAL;
2476 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2478 return WM_gesture_lasso_modal(C, op, event);
2482 /* template to copy from */
2484 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2486 RNA_BEGIN(op->ptr, itemptr, "path") {
2489 RNA_float_get_array(&itemptr, "loc", loc);
2490 printf("Location: %f %f\n", loc[0], loc[1]);
2494 return OPERATOR_FINISHED;
2497 void WM_OT_lasso_gesture(wmOperatorType *ot)
2501 ot->name= "Lasso Gesture";
2502 ot->idname= "WM_OT_lasso_gesture";
2503 ot->description="Select objects within the lasso as you move the pointer";
2505 ot->invoke= WM_gesture_lasso_invoke;
2506 ot->modal= WM_gesture_lasso_modal;
2507 ot->exec= gesture_lasso_exec;
2509 ot->poll= WM_operator_winactive;
2511 prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2512 RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2516 /* *********************** straight line gesture ****************** */
2518 static int straightline_apply(bContext *C, wmOperator *op)
2520 wmGesture *gesture= op->customdata;
2521 rcti *rect= gesture->customdata;
2523 if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2526 /* operator arguments and storage. */
2527 RNA_int_set(op->ptr, "xstart", rect->xmin);
2528 RNA_int_set(op->ptr, "ystart", rect->ymin);
2529 RNA_int_set(op->ptr, "xend", rect->xmax);
2530 RNA_int_set(op->ptr, "yend", rect->ymax);
2533 op->type->exec(C, op);
2539 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2541 op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2543 /* add modal handler */
2544 WM_event_add_modal_handler(C, op);
2546 wm_gesture_tag_redraw(C);
2548 if( RNA_struct_find_property(op->ptr, "cursor") )
2549 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2551 return OPERATOR_RUNNING_MODAL;
2554 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2556 wmGesture *gesture= op->customdata;
2557 rcti *rect= gesture->customdata;
2560 if(event->type== MOUSEMOVE) {
2561 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2563 if(gesture->mode==0) {
2564 rect->xmin= rect->xmax= event->x - sx;
2565 rect->ymin= rect->ymax= event->y - sy;
2568 rect->xmax= event->x - sx;
2569 rect->ymax= event->y - sy;
2570 straightline_apply(C, op);
2573 wm_gesture_tag_redraw(C);
2575 else if (event->type==EVT_MODAL_MAP) {
2576 switch (event->val) {
2577 case GESTURE_MODAL_BEGIN:
2578 if(gesture->mode==0) {
2580 wm_gesture_tag_redraw(C);
2583 case GESTURE_MODAL_SELECT:
2584 if(straightline_apply(C, op)) {
2585 wm_gesture_end(C, op);
2586 return OPERATOR_FINISHED;
2588 wm_gesture_end(C, op);
2589 return OPERATOR_CANCELLED;
2592 case GESTURE_MODAL_CANCEL:
2593 wm_gesture_end(C, op);
2594 return OPERATOR_CANCELLED;
2599 return OPERATOR_RUNNING_MODAL;
2603 /* template to copy from */
2604 void WM_OT_straightline_gesture(wmOperatorType *ot)
2608 ot->name= "Straight Line Gesture";
2609 ot->idname= "WM_OT_straightline_gesture";
2610 ot->description="Draw a straight line as you move the pointer";
2612 ot->invoke= WM_gesture_straightline_invoke;
2613 ot->modal= WM_gesture_straightline_modal;
2614 ot->exec= gesture_straightline_exec;
2616 ot->poll= WM_operator_winactive;
2618 WM_operator_properties_gesture_straightline(ot, 0);
2622 /* *********************** radial control ****************** */
2624 static const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2626 typedef struct wmRadialControl {
2628 float initial_value, value, max_value;
2629 float col[4], tex_col[4];
2630 int initial_mouse[2];
2635 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2637 wmRadialControl *rc = (wmRadialControl*)customdata;
2638 ARegion *ar = CTX_wm_region(C);
2639 float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2643 if(rc->mode == WM_RADIALCONTROL_STRENGTH)
2644 rc->tex_col[3]= (rc->value + 0.5);
2646 if(rc->mode == WM_RADIALCONTROL_SIZE) {
2648 r2= rc->initial_value;
2650 } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2651 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2652 r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2653 } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2654 r1= r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2658 /* Keep cursor in the original place */
2659 x = rc->initial_mouse[0] - ar->winrct.xmin;
2660 y = rc->initial_mouse[1] - ar->winrct.ymin;
2662 glTranslatef((float)x, (float)y, 0.0f);
2666 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2667 glRotatef(angle, 0, 0, 1);
2671 glBindTexture(GL_TEXTURE_2D, rc->tex);
2673 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2674 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2676 glEnable(GL_TEXTURE_2D);
2678 glColor4f(rc->tex_col[0], rc->tex_col[1], rc->tex_col[2], rc->tex_col[3]);
2680 glVertex2f(-r3, -r3);
2682 glVertex2f(r3, -r3);
2686 glVertex2f(-r3, r3);
2688 glDisable(GL_TEXTURE_2D);
2691 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2692 glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2693 glEnable(GL_LINE_SMOOTH);
2694 glRotatef(-angle, 0, 0, 1);
2695 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2696 glRotatef(angle, 0, 0, 1);
2697 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2698 glDisable(GL_LINE_SMOOTH);
2701 glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2702 glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2703 glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2704 glDisable(GL_BLEND);
2707 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2709 wmRadialControl *rc = (wmRadialControl*)op->customdata;
2710 int mode, initial_mouse[2], delta[2];
2712 double new_value = RNA_float_get(op->ptr, "new_value");
2713 int ret = OPERATOR_RUNNING_MODAL;
2714 // float initial_value = RNA_float_get(op->ptr, "initial_value");
2716 mode = RNA_int_get(op->ptr, "mode");
2717 RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2719 switch(event->type) {
2721 delta[0]= initial_mouse[0] - event->x;
2722 delta[1]= initial_mouse[1] - event->y;
2724 //if (mode == WM_RADIALCONTROL_SIZE)
2725 // delta[0]+= initial_value;
2726 //else if(mode == WM_RADIALCONTROL_STRENGTH)
2727 // delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2728 //else if(mode == WM_RADIALCONTROL_ANGLE) {
2729 // delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value*M_PI/180.0f);
2730 // delta[1]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value*M_PI/180.0f);
2733 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2735 if(mode == WM_RADIALCONTROL_SIZE)
2737 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2738 new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2739 } else if(mode == WM_RADIALCONTROL_ANGLE)
2740 new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2743 if(mode == WM_RADIALCONTROL_STRENGTH)
2744 new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2746 new_value = ((int)new_value + 5) / 10*10;
2752 ret = OPERATOR_CANCELLED;
2756 op->type->exec(C, op);
2757 ret = OPERATOR_FINISHED;
2762 if(new_value > rc->max_value)
2763 new_value = rc->max_value;
2764 else if(new_value < 0)
2767 /* Update paint data */
2768 rc->value = new_value;
2770 RNA_float_set(op->ptr, "new_value", new_value);
2772 if(ret != OPERATOR_RUNNING_MODAL) {
2773 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2777 ED_region_tag_redraw(CTX_wm_region(C));
2779 //if (ret != OPERATOR_RUNNING_MODAL) {
2780 // wmWindow *win = CTX_wm_window(C);
2781 // WM_cursor_restore(win);
2787 /* Expects the operator customdata to be an ImBuf (or NULL) */
2788 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2790 wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2791 // wmWindow *win = CTX_wm_window(C);
2792 int mode = RNA_int_get(op->ptr, "mode");
2793 float initial_value = RNA_float_get(op->ptr, "initial_value");
2794 //float initial_size = RNA_float_get(op->ptr, "initial_size");
2800 //if (initial_size == 0)
2801 // initial_size = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2803 if(mode == WM_RADIALCONTROL_SIZE) {
2804 rc->max_value = 200;
2805 mouse[0]-= initial_value;
2807 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2809 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2811 else if(mode == WM_RADIALCONTROL_ANGLE) {
2812 rc->max_value = 360;
2813 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2814 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2815 initial_value *= 180.0f/M_PI;
2818 if(op->customdata) {
2819 ImBuf *im = (ImBuf*)op->customdata;
2820 /* Build GL texture */
2821 glGenTextures(1, &rc->tex);
2822 glBindTexture(GL_TEXTURE_2D, rc->tex);
2823 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2824 MEM_freeN(im->rect_float);
2828 RNA_float_get_array(op->ptr, "color", rc->col);
2829 RNA_float_get_array(op->ptr, "texture_color", rc->tex_col);
2831 RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2832 RNA_float_set(op->ptr, "new_value", initial_value);
2834 op->customdata = rc;
2836 rc->initial_value = initial_value;
2837 rc->initial_mouse[0] = mouse[0];
2838 rc->initial_mouse[1] = mouse[1];
2839 rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2840 wm_radial_control_paint, op->customdata);
2842 //WM_cursor_modal(win, CURSOR_NONE);
2844 /* add modal handler */
2845 WM_event_add_modal_handler(C, op);
2847 WM_radial_control_modal(C, op, event);
2849 return OPERATOR_RUNNING_MODAL;
2852 /* Gets a descriptive string of the operation */
2853 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2855 int mode = RNA_int_get(op->ptr, "mode");
2856 float v = RNA_float_get(op->ptr, "new_value");
2858 if(mode == WM_RADIALCONTROL_SIZE)
2859 BLI_snprintf(str, maxlen, "Size: %d", (int)v);
2860 else if(mode == WM_RADIALCONTROL_STRENGTH)
2861 BLI_snprintf(str, maxlen, "Strength: %d", (int)v);
2862 else if(mode == WM_RADIALCONTROL_ANGLE)
2863 BLI_snprintf(str, maxlen, "Angle: %d", (int)(v * 180.0f/M_PI));
2866 /** Important: this doesn't define an actual operator, it
2867 just sets up the common parts of the radial control op. **/
2868 void WM_OT_radial_control_partial(wmOperatorType *ot)
2870 static EnumPropertyItem radial_mode_items[] = {
2871 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2872 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2873 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2874 {0, NULL, 0, NULL, NULL}};
2875 static float color[4] = {1.0f, 1.0f, 1.0f, 0.5f};
2876 static float tex_color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
2878 /* Should be set in custom invoke() */
2879 RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2881 /* Set internally, should be used in custom exec() to get final value */
2882 RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2884 /* Should be set before calling operator */
2885 RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2888 RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "Initial Mouse", "", INT_MIN, INT_MAX);
2890 RNA_def_float_color(ot->srna, "color", 4, color, 0.0f, FLT_MAX, "Color", "Radial control color", 0.0f, 1.0f);
2891 RNA_def_float_color(ot->srna, "texture_color", 4, tex_color, 0.0f, FLT_MAX, "Texture Color", "Radial control texture color", 0.0f, 1.0f);
2894 /* ************************** timer for testing ***************** */
2896 /* uses no type defines, fully local testing function anyway... ;) */
2898 static void redraw_timer_window_swap(bContext *C)
2900 wmWindow *win= CTX_wm_window(C);
2903 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2904 ED_area_tag_redraw(sa);
2907 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2910 static EnumPropertyItem redraw_timer_type_items[] = {
2911 {0, "DRAW", 0, "Draw Region", "Draw Region"},
2912 {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2913 {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2914 {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2915 {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2916 {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2917 {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2918 {0, NULL, 0, NULL, NULL}};
2920 static int redraw_timer_exec(bContext *C, wmOperator *op)
2922 ARegion *ar= CTX_wm_region(C);
2923 double stime= PIL_check_seconds_timer();
2924 int type = RNA_int_get(op->ptr, "type");
2925 int iter = RNA_int_get(op->ptr, "iterations");
2928 const char *infostr= "";
2932 for(a=0; a<iter; a++) {
2935 ED_region_do_draw(C, ar);
2938 wmWindow *win= CTX_wm_window(C);
2940 ED_region_tag_redraw(ar);
2943 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2946 wmWindow *win= CTX_wm_window(C);
2949 ScrArea *sa_back= CTX_wm_area(C);
2950 ARegion *ar_back= CTX_wm_region(C);
2952 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
2954 CTX_wm_area_set(C, sa);
2956 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
2957 if(ar_iter->swinid) {
2958 CTX_wm_region_set(C, ar_iter);
2959 ED_region_do_draw(C, ar_iter);
2964 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2966 CTX_wm_area_set(C, sa_back);
2967 CTX_wm_region_set(C, ar_back);
2970 redraw_timer_window_swap(C);
2973 Main *bmain= CTX_data_main(C);
2974 Scene *scene= CTX_data_scene(C);
2976 if(a & 1) scene->r.cfra--;
2977 else scene->r.cfra++;
2978 scene_update_for_newframe(bmain, scene, scene->lay);
2982 /* play anim, return on same frame as started with */
2983 Main *bmain= CTX_data_main(C);
2984 Scene *scene= CTX_data_scene(C);
2985 int tot= (scene->r.efra - scene->r.sfra) + 1;
2988 /* todo, ability to escape! */
2990 if(scene->r.cfra > scene->r.efra)
2991 scene->r.cfra= scene->r.sfra;
2993 scene_update_for_newframe(bmain, scene, scene->lay);
2994 redraw_timer_window_swap(C);
3003 time= ((PIL_check_seconds_timer()-stime)*1000);
3005 RNA_enum_description(redraw_timer_type_items, type, &infostr);
3009 BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms, average: %.4f", iter, infostr, time, time/iter);
3011 return OPERATOR_FINISHED;
3014 static void WM_OT_redraw_timer(wmOperatorType *ot)
3016 ot->name= "Redraw Timer";
3017 ot->idname= "WM_OT_redraw_timer";
3018 ot->description="Simple redraw timer to test the speed of updating the interface";
3020 ot->invoke= WM_menu_invoke;
3021 ot->exec= redraw_timer_exec;
3022 ot->poll= WM_operator_winactive;
3024 ot->prop= RNA_def_enum(ot->srna, "type", redraw_timer_type_items, 0, "Type", "");
3025 RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
3029 /* ************************** memory statistics for testing ***************** */
3031 static int memory_statistics_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
3033 MEM_printmemlist_stats();
3034 return OPERATOR_FINISHED;
3037 static void WM_OT_memory_statistics(wmOperatorType *ot)
3039 ot->name= "Memory Statistics";
3040 ot->idname= "WM_OT_memory_statistics";
3041 ot->description= "Print memory statistics to the console";
3043 ot->exec= memory_statistics_exec;
3046 /* ******************************************************* */
3048 /* called on initialize WM_exit() */
3049 void wm_operatortype_free(void)
3053 for(ot= global_ops.first; ot; ot= ot->next) {
3055 wm_operatortype_free_macro(ot);
3057 if(ot->ext.srna) /* python operator, allocs own string */
3058 MEM_freeN((void *)ot->idname);
3061 BLI_freelistN(&global_ops);
3064 /* called on initialize WM_init() */
3065 void wm_operatortype_init(void)
3067 WM_operatortype_append(WM_OT_window_duplicate);
3068 WM_operatortype_append(WM_OT_read_homefile);
3069 WM_operatortype_append(WM_OT_read_factory_settings);
3070 WM_operatortype_append(WM_OT_save_homefile);
3071 WM_operatortype_append(WM_OT_window_fullscreen_toggle);
3072 WM_operatortype_append(WM_OT_quit_blender);
3073 WM_operatortype_append(WM_OT_open_mainfile);
3074 WM_operatortype_append(WM_OT_link_append);
3075 WM_operatortype_append(WM_OT_recover_last_session);
3076 WM_operatortype_append(WM_OT_recover_auto_save);
3077 WM_operatortype_append(WM_OT_save_as_mainfile);
3078 WM_operatortype_append(WM_OT_save_mainfile);
3079 WM_operatortype_append(WM_OT_redraw_timer);
3080 WM_operatortype_append(WM_OT_memory_statistics);
3081 WM_operatortype_append(WM_OT_debug_menu);
3082 WM_operatortype_append(WM_OT_splash);
3083 WM_operatortype_append(WM_OT_search_menu);
3084 WM_operatortype_append(WM_OT_call_menu);
3086 WM_operatortype_append(WM_OT_toggle_console);
3090 /* XXX: move these */
3091 WM_operatortype_append(WM_OT_collada_export);
3092 WM_operatortype_append(WM_OT_collada_import);
3097 /* circleselect-like modal operators */
3098 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
3100 static EnumPropertyItem modal_items[] = {
3101 {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3102 {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3103 {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
3104 {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
3106 {GESTURE_MODAL_SELECT, "SELECT", 0, "Select", ""},
3107 {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3108 {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
3111 {0, NULL, 0, NULL, NULL}};
3113 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
3115 /* this function is called for each spacetype, only needs to add map once */
3118 keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
3120 /* items for modal map */
3121 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3122 WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3124 WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
3125 WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
3127 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
3129 #if 0 // Durien guys like this :S
3130 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3131 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
3133 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); // defailt 2.4x
3134 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); // defailt 2.4x
3137 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
3139 WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3140 WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3141 WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3142 WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3144 /* assign map to operators */
3145 WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
3146 WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
3150 /* straight line modal operators */
3151 static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
3153 static EnumPropertyItem modal_items[] = {
3154 {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3155 {GESTURE_MODAL_SELECT, "SELECT", 0, "Select", ""},
3156 {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3157 {0, NULL, 0, NULL, NULL}};
3159 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Straight Line");
3161 /* this function is called for each spacetype, only needs to add map once */
3164 keymap= WM_modalkeymap_add(keyconf, "Gesture Straight Line", modal_items);
3166 /* items for modal map */
3167 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3168 WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3170 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3171 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_SELECT);
3173 /* assign map to operators */
3174 WM_modalkeymap_assign(keymap, "IMAGE_OT_sample_line");
3178 /* borderselect-like modal operators */
3179 static void gesture_border_modal_keymap(wmKeyConfig *keyconf)
3181 static EnumPropertyItem modal_items[] = {
3182 {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3183 {GESTURE_MODAL_SELECT, "SELECT", 0, "Select", ""},
3184 {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3185 {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3186 {0, NULL, 0, NULL, NULL}};
3188 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Border");
3190 /* this function is called for each spacetype, only needs to add map once */
3193 keymap= WM_modalkeymap_add(keyconf, "Gesture Border", modal_items);
3195 /* items for modal map */
3196 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3197 WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3199 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3200 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, GESTURE_MODAL_SELECT);
3202 #if 0 // Durian guys like this
3203 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_BEGIN);
3204 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3206 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3207 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_DESELECT);
3210 /* assign map to operators */
3211 WM_modalkeymap_assign(keymap, "ACTION_OT_select_border");
3212 WM_modalkeymap_assign(keymap, "ANIM_OT_channels_select_border");
3213 WM_modalkeymap_assign(keymap, "ANIM_OT_previewrange_set");
3214 WM_modalkeymap_assign(keymap, "INFO_OT_select_border");
3215 WM_modalkeymap_assign(keymap, "FILE_OT_select_border");
3216 WM_modalkeymap_assign(keymap, "GRAPH_OT_select_border");
3217 WM_modalkeymap_assign(keymap, "MARKER_OT_select_border");
3218 WM_modalkeymap_assign(keymap, "NLA_OT_select_border");
3219 WM_modalkeymap_assign(keymap, "NODE_OT_select_border");
3220 // WM_modalkeymap_assign(keymap, "SCREEN_OT_border_select"); // template
3221 WM_modalkeymap_assign(keymap, "SEQUENCER_OT_select_border");
3222 WM_modalkeymap_assign(keymap, "SEQUENCER_OT_view_ghost_border");
3223 WM_modalkeymap_assign(keymap, "UV_OT_select_border");
3224 WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3225 WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
3226 WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");
3227 WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_border");
3228 WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border"); // XXX TODO: zoom border should perhaps map rightmouse to zoom out instead of in+cancel
3231 /* zoom to border modal operators */
3232 static void gesture_zoom_border_modal_keymap(wmKeyConfig *keyconf)
3234 static EnumPropertyItem modal_items[] = {
3235 {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3236 {GESTURE_MODAL_IN, "IN", 0, "In", ""},
3237 {GESTURE_MODAL_OUT, "OUT", 0, "Out", ""},
3238 {GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
3239 {0, NULL, 0, NULL, NULL}};
3241 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "Gesture Zoom Border");
3243 /* this function is called for each spacetype, only needs to add map once */
3246 keymap= WM_modalkeymap_add(keyconf, "Gesture Zoom Border", modal_items);
3248 /* items for modal map */
3249 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3250 WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3252 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3253 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_IN);
3255 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_BEGIN);
3256 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_OUT);
3258 /* assign map to operators */
3259 WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
3260 WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom_border");
3263 /* default keymap for windows and screens, only call once per WM */
3264 void wm_window_keymap(wmKeyConfig *keyconf)
3266 wmKeyMap *keymap= WM_keymap_find(keyconf, "Window", 0, 0);
3269 /* note, this doesn't replace existing keymap items */
3270 WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3272 WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
3273 WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3274 WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
3275 WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
3276 WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
3277 WM_keymap_add_item(keymap, "WM_OT_quit_blender", QKEY, KM_PRESS, KM_OSKEY, 0);
3279 WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
3280 WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
3281 WM_keymap_add_menu(keymap, "INFO_MT_file_open_recent", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3282 WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
3283 WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
3284 WM_keymap_add_item(keymap, "WM_OT_link_append", OKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
3285 kmi= WM_keymap_add_item(keymap, "WM_OT_link_append", F1KEY, KM_PRESS, KM_SHIFT, 0);
3286 RNA_boolean_set(kmi->ptr, "link", FALSE);
3287 RNA_boolean_set(kmi->ptr, "instance_groups", FALSE);
3289 WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
3290 WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
3291 WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
3292 WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
<