paintButtons: fix auto-spacing of buttins / labels

in space-restricted setups, buttons would be painted outside
of the assigned space due to errors in position calculations


Origin commit data
------------------
Commit: 32661dd6fc
Author: Stefan Seyfried <seife@tuxbox-git.slipkontur.de>
Date: 2013-12-02 (Mon, 02 Dec 2013)
This commit is contained in:
Stefan Seyfried
2013-12-02 00:52:55 +01:00
committed by vanhofen
parent 0abd72b0fe
commit 593130a40e

View File

@@ -99,7 +99,8 @@ int paintButtons( const int &x,
int w_text = 0; int w_text = 0;
int h_max_text = font->getHeight(); int h_max_text = font->getHeight();
int count_items = 0; int count_icons = 0;
int count_labels = 0;
/* more than 16 buttons? noooooo*/ /* more than 16 buttons? noooooo*/
int iconw[16]; int iconw[16];
int iconh[16]; int iconh[16];
@@ -133,16 +134,18 @@ int paintButtons( const int &x,
h_max_icon = std::max(h_max_icon, h); h_max_icon = std::max(h_max_icon, h);
w_icons += w; w_icons += w;
if (w) if (w)
count_items++; count_icons++;
//text if (content[i].locale) {
buttontext[i] = (content[i].locale ? g_Locale->getText(content[i].locale) : ""); buttontext[i] = g_Locale->getText(content[i].locale);
//text width
//text width fwidth[i] = font->getRenderWidth(buttontext[i], true);
fwidth[i] = font->getRenderWidth(buttontext[i], true); w_text += fwidth[i];
w_text += fwidth[i]; count_labels++;
if (fwidth[i]) } else {
count_items++; buttontext[i] = "";
fwidth[i] = 0;
}
} }
//calculate button heigth //calculate button heigth
@@ -161,23 +164,24 @@ int paintButtons( const int &x,
//baseline //baseline
int y_base = y_footer + h_footer/2; int y_base = y_footer + h_footer/2;
int spacing = maxwidth - w_space * 2 - w_text - w_icons - (count_items - 1) * h_space; int spacing = maxwidth - w_space * 2 - w_text - w_icons - (count_icons + count_labels - 1) * h_space;
#if 0 #if 0
/* debug */ /* debug */
fprintf(stderr, "PB: sp %d mw %d w_t %d w_i %d w_s %d c_i %d\n", fprintf(stderr, "PB: sp %d mw %d w_t %d w_i %d w_s %d c_i %d\n",
spacing, maxwidth, w_text, w_icons, w_space, count_items); spacing, maxwidth, w_text, w_icons, w_space, count_items);
#endif #endif
if (fwidth[cnt - 1] == 0) /* divisor needs to be labels+1 unless rightmost icon has a label */
count_labels++; /* side effect: we don't try to divide by 0 :-) */
if (spacing >= 0) if (spacing >= 0)
{ /* add half of the inter-object space to the */ { /* add half of the inter-object space to the */
spacing /= (int)count; /* left and right (this might break vertical */ spacing /= count_labels; /* left and right (this might break vertical */
x_button += spacing / 2; /* alignment, but nobody is using this (yet) */ x_button += spacing / 2; /* alignment, but nobody is using this (yet) */
} /* and I'm don't know how it should work. */ } /* and I'm don't know how it should work. */
else else
{ {
spacing /= (int)(count - 1); /* one space less than buttons */ /* shorten captions relative to their length */
/* shorten captions */
for (i = 0; i < cnt; i++) for (i = 0; i < cnt; i++)
fwidth[i] += spacing; /* spacing is negative...*/ fwidth[i] = (fwidth[i] * (w_text + spacing)) / w_text; /* spacing is negative...*/
spacing = 0; spacing = 0;
} }
@@ -221,7 +225,9 @@ int paintButtons( const int &x,
else else
{ {
/* increase x position */ /* increase x position */
x_button = x_caption + fwidth[j] + spacing + h_space; x_button = x_caption;
if (fwidth[j])
x_button += fwidth[j] + spacing + h_space;
} }
} }