2
0

guiMLTextCtrl.cpp 73 KB

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