guiMLTextCtrl.cpp 70 KB

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