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"
44 #include "DNA_object_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_userdef_types.h"
48 #include "DNA_windowmanager_types.h"
50 #include "MEM_guardedalloc.h"
56 #include "BLI_blenlib.h"
57 #include "BLI_dynstr.h" /*for WM_operator_pystring */
60 #include "BLO_readfile.h"
62 #include "BKE_blender.h"
63 #include "BKE_context.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_idprop.h"
66 #include "BKE_library.h"
67 #include "BKE_global.h"
69 #include "BKE_report.h"
70 #include "BKE_scene.h"
71 #include "BKE_screen.h" /* BKE_ST_MAXNAME */
72 #include "BKE_utildefines.h"
73 #include "BKE_idcode.h"
76 #include "BIF_glutil.h" /* for paint cursor */
78 #include "IMB_imbuf_types.h"
80 #include "ED_screen.h"
83 #include "RNA_access.h"
84 #include "RNA_define.h"
86 #include "UI_interface.h"
87 #include "UI_resources.h"
94 #include "wm_event_system.h"
95 #include "wm_event_types.h"
96 #include "wm_subwindow.h"
97 #include "wm_window.h"
99 static ListBase global_ops= {NULL, NULL};
101 /* ************ operator API, exported ********** */
104 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
108 char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
109 WM_operator_bl_idname(idname_bl, idname);
112 ot= (wmOperatorType *)BLI_findstring_ptr(&global_ops, idname_bl, offsetof(wmOperatorType, idname));
119 printf("search for unknown operator %s, %s\n", idname_bl, idname);
124 wmOperatorType *WM_operatortype_first(void)
126 return global_ops.first;
129 /* all ops in 1 list (for time being... needs evaluation later) */
130 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
134 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
135 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
139 static char dummy_name[] = "Dummy Name";
140 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
141 ot->name= dummy_name;
144 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.
145 RNA_def_struct_identifier(ot->srna, ot->idname);
146 BLI_addtail(&global_ops, ot);
149 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
153 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
154 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
155 opfunc(ot, userdata);
156 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
157 RNA_def_struct_identifier(ot->srna, ot->idname);
158 BLI_addtail(&global_ops, ot);
161 /* ********************* macro operator ******************** */
167 static void wm_macro_start(wmOperator *op)
169 if (op->customdata == NULL) {
170 op->customdata = MEM_callocN(sizeof(MacroData), "MacroData");
174 static int wm_macro_end(wmOperator *op, int retval)
176 if (retval & OPERATOR_CANCELLED) {
177 MacroData *md = op->customdata;
179 if (md->retval & OPERATOR_FINISHED) {
180 retval |= OPERATOR_FINISHED;
181 retval &= ~OPERATOR_CANCELLED;
185 /* if modal is ending, free custom data */
186 if (retval & (OPERATOR_FINISHED|OPERATOR_CANCELLED)) {
187 if (op->customdata) {
188 MEM_freeN(op->customdata);
189 op->customdata = NULL;
196 /* macro exec only runs exec calls */
197 static int wm_macro_exec(bContext *C, wmOperator *op)
200 int retval= OPERATOR_FINISHED;
204 for(opm= op->macro.first; opm; opm= opm->next) {
206 if(opm->type->exec) {
207 retval= opm->type->exec(C, opm);
209 if (retval & OPERATOR_FINISHED) {
210 MacroData *md = op->customdata;
211 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
213 break; /* operator didn't finish, end macro */
218 return wm_macro_end(op, retval);
221 int wm_macro_invoke_internal(bContext *C, wmOperator *op, wmEvent *event, wmOperator *opm)
223 int retval= OPERATOR_FINISHED;
225 /* start from operator received as argument */
226 for( ; opm; opm= opm->next) {
227 if(opm->type->invoke)
228 retval= opm->type->invoke(C, opm, event);
229 else if(opm->type->exec)
230 retval= opm->type->exec(C, opm);
232 if (retval & OPERATOR_FINISHED) {
233 MacroData *md = op->customdata;
234 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
236 break; /* operator didn't finish, end macro */
240 return wm_macro_end(op, retval);
243 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
246 return wm_macro_invoke_internal(C, op, event, op->macro.first);
249 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
251 wmOperator *opm = op->opm;
252 int retval= OPERATOR_FINISHED;
255 printf("macro error, calling NULL modal()\n");
257 retval = opm->type->modal(C, opm, event);
259 /* if this one is done but it's not the last operator in the macro */
260 if ((retval & OPERATOR_FINISHED) && opm->next) {
261 MacroData *md = op->customdata;
263 md->retval = OPERATOR_FINISHED; /* keep in mind that at least one operator finished */
265 retval = wm_macro_invoke_internal(C, op, event, opm->next);
267 /* if new operator is modal and also added its own handler */
268 if (retval & OPERATOR_RUNNING_MODAL && op->opm != opm) {
269 wmWindow *win = CTX_wm_window(C);
270 wmEventHandler *handler = NULL;
272 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
273 /* first handler in list is the new one */
274 if (handler->op == op)
279 BLI_remlink(&win->modalhandlers, handler);
280 wm_event_free_handler(handler);
283 /* if operator is blocking, grab cursor
284 * This may end up grabbing twice, but we don't care.
286 if(op->opm->type->flag & OPTYPE_BLOCKING) {
287 int bounds[4] = {-1,-1,-1,-1};
288 int wrap = (U.uiflag & USER_CONTINUOUS_MOUSE) && ((op->opm->flag & OP_GRAB_POINTER) || (op->opm->type->flag & OPTYPE_GRAB_POINTER));
291 ARegion *ar= CTX_wm_region(C);
293 bounds[0]= ar->winrct.xmin;
294 bounds[1]= ar->winrct.ymax;
295 bounds[2]= ar->winrct.xmax;
296 bounds[3]= ar->winrct.ymin;
300 WM_cursor_grab(CTX_wm_window(C), wrap, FALSE, bounds);
306 return wm_macro_end(op, retval);
309 static int wm_macro_cancel(bContext *C, wmOperator *op)
311 /* call cancel on the current modal operator, if any */
312 if (op->opm && op->opm->type->cancel) {
313 op->opm->type->cancel(C, op->opm);
316 return wm_macro_end(op, OPERATOR_CANCELLED);
319 /* Names have to be static for now */
320 wmOperatorType *WM_operatortype_append_macro(const char *idname, const char *name, int flag)
324 if(WM_operatortype_find(idname, TRUE)) {
325 printf("Macro error: operator %s exists\n", idname);
329 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
330 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
334 ot->flag= OPTYPE_MACRO|flag;
336 ot->exec= wm_macro_exec;
337 ot->invoke= wm_macro_invoke;
338 ot->modal= wm_macro_modal;
339 ot->cancel= wm_macro_cancel;
343 ot->description= "(undocumented operator)";
345 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.
346 RNA_def_struct_identifier(ot->srna, ot->idname);
348 BLI_addtail(&global_ops, ot);
353 void WM_operatortype_append_macro_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
357 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
358 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
360 ot->flag= OPTYPE_MACRO;
361 ot->exec= wm_macro_exec;
362 ot->invoke= wm_macro_invoke;
363 ot->modal= wm_macro_modal;
364 ot->cancel= wm_macro_cancel;
368 ot->description= "(undocumented operator)";
370 opfunc(ot, userdata);
372 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description);
373 RNA_def_struct_identifier(ot->srna, ot->idname);
375 BLI_addtail(&global_ops, ot);
378 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
380 wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
382 BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
384 /* do this on first use, since operatordefinitions might have been not done yet */
385 WM_operator_properties_alloc(&(otmacro->ptr), &(otmacro->properties), idname);
386 WM_operator_properties_sanitize(otmacro->ptr, 1);
388 BLI_addtail(&ot->macro, otmacro);
391 wmOperatorType *otsub = WM_operatortype_find(idname, 0);
392 RNA_def_pointer_runtime(ot->srna, otsub->idname, otsub->srna,
393 otsub->name, otsub->description);
399 static void wm_operatortype_free_macro(wmOperatorType *ot)
401 wmOperatorTypeMacro *otmacro;
403 for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
405 WM_operator_properties_free(otmacro->ptr);
406 MEM_freeN(otmacro->ptr);
409 BLI_freelistN(&ot->macro);
413 int WM_operatortype_remove(const char *idname)
415 wmOperatorType *ot = WM_operatortype_find(idname, 0);
420 BLI_remlink(&global_ops, ot);
421 RNA_struct_free(&BLENDER_RNA, ot->srna);
424 wm_operatortype_free_macro(ot);
431 /* SOME_OT_op -> some.op */
432 void WM_operator_py_idname(char *to, const char *from)
434 char *sep= strstr(from, "_OT_");
436 int i, ofs= (sep-from);
439 to[i]= tolower(from[i]);
442 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
445 /* should not happen but support just incase */
446 BLI_strncpy(to, from, OP_MAX_TYPENAME);
450 /* some.op -> SOME_OT_op */
451 void WM_operator_bl_idname(char *to, const char *from)
454 char *sep= strchr(from, '.');
457 int i, ofs= (sep-from);
460 to[i]= toupper(from[i]);
462 BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
463 BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
466 /* should not happen but support just incase */
467 BLI_strncpy(to, from, OP_MAX_TYPENAME);
474 /* print a string representation of the operator, with the args that it runs
475 * so python can run it again,
477 * When calling from an existing wmOperator do.
478 * WM_operator_pystring(op->type, op->ptr);
480 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
482 const char *arg_name= NULL;
483 char idname_py[OP_MAX_TYPENAME];
485 PropertyRNA *prop, *iterprop;
487 /* for building the string */
488 DynStr *dynstr= BLI_dynstr_new();
490 int first_iter=1, ok= 1;
493 /* only to get the orginal props for comparisons */
494 PointerRNA opptr_default;
495 PropertyRNA *prop_default;
497 if(all_args==0 || opptr==NULL) {
498 WM_operator_properties_create_ptr(&opptr_default, ot);
501 opptr = &opptr_default;
505 WM_operator_py_idname(idname_py, ot->idname);
506 BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
508 iterprop= RNA_struct_iterator_property(opptr->type);
510 RNA_PROP_BEGIN(opptr, propptr, iterprop) {
512 arg_name= RNA_property_identifier(prop);
514 if (strcmp(arg_name, "rna_type")==0) continue;
516 buf= RNA_property_as_string(C, opptr, prop);
521 /* not verbose, so only add in attributes that use non-default values
522 * slow but good for tooltips */
523 prop_default= RNA_struct_find_property(&opptr_default, arg_name);
526 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
528 if(strcmp(buf, buf_default)==0)
529 ok= 0; /* values match, dont bother printing */
531 MEM_freeN(buf_default);
536 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
545 if(all_args==0 || opptr==&opptr_default )
546 WM_operator_properties_free(&opptr_default);
548 BLI_dynstr_append(dynstr, ")");
550 cstring = BLI_dynstr_get_cstring(dynstr);
551 BLI_dynstr_free(dynstr);
555 void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
557 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
560 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
562 wmOperatorType *ot= WM_operatortype_find(opstring, 0);
565 WM_operator_properties_create_ptr(ptr, ot);
567 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
570 /* similar to the function above except its uses ID properties
571 * used for keymaps and macros */
572 void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
574 if(*properties==NULL) {
575 IDPropertyTemplate val = {0};
576 *properties= IDP_New(IDP_GROUP, val, "wmOpItemProp");
580 *ptr= MEM_callocN(sizeof(PointerRNA), "wmOpItemPtr");
581 WM_operator_properties_create(*ptr, opstring);
584 (*ptr)->data= *properties;
588 void WM_operator_properties_sanitize(PointerRNA *ptr, const short no_context)
590 RNA_STRUCT_BEGIN(ptr, prop) {
591 switch(RNA_property_type(prop)) {
594 RNA_def_property_flag(prop, PROP_ENUM_NO_CONTEXT);
596 RNA_def_property_clear_flag(prop, PROP_ENUM_NO_CONTEXT);
600 StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
602 /* recurse into operator properties */
603 if (RNA_struct_is_a(ptype, &RNA_OperatorProperties)) {
604 PointerRNA opptr = RNA_property_pointer_get(ptr, prop);
605 WM_operator_properties_sanitize(&opptr, no_context);
616 void WM_operator_properties_free(PointerRNA *ptr)
618 IDProperty *properties= ptr->data;
621 IDP_FreeProperty(properties);
622 MEM_freeN(properties);
623 ptr->data= NULL; /* just incase */
627 /* ************ default op callbacks, exported *********** */
629 /* invoke callback, uses enum property named "type" */
630 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
632 PropertyRNA *prop= op->type->prop;
637 printf("WM_menu_invoke: %s has no enum property set\n", op->type->idname);
639 else if (RNA_property_type(prop) != PROP_ENUM) {
640 printf("WM_menu_invoke: %s \"%s\" is not an enum property\n", op->type->idname, RNA_property_identifier(prop));
642 else if (RNA_property_is_set(op->ptr, RNA_property_identifier(prop))) {
643 return op->type->exec(C, op);
646 pup= uiPupMenuBegin(C, op->type->name, 0);
647 layout= uiPupMenuLayout(pup);
648 uiItemsFullEnumO(layout, op->type->idname, (char*)RNA_property_identifier(prop), op->ptr->data, WM_OP_EXEC_REGION_WIN, 0);
649 uiPupMenuEnd(C, pup);
652 return OPERATOR_CANCELLED;
656 /* generic enum search invoke popup */
657 static void operator_enum_search_cb(const struct bContext *C, void *arg_ot, const char *str, uiSearchItems *items)
659 wmOperatorType *ot = (wmOperatorType *)arg_ot;
660 PropertyRNA *prop= ot->prop;
663 printf("WM_enum_search_invoke: %s has no enum property set\n", ot->idname);
665 else if (RNA_property_type(prop) != PROP_ENUM) {
666 printf("WM_enum_search_invoke: %s \"%s\" is not an enum property\n", ot->idname, RNA_property_identifier(prop));
671 EnumPropertyItem *item, *item_array;
674 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
675 RNA_property_enum_items((bContext *)C, &ptr, prop, &item_array, NULL, &do_free);
677 for(item= item_array; item->identifier; item++) {
678 /* note: need to give the intex rather then the dientifier because the enum can be freed */
679 if(BLI_strcasestr(item->name, str))
680 if(0==uiSearchItemAdd(items, item->name, SET_INT_IN_POINTER(item->value), 0))
685 MEM_freeN(item_array);
689 static void operator_enum_call_cb(struct bContext *C, void *arg1, void *arg2)
691 wmOperatorType *ot= arg1;
694 PointerRNA props_ptr;
695 WM_operator_properties_create_ptr(&props_ptr, ot);
696 RNA_property_enum_set(&props_ptr, ot->prop, GET_INT_FROM_POINTER(arg2));
697 WM_operator_name_call(C, ot->idname, WM_OP_EXEC_DEFAULT, &props_ptr);
698 WM_operator_properties_free(&props_ptr);
702 static uiBlock *wm_enum_search_menu(bContext *C, ARegion *ar, void *arg_op)
704 static char search[256]= "";
706 wmWindow *win= CTX_wm_window(C);
709 wmOperator *op= (wmOperator *)arg_op;
711 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
712 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
714 //uiDefBut(block, LABEL, 0, op->type->name, 10, 10, 180, 19, NULL, 0.0, 0.0, 0, 0, ""); // ok, this isnt so easy...
715 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
716 uiButSetSearchFunc(but, operator_enum_search_cb, op->type, operator_enum_call_cb, NULL);
718 /* fake button, it holds space for search items */
719 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
721 uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
722 uiEndBlock(C, block);
724 event= *(win->eventstate); /* XXX huh huh? make api call */
725 event.type= EVT_BUT_OPEN;
727 event.customdata= but;
728 event.customdatafree= FALSE;
729 wm_event_add(win, &event);
735 int WM_enum_search_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
737 uiPupBlock(C, wm_enum_search_menu, op);
738 return OPERATOR_CANCELLED;
741 /* Can't be used as an invoke directly, needs message arg (can be NULL) */
742 int WM_operator_confirm_message(bContext *C, wmOperator *op, const char *message)
746 IDProperty *properties= op->ptr->data;
748 if(properties && properties->len)
749 properties= IDP_CopyProperty(op->ptr->data);
753 pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
754 layout= uiPupMenuLayout(pup);
755 uiItemFullO(layout, op->type->idname, message, 0, properties, WM_OP_EXEC_REGION_WIN, 0);
756 uiPupMenuEnd(C, pup);
758 return OPERATOR_CANCELLED;
762 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
764 return WM_operator_confirm_message(C, op, NULL);
767 /* op->invoke, opens fileselect if path property not set, otherwise executes */
768 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
770 if (RNA_property_is_set(op->ptr, "filepath")) {
771 return WM_operator_call(C, op);
774 WM_event_add_fileselect(C, op);
775 return OPERATOR_RUNNING_MODAL;
779 /* default properties for fileselect */
780 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag)
785 if(flag & WM_FILESEL_FILEPATH)
786 RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "File Path", "Path to file");
788 if(flag & WM_FILESEL_DIRECTORY)
789 RNA_def_string_dir_path(ot->srna, "directory", "", FILE_MAX, "Directory", "Directory of the file");
791 if(flag & WM_FILESEL_FILENAME)
792 RNA_def_string_file_name(ot->srna, "filename", "", FILE_MAX, "File Name", "Name of the file");
794 if (action == FILE_SAVE) {
795 prop= RNA_def_boolean(ot->srna, "check_existing", 1, "Check Existing", "Check and warn on overwriting existing files");
796 RNA_def_property_flag(prop, PROP_HIDDEN);
799 prop= RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
800 RNA_def_property_flag(prop, PROP_HIDDEN);
801 prop= RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
802 RNA_def_property_flag(prop, PROP_HIDDEN);
803 prop= RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
804 RNA_def_property_flag(prop, PROP_HIDDEN);
805 prop= RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
806 RNA_def_property_flag(prop, PROP_HIDDEN);
807 prop= RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
808 RNA_def_property_flag(prop, PROP_HIDDEN);
809 prop= RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
810 RNA_def_property_flag(prop, PROP_HIDDEN);
811 prop= RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
812 RNA_def_property_flag(prop, PROP_HIDDEN);
813 prop= RNA_def_boolean(ot->srna, "filter_btx", (filter & BTXFILE), "Filter btx files", "");
814 RNA_def_property_flag(prop, PROP_HIDDEN);
815 prop= RNA_def_boolean(ot->srna, "filter_collada", (filter & COLLADAFILE), "Filter COLLADA files", "");
816 RNA_def_property_flag(prop, PROP_HIDDEN);
817 prop= RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
818 RNA_def_property_flag(prop, PROP_HIDDEN);
820 prop= RNA_def_int(ot->srna, "filemode", type, FILE_LOADLIB, FILE_SPECIAL,
821 "File Browser Mode", "The setting for the file browser mode to load a .blend file, a library or a special file",
822 FILE_LOADLIB, FILE_SPECIAL);
823 RNA_def_property_flag(prop, PROP_HIDDEN);
825 if(flag & WM_FILESEL_RELPATH)
826 RNA_def_boolean(ot->srna, "relative_path", (U.flag & USER_RELPATHS) ? 1:0, "Relative Path", "Select the file relative to the blend file");
829 void WM_operator_properties_select_all(wmOperatorType *ot) {
830 static EnumPropertyItem select_all_actions[] = {
831 {SEL_TOGGLE, "TOGGLE", 0, "Toggle", "Toggle selection for all elements"},
832 {SEL_SELECT, "SELECT", 0, "Select", "Select all elements"},
833 {SEL_DESELECT, "DESELECT", 0, "Deselect", "Deselect all elements"},
834 {SEL_INVERT, "INVERT", 0, "Invert", "Invert selection of all elements"},
835 {0, NULL, 0, NULL, NULL}
838 RNA_def_enum(ot->srna, "action", select_all_actions, SEL_TOGGLE, "Action", "Selection action to execute");
841 void WM_operator_properties_gesture_border(wmOperatorType *ot, int extend)
843 RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
844 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
845 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
846 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
847 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
850 RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
853 void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
855 RNA_def_int(ot->srna, "xstart", 0, INT_MIN, INT_MAX, "X Start", "", INT_MIN, INT_MAX);
856 RNA_def_int(ot->srna, "xend", 0, INT_MIN, INT_MAX, "X End", "", INT_MIN, INT_MAX);
857 RNA_def_int(ot->srna, "ystart", 0, INT_MIN, INT_MAX, "Y Start", "", INT_MIN, INT_MAX);
858 RNA_def_int(ot->srna, "yend", 0, INT_MIN, INT_MAX, "Y End", "", INT_MIN, INT_MAX);
861 RNA_def_int(ot->srna, "cursor", cursor, 0, INT_MAX, "Cursor", "Mouse cursor style to use during the modal operator", 0, INT_MAX);
866 int WM_operator_winactive(bContext *C)
868 if(CTX_wm_window(C)==NULL) return 0;
872 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
874 wmWindowManager *wm= CTX_wm_manager(C);
875 wmOperator *op= arg_op;
879 uiStyle *style= U.uistyles.first;
880 int columns= 2, width= 300;
883 block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
884 uiBlockClearFlag(block, UI_BLOCK_LOOP);
885 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
887 /* if register is not enabled, the operator gets freed on OPERATOR_FINISHED
888 * ui_apply_but_funcs_after calls ED_undo_operator_repeate_cb and crashes */
889 assert(op->type->flag & OPTYPE_REGISTER);
891 uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, arg_op);
893 if(!op->properties) {
894 IDPropertyTemplate val = {0};
895 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
898 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
899 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, width, 20, style);
900 uiItemL(layout, op->type->name, 0);
902 /* poll() on this operator may still fail, at the moment there is no nice feedback when this happens
903 * just fails silently */
904 if(!WM_operator_repeat_check(C, op)) {
905 uiBlockSetButLock(uiLayoutGetBlock(layout), TRUE, "Operator cannot redo");
906 uiItemL(layout, "* Redo Unsupported *", 0); // XXX, could give some nicer feedback or not show redo panel at all?
911 op->type->ui((bContext*)C, op);
915 uiDefAutoButsRNA(layout, &ptr, columns);
917 uiPopupBoundsBlock(block, 4.0f, 0, 0);
918 uiEndBlock(C, block);
923 /* Only invoked by OK button in popups created with wm_block_create_dialog() */
924 static void dialog_exec_cb(bContext *C, void *arg1, void *arg2)
926 wmOperator *op= arg1;
927 uiBlock *block= arg2;
929 WM_operator_call(C, op);
931 uiPupBlockClose(C, block);
934 void dialog_check_cb(bContext *C, void *op_ptr, void *UNUSED(arg))
936 wmOperator *op= op_ptr;
937 if(op->type->check) {
938 if(op->type->check(C, op)) {
944 /* Dialogs are popups that require user verification (click OK) before exec */
945 static uiBlock *wm_block_create_dialog(bContext *C, ARegion *ar, void *userData)
947 struct { wmOperator *op; int width; int height; } * data = userData;
948 wmWindowManager *wm= CTX_wm_manager(C);
949 wmOperator *op= data->op;
954 uiStyle *style= U.uistyles.first;
957 block = uiBeginBlock(C, ar, "operator dialog", UI_EMBOSS);
958 uiBlockClearFlag(block, UI_BLOCK_LOOP);
959 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
961 if (!op->properties) {
962 IDPropertyTemplate val = {0};
963 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
966 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
967 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
968 uiItemL(layout, op->type->name, 0);
970 uiBlockSetFunc(block, dialog_check_cb, op, NULL);
974 op->type->ui((bContext*)C, op);
978 uiDefAutoButsRNA(layout, &ptr, columns);
980 uiBlockSetFunc(block, NULL, NULL, NULL);
982 /* Create OK button, the callback of which will execute op */
983 btn= uiDefBut(block, BUT, 0, "OK", 0, 0, 0, 20, NULL, 0, 0, 0, 0, "");
984 uiButSetFunc(btn, dialog_exec_cb, op, block);
986 /* center around the mouse */
987 uiPopupBoundsBlock(block, 4.0f, data->width/-2, data->height/2);
988 uiEndBlock(C, block);
993 static uiBlock *wm_operator_create_ui(bContext *C, ARegion *ar, void *userData)
995 struct { wmOperator *op; int width; int height; } * data = userData;
996 wmWindowManager *wm= CTX_wm_manager(C);
997 wmOperator *op= data->op;
1001 uiStyle *style= U.uistyles.first;
1003 block= uiBeginBlock(C, ar, "opui_popup", UI_EMBOSS);
1004 uiBlockClearFlag(block, UI_BLOCK_LOOP);
1005 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1007 if(!op->properties) {
1008 IDPropertyTemplate val = {0};
1009 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
1012 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1013 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, data->width, data->height, style);
1017 op->type->ui((bContext*)C, op);
1021 uiPopupBoundsBlock(block, 4.0f, 0, 0);
1022 uiEndBlock(C, block);
1027 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1029 int retval= OPERATOR_CANCELLED;
1031 if((op->type->flag & OPTYPE_REGISTER)==0) {
1032 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1033 return OPERATOR_CANCELLED;
1036 if(op->type->exec) {
1037 retval= op->type->exec(C, op);
1039 /* ED_undo_push_op(C, op), called by wm_operator_finished now. */
1042 if(retval != OPERATOR_CANCELLED)
1043 uiPupBlock(C, wm_block_create_redo, op);
1048 int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, int height)
1050 struct { wmOperator *op; int width; int height; } data;
1054 data.height= height;
1056 /* op is not executed until popup OK but is clicked */
1057 uiPupBlock(C, wm_block_create_dialog, &data);
1059 return OPERATOR_RUNNING_MODAL;
1062 int WM_operator_ui_popup(bContext *C, wmOperator *op, int width, int height)
1064 struct { wmOperator *op; int width; int height; } data;
1067 data.height = height;
1068 uiPupBlock(C, wm_operator_create_ui, &data);
1069 return OPERATOR_RUNNING_MODAL;
1072 int WM_operator_redo_popup(bContext *C, wmOperator *op)
1074 if((op->type->flag & OPTYPE_REGISTER)==0) {
1075 BKE_reportf(op->reports, RPT_ERROR, "Operator '%s' does not have register enabled, incorrect invoke function.", op->type->idname);
1076 return OPERATOR_CANCELLED;
1079 uiPupBlock(C, wm_block_create_redo, op);
1081 return OPERATOR_CANCELLED;
1084 /* ***************** Debug menu ************************* */
1086 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
1088 wmOperator *op= arg_op;
1091 uiStyle *style= U.uistyles.first;
1093 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1094 uiBlockClearFlag(block, UI_BLOCK_LOOP);
1095 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1097 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
1098 uiItemL(layout, op->type->name, 0);
1102 op->type->ui(C, op);
1106 uiDefAutoButsRNA(layout, op->ptr, 2);
1108 uiPopupBoundsBlock(block, 4.0f, 0, 0);
1109 uiEndBlock(C, block);
1114 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
1116 G.rt= RNA_int_get(op->ptr, "debugval");
1117 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
1118 WM_event_add_notifier(C, NC_WINDOW, NULL);
1120 return OPERATOR_FINISHED;
1123 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1126 RNA_int_set(op->ptr, "debugval", G.rt);
1128 /* pass on operator, so return modal */
1129 uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
1131 return OPERATOR_RUNNING_MODAL;
1134 static void WM_OT_debug_menu(wmOperatorType *ot)
1136 ot->name= "Debug Menu";
1137 ot->idname= "WM_OT_debug_menu";
1138 ot->description= "Open a popup to set the debug level";
1140 ot->invoke= wm_debug_menu_invoke;
1141 ot->exec= wm_debug_menu_exec;
1142 ot->poll= WM_operator_winactive;
1144 RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
1148 /* ***************** Splash Screen ************************* */
1150 static void wm_block_splash_close(bContext *C, void *arg_block, void *UNUSED(arg))
1152 uiPupBlockClose(C, arg_block);
1155 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unused);
1157 /* XXX: hack to refresh splash screen with updated prest menu name,
1158 * since popup blocks don't get regenerated like panels do */
1159 void wm_block_splash_refreshmenu (bContext *UNUSED(C), void *UNUSED(arg_block), void *UNUSED(arg))
1161 /* ugh, causes crashes in other buttons, disabling for now until
1163 uiPupBlockClose(C, arg_block);
1164 uiPupBlock(C, wm_block_create_splash, NULL);
1168 static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *UNUSED(arg))
1172 uiLayout *layout, *split, *col;
1173 uiStyle *style= U.uistyles.first;
1174 struct RecentFile *recent;
1176 MenuType *mt= WM_menutype_find("USERPREF_MT_splash", TRUE);
1179 #ifdef NAN_BUILDINFO
1180 int ver_width, rev_width;
1181 char *version_str = NULL;
1182 char *revision_str = NULL;
1183 char version_buf[128];
1184 char revision_buf[128];
1185 extern char build_rev[];
1187 version_str = &version_buf[0];
1188 revision_str = &revision_buf[0];
1190 sprintf(version_str, "%d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1191 sprintf(revision_str, "r%s", build_rev);
1193 BLF_size(style->widgetlabel.uifont_id, style->widgetlabel.points, U.dpi);
1194 ver_width = BLF_width(style->widgetlabel.uifont_id, version_str)+5;
1195 rev_width = BLF_width(style->widgetlabel.uifont_id, revision_str)+5;
1196 #endif //NAN_BUILDINFO
1198 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1199 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1201 but= uiDefBut(block, BUT_IMAGE, 0, "", 0, 10, 501, 282, NULL, 0.0, 0.0, 0, 0, "");
1202 uiButSetFunc(but, wm_block_splash_close, block, NULL);
1203 uiBlockSetFunc(block, wm_block_splash_refreshmenu, block, NULL);
1205 #ifdef NAN_BUILDINFO
1206 uiDefBut(block, LABEL, 0, version_str, 494-ver_width, 282-24, ver_width, 20, NULL, 0, 0, 0, 0, NULL);
1207 uiDefBut(block, LABEL, 0, revision_str, 494-rev_width, 282-36, rev_width, 20, NULL, 0, 0, 0, 0, NULL);
1208 #endif //NAN_BUILDINFO
1210 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 10, 2, 480, 110, style);
1212 uiBlockSetEmboss(block, UI_EMBOSS);
1213 /* show the splash menu (containing interaction presets), using python */
1216 menu.layout= layout;
1220 // wmWindowManager *wm= CTX_wm_manager(C);
1221 // uiItemM(layout, C, "USERPREF_MT_keyconfigs", U.keyconfigstr, 0);
1224 uiBlockSetEmboss(block, UI_EMBOSSP);
1225 uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1227 split = uiLayoutSplit(layout, 0, 0);
1228 col = uiLayoutColumn(split, 0);
1229 uiItemL(col, "Links", 0);
1230 uiItemStringO(col, "Donations", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/blenderorg/blender-foundation/donation-payment/");
1231 uiItemStringO(col, "Release Log", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/development/release-logs/blender-254-beta/");
1232 uiItemStringO(col, "Manual", ICON_URL, "WM_OT_url_open", "url", "http://wiki.blender.org/index.php/Doc:Manual");
1233 uiItemStringO(col, "Blender Website", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/");
1234 uiItemStringO(col, "User Community", ICON_URL, "WM_OT_url_open", "url", "http://www.blender.org/community/user-community/"); //
1235 BLI_snprintf(url, sizeof(url), "http://www.blender.org/documentation/blender_python_api_%d_%d_%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
1236 uiItemStringO(col, "Python API Reference", ICON_URL, "WM_OT_url_open", "url", url);
1237 uiItemL(col, "", 0);
1239 col = uiLayoutColumn(split, 0);
1240 uiItemL(col, "Recent", 0);
1241 for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
1242 uiItemStringO(col, BLI_path_basename(recent->filepath), ICON_FILE_BLEND, "WM_OT_open_mainfile", "filepath", recent->filepath);
1245 uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");
1246 uiItemL(col, "", 0);
1248 uiCenteredBoundsBlock(block, 0.0f);
1249 uiEndBlock(C, block);
1254 static int wm_splash_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
1256 uiPupBlock(C, wm_block_create_splash, NULL);
1258 return OPERATOR_FINISHED;
1261 static void WM_OT_splash(wmOperatorType *ot)
1263 ot->name= "Splash Screen";
1264 ot->idname= "WM_OT_splash";
1265 ot->description= "Opens a blocking popup region with release info";
1267 ot->invoke= wm_splash_invoke;
1268 ot->poll= WM_operator_winactive;
1272 /* ***************** Search menu ************************* */
1273 static void operator_call_cb(struct bContext *C, void *UNUSED(arg1), void *arg2)
1275 wmOperatorType *ot= arg2;
1278 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1281 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
1283 wmOperatorType *ot = WM_operatortype_first();
1285 for(; ot; ot= ot->next) {
1287 if(BLI_strcasestr(ot->name, str)) {
1288 if(WM_operator_poll((bContext*)C, ot)) {
1290 int len= strlen(ot->name);
1292 /* display name for menu, can hold hotkey */
1293 BLI_strncpy(name, ot->name, 256);
1295 /* check for hotkey */
1297 if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1301 if(0==uiSearchItemAdd(items, name, ot, 0))
1308 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *UNUSED(arg_op))
1310 static char search[256]= "";
1312 wmWindow *win= CTX_wm_window(C);
1316 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1317 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1|UI_BLOCK_MOVEMOUSE_QUIT);
1319 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, 0, 0, "");
1320 uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1322 /* fake button, it holds space for search items */
1323 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
1325 uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
1326 uiEndBlock(C, block);
1328 event= *(win->eventstate); /* XXX huh huh? make api call */
1329 event.type= EVT_BUT_OPEN;
1330 event.val= KM_PRESS;
1331 event.customdata= but;
1332 event.customdatafree= FALSE;
1333 wm_event_add(win, &event);
1338 static int wm_search_menu_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
1340 return OPERATOR_FINISHED;
1343 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1345 uiPupBlock(C, wm_block_search_menu, op);
1347 return OPERATOR_CANCELLED;
1351 static int wm_search_menu_poll(bContext *C)
1353 if(CTX_wm_window(C)==NULL) return 0;
1354 if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0; // XXX - so we can use the shortcut in the console
1355 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
1356 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
1360 static void WM_OT_search_menu(wmOperatorType *ot)
1362 ot->name= "Search Menu";
1363 ot->idname= "WM_OT_search_menu";
1365 ot->invoke= wm_search_menu_invoke;
1366 ot->exec= wm_search_menu_exec;
1367 ot->poll= wm_search_menu_poll;
1370 static int wm_call_menu_exec(bContext *C, wmOperator *op)
1372 char idname[BKE_ST_MAXNAME];
1373 RNA_string_get(op->ptr, "name", idname);
1375 uiPupMenuInvoke(C, idname);
1377 return OPERATOR_CANCELLED;
1380 static void WM_OT_call_menu(wmOperatorType *ot)
1382 ot->name= "Call Menu";
1383 ot->idname= "WM_OT_call_menu";
1385 ot->exec= wm_call_menu_exec;
1387 RNA_def_string(ot->srna, "name", "", BKE_ST_MAXNAME, "Name", "Name of the menu");
1390 /* ************ window / screen operator definitions ************** */
1392 /* this poll functions is needed in place of WM_operator_winactive
1393 * while it crashes on full screen */
1394 static int wm_operator_winactive_normal(bContext *C)
1396 wmWindow *win= CTX_wm_window(C);
1398 if(win==NULL || win->screen==NULL || win->screen->full != SCREENNORMAL)
1404 static void WM_OT_window_duplicate(wmOperatorType *ot)
1406 ot->name= "Duplicate Window";
1407 ot->idname= "WM_OT_window_duplicate";
1408 ot->description="Duplicate the current Blender window";
1410 ot->exec= wm_window_duplicate_exec;
1411 ot->poll= wm_operator_winactive_normal;
1414 static void WM_OT_save_homefile(wmOperatorType *ot)
1416 ot->name= "Save User Settings";
1417 ot->idname= "WM_OT_save_homefile";
1418 ot->description="Make the current file the default .blend file";
1420 ot->invoke= WM_operator_confirm;
1421 ot->exec= WM_write_homefile;
1422 ot->poll= WM_operator_winactive;
1425 static void WM_OT_read_homefile(wmOperatorType *ot)
1427 ot->name= "Reload Start-Up File";
1428 ot->idname= "WM_OT_read_homefile";
1429 ot->description="Open the default file (doesn't save the current file)";
1431 ot->invoke= WM_operator_confirm;
1432 ot->exec= WM_read_homefile;
1433 ot->poll= WM_operator_winactive;
1436 static void WM_OT_read_factory_settings(wmOperatorType *ot)
1438 ot->name= "Load Factory Settings";
1439 ot->idname= "WM_OT_read_factory_settings";
1440 ot->description="Load default file and user preferences";
1442 ot->invoke= WM_operator_confirm;
1443 ot->exec= WM_read_homefile;
1444 ot->poll= WM_operator_winactive;
1447 /* *************** open file **************** */
1449 static void open_set_load_ui(wmOperator *op)
1451 if(!RNA_property_is_set(op->ptr, "load_ui"))
1452 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
1455 static void open_set_use_scripts(wmOperator *op)
1457 if(!RNA_property_is_set(op->ptr, "use_scripts"))
1458 RNA_boolean_set(op->ptr, "use_scripts", !(U.flag & USER_SCRIPT_AUTOEXEC_DISABLE));
1461 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1463 const char *openname= G.main->name;
1465 /* if possible, get the name of the most recently used .blend file */
1466 if (G.recent_files.first) {
1467 struct RecentFile *recent = G.recent_files.first;
1468 openname = recent->filepath;
1471 RNA_string_set(op->ptr, "filepath", openname);
1472 open_set_load_ui(op);
1473 open_set_use_scripts(op);
1475 WM_event_add_fileselect(C, op);
1477 return OPERATOR_RUNNING_MODAL;
1480 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
1482 char path[FILE_MAX];
1484 RNA_string_get(op->ptr, "filepath", path);
1485 open_set_load_ui(op);
1486 open_set_use_scripts(op);
1488 if(RNA_boolean_get(op->ptr, "load_ui"))
1489 G.fileflags &= ~G_FILE_NO_UI;
1491 G.fileflags |= G_FILE_NO_UI;
1493 if(RNA_boolean_get(op->ptr, "use_scripts"))
1494 G.f |= G_SCRIPT_AUTOEXEC;
1496 G.f &= ~G_SCRIPT_AUTOEXEC;
1498 // XXX wm in context is not set correctly after WM_read_file -> crash
1499 // do it before for now, but is this correct with multiple windows?
1500 WM_event_add_notifier(C, NC_WINDOW, NULL);
1502 WM_read_file(C, path, op->reports);
1504 return OPERATOR_FINISHED;
1507 static void WM_OT_open_mainfile(wmOperatorType *ot)
1509 ot->name= "Open Blender File";
1510 ot->idname= "WM_OT_open_mainfile";
1511 ot->description="Open a Blender file";
1513 ot->invoke= wm_open_mainfile_invoke;
1514 ot->exec= wm_open_mainfile_exec;
1515 ot->poll= WM_operator_winactive;
1517 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1519 RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file");
1520 RNA_def_boolean(ot->srna, "use_scripts", 1, "Trusted Source", "Allow blend file execute scripts automatically, default available from system preferences");
1523 /* **************** link/append *************** */
1525 static int wm_link_append_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1527 if(!RNA_property_is_set(op->ptr, "relative_path"))
1528 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1530 if(RNA_property_is_set(op->ptr, "filepath")) {
1531 return WM_operator_call(C, op);
1534 /* XXX TODO solve where to get last linked library from */
1535 RNA_string_set(op->ptr, "filepath", G.lib);
1536 WM_event_add_fileselect(C, op);
1537 return OPERATOR_RUNNING_MODAL;
1541 static short wm_link_append_flag(wmOperator *op)
1545 if(RNA_boolean_get(op->ptr, "autoselect")) flag |= FILE_AUTOSELECT;
1546 if(RNA_boolean_get(op->ptr, "active_layer")) flag |= FILE_ACTIVELAY;
1547 if(RNA_boolean_get(op->ptr, "relative_path")) flag |= FILE_RELPATH;
1548 if(RNA_boolean_get(op->ptr, "link")) flag |= FILE_LINK;
1549 if(RNA_boolean_get(op->ptr, "instance_groups")) flag |= FILE_GROUP_INSTANCE;
1554 static void wm_link_make_library_local(Main *main, const char *libname)
1558 /* and now find the latest append lib file */
1559 for(lib= main->library.first; lib; lib=lib->id.next)
1560 if(BLI_streq(libname, lib->filepath))
1569 static int wm_link_append_exec(bContext *C, wmOperator *op)
1571 Main *bmain= CTX_data_main(C);
1572 Scene *scene= CTX_data_scene(C);
1576 char name[FILE_MAX], dir[FILE_MAX], libname[FILE_MAX], group[GROUP_MAX];
1577 int idcode, totfiles=0;
1581 RNA_string_get(op->ptr, "filename", name);
1582 RNA_string_get(op->ptr, "directory", dir);
1584 /* test if we have a valid data */
1585 if(BLO_is_a_library(dir, libname, group) == 0) {
1586 BKE_report(op->reports, RPT_ERROR, "Not a library");
1587 return OPERATOR_CANCELLED;
1589 else if(group[0] == 0) {
1590 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1591 return OPERATOR_CANCELLED;
1593 else if(BLI_streq(bmain->name, libname)) {
1594 BKE_report(op->reports, RPT_ERROR, "Cannot use current file as library");
1595 return OPERATOR_CANCELLED;
1598 /* check if something is indicated for append/link */
1599 prop = RNA_struct_find_property(op->ptr, "files");
1601 totfiles= RNA_property_collection_length(op->ptr, prop);
1603 if(name[0] == '\0') {
1604 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1605 return OPERATOR_CANCELLED;
1609 else if(name[0] == '\0') {
1610 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
1611 return OPERATOR_CANCELLED;
1614 /* now we have or selected, or an indicated file */
1615 if(RNA_boolean_get(op->ptr, "autoselect"))
1616 scene_deselect_all(scene);
1618 bh = BLO_blendhandle_from_file(libname);
1619 idcode = BKE_idcode_from_name(group);
1621 flag = wm_link_append_flag(op);
1623 /* sanity checks for flag */
1624 if(scene->id.lib && (flag & FILE_GROUP_INSTANCE)) {
1625 /* TODO, user never gets this message */
1626 BKE_reportf(op->reports, RPT_WARNING, "Scene '%s' is linked, group instance disabled", scene->id.name+2);
1627 flag &= ~FILE_GROUP_INSTANCE;
1631 /* tag everything, all untagged data can be made local
1632 * its also generally useful to know what is new
1634 * take extra care flag_all_listbases_ids(LIB_LINK_TAG, 0) is called after! */
1635 flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
1637 /* here appending/linking starts */
1638 mainl = BLO_library_append_begin(C, &bh, libname);
1640 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1643 RNA_BEGIN(op->ptr, itemptr, "files") {
1644 RNA_string_get(&itemptr, "name", name);
1645 BLO_library_append_named_part(C, mainl, &bh, name, idcode, flag);
1649 BLO_library_append_end(C, mainl, &bh, idcode, flag);
1651 /* mark all library linked objects to be updated */
1652 recalc_all_library_objects(bmain);
1654 /* append, rather than linking */
1655 if((flag & FILE_LINK)==0)
1656 wm_link_make_library_local(bmain, libname);
1658 /* important we unset, otherwise these object wont
1659 * link into other scenes from this blend file */
1660 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
1662 /* recreate dependency graph to include new objects */
1663 DAG_scene_sort(bmain, scene);
1664 DAG_ids_flush_update(bmain, 0);
1666 BLO_blendhandle_close(bh);
1668 /* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
1669 BLI_strncpy(G.lib, dir, FILE_MAX);
1671 WM_event_add_notifier(C, NC_WINDOW, NULL);
1673 return OPERATOR_FINISHED;
1676 static void WM_OT_link_append(wmOperatorType *ot)
1678 ot->name= "Link/Append from Library";
1679 ot->idname= "WM_OT_link_append";
1680 ot->description= "Link or Append from a Library .blend file";
1682 ot->invoke= wm_link_append_invoke;
1683 ot->exec= wm_link_append_exec;
1684 ot->poll= WM_operator_winactive;
1686 ot->flag |= OPTYPE_UNDO;
1688 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_LOADLIB, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_DIRECTORY|WM_FILESEL_FILENAME| WM_FILESEL_RELPATH);
1690 RNA_def_boolean(ot->srna, "link", 1, "Link", "Link the objects or datablocks rather than appending");
1691 RNA_def_boolean(ot->srna, "autoselect", 1, "Select", "Select the linked objects");
1692 RNA_def_boolean(ot->srna, "active_layer", 1, "Active Layer", "Put the linked objects on the active layer");
1693 RNA_def_boolean(ot->srna, "instance_groups", 1, "Instance Groups", "Create instances for each group as a DupliGroup");
1695 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
1698 /* *************** recover last session **************** */
1700 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
1702 char filename[FILE_MAX];
1704 G.fileflags |= G_FILE_RECOVER;
1706 // XXX wm in context is not set correctly after WM_read_file -> crash
1707 // do it before for now, but is this correct with multiple windows?
1708 WM_event_add_notifier(C, NC_WINDOW, NULL);
1711 BLI_make_file_string("/", filename, btempdir, "quit.blend");
1712 WM_read_file(C, filename, op->reports);
1714 G.fileflags &= ~G_FILE_RECOVER;
1715 return OPERATOR_FINISHED;
1718 static void WM_OT_recover_last_session(wmOperatorType *ot)
1720 ot->name= "Recover Last Session";
1721 ot->idname= "WM_OT_recover_last_session";
1722 ot->description="Open the last closed file (\"quit.blend\")";
1724 ot->exec= wm_recover_last_session_exec;
1725 ot->poll= WM_operator_winactive;
1728 /* *************** recover auto save **************** */
1730 static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
1732 char path[FILE_MAX];
1734 RNA_string_get(op->ptr, "filepath", path);
1736 G.fileflags |= G_FILE_RECOVER;
1738 // XXX wm in context is not set correctly after WM_read_file -> crash
1739 // do it before for now, but is this correct with multiple windows?
1740 WM_event_add_notifier(C, NC_WINDOW, NULL);
1743 WM_read_file(C, path, op->reports);
1745 G.fileflags &= ~G_FILE_RECOVER;
1747 return OPERATOR_FINISHED;
1750 static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1752 char filename[FILE_MAX];
1754 wm_autosave_location(filename);
1755 RNA_string_set(op->ptr, "filepath", filename);
1756 WM_event_add_fileselect(C, op);
1758 return OPERATOR_RUNNING_MODAL;
1761 static void WM_OT_recover_auto_save(wmOperatorType *ot)
1763 ot->name= "Recover Auto Save";
1764 ot->idname= "WM_OT_recover_auto_save";
1765 ot->description="Open an automatically saved file to recover it";
1767 ot->exec= wm_recover_auto_save_exec;
1768 ot->invoke= wm_recover_auto_save_invoke;
1769 ot->poll= WM_operator_winactive;
1771 WM_operator_properties_filesel(ot, BLENDERFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
1774 /* *************** save file as **************** */
1776 static void untitled(char *name)
1778 if(G.save_over == 0 && strlen(name) < FILE_MAX-16) {
1779 char *c= BLI_last_slash(name);
1782 strcpy(&c[1], "untitled.blend");
1784 strcpy(name, "untitled.blend");
1788 static void save_set_compress(wmOperator *op)
1790 if(!RNA_property_is_set(op->ptr, "compress")) {
1791 if(G.save_over) /* keep flag for existing file */
1792 RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
1793 else /* use userdef for new file */
1794 RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
1798 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1800 char name[FILE_MAX];
1802 save_set_compress(op);
1804 BLI_strncpy(name, G.main->name, FILE_MAX);
1806 RNA_string_set(op->ptr, "filepath", name);
1808 WM_event_add_fileselect(C, op);
1810 return OPERATOR_RUNNING_MODAL;
1813 /* function used for WM_OT_save_mainfile too */
1814 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
1816 char path[FILE_MAX];
1820 save_set_compress(op);
1822 if(RNA_property_is_set(op->ptr, "filepath"))
1823 RNA_string_get(op->ptr, "filepath", path);
1825 BLI_strncpy(path, G.main->name, FILE_MAX);
1829 if(RNA_property_is_set(op->ptr, "copy"))
1830 copy = RNA_boolean_get(op->ptr, "copy");
1832 fileflags= G.fileflags;
1834 /* set compression flag */
1835 if(RNA_boolean_get(op->ptr, "compress")) fileflags |= G_FILE_COMPRESS;
1836 else fileflags &= ~G_FILE_COMPRESS;
1837 if(RNA_boolean_get(op->ptr, "relative_remap")) fileflags |= G_FILE_RELATIVE_REMAP;
1838 else fileflags &= ~G_FILE_RELATIVE_REMAP;
1840 if ( WM_write_file(C, path, fileflags, op->reports, copy) != 0)
1841 return OPERATOR_CANCELLED;
1843 WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1845 return OPERATOR_FINISHED;
1848 /* function used for WM_OT_save_mainfile too */
1849 static int blend_save_check(bContext *UNUSED(C), wmOperator *op)
1851 char filepath[FILE_MAX];
1852 RNA_string_get(op->ptr, "filepath", filepath);
1853 if(BLI_replace_extension(filepath, sizeof(filepath), ".blend")) {
1854 RNA_string_set(op->ptr, "filepath", filepath);
1860 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1862 ot->name= "Save As Blender File";
1863 ot->idname= "WM_OT_save_as_mainfile";
1864 ot->description="Save the current file in the desired location";
1866 ot->invoke= wm_save_as_mainfile_invoke;
1867 ot->exec= wm_save_as_mainfile_exec;
1868 ot->check= blend_save_check;
1869 ot->poll= WM_operator_winactive;
1871 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1872 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1873 RNA_def_boolean(ot->srna, "relative_remap", 1, "Remap Relative", "Remap relative paths when saving in a different directory");
1874 RNA_def_boolean(ot->srna, "copy", 0, "Save Copy", "Save a copy of the actual working state but does not make saved file active.");
1877 /* *************** save file directly ******** */
1879 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1881 char name[FILE_MAX];
1882 int check_existing=1;
1884 /* cancel if no active window */
1885 if (CTX_wm_window(C) == NULL)
1886 return OPERATOR_CANCELLED;
1888 save_set_compress(op);
1890 BLI_strncpy(name, G.main->name, FILE_MAX);
1892 RNA_string_set(op->ptr, "filepath", name);
1894 if (RNA_struct_find_property(op->ptr, "check_existing"))
1895 if (RNA_boolean_get(op->ptr, "check_existing")==0)
1900 uiPupMenuSaveOver(C, op, name);
1902 wm_save_as_mainfile_exec(C, op);
1905 WM_event_add_fileselect(C, op);
1908 return OPERATOR_RUNNING_MODAL;
1911 static void WM_OT_save_mainfile(wmOperatorType *ot)
1913 ot->name= "Save Blender File";
1914 ot->idname= "WM_OT_save_mainfile";
1915 ot->description="Save the current Blender file";
1917 ot->invoke= wm_save_mainfile_invoke;
1918 ot->exec= wm_save_as_mainfile_exec;
1919 ot->check= blend_save_check;
1922 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1923 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file");
1924 RNA_def_boolean(ot->srna, "relative_remap", 0, "Remap Relative", "Remap relative paths when saving in a different directory");
1927 /* XXX: move these collada operators to a more appropriate place */
1930 #include "../../collada/collada.h"
1932 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1934 if(!RNA_property_is_set(op->ptr, "filepath")) {
1935 char filepath[FILE_MAX];
1936 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
1937 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
1938 RNA_string_set(op->ptr, "filepath", filepath);
1941 WM_event_add_fileselect(C, op);
1943 return OPERATOR_RUNNING_MODAL;
1946 /* function used for WM_OT_save_mainfile too */
1947 static int wm_collada_export_exec(bContext *C, wmOperator *op)
1949 char filename[FILE_MAX];
1951 if(!RNA_property_is_set(op->ptr, "filepath")) {
1952 BKE_report(op->reports, RPT_ERROR, "No filename given");
1953 return OPERATOR_CANCELLED;
1956 RNA_string_get(op->ptr, "filepath", filename);
1957 collada_export(CTX_data_scene(C), filename);
1959 return OPERATOR_FINISHED;
1962 static void WM_OT_collada_export(wmOperatorType *ot)
1964 ot->name= "Export COLLADA";
1965 ot->idname= "WM_OT_collada_export";
1967 ot->invoke= wm_collada_export_invoke;
1968 ot->exec= wm_collada_export_exec;
1969 ot->poll= WM_operator_winactive;
1971 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH);
1974 /* function used for WM_OT_save_mainfile too */
1975 static int wm_collada_import_exec(bContext *C, wmOperator *op)
1977 char filename[FILE_MAX];
1979 if(!RNA_property_is_set(op->ptr, "filepath")) {
1980 BKE_report(op->reports, RPT_ERROR, "No filename given");
1981 return OPERATOR_CANCELLED;
1984 RNA_string_get(op->ptr, "filepath", filename);
1985 collada_import(C, filename);
1987 return OPERATOR_FINISHED;
1990 static void WM_OT_collada_import(wmOperatorType *ot)
1992 ot->name= "Import COLLADA";
1993 ot->idname= "WM_OT_collada_import";
1995 ot->invoke= WM_operator_filesel;
1996 ot->exec= wm_collada_import_exec;
1997 ot->poll= WM_operator_winactive;
1999 WM_operator_properties_filesel(ot, FOLDERFILE|COLLADAFILE, FILE_BLENDER, FILE_OPENFILE, WM_FILESEL_FILEPATH);
2006 /* *********************** */
2008 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
2010 ot->name= "Toggle Fullscreen";
2011 ot->idname= "WM_OT_window_fullscreen_toggle";
2012 ot->description="Toggle the current window fullscreen";
2014 ot->exec= wm_window_fullscreen_toggle_exec;
2015 ot->poll= WM_operator_winactive;
2018 static int wm_exit_blender_op(bContext *C, wmOperator *op)
2020 WM_operator_free(op);
2024 return OPERATOR_FINISHED;
2027 static void WM_OT_quit_blender(wmOperatorType *ot)
2029 ot->name= "Quit Blender";
2030 ot->idname= "WM_OT_quit_blender";
2031 ot->description= "Quit Blender";
2033 ot->invoke= WM_operator_confirm;
2034 ot->exec= wm_exit_blender_op;
2035 ot->poll= WM_operator_winactive;
2038 /* *********************** */
2040 static int console= 1;
2041 void WM_toggle_console(bContext *UNUSED(C), short show)
2044 ShowWindow(GetConsoleWindow(),SW_SHOW);
2048 ShowWindow(GetConsoleWindow(),SW_HIDE);
2053 static int wm_toggle_console_op(bContext *C, wmOperator *UNUSED(op))
2056 WM_toggle_console(C, 0);
2059 WM_toggle_console(C, 1);
2061 return OPERATOR_FINISHED;
2064 static void WM_OT_toggle_console(wmOperatorType *ot)
2066 ot->name= "Toggle System Console";
2067 ot->idname= "WM_OT_toggle_console";
2068 ot->description= "Toggle System Console";
2070 ot->exec= wm_toggle_console_op;
2071 ot->poll= WM_operator_winactive;
2075 /* ************ default paint cursors, draw always around cursor *********** */
2077 - returns handler to free
2078 - poll(bContext): returns 1 if draw should happen
2079 - draw(bContext): drawing callback for paint cursor
2082 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
2083 wmPaintCursorDraw draw, void *customdata)
2085 wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
2087 BLI_addtail(&wm->paintcursors, pc);
2089 pc->customdata = customdata;
2096 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
2100 for(pc= wm->paintcursors.first; pc; pc= pc->next) {
2101 if(pc == (wmPaintCursor *)handle) {
2102 BLI_remlink(&wm->paintcursors, pc);
2109 /* ************ window gesture operator-callback definitions ************** */
2111 * These are default callbacks for use in operators requiring gesture input
2114 /* **************** Border gesture *************** */
2116 /* Border gesture has two types:
2117 1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
2118 2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
2120 It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
2123 static int border_apply(bContext *C, wmOperator *op, int gesture_mode)
2125 wmGesture *gesture= op->customdata;
2126 rcti *rect= gesture->customdata;
2128 if(rect->xmin > rect->xmax)
2129 SWAP(int, rect->xmin, rect->xmax);
2130 if(rect->ymin > rect->ymax)
2131 SWAP(int, rect->ymin, rect->ymax);
2133 if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
2136 /* operator arguments and storage. */
2137 RNA_int_set(op->ptr, "xmin", rect->xmin);
2138 RNA_int_set(op->ptr, "ymin", rect->ymin);
2139 RNA_int_set(op->ptr, "xmax", rect->xmax);
2140 RNA_int_set(op->ptr, "ymax", rect->ymax);
2142 /* XXX weak; border should be configured for this without reading event types */
2143 if( RNA_struct_find_property(op->ptr, "gesture_mode") )
2144 RNA_int_set(op->ptr, "gesture_mode", gesture_mode);
2146 op->type->exec(C, op);
2151 static void wm_gesture_end(bContext *C, wmOperator *op)
2153 wmGesture *gesture= op->customdata;
2155 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2156 op->customdata= NULL;
2158 ED_area_tag_redraw(CTX_wm_area(C));
2160 if( RNA_struct_find_property(op->ptr, "cursor") )
2161 WM_cursor_restore(CTX_wm_window(C));
2164 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2166 if(ISTWEAK(event->type))
2167 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
2169 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
2171 /* add modal handler */
2172 WM_event_add_modal_handler(C, op);
2174 wm_gesture_tag_redraw(C);
2176 return OPERATOR_RUNNING_MODAL;
2179 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
2181 wmGesture *gesture= op->customdata;
2182 rcti *rect= gesture->customdata;
2185 if(event->type== MOUSEMOVE) {
2186 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2188 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2189 rect->xmin= rect->xmax= event->x - sx;
2190 rect->ymin= rect->ymax= event->y - sy;
2193 rect->xmax= event->x - sx;
2194 rect->ymax= event->y - sy;
2197 wm_gesture_tag_redraw(C);
2199 else if (event->type==EVT_MODAL_MAP) {
2200 switch (event->val) {
2201 case GESTURE_MODAL_BEGIN:
2202 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
2204 wm_gesture_tag_redraw(C);
2207 case GESTURE_MODAL_SELECT:
2208 case GESTURE_MODAL_DESELECT:
2209 case GESTURE_MODAL_IN:
2210 case GESTURE_MODAL_OUT:
2211 if(border_apply(C, op, event->val)) {
2212 wm_gesture_end(C, op);
2213 return OPERATOR_FINISHED;
2215 wm_gesture_end(C, op);
2216 return OPERATOR_CANCELLED;
2219 case GESTURE_MODAL_CANCEL:
2220 wm_gesture_end(C, op);
2221 return OPERATOR_CANCELLED;
2225 // // Allow view navigation???
2227 // return OPERATOR_PASS_THROUGH;
2230 return OPERATOR_RUNNING_MODAL;
2233 /* **************** circle gesture *************** */
2234 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
2236 #ifdef GESTURE_MEMORY
2237 int circle_select_size= 25; // XXX - need some operator memory thing\!
2240 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
2242 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
2244 /* add modal handler */
2245 WM_event_add_modal_handler(C, op);
2247 wm_gesture_tag_redraw(C);
2249 return OPERATOR_RUNNING_MODAL;
2252 static void gesture_circle_apply(bContext *C, wmOperator *op)
2254 wmGesture *gesture= op->customdata;
2255 rcti *rect= gesture->customdata;
2257 if(RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_NOP)
2260 /* operator arguments and storage. */
2261 RNA_int_set(op->ptr, "x", rect->xmin);
2262 RNA_int_set(op->ptr, "y", rect->ymin);
2263 RNA_int_set(op->ptr, "radius", rect->xmax);
2266 op->type->exec(C, op);
2268 #ifdef GESTURE_MEMORY
2269 circle_select_size= rect->xmax;
2273 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
2275 wmGesture *gesture= op->customdata;
2276 rcti *rect= gesture->customdata;
2279 if(event->type== MOUSEMOVE) {
2280 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2282 rect->xmin= event->x - sx;
2283 rect->ymin= event->y - sy;
2285 wm_gesture_tag_redraw(C);
2288 gesture_circle_apply(C, op);
2290 else if (event->type==EVT_MODAL_MAP) {
2291 switch (event->val) {
2292 case GESTURE_MODAL_CIRCLE_ADD:
2293 rect->xmax += 2 + rect->xmax/10;
2294 wm_gesture_tag_redraw(C);
2296 case GESTURE_MODAL_CIRCLE_SUB:
2297 rect->xmax -= 2 + rect->xmax/10;
2298 if(rect->xmax < 1) rect->xmax= 1;
2299 wm_gesture_tag_redraw(C);
2301 case GESTURE_MODAL_SELECT:
2302 case GESTURE_MODAL_DESELECT:
2303 case GESTURE_MODAL_NOP:
2304 if(RNA_struct_find_property(op->ptr, "gesture_mode"))
2305 RNA_int_set(op->ptr, "gesture_mode", event->val);
2307 if(event->val != GESTURE_MODAL_NOP) {
2308 /* apply first click */
2309 gesture_circle_apply(C, op);
2311 wm_gesture_tag_redraw(C);
2315 case GESTURE_MODAL_CANCEL:
2316 case GESTURE_MODAL_CONFIRM:
2317 wm_gesture_end(C, op);
2318 return OPERATOR_FINISHED; /* use finish or we dont get an undo */
2321 // // Allow view navigation???
2323 // return OPERATOR_PASS_THROUGH;
2326 return OPERATOR_RUNNING_MODAL;
2330 /* template to copy from */
2331 void WM_OT_circle_gesture(wmOperatorType *ot)
2333 ot->name= "Circle Gesture";
2334 ot->idname= "WM_OT_circle_gesture";
2335 ot->description="Enter rotate mode with a circular gesture";
2337 ot->invoke= WM_gesture_circle_invoke;
2338 ot->modal= WM_gesture_circle_modal;
2340 ot->poll= WM_operator_winactive;
2342 RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
2343 RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
2344 RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
2349 /* **************** Tweak gesture *************** */
2351 static void tweak_gesture_modal(bContext *C, wmEvent *event)
2353 wmWindow *window= CTX_wm_window(C);
2354 wmGesture *gesture= window->tweak;
2355 rcti *rect= gesture->customdata;
2358 switch(event->type) {
2360 case INBETWEEN_MOUSEMOVE:
2362 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
2364 rect->xmax= event->x - sx;
2365 rect->ymax= event->y - sy;
2367 if((val= wm_gesture_evaluate(gesture))) {
2370 tevent= *(window->eventstate);
2371 if(gesture->event_type==LEFTMOUSE)
2372 tevent.type= EVT_TWEAK_L;
2373 else if(gesture->event_type==RIGHTMOUSE)
2374 tevent.type= EVT_TWEAK_R;
2376 tevent.type= EVT_TWEAK_M;
2379 wm_event_add(window, &tevent);
2381 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
2389 if(gesture->event_type==event->type) {
2390 WM_gesture_end(C, gesture);
2392 /* when tweak fails we should give the other keymap entries a chance */
2393 event->val= KM_RELEASE;
2397 if(!ISTIMER(event->type)) {
2398 WM_gesture_end(C, gesture);
2404 /* standard tweak, called after window handlers passed on event */
2405 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
2407 wmWindow *win= CTX_wm_window(C);
2409 if(win->tweak==NULL) {
2410 if(CTX_wm_region(C)) {
2411 if(event->val==KM_PRESS) {
2412 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
2413 win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
2418 /* no tweaks if event was handled */
2419 if((action & WM_HANDLER_BREAK)) {
2420 WM_gesture_end(C, win->tweak);
2423 tweak_gesture_modal(C, event);
2427 /* *********************** lasso gesture ****************** */
2429 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
2431 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
2433 /* add modal handler */
2434 WM_event_add_modal_handler(C, op);
2436 wm_gesture_tag_redraw(C);
2438 if( RNA_struct_find_property(op->ptr, "cursor") )
2439 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2441 return OPERATOR_RUNNING_MODAL;
2444 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
2446 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
2448 /* add modal handler */
2449 WM_event_add_modal_handler(C, op);
2451 wm_gesture_tag_redraw(C);
2453 if( RNA_struct_find_property(op->ptr, "cursor") )
2454 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2456 return OPERATOR_RUNNING_MODAL;
2460 static void gesture_lasso_apply(bContext *C, wmOperator *op)
2462 wmGesture *gesture= op->customdata;
2466 short *lasso= gesture->customdata;
2468 /* operator storage as path. */
2470 for(i=0; i<gesture->points; i++, lasso+=2) {
2473 RNA_collection_add(op->ptr, "path", &itemptr);
2474 RNA_float_set_array(&itemptr, "loc", loc);
2477 wm_gesture_end(C, op);
2480 op->type->exec(C, op);
2484 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
2486 wmGesture *gesture= op->customdata;
2489 switch(event->type) {
2491 case INBETWEEN_MOUSEMOVE:
2493 wm_gesture_tag_redraw(C);
2495 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2497 if(gesture->points == gesture->size) {
2498 short *old_lasso = gesture->customdata;
2499 gesture->customdata= MEM_callocN(2*sizeof(short)*(gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
2500 memcpy(gesture->customdata, old_lasso, 2*sizeof(short)*gesture->size);
2501 gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
2502 MEM_freeN(old_lasso);
2503 printf("realloc\n");
2508 short *lasso= gesture->customdata;
2510 lasso += (2 * gesture->points - 2);
2511 x = (event->x - sx - lasso[0]);
2512 y = (event->y - sy - lasso[1]);
2514 /* make a simple distance check to get a smoother lasso
2515 add only when at least 2 pixels between this and previous location */
2518 lasso[0] = event->x - sx;
2519 lasso[1] = event->y - sy;
2528 if(event->val==KM_RELEASE) { /* key release */
2529 gesture_lasso_apply(C, op);
2530 return OPERATOR_FINISHED;
2534 wm_gesture_end(C, op);
2535 return OPERATOR_CANCELLED;
2537 return OPERATOR_RUNNING_MODAL;
2540 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
2542 return WM_gesture_lasso_modal(C, op, event);
2546 /* template to copy from */
2548 static int gesture_lasso_exec(bContext *C, wmOperator *op)
2550 RNA_BEGIN(op->ptr, itemptr, "path") {
2553 RNA_float_get_array(&itemptr, "loc", loc);
2554 printf("Location: %f %f\n", loc[0], loc[1]);
2558 return OPERATOR_FINISHED;
2561 void WM_OT_lasso_gesture(wmOperatorType *ot)
2565 ot->name= "Lasso Gesture";
2566 ot->idname= "WM_OT_lasso_gesture";
2567 ot->description="Select objects within the lasso as you move the pointer";
2569 ot->invoke= WM_gesture_lasso_invoke;
2570 ot->modal= WM_gesture_lasso_modal;
2571 ot->exec= gesture_lasso_exec;
2573 ot->poll= WM_operator_winactive;
2575 prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2576 RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2580 /* *********************** straight line gesture ****************** */
2582 static int straightline_apply(bContext *C, wmOperator *op)
2584 wmGesture *gesture= op->customdata;
2585 rcti *rect= gesture->customdata;
2587 if(rect->xmin==rect->xmax && rect->ymin==rect->ymax)
2590 /* operator arguments and storage. */
2591 RNA_int_set(op->ptr, "xstart", rect->xmin);
2592 RNA_int_set(op->ptr, "ystart", rect->ymin);
2593 RNA_int_set(op->ptr, "xend", rect->xmax);
2594 RNA_int_set(op->ptr, "yend", rect->ymax);
2597 op->type->exec(C, op);
2603 int WM_gesture_straightline_invoke(bContext *C, wmOperator *op, wmEvent *event)
2605 op->customdata= WM_gesture_new(C, event, WM_GESTURE_STRAIGHTLINE);
2607 /* add modal handler */
2608 WM_event_add_modal_handler(C, op);
2610 wm_gesture_tag_redraw(C);
2612 if( RNA_struct_find_property(op->ptr, "cursor") )
2613 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
2615 return OPERATOR_RUNNING_MODAL;
2618 int WM_gesture_straightline_modal(bContext *C, wmOperator *op, wmEvent *event)
2620 wmGesture *gesture= op->customdata;
2621 rcti *rect= gesture->customdata;
2624 if(event->type== MOUSEMOVE) {
2625 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
2627 if(gesture->mode==0) {
2628 rect->xmin= rect->xmax= event->x - sx;
2629 rect->ymin= rect->ymax= event->y - sy;
2632 rect->xmax= event->x - sx;
2633 rect->ymax= event->y - sy;
2634 straightline_apply(C, op);
2637 wm_gesture_tag_redraw(C);
2639 else if (event->type==EVT_MODAL_MAP) {
2640 switch (event->val) {
2641 case GESTURE_MODAL_BEGIN:
2642 if(gesture->mode==0) {
2644 wm_gesture_tag_redraw(C);
2647 case GESTURE_MODAL_SELECT:
2648 if(straightline_apply(C, op)) {
2649 wm_gesture_end(C, op);
2650 return OPERATOR_FINISHED;
2652 wm_gesture_end(C, op);
2653 return OPERATOR_CANCELLED;
2656 case GESTURE_MODAL_CANCEL:
2657 wm_gesture_end(C, op);
2658 return OPERATOR_CANCELLED;
2663 return OPERATOR_RUNNING_MODAL;
2667 /* template to copy from */
2668 void WM_OT_straightline_gesture(wmOperatorType *ot)
2672 ot->name= "Straight Line Gesture";
2673 ot->idname= "WM_OT_straightline_gesture";
2674 ot->description="Draw a straight line as you move the pointer";
2676 ot->invoke= WM_gesture_straightline_invoke;
2677 ot->modal= WM_gesture_straightline_modal;
2678 ot->exec= gesture_straightline_exec;
2680 ot->poll= WM_operator_winactive;
2682 WM_operator_properties_gesture_straightline(ot, 0);
2686 /* *********************** radial control ****************** */
2688 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
2690 typedef struct wmRadialControl {
2692 float initial_value, value, max_value;
2693 float col[4], tex_col[4];
2694 int initial_mouse[2];
2699 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
2701 wmRadialControl *rc = (wmRadialControl*)customdata;
2702 ARegion *ar = CTX_wm_region(C);
2703 float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
2707 if(rc->mode == WM_RADIALCONTROL_STRENGTH)
2708 rc->tex_col[3]= (rc->value + 0.5);
2710 if(rc->mode == WM_RADIALCONTROL_SIZE) {
2712 r2= rc->initial_value;
2714 } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
2715 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
2716 r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2717 } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2718 r1= r2= r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
2722 /* Keep cursor in the original place */
2723 x = rc->initial_mouse[0] - ar->winrct.xmin;
2724 y = rc->initial_mouse[1] - ar->winrct.ymin;
2726 glTranslatef((float)x, (float)y, 0.0f);
2730 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2731 glRotatef(angle, 0, 0, 1);
2735 glBindTexture(GL_TEXTURE_2D, rc->tex);
2737 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2738 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2740 glEnable(GL_TEXTURE_2D);
2742 glColor4f(rc->tex_col[0], rc->tex_col[1], rc->tex_col[2], rc->tex_col[3]);
2744 glVertex2f(-r3, -r3);
2746 glVertex2f(r3, -r3);
2750 glVertex2f(-r3, r3);
2752 glDisable(GL_TEXTURE_2D);
2755 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
2756 glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2757 glEnable(GL_LINE_SMOOTH);
2758 glRotatef(-angle, 0, 0, 1);
2759 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2760 glRotatef(angle, 0, 0, 1);
2761 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
2762 glDisable(GL_LINE_SMOOTH);
2765 glColor4f(rc->col[0], rc->col[1], rc->col[2], rc->col[3]);
2766 glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
2767 glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
2768 glDisable(GL_BLEND);
2773 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2775 wmRadialControl *rc = (wmRadialControl*)op->customdata;
2776 int mode, initial_mouse[2], delta[2];
2778 double new_value = RNA_float_get(op->ptr, "new_value");
2779 int ret = OPERATOR_RUNNING_MODAL;
2780 // float initial_value = RNA_float_get(op->ptr, "initial_value");
2782 mode = RNA_int_get(op->ptr, "mode");
2783 RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
2785 switch(event->type) {
2787 delta[0]= initial_mouse[0] - event->x;
2788 delta[1]= initial_mouse[1] - event->y;
2790 //if (mode == WM_RADIALCONTROL_SIZE)
2791 // delta[0]+= initial_value;
2792 //else if(mode == WM_RADIALCONTROL_STRENGTH)
2793 // delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2794 //else if(mode == WM_RADIALCONTROL_ANGLE) {
2795 // delta[0]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value*M_PI/180.0f);
2796 // delta[1]+= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value*M_PI/180.0f);
2799 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
2801 if(mode == WM_RADIALCONTROL_SIZE)
2803 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2804 new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
2805 } else if(mode == WM_RADIALCONTROL_ANGLE)
2806 new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
2809 if(mode == WM_RADIALCONTROL_STRENGTH)
2810 new_value = ((int)ceil(new_value * 10.f) * 10.0f) / 100.f;
2812 new_value = ((int)new_value + 5) / 10*10;
2818 ret = OPERATOR_CANCELLED;
2822 op->type->exec(C, op);
2823 ret = OPERATOR_FINISHED;
2828 if(new_value > rc->max_value)
2829 new_value = rc->max_value;
2830 else if(new_value < 0)
2833 /* Update paint data */
2834 rc->value = new_value;
2836 RNA_float_set(op->ptr, "new_value", new_value);
2838 if(ret != OPERATOR_RUNNING_MODAL) {
2839 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
2843 ED_region_tag_redraw(CTX_wm_region(C));
2845 //if (ret != OPERATOR_RUNNING_MODAL) {
2846 // wmWindow *win = CTX_wm_window(C);
2847 // WM_cursor_restore(win);
2853 /* Expects the operator customdata to be an ImBuf (or NULL) */
2854 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2856 wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
2857 // wmWindow *win = CTX_wm_window(C);
2858 int mode = RNA_int_get(op->ptr, "mode");
2859 float initial_value = RNA_float_get(op->ptr, "initial_value");
2860 //float initial_size = RNA_float_get(op->ptr, "initial_size");
2866 //if (initial_size == 0)
2867 // initial_size = WM_RADIAL_CONTROL_DISPLAY_SIZE;
2869 if(mode == WM_RADIALCONTROL_SIZE) {
2870 rc->max_value = 200;
2871 mouse[0]-= initial_value;
2873 else if(mode == WM_RADIALCONTROL_STRENGTH) {
2875 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
2877 else if(mode == WM_RADIALCONTROL_ANGLE) {
2878 rc->max_value = 360;
2879 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
2880 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
2881 initial_value *= 180.0f/M_PI;
2884 if(op->customdata) {
2885 ImBuf *im = (ImBuf*)op->customdata;
2886 /* Build GL texture */
2887 glGenTextures(1, &rc->tex);
2888 glBindTexture(GL_TEXTURE_2D, rc->tex);
2889 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
2890 MEM_freeN(im->rect_float);
2894 RNA_float_get_array(op->ptr, "color", rc->col);
2895 RNA_float_get_array(op->ptr, "texture_color", rc->tex_col);
2897 RNA_int_set_array(op->ptr, "initial_mouse", mouse);
2898 RNA_float_set(op->ptr, "new_value", initial_value);
2900 op->customdata = rc;
2902 rc->initial_value = initial_value;
2903 rc->initial_mouse[0] = mouse[0];
2904 rc->initial_mouse[1] = mouse[1];
2905 rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
2906 wm_radial_control_paint, op->customdata);
2908 //WM_cursor_modal(win, CURSOR_NONE);
2910 /* add modal handler */
2911 WM_event_add_modal_handler(C, op);
2913 WM_radial_control_modal(C, op, event);
2915 return OPERATOR_RUNNING_MODAL;
2918 /* Gets a descriptive string of the operation */
2919 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
2921 int mode = RNA_int_get(op->ptr, "mode");
2922 float v = RNA_float_get(op->ptr, "new_value");
2924 if(mode == WM_RADIALCONTROL_SIZE)
2925 BLI_snprintf(str, maxlen, "Size: %d", (int)v);
2926 else if(mode == WM_RADIALCONTROL_STRENGTH)
2927 BLI_snprintf(str, maxlen, "Strength: %d", (int)v);
2928 else if(mode == WM_RADIALCONTROL_ANGLE)
2929 BLI_snprintf(str, maxlen, "Angle: %d", (int)(v * 180.0f/M_PI));
2932 /** Important: this doesn't define an actual operator, it
2933 just sets up the common parts of the radial control op. **/
2934 void WM_OT_radial_control_partial(wmOperatorType *ot)
2936 static EnumPropertyItem radial_mode_items[] = {
2937 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
2938 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
2939 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
2940 {0, NULL, 0, NULL, NULL}};
2941 static float color[4] = {1.0f, 1.0f, 1.0f, 0.5f};
2942 static float tex_color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
2944 /* Should be set in custom invoke() */
2945 RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
2947 /* Set internally, should be used in custom exec() to get final value */
2948 RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
2950 /* Should be set before calling operator */
2951 RNA_def_enum(ot->srna, "mode", radial_mode_items, 0, "Mode", "");
2954 RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "Initial Mouse", "", INT_MIN, INT_MAX);
2956 RNA_def_float_color(ot->srna, "color", 4, color, 0.0f, FLT_MAX, "Color", "Radial control color", 0.0f, 1.0f);
2957 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);
2960 /* ************************** timer for testing ***************** */
2962 /* uses no type defines, fully local testing function anyway... ;) */
2964 static void redraw_timer_window_swap(bContext *C)
2966 wmWindow *win= CTX_wm_window(C);
2969 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
2970 ED_area_tag_redraw(sa);
2973 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
2976 static EnumPropertyItem redraw_timer_type_items[] = {
2977 {0, "DRAW", 0, "Draw Region", "Draw Region"},
2978 {1, "DRAW_SWAP", 0, "Draw Region + Swap", "Draw Region and Swap"},
2979 {2, "DRAW_WIN", 0, "Draw Window", "Draw Window"},
2980 {3, "DRAW_WIN_SWAP", 0, "Draw Window + Swap", "Draw Window and Swap"},
2981 {4, "ANIM_STEP", 0, "Anim Step", "Animation Steps"},
2982 {5, "ANIM_PLAY", 0, "Anim Play", "Animation Playback"},
2983 {6, "UNDO", 0, "Undo/Redo", "Undo/Redo"},
2984 {0, NULL, 0, NULL, NULL}};
2986 static int redraw_timer_exec(bContext *C, wmOperator *op)
2988 ARegion *ar= CTX_wm_region(C);
2989 double stime= PIL_check_seconds_timer();
2990 int type = RNA_int_get(op->ptr, "type");
2991 int iter = RNA_int_get(op->ptr, "iterations");
2994 const char *infostr= "";
2998 for(a=0; a<iter; a++) {
3001 ED_region_do_draw(C, ar);
3004 wmWindow *win= CTX_wm_window(C);
3006 ED_region_tag_redraw(ar);
3009 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
3012 wmWindow *win= CTX_wm_window(C);
3015 ScrArea *sa_back= CTX_wm_area(C);
3016 ARegion *ar_back= CTX_wm_region(C);
3018 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next) {
3020 CTX_wm_area_set(C, sa);
3022 for(ar_iter= sa->regionbase.first; ar_iter; ar_iter= ar_iter->next) {
3023 if(ar_iter->swinid) {
3024 CTX_wm_region_set(C, ar_iter);
3025 ED_region_do_draw(C, ar_iter);
3030 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
3032 CTX_wm_area_set(C, sa_back);
3033 CTX_wm_region_set(C, ar_back);
3036 redraw_timer_window_swap(C);
3039 Main *bmain= CTX_data_main(C);
3040 Scene *scene= CTX_data_scene(C);
3042 if(a & 1) scene->r.cfra--;
3043 else scene->r.cfra++;
3044 scene_update_for_newframe(bmain, scene, scene->lay);
3048 /* play anim, return on same frame as started with */
3049 Main *bmain= CTX_data_main(C);
3050 Scene *scene= CTX_data_scene(C);
3051 int tot= (scene->r.efra - scene->r.sfra) + 1;
3054 /* todo, ability to escape! */
3056 if(scene->r.cfra > scene->r.efra)
3057 scene->r.cfra= scene->r.sfra;
3059 scene_update_for_newframe(bmain, scene, scene->lay);
3060 redraw_timer_window_swap(C);
3069 time= ((PIL_check_seconds_timer()-stime)*1000);
3071 RNA_enum_description(redraw_timer_type_items, type, &infostr);
3075 BKE_reportf(op->reports, RPT_WARNING, "%d x %s: %.2f ms, average: %.4f", iter, infostr, time, time/iter);
3077 return OPERATOR_FINISHED;
3080 static void WM_OT_redraw_timer(wmOperatorType *ot)
3082 ot->name= "Redraw Timer";
3083 ot->idname= "WM_OT_redraw_timer";
3084 ot->description="Simple redraw timer to test the speed of updating the interface";
3086 ot->invoke= WM_menu_invoke;
3087 ot->exec= redraw_timer_exec;
3088 ot->poll= WM_operator_winactive;
3090 ot->prop= RNA_def_enum(ot->srna, "type", redraw_timer_type_items, 0, "Type", "");
3091 RNA_def_int(ot->srna, "iterations", 10, 1,INT_MAX, "Iterations", "Number of times to redraw", 1,1000);
3095 /* ************************** memory statistics for testing ***************** */
3097 static int memory_statistics_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
3099 MEM_printmemlist_stats();
3100 return OPERATOR_FINISHED;
3103 static void WM_OT_memory_statistics(wmOperatorType *ot)
3105 ot->name= "Memory Statistics";
3106 ot->idname= "WM_OT_memory_statistics";
3107 ot->description= "Print memory statistics to the console";
3109 ot->exec= memory_statistics_exec;
3112 /* ******************************************************* */
3114 /* called on initialize WM_exit() */
3115 void wm_operatortype_free(void)
3119 for(ot= global_ops.first; ot; ot= ot->next) {
3121 wm_operatortype_free_macro(ot);
3123 if(ot->ext.srna) /* python operator, allocs own string */
3124 MEM_freeN((void *)ot->idname);
3127 BLI_freelistN(&global_ops);
3130 /* called on initialize WM_init() */
3131 void wm_operatortype_init(void)
3133 WM_operatortype_append(WM_OT_window_duplicate);
3134 WM_operatortype_append(WM_OT_read_homefile);
3135 WM_operatortype_append(WM_OT_read_factory_settings);
3136 WM_operatortype_append(WM_OT_save_homefile);
3137 WM_operatortype_append(WM_OT_window_fullscreen_toggle);
3138 WM_operatortype_append(WM_OT_quit_blender);
3139 WM_operatortype_append(WM_OT_open_mainfile);
3140 WM_operatortype_append(WM_OT_link_append);
3141 WM_operatortype_append(WM_OT_recover_last_session);
3142 WM_operatortype_append(WM_OT_recover_auto_save);
3143 WM_operatortype_append(WM_OT_save_as_mainfile);
3144 WM_operatortype_append(WM_OT_save_mainfile);
3145 WM_operatortype_append(WM_OT_redraw_timer);
3146 WM_operatortype_append(WM_OT_memory_statistics);
3147 WM_operatortype_append(WM_OT_debug_menu);
3148 WM_operatortype_append(WM_OT_splash);
3149 WM_operatortype_append(WM_OT_search_menu);
3150 WM_operatortype_append(WM_OT_call_menu);
3152 WM_operatortype_append(WM_OT_toggle_console);
3156 /* XXX: move these */
3157 WM_operatortype_append(WM_OT_collada_export);
3158 WM_operatortype_append(WM_OT_collada_import);
3163 /* circleselect-like modal operators */
3164 static void gesture_circle_modal_keymap(wmKeyConfig *keyconf)
3166 static EnumPropertyItem modal_items[] = {
3167 {GESTURE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3168 {GESTURE_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3169 {GESTURE_MODAL_CIRCLE_ADD, "ADD", 0, "Add", ""},
3170 {GESTURE_MODAL_CIRCLE_SUB, "SUBTRACT", 0, "Subtract", ""},
3172 {GESTURE_MODAL_SELECT, "SELECT", 0, "Select", ""},
3173 {GESTURE_MODAL_DESELECT,"DESELECT", 0, "DeSelect", ""},
3174 {GESTURE_MODAL_NOP,"NOP", 0, "No Operation", ""},
3177 {0, NULL, 0, NULL, NULL}};
3179 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Gesture Circle");
3181 /* this function is called for each spacetype, only needs to add map once */
3184 keymap= WM_modalkeymap_add(keyconf, "View3D Gesture Circle", modal_items);
3186 /* items for modal map */
3187 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3188 WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, GESTURE_MODAL_CANCEL);
3190 WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, GESTURE_MODAL_CONFIRM);
3191 WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, 0, 0, GESTURE_MODAL_CONFIRM);
3193 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_SELECT);
3195 #if 0 // Durien guys like this :S
3196 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_SHIFT, 0, GESTURE_MODAL_DESELECT);
3197 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_SHIFT, 0, GESTURE_MODAL_NOP);
3199 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_DESELECT); // defailt 2.4x
3200 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP); // defailt 2.4x
3203 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, 0, 0, GESTURE_MODAL_NOP);
3205 WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3206 WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_SUB);
3207 WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3208 WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, 0, 0, GESTURE_MODAL_CIRCLE_ADD);
3210 /* assign map to operators */
3211 WM_modalkeymap_assign(keymap, "VIEW3D_OT_select_circle");
3212 WM_modalkeymap_assign(keymap, "UV_OT_circle_select");
3216 /* straight line modal operators */
3217 static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)