IonicWind Software

Aurora Compiler => Coding Help - Aurora 101 => Topic started by: sapero on June 09, 2009, 02:56:46 PM

Title: Calculating button width
Post by: sapero on June 09, 2009, 02:56:46 PM
I have an owner drawn button with image and text. Button text can wrap into two lines. The minimum button width is 2*margin + image_width.
If button text contains a large word wider (in pixels) than the image, button width should be adjusted to MAX(minimum_width, 2*margin + width_of_largest_word).

I've created the following function to calculate this. It tokenizes the string (strtok/wcstok CRT api's), measures the width of each word and returns the widest result.
What Punctuation Marks (separators) should I use? Space - always. Digits? No, I dont think so.
. ? ! : ; - ~ ( ) [ ] . ’ â€Ã...“ â€Ã, / ,
Is this enough?
sub CalculateTextWidths(HDC hdc, wstring *pwsztext, int *ppTextWidth, int *ppMaxWordWidth)
{
int maxw = 0;
*ppTextWidth = 0;

RECT rc;
if (pwsztext)
{
DrawTextW(hdc, pwsztext, wcslen(pwsztext), &rc, DT_SINGLELINE|DT_CALCRECT);
*ppTextWidth = rc.Width();

// copy the string because tokenizing modifies the text
BSTR bstrText = SysAllocString(pwsztext);

WCHAR *token = wcstok(bstrText, L" "); // L".?!:;-~()[].’â€Ã...“â€Ã,/,"
while (token)
{
DrawTextW(hdc, token, wcslen(token), &rc, DT_SINGLELINE|DT_CALCRECT);
maxw = __max(maxw, rc.Width());
token = wcstok(NULL, L" ");
}
SysFreeString(bstrText);
}
*ppMaxWordWidth = maxw;
}


To draw button text on the window, I'm using DrawText with DT_WORDBREAK flag.
Title: Re: Calculating button width
Post by: Ficko on June 09, 2009, 03:15:35 PM
Could "GetTextExtentPoint32" maybe simplify the calculation? :o
Just guessing. :D
Title: Re: Calculating button width
Post by: sapero on June 09, 2009, 03:48:40 PM
No, DrawText with DT_CALCRECT flag computes the width and height of the specified string of text, also does same job as GetTextExtentPoint32.
The subroutine above does not draw any text, it is used to resize the button if needed.

For button text "Button 1" and image 32x32, the "Button" string part is drawn in the first line, and the second part "1" in second line. 32 pixels is enough for "Button" string, but "MySuperDuperButton 1" is too large, I need to resize the button to the width of "MySuperDuperButton" plus margins.