Strikethrough and Bold tags for GuiMLTextCtrl
by Orion Elenzil · 11/13/2006 (4:35 pm) · 8 comments
Dunno if this has already been implemented in TGE 1.4 or 1.5, but if it hasn't this should work with TGE 1.3 - 1.5 and probably beyond.
This is a simple change which adds support for a strikethrough and bold tag. Use it like this:
GuiMLTextCtrl.h
inside struct Style, right after the line bool used;, add this line:
GuiMLTextCtrl.cc
find this line: if(atom->url && !atom->url->noUnderline), and right *BEFORE* it, add this block:
- it's important to add that block Before the atom->url block.
in method GuiMLTextCtrl::allocStyle(), find this line: ret->next = style->next;, and right Before it, add this line:
in method GuiMLTextCtrl::reflow(), find this line:mCurStyle->linkColorHL = mProfile->mFontColors[GuiControlProfile::ColorUser1];, and right after it add this line:
still in reflow(), find this block:
.. and that's it!
Note,
i haven't added these new tags to the code which filters out tags yet, so, uh, yer warned.
Another Note,
this implementation of Bold is pretty hacky.
If you want to avoid relying on something hacky, you can always use the stock GuiMLText font tag to specify the Bold version of the font. The nice thing about my b tag is that it inherits the current font.
This is a simple change which adds support for a strikethrough and bold tag. Use it like this:
MyGUIMLTextControl.setText("blah blah <strikethrough>and <b>blah");
It works with <spush> and <spop> as well.- Moderator Note - Wrapped the previous text in a code block to prevent all following text from being bold.GuiMLTextCtrl.h
inside struct Style, right after the line bool used;, add this line:
bool strikethrough;
GuiMLTextCtrl.cc
find this line: if(atom->url && !atom->url->noUnderline), and right *BEFORE* it, add this block:
if(atom->style->strikethrough)
{
Point2I p1 = drawPoint;
p1.y += (S32)((F32)(atom->baseLine) * 0.7f);
Point2I p2 = p1;
p2.x += font->getStrNWidthPrecise(mTextBuffer + start, end - start);
// overshoot the characters slightly
p1.x -= 2;
p2.x += 2;
dglDrawLine(p1, p2, color);
}- it's important to add that block Before the atom->url block.
in method GuiMLTextCtrl::allocStyle(), find this line: ret->next = style->next;, and right Before it, add this line:
ret->strikethrough = style->strikethrough;
in method GuiMLTextCtrl::reflow(), find this line:mCurStyle->linkColorHL = mProfile->mFontColors[GuiControlProfile::ColorUser1];, and right after it add this line:
mCurStyle->strikethrough = false;
still in reflow(), find this block:
if(!dStrnicmp(str +1, "just:center>", 12))
{
mCurJustify = CenterJustify;
mScanPos += 13;
continue;
}and right after it add this block:#define CURFORMATTOKEN "strikethrough>"
if(!dStrnicmp(str + 1, CURFORMATTOKEN, sizeof(CURFORMATTOKEN)-1))
{
processEmitAtoms();
if(mCurStyle->used)
mCurStyle = allocStyle(mCurStyle);
mCurStyle->strikethrough = true;
mScanPos += sizeof(CURFORMATTOKEN);
continue;
}
#undef CURFORMATTOKEN
#define CURFORMATTOKEN "b>"
if(!dStrnicmp(str + 1, CURFORMATTOKEN, sizeof(CURFORMATTOKEN)-1))
{
if (!mCurStyle->font->faceName || (dStrstr(mCurStyle->font->faceName, " bold") != NULL))
continue;
dSprintf(fontNameBuf, sizeof(fontNameBuf), "%s bold", mCurStyle->font->faceName);
nextFont = allocFont(fontNameBuf, dStrlen(fontNameBuf), mCurStyle->font->size);
if(nextFont)
{
if(mCurStyle->used)
mCurStyle = allocStyle(mCurStyle);
mCurStyle->font = nextFont;
}
mScanPos += sizeof(CURFORMATTOKEN);
continue;
}
#undef CURFORMATTOKEN.. note, if you want to get rid of the stuff i'm doing with #define, go ahead, there's nothing magic about it, but i think the #define stuff is a lot better than the surrounding code because it reduces the chances of errors when you're counting characters on your fingers, and also makes it worlds easier to copy the codeblock as a template for a new block... and that's it!
Note,
i haven't added these new tags to the code which filters out tags yet, so, uh, yer warned.
Another Note,
this implementation of Bold is pretty hacky.
If you want to avoid relying on something hacky, you can always use the stock GuiMLText font tag to specify the Bold version of the font. The nice thing about my b tag is that it inherits the current font.
About the author
#2
Thanks Orion!
11/14/2006 (8:59 am)
Very cool. Text is going to be a big part of my project (weird, I know), so this should come in handy.Thanks Orion!
#3
10/10/2007 (3:49 pm)
Where do I put this is my defaultProfiles.cs and how?
#4
10/10/2007 (3:54 pm)
these are mltext *tags*, not a profile property.
#5
10/10/2007 (3:55 pm)
ok cool. ya i figured out how to use them and this is really awesome!
#6
Any ideas?
I am using TGE 1.5.2
10/10/2007 (4:18 pm)
I tried, it failed, I tried to fix, it failed, I restored to original, succeeded, tried again, failed...Compiling...
guiMLTextCtrl.cc
..\engine\gui\controls\guiMLTextCtrl.cc(232) : error C2676: binary '+' : 'StringBuffer' does not define this operator or a conversion to a type acceptable to the predefined operator
..\engine\gui\controls\guiMLTextCtrl.cc(1757) : error C2666: 'dStrstr' : 2 overloads have similar conversions
../engine\platform/platform.h(464): could be 'char *dStrstr(const char *,const char *)'
../engine\platform/platform.h(463): or 'char *dStrstr(char *,char *)'
while trying to match the argument list '(char *, const char [6])'
..\engine\gui\controls\guiMLTextCtrl.cc(1760) : error C2065: 'fontNameBuf' : undeclared identifier
..\engine\gui\controls\guiMLTextCtrl.cc(1760) : error C2065: 'fontNameBuf' : undeclared identifier
..\engine\gui\controls\guiMLTextCtrl.cc(1760) : error C2070: ''unknown-type'': illegal sizeof operand
..\engine\gui\controls\guiMLTextCtrl.cc(1761) : error C2065: 'fontNameBuf' : undeclared identifier
..\engine\gui\controls\guiMLTextCtrl.cc(1761) : error C2065: 'fontNameBuf' : undeclared identifierAny ideas?
I am using TGE 1.5.2
#7
10/10/2007 (4:21 pm)
hm, i dunno. this is built for TGE 1.3, maybe your codebase is different than that ?
#8
To get past "error binary +", simply change
12/02/2009 (8:43 pm)
I only needed the strikethrough capability, so that's all I added. (Thanks, Orion!)To get past "error binary +", simply change
p2.x += font->getStrNWidthPrecise(mTextBuffer + start, end - start);to
p2.x += font->getStrNWidthPrecise( tmp, tmpLen );

Associate Orion Elenzil
Real Life Plus
Note, the screenshot was generated with the following ML text string:
foo.setText("<just:center><font:arial:30><spush><strikethrough>Strikethrough<spop><br>and spush><b>Bold<spop> !");