guiMLTextCtrl.cpp 73 KB

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