editctrl.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Combat *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/editctrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/17/02 1:26p $*
  29. * *
  30. * $Revision:: 25 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "editctrl.h"
  36. #include "assetmgr.h"
  37. #include "refcount.h"
  38. #include "font3d.h"
  39. #include "mousemgr.h"
  40. #include "ww3d.h"
  41. #include "dialogmgr.h"
  42. #include "stylemgr.h"
  43. #include "dialogbase.h"
  44. ////////////////////////////////////////////////////////////////
  45. //
  46. // EditCtrlClass
  47. //
  48. ////////////////////////////////////////////////////////////////
  49. EditCtrlClass::EditCtrlClass (void) :
  50. CaretPos (0),
  51. ScrollPos (0),
  52. NumChars(0),
  53. TextLimit(0xFFFF),
  54. CaretBlinkDelay (500),
  55. LastCaretBlink (0),
  56. HilightStartPos (-1),
  57. HilightEndPos (-1),
  58. HilightAnchorPos (-1),
  59. WasButtonPressedOnMe (false),
  60. IsCaretDisplayed (false),
  61. mInComposition(false)
  62. #ifdef SHOW_IME_TYPING
  63. , mShowIMETypingText(false)
  64. #endif
  65. {
  66. //
  67. // Set the font for the text renderers
  68. //
  69. StyleMgrClass::Assign_Font (&TextRenderer, StyleMgrClass::FONT_CONTROLS);
  70. StyleMgrClass::Configure_Renderer (&ControlRenderer);
  71. StyleMgrClass::Configure_Renderer (&CaretRenderer);
  72. StyleMgrClass::Configure_Renderer (&HilightRenderer);
  73. mIME = DialogMgrClass::Get_IME();
  74. }
  75. ////////////////////////////////////////////////////////////////
  76. //
  77. // ~EditCtrlClass
  78. //
  79. ////////////////////////////////////////////////////////////////
  80. EditCtrlClass::~EditCtrlClass (void)
  81. {
  82. if (mIME) {
  83. mIME->Release_Ref();
  84. }
  85. return ;
  86. }
  87. ////////////////////////////////////////////////////////////////
  88. //
  89. // Create_Text_Renderers
  90. //
  91. ////////////////////////////////////////////////////////////////
  92. void
  93. EditCtrlClass::Create_Text_Renderers (void)
  94. {
  95. HilightRenderer.Reset ();
  96. HilightRenderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
  97. StyleMgrClass::Configure_Hilighter (&HilightRenderer);
  98. //
  99. // Get the string we'll be displaying
  100. //
  101. WideStringClass display_string(0, true);
  102. Get_Display_Text(display_string);
  103. //
  104. // Index into the text buffer
  105. //
  106. const WCHAR* text = display_string.Peek_Buffer();
  107. if (ScrollPos >= 0 && ScrollPos < display_string.Get_Length()) {
  108. text += ScrollPos;
  109. }
  110. //
  111. // Start fresh
  112. //
  113. TextRenderer.Reset();
  114. //
  115. // Draw the text
  116. //
  117. uint32 color = StyleMgrClass::Get_Text_Color();
  118. uint32 shadowColor = StyleMgrClass::Get_Text_Shadow_Color();
  119. if (IsEnabled == false) {
  120. color = StyleMgrClass::Get_Disabled_Text_Color();
  121. shadowColor = StyleMgrClass::Get_Disabled_Text_Shadow_Color();
  122. }
  123. StyleMgrClass::Render_Text(text, &TextRenderer, color, shadowColor,
  124. ClientRect, true, true, StyleMgrClass::LEFT_JUSTIFY, IsEnabled);
  125. // Draw the composition markers
  126. if (mIME && mInComposition) {
  127. const wchar_t* compString = mIME->GetCompositionString();
  128. Vector2 compSize = TextRenderer.Get_Text_Extents(compString);
  129. if (compSize.X > 0.0f) {
  130. // Underline the composition string
  131. Vector2 startPos;
  132. startPos.X = Pos_From_Character(CaretPos);
  133. startPos.Y = ClientRect.Bottom - 2;
  134. Vector2 stopPos(startPos);
  135. stopPos.X += compSize.X;
  136. unsigned long underlineColor = StyleMgrClass::Get_Disabled_Line_Color();
  137. ControlRenderer.Add_Line(startPos, stopPos, 1.0f, underlineColor);
  138. // Hilight the conversion target
  139. unsigned long start = 0;
  140. unsigned long end = 0;
  141. mIME->GetTargetClause(start, end);
  142. if (end > 0) {
  143. HilightStartPos = (CaretPos + start);
  144. HilightEndPos = (CaretPos + end);
  145. }
  146. }
  147. }
  148. //
  149. // Do the hilight
  150. //
  151. if (HilightStartPos >= 0 && HilightStartPos != HilightEndPos && IsEnabled) {
  152. RectClass hilight_rect;
  153. hilight_rect.Top = ClientRect.Top + 1;
  154. hilight_rect.Bottom = ClientRect.Bottom - 1;
  155. hilight_rect.Left = Pos_From_Character (HilightStartPos);
  156. hilight_rect.Right = Pos_From_Character (HilightEndPos);
  157. StyleMgrClass::Render_Hilight (&HilightRenderer, hilight_rect);
  158. }
  159. return ;
  160. }
  161. ////////////////////////////////////////////////////////////////
  162. //
  163. // Create_Control_Renderers
  164. //
  165. ////////////////////////////////////////////////////////////////
  166. void
  167. EditCtrlClass::Create_Control_Renderers (void)
  168. {
  169. //
  170. // Configure this renderer
  171. //
  172. ControlRenderer.Reset ();
  173. ControlRenderer.Enable_Texturing (false);
  174. //
  175. // Determine which color to draw the outline in
  176. //
  177. int color = StyleMgrClass::Get_Line_Color ();
  178. int bkcolor = StyleMgrClass::Get_Bk_Color ();
  179. if (IsEnabled == false) {
  180. color = StyleMgrClass::Get_Disabled_Line_Color ();
  181. bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
  182. }
  183. //
  184. // Draw the control outline
  185. //
  186. RectClass rect = Rect;
  187. ControlRenderer.Add_Outline (rect, 1.0F, color);
  188. //
  189. // Now draw the client area
  190. //
  191. rect.Right -= 1;
  192. rect.Bottom -= 1;
  193. ControlRenderer.Add_Quad (rect, bkcolor);
  194. return ;
  195. }
  196. ////////////////////////////////////////////////////////////////
  197. //
  198. // On_Set_Cursor
  199. //
  200. ////////////////////////////////////////////////////////////////
  201. void
  202. EditCtrlClass::On_Set_Cursor (const Vector2 &mouse_pos)
  203. {
  204. //
  205. // Change the mouse cursor if necessary
  206. //
  207. if (ClientRect.Contains (mouse_pos)) {
  208. MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_TEXT);
  209. }
  210. return ;
  211. }
  212. ////////////////////////////////////////////////////////////////
  213. //
  214. // Update_Client_Rect
  215. //
  216. ////////////////////////////////////////////////////////////////
  217. void
  218. EditCtrlClass::Update_Client_Rect (void)
  219. {
  220. //
  221. // Determine what one character spacing would be
  222. //
  223. float char_width = TextRenderer.Get_Text_Extents (L"I").X;
  224. //
  225. // Shrink the client area
  226. //
  227. ClientRect = Rect;
  228. ClientRect.Inflate (Vector2 (-char_width, -2));
  229. Set_Dirty ();
  230. return ;
  231. }
  232. ////////////////////////////////////////////////////////////////
  233. //
  234. // Render
  235. //
  236. ////////////////////////////////////////////////////////////////
  237. void
  238. EditCtrlClass::Render (void)
  239. {
  240. //
  241. // Recreate the renderers (if necessary)
  242. //
  243. if (IsDirty) {
  244. Create_Control_Renderers ();
  245. Create_Text_Renderers ();
  246. Create_Caret_Renderer ();
  247. }
  248. //
  249. // Render the background and text for the current state
  250. //
  251. ControlRenderer.Render ();
  252. TextRenderer.Render ();
  253. HilightRenderer.Render ();
  254. //
  255. // Render the caret (if necessary)
  256. //
  257. Update_Caret ();
  258. if (HasFocus && IsCaretDisplayed) {
  259. CaretRenderer.Render ();
  260. }
  261. #ifdef SHOW_IME_TYPING
  262. if (mShowIMETypingText) {
  263. mIMETypingTip.Render();
  264. }
  265. #endif
  266. DialogControlClass::Render ();
  267. return ;
  268. }
  269. ////////////////////////////////////////////////////////////////
  270. // Get_Display_Text
  271. ////////////////////////////////////////////////////////////////
  272. void EditCtrlClass::Get_Display_Text(WideStringClass &text)
  273. {
  274. // Resize to accomodate entire string
  275. int length = Title.Get_Length();
  276. if (mIME && mInComposition) {
  277. const wchar_t* compString = mIME->GetCompositionString();
  278. if (compString) {
  279. length += wcslen(compString);
  280. }
  281. }
  282. text.Get_Buffer(length);
  283. // If password then replace text with astrisks
  284. if ((Style & ES_PASSWORD) != 0) {
  285. int len = Title.Get_Length();
  286. wchar_t* buffer = text.Peek_Buffer();
  287. for (int index = 0; index < len; ++index) {
  288. buffer[index] = L'*';
  289. }
  290. buffer[index] = 0;
  291. } else {
  292. text = Title;
  293. }
  294. // Insert IME composition at cursor position
  295. if (mIME && mInComposition) {
  296. const wchar_t* compString = mIME->GetCompositionString();
  297. if (compString) {
  298. WideStringClass temp(length, true);
  299. temp = (text.Peek_Buffer() + CaretPos);
  300. text.Erase(CaretPos, (text.Get_Length() - CaretPos));
  301. text += compString;
  302. text += temp;
  303. }
  304. }
  305. }
  306. ////////////////////////////////////////////////////////////////
  307. //
  308. // On_LButton_Down
  309. //
  310. ////////////////////////////////////////////////////////////////
  311. void
  312. EditCtrlClass::On_LButton_Down (const Vector2 &mouse_pos)
  313. {
  314. if (mInComposition) {
  315. return;
  316. }
  317. Set_Capture ();
  318. if (HasFocus) {
  319. //
  320. // Update the caret position
  321. //
  322. Set_Caret_Pos (Character_From_Pos (mouse_pos));
  323. HilightAnchorPos = CaretPos;
  324. HilightStartPos = -1;
  325. HilightEndPos = -1;
  326. //
  327. // Force a repaint
  328. //
  329. Set_Dirty ();
  330. WasButtonPressedOnMe = true;
  331. }
  332. return ;
  333. }
  334. ////////////////////////////////////////////////////////////////
  335. //
  336. // On_LButton_Up
  337. //
  338. ////////////////////////////////////////////////////////////////
  339. void
  340. EditCtrlClass::On_LButton_Up (const Vector2 &mouse_pos)
  341. {
  342. if (mInComposition) {
  343. return;
  344. }
  345. Release_Capture ();
  346. //
  347. // Reset our flags
  348. //
  349. WasButtonPressedOnMe = false;
  350. return ;
  351. }
  352. ////////////////////////////////////////////////////////////////
  353. //
  354. // On_Mouse_Move
  355. //
  356. ////////////////////////////////////////////////////////////////
  357. void
  358. EditCtrlClass::On_Mouse_Move (const Vector2 &mouse_pos)
  359. {
  360. if (mInComposition) {
  361. return;
  362. }
  363. //
  364. // Is the user "dragging" inside the control
  365. //
  366. if (HasFocus && WasButtonPressedOnMe) {
  367. //
  368. // Has the caret position changed?
  369. //
  370. int new_caret_pos = Character_From_Pos (mouse_pos);
  371. if (new_caret_pos != CaretPos) {
  372. //
  373. // Calculate the new caret and hilight positions
  374. //
  375. Set_Caret_Pos (new_caret_pos);
  376. HilightStartPos = min (HilightAnchorPos, CaretPos);
  377. HilightEndPos = max (HilightAnchorPos, CaretPos);
  378. //
  379. // Force a repaint
  380. //
  381. Set_Dirty ();
  382. }
  383. }
  384. return ;
  385. }
  386. ////////////////////////////////////////////////////////////////
  387. //
  388. // Update_Caret
  389. //
  390. ////////////////////////////////////////////////////////////////
  391. void
  392. EditCtrlClass::Update_Caret (void)
  393. {
  394. //
  395. // Blink the caret if necessary
  396. //
  397. uint32 curr_time = DialogMgrClass::Get_Time ();
  398. if ((curr_time - LastCaretBlink) > CaretBlinkDelay) {
  399. IsCaretDisplayed = !IsCaretDisplayed;
  400. LastCaretBlink = curr_time;
  401. }
  402. return ;
  403. }
  404. ////////////////////////////////////////////////////////////////
  405. //
  406. // Create_Caret_Renderer
  407. //
  408. ////////////////////////////////////////////////////////////////
  409. void
  410. EditCtrlClass::Create_Caret_Renderer (void)
  411. {
  412. CaretRenderer.Reset ();
  413. //
  414. // Configure this renderer
  415. //
  416. CaretRenderer.Enable_Texturing (false);
  417. CaretRenderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
  418. //
  419. // Determine how many characters we should render the caret after
  420. //
  421. WideStringClass temp_copy(0, true);
  422. Get_Display_Text(temp_copy);
  423. WCHAR *text = temp_copy.Peek_Buffer();
  424. int caretPos = Get_Caret_Pos();
  425. text[caretPos] = 0;
  426. text = &text[ScrollPos];
  427. //
  428. // Calculate where in the client area we should build the caret
  429. //
  430. Vector2 text_extent = TextRenderer.Get_Text_Extents (text);
  431. float space_extent = 1;
  432. //
  433. // Create a bounding rectangle for the caret
  434. //
  435. RectClass rect;
  436. rect.Left = int(ClientRect.Left + text_extent.X);
  437. rect.Top = int(ClientRect.Top + 1);
  438. rect.Right = int(rect.Left + max (space_extent, 1.0F));
  439. rect.Bottom = int(ClientRect.Bottom - 1);
  440. //
  441. // Draw the caret
  442. //
  443. CaretRenderer.Add_Quad(rect, StyleMgrClass::Get_Text_Color());
  444. }
  445. ////////////////////////////////////////////////////////////////
  446. //
  447. // Character_From_Pos
  448. //
  449. ////////////////////////////////////////////////////////////////
  450. int
  451. EditCtrlClass::Character_From_Pos (const Vector2 &mouse_pos)
  452. {
  453. //
  454. // Get the text we will be displaying
  455. //
  456. WideStringClass display_text(0, true);
  457. Get_Display_Text(display_text);
  458. //
  459. // Index into the buffer
  460. //
  461. const WCHAR *text = display_text.Peek_Buffer () + ScrollPos;
  462. int char_index = display_text.Get_Length ();
  463. float x_pos = mouse_pos.X - ClientRect.Left;
  464. float curr_x_pos = 0;
  465. //
  466. // Loop over all the characters in the remainder of the string until
  467. // we've moved past the x-position we we're looking for.
  468. //
  469. int count = ::wcslen (text);
  470. for (int index = 0; index < count; index ++) {
  471. //
  472. // Get the width of the character
  473. //
  474. WCHAR char_string[2] = { text[index], 0 };
  475. float char_width = TextRenderer.Get_Text_Extents (char_string).X;
  476. //
  477. // Did we move past the position we were looking for?
  478. //
  479. if (curr_x_pos + (char_width / 2) >= x_pos) {
  480. char_index = index + ScrollPos;
  481. break;
  482. }
  483. //
  484. // Increment our x-position
  485. //
  486. curr_x_pos += char_width;
  487. }
  488. //
  489. // Return the character index
  490. //
  491. return char_index;
  492. }
  493. ////////////////////////////////////////////////////////////////
  494. //
  495. // Pos_From_Character
  496. //
  497. ////////////////////////////////////////////////////////////////
  498. float
  499. EditCtrlClass::Pos_From_Character (int char_index)
  500. {
  501. //
  502. // Ensure the character isn't scrolled off the screen
  503. //
  504. char_index = max (ScrollPos, char_index);
  505. //
  506. // Get the width of the string up to this character index
  507. //
  508. WideStringClass temp_copy(0, true);
  509. Get_Display_Text (temp_copy);
  510. WCHAR *text = temp_copy.Peek_Buffer ();
  511. text[char_index] = 0;
  512. text = &text[ScrollPos];
  513. float width = TextRenderer.Get_Text_Extents (text).X;
  514. //
  515. // Clip the position to the right side of the control
  516. //
  517. float position = min (ClientRect.Left + width, ClientRect.Right);
  518. return position;
  519. }
  520. ////////////////////////////////////////////////////////////////
  521. //
  522. // On_Set_Focus
  523. //
  524. ////////////////////////////////////////////////////////////////
  525. void
  526. EditCtrlClass::On_Set_Focus (void)
  527. {
  528. //
  529. // Hilight the contents of the window
  530. //
  531. Set_Caret_Pos (Title.Get_Length ());
  532. HilightAnchorPos = 0;
  533. HilightStartPos = 0;
  534. HilightEndPos = CaretPos;
  535. Set_Dirty ();
  536. if (mIME) {
  537. if (IsIMEAllowed()) {
  538. Observer<IME::CompositionEvent>::NotifyMe(*mIME);
  539. Observer<IME::CandidateEvent>::NotifyMe(*mIME);
  540. mIME->Activate();
  541. } else {
  542. mIME->Disable();
  543. }
  544. }
  545. DialogControlClass::On_Set_Focus ();
  546. return ;
  547. }
  548. ////////////////////////////////////////////////////////////////
  549. //
  550. // On_Kill_Focus
  551. //
  552. ////////////////////////////////////////////////////////////////
  553. void
  554. EditCtrlClass::On_Kill_Focus (DialogControlClass *focus)
  555. {
  556. if (mIME) {
  557. if (IsIMEAllowed()) {
  558. mIME->Deactivate();
  559. Observer<IME::CompositionEvent>::StopObserving();
  560. Observer<IME::CandidateEvent>::StopObserving();
  561. } else {
  562. mIME->Enable();
  563. }
  564. }
  565. WasButtonPressedOnMe = false;
  566. //
  567. // Remove any hilight
  568. //
  569. Set_Caret_Pos (0);
  570. HilightStartPos = -1;
  571. HilightEndPos = -1;
  572. Set_Dirty ();
  573. DialogControlClass::On_Kill_Focus (focus);
  574. return ;
  575. }
  576. ////////////////////////////////////////////////////////////////
  577. //
  578. // On_Key_Down
  579. //
  580. ////////////////////////////////////////////////////////////////
  581. bool
  582. EditCtrlClass::On_Key_Down (uint32 key_id, uint32 key_data)
  583. {
  584. bool handled = false;
  585. //
  586. // Check to see if the advise sinks are processing this input...
  587. //
  588. if (AdviseSink != NULL) {
  589. handled = AdviseSink->On_EditCtrl_Key_Down (this, key_id, key_data);
  590. }
  591. if (handled == false) {
  592. handled = Parent->On_EditCtrl_Key_Down (this, key_id, key_data);
  593. }
  594. if (handled) {
  595. return true;
  596. }
  597. handled = true;
  598. bool changed = false;
  599. bool is_dirty = true;
  600. int old_caret = CaretPos;
  601. bool update_hilight = ((DialogMgrClass::Get_VKey_State (VK_SHIFT) & VKEY_PRESSED) == VKEY_PRESSED);
  602. switch (key_id)
  603. {
  604. case VK_BACK:
  605. if (HilightStartPos >= 0) {
  606. changed = Delete_Selection ();
  607. } else if (CaretPos > 0) {
  608. //
  609. // Delete the previous character
  610. //
  611. Title.Erase (CaretPos - 1, 1);
  612. Set_Caret_Pos (CaretPos - 1);
  613. assert(NumChars > 0);
  614. NumChars--;
  615. changed = true;
  616. }
  617. update_hilight = false;
  618. break;
  619. case VK_DELETE:
  620. if (HilightStartPos >= 0) {
  621. changed = Delete_Selection ();
  622. } else if (CaretPos < Title.Get_Length ()) {
  623. //
  624. // Delete the next character
  625. //
  626. Title.Erase (CaretPos, 1);
  627. assert(NumChars > 0);
  628. NumChars--;
  629. changed = true;
  630. }
  631. update_hilight = false;
  632. break;
  633. case VK_HOME:
  634. Set_Caret_Pos (0);
  635. break;
  636. case VK_END:
  637. Set_Caret_Pos (Title.Get_Length ());
  638. break;
  639. case VK_LEFT:
  640. //
  641. // Should snap the caret to words, or just increment it?
  642. //
  643. if (DialogMgrClass::Get_VKey_State (VK_CONTROL) & VKEY_PRESSED) {
  644. Set_Caret_Pos (Find_Word_Start (CaretPos, -1));
  645. } else {
  646. Set_Caret_Pos (max (0, CaretPos - 1));
  647. }
  648. break;
  649. case VK_RIGHT:
  650. //
  651. // Should snap the caret to words, or just increment it?
  652. //
  653. if (DialogMgrClass::Get_VKey_State (VK_CONTROL) & VKEY_PRESSED) {
  654. Set_Caret_Pos (Find_Word_Start (CaretPos, 1));
  655. } else {
  656. Set_Caret_Pos (min (Title.Get_Length (), CaretPos + 1));
  657. }
  658. break;
  659. case VK_RETURN:
  660. //
  661. // Notify the parent that the user pressed enter
  662. //
  663. ADVISE_NOTIFY (On_EditCtrl_Enter_Pressed (this, Get_ID ()));
  664. is_dirty = false;
  665. break;
  666. case VK_UP:
  667. case VK_DOWN:
  668. //
  669. // Don't do anything
  670. //
  671. is_dirty = false;
  672. handled = false;
  673. break;
  674. default:
  675. is_dirty = false;
  676. handled = false;
  677. break;
  678. }
  679. if (update_hilight) {
  680. //
  681. // Update the hilight position
  682. //
  683. Update_Hilight (CaretPos, old_caret);
  684. } else if (handled) {
  685. //
  686. // Reset the hilight position
  687. //
  688. HilightStartPos = -1;
  689. HilightAnchorPos = -1;
  690. }
  691. if (changed) {
  692. ADVISE_NOTIFY(On_EditCtrl_Change(this, Get_ID()));
  693. }
  694. //
  695. // Repaint the control if necessary
  696. //
  697. if (is_dirty) {
  698. Set_Dirty ();
  699. }
  700. return handled;
  701. }
  702. void EditCtrlClass::On_Unicode_Char(WCHAR unicode)
  703. {
  704. if (unicode >= 32) {
  705. // Delete the old selection
  706. Delete_Selection ();
  707. if (NumChars < TextLimit) {
  708. NumChars++;
  709. // Build the new string
  710. WideStringClass new_title = Title;
  711. new_title.Erase (CaretPos, new_title.Get_Length () - CaretPos);
  712. new_title += unicode;
  713. new_title += Title.Peek_Buffer () + CaretPos;
  714. Title = new_title;
  715. // Move the caret to the end of the text the user just typed
  716. Set_Caret_Pos (CaretPos + 1);
  717. //
  718. // Reset the hilight position
  719. //
  720. HilightStartPos = -1;
  721. HilightAnchorPos = -1;
  722. ADVISE_NOTIFY(On_EditCtrl_Change(this, Get_ID()));
  723. // Force a repaint
  724. Set_Dirty ();
  725. }
  726. }
  727. }
  728. void EditCtrlClass::Insert_String(const WCHAR* string)
  729. {
  730. int count = wcslen(string);
  731. if (count > 0) {
  732. // Delete the old selection
  733. Delete_Selection();
  734. WideStringClass new_title = Title;
  735. new_title.Erase(CaretPos, new_title.Get_Length () - CaretPos);
  736. // If the string exceed the text limit then only insert characters upto
  737. // the text limit.
  738. if ((NumChars + count) > TextLimit) {
  739. // Truncate the insert string so that it will fit.
  740. WideStringClass insert(count, true);
  741. insert = string;
  742. count = (TextLimit - NumChars);
  743. insert.Erase(count, insert.Get_Length() - count);
  744. new_title += insert;
  745. } else {
  746. new_title += string;
  747. }
  748. new_title += Title.Peek_Buffer() + CaretPos;
  749. Title = new_title;
  750. NumChars += count;
  751. // Move the caret to the end of the text the user just typed
  752. Set_Caret_Pos(CaretPos + count);
  753. ADVISE_NOTIFY(On_EditCtrl_Change(this, Get_ID()));
  754. // Force a repaint
  755. Set_Dirty();
  756. }
  757. }
  758. ////////////////////////////////////////////////////////////////
  759. //
  760. // Delete_Selection
  761. //
  762. ////////////////////////////////////////////////////////////////
  763. bool
  764. EditCtrlClass::Delete_Selection (void)
  765. {
  766. if (HilightStartPos >= 0) {
  767. //
  768. // Delete the characters and update the caret
  769. //
  770. int count = (HilightEndPos - HilightStartPos);
  771. Title.Erase (HilightStartPos, count);
  772. Set_Caret_Pos (HilightStartPos);
  773. assert(NumChars >= count);
  774. NumChars -= count;
  775. //
  776. // Remove any hilighting
  777. //
  778. HilightStartPos = -1;
  779. HilightEndPos = -1;
  780. //
  781. // Force a repaint
  782. //
  783. Set_Dirty ();
  784. return true;
  785. }
  786. return false;
  787. }
  788. ////////////////////////////////////////////////////////////////
  789. //
  790. // On_Create
  791. //
  792. ////////////////////////////////////////////////////////////////
  793. void
  794. EditCtrlClass::On_Create (void)
  795. {
  796. //TextColor.Set (0.35F, 1.0F, 0.35F);
  797. //Set_Text (L"This is a test...");
  798. return ;
  799. }
  800. ////////////////////////////////////////////////////////////////
  801. //
  802. // Set_Caret_Pos
  803. //
  804. ////////////////////////////////////////////////////////////////
  805. void
  806. EditCtrlClass::Set_Caret_Pos (int new_pos)
  807. {
  808. //
  809. // Remove the hilight
  810. //
  811. HilightStartPos = -1;
  812. //
  813. // Did the caret position change?
  814. //
  815. if (CaretPos != new_pos) {
  816. CaretPos = new_pos;
  817. //
  818. // Force the caret to be displayed (i.e. no blink)
  819. //
  820. IsCaretDisplayed = true;
  821. LastCaretBlink = DialogMgrClass::Get_Time ();
  822. //
  823. // Handle scrolling
  824. //
  825. Update_Scroll_Pos ();
  826. //
  827. // Force a repaint
  828. //
  829. Set_Dirty ();
  830. }
  831. return ;
  832. }
  833. ////////////////////////////////////////////////////////////////
  834. //
  835. // Update_Hilight
  836. //
  837. ////////////////////////////////////////////////////////////////
  838. void
  839. EditCtrlClass::Update_Hilight (int new_pos, int anchor_pos)
  840. {
  841. HilightAnchorPos = (HilightAnchorPos >= 0) ? HilightAnchorPos : anchor_pos;
  842. HilightStartPos = min (new_pos, HilightAnchorPos);
  843. HilightEndPos = max (new_pos, HilightAnchorPos);
  844. return ;
  845. }
  846. ////////////////////////////////////////////////////////////////
  847. //
  848. // Find_Word_Start
  849. //
  850. ////////////////////////////////////////////////////////////////
  851. int
  852. EditCtrlClass::Find_Word_Start (int pos, int increment)
  853. {
  854. int count = Title.Get_Length ();
  855. //
  856. // Determine what the extreme end posiiton should be
  857. //
  858. int extreme = pos + increment * 1000;
  859. int retval = 0;
  860. if (extreme > count) {
  861. retval = count;
  862. }
  863. //
  864. // Loop over all the characters until we've found the word break
  865. //
  866. for (int index = pos + increment; index < count && index >= 0; index += increment) {
  867. //
  868. // Is this a space character?
  869. //
  870. bool is_space = (Title[index] == L' ');
  871. //
  872. // If we've already found the word break and this is the
  873. // start of a new word, then return its index to the caller
  874. //
  875. if (!is_space && ((index == 0) || (Title[index - 1] == L' '))) {
  876. retval = index;
  877. break;
  878. }
  879. }
  880. return retval;
  881. }
  882. ////////////////////////////////////////////////////////////////
  883. //
  884. // Update_Scroll_Pos
  885. //
  886. ////////////////////////////////////////////////////////////////
  887. void
  888. EditCtrlClass::Update_Scroll_Pos (void)
  889. {
  890. int old_scroll = ScrollPos;
  891. int caretPos = Get_Caret_Pos();
  892. //
  893. // Find the current x-position of the caret
  894. //
  895. float x_pos = Pos_From_Character(caretPos);
  896. if (x_pos <= ClientRect.Left) {
  897. //
  898. // Simply ensure the caret is scrolled into view
  899. //
  900. ScrollPos = caretPos - 2;
  901. } else if (x_pos >= ClientRect.Right) {
  902. //
  903. // Make a temporary copy of the text so we can
  904. // modify it
  905. //
  906. WideStringClass temp_string(0, true);
  907. Get_Display_Text(temp_string);
  908. WCHAR *text = temp_string.Peek_Buffer();
  909. text[caretPos] = 0;
  910. //
  911. // Find how far we'd have to scroll in order to bring the
  912. // caret back into view.
  913. //
  914. for (int index = 0; index < caretPos; ++index) {
  915. float width = TextRenderer.Get_Text_Extents (text + index).X;
  916. if (width < ClientRect.Width ()) {
  917. ScrollPos = index + 2;
  918. break;
  919. }
  920. }
  921. }
  922. //
  923. // Adjust the scroll range to stay within our boundaries
  924. //
  925. ScrollPos = min (ScrollPos, Title.Get_Length () - 1);
  926. ScrollPos = max (ScrollPos, 0);
  927. //
  928. // Repaint if necessary
  929. //
  930. if (old_scroll != ScrollPos) {
  931. Set_Dirty ();
  932. }
  933. return ;
  934. }
  935. ////////////////////////////////////////////////////////////////
  936. //
  937. // Get_Int
  938. //
  939. ////////////////////////////////////////////////////////////////
  940. int
  941. EditCtrlClass::Get_Int (void)
  942. {
  943. return _wtoi (Get_Text ());
  944. }
  945. ////////////////////////////////////////////////////////////////
  946. //
  947. // Set_Int
  948. //
  949. ////////////////////////////////////////////////////////////////
  950. void
  951. EditCtrlClass::Set_Int (int value)
  952. {
  953. WideStringClass text(64, true);
  954. text.Format (L"%d", value);
  955. Set_Text (text);
  956. return ;
  957. }
  958. ////////////////////////////////////////////////////////////////
  959. //
  960. // Set_Text
  961. //
  962. ////////////////////////////////////////////////////////////////
  963. void
  964. EditCtrlClass::Set_Text (const WCHAR *title)
  965. {
  966. int count = wcslen(title);
  967. // If the string is too long then truncate it so that it will fit.
  968. if (count > TextLimit) {
  969. WideStringClass text(count, true);
  970. text = title;
  971. text.Erase(TextLimit, text.Get_Length() - TextLimit);
  972. DialogControlClass::Set_Text (text);
  973. count = TextLimit;
  974. } else {
  975. DialogControlClass::Set_Text (title);
  976. }
  977. NumChars = count;
  978. Set_Caret_Pos (Title.Get_Length ());
  979. HilightAnchorPos = CaretPos;
  980. HilightStartPos = -1;
  981. HilightEndPos = -1;
  982. return ;
  983. }
  984. ////////////////////////////////////////////////////////////////
  985. //
  986. // Get_Text_Length
  987. //
  988. ////////////////////////////////////////////////////////////////
  989. int EditCtrlClass::Get_Text_Length(void) const
  990. {
  991. return NumChars;
  992. }
  993. ////////////////////////////////////////////////////////////////
  994. //
  995. // Set_Text_Limit
  996. //
  997. ////////////////////////////////////////////////////////////////
  998. void
  999. EditCtrlClass::Set_Text_Limit (int numChars)
  1000. {
  1001. if (numChars > 0) {
  1002. TextLimit = numChars;
  1003. } else {
  1004. TextLimit = 0xFFFF;
  1005. }
  1006. return ;
  1007. }
  1008. ////////////////////////////////////////////////////////////////
  1009. //
  1010. // Get_Text_Limit
  1011. //
  1012. ////////////////////////////////////////////////////////////////
  1013. int
  1014. EditCtrlClass::Get_Text_Limit (void) const
  1015. {
  1016. return TextLimit;
  1017. }
  1018. ////////////////////////////////////////////////////////////////
  1019. //
  1020. // Get_Caret_Pos
  1021. //
  1022. ////////////////////////////////////////////////////////////////
  1023. int
  1024. EditCtrlClass::Get_Caret_Pos (void) const
  1025. {
  1026. if (mIME && mInComposition) {
  1027. return (CaretPos + mIME->GetCompositionCursorPos());
  1028. }
  1029. return CaretPos;
  1030. }
  1031. ////////////////////////////////////////////////////////////////
  1032. //
  1033. // Set_Sel
  1034. //
  1035. ////////////////////////////////////////////////////////////////
  1036. void
  1037. EditCtrlClass::Set_Sel (int start_index, int end_index)
  1038. {
  1039. HilightAnchorPos = start_index;
  1040. HilightStartPos = start_index;
  1041. HilightEndPos = end_index;
  1042. Set_Dirty ();
  1043. return ;
  1044. }
  1045. ////////////////////////////////////////////////////////////////
  1046. //
  1047. // Get_Sel
  1048. //
  1049. ////////////////////////////////////////////////////////////////
  1050. void
  1051. EditCtrlClass::Get_Sel (int &start_index, int &end_index) const
  1052. {
  1053. start_index = HilightStartPos;
  1054. end_index = HilightEndPos;
  1055. return ;
  1056. }
  1057. /******************************************************************************
  1058. *
  1059. * NAME
  1060. *
  1061. * DESCRIPTION
  1062. *
  1063. * INPUTS
  1064. *
  1065. * RESULT
  1066. *
  1067. ******************************************************************************/
  1068. bool EditCtrlClass::IsIMEAllowed(void) const
  1069. {
  1070. // We use the ES_OEMCONVERT style to indicate that IME conversion
  1071. // is not allowed for this control. This style bit is set in the
  1072. // resource editor.
  1073. return ((Style & (ES_NUMBER|ES_OEMCONVERT)) == 0);
  1074. }
  1075. #ifdef SHOW_IME_TYPING
  1076. /******************************************************************************
  1077. *
  1078. * NAME
  1079. *
  1080. * DESCRIPTION
  1081. *
  1082. * INPUTS
  1083. *
  1084. * RESULT
  1085. *
  1086. ******************************************************************************/
  1087. void EditCtrlClass::Set_IME_Typing_Text_Pos(void)
  1088. {
  1089. Vector2 charExtent = TextRenderer.Get_Text_Extents(L"W");
  1090. int caretPos = Get_Caret_Pos();
  1091. Vector2 pos;
  1092. pos.X = (Pos_From_Character(caretPos) + (charExtent.X * 0.25f));
  1093. pos.Y = (ClientRect.Top + ((ClientRect.Bottom - ClientRect.Top) * 0.25f));
  1094. mIMETypingTip.Set_Position(pos);
  1095. }
  1096. /******************************************************************************
  1097. *
  1098. * NAME
  1099. *
  1100. * DESCRIPTION
  1101. *
  1102. * INPUTS
  1103. *
  1104. * RESULT
  1105. *
  1106. ******************************************************************************/
  1107. void EditCtrlClass::Show_IME_Typing_Text(const wchar_t* text)
  1108. {
  1109. mShowIMETypingText = (text && wcslen(text) > 0);
  1110. if (mShowIMETypingText) {
  1111. Set_IME_Typing_Text_Pos();
  1112. mIMETypingTip.Set_Text(text);
  1113. } else {
  1114. mIMETypingTip.Set_Text(L"");
  1115. }
  1116. }
  1117. /******************************************************************************
  1118. *
  1119. * NAME
  1120. *
  1121. * DESCRIPTION
  1122. *
  1123. * INPUTS
  1124. *
  1125. * RESULT
  1126. *
  1127. ******************************************************************************/
  1128. void EditCtrlClass::Hide_IME_Typing_Text(void)
  1129. {
  1130. mShowIMETypingText = false;
  1131. mIMETypingTip.Set_Text(L"");
  1132. }
  1133. #endif
  1134. /******************************************************************************
  1135. *
  1136. * NAME
  1137. *
  1138. * DESCRIPTION
  1139. *
  1140. * INPUTS
  1141. *
  1142. * RESULT
  1143. *
  1144. ******************************************************************************/
  1145. void EditCtrlClass::PositionCandidateList(void)
  1146. {
  1147. if (mIME) {
  1148. //-------------------------------------------------------------------------
  1149. // Position the candidate window under the edit control
  1150. //-------------------------------------------------------------------------
  1151. unsigned long start = 0;
  1152. unsigned long end = 0;
  1153. mIME->GetTargetClause(start, end);
  1154. int caretPos = CaretPos + start;
  1155. Vector2 pos;
  1156. pos.X = Pos_From_Character(caretPos);
  1157. pos.Y = (Rect.Bottom + 2.0f);
  1158. mCandidateList.Set_Window_Pos(pos);
  1159. //-------------------------------------------------------------------------
  1160. // Reposition the candidate list if it will go off the screen.
  1161. //-------------------------------------------------------------------------
  1162. bool reposition = false;
  1163. const RectClass& screen = Render2DClass::Get_Screen_Resolution();
  1164. const RectClass& ctrlRect = mCandidateList.Get_Window_Rect();
  1165. // If the list will go off the bottom of the screen move it above
  1166. // the edit control.
  1167. if (ctrlRect.Bottom > screen.Bottom) {
  1168. pos.Y = ((Rect.Top - 2.0f) - ctrlRect.Height());
  1169. WWASSERT((pos.Y >= 0.0f) && "CandidateCtrl off the top of the screen");
  1170. reposition = true;
  1171. }
  1172. // Do not allow the control to go off the right of the screen.
  1173. if (ctrlRect.Right > screen.Right) {
  1174. pos.X -= ((ctrlRect.Right - screen.Right) - 1);
  1175. WWASSERT((pos.X >= 0.0f) && "CandidateCtrl of the left of the screen");
  1176. reposition = true;
  1177. }
  1178. if (reposition) {
  1179. mCandidateList.Set_Window_Pos(pos);
  1180. }
  1181. }
  1182. }
  1183. /******************************************************************************
  1184. *
  1185. * NAME
  1186. *
  1187. * DESCRIPTION
  1188. *
  1189. * INPUTS
  1190. *
  1191. * RESULT
  1192. *
  1193. ******************************************************************************/
  1194. void EditCtrlClass::HandleNotification(IME::CompositionEvent& imeEvent)
  1195. {
  1196. switch (imeEvent.GetAction()) {
  1197. #ifdef SHOW_IME_TYPING
  1198. case IME::COMPOSITION_TYPING:
  1199. Show_IME_Typing_Text(imeEvent.Subject()->GetTypingString());
  1200. break;
  1201. #endif
  1202. case IME::COMPOSITION_START:
  1203. mInComposition = true;
  1204. Delete_Selection();
  1205. #ifdef SHOW_IME_TYPING
  1206. Show_IME_Typing_Text(imeEvent.Subject()->GetTypingString());
  1207. #endif
  1208. Set_Dirty(true);
  1209. break;
  1210. case IME::COMPOSITION_CHANGE:
  1211. Update_Scroll_Pos();
  1212. Set_Dirty(true);
  1213. break;
  1214. case IME::COMPOSITION_CANCEL:
  1215. case IME::COMPOSITION_END:
  1216. mInComposition = false;
  1217. #ifdef SHOW_IME_TYPING
  1218. Hide_IME_Typing_Text();
  1219. #endif
  1220. // Remove hilight
  1221. HilightStartPos = -1;
  1222. HilightEndPos = -1;
  1223. Set_Dirty(true);
  1224. break;
  1225. case IME::COMPOSITION_RESULT:
  1226. HilightStartPos = -1;
  1227. HilightEndPos = -1;
  1228. Insert_String(imeEvent.Subject()->GetResultString());
  1229. Update_Scroll_Pos();
  1230. break;
  1231. default:
  1232. break;
  1233. }
  1234. }
  1235. /******************************************************************************
  1236. *
  1237. * NAME
  1238. *
  1239. * DESCRIPTION
  1240. *
  1241. * INPUTS
  1242. *
  1243. * RESULT
  1244. *
  1245. ******************************************************************************/
  1246. void EditCtrlClass::HandleNotification(IME::CandidateEvent& imeEvent)
  1247. {
  1248. switch (imeEvent.GetAction()) {
  1249. case IME::CANDIDATE_OPEN: {
  1250. #ifdef SHOW_IME_TYPING
  1251. Hide_IME_Typing_Text();
  1252. #endif
  1253. mCandidateList.Init(imeEvent.Subject());
  1254. PositionCandidateList();
  1255. Parent->Add_Control(&mCandidateList);
  1256. }
  1257. break;
  1258. case IME::CANDIDATE_CHANGE:
  1259. mCandidateList.Changed(imeEvent.Subject());
  1260. break;
  1261. case IME::CANDIDATE_CLOSE:
  1262. mCandidateList.Reset();
  1263. Parent->Remove_Control(&mCandidateList);
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. }