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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
30 #define _USE_MATH_DEFINES
37 #include "DNA_screen_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
40 #include "DNA_windowmanager_types.h"
42 #include "MEM_guardedalloc.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_dynstr.h" /*for WM_operator_pystring */
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_idprop.h"
52 #include "BKE_library.h"
53 #include "BKE_global.h"
55 #include "BKE_scene.h"
56 #include "BKE_utildefines.h"
59 #include "BIF_glutil.h" /* for paint cursor */
61 #include "IMB_imbuf_types.h"
63 #include "ED_screen.h"
66 #include "RNA_access.h"
67 #include "RNA_define.h"
69 #include "UI_interface.h"
70 #include "UI_resources.h"
77 #include "wm_event_system.h"
78 #include "wm_subwindow.h"
79 #include "wm_window.h"
83 static ListBase global_ops= {NULL, NULL};
85 /* ************ operator API, exported ********** */
88 wmOperatorType *WM_operatortype_find(const char *idname, int quiet)
92 char idname_bl[OP_MAX_TYPENAME]; // XXX, needed to support python style names without the _OT_ syntax
93 WM_operator_bl_idname(idname_bl, idname);
95 for(ot= global_ops.first; ot; ot= ot->next) {
96 if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
101 printf("search for unknown operator %s, %s\n", idname_bl, idname);
106 wmOperatorType *WM_operatortype_exists(const char *idname)
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);
113 for(ot= global_ops.first; ot; ot= ot->next) {
114 if(strncmp(ot->idname, idname_bl, OP_MAX_TYPENAME)==0)
120 wmOperatorType *WM_operatortype_first(void)
122 return global_ops.first;
125 /* all ops in 1 list (for time being... needs evaluation later) */
126 void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
130 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
131 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
135 static char dummy_name[] = "Dummy Name";
136 fprintf(stderr, "ERROR: Operator %s has no name property!\n", ot->idname);
137 ot->name= dummy_name;
140 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.
141 RNA_def_struct_identifier(ot->srna, ot->idname);
142 BLI_addtail(&global_ops, ot);
145 void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
149 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
150 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
151 opfunc(ot, userdata);
152 RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:"(undocumented operator)");
153 RNA_def_struct_identifier(ot->srna, ot->idname);
154 BLI_addtail(&global_ops, ot);
157 /* ********************* macro operator ******************** */
159 /* macro exec only runs exec calls */
160 static int wm_macro_exec(bContext *C, wmOperator *op)
163 int retval= OPERATOR_FINISHED;
165 // printf("macro exec %s\n", op->type->idname);
167 for(opm= op->macro.first; opm; opm= opm->next) {
169 if(opm->type->exec) {
170 // printf("macro exec %s\n", opm->type->idname);
171 retval= opm->type->exec(C, opm);
173 if(!(retval & OPERATOR_FINISHED))
178 // printf("macro ended not finished\n");
180 // printf("macro end\n");
185 static int wm_macro_invoke(bContext *C, wmOperator *op, wmEvent *event)
188 int retval= OPERATOR_FINISHED;
190 // printf("macro invoke %s\n", op->type->idname);
192 for(opm= op->macro.first; opm; opm= opm->next) {
194 if(opm->type->invoke)
195 retval= opm->type->invoke(C, opm, event);
196 else if(opm->type->exec)
197 retval= opm->type->exec(C, opm);
199 if(!(retval & OPERATOR_FINISHED))
204 // printf("macro ended not finished\n");
206 // printf("macro end\n");
212 static int wm_macro_modal(bContext *C, wmOperator *op, wmEvent *event)
214 // printf("macro modal %s\n", op->type->idname);
217 printf("macro error, calling NULL modal()\n");
219 // printf("macro modal %s\n", op->opm->type->idname);
220 return op->opm->type->modal(C, op->opm, event);
223 return OPERATOR_FINISHED;
226 /* Names have to be static for now */
227 wmOperatorType *WM_operatortype_append_macro(char *idname, char *name, int flag)
231 if(WM_operatortype_exists(idname)) {
232 printf("Macro error: operator %s exists\n", idname);
236 ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
237 ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
241 ot->flag= OPTYPE_MACRO | flag;
243 ot->exec= wm_macro_exec;
244 ot->invoke= wm_macro_invoke;
245 ot->modal= wm_macro_modal;
248 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.
249 RNA_def_struct_identifier(ot->srna, ot->idname);
251 BLI_addtail(&global_ops, ot);
256 wmOperatorTypeMacro *WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
258 wmOperatorTypeMacro *otmacro= MEM_callocN(sizeof(wmOperatorTypeMacro), "wmOperatorTypeMacro");
260 BLI_strncpy(otmacro->idname, idname, OP_MAX_TYPENAME);
262 /* do this on first use, since operatordefinitions might have been not done yet */
263 // otmacro->ptr= MEM_callocN(sizeof(PointerRNA), "optype macro ItemPtr");
264 // WM_operator_properties_create(otmacro->ptr, idname);
266 BLI_addtail(&ot->macro, otmacro);
271 static void wm_operatortype_free_macro(wmOperatorType *ot)
273 wmOperatorTypeMacro *otmacro;
275 for(otmacro= ot->macro.first; otmacro; otmacro= otmacro->next) {
277 WM_operator_properties_free(otmacro->ptr);
278 MEM_freeN(otmacro->ptr);
281 BLI_freelistN(&ot->macro);
285 int WM_operatortype_remove(const char *idname)
287 wmOperatorType *ot = WM_operatortype_find(idname, 0);
292 BLI_remlink(&global_ops, ot);
293 RNA_struct_free(&BLENDER_RNA, ot->srna);
296 wm_operatortype_free_macro(ot);
303 /* SOME_OT_op -> some.op */
304 void WM_operator_py_idname(char *to, const char *from)
306 char *sep= strstr(from, "_OT_");
308 int i, ofs= (sep-from);
311 to[i]= tolower(from[i]);
314 BLI_strncpy(to+(ofs+1), sep+4, OP_MAX_TYPENAME);
317 /* should not happen but support just incase */
318 BLI_strncpy(to, from, OP_MAX_TYPENAME);
322 /* some.op -> SOME_OT_op */
323 void WM_operator_bl_idname(char *to, const char *from)
325 char *sep= strchr(from, '.');
328 int i, ofs= (sep-from);
331 to[i]= toupper(from[i]);
333 BLI_strncpy(to+ofs, "_OT_", OP_MAX_TYPENAME);
334 BLI_strncpy(to+(ofs+4), sep+1, OP_MAX_TYPENAME);
337 /* should not happen but support just incase */
338 BLI_strncpy(to, from, OP_MAX_TYPENAME);
342 /* print a string representation of the operator, with the args that it runs
343 * so python can run it again,
345 * When calling from an existing wmOperator do.
346 * WM_operator_pystring(op->type, op->ptr);
348 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
350 const char *arg_name= NULL;
351 char idname_py[OP_MAX_TYPENAME];
353 PropertyRNA *prop, *iterprop;
355 /* for building the string */
356 DynStr *dynstr= BLI_dynstr_new();
358 int first_iter=1, ok= 1;
361 /* only to get the orginal props for comparisons */
362 PointerRNA opptr_default;
363 PropertyRNA *prop_default;
366 WM_operator_properties_create(&opptr_default, ot->idname);
370 WM_operator_py_idname(idname_py, ot->idname);
371 BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
373 iterprop= RNA_struct_iterator_property(opptr->type);
375 RNA_PROP_BEGIN(opptr, propptr, iterprop) {
377 arg_name= RNA_property_identifier(prop);
379 if (strcmp(arg_name, "rna_type")==0) continue;
381 buf= RNA_property_as_string(C, opptr, prop);
386 /* not verbose, so only add in attributes that use non-default values
387 * slow but good for tooltips */
388 prop_default= RNA_struct_find_property(&opptr_default, arg_name);
391 buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
393 if(strcmp(buf, buf_default)==0)
394 ok= 0; /* values match, dont bother printing */
396 MEM_freeN(buf_default);
401 BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
411 WM_operator_properties_free(&opptr_default);
413 BLI_dynstr_append(dynstr, ")");
415 cstring = BLI_dynstr_get_cstring(dynstr);
416 BLI_dynstr_free(dynstr);
420 void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
422 wmOperatorType *ot= WM_operatortype_find(opstring, 0);
425 RNA_pointer_create(NULL, ot->srna, NULL, ptr);
427 RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
430 void WM_operator_properties_free(PointerRNA *ptr)
432 IDProperty *properties= ptr->data;
435 IDP_FreeProperty(properties);
436 MEM_freeN(properties);
440 /* ************ default op callbacks, exported *********** */
442 /* invoke callback, uses enum property named "type" */
443 /* only weak thing is the fixed property name... */
444 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
446 PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
451 printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
453 else if (RNA_property_type(prop) != PROP_ENUM) {
454 printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
457 pup= uiPupMenuBegin(C, op->type->name, 0);
458 layout= uiPupMenuLayout(pup);
459 uiItemsEnumO(layout, op->type->idname, "type");
460 uiPupMenuEnd(C, pup);
463 return OPERATOR_CANCELLED;
467 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
472 pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
473 layout= uiPupMenuLayout(pup);
474 uiItemO(layout, NULL, 0, op->type->idname);
475 uiPupMenuEnd(C, pup);
477 return OPERATOR_CANCELLED;
480 /* op->invoke, opens fileselect if filename property not set, otherwise executes */
481 int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
483 if (RNA_property_is_set(op->ptr, "filename")) {
484 return WM_operator_call(C, op);
487 WM_event_add_fileselect(C, op);
488 return OPERATOR_RUNNING_MODAL;
492 /* default properties for fileselect */
493 void WM_operator_properties_filesel(wmOperatorType *ot, int filter)
495 RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "Path to file.");
497 RNA_def_boolean(ot->srna, "filter_blender", (filter & BLENDERFILE), "Filter .blend files", "");
498 RNA_def_boolean(ot->srna, "filter_image", (filter & IMAGEFILE), "Filter image files", "");
499 RNA_def_boolean(ot->srna, "filter_movie", (filter & MOVIEFILE), "Filter movie files", "");
500 RNA_def_boolean(ot->srna, "filter_python", (filter & PYSCRIPTFILE), "Filter python files", "");
501 RNA_def_boolean(ot->srna, "filter_font", (filter & FTFONTFILE), "Filter font files", "");
502 RNA_def_boolean(ot->srna, "filter_sound", (filter & SOUNDFILE), "Filter sound files", "");
503 RNA_def_boolean(ot->srna, "filter_text", (filter & TEXTFILE), "Filter text files", "");
504 RNA_def_boolean(ot->srna, "filter_folder", (filter & FOLDERFILE), "Filter folders", "");
508 int WM_operator_winactive(bContext *C)
510 if(CTX_wm_window(C)==NULL) return 0;
515 static void redo_cb(bContext *C, void *arg_op, void *arg2)
517 wmOperator *lastop= arg_op;
520 ED_undo_pop_op(C, lastop);
521 WM_operator_repeat(C, lastop);
525 static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
527 wmWindowManager *wm= CTX_wm_manager(C);
528 wmOperator *op= arg_op;
532 uiStyle *style= U.uistyles.first;
534 block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
535 uiBlockClearFlag(block, UI_BLOCK_LOOP);
536 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
537 uiBlockSetFunc(block, redo_cb, arg_op, NULL);
539 if(!op->properties) {
540 IDPropertyTemplate val = {0};
541 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
544 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
545 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
546 uiItemL(layout, op->type->name, 0);
549 op->type->ui((bContext*)C, &ptr, layout);
551 uiDefAutoButsRNA(C, layout, &ptr, 2);
553 uiPopupBoundsBlock(block, 4.0f, 0, 0);
554 uiEndBlock(C, block);
559 int WM_operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
561 int retval= OPERATOR_CANCELLED;
564 retval= op->type->exec(C, op);
566 if(retval != OPERATOR_CANCELLED)
567 uiPupBlock(C, wm_block_create_redo, op);
572 int WM_operator_redo_popup(bContext *C, wmOperator *op)
574 uiPupBlock(C, wm_block_create_redo, op);
576 return OPERATOR_CANCELLED;
579 /* ***************** Debug menu ************************* */
581 static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
583 wmOperator *op= arg_op;
586 uiStyle *style= U.uistyles.first;
588 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
589 uiBlockClearFlag(block, UI_BLOCK_LOOP);
590 uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
592 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
593 uiItemL(layout, op->type->name, 0);
596 op->type->ui(C, op->ptr, layout);
598 uiDefAutoButsRNA(C, layout, op->ptr, 2);
600 uiPopupBoundsBlock(block, 4.0f, 0, 0);
601 uiEndBlock(C, block);
606 static int wm_debug_menu_exec(bContext *C, wmOperator *op)
608 G.rt= RNA_int_get(op->ptr, "debugval");
609 ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
610 WM_event_add_notifier(C, NC_WINDOW, NULL);
612 return OPERATOR_FINISHED;
615 static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
618 RNA_int_set(op->ptr, "debugval", G.rt);
620 /* pass on operator, so return modal */
621 uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
623 return OPERATOR_RUNNING_MODAL;
626 static void WM_OT_debug_menu(wmOperatorType *ot)
628 ot->name= "Debug Menu";
629 ot->idname= "WM_OT_debug_menu";
631 ot->invoke= wm_debug_menu_invoke;
632 ot->exec= wm_debug_menu_exec;
633 ot->poll= WM_operator_winactive;
635 RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
638 /* ***************** Search menu ************************* */
639 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
641 wmOperatorType *ot= arg2;
644 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
647 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
649 wmOperatorType *ot = WM_operatortype_first();
651 for(; ot; ot= ot->next) {
653 if(BLI_strcasestr(ot->name, str)) {
654 if(ot->poll==NULL || ot->poll((bContext *)C)) {
656 int len= strlen(ot->name);
658 /* display name for menu, can hold hotkey */
659 BLI_strncpy(name, ot->name, 256);
661 /* check for hotkey */
663 if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
667 if(0==uiSearchItemAdd(items, name, ot, 0))
674 static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
676 static char search[256]= "";
678 wmWindow *win= CTX_wm_window(C);
682 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
683 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
685 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
686 uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
688 /* fake button, it holds space for search items */
689 uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
691 uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
692 uiEndBlock(C, block);
694 event= *(win->eventstate); /* XXX huh huh? make api call */
695 event.type= EVT_BUT_OPEN;
697 event.customdata= but;
698 event.customdatafree= FALSE;
699 wm_event_add(win, &event);
704 static int wm_search_menu_exec(bContext *C, wmOperator *op)
707 return OPERATOR_FINISHED;
710 static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
713 uiPupBlock(C, wm_block_search_menu, op);
715 return OPERATOR_CANCELLED;
719 int wm_search_menu_poll(bContext *C)
721 if(CTX_wm_window(C)==NULL) return 0;
722 if(CTX_wm_area(C) && CTX_wm_area(C)->spacetype==SPACE_CONSOLE) return 0; // XXX - so we can use the shortcut in the console
726 static void WM_OT_search_menu(wmOperatorType *ot)
728 ot->name= "Search Menu";
729 ot->idname= "WM_OT_search_menu";
731 ot->invoke= wm_search_menu_invoke;
732 ot->exec= wm_search_menu_exec;
733 ot->poll= wm_search_menu_poll;
737 /* ************ window / screen operator definitions ************** */
739 static void WM_OT_window_duplicate(wmOperatorType *ot)
741 ot->name= "Duplicate Window";
742 ot->idname= "WM_OT_window_duplicate";
744 ot->exec= wm_window_duplicate_op;
745 ot->poll= WM_operator_winactive;
748 static void WM_OT_save_homefile(wmOperatorType *ot)
750 ot->name= "Save User Settings";
751 ot->idname= "WM_OT_save_homefile";
753 ot->invoke= WM_operator_confirm;
754 ot->exec= WM_write_homefile;
755 ot->poll= WM_operator_winactive;
758 static void WM_OT_read_homefile(wmOperatorType *ot)
760 ot->name= "Reload Start-Up File";
761 ot->idname= "WM_OT_read_homefile";
763 ot->invoke= WM_operator_confirm;
764 ot->exec= WM_read_homefile;
765 ot->poll= WM_operator_winactive;
767 RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
771 /* ********* recent file *********** */
773 static int recentfile_exec(bContext *C, wmOperator *op)
775 int event= RNA_enum_get(op->ptr, "file");
777 // XXX wm in context is not set correctly after WM_read_file -> crash
778 // do it before for now, but is this correct with multiple windows?
781 if (G.sce[0] && (event==1)) {
782 WM_event_add_notifier(C, NC_WINDOW, NULL);
783 WM_read_file(C, G.sce, op->reports);
786 struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
788 WM_event_add_notifier(C, NC_WINDOW, NULL);
789 WM_read_file(C, recent->filename, op->reports);
796 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
801 pup= uiPupMenuBegin(C, "Open Recent", 0);
802 layout= uiPupMenuLayout(pup);
803 uiItemsEnumO(layout, op->type->idname, "file");
804 uiPupMenuEnd(C, pup);
806 return OPERATOR_CANCELLED;
809 static EnumPropertyItem *open_recentfile_itemf(bContext *C, PointerRNA *ptr, int *free)
811 EnumPropertyItem tmp = {0, "", 0, "", ""};
812 EnumPropertyItem *item= NULL;
813 struct RecentFile *recent;
814 int totitem= 0, i, ofs= 0;
818 tmp.identifier= G.sce;
820 RNA_enum_item_add(&item, &totitem, &tmp);
824 /* dynamically construct enum */
825 for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
826 if(strcmp(recent->filename, G.sce)) {
828 tmp.identifier= recent->filename;
829 tmp.name= recent->filename;
830 RNA_enum_item_add(&item, &totitem, &tmp);
834 RNA_enum_item_end(&item, &totitem);
840 static void WM_OT_open_recentfile(wmOperatorType *ot)
843 static EnumPropertyItem file_items[]= {
844 {0, NULL, 0, NULL, NULL}};
846 ot->name= "Open Recent File";
847 ot->idname= "WM_OT_open_recentfile";
849 ot->invoke= wm_recentfile_invoke;
850 ot->exec= recentfile_exec;
851 ot->poll= WM_operator_winactive;
853 prop= RNA_def_enum(ot->srna, "file", file_items, 1, "File", "");
854 RNA_def_enum_funcs(prop, open_recentfile_itemf);
857 /* ********* main file *********** */
859 static void untitled(char *name)
861 if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
862 char *c= BLI_last_slash(name);
865 strcpy(&c[1], "untitled.blend");
867 strcpy(name, "untitled.blend");
871 static void load_set_load_ui(wmOperator *op)
873 if(!RNA_property_is_set(op->ptr, "load_ui"))
874 RNA_boolean_set(op->ptr, "load_ui", !(U.flag & USER_FILENOUI));
877 static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
879 RNA_string_set(op->ptr, "filename", G.sce);
880 load_set_load_ui(op);
882 WM_event_add_fileselect(C, op);
884 return OPERATOR_RUNNING_MODAL;
887 static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
889 char filename[FILE_MAX];
891 RNA_string_get(op->ptr, "filename", filename);
892 load_set_load_ui(op);
894 if(RNA_boolean_get(op->ptr, "load_ui"))
895 G.fileflags &= ~G_FILE_NO_UI;
897 G.fileflags |= G_FILE_NO_UI;
899 // XXX wm in context is not set correctly after WM_read_file -> crash
900 // do it before for now, but is this correct with multiple windows?
901 WM_event_add_notifier(C, NC_WINDOW, NULL);
903 WM_read_file(C, filename, op->reports);
908 static void WM_OT_open_mainfile(wmOperatorType *ot)
910 ot->name= "Open Blender File";
911 ot->idname= "WM_OT_open_mainfile";
913 ot->invoke= wm_open_mainfile_invoke;
914 ot->exec= wm_open_mainfile_exec;
915 ot->poll= WM_operator_winactive;
917 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
919 RNA_def_boolean(ot->srna, "load_ui", 1, "Load UI", "Load user interface setup in the .blend file.");
922 static int wm_recover_last_session_exec(bContext *C, wmOperator *op)
924 char scestr[FILE_MAX], filename[FILE_MAX];
927 /* back up some values */
928 BLI_strncpy(scestr, G.sce, sizeof(scestr));
929 save_over = G.save_over;
931 // XXX wm in context is not set correctly after WM_read_file -> crash
932 // do it before for now, but is this correct with multiple windows?
933 WM_event_add_notifier(C, NC_WINDOW, NULL);
936 BLI_make_file_string("/", filename, btempdir, "quit.blend");
937 WM_read_file(C, filename, op->reports);
940 G.save_over = save_over;
941 BLI_strncpy(G.sce, scestr, sizeof(G.sce));
946 static void WM_OT_recover_last_session(wmOperatorType *ot)
948 ot->name= "Recover Last Session";
949 ot->idname= "WM_OT_recover_last_session";
951 ot->exec= wm_recover_last_session_exec;
952 ot->poll= WM_operator_winactive;
955 static void save_set_compress(wmOperator *op)
957 if(!RNA_property_is_set(op->ptr, "compress")) {
958 if(G.save_over) /* keep flag for existing file */
959 RNA_boolean_set(op->ptr, "compress", G.fileflags & G_FILE_COMPRESS);
960 else /* use userdef for new file */
961 RNA_boolean_set(op->ptr, "compress", U.flag & USER_FILECOMPRESS);
965 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
969 save_set_compress(op);
971 BLI_strncpy(name, G.sce, FILE_MAX);
973 RNA_string_set(op->ptr, "filename", name);
975 WM_event_add_fileselect(C, op);
977 return OPERATOR_RUNNING_MODAL;
980 /* function used for WM_OT_save_mainfile too */
981 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
983 char filename[FILE_MAX];
986 save_set_compress(op);
987 compress= RNA_boolean_get(op->ptr, "compress");
989 if(RNA_property_is_set(op->ptr, "filename"))
990 RNA_string_get(op->ptr, "filename", filename);
992 BLI_strncpy(filename, G.sce, FILE_MAX);
996 WM_write_file(C, filename, compress, op->reports);
998 WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
1003 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
1005 ot->name= "Save As Blender File";
1006 ot->idname= "WM_OT_save_as_mainfile";
1008 ot->invoke= wm_save_as_mainfile_invoke;
1009 ot->exec= wm_save_as_mainfile_exec;
1010 ot->poll= WM_operator_winactive;
1012 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
1013 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1016 /* *************** Save file directly ******** */
1018 static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
1020 char name[FILE_MAX];
1022 save_set_compress(op);
1024 BLI_strncpy(name, G.sce, FILE_MAX);
1026 RNA_string_set(op->ptr, "filename", name);
1027 uiPupMenuSaveOver(C, op, name);
1029 return OPERATOR_RUNNING_MODAL;
1032 static void WM_OT_save_mainfile(wmOperatorType *ot)
1034 ot->name= "Save Blender File";
1035 ot->idname= "WM_OT_save_mainfile";
1037 ot->invoke= wm_save_mainfile_invoke;
1038 ot->exec= wm_save_as_mainfile_exec;
1039 ot->poll= WM_operator_winactive;
1041 WM_operator_properties_filesel(ot, FOLDERFILE|BLENDERFILE);
1042 RNA_def_boolean(ot->srna, "compress", 0, "Compress", "Write compressed .blend file.");
1046 /* *********************** */
1048 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
1050 ot->name= "Toggle Fullscreen";
1051 ot->idname= "WM_OT_window_fullscreen_toggle";
1053 ot->exec= wm_window_fullscreen_toggle_op;
1054 ot->poll= WM_operator_winactive;
1057 static int wm_exit_blender_op(bContext *C, wmOperator *op)
1059 WM_operator_free(op);
1063 return OPERATOR_FINISHED;
1066 static void WM_OT_exit_blender(wmOperatorType *ot)
1068 ot->name= "Exit Blender";
1069 ot->idname= "WM_OT_exit_blender";
1071 ot->invoke= WM_operator_confirm;
1072 ot->exec= wm_exit_blender_op;
1073 ot->poll= WM_operator_winactive;
1076 /* ************ default paint cursors, draw always around cursor *********** */
1078 - returns handler to free
1079 - poll(bContext): returns 1 if draw should happen
1080 - draw(bContext): drawing callback for paint cursor
1083 void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
1084 wmPaintCursorDraw draw, void *customdata)
1086 wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
1088 BLI_addtail(&wm->paintcursors, pc);
1090 pc->customdata = customdata;
1097 void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
1101 for(pc= wm->paintcursors.first; pc; pc= pc->next) {
1102 if(pc == (wmPaintCursor *)handle) {
1103 BLI_remlink(&wm->paintcursors, pc);
1110 /* ************ window gesture operator-callback definitions ************** */
1112 * These are default callbacks for use in operators requiring gesture input
1115 /* **************** Border gesture *************** */
1117 /* Border gesture has two types:
1118 1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
1119 2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
1121 It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
1124 static int border_apply(bContext *C, wmOperator *op, int event_type, int event_orig)
1126 wmGesture *gesture= op->customdata;
1127 rcti *rect= gesture->customdata;
1129 if(rect->xmin > rect->xmax)
1130 SWAP(int, rect->xmin, rect->xmax);
1131 if(rect->ymin > rect->ymax)
1132 SWAP(int, rect->ymin, rect->ymax);
1134 if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
1137 /* operator arguments and storage. */
1138 RNA_int_set(op->ptr, "xmin", rect->xmin);
1139 RNA_int_set(op->ptr, "ymin", rect->ymin);
1140 RNA_int_set(op->ptr, "xmax", rect->xmax);
1141 RNA_int_set(op->ptr, "ymax", rect->ymax);
1143 /* XXX weak; border should be configured for this without reading event types */
1144 if( RNA_struct_find_property(op->ptr, "event_type") ) {
1145 if(ELEM4(event_orig, EVT_TWEAK_L, EVT_TWEAK_R, EVT_TWEAK_A, EVT_TWEAK_S))
1146 event_type= LEFTMOUSE;
1148 RNA_int_set(op->ptr, "event_type", event_type);
1150 op->type->exec(C, op);
1155 static void wm_gesture_end(bContext *C, wmOperator *op)
1157 wmGesture *gesture= op->customdata;
1159 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
1160 op->customdata= NULL;
1162 ED_area_tag_redraw(CTX_wm_area(C));
1164 if( RNA_struct_find_property(op->ptr, "cursor") )
1165 WM_cursor_restore(CTX_wm_window(C));
1168 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1170 if(WM_key_event_is_tweak(event->type))
1171 op->customdata= WM_gesture_new(C, event, WM_GESTURE_RECT);
1173 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
1175 /* add modal handler */
1176 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1178 wm_gesture_tag_redraw(C);
1180 return OPERATOR_RUNNING_MODAL;
1183 int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
1185 wmGesture *gesture= op->customdata;
1186 rcti *rect= gesture->customdata;
1189 switch(event->type) {
1192 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1194 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1195 rect->xmin= rect->xmax= event->x - sx;
1196 rect->ymin= rect->ymax= event->y - sy;
1199 rect->xmax= event->x - sx;
1200 rect->ymax= event->y - sy;
1203 wm_gesture_tag_redraw(C);
1210 if(event->val==KM_PRESS) {
1211 if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
1213 wm_gesture_tag_redraw(C);
1217 if(border_apply(C, op, event->type, gesture->event_type)) {
1218 wm_gesture_end(C, op);
1219 return OPERATOR_FINISHED;
1221 wm_gesture_end(C, op);
1222 return OPERATOR_CANCELLED;
1226 wm_gesture_end(C, op);
1227 return OPERATOR_CANCELLED;
1229 return OPERATOR_RUNNING_MODAL;
1232 /* **************** circle gesture *************** */
1233 /* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
1235 int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
1237 op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
1239 /* add modal handler */
1240 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1242 wm_gesture_tag_redraw(C);
1244 return OPERATOR_RUNNING_MODAL;
1247 static void gesture_circle_apply(bContext *C, wmOperator *op)
1249 wmGesture *gesture= op->customdata;
1250 rcti *rect= gesture->customdata;
1252 /* operator arguments and storage. */
1253 RNA_int_set(op->ptr, "x", rect->xmin);
1254 RNA_int_set(op->ptr, "y", rect->ymin);
1255 RNA_int_set(op->ptr, "radius", rect->xmax);
1258 op->type->exec(C, op);
1261 int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
1263 wmGesture *gesture= op->customdata;
1264 rcti *rect= gesture->customdata;
1267 switch(event->type) {
1270 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1272 rect->xmin= event->x - sx;
1273 rect->ymin= event->y - sy;
1275 wm_gesture_tag_redraw(C);
1278 gesture_circle_apply(C, op);
1282 rect->xmax += 2 + rect->xmax/10;
1283 wm_gesture_tag_redraw(C);
1285 case WHEELDOWNMOUSE:
1286 rect->xmax -= 2 + rect->xmax/10;
1287 if(rect->xmax < 1) rect->xmax= 1;
1288 wm_gesture_tag_redraw(C);
1293 if(event->val==0) { /* key release */
1294 wm_gesture_end(C, op);
1295 return OPERATOR_FINISHED;
1298 if( RNA_struct_find_property(op->ptr, "event_type") )
1299 RNA_int_set(op->ptr, "event_type", event->type);
1301 /* apply first click */
1302 gesture_circle_apply(C, op);
1307 wm_gesture_end(C, op);
1308 return OPERATOR_CANCELLED;
1310 return OPERATOR_RUNNING_MODAL;
1314 /* template to copy from */
1315 void WM_OT_circle_gesture(wmOperatorType *ot)
1317 ot->name= "Circle Gesture";
1318 ot->idname= "WM_OT_circle_gesture";
1320 ot->invoke= WM_gesture_circle_invoke;
1321 ot->modal= WM_gesture_circle_modal;
1323 ot->poll= WM_operator_winactive;
1325 RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
1326 RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
1327 RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
1332 /* **************** Tweak gesture *************** */
1334 static void tweak_gesture_modal(bContext *C, wmEvent *event)
1336 wmWindow *window= CTX_wm_window(C);
1337 wmGesture *gesture= window->tweak;
1338 rcti *rect= gesture->customdata;
1341 switch(event->type) {
1344 wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
1346 rect->xmax= event->x - sx;
1347 rect->ymax= event->y - sy;
1349 if((val= wm_gesture_evaluate(C, gesture))) {
1352 event= *(window->eventstate);
1353 if(gesture->event_type==LEFTMOUSE)
1354 event.type= EVT_TWEAK_L;
1355 else if(gesture->event_type==RIGHTMOUSE)
1356 event.type= EVT_TWEAK_R;
1358 event.type= EVT_TWEAK_M;
1361 wm_event_add(window, &event);
1363 WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
1364 window->tweak= NULL;
1372 if(gesture->event_type==event->type) {
1373 WM_gesture_end(C, gesture);
1374 window->tweak= NULL;
1376 /* when tweak fails we should give the other keymap entries a chance */
1377 event->val= KM_RELEASE;
1381 WM_gesture_end(C, gesture);
1382 window->tweak= NULL;
1386 /* standard tweak, called after window handlers passed on event */
1387 void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
1389 wmWindow *win= CTX_wm_window(C);
1391 if(win->tweak==NULL) {
1392 if(CTX_wm_region(C)) {
1393 if(event->val) { // pressed
1394 if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
1395 win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
1400 if(action==WM_HANDLER_BREAK) {
1401 WM_gesture_end(C, win->tweak);
1405 tweak_gesture_modal(C, event);
1409 /* *********************** lasso gesture ****************** */
1411 int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
1413 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
1415 /* add modal handler */
1416 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1418 wm_gesture_tag_redraw(C);
1420 if( RNA_struct_find_property(op->ptr, "cursor") )
1421 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1423 return OPERATOR_RUNNING_MODAL;
1426 int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
1428 op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
1430 /* add modal handler */
1431 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1433 wm_gesture_tag_redraw(C);
1435 if( RNA_struct_find_property(op->ptr, "cursor") )
1436 WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
1438 return OPERATOR_RUNNING_MODAL;
1442 static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
1444 wmGesture *gesture= op->customdata;
1448 short *lasso= gesture->customdata;
1450 /* operator storage as path. */
1452 for(i=0; i<gesture->points; i++, lasso+=2) {
1455 RNA_collection_add(op->ptr, "path", &itemptr);
1456 RNA_float_set_array(&itemptr, "loc", loc);
1459 wm_gesture_end(C, op);
1462 op->type->exec(C, op);
1466 int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
1468 wmGesture *gesture= op->customdata;
1471 switch(event->type) {
1474 wm_gesture_tag_redraw(C);
1476 wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
1477 if(gesture->points < WM_LASSO_MAX_POINTS) {
1478 short *lasso= gesture->customdata;
1479 lasso += 2 * gesture->points;
1480 lasso[0] = event->x - sx;
1481 lasso[1] = event->y - sy;
1485 gesture_lasso_apply(C, op, event->type);
1486 return OPERATOR_FINISHED;
1493 if(event->val==0) { /* key release */
1494 gesture_lasso_apply(C, op, event->type);
1495 return OPERATOR_FINISHED;
1499 wm_gesture_end(C, op);
1500 return OPERATOR_CANCELLED;
1502 return OPERATOR_RUNNING_MODAL;
1505 int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
1507 return WM_gesture_lasso_modal(C, op, event);
1511 /* template to copy from */
1513 static int gesture_lasso_exec(bContext *C, wmOperator *op)
1515 RNA_BEGIN(op->ptr, itemptr, "path") {
1518 RNA_float_get_array(&itemptr, "loc", loc);
1519 printf("Location: %f %f\n", loc[0], loc[1]);
1523 return OPERATOR_FINISHED;
1526 void WM_OT_lasso_gesture(wmOperatorType *ot)
1530 ot->name= "Lasso Gesture";
1531 ot->idname= "WM_OT_lasso_gesture";
1533 ot->invoke= WM_gesture_lasso_invoke;
1534 ot->modal= WM_gesture_lasso_modal;
1535 ot->exec= gesture_lasso_exec;
1537 ot->poll= WM_operator_winactive;
1539 prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1540 RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1544 /* *********************** radial control ****************** */
1546 const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
1548 typedef struct wmRadialControl {
1550 float initial_value, value, max_value;
1551 int initial_mouse[2];
1556 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
1558 wmRadialControl *rc = (wmRadialControl*)customdata;
1559 ARegion *ar = CTX_wm_region(C);
1560 float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
1562 /* Keep cursor in the original place */
1563 x = rc->initial_mouse[0] - ar->winrct.xmin;
1564 y = rc->initial_mouse[1] - ar->winrct.ymin;
1568 glTranslatef((float)x, (float)y, 0.0f);
1570 if(rc->mode == WM_RADIALCONTROL_SIZE) {
1572 r2= rc->initial_value;
1574 } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
1575 r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
1576 r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1577 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1578 } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1579 r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1580 r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
1584 glColor4ub(255, 255, 255, 128);
1585 glEnable( GL_LINE_SMOOTH );
1588 if(rc->mode == WM_RADIALCONTROL_ANGLE)
1589 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1592 const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
1594 if(rc->mode == WM_RADIALCONTROL_ANGLE) {
1595 glRotatef(angle, 0, 0, 1);
1596 fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
1599 glBindTexture(GL_TEXTURE_2D, rc->tex);
1601 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1602 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1604 glEnable(GL_TEXTURE_2D);
1606 glColor4f(0,0,0, str);
1608 glVertex2f(-r3, -r3);
1610 glVertex2f(r3, -r3);
1614 glVertex2f(-r3, r3);
1616 glDisable(GL_TEXTURE_2D);
1619 glColor4ub(255, 255, 255, 128);
1620 glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
1621 glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
1622 glDisable(GL_BLEND);
1623 glDisable( GL_LINE_SMOOTH );
1628 int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1630 wmRadialControl *rc = (wmRadialControl*)op->customdata;
1631 int mode, initial_mouse[2], delta[2];
1633 double new_value = RNA_float_get(op->ptr, "new_value");
1634 int ret = OPERATOR_RUNNING_MODAL;
1636 mode = RNA_int_get(op->ptr, "mode");
1637 RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
1639 switch(event->type) {
1641 delta[0]= initial_mouse[0] - event->x;
1642 delta[1]= initial_mouse[1] - event->y;
1643 dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
1645 if(mode == WM_RADIALCONTROL_SIZE)
1647 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1648 new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
1649 } else if(mode == WM_RADIALCONTROL_ANGLE)
1650 new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
1653 if(mode == WM_RADIALCONTROL_STRENGTH)
1654 new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
1656 new_value = ((int)new_value + 5) / 10*10;
1662 ret = OPERATOR_CANCELLED;
1666 op->type->exec(C, op);
1667 ret = OPERATOR_FINISHED;
1672 if(new_value > rc->max_value)
1673 new_value = rc->max_value;
1674 else if(new_value < 0)
1677 /* Update paint data */
1678 rc->value = new_value;
1680 RNA_float_set(op->ptr, "new_value", new_value);
1682 if(ret != OPERATOR_RUNNING_MODAL) {
1683 WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
1687 ED_region_tag_redraw(CTX_wm_region(C));
1692 /* Expects the operator customdata to be an ImBuf (or NULL) */
1693 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1695 wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
1696 int mode = RNA_int_get(op->ptr, "mode");
1697 float initial_value = RNA_float_get(op->ptr, "initial_value");
1698 int mouse[2] = {event->x, event->y};
1700 if(mode == WM_RADIALCONTROL_SIZE) {
1701 rc->max_value = 200;
1702 mouse[0]-= initial_value;
1704 else if(mode == WM_RADIALCONTROL_STRENGTH) {
1706 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
1708 else if(mode == WM_RADIALCONTROL_ANGLE) {
1709 rc->max_value = 360;
1710 mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
1711 mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
1712 initial_value *= 180.0f/M_PI;
1715 if(op->customdata) {
1716 ImBuf *im = (ImBuf*)op->customdata;
1717 /* Build GL texture */
1718 glGenTextures(1, &rc->tex);
1719 glBindTexture(GL_TEXTURE_2D, rc->tex);
1720 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
1721 MEM_freeN(im->rect_float);
1725 RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1726 RNA_float_set(op->ptr, "new_value", initial_value);
1728 op->customdata = rc;
1730 rc->initial_value = initial_value;
1731 rc->initial_mouse[0] = mouse[0];
1732 rc->initial_mouse[1] = mouse[1];
1733 rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
1734 wm_radial_control_paint, op->customdata);
1736 /* add modal handler */
1737 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1739 WM_radial_control_modal(C, op, event);
1741 return OPERATOR_RUNNING_MODAL;
1744 /* Gets a descriptive string of the operation */
1745 void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
1747 int mode = RNA_int_get(op->ptr, "mode");
1748 float v = RNA_float_get(op->ptr, "new_value");
1750 if(mode == WM_RADIALCONTROL_SIZE)
1751 sprintf(str, "Size: %d", (int)v);
1752 else if(mode == WM_RADIALCONTROL_STRENGTH)
1753 sprintf(str, "Strength: %d", (int)v);
1754 else if(mode == WM_RADIALCONTROL_ANGLE)
1755 sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
1758 /** Important: this doesn't define an actual operator, it
1759 just sets up the common parts of the radial control op. **/
1760 void WM_OT_radial_control_partial(wmOperatorType *ot)
1762 static EnumPropertyItem prop_mode_items[] = {
1763 {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
1764 {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
1765 {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
1766 {0, NULL, 0, NULL, NULL}};
1768 /* Should be set in custom invoke() */
1769 RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
1771 /* Set internally, should be used in custom exec() to get final value */
1772 RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
1774 /* Should be set before calling operator */
1775 RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
1778 RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1781 /* ************************** timer for testing ***************** */
1783 /* uses no type defines, fully local testing function anyway... ;) */
1785 static int ten_timer_exec(bContext *C, wmOperator *op)
1787 ARegion *ar= CTX_wm_region(C);
1788 double stime= PIL_check_seconds_timer();
1789 int type = RNA_int_get(op->ptr, "type");
1795 for(a=0; a<10; a++) {
1797 ED_region_do_draw(C, ar);
1800 wmWindow *win= CTX_wm_window(C);
1802 ED_region_tag_redraw(ar);
1805 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
1808 wmWindow *win= CTX_wm_window(C);
1811 for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
1812 ED_area_tag_redraw(sa);
1815 CTX_wm_window_set(C, win); /* XXX context manipulation warning! */
1818 Scene *scene= CTX_data_scene(C);
1820 if(a & 1) scene->r.cfra--;
1821 else scene->r.cfra++;
1822 scene_update_for_newframe(scene, scene->lay);
1830 time= (int) ((PIL_check_seconds_timer()-stime)*1000);
1832 if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
1833 if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
1834 if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
1835 if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
1836 if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
1840 uiPupMenuNotice(C, tmpstr);
1842 return OPERATOR_FINISHED;
1845 static void WM_OT_ten_timer(wmOperatorType *ot)
1847 static EnumPropertyItem prop_type_items[] = {
1848 {0, "DRAW", 0, "Draw Region", ""},
1849 {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
1850 {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
1851 {3, "ANIMSTEP", 0, "Anim Step", ""},
1852 {4, "UNDO", 0, "Undo/Redo", ""},
1853 {0, NULL, 0, NULL, NULL}};
1855 ot->name= "Ten Timer";
1856 ot->idname= "WM_OT_ten_timer";
1858 ot->invoke= WM_menu_invoke;
1859 ot->exec= ten_timer_exec;
1860 ot->poll= WM_operator_winactive;
1862 RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
1868 /* ******************************************************* */
1870 /* called on initialize WM_exit() */
1871 void wm_operatortype_free(void)
1875 for(ot= global_ops.first; ot; ot= ot->next)
1877 wm_operatortype_free_macro(ot);
1879 BLI_freelistN(&global_ops);
1882 /* called on initialize WM_init() */
1883 void wm_operatortype_init(void)
1885 WM_operatortype_append(WM_OT_window_duplicate);
1886 WM_operatortype_append(WM_OT_read_homefile);
1887 WM_operatortype_append(WM_OT_save_homefile);
1888 WM_operatortype_append(WM_OT_window_fullscreen_toggle);
1889 WM_operatortype_append(WM_OT_exit_blender);
1890 WM_operatortype_append(WM_OT_open_recentfile);
1891 WM_operatortype_append(WM_OT_open_mainfile);
1892 WM_operatortype_append(WM_OT_recover_last_session);
1893 WM_operatortype_append(WM_OT_jobs_timer);
1894 WM_operatortype_append(WM_OT_save_as_mainfile);
1895 WM_operatortype_append(WM_OT_save_mainfile);
1896 WM_operatortype_append(WM_OT_ten_timer);
1897 WM_operatortype_append(WM_OT_debug_menu);
1898 WM_operatortype_append(WM_OT_search_menu);
1901 /* default keymap for windows and screens, only call once per WM */
1902 void wm_window_keymap(wmWindowManager *wm)
1904 ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
1906 /* items to make WM work */
1907 WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
1909 /* note, this doesn't replace existing keymap items */
1910 WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
1911 WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_CTRL, 0);
1912 WM_keymap_add_item(keymap, "WM_OT_read_homefile", NKEY, KM_PRESS, KM_OSKEY, 0);
1913 WM_keymap_add_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
1914 WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
1915 WM_keymap_add_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_OSKEY, 0);
1916 WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
1917 WM_keymap_add_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
1918 WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_CTRL, 0);
1919 WM_keymap_add_item(keymap, "WM_OT_open_mainfile", OKEY, KM_PRESS, KM_OSKEY, 0);
1920 WM_keymap_add_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
1921 WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_CTRL, 0);
1922 WM_keymap_add_item(keymap, "WM_OT_save_mainfile", SKEY, KM_PRESS, KM_OSKEY, 0);
1923 WM_keymap_add_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
1924 WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
1925 WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", SKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
1926 WM_keymap_add_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
1927 WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, KM_SHIFT, 0);
1928 WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
1931 WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1932 WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
1933 WM_keymap_verify_item(keymap, "WM_OT_search_menu", SPACEKEY, KM_PRESS, 0, 0);