UI List: ctrl click on names in list can be used for renaming.
[blender-staging.git] / source / blender / editors / interface / interface_handlers.c
index e46def3be6fb83ce983072ce8f04f030b1d47eb5..c871fdcf7465846f7a68f8447480d7df3672e838 100644 (file)
@@ -108,6 +108,9 @@ static bool ui_mouse_motion_keynav_test(struct uiKeyNavLock *keynav, const wmEve
 /* pixels to move the cursor to get out of keyboard navigation */
 #define BUTTON_KEYNAV_PX_LIMIT      4
 
+#define MENU_TOWARDS_MARGIN 20  /* margin in pixels */
+#define MENU_TOWARDS_WIGGLE_ROOM 64  /* tolerance in pixels */
+
 typedef enum uiButtonActivateType {
        BUTTON_ACTIVATE_OVER,
        BUTTON_ACTIVATE,
@@ -149,7 +152,9 @@ typedef struct uiHandleButtonData {
        char *str, *origstr;
        double value, origvalue, startvalue;
        float vec[3], origvec[3];
+#if 0  /* UNUSED */
        int togdual, togonly;
+#endif
        ColorBand *coba;
 
        /* tooltip */
@@ -167,6 +172,7 @@ typedef struct uiHandleButtonData {
        /* coords are Window/uiBlock relative (depends on the button) */
        int draglastx, draglasty;
        int dragstartx, dragstarty;
+       int draglastvalue;
        bool dragchange, draglock;
        int dragsel;
        float dragf, dragfstart;
@@ -225,18 +231,42 @@ typedef struct uiAfterFunc {
        bContextStore *context;
 
        char undostr[BKE_UNDO_STR_MAX];
-
-       int autokey;
 } uiAfterFunc;
 
+
+
+static bool ui_is_but_interactive(uiBut *but, const bool ctrl);
 static bool ui_but_contains_pt(uiBut *but, int mx, int my);
 static bool ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y);
+static uiBut *ui_but_find_mouse_over(ARegion *ar, const wmEvent *event, int x, int y);
 static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState state);
 static int ui_handler_region_menu(bContext *C, const wmEvent *event, void *userdata);
 static void ui_handle_button_activate(bContext *C, ARegion *ar, uiBut *but, uiButtonActivateType type);
 static void button_timers_tooltip_remove(bContext *C, uiBut *but);
 
+/* buttons clipboard */
+static ColorBand but_copypaste_coba = {0};
+static CurveMapping but_copypaste_curve = {0};
+static bool but_copypaste_curve_alive = false;
+
 /* ******************** menu navigation helpers ************** */
+enum eSnapType {
+       SNAP_OFF = 0,
+       SNAP_ON,
+       SNAP_ON_SMALL,
+};
+
+static enum eSnapType ui_event_to_snap(const wmEvent *event)
+{
+       return (event->ctrl) ? (event->shift) ? SNAP_ON_SMALL : SNAP_ON : SNAP_OFF;
+}
+
+static void ui_color_snap_hue(const enum eSnapType snap, float *r_hue)
+{
+       const float snap_increment = (snap == SNAP_ON_SMALL) ? 24 : 12;
+       BLI_assert(snap != SNAP_OFF);
+       *r_hue = floorf(0.5f + ((*r_hue) * snap_increment)) / snap_increment;
+}
 
 /* assumes event type is MOUSEPAN */
 void ui_pan_to_scroll(const wmEvent *event, int *type, int *val)
@@ -321,7 +351,7 @@ static uiBut *ui_but_last(uiBlock *block)
 static bool ui_is_a_warp_but(uiBut *but)
 {
        if (U.uiflag & USER_CONTINUOUS_MOUSE) {
-               if (ELEM7(but->type, NUM, NUMSLI, NUMABS, HSVCIRCLE, TRACKPREVIEW, HSVCUBE, BUT_CURVE)) {
+               if (ELEM6(but->type, NUM, NUMSLI, HSVCIRCLE, TRACKPREVIEW, HSVCUBE, BUT_CURVE)) {
                        return true;
                }
        }
@@ -329,7 +359,7 @@ static bool ui_is_a_warp_but(uiBut *but)
        return false;
 }
 
-static float ui_mouse_scale_warp_factor(const short shift)
+static float ui_mouse_scale_warp_factor(const bool shift)
 {
        return shift ? 0.05f : 1.0f;
 }
@@ -337,7 +367,7 @@ static float ui_mouse_scale_warp_factor(const short shift)
 static void ui_mouse_scale_warp(uiHandleButtonData *data,
                                 const float mx, const float my,
                                 float *r_mx, float *r_my,
-                                const short shift)
+                                const bool shift)
 {
        const float fac = ui_mouse_scale_warp_factor(shift);
        
@@ -431,7 +461,7 @@ static void ui_apply_undo(uiBut *but)
 
                /* define which string to use for undo */
                if (ELEM(but->type, LINK, INLINK)) str = "Add button link";
-               else if (ELEM(but->type, MENU, ICONTEXTROW)) str = but->drawstr;
+               else if (but->type == MENU) str = but->drawstr;
                else if (but->drawstr[0]) str = but->drawstr;
                else str = but->tip;
 
@@ -544,7 +574,7 @@ static void ui_apply_but_BUTM(bContext *C, uiBut *but, uiHandleButtonData *data)
 
 static void ui_apply_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data)
 {
-       if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
+       if (but->type == MENU)
                ui_set_but_val(but, data->value);
 
        ui_check_but(but);
@@ -558,16 +588,6 @@ static void ui_apply_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data)
        double value;
        int w, lvalue, push;
        
-       /* local hack... */
-       if (but->type == BUT_TOGDUAL && data->togdual) {
-               if (but->pointype == UI_BUT_POIN_SHORT) {
-                       but->poin += 2;
-               }
-               else if (but->pointype == UI_BUT_POIN_INT) {
-                       but->poin += 4;
-               }
-       }
-       
        value = ui_get_but_val(but);
        lvalue = (int)value;
        
@@ -576,17 +596,6 @@ static void ui_apply_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data)
                if (w) lvalue = UI_BITBUT_CLR(lvalue, but->bitnr);
                else   lvalue = UI_BITBUT_SET(lvalue, but->bitnr);
                
-               if (but->type == TOGR) {
-                       if (!data->togonly) {
-                               lvalue = 1 << (but->bitnr);
-       
-                               ui_set_but_val(but, (double)lvalue);
-                       }
-                       else {
-                               if (lvalue == 0) lvalue = 1 << (but->bitnr);
-                       }
-               }
-               
                ui_set_but_val(but, (double)lvalue);
                if (but->type == ICONTOG || but->type == ICONTOGN) ui_check_but(but);
        }
@@ -600,16 +609,6 @@ static void ui_apply_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data)
                if (but->type == ICONTOG || but->type == ICONTOGN) ui_check_but(but);
        }
        
-       /* end local hack... */
-       if (but->type == BUT_TOGDUAL && data->togdual) {
-               if (but->pointype == UI_BUT_POIN_SHORT) {
-                       but->poin -= 2;
-               }
-               else if (but->pointype == UI_BUT_POIN_INT) {
-                       but->poin -= 4;
-               }
-       }
-       
        ui_apply_but_func(C, but);
 
        data->retval = but->retval;
@@ -673,41 +672,6 @@ static void ui_apply_but_NUM(bContext *C, uiBut *but, uiHandleButtonData *data)
        data->applied = true;
 }
 
-static void ui_apply_but_TOG3(bContext *C, uiBut *but, uiHandleButtonData *data)
-{ 
-       if (but->pointype == UI_BUT_POIN_SHORT) {
-               short *sp = (short *)but->poin;
-               
-               if (UI_BITBUT_TEST(sp[1], but->bitnr)) {
-                       sp[1] = UI_BITBUT_CLR(sp[1], but->bitnr);
-                       sp[0] = UI_BITBUT_CLR(sp[0], but->bitnr);
-               }
-               else if (UI_BITBUT_TEST(sp[0], but->bitnr)) {
-                       sp[1] = UI_BITBUT_SET(sp[1], but->bitnr);
-               }
-               else {
-                       sp[0] = UI_BITBUT_SET(sp[0], but->bitnr);
-               }
-       }
-       else {
-               if (UI_BITBUT_TEST(*(but->poin + 2), but->bitnr)) {
-                       *(but->poin + 2) = UI_BITBUT_CLR(*(but->poin + 2), but->bitnr);
-                       *(but->poin)     = UI_BITBUT_CLR(*(but->poin),     but->bitnr);
-               }
-               else if (UI_BITBUT_TEST(*(but->poin), but->bitnr)) {
-                       *(but->poin + 2) = UI_BITBUT_SET(*(but->poin + 2), but->bitnr);
-               }
-               else {
-                       *(but->poin) = UI_BITBUT_SET(*(but->poin), but->bitnr);
-               }
-       }
-       
-       ui_check_but(but);
-       ui_apply_but_func(C, but);
-       data->retval = but->retval;
-       data->applied = true;
-}
-
 static void ui_apply_but_VEC(bContext *C, uiBut *but, uiHandleButtonData *data)
 {
        ui_set_but_vectorf(but, data->vec);
@@ -732,24 +696,6 @@ static void ui_apply_but_CURVE(bContext *C, uiBut *but, uiHandleButtonData *data
        data->applied = true;
 }
 
-static void ui_apply_but_IDPOIN(bContext *C, uiBut *but, uiHandleButtonData *data)
-{
-       ui_set_but_string(C, but, data->str);
-       ui_check_but(but);
-       ui_apply_but_func(C, but);
-       data->retval = but->retval;
-       data->applied = true;
-}
-
-#ifdef WITH_INTERNATIONAL
-static void ui_apply_but_CHARTAB(bContext *C, uiBut *but, uiHandleButtonData *data)
-{
-       ui_apply_but_func(C, but);
-       data->retval = but->retval;
-       data->applied = true;
-}
-#endif
-
 /* ****************** drag drop code *********************** */
 
 #ifdef USE_DRAG_TOGGLE
@@ -762,6 +708,7 @@ typedef struct uiDragToggleHandle {
        eButType but_type_start;
 
        bool xy_lock[2];
+       int  xy_init[2];
        int  xy_last[2];
 } uiDragToggleHandle;
 
@@ -783,7 +730,9 @@ static bool ui_drag_toggle_set_xy_xy(bContext *C, ARegion *ar, const bool is_set
                ui_window_to_block_fl(ar, block, &xy_b_block[0], &xy_b_block[1]);
 
                for (but = block->buttons.first; but; but = but->next) {
-                       if (ui_is_but_interactive(but)) {
+                       /* Note: ctrl is always true here because (at least for now) we always want to consider text control
+                        *       in this case, even when not embossed. */
+                       if (ui_is_but_interactive(but, true)) {
                                if (BLI_rctf_isect_segment(&but->rect, xy_a_block, xy_b_block)) {
 
                                        /* execute the button */
@@ -809,7 +758,7 @@ static bool ui_drag_toggle_set_xy_xy(bContext *C, ARegion *ar, const bool is_set
        return change;
 }
 
-static void ui_drag_toggle_set(bContext *C, uiDragToggleHandle *drag_info, const int xy_input[2])
+static void ui_drag_toggle_set(bContext *C, uiDragToggleHandle *drag_info, const wmEvent *event)
 {
        ARegion *ar = CTX_wm_region(C);
        bool do_draw = false;
@@ -823,7 +772,7 @@ static void ui_drag_toggle_set(bContext *C, uiDragToggleHandle *drag_info, const
         */
        if (drag_info->is_init == false) {
                /* first store the buttons original coords */
-               uiBut *but = ui_but_find_mouse_over(ar, xy_input[0], xy_input[1]);
+               uiBut *but = ui_but_find_mouse_over(ar, event, 0, 0);
 
                if (but) {
                        if (but->flag & UI_BUT_DRAG_LOCK) {
@@ -851,8 +800,8 @@ static void ui_drag_toggle_set(bContext *C, uiDragToggleHandle *drag_info, const
        /* done with axis locking */
 
 
-       xy[0] = (drag_info->xy_lock[0] == false) ? xy_input[0] : drag_info->xy_last[0];
-       xy[1] = (drag_info->xy_lock[1] == false) ? xy_input[1] : drag_info->xy_last[1];
+       xy[0] = (drag_info->xy_lock[0] == false) ? event->x : drag_info->xy_last[0];
+       xy[1] = (drag_info->xy_lock[1] == false) ? event->y : drag_info->xy_last[1];
 
 
        /* touch all buttons between last mouse coord and this one */
@@ -886,13 +835,20 @@ static int ui_handler_region_drag_toggle(bContext *C, const wmEvent *event, void
                }
                case MOUSEMOVE:
                {
-                       ui_drag_toggle_set(C, drag_info, &event->x);
+                       ui_drag_toggle_set(C, drag_info, event);
                        break;
                }
        }
 
        if (done) {
                wmWindow *win = CTX_wm_window(C);
+               ARegion *ar = CTX_wm_region(C);
+               uiBut *but = ui_but_find_mouse_over(ar, NULL, drag_info->xy_init[0], drag_info->xy_init[1]);
+
+               if (but) {
+                       ui_apply_undo(but);
+               }
+
                WM_event_remove_ui_handler(&win->modalhandlers,
                                           ui_handler_region_drag_toggle,
                                           ui_handler_region_drag_toggle_remove,
@@ -907,6 +863,13 @@ static int ui_handler_region_drag_toggle(bContext *C, const wmEvent *event, void
        }
 }
 
+static bool ui_is_but_drag_toggle(uiBut *but)
+{
+       return ((ui_is_but_bool(but) == true) &&
+               /* menu check is importnt so the button dragged over isn't removed instantly */
+               (ui_block_is_menu(but->block) == false));
+}
+
 #endif  /* USE_DRAG_TOGGLE */
 
 
@@ -922,7 +885,7 @@ static bool ui_but_mouse_inside_icon(uiBut *but, ARegion *ar, const wmEvent *eve
        if (but->imb) {
                /* use button size itself */
        }
-       else if (but->flag & UI_ICON_LEFT) {
+       else if (but->drawflag & UI_BUT_ICON_LEFT) {
                rect.xmax = rect.xmin + (BLI_rcti_size_y(&rect));
        }
        else {
@@ -948,10 +911,15 @@ static bool ui_but_start_drag(bContext *C, uiBut *but, uiHandleButtonData *data,
                        uiDragToggleHandle *drag_info = MEM_callocN(sizeof(*drag_info), __func__);
                        ARegion *ar_prev;
 
+                       /* call here because regular mouse-up event wont run,
+                        * typically 'button_activate_exit()' handles this */
+                       ui_apply_autokey(C, but);
+
                        drag_info->is_set = ui_is_but_push(but);
                        drag_info->but_cent_start[0] = BLI_rctf_cent_x(&but->rect);
                        drag_info->but_cent_start[1] = BLI_rctf_cent_y(&but->rect);
                        drag_info->but_type_start = but->type;
+                       copy_v2_v2_int(drag_info->xy_init, &event->x);
                        copy_v2_v2_int(drag_info->xy_last, &event->x);
 
                        /* needed for toggle drag on popups */
@@ -1283,11 +1251,9 @@ static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleBut
                        break;
                case TOGBUT: 
                case TOG: 
-               case TOGR: 
                case ICONTOG:
                case ICONTOGN:
                case TOGN:
-               case BUT_TOGDUAL:
                case OPTION:
                case OPTIONN:
                        ui_apply_but_TOG(C, but, data);
@@ -1298,17 +1264,10 @@ static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleBut
                        break;
                case SCROLL:
                case NUM:
-               case NUMABS:
-               case SLI:
                case NUMSLI:
                        ui_apply_but_NUM(C, but, data);
                        break;
-               case TOG3:
-                       ui_apply_but_TOG3(C, but, data);
-                       break;
                case MENU:
-               case ICONROW:
-               case ICONTEXTROW:
                case BLOCK:
                case PULLDOWN:
                        ui_apply_but_BLOCK(C, but, data);
@@ -1333,14 +1292,6 @@ static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleBut
                case BUT_CURVE:
                        ui_apply_but_CURVE(C, but, data);
                        break;
-               case IDPOIN:
-                       ui_apply_but_IDPOIN(C, but, data);
-                       break;
-#ifdef WITH_INTERNATIONAL
-               case CHARTAB:
-                       ui_apply_but_CHARTAB(C, but, data);
-                       break;
-#endif
                case KEYEVT:
                case HOTKEYEVT:
                        ui_apply_but_BUT(C, but, data);
@@ -1383,12 +1334,17 @@ static void ui_but_drop(bContext *C, const wmEvent *event, uiBut *but, uiHandleB
        for (wmd = drags->first; wmd; wmd = wmd->next) {
                if (wmd->type == WM_DRAG_ID) {
                        /* align these types with UI_but_active_drop_name */
-                       if (ELEM4(but->type, TEX, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+                       if (ELEM3(but->type, TEX, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                                ID *id = (ID *)wmd->poin;
                                
-                               if (but->poin == NULL && but->rnapoin.data == NULL) {}
                                button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
                                BLI_strncpy(data->str, id->name + 2, data->maxlen);
+
+                               if (ELEM(but->type, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+                                       but->changed = true;
+                                       ui_searchbox_update(C, data->searchbox, but, true);
+                               }
+
                                button_activate_state(C, but, BUTTON_STATE_EXIT);
                        }
                }
@@ -1401,7 +1357,6 @@ static void ui_but_drop(bContext *C, const wmEvent *event, uiBut *but, uiHandleB
 /* c = copy, v = paste */
 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
 {
-       static ColorBand but_copypaste_coba = {0};
        char buf[UI_MAX_DRAW_STR + 1] = {0};
 
        if (mode == 'v' && but->lock  == TRUE) {
@@ -1424,7 +1379,7 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
        }
        
        /* numeric value */
-       if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
+       if (ELEM(but->type, NUM, NUMSLI)) {
                
                if (but->poin == NULL && but->rnapoin.data == NULL) {
                        /* pass */
@@ -1449,6 +1404,32 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
                }
        }
 
+       /* NORMAL button */
+       else if (but->type == BUT_NORMAL) {
+               float xyz[3];
+
+               if (but->poin == NULL && but->rnapoin.data == NULL) {
+                       /* pass */
+               }
+               else if (mode == 'c') {
+                       ui_get_but_vectorf(but, xyz);
+                       BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", xyz[0], xyz[1], xyz[2]);
+                       WM_clipboard_text_set(buf, 0);
+               }
+               else {
+                       if (sscanf(buf, "[%f, %f, %f]", &xyz[0], &xyz[1], &xyz[2]) == 3) {
+                               if (normalize_v3(xyz) == 0.0f) {
+                                       /* better set Z up then have a zero vector */
+                                       xyz[2] = 1.0;
+                               }
+                               button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
+                               ui_set_but_vectorf(but, xyz);
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
+               }
+       }
+
+
        /* RGB triple */
        else if (but->type == COLOR) {
                float rgba[4];
@@ -1488,7 +1469,7 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
        }
 
        /* text/string and ID data */
-       else if (ELEM4(but->type, TEX, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+       else if (ELEM3(but->type, TEX, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                uiHandleButtonData *active_data = but->active;
 
                if (but->poin == NULL && but->rnapoin.data == NULL) {
@@ -1509,6 +1490,7 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
 
                        if (ELEM(but->type, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                                /* else uiSearchboxData.active member is not updated [#26856] */
+                               but->changed = true;
                                ui_searchbox_update(C, data->searchbox, but, true);
                        }
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
@@ -1534,6 +1516,28 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                }
        }
+       else if (but->type == BUT_CURVE) {
+               if (mode == 'c') {
+                       if (but->poin == NULL)
+                               return;
+
+                       but_copypaste_curve_alive = true;
+                       curvemapping_free_data(&but_copypaste_curve);
+                       curvemapping_copy_data(&but_copypaste_curve, (CurveMapping *) but->poin);
+               }
+               else {
+                       if (!but_copypaste_curve_alive)
+                               return;
+
+                       if (!but->poin)
+                               but->poin = MEM_callocN(sizeof(CurveMapping), "curvemapping");
+
+                       button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
+                       curvemapping_free_data((CurveMapping *) but->poin);
+                       curvemapping_copy_data((CurveMapping *) but->poin, &but_copypaste_curve);
+                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+               }
+       }
        /* operator button (any type) */
        else if (but->optype) {
                if (mode == 'c') {
@@ -1541,7 +1545,7 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
                        char *str;
                        opptr = uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
 
-                       str = WM_operator_pystring(C, but->optype, opptr, 0);
+                       str = WM_operator_pystring_ex(C, NULL, false, but->optype, opptr);
 
                        WM_clipboard_text_set(str, 0);
 
@@ -1792,7 +1796,7 @@ static bool ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char as
 }
 
 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, strCursorJumpDirection direction,
-                             int select, strCursorJumpType jump)
+                             const bool select, strCursorJumpType jump)
 {
        const char *str = data->str;
        const int len = strlen(str);
@@ -1917,20 +1921,19 @@ static bool ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int directi
 static bool ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
 {
        char *str;
-       /* TODO, should return false if it cant autocomp. */
-       bool changed = true;
+       bool change = true;
 
        str = data->str;
 
        if (data->searchbox)
-               ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
+               change = ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
        else
-               but->autocomplete_func(C, str, but->autofunc_arg);
+               change = but->autocomplete_func(C, str, but->autofunc_arg);
 
        but->pos = strlen(str);
        but->selsta = but->selend = but->pos;
 
-       return changed;
+       return change;
 }
 
 /* mode for ui_textedit_copypaste() */
@@ -1942,7 +1945,6 @@ enum {
 
 static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const int mode)
 {
-       char buf[UI_MAX_DRAW_STR] = {0};
        char *str, *p, *pbuf;
        int x;
        bool changed = false;
@@ -1958,6 +1960,7 @@ static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const in
                p = pbuf = WM_clipboard_text_get(0);
 
                if (p && p[0]) {
+                       char buf[UI_MAX_DRAW_STR] = {0};
                        unsigned int y;
                        buf_len = 0;
                        while (*p && *p != '\r' && *p != '\n' && buf_len < UI_MAX_DRAW_STR - 1) {
@@ -1994,14 +1997,12 @@ static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const in
        /* cut & copy */
        else if (ELEM(mode, UI_TEXTEDIT_COPY, UI_TEXTEDIT_CUT)) {
                /* copy the contents to the copypaste buffer */
-               for (x = but->selsta; x <= but->selend; x++) {
-                       if (x == but->selend)
-                               buf[x] = '\0';
-                       else
-                               buf[(x - but->selsta)] = str[x];
-               }
+               int sellen = but->selend - but->selsta;
+               char *buf = MEM_mallocN(sizeof(char) * (sellen + 1), "ui_textedit_copypaste");
 
+               BLI_strncpy(buf, str + but->selsta, sellen + 1);
                WM_clipboard_text_set(buf, 0);
+               MEM_freeN(buf);
                
                /* for cut only, delete the selection afterwards */
                if (mode == UI_TEXTEDIT_CUT) {
@@ -2032,7 +2033,7 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
                BLI_str_rstrip_float_zero(data->str, '\0');
        }
 
-       if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
+       if (ELEM(but->type, NUM, NUMSLI)) {
                ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
        }
 
@@ -2060,7 +2061,7 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
 
        ui_check_but(but);
        
-       WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
+       WM_cursor_modal_set(CTX_wm_window(C), BC_TEXTEDITCURSOR);
 }
 
 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
@@ -2092,7 +2093,7 @@ static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
                but->pos = -1;
        }
        
-       WM_cursor_restore(CTX_wm_window(C));
+       WM_cursor_modal_restore(CTX_wm_window(C));
 }
 
 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
@@ -2104,7 +2105,7 @@ static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonDa
                return;
 
        for (but = actbut->next; but; but = but->next) {
-               if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+               if (ELEM5(but->type, TEX, NUM, NUMSLI, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                        if (!(but->flag & UI_BUT_DISABLED)) {
                                data->postbut = but;
                                data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
@@ -2113,7 +2114,7 @@ static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonDa
                }
        }
        for (but = block->buttons.first; but != actbut; but = but->next) {
-               if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+               if (ELEM5(but->type, TEX, NUM, NUMSLI, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                        if (!(but->flag & UI_BUT_DISABLED)) {
                                data->postbut = but;
                                data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
@@ -2132,7 +2133,7 @@ static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonDa
                return;
 
        for (but = actbut->prev; but; but = but->prev) {
-               if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+               if (ELEM5(but->type, TEX, NUM, NUMSLI, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                        if (!(but->flag & UI_BUT_DISABLED)) {
                                data->postbut = but;
                                data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
@@ -2141,7 +2142,7 @@ static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonDa
                }
        }
        for (but = block->buttons.last; but != actbut; but = but->prev) {
-               if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
+               if (ELEM5(but->type, TEX, NUM, NUMSLI, SEARCH_MENU, SEARCH_MENU_UNLINK)) {
                        if (!(but->flag & UI_BUT_DISABLED)) {
                                data->postbut = but;
                                data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
@@ -2176,18 +2177,23 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                        break;
                case RIGHTMOUSE:
                case ESCKEY:
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
-                       retval = WM_UI_HANDLER_BREAK;
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                               retval = WM_UI_HANDLER_BREAK;
+                       }
                        break;
                case LEFTMOUSE:
                {
+                       bool had_selection = but->selsta != but->selend;
+                       
                        /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
                        if (data->searchbox)
                                inbox = ui_searchbox_inside(data->searchbox, event->x, event->y);
 
-                       if (event->val == KM_PRESS) {
+                       /* for double click: we do a press again for when you first click on button (selects all text, no cursor pos) */
+                       if (event->val == KM_PRESS || event->val == KM_DBL_CLICK) {
                                mx = event->x;
                                my = event->y;
                                ui_window_to_block(data->region, block, &mx, &my);
@@ -2208,9 +2214,20 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                        retval = WM_UI_HANDLER_BREAK;
                                }
                        }
-                       else if (inbox) {
-                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       
+                       /* only select a word in button if there was no selection before */
+                       if (event->val == KM_DBL_CLICK && had_selection == false) {
+                               ui_textedit_move(but, data, STRCUR_DIR_PREV, false, STRCUR_JUMP_DELIM);
+                               ui_textedit_move(but, data, STRCUR_DIR_NEXT, true, STRCUR_JUMP_DELIM);
                                retval = WM_UI_HANDLER_BREAK;
+                               changed = true;
+                       }
+                       else if (inbox) {
+                               /* if we allow activation on key press, it gives problems launching operators [#35713] */
+                               if (event->val == KM_RELEASE) {
+                                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                                       retval = WM_UI_HANDLER_BREAK;
+                               }
                        }
                        break;
                }
@@ -2234,12 +2251,12 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                break;
                        case RIGHTARROWKEY:
                                ui_textedit_move(but, data, STRCUR_DIR_NEXT,
-                                                event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
+                                                event->shift != 0, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
                                retval = WM_UI_HANDLER_BREAK;
                                break;
                        case LEFTARROWKEY:
                                ui_textedit_move(but, data, STRCUR_DIR_PREV,
-                                                event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
+                                                event->shift != 0, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
                                retval = WM_UI_HANDLER_BREAK;
                                break;
                        case WHEELDOWNMOUSE:
@@ -2251,10 +2268,10 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                        ui_searchbox_event(C, data->searchbox, but, event);
                                        break;
                                }
-                       /* pass on purposedly */
+                               /* fall-through */
                        case ENDKEY:
                                ui_textedit_move(but, data, STRCUR_DIR_NEXT,
-                                                event->shift, STRCUR_JUMP_ALL);
+                                                event->shift != 0, STRCUR_JUMP_ALL);
                                retval = WM_UI_HANDLER_BREAK;
                                break;
                        case WHEELUPMOUSE:
@@ -2266,10 +2283,10 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                        ui_searchbox_event(C, data->searchbox, but, event);
                                        break;
                                }
-                       /* pass on purposedly */
+                               /* fall-through */
                        case HOMEKEY:
                                ui_textedit_move(but, data, STRCUR_DIR_PREV,
-                                                event->shift, STRCUR_JUMP_ALL);
+                                                event->shift != 0, STRCUR_JUMP_ALL);
                                retval = WM_UI_HANDLER_BREAK;
                                break;
                        case PADENTER:
@@ -2285,13 +2302,21 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
 
                        case BACKSPACEKEY:
                                changed = ui_textedit_delete(but, data, 0,
-                                                            event->shift ? STRCUR_JUMP_ALL : (event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE));
+                                                            event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
                                retval = WM_UI_HANDLER_BREAK;
                                break;
                                
                        case AKEY:
+
                                /* Ctrl + A: Select all */
-                               if (event->ctrl && !(event->alt || event->shift || event->oskey)) {
+#if defined(__APPLE__)
+                               /* OSX uses cmd-a systemwide, so add it */
+                               if ((event->oskey && !(event->alt || event->shift || event->ctrl)) ||
+                                   (event->ctrl  && !(event->alt || event->shift || event->oskey)))
+#else
+                               if (event->ctrl && !(event->alt || event->shift || event->oskey))
+#endif
+                               {
                                        ui_textedit_move(but, data, STRCUR_DIR_PREV,
                                                         false, STRCUR_JUMP_ALL);
                                        ui_textedit_move(but, data, STRCUR_DIR_NEXT,
@@ -2325,7 +2350,7 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
 
                        /* exception that's useful for number buttons, some keyboard
                         * numpads have a comma instead of a period */
-                       if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
+                       if (ELEM(but->type, NUM, NUMSLI)) { /* could use data->min*/
                                if (event->type == PADPERIOD && ascii == ',') {
                                        ascii = '.';
                                        utf8_buf = NULL; /* force ascii fallback */
@@ -2497,14 +2522,6 @@ static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data
                                menustr = but->str;
                        }
                        break;
-               case ICONROW:
-                       menufunc = ui_block_func_ICONROW;
-                       arg = but;
-                       break;
-               case ICONTEXTROW:
-                       menufunc = ui_block_func_ICONTEXTROW;
-                       arg = but;
-                       break;
                case COLOR:
                        ui_get_but_vectorf(but, data->origvec);
                        copy_v3_v3(data->vec, data->origvec);
@@ -2642,9 +2659,11 @@ static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data
                                return WM_UI_HANDLER_BREAK;
                        }
                        else if (event->type == ESCKEY) {
-                               data->cancel = true;
-                               data->escapecancel = true;
-                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                               if (event->val == KM_PRESS) {
+                                       data->cancel = true;
+                                       data->escapecancel = true;
+                                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                               }
                        }
                        
                }
@@ -2681,8 +2700,11 @@ static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, c
 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
 {
        if (data->state == BUTTON_STATE_HIGHLIGHT) {
-               if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
-                       if (but->dt == UI_EMBOSSN && !event->ctrl) {
+               if (ELEM4(event->type, LEFTMOUSE, EVT_BUT_OPEN, PADENTER, RETKEY) && event->val == KM_PRESS) {
+                       if (ELEM(event->type, PADENTER, RETKEY) && (!ui_is_but_utf8(but))) {
+                               /* pass - allow filesel, enter to execute */
+                       }
+                       else if (but->dt == UI_EMBOSSN && !event->ctrl) {
                                /* pass */
                        }
                        else {
@@ -2706,7 +2728,9 @@ static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButton
 static int ui_do_but_SEARCH_UNLINK(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
 {
        /* unlink icon is on right */
-       if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
+       if (ELEM4(event->type, LEFTMOUSE, EVT_BUT_OPEN, PADENTER, RETKEY) && event->val == KM_PRESS &&
+           ui_is_but_search_unlink_visible(but))
+       {
                ARegion *ar = data->region;
                rcti rect;
                int x = event->x, y = event->y;
@@ -2716,15 +2740,15 @@ static int ui_do_but_SEARCH_UNLINK(bContext *C, uiBlock *block, uiBut *but, uiHa
                BLI_rcti_rctf_copy(&rect, &but->rect);
                
                rect.xmin = rect.xmax - (BLI_rcti_size_y(&rect));
-               if ( BLI_rcti_isect_pt(&rect, x, y) ) {
+               if (BLI_rcti_isect_pt(&rect, x, y)) {
                        /* most likely NULL, but let's check, and give it temp zero string */
                        if (data->str == NULL)
-                               data->str = MEM_callocN(16, "temp str");
+                               data->str = MEM_callocN(1, "temp str");
                        data->str[0] = 0;
-                       
+
                        ui_apply_but_TEX(C, but, data);
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
-                       
+
                        return WM_UI_HANDLER_BREAK;
                }
        }
@@ -2735,9 +2759,11 @@ static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, cons
 {
 #ifdef USE_DRAG_TOGGLE
        if (data->state == BUTTON_STATE_HIGHLIGHT) {
-               if (event->type == LEFTMOUSE && event->val == KM_PRESS && ui_is_but_bool(but)) {
+               if (event->type == LEFTMOUSE && event->val == KM_PRESS && ui_is_but_drag_toggle(but)) {
+#if 0          /* UNUSED */
                        data->togdual = event->ctrl;
                        data->togonly = !event->shift;
+#endif
                        ui_apply_button(C, but->block, but, data, true);
                        button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
                        data->dragstartx = event->x;
@@ -2753,8 +2779,10 @@ static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, cons
 #endif
        if (data->state == BUTTON_STATE_HIGHLIGHT) {
                if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
+#if 0          /* UNUSED */
                        data->togdual = event->ctrl;
                        data->togonly = !event->shift;
+#endif
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                        return WM_UI_HANDLER_BREAK;
                }
@@ -2780,7 +2808,7 @@ static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, con
                        }
                }
 #ifdef USE_DRAG_TOGGLE
-               if (event->type == LEFTMOUSE && ui_is_but_bool(but)) {
+               if (event->type == LEFTMOUSE && ui_is_but_drag_toggle(but)) {
                        button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
                        data->dragstartx = event->x;
                        data->dragstarty = event->y;
@@ -2822,9 +2850,10 @@ static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, con
 }
 
 /* var names match ui_numedit_but_NUM */
-static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
+static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange,
+                                    const enum eSnapType snap)
 {
-       if (tempf == softmin || tempf == softmax || snap == 0) {
+       if (tempf == softmin || tempf == softmax || snap == SNAP_OFF) {
                /* pass */
        }
        else {
@@ -2850,16 +2879,19 @@ static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, floa
                        softrange /= fac;
                }
 
-               if (snap == 1) {
+               if (snap == SNAP_ON) {
                        if      (softrange < 2.10f) tempf = 0.1f  * floorf(10.0f * tempf);
                        else if (softrange < 21.0f) tempf = floorf(tempf);
                        else                        tempf = 10.0f * floorf(tempf / 10.0f);
                }
-               else if (snap == 2) {
+               else if (snap == SNAP_ON_SMALL) {
                        if      (softrange < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
                        else if (softrange < 21.0f) tempf = 0.1f  * floorf(10.0f * tempf);
                        else                        tempf = floor(tempf);
                }
+               else {
+                       BLI_assert(0);
+               }
                
                if (fac != 1.0f)
                        tempf *= fac;
@@ -2868,18 +2900,19 @@ static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, floa
        return tempf;
 }
 
-static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
+static float ui_numedit_apply_snap(int temp, float softmin, float softmax,
+                                   const enum eSnapType snap)
 {
        if (temp == softmin || temp == softmax)
                return temp;
 
        switch (snap) {
-               case 0:
+               case SNAP_OFF:
                        break;
-               case 1:
+               case SNAP_ON:
                        temp = 10 * (temp / 10);
                        break;
-               case 2:
+               case SNAP_ON_SMALL:
                        temp = 100 * (temp / 100);
                        break;
        }
@@ -2887,7 +2920,9 @@ static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int s
        return temp;
 }
 
-static bool ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
+static bool ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data,
+                               int mx,
+                               const enum eSnapType snap, float fac)
 {
        float deler, tempf, softmin, softmax, softrange;
        int lvalue, temp;
@@ -3083,9 +3118,11 @@ static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButton
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY || event->type == RIGHTMOUSE) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
                        if (data->dragchange)
@@ -3094,16 +3131,14 @@ static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButton
                                click = 1;
                }
                else if (event->type == MOUSEMOVE) {
+                       const enum eSnapType snap = ui_event_to_snap(event);
                        float fac;
-                       int snap;
 
                        fac = 1.0f;
                        if (event->shift) fac /= 10.0f;
                        if (event->alt)   fac /= 20.0f;
-                       
-                       snap = (event->ctrl) ? (event->shift) ? 2 : 1 : 0;
 
-                       if (ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx : mx)))
+                       if (ui_numedit_but_NUM(but, data, (ui_is_a_warp_but(but) ? screen_mx : mx), snap, fac))
                                ui_numedit_apply(C, block, but, data);
                }
                retval = WM_UI_HANDLER_BREAK;
@@ -3184,7 +3219,8 @@ static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButton
 }
 
 static bool ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data,
-                               const bool is_horizontal, const bool shift, const bool ctrl, int mx)
+                               int mx, const bool is_horizontal,
+                               const bool snap, const bool shift)
 {
        float deler, f, tempf, softmin, softmax, softrange;
        int temp, lvalue;
@@ -3239,7 +3275,7 @@ static bool ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data,
        tempf = softmin + f * softrange;
        temp = floorf(tempf + 0.5f);
 
-       if (ctrl) {
+       if (snap) {
                if (tempf == softmin || tempf == softmax) {
                        /* pass */
                }
@@ -3347,9 +3383,11 @@ static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButton
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY || event->type == RIGHTMOUSE) {
-                       data->cancel = TRUE;
-                       data->escapecancel = TRUE;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = TRUE;
+                               data->escapecancel = TRUE;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
                        if (data->dragchange)
@@ -3358,7 +3396,7 @@ static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButton
                                click = 1;
                }
                else if (event->type == MOUSEMOVE) {
-                       if (ui_numedit_but_SLI(but, data, true, event->shift, event->ctrl, mx))
+                       if (ui_numedit_but_SLI(but, data, mx, true, event->ctrl != 0, event->shift != 0))
                                ui_numedit_apply(C, block, but, data);
                }
                retval = WM_UI_HANDLER_BREAK;
@@ -3464,15 +3502,17 @@ static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleBut
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                }
                else if (event->type == MOUSEMOVE) {
-                       if (ui_numedit_but_SLI(but, data, horizontal, false, false, (horizontal) ? mx : my))
+                       if (ui_numedit_but_SLI(but, data, (horizontal) ? mx : my, horizontal, false, false))
                                ui_numedit_apply(C, block, but, data);
                }
 
@@ -3482,6 +3522,102 @@ static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleBut
        return retval;
 }
 
+static int ui_do_but_LISTBOX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
+{
+       uiList *ui_list = but->custom_data;
+       int *size = (int *)but->poin;
+       int mx, my, raw_dir_sign;
+       int retval = WM_UI_HANDLER_CONTINUE;
+
+       mx = event->x;
+       my = event->y;
+
+       /* We find the direction of the mouse since last time, before converting coordinates into block's space.
+        * We'll use it to avoid flickering in case some rows are higher than UI_UNIT_Y.
+        */
+       raw_dir_sign = (data->draglasty - my < 0) ? -1 : 1;
+       data->draglasty = my;
+
+       ui_window_to_block(data->region, block, &mx, &my);
+
+       if (data->state == BUTTON_STATE_NUM_EDITING) {
+               if (event->type == ESCKEY) {
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               *size = (int)data->origvalue;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                               ui_list->flag &= ~UILST_RESIZING;
+                               ED_region_tag_redraw(data->region);
+                       }
+               }
+               else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
+                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       ui_list->flag &= ~UILST_RESIZING;
+                       ED_region_tag_redraw(data->region);
+               }
+               else if (event->type == MOUSEMOVE) {
+                       /* If we switched from dragged to auto size, suspend shrinking dragging and set dragstarty to a temp
+                        * refpoint.
+                        */
+                       if (data->draglastvalue > 0 && *size == 0) {
+                               data->draglastvalue = *size;
+                               data->dragstartx = data->dragstarty;  /* draglasty already used... */
+                               data->dragstarty = my;
+                       }
+                       else {
+                               int delta = data->dragstarty - my;
+                               /* We only actually do something if the real mousemouve direction matches the "virtual"
+                                * mousemove direction in current block's space. This avoids flickering when drag-resizing lists with
+                                * items drawing higher that UI_UNIT_Y.
+                                */
+                               if (delta * raw_dir_sign > 0) {
+                                       /* Number of rows to show/hide, UI_UNIT_Y should work nice in most cases. */
+                                       delta = (int)floorf(((float)delta / (float)UI_UNIT_Y) + 0.5f);
+
+                                       /* If we are not in autosize mode, default behavior... */
+                                       if (*size > 0 && delta != 0) {
+                                               /* This prevents some instability in case some items draw more/less than UI_UNIT_Y height. */
+                                               delta = (delta < -5) ? -5 : (delta > 5) ? 5 : delta;
+                                               /* We can't use ui_numedit_apply()... */
+                                               /* list template will clamp, but we do not want to reach 0 aka autosize mode! */
+                                               *size = max_ii(*size + delta, 1);
+
+                                               /* Used to detect switch to/from autosize mode. */
+                                               data->draglastvalue = *size;
+
+                                               data->dragchange = true;
+                                               data->applied = data->applied_interactive = true;
+
+                                               ui_list->flag |= UILST_SCROLL_TO_ACTIVE_ITEM;
+                                               ED_region_tag_redraw(data->region);
+                                       }
+                                       /* If we are leaving autosize mode (growing dragging), restore to minimal size. */
+                                       else if (delta > 0) {
+                                               /* We can't use ui_numedit_apply()... */
+                                               *size = ui_list->dyn_data->visual_height_min;
+
+                                               /* Restore real dragstarty value! */
+                                               data->dragstarty = data->dragstartx;
+
+                                               /* Used to detect switch to/from autosize mode. */
+                                               data->draglastvalue = *size;
+
+                                               data->dragchange = true;
+                                               data->applied = data->applied_interactive = true;
+
+                                               ui_list->flag |= UILST_SCROLL_TO_ACTIVE_ITEM;
+                                               ED_region_tag_redraw(data->region);
+                                       }
+                               }
+                       }
+               }
+
+               retval = WM_UI_HANDLER_BREAK;
+       }
+
+       return retval;
+}
 
 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
 {
@@ -3498,7 +3634,7 @@ static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, co
                        }
                }
 #ifdef USE_DRAG_TOGGLE
-               if (event->type == LEFTMOUSE && ui_is_but_bool(but)) {
+               if (event->type == LEFTMOUSE && ui_is_but_drag_toggle(but)) {
                        button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
                        data->dragstartx = event->x;
                        data->dragstarty = event->y;
@@ -3510,7 +3646,7 @@ static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, co
                        button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
                        return WM_UI_HANDLER_BREAK;
                }
-               else if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
+               else if (but->type == MENU) {
                        if (ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
                                const int direction = (event->type == WHEELDOWNMOUSE) ? -1 : 1;
 
@@ -3542,31 +3678,6 @@ static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, co
                                return WM_UI_HANDLER_BREAK;
                        }
                }
-               else if (but->type == COLOR) {
-                       if (ELEM3(event->type, MOUSEPAN, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
-                               float *hsv = ui_block_hsv_get(but->block);
-                               float col[3];
-                               
-                               ui_get_but_vectorf(but, col);
-                               rgb_to_hsv_compat_v(col, hsv);
-
-                               if (event->type == WHEELDOWNMOUSE)
-                                       hsv[2] = CLAMPIS(hsv[2] - 0.05f, 0.0f, 1.0f);
-                               else if (event->type == WHEELUPMOUSE)
-                                       hsv[2] = CLAMPIS(hsv[2] + 0.05f, 0.0f, 1.0f);
-                               else {
-                                       float fac = 0.005 * (event->y - event->prevy);
-                                       hsv[2] = CLAMPIS(hsv[2] + fac, 0.0f, 1.0f);
-                               }
-                               
-                               hsv_to_rgb_v(hsv, data->vec);
-                               ui_set_but_vectorf(but, data->vec);
-                               
-                               button_activate_state(C, but, BUTTON_STATE_EXIT);
-                               ui_apply_button(C, but->block, but, data, true);
-                               return WM_UI_HANDLER_BREAK;
-                       }
-               }
        }
        else if (data->state == BUTTON_STATE_WAIT_DRAG) {
                
@@ -3592,7 +3703,9 @@ static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, co
        return WM_UI_HANDLER_CONTINUE;
 }
 
-static bool ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
+static bool ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data,
+                                  int mx, int my,
+                                  const enum eSnapType snap)
 {
        float dx, dy, rad, radsq, mrad, *fp;
        int mdx, mdy;
@@ -3648,39 +3761,93 @@ static bool ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx,
        }
        normalize_v3(fp);
 
+       if (snap != SNAP_OFF) {
+               const int snap_steps = (snap == SNAP_ON) ? 4 : 12;  /* 45 or 15 degree increments */
+               const float snap_steps_angle = M_PI / snap_steps;
+               float angle, angle_snap;
+               int i;
+
+               /* round each axis of 'fp' to the next increment
+                * do this in "angle" space - this gives increments of same size */
+               for (i = 0; i < 3; i++) {
+                       angle = asinf(fp[i]);
+                       angle_snap = floorf(0.5f + (angle / snap_steps_angle)) * snap_steps_angle;
+                       fp[i] = sinf(angle_snap);
+               }
+               normalize_v3(fp);
+               changed = !compare_v3v3(fp, data->origvec, FLT_EPSILON);
+       }
+
        data->draglastx = mx;
        data->draglasty = my;
 
        return changed;
 }
 
-static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
+static int ui_do_but_COLOR(bContext *C, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
 {
-       int mx, my;
-
-       mx = event->x;
-       my = event->y;
-       ui_window_to_block(data->region, block, &mx, &my);
-
        if (data->state == BUTTON_STATE_HIGHLIGHT) {
-               if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
-                       data->dragstartx = mx;
-                       data->dragstarty = my;
-                       data->draglastx = mx;
-                       data->draglasty = my;
-                       button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
-
-                       /* also do drag the first time */
-                       if (ui_numedit_but_NORMAL(but, data, mx, my))
-                               ui_numedit_apply(C, block, but, data);
-                       
+               if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
+                       button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
+                       return WM_UI_HANDLER_BREAK;
+               }
+               else if (ELEM3(event->type, MOUSEPAN, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
+                       float *hsv = ui_block_hsv_get(but->block);
+                       float col[3];
+
+                       ui_get_but_vectorf(but, col);
+                       rgb_to_hsv_compat_v(col, hsv);
+
+                       if (event->type == WHEELDOWNMOUSE)
+                               hsv[2] = CLAMPIS(hsv[2] - 0.05f, 0.0f, 1.0f);
+                       else if (event->type == WHEELUPMOUSE)
+                               hsv[2] = CLAMPIS(hsv[2] + 0.05f, 0.0f, 1.0f);
+                       else {
+                               float fac = 0.005 * (event->y - event->prevy);
+                               hsv[2] = CLAMPIS(hsv[2] + fac, 0.0f, 1.0f);
+                       }
+
+                       hsv_to_rgb_v(hsv, data->vec);
+                       ui_set_but_vectorf(but, data->vec);
+
+                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       ui_apply_button(C, but->block, but, data, true);
+                       return WM_UI_HANDLER_BREAK;
+               }
+       }
+
+       return WM_UI_HANDLER_CONTINUE;
+}
+
+static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
+{
+       int mx, my;
+
+       mx = event->x;
+       my = event->y;
+       ui_window_to_block(data->region, block, &mx, &my);
+
+       if (data->state == BUTTON_STATE_HIGHLIGHT) {
+               if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
+                       const enum eSnapType snap = ui_event_to_snap(event);
+                       data->dragstartx = mx;
+                       data->dragstarty = my;
+                       data->draglastx = mx;
+                       data->draglasty = my;
+                       button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
+
+                       /* also do drag the first time */
+                       if (ui_numedit_but_NORMAL(but, data, mx, my, snap))
+                               ui_numedit_apply(C, block, but, data);
+                       
                        return WM_UI_HANDLER_BREAK;
                }
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
-                               if (ui_numedit_but_NORMAL(but, data, mx, my))
+                               const enum eSnapType snap = ui_event_to_snap(event);
+                               if (ui_numedit_but_NORMAL(but, data, mx, my, snap))
                                        ui_numedit_apply(C, block, but, data);
                        }
                }
@@ -3707,15 +3874,17 @@ static void clamp_axis_max_v3(float v[3], const float max)
        }
 }
 
-static bool ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my, const short shift)
+static bool ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data,
+                                   int mx, int my,
+                                   const enum eSnapType snap, const bool shift)
 {
        float rgb[3];
        float *hsv = ui_block_hsv_get(but->block);
        float x, y;
        float mx_fl, my_fl;
        bool changed = true;
-       int color_profile = but->block->color_profile;
-       
+       bool use_display_colorspace = ui_hsvcube_use_display_colorspace(but);
+
        ui_mouse_scale_warp(data, mx, my, &mx_fl, &my_fl, shift);
 
 #ifdef USE_CONT_MOUSE_CORRECT
@@ -3727,14 +3896,9 @@ static bool ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx,
        }
 #endif
 
-       if (but->rnaprop) {
-               if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
-                       color_profile = FALSE;
-       }
-
        ui_get_but_vectorf(but, rgb);
 
-       if (color_profile && (int)but->a1 != UI_GRAD_SV)
+       if (use_display_colorspace)
                ui_block_to_display_space_v3(but->block, rgb);
 
        rgb_to_hsv_compat_v(rgb, hsv);
@@ -3748,7 +3912,7 @@ static bool ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx,
                
                /* calculate original hsv again */
                copy_v3_v3(rgb, data->origvec);
-               if (color_profile && (int)but->a1 != UI_GRAD_SV)
+               if (use_display_colorspace)
                        ui_block_to_display_space_v3(but->block, rgb);
                
                copy_v3_v3(hsvo, ui_block_hsv_get(but->block));
@@ -3797,12 +3961,19 @@ static bool ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx,
 
                        break;
                default:
-                       assert(!"invalid hsv type");
+                       BLI_assert(0);
+                       break;
+       }
+
+       if (snap != SNAP_OFF) {
+               if (ELEM3((int)but->a1, UI_GRAD_HV, UI_GRAD_HS, UI_GRAD_H)) {
+                       ui_color_snap_hue(snap, &hsv[0]);
+               }
        }
 
        hsv_to_rgb_v(hsv, rgb);
 
-       if (color_profile && ((int)but->a1 != UI_GRAD_SV))
+       if (use_display_colorspace)
                ui_block_to_scene_linear_v3(but->block, rgb);
 
        /* clamp because with color conversion we can exceed range [#34295] */
@@ -3818,22 +3989,18 @@ static bool ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx,
        return changed;
 }
 
-static void ui_ndofedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, wmNDOFMotionData *ndof, const short shift)
+static void ui_ndofedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data,
+                                    wmNDOFMotionData *ndof,
+                                    const enum eSnapType snap, const bool shift)
 {
        float *hsv = ui_block_hsv_get(but->block);
        float rgb[3];
        float sensitivity = (shift ? 0.15f : 0.3f) * ndof->dt;
-       
-       int color_profile = but->block->color_profile;
-       
-       if (but->rnaprop) {
-               if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
-                       color_profile = FALSE;
-       }
+       bool use_display_colorspace = ui_hsvcube_use_display_colorspace(but);
 
        ui_get_but_vectorf(but, rgb);
 
-       if (color_profile && (int)but->a1 != UI_GRAD_SV)
+       if (use_display_colorspace)
                ui_block_to_display_space_v3(but->block, rgb);
 
        rgb_to_hsv_compat_v(rgb, hsv);
@@ -3867,13 +4034,21 @@ static void ui_ndofedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, wmNDOF
                        hsv[2] += ndof->rx * sensitivity;
                        
                        CLAMP(hsv[2], but->softmin, but->softmax);
+                       break;
                default:
                        assert(!"invalid hsv type");
+                       break;
+       }
+
+       if (snap != SNAP_OFF) {
+               if (ELEM3((int)but->a1, UI_GRAD_HV, UI_GRAD_HS, UI_GRAD_H)) {
+                       ui_color_snap_hue(snap, &hsv[0]);
+               }
        }
 
        hsv_to_rgb_v(hsv, rgb);
 
-       if (color_profile && (int)but->a1 != UI_GRAD_SV)
+       if (use_display_colorspace)
                ui_block_to_scene_linear_v3(but->block, rgb);
 
        copy_v3_v3(data->vec, rgb);
@@ -3890,6 +4065,8 @@ static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleBu
 
        if (data->state == BUTTON_STATE_HIGHLIGHT) {
                if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
+                       const enum eSnapType snap = ui_event_to_snap(event);
+
                        data->dragstartx = mx;
                        data->dragstarty = my;
                        data->draglastx = mx;
@@ -3897,15 +4074,16 @@ static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleBu
                        button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
 
                        /* also do drag the first time */
-                       if (ui_numedit_but_HSVCUBE(but, data, mx, my, event->shift))
+                       if (ui_numedit_but_HSVCUBE(but, data, mx, my, snap, event->shift != 0))
                                ui_numedit_apply(C, block, but, data);
                        
                        return WM_UI_HANDLER_BREAK;
                }
                else if (event->type == NDOF_MOTION) {
                        wmNDOFMotionData *ndof = (wmNDOFMotionData *) event->customdata;
+                       const enum eSnapType snap = ui_event_to_snap(event);
                        
-                       ui_ndofedit_but_HSVCUBE(but, data, ndof, event->shift);
+                       ui_ndofedit_but_HSVCUBE(but, data, ndof, snap, event->shift != 0);
                        
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                        ui_apply_button(C, but->block, but, data, true);
@@ -3948,13 +4126,17 @@ static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleBu
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY || event->type == RIGHTMOUSE) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
-                               if (ui_numedit_but_HSVCUBE(but, data, mx, my, event->shift))
+                               const enum eSnapType snap = ui_event_to_snap(event);
+
+                               if (ui_numedit_but_HSVCUBE(but, data, mx, my, snap, event->shift != 0))
                                        ui_numedit_apply(C, block, but, data);
                        }
                }
@@ -3968,7 +4150,9 @@ static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleBu
        return WM_UI_HANDLER_CONTINUE;
 }
 
-static bool ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, float mx, float my, int shift)
+static bool ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data,
+                                     float mx, float my,
+                                     const enum eSnapType snap, const bool shift)
 {
        rcti rect;
        bool changed = true;
@@ -4026,6 +4210,10 @@ static bool ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, float
        if (but->flag & UI_BUT_COLOR_CUBIC)
                hsv[1] = 1.0f - sqrt3f(1.0f - hsv[1]);
 
+       if (snap != SNAP_OFF) {
+               ui_color_snap_hue(snap, &hsv[0]);
+       }
+
        hsv_to_rgb_v(hsv, rgb);
 
        if ((but->flag & UI_BUT_VEC_SIZE_LOCK) && (rgb[0] || rgb[1] || rgb[2])) {
@@ -4041,7 +4229,9 @@ static bool ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, float
        return changed;
 }
 
-static void ui_ndofedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, wmNDOFMotionData *ndof, const short shift)
+static void ui_ndofedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data,
+                                      wmNDOFMotionData *ndof,
+                                      const enum eSnapType snap, const bool shift)
 {
        float *hsv = ui_block_hsv_get(but->block);
        float rgb[3];
@@ -4082,7 +4272,11 @@ static void ui_ndofedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, wmND
        if (but->flag & UI_BUT_COLOR_LOCK) { // lock
                if (hsv[2] == 0.0f) hsv[2] = 0.0001f;
        }
-       
+
+       if (snap != SNAP_OFF) {
+               ui_color_snap_hue(snap, &hsv[0]);
+       }
+
        hsv_to_rgb_v(hsv, data->vec);
        
        if ((but->flag & UI_BUT_VEC_SIZE_LOCK) && (data->vec[0] || data->vec[1] || data->vec[2])) {
@@ -4103,6 +4297,7 @@ static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandle
        
        if (data->state == BUTTON_STATE_HIGHLIGHT) {
                if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
+                       const enum eSnapType snap = ui_event_to_snap(event);
                        data->dragstartx = mx;
                        data->dragstarty = my;
                        data->draglastx = mx;
@@ -4110,15 +4305,16 @@ static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandle
                        button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
                        
                        /* also do drag the first time */
-                       if (ui_numedit_but_HSVCIRCLE(but, data, mx, my, event->shift))
+                       if (ui_numedit_but_HSVCIRCLE(but, data, mx, my, snap, event->shift != 0))
                                ui_numedit_apply(C, block, but, data);
                        
                        return WM_UI_HANDLER_BREAK;
                }
                else if (event->type == NDOF_MOTION) {
+                       const enum eSnapType snap = ui_event_to_snap(event);
                        wmNDOFMotionData *ndof = (wmNDOFMotionData *) event->customdata;
                        
-                       ui_ndofedit_but_HSVCIRCLE(but, data, ndof, event->shift);
+                       ui_ndofedit_but_HSVCIRCLE(but, data, ndof, snap, event->shift != 0);
 
                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                        ui_apply_button(C, but->block, but, data, true);
@@ -4159,9 +4355,11 @@ static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandle
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY || event->type == RIGHTMOUSE) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                /* XXX hardcoded keymap check.... */
                else if (event->type == WHEELDOWNMOUSE) {
@@ -4178,8 +4376,11 @@ static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandle
                }
                else if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
-                               if (ui_numedit_but_HSVCIRCLE(but, data, mx, my, event->shift))
+                               const enum eSnapType snap = ui_event_to_snap(event);
+
+                               if (ui_numedit_but_HSVCIRCLE(but, data, mx, my, snap, event->shift != 0)) {
                                        ui_numedit_apply(C, block, but, data);
+                               }
                        }
                }
                else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
@@ -4274,8 +4475,9 @@ static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandle
        return WM_UI_HANDLER_CONTINUE;
 }
 
-static bool ui_numedit_but_CURVE(uiBlock *block, uiBut *but, uiHandleButtonData *data, int snap,
-                                int evtx, int evty, const short shift)
+static bool ui_numedit_but_CURVE(uiBlock *block, uiBut *but, uiHandleButtonData *data,
+                                 int evtx, int evty,
+                                 bool snap, const bool shift)
 {
        CurveMapping *cumap = (CurveMapping *)but->poin;
        CurveMap *cuma = cumap->cm + cumap->cur;
@@ -4303,7 +4505,7 @@ static bool ui_numedit_but_CURVE(uiBlock *block, uiBut *but, uiHandleButtonData
                d[1] = my - data->dragstarty;
 
                if (len_v2(d) < 3.0f)
-                       snap = 0;
+                       snap = false;
        }
 
        if (data->dragsel != -1) {
@@ -4498,7 +4700,7 @@ static int ui_do_but_CURVE(bContext *C, uiBlock *block, uiBut *but, uiHandleButt
                if (event->type == MOUSEMOVE) {
                        if (event->x != data->draglastx || event->y != data->draglasty) {
                                
-                               if (ui_numedit_but_CURVE(block, but, data, event->ctrl, event->x, event->y, event->shift))
+                               if (ui_numedit_but_CURVE(block, but, data, event->x, event->y, event->ctrl != 0, event->shift != 0))
                                        ui_numedit_apply(C, block, but, data);
                        }
                }
@@ -4604,9 +4806,11 @@ static int ui_do_but_HISTOGRAM(bContext *C, uiBlock *block, uiBut *but, uiHandle
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
@@ -4687,9 +4891,11 @@ static int ui_do_but_WAVEFORM(bContext *C, uiBlock *block, uiBut *but, uiHandleB
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
@@ -4754,9 +4960,11 @@ static int ui_do_but_VECTORSCOPE(bContext *C, uiBlock *block, uiBut *but, uiHand
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
@@ -4773,112 +4981,6 @@ static int ui_do_but_VECTORSCOPE(bContext *C, uiBlock *block, uiBut *but, uiHand
        return WM_UI_HANDLER_CONTINUE;
 }
 
-#ifdef WITH_INTERNATIONAL
-static int ui_do_but_CHARTAB(bContext *UNUSED(C), uiBlock *UNUSED(block), uiBut *UNUSED(but), uiHandleButtonData *UNUSED(data), const wmEvent *UNUSED(event))
-{
-       /* XXX 2.50 bad global and state access */
-#if 0
-       float sx, sy, ex, ey;
-       float width, height;
-       float butw, buth;
-       int mx, my, x, y, cs, che;
-
-       mx = event->x;
-       my = event->y;
-       ui_window_to_block(data->region, block, &mx, &my);
-
-       if (data->state == BUTTON_STATE_HIGHLIGHT) {
-               if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
-                       /* Calculate the size of the button */
-                       width  = abs(BLI_rctf_size_x(&but->rect));
-                       height = abs(BLI_rctf_size_y(&but->rect));
-
-                       butw = floor(width / 12);
-                       buth = floor(height / 6);
-
-                       /* Initialize variables */
-                       sx = but->rect.xmin;
-                       ex = but->rect.xmin + butw;
-                       sy = but->rect.ymin + height - buth;
-                       ey = but->rect.ymin + height;
-
-                       cs = G.charstart;
-
-                       /* And the character is */
-                       x = (int) ((mx / butw) - 0.5);
-                       y = (int) (6 - ((my / buth) - 0.5));
-
-                       che = cs + (y * 12) + x;
-
-                       if (che > G.charmax)
-                               che = 0;
-
-                       if (G.obedit) {
-                               do_textedit(0, 0, che);
-                       }
-
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
-                       return WM_UI_HANDLER_BREAK;
-               }
-               else if (ELEM(event->type, WHEELUPMOUSE, PAGEUPKEY)) {
-                       for (but = block->buttons.first; but; but = but->next) {
-                               if (but->type == CHARTAB) {
-                                       G.charstart = G.charstart - (12 * 6);
-                                       if (G.charstart < 0)
-                                               G.charstart = 0;
-                                       if (G.charstart < G.charmin)
-                                               G.charstart = G.charmin;
-                                       ui_draw_but(but);
-
-                                       //Really nasty... to update the num button from the same butblock
-                                       for (bt = block->buttons.first; bt; bt = bt->next)
-                                       {
-                                               if (ELEM(bt->type, NUM, NUMABS)) {
-                                                       ui_check_but(bt);
-                                                       ui_draw_but(bt);
-                                               }
-                                       }
-                                       retval = UI_CONT;
-                                       break;
-                               }
-                       }
-
-                       return WM_UI_HANDLER_BREAK;
-               }
-               else if (ELEM(event->type, WHEELDOWNMOUSE, PAGEDOWNKEY)) {
-                       for (but = block->buttons.first; but; but = but->next) {
-                               if (but->type == CHARTAB) {
-                                       G.charstart = G.charstart + (12 * 6);
-                                       if (G.charstart > (0xffff - 12 * 6))
-                                               G.charstart = 0xffff - (12 * 6);
-                                       if (G.charstart > G.charmax - 12 * 6)
-                                               G.charstart = G.charmax - 12 * 6;
-                                       ui_draw_but(but);
-
-                                       for (bt = block->buttons.first; bt; bt = bt->next)
-                                       {
-                                               if (ELEM(bt->type, NUM, NUMABS)) {
-                                                       ui_check_but(bt);
-                                                       ui_draw_but(bt);
-                                               }
-                                       }
-                                       
-                                       but->flag |= UI_ACTIVE;
-                                       retval = UI_RETURN_OK;
-                                       break;
-                               }
-                       }
-
-                       return WM_UI_HANDLER_BREAK;
-               }
-       }
-#endif
-
-       return WM_UI_HANDLER_CONTINUE;
-}
-#endif
-
-
 static int ui_do_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
 {      
        VECCOPY2D(but->linkto, event->mval);
@@ -4907,7 +5009,8 @@ static int ui_do_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data, con
 }
 
 static bool ui_numedit_but_TRACKPREVIEW(bContext *C, uiBut *but, uiHandleButtonData *data,
-                                        int mx, int my, const short shift)
+                                        int mx, int my,
+                                        const bool shift)
 {
        MovieClipScopes *scopes = (MovieClipScopes *)but->poin;
        bool changed = true;
@@ -4963,7 +5066,7 @@ static int ui_do_but_TRACKPREVIEW(bContext *C, uiBlock *block, uiBut *but, uiHan
                        button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
 
                        /* also do drag the first time */
-                       if (ui_numedit_but_TRACKPREVIEW(C, but, data, mx, my, event->shift))
+                       if (ui_numedit_but_TRACKPREVIEW(C, but, data, mx, my, event->shift != 0))
                                ui_numedit_apply(C, block, but, data);
 
                        return WM_UI_HANDLER_BREAK;
@@ -4971,13 +5074,15 @@ static int ui_do_but_TRACKPREVIEW(bContext *C, uiBlock *block, uiBut *but, uiHan
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                if (event->type == ESCKEY) {
-                       data->cancel = true;
-                       data->escapecancel = true;
-                       button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       if (event->val == KM_PRESS) {
+                               data->cancel = true;
+                               data->escapecancel = true;
+                               button_activate_state(C, but, BUTTON_STATE_EXIT);
+                       }
                }
                else if (event->type == MOUSEMOVE) {
                        if (mx != data->draglastx || my != data->draglasty) {
-                               if (ui_numedit_but_TRACKPREVIEW(C, but, data, mx, my, event->shift))
+                               if (ui_numedit_but_TRACKPREVIEW(C, but, data, mx, my, event->shift != 0))
                                        ui_numedit_apply(C, block, but, data);
                        }
                }
@@ -5123,7 +5228,6 @@ static bool ui_but_menu(bContext *C, uiBut *but)
        uiPopupMenu *pup;
        uiLayout *layout;
        bool is_array, is_array_component;
-       const char *name;
        uiStringInfo label = {BUT_GET_LABEL, NULL};
 
 /*     if ((but->rnapoin.data && but->rnaprop) == 0 && but->optype == NULL)*/
@@ -5136,19 +5240,23 @@ static bool ui_but_menu(bContext *C, uiBut *but)
        
        button_timers_tooltip_remove(C, but);
 
+       /* highly unlikely getting the label ever fails */
        uiButGetStrInfo(C, but, &label, NULL);
-       name = label.strinfo;
 
-       pup = uiPupMenuBegin(C, name, ICON_NONE);
+       pup = uiPupMenuBegin(C, label.strinfo ? label.strinfo : "", ICON_NONE);
        layout = uiPupMenuLayout(pup);
-
        if (label.strinfo)
                MEM_freeN(label.strinfo);
 
        uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
 
        if (but->rnapoin.data && but->rnaprop) {
-               bool is_anim = RNA_property_animateable(&but->rnapoin, but->rnaprop);
+               PointerRNA *ptr = &but->rnapoin;
+               PropertyRNA *prop = but->rnaprop;
+               bool is_anim = RNA_property_animateable(ptr, prop);
+               bool is_editable = RNA_property_editable(ptr, prop);
+               /*bool is_idprop = RNA_property_is_idprop(prop);*/ /* XXX does not work as expected, not strictly needed */
+               bool is_set = RNA_property_is_set(ptr, prop);
 
                /* second slower test, saved people finding keyframe items in menus when its not possible */
                if (is_anim)
@@ -5295,6 +5403,10 @@ static bool ui_but_menu(bContext *C, uiBut *but)
                        uiItemBooleanO(layout, CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Reset to Default Value"),
                                ICON_NONE, "UI_OT_reset_default_button", "all", 1);
                }
+               if (is_editable /*&& is_idprop*/ && is_set) {
+                       uiItemO(layout, CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Unset"),
+                               ICON_NONE, "UI_OT_unset_property_button");
+               }
                
                uiItemO(layout, CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Data Path"),
                        ICON_NONE, "UI_OT_copy_data_path_button");
@@ -5342,13 +5454,12 @@ static bool ui_but_menu(bContext *C, uiBut *but)
        }
 
        /* Show header tools for header buttons. */
-       if (CTX_wm_region(C)) {
+       {
                ARegion *ar = CTX_wm_region(C);
-                       if (ar->regiontype == RGN_TYPE_HEADER) {
-                       
-                               uiItemMenuF(layout, IFACE_("Header"), ICON_NONE, ED_screens_header_tools_menu_create, NULL);
-                               uiItemS(layout);
-                       }
+               if (ar && (ar->regiontype == RGN_TYPE_HEADER)) {
+                       uiItemMenuF(layout, IFACE_("Header"), ICON_NONE, ED_screens_header_tools_menu_create, NULL);
+                       uiItemS(layout);
+               }
        }
 
        {   /* Docs */
@@ -5437,6 +5548,28 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                else if (event->type == EVT_DROP) {
                        ui_but_drop(C, event, but, data);
                }
+               /* handle eyedropper */
+               else if ((event->type == EKEY) && (event->val == KM_PRESS)) {
+                       if (event->alt || event->shift || event->ctrl || event->oskey) {
+                               /* pass */
+                       }
+                       else {
+                               if (but->type == COLOR) {
+                                       WM_operator_name_call(C, "UI_OT_eyedropper_color", WM_OP_INVOKE_DEFAULT, NULL);
+                                       return WM_UI_HANDLER_BREAK;
+                               }
+                               else if (but->type == SEARCH_MENU_UNLINK) {
+                                       if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_POINTER) {
+                                               StructRNA *type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
+                                               const short idcode = RNA_type_to_ID_code(type);
+                                               if ((idcode == ID_OB) || OB_DATA_SUPPORT_ID(idcode)) {
+                                                       WM_operator_name_call(C, "UI_OT_eyedropper_id", WM_OP_INVOKE_DEFAULT, NULL);
+                                                       return WM_UI_HANDLER_BREAK;
+                                               }
+                                       }
+                               }
+                       }
+               }
                /* handle keyframing */
                else if ((event->type == IKEY) &&
                         !ELEM(KM_MOD_FIRST, event->ctrl, event->oskey) &&
@@ -5454,7 +5587,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                                ui_but_anim_insert_keyframe(C);
                        }
                        
-                       ED_region_tag_redraw(CTX_wm_region(C));
+                       ED_region_tag_redraw(data->region);
                        
                        return WM_UI_HANDLER_BREAK;
                }
@@ -5468,7 +5601,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        else
                                ui_but_anim_add_driver(C);
                                
-                       ED_region_tag_redraw(CTX_wm_region(C));
+                       ED_region_tag_redraw(data->region);
                        
                        return WM_UI_HANDLER_BREAK;
                }
@@ -5482,17 +5615,10 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        else
                                ui_but_anim_add_keyingset(C);
                                
-                       ED_region_tag_redraw(CTX_wm_region(C));
+                       ED_region_tag_redraw(data->region);
                        
                        return WM_UI_HANDLER_BREAK;
                }
-               /* reset to default */
-               /* XXX hardcoded keymap check.... */
-               else if (event->type == BACKSPACEKEY && event->val == KM_PRESS) {
-                       /* ctrl+backspace = reset active button; backspace = reset a whole array*/
-                       if (!(ELEM3(but->type, HSVCIRCLE, HSVCUBE, HISTOGRAM)))
-                               ui_set_but_default(C, !event->ctrl);
-               }
                /* handle menu */
                else if (event->type == RIGHTMOUSE && event->val == KM_PRESS) {
                        /* RMB has two options now */
@@ -5507,7 +5633,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                /* this should become disabled button .. */
                if (but->lock == true) {
                        if (but->lockstr) {
-                               BKE_report(NULL, RPT_WARNING, but->lockstr);
+                               WM_report(C, RPT_INFO, but->lockstr);
                                button_activate_state(C, but, BUTTON_STATE_EXIT);
                                return WM_UI_HANDLER_BREAK;
                        }
@@ -5532,11 +5658,9 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        break;
                case TOGBUT:
                case TOG:
-               case TOGR:
                case ICONTOG:
                case ICONTOGN:
                case TOGN:
-               case BUT_TOGDUAL:
                case OPTION:
                case OPTIONN:
                        retval = ui_do_but_TOG(C, but, data, event);
@@ -5545,17 +5669,16 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        retval = ui_do_but_SCROLL(C, block, but, data, event);
                        break;
                case NUM:
-               case NUMABS:
                        retval = ui_do_but_NUM(C, block, but, data, event);
                        break;
-               case SLI:
                case NUMSLI:
                        retval = ui_do_but_SLI(C, block, but, data, event);
                        break;
-               case ROUNDBOX:
                case LISTBOX:
+                       retval = ui_do_but_LISTBOX(C, block, but, data, event);
+                       break;
+               case ROUNDBOX:
                case LABEL:
-               case TOG3:
                case ROW:
                case LISTROW:
                case BUT_IMAGE:
@@ -5573,7 +5696,6 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        retval = ui_do_but_VECTORSCOPE(C, block, but, data, event);
                        break;
                case TEX:
-               case IDPOIN:
                case SEARCH_MENU:
                        retval = ui_do_but_TEX(C, block, but, data, event);
                        break;
@@ -5581,8 +5703,6 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        retval = ui_do_but_SEARCH_UNLINK(C, block, but, data, event);
                        break;
                case MENU:
-               case ICONROW:
-               case ICONTEXTROW:
                case BLOCK:
                case PULLDOWN:
                        retval = ui_do_but_BLOCK(C, but, data, event);
@@ -5594,7 +5714,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        if (but->a1 == UI_GRAD_V_ALT)  /* signal to prevent calling up color picker */
                                retval = ui_do_but_EXIT(C, but, data, event);
                        else
-                               retval = ui_do_but_BLOCK(C, but, data, event);
+                               retval = ui_do_but_COLOR(C, but, data, event);
                        break;
                case BUT_NORMAL:
                        retval = ui_do_but_NORMAL(C, block, but, data, event);
@@ -5611,16 +5731,6 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                case HSVCIRCLE:
                        retval = ui_do_but_HSVCIRCLE(C, block, but, data, event);
                        break;
-#ifdef WITH_INTERNATIONAL
-               case CHARTAB:
-                       retval = ui_do_but_CHARTAB(C, block, but, data, event);
-                       break;
-#else
-                       /* do nothing */
-               case CHARTAB:
-                       break;
-#endif
-
                case LINK:
                case INLINK:
                        retval = ui_do_but_LINK(C, but, data, event);
@@ -5634,7 +5744,22 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                case BUT_EXTRA:
                        break;
        }
-       
+
+
+       /* reset to default (generic function, only use if not handled by switch above) */
+       /* XXX hardcoded keymap check.... */
+       if (data->state == BUTTON_STATE_HIGHLIGHT) {
+               if ((retval == WM_UI_HANDLER_CONTINUE) &&
+                   (event->type == BACKSPACEKEY && event->val == KM_PRESS))
+               {
+                       /* ctrl+backspace = reset active button; backspace = reset a whole array*/
+                       ui_set_but_default(C, !event->ctrl);
+                       ED_region_tag_redraw(data->region);
+                       retval = WM_UI_HANDLER_BREAK;
+               }
+       }
+
+
        return retval;
 }
 
@@ -5693,7 +5818,7 @@ int UI_but_active_drop_name(bContext *C)
        uiBut *but = ui_but_find_activated(ar);
 
        if (but) {
-               if (ELEM4(but->type, TEX, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK))
+               if (ELEM3(but->type, TEX, SEARCH_MENU, SEARCH_MENU_UNLINK))
                        return 1;
        }
        
@@ -5762,11 +5887,12 @@ static bool ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y)
 
 /**
  * Can we mouse over the button or is it hidden/disabled/layout.
+ * Note: ctrl is kind of a hack currently, so that non-embossed TEX button behaves as a label when ctrl is not pressed.
  */
-bool ui_is_but_interactive(uiBut *but)
+static bool ui_is_but_interactive(uiBut *but, const bool ctrl)
 {
        /* note, LABEL is included for highlights, this allows drags */
-       if (but->type == LABEL && but->dragpoin == NULL)
+       if ((but->type == LABEL) && but->dragpoin == NULL)
                return false;
        if (ELEM3(but->type, ROUNDBOX, SEPR, LISTBOX))
                return false;
@@ -5774,15 +5900,32 @@ bool ui_is_but_interactive(uiBut *but)
                return false;
        if (but->flag & UI_SCROLLED)
                return false;
+       if ((but->type == TEX) && (but->dt & UI_EMBOSSN) && !ctrl)
+               return false;
 
        return true;
 }
 
-uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
+bool ui_is_but_search_unlink_visible(uiBut *but)
+{
+       BLI_assert(but->type == SEARCH_MENU_UNLINK);
+       return ((but->editstr == NULL) &&
+               (but->drawstr[0] != '\0'));
+}
+
+/* x and y are only used in case event is NULL... */
+static uiBut *ui_but_find_mouse_over(ARegion *ar, const wmEvent *event, int x, int y)
 {
        uiBlock *block;
        uiBut *but, *butover = NULL;
        int mx, my;
+       bool ctrl = true;
+
+       if (event) {
+               x = event->x;
+               y = event->y;
+               ctrl = event->ctrl;
+       }
 
 //     if (!win->active)
 //             return NULL;
@@ -5795,7 +5938,7 @@ uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
                ui_window_to_block(ar, block, &mx, &my);
 
                for (but = block->buttons.first; but; but = but->next) {
-                       if (ui_is_but_interactive(but)) {
+                       if (ui_is_but_interactive(but, ctrl)) {
                                if (ui_but_contains_pt(but, mx, my)) {
                                        butover = but;
                                }
@@ -5820,8 +5963,6 @@ static uiBut *ui_list_find_mouse_over(ARegion *ar, int x, int y)
        uiBut *but;
        int mx, my;
 
-//     if (!win->active)
-//             return NULL;
        if (!ui_mouse_inside_region(ar, x, y))
                return NULL;
 
@@ -5903,7 +6044,7 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s
                button_tooltip_timer_reset(C, but);
 
                /* automatic open pulldown block timer */
-               if (ELEM3(but->type, BLOCK, PULLDOWN, ICONTEXTROW)) {
+               if (ELEM(but->type, BLOCK, PULLDOWN)) {
                        if (data->used_mouse && !data->autoopentimer) {
                                int time;
 
@@ -5947,6 +6088,10 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s
        }
        else if (data->state == BUTTON_STATE_NUM_EDITING) {
                ui_numedit_end(but, data);
+
+               if (but->flag & UI_BUT_DRIVEN)
+                       WM_report(C, RPT_INFO, "Can't edit driven number value, see graph editor for the driver setup.");
+
                if (ui_is_a_warp_but(but)) {
 
 #ifdef USE_CONT_MOUSE_CORRECT
@@ -5962,7 +6107,7 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s
                                WM_cursor_grab_disable(data->window, NULL);
                        }
 #else
-                       WM_cursor_grab_disable(data->window, );
+                       WM_cursor_grab_disable(data->window, NULL);
 #endif
                }
        }
@@ -6363,7 +6508,7 @@ static int ui_handle_button_over(bContext *C, const wmEvent *event, ARegion *ar)
        uiBut *but;
 
        if (event->type == MOUSEMOVE) {
-               but = ui_but_find_mouse_over(ar, event->x, event->y);
+               but = ui_but_find_mouse_over(ar, event, 0, 0);
                if (but) {
                        button_activate_init(C, ar, but, BUTTON_ACTIVATE_OVER);
                }
@@ -6387,7 +6532,7 @@ void ui_button_activate_do(bContext *C, ARegion *ar, uiBut *but)
        
        button_activate_init(C, ar, but, BUTTON_ACTIVATE_OVER);
        
-       event = *(win->eventstate);  /* XXX huh huh? make api call */
+       wm_event_init_from_window(win, &event);
        event.type = EVT_BUT_OPEN;
        event.val = KM_PRESS;
        event.customdata = but;
@@ -6455,7 +6600,7 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
                                        data->cancel = TRUE;
                                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                                }
-                               else if (ui_but_find_mouse_over(ar, event->x, event->y) != but) {
+                               else if (ui_but_find_mouse_over(ar, event, 0, 0) != but) {
                                        data->cancel = TRUE;
                                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                                }
@@ -6494,11 +6639,11 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
                        case MIDDLEMOUSE:
                        case MOUSEPAN:
                                button_timers_tooltip_remove(C, but);
-
-                       /* pass on purposedly */
+                               /* fall-through */
                        default:
                                /* handle button type specific events */
                                retval = ui_do_button(C, block, but, event);
+                               break;
                }
        }
        else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
@@ -6548,6 +6693,7 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
                                if (event->customdata == data->flashtimer) {
                                        button_activate_state(C, but, BUTTON_STATE_EXIT);
                                }
+                               break;
                        }
                }
 
@@ -6566,7 +6712,7 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
                                        }
                                }
 
-                               bt = ui_but_find_mouse_over(ar, event->x, event->y);
+                               bt = ui_but_find_mouse_over(ar, event, 0, 0);
                                
                                if (bt && bt->active != data) {
                                        if (but->type != COLOR) {  /* exception */
@@ -6586,6 +6732,8 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
                // retval = WM_UI_HANDLER_BREAK; XXX why ?
        }
 
+       /* may have been re-allocated above (eyedropper for eg) */
+       data = but->active;
        if (data->state == BUTTON_STATE_EXIT) {
                uiBut *post_but = data->postbut;
                uiButtonActivateType post_type = data->posttype;
@@ -6604,7 +6752,7 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
                         * it stays active while the mouse is over it.
                         * This avoids adding mousemoves, see: [#33466] */
                        if (ELEM(state_orig, BUTTON_STATE_INIT, BUTTON_STATE_HIGHLIGHT)) {
-                               if (ui_but_find_mouse_over(ar, event->x, event->y) == but) {
+                               if (ui_but_find_mouse_over(ar, event, 0, 0) == but) {
                                        button_activate_init(C, ar, but, BUTTON_ACTIVATE_OVER);
                                }
                        }
@@ -6616,84 +6764,166 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
 
 static int ui_handle_list_event(bContext *C, const wmEvent *event, ARegion *ar)
 {
-       uiBut *but = ui_list_find_mouse_over(ar, event->x, event->y);
+       uiBut *but, *dragbut;
+       uiList *ui_list;
+       uiListDyn *dyn_data;
        int retval = WM_UI_HANDLER_CONTINUE;
-       int value, min, max;
        int type = event->type, val = event->val;
+       int mx, my;
+       bool is_over_dragbut = false;
 
-       if (but) {
-               uiList *ui_list = but->custom_data;
+       but = ui_list_find_mouse_over(ar, event->x, event->y);
+       if (!but) {
+               return retval;
+       }
 
-               if (ui_list) {
-                       
-                       /* convert pan to scrollwheel */
-                       if (type == MOUSEPAN) {
-                               ui_pan_to_scroll(event, &type, &val);
-                               
-                               /* if type still is mousepan, we call it handled, since delta-y accumulate */
-                               /* also see wm_event_system.c do_wheel_ui hack */
-                               if (type == MOUSEPAN)
-                                       retval = WM_UI_HANDLER_BREAK;
-                       }
-                       
-                       if (val == KM_PRESS) {
-                               
-                               if (ELEM(type, UPARROWKEY, DOWNARROWKEY) ||
-                                       ((ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE) && event->alt)))
-                               {
-                                       /* activate up/down the list */
-                                       value = RNA_property_int_get(&but->rnapoin, but->rnaprop);
+       ui_list = but->custom_data;
+       if (!ui_list || !ui_list->dyn_data) {
+               return retval;
+       }
+       dyn_data = ui_list->dyn_data;
 
-                                       if (ELEM(type, UPARROWKEY, WHEELUPMOUSE))
-                                               value--;
-                                       else
-                                               value++;
+       mx = event->x;
+       my = event->y;
+       ui_window_to_block(ar, but->block, &mx, &my);
+
+       /* Find our "dragging" button. */
+       for (dragbut = but->block->buttons.first; dragbut; dragbut = dragbut->next) {
+               if (dragbut->poin == (void *)ui_list) {
+                       break;
+               }
+       }
+       if (dragbut && dragbut == ui_but_find_mouse_over(ar, event, 0, 0)) {
+               is_over_dragbut = true;
+       }
+
+       if (is_over_dragbut && type == LEFTMOUSE && val == KM_PRESS && !(but->flag & UI_BUT_DISABLED)) {
+               uiHandleButtonData *data;
+               int *size = (int *)but->poin;
+
+               ui_handle_button_activate(C, ar, but, BUTTON_ACTIVATE);
+               button_activate_state(C, but, BUTTON_STATE_INIT);
 
-                                       CLAMP(value, 0, ui_list->list_last_len - 1);
+               data = but->active;
+               data->dragstarty = my;
 
-                                       if (value < ui_list->list_scroll)
-                                               ui_list->list_scroll = value;
-                                       else if (value >= ui_list->list_scroll + ui_list->list_size)
-                                               ui_list->list_scroll = value - ui_list->list_size + 1;
+               button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
 
-                                       RNA_property_int_range(&but->rnapoin, but->rnaprop, &min, &max);
-                                       value = CLAMPIS(value, min, max);
+               /* Again, have to override values set by ui_numedit_begin, because our listbox button also has a rnapoin... */
+               *size = data->origvalue = (double)dyn_data->visual_height;
+               ui_list->flag |= UILST_RESIZING;
 
+               retval = WM_UI_HANDLER_BREAK;
+       }
+       else {
+               /* convert pan to scrollwheel */
+               if (type == MOUSEPAN) {
+                       ui_pan_to_scroll(event, &type, &val);
+
+                       /* if type still is mousepan, we call it handled, since delta-y accumulate */
+                       /* also see wm_event_system.c do_wheel_ui hack */
+                       if (type == MOUSEPAN)
+                               retval = WM_UI_HANDLER_BREAK;
+               }
+
+               if (val == KM_PRESS) {
+                       if (ELEM(type, UPARROWKEY, DOWNARROWKEY) ||
+                           ((ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE) && event->alt)))
+                       {
+                               const int value_orig = RNA_property_int_get(&but->rnapoin, but->rnaprop);
+                               int value, min, max, inc;
+
+                               /* activate up/down the list */
+                               value = value_orig;
+                               if ((ui_list->filter_sort_flag & UILST_FLT_SORT_REVERSE) != 0) {
+                                       inc = ELEM(type, UPARROWKEY, WHEELUPMOUSE) ? 1 : -1;
+                               }
+                               else {
+                                       inc = ELEM(type, UPARROWKEY, WHEELUPMOUSE) ? -1 : 1;
+                               }
+
+                               if (dyn_data->items_filter_neworder || dyn_data->items_filter_flags) {
+                                       /* If we have a display order different from collection order, we have some work! */
+                                       int *org_order = MEM_mallocN(dyn_data->items_shown * sizeof(int), AT);
+                                       int *new_order = dyn_data->items_filter_neworder;
+                                       int i, org_idx = -1, len = dyn_data->items_len;
+                                       int current_idx = -1;
+                                       int filter_exclude = ui_list->filter_flag & UILST_FLT_EXCLUDE;
+
+                                       for (i = 0; i < len; i++) {
+                                               if (!dyn_data->items_filter_flags ||
+                                                       ((dyn_data->items_filter_flags[i] & UILST_FLT_ITEM) ^ filter_exclude))
+                                               {
+                                                       org_order[new_order ? new_order[++org_idx] : ++org_idx] = i;
+                                                       if (i == value) {
+                                                               current_idx = new_order ? new_order[org_idx] : org_idx;
+                                                       }
+                                               }
+                                               else if (i == value && org_idx >= 0) {
+                                                       current_idx = -(new_order ? new_order[org_idx] : org_idx) - 1;
+                                               }
+                                       }
+                                       /* Now, org_order maps displayed indices to real indices,
+                                        * and current_idx either contains the displayed index of active value (positive),
+                                        *                 or its more-nearest one (negated).
+                                        */
+                                       if (current_idx < 0) {
+                                               current_idx = (current_idx * -1) + (inc < 0 ? inc : inc - 1);
+                                       }
+                                       else {
+                                               current_idx += inc;
+                                       }
+                                       CLAMP(current_idx, 0, dyn_data->items_shown - 1);
+                                       value = org_order[current_idx];
+                                       MEM_freeN(org_order);
+                               }
+                               else {
+                                       value += inc;
+                               }
+
+                               CLAMP(value, 0, dyn_data->items_len - 1);
+
+                               RNA_property_int_range(&but->rnapoin, but->rnaprop, &min, &max);
+                               CLAMP(value, min, max);
+
+                               if (value != value_orig) {
                                        RNA_property_int_set(&but->rnapoin, but->rnaprop, value);
                                        RNA_property_update(C, &but->rnapoin, but->rnaprop);
-                                       ED_region_tag_redraw(ar);
 
-                                       retval = WM_UI_HANDLER_BREAK;
+                                       ui_apply_undo(but);
+
+                                       ui_list->flag |= UILST_SCROLL_TO_ACTIVE_ITEM;
+                                       ED_region_tag_redraw(ar);
                                }
-                               else if (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE) && event->shift) {
-                                       /* silly replacement for proper grip */
-                                       if (ui_list->list_grip_size == 0)
-                                               ui_list->list_grip_size = ui_list->list_size;
+                               retval = WM_UI_HANDLER_BREAK;
+                       }
+                       else if (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE) && event->shift) {
+                               /* We now have proper grip, but keep this anyway! */
+                               if (ui_list->list_grip == 0)
+                                       ui_list->list_grip = dyn_data->visual_height;
+                               /* list template will clamp */
+                               if (type == WHEELUPMOUSE)
+                                       ui_list->list_grip--;
+                               else
+                                       ui_list->list_grip++;
+
+                               ui_list->flag |= UILST_SCROLL_TO_ACTIVE_ITEM;
+                               ED_region_tag_redraw(ar);
 
+                               retval = WM_UI_HANDLER_BREAK;
+                       }
+                       else if (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
+                               if (dyn_data->height > dyn_data->visual_height) {
+                                       /* list template will clamp */
                                        if (type == WHEELUPMOUSE)
-                                               ui_list->list_grip_size--;
+                                               ui_list->list_scroll--;
                                        else
-                                               ui_list->list_grip_size++;
-
-                                       ui_list->list_grip_size = MAX2(ui_list->list_grip_size, 1);
+                                               ui_list->list_scroll++;
 
                                        ED_region_tag_redraw(ar);
 
                                        retval = WM_UI_HANDLER_BREAK;
                                }
-                               else if (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
-                                       if (ui_list->list_last_len > ui_list->list_size) {
-                                               /* list template will clamp */
-                                               if (type == WHEELUPMOUSE)
-                                                       ui_list->list_scroll--;
-                                               else
-                                                       ui_list->list_scroll++;
-
-                                               ED_region_tag_redraw(ar);
-
-                                               retval = WM_UI_HANDLER_BREAK;
-                                       }
-                               }
                        }
                }
        }
@@ -6713,7 +6943,7 @@ static void ui_handle_button_return_submenu(bContext *C, const wmEvent *event, u
        if ((menu->menuretval & UI_RETURN_OK) || (menu->menuretval & UI_RETURN_UPDATE)) {
                if (but->type == COLOR)
                        copy_v3_v3(data->vec, menu->retvec);
-               else if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
+               else if (but->type == MENU)
                        data->value = menu->retvalue;
        }
 
@@ -6764,12 +6994,14 @@ static void ui_handle_button_return_submenu(bContext *C, const wmEvent *event, u
  * - only for 1 second
  */
 
-static void ui_mouse_motion_towards_init(uiPopupBlockHandle *menu, int mx, int my, const bool force)
+static void ui_mouse_motion_towards_init_ex(uiPopupBlockHandle *menu, const int xy[2], const bool force)
 {
+       BLI_assert(((uiBlock *)menu->region->uiblocks.first)->flag & UI_BLOCK_MOVEMOUSE_QUIT);
+
        if (!menu->dotowards || force) {
                menu->dotowards = true;
-               menu->towardsx = mx;
-               menu->towardsy = my;
+               menu->towards_xy[0] = xy[0];
+               menu->towards_xy[1] = xy[1];
 
                if (force)
                        menu->towardstime = DBL_MAX;  /* unlimited time */
@@ -6778,38 +7010,82 @@ static void ui_mouse_motion_towards_init(uiPopupBlockHandle *menu, int mx, int m
        }
 }
 
-static bool ui_mouse_motion_towards_check(uiBlock *block, uiPopupBlockHandle *menu, int mx, int my)
+static void ui_mouse_motion_towards_init(uiPopupBlockHandle *menu, const int xy[2])
+{
+       ui_mouse_motion_towards_init_ex(menu, xy, false);
+}
+
+static void ui_mouse_motion_towards_reinit(uiPopupBlockHandle *menu, const int xy[2])
 {
-       float p1[2], p2[2], p3[2], p4[2], oldp[2], newp[2];
+       ui_mouse_motion_towards_init_ex(menu, xy, true);
+}
+
+static bool ui_mouse_motion_towards_check(uiBlock *block, uiPopupBlockHandle *menu, const int xy[2],
+                                          const bool use_wiggle_room)
+{
+       float p1[2], p2[2], p3[2], p4[2];
+       float oldp[2] = {menu->towards_xy[0], menu->towards_xy[1]};
+       const float newp[2] = {xy[0], xy[1]};
        bool closer;
+       const float margin = MENU_TOWARDS_MARGIN;
+       rctf rect_px;
+
+       BLI_assert(block->flag & UI_BLOCK_MOVEMOUSE_QUIT);
+
+
+       /* annoying fix for [#36269], this is a bit odd but in fact works quite well
+        * don't mouse-out of a menu if another menu has been created after it.
+        * if this causes problems we could remove it and check on a different fix - campbell */
+       if (menu->region->next) {
+               /* am I the last menu (test) */
+               ARegion *ar = menu->region->next;
+               do {
+                       uiBlock *block_iter = ar->uiblocks.first;
+                       if (block_iter && ui_block_is_menu(block_iter)) {
+                               return true;
+                       }
+               } while ((ar = ar->next));
+       }
+       /* annoying fix end! */
+
 
        if (!menu->dotowards) {
-               return 0;
+               return false;
        }
 
+       if (len_squared_v2v2(oldp, newp) < (4.0f * 4.0f))
+               return menu->dotowards;
+
        /* verify that we are moving towards one of the edges of the
         * menu block, in other words, in the triangle formed by the
         * initial mouse location and two edge points. */
-       p1[0] = block->rect.xmin - 20;
-       p1[1] = block->rect.ymin - 20;
+       ui_block_to_window_rctf(menu->region, block, &rect_px, &block->rect);
 
-       p2[0] = block->rect.xmax + 20;
-       p2[1] = block->rect.ymin - 20;
-       
-       p3[0] = block->rect.xmax + 20;
-       p3[1] = block->rect.ymax + 20;
+       p1[0] = rect_px.xmin - margin;
+       p1[1] = rect_px.ymin - margin;
 
-       p4[0] = block->rect.xmin - 20;
-       p4[1] = block->rect.ymax + 20;
+       p2[0] = rect_px.xmax + margin;
+       p2[1] = rect_px.ymin - margin;
+       
+       p3[0] = rect_px.xmax + margin;
+       p3[1] = rect_px.ymax + margin;
 
-       oldp[0] = menu->towardsx;
-       oldp[1] = menu->towardsy;
+       p4[0] = rect_px.xmin - margin;
+       p4[1] = rect_px.ymax + margin;
 
-       newp[0] = mx;
-       newp[1] = my;
+       /* allow for some wiggle room, if the user moves a few pixels away,
+        * don't immediately quit (only for top level menus) */
+       if (use_wiggle_room) {
+               const float cent[2] = {
+                   BLI_rctf_cent_x(&rect_px),
+                   BLI_rctf_cent_y(&rect_px)};
+               float delta[2];
 
-       if (len_squared_v2v2(oldp, newp) < (4.0f * 4.0f))
-               return menu->dotowards;
+               sub_v2_v2v2(delta, oldp, cent);
+               normalize_v2(delta);
+               mul_v2_fl(delta, MENU_TOWARDS_WIGGLE_ROOM);
+               add_v2_v2(oldp, delta);
+       }
 
        closer = (isect_point_tri_v2(newp, oldp, p1, p2) ||
                  isect_point_tri_v2(newp, oldp, p2, p3) ||
@@ -6972,7 +7248,8 @@ static int ui_handle_menu_button(bContext *C, const wmEvent *event, uiPopupBlock
        return retval;
 }
 
-static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockHandle *menu, int level)
+static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockHandle *menu,
+                                int level, const bool is_parent_inside)
 {
        ARegion *ar;
        uiBlock *block;
@@ -6996,10 +7273,12 @@ static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockH
        but = ui_but_find_activated(ar);
 
        if (but && button_modal_state(but->active->state)) {
-               /* if a button is activated modal, always reset the start mouse
-                * position of the towards mechanism to avoid loosing focus,
-                * and don't handle events */
-               ui_mouse_motion_towards_init(menu, mx, my, true);
+               if (block->flag & UI_BLOCK_MOVEMOUSE_QUIT) {
+                       /* if a button is activated modal, always reset the start mouse
+                        * position of the towards mechanism to avoid loosing focus,
+                        * and don't handle events */
+                       ui_mouse_motion_towards_reinit(menu, &event->x);
+               }
        }
        else if (event->type == TIMER) {
                if (event->customdata == menu->scrolltimer)
@@ -7008,7 +7287,9 @@ static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockH
        else {
                /* for ui_mouse_motion_towards_block */
                if (event->type == MOUSEMOVE) {
-                       ui_mouse_motion_towards_init(menu, mx, my, false);
+                       if (block->flag & UI_BLOCK_MOVEMOUSE_QUIT) {
+                               ui_mouse_motion_towards_init(menu, &event->x);
+                       }
                        
                        /* add menu scroll timer, if needed */
                        if (ui_menu_scroll_test(block, my))
@@ -7177,7 +7458,7 @@ static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockH
                                                for (but = block->buttons.first; but; but = but->next) {
                                                        int doit = FALSE;
                                                        
-                                                       if (but->type != LABEL && but->type != SEPR)
+                                                       if (!ELEM(but->type, LABEL, SEPR))
                                                                count++;
                                                        
                                                        /* exception for rna layer buts */
@@ -7187,11 +7468,6 @@ static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockH
                                                                                doit = TRUE;
                                                                }
                                                        }
-                                                       /* exception for menus like layer buts, with button aligning they're not drawn in order */
-                                                       else if (but->type == TOGR) {
-                                                               if (but->bitnr == act - 1)
-                                                                       doit = TRUE;
-                                                       }
                                                        else if (count == act) {
                                                                doit = TRUE;
                                                        }
@@ -7318,11 +7594,12 @@ static int ui_handle_menu_event(bContext *C, const wmEvent *event, uiPopupBlockH
                                        menu->menuretval = UI_RETURN_CANCEL | UI_RETURN_POPUP_OK;
                        }
                        else {
-                               ui_mouse_motion_towards_check(block, menu, mx, my);
 
                                /* check mouse moving outside of the menu */
                                if (inside == 0 && (block->flag & UI_BLOCK_MOVEMOUSE_QUIT)) {
                                        uiSafetyRct *saferct;
+
+                                       ui_mouse_motion_towards_check(block, menu, &event->x, is_parent_inside == false);
                                        
                                        /* check for all parent rects, enables arrowkeys to be used */
                                        for (saferct = block->saferct.first; saferct; saferct = saferct->next) {
@@ -7381,7 +7658,6 @@ static int ui_handle_menu_return_submenu(bContext *C, const wmEvent *event, uiPo
        uiBlock *block;
        uiHandleButtonData *data;
        uiPopupBlockHandle *submenu;
-       int mx, my, update;
 
        ar = menu->region;
        block = ar->uiblocks.first;
@@ -7391,6 +7667,8 @@ static int ui_handle_menu_return_submenu(bContext *C, const wmEvent *event, uiPo
        submenu = data->menu;
 
        if (submenu->menuretval) {
+               bool update;
+
                /* first decide if we want to close our own menu cascading, if
                 * so pass on the sub menu return value to our own menu handle */
                if ((submenu->menuretval & UI_RETURN_OK) || (submenu->menuretval & UI_RETURN_CANCEL)) {
@@ -7400,7 +7678,7 @@ static int ui_handle_menu_return_submenu(bContext *C, const wmEvent *event, uiPo
                        }
                }
 
-               update = (submenu->menuretval & UI_RETURN_UPDATE);
+               update = (submenu->menuretval & UI_RETURN_UPDATE) != 0;
 
                /* now let activated button in this menu exit, which
                 * will actually close the submenu too */
@@ -7410,12 +7688,11 @@ static int ui_handle_menu_return_submenu(bContext *C, const wmEvent *event, uiPo
                        submenu->menuretval = 0;
        }
 
-       /* for cases where close does not cascade, allow the user to
-        * move the mouse back towards the menu without closing */
-       mx = event->x;
-       my = event->y;
-       ui_window_to_block(ar, block, &mx, &my);
-       ui_mouse_motion_towards_init(menu, mx, my, true);
+       if (block->flag & UI_BLOCK_MOVEMOUSE_QUIT) {
+               /* for cases where close does not cascade, allow the user to
+                * move the mouse back towards the menu without closing */
+               ui_mouse_motion_towards_reinit(menu, &event->x);
+       }
 
        if (menu->menuretval)
                return WM_UI_HANDLER_CONTINUE;
@@ -7423,20 +7700,35 @@ static int ui_handle_menu_return_submenu(bContext *C, const wmEvent *event, uiPo
                return WM_UI_HANDLER_BREAK;
 }
 
-static int ui_handle_menus_recursive(bContext *C, const wmEvent *event, uiPopupBlockHandle *menu, int level)
+static int ui_handle_menus_recursive(bContext *C, const wmEvent *event, uiPopupBlockHandle *menu,
+                                     int level, const bool is_parent_inside)
 {
        uiBut *but;
        uiHandleButtonData *data;
        uiPopupBlockHandle *submenu;
        int retval = WM_UI_HANDLER_CONTINUE;
+       bool do_towards_reinit = false;
 
        /* check if we have a submenu, and handle events for it first */
        but = ui_but_find_activated(menu->region);
        data = (but) ? but->active : NULL;
        submenu = (data) ? data->menu : NULL;
 
-       if (submenu)
-               retval = ui_handle_menus_recursive(C, event, submenu, level + 1);
+       if (submenu) {
+               bool inside = false;
+
+               if (is_parent_inside == false) {
+                       int mx, my;
+                       uiBlock *block = menu->region->uiblocks.first;
+
+                       mx = event->x;
+                       my = event->y;
+                       ui_window_to_block(menu->region, block, &mx, &my);
+                       inside = BLI_rctf_isect_pt(&block->rect, mx, my);
+               }
+
+               retval = ui_handle_menus_recursive(C, event, submenu, level + 1, is_parent_inside || inside);
+       }
 
        /* now handle events for our own menu */
        if (retval == WM_UI_HANDLER_CONTINUE || event->type == TIMER) {
@@ -7455,13 +7747,27 @@ static int ui_handle_menus_recursive(bContext *C, const wmEvent *event, uiPopupB
                }
 
                if (do_but_search) {
+                       uiBlock *block = menu->region->uiblocks.first;
+
                        retval = ui_handle_menu_button(C, event, menu);
+
+                       if (block->flag & UI_BLOCK_MOVEMOUSE_QUIT) {
+                               /* when there is a active search button and we close it,
+                                * we need to reinit the mouse coords [#35346] */
+                               if (ui_but_find_activated(menu->region) != but) {
+                                       do_towards_reinit = true;
+                               }
+                       }
                }
                else {
-                       retval = ui_handle_menu_event(C, event, menu, level);
+                       retval = ui_handle_menu_event(C, event, menu, level, is_parent_inside);
                }
        }
 
+       if (do_towards_reinit) {
+               ui_mouse_motion_towards_reinit(menu, &event->x);
+       }
+
        return retval;
 }
 
@@ -7550,7 +7856,7 @@ static int ui_handler_region_menu(bContext *C, const wmEvent *event, void *UNUSE
                        /* handle events for menus and their buttons recursively,
                         * this will handle events from the top to the bottom menu */
                        if (data->menu)
-                               retval = ui_handle_menus_recursive(C, event, data->menu, 0);
+                               retval = ui_handle_menus_recursive(C, event, data->menu, 0, false);
 
                        /* handle events for the activated button */
                        if ((data->menu && (retval == WM_UI_HANDLER_CONTINUE)) ||
@@ -7596,7 +7902,7 @@ static int ui_handler_popup(bContext *C, const wmEvent *event, void *userdata)
                retval = WM_UI_HANDLER_CONTINUE;
        }
 
-       ui_handle_menus_recursive(C, event, menu, 0);
+       ui_handle_menus_recursive(C, event, menu, 0, false);
 
        /* free if done, does not free handle itself */
        if (menu->menuretval) {
@@ -7606,6 +7912,14 @@ static int ui_handler_popup(bContext *C, const wmEvent *event, void *userdata)
                ui_popup_block_free(C, menu);
                UI_remove_popup_handlers(&CTX_wm_window(C)->modalhandlers, menu);
 
+#ifdef USE_DRAG_TOGGLE
+               {
+                       wmWindow *win = CTX_wm_window(C);
+                       WM_event_free_ui_handler_all(C, &win->modalhandlers,
+                                                    ui_handler_region_drag_toggle, ui_handler_region_drag_toggle_remove);
+               }
+#endif
+
                if ((temp.menuretval & UI_RETURN_OK) || (temp.menuretval & UI_RETURN_POPUP_OK)) {
                        if (temp.popup_func)
                                temp.popup_func(C, temp.popup_arg, temp.retvalue);
@@ -7614,6 +7928,8 @@ static int ui_handler_popup(bContext *C, const wmEvent *event, void *userdata)
                }
                else if (temp.cancel_func)
                        temp.cancel_func(C, temp.popup_arg);
+
+               WM_event_add_mousemove(C);
        }
        else {
                /* re-enable tooltips */
@@ -7654,6 +7970,11 @@ void UI_remove_popup_handlers(ListBase *handlers, uiPopupBlockHandle *popup)
        WM_event_remove_ui_handler(handlers, ui_handler_popup, ui_handler_remove_popup, popup, FALSE);
 }
 
+void UI_remove_popup_handlers_all(bContext *C, ListBase *handlers)
+{
+       WM_event_free_ui_handler_all(C, handlers, ui_handler_popup, ui_handler_remove_popup);
+}
+
 bool UI_textbutton_activate_event(const bContext *C, ARegion *ar,
                                   const void *rna_poin_data, const char *rna_prop_id)
 {
@@ -7683,3 +8004,7 @@ bool UI_textbutton_activate_event(const bContext *C, ARegion *ar,
        }
 }
 
+void ui_button_clipboard_free(void)
+{
+       curvemapping_free_data(&but_copypaste_curve);
+}