2
0

guiMLTextCtrl.cpp 70 KB

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