merge from trunk #37722
[blender-staging.git] / source / blender / editors / interface / interface_regions.c
index 55b908a44ce1526a2a26f1e4ea237942dae47965..62043f240e453fb6c44b4e38578edbb196226cb8 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2008 Blender Foundation.
  * All rights reserved.
  * ***** 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 "DNA_screen_types.h"
-#include "DNA_view2d_types.h"
 #include "DNA_userdef_types.h"
-#include "DNA_windowmanager_types.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"
@@ -67,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
@@ -77,7 +76,7 @@
 /*********************** Menu Data Parsing ********************* */
 
 typedef struct MenuEntry {
-       char *str;
+       const char *str;
        int retval;
        int icon;
        int sepr;
@@ -85,7 +84,7 @@ typedef struct MenuEntry {
 
 typedef struct MenuData {
        char *instr;
-       char *title;
+       const char *title;
        int titleicon;
        
        MenuEntry *items;
@@ -105,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;
@@ -113,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;
@@ -135,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)
@@ -156,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) {
@@ -276,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;
 
@@ -289,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);
@@ -307,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;
@@ -327,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;
 
@@ -363,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++;
        }
 
@@ -374,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++;
                }
        }
@@ -384,25 +414,46 @@ 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= 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->color[data->totline]= 0x888888;
+                               data->totline++;
+                       }
+               }
+               
                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);
-                               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->color[data->totline]= 0x888888;
+                               data->totline++;
+                       }
+               }
        }
        else if (but->optype) {
                PointerRNA *opptr;
@@ -412,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;
@@ -428,42 +496,50 @@ 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 */
        data->fstyle= style->widget; /* copy struct */
        data->fstyle.align= UI_STYLE_TEXT_CENTER;
-       ui_fontscale(&data->fstyle.points, aspect);
        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;
-       fonth *= aspect;
 
        ar->regiondata= data;
 
        data->toth= fonth;
-       data->lineh= h*aspect;
-       data->spaceh= 5*aspect;
+       data->lineh= h;
+       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 - 10.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; 
        
@@ -493,9 +569,11 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                        x2= winx;
                }
        }
-       if(y1 < 0) {
-               y1 += 56*aspect;
-               y2 += 56*aspect;
+       /* 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 */
@@ -547,6 +625,8 @@ typedef struct uiSearchboxData {
        uiSearchItems items;
        int active;             /* index in items array */
        int noback;             /* when menu opened with enough space for this */
+       int preview;    /* draw thumbnail previews, rather than list */
+       int prv_rows, prv_cols;
 } uiSearchboxData;
 
 #define SEARCH_ITEMS   10
@@ -580,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++;
        
@@ -591,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 */
@@ -628,14 +711,34 @@ static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
 
 static void ui_searchbox_butrect(rcti *rect, uiSearchboxData *data, int itemnr)
 {
-       int buth= (data->bbox.ymax-data->bbox.ymin - 2*MENU_TOP)/SEARCH_ITEMS;
-       
-       *rect= data->bbox;
-       rect->xmin= data->bbox.xmin + 3.0f;
-       rect->xmax= data->bbox.xmax - 3.0f;
-       
-       rect->ymax= data->bbox.ymax - MENU_TOP - itemnr*buth;
-       rect->ymin= rect->ymax - buth;
+       /* thumbnail preview */
+       if (data->preview) {
+               int buth = (data->bbox.ymax - data->bbox.ymin - 2*MENU_TOP) / data->prv_rows;
+               int butw = (data->bbox.xmax - data->bbox.xmin) / data->prv_cols;
+               int row, col;
+               
+               *rect= data->bbox;
+               
+               col = itemnr % data->prv_cols;
+               row = itemnr / data->prv_cols;
+               
+               rect->xmin += col * butw;
+               rect->xmax = rect->xmin + butw;
+               
+               rect->ymax = data->bbox.ymax - MENU_TOP - (row * buth);
+               rect->ymin = rect->ymax - buth;
+       }
+       /* list view */
+       else {
+               int buth= (data->bbox.ymax-data->bbox.ymin - 2*MENU_TOP)/SEARCH_ITEMS;
+               
+               *rect= data->bbox;
+               rect->xmin= data->bbox.xmin + 3.0f;
+               rect->xmax= data->bbox.xmax - 3.0f;
+               
+               rect->ymax= data->bbox.ymax - MENU_TOP - itemnr*buth;
+               rect->ymin= rect->ymax - buth;
+       }
        
 }
 
@@ -785,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;
        
@@ -793,44 +896,72 @@ 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) {
                rcti rect;
                int a;
+               
+               if (data->preview) {
+                       /* draw items */
+                       for(a=0; a<data->items.totitem; a++) {
+                               ui_searchbox_butrect(&rect, data, a);
                                
-               /* draw items */
-               for(a=0; a<data->items.totitem; a++) {
-                       ui_searchbox_butrect(&rect, data, a);
+                               /* widget itself */
+                               if (data->preview)
+                                       ui_draw_preview_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
+                               else 
+                                       ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
+                       }
                        
-                       /* widget itself */
-                       ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
+                       /* indicate more */
+                       if(data->items.more) {
+                               ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
+                               glEnable(GL_BLEND);
+                               UI_icon_draw(rect.xmax-18, rect.ymin-7, ICON_TRIA_DOWN);
+                               glDisable(GL_BLEND);
+                       }
+                       if(data->items.offset) {
+                               ui_searchbox_butrect(&rect, data, 0);
+                               glEnable(GL_BLEND);
+                               UI_icon_draw(rect.xmin, rect.ymax-9, ICON_TRIA_UP);
+                               glDisable(GL_BLEND);
+                       }
                        
-               }
-               /* indicate more */
-               if(data->items.more) {
-                       ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
-                       glEnable(GL_BLEND);
-                       UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymin-9, ICON_TRIA_DOWN);
-                       glDisable(GL_BLEND);
-               }
-               if(data->items.offset) {
-                       ui_searchbox_butrect(&rect, data, 0);
-                       glEnable(GL_BLEND);
-                       UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymax-7, ICON_TRIA_UP);
-                       glDisable(GL_BLEND);
+               } else {
+                       /* draw items */
+                       for(a=0; a<data->items.totitem; a++) {
+                               ui_searchbox_butrect(&rect, data, a);
+                               
+                               /* widget itself */
+                               ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
+                               
+                       }
+                       /* indicate more */
+                       if(data->items.more) {
+                               ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
+                               glEnable(GL_BLEND);
+                               UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymin-9, ICON_TRIA_DOWN);
+                               glDisable(GL_BLEND);
+                       }
+                       if(data->items.offset) {
+                               ui_searchbox_butrect(&rect, data, 0);
+                               glEnable(GL_BLEND);
+                               UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymax-7, ICON_TRIA_UP);
+                               glDisable(GL_BLEND);
+                       }
                }
        }
 }
 
-static void ui_searchbox_region_free(ARegion *ar)
+static void ui_searchbox_region_free_cb(ARegion *ar)
 {
        uiSearchboxData *data= ar->regiondata;
        int a;
 
        /* free search data */
-       for(a=0; a<SEARCH_ITEMS; a++)
+       for(a=0; a<data->items.maxitem; a++)
                MEM_freeN(data->items.names[a]);
        MEM_freeN(data->items.names);
        MEM_freeN(data->items.pointers);
@@ -840,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
@@ -856,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 */
@@ -876,8 +1005,13 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        if(but->block->flag & UI_BLOCK_LOOP)
                data->noback= 1;
        
-       /* compute position */
+       if (but->a1 > 0 && but->a2 > 0) {
+               data->preview = 1;
+               data->prv_rows = but->a1;
+               data->prv_cols = but->a2;
+       }
        
+       /* compute position */
        if(but->block->flag & UI_BLOCK_LOOP) {
                /* this case is search menu inside other menu */
                /* we copy region size */
@@ -891,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 {
@@ -943,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;
@@ -972,13 +1106,17 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        ED_region_tag_redraw(ar);
        
        /* prepare search data */
-       data->items.maxitem= SEARCH_ITEMS;
+       if (data->preview) {
+               data->items.maxitem= data->prv_rows * data->prv_cols;
+       } else {
+               data->items.maxitem= SEARCH_ITEMS;
+       }
        data->items.maxstrlen= but->hardmax;
        data->items.totitem= 0;
-       data->items.names= MEM_callocN(SEARCH_ITEMS*sizeof(void *), "search names");
-       data->items.pointers= MEM_callocN(SEARCH_ITEMS*sizeof(void *), "search pointers");
-       data->items.icons= MEM_callocN(SEARCH_ITEMS*sizeof(int), "search icons");
-       for(x1=0; x1<SEARCH_ITEMS; x1++)
+       data->items.names= MEM_callocN(data->items.maxitem*sizeof(void *), "search names");
+       data->items.pointers= MEM_callocN(data->items.maxitem*sizeof(void *), "search pointers");
+       data->items.icons= MEM_callocN(data->items.maxitem*sizeof(int), "search icons");
+       for(x1=0; x1<data->items.maxitem; x1++)
                data->items.names[x1]= MEM_callocN(but->hardmax+1, "search pointers");
        
        return ar;
@@ -989,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 **********************/
 
@@ -1047,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 */
@@ -1122,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)
                        }
                }
                
@@ -1137,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);
@@ -1162,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 */
                
@@ -1213,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);
@@ -1280,9 +1531,12 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
                saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
                saferct->safety= block->safety;
                BLI_addhead(&block->saferct, saferct);
-               block->flag |= UI_BLOCK_POPUP;
+               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 */
@@ -1290,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;
@@ -1302,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);
        
@@ -1322,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;
@@ -1362,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;
                }
@@ -1377,7 +1638,7 @@ static void ui_block_func_MENUSTR(bContext *C, uiLayout *layout, void *arg_str)
        }
 
        /* create items */
-       split= uiLayoutSplit(layout, 0);
+       split= uiLayoutSplit(layout, 0, 0);
 
        for(a=0, b=0; a<md->nitems; a++, b++) {
                if(block->flag & UI_BLOCK_NO_FLIP)
@@ -1409,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;
@@ -1423,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;
@@ -1457,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;
@@ -1476,68 +1737,65 @@ static void ui_warp_pointer(short x, short y)
 #define DPICK  6.0
 #define BPICK  24.0
 
-#define UI_PALETTE_TOT 16
-/* note; in tot+1 the old color is stored */
-static float palette[UI_PALETTE_TOT+1][3]= {
-{0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64}, 
-{0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18}, 
-{1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56}, 
-{0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
-};  
-
 /* for picker, while editing hsv */
 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);
 }
 
-static void update_picker_hex(uiBlock *block, float *rgb)
+/* also used by small picker, be careful with name checks below... */
+static void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
 {
        uiBut *bt;
-       char col[16];
+       float *hsv= ui_block_hsv_get(block);
        
-       sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
+       /* 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) {
-               if(strcmp(bt->str, "Hex: ")==0)
-                       strcpy(bt->poin, col);
-
-               ui_check_but(bt);
-       }
-}
-
-/* also used by small picker, be careful with name checks below... */
-void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
-{
-       uiBut *bt;
-       float r, g, b;
-       float rgb[3];
-       
-       // this updates button strings, is hackish... but button pointers are on stack of caller function
-       hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
-       
-       rgb[0] = r; rgb[1] = g; rgb[2] = b;
-       update_picker_hex(block, rgb);
+               if (bt->rnaprop) {
+                       
+                       ui_set_but_vectorf(bt, rgb);
+                       
+               }
+               else if(strcmp(bt->str, "Hex: ")==0) {
+                       float rgb_gamma[3];
+                       double intpart;
+                       char col[16];
+                       
+                       /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
+                       
+                       if (block->color_profile == BLI_PR_NONE) {
+                               copy_v3_v3(rgb_gamma, rgb);
+                       } else {
+                               /* make an sRGB version, for Hex code */
+                               linearrgb_to_srgb_v3_v3(rgb_gamma, rgb);
+                       }
+                       
+                       if (rgb_gamma[0] > 1.0f) rgb_gamma[0] = modf(rgb_gamma[0], &intpart);
+                       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);
 
-       for(bt= block->buttons.first; bt; bt= bt->next) {
-               if(ELEM(bt->type, HSVCUBE, HSVCIRCLE)) {
-                       VECCOPY(bt->hsv, hsv);
-                       ui_set_but_hsv(bt);
+                       sprintf(col, "%02X%02X%02X", FTOCHAR(rgb_gamma[0]), FTOCHAR(rgb_gamma[1]), FTOCHAR(rgb_gamma[2]));
+                       
+                       strcpy(bt->poin, col);
                }
                else if(bt->str[1]==' ') {
                        if(bt->str[0]=='R') {
-                               ui_set_but_val(bt, r);
+                               ui_set_but_val(bt, rgb[0]);
                        }
                        else if(bt->str[0]=='G') {
-                               ui_set_but_val(bt, g);
+                               ui_set_but_val(bt, rgb[1]);
                        }
                        else if(bt->str[0]=='B') {
-                               ui_set_but_val(bt, b);
+                               ui_set_but_val(bt, rgb[2]);
                        }
                        else if(bt->str[0]=='H') {
                                ui_set_but_val(bt, hsv[0]);
@@ -1554,315 +1812,68 @@ void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
        }
 }
 
-static void ui_update_block_buts_hex(uiBlock *block, char *hexcol)
-{
-       uiBut *bt;
-       float r=0, g=0, b=0;
-       float h, s, v;
-       
-       
-       // this updates button strings, is hackish... but button pointers are on stack of caller function
-       hex_to_rgb(hexcol, &r, &g, &b);
-       rgb_to_hsv(r, g, b, &h, &s, &v);
-
-       for(bt= block->buttons.first; bt; bt= bt->next) {
-               if(bt->type==HSVCUBE) {
-                       bt->hsv[0] = h;
-                       bt->hsv[1] = s;                 
-                       bt->hsv[2] = v;
-                       ui_set_but_hsv(bt);
-               }
-               else if(bt->str[1]==' ') {
-                       if(bt->str[0]=='R') {
-                               ui_set_but_val(bt, r);
-                       }
-                       else if(bt->str[0]=='G') {
-                               ui_set_but_val(bt, g);
-                       }
-                       else if(bt->str[0]=='B') {
-                               ui_set_but_val(bt, b);
-                       }
-                       else if(bt->str[0]=='H') {
-                               ui_set_but_val(bt, h);
-                       }
-                       else if(bt->str[0]=='S') {
-                               ui_set_but_val(bt, s);
-                       }
-                       else if(bt->str[0]=='V') {
-                               ui_set_but_val(bt, v);
-                       }
-               }
-
-               ui_check_but(bt);
-       }
-}
-
-/* bt1 is palette but, col1 is original color */
-/* callback to copy from/to palette */
-static void do_palette_cb(bContext *C, void *bt1, void *col1)
+static void do_picker_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
-       wmWindow *win= CTX_wm_window(C);
-       uiBut *but1= (uiBut *)bt1;
-       uiPopupBlockHandle *popup= but1->block->handle;
-       float *col= (float *)col1;
-       float *fp, hsv[3];
-       
-       fp= (float *)but1->poin;
+       uiBut *but= (uiBut *)bt1;
+       uiPopupBlockHandle *popup= but->block->handle;
+       PropertyRNA *prop = but->rnaprop;
+       PointerRNA ptr = but->rnapoin;
+       float rgb[4];
        
-       if(win->eventstate->ctrl) {
-               VECCOPY(fp, col);
-       }
-       else {
-               VECCOPY(col, fp);
+       if (prop) {
+               RNA_property_float_get_array(&ptr, prop, rgb);
+               ui_update_block_buts_rgb(but->block, rgb);
        }
        
-       rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
-       ui_update_block_buts_hsv(but1->block, hsv);
-       update_picker_hex(but1->block, col);
-
        if(popup)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
-static void do_hsv_cb(bContext *C, void *bt1, void *unused)
+static void do_hsv_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
-       uiBut *but1= (uiBut *)bt1;
-       uiPopupBlockHandle *popup= but1->block->handle;
-
-       if(popup)
-               popup->menuretval= UI_RETURN_UPDATE;
-}
-
-/* bt1 is num but, hsv1 is pointer to original color in hsv space*/
-/* callback to handle changes in num-buts in picker */
-static void do_palette1_cb(bContext *C, void *bt1, void *hsv1)
-{
-       uiBut *but1= (uiBut *)bt1;
-       uiPopupBlockHandle *popup= but1->block->handle;
-       float *hsv= (float *)hsv1;
-       float *fp= NULL;
+       uiBut *but= (uiBut *)bt1;
+       uiPopupBlockHandle *popup= but->block->handle;
+       float rgb[3];
+       float *hsv= ui_block_hsv_get(but->block);
        
-       if(but1->str[1]==' ') {
-               if(but1->str[0]=='R') fp= (float *)but1->poin;
-               else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
-               else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
-       }
-       if(fp) {
-               rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
-       } 
-       ui_update_block_buts_hsv(but1->block, hsv);
-
-       if(popup)
-               popup->menuretval= UI_RETURN_UPDATE;
-}
-
-/* bt1 is num but, col1 is pointer to original color */
-/* callback to handle changes in num-buts in picker */
-static void do_palette2_cb(bContext *C, void *bt1, void *col1)
-{
-       uiBut *but1= (uiBut *)bt1;
-       uiPopupBlockHandle *popup= but1->block->handle;
-       float *rgb= (float *)col1;
-       float *fp= NULL;
+       hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
        
-       if(but1->str[1]==' ') {
-               if(but1->str[0]=='H') fp= (float *)but1->poin;
-               else if(but1->str[0]=='S') fp= ((float *)but1->poin)-1;
-               else if(but1->str[0]=='V') fp= ((float *)but1->poin)-2;
-       }
-       if(fp) {
-               hsv_to_rgb(fp[0], fp[1], fp[2], rgb, rgb+1, rgb+2);
-       } 
-       ui_update_block_buts_hsv(but1->block, fp);
-
-       if(popup)
-               popup->menuretval= UI_RETURN_UPDATE;
-}
-
-static void do_palette_hex_cb(bContext *C, void *bt1, void *hexcl)
-{
-       uiBut *but1= (uiBut *)bt1;
-       uiPopupBlockHandle *popup= but1->block->handle;
-       char *hexcol= (char *)hexcl;
+       ui_update_block_buts_rgb(but->block, rgb);
        
-       ui_update_block_buts_hex(but1->block, hexcol);  
-
        if(popup)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
-/* used for both 3d view and image window */
-static void do_palette_sample_cb(bContext *C, void *bt1, void *col1)   /* frontbuf */
+static void do_hex_rna_cb(bContext *UNUSED(C), void *bt1, void *hexcl)
 {
-       /* XXX 2.50 this should become an operator? */
-#if 0
-       uiBut *but1= (uiBut *)bt1;
-       uiBut *but;
-       float tempcol[4];
-       int x=0, y=0;
-       short mval[2];
-       float hsv[3];
-       short capturing;
-       int oldcursor;
-       Window *win;
-       unsigned short dev;
-       
-       oldcursor=get_cursor();
-       win=winlay_get_active_window();
-       
-       while (get_mbut() & L_MOUSE) UI_wait_for_statechange();
-       
-       SetBlenderCursor(BC_EYEDROPPER_CURSOR);
-       
-       /* loop and wait for a mouse click */
-       capturing = TRUE;
-       while(capturing) {
-               char ascii;
-               short val;
-               
-               dev = extern_qread_ext(&val, &ascii);
-               
-               if(dev==INPUTCHANGE) break;
-               if(get_mbut() & R_MOUSE) break;
-               else if(get_mbut() & L_MOUSE) {
-                       uiGetMouse(mywinget(), mval);
-                       x= mval[0]; y= mval[1];
-                       
-                       capturing = FALSE;
-                       break;
-               }
-               else if(dev==ESCKEY) break;
-       }
-       window_set_cursor(win, oldcursor);
-       
-       if(capturing) return;
-       
-       if(x<0 || y<0) return;
-       
-       /* if we've got a glick, use OpenGL to sample the color under the mouse pointer */
-       glReadBuffer(GL_FRONT);
-       glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, tempcol);
-       glReadBuffer(GL_BACK);
-       
-       /* and send that color back to the picker */
-       rgb_to_hsv(tempcol[0], tempcol[1], tempcol[2], hsv, hsv+1, hsv+2);
-       ui_update_block_buts_hsv(but1->block, hsv);
-       update_picker_hex(but1->block, tempcol);
-       
-       for (but= but1->block->buttons.first; but; but= but->next) {
-               ui_check_but(but);
-               ui_draw_but(but);
-       }
-       
-       but= but1->block->buttons.first;
-       ui_block_flush_back(but->block);
-#endif
-}
-
-/* color picker, Gimp version. mode: 'f' = floating panel, 'p' =  popup */
-/* col = read/write to, hsv/old/hexcol = memory for temporal use */
-void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
-{
-       uiBut *bt;
-       float h, offs;
-       int a;
-
-       VECCOPY(old, col);      // old color stored there, for palette_cb to work
+       uiBut *but= (uiBut *)bt1;
+       uiPopupBlockHandle *popup= but->block->handle;
+       char *hexcol= (char *)hexcl;
+       float rgb[3];
        
-       // the cube intersection
-       bt= uiDefButF(block, HSVCUBE, retval, "",       0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
-       uiButSetFunc(bt, do_hsv_cb, bt, NULL);
-
-       bt= uiDefButF(block, HSVCUBE, retval, "",       0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
-       uiButSetFunc(bt, do_hsv_cb, bt, NULL);
-
-       // palette
+       hex_to_rgb(hexcol, rgb, rgb+1, rgb+2);
        
-       bt=uiDefButF(block, COL, retval, "",            FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
-       uiButSetFunc(bt, do_palette_cb, bt, col);
-       uiDefButF(block, COL, retval, "",               FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
-
-       h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
-       uiBlockBeginAlign(block);
-       for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
-               bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK, 65.0+(float)a*h, BPICK/2, h, palette[a+UI_PALETTE_TOT/2], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");
-               uiButSetFunc(bt, do_palette_cb, bt, col);
-               bt= uiDefButF(block, COL, retval, "",   FPICK+DPICK+BPICK/2, 65.0+(float)a*h, BPICK/2, h, palette[a], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");               
-               uiButSetFunc(bt, do_palette_cb, bt, col);
+       /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
+       if (but->block->color_profile != BLI_PR_NONE) {
+               /* so we need to linearise it for Blender */
+               srgb_to_linearrgb_v3_v3(rgb, rgb);
        }
-       uiBlockEndAlign(block);
        
-       // buttons
-       rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
-       sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
-
-       offs= FPICK+2*DPICK+BPICK;
-
-       /* note; made this a TOG now, with NULL pointer. Is because BUT now gets handled with a afterfunc */
-       bt= uiDefIconTextBut(block, TOG, UI_RETURN_OK, ICON_EYEDROPPER, "Sample", offs+55, 170, 85, 20, NULL, 0, 0, 0, 0, "Sample the color underneath the following mouse click (ESC or RMB to cancel)");
-       uiButSetFunc(bt, do_palette_sample_cb, bt, col);
-       uiButSetFlag(bt, UI_TEXT_LEFT);
-       
-       bt= uiDefBut(block, TEX, retval, "Hex: ", offs, 140, 140, 20, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
-       uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
-
-       uiBlockBeginAlign(block);
-       bt= uiDefButF(block, NUMSLI, retval, "R ",      offs, 110, 140,20, col, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette1_cb, bt, hsv);
-       bt= uiDefButF(block, NUMSLI, retval, "G ",      offs, 90, 140,20, col+1, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette1_cb, bt, hsv);
-       bt= uiDefButF(block, NUMSLI, retval, "B ",      offs, 70, 140,20, col+2, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette1_cb, bt, hsv);
-       
-       uiBlockBeginAlign(block);
-       bt= uiDefButF(block, NUMSLI, retval, "H ",      offs, 40, 140,20, hsv, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette2_cb, bt, col);
-       bt= uiDefButF(block, NUMSLI, retval, "S ",      offs, 20, 140,20, hsv+1, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette2_cb, bt, col);
-       bt= uiDefButF(block, NUMSLI, retval, "V ",      offs, 0, 140,20, hsv+2, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette2_cb, bt, col);
-       uiBlockEndAlign(block);
-}
-
-/* bt1 is num but, hsv1 is pointer to original color in hsv space*/
-/* callback to handle changes */
-static void do_picker_small_cb(bContext *C, void *bt1, void *hsv1)
-{
-       uiBut *but1= (uiBut *)bt1;
-       uiPopupBlockHandle *popup= but1->block->handle;
-       float *hsv= (float *)hsv1;
-       float *fp= NULL;
-       
-       fp= (float *)but1->poin;
-       rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
-
-       ui_update_block_buts_hsv(but1->block, hsv);
+       ui_update_block_buts_rgb(but->block, rgb);
        
        if(popup)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
-/* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
-#define SPICK1 150.0
-#define DPICK1 6.0
-
-/* only the color, a HS circle and V slider */
-static void uiBlockPickerSmall(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
+static void close_popup_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
 {
-       uiBut *bt;
-       
-       VECCOPY(old, col);      // old color stored there, for palette_cb to work
+       uiBut *but= (uiBut *)bt1;
+       uiPopupBlockHandle *popup= but->block->handle;
        
-       /* HS circle */
-       bt= uiDefButF(block, HSVCIRCLE, retval, "",     0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
-
-       /* value */
-       bt= uiDefButF(block, HSVCUBE, retval, "",       SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
-       uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
+       if(popup)
+               popup->menuretval= UI_RETURN_OK;
 }
 
-
 static void picker_new_hide_reveal(uiBlock *block, short colormode)
 {
        uiBut *bt;
@@ -1870,6 +1881,13 @@ static void picker_new_hide_reveal(uiBlock *block, short colormode)
        /* tag buttons */
        for(bt= block->buttons.first; bt; bt= bt->next) {
                
+               if (bt->type == LABEL) {
+                       if( bt->str[1]=='G') {
+                               if(colormode==2) bt->flag &= ~UI_HIDDEN;
+                               else bt->flag |= UI_HIDDEN;
+                       }
+               }
+               
                if(bt->type==NUMSLI || bt->type==TEX) {
                        if( bt->str[1]=='e') {
                                if(colormode==2) bt->flag &= ~UI_HIDDEN;
@@ -1887,81 +1905,158 @@ 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);
-
        picker_new_hide_reveal(bt->block, colormode);
 }
 
+/* picker sizes S hsize, F full size, D spacer, B button/pallette height  */
+#define SPICK1 150.0
+#define DPICK1 6.0
+
+#define PICKER_H       150
+#define PICKER_W       150
+#define PICKER_SPACE   6
+#define PICKER_BAR             14
+
+#define PICKER_TOTAL_W (PICKER_W+PICKER_SPACE+PICKER_BAR)
+
+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, 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");
+       uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
+}
+
+
+static void square_picker(uiBlock *block, PointerRNA *ptr, const char *propname, 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");
+       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");
+       uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
+}
+
 
 /* a HS circle, V slider, rgb/hsv/hex sliders */
-static void uiBlockPickerNew(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
+static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyRNA *prop)
 {
        static short colormode= 0;      /* temp? 0=rgb, 1=hsv, 2=hex */
        uiBut *bt;
-       int width;
+       int width, butwidth;
        static char tip[50];
+       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);
        
-       VECCOPY(old, col);      // old color stored there, for palette_cb to work
+       ui_block_hsv_get(block);
        
-       /* existence of profile means storage is in linear colour space, with display correction */
-       if (block->color_profile == BLI_PR_NONE)
+       width= PICKER_TOTAL_W;
+       butwidth = width - UI_UNIT_X - 10;
+       
+       /* 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");
-       else
+               copy_v3_v3(rgb_gamma, rgb);
+       } else {
                sprintf(tip, "Value in Linear RGB Color Space");
+               /* make an sRGB version, for Hex code */
+               linearrgb_to_srgb_v3_v3(rgb_gamma, rgb);
+       }
        
-       /* HS circle */
-       bt= uiDefButF(block, HSVCIRCLE, retval, "",     0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
-       
-       /* value */
-       bt= uiDefButF(block, HSVCUBE, retval, "",       SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
-       uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
+       /* 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);
+
+       switch (U.color_picker_type) {
+               case USER_CP_CIRCLE:
+                       circle_picker(block, ptr, propname);
+                       break;
+               case USER_CP_SQUARE_SV:
+                       square_picker(block, ptr, propname, UI_GRAD_SV);
+                       break;
+               case USER_CP_SQUARE_HS:
+                       square_picker(block, ptr, propname, UI_GRAD_HS);
+                       break;
+               case USER_CP_SQUARE_HV:
+                       square_picker(block, ptr, propname, UI_GRAD_HV);
+                       break;
+       }
        
        /* mode */
-       width= (SPICK1+DPICK1+14)/3;
        uiBlockBeginAlign(block);
-       bt= uiDefButS(block, ROW, retval, "RGB",        0, -30, width, 19, &colormode, 0.0, 0.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_new_mode_cb, bt, col);
-       bt= uiDefButS(block, ROW, retval, "HSV",        width, -30, width, 19, &colormode, 0.0, 1.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_new_mode_cb, bt, hsv);
-       bt= uiDefButS(block, ROW, retval, "Hex",        2*width, -30, width, 19, &colormode, 0.0, 2.0, 0, 0, "");
-       uiButSetFunc(bt, do_picker_new_mode_cb, bt, hexcol);
+       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, 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, 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, NULL);
        uiBlockEndAlign(block);
-       
-       /* sliders or hex */
-       width= (SPICK1+DPICK1+14);
-       rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
-       sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));      
 
+       bt= uiDefIconButO(block, BUT, "UI_OT_eyedropper", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, butwidth+10, -60, UI_UNIT_X, UI_UNIT_Y, NULL);
+       uiButSetFunc(bt, close_popup_cb, bt, NULL);
+       
+       /* RGB values */
        uiBlockBeginAlign(block);
-       bt= uiDefButF(block, NUMSLI, 0, "R ",   0, -60, width, 19, col, 0.0, 1.0, 10, 3, tip);
-       uiButSetFunc(bt, do_palette1_cb, bt, hsv);
-       bt= uiDefButF(block, NUMSLI, 0, "G ",   0, -80, width, 19, col+1, 0.0, 1.0, 10, 3, tip);
-       uiButSetFunc(bt, do_palette1_cb, bt, hsv);
-       bt= uiDefButF(block, NUMSLI, 0, "B ",   0, -100, width, 19, col+2, 0.0, 1.0, 10, 3, tip);
-       uiButSetFunc(bt, do_palette1_cb, bt, hsv);
-       uiBlockEndAlign(block);
+       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, 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");
+       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_NONE);
+       // but need to use uiButSetFunc for updating other fake buttons
+       
+       /* HSV values */
        uiBlockBeginAlign(block);
-       bt= uiDefButF(block, NUMSLI, 0, "H ",   0, -60, width, 19, hsv, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette2_cb, bt, col);
-       bt= uiDefButF(block, NUMSLI, 0, "S ",   0, -80, width, 19, hsv+1, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette2_cb, bt, col);
-       bt= uiDefButF(block, NUMSLI, 0, "V ",   0, -100, width, 19, hsv+2, 0.0, 1.0, 10, 3, "");
-       uiButSetFunc(bt, do_palette2_cb, bt, col);
+       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, "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, max, 10, 3, "Value");
+       uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
        uiBlockEndAlign(block);
 
-       bt= uiDefBut(block, TEX, 0, "Hex: ", 0, -80, width, 19, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
-       uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
+       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;
        
@@ -1977,16 +2072,17 @@ 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);
                                
-                               ui_update_block_buts_hsv(block, but->hsv);
+                               ui_update_block_buts_rgb(block, col);
                                if(popup)
                                        popup->menuretval= UI_RETURN_UPDATE;
                                
@@ -1999,15 +2095,11 @@ static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *eve
 
 uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
 {
-       wmWindow *win= CTX_wm_window(C); // XXX temp, needs to become keymap to detect type?
        uiBut *but= arg_but;
        uiBlock *block;
-       static float hsvcol[3], oldcol[3];
-       static char hexcol[128];
        
        block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS);
        
-       /* XXX ideally the colour picker buttons would reference the rna property itself */
        if (but->rnaprop) {
                if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA) {
                        block->color_profile = BLI_PR_NONE;
@@ -2017,26 +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);
-       if(win->eventstate->shift) {
-               uiBlockPickerButtons(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
-               block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
-               uiBoundsBlock(block, 3);
-       }
-       else if(win->eventstate->alt) {
-               uiBlockPickerSmall(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
-               block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_OUT_1;
-               uiBoundsBlock(block, 10);
-               
-               block->block_event_func= ui_picker_small_wheel;
-       }
-       else {
-               uiBlockPickerNew(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
-               block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
-               uiBoundsBlock(block, 10);
-               
-               block->block_event_func= ui_picker_small_wheel;
-       }
        
+       uiBlockPicker(block, handle->retvec, &but->rnapoin, but->rnaprop);
+       
+       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_cb;
        
        /* and lets go */
        block->direction= UI_TOP;
@@ -2044,92 +2123,21 @@ uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_bu
        return block;
 }
 
-/* ******************** Color band *************** */
-
-static int vergcband(const void *a1, const void *a2)
-{
-       const CBData *x1=a1, *x2=a2;
-       
-       if( x1->pos > x2->pos ) return 1;
-       else if( x1->pos < x2->pos) return -1;
-       return 0;
-}
-
-static void colorband_pos_cb(bContext *C, void *coba_v, void *unused_v)
-{
-       ColorBand *coba= coba_v;
-       int a;
-       
-       if(coba->tot<2) return;
-       
-       for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
-       qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
-       for(a=0; a<coba->tot; a++) {
-               if(coba->data[a].cur==coba->cur) {
-                       /* if(coba->cur!=a) addqueue(curarea->win, REDRAW, 0); */       /* button cur */
-                       coba->cur= a;
-                       break;
-               }
-       }
-}
+/************************ Popup Menu Memory ****************************/
 
-static void colorband_add_cb(bContext *C, void *coba_v, void *unused_v)
+static int ui_popup_string_hash(char *str)
 {
-       ColorBand *coba= coba_v;
-       
-       if(coba->tot < MAXCOLORBAND-1) coba->tot++;
-       coba->cur= coba->tot-1;
-       
-       colorband_pos_cb(C, coba, NULL);
-//     BIF_undo_push("Add colorband");
-       
-}
+       /* sometimes button contains hotkey, sometimes not, strip for proper compare */
+       int hash;
+       char *delimit= strchr(str, '|');
 
-static void colorband_del_cb(bContext *C, void *coba_v, void *unused_v)
-{
-       ColorBand *coba= coba_v;
-       int a;
-       
-       if(coba->tot<2) return;
-       
-       for(a=coba->cur; a<coba->tot; a++) {
-               coba->data[a]= coba->data[a+1];
-       }
-       if(coba->cur) coba->cur--;
-       coba->tot--;
-       
-//     BIF_undo_push("Delete colorband");
-//     BIF_preview_changed(ID_TE);
-}
+       if(delimit) *delimit= 0;
+       hash= BLI_ghashutil_strhash(str);
+       if(delimit) *delimit= '|';
 
-void uiBlockColorbandButtons(uiBlock *block, ColorBand *coba, rctf *butr, int event)
-{
-       CBData *cbd;
-       uiBut *bt;
-       float unit= (butr->xmax-butr->xmin)/14.0f;
-       float xs= butr->xmin;
-       
-       cbd= coba->data + coba->cur;
-       
-       uiBlockBeginAlign(block);
-       uiDefButF(block, COL, event,            "",                     xs,butr->ymin+20.0f,2.0f*unit,20,                               &(cbd->r), 0, 0, 0, 0, "");
-       uiDefButF(block, NUM, event,            "A:",           xs+2.0f*unit,butr->ymin+20.0f,4.0f*unit,20,     &(cbd->a), 0.0f, 1.0f, 10, 2, "");
-       bt= uiDefBut(block, BUT, event, "Add",          xs+6.0f*unit,butr->ymin+20.0f,2.0f*unit,20,     NULL, 0, 0, 0, 0, "Adds a new color position to the colorband");
-       uiButSetFunc(bt, colorband_add_cb, coba, NULL);
-       bt= uiDefBut(block, BUT, event, "Del",          xs+8.0f*unit, butr->ymin+20.0f, 2.0f*unit, 20,  NULL, 0, 0, 0, 0, "Deletes the active position");
-       uiButSetFunc(bt, colorband_del_cb, coba, NULL);
-       
-       uiDefButS(block, MENU, event,           "Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4",
-                         xs + 10.0f*unit, butr->ymin+20.0f, unit*4.0f, 20,             &coba->ipotype, 0.0, 0.0, 0, 0, "Sets interpolation type");
-       
-       uiDefBut(block, BUT_COLORBAND, event, "",               xs, butr->ymin, butr->xmax-butr->xmin, 20.0f, coba, 0, 0, 0, 0, "");
-       uiBlockEndAlign(block);
-       
+       return hash;
 }
 
-
-/************************ Popup Menu Memory ****************************/
-
 static int ui_popup_menu_hash(char *str)
 {
        return BLI_ghashutil_strhash(str);
@@ -2138,7 +2146,7 @@ static int ui_popup_menu_hash(char *str)
 /* but == NULL read, otherwise set */
 uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
 {
-       static char mem[256], first=1;
+       static int mem[256], first=1;
        int hash= block->puphash;
        
        if(first) {
@@ -2149,12 +2157,16 @@ uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
 
        if(but) {
                /* set */
-               mem[hash & 255 ]= BLI_findindex(&block->buttons, but);
+               mem[hash & 255 ]= ui_popup_string_hash(but->str);
                return NULL;
        }
        else {
                /* get */
-               return BLI_findlink(&block->buttons, mem[hash & 255]);
+               for(but=block->buttons.first; but; but=but->next)
+                       if(ui_popup_string_hash(but->str) == mem[hash & 255])
+                               return but;
+
+               return NULL;
        }
 }
 
@@ -2179,7 +2191,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
        ScrArea *sa;
        ARegion *ar;
        uiPopupMenu *pup= arg_pup;
-       int offset, direction, minwidth, flip;
+       int offset[2], direction, minwidth, width, height, flip;
 
        if(pup->menu_func) {
                pup->block->handle= handle;
@@ -2215,7 +2227,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
 
        block->direction= direction;
 
-       uiBlockLayoutResolve(block, NULL, NULL);
+       uiBlockLayoutResolve(block, &width, &height);
 
        uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
        
@@ -2224,17 +2236,27 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
                uiBlockSetDirection(block, direction);
 
                /* offset the mouse position, possibly based on earlier selection */
-               offset= 1.5*MENU_BUTTON_HEIGHT;
-
-               if(block->flag & UI_BLOCK_POPUP_MEMORY) {
-                       bt= ui_popup_menu_memory(block, NULL);
+               if((block->flag & UI_BLOCK_POPUP_MEMORY) &&
+                       (bt= ui_popup_menu_memory(block, NULL))) {
+                       /* position mouse on last clicked item, at 0.8*width of the
+                          button, so it doesn't overlap the text too much, also note
+                          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*UI_UNIT_Y);
+               }
+               else {
+                       /* position mouse at 0.8*width of the button and below the tile
+                          on the first item */
+                       offset[0]= 0;
+                       for(bt=block->buttons.first; bt; bt=bt->next)
+                               offset[0]= MIN2(offset[0], -(bt->x1 + 0.8f*(bt->x2 - bt->x1)));
 
-                       if(bt)
-                               offset= -bt->y1 - 0.5f*MENU_BUTTON_HEIGHT;
+                       offset[1]= 1.5*UI_UNIT_Y;
                }
 
                block->minbounds= minwidth;
-               uiMenuPopupBoundsBlock(block, 1, 20, offset);
+               uiMenuPopupBoundsBlock(block, 1, offset[0], offset[1]);
        }
        else {
                /* for a header menu we set the direction automatic */
@@ -2282,6 +2304,7 @@ 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) {
@@ -2333,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;
                }
        }
@@ -2393,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);
 
@@ -2423,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];
@@ -2435,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);
 
@@ -2453,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;
 
@@ -2462,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];
@@ -2491,16 +2516,19 @@ void uiPupMenuReports(bContext *C, ReportList *reports)
        ds= BLI_dynstr_new();
 
        for(report=reports->list.first; report; report=report->next) {
-               if(report->type >= RPT_ERROR)
+               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);
                else if(report->type >= RPT_WARNING)
                        BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
                else if(report->type >= RPT_INFO)
-                       BLI_dynstr_appendf(ds, "Info %%t|%s", report->message);
+                       BLI_dynstr_appendf(ds, "Info %%i%d%%t|%s", ICON_INFO, report->message);
        }
 
        str= BLI_dynstr_get_cstring(ds);
-       ui_popup_menu_create(C, NULL, NULL, NULL, NULL, str);
+       if(str[0] != '\0')
+               ui_popup_menu_create(C, NULL, NULL, NULL, NULL, str);
        MEM_freeN(str);
 
        BLI_dynstr_free(ds);
@@ -2521,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;
@@ -2535,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;
@@ -2554,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);
@@ -2571,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)
 {
@@ -2580,3 +2628,7 @@ void uiPupBlockClose(bContext *C, uiBlock *block)
        }
 }
 
+float *ui_block_hsv_get(uiBlock *block)
+{
+       return block->_hsv;
+}