| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532 |
- /*
- ** Command & Conquer Renegade(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** 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 ***
- ***********************************************************************************************
- * *
- * Project Name : Combat *
- * *
- * $Archive:: /Commando/Code/wwui/editctrl.cpp $*
- * *
- * Author:: Patrick Smith *
- * *
- * $Modtime:: 1/17/02 1:26p $*
- * *
- * $Revision:: 25 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "editctrl.h"
- #include "assetmgr.h"
- #include "refcount.h"
- #include "font3d.h"
- #include "mousemgr.h"
- #include "ww3d.h"
- #include "dialogmgr.h"
- #include "stylemgr.h"
- #include "dialogbase.h"
- ////////////////////////////////////////////////////////////////
- //
- // EditCtrlClass
- //
- ////////////////////////////////////////////////////////////////
- EditCtrlClass::EditCtrlClass (void) :
- CaretPos (0),
- ScrollPos (0),
- NumChars(0),
- TextLimit(0xFFFF),
- CaretBlinkDelay (500),
- LastCaretBlink (0),
- HilightStartPos (-1),
- HilightEndPos (-1),
- HilightAnchorPos (-1),
- WasButtonPressedOnMe (false),
- IsCaretDisplayed (false),
- mInComposition(false)
- #ifdef SHOW_IME_TYPING
- , mShowIMETypingText(false)
- #endif
- {
- //
- // Set the font for the text renderers
- //
- StyleMgrClass::Assign_Font (&TextRenderer, StyleMgrClass::FONT_CONTROLS);
- StyleMgrClass::Configure_Renderer (&ControlRenderer);
-
- StyleMgrClass::Configure_Renderer (&CaretRenderer);
- StyleMgrClass::Configure_Renderer (&HilightRenderer);
- mIME = DialogMgrClass::Get_IME();
- }
- ////////////////////////////////////////////////////////////////
- //
- // ~EditCtrlClass
- //
- ////////////////////////////////////////////////////////////////
- EditCtrlClass::~EditCtrlClass (void)
- {
- if (mIME) {
- mIME->Release_Ref();
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Create_Text_Renderers
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Create_Text_Renderers (void)
- {
- HilightRenderer.Reset ();
- HilightRenderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
- StyleMgrClass::Configure_Hilighter (&HilightRenderer);
- //
- // Get the string we'll be displaying
- //
- WideStringClass display_string(0, true);
- Get_Display_Text(display_string);
- //
- // Index into the text buffer
- //
- const WCHAR* text = display_string.Peek_Buffer();
- if (ScrollPos >= 0 && ScrollPos < display_string.Get_Length()) {
- text += ScrollPos;
- }
- //
- // Start fresh
- //
- TextRenderer.Reset();
- //
- // Draw the text
- //
- uint32 color = StyleMgrClass::Get_Text_Color();
- uint32 shadowColor = StyleMgrClass::Get_Text_Shadow_Color();
- if (IsEnabled == false) {
- color = StyleMgrClass::Get_Disabled_Text_Color();
- shadowColor = StyleMgrClass::Get_Disabled_Text_Shadow_Color();
- }
- StyleMgrClass::Render_Text(text, &TextRenderer, color, shadowColor,
- ClientRect, true, true, StyleMgrClass::LEFT_JUSTIFY, IsEnabled);
- // Draw the composition markers
- if (mIME && mInComposition) {
- const wchar_t* compString = mIME->GetCompositionString();
- Vector2 compSize = TextRenderer.Get_Text_Extents(compString);
- if (compSize.X > 0.0f) {
- // Underline the composition string
- Vector2 startPos;
- startPos.X = Pos_From_Character(CaretPos);
- startPos.Y = ClientRect.Bottom - 2;
- Vector2 stopPos(startPos);
- stopPos.X += compSize.X;
- unsigned long underlineColor = StyleMgrClass::Get_Disabled_Line_Color();
- ControlRenderer.Add_Line(startPos, stopPos, 1.0f, underlineColor);
- // Hilight the conversion target
- unsigned long start = 0;
- unsigned long end = 0;
- mIME->GetTargetClause(start, end);
- if (end > 0) {
- HilightStartPos = (CaretPos + start);
- HilightEndPos = (CaretPos + end);
- }
- }
- }
- //
- // Do the hilight
- //
- if (HilightStartPos >= 0 && HilightStartPos != HilightEndPos && IsEnabled) {
- RectClass hilight_rect;
- hilight_rect.Top = ClientRect.Top + 1;
- hilight_rect.Bottom = ClientRect.Bottom - 1;
- hilight_rect.Left = Pos_From_Character (HilightStartPos);
- hilight_rect.Right = Pos_From_Character (HilightEndPos);
- StyleMgrClass::Render_Hilight (&HilightRenderer, hilight_rect);
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Create_Control_Renderers
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Create_Control_Renderers (void)
- {
- //
- // Configure this renderer
- //
- ControlRenderer.Reset ();
- ControlRenderer.Enable_Texturing (false);
- //
- // Determine which color to draw the outline in
- //
- int color = StyleMgrClass::Get_Line_Color ();
- int bkcolor = StyleMgrClass::Get_Bk_Color ();
- if (IsEnabled == false) {
- color = StyleMgrClass::Get_Disabled_Line_Color ();
- bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
- }
- //
- // Draw the control outline
- //
- RectClass rect = Rect;
- ControlRenderer.Add_Outline (rect, 1.0F, color);
- //
- // Now draw the client area
- //
- rect.Right -= 1;
- rect.Bottom -= 1;
- ControlRenderer.Add_Quad (rect, bkcolor);
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_Set_Cursor
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_Set_Cursor (const Vector2 &mouse_pos)
- {
- //
- // Change the mouse cursor if necessary
- //
- if (ClientRect.Contains (mouse_pos)) {
- MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_TEXT);
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Update_Client_Rect
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Update_Client_Rect (void)
- {
- //
- // Determine what one character spacing would be
- //
- float char_width = TextRenderer.Get_Text_Extents (L"I").X;
- //
- // Shrink the client area
- //
- ClientRect = Rect;
- ClientRect.Inflate (Vector2 (-char_width, -2));
- Set_Dirty ();
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Render
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Render (void)
- {
- //
- // Recreate the renderers (if necessary)
- //
- if (IsDirty) {
- Create_Control_Renderers ();
- Create_Text_Renderers ();
- Create_Caret_Renderer ();
- }
- //
- // Render the background and text for the current state
- //
- ControlRenderer.Render ();
- TextRenderer.Render ();
- HilightRenderer.Render ();
- //
- // Render the caret (if necessary)
- //
- Update_Caret ();
- if (HasFocus && IsCaretDisplayed) {
- CaretRenderer.Render ();
- }
- #ifdef SHOW_IME_TYPING
- if (mShowIMETypingText) {
- mIMETypingTip.Render();
- }
- #endif
- DialogControlClass::Render ();
- return ;
- }
- ////////////////////////////////////////////////////////////////
- // Get_Display_Text
- ////////////////////////////////////////////////////////////////
- void EditCtrlClass::Get_Display_Text(WideStringClass &text)
- {
- // Resize to accomodate entire string
- int length = Title.Get_Length();
- if (mIME && mInComposition) {
- const wchar_t* compString = mIME->GetCompositionString();
- if (compString) {
- length += wcslen(compString);
- }
- }
- text.Get_Buffer(length);
- // If password then replace text with astrisks
- if ((Style & ES_PASSWORD) != 0) {
- int len = Title.Get_Length();
- wchar_t* buffer = text.Peek_Buffer();
- for (int index = 0; index < len; ++index) {
- buffer[index] = L'*';
- }
- buffer[index] = 0;
- } else {
- text = Title;
- }
- // Insert IME composition at cursor position
- if (mIME && mInComposition) {
- const wchar_t* compString = mIME->GetCompositionString();
- if (compString) {
- WideStringClass temp(length, true);
- temp = (text.Peek_Buffer() + CaretPos);
- text.Erase(CaretPos, (text.Get_Length() - CaretPos));
- text += compString;
- text += temp;
- }
- }
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_LButton_Down
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_LButton_Down (const Vector2 &mouse_pos)
- {
- if (mInComposition) {
- return;
- }
- Set_Capture ();
- if (HasFocus) {
-
- //
- // Update the caret position
- //
- Set_Caret_Pos (Character_From_Pos (mouse_pos));
- HilightAnchorPos = CaretPos;
- HilightStartPos = -1;
- HilightEndPos = -1;
- //
- // Force a repaint
- //
- Set_Dirty ();
- WasButtonPressedOnMe = true;
- }
-
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_LButton_Up
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_LButton_Up (const Vector2 &mouse_pos)
- {
- if (mInComposition) {
- return;
- }
- Release_Capture ();
- //
- // Reset our flags
- //
- WasButtonPressedOnMe = false;
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_Mouse_Move
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_Mouse_Move (const Vector2 &mouse_pos)
- {
- if (mInComposition) {
- return;
- }
- //
- // Is the user "dragging" inside the control
- //
- if (HasFocus && WasButtonPressedOnMe) {
- //
- // Has the caret position changed?
- //
- int new_caret_pos = Character_From_Pos (mouse_pos);
- if (new_caret_pos != CaretPos) {
- //
- // Calculate the new caret and hilight positions
- //
- Set_Caret_Pos (new_caret_pos);
- HilightStartPos = min (HilightAnchorPos, CaretPos);
- HilightEndPos = max (HilightAnchorPos, CaretPos);
- //
- // Force a repaint
- //
- Set_Dirty ();
- }
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Update_Caret
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Update_Caret (void)
- {
- //
- // Blink the caret if necessary
- //
- uint32 curr_time = DialogMgrClass::Get_Time ();
- if ((curr_time - LastCaretBlink) > CaretBlinkDelay) {
- IsCaretDisplayed = !IsCaretDisplayed;
- LastCaretBlink = curr_time;
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Create_Caret_Renderer
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Create_Caret_Renderer (void)
- {
- CaretRenderer.Reset ();
- //
- // Configure this renderer
- //
- CaretRenderer.Enable_Texturing (false);
- CaretRenderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
- //
- // Determine how many characters we should render the caret after
- //
- WideStringClass temp_copy(0, true);
- Get_Display_Text(temp_copy);
- WCHAR *text = temp_copy.Peek_Buffer();
- int caretPos = Get_Caret_Pos();
- text[caretPos] = 0;
- text = &text[ScrollPos];
- //
- // Calculate where in the client area we should build the caret
- //
- Vector2 text_extent = TextRenderer.Get_Text_Extents (text);
- float space_extent = 1;
- //
- // Create a bounding rectangle for the caret
- //
- RectClass rect;
- rect.Left = int(ClientRect.Left + text_extent.X);
- rect.Top = int(ClientRect.Top + 1);
- rect.Right = int(rect.Left + max (space_extent, 1.0F));
- rect.Bottom = int(ClientRect.Bottom - 1);
- //
- // Draw the caret
- //
- CaretRenderer.Add_Quad(rect, StyleMgrClass::Get_Text_Color());
- }
- ////////////////////////////////////////////////////////////////
- //
- // Character_From_Pos
- //
- ////////////////////////////////////////////////////////////////
- int
- EditCtrlClass::Character_From_Pos (const Vector2 &mouse_pos)
- {
- //
- // Get the text we will be displaying
- //
- WideStringClass display_text(0, true);
- Get_Display_Text(display_text);
- //
- // Index into the buffer
- //
- const WCHAR *text = display_text.Peek_Buffer () + ScrollPos;
- int char_index = display_text.Get_Length ();
- float x_pos = mouse_pos.X - ClientRect.Left;
- float curr_x_pos = 0;
- //
- // Loop over all the characters in the remainder of the string until
- // we've moved past the x-position we we're looking for.
- //
- int count = ::wcslen (text);
- for (int index = 0; index < count; index ++) {
-
- //
- // Get the width of the character
- //
- WCHAR char_string[2] = { text[index], 0 };
- float char_width = TextRenderer.Get_Text_Extents (char_string).X;
-
- //
- // Did we move past the position we were looking for?
- //
- if (curr_x_pos + (char_width / 2) >= x_pos) {
- char_index = index + ScrollPos;
- break;
- }
- //
- // Increment our x-position
- //
- curr_x_pos += char_width;
- }
- //
- // Return the character index
- //
- return char_index;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Pos_From_Character
- //
- ////////////////////////////////////////////////////////////////
- float
- EditCtrlClass::Pos_From_Character (int char_index)
- {
- //
- // Ensure the character isn't scrolled off the screen
- //
- char_index = max (ScrollPos, char_index);
- //
- // Get the width of the string up to this character index
- //
- WideStringClass temp_copy(0, true);
- Get_Display_Text (temp_copy);
- WCHAR *text = temp_copy.Peek_Buffer ();
- text[char_index] = 0;
- text = &text[ScrollPos];
- float width = TextRenderer.Get_Text_Extents (text).X;
- //
- // Clip the position to the right side of the control
- //
- float position = min (ClientRect.Left + width, ClientRect.Right);
- return position;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_Set_Focus
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_Set_Focus (void)
- {
- //
- // Hilight the contents of the window
- //
- Set_Caret_Pos (Title.Get_Length ());
- HilightAnchorPos = 0;
- HilightStartPos = 0;
- HilightEndPos = CaretPos;
- Set_Dirty ();
- if (mIME) {
- if (IsIMEAllowed()) {
- Observer<IME::CompositionEvent>::NotifyMe(*mIME);
- Observer<IME::CandidateEvent>::NotifyMe(*mIME);
- mIME->Activate();
- } else {
- mIME->Disable();
- }
- }
- DialogControlClass::On_Set_Focus ();
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_Kill_Focus
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_Kill_Focus (DialogControlClass *focus)
- {
- if (mIME) {
- if (IsIMEAllowed()) {
- mIME->Deactivate();
- Observer<IME::CompositionEvent>::StopObserving();
- Observer<IME::CandidateEvent>::StopObserving();
- } else {
- mIME->Enable();
- }
- }
- WasButtonPressedOnMe = false;
- //
- // Remove any hilight
- //
- Set_Caret_Pos (0);
- HilightStartPos = -1;
- HilightEndPos = -1;
- Set_Dirty ();
- DialogControlClass::On_Kill_Focus (focus);
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_Key_Down
- //
- ////////////////////////////////////////////////////////////////
- bool
- EditCtrlClass::On_Key_Down (uint32 key_id, uint32 key_data)
- {
- bool handled = false;
- //
- // Check to see if the advise sinks are processing this input...
- //
- if (AdviseSink != NULL) {
- handled = AdviseSink->On_EditCtrl_Key_Down (this, key_id, key_data);
- }
- if (handled == false) {
- handled = Parent->On_EditCtrl_Key_Down (this, key_id, key_data);
- }
- if (handled) {
- return true;
- }
- handled = true;
- bool changed = false;
- bool is_dirty = true;
- int old_caret = CaretPos;
- bool update_hilight = ((DialogMgrClass::Get_VKey_State (VK_SHIFT) & VKEY_PRESSED) == VKEY_PRESSED);
- switch (key_id)
- {
- case VK_BACK:
- if (HilightStartPos >= 0) {
- changed = Delete_Selection ();
- } else if (CaretPos > 0) {
-
- //
- // Delete the previous character
- //
- Title.Erase (CaretPos - 1, 1);
- Set_Caret_Pos (CaretPos - 1);
- assert(NumChars > 0);
- NumChars--;
-
- changed = true;
- }
- update_hilight = false;
- break;
- case VK_DELETE:
- if (HilightStartPos >= 0) {
- changed = Delete_Selection ();
- } else if (CaretPos < Title.Get_Length ()) {
-
- //
- // Delete the next character
- //
- Title.Erase (CaretPos, 1);
- assert(NumChars > 0);
- NumChars--;
-
- changed = true;
- }
- update_hilight = false;
- break;
- case VK_HOME:
- Set_Caret_Pos (0);
- break;
- case VK_END:
- Set_Caret_Pos (Title.Get_Length ());
- break;
- case VK_LEFT:
- //
- // Should snap the caret to words, or just increment it?
- //
- if (DialogMgrClass::Get_VKey_State (VK_CONTROL) & VKEY_PRESSED) {
- Set_Caret_Pos (Find_Word_Start (CaretPos, -1));
- } else {
- Set_Caret_Pos (max (0, CaretPos - 1));
- }
- break;
- case VK_RIGHT:
-
- //
- // Should snap the caret to words, or just increment it?
- //
- if (DialogMgrClass::Get_VKey_State (VK_CONTROL) & VKEY_PRESSED) {
- Set_Caret_Pos (Find_Word_Start (CaretPos, 1));
- } else {
- Set_Caret_Pos (min (Title.Get_Length (), CaretPos + 1));
- }
- break;
- case VK_RETURN:
- //
- // Notify the parent that the user pressed enter
- //
- ADVISE_NOTIFY (On_EditCtrl_Enter_Pressed (this, Get_ID ()));
- is_dirty = false;
- break;
- case VK_UP:
- case VK_DOWN:
- //
- // Don't do anything
- //
- is_dirty = false;
- handled = false;
- break;
- default:
- is_dirty = false;
- handled = false;
- break;
- }
- if (update_hilight) {
- //
- // Update the hilight position
- //
- Update_Hilight (CaretPos, old_caret);
- } else if (handled) {
-
- //
- // Reset the hilight position
- //
- HilightStartPos = -1;
- HilightAnchorPos = -1;
- }
- if (changed) {
- ADVISE_NOTIFY(On_EditCtrl_Change(this, Get_ID()));
- }
- //
- // Repaint the control if necessary
- //
- if (is_dirty) {
- Set_Dirty ();
- }
- return handled;
- }
- void EditCtrlClass::On_Unicode_Char(WCHAR unicode)
- {
- if (unicode >= 32) {
- // Delete the old selection
- Delete_Selection ();
- if (NumChars < TextLimit) {
- NumChars++;
- // Build the new string
- WideStringClass new_title = Title;
- new_title.Erase (CaretPos, new_title.Get_Length () - CaretPos);
- new_title += unicode;
- new_title += Title.Peek_Buffer () + CaretPos;
- Title = new_title;
-
- // Move the caret to the end of the text the user just typed
- Set_Caret_Pos (CaretPos + 1);
- //
- // Reset the hilight position
- //
- HilightStartPos = -1;
- HilightAnchorPos = -1;
-
- ADVISE_NOTIFY(On_EditCtrl_Change(this, Get_ID()));
- // Force a repaint
- Set_Dirty ();
- }
- }
- }
- void EditCtrlClass::Insert_String(const WCHAR* string)
- {
- int count = wcslen(string);
- if (count > 0) {
- // Delete the old selection
- Delete_Selection();
- WideStringClass new_title = Title;
- new_title.Erase(CaretPos, new_title.Get_Length () - CaretPos);
- // If the string exceed the text limit then only insert characters upto
- // the text limit.
- if ((NumChars + count) > TextLimit) {
- // Truncate the insert string so that it will fit.
- WideStringClass insert(count, true);
- insert = string;
- count = (TextLimit - NumChars);
- insert.Erase(count, insert.Get_Length() - count);
- new_title += insert;
- } else {
- new_title += string;
- }
- new_title += Title.Peek_Buffer() + CaretPos;
- Title = new_title;
- NumChars += count;
- // Move the caret to the end of the text the user just typed
- Set_Caret_Pos(CaretPos + count);
- ADVISE_NOTIFY(On_EditCtrl_Change(this, Get_ID()));
- // Force a repaint
- Set_Dirty();
- }
- }
- ////////////////////////////////////////////////////////////////
- //
- // Delete_Selection
- //
- ////////////////////////////////////////////////////////////////
- bool
- EditCtrlClass::Delete_Selection (void)
- {
- if (HilightStartPos >= 0) {
- //
- // Delete the characters and update the caret
- //
- int count = (HilightEndPos - HilightStartPos);
- Title.Erase (HilightStartPos, count);
- Set_Caret_Pos (HilightStartPos);
- assert(NumChars >= count);
- NumChars -= count;
- //
- // Remove any hilighting
- //
- HilightStartPos = -1;
- HilightEndPos = -1;
- //
- // Force a repaint
- //
- Set_Dirty ();
- return true;
- }
- return false;
- }
- ////////////////////////////////////////////////////////////////
- //
- // On_Create
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::On_Create (void)
- {
- //TextColor.Set (0.35F, 1.0F, 0.35F);
- //Set_Text (L"This is a test...");
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Set_Caret_Pos
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Set_Caret_Pos (int new_pos)
- {
- //
- // Remove the hilight
- //
- HilightStartPos = -1;
-
- //
- // Did the caret position change?
- //
- if (CaretPos != new_pos) {
- CaretPos = new_pos;
- //
- // Force the caret to be displayed (i.e. no blink)
- //
- IsCaretDisplayed = true;
- LastCaretBlink = DialogMgrClass::Get_Time ();
- //
- // Handle scrolling
- //
- Update_Scroll_Pos ();
-
- //
- // Force a repaint
- //
- Set_Dirty ();
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Update_Hilight
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Update_Hilight (int new_pos, int anchor_pos)
- {
- HilightAnchorPos = (HilightAnchorPos >= 0) ? HilightAnchorPos : anchor_pos;
- HilightStartPos = min (new_pos, HilightAnchorPos);
- HilightEndPos = max (new_pos, HilightAnchorPos);
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Find_Word_Start
- //
- ////////////////////////////////////////////////////////////////
- int
- EditCtrlClass::Find_Word_Start (int pos, int increment)
- {
- int count = Title.Get_Length ();
- //
- // Determine what the extreme end posiiton should be
- //
- int extreme = pos + increment * 1000;
- int retval = 0;
- if (extreme > count) {
- retval = count;
- }
-
- //
- // Loop over all the characters until we've found the word break
- //
- for (int index = pos + increment; index < count && index >= 0; index += increment) {
-
- //
- // Is this a space character?
- //
- bool is_space = (Title[index] == L' ');
- //
- // If we've already found the word break and this is the
- // start of a new word, then return its index to the caller
- //
- if (!is_space && ((index == 0) || (Title[index - 1] == L' '))) {
- retval = index;
- break;
- }
- }
- return retval;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Update_Scroll_Pos
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Update_Scroll_Pos (void)
- {
- int old_scroll = ScrollPos;
- int caretPos = Get_Caret_Pos();
- //
- // Find the current x-position of the caret
- //
- float x_pos = Pos_From_Character(caretPos);
- if (x_pos <= ClientRect.Left) {
- //
- // Simply ensure the caret is scrolled into view
- //
- ScrollPos = caretPos - 2;
- } else if (x_pos >= ClientRect.Right) {
- //
- // Make a temporary copy of the text so we can
- // modify it
- //
- WideStringClass temp_string(0, true);
- Get_Display_Text(temp_string);
- WCHAR *text = temp_string.Peek_Buffer();
- text[caretPos] = 0;
- //
- // Find how far we'd have to scroll in order to bring the
- // caret back into view.
- //
- for (int index = 0; index < caretPos; ++index) {
- float width = TextRenderer.Get_Text_Extents (text + index).X;
- if (width < ClientRect.Width ()) {
- ScrollPos = index + 2;
- break;
- }
- }
- }
-
- //
- // Adjust the scroll range to stay within our boundaries
- //
- ScrollPos = min (ScrollPos, Title.Get_Length () - 1);
- ScrollPos = max (ScrollPos, 0);
- //
- // Repaint if necessary
- //
- if (old_scroll != ScrollPos) {
- Set_Dirty ();
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Get_Int
- //
- ////////////////////////////////////////////////////////////////
- int
- EditCtrlClass::Get_Int (void)
- {
- return _wtoi (Get_Text ());
- }
- ////////////////////////////////////////////////////////////////
- //
- // Set_Int
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Set_Int (int value)
- {
- WideStringClass text(64, true);
- text.Format (L"%d", value);
- Set_Text (text);
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Set_Text
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Set_Text (const WCHAR *title)
- {
- int count = wcslen(title);
- // If the string is too long then truncate it so that it will fit.
- if (count > TextLimit) {
- WideStringClass text(count, true);
- text = title;
- text.Erase(TextLimit, text.Get_Length() - TextLimit);
- DialogControlClass::Set_Text (text);
- count = TextLimit;
- } else {
- DialogControlClass::Set_Text (title);
- }
- NumChars = count;
- Set_Caret_Pos (Title.Get_Length ());
- HilightAnchorPos = CaretPos;
- HilightStartPos = -1;
- HilightEndPos = -1;
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Get_Text_Length
- //
- ////////////////////////////////////////////////////////////////
- int EditCtrlClass::Get_Text_Length(void) const
- {
- return NumChars;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Set_Text_Limit
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Set_Text_Limit (int numChars)
- {
- if (numChars > 0) {
- TextLimit = numChars;
- } else {
- TextLimit = 0xFFFF;
- }
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Get_Text_Limit
- //
- ////////////////////////////////////////////////////////////////
- int
- EditCtrlClass::Get_Text_Limit (void) const
- {
- return TextLimit;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Get_Caret_Pos
- //
- ////////////////////////////////////////////////////////////////
- int
- EditCtrlClass::Get_Caret_Pos (void) const
- {
- if (mIME && mInComposition) {
- return (CaretPos + mIME->GetCompositionCursorPos());
- }
- return CaretPos;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Set_Sel
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Set_Sel (int start_index, int end_index)
- {
- HilightAnchorPos = start_index;
- HilightStartPos = start_index;
- HilightEndPos = end_index;
- Set_Dirty ();
- return ;
- }
- ////////////////////////////////////////////////////////////////
- //
- // Get_Sel
- //
- ////////////////////////////////////////////////////////////////
- void
- EditCtrlClass::Get_Sel (int &start_index, int &end_index) const
- {
- start_index = HilightStartPos;
- end_index = HilightEndPos;
- return ;
- }
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- bool EditCtrlClass::IsIMEAllowed(void) const
- {
- // We use the ES_OEMCONVERT style to indicate that IME conversion
- // is not allowed for this control. This style bit is set in the
- // resource editor.
- return ((Style & (ES_NUMBER|ES_OEMCONVERT)) == 0);
- }
- #ifdef SHOW_IME_TYPING
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- void EditCtrlClass::Set_IME_Typing_Text_Pos(void)
- {
- Vector2 charExtent = TextRenderer.Get_Text_Extents(L"W");
- int caretPos = Get_Caret_Pos();
- Vector2 pos;
- pos.X = (Pos_From_Character(caretPos) + (charExtent.X * 0.25f));
- pos.Y = (ClientRect.Top + ((ClientRect.Bottom - ClientRect.Top) * 0.25f));
- mIMETypingTip.Set_Position(pos);
- }
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- void EditCtrlClass::Show_IME_Typing_Text(const wchar_t* text)
- {
- mShowIMETypingText = (text && wcslen(text) > 0);
- if (mShowIMETypingText) {
- Set_IME_Typing_Text_Pos();
- mIMETypingTip.Set_Text(text);
- } else {
- mIMETypingTip.Set_Text(L"");
- }
- }
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- void EditCtrlClass::Hide_IME_Typing_Text(void)
- {
- mShowIMETypingText = false;
- mIMETypingTip.Set_Text(L"");
- }
- #endif
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- void EditCtrlClass::PositionCandidateList(void)
- {
- if (mIME) {
- //-------------------------------------------------------------------------
- // Position the candidate window under the edit control
- //-------------------------------------------------------------------------
- unsigned long start = 0;
- unsigned long end = 0;
- mIME->GetTargetClause(start, end);
- int caretPos = CaretPos + start;
- Vector2 pos;
- pos.X = Pos_From_Character(caretPos);
- pos.Y = (Rect.Bottom + 2.0f);
- mCandidateList.Set_Window_Pos(pos);
- //-------------------------------------------------------------------------
- // Reposition the candidate list if it will go off the screen.
- //-------------------------------------------------------------------------
- bool reposition = false;
- const RectClass& screen = Render2DClass::Get_Screen_Resolution();
- const RectClass& ctrlRect = mCandidateList.Get_Window_Rect();
- // If the list will go off the bottom of the screen move it above
- // the edit control.
- if (ctrlRect.Bottom > screen.Bottom) {
- pos.Y = ((Rect.Top - 2.0f) - ctrlRect.Height());
- WWASSERT((pos.Y >= 0.0f) && "CandidateCtrl off the top of the screen");
- reposition = true;
- }
- // Do not allow the control to go off the right of the screen.
- if (ctrlRect.Right > screen.Right) {
- pos.X -= ((ctrlRect.Right - screen.Right) - 1);
- WWASSERT((pos.X >= 0.0f) && "CandidateCtrl of the left of the screen");
- reposition = true;
- }
- if (reposition) {
- mCandidateList.Set_Window_Pos(pos);
- }
- }
- }
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- void EditCtrlClass::HandleNotification(IME::CompositionEvent& imeEvent)
- {
- switch (imeEvent.GetAction()) {
- #ifdef SHOW_IME_TYPING
- case IME::COMPOSITION_TYPING:
- Show_IME_Typing_Text(imeEvent.Subject()->GetTypingString());
- break;
- #endif
- case IME::COMPOSITION_START:
- mInComposition = true;
- Delete_Selection();
- #ifdef SHOW_IME_TYPING
- Show_IME_Typing_Text(imeEvent.Subject()->GetTypingString());
- #endif
- Set_Dirty(true);
- break;
- case IME::COMPOSITION_CHANGE:
- Update_Scroll_Pos();
- Set_Dirty(true);
- break;
- case IME::COMPOSITION_CANCEL:
- case IME::COMPOSITION_END:
- mInComposition = false;
- #ifdef SHOW_IME_TYPING
- Hide_IME_Typing_Text();
- #endif
- // Remove hilight
- HilightStartPos = -1;
- HilightEndPos = -1;
- Set_Dirty(true);
- break;
- case IME::COMPOSITION_RESULT:
- HilightStartPos = -1;
- HilightEndPos = -1;
- Insert_String(imeEvent.Subject()->GetResultString());
- Update_Scroll_Pos();
- break;
- default:
- break;
- }
- }
- /******************************************************************************
- *
- * NAME
- *
- * DESCRIPTION
- *
- * INPUTS
- *
- * RESULT
- *
- ******************************************************************************/
- void EditCtrlClass::HandleNotification(IME::CandidateEvent& imeEvent)
- {
- switch (imeEvent.GetAction()) {
- case IME::CANDIDATE_OPEN: {
- #ifdef SHOW_IME_TYPING
- Hide_IME_Typing_Text();
- #endif
- mCandidateList.Init(imeEvent.Subject());
- PositionCandidateList();
- Parent->Add_Control(&mCandidateList);
- }
- break;
- case IME::CANDIDATE_CHANGE:
- mCandidateList.Changed(imeEvent.Subject());
- break;
- case IME::CANDIDATE_CLOSE:
- mCandidateList.Reset();
- Parent->Remove_Control(&mCandidateList);
- break;
- default:
- break;
- }
- }
|