merge from trunk #37722
[blender-staging.git] / source / blender / editors / interface / interface_regions.c
index 7f70bfe335a3fc85c61270e163724815c8567aa1..62043f240e453fb6c44b4e38578edbb196226cb8 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 <string.h>
+#include <assert.h>
 
 #include "MEM_guardedalloc.h"
 
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
 #include "BLI_dynstr.h"
 #include "BLI_ghash.h"
 
 #include "BKE_context.h"
-#include "BKE_icons.h"
-#include "BKE_report.h"
 #include "BKE_screen.h"
-#include "BKE_texture.h"
-#include "BKE_utildefines.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
@@ -64,7 +67,6 @@
 
 #include "interface_intern.h"
 
-#define MENU_BUTTON_HEIGHT     20
 #define MENU_SEPR_HEIGHT       6
 #define B_NOP                  -1
 #define MENU_SHADOW_SIDE       8
@@ -74,7 +76,7 @@
 /*********************** Menu Data Parsing ********************* */
 
 typedef struct MenuEntry {
-       char *str;
+       const char *str;
        int retval;
        int icon;
        int sepr;
@@ -82,7 +84,7 @@ typedef struct MenuEntry {
 
 typedef struct MenuData {
        char *instr;
-       char *title;
+       const char *title;
        int titleicon;
        
        MenuEntry *items;
@@ -102,7 +104,7 @@ static MenuData *menudata_new(char *instr)
        return md;
 }
 
-static void menudata_set_title(MenuData *md, char *title, int titleicon)
+static void menudata_set_title(MenuData *md, const char *title, int titleicon)
 {
        if (!md->title)
                md->title= title;
@@ -110,7 +112,7 @@ static void menudata_set_title(MenuData *md, char *title, int titleicon)
                md->titleicon= titleicon;
 }
 
-static void menudata_add_item(MenuData *md, char *str, int retval, int icon, int sepr)
+static void menudata_add_item(MenuData *md, const char *str, int retval, int icon, int sepr)
 {
        if (md->nitems==md->itemssize) {
                int nsize= md->itemssize?(md->itemssize<<1):1;
@@ -132,7 +134,7 @@ static void menudata_add_item(MenuData *md, char *str, int retval, int icon, int
        md->nitems++;
 }
 
-void menudata_free(MenuData *md)
+static void menudata_free(MenuData *md)
 {
        MEM_freeN(md->instr);
        if (md->items)
@@ -153,11 +155,12 @@ 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(char *str) 
 {
        char *instr= BLI_strdup(str);
        MenuData *md= menudata_new(instr);
-       char *nitem= NULL, *s= instr;
+       const char *nitem= NULL;
+       char *s= instr;
        int nicon=0, nretval= 1, nitem_is_title= 0, nitem_is_sepr= 0;
        
        while (1) {
@@ -273,7 +276,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;
 
@@ -286,7 +289,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);
@@ -304,12 +307,12 @@ typedef struct uiTooltipData {
        rcti bbox;
        uiFontStyle fstyle;
        char lines[MAX_TOOLTIP_LINES][512];
-       int linedark[MAX_TOOLTIP_LINES];
+       unsigned int color[MAX_TOOLTIP_LINES];
        int totline;
        int toth, spaceh, lineh;
 } uiTooltipData;
 
-static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
+static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
 {
        uiTooltipData *data= ar->regiondata;
        rcti bbox= data->bbox;
@@ -324,16 +327,14 @@ static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
        bbox.ymin= bbox.ymax - data->lineh;
 
        for(a=0; a<data->totline; a++) {
-               if(!data->linedark[a]) glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-               else glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
-
+               cpack(data->color[a]);
                uiStyleFontDraw(&data->fstyle, &bbox, data->lines[a]);
                bbox.ymin -= data->lineh + data->spaceh;
                bbox.ymax -= data->lineh + data->spaceh;
        }
 }
 
-static void ui_tooltip_region_free(ARegion *ar)
+static void ui_tooltip_region_free_cb(ARegion *ar)
 {
        uiTooltipData *data;
 
@@ -360,8 +361,40 @@ 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 spesific 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(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;
                data->totline++;
        }
 
@@ -371,7 +404,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
                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);
-                       data->linedark[data->totline]= 1;
+                       data->color[data->totline]= 0x888888;
                        data->totline++;
                }
        }
@@ -381,18 +414,18 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                ui_get_but_string(but, buf, sizeof(buf));
                if(buf[0]) {
                        BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Value: %s", buf);
-                       data->linedark[data->totline]= 1;
+                       data->color[data->totline]= 0x888888;
                        data->totline++;
                }
        }
 
        if(but->rnaprop) {
-               int unit_type = RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
+               int unit_type= uiButGetUnitType(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));
-                               data->linedark[data->totline]= 1;
+                               data->color[data->totline]= 0x888888;
                                data->totline++;
                        }
                }
@@ -401,21 +434,23 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                        if(ui_but_anim_expression_get(but, buf, sizeof(buf))) {
                                /* expression */
                                BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Expression: %s", buf);
-                               data->linedark[data->totline]= 1;
+                               data->color[data->totline]= 0x888888;
                                data->totline++;
                        }
                }
 
                /* rna info */
-               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s.%s", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
-               data->linedark[data->totline]= 1;
-               data->totline++;
+               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));
+                       data->color[data->totline]= 0x888888;
+                       data->totline++;
+               }
                
                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);
-                               data->linedark[data->totline]= 1;
+                               data->color[data->totline]= 0x888888;
                                data->totline++;
                        }
                }
@@ -428,13 +463,30 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                str= WM_operator_pystring(C, but->optype, opptr, 0);
 
                /* operator info */
-               BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s", str);
-               data->linedark[data->totline]= 1;
-               data->totline++;
+               if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
+                       BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s", str);
+                       data->color[data->totline]= 0x888888;
+                       data->totline++;
+               }
 
                MEM_freeN(str);
+
+               /* second check if we are disabled - why */
+               if(but->flag & UI_BUT_DISABLED) {
+                       const char *poll_msg;
+                       CTX_wm_operator_poll_msg_set(C, NULL);
+                       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);
+                               data->color[data->totline]= 0x6666ff; /* alert */
+                               data->totline++;                        
+                       }
+               }
        }
 
+       assert(data->totline < MAX_TOOLTIP_LINES);
+       
        if(data->totline == 0) {
                MEM_freeN(data);
                return NULL;
@@ -444,8 +496,8 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
        ar= ui_add_temporary_region(CTX_wm_screen(C));
 
        memset(&type, 0, sizeof(ARegionType));
-       type.draw= ui_tooltip_region_draw;
-       type.free= ui_tooltip_region_free;
+       type.draw= ui_tooltip_region_draw_cb;
+       type.free= ui_tooltip_region_free_cb;
        ar->type= &type;
        
        /* set font, get bb */
@@ -453,12 +505,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->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->lines[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;
@@ -467,17 +524,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; 
        
@@ -507,9 +569,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 */
@@ -596,9 +660,12 @@ int uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int icon
                return 1;
        }
        
-       BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
-       items->pointers[items->totitem]= poin;
-       items->icons[items->totitem]= iconid;
+       if(items->names)
+               BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
+       if(items->pointers)
+               items->pointers[items->totitem]= poin;
+       if(items->icons)
+               items->icons[items->totitem]= iconid;
        
        items->totitem++;
        
@@ -607,7 +674,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 */
@@ -658,7 +725,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 */
@@ -821,7 +888,7 @@ void ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
        }
 }
 
-static void ui_searchbox_region_draw(const bContext *C, ARegion *ar)
+static void ui_searchbox_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
 {
        uiSearchboxData *data= ar->regiondata;
        
@@ -829,7 +896,7 @@ static void ui_searchbox_region_draw(const bContext *C, ARegion *ar)
        wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
 
        if(!data->noback)
-               ui_draw_search_back(U.uistyles.first, NULL, &data->bbox);
+               ui_draw_search_back(NULL, NULL, &data->bbox); /* style not used yet */
        
        /* draw text */
        if(data->items.totitem) {
@@ -888,7 +955,7 @@ static void ui_searchbox_region_draw(const bContext *C, ARegion *ar)
        }
 }
 
-static void ui_searchbox_region_free(ARegion *ar)
+static void ui_searchbox_region_free_cb(ARegion *ar)
 {
        uiSearchboxData *data= ar->regiondata;
        int a;
@@ -904,8 +971,6 @@ static void ui_searchbox_region_free(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
@@ -920,8 +985,8 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        ar= ui_add_temporary_region(CTX_wm_screen(C));
        
        memset(&type, 0, sizeof(ARegionType));
-       type.draw= ui_searchbox_region_draw;
-       type.free= ui_searchbox_region_free;
+       type.draw= ui_searchbox_region_draw_cb;
+       type.free= ui_searchbox_region_free_cb;
        ar->type= &type;
        
        /* create searchbox data */
@@ -960,7 +1025,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 {
@@ -1012,9 +1077,9 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
                                x2= winx;
                        }
                }
-               if(y1 < 0) {
+               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;
@@ -1062,6 +1127,47 @@ void ui_searchbox_free(bContext *C, ARegion *ar)
        ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
 }
 
+/* sets red alert if button holds a string it can't find */
+/* XXX weak: search_func adds all partial matches... */
+void ui_but_search_test(uiBut *but)
+{
+       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;
+       items->names= MEM_callocN(items->maxitem*sizeof(void *), "search names");
+       for(x1=0; x1<items->maxitem; x1++)
+               items->names[x1]= MEM_callocN(but->hardmax+1, "search names");
+       
+       but->search_func(but->block->evil_C, but->search_arg, but->drawstr, items);
+       
+       /* only redalert when we are sure of it, this can miss cases when >10 matches */
+       if(items->totitem==0)
+               uiButSetFlag(but, UI_BUT_REDALERT);
+       else if(items->more==0) {
+               for(x1= 0; x1<items->totitem; x1++)
+                       if(strcmp(but->drawstr, items->names[x1])==0)
+                               break;
+               if(x1==items->totitem)
+                       uiButSetFlag(but, UI_BUT_REDALERT);
+       }
+       
+       for(x1=0; x1<items->maxitem; x1++)
+               MEM_freeN(items->names[x1]);
+       MEM_freeN(items->names);
+       MEM_freeN(items);
+}
+
 
 /************************* Creating Menu Blocks **********************/
 
@@ -1120,17 +1226,26 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
        if(but) {
                int left=0, right=0, top=0, down=0;
                int winx, winy;
+               // 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 */
@@ -1195,7 +1310,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)
                        }
                }
                
@@ -1210,9 +1325,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);
@@ -1235,8 +1357,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 */
                
@@ -1286,6 +1408,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);
@@ -1356,6 +1534,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 */
@@ -1363,7 +1544,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;
@@ -1375,12 +1556,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);
        
@@ -1395,12 +1579,16 @@ 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);
 }
 
 /***************************** Menu Button ***************************/
 
-static void ui_block_func_MENUSTR(bContext *C, uiLayout *layout, void *arg_str)
+static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *arg_str)
 {
        uiBlock *block= uiLayoutGetBlock(layout);
        uiPopupBlockHandle *handle= block->handle;
@@ -1435,7 +1623,7 @@ static void ui_block_func_MENUSTR(bContext *C, uiLayout *layout, void *arg_str)
                        uiItemL(layout, md->title, md->titleicon);
                }
                else {
-                       uiItemL(layout, md->title, 0);
+                       uiItemL(layout, md->title, ICON_NONE);
                        bt= block->buttons.last;
                        bt->flag= UI_TEXT_LEFT;
                }
@@ -1482,7 +1670,7 @@ static void ui_block_func_MENUSTR(bContext *C, uiLayout *layout, void *arg_str)
        menudata_free(md);
 }
 
-void ui_block_func_ICONROW(bContext *C, uiLayout *layout, void *arg_but)
+void ui_block_func_ICONROW(bContext *UNUSED(C), uiLayout *layout, void *arg_but)
 {
        uiBlock *block= uiLayoutGetBlock(layout);
        uiPopupBlockHandle *handle= block->handle;
@@ -1496,7 +1684,7 @@ void ui_block_func_ICONROW(bContext *C, uiLayout *layout, void *arg_but)
                        &handle->retvalue, (float)a, 0.0, 0, 0, "");
 }
 
-void ui_block_func_ICONTEXTROW(bContext *C, uiLayout *layout, void *arg_but)
+void ui_block_func_ICONTEXTROW(bContext *UNUSED(C), uiLayout *layout, void *arg_but)
 {
        uiBlock *block= uiLayoutGetBlock(layout);
        uiPopupBlockHandle *handle= block->handle;
@@ -1530,7 +1718,7 @@ void ui_block_func_ICONTEXTROW(bContext *C, uiLayout *layout, void *arg_but)
 }
 
 #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;
@@ -1553,18 +1741,22 @@ static void ui_warp_pointer(short x, short y)
 void ui_set_but_hsv(uiBut *but)
 {
        float col[3];
+       float *hsv= ui_block_hsv_get(but->block);
        
-       hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
+       hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
        ui_set_but_vectorf(but, col);
 }
 
 /* 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[3];
+       float *hsv= ui_block_hsv_get(block);
        
-       rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
+       /* this is to keep the H and S value when V is equal to zero
+        * and we are working in HSV mode, of course!
+        */
+       rgb_to_hsv_compat(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
        
        // this updates button strings, is hackish... but button pointers are on stack of caller function
        for(bt= block->buttons.first; bt; bt= bt->next) {
@@ -1591,7 +1783,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", (unsigned int)(rgb_gamma[0]*255.0), (unsigned int)(rgb_gamma[1]*255.0), (unsigned int)(rgb_gamma[2]*255.0));
+                       sprintf(col, "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
                        
                        strcpy(bt->poin, col);
                }
@@ -1620,7 +1812,7 @@ void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
        }
 }
 
-static void do_picker_rna_cb(bContext *C, void *bt1, void *unused)
+static void do_picker_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
        uiBut *but= (uiBut *)bt1;
        uiPopupBlockHandle *popup= but->block->handle;
@@ -1637,12 +1829,12 @@ static void do_picker_rna_cb(bContext *C, void *bt1, void *unused)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
-static void do_hsv_rna_cb(bContext *C, void *bt1, void *hsv_arg)
+static void do_hsv_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
        uiBut *but= (uiBut *)bt1;
        uiPopupBlockHandle *popup= but->block->handle;
-       float *hsv = (float *)hsv_arg;
        float rgb[3];
+       float *hsv= ui_block_hsv_get(but->block);
        
        hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
        
@@ -1652,7 +1844,7 @@ static void do_hsv_rna_cb(bContext *C, void *bt1, void *hsv_arg)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
-static void do_hex_rna_cb(bContext *C, void *bt1, void *hexcl)
+static void do_hex_rna_cb(bContext *UNUSED(C), void *bt1, void *hexcl)
 {
        uiBut *but= (uiBut *)bt1;
        uiPopupBlockHandle *popup= but->block->handle;
@@ -1673,7 +1865,7 @@ static void do_hex_rna_cb(bContext *C, void *bt1, void *hexcl)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
-static void close_popup_cb(bContext *C, void *bt1, void *arg)
+static void close_popup_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
        uiBut *but= (uiBut *)bt1;
        uiPopupBlockHandle *popup= but->block->handle;
@@ -1713,7 +1905,7 @@ static void picker_new_hide_reveal(uiBlock *block, short colormode)
        }
 }
 
-static void do_picker_new_mode_cb(bContext *C, void *bt1, void *colv)
+static void do_picker_new_mode_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
        uiBut *bt= bt1;
        short colormode= ui_get_but_val(bt);
@@ -1736,11 +1928,11 @@ static void circle_picker(uiBlock *block, PointerRNA *ptr, const char *propname)
        uiBut *bt;
        
        /* HS circle */
-       bt= uiDefButR(block, HSVCIRCLE, 0, "",  0, 0, PICKER_H, PICKER_W, ptr, propname, -1, 0.0, 0.0, 0, 0, "");
+       bt= uiDefButR(block, HSVCIRCLE, 0, "",  0, 0, PICKER_H, PICKER_W, ptr, propname, 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, -1, 0.0, 0.0, UI_GRAD_V_ALT, 0, "");
+       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");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
 }
 
@@ -1751,11 +1943,11 @@ static void square_picker(uiBlock *block, PointerRNA *ptr, const char *propname,
        int bartype = type + 3;
        
        /* HS square */
-       bt= uiDefButR(block, HSVCUBE, 0, "",    0, PICKER_BAR+PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, propname, -1, 0.0, 0.0, type, 0, "");
+       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");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
        
        /* value */
-       bt= uiDefButR(block, HSVCUBE, 0, "",            0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, propname, -1, 0.0, 0.0, bartype, 0, "");
+       bt= uiDefButR(block, HSVCUBE, 0, "",            0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, propname, 0, 0.0, 0.0, bartype, 0, "Value");
        uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
 }
 
@@ -1767,15 +1959,18 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        uiBut *bt;
        int width, butwidth;
        static char tip[50];
-       static float hsv[3];
        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);
        
        width= PICKER_TOTAL_W;
        butwidth = width - UI_UNIT_X - 10;
        
-       /* existence of profile means storage is in linear colour space, with display correction */
+       /* 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");
                copy_v3_v3(rgb_gamma, rgb);
@@ -1785,8 +1980,11 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
                linearrgb_to_srgb_v3_v3(rgb_gamma, rgb);
        }
        
+       /* sneaky way to check for alpha */
+       rgb[3]= FLT_MAX;
+
+       RNA_property_float_ui_range(ptr, prop, &min, &max, &step, &precision);
        RNA_property_float_get_array(ptr, prop, rgb);
-       rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
 
        switch (U.color_picker_type) {
                case USER_CP_CIRCLE:
@@ -1806,11 +2004,11 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        /* mode */
        uiBlockBeginAlign(block);
        bt= uiDefButS(block, ROW, 0, "RGB",     0, -30, width/3, UI_UNIT_Y, &colormode, 0.0, 0.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_new_mode_cb, bt, rgb);
+       uiButSetFunc(bt, do_picker_new_mode_cb, bt, NULL);
        bt= uiDefButS(block, ROW, 0, "HSV",     width/3, -30, width/3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_new_mode_cb, bt, hsv);
+       uiButSetFunc(bt, do_picker_new_mode_cb, bt, NULL);
        bt= uiDefButS(block, ROW, 0, "Hex",     2*width/3, -30, width/3, UI_UNIT_Y, &colormode, 0.0, 2.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_new_mode_cb, bt, hexcol);
+       uiButSetFunc(bt, do_picker_new_mode_cb, bt, NULL);
        uiBlockEndAlign(block);
 
        bt= uiDefIconButO(block, BUT, "UI_OT_eyedropper", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, butwidth+10, -60, UI_UNIT_X, UI_UNIT_Y, NULL);
@@ -1818,38 +2016,47 @@ 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, 0, "");
+       bt= uiDefButR(block, NUMSLI, 0, "R ",   0, -60, butwidth, UI_UNIT_Y, ptr, propname, 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, 0, "");
+       bt= uiDefButR(block, NUMSLI, 0, "G ",   0, -80, butwidth, UI_UNIT_Y, ptr, propname, 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, 0, "");
+       bt= uiDefButR(block, NUMSLI, 0, "B ",   0, -100, butwidth, UI_UNIT_Y, ptr, propname, 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, "", 0);
+
+       // 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 */
        uiBlockBeginAlign(block);
-       bt= uiDefButF(block, NUMSLI, 0, "H ",   0, -60, butwidth, UI_UNIT_Y, hsv, 0.0, 1.0, 10, 3, "");
+       bt= uiDefButF(block, NUMSLI, 0, "H ",   0, -60, butwidth, UI_UNIT_Y, hsv, 0.0, 1.0, 10, 3, "Hue");
        uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
-       bt= uiDefButF(block, NUMSLI, 0, "S ",   0, -80, butwidth, UI_UNIT_Y, hsv+1, 0.0, 1.0, 10, 3, "");
+       bt= uiDefButF(block, NUMSLI, 0, "S ",   0, -80, butwidth, UI_UNIT_Y, hsv+1, 0.0, 1.0, 10, 3, "Saturation");
        uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
-       bt= uiDefButF(block, NUMSLI, 0, "V ",   0, -100, butwidth, UI_UNIT_Y, hsv+2, 0.0, 1.0, 10, 3, "");
+       bt= uiDefButF(block, NUMSLI, 0, "V ",   0, -100, butwidth, UI_UNIT_Y, hsv+2, 0.0, max, 10, 3, "Value");
        uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
        uiBlockEndAlign(block);
-       
-       rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
-       
-       sprintf(hexcol, "%02X%02X%02X", (unsigned int)(rgb_gamma[0]*255.0), (unsigned int)(rgb_gamma[1]*255.0), (unsigned int)(rgb_gamma[2]*255.0));    
+
+       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");
+               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]));
 
        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);
        uiDefBut(block, LABEL, 0, "(Gamma Corrected)", 0, -80, butwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
 
+       rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
+
        picker_new_hide_reveal(block, colormode);
 }
 
 
-static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *event)
+static int ui_picker_small_wheel_cb(const bContext *UNUSED(C), uiBlock *block, wmEvent *event)
 {
        float add= 0.0f;
        
@@ -1865,12 +2072,13 @@ static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *eve
                        if(but->type==HSVCUBE && but->active==NULL) {
                                uiPopupBlockHandle *popup= block->handle;
                                float col[3];
+                               float *hsv= ui_block_hsv_get(block);
                                
                                ui_get_but_vectorf(but, col);
                                
-                               rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
-                               but->hsv[2]= CLAMPIS(but->hsv[2]+add, 0.0f, 1.0f);
-                               hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
+                               rgb_to_hsv_compat(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
+                               hsv[2]= CLAMPIS(hsv[2]+add, 0.0f, 1.0f);
+                               hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
 
                                ui_set_but_vectorf(but, col);
                                
@@ -1901,12 +2109,13 @@ uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_bu
        uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
        
        VECCOPY(handle->retvec, but->editvec);
-
+       
        uiBlockPicker(block, handle->retvec, &but->rnapoin, but->rnaprop);
-       block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
+       
+       block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN|UI_BLOCK_OUT_1;
        uiBoundsBlock(block, 10);
        
-       block->block_event_func= ui_picker_small_wheel;
+       block->block_event_func= ui_picker_small_wheel_cb;
        
        /* and lets go */
        block->direction= UI_TOP;
@@ -2034,7 +2243,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
@@ -2043,7 +2252,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;
@@ -2095,13 +2304,13 @@ uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut
                pup->mx= window->eventstate->x;
                pup->my= window->eventstate->y;
                pup->popup= 1;
+               pup->block->flag |= UI_BLOCK_NO_FLIP;
        }
 
        if(str) {
                /* menu is created from a string */
                pup->menu_func= ui_block_func_MENUSTR;
                pup->menu_arg= str;
-               // XXX pup->block->flag |= UI_BLOCK_NO_FLIP;
        }
        else {
                /* menu is created from a callback */
@@ -2147,10 +2356,10 @@ uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
                
                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, "");
+                       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, (char*)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;
                }
        }
@@ -2207,28 +2416,30 @@ static void confirm_cancel_operator(void *opv)
        WM_operator_free(opv);
 }
 
-static void vconfirm_opname(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
+static void vconfirm_opname(bContext *C, const char *opname, const char *title, const char *itemfmt, va_list ap)
 {
        uiPopupBlockHandle *handle;
        char *s, buf[512];
 
        s= buf;
        if (title) s+= sprintf(s, "%s%%t|", title);
-       vsprintf(s, itemfmt, ap);
+       vsnprintf(s, sizeof(buf) - (s - buf), itemfmt, ap);
+       buf[sizeof(buf) - 1]= '\0';
 
        handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
 
        handle->popup_func= operator_name_cb;
-       handle->popup_arg= opname;
+       handle->popup_arg= (void *)opname;
 }
 
-static void confirm_operator(bContext *C, wmOperator *op, char *title, char *item)
+static void confirm_operator(bContext *C, wmOperator *op, const char *title, const char *item)
 {
        uiPopupBlockHandle *handle;
        char *s, buf[512];
        
        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);
 
@@ -2237,7 +2448,7 @@ static void confirm_operator(bContext *C, wmOperator *op, char *title, char *ite
        handle->cancel_func= confirm_cancel_operator;
 }
 
-void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
+void uiPupMenuOkee(bContext *C, const char *opname, const char *str, ...)
 {
        va_list ap;
        char titlestr[256];
@@ -2249,7 +2460,7 @@ void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
        va_end(ap);
 }
 
-void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
+void uiPupMenuSaveOver(bContext *C, wmOperator *op, const char *filename)
 {
        size_t len= strlen(filename);
 
@@ -2267,7 +2478,7 @@ void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
                confirm_operator(C, op, "Save Over", filename);
 }
 
-void uiPupMenuNotice(bContext *C, char *str, ...)
+void uiPupMenuNotice(bContext *C, const char *str, ...)
 {
        va_list ap;
 
@@ -2276,7 +2487,7 @@ void uiPupMenuNotice(bContext *C, char *str, ...)
        va_end(ap);
 }
 
-void uiPupMenuError(bContext *C, char *str, ...)
+void uiPupMenuError(bContext *C, const char *str, ...)
 {
        va_list ap;
        char nfmt[256];
@@ -2305,7 +2516,7 @@ void uiPupMenuReports(bContext *C, ReportList *reports)
        ds= BLI_dynstr_new();
 
        for(report=reports->list.first; report; report=report->next) {
-               if(report->type <= reports->printlevel)
+               if(report->type < reports->printlevel)
                        ; /* pass */
                else if(report->type >= RPT_ERROR)
                        BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
@@ -2338,7 +2549,7 @@ void uiPupMenuInvoke(bContext *C, const char *idname)
        if(mt->poll && mt->poll(C, mt)==0)
                return;
 
-       pup= uiPupMenuBegin(C, mt->label, 0);
+       pup= uiPupMenuBegin(C, mt->label, ICON_NONE);
        layout= uiPupMenuLayout(pup);
 
        menu.layout= layout;
@@ -2352,7 +2563,7 @@ void uiPupMenuInvoke(bContext *C, const char *idname)
 
 /*************************** Popup Block API **************************/
 
-void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, char *opname, int opcontext)
+void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, const char *opname, int opcontext)
 {
        wmWindow *window= CTX_wm_window(C);
        uiPopupBlockHandle *handle;
@@ -2371,6 +2582,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);
@@ -2388,6 +2618,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)
 {
@@ -2397,3 +2628,7 @@ void uiPupBlockClose(bContext *C, uiBlock *block)
        }
 }
 
+float *ui_block_hsv_get(uiBlock *block)
+{
+       return block->_hsv;
+}