style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / editors / interface / interface_regions.c
index c3cbb9dbe5f533f6daf16acf9095bdd4ade588ed..ef6cbc9ae4850bdb644a0203fa330fc4ab2160f0 100644 (file)
@@ -138,8 +138,9 @@ static void menudata_add_item(MenuData *md, const char *str, int retval, int ico
 static void menudata_free(MenuData *md)
 {
        MEM_freeN((void *)md->instr);
-       if (md->items)
+       if (md->items) {
                MEM_freeN(md->items);
+       }
        MEM_freeN(md);
 }
 
@@ -153,8 +154,8 @@ static void menudata_free(MenuData *md)
         * indicates a seperator, sss%l indicates a label and
         * new column.
         * 
-        * @param str String to be parsed.
-        * @retval new menudata structure, free with menudata_free()
+        * \param str String to be parsed.
+        * \retval new menudata structure, free with menudata_free()
         */
 static MenuData *decompose_menu_string(const char *str)
 {
@@ -173,32 +174,36 @@ static MenuData *decompose_menu_string(const char *str)
 
                                *s= '\0';
                                s++;
-                       } else if (s[1]=='t') {
+                       }
+                       else if (s[1]=='t') {
                                nitem_is_title= (s != instr); /* check for empty title */
 
                                *s= '\0';
                                s++;
-                       } else if (s[1]=='l') {
+                       }
+                       else if (s[1]=='l') {
                                nitem_is_sepr= 1;
-                               if(!nitem) nitem= "";
+                               if (!nitem) nitem= "";
 
                                *s= '\0';
                                s++;
-                       } else if (s[1]=='i') {
+                       }
+                       else if (s[1]=='i') {
                                nicon= atoi(s+2);
                                
                                *s= '\0';
                                s++;
                        }
-               } else if (c=='|' || c == '\n' || c=='\0') {
+               }
+               else if (c=='|' || c == '\n' || c=='\0') {
                        if (nitem) {
                                *s= '\0';
 
-                               if(nitem_is_title) {
+                               if (nitem_is_title) {
                                        menudata_set_title(md, nitem, nicon);
                                        nitem_is_title= 0;
                                }
-                               else if(nitem_is_sepr) {
+                               else if (nitem_is_sepr) {
                                        /* prevent separator to get a value */
                                        menudata_add_item(md, nitem, -1, nicon, 1);
                                        nretval= md->nitems+1;
@@ -216,7 +221,8 @@ static MenuData *decompose_menu_string(const char *str)
                        if (c=='\0') {
                                break;
                        }
-               } else if (!nitem) {
+               }
+               else if (!nitem) {
                        nitem= s;
                }
 
@@ -253,20 +259,20 @@ int ui_step_name_menu(uiBut *but, int step)
                if (md->items[i].retval==value)
                        break;
        
-       if(step==1) {
+       if (step==1) {
                /* skip separators */
-               for(; i<md->nitems-1; i++) {
-                       if(md->items[i+1].retval != -1) {
+               for (; i<md->nitems-1; i++) {
+                       if (md->items[i+1].retval != -1) {
                                value= md->items[i+1].retval;
                                break;
                        }
                }
        }
        else {
-               if(i>0) {
+               if (i>0) {
                        /* skip separators */
-                       for(; i>0; i--) {
-                               if(md->items[i-1].retval != -1) {
+                       for (; i>0; i--) {
+                               if (md->items[i-1].retval != -1) {
                                        value= md->items[i-1].retval;
                                        break;
                                }
@@ -297,7 +303,7 @@ static ARegion *ui_add_temporary_region(bScreen *sc)
 
 static void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
 {
-       if(CTX_wm_window(C))
+       if (CTX_wm_window(C))
                wm_draw_region_clear(CTX_wm_window(C), ar);
 
        ED_region_exit(C, ar);
@@ -307,34 +313,90 @@ static void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
 
 /************************* Creating Tooltips **********************/
 
-#define MAX_TOOLTIP_LINES 8
+typedef enum {
+       UI_TIP_LC_MAIN,
+       UI_TIP_LC_NORMAL,
+       UI_TIP_LC_PYTHON,
+       UI_TIP_LC_ALERT,
+       UI_TIP_LC_SUBMENU
+} uiTooltipLineColor;
+#define UI_TIP_LC_MAX 5
 
+#define MAX_TOOLTIP_LINES 8
 typedef struct uiTooltipData {
        rcti bbox;
        uiFontStyle fstyle;
        char lines[MAX_TOOLTIP_LINES][512];
-       unsigned int color[MAX_TOOLTIP_LINES];
+       uiTooltipLineColor color_id[MAX_TOOLTIP_LINES];
        int totline;
        int toth, spaceh, lineh;
 } uiTooltipData;
 
+static void rgb_tint(float col[3],
+                     float h, float h_strength,
+                     float v, float v_strength)
+{
+       float col_hsv_from[3];
+       float col_hsv_to[3];
+
+       rgb_to_hsv(col[0], col[1], col[2],    col_hsv_from + 0, col_hsv_from + 1, col_hsv_from + 2);
+
+       col_hsv_to[0] = h;
+       col_hsv_to[1] = h_strength;
+       col_hsv_to[2] = (col_hsv_from[2] * (1.0f - v_strength)) + (v * v_strength);
+
+       hsv_to_rgb(col_hsv_to[0], col_hsv_to[1], col_hsv_to[2], col + 0, col + 1, col + 2);
+}
+
 static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
 {
        uiTooltipData *data= ar->regiondata;
+       uiWidgetColors* theme = ui_tooltip_get_theme();
        rcti bbox= data->bbox;
-       int a;
-       
-       ui_draw_menu_back(UI_GetStyle(), NULL, &data->bbox);
+       float tip_colors[UI_TIP_LC_MAX][3];
+
+       float *main_color    = tip_colors[UI_TIP_LC_MAIN]; /* the color from the theme */
+       float *normal_color  = tip_colors[UI_TIP_LC_NORMAL];
+       float *python_color  = tip_colors[UI_TIP_LC_PYTHON];
+       float *alert_color   = tip_colors[UI_TIP_LC_ALERT];
+       float *submenu_color = tip_colors[UI_TIP_LC_SUBMENU];
+
+       float background_color[3];
+       float tone_bg;
+       int i;
+
+       /* draw background */
+       ui_draw_tooltip_background(UI_GetStyle(), NULL, &bbox);
+
+       /* set background_color */
+       rgb_uchar_to_float(background_color, (const unsigned char *)theme->inner);
+
+       /* calculate normal_color */
+       rgb_uchar_to_float(main_color, (const unsigned char *)theme->text);
+       copy_v3_v3(normal_color, main_color);
+       copy_v3_v3(python_color, main_color);
+       copy_v3_v3(alert_color, main_color);
+       copy_v3_v3(submenu_color, main_color);
+
+       /* find the brightness difference between background and text colors */
        
+       tone_bg = rgb_to_grayscale(background_color);
+       /* tone_fg = rgb_to_grayscale(main_color); */
+
+       rgb_tint(normal_color, 0.0f, 0.0f, tone_bg, 0.3f);   /* a shade darker (to bg) */
+       rgb_tint(python_color, 0.666f, 0.25f, tone_bg, 0.3f); /* blue */
+       rgb_tint(alert_color, 0.0f, 0.8f, tone_bg, 0.1f);    /* bright red */
+       rgb_tint(submenu_color, 0.0f, 0.0f, tone_bg, 0.3f);  /* a shade darker (to bg) */
+
        /* draw text */
        uiStyleFontSet(&data->fstyle);
 
-       bbox.ymax= bbox.ymax - 0.5f*((bbox.ymax - bbox.ymin) - data->toth);
-       bbox.ymin= bbox.ymax - data->lineh;
+       bbox.ymax = bbox.ymax - 0.5f*((bbox.ymax - bbox.ymin) - data->toth);
+       bbox.ymin = bbox.ymax - data->lineh;
 
-       for(a=0; a<data->totline; a++) {
-               cpack(data->color[a]);
-               uiStyleFontDraw(&data->fstyle, &bbox, data->lines[a]);
+       for (i = 0; i < data->totline; i++) {
+               glColor3fv(tip_colors[data->color_id[i]]);
+               uiStyleFontDraw(&data->fstyle, &bbox, data->lines[i]);
                bbox.ymin -= data->lineh + data->spaceh;
                bbox.ymax -= data->lineh + data->spaceh;
        }
@@ -361,90 +423,91 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
        float x1f, x2f, y1f, y2f;
        int x1, x2, y1, y2, winx, winy, ofsx, ofsy, w, h, a;
 
-       if(but->flag & UI_BUT_NO_TOOLTIP)
+       if (but->flag & UI_BUT_NO_TOOLTIP)
                return NULL;
 
        /* create tooltip data */
        data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
 
        /* special case, enum rna buttons only have enum item description, use general enum description too before the specific one */
-       if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
+       if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
                const char *descr= RNA_property_description(but->rnaprop);
-               if(descr && descr[0]) {
+               if (descr && descr[0]) {
                        BLI_strncpy(data->lines[data->totline], descr, sizeof(data->lines[0]));
-                       data->color[data->totline]= 0xFFFFFF;
+                       data->color_id[data->totline] = UI_TIP_LC_MAIN;
                        data->totline++;
                }
 
-               if(ELEM(but->type, ROW, MENU)) {
+               if (ELEM(but->type, ROW, MENU)) {
                        EnumPropertyItem *item;
                        int i, totitem, free;
                        int value = (but->type == ROW)? but->hardmax: ui_get_but_val(but);
 
                        RNA_property_enum_items_gettexted(C, &but->rnapoin, but->rnaprop, &item, &totitem, &free);
 
-                       for(i=0; i<totitem; i++) {
-                               if(item[i].identifier[0] && item[i].value == value) {
-                                       if(item[i].description && item[i].description[0]) {
+                       for (i=0; i<totitem; i++) {
+                               if (item[i].identifier[0] && item[i].value == value) {
+                                       if (item[i].description && 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]= 0xDDDDDD;
+                        data->color_id[data->totline] = UI_TIP_LC_SUBMENU;
                                                data->totline++;
                                        }
                                        break;
                                }
                        }
 
-                       if(free)
+                       if (free) {
                                MEM_freeN(item);
+                       }
                }
        }
        
-       if(but->tip && but->tip[0] != '\0') {
+       if (but->tip && but->tip[0] != '\0') {
                BLI_strncpy(data->lines[data->totline], but->tip, sizeof(data->lines[0]));
-               data->color[data->totline]= 0xFFFFFF;
+               data->color_id[data->totline] = UI_TIP_LC_MAIN;
                data->totline++;
        }
 
-       if(but->optype && !(but->block->flag & UI_BLOCK_LOOP)) {
+       if (but->optype && !(but->block->flag & UI_BLOCK_LOOP)) {
                /* operator keymap (not menus, they already have it) */
                prop= (but->opptr)? but->opptr->data: NULL;
 
-               if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
+               if (WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
                                                buf, sizeof(buf)))
                {
                        BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), buf);
-                       data->color[data->totline]= 0x888888;
+                       data->color_id[data->totline] = UI_TIP_LC_NORMAL;
                        data->totline++;
                }
        }
 
-       if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
+       if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
                /* full string */
                ui_get_but_string(but, buf, sizeof(buf));
-               if(buf[0]) {
+               if (buf[0]) {
                        BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Value: %s"), buf);
-                       data->color[data->totline]= 0x888888;
+                       data->color_id[data->totline] = UI_TIP_LC_NORMAL;
                        data->totline++;
                }
        }
 
-       if(but->rnaprop) {
+       if (but->rnaprop) {
                int unit_type= uiButGetUnitType(but);
                
                if (unit_type == PROP_UNIT_ROTATION) {
                        if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
                                float value= RNA_property_array_check(but->rnaprop) ? RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) : RNA_property_float_get(&but->rnapoin, but->rnaprop);
                                BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Radians: %f"), value);
-                               data->color[data->totline]= 0x888888;
+                               data->color_id[data->totline] = UI_TIP_LC_NORMAL;
                                data->totline++;
                        }
                }
                
-               if(but->flag & UI_BUT_DRIVEN) {
-                       if(ui_but_anim_expression_get(but, buf, sizeof(buf))) {
+               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]), TIP_("Expression: %s"), buf);
-                               data->color[data->totline]= 0x888888;
+                               data->color_id[data->totline] = UI_TIP_LC_NORMAL;
                                data->totline++;
                        }
                }
@@ -452,15 +515,15 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                /* rna info */
                if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
                        BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s.%s"), RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
-                       data->color[data->totline]= 0x888888;
+                       data->color_id[data->totline] = UI_TIP_LC_PYTHON;
                        data->totline++;
                }
                
-               if(but->rnapoin.id.data) {
+               if (but->rnapoin.id.data) {
                        ID *id= but->rnapoin.id.data;
-                       if(id->lib && id->lib->name) {
+                       if (id->lib && id->lib->name) {
                                BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Library: %s"), id->lib->name);
-                               data->color[data->totline]= 0x888888;
+                               data->color_id[data->totline] = UI_TIP_LC_NORMAL;
                                data->totline++;
                        }
                }
@@ -475,21 +538,21 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                /* operator info */
                if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
                        BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), str);
-                       data->color[data->totline]= 0x888888;
+                       data->color_id[data->totline] = UI_TIP_LC_PYTHON;
                        data->totline++;
                }
 
                MEM_freeN(str);
 
                /* second check if we are disabled - why */
-               if(but->flag & UI_BUT_DISABLED) {
+               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) {
+                       if (poll_msg) {
                                BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Disabled: %s"), poll_msg);
-                               data->color[data->totline]= 0x6666ff; /* alert */
+                               data->color_id[data->totline] = UI_TIP_LC_ALERT; /* alert */
                                data->totline++;                        
                        }
                }
@@ -499,7 +562,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
                        MenuType *mt= uiButGetMenuType(but);
                        if (mt) {
                                BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), mt->idname);
-                               data->color[data->totline]= 0x888888;
+                               data->color_id[data->totline] = UI_TIP_LC_PYTHON;
                                data->totline++;
                        }
                }
@@ -508,7 +571,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
        assert(data->totline < MAX_TOOLTIP_LINES);
        
-       if(data->totline == 0) {
+       if (data->totline == 0) {
                MEM_freeN(data);
                return NULL;
        }
@@ -533,7 +596,7 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
        h= BLF_height_max(data->fstyle.uifont_id);
 
-       for(a=0, fontw=0, fonth=0; a<data->totline; a++) {
+       for (a=0, fontw=0, fonth=0; a<data->totline; a++) {
                w= BLF_width(data->fstyle.uifont_id, data->lines[a]);
                fontw= MAX2(fontw, w);
                fonth += (a == 0)? h: h+TIP_MARGIN_Y;
@@ -564,9 +627,9 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
        /* copy to int, gets projected if possible too */
        x1= x1f; y1= y1f; x2= x2f; y2= y2f; 
        
-       if(butregion) {
+       if (butregion) {
                /* XXX temp, region v2ds can be empty still */
-               if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
+               if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
                        UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
                        UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
                }
@@ -579,9 +642,9 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
 
        wm_window_get_size(CTX_wm_window(C), &winx, &winy);
 
-       if(x2 > winx) {
+       if (x2 > winx) {
                /* super size */
-               if(x2 > winx + x1) {
+               if (x2 > winx + x1) {
                        x2= winx;
                        x1= 0;
                }
@@ -592,22 +655,22 @@ ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
        }
        /* ensure at least 5 px above screen bounds
         * 25 is just a guess to be above the menu item */
-       if(y1 < 5) {
+       if (y1 < 5) {
                y2 += (-y1) + 30;
                y1 = 30;
        }
 
        /* widget rect, in region coords */
-       data->bbox.xmin= MENU_SHADOW_SIDE;
-       data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
-       data->bbox.ymin= MENU_SHADOW_BOTTOM;
-       data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
+       data->bbox.xmin = MENU_SHADOW_SIDE;
+       data->bbox.xmax = x2-x1 + MENU_SHADOW_SIDE;
+       data->bbox.ymin = MENU_SHADOW_BOTTOM;
+       data->bbox.ymax = y2-y1 + MENU_SHADOW_BOTTOM;
        
        /* region bigger for shadow */
-       ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
-       ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
-       ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
-       ar->winrct.ymax= y2 + MENU_TOP;
+       ar->winrct.xmin = x1 - MENU_SHADOW_SIDE;
+       ar->winrct.xmax = x2 + MENU_SHADOW_SIDE;
+       ar->winrct.ymin = y1 - MENU_SHADOW_BOTTOM;
+       ar->winrct.ymax = y2 + MENU_TOP;
 
        /* adds subwindow */
        ED_region_init(C, ar);
@@ -657,35 +720,35 @@ typedef struct uiSearchboxData {
 int uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int iconid)
 {
        /* hijack for autocomplete */
-       if(items->autocpl) {
+       if (items->autocpl) {
                autocomplete_do_name(items->autocpl, name);
                return 1;
        }
        
        /* hijack for finding active item */
-       if(items->active) {
-               if(poin==items->active)
+       if (items->active) {
+               if (poin==items->active)
                        items->offset_i= items->totitem;
                items->totitem++;
                return 1;
        }
        
-       if(items->totitem>=items->maxitem) {
+       if (items->totitem>=items->maxitem) {
                items->more= 1;
                return 0;
        }
        
        /* skip first items in list */
-       if(items->offset_i > 0) {
+       if (items->offset_i > 0) {
                items->offset_i--;
                return 1;
        }
        
-       if(items->names)
+       if (items->names)
                BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
-       if(items->pointers)
+       if (items->pointers)
                items->pointers[items->totitem]= poin;
-       if(items->icons)
+       if (items->icons)
                items->icons[items->totitem]= iconid;
        
        items->totitem++;
@@ -706,10 +769,10 @@ static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
        /* apply step */
        data->active+= step;
        
-       if(data->items.totitem==0)
+       if (data->items.totitem==0)
                data->active= 0;
-       else if(data->active > data->items.totitem) {
-               if(data->items.more) {
+       else if (data->active > data->items.totitem) {
+               if (data->items.more) {
                        data->items.offset++;
                        data->active= data->items.totitem;
                        ui_searchbox_update(C, ar, but, 0);
@@ -717,13 +780,13 @@ static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
                else
                        data->active= data->items.totitem;
        }
-       else if(data->active < 1) {
-               if(data->items.offset) {
+       else if (data->active < 1) {
+               if (data->items.offset) {
                        data->items.offset--;
                        data->active= 1;
                        ui_searchbox_update(C, ar, but, 0);
                }
-               else if(data->active < 0)
+               else if (data->active < 0)
                        data->active= 0;
        }
        
@@ -754,11 +817,11 @@ 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->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;
+               rect->ymax = data->bbox.ymax - MENU_TOP - itemnr*buth;
+               rect->ymin = rect->ymax - buth;
        }
        
 }
@@ -778,13 +841,13 @@ void ui_searchbox_apply(uiBut *but, ARegion *ar)
 
        but->func_arg2= NULL;
        
-       if(data->active) {
+       if (data->active) {
                char *name= data->items.names[data->active-1];
                char *cpoin= strchr(name, '|');
                
-               if(cpoin) cpoin[0]= 0;
+               if (cpoin) cpoin[0]= 0;
                BLI_strncpy(but->editstr, name, data->items.maxstrlen);
-               if(cpoin) cpoin[0]= '|';
+               if (cpoin) cpoin[0]= '|';
                
                but->func_arg2= data->items.pointers[data->active-1];
        }
@@ -804,14 +867,14 @@ void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, wmEvent *event)
                        ui_searchbox_select(C, ar, but, 1);
                        break;
                case MOUSEMOVE:
-                       if(BLI_in_rcti(&ar->winrct, event->x, event->y)) {
+                       if (BLI_in_rcti(&ar->winrct, event->x, event->y)) {
                                rcti rect;
                                int a;
                                
-                               for(a=0; a<data->items.totitem; a++) {
+                               for (a=0; a<data->items.totitem; a++) {
                                        ui_searchbox_butrect(&rect, data, a);
-                                       if(BLI_in_rcti(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
-                                               if( data->active!= a+1) {
+                                       if (BLI_in_rcti(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
+                                               if ( data->active!= a+1) {
                                                        data->active= a+1;
                                                        ui_searchbox_select(C, ar, but, 0);
                                                        break;
@@ -831,7 +894,7 @@ void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, int reset)
        /* reset vars */
        data->items.totitem= 0;
        data->items.more= 0;
-       if(reset==0) {
+       if (reset==0) {
                data->items.offset_i= data->items.offset;
        }
        else {
@@ -839,21 +902,21 @@ void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, int reset)
                data->active= 0;
                
                /* handle active */
-               if(but->search_func && but->func_arg2) {
+               if (but->search_func && but->func_arg2) {
                        data->items.active= but->func_arg2;
                        but->search_func(C, but->search_arg, but->editstr, &data->items);
                        data->items.active= NULL;
                        
                        /* found active item, calculate real offset by centering it */
-                       if(data->items.totitem) {
+                       if (data->items.totitem) {
                                /* first case, begin of list */
-                               if(data->items.offset_i < data->items.maxitem) {
+                               if (data->items.offset_i < data->items.maxitem) {
                                        data->active= data->items.offset_i+1;
                                        data->items.offset_i= 0;
                                }
                                else {
                                        /* second case, end of list */
-                                       if(data->items.totitem - data->items.offset_i <= data->items.maxitem) {
+                                       if (data->items.totitem - data->items.offset_i <= data->items.maxitem) {
                                                data->active= 1 + data->items.offset_i - data->items.totitem + data->items.maxitem;
                                                data->items.offset_i= data->items.totitem - data->items.maxitem;
                                        }
@@ -870,22 +933,22 @@ void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, int reset)
        }
        
        /* callback */
-       if(but->search_func)
+       if (but->search_func)
                but->search_func(C, but->search_arg, but->editstr, &data->items);
        
        /* handle case where editstr is equal to one of items */
-       if(reset && data->active==0) {
+       if (reset && data->active==0) {
                int a;
                
-               for(a=0; a<data->items.totitem; a++) {
+               for (a=0; a<data->items.totitem; a++) {
                        char *cpoin= strchr(data->items.names[a], '|');
                        
-                       if(cpoin) cpoin[0]= 0;
-                       if(0==strcmp(but->editstr, data->items.names[a]))
+                       if (cpoin) cpoin[0]= 0;
+                       if (0==strcmp(but->editstr, data->items.names[a]))
                                data->active= a+1;
-                       if(cpoin) cpoin[0]= '|';
+                       if (cpoin) cpoin[0]= '|';
                }
-               if(data->items.totitem==1 && but->editstr[0])
+               if (data->items.totitem==1 && but->editstr[0])
                        data->active= 1;
        }
 
@@ -899,7 +962,7 @@ void ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
 {
        uiSearchboxData *data= ar->regiondata;
 
-       if(str[0]) {
+       if (str[0]) {
                data->items.autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
 
                but->search_func(C, but->search_arg, but->editstr, &data->items);
@@ -916,17 +979,17 @@ static void ui_searchbox_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
        /* pixel space */
        wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
 
-       if(!data->noback)
+       if (!data->noback)
                ui_draw_search_back(NULL, NULL, &data->bbox); /* style not used yet */
        
        /* draw text */
-       if(data->items.totitem) {
+       if (data->items.totitem) {
                rcti rect;
                int a;
                
                if (data->preview) {
                        /* draw items */
-                       for(a=0; a<data->items.totitem; a++) {
+                       for (a=0; a<data->items.totitem; a++) {
                                ui_searchbox_butrect(&rect, data, a);
                                
                                /* widget itself */
@@ -937,22 +1000,23 @@ static void ui_searchbox_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
                        }
                        
                        /* indicate more */
-                       if(data->items.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) {
+                       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);
                        }
                        
-               } else {
+               }
+               else {
                        /* draw items */
-                       for(a=0; a<data->items.totitem; a++) {
+                       for (a=0; a<data->items.totitem; a++) {
                                ui_searchbox_butrect(&rect, data, a);
                                
                                /* widget itself */
@@ -960,13 +1024,13 @@ static void ui_searchbox_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
                                
                        }
                        /* indicate more */
-                       if(data->items.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) {
+                       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);
@@ -982,8 +1046,9 @@ static void ui_searchbox_region_free_cb(ARegion *ar)
        int a;
 
        /* free search data */
-       for(a=0; a<data->items.maxitem; 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);
        MEM_freeN(data->items.icons);
@@ -1022,8 +1087,8 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        ar->regiondata= data;
        
        /* special case, hardcoded feature, not draw backdrop when called from menus,
-          assume for design that popup already added it */
-       if(but->block->flag & UI_BLOCK_LOOP)
+        * assume for design that popup already added it */
+       if (but->block->flag & UI_BLOCK_LOOP)
                data->noback= 1;
        
        if (but->a1 > 0 && but->a2 > 0) {
@@ -1033,20 +1098,20 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        }
        
        /* compute position */
-       if(but->block->flag & UI_BLOCK_LOOP) {
+       if (but->block->flag & UI_BLOCK_LOOP) {
                /* this case is search menu inside other menu */
                /* we copy region size */
 
                ar->winrct= butregion->winrct;
                
                /* widget rect, in region coords */
-               data->bbox.xmin= MENU_SHADOW_SIDE;
-               data->bbox.xmax= (ar->winrct.xmax-ar->winrct.xmin) - MENU_SHADOW_SIDE;
-               data->bbox.ymin= MENU_SHADOW_BOTTOM;
-               data->bbox.ymax= (ar->winrct.ymax-ar->winrct.ymin) - MENU_SHADOW_BOTTOM;
+               data->bbox.xmin = MENU_SHADOW_SIDE;
+               data->bbox.xmax = (ar->winrct.xmax-ar->winrct.xmin) - MENU_SHADOW_SIDE;
+               data->bbox.ymin = MENU_SHADOW_BOTTOM;
+               data->bbox.ymax = (ar->winrct.ymax-ar->winrct.ymin) - MENU_SHADOW_BOTTOM;
                
                /* check if button is lower half */
-               if( but->y2 < (but->block->miny+but->block->maxy)/2 ) {
+               if ( but->y2 < (but->block->miny+but->block->maxy)/2 ) {
                        data->bbox.ymin += (but->y2-but->y1);
                }
                else {
@@ -1068,12 +1133,12 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
                y2f += ofsy;
        
                /* minimal width */
-               if(x2f - x1f < 150) x2f= x1f+150; // XXX arbitrary
+               if (x2f - x1f < 150) x2f= x1f+150; // XXX arbitrary
                
                /* copy to int, gets projected if possible too */
                x1= x1f; y1= y1f; x2= x2f; y2= y2f; 
                
-               if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
+               if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
                        UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
                        UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
                }
@@ -1085,9 +1150,9 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
 
                wm_window_get_size(CTX_wm_window(C), &winx, &winy);
                
-               if(x2 > winx) {
+               if (x2 > winx) {
                        /* super size */
-                       if(x2 > winx + x1) {
+                       if (x2 > winx + x1) {
                                x2= winx;
                                x1= 0;
                        }
@@ -1097,7 +1162,7 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
                        }
                }
 
-               if(y1 < 0) {
+               if (y1 < 0) {
                        int newy1;
                        UI_view2d_to_region_no_clip(&butregion->v2d, 0, but->y2 + ofsy, NULL, &newy1);
                        newy1 += butregion->winrct.ymin;
@@ -1107,16 +1172,16 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
                }
 
                /* widget rect, in region coords */
-               data->bbox.xmin= MENU_SHADOW_SIDE;
-               data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
-               data->bbox.ymin= MENU_SHADOW_BOTTOM;
-               data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
+               data->bbox.xmin = MENU_SHADOW_SIDE;
+               data->bbox.xmax = x2-x1 + MENU_SHADOW_SIDE;
+               data->bbox.ymin = MENU_SHADOW_BOTTOM;
+               data->bbox.ymax = y2-y1 + MENU_SHADOW_BOTTOM;
                
                /* region bigger for shadow */
-               ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
-               ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
-               ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
-               ar->winrct.ymax= y2;
+               ar->winrct.xmin = x1 - MENU_SHADOW_SIDE;
+               ar->winrct.xmax = x2 + MENU_SHADOW_SIDE;
+               ar->winrct.ymin = y1 - MENU_SHADOW_BOTTOM;
+               ar->winrct.ymax = y2;
        }
        
        /* adds subwindow */
@@ -1128,7 +1193,8 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        /* prepare search data */
        if (data->preview) {
                data->items.maxitem= data->prv_rows * data->prv_cols;
-       } else {
+       }
+       else {
                data->items.maxitem= SEARCH_ITEMS;
        }
        data->items.maxstrlen= but->hardmax;
@@ -1136,7 +1202,7 @@ ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
        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++)
+       for (x1=0; x1<data->items.maxitem; x1++)
                data->items.names[x1]= MEM_callocN(but->hardmax+1, "search pointers");
        
        return ar;
@@ -1156,7 +1222,7 @@ void ui_but_search_test(uiBut *but)
 
        /* 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) {
+       if (but->rnaprop && RNA_property_type(but->rnaprop) != PROP_STRING) {
                return;
        }
 
@@ -1166,24 +1232,25 @@ void ui_but_search_test(uiBut *but)
        items->maxitem= 10;
        items->maxstrlen= 256;
        items->names= MEM_callocN(items->maxitem*sizeof(void *), "search names");
-       for(x1=0; x1<items->maxitem; x1++)
+       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)
+       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)
+       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)
+               if (x1==items->totitem)
                        uiButSetFlag(but, UI_BUT_REDALERT);
        }
        
-       for(x1=0; x1<items->maxitem; x1++)
+       for (x1 = 0; x1 < items->maxitem; x1++) {
                MEM_freeN(items->names[x1]);
+       }
        MEM_freeN(items->names);
        MEM_freeN(items);
 }
@@ -1202,25 +1269,25 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
        short dir1= 0, dir2=0;
        
        /* transform to window coordinates, using the source button region/block */
-       butrct.xmin= but->x1; butrct.xmax= but->x2;
-       butrct.ymin= but->y1; butrct.ymax= but->y2;
+       butrct.xmin = but->x1; butrct.xmax = but->x2;
+       butrct.ymin = but->y1; butrct.ymax = but->y2;
 
        ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
        ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
 
        /* calc block rect */
-       if(block->minx == 0.0f && block->maxx == 0.0f) {
-               if(block->buttons.first) {
+       if (block->minx == 0.0f && block->maxx == 0.0f) {
+               if (block->buttons.first) {
                        block->minx= block->miny= 10000;
                        block->maxx= block->maxy= -10000;
                        
                        bt= block->buttons.first;
-                       while(bt) {
-                               if(bt->x1 < block->minx) block->minx= bt->x1;
-                               if(bt->y1 < block->miny) block->miny= bt->y1;
+                       while (bt) {
+                               if (bt->x1 < block->minx) block->minx= bt->x1;
+                               if (bt->y1 < block->miny) block->miny= bt->y1;
 
-                               if(bt->x2 > block->maxx) block->maxx= bt->x2;
-                               if(bt->y2 > block->maxy) block->maxy= bt->y2;
+                               if (bt->x2 > block->maxx) block->maxx= bt->x2;
+                               if (bt->y2 > block->maxy) block->maxy= bt->y2;
                                
                                bt= bt->next;
                        }
@@ -1250,16 +1317,16 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
 
                wm_window_get_size(window, &winx, &winy);
 
-               if(block->direction & UI_CENTER) center= ysize/2;
+               if (block->direction & UI_CENTER) center= ysize/2;
                else center= 0;
                
                /* 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.0f) down= 1;
-               if( butrct.ymax+ysize-center < winy) top= 1;
+               if ( butrct.xmin-xsize > 0.0f) left= 1;
+               if ( butrct.xmax+xsize < winx) right= 1;
+               if ( butrct.ymin-ysize+center > 0.0f) down= 1;
+               if ( butrct.ymax+ysize-center < winy) top= 1;
                
-               if(top==0 && down==0) {
+               if (top==0 && down==0) {
                        if (butrct.ymin-ysize < winy-butrct.ymax-ysize)
                                top= 1;
                        else
@@ -1269,74 +1336,74 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
                dir1= block->direction & UI_DIRECTION;
 
                /* secundary directions */
-               if(dir1 & (UI_TOP|UI_DOWN)) {
-                       if(dir1 & UI_LEFT) dir2= UI_LEFT;
-                       else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
+               if (dir1 & (UI_TOP|UI_DOWN)) {
+                       if (dir1 & UI_LEFT) dir2= UI_LEFT;
+                       else if (dir1 & UI_RIGHT) dir2= UI_RIGHT;
                        dir1 &= (UI_TOP|UI_DOWN);
                }
 
-               if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
-               if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
+               if (dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
+               if (dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
                
-               /* no space at all? dont change */
-               if(left || right) {
-                       if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
-                       if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
+               /* no space at all? don't change */
+               if (left || right) {
+                       if (dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
+                       if (dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
                        /* this is aligning, not append! */
-                       if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
-                       if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
+                       if (dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
+                       if (dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
                }
-               if(down || top) {
-                       if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
-                       if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
-                       if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
-                       if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
+               if (down || top) {
+                       if (dir1==UI_TOP && top==0) dir1= UI_DOWN;
+                       if (dir1==UI_DOWN && down==0) dir1= UI_TOP;
+                       if (dir2==UI_TOP && top==0) dir2= UI_DOWN;
+                       if (dir2==UI_DOWN && down==0) dir2= UI_TOP;
                }
                
-               if(dir1==UI_LEFT) {
+               if (dir1==UI_LEFT) {
                        xof= butrct.xmin - block->maxx;
-                       if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
+                       if (dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
                        else yof= butrct.ymax - block->maxy+center;
                }
-               else if(dir1==UI_RIGHT) {
+               else if (dir1==UI_RIGHT) {
                        xof= butrct.xmax - block->minx;
-                       if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
+                       if (dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
                        else yof= butrct.ymax - block->maxy+center;
                }
-               else if(dir1==UI_TOP) {
+               else if (dir1==UI_TOP) {
                        yof= butrct.ymax - block->miny;
-                       if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
+                       if (dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
                        else xof= butrct.xmin - block->minx;
                        // changed direction? 
-                       if((dir1 & block->direction)==0) {
-                               if(block->direction & UI_SHIFT_FLIPPED)
+                       if ((dir1 & block->direction)==0) {
+                               if (block->direction & UI_SHIFT_FLIPPED)
                                        xof+= dir2==UI_LEFT?25:-25;
                                uiBlockFlipOrder(block);
                        }
                }
-               else if(dir1==UI_DOWN) {
+               else if (dir1==UI_DOWN) {
                        yof= butrct.ymin - block->maxy;
-                       if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
+                       if (dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
                        else xof= butrct.xmin - block->minx;
                        // changed direction?
-                       if((dir1 & block->direction)==0) {
-                               if(block->direction & UI_SHIFT_FLIPPED)
+                       if ((dir1 & block->direction)==0) {
+                               if (block->direction & UI_SHIFT_FLIPPED)
                                        xof+= dir2==UI_LEFT?25:-25;
                                uiBlockFlipOrder(block);
                        }
                }
 
                /* and now we handle the exception; no space below or to top */
-               if(top==0 && down==0) {
-                       if(dir1==UI_LEFT || dir1==UI_RIGHT) {
+               if (top==0 && down==0) {
+                       if (dir1==UI_LEFT || dir1==UI_RIGHT) {
                                // align with bottom of screen 
                                // yof= ysize; (not with menu scrolls)
                        }
                }
                
                /* or no space left or right */
-               if(left==0 && right==0) {
-                       if(dir1==UI_TOP || dir1==UI_DOWN) {
+               if (left==0 && right==0) {
+                       if (dir1==UI_TOP || dir1==UI_DOWN) {
                                // align with left size of screen 
                                xof= -block->minx+5;
                        }
@@ -1347,16 +1414,16 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
                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 */
+               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 offset, buttons in window coords */
        
-       for(bt= block->buttons.first; bt; bt= bt->next) {
+       for (bt= block->buttons.first; bt; bt= bt->next) {
                ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
                ui_block_to_window_fl(butregion, but->block, &bt->x2, &bt->y2);
 
@@ -1376,38 +1443,38 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
        block->maxy += yof;
 
        /* safety calculus */
-       if(but) {
+       if (but) {
                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 */
                
                // parent button to left
-               if( midx < block->minx ) block->safety.xmin= block->minx-3; 
-               else block->safety.xmin= block->minx-40;
+               if ( midx < block->minx ) block->safety.xmin = block->minx-3; 
+               else block->safety.xmin = block->minx-40;
                // parent button to right
-               if( midx > block->maxx ) block->safety.xmax= block->maxx+3; 
-               else block->safety.xmax= block->maxx+40;
+               if ( midx > block->maxx ) block->safety.xmax = block->maxx+3; 
+               else block->safety.xmax = block->maxx+40;
                
                // parent button on bottom
-               if( midy < block->miny ) block->safety.ymin= block->miny-3; 
-               else block->safety.ymin= block->miny-40;
+               if ( midy < block->miny ) block->safety.ymin = block->miny-3; 
+               else block->safety.ymin = block->miny-40;
                // parent button on top
-               if( midy > block->maxy ) block->safety.ymax= block->maxy+3; 
-               else block->safety.ymax= block->maxy+40;
+               if ( midy > block->maxy ) block->safety.ymax = block->maxy+3; 
+               else block->safety.ymax = block->maxy+40;
                
                // exception for switched pulldowns...
-               if(dir1 && (dir1 & block->direction)==0) {
-                       if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3; 
-                       if(dir2==UI_LEFT) block->safety.xmin= block->minx-3; 
+               if (dir1 && (dir1 & block->direction)==0) {
+                       if (dir2==UI_RIGHT) block->safety.xmax = block->maxx+3; 
+                       if (dir2==UI_LEFT) block->safety.xmin = block->minx-3; 
                }
                block->direction= dir1;
        }
        else {
-               block->safety.xmin= block->minx-40;
-               block->safety.ymin= block->miny-40;
-               block->safety.xmax= block->maxx+40;
-               block->safety.ymax= block->maxy+40;
+               block->safety.xmin = block->minx-40;
+               block->safety.ymin = block->miny-40;
+               block->safety.xmax = block->maxx+40;
+               block->safety.ymax = block->maxy+40;
        }
 
        /* keep a list of these, needed for pulldown menus */
@@ -1415,7 +1482,7 @@ static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but,
        saferct->parent= butrct;
        saferct->safety= block->safety;
        BLI_freelistN(&block->saferct);
-       if(but)
+       if (but)
                BLI_duplicatelist(&block->saferct, &but->block->saferct);
        BLI_addhead(&block->saferct, saferct);
 }
@@ -1424,7 +1491,7 @@ static void ui_block_region_draw(const bContext *C, ARegion *ar)
 {
        uiBlock *block;
 
-       for(block=ar->uiblocks.first; block; block=block->next)
+       for (block=ar->uiblocks.first; block; block=block->next)
                uiDrawBlock(C, block);
 }
 
@@ -1434,14 +1501,14 @@ static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
        
        wm_window_get_size(window, &winx, &winy);
        
-       if(block->minx < MENU_SHADOW_SIDE)
+       if (block->minx < MENU_SHADOW_SIDE)
                block->minx= MENU_SHADOW_SIDE;
-       if(block->maxx > winx-MENU_SHADOW_SIDE)
+       if (block->maxx > winx-MENU_SHADOW_SIDE)
                block->maxx= winx-MENU_SHADOW_SIDE;
        
-       if(block->miny < MENU_SHADOW_BOTTOM)
+       if (block->miny < MENU_SHADOW_BOTTOM)
                block->miny= MENU_SHADOW_BOTTOM;
-       if(block->maxy > winy-MENU_TOP)
+       if (block->maxy > winy-MENU_TOP)
                block->maxy= winy-MENU_TOP;
 }
 
@@ -1453,33 +1520,33 @@ void ui_popup_block_scrolltest(uiBlock *block)
        
        block->flag &= ~(UI_BLOCK_CLIPBOTTOM|UI_BLOCK_CLIPTOP);
        
-       for(bt= block->buttons.first; bt; bt= bt->next)
+       for (bt= block->buttons.first; bt; bt= bt->next)
                bt->flag &= ~UI_SCROLLED;
        
-       if(block->buttons.first==block->buttons.last)
+       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) {
+       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(is_flip && bt->next && bt->next->y1 > bt->y1)
+                       if (bt->y2 < block->miny +10) {
+                               if (is_flip && bt->next && bt->next->y1 > bt->y1)
                                        bt->next->flag |= UI_SCROLLED;
-                               else if(!is_flip && bt->prev && bt->prev->y1 > bt->y1)
+                               else if (!is_flip && bt->prev && bt->prev->y1 > bt->y1)
                                        bt->prev->flag |= UI_SCROLLED;
                        }
                }
-               if(bt->y2 > block->maxy) {
+               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(!is_flip && bt->next && bt->next->y2 < bt->y2)
+                       if (bt->y1 > block->maxy -10) {
+                               if (!is_flip && bt->next && bt->next->y2 < bt->y2)
                                        bt->next->flag |= UI_SCROLLED;
-                               else if(is_flip && bt->prev && bt->prev->y2 < bt->y2)
+                               else if (is_flip && bt->prev && bt->prev->y2 < bt->y2)
                                        bt->prev->flag |= UI_SCROLLED;
                        }
                }
@@ -1514,12 +1581,12 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
        UI_add_region_handlers(&ar->handlers);
 
        /* create ui block */
-       if(create_func)
+       if (create_func)
                block= create_func(C, handle->region, arg);
        else
                block= handle_create_func(C, handle, arg);
        
-       if(block->handle) {
+       if (block->handle) {
                memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
                MEM_freeN(handle);
                handle= block->handle;
@@ -1529,14 +1596,16 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
 
        ar->regiondata= handle;
 
-       if(!block->endblock)
+       if (!block->endblock)
                uiEndBlock(C, block);
 
        /* if this is being created from a button */
-       if(but) {
-               if(ELEM(but->type, BLOCK, PULLDOWN))
+       if (but) {
+               if (ELEM(but->type, BLOCK, PULLDOWN))
                        block->xofs = -2;       /* for proper alignment */
 
+               block->aspect = but->block->aspect;
+
                ui_block_position(window, butregion, but, block);
        }
        else {
@@ -1553,17 +1622,17 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut
        /* 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 */
-       ar->winrct.xmin= block->minx - MENU_SHADOW_SIDE;
-       ar->winrct.xmax= block->maxx + MENU_SHADOW_SIDE;
-       ar->winrct.ymin= block->miny - MENU_SHADOW_BOTTOM;
-       ar->winrct.ymax= block->maxy + MENU_TOP;
+       ar->winrct.xmin = block->minx - MENU_SHADOW_SIDE;
+       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;
        block->maxy -= ar->winrct.ymin;
 
-       for(bt= block->buttons.first; bt; bt= bt->next) {
+       for (bt= block->buttons.first; bt; bt= bt->next) {
                bt->x1 -= ar->winrct.xmin;
                bt->x2 -= ar->winrct.xmin;
                bt->y1 -= ar->winrct.ymin;
@@ -1593,7 +1662,7 @@ void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
 {
        ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
        
-       if(handle->scrolltimer)
+       if (handle->scrolltimer)
                WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), handle->scrolltimer);
        
        MEM_freeN(handle);
@@ -1620,20 +1689,20 @@ static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *a
 
        /* columns and row estimation */
        columns= (md->nitems+20)/20;
-       if(columns<1)
+       if (columns<1)
                columns= 1;
-       if(columns>8)
+       if (columns>8)
                columns= (md->nitems+25)/25;
        
        rows= md->nitems/columns;
-       if(rows<1)
+       if (rows<1)
                rows= 1;
-       while(rows*columns<md->nitems)
+       while (rows*columns<md->nitems)
                rows++;
 
        /* create title */
-       if(md->title) {
-               if(md->titleicon) {
+       if (md->title) {
+               if (md->titleicon) {
                        uiItemL(layout, md->title, md->titleicon);
                }
                else {
@@ -1645,8 +1714,8 @@ static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *a
 
        /* inconsistent, but menus with labels do not look good flipped */
        entry= md->items;
-       for(a=0; a<md->nitems; a++, entry++) {
-               if(entry->sepr && entry->str[0]) {
+       for (a=0; a<md->nitems; a++, entry++) {
+               if (entry->sepr && entry->str[0]) {
                        block->flag |= UI_BLOCK_NO_FLIP;
                        break;
                }
@@ -1655,18 +1724,18 @@ static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *a
        /* create items */
        split= uiLayoutSplit(layout, 0, 0);
 
-       for(a=0; a<md->nitems; a++) {
-               if(a == column_end) {
+       for (a=0; a<md->nitems; a++) {
+               if (a == column_end) {
                        /* start new column, and find out where it ends in advance, so we
-                          can flip the order of items properly per column */
+                        * can flip the order of items properly per column */
                        column_start= a;
                        column_end= md->nitems;
 
-                       for(b=a+1; b<md->nitems; b++) {
+                       for (b=a+1; b<md->nitems; b++) {
                                entry= &md->items[b];
 
                                /* new column on N rows or on separation label */
-                               if(((b-a) % rows == 0) || (entry->sepr && entry->str[0])) {
+                               if (((b-a) % rows == 0) || (entry->sepr && entry->str[0])) {
                                        column_end = b;
                                        break;
                                }
@@ -1675,17 +1744,17 @@ static void ui_block_func_MENUSTR(bContext *UNUSED(C), uiLayout *layout, void *a
                        column= uiLayoutColumn(split, 0);
                }
 
-               if(block->flag & UI_BLOCK_NO_FLIP)
+               if (block->flag & UI_BLOCK_NO_FLIP)
                        entry= &md->items[a];
                else
                        entry= &md->items[column_start + column_end-1-a];
 
-               if(entry->sepr) {
+               if (entry->sepr) {
                        uiItemL(column, entry->str, entry->icon);
                        bt= block->buttons.last;
                        bt->flag= UI_TEXT_LEFT;
                }
-               else if(entry->icon) {
+               else if (entry->icon) {
                        uiDefIconTextButF(block, BUTM|FLO, B_NOP, entry->icon, entry->str, 0, 0,
                                UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
                }
@@ -1707,7 +1776,7 @@ void ui_block_func_ICONROW(bContext *UNUSED(C), uiLayout *layout, void *arg_but)
        
        uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
        
-       for(a=(int)but->hardmin; a<=(int)but->hardmax; a++)
+       for (a=(int)but->hardmin; a<=(int)but->hardmax; a++)
                uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->hardmin), 0, 0, UI_UNIT_X*5, UI_UNIT_Y,
                        &handle->retvalue, (float)a, 0.0, 0, 0, "");
 }
@@ -1726,16 +1795,16 @@ void ui_block_func_ICONTEXTROW(bContext *UNUSED(C), uiLayout *layout, void *arg_
        md= decompose_menu_string(but->str);
 
        /* title */
-       if(md->title) {
+       if (md->title) {
                bt= uiDefBut(block, LABEL, 0, md->title, 0, 0, UI_UNIT_X*5, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
                bt->flag= UI_TEXT_LEFT;
        }
 
        /* loop through the menu options and draw them out with icons & text labels */
-       for(a=0; a<md->nitems; a++) {
+       for (a=0; a<md->nitems; a++) {
                entry= &md->items[md->nitems-a-1];
 
-               if(entry->sepr)
+               if (entry->sepr)
                        uiItemS(layout);
                else
                        uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(entry->retval-but->hardmin)), entry->str,
@@ -1749,11 +1818,11 @@ void ui_block_func_ICONTEXTROW(bContext *UNUSED(C), uiLayout *layout, void *arg_
 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;
-          this causes a menu being pressed immediately ... */
-       #ifndef __APPLE__
+       /* OSX has very poor mouse-warp support, it sends events;
+        * this causes a menu being pressed immediately ... */
+#  ifndef __APPLE__
        warp_pointer(x, y);
-       #endif
+#  endif
 }
 #endif
 
@@ -1787,13 +1856,13 @@ static void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
        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) {
+       for (bt= block->buttons.first; bt; bt= bt->next) {
                if (bt->rnaprop) {
                        
                        ui_set_but_vectorf(bt, rgb);
                        
                }
-               else if(strcmp(bt->str, "Hex: ")==0) {
+               else if (strcmp(bt->str, "Hex: ")==0) {
                        float rgb_gamma[3];
                        double intpart;
                        char col[16];
@@ -1802,7 +1871,8 @@ static void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
                        
                        if (block->color_profile == BLI_PR_NONE) {
                                copy_v3_v3(rgb_gamma, rgb);
-                       } else {
+                       }
+                       else {
                                /* make an sRGB version, for Hex code */
                                linearrgb_to_srgb_v3_v3(rgb_gamma, rgb);
                        }
@@ -1815,23 +1885,23 @@ static void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
                        
                        strcpy(bt->poin, col);
                }
-               else if(bt->str[1]==' ') {
-                       if(bt->str[0]=='R') {
+               else if (bt->str[1]==' ') {
+                       if (bt->str[0]=='R') {
                                ui_set_but_val(bt, rgb[0]);
                        }
-                       else if(bt->str[0]=='G') {
+                       else if (bt->str[0]=='G') {
                                ui_set_but_val(bt, rgb[1]);
                        }
-                       else if(bt->str[0]=='B') {
+                       else if (bt->str[0]=='B') {
                                ui_set_but_val(bt, rgb[2]);
                        }
-                       else if(bt->str[0]=='H') {
+                       else if (bt->str[0]=='H') {
                                ui_set_but_val(bt, hsv[0]);
                        }
-                       else if(bt->str[0]=='S') {
+                       else if (bt->str[0]=='S') {
                                ui_set_but_val(bt, hsv[1]);
                        }
-                       else if(bt->str[0]=='V') {
+                       else if (bt->str[0]=='V') {
                                ui_set_but_val(bt, hsv[2]);
                        }
                }               
@@ -1853,7 +1923,7 @@ static void do_picker_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
                ui_update_block_buts_rgb(but->block, rgb);
        }
        
-       if(popup)
+       if (popup)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
@@ -1868,7 +1938,7 @@ static void do_hsv_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
        
        ui_update_block_buts_rgb(but->block, rgb);
        
-       if(popup)
+       if (popup)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
@@ -1889,7 +1959,7 @@ static void do_hex_rna_cb(bContext *UNUSED(C), void *bt1, void *hexcl)
        
        ui_update_block_buts_rgb(but->block, rgb);
        
-       if(popup)
+       if (popup)
                popup->menuretval= UI_RETURN_UPDATE;
 }
 
@@ -1898,7 +1968,7 @@ static void close_popup_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
        uiBut *but= (uiBut *)bt1;
        uiPopupBlockHandle *popup= but->block->handle;
        
-       if(popup)
+       if (popup)
                popup->menuretval= UI_RETURN_OK;
 }
 
@@ -1907,26 +1977,26 @@ static void picker_new_hide_reveal(uiBlock *block, short colormode)
        uiBut *bt;
        
        /* tag buttons */
-       for(bt= block->buttons.first; bt; bt= bt->next) {
+       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;
+                       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;
+               if (bt->type==NUMSLI || bt->type==TEX) {
+                       if ( bt->str[1]=='e') {
+                               if (colormode==2) bt->flag &= ~UI_HIDDEN;
                                else bt->flag |= UI_HIDDEN;
                        }
-                       else if( ELEM3(bt->str[0], 'R', 'G', 'B')) {
-                               if(colormode==0) bt->flag &= ~UI_HIDDEN;
+                       else if ( ELEM3(bt->str[0], 'R', 'G', 'B')) {
+                               if (colormode==0) bt->flag &= ~UI_HIDDEN;
                                else bt->flag |= UI_HIDDEN;
                        }
-                       else if( ELEM3(bt->str[0], 'H', 'S', 'V')) {
-                               if(colormode==1) bt->flag &= ~UI_HIDDEN;
+                       else if ( ELEM3(bt->str[0], 'H', 'S', 'V')) {
+                               if (colormode==1) bt->flag &= ~UI_HIDDEN;
                                else bt->flag |= UI_HIDDEN;
                        }
                }
@@ -2001,7 +2071,8 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        if (block->color_profile == BLI_PR_NONE) {
                BLI_strncpy(tip, "Value in Display Color Space", sizeof(tip));
                copy_v3_v3(rgb_gamma, rgb);
-       } else {
+       }
+       else {
                BLI_strncpy(tip, "Value in Linear RGB Color Space", sizeof(tip));
                /* make an sRGB version, for Hex code */
                linearrgb_to_srgb_v3_v3(rgb_gamma, rgb);
@@ -2063,7 +2134,7 @@ static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyR
        uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
        uiBlockEndAlign(block);
 
-       if(rgb[3] != FLT_MAX) {
+       if (rgb[3] != FLT_MAX) {
                bt= uiDefButR_prop(block, NUMSLI, 0, "A ",      0, -120, butwidth, UI_UNIT_Y, ptr, prop, 3, 0.0, 0.0, 0, 0, "Alpha");
                uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
        }
@@ -2087,16 +2158,16 @@ static int ui_picker_small_wheel_cb(const bContext *UNUSED(C), uiBlock *block, w
 {
        float add= 0.0f;
        
-       if(event->type==WHEELUPMOUSE)
+       if (event->type==WHEELUPMOUSE)
                add= 0.05f;
-       else if(event->type==WHEELDOWNMOUSE)
+       else if (event->type==WHEELDOWNMOUSE)
                add= -0.05f;
        
-       if(add!=0.0f) {
+       if (add!=0.0f) {
                uiBut *but;
                
-               for(but= block->buttons.first; but; but= but->next) {
-                       if(but->type==HSVCUBE && but->active==NULL) {
+               for (but= block->buttons.first; but; but= but->next) {
+                       if (but->type==HSVCUBE && but->active==NULL) {
                                uiPopupBlockHandle *popup= block->handle;
                                float col[3];
                                float *hsv= ui_block_hsv_get(block);
@@ -2110,7 +2181,7 @@ static int ui_picker_small_wheel_cb(const bContext *UNUSED(C), uiBlock *block, w
                                ui_set_but_vectorf(but, col);
                                
                                ui_update_block_buts_rgb(block, col);
-                               if(popup)
+                               if (popup)
                                        popup->menuretval= UI_RETURN_UPDATE;
                                
                                return 1;
@@ -2158,9 +2229,9 @@ static int ui_popup_string_hash(char *str)
        int hash;
        char *delimit= strchr(str, '|');
 
-       if(delimit) *delimit= 0;
+       if (delimit) *delimit= 0;
        hash= BLI_ghashutil_strhash(str);
-       if(delimit) *delimit= '|';
+       if (delimit) *delimit= '|';
 
        return hash;
 }
@@ -2176,21 +2247,21 @@ uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
        static int mem[256], first=1;
        int hash= block->puphash;
        
-       if(first) {
+       if (first) {
                /* init */
                memset(mem, -1, sizeof(mem));
                first= 0;
        }
 
-       if(but) {
+       if (but) {
                /* set */
                mem[hash & 255 ]= ui_popup_string_hash(but->str);
                return NULL;
        }
        else {
                /* get */
-               for(but=block->buttons.first; but; but=but->next)
-                       if(ui_popup_string_hash(but->str) == 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;
@@ -2218,17 +2289,17 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
        uiPopupMenu *pup= arg_pup;
        int offset[2], direction, minwidth, width, height, flip;
 
-       if(pup->menu_func) {
+       if (pup->menu_func) {
                pup->block->handle= handle;
                pup->menu_func(C, pup->layout, pup->menu_arg);
                pup->block->handle= NULL;
        }
 
-       if(pup->but) {
+       if (pup->but) {
                /* minimum width to enforece */
                minwidth= pup->but->x2 - pup->but->x1;
 
-               if(pup->but->type == PULLDOWN || pup->but->menu_create_func) {
+               if (pup->but->type == PULLDOWN || pup->but->menu_create_func) {
                        direction= UI_DOWN;
                        flip= 1;
                }
@@ -2246,8 +2317,8 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
        block= pup->block;
        
        /* in some cases we create the block before the region,
-          so we set it delayed here if necessary */
-       if(BLI_findindex(&handle->region->uiblocks, block) == -1)
+        * so we set it delayed here if necessary */
+       if (BLI_findindex(&handle->region->uiblocks, block) == -1)
                uiBlockSetRegion(block, handle->region);
 
        block->direction= direction;
@@ -2256,25 +2327,25 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
 
        uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
        
-       if(pup->popup) {
+       if (pup->popup) {
                uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
                uiBlockSetDirection(block, direction);
 
                /* offset the mouse position, possibly based on earlier selection */
-               if((block->flag & UI_BLOCK_POPUP_MEMORY) &&
+               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 */
+                        * 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 */
+                        * on the first item */
                        offset[0]= 0;
-                       for(bt=block->buttons.first; bt; bt=bt->next)
+                       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*UI_UNIT_Y;
@@ -2285,11 +2356,11 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
        }
        else {
                /* for a header menu we set the direction automatic */
-               if(!pup->slideout && flip) {
+               if (!pup->slideout && flip) {
                        ScrArea *sa= CTX_wm_area(C);
-                       if(sa && sa->headertype==HEADERDOWN) {
+                       if (sa && sa->headertype==HEADERDOWN) {
                                ARegion *ar= CTX_wm_region(C);
-                               if(ar && ar->regiontype == RGN_TYPE_HEADER) {
+                               if (ar && ar->regiontype == RGN_TYPE_HEADER) {
                                        uiBlockSetDirection(block, UI_TOP);
                                        uiBlockFlipOrder(block);
                                }
@@ -2301,7 +2372,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
        }
 
        /* if menu slides out of other menu, override direction */
-       if(pup->slideout)
+       if (pup->slideout)
                uiBlockSetDirection(block, UI_RIGHT);
 
        uiEndBlock(C, block);
@@ -2322,7 +2393,7 @@ uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut
        pup->but= but;
        uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
 
-       if(!but) {
+       if (!but) {
                /* no button to start from, means we are a popup */
                pup->mx= window->eventstate->x;
                pup->my= window->eventstate->y;
@@ -2337,13 +2408,13 @@ uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut
                /* if this is an rna button then we can assume its an enum
                 * flipping enums is generally not good since the order can be
                 * important [#28786] */
-               if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
+               if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
                        pup->block->flag |= UI_BLOCK_NO_FLIP;
                }
        }
 #endif
 
-       if(str) {
+       if (str) {
                /* menu is created from a string */
                pup->menu_func= ui_block_func_MENUSTR;
                pup->menu_arg= str;
@@ -2356,7 +2427,7 @@ uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut
        
        handle= ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
 
-       if(!but) {
+       if (!but) {
                handle->popup= 1;
 
                UI_add_popup_handlers(C, &window->modalhandlers, handle);
@@ -2387,15 +2458,15 @@ uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
        pup->block->handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
        
        /* create title button */
-       if(title && title[0]) {
+       if (title && title[0]) {
                char titlestr[256];
                
-               if(icon) {
+               if (icon) {
                        BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
                        uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
                }
                else {
-                       but= uiDefBut(pup->block, LABEL, 0, title, 0, 0, 200, UI_UNIT_Y, 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;
                }
        }
@@ -2433,7 +2504,7 @@ static void operator_name_cb(bContext *C, void *arg, int retval)
 {
        const char *opname= arg;
 
-       if(opname && retval > 0)
+       if (opname && retval > 0)
                WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
 }
 
@@ -2441,7 +2512,7 @@ static void operator_cb(bContext *C, void *arg, int retval)
 {
        wmOperator *op= arg;
        
-       if(op && retval > 0)
+       if (op && retval > 0)
                WM_operator_call(C, op);
        else
                WM_operator_free(op);
@@ -2474,7 +2545,7 @@ static void confirm_operator(bContext *C, wmOperator *op, const char *title, con
        char *s, buf[512];
        
        s= buf;
-       if (title) s+= sprintf(s, "%s%%t|%s", title, item);
+       if (title) s+= BLI_snprintf(s, sizeof(buf), "%s%%t|%s", title, item);
        (void)s;
        
        handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
@@ -2536,26 +2607,26 @@ void uiPupMenuReports(bContext *C, ReportList *reports)
        DynStr *ds;
        char *str;
 
-       if(!reports || !reports->list.first)
+       if (!reports || !reports->list.first)
                return;
-       if(!CTX_wm_window(C))
+       if (!CTX_wm_window(C))
                return;
 
        ds= BLI_dynstr_new();
 
-       for(report=reports->list.first; report; report=report->next) {
-               if(report->type < reports->printlevel)
+       for (report=reports->list.first; report; report=report->next) {
+               if (report->type < reports->printlevel)
                        ; /* pass */
-               else if(report->type >= RPT_ERROR)
+               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)
+               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)
+               else if (report->type >= RPT_INFO)
                        BLI_dynstr_appendf(ds, "Info %%i%d%%t|%s", ICON_INFO, report->message);
        }
 
        str= BLI_dynstr_get_cstring(ds);
-       if(str[0] != '\0')
+       if (str[0] != '\0')
                ui_popup_menu_create(C, NULL, NULL, NULL, NULL, str);
        MEM_freeN(str);
 
@@ -2567,14 +2638,14 @@ void uiPupMenuInvoke(bContext *C, const char *idname)
        uiPopupMenu *pup;
        uiLayout *layout;
        Menu menu;
-       MenuType *mt= WM_menutype_find(idname, TRUE);
+       MenuType *mt = WM_menutype_find(idname, TRUE);
 
-       if(mt==NULL) {
+       if (mt==NULL) {
                printf("%s: named menu \"%s\" not found\n", __func__, idname);
                return;
        }
 
-       if(mt->poll && mt->poll(C, mt)==0)
+       if (mt->poll && mt->poll(C, mt)==0)
                return;
 
        pup= uiPupMenuBegin(C, mt->label, ICON_NONE);
@@ -2650,9 +2721,14 @@ void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int
 
 void uiPupBlockClose(bContext *C, uiBlock *block)
 {
-       if(block->handle) {
-               UI_remove_popup_handlers(&CTX_wm_window(C)->modalhandlers, block->handle);
-               ui_popup_block_free(C, block->handle);
+       if (block->handle) {
+               wmWindow *win = CTX_wm_window(C);
+
+               /* if loading new .blend while popup is open, window will be NULL */
+               if (win) {
+                       UI_remove_popup_handlers(&win->modalhandlers, block->handle);
+                       ui_popup_block_free(C, block->handle);
+               }
        }
 }