Multiple lines w/ text.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
typedef std::string<_TCHAR> tstring;
void
drawText(
CGapiSurface& surface,
CGapiBitmapFont& font,
const RECT& kRect,
const tstring& ks)
{
if (ks.empty())
{
return;
}
const int knLineHeight = font.GetHeight();
int nBeginPos = 0;
int nEndPos = ks.find(_T(" "));
tstring sWord = ks.substr(0, nEndPos);
DWORD nWordWidth;
int nX = kRect.left;
int nY = kRect.top;
if (kRect.bottom < nY + knLineHeight)
{
// The first line doesn't fit, so abort.
return;
}
while (true)
{
// Get the width of the word.
font.GetStringWidth(sWord.c_str(), &nWordWidth);
if (nX + nWordWidth <= kRect.right)
{
// The word fits on this line so draw it.
surface.DrawText(
nX,
nY,
sWord.c_str(),
&font,
0,
NULL,
0,
NULL);
// Advance the position.
nX += nWordWidth;
if (nEndPos == tstring::npos)
{
// We have drawn all the words.
break;
}
else
{
// Advance to the next word.
nBeginPos = nEndPos;
nEndPos = ks.find(_T(" "), nEndPos + 1);
sWord = ks.substr(nBeginPos, nEndPos - nBeginPos);
}
}
else
{
// The word doesn't fit on this line.
if (nX == kRect.left)
{
// This is already a new line, so abort.
break;
}
else
{
// Make a new line.
nY += knLineHeight;
if (kRect.bottom < nY + knLineHeight)
{
// The new line doesn't fit, so abort.
break;
}
// Prepare the new line by removing the leading space.
nX = kRect.left;
sWord = sWord.erase(sWord.begin());
}
}
}
}
92 lines; 26 keywds; 5 nums; 161 ops; 2 strs; 11 coms Syntactic Coloring v0.4 - Dan East
1
typedef std::basic_string<_TCHAR> tstring;
1 lines; 1 keywds; 0 nums; 5 ops; 0 strs; 0 coms Syntactic Coloring v0.4 - Dan East
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool guiTextbox::drawText( CGapiSurface* pSurface ,
CGapiBitmapFont* pFont ,
long lx ,
long ly ,
TCHAR* pszString,
dword dwFlags,
bool bMultiline )
{
// Hardcoded for this example
long lFontHeight = 7;
long lLineSpacing = 4;
// Get the first line - reading up to the carrage return
TCHAR* pszTok = NULL;
// If the caller is requesting multiline functionality, then do so using strtok to
// separate the lines using the carrage return
if( bMultiline )
{
pszTok = _tcstok( pszString, _T("\n") );
// While there is a valid string....
while( pszTok )
{
// Draw the text unto the surface.
if( pSurface->DrawText( lx, ly, pszTok, pFont, dwFlags, NULL, 0UL, NULL ) != S_OK )
return false;
// Increment to the next line
ly += (lFontHeight + lLineSpacing);
// Use the strtok function to obtain the next line
pszTok = _tcstok( NULL, _T("\n") );
}
}
else
{
// Otherwise, just draw the string normally.
if( pSurface->DrawText( lx, ly, pszString, pFont, dwFlags, NULL, 0UL, NULL ) != S_OK )
return false;
}
return true;
}
46 lines; 17 keywds; 4 nums; 85 ops; 2 strs; 9 coms Syntactic Coloring v0.4 - Dan East
Layre5150 wrote:I should also add that the code above is meant for a static window, one that is not dynamicially resizable. If that is your aim, then you would have to check word for word and handle all the re-formatting within the function.