merge with 2.5 at r18679
[blender.git] / source / blender / editors / interface / interface_handlers.c
index 761e0ff071264df64b4d1f27c320c3cb6a4a39b2..bfe6057cfce429114fa865435d491b91d6142a77 100644 (file)
@@ -41,7 +41,9 @@
 #include "PIL_time.h"
 
 #include "BKE_colortools.h"
-#include "BKE_global.h"
+#include "BKE_context.h"
+#include "BKE_idprop.h"
+#include "BKE_report.h"
 #include "BKE_texture.h"
 #include "BKE_utildefines.h"
 
 
 #include "UI_interface.h"
 #include "UI_text.h"
-#include "interface.h"
+#include "interface_intern.h"
+
+#include "RNA_access.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 
 /***************** structs and defines ****************/
 
-#define BUTTON_TOOLTIP_DELAY           500
-#define BUTTON_FLASH_DELAY                     20
+#define BUTTON_TOOLTIP_DELAY           0.500
+#define BUTTON_FLASH_DELAY                     0.020
 #define BUTTON_AUTO_OPEN_THRESH                0.3
 #define BUTTON_MOUSE_TOWARDS_THRESH    1.0
 
@@ -92,7 +96,7 @@ typedef struct uiHandleButtonData {
        uiHandleButtonState state;
        int cancel, retval;
        int applied, appliedinteractive;
-       wmTimerHandle *flashtimer;
+       wmTimer *flashtimer;
 
        /* edited value */
        char *str, *origstr;
@@ -104,8 +108,8 @@ typedef struct uiHandleButtonData {
 
        /* tooltip */
        ARegion *tooltip;
-       wmTimerHandle *tooltiptimer;
-       wmTimerHandle *autoopentimer;
+       wmTimer *tooltiptimer;
+       wmTimer *autoopentimer;
 
        /* text selection/editing */
        int maxlen, selextend, selstartx;
@@ -143,6 +147,10 @@ typedef struct uiAfterFunc {
 
        const char *opname;
        int opcontext;
+       PointerRNA *opptr;
+
+       PointerRNA rnapoin;
+       PropertyRNA *rnaprop;
 } uiAfterFunc;
 
 static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState state);
@@ -163,7 +171,7 @@ static void ui_apply_but_func(bContext *C, uiBut *but)
         * handling is done, i.e. menus are closed, in order to avoid conflicts
         * with these functions removing the buttons we are working with */
 
-       if(but->func || block->handle_func || (but->type == BUTM && block->butm_func)) {
+       if(but->func || block->handle_func || (but->type == BUTM && block->butm_func) || but->opname || but->rnaprop) {
                after= MEM_callocN(sizeof(uiAfterFunc), "uiAfterFunc");
 
                after->func= but->func;
@@ -182,6 +190,14 @@ static void ui_apply_but_func(bContext *C, uiBut *but)
 
                after->opname= but->opname;
                after->opcontext= but->opcontext;
+               after->opptr= but->opptr;
+
+               after->rnapoin= but->rnapoin;
+               after->rnaprop= but->rnaprop;
+
+               but->opname= NULL;
+               but->opcontext= 0;
+               but->opptr= NULL;
 
                BLI_addtail(&UIAfterFuncs, after);
        }
@@ -205,8 +221,15 @@ static void ui_apply_but_funcs_after(bContext *C)
                if(after->butm_func)
                        after->butm_func(C, after->butm_func_arg, after->a2);
 
-               if(after->opname) /* make WM_operatora_call option? */
-                       WM_operator_call(C, after->opname, after->opcontext);
+               if(after->opname)
+                       WM_operator_name_call(C, after->opname, after->opcontext, after->opptr);
+               if(after->opptr) {
+                       WM_operator_properties_free(after->opptr);
+                       MEM_freeN(after->opptr);
+               }
+
+               if(after->rnapoin.data)
+                       RNA_property_update(C, &after->rnapoin, after->rnaprop);
        }
 
        BLI_freelistN(&funcs);
@@ -334,7 +357,7 @@ static void ui_apply_but_NUM(bContext *C, uiBut *but, uiHandleButtonData *data)
                /* XXX 2.50 missing python api */
 #if 0
                if(BPY_button_eval(data->str, &data->value)) {
-                       WM_report(C, WM_LOG_WARNING, "Invalid Python expression, check console");
+                       BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console");
                        data->value = 0.0f; /* Zero out value on error */
                        
                        if(data->str[0]) {
@@ -1403,7 +1426,7 @@ static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data
        }
 
        /* this makes adjacent blocks auto open from now on */
-       if(but->block->auto_open==0) but->block->auto_open= 1;
+       //if(but->block->auto_open==0) but->block->auto_open= 1;
 }
 
 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
@@ -1430,6 +1453,10 @@ static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEv
                        button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
                        return WM_UI_HANDLER_BREAK;
                }
+               else if(event->type == LEFTMOUSE && but->block->handle) {
+                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       return WM_UI_HANDLER_BREAK;
+               }
                else if(ELEM(event->type, PADENTER, RETKEY) && event->val) {
                        button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
                        return WM_UI_HANDLER_BREAK;
@@ -2521,14 +2548,14 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, wmEvent *event)
        if(ELEM(event->type, LEFTMOUSE, RETKEY)) {
                if(but->lock) {
                        if(but->lockstr) {
-                               WM_report(C, WM_LOG_WARNING, but->lockstr);
+                               BKE_report(CTX_reports(C), RPT_WARNING, but->lockstr);
                                button_activate_state(C, but, BUTTON_STATE_EXIT);
                                return WM_UI_HANDLER_BREAK;
                        }
                } 
                else if(but->pointype && but->poin==0) {
                        /* there's a pointer needed */
-                       WM_reportf(C, WM_LOG_WARNING, "DoButton pointer error: %s", but->str);
+                       BKE_reportf(CTX_reports(C), RPT_WARNING, "DoButton pointer error: %s", but->str);
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                        return WM_UI_HANDLER_BREAK;
                }
@@ -2659,10 +2686,9 @@ static void ui_blocks_set_tooltips(ARegion *ar, int enable)
                block->tooltipdisabled= !enable;
 }
 
-static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
+static int ui_mouse_inside_region(ARegion *ar, int x, int y)
 {
        uiBlock *block;
-       uiBut *but, *butover= NULL;
        int mx, my;
 
        /* check if the mouse is in the region, and in case of a view2d also check
@@ -2671,7 +2697,7 @@ static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
                for(block=ar->uiblocks.first; block; block=block->next)
                        block->auto_open= 0;
 
-               return NULL;
+               return 0;
        }
 
        if(ar->v2d.mask.xmin!=ar->v2d.mask.xmax) {
@@ -2680,9 +2706,34 @@ static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
                ui_window_to_region(ar, &mx, &my);
 
                if(!BLI_in_rcti(&ar->v2d.mask, mx, my))
-                       return NULL;
+                       return 0;
        }
 
+       return 1;
+}
+
+static int ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y)
+{
+       if(!ui_mouse_inside_region(ar, x, y))
+               return 0;
+
+       ui_window_to_block(ar, but->block, &x, &y);
+
+       if(!ui_but_contains_pt(but, x, y))
+               return 0;
+       
+       return 1;
+}
+
+static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
+{
+       uiBlock *block;
+       uiBut *but, *butover= NULL;
+       int mx, my;
+
+       if(!ui_mouse_inside_region(ar, x, y))
+               return NULL;
+
        for(block=ar->uiblocks.first; block; block=block->next) {
                mx= x;
                my= y;
@@ -2721,7 +2772,7 @@ static void button_tooltip_timer_reset(uiBut *but)
 
        if(U.flag & USER_TOOLTIPS)
                if(!but->block->tooltipdisabled)
-                       data->tooltiptimer= WM_event_add_window_timer(data->window, BUTTON_TOOLTIP_DELAY, ~0);
+                       data->tooltiptimer= WM_event_add_window_timer(data->window, TIMER, BUTTON_TOOLTIP_DELAY);
 }
 
 static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState state)
@@ -2749,7 +2800,7 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s
                                else time= -1;
 
                                if(time >= 0)
-                                       data->autoopentimer= WM_event_add_window_timer(data->window, time*20, ~0);
+                                       data->autoopentimer= WM_event_add_window_timer(data->window, TIMER, 0.02*(double)time);
                        }
                }
        }
@@ -2791,7 +2842,7 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s
 
        /* add a short delay before exiting, to ensure there is some feedback */
        if(state == BUTTON_STATE_WAIT_FLASH) {
-               data->flashtimer= WM_event_add_window_timer(data->window, BUTTON_FLASH_DELAY, ~0);
+               data->flashtimer= WM_event_add_window_timer(data->window, TIMER, BUTTON_FLASH_DELAY);
        }
        else if(data->flashtimer) {
                WM_event_remove_window_timer(data->window, data->flashtimer);
@@ -2823,9 +2874,9 @@ static void button_activate_init(bContext *C, ARegion *ar, uiBut *but, uiButtonA
 
        /* setup struct */
        data= MEM_callocN(sizeof(uiHandleButtonData), "uiHandleButtonData");
-       data->window= C->window;
+       data->window= CTX_wm_window(C);
        data->region= ar;
-       data->interactive= 0;
+       data->interactive= 1;
        data->state = BUTTON_STATE_INIT;
 
        /* activate button */
@@ -2949,7 +3000,7 @@ static int ui_handle_button_event(bContext *C, wmEvent *event, uiBut *but)
        ARegion *ar;
        uiBut *postbut;
        uiButtonActivateType posttype;
-       int retval, mx, my;
+       int retval;
 
        data= but->active;
        block= but->block;
@@ -2961,11 +3012,7 @@ static int ui_handle_button_event(bContext *C, wmEvent *event, uiBut *but)
                switch(event->type) {
                        case MOUSEMOVE:
                                /* verify if we are still over the button, if not exit */
-                               mx= event->x;
-                               my= event->y;
-                               ui_window_to_block(ar, block, &mx, &my);
-
-                               if(!ui_but_contains_pt(but, mx, my)) {
+                               if(!ui_mouse_inside_button(ar, but, event->x, event->y)) {
                                        data->cancel= 1;
                                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                                }
@@ -2990,11 +3037,7 @@ static int ui_handle_button_event(bContext *C, wmEvent *event, uiBut *but)
                                        WM_event_remove_window_timer(data->window, data->autoopentimer);
                                        data->autoopentimer= NULL;
 
-                                       mx= event->x;
-                                       my= event->y;
-                                       ui_window_to_block(ar, block, &mx, &my);
-
-                                       if(ui_but_contains_pt(but, mx, my))
+                                       if(ui_mouse_inside_button(ar, but, event->x, event->y))
                                                button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
                                }
 
@@ -3010,11 +3053,7 @@ static int ui_handle_button_event(bContext *C, wmEvent *event, uiBut *but)
                switch(event->type) {
                        case MOUSEMOVE:
                                /* deselect the button when moving the mouse away */
-                               mx= event->x;
-                               my= event->y;
-                               ui_window_to_block(ar, block, &mx, &my);
-
-                               if(ui_but_contains_pt(but, mx, my)) {
+                               if(ui_mouse_inside_button(ar, but, event->x, event->y)) {
                                        if(!(but->flag & UI_SELECT)) {
                                                but->flag |= UI_SELECT;
                                                data->cancel= 0;
@@ -3082,7 +3121,7 @@ static int ui_handle_button_event(bContext *C, wmEvent *event, uiBut *but)
        return retval;
 }
 
-static void ui_handle_button_closed_submenu(bContext *C, uiBut *but)
+static void ui_handle_button_closed_submenu(bContext *C, wmEvent *event, uiBut *but)
 {
        uiHandleButtonData *data;
        uiMenuBlockHandle *menu;
@@ -3105,8 +3144,15 @@ static void ui_handle_button_closed_submenu(bContext *C, uiBut *but)
 
                button_activate_exit(C, data, but, 1);
        }
-       else if(menu->menuretval == UI_RETURN_OUT)
-               button_activate_state(C, but, BUTTON_STATE_HIGHLIGHT);
+       else if(menu->menuretval == UI_RETURN_OUT) {
+               if(ui_mouse_inside_button(data->region, but, event->x, event->y)) {
+                       button_activate_state(C, but, BUTTON_STATE_HIGHLIGHT);
+               }
+               else {
+                       data->cancel= 1;
+                       button_activate_exit(C, data, but, 1);
+               }
+       }
 }
 
 /* ******************** menu navigation helpers ************** */
@@ -3292,16 +3338,15 @@ int ui_handle_menu_event(bContext *C, wmEvent *event, uiMenuBlockHandle *menu, i
                                if(inside || (block->flag & UI_BLOCK_LOOP)) {
                                        if(event->val) {
                                                but= ui_but_find_activated(ar);
-
                                                if(but) {
-                                                       if(ELEM(event->type, UPARROWKEY, WHEELUPMOUSE)) {
-                                                               if(block->direction & UI_TOP) but= ui_but_next(but);
-                                                               else but= ui_but_prev(but);
-                                                       }
-                                                       else {
+                                                       if(ELEM(event->type, DOWNARROWKEY, WHEELDOWNMOUSE)) {
                                                                if(block->direction & UI_TOP) but= ui_but_prev(but);
                                                                else but= ui_but_next(but);
                                                        }
+                                                       else {
+                                                               if(block->direction & UI_TOP) but= ui_but_next(but);
+                                                               else but= ui_but_prev(but);
+                                                       }
 
                                                        if(but)
                                                                ui_handle_button_activate(C, ar, but, BUTTON_ACTIVATE);
@@ -3451,7 +3496,7 @@ int ui_handle_menu_event(bContext *C, wmEvent *event, uiMenuBlockHandle *menu, i
                return retval;
 }
 
-static int ui_handle_menu_closed_submenu(bContext *C, uiMenuBlockHandle *menu)
+static int ui_handle_menu_closed_submenu(bContext *C, wmEvent *event, uiMenuBlockHandle *menu)
 {
        ARegion *ar;
        uiBut *but;
@@ -3464,6 +3509,7 @@ static int ui_handle_menu_closed_submenu(bContext *C, uiMenuBlockHandle *menu)
 
        but= ui_but_find_activated(ar);
        data= but->active;
+       submenu= data->menu;
 
        if(submenu->menuretval) {
                /* first decide if we want to close our own menu cascading, if
@@ -3477,7 +3523,7 @@ static int ui_handle_menu_closed_submenu(bContext *C, uiMenuBlockHandle *menu)
 
                /* now let activated button in this menu exit, which
                 * will actually close the submenu too */
-               ui_handle_button_closed_submenu(C, but);
+               ui_handle_button_closed_submenu(C, event, but);
        }
 
        if(menu->menuretval)
@@ -3504,7 +3550,7 @@ static int ui_handle_menus_recursive(bContext *C, wmEvent *event, uiMenuBlockHan
        /* now handle events for our own menu */
        if(retval == WM_UI_HANDLER_CONTINUE || event->type == TIMER) {
                if(submenu && submenu->menuretval)
-                       retval= ui_handle_menu_closed_submenu(C, menu);
+                       retval= ui_handle_menu_closed_submenu(C, event, menu);
                else
                        retval= ui_handle_menu_event(C, event, menu, (submenu == NULL));
        }
@@ -3521,7 +3567,7 @@ static int ui_handler_region(bContext *C, wmEvent *event, void *userdata)
        int retval;
 
        /* here we handle buttons at the region level, non-modal */
-       ar= C->region;
+       ar= CTX_wm_region(C);
        retval= WM_UI_HANDLER_CONTINUE;
 
        if(ar==NULL) return retval;
@@ -3530,10 +3576,15 @@ static int ui_handler_region(bContext *C, wmEvent *event, void *userdata)
        /* either handle events for already activated button or try to activate */
        but= ui_but_find_activated(ar);
 
-       if(but)
-               retval= ui_handle_button_event(C, event, but);
-       else
-               retval= ui_handle_button_over(C, event, ar);
+       if(!but || !button_modal_state(but->active->state))
+               retval= ui_handler_panel_region(C, event);
+
+       if(retval == WM_UI_HANDLER_CONTINUE) {
+               if(but)
+                       retval= ui_handle_button_event(C, event, but);
+               else
+                       retval= ui_handle_button_over(C, event, ar);
+       }
 
        /* re-enable tooltips */
        if(event->type == MOUSEMOVE && (event->x!=event->prevx || event->y!=event->prevy))
@@ -3550,12 +3601,12 @@ static void ui_handler_remove_region(bContext *C, void *userdata)
        bScreen *sc;
        ARegion *ar;
 
-       ar= C->region;
+       ar= CTX_wm_region(C);
        if(ar == NULL) return;
 
        uiFreeBlocks(C, &ar->uiblocks);
        
-       sc= C->screen;
+       sc= CTX_wm_screen(C);
        if(sc == NULL) return;
 
        /* delayed apply callbacks, but not for screen level regions, those
@@ -3574,7 +3625,7 @@ static int ui_handler_region_menu(bContext *C, wmEvent *event, void *userdata)
 
        /* here we handle buttons at the window level, modal, for example
         * while number sliding, text editing, or when a menu block is open */
-       ar= C->region;
+       ar= CTX_wm_region(C);
        but= ui_but_find_activated(ar);
 
        if(but) {
@@ -3589,7 +3640,7 @@ static int ui_handler_region_menu(bContext *C, wmEvent *event, void *userdata)
                        /* handle events for the activated button */
                        if(retval == WM_UI_HANDLER_CONTINUE || event->type == TIMER) {
                                if(data->menu->menuretval)
-                                       ui_handle_button_closed_submenu(C, but);
+                                       ui_handle_button_closed_submenu(C, event, but);
                                else
                                        ui_handle_button_event(C, event, but);
                        }
@@ -3611,6 +3662,7 @@ static int ui_handler_region_menu(bContext *C, wmEvent *event, void *userdata)
        return WM_UI_HANDLER_BREAK;
 }
 
+/* two types of popups, one with operator + enum, other with regular callbacks */
 static int ui_handler_popup(bContext *C, wmEvent *event, void *userdata)
 {
        uiMenuBlockHandle *menu= userdata;
@@ -3619,11 +3671,26 @@ static int ui_handler_popup(bContext *C, wmEvent *event, void *userdata)
 
        /* free if done, does not free handle itself */
        if(menu->menuretval) {
+               /* copy values, we have to free first (closes region) */
+               uiMenuBlockHandle temp= *menu;
+               
                ui_menu_block_free(C, menu);
-               WM_event_remove_ui_handler(&C->window->handlers, ui_handler_popup, ui_handler_remove_popup, menu);
+               WM_event_remove_ui_handler(&CTX_wm_window(C)->handlers, ui_handler_popup, ui_handler_remove_popup, menu);
 
-               if(menu->menuretval == UI_RETURN_OK && menu->popup_func)
-                       menu->popup_func(C, menu->popup_arg, menu->retvalue);
+               if(temp.menuretval == UI_RETURN_OK) {
+                       if(temp.popup_func) {
+                               temp.popup_func(C, temp.op_arg, temp.retvalue);
+                       }
+                       else if(temp.op_arg) {
+                               if(temp.propname)
+                                       RNA_enum_set(temp.op_arg->ptr, temp.propname, temp.retvalue);
+                               WM_operator_call(C, temp.op_arg);
+                       }
+               }
+               /* always free operator */
+               else if(temp.op_arg)
+                       WM_operator_free(temp.op_arg);
+               
        }
        else {
                /* re-enable tooltips */
@@ -3659,4 +3726,3 @@ void UI_add_popup_handlers(ListBase *handlers, uiMenuBlockHandle *menu)
 {
        WM_event_add_ui_handler(NULL, handlers, ui_handler_popup, ui_handler_remove_popup, menu);
 }
-