From c5dc7621226a3234be4280cdc5c6c259e847fa50 Mon Sep 17 00:00:00 2001 From: fvogel Date: Fri, 31 Mar 2017 21:27:49 +0000 Subject: Fix [3089640fff], [6020ee2d03], [e016579efb] and [6bf197edbf]: ttk::notebook tabs can disappear (tab width incorrectly calculated). Patch from Koen Danckaert. --- generic/ttk/ttkNotebook.c | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/generic/ttk/ttkNotebook.c b/generic/ttk/ttkNotebook.c index 81a8b64..ac13d5f 100644 --- a/generic/ttk/ttkNotebook.c +++ b/generic/ttk/ttkNotebook.c @@ -437,16 +437,7 @@ static int NotebookSize(void *clientData, int *widthPtr, int *heightPtr) /* SqueezeTabs -- * Squeeze or stretch tabs to fit within the tab area parcel. * - * All tabs are adjusted by an equal amount, but will not be made - * smaller than the minimum width. (If all the tabs still do - * not fit in the available space, the rightmost ones will - * be further squozen by PlaceTabs()). - * - * The algorithm does not always yield an optimal layout, but does - * have the important property that decreasing the available width - * by one pixel will cause at most one tab to shrink by one pixel; - * this means that tabs resize "smoothly" when the window shrinks - * and grows. + * All tabs are adjusted by an equal amount. * * @@@ <> bug: only works for horizontal orientations * @@@ <> does not account for hidden tabs. @@ -458,25 +449,17 @@ static void SqueezeTabs( int nTabs = Ttk_NumberSlaves(nb->notebook.mgr); if (nTabs > 0) { - int difference = available - needed, - delta = difference / nTabs, - remainder = difference % nTabs, - slack = 0; + int difference = available - needed; + double delta = (double)difference / needed; + double slack = 0; int i; - if (remainder < 0) { remainder += nTabs; --delta; } - + /* Ignore minTabWidth; it's ignored in TabRowSize() too. */ for (i = 0; i < nTabs; ++i) { Tab *tab = Ttk_SlaveData(nb->notebook.mgr,i); - int adj = delta + (i < remainder) + slack; - - if (tab->width + adj >= minTabWidth) { - tab->width += adj; - slack = 0; - } else { - slack = adj - (minTabWidth - tab->width); - tab->width = minTabWidth; - } + double ad = slack + tab->width * delta; + tab->width += (int)ad; + slack = ad - (int)ad; } } } -- cgit v0.12 From f4c9b668c5a0329779df76cc8a18d4fc2fc6a6e8 Mon Sep 17 00:00:00 2001 From: fvogel Date: Sun, 2 Apr 2017 21:01:42 +0000 Subject: The -mintabwidth style option is honored whenever possible. If there is not enough space available, then priority is given to displaying all tabs (previous behaviour was they disappeared) over mandatorily honoring -mintabwidth. --- generic/ttk/ttkNotebook.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/generic/ttk/ttkNotebook.c b/generic/ttk/ttkNotebook.c index ac13d5f..389d520 100644 --- a/generic/ttk/ttkNotebook.c +++ b/generic/ttk/ttkNotebook.c @@ -325,6 +325,8 @@ static Ttk_State TabState(Notebook *nb, int index) /* TabrowSize -- * Compute max height and total width of all tabs (horizontal layouts) * or total height and max width (vertical layouts). + * The -mintabwidth style option is taken into account (for the width + * only). * * Side effects: * Sets width and height fields for all tabs. @@ -334,7 +336,7 @@ static Ttk_State TabState(Notebook *nb, int index) * (max height/width) but not parallel (total width/height). */ static void TabrowSize( - Notebook *nb, Ttk_Orient orient, int *widthPtr, int *heightPtr) + Notebook *nb, Ttk_Orient orient, int minTabWidth, int *widthPtr, int *heightPtr) { Ttk_Layout tabLayout = nb->notebook.tabLayout; int tabrowWidth = 0, tabrowHeight = 0; @@ -346,6 +348,7 @@ static void TabrowSize( Ttk_RebindSublayout(tabLayout, tab); Ttk_LayoutSize(tabLayout,tabState,&tab->width,&tab->height); + tab->width = MAX(tab->width, minTabWidth); if (orient == TTK_ORIENT_HORIZONTAL) { tabrowHeight = MAX(tabrowHeight, tab->height); @@ -406,7 +409,7 @@ static int NotebookSize(void *clientData, int *widthPtr, int *heightPtr) /* Tab row: */ - TabrowSize(nb, nbstyle.tabOrient, &tabrowWidth, &tabrowHeight); + TabrowSize(nb, nbstyle.tabOrient, nbstyle.minTabWidth, &tabrowWidth, &tabrowHeight); tabrowHeight += Ttk_PaddingHeight(nbstyle.tabMargins); tabrowWidth += Ttk_PaddingWidth(nbstyle.tabMargins); @@ -436,6 +439,7 @@ static int NotebookSize(void *clientData, int *widthPtr, int *heightPtr) /* SqueezeTabs -- * Squeeze or stretch tabs to fit within the tab area parcel. + * This happens independently of the -mintabwidth style option. * * All tabs are adjusted by an equal amount. * @@ -444,7 +448,7 @@ static int NotebookSize(void *clientData, int *widthPtr, int *heightPtr) */ static void SqueezeTabs( - Notebook *nb, int needed, int available, int minTabWidth) + Notebook *nb, int needed, int available) { int nTabs = Ttk_NumberSlaves(nb->notebook.mgr); @@ -454,7 +458,6 @@ static void SqueezeTabs( double slack = 0; int i; - /* Ignore minTabWidth; it's ignored in TabRowSize() too. */ for (i = 0; i < nTabs; ++i) { Tab *tab = Ttk_SlaveData(nb->notebook.mgr,i); double ad = slack + tab->width * delta; @@ -522,8 +525,13 @@ static void NotebookDoLayout(void *recordPtr) Ttk_PlaceLayout(nb->core.layout, nb->core.state, Ttk_WinBox(nbwin)); /* Place tabs: + * Note: TabrowSize() takes into account -mintabwidth, but the tabs will + * actually have this minimum size when displayed only if there is enough + * space to draw the tabs with this width. Otherwise some of the tabs can + * be squeezed to a size smaller than -mintabwidth because we prefer + * displaying all tabs than than honoring -mintabwidth for all of them. */ - TabrowSize(nb, nbstyle.tabOrient, &tabrowWidth, &tabrowHeight); + TabrowSize(nb, nbstyle.tabOrient, nbstyle.minTabWidth, &tabrowWidth, &tabrowHeight); tabrowBox = Ttk_PadBox( Ttk_PositionBox(&cavity, tabrowWidth + Ttk_PaddingWidth(nbstyle.tabMargins), @@ -531,7 +539,7 @@ static void NotebookDoLayout(void *recordPtr) nbstyle.tabPosition), nbstyle.tabMargins); - SqueezeTabs(nb, tabrowWidth, tabrowBox.width, nbstyle.minTabWidth); + SqueezeTabs(nb, tabrowWidth, tabrowBox.width); PlaceTabs(nb, tabrowBox, nbstyle.tabPlacement); /* Layout for client area frame: -- cgit v0.12 From 00b1b03df4ce671eea9cc375ea1d6d7d892d73ea Mon Sep 17 00:00:00 2001 From: fvogel Date: Thu, 6 Apr 2017 20:01:59 +0000 Subject: Slightly better fix anonymously proposed in [6020ee2d03]. Use floor() instead of casting to an int. --- generic/ttk/ttkNotebook.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/generic/ttk/ttkNotebook.c b/generic/ttk/ttkNotebook.c index 389d520..b649e4b 100644 --- a/generic/ttk/ttkNotebook.c +++ b/generic/ttk/ttkNotebook.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -454,15 +455,17 @@ static void SqueezeTabs( if (nTabs > 0) { int difference = available - needed; - double delta = (double)difference / needed; + double fraction = (double)difference / needed; double slack = 0; + double ad; int i; for (i = 0; i < nTabs; ++i) { Tab *tab = Ttk_SlaveData(nb->notebook.mgr,i); - double ad = slack + tab->width * delta; + + ad = slack + tab->width * fraction; tab->width += (int)ad; - slack = ad - (int)ad; + slack = ad - floor(ad); } } } -- cgit v0.12