Fix T37843: area split widget missing in python console editor.
[blender.git] / source / blender / editors / screen / area.c
index 1cdeeb174f6bb9810851ea8cd46e0dc400116826..a1daa3b44497673d55f2bdffc56140e9a29cd479 100644 (file)
@@ -38,8 +38,9 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
-#include "BLI_rand.h"
 #include "BLI_utildefines.h"
+#include "BLI_alloca.h"
+#include "BLI_linklist_stack.h"
 
 #include "BLF_translation.h"
 
@@ -47,6 +48,9 @@
 #include "BKE_global.h"
 #include "BKE_screen.h"
 
+#include "RNA_access.h"
+#include "RNA_types.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 #include "wm_subwindow.h"
@@ -67,6 +71,8 @@
 
 #include "screen_intern.h"
 
+extern void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3); /* xxx temp */
+
 /* general area and region code */
 
 static void region_draw_emboss(ARegion *ar, rcti *scirct)
@@ -112,7 +118,7 @@ void ED_region_pixelspace(ARegion *ar)
 }
 
 /* only exported for WM */
-void ED_region_do_listen(ARegion *ar, wmNotifier *note)
+void ED_region_do_listen(bScreen *sc, ScrArea *sa, ARegion *ar, wmNotifier *note)
 {
        /* generic notes first */
        switch (note->category) {
@@ -126,15 +132,15 @@ void ED_region_do_listen(ARegion *ar, wmNotifier *note)
        }
 
        if (ar->type && ar->type->listener)
-               ar->type->listener(ar, note);
+               ar->type->listener(sc, sa, ar, note);
 }
 
 /* only exported for WM */
-void ED_area_do_listen(ScrArea *sa, wmNotifier *note)
+void ED_area_do_listen(bScreen *sc, ScrArea *sa, wmNotifier *note)
 {
        /* no generic notes? */
        if (sa->type && sa->type->listener) {
-               sa->type->listener(sa, note);
+               sa->type->listener(sc, sa, note);
        }
 }
 
@@ -148,25 +154,6 @@ void ED_area_do_refresh(bContext *C, ScrArea *sa)
        sa->do_refresh = FALSE;
 }
 
-/* based on screen region draw tags, set draw tags in azones, and future region tabs etc */
-/* only exported for WM */
-void ED_area_overdraw_flush(ScrArea *sa, ARegion *ar)
-{
-       AZone *az;
-       
-       for (az = sa->actionzones.first; az; az = az->next) {
-               int xs, ys;
-               
-               xs = (az->x1 + az->x2) / 2;
-               ys = (az->y1 + az->y2) / 2;
-
-               /* test if inside */
-               if (BLI_rcti_isect_pt(&ar->winrct, xs, ys)) {
-                       az->do_draw = TRUE;
-               }
-       }
-}
-
 /**
  * \brief Corner widgets use for dragging and splitting the view.
  */
@@ -175,8 +162,8 @@ static void area_draw_azone(short x1, short y1, short x2, short y2)
        int dx = x2 - x1;
        int dy = y2 - y1;
 
-       dx = copysign(ceilf(0.3f * fabsf(dx)), dx);
-       dy = copysign(ceilf(0.3f * fabsf(dy)), dy);
+       dx = copysign(ceilf(0.3f * abs(dx)), dx);
+       dy = copysign(ceilf(0.3f * abs(dy)), dy);
 
        glEnable(GL_BLEND);
        glEnable(GL_LINE_SMOOTH);
@@ -235,8 +222,8 @@ static void region_draw_azone_icon(AZone *az)
 
 static void draw_azone_plus(float x1, float y1, float x2, float y2)
 {
-       float width = 2.0f;
-       float pad = 4.0f;
+       float width = 0.1f * U.widget_unit;
+       float pad = 0.2f * U.widget_unit;
        
        glRectf((x1 + x2 - width) * 0.5f, y1 + pad, (x1 + x2 + width) * 0.5f, y2 - pad);
        glRectf(x1 + pad, (y1 + y2 - width) * 0.5f, (x1 + x2 - width) * 0.5f, (y1 + y2 + width) * 0.5f);
@@ -245,8 +232,6 @@ static void draw_azone_plus(float x1, float y1, float x2, float y2)
 
 static void region_draw_azone_tab_plus(AZone *az)
 {
-       extern void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3); /* xxx temp */
-       
        glEnable(GL_BLEND);
        
        /* add code to draw region hidden as 'too small' */
@@ -321,8 +306,6 @@ static void region_draw_azone_tab(AZone *az)
 
 static void region_draw_azone_tria(AZone *az)
 {
-       extern void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3); /* xxx temp */
-       
        glEnable(GL_BLEND);
        //UI_GetThemeColor3fv(TH_HEADER, col);
        glColor4f(0.0f, 0.0f, 0.0f, 0.35f);
@@ -350,78 +333,50 @@ static void region_draw_azone_tria(AZone *az)
        glDisable(GL_BLEND);
 }
 
-/* only exported for WM */
-void ED_area_overdraw(bContext *C)
+static void region_draw_azones(ScrArea *sa, ARegion *ar)
 {
-       wmWindow *win = CTX_wm_window(C);
-       bScreen *screen = CTX_wm_screen(C);
-       ScrArea *sa;
-       
-       /* Draw AZones, in screenspace */
-       wmSubWindowSet(win, screen->mainwin);
+       AZone *az;
+
+       if (!sa)
+               return;
 
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+       glPushMatrix();
+       glTranslatef(-ar->winrct.xmin, -ar->winrct.ymin, 0.0f);
        
-       for (sa = screen->areabase.first; sa; sa = sa->next) {
-               AZone *az;
-               for (az = sa->actionzones.first; az; az = az->next) {
-                       if (az->do_draw) {
-                               if (az->type == AZONE_AREA) {
-                                       area_draw_azone(az->x1, az->y1, az->x2, az->y2);
-                               }
-                               else if (az->type == AZONE_REGION) {
-                                       
-                                       if (az->ar) {
-                                               /* only display tab or icons when the region is hidden */
-                                               if (az->ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
-                                                       if (G.debug_value == 3)
-                                                               region_draw_azone_icon(az);
-                                                       else if (G.debug_value == 2)
-                                                               region_draw_azone_tria(az);
-                                                       else if (G.debug_value == 1)
-                                                               region_draw_azone_tab(az);
-                                                       else
-                                                               region_draw_azone_tab_plus(az);
-                                               }
+       for (az = sa->actionzones.first; az; az = az->next) {
+               /* test if action zone is over this region */
+               rcti azrct;
+               BLI_rcti_init(&azrct, az->x1, az->x2, az->y1, az->y2);
+
+               if (BLI_rcti_isect(&ar->drawrct, &azrct, NULL)) {
+                       if (az->type == AZONE_AREA) {
+                               area_draw_azone(az->x1, az->y1, az->x2, az->y2);
+                       }
+                       else if (az->type == AZONE_REGION) {
+                               
+                               if (az->ar) {
+                                       /* only display tab or icons when the region is hidden */
+                                       if (az->ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
+                                               if (G.debug_value == 3)
+                                                       region_draw_azone_icon(az);
+                                               else if (G.debug_value == 2)
+                                                       region_draw_azone_tria(az);
+                                               else if (G.debug_value == 1)
+                                                       region_draw_azone_tab(az);
+                                               else
+                                                       region_draw_azone_tab_plus(az);
                                        }
                                }
-                               
-                               az->do_draw = FALSE;
                        }
                }
        }
-       glDisable(GL_BLEND);
-       
-}
 
-/* get scissor rect, checking overlapping regions */
-void region_scissor_winrct(ARegion *ar, rcti *winrct)
-{
-       *winrct = ar->winrct;
-       
-       if (ELEM(ar->alignment, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT))
-               return;
+       glPopMatrix();
 
-       while (ar->prev) {
-               ar = ar->prev;
-               
-               if (BLI_rcti_isect(winrct, &ar->winrct, NULL)) {
-                       if (ar->flag & RGN_FLAG_HIDDEN) {
-                               /* pass */
-                       }
-                       else if (ar->alignment & RGN_SPLIT_PREV) {
-                               /* pass */
-                       }
-                       else if (ar->alignment == RGN_OVERLAP_LEFT) {
-                               winrct->xmin = ar->winrct.xmax + 1;
-                       }
-                       else if (ar->alignment == RGN_OVERLAP_RIGHT) {
-                               winrct->xmax = ar->winrct.xmin - 1;
-                       }
-                       else break;
-               }
-       }
+       glDisable(GL_BLEND);
 }
 
 /* only exported for WM */
@@ -430,15 +385,11 @@ void ED_region_set(const bContext *C, ARegion *ar)
 {
        wmWindow *win = CTX_wm_window(C);
        ScrArea *sa = CTX_wm_area(C);
-       rcti winrct;
-       
-       /* checks other overlapping regions */
-       region_scissor_winrct(ar, &winrct);
        
-       ar->drawrct = winrct;
+       ar->drawrct = ar->winrct;
        
        /* note; this sets state, so we can use wmOrtho and friends */
-       wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
+       wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct, true);
        
        UI_SetTheme(sa ? sa->spacetype : 0, ar->type ? ar->type->regionid : 0);
        
@@ -452,30 +403,29 @@ void ED_region_do_draw(bContext *C, ARegion *ar)
        wmWindow *win = CTX_wm_window(C);
        ScrArea *sa = CTX_wm_area(C);
        ARegionType *at = ar->type;
-       rcti winrct;
-       
+       bool scissor_pad;
+
        /* see BKE_spacedata_draw_locks() */
        if (at->do_lock)
                return;
        
-       /* checks other overlapping regions */
-       region_scissor_winrct(ar, &winrct);
-       
        /* if no partial draw rect set, full rect */
-       if (ar->drawrct.xmin == ar->drawrct.xmax)
-               ar->drawrct = winrct;
+       if (ar->drawrct.xmin == ar->drawrct.xmax) {
+               ar->drawrct = ar->winrct;
+               scissor_pad = true;
+       }
        else {
                /* extra clip for safety */
-               ar->drawrct.xmin = MAX2(winrct.xmin, ar->drawrct.xmin);
-               ar->drawrct.ymin = MAX2(winrct.ymin, ar->drawrct.ymin);
-               ar->drawrct.xmax = MIN2(winrct.xmax, ar->drawrct.xmax);
-               ar->drawrct.ymax = MIN2(winrct.ymax, ar->drawrct.ymax);
+               BLI_rcti_isect(&ar->winrct, &ar->drawrct, &ar->drawrct);
+               scissor_pad = false;
        }
+
+       ar->do_draw |= RGN_DRAWING;
        
        /* note; this sets state, so we can use wmOrtho and friends */
-       wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct);
+       wmSubWindowScissorSet(win, ar->swinid, &ar->drawrct, scissor_pad);
        
-       UI_SetTheme(sa ? sa->spacetype : 0, ar->type ? ar->type->regionid : 0);
+       UI_SetTheme(sa ? sa->spacetype : 0, at->regionid);
        
        /* optional header info instead? */
        if (ar->headerstr) {
@@ -483,7 +433,7 @@ void ED_region_do_draw(bContext *C, ARegion *ar)
                glClear(GL_COLOR_BUFFER_BIT);
                
                UI_ThemeColor(TH_TEXT);
-               BLF_draw_default(20, 8, 0.0f, ar->headerstr, BLF_DRAW_STR_DUMMY_MAX);
+               BLF_draw_default(UI_UNIT_X, 0.4f * UI_UNIT_Y, 0.0f, ar->headerstr, BLF_DRAW_STR_DUMMY_MAX);
        }
        else if (at->draw) {
                at->draw(C, ar);
@@ -494,13 +444,24 @@ void ED_region_do_draw(bContext *C, ARegion *ar)
 
        ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
 
-       ar->do_draw = FALSE;
+       region_draw_azones(sa, ar);
+
+       /* for debugging unneeded area redraws and partial redraw */
+#if 0
+       glEnable(GL_BLEND);
+       glColor4f(drand48(), drand48(), drand48(), 0.1f);
+       glRectf(ar->drawrct.xmin - ar->winrct.xmin, ar->drawrct.ymin - ar->winrct.ymin,
+               ar->drawrct.xmax - ar->winrct.xmin, ar->drawrct.ymax - ar->winrct.ymin);
+       glDisable(GL_BLEND);
+#endif
+
+       ar->do_draw = 0;
        memset(&ar->drawrct, 0, sizeof(ar->drawrct));
        
        uiFreeInactiveBlocks(C, &ar->uiblocks);
 
        if (sa)
-               region_draw_emboss(ar, &winrct);
+               region_draw_emboss(ar, &ar->winrct);
 }
 
 /* **********************************
@@ -510,7 +471,9 @@ void ED_region_do_draw(bContext *C, ARegion *ar)
 
 void ED_region_tag_redraw(ARegion *ar)
 {
-       if (ar) {
+       /* don't tag redraw while drawing, it shouldn't happen normally
+        * but python scripts can cause this to happen indirectly */
+       if (ar && !(ar->do_draw & RGN_DRAWING)) {
                /* zero region means full region redraw */
                ar->do_draw = RGN_DRAW;
                memset(&ar->drawrct, 0, sizeof(ar->drawrct));
@@ -525,7 +488,7 @@ void ED_region_tag_redraw_overlay(ARegion *ar)
 
 void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
 {
-       if (ar) {
+       if (ar && !(ar->do_draw & RGN_DRAWING)) {
                if (!ar->do_draw) {
                        /* no redraw set yet, set partial region */
                        ar->do_draw = RGN_DRAW_PARTIAL;
@@ -533,10 +496,7 @@ void ED_region_tag_redraw_partial(ARegion *ar, rcti *rct)
                }
                else if (ar->drawrct.xmin != ar->drawrct.xmax) {
                        /* partial redraw already set, expand region */
-                       ar->drawrct.xmin = MIN2(ar->drawrct.xmin, rct->xmin);
-                       ar->drawrct.ymin = MIN2(ar->drawrct.ymin, rct->ymin);
-                       ar->drawrct.xmax = MAX2(ar->drawrct.xmax, rct->xmax);
-                       ar->drawrct.ymax = MAX2(ar->drawrct.ymax, rct->ymax);
+                       BLI_rcti_union(&ar->drawrct, rct);
                }
        }
 }
@@ -614,8 +574,8 @@ static void area_azone_initialize(bScreen *screen, ScrArea *sa)
        az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
        BLI_addtail(&(sa->actionzones), az);
        az->type = AZONE_AREA;
-       az->x1 = sa->totrct.xmin - 1;
-       az->y1 = sa->totrct.ymin - 1;
+       az->x1 = sa->totrct.xmin;
+       az->y1 = sa->totrct.ymin;
        az->x2 = sa->totrct.xmin + (AZONESPOT - 1);
        az->y2 = sa->totrct.ymin + (AZONESPOT - 1);
        BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
@@ -623,15 +583,15 @@ static void area_azone_initialize(bScreen *screen, ScrArea *sa)
        az = (AZone *)MEM_callocN(sizeof(AZone), "actionzone");
        BLI_addtail(&(sa->actionzones), az);
        az->type = AZONE_AREA;
-       az->x1 = sa->totrct.xmax + 1;
-       az->y1 = sa->totrct.ymax + 1;
+       az->x1 = sa->totrct.xmax;
+       az->y1 = sa->totrct.ymax;
        az->x2 = sa->totrct.xmax - (AZONESPOT - 1);
        az->y2 = sa->totrct.ymax - (AZONESPOT - 1);
        BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
 }
 
-#define AZONEPAD_EDGE   4
-#define AZONEPAD_ICON   9
+#define AZONEPAD_EDGE   (0.1f * U.widget_unit)
+#define AZONEPAD_ICON   (0.45f * U.widget_unit)
 static void region_azone_edge(AZone *az, ARegion *ar)
 {
        switch (az->edge) {
@@ -639,22 +599,22 @@ static void region_azone_edge(AZone *az, ARegion *ar)
                        az->x1 = ar->winrct.xmin;
                        az->y1 = ar->winrct.ymax - AZONEPAD_EDGE;
                        az->x2 = ar->winrct.xmax;
-                       az->y2 = ar->winrct.ymax;
+                       az->y2 = ar->winrct.ymax + AZONEPAD_EDGE;
                        break;
                case AE_BOTTOM_TO_TOPLEFT:
                        az->x1 = ar->winrct.xmin;
                        az->y1 = ar->winrct.ymin + AZONEPAD_EDGE;
                        az->x2 = ar->winrct.xmax;
-                       az->y2 = ar->winrct.ymin;
+                       az->y2 = ar->winrct.ymin - AZONEPAD_EDGE;
                        break;
                case AE_LEFT_TO_TOPRIGHT:
-                       az->x1 = ar->winrct.xmin;
+                       az->x1 = ar->winrct.xmin - AZONEPAD_EDGE;
                        az->y1 = ar->winrct.ymin;
                        az->x2 = ar->winrct.xmin + AZONEPAD_EDGE;
                        az->y2 = ar->winrct.ymax;
                        break;
                case AE_RIGHT_TO_TOPLEFT:
-                       az->x1 = ar->winrct.xmax;
+                       az->x1 = ar->winrct.xmax + AZONEPAD_EDGE;
                        az->y1 = ar->winrct.ymin;
                        az->x2 = ar->winrct.xmax - AZONEPAD_EDGE;
                        az->y2 = ar->winrct.ymax;
@@ -723,8 +683,8 @@ static void region_azone_icon(ScrArea *sa, AZone *az, ARegion *ar)
        }
 }
 
-#define AZONEPAD_TAB_PLUSW  14
-#define AZONEPAD_TAB_PLUSH  14
+#define AZONEPAD_TAB_PLUSW  (0.7f * U.widget_unit)
+#define AZONEPAD_TAB_PLUSH  (0.7f * U.widget_unit)
 
 /* region already made zero sized, in shape of edge */
 static void region_azone_tab_plus(ScrArea *sa, AZone *az, ARegion *ar)
@@ -739,28 +699,28 @@ static void region_azone_tab_plus(ScrArea *sa, AZone *az, ARegion *ar)
        switch (az->edge) {
                case AE_TOP_TO_BOTTOMRIGHT:
                        if (ar->winrct.ymax == sa->totrct.ymin) add = 1; else add = 0;
-                       az->x1 = ar->winrct.xmax - 2.5 * AZONEPAD_TAB_PLUSW;
+                       az->x1 = ar->winrct.xmax - 2.5f * AZONEPAD_TAB_PLUSW;
                        az->y1 = ar->winrct.ymax - add;
-                       az->x2 = ar->winrct.xmax - 1.5 * AZONEPAD_TAB_PLUSW;
+                       az->x2 = ar->winrct.xmax - 1.5f * AZONEPAD_TAB_PLUSW;
                        az->y2 = ar->winrct.ymax - add + AZONEPAD_TAB_PLUSH;
                        break;
                case AE_BOTTOM_TO_TOPLEFT:
-                       az->x1 = ar->winrct.xmax - 2.5 * AZONEPAD_TAB_PLUSW;
+                       az->x1 = ar->winrct.xmax - 2.5f * AZONEPAD_TAB_PLUSW;
                        az->y1 = ar->winrct.ymin - AZONEPAD_TAB_PLUSH;
-                       az->x2 = ar->winrct.xmax - 1.5 * AZONEPAD_TAB_PLUSW;
+                       az->x2 = ar->winrct.xmax - 1.5f * AZONEPAD_TAB_PLUSW;
                        az->y2 = ar->winrct.ymin;
                        break;
                case AE_LEFT_TO_TOPRIGHT:
                        az->x1 = ar->winrct.xmin - AZONEPAD_TAB_PLUSH;
-                       az->y1 = ar->winrct.ymax - 2.5 * AZONEPAD_TAB_PLUSW;
+                       az->y1 = ar->winrct.ymax - 2.5f * AZONEPAD_TAB_PLUSW;
                        az->x2 = ar->winrct.xmin;
-                       az->y2 = ar->winrct.ymax - 1.5 * AZONEPAD_TAB_PLUSW;
+                       az->y2 = ar->winrct.ymax - 1.5f * AZONEPAD_TAB_PLUSW;
                        break;
                case AE_RIGHT_TO_TOPLEFT:
                        az->x1 = ar->winrct.xmax - 1;
-                       az->y1 = ar->winrct.ymax - 2.5 * AZONEPAD_TAB_PLUSW;
+                       az->y1 = ar->winrct.ymax - 2.5f * AZONEPAD_TAB_PLUSW;
                        az->x2 = ar->winrct.xmax - 1 + AZONEPAD_TAB_PLUSH;
-                       az->y2 = ar->winrct.ymax - 1.5 * AZONEPAD_TAB_PLUSW;
+                       az->y2 = ar->winrct.ymax - 1.5f * AZONEPAD_TAB_PLUSW;
                        break;
        }
        /* rect needed for mouse pointer test */
@@ -768,8 +728,8 @@ static void region_azone_tab_plus(ScrArea *sa, AZone *az, ARegion *ar)
 }      
 
 
-#define AZONEPAD_TABW   18
-#define AZONEPAD_TABH   7
+#define AZONEPAD_TABW   (0.9f * U.widget_unit)
+#define AZONEPAD_TABH   (0.35f * U.widget_unit)
 
 /* region already made zero sized, in shape of edge */
 static void region_azone_tab(ScrArea *sa, AZone *az, ARegion *ar)
@@ -812,8 +772,8 @@ static void region_azone_tab(ScrArea *sa, AZone *az, ARegion *ar)
        BLI_rcti_init(&az->rect, az->x1, az->x2, az->y1, az->y2);
 }      
 
-#define AZONEPAD_TRIAW  16
-#define AZONEPAD_TRIAH  9
+#define AZONEPAD_TRIAW  (0.8f * U.widget_unit)
+#define AZONEPAD_TRIAH  (0.45f * U.widget_unit)
 
 
 /* region already made zero sized, in shape of edge */
@@ -895,9 +855,9 @@ static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
                region_azone_initialize(sa, ar, AE_BOTTOM_TO_TOPLEFT);
        else if (alignment == RGN_ALIGN_BOTTOM)
                region_azone_initialize(sa, ar, AE_TOP_TO_BOTTOMRIGHT);
-       else if (ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
+       else if (alignment == RGN_ALIGN_RIGHT)
                region_azone_initialize(sa, ar, AE_LEFT_TO_TOPRIGHT);
-       else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
+       else if (alignment == RGN_ALIGN_LEFT)
                region_azone_initialize(sa, ar, AE_RIGHT_TO_TOPLEFT);
 }
 
@@ -905,14 +865,73 @@ static void region_azone_add(ScrArea *sa, ARegion *ar, int alignment)
 static int rct_fits(rcti *rect, char dir, int size)
 {
        if (dir == 'h') {
-               return BLI_rcti_size_x(rect) - size;
+               return BLI_rcti_size_x(rect) + 1 - size;
        }
        else {  /* 'v' */
-               return BLI_rcti_size_y(rect) - size;
+               return BLI_rcti_size_y(rect) + 1 - size;
        }
 }
 
-static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
+/* *************************************************************** */
+
+/* ar should be overlapping */
+/* function checks if some overlapping region was defined before - on same place */
+static void region_overlap_fix(ScrArea *sa, ARegion *ar)
+{
+       ARegion *ar1 = ar->prev;
+       
+       /* find overlapping previous region on same place */
+       while (ar1) {
+               if (ar1->overlap) {
+                       if ((ar1->alignment & RGN_SPLIT_PREV) == 0)
+                               if (BLI_rcti_isect(&ar1->winrct, &ar->winrct, NULL))
+                                       break;
+               }
+               ar1 = ar1->prev;
+       }
+       
+       /* translate or close */
+       if (ar1) {
+               int align1 = ar1->alignment & ~RGN_SPLIT_PREV;
+
+               if (align1 == RGN_ALIGN_LEFT) {
+                       if (ar->winrct.xmax + ar1->winx > sa->winx - U.widget_unit)
+                               ar->flag |= RGN_FLAG_TOO_SMALL;
+                       else
+                               BLI_rcti_translate(&ar->winrct, ar1->winx, 0);
+               }
+               else if (align1 == RGN_ALIGN_RIGHT) {
+                       if (ar->winrct.xmin - ar1->winx < U.widget_unit)
+                               ar->flag |= RGN_FLAG_TOO_SMALL;
+                       else
+                               BLI_rcti_translate(&ar->winrct, -ar1->winx, 0);
+               }
+       }
+
+       
+       
+}
+
+/* overlapping regions only in the following restricted cases */
+static int region_is_overlap(wmWindow *win, ScrArea *sa, ARegion *ar)
+{
+       if (U.uiflag2 & USER_REGION_OVERLAP) {
+               if (WM_is_draw_triple(win)) {
+                       if (ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ)) {
+                               if (ELEM3(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI, RGN_TYPE_TOOL_PROPS))
+                                       return 1;
+                       }
+                       else if (sa->spacetype == SPACE_IMAGE) {
+                               if (ELEM4(ar->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_UI, RGN_TYPE_TOOL_PROPS, RGN_TYPE_PREVIEW))
+                                       return 1;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+static void region_rect_recursive(wmWindow *win, ScrArea *sa, ARegion *ar, rcti *remainder, int quad)
 {
        rcti *remainder_prev = remainder;
        int prefsizex, prefsizey;
@@ -931,22 +950,26 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
        
        alignment = ar->alignment & ~RGN_SPLIT_PREV;
        
+       /* set here, assuming userpref switching forces to call this again */
+       ar->overlap = region_is_overlap(win, sa, ar);
+
        /* clear state flags first */
        ar->flag &= ~RGN_FLAG_TOO_SMALL;
        /* user errors */
        if (ar->next == NULL && alignment != RGN_ALIGN_QSPLIT)
                alignment = RGN_ALIGN_NONE;
        
-       /* prefsize, for header we stick to exception */
-       prefsizex = ar->sizex ? ar->sizex : ar->type->prefsizex;
+       /* prefsize, for header we stick to exception (prevent dpi rounding error) */
+       prefsizex = UI_DPI_FAC * (ar->sizex > 1 ? ar->sizex + 0.5f : ar->type->prefsizex);
+       
        if (ar->regiontype == RGN_TYPE_HEADER) {
-               prefsizey = ar->type->prefsizey;
+               prefsizey = ED_area_headersize();
        }
        else if (ar->regiontype == RGN_TYPE_UI && sa->spacetype == SPACE_FILE) {
                prefsizey = UI_UNIT_Y * 2 + (UI_UNIT_Y / 2);
        }
        else {
-               prefsizey = ar->sizey ? ar->sizey : ar->type->prefsizey;
+               prefsizey = UI_DPI_FAC * (ar->sizey > 1 ? ar->sizey + 0.5f : ar->type->prefsizey);
        }
 
 
@@ -988,7 +1011,7 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
                        }
                }
        }
-       else if (ELEM4(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT, RGN_OVERLAP_LEFT, RGN_OVERLAP_RIGHT)) {
+       else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
                
                if (rct_fits(remainder, 'h', prefsizex) < 0) {
                        ar->flag |= RGN_FLAG_TOO_SMALL;
@@ -1001,14 +1024,14 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
                        
                        ar->winrct = *remainder;
                        
-                       if (ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT)) {
+                       if (alignment == RGN_ALIGN_RIGHT) {
                                ar->winrct.xmin = ar->winrct.xmax - prefsizex + 1;
-                               if (alignment == RGN_ALIGN_RIGHT)
+                               if (ar->overlap == 0)
                                        remainder->xmax = ar->winrct.xmin - 1;
                        }
                        else {
                                ar->winrct.xmax = ar->winrct.xmin + prefsizex - 1;
-                               if (alignment == RGN_ALIGN_LEFT)
+                               if (ar->overlap == 0)
                                        remainder->xmin = ar->winrct.xmax + 1;
                        }
                }
@@ -1056,7 +1079,9 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
                                if (G.debug & G_DEBUG)
                                        printf("region quadsplit failed\n");
                        }
-                       else quad = 1;
+                       else {
+                               quad = 1;
+                       }
                }
                if (quad) {
                        if (quad == 1) { /* left bottom */
@@ -1085,6 +1110,15 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
        ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
        ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
        
+       /* if region opened normally, we store this for hide/reveal usage */
+       /* prevent rounding errors for UI_DPI_FAC mult and divide */
+       if (ar->winx > 1) ar->sizex = (ar->winx + 0.5f) /  UI_DPI_FAC;
+       if (ar->winy > 1) ar->sizey = (ar->winy + 0.5f) /  UI_DPI_FAC;
+               
+       /* exception for multiple overlapping regions on same spot */
+       if (ar->overlap)
+               region_overlap_fix(sa, ar);
+
        /* set winrect for azones */
        if (ar->flag & (RGN_FLAG_HIDDEN | RGN_FLAG_TOO_SMALL)) {
                ar->winrct = *remainder;
@@ -1093,9 +1127,9 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
                        ar->winrct.ymin = ar->winrct.ymax;
                else if (alignment == RGN_ALIGN_BOTTOM)
                        ar->winrct.ymax = ar->winrct.ymin;
-               else if (ELEM(alignment, RGN_ALIGN_RIGHT, RGN_OVERLAP_RIGHT))
+               else if (alignment == RGN_ALIGN_RIGHT)
                        ar->winrct.xmin = ar->winrct.xmax;
-               else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_OVERLAP_LEFT))
+               else if (alignment == RGN_ALIGN_LEFT)
                        ar->winrct.xmax = ar->winrct.xmin;
                else /* prevent winrct to be valid */
                        ar->winrct.xmax = ar->winrct.xmin;
@@ -1124,21 +1158,21 @@ static void region_rect_recursive(ScrArea *sa, ARegion *ar, rcti *remainder, int
                region_azone_add(sa, ar, alignment);
        }
 
-       region_rect_recursive(sa, ar->next, remainder, quad);
+       region_rect_recursive(win, sa, ar->next, remainder, quad);
 }
 
 static void area_calc_totrct(ScrArea *sa, int sizex, int sizey)
 {
-       short rt = 0; // CLAMPIS(G.debug_value, 0, 16);
+       short rt = (short) U.pixelsize;
 
-       if (sa->v1->vec.x > 0) sa->totrct.xmin = sa->v1->vec.x + 1 + rt;
+       if (sa->v1->vec.x > 0) sa->totrct.xmin = sa->v1->vec.x + rt;
        else sa->totrct.xmin = sa->v1->vec.x;
-       if (sa->v4->vec.x < sizex - 1) sa->totrct.xmax = sa->v4->vec.x - 1 - rt;
+       if (sa->v4->vec.x < sizex - 1) sa->totrct.xmax = sa->v4->vec.x - rt;
        else sa->totrct.xmax = sa->v4->vec.x;
        
-       if (sa->v1->vec.y > 0) sa->totrct.ymin = sa->v1->vec.y + 1 + rt;
+       if (sa->v1->vec.y > 0) sa->totrct.ymin = sa->v1->vec.y + rt;
        else sa->totrct.ymin = sa->v1->vec.y;
-       if (sa->v2->vec.y < sizey - 1) sa->totrct.ymax = sa->v2->vec.y - 1 - rt;
+       if (sa->v2->vec.y < sizey - 1) sa->totrct.ymax = sa->v2->vec.y - rt;
        else sa->totrct.ymax = sa->v2->vec.y;
        
        /* for speedup */
@@ -1232,14 +1266,14 @@ void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
                ar->type = BKE_regiontype_from_id(sa->type, ar->regiontype);
        
        /* area sizes */
-       area_calc_totrct(sa, win->sizex, win->sizey);
+       area_calc_totrct(sa, WM_window_pixels_x(win), WM_window_pixels_y(win));
        
        /* clear all azones, add the area triange widgets */
        area_azone_initialize(win->screen, sa);
 
        /* region rect sizes */
        rect = sa->totrct;
-       region_rect_recursive(sa, sa->regionbase.first, &rect, 0);
+       region_rect_recursive(win, sa, sa->regionbase.first, &rect, 0);
        
        /* default area handlers */
        ed_default_handlers(wm, sa, &sa->handlers, sa->type->keymapflag);
@@ -1262,17 +1296,13 @@ void ED_area_initialize(wmWindowManager *wm, wmWindow *win, ScrArea *sa)
                        /* prevent uiblocks to run */
                        uiFreeBlocks(NULL, &ar->uiblocks);
                }
-               
-               /* rechecks 2d matrix for header on dpi changing, do not do for other regions, it resets view && blocks view2d operator polls (ton) */
-               if (ar->regiontype == RGN_TYPE_HEADER)
-                       ar->v2d.flag &= ~V2D_IS_INITIALISED;
        }
 }
 
 /* externally called for floating regions like menus */
 void ED_region_init(bContext *C, ARegion *ar)
 {
-//     ARegionType *at= ar->type;
+//     ARegionType *at = ar->type;
        
        /* refresh can be called before window opened */
        region_subwindow(CTX_wm_window(C), ar);
@@ -1280,22 +1310,38 @@ void ED_region_init(bContext *C, ARegion *ar)
        ar->winx = BLI_rcti_size_x(&ar->winrct) + 1;
        ar->winy = BLI_rcti_size_y(&ar->winrct) + 1;
        
+       /* v2d mask is used to subtract scrollbars from a 2d view. Needs initialize here. */
+       BLI_rcti_init(&ar->v2d.mask, 0, ar->winx - 1, 0, ar->winy -1);
+
        /* UI convention */
        wmOrtho2(-0.01f, ar->winx - 0.01f, -0.01f, ar->winy - 0.01f);
        glLoadIdentity();
 }
 
-void ED_region_toggle_hidden(bContext *C, ARegion *ar)
+/* for quick toggle, can skip fades */
+void region_toggle_hidden(bContext *C, ARegion *ar, int do_fade)
 {
        ScrArea *sa = CTX_wm_area(C);
-
+       
        ar->flag ^= RGN_FLAG_HIDDEN;
+       
+       if (do_fade && ar->overlap) {
+               /* starts a timer, and in end calls the stuff below itself (region_sblend_invoke()) */
+               region_blend_start(C, sa, ar);
+       }
+       else {
+               if (ar->flag & RGN_FLAG_HIDDEN)
+                       WM_event_remove_handlers(C, &ar->handlers);
+               
+               ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
+               ED_area_tag_redraw(sa);
+       }
+}
 
-       if (ar->flag & RGN_FLAG_HIDDEN)
-               WM_event_remove_handlers(C, &ar->handlers);
-
-       ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
-       ED_area_tag_redraw(sa);
+/* exported to all editors, uses fading default */
+void ED_region_toggle_hidden(bContext *C, ARegion *ar)
+{
+       region_toggle_hidden(C, ar, 1);
 }
 
 /* sa2 to sa1, we swap spaces for fullscreen to keep all allocated data */
@@ -1308,6 +1354,7 @@ void area_copy_data(ScrArea *sa1, ScrArea *sa2, int swap_space)
        
        sa1->headertype = sa2->headertype;
        sa1->spacetype = sa2->spacetype;
+       sa1->type = sa2->type;
        sa1->butspacetype = sa2->butspacetype;
        
        if (swap_space == 1) {
@@ -1405,7 +1452,6 @@ void ED_area_newspace(bContext *C, ScrArea *sa, int type)
                }
                
                if (sl) {
-                       
                        /* swap regions */
                        slold->regionbase = sa->regionbase;
                        sa->regionbase = sl->regionbase;
@@ -1448,7 +1494,7 @@ void ED_area_prevspace(bContext *C, ScrArea *sa)
 {
        SpaceLink *sl = (sa) ? sa->spacedata.first : CTX_wm_space_data(C);
 
-       if (sl->next) {
+       if (sl && sl->next) {
                /* workaround for case of double prevspace, render window
                 * with a file browser on top of it */
                if (sl->next->spacetype == SPACE_FILE && sl->next->next)
@@ -1457,7 +1503,8 @@ void ED_area_prevspace(bContext *C, ScrArea *sa)
                        ED_area_newspace(C, sa, sl->next->spacetype);
        }
        else {
-               ED_area_newspace(C, sa, SPACE_INFO);
+               /* no change */
+               return;
        }
        ED_area_tag_redraw(sa);
 
@@ -1465,78 +1512,26 @@ void ED_area_prevspace(bContext *C, ScrArea *sa)
        WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, sa);
 }
 
-static const char *editortype_pup(void)
-{
-       const char *types = N_(
-           "Editor type: %t"
-           "|3D View %x1"
-
-           "|%l"
-
-           "|Timeline %x15"
-           "|Graph Editor %x2"
-           "|DopeSheet %x12"
-           "|NLA Editor %x13"
-
-           "|%l"
-
-           "|UV/Image Editor %x6"
-
-           "|Video Sequence Editor %x8"
-           "|Movie Clip Editor %x20"
-           "|Text Editor %x9"
-           "|Node Editor %x16"
-           "|Logic Editor %x17"
-
-           "|%l"
-
-           "|Properties %x4"
-           "|Outliner %x3"
-           "|User Preferences %x19"
-           "|Info %x7"
-
-           "|%l"
-
-           "|File Browser %x5"
-
-           "|%l"
-
-           "|Python Console %x18"
-           );
-
-       return IFACE_(types);
-}
-
-static void spacefunc(struct bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
-{
-       ED_area_newspace(C, CTX_wm_area(C), CTX_wm_area(C)->butspacetype);
-       ED_area_tag_redraw(CTX_wm_area(C));
-
-       /* send space change notifier */
-       WM_event_add_notifier(C, NC_SPACE | ND_SPACE_CHANGED, CTX_wm_area(C));
-}
-
 /* returns offset for next button in header */
 int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
 {
        ScrArea *sa = CTX_wm_area(C);
-       uiBut *but;
-       int xco = 8;
-       
-       but = uiDefIconTextButC(block, ICONTEXTROW, 0, ICON_VIEW3D, 
-                               editortype_pup(), xco, yco, UI_UNIT_X + 10, UI_UNIT_Y,
-                               &(sa->butspacetype), 1.0, SPACEICONMAX, 0, 0,
-                               TIP_("Display current editor type (click for a menu of available types)"));
-       uiButSetFunc(but, spacefunc, NULL, NULL);
-       uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
-       
-       return xco + UI_UNIT_X + 14;
+       bScreen *scr = CTX_wm_screen(C);
+       PointerRNA areaptr;
+       int xco = 0.4 * U.widget_unit;
+
+       RNA_pointer_create(&(scr->id), &RNA_Area, sa, &areaptr);
+
+       uiDefButR(block, MENU, 0, NULL, xco, yco, 1.5 * U.widget_unit, U.widget_unit,
+                 &areaptr, "type", 0, 0.0f, 0.0f, 0.0f, 0.0f, "");
+
+       return xco + 1.7 * U.widget_unit;
 }
 
 int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
 {
        ScrArea *sa = CTX_wm_area(C);
-       int xco = 8;
+       int xco = 0.4 * U.widget_unit;
        uiBut *but;
        
        if (!sa->full)
@@ -1547,23 +1542,23 @@ int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
        if (sa->flag & HEADER_NO_PULLDOWN) {
                but = uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
                                       ICON_DISCLOSURE_TRI_RIGHT,
-                                      xco, yco, UI_UNIT_X, UI_UNIT_Y - 2,
+                                      xco, yco, U.widget_unit, U.widget_unit * 0.9f,
                                       &(sa->flag), 0, 0, 0, 0,
-                                      "Show pulldown menus");
+                                      TIP_("Show pulldown menus"));
        }
        else {
                but = uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, 0,
                                       ICON_DISCLOSURE_TRI_DOWN,
-                                      xco, yco, UI_UNIT_X, UI_UNIT_Y - 2,
+                                      xco, yco, U.widget_unit, U.widget_unit * 0.9f,
                                       &(sa->flag), 0, 0, 0, 0,
-                                      "Hide pulldown menus");
+                                      TIP_("Hide pulldown menus"));
        }
 
        uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
 
        uiBlockSetEmboss(block, UI_EMBOSS);
        
-       return xco + UI_UNIT_X;
+       return xco + U.widget_unit;
 }
 
 /************************ standard UI regions ************************/
@@ -1571,42 +1566,124 @@ int ED_area_header_standardbuttons(const bContext *C, uiBlock *block, int yco)
 void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *context, int contextnr)
 {
        ScrArea *sa = CTX_wm_area(C);
-       uiStyle *style = UI_GetStyle();
+       uiStyle *style = UI_GetStyleDraw();
        uiBlock *block;
        PanelType *pt;
        Panel *panel;
        View2D *v2d = &ar->v2d;
        View2DScrollers *scrollers;
-       int x, y, xco, yco, w, em, triangle, open, newcontext = 0;
+       int x, y, xco, yco, w, em, triangle;
+       bool is_context_new = 0;
+       int redo;
+       int scroll;
 
-       if (contextnr >= 0)
-               newcontext = UI_view2d_tab_set(v2d, contextnr);
+       bool use_category_tabs = (ar->regiontype == RGN_TYPE_TOOLS);  /* XXX, should use some better check? */
+       /* offset panels for small vertical tab area */
+       const char *category = NULL;
+       const int category_tabs_width = UI_PANEL_CATEGORY_MARGIN_WIDTH;
+       int margin_x = 0;
+
+       BLI_SMALLSTACK_DECLARE(pt_stack, PanelType *);
 
+       if (contextnr >= 0)
+               is_context_new = UI_view2d_tab_set(v2d, contextnr);
+       
+       /* before setting the view */
        if (vertical) {
-               w = BLI_rctf_size_x(&v2d->cur);
-               em = (ar->type->prefsizex) ? UI_UNIT_Y / 2 : UI_UNIT_Y;
+               /* only allow scrolling in vertical direction */
+               v2d->keepofs |= V2D_LOCKOFS_X | V2D_KEEPOFS_Y;
+               v2d->keepofs &= ~(V2D_LOCKOFS_Y | V2D_KEEPOFS_X);
+               v2d->scroll &= ~(V2D_SCROLL_BOTTOM);
+               v2d->scroll |= (V2D_SCROLL_RIGHT);
        }
        else {
-               w = UI_PANEL_WIDTH;
-               em = (ar->type->prefsizex) ? UI_UNIT_Y / 2 : UI_UNIT_Y;
+               /* for now, allow scrolling in both directions (since layouts are optimized for vertical,
+                * they often don't fit in horizontal layout)
+                */
+               v2d->keepofs &= ~(V2D_LOCKOFS_X | V2D_LOCKOFS_Y | V2D_KEEPOFS_X | V2D_KEEPOFS_Y);
+               v2d->scroll |= (V2D_SCROLL_BOTTOM);
+               v2d->scroll &= ~(V2D_SCROLL_RIGHT);
        }
 
-       /* create panels */
-       uiBeginPanels(C, ar);
+       scroll = v2d->scroll;
 
-       /* set view2d view matrix for scrolling (without scrollers) */
-       UI_view2d_view_ortho(v2d);
 
-       for (pt = ar->type->paneltypes.first; pt; pt = pt->next) {
+       /* collect panels to draw */
+       for (pt = ar->type->paneltypes.last; pt; pt = pt->prev) {
                /* verify context */
-               if (context)
-                       if (pt->context[0] && strcmp(context, pt->context) != 0)
-                               continue;
+               if (context && pt->context[0] && !STREQ(context, pt->context)) {
+                       continue;
+               }
 
                /* draw panel */
                if (pt->draw && (!pt->poll || pt->poll(C, pt))) {
+                       BLI_SMALLSTACK_PUSH(pt_stack, pt);
+               }
+       }
+
+
+       /* collect categories */
+       if (use_category_tabs) {
+               UI_panel_category_clear_all(ar);
+
+               /* gather unique categories */
+               BLI_SMALLSTACK_ITER_BEGIN(pt_stack, pt)
+               {
+                       if (pt->category[0]) {
+                               if (!UI_panel_category_find(ar, pt->category)) {
+                                       UI_panel_category_add(ar, pt->category);
+                               }
+                       }
+               }
+               BLI_SMALLSTACK_ITER_END;
+
+               if (!UI_panel_category_is_visible(ar)) {
+                       use_category_tabs = false;
+               }
+               else {
+                       category = UI_panel_category_active_get(ar, true);
+                       margin_x = category_tabs_width;
+               }
+       }
+
+
+       /* sortof hack - but we cannot predict the height of panels, until it's being generated */
+       /* the layout engine works with fixed width (from v2d->cur), which is being set at end of the loop */
+       /* in case scroller settings (hide flags) differ from previous, the whole loop gets done again */
+       for (redo = 2; redo > 0; redo--) {
+               
+               if (vertical) {
+                       w = BLI_rctf_size_x(&v2d->cur);
+                       em = (ar->type->prefsizex) ? 10 : 20; /* works out to 10*UI_UNIT_X or 20*UI_UNIT_X */
+               }
+               else {
+                       w = UI_PANEL_WIDTH;
+                       em = (ar->type->prefsizex) ? 10 : 20;
+               }
+
+               w -= margin_x;
+               
+               /* create panels */
+               uiBeginPanels(C, ar);
+
+               /* set view2d view matrix  - uiBeginBlock() stores it */
+               UI_view2d_view_ortho(v2d);
+
+               BLI_SMALLSTACK_ITER_BEGIN(pt_stack, pt)
+               {
+                       bool open;
+
+                       panel = uiPanelFindByType(ar, pt);
+
+                       if (use_category_tabs && pt->category[0] && !STREQ(category, pt->category)) {
+                               if ((panel == NULL) || ((panel->flag & PNL_PIN) == 0)) {
+                                       continue;
+                               }
+                       }
+
+                       /* draw panel */
                        block = uiBeginBlock(C, ar, pt->idname, UI_EMBOSS);
-                       panel = uiBeginPanel(sa, ar, block, pt, &open);
+                       panel = uiBeginPanel(sa, ar, block, pt, panel, &open);
 
                        /* bad fixed values */
                        triangle = (int)(UI_UNIT_Y * 1.1f);
@@ -1614,7 +1691,7 @@ void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *
                        if (pt->draw_header && !(pt->flag & PNL_NO_HEADER) && (open || vertical)) {
                                /* for enabled buttons */
                                panel->layout = uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER,
-                                                             triangle, UI_UNIT_Y + style->panelspace + 2, UI_UNIT_Y, 1, style);
+                                                             triangle, (UI_UNIT_Y * 1.1f) + style->panelspace, UI_UNIT_Y, 1, style);
 
                                pt->draw_header(C, panel);
 
@@ -1628,13 +1705,13 @@ void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *
 
                        if (open) {
                                short panelContext;
-                               
+
                                /* panel context can either be toolbar region or normal panels region */
                                if (ar->regiontype == RGN_TYPE_TOOLS)
                                        panelContext = UI_LAYOUT_TOOLBAR;
                                else
                                        panelContext = UI_LAYOUT_PANEL;
-                               
+
                                panel->layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, panelContext,
                                                              style->panelspace, 0, w - 2 * style->panelspace, em, style);
 
@@ -1653,47 +1730,59 @@ void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *
 
                        uiEndBlock(C, block);
                }
-       }
-
-       /* align panels and return size */
-       uiEndPanels(C, ar, &x, &y);
+               BLI_SMALLSTACK_ITER_END;
 
-       /* clear */
-       UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
-       glClear(GL_COLOR_BUFFER_BIT);
-       
-       /* before setting the view */
-       if (vertical) {
-               /* only allow scrolling in vertical direction */
-               v2d->keepofs |= V2D_LOCKOFS_X | V2D_KEEPOFS_Y;
-               v2d->keepofs &= ~(V2D_LOCKOFS_Y | V2D_KEEPOFS_X);
-               v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
-               v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
+               /* align panels and return size */
+               uiEndPanels(C, ar, &x, &y);
                
-               /* don't jump back when panels close or hide */
-               if (!newcontext)
-                       y = MAX2(-y, -v2d->cur.ymin);
-               else
+               /* before setting the view */
+               if (vertical) {
+                       /* we always keep the scroll offset - so the total view gets increased with the scrolled away part */
+                       if (v2d->cur.ymax < - 0.001f)
+                               y = min_ii(y, v2d->cur.ymin);
+                       
+                       y = -y;
+               }
+               else {
+                       /* don't jump back when panels close or hide */
+                       if (!is_context_new)
+                               x = max_ii(x, v2d->cur.xmax);
                        y = -y;
+               }
+               
+               /* this also changes the 'cur' */
+               UI_view2d_totRect_set(v2d, x, y);
+               
+               if (scroll != v2d->scroll) {
+                       /* Note: this code scales fine, but because of rounding differences, positions of elements
+                        * flip +1 or -1 pixel compared to redoing the entire layout again.
+                        * Leaving in commented code for future tests */
+#if 0
+                       uiScalePanels(ar, BLI_rctf_size_x(&v2d->cur));
+                       break;
+#endif
+               }
+               else {
+                       break;
+               }
+       }
+       
+       BLI_SMALLSTACK_FREE(pt_stack);
+       
+       /* clear */
+       if (ar->overlap) {
+               /* view should be in pixelspace */
+               UI_view2d_view_restore(C);
+               glEnable(GL_BLEND);
+               UI_ThemeColor4((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
+               glRecti(0, 0, BLI_rcti_size_x(&ar->winrct), BLI_rcti_size_y(&ar->winrct));
+               glDisable(GL_BLEND);
        }
        else {
-               /* for now, allow scrolling in both directions (since layouts are optimized for vertical,
-                * they often don't fit in horizontal layout)
-                */
-               v2d->keepofs &= ~(V2D_LOCKOFS_X | V2D_LOCKOFS_Y | V2D_KEEPOFS_X | V2D_KEEPOFS_Y);
-               //v2d->keepofs |= V2D_LOCKOFS_Y|V2D_KEEPOFS_X;
-               //v2d->keepofs &= ~(V2D_LOCKOFS_X|V2D_KEEPOFS_Y);
-               v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
-               v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_HIDE;
-
-               /* don't jump back when panels close or hide */
-               if (!newcontext)
-                       x = MAX2(x, v2d->cur.xmax);
-               y = -y;
+               UI_ThemeClearColor((ar->type->regionid == RGN_TYPE_PREVIEW) ? TH_PREVIEW_BACK : TH_BACK);
+               glClear(GL_COLOR_BUFFER_BIT);
        }
-
-       /* +V2D_SCROLL_HEIGHT is workaround to set the actual height */
-       UI_view2d_totRect_set(v2d, x + V2D_SCROLL_WIDTH, y + V2D_SCROLL_HEIGHT);
+       
 
        /* set the view */
        UI_view2d_view_ortho(v2d);
@@ -1704,6 +1793,10 @@ void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *
        /* restore view matrix */
        UI_view2d_view_restore(C);
        
+       if (use_category_tabs) {
+               UI_panel_category_draw_all(ar, category);
+       }
+
        /* scrollers */
        scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
        UI_view2d_scrollers_draw(C, v2d, scrollers);
@@ -1713,17 +1806,6 @@ void ED_region_panels(const bContext *C, ARegion *ar, int vertical, const char *
 void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
 {
        wmKeyMap *keymap;
-       
-       /* XXX quick hacks for files saved with 2.5 already (i.e. the builtin defaults file)
-        * scrollbars for button regions */
-       ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
-       ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
-       ar->v2d.scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
-       ar->v2d.keepzoom |= V2D_KEEPZOOM;
-
-       /* correctly initialized User-Prefs? */
-       if (!(ar->v2d.align & V2D_ALIGN_NO_POS_Y))
-               ar->v2d.flag &= ~V2D_IS_INITIALISED;
 
        UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
 
@@ -1733,7 +1815,7 @@ void ED_region_panels_init(wmWindowManager *wm, ARegion *ar)
 
 void ED_region_header(const bContext *C, ARegion *ar)
 {
-       uiStyle *style = UI_GetStyle();
+       uiStyle *style = UI_GetStyleDraw();
        uiBlock *block;
        uiLayout *layout;
        HeaderType *ht;
@@ -1748,8 +1830,8 @@ void ED_region_header(const bContext *C, ARegion *ar)
        /* set view2d view matrix for scrolling (without scrollers) */
        UI_view2d_view_ortho(&ar->v2d);
 
-       xco = maxco = 8;
-       yco = headery - 4;
+       xco = maxco = 0.4f * UI_UNIT_X;
+       yco = headery - floor(0.2f * UI_UNIT_Y);
 
        /* draw all headers types */
        for (ht = ar->type->headertypes.first; ht; ht = ht->next) {
@@ -1778,7 +1860,7 @@ void ED_region_header(const bContext *C, ARegion *ar)
        }
 
        /* always as last  */
-       UI_view2d_totRect_set(&ar->v2d, maxco + UI_UNIT_X + 80, BLI_rctf_size_y(&ar->v2d.tot));
+       UI_view2d_totRect_set(&ar->v2d, maxco + UI_UNIT_X + 80, headery);
 
        /* restore view matrix? */
        UI_view2d_view_restore(C);
@@ -1792,42 +1874,50 @@ void ED_region_header_init(ARegion *ar)
 /* UI_UNIT_Y is defined as U variable now, depending dpi */
 int ED_area_headersize(void)
 {
-       return UI_UNIT_Y + 6;
+       return (int)(HEADERY * UI_DPI_FAC);
 }
 
-void ED_region_info_draw(ARegion *ar, const char *text, int block, float alpha)
+void ED_region_info_draw(ARegion *ar, const char *text, int block, float fill_color[4])
 {
-       const int header_height = 18;
-       uiStyle *style = UI_GetStyle();
+       const int header_height = UI_UNIT_Y;
+       uiStyle *style = UI_GetStyleDraw();
        int fontid = style->widget.uifont_id;
+       GLint scissor[4];
        rcti rect;
 
-       BLF_size(fontid, 11.0f, 72);
-
        /* background box */
-       rect = ar->winrct;
-       rect.xmin = 0;
+       ED_region_visible_rect(ar, &rect);
        rect.ymin = BLI_rcti_size_y(&ar->winrct) - header_height;
 
-       if (block) {
-               rect.xmax = BLI_rcti_size_x(&ar->winrct);
-       }
-       else {
-               rect.xmax = rect.xmin + BLF_width(fontid, text) + 24;
-       }
+       /* box fill entire width or just around text */
+       if (!block)
+               rect.xmax = min_ii(rect.xmax, rect.xmin + BLF_width(fontid, text, BLF_DRAW_STR_DUMMY_MAX) + 1.2f * U.widget_unit);
 
        rect.ymax = BLI_rcti_size_y(&ar->winrct);
 
+       /* setup scissor */
+       glGetIntegerv(GL_SCISSOR_BOX, scissor);
+       glScissor(ar->winrct.xmin + rect.xmin, ar->winrct.ymin + rect.ymin,
+                 BLI_rcti_size_x(&rect) + 1, BLI_rcti_size_y(&rect) + 1);
+
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glColor4f(0.0f, 0.0f, 0.0f, alpha);
+       glColor4fv(fill_color);
        glRecti(rect.xmin, rect.ymin, rect.xmax + 1, rect.ymax + 1);
        glDisable(GL_BLEND);
 
        /* text */
        UI_ThemeColor(TH_TEXT_HI);
-       BLF_position(fontid, 12, rect.ymin + 5, 0.0f);
+       BLF_clipping(fontid, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+       BLF_enable(fontid, BLF_CLIPPING);
+       BLF_position(fontid, rect.xmin + 0.6f * U.widget_unit, rect.ymin + 0.3f * U.widget_unit, 0.0f);
+
        BLF_draw(fontid, text, BLF_DRAW_STR_DUMMY_MAX);
+
+       BLF_disable(fontid, BLF_CLIPPING);
+
+       /* restore scissor as it was before */
+       glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
 }
 
 void ED_region_grid_draw(ARegion *ar, float zoomx, float zoomy)
@@ -1889,3 +1979,34 @@ void ED_region_grid_draw(ARegion *ar, float zoomx, float zoomy)
        }
        glEnd();
 }
+
+/* If the area has overlapping regions, it returns visible rect for Region *ar */
+/* rect gets returned in local region coordinates */
+void ED_region_visible_rect(ARegion *ar, rcti *rect)
+{
+       ARegion *arn = ar;
+       
+       /* allow function to be called without area */
+       while (arn->prev)
+               arn = arn->prev;
+       
+       *rect = ar->winrct;
+       
+       /* check if a region overlaps with the current one */
+       for (; arn; arn = arn->next) {
+               if (ar != arn && arn->overlap) {
+                       if (BLI_rcti_isect(rect, &arn->winrct, NULL)) {
+                               
+                               /* overlap left, also check 1 pixel offset (2 regions on one side) */
+                               if (ABS(rect->xmin - arn->winrct.xmin) < 2)
+                                       rect->xmin = arn->winrct.xmax;
+
+                               /* overlap right */
+                               if (ABS(rect->xmax - arn->winrct.xmax) < 2)
+                                       rect->xmax = arn->winrct.xmin;
+                       }
+               }
+       }
+       BLI_rcti_translate(rect, -ar->winrct.xmin, -ar->winrct.ymin);
+}
+