RNA
[blender.git] / source / blender / windowmanager / intern / wm_operators.c
index 39e197ede0d4faac842d017265aaa258708a6dbb..46e9df10adcf2f790af7db5a458b528be588e773 100644 (file)
  */
 
 #include <float.h>
+#define _USE_MATH_DEFINES
 #include <math.h>
 #include <string.h>
 
 #include "DNA_ID.h"
 #include "DNA_screen_types.h"
+#include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
 #include "DNA_windowmanager_types.h"
 
 #include "MEM_guardedalloc.h"
 
+#include "PIL_time.h"
+
 #include "BLI_blenlib.h"
 #include "BLI_dynstr.h" /*for WM_operator_pystring */
 
 #include "BKE_library.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
+#include "BKE_scene.h"
 #include "BKE_utildefines.h"
 
 #include "BIF_gl.h"
 #include "BIF_glutil.h" /* for paint cursor */
 
-#include "ED_fileselect.h"
+#include "IMB_imbuf_types.h"
+
 #include "ED_screen.h"
+#include "ED_util.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 #include "WM_types.h"
 
 #include "wm.h"
-#include "wm_window.h"
-#include "wm_subwindow.h"
+#include "wm_draw.h"
 #include "wm_event_system.h"
+#include "wm_subwindow.h"
+#include "wm_window.h"
 
 
 
@@ -99,7 +107,7 @@ void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
        ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
        ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
        opfunc(ot);
-       RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
+       RNA_def_struct_ui_text(ot->srna, ot->name, ot->description ? ot->description:""); // XXX All ops should have a description but for now allow them not to.
        RNA_def_struct_identifier(ot->srna, ot->idname);
        BLI_addtail(&global_ops, ot);
 }
@@ -146,12 +154,12 @@ char *WM_operator_pystring(wmOperator *op)
 
        BLI_dynstr_appendf(dynstr, "%s(", op->idname);
 
-       iterprop= RNA_struct_iterator_property(op->ptr);
+       iterprop= RNA_struct_iterator_property(op->ptr->type);
        RNA_property_collection_begin(op->ptr, iterprop, &iter);
 
        for(; iter.valid; RNA_property_collection_next(&iter)) {
                prop= iter.ptr.data;
-               arg_name= RNA_property_identifier(&iter.ptr, prop);
+               arg_name= RNA_property_identifier(prop);
 
                if (strcmp(arg_name, "rna_type")==0) continue;
 
@@ -199,39 +207,49 @@ void WM_operator_properties_free(PointerRNA *ptr)
 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
-       const EnumPropertyItem *item;
-       int totitem, i, len= strlen(op->type->name) + 8;
-       char *menu, *p;
-       
-       if(prop) {
-               RNA_property_enum_items(op->ptr, prop, &item, &totitem);
-               
-               for (i=0; i<totitem; i++)
-                       len+= strlen(item[i].name) + 8;
-               
-               menu= MEM_callocN(len, "string");
-               
-               p= menu + sprintf(menu, "%s %%t", op->type->name);
-               for (i=0; i<totitem; i++)
-                       p+= sprintf(p, "|%s %%x%d", item[i].name, item[i].value);
-               
-               uiPupmenuOperator(C, totitem/30, op, "type", menu);
-               MEM_freeN(menu);
-               
-               return OPERATOR_RUNNING_MODAL;
+       uiPopupMenu *pup;
+       uiLayout *layout;
+
+       if(prop==NULL) {
+               printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
        }
+       else if (RNA_property_type(prop) != PROP_ENUM) {
+               printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
+       }
+       else {
+               pup= uiPupMenuBegin(C, op->type->name, 0);
+               layout= uiPupMenuLayout(pup);
+               uiItemsEnumO(layout, op->type->idname, "type");
+               uiPupMenuEnd(C, pup);
+       }
+
        return OPERATOR_CANCELLED;
 }
 
 /* op->invoke */
 int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
 {
-       char buf[512];
-       
-       sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
-       uiPupmenuOperator(C, 0, op, NULL, buf);
+       uiPopupMenu *pup;
+       uiLayout *layout;
+
+       pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
+       layout= uiPupMenuLayout(pup);
+       uiItemO(layout, NULL, 0, op->type->idname);
+       uiPupMenuEnd(C, pup);
        
-       return OPERATOR_RUNNING_MODAL;
+       return OPERATOR_CANCELLED;
+}
+
+/* op->invoke, opens fileselect if filename property not set, otherwise executes */
+int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
+{
+       if (RNA_property_is_set(op->ptr, "filename")) {
+               return WM_operator_call(C, op);
+       } 
+       else {
+               WM_event_add_fileselect(C, op);
+               return OPERATOR_RUNNING_MODAL;
+       }
 }
 
 /* op->poll */
@@ -241,6 +259,211 @@ int WM_operator_winactive(bContext *C)
        return 1;
 }
 
+/* op->invoke */
+static void redo_cb(bContext *C, void *arg_op, void *arg2)
+{
+       wmOperator *lastop= arg_op;
+       
+       if(lastop) {
+               ED_undo_pop(C);
+               WM_operator_repeat(C, lastop);
+       }
+}
+
+static uiBlock *wm_block_create_redo(bContext *C, ARegion *ar, void *arg_op)
+{
+       wmWindowManager *wm= CTX_wm_manager(C);
+       wmOperator *op= arg_op;
+       PointerRNA ptr;
+       uiBlock *block;
+       uiLayout *layout;
+       uiStyle *style= U.uistyles.first;
+       
+       block= uiBeginBlock(C, ar, "redo_popup", UI_EMBOSS);
+       uiBlockClearFlag(block, UI_BLOCK_LOOP);
+       uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
+       uiBlockSetFunc(block, redo_cb, arg_op, NULL);
+
+       if(!op->properties) {
+               IDPropertyTemplate val = {0};
+               op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
+       }
+
+       RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
+       layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
+       uiDefAutoButsRNA(C, layout, &ptr);
+
+       uiPopupBoundsBlock(block, 4.0f, 0, 0);
+       uiEndBlock(C, block);
+
+       return block;
+}
+
+int WM_operator_redo(bContext *C, wmOperator *op, wmEvent *event)
+{
+       int retval= OPERATOR_CANCELLED;
+       
+       if(op->type->exec)
+               retval= op->type->exec(C, op);
+
+       if(retval != OPERATOR_CANCELLED)
+               uiPupBlock(C, wm_block_create_redo, op);
+
+       return retval;
+}
+
+int WM_operator_redo_popup(bContext *C, wmOperator *op)
+{
+       uiPupBlock(C, wm_block_create_redo, op);
+
+       return OPERATOR_CANCELLED;
+}
+
+/* ***************** Debug menu ************************* */
+
+static uiBlock *wm_block_create_menu(bContext *C, ARegion *ar, void *arg_op)
+{
+       wmOperator *op= arg_op;
+       uiBlock *block;
+       uiLayout *layout;
+       uiStyle *style= U.uistyles.first;
+       
+       block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
+       uiBlockClearFlag(block, UI_BLOCK_LOOP);
+       uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN|UI_BLOCK_RET_1);
+       
+       layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 300, 20, style);
+       uiDefAutoButsRNA(C, layout, op->ptr);
+       
+       uiPopupBoundsBlock(block, 4.0f, 0, 0);
+       uiEndBlock(C, block);
+       
+       return block;
+}
+
+static int wm_debug_menu_exec(bContext *C, wmOperator *op)
+{
+       G.rt= RNA_int_get(op->ptr, "debugval");
+       ED_screen_refresh(CTX_wm_manager(C), CTX_wm_window(C));
+       WM_event_add_notifier(C, NC_WINDOW, NULL);
+       
+       return OPERATOR_FINISHED;       
+}
+
+static int wm_debug_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       
+       RNA_int_set(op->ptr, "debugval", G.rt);
+       
+       /* pass on operator, so return modal */
+       uiPupBlockOperator(C, wm_block_create_menu, op, WM_OP_EXEC_DEFAULT);
+       
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static void WM_OT_debug_menu(wmOperatorType *ot)
+{
+       ot->name= "Debug Menu";
+       ot->idname= "WM_OT_debug_menu";
+       
+       ot->invoke= wm_debug_menu_invoke;
+       ot->exec= wm_debug_menu_exec;
+       ot->poll= WM_operator_winactive;
+       
+       RNA_def_int(ot->srna, "debugval", 0, -10000, 10000, "Debug Value", "", INT_MIN, INT_MAX);
+}
+
+/* ***************** Search menu ************************* */
+static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
+{
+       wmOperatorType *ot= arg2;
+       
+       if(ot)
+               WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
+}
+
+static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
+{
+       wmOperatorType *ot = WM_operatortype_first();
+       
+       for(; ot; ot= ot->next) {
+               
+               if(BLI_strcasestr(ot->name, str)) {
+                       if(ot->poll==NULL || ot->poll((bContext *)C)) {
+                               char name[256];
+                               int len= strlen(ot->name);
+                               
+                               /* display name for menu, can hold hotkey */
+                               BLI_strncpy(name, ot->name, 256);
+                               
+                               /* check for hotkey */
+                               if(len < 256-6) {
+                                       if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
+                                               name[len]= '|';
+                               }
+                               
+                               if(0==uiSearchItemAdd(items, name, ot))
+                                       break;
+                       }
+               }
+       }
+}
+
+static uiBlock *wm_block_search_menu(bContext *C, ARegion *ar, void *arg_op)
+{
+       static char search[256]= "";
+       wmEvent event;
+       wmWindow *win= CTX_wm_window(C);
+       uiBlock *block;
+       uiBut *but;
+       
+       block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
+       uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_RET_1);
+       
+       but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 10, 180, 19, "");
+       uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb);
+       
+       /* fake button, it holds space for search items */
+       uiDefBut(block, LABEL, 0, "", 10, 10 - uiSearchBoxhHeight(), 180, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
+       
+       uiPopupBoundsBlock(block, 6.0f, 0, -20); /* move it downwards, mouse over button */
+       uiEndBlock(C, block);
+       
+       event= *(win->eventstate);      /* XXX huh huh? make api call */
+       event.type= EVT_BUT_OPEN;
+       event.val= KM_PRESS;
+       event.customdata= but;
+       event.customdatafree= FALSE;
+       wm_event_add(win, &event);
+       
+       return block;
+}
+
+static int wm_search_menu_exec(bContext *C, wmOperator *op)
+{
+       
+       return OPERATOR_FINISHED;       
+}
+
+static int wm_search_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       
+       uiPupBlock(C, wm_block_search_menu, op);
+       
+       return OPERATOR_CANCELLED;
+}
+
+static void WM_OT_search_menu(wmOperatorType *ot)
+{
+       ot->name= "Search Menu";
+       ot->idname= "WM_OT_search_menu";
+       
+       ot->invoke= wm_search_menu_invoke;
+       ot->exec= wm_search_menu_exec;
+       ot->poll= WM_operator_winactive;
+}
+
+
 /* ************ window / screen operator definitions ************** */
 
 static void WM_OT_window_duplicate(wmOperatorType *ot)
@@ -261,32 +484,23 @@ static void WM_OT_save_homefile(wmOperatorType *ot)
        ot->invoke= WM_operator_confirm;
        ot->exec= WM_write_homefile;
        ot->poll= WM_operator_winactive;
-       
-       ot->flag= OPTYPE_REGISTER;
 }
 
-/* ********* recent file *********** */
-
-static void recent_filelist(char *pup)
+static void WM_OT_read_homefile(wmOperatorType *ot)
 {
-       struct RecentFile *recent;
-       int i, ofs= 0;
-       char *p;
+       ot->name= "Reload Start-Up File";
+       ot->idname= "WM_OT_read_homefile";
        
-       p= pup + sprintf(pup, "Open Recent%%t");
-       
-       if (G.sce[0]) {
-               p+= sprintf(p, "|%s %%x%d", G.sce, 1);
-               ofs = 1;
-       }
+       ot->invoke= WM_operator_confirm;
+       ot->exec= WM_read_homefile;
+       ot->poll= WM_operator_winactive;
        
-       for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
-               if (strcmp(recent->filename, G.sce)) {
-                       p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
-               }
-       }
+       RNA_def_boolean(ot->srna, "factory", 0, "Factory Settings", "");
 }
 
+
+/* ********* recent file *********** */
+
 static int recentfile_exec(bContext *C, wmOperator *op)
 {
        int event= RNA_enum_get(op->ptr, "nr");
@@ -312,12 +526,26 @@ static int recentfile_exec(bContext *C, wmOperator *op)
 
 static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       char pup[2048];
+       struct RecentFile *recent;
+       uiPopupMenu *pup;
+       uiLayout *layout;
+       int i, ofs= 0;
+
+       pup= uiPupMenuBegin(C, "Open Recent", 0);
+       layout= uiPupMenuLayout(pup);
+
+       if(G.sce[0]) {
+               uiItemIntO(layout, G.sce, 0, op->type->idname, "nr", 1);
+               ofs = 1;
+       }
        
-       recent_filelist(pup);
-       uiPupmenuOperator(C, 0, op, "nr", pup);
+       for(recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++)
+               if(strcmp(recent->filename, G.sce))
+                       uiItemIntO(layout, recent->filename, 0, op->type->idname, "nr", i+ofs+1);
+
+       uiPupMenuEnd(C, pup);
        
-       return OPERATOR_RUNNING_MODAL;
+       return OPERATOR_CANCELLED;
 }
 
 static void WM_OT_open_recentfile(wmOperatorType *ot)
@@ -329,31 +557,34 @@ static void WM_OT_open_recentfile(wmOperatorType *ot)
        ot->exec= recentfile_exec;
        ot->poll= WM_operator_winactive;
        
-       ot->flag= OPTYPE_REGISTER;
-       
        RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
-
 }
 
 /* ********* main file *********** */
 
-static int wm_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static void untitled(char *name)
 {
-       SpaceFile *sfile;
-       
-       ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
+       if (G.save_over == 0 && strlen(name) < FILE_MAX-16) {
+               char *c= BLI_last_slash(name);
+               
+               if (c)
+                       strcpy(&c[1], "untitled.blend");
+               else
+                       strcpy(name, "untitled.blend");
+       }
+}
 
-       /* settings for filebrowser */
-       sfile= (SpaceFile*)CTX_wm_space_data(C);
-       sfile->op = op;
-       ED_fileselect_set_params(sfile, FILE_BLENDER, "Load", "C:\\", 0, 0, 0);
 
-       /* screen and area have been reset already in ED_screen_full_newspace */
+static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+
+       RNA_string_set(op->ptr, "filename", G.sce);
+       WM_event_add_fileselect(C, op);
 
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int wm_mainfile_exec(bContext *C, wmOperator *op)
+static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
 {
        char filename[FILE_MAX];
        RNA_string_get(op->ptr, "filename", filename);
@@ -372,8 +603,8 @@ static void WM_OT_open_mainfile(wmOperatorType *ot)
        ot->name= "Open Blender File";
        ot->idname= "WM_OT_open_mainfile";
        
-       ot->invoke= wm_mainfile_invoke;
-       ot->exec= wm_mainfile_exec;
+       ot->invoke= wm_open_mainfile_invoke;
+       ot->exec= wm_open_mainfile_exec;
        ot->poll= WM_operator_winactive;
        
        ot->flag= 0;
@@ -384,36 +615,38 @@ static void WM_OT_open_mainfile(wmOperatorType *ot)
 
 static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       SpaceFile *sfile;
+       char name[FILE_MAX];
        
-       ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
-
-       /* settings for filebrowser */
-       sfile= (SpaceFile*)CTX_wm_space_data(C);
-       sfile->op = op;
-       // XXX replace G.sce
-       ED_fileselect_set_params(sfile, FILE_BLENDER, "Save As", G.sce, 0, 0, 0);
-
-       /* screen and area have been reset already in ED_screen_full_newspace */
+       BLI_strncpy(name, G.sce, FILE_MAX);
+       untitled(name);
+       RNA_string_set(op->ptr, "filename", name);
+       
+       WM_event_add_fileselect(C, op);
 
        return OPERATOR_RUNNING_MODAL;
 }
 
+/* function used for WM_OT_save_mainfile too */
 static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
 {
        char filename[FILE_MAX];
-       RNA_string_get(op->ptr, "filename", filename);
        
+       if(RNA_property_is_set(op->ptr, "filename"))
+               RNA_string_get(op->ptr, "filename", filename);
+       else {
+               BLI_strncpy(filename, G.sce, FILE_MAX);
+               untitled(filename);
+       }
        WM_write_file(C, filename, op->reports);
        
-       WM_event_add_notifier(C, NC_WINDOW, NULL);
+       WM_event_add_notifier(C, NC_WM|ND_FILESAVE, NULL);
 
        return 0;
 }
 
 static void WM_OT_save_as_mainfile(wmOperatorType *ot)
 {
-       ot->name= "Open Blender File";
+       ot->name= "Save As Blender File";
        ot->idname= "WM_OT_save_as_mainfile";
        
        ot->invoke= wm_save_as_mainfile_invoke;
@@ -426,6 +659,36 @@ static void WM_OT_save_as_mainfile(wmOperatorType *ot)
 
 }
 
+/* *************** Save file directly ******** */
+
+static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       char name[FILE_MAX];
+       
+       BLI_strncpy(name, G.sce, FILE_MAX);
+       untitled(name);
+       RNA_string_set(op->ptr, "filename", name);
+       uiPupMenuSaveOver(C, op, name);
+
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static void WM_OT_save_mainfile(wmOperatorType *ot)
+{
+       ot->name= "Save Blender File";
+       ot->idname= "WM_OT_save_mainfile";
+       
+       ot->invoke= wm_save_mainfile_invoke;
+       ot->exec= wm_save_as_mainfile_exec;
+       ot->poll= WM_operator_winactive;
+       
+       ot->flag= 0;
+       
+       RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
+       
+}
+
+
 /* *********************** */
 
 static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
@@ -438,6 +701,15 @@ static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
     ot->poll= WM_operator_winactive;
 }
 
+static int wm_exit_blender_op(bContext *C, wmOperator *op)
+{
+       WM_operator_free(op);
+       
+       WM_exit(C);     
+       
+       return OPERATOR_FINISHED;
+}
+
 static void WM_OT_exit_blender(wmOperatorType *ot)
 {
        ot->name= "Exit Blender";
@@ -496,7 +768,7 @@ void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
    It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
 */
 
-static void border_apply(bContext *C, wmOperator *op, int event_type)
+static int border_apply(bContext *C, wmOperator *op, int event_type)
 {
        wmGesture *gesture= op->customdata;
        rcti *rect= gesture->customdata;
@@ -506,6 +778,9 @@ static void border_apply(bContext *C, wmOperator *op, int event_type)
        if(rect->ymin > rect->ymax)
                SWAP(int, rect->ymin, rect->ymax);
        
+       if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
+               return 0;
+               
        /* operator arguments and storage. */
        RNA_int_set(op->ptr, "xmin", rect->xmin);
        RNA_int_set(op->ptr, "ymin", rect->ymin);
@@ -515,6 +790,8 @@ static void border_apply(bContext *C, wmOperator *op, int event_type)
                RNA_int_set(op->ptr, "event_type", event_type);
        
        op->type->exec(C, op);
+       
+       return 1;
 }
 
 static void wm_gesture_end(bContext *C, wmOperator *op)
@@ -526,6 +803,8 @@ static void wm_gesture_end(bContext *C, wmOperator *op)
 
        ED_area_tag_redraw(CTX_wm_area(C));
        
+       if( RNA_struct_find_property(op->ptr, "cursor") )
+               WM_cursor_restore(CTX_wm_window(C));
 }
 
 int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
@@ -574,9 +853,12 @@ int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
                                }
                        }
                        else {
-                               border_apply(C, op, event->type);
+                               if(border_apply(C, op, event->type)) {
+                                       wm_gesture_end(C, op);
+                                       return OPERATOR_FINISHED;
+                               }
                                wm_gesture_end(C, op);
-                               return OPERATOR_FINISHED;
+                               return OPERATOR_CANCELLED;
                        }
                        break;
                case ESCKEY:
@@ -688,22 +970,10 @@ void WM_OT_circle_gesture(wmOperatorType *ot)
 
 /* **************** Tweak gesture *************** */
 
-static int tweak_gesture_invoke(bContext *C, wmOperator *op, wmEvent *event)
-{
-       op->customdata= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
-       
-       /* add modal handler */
-       WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
-       
-       wm_gesture_tag_redraw(C);
-       
-       return OPERATOR_RUNNING_MODAL;
-}
-
-static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
+static void tweak_gesture_modal(bContext *C, wmEvent *event)
 {
        wmWindow *window= CTX_wm_window(C);
-       wmGesture *gesture= op->customdata;
+       wmGesture *gesture= window->tweak;
        rcti *rect= gesture->customdata;
        int sx, sy, val;
        
@@ -717,7 +987,7 @@ static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
                        
                        if((val= wm_gesture_evaluate(C, gesture))) {
                                wmEvent event;
-                                       
+
                                event= *(window->eventstate);
                                if(gesture->event_type==LEFTMOUSE)
                                        event.type= EVT_TWEAK_L;
@@ -729,11 +999,9 @@ static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
                                /* mouse coords! */
                                wm_event_add(window, &event);
                                
-                               wm_gesture_end(C, op);
-                               return OPERATOR_FINISHED;
+                               WM_gesture_end(C, gesture);     /* frees gesture itself, and unregisters from window */
+                               window->tweak= NULL;
                        }
-                       else
-                               wm_gesture_tag_redraw(C);
                        
                        break;
                        
@@ -741,24 +1009,40 @@ static int tweak_gesture_modal(bContext *C, wmOperator *op, wmEvent *event)
                case RIGHTMOUSE:
                case MIDDLEMOUSE:
                        if(gesture->event_type==event->type) {
-                               wm_gesture_evaluate(C, gesture);
-                               wm_gesture_end(C, op);
-                               return OPERATOR_FINISHED;
+                               WM_gesture_end(C, gesture);
+                               window->tweak= NULL;
+                               
+                               /* when tweak fails we should give the other keymap entries a chance */
+                               event->val= KM_RELEASE;
                        }
                        break;
+               default:
+                       WM_gesture_end(C, gesture);
+                       window->tweak= NULL;
        }
-       return OPERATOR_RUNNING_MODAL;
 }
 
-void WM_OT_tweak_gesture(wmOperatorType *ot)
+/* standard tweak, called after window handlers passed on event */
+void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
 {
-       ot->name= "Tweak Gesture";
-       ot->idname= "WM_OT_tweak_gesture";
+       wmWindow *win= CTX_wm_window(C);
        
-       ot->invoke= tweak_gesture_invoke;
-       ot->modal= tweak_gesture_modal;
-
-       ot->poll= WM_operator_winactive;
+       if(win->tweak==NULL) {
+               if(CTX_wm_region(C)) {
+                       if(event->val) { // pressed
+                               if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
+                                       win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
+                       }
+               }
+       }
+       else {
+               if(action==WM_HANDLER_BREAK) {
+                       WM_gesture_end(C, win->tweak);
+                       win->tweak= NULL;
+               }
+               else
+                       tweak_gesture_modal(C, event);
+       }
 }
 
 /* *********************** lasso gesture ****************** */
@@ -772,6 +1056,24 @@ int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
        
        wm_gesture_tag_redraw(C);
        
+       if( RNA_struct_find_property(op->ptr, "cursor") )
+               WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
+       
+       return OPERATOR_RUNNING_MODAL;
+}
+
+int WM_gesture_lines_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       op->customdata= WM_gesture_new(C, event, WM_GESTURE_LINES);
+       
+       /* add modal handler */
+       WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
+       
+       wm_gesture_tag_redraw(C);
+       
+       if( RNA_struct_find_property(op->ptr, "cursor") )
+               WM_cursor_modal(CTX_wm_window(C), RNA_int_get(op->ptr, "cursor"));
+       
        return OPERATOR_RUNNING_MODAL;
 }
 
@@ -839,6 +1141,11 @@ int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
        return OPERATOR_RUNNING_MODAL;
 }
 
+int WM_gesture_lines_modal(bContext *C, wmOperator *op, wmEvent *event)
+{
+       return WM_gesture_lasso_modal(C, op, event);
+}
+
 #if 0
 /* template to copy from */
 
@@ -875,35 +1182,89 @@ void WM_OT_lasso_gesture(wmOperatorType *ot)
 
 /* *********************** radial control ****************** */
 
+const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
+
 typedef struct wmRadialControl {
-       float radius;
+       int mode;
+       float initial_value, value, max_value;
        int initial_mouse[2];
        void *cursor;
-       // XXX: texture data
+       GLuint tex;
 } wmRadialControl;
 
 static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
 {
-       wmRadialControl *p = (wmRadialControl*)customdata;
+       wmRadialControl *rc = (wmRadialControl*)customdata;
        ARegion *ar = CTX_wm_region(C);
+       float r1=0.0f, r2=0.0f, r3=0.0f, angle=0.0f;
 
        /* Keep cursor in the original place */
-       x = p->initial_mouse[0] - ar->winrct.xmin;
-       y = p->initial_mouse[1] - ar->winrct.ymin;
+       x = rc->initial_mouse[0] - ar->winrct.xmin;
+       y = rc->initial_mouse[1] - ar->winrct.ymin;
+
+       glPushMatrix();
        
        glTranslatef((float)x, (float)y, 0.0f);
-       
+
+       if(rc->mode == WM_RADIALCONTROL_SIZE) {
+               r1= rc->value;
+               r2= rc->initial_value;
+               r3= r1;
+       } else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
+               r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
+               r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
+               r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
+       } else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
+               r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
+               r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
+               angle = rc->value;
+       }
+
        glColor4ub(255, 255, 255, 128);
        glEnable( GL_LINE_SMOOTH );
        glEnable(GL_BLEND);
-       glutil_draw_lined_arc(0.0, M_PI*2.0, p->radius, 40);
+
+       if(rc->mode == WM_RADIALCONTROL_ANGLE)
+               fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
+
+       if(rc->tex) {
+               const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
+
+               if(rc->mode == WM_RADIALCONTROL_ANGLE) {
+                       glRotatef(angle, 0, 0, 1);
+                       fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
+               }
+
+               glBindTexture(GL_TEXTURE_2D, rc->tex);
+
+               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+               glEnable(GL_TEXTURE_2D);
+               glBegin(GL_QUADS);
+               glColor4f(0,0,0, str);
+               glTexCoord2f(0,0);
+               glVertex2f(-r3, -r3);
+               glTexCoord2f(1,0);
+               glVertex2f(r3, -r3);
+               glTexCoord2f(1,1);
+               glVertex2f(r3, r3);
+               glTexCoord2f(0,1);
+               glVertex2f(-r3, r3);
+               glEnd();
+               glDisable(GL_TEXTURE_2D);
+       }
+
+       glColor4ub(255, 255, 255, 128); 
+       glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
+       glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
        glDisable(GL_BLEND);
        glDisable( GL_LINE_SMOOTH );
        
-       glTranslatef((float)-x, (float)-y, 0.0f);
+       glPopMatrix();
 }
 
-static int wm_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
+int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
        wmRadialControl *rc = (wmRadialControl*)op->customdata;
        int mode, initial_mouse[2], delta[2];
@@ -923,13 +1284,16 @@ static int wm_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
                if(mode == WM_RADIALCONTROL_SIZE)
                        new_value = dist;
                else if(mode == WM_RADIALCONTROL_STRENGTH) {
-                       float fin = (200.0f - dist) * 0.5f;
-                       new_value = fin>=0 ? fin : 0;
+                       new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
                } else if(mode == WM_RADIALCONTROL_ANGLE)
                        new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
                
-               if(event->ctrl)
-                       new_value = ((int)new_value + 5) / 10*10;       
+               if(event->ctrl) {
+                       if(mode == WM_RADIALCONTROL_STRENGTH)
+                               new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
+                       else
+                               new_value = ((int)new_value + 5) / 10*10;
+               }
                
                break;
        case ESCKEY:
@@ -943,8 +1307,14 @@ static int wm_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
                break;
        }
 
+       /* Clamp */
+       if(new_value > rc->max_value)
+               new_value = rc->max_value;
+       else if(new_value < 0)
+               new_value = 0;
+
        /* Update paint data */
-       rc->radius = new_value;
+       rc->value = new_value;
 
        RNA_float_set(op->ptr, "new_value", new_value);
 
@@ -958,6 +1328,7 @@ static int wm_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
        return ret;
 }
 
+/* Expects the operator customdata to be an ImBuf (or NULL) */
 int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
@@ -965,19 +1336,37 @@ int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
        float initial_value = RNA_float_get(op->ptr, "initial_value");
        int mouse[2] = {event->x, event->y};
 
-       if(mode == WM_RADIALCONTROL_SIZE)
+       if(mode == WM_RADIALCONTROL_SIZE) {
+               rc->max_value = 200;
                mouse[0]-= initial_value;
-       else if(mode == WM_RADIALCONTROL_STRENGTH)
-               mouse[0]-= 200 - 2*initial_value;
+       }
+       else if(mode == WM_RADIALCONTROL_STRENGTH) {
+               rc->max_value = 1;
+               mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
+       }
        else if(mode == WM_RADIALCONTROL_ANGLE) {
-               mouse[0]-= 200 * cos(initial_value * M_PI / 180.0);
-               mouse[1]-= 200 * sin(initial_value * M_PI / 180.0);
+               rc->max_value = 360;
+               mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
+               mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
+               initial_value *= 180.0f/M_PI;
+       }
+
+       if(op->customdata) {
+               ImBuf *im = (ImBuf*)op->customdata;
+               /* Build GL texture */
+               glGenTextures(1, &rc->tex);
+               glBindTexture(GL_TEXTURE_2D, rc->tex);
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
+               MEM_freeN(im->rect_float);
+               MEM_freeN(im);
        }
 
        RNA_int_set_array(op->ptr, "initial_mouse", mouse);
        RNA_float_set(op->ptr, "new_value", initial_value);
                
        op->customdata = rc;
+       rc->mode = mode;
+       rc->initial_value = initial_value;
        rc->initial_mouse[0] = mouse[0];
        rc->initial_mouse[1] = mouse[1];
        rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
@@ -986,22 +1375,34 @@ int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
        /* add modal handler */
        WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
        
-       wm_radial_control_modal(C, op, event);
+       WM_radial_control_modal(C, op, event);
        
        return OPERATOR_RUNNING_MODAL;
 }
 
+/* Gets a descriptive string of the operation */
+void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
+{
+       int mode = RNA_int_get(op->ptr, "mode");
+       float v = RNA_float_get(op->ptr, "new_value");
+
+       if(mode == WM_RADIALCONTROL_SIZE)
+               sprintf(str, "Size: %d", (int)v);
+       else if(mode == WM_RADIALCONTROL_STRENGTH)
+               sprintf(str, "Strength: %d", (int)v);
+       else if(mode == WM_RADIALCONTROL_ANGLE)
+               sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
+}
+
 /** Important: this doesn't define an actual operator, it
     just sets up the common parts of the radial control op. **/
 void WM_OT_radial_control_partial(wmOperatorType *ot)
 {
        static EnumPropertyItem prop_mode_items[] = {
-               {WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
-               {WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
-               {WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
-               {0, NULL, NULL, NULL}};
-
-       ot->modal= wm_radial_control_modal;
+               {WM_RADIALCONTROL_SIZE, "SIZE", 0, "Size", ""},
+               {WM_RADIALCONTROL_STRENGTH, "STRENGTH", 0, "Strength", ""},
+               {WM_RADIALCONTROL_ANGLE, "ANGLE", 0, "Angle", ""},
+               {0, NULL, 0, NULL, NULL}};
 
        /* Should be set in custom invoke() */
        RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
@@ -1016,6 +1417,93 @@ void WM_OT_radial_control_partial(wmOperatorType *ot)
        RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
 }
 
+/* ************************** timer for testing ***************** */
+
+/* uses no type defines, fully local testing function anyway... ;) */
+
+static int ten_timer_exec(bContext *C, wmOperator *op)
+{
+       ARegion *ar= CTX_wm_region(C);
+       double stime= PIL_check_seconds_timer();
+       int type = RNA_int_get(op->ptr, "type");
+       int a, time;
+       char tmpstr[128];
+       
+       WM_cursor_wait(1);
+       
+       for(a=0; a<10; a++) {
+               if (type==0) {
+                       ED_region_do_draw(C, ar);
+               } 
+               else if (type==1) {
+                       wmWindow *win= CTX_wm_window(C);
+                       
+                       ED_region_tag_redraw(ar);
+                       wm_draw_update(C);
+                       
+                       CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
+               }
+               else if (type==2) {
+                       wmWindow *win= CTX_wm_window(C);
+                       ScrArea *sa;
+                       
+                       for(sa= CTX_wm_screen(C)->areabase.first; sa; sa= sa->next)
+                               ED_area_tag_redraw(sa);
+                       wm_draw_update(C);
+                       
+                       CTX_wm_window_set(C, win);      /* XXX context manipulation warning! */
+               }
+               else if (type==3) {
+                       Scene *scene= CTX_data_scene(C);
+                       
+                       if(a & 1) scene->r.cfra--;
+                       else scene->r.cfra++;
+                       scene_update_for_newframe(scene, scene->lay);
+               }
+               else {
+                       ED_undo_pop(C);
+                       ED_undo_redo(C);
+               }
+       }
+       
+       time= (int) ((PIL_check_seconds_timer()-stime)*1000);
+       
+       if(type==0) sprintf(tmpstr, "10 x Draw Region: %d ms", time);
+       if(type==1) sprintf(tmpstr, "10 x Draw Region and Swap: %d ms", time);
+       if(type==2) sprintf(tmpstr, "10 x Draw Window and Swap: %d ms", time);
+       if(type==3) sprintf(tmpstr, "Anim Step: %d ms", time);
+       if(type==4) sprintf(tmpstr, "10 x Undo/Redo: %d ms", time);
+       
+       WM_cursor_wait(0);
+       
+       uiPupMenuNotice(C, tmpstr);
+       
+       return OPERATOR_FINISHED;
+}
+
+static void WM_OT_ten_timer(wmOperatorType *ot)
+{
+       static EnumPropertyItem prop_type_items[] = {
+       {0, "DRAW", 0, "Draw Region", ""},
+       {1, "DRAWSWAP", 0, "Draw Region + Swap", ""},
+       {2, "DRAWWINSWAP", 0, "Draw Window + Swap", ""},
+       {3, "ANIMSTEP", 0, "Anim Step", ""},
+       {4, "UNDO", 0, "Undo/Redo", ""},
+       {0, NULL, 0, NULL, NULL}};
+       
+       ot->name= "Ten Timer";
+       ot->idname= "WM_OT_ten_timer";
+       
+       ot->invoke= WM_menu_invoke;
+       ot->exec= ten_timer_exec;
+       ot->poll= WM_operator_winactive;
+       
+       RNA_def_enum(ot->srna, "type", prop_type_items, 0, "Type", "");
+
+}
+
+
+
 /* ******************************************************* */
  
 /* called on initialize WM_exit() */
@@ -1028,14 +1516,18 @@ void wm_operatortype_free(void)
 void wm_operatortype_init(void)
 {
        WM_operatortype_append(WM_OT_window_duplicate);
+       WM_operatortype_append(WM_OT_read_homefile);
        WM_operatortype_append(WM_OT_save_homefile);
        WM_operatortype_append(WM_OT_window_fullscreen_toggle);
        WM_operatortype_append(WM_OT_exit_blender);
-       WM_operatortype_append(WM_OT_tweak_gesture);
        WM_operatortype_append(WM_OT_open_recentfile);
        WM_operatortype_append(WM_OT_open_mainfile);
        WM_operatortype_append(WM_OT_jobs_timer);
        WM_operatortype_append(WM_OT_save_as_mainfile);
+       WM_operatortype_append(WM_OT_save_mainfile);
+       WM_operatortype_append(WM_OT_ten_timer);
+       WM_operatortype_append(WM_OT_debug_menu);
+       WM_operatortype_append(WM_OT_search_menu);
 }
 
 /* default keymap for windows and screens, only call once per WM */
@@ -1047,12 +1539,20 @@ void wm_window_keymap(wmWindowManager *wm)
        WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
        
        /* note, this doesn't replace existing keymap items */
-       WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
+       WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
+       WM_keymap_verify_item(keymap, "WM_OT_read_homefile", XKEY, KM_PRESS, KM_CTRL, 0);
        WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
        WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
        WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
+       WM_keymap_verify_item(keymap, "WM_OT_save_mainfile", WKEY, KM_PRESS, KM_CTRL, 0);
        WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
        WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
        WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
+
+       /* debug/testing */
+       WM_keymap_verify_item(keymap, "WM_OT_ten_timer", TKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
+       WM_keymap_verify_item(keymap, "WM_OT_debug_menu", DKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
+       WM_keymap_verify_item(keymap, "WM_OT_search_menu", FKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
+       
 }