guiMLTextCtrl.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gui/controls/guiMLTextCtrl.h"
  23. #include "gui/containers/guiScrollCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "gui/core/guiCanvas.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "core/frameAllocator.h"
  29. #include "core/strings/unicode.h"
  30. #include "sfx/sfxSystem.h"
  31. #include "sfx/sfxTrack.h"
  32. #include "sfx/sfxTypes.h"
  33. #include "console/engineAPI.h"
  34. IMPLEMENT_CONOBJECT( GuiMLTextCtrl );
  35. ConsoleDocClass( GuiMLTextCtrl,
  36. "@brief A text control that uses the Gui Markup Language ('ML') tags to dynamically change the text.\n\n"
  37. "Example of dynamic changes include colors, styles, and/or hyperlinks. These changes can occur without "
  38. "having to use separate text controls with separate text profiles.\n\n"
  39. "@tsexample\n"
  40. "new GuiMLTextCtrl(CenterPrintText)\n"
  41. "{\n"
  42. " lineSpacing = \"2\";\n"
  43. " allowColorChars = \"0\";\n"
  44. " maxChars = \"-1\";\n"
  45. " deniedSound = \"DeniedSoundProfile\";\n"
  46. " text = \"The Text for This Control.\";\n"
  47. " useURLMouseCursor = \"true\";\n"
  48. " //Properties not specific to this control have been omitted from this example.\n"
  49. "};\n"
  50. "@endtsexample\n\n"
  51. "@see GuiControl\n\n"
  52. "@ingroup GuiCore\n"
  53. );
  54. IMPLEMENT_CALLBACK( GuiMLTextCtrl, onURL, void, ( const char* url ),( url ),
  55. "@brief Called whenever a URL was clicked on within the control.\n\n"
  56. "@param url The URL address that was clicked on.\n"
  57. "@tsexample\n"
  58. "// A URL address was clicked on in the control, causing the callback to occur.\n"
  59. "GuiMLTextCtrl::onUrl(%this,%url)\n"
  60. " {\n"
  61. " // Code to run whenever a URL was clicked on\n"
  62. " }\n"
  63. "@endtsexample\n\n"
  64. "@see GuiControl\n\n"
  65. );
  66. IMPLEMENT_CALLBACK( GuiMLTextCtrl, onResize, void, ( S32 width, S32 maxY ),( width, maxY ),
  67. "@brief Called whenever the control size changes.\n\n"
  68. "@param width The new width value for the control\n"
  69. "@param maxY The current maximum allowed Y value for the control\n\n"
  70. "@tsexample\n"
  71. "// Control size changed, causing the callback to occur.\n"
  72. "GuiMLTextCtrl::onResize(%this,%width,%maxY)\n"
  73. " {\n"
  74. " // Code to call when the control size changes\n"
  75. " }\n"
  76. "@endtsexample\n\n"
  77. "@see GuiControl\n\n"
  78. );
  79. GFX_ImplementTextureProfile(GFXMLTextureProfile,
  80. GFXTextureProfile::DiffuseMap,
  81. GFXTextureProfile::PreserveSize |
  82. GFXTextureProfile::Static,
  83. GFXTextureProfile::NONE);
  84. const U32 GuiMLTextCtrl::csmTextBufferGrowthSize = 1024;
  85. DefineEngineMethod( GuiMLTextCtrl, setText, void, (const char* text),,
  86. "@brief Set the text contained in the control.\n\n"
  87. "@param text The text to display in the control.\n"
  88. "@tsexample\n"
  89. "// Define the text to display\n"
  90. "%text = \"Nifty Control Text\";\n\n"
  91. "// Set the text displayed within the control\n"
  92. "%thisGuiMLTextCtrl.setText(%text);\n"
  93. "@endtsexample\n\n"
  94. "@see GuiControl")
  95. {
  96. object->setText(text, dStrlen(text));
  97. }
  98. DefineEngineMethod( GuiMLTextCtrl, getText, const char*, (),,
  99. "@brief Returns the text from the control, including TorqueML characters.\n\n"
  100. "@tsexample\n"
  101. "// Get the text displayed in the control\n"
  102. "%controlText = %thisGuiMLTextCtrl.getText();\n"
  103. "@endtsexample\n\n"
  104. "@return Text string displayed in the control, including any TorqueML characters.\n\n"
  105. "@see GuiControl")
  106. {
  107. return( object->getTextContent() );
  108. }
  109. DefineEngineMethod( GuiMLTextCtrl, addText, void, ( const char* text, bool reformat), (true),
  110. "@brief Appends the text in the control with additional text. Also .\n\n"
  111. "@param text New text to append to the existing text.\n"
  112. "@param reformat If true, the control will also be visually reset (defaults to true).\n"
  113. "@tsexample\n"
  114. "// Define new text to add\n"
  115. "%text = \"New Text to Add\";\n\n"
  116. "// Set reformat boolean\n"
  117. "%reformat = \"true\";\n\n"
  118. "// Inform the control to add the new text\n"
  119. "%thisGuiMLTextCtrl.addText(%text,%reformat);\n"
  120. "@endtsexample\n\n"
  121. "@see GuiControl")
  122. {
  123. object->addText(text, dStrlen(text), reformat);
  124. }
  125. DefineEngineMethod( GuiMLTextCtrl, setCursorPosition, bool, (S32 newPos),,
  126. "@brief Change the text cursor's position to a new defined offset within the text in the control.\n\n"
  127. "@param newPos Offset to place cursor.\n"
  128. "@tsexample\n"
  129. "// Define cursor offset position\n"
  130. "%position = \"23\";\n\n"
  131. "// Inform the GuiMLTextCtrl object to move the cursor to the new position.\n"
  132. "%thisGuiMLTextCtrl.setCursorPosition(%position);\n"
  133. "@endtsexample\n\n"
  134. "@return Returns true if the cursor position moved, or false if the position was not changed.\n\n"
  135. "@see GuiControl")
  136. {
  137. return object->setCursorPosition(newPos);
  138. }
  139. DefineEngineMethod( GuiMLTextCtrl, scrollToTag, void, (S32 tagID),,
  140. "@brief Scroll down to a specified tag.\n\n"
  141. "Detailed description\n\n"
  142. "@param tagID TagID to scroll the control to\n"
  143. "@tsexample\n"
  144. "// Define the TagID we want to scroll the control to\n"
  145. "%tagId = \"4\";\n\n"
  146. "// Inform the GuiMLTextCtrl to scroll to the defined TagID\n"
  147. "%thisGuiMLTextCtrl.scrollToTag(%tagId);\n"
  148. "@endtsexample\n\n"
  149. "@see GuiControl")
  150. {
  151. object->scrollToTag( tagID );
  152. }
  153. DefineEngineMethod( GuiMLTextCtrl, scrollToTop, void, (),,
  154. "@brief Scroll to the top of the text.\n\n"
  155. "@tsexample\n"
  156. "// Inform GuiMLTextCtrl object to scroll to its top\n"
  157. "%thisGuiMLTextCtrl.scrollToTop();\n"
  158. "@endtsexample\n\n"
  159. "@see GuiControl")
  160. {
  161. object->scrollToTop();
  162. }
  163. DefineEngineMethod( GuiMLTextCtrl, scrollToBottom, void, (),,
  164. "@brief Scroll to the bottom of the text.\n\n"
  165. "@tsexample\n"
  166. "// Inform GuiMLTextCtrl object to scroll to its bottom\n"
  167. "%thisGuiMLTextCtrl.scrollToBottom();\n"
  168. "@endtsexample\n\n"
  169. "@see GuiControl")
  170. {
  171. object->scrollToBottom();
  172. }
  173. DefineEngineFunction(StripMLControlChars, const char*, (const char* inString),,
  174. "@brief Strip TorqueML control characters from the specified string, returning a 'clean' version.\n\n"
  175. "@param inString String to strip TorqueML control characters from.\n"
  176. "@tsexample\n"
  177. "// Define the string to strip TorqueML control characters from\n"
  178. "%string = \"<font:Arial:24>How Now <color:c43c12>Brown <color:000000>Cow\";\n\n"
  179. "// Request the stripped version of the string\n"
  180. "%strippedString = StripMLControlChars(%string);\n"
  181. "@endtsexample\n\n"
  182. "@return Version of the inputted string with all TorqueML characters removed.\n\n"
  183. "@see References\n\n"
  184. "@ingroup GuiCore")
  185. {
  186. return GuiMLTextCtrl::stripControlChars(inString);
  187. }
  188. DefineEngineMethod( GuiMLTextCtrl, forceReflow, void, (),,
  189. "@brief Forces the text control to reflow the text after new text is added, possibly resizing the control.\n\n"
  190. "@tsexample\n"
  191. "// Define new text to add\n"
  192. "%newText = \"BACON!\";\n\n"
  193. "// Add the new text to the control\n"
  194. "%thisGuiMLTextCtrl.addText(%newText);\n\n"
  195. "// Inform the GuiMLTextCtrl object to force a reflow to ensure the added text fits properly.\n"
  196. "%thisGuiMLTextCtrl.forceReflow();\n"
  197. "@endtsexample\n\n"
  198. "@see GuiControl")
  199. {
  200. if(!object->isAwake())
  201. Con::errorf("GuiMLTextCtrl::forceReflow can only be called on visible controls.");
  202. else
  203. object->reflow();
  204. }
  205. //--------------------------------------------------------------------------
  206. GuiMLTextCtrl::GuiMLTextCtrl()
  207. : mTabStops( NULL ),
  208. mTabStopCount( 0 ),
  209. mCurTabStop( 0 ),
  210. mCurStyle( NULL ),
  211. mCurLMargin( 0 ),
  212. mCurRMargin( 100 ),
  213. mCurJustify( LeftJustify ),
  214. mCurDiv( 0 ),
  215. mCurY( 0 ),
  216. mCurClipX( 0 ),
  217. mLineAtoms( NULL ),
  218. mLineList( NULL ),
  219. mLineAtomPtr( &mLineAtoms ),
  220. mLineInsert( &mLineList ),
  221. mScanPos( 0 ),
  222. mCurX( 0 ),
  223. mMaxY( 0 ),
  224. mCurURL( NULL ),
  225. mLineStart( 0 ),
  226. mVertMoveAnchor( 0 ),
  227. mVertMoveAnchorValid( false ),
  228. mIsEditCtrl( false ),
  229. mSelectionAnchor( 0 ),
  230. mCursorPosition( false ),
  231. mMaxBufferSize( -1 ),
  232. mInitialText( StringTable->EmptyString() ),
  233. mSelectionActive( false ),
  234. mSelectionStart( 0 ),
  235. mSelectionEnd( 0 ),
  236. mLineSpacingPixels( 2 ),
  237. mAllowColorChars( false ),
  238. mBitmapList( 0 ),
  239. mUseURLMouseCursor( false ),
  240. mBitmapRefList( 0 ),
  241. mTagList( NULL ),
  242. mDirty( true ),
  243. mAlpha( 1.0f ),
  244. mHitURL( 0 ),
  245. mFontList( NULL )
  246. {
  247. mActive = true;
  248. //mInitialText = StringTable->EmptyString();
  249. Sim::findObject("InputDeniedSound", mDeniedSound);
  250. }
  251. //--------------------------------------------------------------------------
  252. GuiMLTextCtrl::~GuiMLTextCtrl()
  253. {
  254. mCursorPosition = 0;
  255. mSelectionActive = false;
  256. mSelectionStart = 0;
  257. mSelectionEnd = 0;
  258. freeResources();
  259. }
  260. //--------------------------------------------------------------------------
  261. void GuiMLTextCtrl::initPersistFields()
  262. {
  263. addGroup( "Text" );
  264. addField("lineSpacing", TypeS32, Offset(mLineSpacingPixels, GuiMLTextCtrl), "The number of blank pixels to place between each line.\n");
  265. addField("allowColorChars", TypeBool, Offset(mAllowColorChars, GuiMLTextCtrl), "If true, the control will allow characters to have unique colors.");
  266. addField("maxChars", TypeS32, Offset(mMaxBufferSize, GuiMLTextCtrl), "Maximum number of characters that the control will display.");
  267. addField("deniedSound", TypeSFXTrackName, Offset(mDeniedSound, GuiMLTextCtrl), "If the text will not fit in the control, the deniedSound is played.");
  268. addField("text", TypeCaseString, Offset( mInitialText, GuiMLTextCtrl ), "Text to display in this control.");
  269. addField("useURLMouseCursor", TypeBool, Offset(mUseURLMouseCursor, GuiMLTextCtrl), "If true, the mouse cursor will turn into a hand cursor while over a link in the text.\n"
  270. "This is dependant on the markup language used by the GuiMLTextCtrl\n");
  271. endGroup( "Text" );
  272. Parent::initPersistFields();
  273. }
  274. DefineEngineMethod( GuiMLTextCtrl, setAlpha, void, (F32 alphaVal),,
  275. "@brief Sets the alpha value of the control.\n\n"
  276. "@param alphaVal n - 1.0 floating value for the alpha\n"
  277. "@tsexample\n"
  278. "// Define the alphe value\n"
  279. "%alphaVal = \"0.5\";\n\n"
  280. "// Inform the control to update its alpha value.\n"
  281. "%thisGuiMLTextCtrl.setAlpha(%alphaVal);\n"
  282. "@endtsexample\n\n"
  283. "@see GuiControl")
  284. {
  285. object->setAlpha(alphaVal);
  286. }
  287. //--------------------------------------------------------------------------
  288. bool GuiMLTextCtrl::onAdd()
  289. {
  290. if(!Parent::onAdd())
  291. return false;
  292. if (!mTextBuffer.length() && mInitialText[0] != 0)
  293. setText(mInitialText, dStrlen(mInitialText)+1);
  294. return true;
  295. }
  296. //--------------------------------------------------------------------------
  297. bool GuiMLTextCtrl::onWake()
  298. {
  299. if (Parent::onWake() == false)
  300. return false;
  301. mDirty = true;
  302. return true;
  303. }
  304. //--------------------------------------------------------------------------
  305. void GuiMLTextCtrl::onPreRender()
  306. {
  307. if(mDirty)
  308. reflow();
  309. }
  310. //--------------------------------------------------------------------------
  311. void GuiMLTextCtrl::drawAtomText(bool sel, U32 start, U32 end, Atom *atom, Line *line, Point2I offset)
  312. {
  313. GFont *font = atom->style->font->fontRes;
  314. U32 xOff = 0;
  315. if(start != atom->textStart)
  316. {
  317. const UTF16* buff = mTextBuffer.getPtr() + atom->textStart;
  318. xOff += font->getStrNWidth(buff, start - atom->textStart);
  319. }
  320. Point2I drawPoint(offset.x + atom->xStart + xOff, offset.y + atom->yStart);
  321. ColorI color;
  322. if(atom->url)
  323. {
  324. if(atom->url->mouseDown)
  325. color = atom->style->linkColorHL;
  326. else
  327. color = atom->style->linkColor;
  328. }
  329. else
  330. color = atom->style->color;
  331. const UTF16* tmp = mTextBuffer.getPtr() + start;
  332. U32 tmpLen = end-start;
  333. GFXDrawUtil *drawer = GFX->getDrawUtil();
  334. if(!sel)
  335. {
  336. if(atom->style->shadowOffset.x || atom->style->shadowOffset.y)
  337. {
  338. ColorI shadowColor = atom->style->shadowColor;
  339. shadowColor.alpha = (S32)(mAlpha * shadowColor.alpha);
  340. drawer->setBitmapModulation(shadowColor);
  341. drawer->drawTextN(font, drawPoint + atom->style->shadowOffset,
  342. tmp, tmpLen, mAllowColorChars ? mProfile->mFontColors : NULL);
  343. }
  344. color.alpha = (S32)(mAlpha * color.alpha);
  345. drawer->setBitmapModulation(color);
  346. drawer->drawTextN(font, drawPoint, tmp, end-start, mAllowColorChars ? mProfile->mFontColors : NULL);
  347. //if the atom was "clipped", see if we need to draw a "..." at the end
  348. if (atom->isClipped)
  349. {
  350. Point2I p2 = drawPoint;
  351. p2.x += font->getStrNWidthPrecise(tmp, tmpLen);
  352. drawer->drawTextN(font, p2, "...", 3, mAllowColorChars ? mProfile->mFontColors : NULL);
  353. }
  354. }
  355. else
  356. {
  357. RectI rect;
  358. rect.point.x = drawPoint.x;
  359. rect.point.y = line->y + offset.y;
  360. rect.extent.x = font->getStrNWidth(tmp, tmpLen) + 1;
  361. rect.extent.y = line->height + 1;
  362. drawer->drawRectFill(rect, mProfile->mFillColorHL);
  363. drawer->setBitmapModulation( mProfile->mFontColorHL ); // over-ride atom color:
  364. drawer->drawTextN(font, drawPoint, tmp, tmpLen, mAllowColorChars ? mProfile->mFontColors : NULL);
  365. //if the atom was "clipped", see if we need to draw a "..." at the end
  366. if (atom->isClipped)
  367. {
  368. Point2I p2 = drawPoint;
  369. p2.x += font->getStrNWidthPrecise(tmp, end - atom->textStart);
  370. drawer->drawTextN(font, p2, "...", 3, mAllowColorChars ? mProfile->mFontColors : NULL);
  371. }
  372. }
  373. if(atom->url && !atom->url->noUnderline)
  374. {
  375. drawPoint.y += atom->baseLine + 2;
  376. Point2I p2 = drawPoint;
  377. p2.x += font->getStrNWidthPrecise(tmp, end - atom->textStart);
  378. drawer->drawLine(drawPoint, p2, color);
  379. }
  380. }
  381. //--------------------------------------------------------------------------
  382. void GuiMLTextCtrl::onRender(Point2I offset, const RectI& updateRect)
  383. {
  384. Parent::onRender(offset, updateRect);
  385. GFXDrawUtil *drawer = GFX->getDrawUtil();
  386. // draw all the bitmaps
  387. for(BitmapRef *walk = mBitmapRefList; walk; walk = walk->next)
  388. {
  389. RectI screenBounds = *walk;
  390. screenBounds.point += offset;
  391. if(!screenBounds.overlaps(updateRect))
  392. continue;
  393. drawer->clearBitmapModulation();
  394. drawer->drawBitmap(walk->bitmap->bitmapObject, screenBounds.point);
  395. //GFX->drawRectFill(screenBounds, mProfile->mFillColor);
  396. }
  397. offset += mProfile->mTextOffset;
  398. // draw all the text and dividerStyles
  399. for(Line *lwalk = mLineList; lwalk; lwalk = lwalk->next)
  400. {
  401. RectI lineRect(offset.x, offset.y + lwalk->y, getWidth(), lwalk->height);
  402. if(!lineRect.overlaps(updateRect))
  403. continue;
  404. if(lwalk->divStyle)
  405. drawer->drawRectFill(lineRect, mProfile->mFillColorHL);
  406. for(Atom *awalk = lwalk->atomList; awalk; awalk = awalk->next)
  407. {
  408. if(!mSelectionActive || mSelectionEnd < awalk->textStart || mSelectionStart >= awalk->textStart + awalk->len)
  409. drawAtomText(false, awalk->textStart, awalk->textStart + awalk->len, awalk, lwalk, offset);
  410. else
  411. {
  412. U32 selectionStart = getMax(awalk->textStart, mSelectionStart);
  413. U32 selectionEnd = getMin(awalk->textStart + awalk->len, mSelectionEnd + 1);
  414. // draw some unselected text
  415. if(selectionStart > awalk->textStart)
  416. drawAtomText(false, awalk->textStart, selectionStart, awalk, lwalk, offset);
  417. // draw the selection
  418. drawAtomText(true, selectionStart, selectionEnd, awalk, lwalk, offset);
  419. if(selectionEnd < awalk->textStart + awalk->len)
  420. drawAtomText(false, selectionEnd, awalk->textStart + awalk->len, awalk, lwalk, offset);
  421. }
  422. }
  423. }
  424. drawer->clearBitmapModulation();
  425. }
  426. //--------------------------------------------------------------------------
  427. void GuiMLTextCtrl::freeLineBuffers()
  428. {
  429. mViewChunker.freeBlocks();
  430. mLineList = NULL;
  431. mBitmapRefList = NULL;
  432. mTagList = NULL;
  433. mHitURL = 0;
  434. mDirty = true;
  435. }
  436. //--------------------------------------------------------------------------
  437. void GuiMLTextCtrl::freeResources()
  438. {
  439. for(Font* walk = mFontList; walk; walk = walk->next)
  440. {
  441. walk->fontRes = NULL;
  442. delete[] walk->faceName;
  443. }
  444. for(Bitmap* bwalk = mBitmapList; bwalk; bwalk = bwalk->next)
  445. bwalk->bitmapObject = 0;
  446. mFontList = NULL;
  447. mBitmapList = NULL;
  448. mResourceChunker.freeBlocks();
  449. mDirty = true;
  450. freeLineBuffers();
  451. }
  452. //--------------------------------------------------------------------------
  453. void GuiMLTextCtrl::onSleep()
  454. {
  455. freeResources();
  456. Parent::onSleep();
  457. }
  458. //--------------------------------------------------------------------------
  459. void GuiMLTextCtrl::inspectPostApply()
  460. {
  461. Parent::inspectPostApply();
  462. setText(mInitialText, dStrlen(mInitialText));
  463. mLineSpacingPixels = 0;
  464. }
  465. //--------------------------------------------------------------------------
  466. void GuiMLTextCtrl::parentResized(const RectI& oldParentRect, const RectI& newParentRect)
  467. {
  468. Parent::parentResized(oldParentRect, newParentRect);
  469. mDirty = true;
  470. }
  471. //--------------------------------------------------------------------------
  472. U32 GuiMLTextCtrl::getNumChars() const
  473. {
  474. return mTextBuffer.length();
  475. }
  476. //--------------------------------------------------------------------------
  477. U32 GuiMLTextCtrl::getText(char* pBuffer, const U32 bufferSize) const
  478. {
  479. mTextBuffer.getCopy8(pBuffer, bufferSize);
  480. return getMin(mTextBuffer.length(), bufferSize);
  481. }
  482. //--------------------------------------------------------------------------
  483. const char* GuiMLTextCtrl::getTextContent()
  484. {
  485. if ( mTextBuffer.length() > 0 )
  486. {
  487. UTF8* returnString = Con::getReturnBuffer( mTextBuffer.getUTF8BufferSizeEstimate() );
  488. mTextBuffer.getCopy8(returnString, mTextBuffer.getUTF8BufferSizeEstimate() );
  489. return returnString;
  490. }
  491. return( "" );
  492. }
  493. //--------------------------------------------------------------------------
  494. const char *GuiMLTextCtrl::getScriptValue()
  495. {
  496. return getTextContent();
  497. }
  498. //--------------------------------------------------------------------------
  499. void GuiMLTextCtrl::setScriptValue(const char *newText)
  500. {
  501. setText(newText, dStrlen(newText));
  502. }
  503. //--------------------------------------------------------------------------
  504. void GuiMLTextCtrl::setText(const char* textBuffer, const U32 numChars)
  505. {
  506. U32 chars = numChars;
  507. if(numChars >= mMaxBufferSize)
  508. chars = mMaxBufferSize;
  509. // leaving this usage because we StringBuffer.set((UTF8*)) cannot take a numChars arg.
  510. // perhaps it should? -paxorr
  511. FrameTemp<UTF8> tmp(chars+1);
  512. dStrncpy(tmp, textBuffer, chars);
  513. tmp[chars] = 0;
  514. mTextBuffer.set(tmp);
  515. //after setting text, always set the cursor to the beginning
  516. setCursorPosition(0);
  517. clearSelection();
  518. mDirty = true;
  519. scrollToTop();
  520. }
  521. //--------------------------------------------------------------------------
  522. void GuiMLTextCtrl::addText(const char* textBuffer, const U32 numChars, bool reformat)
  523. {
  524. if(numChars >= mMaxBufferSize)
  525. return;
  526. FrameTemp<UTF8> tmp(numChars+1);
  527. dStrncpy(tmp, textBuffer, numChars);
  528. tmp[numChars] = 0;
  529. mTextBuffer.append(tmp);
  530. //after setting text, always set the cursor to the beginning
  531. if (reformat)
  532. {
  533. setCursorPosition(0);
  534. clearSelection();
  535. mDirty = true;
  536. scrollToTop();
  537. }
  538. }
  539. //--------------------------------------------------------------------------
  540. bool GuiMLTextCtrl::setCursorPosition(const S32 newPosition)
  541. {
  542. if (newPosition < 0)
  543. {
  544. mCursorPosition = 0;
  545. return true;
  546. }
  547. else if (newPosition >= mTextBuffer.length() - 1)
  548. {
  549. mCursorPosition = mTextBuffer.length();
  550. return true;
  551. }
  552. else
  553. {
  554. mCursorPosition = newPosition;
  555. return false;
  556. }
  557. }
  558. //--------------------------------------------------------------------------
  559. void GuiMLTextCtrl::ensureCursorOnScreen()
  560. {
  561. // If our parent isn't a scroll control, or we're not the only control
  562. // in the content region, bail...
  563. GuiControl* pParent = getParent();
  564. GuiScrollCtrl *sc = dynamic_cast<GuiScrollCtrl*>(pParent);
  565. if(!sc)
  566. return;
  567. // Ok. Now we know that our parent is a scroll control. Let's find the
  568. // top of the cursor, and it's bottom. We can then scroll the parent control
  569. // if appropriate...
  570. Point2I cursorTopP, cursorBottomP;
  571. ColorI color;
  572. getCursorPositionAndColor(cursorTopP, cursorBottomP, color);
  573. sc->scrollRectVisible(RectI(cursorTopP.x, cursorTopP.y, 1, cursorBottomP.y - cursorTopP.y));
  574. }
  575. //--------------------------------------
  576. void GuiMLTextCtrl::getCursorPositionAndColor(Point2I &cursorTop, Point2I &cursorBottom, ColorI &color)
  577. {
  578. S32 x = 0;
  579. S32 y = 0;
  580. S32 height = (mProfile && mProfile->mFont) ? mProfile->mFont->getHeight() : 0;
  581. color = mProfile->mCursorColor;
  582. for(Line *walk = mLineList; walk; walk = walk->next)
  583. {
  584. if ((mCursorPosition < walk->textStart + walk->len) || (walk->next == NULL))
  585. {
  586. // it's in the atoms on this line...
  587. y = walk->y;
  588. height = walk->height;
  589. for(Atom *awalk = walk->atomList; awalk; awalk = awalk->next)
  590. {
  591. if(mCursorPosition < awalk->textStart)
  592. {
  593. x = awalk->xStart;
  594. goto done;
  595. }
  596. if(mCursorPosition > awalk->textStart + awalk->len)
  597. {
  598. x = awalk->xStart + awalk->width;
  599. continue;
  600. }
  601. // it's in the text block...
  602. x = awalk->xStart;
  603. GFont *font = awalk->style->font->fontRes;
  604. const UTF16* buff = mTextBuffer.getPtr() + awalk->textStart;
  605. x += font->getStrNWidth(buff, mCursorPosition - awalk->textStart);// - 1);
  606. color = awalk->style->color;
  607. goto done;
  608. }
  609. //if it's within this walk's width, but we didn't find an atom, leave the cursor at the beginning of the line...
  610. goto done;
  611. }
  612. }
  613. done:
  614. cursorTop.set(x, y);
  615. cursorBottom.set(x, y + height);
  616. }
  617. //--------------------------------------------------------------------------
  618. // Keyboard events...
  619. bool GuiMLTextCtrl::onKeyDown(const GuiEvent& event)
  620. {
  621. //only cut/copy work with this control...
  622. if (event.modifier & SI_COPYPASTE)
  623. {
  624. switch(event.keyCode)
  625. {
  626. //copy
  627. case KEY_C:
  628. {
  629. //make sure we actually have something selected
  630. if (mSelectionActive)
  631. {
  632. copyToClipboard(mSelectionStart, mSelectionEnd);
  633. setUpdate();
  634. }
  635. return true;
  636. }
  637. default:
  638. break;
  639. }
  640. }
  641. // Otherwise, let the parent have the event...
  642. return Parent::onKeyDown(event);
  643. }
  644. //--------------------------------------------------------------------------
  645. // Mousing events...
  646. void GuiMLTextCtrl::onMouseDown(const GuiEvent& event)
  647. {
  648. if(!mActive)
  649. return;
  650. Atom *hitAtom = findHitAtom(globalToLocalCoord(event.mousePoint));
  651. if(hitAtom && !mIsEditCtrl)
  652. {
  653. mouseLock();
  654. mHitURL = hitAtom->url;
  655. if (mHitURL)
  656. mHitURL->mouseDown = true;
  657. }
  658. setFirstResponder();
  659. mouseLock();
  660. mSelectionActive = false;
  661. mSelectionAnchor = getTextPosition(globalToLocalCoord(event.mousePoint));
  662. mSelectionAnchorDropped = event.mousePoint;
  663. if (mSelectionAnchor < 0)
  664. mSelectionAnchor = 0;
  665. else if (mSelectionAnchor >= mTextBuffer.length() - 1)
  666. mSelectionAnchor = mTextBuffer.length();
  667. mVertMoveAnchorValid = false;
  668. setUpdate();
  669. }
  670. //--------------------------------------------------------------------------
  671. void GuiMLTextCtrl::onMouseDragged(const GuiEvent& event)
  672. {
  673. if (!mActive || (getRoot()->getMouseLockedControl() != this))
  674. return;
  675. Atom *hitAtom = findHitAtom(globalToLocalCoord(event.mousePoint));
  676. bool down = false;
  677. //note mHitURL can't be set unless this is (!mIsEditCtrl)
  678. if(hitAtom && hitAtom->url == mHitURL)
  679. down = true;
  680. if(mHitURL && down != mHitURL->mouseDown)
  681. mHitURL->mouseDown = down;
  682. if (!mHitURL)
  683. {
  684. S32 newSelection = 0;
  685. newSelection = getTextPosition(globalToLocalCoord(event.mousePoint));
  686. if (newSelection < 0)
  687. newSelection = 0;
  688. else if (newSelection > mTextBuffer.length())
  689. newSelection = mTextBuffer.length();
  690. if (newSelection == mSelectionAnchor)
  691. {
  692. mSelectionActive = false;
  693. }
  694. else if (newSelection > mSelectionAnchor)
  695. {
  696. mSelectionActive = true;
  697. mSelectionStart = mSelectionAnchor;
  698. mSelectionEnd = newSelection - 1;
  699. }
  700. else
  701. {
  702. mSelectionStart = newSelection;
  703. mSelectionEnd = mSelectionAnchor - 1;
  704. mSelectionActive = true;
  705. }
  706. setCursorPosition(newSelection);
  707. mDirty = true;
  708. }
  709. setUpdate();
  710. }
  711. //--------------------------------------------------------------------------
  712. void GuiMLTextCtrl::onMouseUp(const GuiEvent& event)
  713. {
  714. if (!mActive || (getRoot()->getMouseLockedControl() != this))
  715. return;
  716. mouseUnlock();
  717. //see if we've clicked on a URL
  718. Atom *hitAtom = findHitAtom(globalToLocalCoord(event.mousePoint));
  719. if (mHitURL && hitAtom && hitAtom->url == mHitURL && mHitURL->mouseDown)
  720. {
  721. mHitURL->mouseDown = false;
  722. // Convert URL from UTF16 to UTF8.
  723. UTF8* url = mTextBuffer.createSubstring8(mHitURL->textStart, mHitURL->len);
  724. onURL_callback(url);
  725. delete[] url;
  726. mHitURL = NULL;
  727. setUpdate();
  728. return;
  729. }
  730. //else, update our selection
  731. else
  732. {
  733. if ((event.mousePoint - mSelectionAnchorDropped).len() < 3)
  734. mSelectionActive = false;
  735. setCursorPosition(getTextPosition(globalToLocalCoord(event.mousePoint)));
  736. mVertMoveAnchorValid = false;
  737. setUpdate();
  738. }
  739. }
  740. //--------------------------------------------------------------------------
  741. void GuiMLTextCtrl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  742. {
  743. if(!mUseURLMouseCursor)
  744. {
  745. Parent::getCursor(cursor, showCursor, lastGuiEvent);
  746. return;
  747. }
  748. GuiCanvas *pRoot = getRoot();
  749. if( !pRoot )
  750. return;
  751. PlatformWindow *pWindow = pRoot->getPlatformWindow();
  752. AssertFatal(pWindow != NULL,"GuiControl without owning platform window! This should not be possible.");
  753. PlatformCursorController *pController = pWindow->getCursorController();
  754. AssertFatal(pController != NULL,"PlatformWindow without an owned CursorController!");
  755. Atom *hitAtom = findHitAtom(globalToLocalCoord(lastGuiEvent.mousePoint));
  756. if(hitAtom && !mIsEditCtrl && hitAtom->url)
  757. {
  758. if(pRoot->mCursorChanged != PlatformCursorController::curHand)
  759. {
  760. // We've already changed the cursor, so set it back before we change it again.
  761. if(pRoot->mCursorChanged != -1)
  762. pController->popCursor();
  763. // Now change the cursor shape
  764. pController->pushCursor(PlatformCursorController::curHand);
  765. pRoot->mCursorChanged = PlatformCursorController::curHand;
  766. }
  767. }
  768. else if(pRoot->mCursorChanged != -1)
  769. {
  770. // Restore the cursor we changed
  771. pController->popCursor();
  772. pRoot->mCursorChanged = -1;
  773. }
  774. }
  775. //--------------------------------------------------------------------------
  776. void GuiMLTextCtrl::insertChars(const char* inputChars,
  777. const U32 numInputChars,
  778. const U32 position)
  779. {
  780. AssertFatal(isSelectionActive() == false, "GuiMLTextCtrl::insertChars: don't use this function when there's a selection active!");
  781. AssertFatal(position <= mTextBuffer.length(), "GuiMLTextCtrl::insertChars: can't insert outside of current text!");
  782. //make sure the text will fit...
  783. S32 numCharsToInsert = numInputChars;
  784. if (mMaxBufferSize > 0 && mTextBuffer.length() + numInputChars + 1 > mMaxBufferSize)
  785. numCharsToInsert = mMaxBufferSize - mTextBuffer.length() - 1;
  786. if (numCharsToInsert <= 0)
  787. {
  788. // Play the "Denied" sound:
  789. if ( numInputChars > 0 && mDeniedSound )
  790. SFX->playOnce(mDeniedSound);
  791. return;
  792. }
  793. mTextBuffer.insert(position, inputChars );
  794. if (mCursorPosition >= position)
  795. {
  796. // Cursor was at or after the inserted text, move forward...
  797. mCursorPosition += numCharsToInsert;
  798. }
  799. AssertFatal(mCursorPosition <= mTextBuffer.length(), "GuiMLTextCtrl::insertChars: bad cursor position");
  800. mDirty = true;
  801. }
  802. //--------------------------------------------------------------------------
  803. void GuiMLTextCtrl::deleteChars(const U32 rangeStart,
  804. const U32 rangeEnd)
  805. {
  806. AssertFatal(isSelectionActive() == false, "GuiMLTextCtrl::deleteChars: don't use this function when there's a selection active");
  807. AssertFatal(rangeStart <= mTextBuffer.length() && rangeEnd <= mTextBuffer.length(),
  808. avar("GuiMLTextCtrl::deleteChars: can't delete outside of current text (%d, %d, %d)",
  809. rangeStart, rangeEnd, mTextBuffer.length()));
  810. AssertFatal(rangeStart <= rangeEnd, "GuiMLTextCtrl::deleteChars: invalid delete range");
  811. // Currently deleting text doesn't resize the text buffer, perhaps this should
  812. // change?
  813. mTextBuffer.cut(rangeStart, rangeEnd - rangeStart);
  814. if (mCursorPosition <= rangeStart)
  815. {
  816. // Cursor placed before deleted text, ignore
  817. }
  818. else if (mCursorPosition > rangeStart && mCursorPosition <= rangeEnd)
  819. {
  820. // Cursor inside deleted text, set to start of range
  821. mCursorPosition = rangeStart;
  822. }
  823. else
  824. {
  825. // Cursor after deleted text, decrement by number of chars deleted
  826. mCursorPosition -= (rangeEnd - rangeStart) + 1;
  827. }
  828. AssertFatal(mCursorPosition <= mTextBuffer.length(), "GuiMLTextCtrl::deleteChars: bad cursor position");
  829. mDirty = true;
  830. }
  831. //--------------------------------------------------------------------------
  832. void GuiMLTextCtrl::copyToClipboard(const U32 rangeStart, const U32 rangeEnd)
  833. {
  834. AssertFatal(rangeStart < mTextBuffer.length() && rangeEnd < mTextBuffer.length(),
  835. avar("GuiMLTextCtrl::copyToClipboard: can't copy outside of current text (%d, %d, %d)",
  836. rangeStart, rangeEnd, mTextBuffer.length()));
  837. AssertFatal(rangeStart <= rangeEnd, "GuiMLTextCtrl::copyToClipboard: invalid copy range");
  838. //copy the selection to the clipboard
  839. UTF8* selection = mTextBuffer.createSubstring8(rangeStart, rangeEnd-rangeStart+1);
  840. Platform::setClipboard(selection);
  841. delete[] selection;
  842. }
  843. //--------------------------------------------------------------------------
  844. bool GuiMLTextCtrl::isSelectionActive() const
  845. {
  846. return mSelectionActive;
  847. }
  848. //--------------------------------------------------------------------------
  849. void GuiMLTextCtrl::clearSelection()
  850. {
  851. mSelectionActive = false;
  852. mSelectionStart = 0;
  853. mSelectionEnd = 0;
  854. }
  855. //--------------------------------------------------------------------------
  856. void GuiMLTextCtrl::scrollToTag( U32 id )
  857. {
  858. // If the parent control is not a GuiScrollContentCtrl, then this call is invalid:
  859. GuiScrollCtrl *pappy = dynamic_cast<GuiScrollCtrl*>(getParent());
  860. if ( !pappy )
  861. return;
  862. // Find the indicated tag:
  863. LineTag* tag = NULL;
  864. for ( tag = mTagList; tag; tag = tag->next )
  865. {
  866. if ( tag->id == id )
  867. break;
  868. }
  869. if ( !tag )
  870. {
  871. Con::warnf( ConsoleLogEntry::General, "GuiMLTextCtrl::scrollToTag - tag id %d not found!", id );
  872. return;
  873. }
  874. pappy->scrollRectVisible(RectI(0, tag->y, 1, 1));
  875. }
  876. //--------------------------------------------------------------------------
  877. void GuiMLTextCtrl::scrollToTop()
  878. {
  879. // If the parent control is not a GuiScrollContentCtrl, then this call is invalid:
  880. GuiScrollCtrl *pappy = dynamic_cast<GuiScrollCtrl*>(getParent());
  881. if ( !pappy )
  882. return;
  883. pappy->scrollRectVisible(RectI(0,0,0,0));
  884. }
  885. //--------------------------------------------------------------------------
  886. void GuiMLTextCtrl::scrollToBottom()
  887. {
  888. // If the parent control is not a GuiScrollContentCtrl, then this call is invalid:
  889. GuiScrollCtrl *pappy = dynamic_cast<GuiScrollCtrl*>(getParent());
  890. if ( !pappy )
  891. return;
  892. // Figure bounds for the bottom left corner
  893. RectI cornerBounds (0, getPosition().y + getExtent().y, 1, 1);
  894. pappy->scrollRectVisible(cornerBounds);
  895. }
  896. //--------------------------------------------------------------------------
  897. GuiMLTextCtrl::Atom *GuiMLTextCtrl::findHitAtom(const Point2I localCoords)
  898. {
  899. AssertFatal(mAwake, "Can't get the text position of a sleeping control.");
  900. if(mDirty)
  901. reflow();
  902. for(Line *walk = mLineList; walk; walk = walk->next)
  903. {
  904. if(localCoords.y < walk->y)
  905. return NULL;
  906. if(localCoords.y >= walk->y && localCoords.y < walk->y + walk->height)
  907. {
  908. for(Atom *awalk = walk->atomList; awalk; awalk = awalk->next)
  909. {
  910. if(localCoords.x < awalk->xStart)
  911. return NULL;
  912. if(localCoords.x >= awalk->xStart + awalk->width)
  913. continue;
  914. return awalk;
  915. }
  916. }
  917. }
  918. return NULL;
  919. }
  920. //--------------------------------------------------------------------------
  921. S32 GuiMLTextCtrl::getTextPosition(const Point2I& localCoords)
  922. {
  923. AssertFatal(mAwake, "Can't get the text position of a sleeping control.");
  924. if(mDirty)
  925. reflow();
  926. for(Line *walk = mLineList; walk; walk = walk->next)
  927. {
  928. if((S32)localCoords.y < (S32)walk->y)
  929. return walk->textStart;
  930. if(localCoords.y >= walk->y && localCoords.y < walk->y + walk->height)
  931. {
  932. for(Atom *awalk = walk->atomList; awalk; awalk = awalk->next)
  933. {
  934. if(localCoords.x < awalk->xStart)
  935. return awalk->textStart;
  936. if(localCoords.x >= awalk->xStart + awalk->width)
  937. continue;
  938. // it's in the text block...
  939. GFont *font = awalk->style->font->fontRes;
  940. const UTF16 *tmp16 = mTextBuffer.getPtr() + awalk->textStart;
  941. U32 bp = font->getBreakPos(tmp16, awalk->len, localCoords.x - awalk->xStart, false);
  942. return awalk->textStart + bp;
  943. }
  944. return walk->textStart + walk->len;
  945. }
  946. }
  947. return mTextBuffer.length() - 1;
  948. }
  949. //--------------------------------------------------------------------------
  950. GuiMLTextCtrl::Font *GuiMLTextCtrl::allocFont(const char *faceName, U32 faceNameLen, U32 size)
  951. {
  952. // check if it's in the font list currently:
  953. for(Font *walk = mFontList; walk; walk = walk->next)
  954. if(faceNameLen == walk->faceNameLen &&
  955. !dStrncmp(walk->faceName, faceName, faceNameLen) &&
  956. size == walk->size)
  957. return walk;
  958. // Create!
  959. Font *ret;
  960. ret = constructInPlace((Font *) mResourceChunker.alloc(sizeof(Font)));
  961. ret->faceName = new char[faceNameLen+1];
  962. dStrncpy(ret->faceName, faceName, faceNameLen);
  963. ret->faceName[faceNameLen] = '\0';
  964. ret->faceNameLen = faceNameLen;
  965. ret->size = size;
  966. ret->next = mFontList;
  967. ret->fontRes = GFont::create(ret->faceName, size, GuiControlProfile::sFontCacheDirectory);
  968. if(ret->fontRes != NULL)
  969. {
  970. ret->next = mFontList;
  971. mFontList = ret;
  972. return ret;
  973. }
  974. return NULL;
  975. }
  976. //--------------------------------------------------------------------------
  977. GuiMLTextCtrl::Bitmap *GuiMLTextCtrl::allocBitmap(const char *bitmapName, U32 bitmapNameLen)
  978. {
  979. for(Bitmap *walk = mBitmapList; walk; walk = walk->next)
  980. if(bitmapNameLen == walk->bitmapNameLen &&
  981. !dStrncmp(walk->bitmapName, bitmapName, bitmapNameLen))
  982. return walk;
  983. Bitmap *ret = constructInPlace((Bitmap *) mResourceChunker.alloc(sizeof(Bitmap)));
  984. const U32 BitmapNameSize = sizeof(ret->bitmapName);
  985. dStrncpy(ret->bitmapName, bitmapName, getMin(bitmapNameLen,BitmapNameSize));
  986. if (bitmapNameLen < BitmapNameSize)
  987. ret->bitmapName[bitmapNameLen] = 0;
  988. else
  989. ret->bitmapName[BitmapNameSize - 1] = 0;
  990. ret->bitmapNameLen = bitmapNameLen;
  991. ret->bitmapObject.set(ret->bitmapName, &GFXMLTextureProfile, avar("%s() - ret->bitmapObject (line %d)", __FUNCTION__, __LINE__));
  992. //ret->bitmapObject.set(bitmapName, &GFXMLTextureProfile);
  993. if( bool(ret->bitmapObject) )
  994. {
  995. ret->next = mBitmapList;
  996. mBitmapList = ret;
  997. return ret;
  998. }
  999. return NULL;
  1000. }
  1001. //--------------------------------------------------------------------------
  1002. GuiMLTextCtrl::LineTag *GuiMLTextCtrl::allocLineTag(U32 id)
  1003. {
  1004. for ( LineTag* walk = mTagList; walk; walk = walk->next )
  1005. {
  1006. if ( walk->id == id )
  1007. {
  1008. Con::warnf( ConsoleLogEntry::General, "GuiMLTextCtrl - can't add duplicate line tags!" );
  1009. return( NULL );
  1010. }
  1011. }
  1012. LineTag* newTag = (LineTag*) mViewChunker.alloc( sizeof( LineTag ) );
  1013. newTag->id = id;
  1014. newTag->y = mCurY;
  1015. newTag->next = mTagList;
  1016. mTagList = newTag;
  1017. return( newTag );
  1018. }
  1019. //--------------------------------------------------------------------------
  1020. void GuiMLTextCtrl::emitNewLine(U32 textStart)
  1021. {
  1022. //clear any clipping
  1023. mCurClipX = 0;
  1024. Line *l = (Line *) mViewChunker.alloc(sizeof(Line));
  1025. l->height = mCurStyle->font->fontRes->getHeight();
  1026. l->y = mCurY;
  1027. l->textStart = mLineStart;
  1028. l->len = textStart - l->textStart;
  1029. mLineStart = textStart;
  1030. l->atomList = mLineAtoms;
  1031. l->next = 0;
  1032. l->divStyle = mCurDiv;
  1033. *mLineInsert = l;
  1034. mLineInsert = &(l->next);
  1035. mCurX = mCurLMargin;
  1036. mCurTabStop = 0;
  1037. if(mLineAtoms)
  1038. {
  1039. // scan through the atoms in the line, get the largest height
  1040. U32 maxBaseLine = 0;
  1041. U32 maxDescent = 0;
  1042. Atom* walk;
  1043. for(walk = mLineAtoms; walk; walk = walk->next)
  1044. {
  1045. if(walk->baseLine > maxBaseLine)
  1046. maxBaseLine = walk->baseLine;
  1047. if(walk->descent > maxDescent)
  1048. maxDescent = walk->descent;
  1049. if(!walk->next)
  1050. {
  1051. l->len = walk->textStart + walk->len - l->textStart;
  1052. mLineStart = walk->textStart + walk->len;
  1053. }
  1054. }
  1055. l->height = maxBaseLine + maxDescent;
  1056. for(walk = mLineAtoms; walk; walk = walk->next)
  1057. walk->yStart = mCurY + maxBaseLine - walk->baseLine;
  1058. }
  1059. mCurY += l->height;
  1060. mLineAtoms = NULL;
  1061. mLineAtomPtr = &mLineAtoms;
  1062. // clear out the blocker list
  1063. BitmapRef **blockList = &mBlockList;
  1064. while(*blockList)
  1065. {
  1066. BitmapRef *blk = *blockList;
  1067. if(blk->point.y + blk->extent.y <= mCurY)
  1068. *blockList = blk->nextBlocker;
  1069. else
  1070. blockList = &(blk->nextBlocker);
  1071. }
  1072. if(mCurY > mMaxY)
  1073. mMaxY = mCurY;
  1074. }
  1075. //--------------------------------------------------------------------------
  1076. void GuiMLTextCtrl::emitBitmapToken(GuiMLTextCtrl::Bitmap *bmp, U32 textStart, bool bitmapBreak)
  1077. {
  1078. if(mCurRMargin <= mCurLMargin)
  1079. return;
  1080. if(mCurRMargin - mCurLMargin < bmp->bitmapObject->getWidth())
  1081. return;
  1082. BitmapRef *ref = (BitmapRef *) mViewChunker.alloc(sizeof(BitmapRef));
  1083. ref->bitmap = bmp;
  1084. ref->next = mBitmapRefList;
  1085. mBitmapRefList = ref;
  1086. // now we gotta insert it into the blocker list and figure out where it's spos to go...
  1087. ref->extent.x = bmp->bitmapObject->getBitmapWidth();
  1088. ref->extent.y = bmp->bitmapObject->getBitmapHeight();
  1089. // find the first space in the blocker list that will fit this thats > curLMargin
  1090. while(bitmapBreak && mBlockList != &mSentinel)
  1091. emitNewLine(textStart);
  1092. for(;;)
  1093. {
  1094. // loop til we find a line that fits...
  1095. // we'll have to emitLine repeatedly to clear out the block lists...
  1096. BitmapRef **walk = &mBlockList;
  1097. U32 minx = mCurX;
  1098. U32 maxx = mCurRMargin;
  1099. while(*walk)
  1100. {
  1101. BitmapRef *blk = *walk;
  1102. if(blk->point.x > minx)
  1103. {
  1104. U32 right = maxx;
  1105. if(blk->point.x < right)
  1106. right = blk->point.x;
  1107. U32 width = right - minx;
  1108. if(right > minx && width >= ref->extent.x) // we've found the spot...
  1109. {
  1110. // insert it:
  1111. U32 x = minx;
  1112. if(mCurJustify == CenterJustify)
  1113. x += (width - ref->extent.x) >> 1;
  1114. else if(mCurJustify == RightJustify)
  1115. x += width - ref->extent.x;
  1116. ref->point.x = x;
  1117. ref->point.y = mCurY;
  1118. ref->nextBlocker = blk;
  1119. *walk = ref;
  1120. if(ref->point.y + ref->extent.y > mMaxY)
  1121. mMaxY = ref->point.y + ref->extent.y;
  1122. return;
  1123. }
  1124. }
  1125. if(minx < blk->point.x + blk->extent.x)
  1126. minx = blk->point.x + blk->extent.x;
  1127. // move on to the next blocker...
  1128. walk = &(blk->nextBlocker);
  1129. }
  1130. // go to the next line...
  1131. emitNewLine(textStart);
  1132. }
  1133. }
  1134. //--------------------------------------------------------------------------
  1135. void GuiMLTextCtrl::emitTextToken(U32 textStart, U32 len)
  1136. {
  1137. if(mCurRMargin <= mCurLMargin)
  1138. return;
  1139. GFont *font = mCurStyle->font->fontRes;
  1140. Atom *a = (Atom *) mViewChunker.alloc(sizeof(Atom));
  1141. a->url = mCurURL;
  1142. a->style = mCurStyle;
  1143. mCurStyle->used = true;
  1144. a->baseLine = font->getBaseline();
  1145. a->descent = font->getDescent();
  1146. a->textStart = textStart;
  1147. a->len = len;
  1148. a->isClipped = false;
  1149. a->next = NULL;
  1150. *mEmitAtomPtr = a;
  1151. mEmitAtomPtr = &(a->next);
  1152. }
  1153. //--------------------------------------------------------------------------
  1154. void GuiMLTextCtrl::processEmitAtoms()
  1155. {
  1156. Atom *atomList = mEmitAtoms;
  1157. mEmitAtoms = NULL;
  1158. mEmitAtomPtr = &mEmitAtoms;
  1159. bool bailout = false;
  1160. while(atomList)
  1161. {
  1162. // split the tokenlist by space
  1163. // first find the first space that the text can go into:
  1164. BitmapRef *br = mBlockList;
  1165. //bool bailout = false; // Scoping error here? Moved up one scope. -pw
  1166. Atom *list = atomList;
  1167. while(br && atomList)
  1168. {
  1169. // if the blocker is before the current x, ignore it.
  1170. if(br->point.x + br->extent.x <= mCurX)
  1171. {
  1172. br = br->nextBlocker;
  1173. continue;
  1174. }
  1175. // if cur x is in the middle of the blocker
  1176. // advance cur x to right edge of blocker.
  1177. if(mCurX >= br->point.x)
  1178. {
  1179. mCurX = br->point.x + br->extent.x;
  1180. br = br->nextBlocker;
  1181. continue;
  1182. }
  1183. // get the remaining width
  1184. U32 right = br->point.x;
  1185. if(right > mCurRMargin)
  1186. right = mCurRMargin;
  1187. //if we're clipping text, readjust
  1188. if (mCurClipX > 0 && right > mCurClipX)
  1189. right = mCurClipX;
  1190. // if there's no room, break to the next line...
  1191. if(right <= mCurX)
  1192. break;
  1193. // we've got some space:
  1194. U32 width = right - mCurX;
  1195. atomList = splitAtomListEmit(atomList, width);
  1196. if(atomList) // there's more, so advance cur x
  1197. {
  1198. mCurX = br->point.x + br->extent.x;
  1199. br = br->nextBlocker;
  1200. }
  1201. }
  1202. if(mBlockList == &mSentinel && atomList == list)
  1203. {
  1204. if(bailout)
  1205. return;
  1206. else
  1207. bailout = true;
  1208. }
  1209. // is there more to process for the next line?
  1210. if(atomList)
  1211. emitNewLine(mScanPos);
  1212. }
  1213. }
  1214. //--------------------------------------------------------------------------
  1215. GuiMLTextCtrl::Atom *GuiMLTextCtrl::splitAtomListEmit(Atom *list, U32 width)
  1216. {
  1217. U32 totalWidth = 0;
  1218. Atom *emitList = 0;
  1219. Atom **emitPtr = &emitList;
  1220. bool adjustClipAtom = false;
  1221. Atom *clipAtom = NULL;
  1222. bool emitted = false;
  1223. while(list)
  1224. {
  1225. GFont *font = list->style->font->fontRes;
  1226. U32 breakPos;
  1227. const UTF16 *tmp16 = mTextBuffer.getPtr() + list->textStart;
  1228. //if we're clipping the text, we don't break within an atom, we adjust the atom to only render
  1229. //the portion of text that does fit, and to ignore the rest...
  1230. if (mCurClipX > 0)
  1231. {
  1232. //find out how many character's fit within the given width
  1233. breakPos = font->getBreakPos(tmp16, list->len, width - totalWidth, false);
  1234. //if there isn't room for even the first character...
  1235. if (breakPos == 0)
  1236. {
  1237. //set the atom's len and width to prevent it from being drawn
  1238. list->len = 0;
  1239. list->width = 0;
  1240. adjustClipAtom = true;
  1241. }
  1242. //if our text doesn't fit within the clip region, add a "..."
  1243. else if (breakPos != list->len)
  1244. {
  1245. U32 etcWidth = font->getStrNWidthPrecise("...", 3);
  1246. breakPos = font->getBreakPos(tmp16, list->len, width - totalWidth - etcWidth, false);
  1247. //again, if there isn't even room for a single character before the "...."
  1248. if (breakPos == 0)
  1249. {
  1250. //set the atom's len and width to prevent it from being drawn
  1251. list->len = 0;
  1252. list->width = 0;
  1253. adjustClipAtom = true;
  1254. }
  1255. else
  1256. {
  1257. //set the char len to the break pos, and the rest of the characters in this atom will be ignored
  1258. list->len = breakPos;
  1259. list->width = width - totalWidth;
  1260. //mark this one as clipped
  1261. list->isClipped = true;
  1262. clipAtom = NULL;
  1263. }
  1264. }
  1265. //otherwise no need to treat this atom any differently..
  1266. else
  1267. {
  1268. //set the atom width == to the string length
  1269. list->width = font->getStrNWidthPrecise(tmp16, breakPos);
  1270. //set the pointer to the last atom that fit within the clip region
  1271. clipAtom = list;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. breakPos = font->getBreakPos(tmp16, list->len, width - totalWidth, true);
  1277. if(breakPos == 0 || (breakPos < list->len && mTextBuffer.getChar(list->textStart + breakPos - 1)!= ' ' && emitted))
  1278. break;
  1279. //set the atom width == to the string length
  1280. list->width = font->getStrNWidthPrecise(tmp16, breakPos);
  1281. }
  1282. //update the total width
  1283. totalWidth += list->width;
  1284. // see if this is the last atom that will fit:
  1285. Atom *emit = list;
  1286. *emitPtr = emit;
  1287. emitPtr = &(emit->next);
  1288. emitted = true;
  1289. //if we're clipping, don't split the atom, otherwise, see if it needs to be split
  1290. if(!list->isClipped && breakPos != list->len)
  1291. {
  1292. Atom *a = (Atom *) mViewChunker.alloc(sizeof(Atom));
  1293. a->url = list->url;
  1294. a->textStart = list->textStart + breakPos;
  1295. a->len = list->len - breakPos;
  1296. a->next = list->next;
  1297. a->baseLine = list->baseLine;
  1298. a->descent = list->descent;
  1299. a->style = list->style;
  1300. a->isClipped = false;
  1301. list = a;
  1302. emit->len = breakPos;
  1303. break;
  1304. }
  1305. list = list->next;
  1306. if(totalWidth > width)
  1307. break;
  1308. }
  1309. //if we had to completely clip an atom(s), the last (partially) visible atom should be modified to include a "..."
  1310. if (adjustClipAtom && clipAtom)
  1311. {
  1312. GFont *font = clipAtom->style->font->fontRes;
  1313. U32 breakPos;
  1314. U32 etcWidth = font->getStrNWidthPrecise("...", 3);
  1315. const UTF16 *tmp16 = mTextBuffer.getPtr() + clipAtom->textStart;
  1316. breakPos = font->getBreakPos(tmp16, clipAtom->len, clipAtom->width - etcWidth, false);
  1317. if (breakPos != 0)
  1318. {
  1319. clipAtom->isClipped = true;
  1320. clipAtom->len = breakPos;
  1321. }
  1322. }
  1323. // terminate the emit list:
  1324. *emitPtr = 0;
  1325. // now emit it:
  1326. // going from mCurX to mCurX + width:
  1327. if(mCurJustify == CenterJustify)
  1328. {
  1329. if ( width > totalWidth )
  1330. mCurX += (width - totalWidth) >> 1;
  1331. }
  1332. else if(mCurJustify == RightJustify)
  1333. {
  1334. if ( width > totalWidth )
  1335. mCurX += width - totalWidth;
  1336. }
  1337. while(emitList)
  1338. {
  1339. emitList->xStart = mCurX;
  1340. mCurX += emitList->width;
  1341. Atom *temp = emitList->next;
  1342. *mLineAtomPtr = emitList;
  1343. emitList->next = 0;
  1344. mLineAtomPtr = &(emitList->next);
  1345. emitList = temp;
  1346. }
  1347. return list;
  1348. }
  1349. //--------------------------------------------------------------------------
  1350. static bool scanforchar(const char *str, U32 &idx, char c)
  1351. {
  1352. U32 startidx = idx;
  1353. while(str[idx] != c && str[idx] && str[idx] != ':' && str[idx] != '>' && str[idx] != '\n')
  1354. idx++;
  1355. return str[idx] == c && startidx != idx;
  1356. }
  1357. //--------------------------------------------------------------------------
  1358. static bool scanforURL(const char *str, U32 &idx, char c)
  1359. {
  1360. U32 startidx = idx;
  1361. while(str[idx] != c && str[idx] && str[idx] != '>' && str[idx] != '\n')
  1362. idx++;
  1363. return str[idx] == c && startidx != idx;
  1364. }
  1365. //--------------------------------------------------------------------------
  1366. static S32 getHexVal(char c)
  1367. {
  1368. if(c >= '0' && c <= '9')
  1369. return c - '0';
  1370. else if(c >= 'A' && c <= 'Z')
  1371. return c - 'A' + 10;
  1372. else if(c >= 'a' && c <= 'z')
  1373. return c - 'a' + 10;
  1374. return -1;
  1375. }
  1376. //--------------------------------------------------------------------------
  1377. GuiMLTextCtrl::Style *GuiMLTextCtrl::allocStyle(GuiMLTextCtrl::Style *style)
  1378. {
  1379. Style *ret = (Style *) mViewChunker.alloc(sizeof(Style));
  1380. ret->used = false;
  1381. if(style)
  1382. {
  1383. ret->font = style->font;
  1384. ret->color = style->color;
  1385. ret->linkColor = style->linkColor;
  1386. ret->linkColorHL = style->linkColorHL;
  1387. ret->shadowColor = style->shadowColor;
  1388. ret->shadowOffset = style->shadowOffset;
  1389. ret->next = style->next;
  1390. }
  1391. else
  1392. {
  1393. ret->font = 0;
  1394. ret->next = 0;
  1395. }
  1396. return ret;
  1397. }
  1398. //--------------------------------------------------------------------------
  1399. void GuiMLTextCtrl::reflow()
  1400. {
  1401. AssertFatal(mAwake, "Can't reflow a sleeping control.");
  1402. freeLineBuffers();
  1403. mDirty = false;
  1404. mScanPos = 0;
  1405. mLineList = NULL;
  1406. mLineInsert = &mLineList;
  1407. mCurStyle = allocStyle(NULL);
  1408. mCurStyle->font = allocFont((char *) mProfile->mFontType, dStrlen(mProfile->mFontType), mProfile->mFontSize);
  1409. if(!mCurStyle->font)
  1410. return;
  1411. mCurStyle->color = mProfile->mFontColor;
  1412. mCurStyle->shadowColor = mProfile->mFontColor;
  1413. mCurStyle->shadowOffset.set(0,0);
  1414. mCurStyle->linkColor = mProfile->mFontColors[GuiControlProfile::ColorUser0];
  1415. mCurStyle->linkColorHL = mProfile->mFontColors[GuiControlProfile::ColorUser1];
  1416. U32 width = getWidth();
  1417. mCurLMargin = 0;
  1418. mCurRMargin = width;
  1419. mCurJustify = LeftJustify;
  1420. mCurDiv = 0;
  1421. mCurY = 0;
  1422. mCurX = 0;
  1423. mCurClipX = 0;
  1424. mLineAtoms = NULL;
  1425. mLineAtomPtr = &mLineAtoms;
  1426. mSentinel.point.x = width;
  1427. mSentinel.point.y = 0;
  1428. mSentinel.extent.x = 0;
  1429. mSentinel.extent.y = 0x7FFFFF;
  1430. mSentinel.nextBlocker = NULL;
  1431. mLineStart = 0;
  1432. mEmitAtoms = 0;
  1433. mMaxY = 0;
  1434. mEmitAtomPtr = &mEmitAtoms;
  1435. mBlockList = &mSentinel;
  1436. Font *nextFont;
  1437. LineTag *nextTag;
  1438. mTabStops = 0;
  1439. mCurTabStop = 0;
  1440. mTabStopCount = 0;
  1441. mCurURL = 0;
  1442. Style *newStyle;
  1443. U32 textStart;
  1444. U32 len;
  1445. U32 idx;
  1446. U32 sizidx;
  1447. for(;;)
  1448. {
  1449. UTF16 curChar = mTextBuffer.getChar(mScanPos);
  1450. if(!curChar)
  1451. break;
  1452. if(curChar == '\n')
  1453. {
  1454. textStart = mScanPos;
  1455. len = 1;
  1456. mScanPos++;
  1457. processEmitAtoms();
  1458. emitNewLine(textStart);
  1459. mCurDiv = 0;
  1460. continue;
  1461. }
  1462. if(curChar == '\t')
  1463. {
  1464. textStart = mScanPos;
  1465. len = 1;
  1466. mScanPos++;
  1467. processEmitAtoms();
  1468. if(mTabStopCount)
  1469. {
  1470. if(mCurTabStop < mTabStopCount)
  1471. {
  1472. if(mCurX < mTabStops[mCurTabStop])
  1473. mCurX = mTabStops[mCurTabStop];
  1474. }
  1475. mCurTabStop++;
  1476. }
  1477. continue;
  1478. }
  1479. if(curChar == '<')
  1480. {
  1481. // it's probably some kind of tag:
  1482. // Get a pointer into the utf8 version of the buffer,
  1483. // because we're still scanning text in in utf8 mode.
  1484. const UTF8 *str = mTextBuffer.getPtr8();
  1485. str = getNthCodepoint(str, mScanPos);
  1486. // And go!
  1487. if(!dStrnicmp(str + 1, "br>", 3))
  1488. {
  1489. mScanPos += 4;
  1490. len = 4;
  1491. textStart = mScanPos + 4;
  1492. processEmitAtoms();
  1493. emitNewLine(textStart);
  1494. mCurDiv = 0;
  1495. continue;
  1496. }
  1497. if(!dStrnicmp(str + 1, "font:", 5))
  1498. {
  1499. // scan for the second colon...
  1500. // at each level it should drop out to the text case below...
  1501. idx = 6;
  1502. if(!scanforchar(str, idx, ':'))
  1503. goto textemit;
  1504. sizidx = idx + 1;
  1505. if(!scanforchar(str, sizidx, '>'))
  1506. goto textemit;
  1507. U32 size = dAtoi(str + idx + 1);
  1508. if(!size || size > 64)
  1509. goto textemit;
  1510. textStart = mScanPos + 6;
  1511. len = idx - 6;
  1512. mScanPos += sizidx + 1;
  1513. nextFont = allocFont(str + 6, len, size);
  1514. if(nextFont)
  1515. {
  1516. if(mCurStyle->used)
  1517. mCurStyle = allocStyle(mCurStyle);
  1518. mCurStyle->font = nextFont;
  1519. }
  1520. continue;
  1521. }
  1522. if ( !dStrnicmp( str + 1, "tag:", 4 ) )
  1523. {
  1524. idx = 5;
  1525. if ( !scanforchar( str, idx, '>' ) )
  1526. goto textemit;
  1527. U32 tagId = dAtoi( str + 5 );
  1528. nextTag = allocLineTag( tagId );
  1529. mScanPos += idx + 1;
  1530. continue;
  1531. }
  1532. if(!dStrnicmp(str +1, "color:", 6))
  1533. {
  1534. idx = 7;
  1535. if(!scanforchar(str, idx, '>'))
  1536. goto textemit;
  1537. if(idx != 13 && idx != 15)
  1538. goto textemit;
  1539. ColorI color;
  1540. color.red = getHexVal(str[7]) * 16 + getHexVal(str[8]);
  1541. color.green = getHexVal(str[9]) * 16 + getHexVal(str[10]);
  1542. color.blue = getHexVal(str[11]) * 16 + getHexVal(str[12]);
  1543. if(idx == 15)
  1544. color.alpha = getHexVal(str[13]) * 16 + getHexVal(str[14]);
  1545. else
  1546. color.alpha = 255;
  1547. mScanPos += idx + 1;
  1548. if(mCurStyle->used)
  1549. mCurStyle = allocStyle(mCurStyle);
  1550. mCurStyle->color = color;
  1551. continue;
  1552. }
  1553. if(!dStrnicmp(str +1, "shadowcolor:", 12))
  1554. {
  1555. idx = 13;
  1556. if(!scanforchar(str, idx, '>'))
  1557. goto textemit;
  1558. if(idx != 19 && idx != 21)
  1559. goto textemit;
  1560. ColorI color;
  1561. color.red = getHexVal(str[13]) * 16 + getHexVal(str[14]);
  1562. color.green = getHexVal(str[15]) * 16 + getHexVal(str[16]);
  1563. color.blue = getHexVal(str[17]) * 16 + getHexVal(str[18]);
  1564. if(idx == 21)
  1565. color.alpha = getHexVal(str[19]) * 16 + getHexVal(str[20]);
  1566. else
  1567. color.alpha = 255;
  1568. mScanPos += idx + 1;
  1569. if(mCurStyle->used)
  1570. mCurStyle = allocStyle(mCurStyle);
  1571. mCurStyle->shadowColor = color;
  1572. continue;
  1573. }
  1574. if(!dStrnicmp(str +1, "linkcolor:", 10))
  1575. {
  1576. idx = 11;
  1577. if(!scanforchar(str, idx, '>'))
  1578. goto textemit;
  1579. if(idx != 17 && idx != 19)
  1580. goto textemit;
  1581. ColorI color;
  1582. color.red = getHexVal(str[11]) * 16 + getHexVal(str[12]);
  1583. color.green = getHexVal(str[13]) * 16 + getHexVal(str[14]);
  1584. color.blue = getHexVal(str[15]) * 16 + getHexVal(str[16]);
  1585. if(idx == 19)
  1586. color.alpha = getHexVal(str[17]) * 16 + getHexVal(str[18]);
  1587. else
  1588. color.alpha = 255;
  1589. mScanPos += idx + 1;
  1590. if(mCurStyle->used)
  1591. mCurStyle = allocStyle(mCurStyle);
  1592. mCurStyle->linkColor = color;
  1593. continue;
  1594. }
  1595. if(!dStrnicmp(str +1, "linkcolorhl:", 12))
  1596. {
  1597. idx = 13;
  1598. if(!scanforchar(str, idx, '>'))
  1599. goto textemit;
  1600. if(idx != 19 && idx != 21)
  1601. goto textemit;
  1602. ColorI color;
  1603. color.red = getHexVal(str[13]) * 16 + getHexVal(str[14]);
  1604. color.green = getHexVal(str[15]) * 16 + getHexVal(str[16]);
  1605. color.blue = getHexVal(str[17]) * 16 + getHexVal(str[18]);
  1606. if(idx == 21)
  1607. color.alpha = getHexVal(str[19]) * 16 + getHexVal(str[20]);
  1608. else
  1609. color.alpha = 255;
  1610. mScanPos += idx + 1;
  1611. if(mCurStyle->used)
  1612. mCurStyle = allocStyle(mCurStyle);
  1613. mCurStyle->linkColorHL = color;
  1614. continue;
  1615. }
  1616. if(!dStrnicmp(str +1, "shadow:", 7))
  1617. {
  1618. idx = 8;
  1619. if(!scanforchar(str, idx, ':'))
  1620. goto textemit;
  1621. U32 yidx = idx + 1;
  1622. if(!scanforchar(str, yidx, '>'))
  1623. goto textemit;
  1624. mScanPos += yidx + 1;
  1625. Point2I offset;
  1626. offset.x = dAtoi(str + 8);
  1627. offset.y = dAtoi(str + idx + 1);
  1628. if(mCurStyle->used)
  1629. mCurStyle = allocStyle(mCurStyle);
  1630. mCurStyle->shadowOffset = offset;
  1631. continue;
  1632. }
  1633. if(!dStrnicmp(str +1, "bitmap", 6))
  1634. {
  1635. S32 start = 8;
  1636. bool bitBrk = false;
  1637. if(str[7] == 'k' && str[8] == ':')
  1638. {
  1639. bitBrk = true;
  1640. start = 9;
  1641. }
  1642. else if(str[7] != ':')
  1643. goto textemit;
  1644. idx = start;
  1645. if(!scanforchar(str, idx, '>'))
  1646. goto textemit;
  1647. textStart = mScanPos + start;
  1648. len = idx - start;
  1649. mScanPos += idx + 1;
  1650. processEmitAtoms();
  1651. Bitmap *bmp;
  1652. bmp = allocBitmap(str + 8, len);
  1653. if(bmp)
  1654. emitBitmapToken(bmp, textStart, bitBrk);
  1655. continue;
  1656. }
  1657. if(!dStrnicmp(str +1, "spush>", 6))
  1658. {
  1659. mScanPos += 7;
  1660. newStyle = allocStyle(mCurStyle); // copy out all the attributes...
  1661. newStyle->next = mCurStyle;
  1662. mCurStyle = newStyle;
  1663. continue;
  1664. }
  1665. if(!dStrnicmp(str +1, "spop>", 5))
  1666. {
  1667. mScanPos += 6;
  1668. if(mCurStyle->next)
  1669. mCurStyle = mCurStyle->next;
  1670. continue;
  1671. }
  1672. if(!dStrnicmp(str +1, "sbreak>", 7))
  1673. {
  1674. mScanPos += 8;
  1675. processEmitAtoms();
  1676. while(mBlockList != &mSentinel)
  1677. emitNewLine(mScanPos);
  1678. continue;
  1679. }
  1680. if(!dStrnicmp(str +1, "just:left>", 10))
  1681. {
  1682. processEmitAtoms();
  1683. mCurJustify = LeftJustify;
  1684. mScanPos += 11;
  1685. continue;
  1686. }
  1687. if(!dStrnicmp(str +1, "just:right>", 11))
  1688. {
  1689. processEmitAtoms();
  1690. mCurJustify = RightJustify;
  1691. mScanPos += 12;
  1692. continue;
  1693. }
  1694. if(!dStrnicmp(str +1, "just:center>", 12))
  1695. {
  1696. processEmitAtoms();
  1697. mCurJustify = CenterJustify;
  1698. mScanPos += 13;
  1699. continue;
  1700. }
  1701. if(!dStrnicmp(str +1, "a:", 2))
  1702. {
  1703. idx = 3;
  1704. if(!scanforURL(str, idx, '>'))
  1705. goto textemit;
  1706. mCurURL = (URL *) mViewChunker.alloc(sizeof(URL));
  1707. mCurURL->mouseDown = false;
  1708. mCurURL->textStart = mScanPos + 3;
  1709. mCurURL->len = idx - 3;
  1710. mCurURL->noUnderline = false;
  1711. //if the URL is a "gamelink", don't underline...
  1712. if (!dStrnicmp(str + 3, "gamelink", 8))
  1713. mCurURL->noUnderline = true;
  1714. mScanPos += idx + 1;
  1715. continue;
  1716. }
  1717. if(!dStrnicmp(str+1, "/a>", 3))
  1718. {
  1719. mCurURL = NULL;
  1720. mScanPos += 4;
  1721. continue;
  1722. }
  1723. U32 margin;
  1724. if(!dStrnicmp(str + 1, "lmargin%:", 9))
  1725. {
  1726. idx = 10;
  1727. if(!scanforchar(str, idx, '>'))
  1728. goto textemit;
  1729. margin = (getWidth() * dAtoi(str + 10)) / 100;
  1730. mScanPos += idx + 1;
  1731. goto setleftmargin;
  1732. }
  1733. if(!dStrnicmp(str + 1, "lmargin:", 8))
  1734. {
  1735. idx = 9;
  1736. if(!scanforchar(str, idx, '>'))
  1737. goto textemit;
  1738. margin = dAtoi(str + 9);
  1739. mScanPos += idx + 1;
  1740. setleftmargin:
  1741. processEmitAtoms();
  1742. U32 oldLMargin;
  1743. oldLMargin = mCurLMargin;
  1744. mCurLMargin = margin;
  1745. if(mCurLMargin >= width)
  1746. mCurLMargin = width - 1;
  1747. if(mCurX == oldLMargin)
  1748. mCurX = mCurLMargin;
  1749. if(mCurX < mCurLMargin)
  1750. mCurX = mCurLMargin;
  1751. continue;
  1752. }
  1753. if(!dStrnicmp(str + 1, "rmargin%:", 9))
  1754. {
  1755. idx = 10;
  1756. if(!scanforchar(str, idx, '>'))
  1757. goto textemit;
  1758. margin = (getWidth() * dAtoi(str + 10)) / 100;
  1759. mScanPos += idx + 1;
  1760. goto setrightmargin;
  1761. }
  1762. if(!dStrnicmp(str + 1, "rmargin:", 8))
  1763. {
  1764. idx = 9;
  1765. if(!scanforchar(str, idx, '>'))
  1766. goto textemit;
  1767. margin = dAtoi(str + 9);
  1768. mScanPos += idx + 1;
  1769. setrightmargin:
  1770. processEmitAtoms();
  1771. mCurRMargin = margin;
  1772. if(mCurLMargin >= width)
  1773. mCurLMargin = width;
  1774. if (mCurClipX > mCurRMargin)
  1775. mCurClipX = mCurRMargin;
  1776. continue;
  1777. }
  1778. if(!dStrnicmp(str + 1, "clip:", 5))
  1779. {
  1780. U32 clipWidth = 0;
  1781. idx = 6;
  1782. if(!scanforchar(str, idx, '>'))
  1783. goto textemit;
  1784. clipWidth = dAtoi(str + 6);
  1785. mScanPos += idx + 1;
  1786. processEmitAtoms();
  1787. if (clipWidth > 0)
  1788. mCurClipX = mCurX + clipWidth;
  1789. else
  1790. mCurClipX = 0;
  1791. if(mCurClipX > mCurRMargin)
  1792. mCurClipX = mCurRMargin;
  1793. continue;
  1794. }
  1795. if(!dStrnicmp(str + 1, "/clip>", 6))
  1796. {
  1797. processEmitAtoms();
  1798. mCurClipX = 0;
  1799. mScanPos += 7;
  1800. continue;
  1801. }
  1802. if(!dStrnicmp(str + 1, "div:", 4))
  1803. {
  1804. idx = 5;
  1805. if(!scanforchar(str, idx, '>'))
  1806. goto textemit;
  1807. mScanPos += idx + 1;
  1808. mCurDiv = dAtoi(str + 5);
  1809. continue;
  1810. }
  1811. if(!dStrnicmp(str + 1, "tab:", 4))
  1812. {
  1813. idx = 5;
  1814. if(!scanforchar(str, idx, '>'))
  1815. goto textemit;
  1816. // scan for tab stops...
  1817. mTabStopCount = 1;
  1818. idx = 5;
  1819. while(scanforchar(str, idx, ','))
  1820. {
  1821. idx++;
  1822. mTabStopCount++;
  1823. }
  1824. idx = 5;
  1825. mTabStops = (U32 *) mViewChunker.alloc(sizeof(U32) * mTabStopCount);
  1826. mTabStops[0] = dAtoi(str + idx);
  1827. U32 i = 1;
  1828. while(scanforchar(str, idx, ','))
  1829. {
  1830. idx++;
  1831. mTabStops[i] = dAtoi(str + idx);
  1832. i++;
  1833. }
  1834. mScanPos += idx + 1;
  1835. continue;
  1836. }
  1837. }
  1838. // default case:
  1839. textemit:
  1840. textStart = mScanPos;
  1841. idx = 1;
  1842. while(mTextBuffer.getChar(mScanPos+idx) != '\t' && mTextBuffer.getChar(mScanPos+idx) != '<' && mTextBuffer.getChar(mScanPos+idx) != '\n' && mTextBuffer.getChar(mScanPos+idx))
  1843. idx++;
  1844. len = idx;
  1845. mScanPos += idx;
  1846. emitTextToken(textStart, len);
  1847. }
  1848. processEmitAtoms();
  1849. emitNewLine(mScanPos);
  1850. setHeight( mMaxY );
  1851. onResize_callback( getWidth(), mMaxY );
  1852. //make sure the cursor is still visible - this handles if we're a child of a scroll ctrl...
  1853. ensureCursorOnScreen();
  1854. }
  1855. //-----------------------------------------------------------------------------
  1856. char* GuiMLTextCtrl::stripControlChars(const char *inString)
  1857. {
  1858. if (! bool(inString))
  1859. return NULL;
  1860. U32 maxBufLength = 64;
  1861. char *strippedBuffer = Con::getReturnBuffer(maxBufLength);
  1862. char *stripBufPtr = &strippedBuffer[0];
  1863. const char *bufPtr = (char *) inString;
  1864. U32 idx, sizidx;
  1865. for(;;)
  1866. {
  1867. //if we've reached the end of the string, or run out of room in the stripped Buffer...
  1868. if(*bufPtr == '\0' || (U32(stripBufPtr - strippedBuffer) >= maxBufLength - 1))
  1869. break;
  1870. if (*bufPtr == '\n')
  1871. {
  1872. U32 walked;
  1873. oneUTF8toUTF32(bufPtr,&walked);
  1874. bufPtr += walked;
  1875. continue;
  1876. }
  1877. if(*bufPtr == '\t')
  1878. {
  1879. U32 walked;
  1880. oneUTF8toUTF32(bufPtr,&walked);
  1881. bufPtr += walked;
  1882. continue;
  1883. }
  1884. if(*bufPtr < 0x20 && *bufPtr >= 0)
  1885. {
  1886. U32 walked;
  1887. oneUTF8toUTF32(bufPtr,&walked);
  1888. bufPtr += walked;
  1889. continue;
  1890. }
  1891. if(*bufPtr == '<')
  1892. {
  1893. // it's probably some kind of tag:
  1894. if(!dStrnicmp(bufPtr + 1, "font:", 5))
  1895. {
  1896. // scan for the second colon...
  1897. // at each level it should drop out to the text case below...
  1898. idx = 6;
  1899. if(!scanforchar((char*)bufPtr, idx, ':'))
  1900. goto textemit;
  1901. sizidx = idx + 1;
  1902. if(!scanforchar((char*)bufPtr, sizidx, '>'))
  1903. goto textemit;
  1904. bufPtr += sizidx + 1;
  1905. continue;
  1906. }
  1907. if (!dStrnicmp(bufPtr + 1, "tag:", 4 ))
  1908. {
  1909. idx = 5;
  1910. if ( !scanforchar((char*)bufPtr, idx, '>' ))
  1911. goto textemit;
  1912. bufPtr += idx + 1;
  1913. continue;
  1914. }
  1915. if(!dStrnicmp(bufPtr + 1, "color:", 6))
  1916. {
  1917. idx = 7;
  1918. if(!scanforchar((char*)bufPtr, idx, '>'))
  1919. goto textemit;
  1920. if(idx != 13)
  1921. goto textemit;
  1922. bufPtr += 14;
  1923. continue;
  1924. }
  1925. if(!dStrnicmp(bufPtr +1, "bitmap:", 7))
  1926. {
  1927. idx = 8;
  1928. if(!scanforchar((char*)bufPtr, idx, '>'))
  1929. goto textemit;
  1930. bufPtr += idx + 1;
  1931. continue;
  1932. }
  1933. if(!dStrnicmp(bufPtr +1, "spush>", 6))
  1934. {
  1935. bufPtr += 7;
  1936. continue;
  1937. }
  1938. if(!dStrnicmp(bufPtr +1, "spop>", 5))
  1939. {
  1940. bufPtr += 6;
  1941. continue;
  1942. }
  1943. if(!dStrnicmp(bufPtr +1, "sbreak>", 7))
  1944. {
  1945. bufPtr += 8;
  1946. continue;
  1947. }
  1948. if(!dStrnicmp(bufPtr +1, "just:left>", 10))
  1949. {
  1950. bufPtr += 11;
  1951. continue;
  1952. }
  1953. if(!dStrnicmp(bufPtr +1, "just:right>", 11))
  1954. {
  1955. bufPtr += 12;
  1956. continue;
  1957. }
  1958. if(!dStrnicmp(bufPtr +1, "just:center>", 12))
  1959. {
  1960. bufPtr += 13;
  1961. continue;
  1962. }
  1963. if(!dStrnicmp(bufPtr +1, "a:", 2))
  1964. {
  1965. idx = 3;
  1966. if(!scanforchar((char*)bufPtr, idx, '>'))
  1967. goto textemit;
  1968. bufPtr += idx + 1;
  1969. continue;
  1970. }
  1971. if(!dStrnicmp(bufPtr+1, "/a>", 3))
  1972. {
  1973. bufPtr += 4;
  1974. continue;
  1975. }
  1976. if(!dStrnicmp(bufPtr + 1, "lmargin%:", 9))
  1977. {
  1978. idx = 10;
  1979. if(!scanforchar((char*)bufPtr, idx, '>'))
  1980. goto textemit;
  1981. bufPtr += idx + 1;
  1982. goto setleftmargin;
  1983. }
  1984. if(!dStrnicmp(bufPtr + 1, "lmargin:", 8))
  1985. {
  1986. idx = 9;
  1987. if(!scanforchar((char*)bufPtr, idx, '>'))
  1988. goto textemit;
  1989. bufPtr += idx + 1;
  1990. setleftmargin:
  1991. continue;
  1992. }
  1993. if(!dStrnicmp(bufPtr + 1, "rmargin%:", 9))
  1994. {
  1995. idx = 10;
  1996. if(!scanforchar((char*)bufPtr, idx, '>'))
  1997. goto textemit;
  1998. bufPtr += idx + 1;
  1999. goto setrightmargin;
  2000. }
  2001. if(!dStrnicmp(bufPtr + 1, "rmargin:", 8))
  2002. {
  2003. idx = 9;
  2004. if(!scanforchar((char*)bufPtr, idx, '>'))
  2005. goto textemit;
  2006. bufPtr += idx + 1;
  2007. setrightmargin:
  2008. continue;
  2009. }
  2010. if(!dStrnicmp(bufPtr + 1, "clip:", 5))
  2011. {
  2012. idx = 6;
  2013. if(!scanforchar((char*)bufPtr, idx, '>'))
  2014. goto textemit;
  2015. bufPtr += idx + 1;
  2016. continue;
  2017. }
  2018. if(!dStrnicmp(bufPtr + 1, "/clip>", 6))
  2019. {
  2020. bufPtr += 7;
  2021. continue;
  2022. }
  2023. if(!dStrnicmp(bufPtr + 1, "div:", 4))
  2024. {
  2025. idx = 5;
  2026. if(!scanforchar((char*)bufPtr, idx, '>'))
  2027. goto textemit;
  2028. bufPtr += idx + 1;
  2029. continue;
  2030. }
  2031. if(!dStrnicmp(bufPtr + 1, "tab:", 4))
  2032. {
  2033. idx = 5;
  2034. if(!scanforchar((char*)bufPtr, idx, '>'))
  2035. goto textemit;
  2036. bufPtr += idx + 1;
  2037. continue;
  2038. }
  2039. }
  2040. // default case:
  2041. textemit:
  2042. *stripBufPtr++ = *bufPtr++;
  2043. while(*bufPtr != '\t' && *bufPtr != '<' && *bufPtr != '\n' && (*bufPtr >= 0x20 || *bufPtr < 0))
  2044. *stripBufPtr++ = *bufPtr++;
  2045. }
  2046. //we're finished - terminate the string
  2047. *stripBufPtr = '\0';
  2048. return strippedBuffer;
  2049. }
  2050. //--------------------------------------------------------------------------
  2051. bool GuiMLTextCtrl::resize( const Point2I& newPosition, const Point2I& newExtent )
  2052. {
  2053. if( Parent::resize( newPosition, newExtent ) )
  2054. {
  2055. mDirty = true;
  2056. return true;
  2057. }
  2058. return false;
  2059. }