Cleaning i18n code.
[blender.git] / source / blender / editors / interface / interface_regions.c
index 4574c1fd4adfc91d197a40b693c1bb2ced6f6940..bc4e3cf9f131050b8ad2dac305cfbcb80eb09186 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/editors/interface/interface_regions.c
+ *  \ingroup edinterface
+ */
+
+
 
 #include <stdarg.h>
 #include <stdlib.h>
 #include "UI_view2d.h"
 
 #include "BLF_api.h"
+#include "BLF_translation.h"
 
 #include "ED_screen.h"
 
 #include "interface_intern.h"
 
-#define MENU_BUTTON_HEIGHT     20
 #define MENU_SEPR_HEIGHT       6
 #define B_NOP                  -1
 #define MENU_SHADOW_SIDE       8
@@ -79,7 +84,7 @@ typedef struct MenuEntry {
 } MenuEntry;
 
 typedef struct MenuData {
-       char *instr;
+       const char *instr;
        const char *title;
        int titleicon;
        
@@ -87,7 +92,7 @@ typedef struct MenuData {
        int nitems, itemssize;
 } MenuData;
 
-static MenuData *menudata_new(char *instr)
+static MenuData *menudata_new(const char *instr)
 {
        MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
 
@@ -130,9 +135,9 @@ static void menudata_add_item(MenuData *md, const char *str, int retval, int ico
        md->nitems++;
 }
 
-void menudata_free(MenuData *md)
+static void menudata_free(MenuData *md)
 {
-       MEM_freeN(md->instr);
+       MEM_freeN((void *)md->instr);
        if (md->items)
                MEM_freeN(md->items);
        MEM_freeN(md);
@@ -151,7 +156,7 @@ void menudata_free(MenuData *md)
         * @param str String to be parsed.
         * @retval new menudata structure, free with menudata_free()
         */
-MenuData *decompose_menu_string(char *str) 
+static MenuData *decompose_menu_string(const char *str)
 {
        char *instr= BLI_strdup(str);
        MenuData *md= menudata_new(instr);
@@ -169,7 +174,7 @@ MenuData *decompose_menu_string(char *str)
                                *s= '\0';
                                s++;
                        } else if (s[1]=='t') {
-                               nitem_is_title= 1;
+                               nitem_is_title= (s != instr); /* check for empty title */
 
                                *s= '\0';
                                s++;
@@ -208,11 +213,13 @@ MenuData *decompose_menu_string(char *str)
                                nicon= 0;
                        }
                        
-                       if (c=='\0')
+                       if (c=='\0') {
                                break;
-               } else if (!nitem)
+                       }
+               } else if (!nitem) {
                        nitem= s;
-               
+               }
+
                s++;
        }
        
@@ -225,9 +232,12 @@ void ui_set_name_menu(uiBut *but, int value)
        int i;
        
        md= decompose_menu_string(but->str);
-       for (i=0; i<md->nitems; i++)
-               if (md->items[i].retval==value)
-                       strcpy(but->drawstr, md->items[i].str);
+       for (i=0; i<md->nitems; i++) {
+               if (md->items[i].retval==value) {
+                       BLI_strncpy(but->drawstr, md->items[i].str, sizeof(but->drawstr));
+                       break;
+               }
+       }
        
        menudata_free(md);
 }
@@ -272,7 +282,7 @@ int ui_step_name_menu(uiBut *but, int step)
 
 /******************** Creating Temporary regions ******************/
 
-ARegion *ui_add_temporary_region(bScreen *sc)
+static ARegion *ui_add_temporary_region(bScreen *sc)
 {
        ARegion *ar;
 
@@ -285,7 +295,7 @@ ARegion *ui_add_temporary_region(bScreen *sc)
        return ar;
 }
 
-void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
+static void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
 {
        if(CTX_wm_window(C))
                wm_draw_region_clear(CTX_wm_window(C), ar);
@@ -314,7 +324,7 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
        rcti bbox= data->bbox;
        int a;
        
-       ui_draw_menu_back(U.uistyles.first, NULL, &data->bbox);
+       ui_draw_menu_back(UI_GetStyle(), NULL, &data->bbox);
        
        /* draw text */
        uiStyleFontSet(&data->fstyle);
@@ -341,7 +351,7 @@ static void ui_tooltip_region_free_cb(ARegion *ar)
 
 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 {
-       uiStyle *style= U.uistyles.first;       // XXX pass on as arg
+       uiStyle *style= UI_GetStyle();
        static ARegionType type;
        ARegion *ar;
        uiTooltipData *data;
@@ -357,6 +367,37 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
        /* create tooltip data */
        data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
 
+       /* special case, enum rna buttons only have enum item description, use general enum description too before the specific one */
+       if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
+               const char *descr= RNA_property_description(but->rnaprop);
+               if(descr && descr[0]) {
+                       BLI_strncpy(data->lines[data->totline], descr, sizeof(data->lines[0]));
+                       data->color[data->totline]= 0xFFFFFF;
+                       data->totline++;
+               }
+
+               if(but->type == ROW) {
+                       EnumPropertyItem *item;
+                       int i, totitem, free;
+
+                       RNA_property_enum_items_gettexted(C, &but->rnapoin, but->rnaprop, &item, &totitem, &free);
+
+                       for(i=0; i<totitem; i++) {
+                               if(item[i].identifier[0] && item[i].value == (int)but->hardmax) {
+                                       if(item[i].description[0]) {
+                                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "%s: %s", item[i].name, item[i].description);
+                                               data->color[data->totline]= 0xFFFFFF;
+                                               data->totline++;
+                                       }
+                                       break;
+                               }
+                       }
+
+                       if(free)
+                               MEM_freeN(item);
+               }
+       }
+       
        if(but->tip && strlen(but->tip)) {
                BLI_strncpy(data->lines[data->totline], but->tip, sizeof(data->lines[0]));
                data->color[data->totline]= 0xFFFFFF;
@@ -368,7 +409,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                prop= (but->opptr)? but->opptr->data: NULL;
 
                if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
-                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Shortcut: %s", buf);
+                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), buf);
                        data->color[data->totline]= 0x888888;
                        data->totline++;
                }
@@ -378,7 +419,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                /* full string */
                ui_get_but_string(but, buf, sizeof(buf));
                if(buf[0]) {
-                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Value: %s", buf);
+                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Value: %s"), buf);
                        data->color[data->totline]= 0x888888;
                        data->totline++;
                }
@@ -389,7 +430,8 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                
                if (unit_type == PROP_UNIT_ROTATION) {
                        if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
-                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Radians: %f", RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex));
+                               float value= RNA_property_array_check(but->rnaprop) ? RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) : RNA_property_float_get(&but->rnapoin, but->rnaprop);
+                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Radians: %f"), value);
                                data->color[data->totline]= 0x888888;
                                data->totline++;
                        }
@@ -398,7 +440,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                if(but->flag & UI_BUT_DRIVEN) {
                        if(ui_but_anim_expression_get(but, buf, sizeof(buf))) {
                                /* expression */
-                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Expression: %s", buf);
+                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Expression: %s"), buf);
                                data->color[data->totline]= 0x888888;
                                data->totline++;
                        }
@@ -406,7 +448,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
                /* rna info */
                if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
-                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s.%s", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
+                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s.%s"), RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
                        data->color[data->totline]= 0x888888;
                        data->totline++;
                }
@@ -414,7 +456,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                if(but->rnapoin.id.data) {
                        ID *id= but->rnapoin.id.data;
                        if(id->lib && id->lib->name) {
-                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Library: %s", id->lib->name);
+                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Library: %s"), id->lib->name);
                                data->color[data->totline]= 0x888888;
                                data->totline++;
                        }
@@ -429,7 +471,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
                /* operator info */
                if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
-                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s", str);
+                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), str);
                        data->color[data->totline]= 0x888888;
                        data->totline++;
                }
@@ -443,12 +485,23 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                        WM_operator_poll_context(C, but->optype, but->opcontext);
                        poll_msg= CTX_wm_operator_poll_msg_get(C);
                        if(poll_msg) {
-                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Disabled: %s", poll_msg);
+                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Disabled: %s"), poll_msg);
                                data->color[data->totline]= 0x6666ff; /* alert */
                                data->totline++;                        
                        }
                }
        }
+       else if (ELEM(but->type, MENU, PULLDOWN)) {
+               if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
+                       if(but->menu_create_func && WM_menutype_contains((MenuType *)but->poin)) {
+                               MenuType *mt= (MenuType *)but->poin;
+                               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), mt->idname);
+                               data->color[data->totline]= 0x888888;
+                               data->totline++;
+                       }
+               }
+
+       }
 
        assert(data->totline < MAX_TOOLTIP_LINES);
        
@@ -470,12 +523,17 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
        data->fstyle.align= UI_STYLE_TEXT_CENTER;
        uiStyleFontSet(&data->fstyle);
 
-       h= BLF_height(data->fstyle.uifont_id, data->lines[0]);
+       /* these defines may need to be tweaked depending on font */
+#define TIP_MARGIN_Y 2
+#define TIP_BORDER_X 16.0f
+#define TIP_BORDER_Y 6.0f
+
+       h= BLF_height_max(data->fstyle.uifont_id);
 
        for(a=0, fontw=0, fonth=0; a<data->totline; a++) {
                w= BLF_width(data->fstyle.uifont_id, data->lines[a]);
                fontw= MAX2(fontw, w);
-               fonth += (a == 0)? h: h+5;
+               fonth += (a == 0)? h: h+TIP_MARGIN_Y;
        }
 
        fontw *= aspect;
@@ -484,17 +542,22 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
        data->toth= fonth;
        data->lineh= h;
-       data->spaceh= 5;
+       data->spaceh= TIP_MARGIN_Y;
+
 
        /* compute position */
        ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
        ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
 
-       x1f= (but->x1+but->x2)/2.0f + ofsx - 16.0f*aspect;
-       x2f= x1f + fontw + 16.0f*aspect;
-       y2f= but->y1 + ofsy - 15.0f*aspect;
-       y1f= y2f - fonth*aspect - 15.0f*aspect;
+       x1f= (but->x1 + but->x2) * 0.5f + ofsx - (TIP_BORDER_X * aspect);
+       x2f= x1f + fontw + (TIP_BORDER_X * aspect);
+       y2f= but->y1 + ofsy - (TIP_BORDER_Y * aspect);
+       y1f= y2f - fonth*aspect - (TIP_BORDER_Y * aspect);
        
+#undef TIP_MARGIN_Y
+#undef TIP_BORDER_X
+#undef TIP_BORDER_Y
+
        /* copy to int, gets projected if possible too */
        x1= x1f; y1= y1f; x2= x2f; y2= y2f; 
        
@@ -524,9 +587,11 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                        x2= winx;
                }
        }
-       if(y1 < 0) {
-               y1 += 56;
-               y2 += 56;
+       /* ensure at least 5 px above screen bounds
+        * 25 is just a guess to be above the menu item */
+       if(y1 < 5) {
+               y2 += (-y1) + 30;
+               y1 = 30;
        }
 
        /* widget rect, in region coords */
@@ -627,7 +692,7 @@ int uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int icon
 
 int uiSearchBoxhHeight(void)
 {
-       return SEARCH_ITEMS*MENU_BUTTON_HEIGHT + 2*MENU_TOP;
+       return SEARCH_ITEMS*UI_UNIT_Y + 2*MENU_TOP;
 }
 
 /* ar is the search box itself */
@@ -678,7 +743,7 @@ static void ui_searchbox_butrect(rcti *rect, uiSearchboxData *data, int itemnr)
                rect->xmin += col * butw;
                rect->xmax = rect->xmin + butw;
                
-               rect->ymax = data->bbox.ymax - (row * buth);
+               rect->ymax = data->bbox.ymax - MENU_TOP - (row * buth);
                rect->ymin = rect->ymax - buth;
        }
        /* list view */
@@ -924,11 +989,9 @@ static void ui_searchbox_region_free_cb(ARegion *ar)
        ar->regiondata= NULL;
 }
 
-static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block);
-
 ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
 {
-       uiStyle *style= U.uistyles.first;       // XXX pass on as arg
+       uiStyle *style= UI_GetStyle();
        static ARegionType type;
        ARegion *ar;
        uiSearchboxData *data;
@@ -980,7 +1043,7 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
                data->bbox.ymax= (ar->winrct.ymax-ar->winrct.ymin) - MENU_SHADOW_BOTTOM;
                
                /* check if button is lower half */
-               if( but->y2 < (but->block->minx+but->block->maxx)/2 ) {
+               if( but->y2 < (but->block->miny+but->block->maxy)/2 ) {
                        data->bbox.ymin += (but->y2-but->y1);
                }
                else {
@@ -1034,7 +1097,7 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
                }
                if(y1 < 0) { /* XXX butregion NULL check?, there is one above */
                        int newy1;
-                       UI_view2d_to_region_no_clip(&butregion->v2d, 0, but->y2 + ofsy, 0, &newy1);
+                       UI_view2d_to_region_no_clip(&butregion->v2d, 0, but->y2 + ofsy, NULL, &newy1);
                        newy1 += butregion->winrct.ymin;
 
                        y2= y2-y1 + newy1;
@@ -1086,9 +1149,17 @@ void ui_searchbox_free(bContext *C, ARegion *ar)
 /* XXX weak: search_func adds all partial matches... */
 void ui_but_search_test(uiBut *but)
 {
-       uiSearchItems *items= MEM_callocN(sizeof(uiSearchItems), "search items");
+       uiSearchItems *items;
        int x1;
-       
+
+       /* possibly very large lists (such as ID datablocks) only
+        * only validate string RNA buts (not pointers) */
+       if(but->rnaprop && RNA_property_type(but->rnaprop) != PROP_STRING) {
+               return;
+       }
+
+       items= MEM_callocN(sizeof(uiSearchItems), "search items");
+
        /* setup search struct */
        items->maxitem= 10;
        items->maxstrlen= 256;
@@ -1124,7 +1195,7 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
        uiBut *bt;
        uiSafetyRct *saferct;
        rctf butrct;
-       float aspect;
+       /*float aspect;*/ /*UNUSED*/
        int xsize, ysize, xof=0, yof=0, center;
        short dir1= 0, dir2=0;
        
@@ -1159,7 +1230,7 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
                }
        }
        
-       aspect= (float)(block->maxx - block->minx + 4);
+       /*aspect= (float)(block->maxx - block->minx + 4);*/ /*UNUSED*/
        ui_block_to_window_fl(butregion, but->block, &block->minx, &block->miny);
        ui_block_to_window_fl(butregion, but->block, &block->maxx, &block->maxy);
 
@@ -1168,23 +1239,31 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
        
        xsize= block->maxx - block->minx+4; // 4 for shadow
        ysize= block->maxy - block->miny+4;
-       aspect/= (float)xsize;
+       /*aspect/= (float)xsize;*/ /*UNUSED*/
 
-       if(but) {
+       {
                int left=0, right=0, top=0, down=0;
                int winx, winy;
-               int offscreen;
+               // int offscreen;
 
                wm_window_get_size(window, &winx, &winy);
 
                if(block->direction & UI_CENTER) center= ysize/2;
                else center= 0;
-
-               if( butrct.xmin-xsize > 0.0) left= 1;
+               
+               /* check if there's space at all */
+               if( butrct.xmin-xsize > 0.0f) left= 1;
                if( butrct.xmax+xsize < winx) right= 1;
-               if( butrct.ymin-ysize+center > 0.0) down= 1;
+               if( butrct.ymin-ysize+center > 0.0f) down= 1;
                if( butrct.ymax+ysize-center < winy) top= 1;
                
+               if(top==0 && down==0) {
+                       if (butrct.ymin-ysize < winy-butrct.ymax-ysize)
+                               top= 1;
+                       else
+                               down= 1;
+               }
+               
                dir1= block->direction & UI_DIRECTION;
 
                /* secundary directions */
@@ -1249,7 +1328,7 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
                if(top==0 && down==0) {
                        if(dir1==UI_LEFT || dir1==UI_RIGHT) {
                                // align with bottom of screen 
-                               yof= ysize;
+                               // yof= ysize; (not with menu scrolls)
                        }
                }
                
@@ -1264,15 +1343,16 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
                // apply requested offset in the block
                xof += block->xofs/block->aspect;
                yof += block->yofs/block->aspect;
-
+#if 0
                /* clamp to window bounds, could be made into an option if its ever annoying */
                if(     (offscreen= (block->miny+yof)) < 0)      yof -= offscreen; /* bottom */
                else if((offscreen= (block->maxy+yof)-winy) > 0) yof -= offscreen; /* top */
                if(     (offscreen= (block->minx+xof)) < 0)      xof -= offscreen; /* left */
                else if((offscreen= (block->maxx+xof)-winx) > 0) xof -= offscreen; /* right */
+#endif
        }
        
-       /* apply */
+       /* apply offset, buttons in window coords */
        
        for(bt= block->buttons.first; bt; bt= bt->next) {
                ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
@@ -1295,8 +1375,8 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
 
        /* safety calculus */
        if(but) {
-               float midx= (butrct.xmin+butrct.xmax)/2.0;
-               float midy= (butrct.ymin+butrct.ymax)/2.0;
+               float midx= (butrct.xmin+butrct.xmax)/2.0f;
+               float midy= (butrct.ymin+butrct.ymax)/2.0f;
                
                /* when you are outside parent button, safety there should be smaller */
                
@@ -1346,6 +1426,62 @@ static void ui_block_region_draw(const bContext *C, ARegion *ar)
                uiDrawBlock(C, block);
 }
 
+static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
+{
+       int winx, winy;
+       
+       wm_window_get_size(window, &winx, &winy);
+       
+       if(block->minx < MENU_SHADOW_SIDE)
+               block->minx= MENU_SHADOW_SIDE;
+       if(block->maxx > winx-MENU_SHADOW_SIDE)
+               block->maxx= winx-MENU_SHADOW_SIDE;
+       
+       if(block->miny < MENU_SHADOW_BOTTOM)
+               block->miny= MENU_SHADOW_BOTTOM;
+       if(block->maxy > winy-MENU_TOP)
+               block->maxy= winy-MENU_TOP;
+}
+
+void ui_popup_block_scrolltest(uiBlock *block)
+{
+       uiBut *bt;
+       
+       block->flag &= ~(UI_BLOCK_CLIPBOTTOM|UI_BLOCK_CLIPTOP);
+       
+       for(bt= block->buttons.first; bt; bt= bt->next)
+               bt->flag &= ~UI_SCROLLED;
+       
+       if(block->buttons.first==block->buttons.last)
+               return;
+       
+       /* mark buttons that are outside boundary and the ones next to it for arrow(s) */
+       for(bt= block->buttons.first; bt; bt= bt->next) {
+               if(bt->y1 < block->miny) {
+                       bt->flag |= UI_SCROLLED;
+                       block->flag |= UI_BLOCK_CLIPBOTTOM;
+                       /* make space for arrow */
+                       if(bt->y2 < block->miny +10) {
+                               if(bt->next && bt->next->y1 > bt->y1)
+                                       bt->next->flag |= UI_SCROLLED;
+                               if(bt->prev && bt->prev->y1 > bt->y1)
+                                       bt->prev->flag |= UI_SCROLLED;
+                       }
+               }
+               if(bt->y2 > block->maxy) {
+                       bt->flag |= UI_SCROLLED;
+                       block->flag |= UI_BLOCK_CLIPTOP;
+                       /* make space for arrow */
+                       if(bt->y1 > block->maxy -10) {
+                               if(bt->next && bt->next->y2 < bt->y2)
+                                       bt->next->flag |= UI_SCROLLED;
+                               if(bt->prev && bt->prev->y2 < bt->y2)
+                                       bt->prev->flag |= UI_SCROLLED;
+                       }
+               }
+       }
+}
+
 uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg)
 {
        wmWindow *window= CTX_wm_window(C);
@@ -1416,6 +1552,9 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
                block->flag |= UI_BLOCK_POPUP|UI_BLOCK_NUMSELECT;
        }
 
+       /* clip block with window boundary */
+       ui_popup_block_clip(window, block);
+       
        /* the block and buttons were positioned in window space as in 2.4x, now
         * these menu blocks are regions so we bring it back to region space.
         * additionally we add some padding for the menu shadow or rounded menus */
@@ -1423,7 +1562,7 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
        ar->winrct.xmax= block->maxx + MENU_SHADOW_SIDE;
        ar->winrct.ymin= block->miny - MENU_SHADOW_BOTTOM;
        ar->winrct.ymax= block->maxy + MENU_TOP;
-
+       
        block->minx -= ar->winrct.xmin;
        block->maxx -= ar->winrct.xmin;
        block->miny -= ar->winrct.ymin;
@@ -1435,12 +1574,15 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
                bt->y1 -= ar->winrct.ymin;
                bt->y2 -= ar->winrct.ymin;
        }
-
+       
        block->flag |= UI_BLOCK_LOOP;
 
        /* adds subwindow */
        ED_region_init(C, ar);
 
+       /* checks which buttons are visible, sets flags to prevent draw (do after region init) */
+       ui_popup_block_scrolltest(block);
+       
        /* get winmat now that we actually have the subwindow */
        wmSubWindowSet(window, ar->swinid);
        
@@ -1455,6 +1597,10 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
 {
        ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
+       
+       if(handle->scrolltimer)
+               WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), handle->scrolltimer);
+       
        MEM_freeN(handle);
 }
 
@@ -1468,7 +1614,7 @@ static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *a
        uiBut *bt;
        MenuData *md;
        MenuEntry *entry;
-       char *instr= arg_str;
+       const char *instr= arg_str;
        int columns, rows, a, b;
 
        uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
@@ -1495,18 +1641,19 @@ static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *a
                        uiItemL(layout, md->title, md->titleicon);
                }
                else {
-                       uiItemL(layout, md->title, ICON_NULL);
+                       uiItemL(layout, md->title, ICON_NONE);
                        bt= block->buttons.last;
                        bt->flag= UI_TEXT_LEFT;
                }
        }
 
        /* inconsistent, but menus with labels do not look good flipped */
-       for(a=0, b=0; a<md->nitems; a++, b++) {
-               entry= &md->items[a];
-
-               if(entry->sepr && entry->str[0])
+       entry= md->items;
+       for(a=0; a<md->nitems; a++, entry++) {
+               if(entry->sepr && entry->str[0]) {
                        block->flag |= UI_BLOCK_NO_FLIP;
+                       break;
+               }
        }
 
        /* create items */
@@ -1590,7 +1737,7 @@ void ui_block_func_ICONTEXTROW(bContext *UNUSED(C), uiLayout *layout, void *arg_
 }
 
 #if 0
-static void ui_warp_pointer(short x, short y)
+static void ui_warp_pointer(int x, int y)
 {
        /* XXX 2.50 which function to use for this? */
        /* OSX has very poor mousewarp support, it sends events;
@@ -1620,7 +1767,7 @@ void ui_set_but_hsv(uiBut *but)
 }
 
 /* also used by small picker, be careful with name checks below... */
-void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
+static void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
 {
        uiBut *bt;
        float *hsv= ui_block_hsv_get(block);
@@ -1655,7 +1802,7 @@ void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
                        if (rgb_gamma[1] > 1.0f) rgb_gamma[1] = modf(rgb_gamma[1], &intpart);
                        if (rgb_gamma[2] > 1.0f) rgb_gamma[2] = modf(rgb_gamma[2], &intpart);
 
-                       sprintf(col, "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
+                       BLI_snprintf(col, sizeof(col), "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
                        
                        strcpy(bt->poin, col);
                }
@@ -1795,31 +1942,31 @@ static void do_picker_new_mode_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(a
 
 #define PICKER_TOTAL_W (PICKER_W+PICKER_SPACE+PICKER_BAR)
 
-static void circle_picker(uiBlock *block, PointerRNA *ptr, const char *propname)
+static void circle_picker(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop)
 {
        uiBut *bt;
        
        /* HS circle */
-       bt= uiDefButR(block, HSVCIRCLE, 0, "",  0, 0, PICKER_H, PICKER_W, ptr, propname, 0, 0.0, 0.0, 0, 0, "Color");
+       bt= uiDefButR_prop(block, HSVCIRCLE, 0, "",     0, 0, PICKER_H, PICKER_W, ptr, prop, 0, 0.0, 0.0, 0, 0, "Color");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
        
        /* value */
-       bt= uiDefButR(block, HSVCUBE, 0, "", PICKER_W+PICKER_SPACE,0,PICKER_BAR,PICKER_H, ptr, propname, 0, 0.0, 0.0, UI_GRAD_V_ALT, 0, "Value");
+       bt= uiDefButR_prop(block, HSVCUBE, 0, "", PICKER_W+PICKER_SPACE,0,PICKER_BAR,PICKER_H, ptr, prop, 0, 0.0, 0.0, UI_GRAD_V_ALT, 0, "Value");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
 }
 
 
-static void square_picker(uiBlock *block, PointerRNA *ptr, const char *propname, int type)
+static void square_picker(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int type)
 {
        uiBut *bt;
        int bartype = type + 3;
        
        /* HS square */
-       bt= uiDefButR(block, HSVCUBE, 0, "",    0, PICKER_BAR+PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, propname, 0, 0.0, 0.0, type, 0, "Color");
+       bt= uiDefButR_prop(block, HSVCUBE, 0, "",       0, PICKER_BAR+PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, prop, 0, 0.0, 0.0, type, 0, "Color");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
        
        /* value */
-       bt= uiDefButR(block, HSVCUBE, 0, "",            0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, propname, 0, 0.0, 0.0, bartype, 0, "Value");
+       bt= uiDefButR_prop(block, HSVCUBE, 0, "",               0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, prop, 0, 0.0, 0.0, bartype, 0, "Value");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
 }
 
@@ -1834,7 +1981,6 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        static char hexcol[128];
        float rgb_gamma[3];
        float min, max, step, precision;
-       const char *propname = RNA_property_identifier(prop);
        float *hsv= ui_block_hsv_get(block);
        
        ui_block_hsv_get(block);
@@ -1844,10 +1990,10 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        
        /* existence of profile means storage is in linear color space, with display correction */
        if (block->color_profile == BLI_PR_NONE) {
-               sprintf(tip, "Value in Display Color Space");
+               BLI_strncpy(tip, "Value in Display Color Space", sizeof(tip));
                copy_v3_v3(rgb_gamma, rgb);
        } else {
-               sprintf(tip, "Value in Linear RGB Color Space");
+               BLI_strncpy(tip, "Value in Linear RGB Color Space", sizeof(tip));
                /* make an sRGB version, for Hex code */
                linearrgb_to_srgb_v3_v3(rgb_gamma, rgb);
        }
@@ -1860,16 +2006,16 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
 
        switch (U.color_picker_type) {
                case USER_CP_CIRCLE:
-                       circle_picker(block, ptr, propname);
+                       circle_picker(block, ptr, prop);
                        break;
                case USER_CP_SQUARE_SV:
-                       square_picker(block, ptr, propname, UI_GRAD_SV);
+                       square_picker(block, ptr, prop, UI_GRAD_SV);
                        break;
                case USER_CP_SQUARE_HS:
-                       square_picker(block, ptr, propname, UI_GRAD_HS);
+                       square_picker(block, ptr, prop, UI_GRAD_HS);
                        break;
                case USER_CP_SQUARE_HV:
-                       square_picker(block, ptr, propname, UI_GRAD_HV);
+                       square_picker(block, ptr, prop, UI_GRAD_HV);
                        break;
        }
        
@@ -1888,14 +2034,14 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        
        /* RGB values */
        uiBlockBeginAlign(block);
-       bt= uiDefButR(block, NUMSLI, 0, "R ",   0, -60, butwidth, UI_UNIT_Y, ptr, propname, 0, 0.0, 0.0, 0, 3, "Red");
+       bt= uiDefButR_prop(block, NUMSLI, 0, "R ",      0, -60, butwidth, UI_UNIT_Y, ptr, prop, 0, 0.0, 0.0, 0, 3, "Red");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
-       bt= uiDefButR(block, NUMSLI, 0, "G ",   0, -80, butwidth, UI_UNIT_Y, ptr, propname, 1, 0.0, 0.0, 0, 3, "Green");
+       bt= uiDefButR_prop(block, NUMSLI, 0, "G ",      0, -80, butwidth, UI_UNIT_Y, ptr, prop, 1, 0.0, 0.0, 0, 3, "Green");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
-       bt= uiDefButR(block, NUMSLI, 0, "B ",   0, -100, butwidth, UI_UNIT_Y, ptr, propname, 2, 0.0, 0.0, 0, 3, "Blue");
+       bt= uiDefButR_prop(block, NUMSLI, 0, "B ",      0, -100, butwidth, UI_UNIT_Y, ptr, prop, 2, 0.0, 0.0, 0, 3, "Blue");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
 
-       // could use uiItemFullR(col, ptr, prop, -1, 0, UI_ITEM_R_EXPAND|UI_ITEM_R_SLIDER, "", ICON_NULL);
+       // could use uiItemFullR(col, ptr, prop, -1, 0, UI_ITEM_R_EXPAND|UI_ITEM_R_SLIDER, "", ICON_NONE);
        // but need to use uiButSetFunc for updating other fake buttons
        
        /* HSV values */
@@ -1909,14 +2055,14 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        uiBlockEndAlign(block);
 
        if(rgb[3] != FLT_MAX) {
-               bt= uiDefButR(block, NUMSLI, 0, "A ",   0, -120, butwidth, UI_UNIT_Y, ptr, propname, 3, 0.0, 0.0, 0, 0, "Alpha");
+               bt= uiDefButR_prop(block, NUMSLI, 0, "A ",      0, -120, butwidth, UI_UNIT_Y, ptr, prop, 3, 0.0, 0.0, 0, 0, "Alpha");
                uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
        }
        else {
                rgb[3]= 1.0f;
        }
 
-       sprintf(hexcol, "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
+       BLI_snprintf(hexcol, sizeof(hexcol), "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
 
        bt= uiDefBut(block, TEX, 0, "Hex: ", 0, -60, butwidth, UI_UNIT_Y, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
        uiButSetFunc(bt, do_hex_rna_cb, bt, hexcol);
@@ -2010,7 +2156,7 @@ static int ui_popup_string_hash(char *str)
        return hash;
 }
 
-static int ui_popup_menu_hash(char *str)
+static int ui_popup_menu_hash(const char *str)
 {
        return BLI_ghashutil_strhash(str);
 }
@@ -2060,8 +2206,6 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
 {
        uiBlock *block;
        uiBut *bt;
-       ScrArea *sa;
-       ARegion *ar;
        uiPopupMenu *pup= arg_pup;
        int offset[2], direction, minwidth, width, height, flip;
 
@@ -2115,7 +2259,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
                           the offset is negative because we are inverse moving the
                           block to be under the mouse */
                        offset[0]= -(bt->x1 + 0.8f*(bt->x2 - bt->x1));
-                       offset[1]= -(bt->y1 + 0.5f*MENU_BUTTON_HEIGHT);
+                       offset[1]= -(bt->y1 + 0.5f*UI_UNIT_Y);
                }
                else {
                        /* position mouse at 0.8*width of the button and below the tile
@@ -2124,7 +2268,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
                        for(bt=block->buttons.first; bt; bt=bt->next)
                                offset[0]= MIN2(offset[0], -(bt->x1 + 0.8f*(bt->x2 - bt->x1)));
 
-                       offset[1]= 1.5*MENU_BUTTON_HEIGHT;
+                       offset[1]= 1.5*UI_UNIT_Y;
                }
 
                block->minbounds= minwidth;
@@ -2133,10 +2277,9 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
        else {
                /* for a header menu we set the direction automatic */
                if(!pup->slideout && flip) {
-                       sa= CTX_wm_area(C);
-                       ar= CTX_wm_region(C);
-
+                       ScrArea *sa= CTX_wm_area(C);
                        if(sa && sa->headertype==HEADERDOWN) {
+                               ARegion *ar= CTX_wm_region(C);
                                if(ar && ar->regiontype == RGN_TYPE_HEADER) {
                                        uiBlockSetDirection(block, UI_TOP);
                                        uiBlockFlipOrder(block);
@@ -2160,10 +2303,9 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but, uiMenuCreateFunc menu_func, void *arg, char *str)
 {
        wmWindow *window= CTX_wm_window(C);
-       uiStyle *style= U.uistyles.first;
+       uiStyle *style= UI_GetStyle();
        uiPopupBlockHandle *handle;
        uiPopupMenu *pup;
-       
        pup= MEM_callocN(sizeof(uiPopupMenu), "menu dummy");
        pup->block= uiBeginBlock(C, NULL, "ui_button_menu_create", UI_EMBOSSP);
        pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
@@ -2178,6 +2320,19 @@ uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut
                pup->popup= 1;
                pup->block->flag |= UI_BLOCK_NO_FLIP;
        }
+       /* some enums reversing is strange, currently we have no good way to
+        * reverse some enum's but not others, so reverse all so the first menu
+        * items are always close to the mouse cursor */
+#if 0
+       else {
+               /* if this is an rna button then we can assume its an enum
+                * flipping enums is generally not good since the order can be
+                * important [#28786] */
+               if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
+                       pup->block->flag |= UI_BLOCK_NO_FLIP;
+               }
+       }
+#endif
 
        if(str) {
                /* menu is created from a string */
@@ -2209,7 +2364,7 @@ uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut
 /* only return handler, and set optional title */
 uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
 {
-       uiStyle *style= U.uistyles.first;
+       uiStyle *style= UI_GetStyle();
        uiPopupMenu *pup= MEM_callocN(sizeof(uiPopupMenu), "popup menu");
        uiBut *but;
        
@@ -2227,11 +2382,11 @@ uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
                char titlestr[256];
                
                if(icon) {
-                       sprintf(titlestr, " %s", title);
-                       uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
+                       BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
+                       uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
                }
                else {
-                       but= uiDefBut(pup->block, LABEL, 0, title, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
+                       but= uiDefBut(pup->block, LABEL, 0, title, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
                        but->flag= UI_TEXT_LEFT;
                }
        }
@@ -2311,6 +2466,7 @@ static void confirm_operator(bContext *C, wmOperator *op, const char *title, con
        
        s= buf;
        if (title) s+= sprintf(s, "%s%%t|%s", title, item);
+       (void)s;
        
        handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
 
@@ -2324,7 +2480,7 @@ void uiPupMenuOkee(bContext *C, const char *opname, const char *str, ...)
        va_list ap;
        char titlestr[256];
 
-       sprintf(titlestr, "OK? %%i%d", ICON_QUESTION);
+       BLI_snprintf(titlestr, sizeof(titlestr), "OK? %%i%d", ICON_QUESTION);
 
        va_start(ap, str);
        vconfirm_opname(C, opname, titlestr, str, ap);
@@ -2364,9 +2520,9 @@ void uiPupMenuError(bContext *C, const char *str, ...)
        char nfmt[256];
        char titlestr[256];
 
-       sprintf(titlestr, "Error %%i%d", ICON_ERROR);
+       BLI_snprintf(titlestr, sizeof(titlestr), "Error %%i%d", ICON_ERROR);
 
-       sprintf(nfmt, "%s", str);
+       BLI_strncpy(nfmt, str, sizeof(nfmt));
 
        va_start(ap, str);
        vconfirm_opname(C, NULL, titlestr, nfmt, ap);
@@ -2413,14 +2569,14 @@ void uiPupMenuInvoke(bContext *C, const char *idname)
        MenuType *mt= WM_menutype_find(idname, TRUE);
 
        if(mt==NULL) {
-               printf("uiPupMenuInvoke: named menu \"%s\" not found\n", idname);
+               printf("%s: named menu \"%s\" not found\n", __func__, idname);
                return;
        }
 
        if(mt->poll && mt->poll(C, mt)==0)
                return;
 
-       pup= uiPupMenuBegin(C, mt->label, ICON_NULL);
+       pup= uiPupMenuBegin(C, mt->label, ICON_NONE);
        layout= uiPupMenuLayout(pup);
 
        menu.layout= layout;
@@ -2453,6 +2609,25 @@ void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
        uiPupBlockO(C, func, arg, NULL, 0);
 }
 
+void uiPupBlockEx(bContext *C, uiBlockCreateFunc func, uiBlockCancelFunc cancel_func, void *arg)
+{
+       wmWindow *window= CTX_wm_window(C);
+       uiPopupBlockHandle *handle;
+       
+       handle= ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
+       handle->popup= 1;
+       handle->retvalue= 1;
+
+       handle->popup_arg= arg;
+       // handle->popup_func= operator_cb;
+       handle->cancel_func= cancel_func;
+       // handle->opcontext= opcontext;
+       
+       UI_add_popup_handlers(C, &window->modalhandlers, handle);
+       WM_event_add_mousemove(C);
+}
+
+#if 0 /* UNUSED */
 void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
 {
        wmWindow *window= CTX_wm_window(C);
@@ -2470,6 +2645,7 @@ void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int
        UI_add_popup_handlers(C, &window->modalhandlers, handle);
        WM_event_add_mousemove(C);
 }
+#endif
 
 void uiPupBlockClose(bContext *C, uiBlock *block)
 {