Thread Tools Display Modes
10-04-11, 09:25 AM   #1
killerpet1986
A Fallenroot Satyr
 
killerpet1986's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2008
Posts: 22
Post Getting EditBox Highlighted Text

Is there any way to get the start and end position of the text being highlighted in an editbox?

EditBox:GetCursorPosition() will only return the end position. I guess I could call EditBox:GetCursorPosition() when an OnMouseDown event occurs. Is there any other way?
  Reply With Quote
10-04-11, 01:44 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I am not certain as to a better solution, but... your solution above would not be reliable. I do not use the mouse at all when selecting text in an editbox. I either use Ctrl-A to copy all or hold down Shift and use the arrow keys. This second method also requires you to hold down Alt in order to navigate with the arrow keys, unless using a chat addon that removes this requirement.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
10-04-11, 02:46 PM   #3
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
I can't really think of an easy way to do this. I had a look through some Blizzard UI code because I know it uses text selections occasionally (e.g. mail autocomplete) but everything in there seems to only ever set the selection, never read it. It seems like an odd omission from the API and something Blizzard should probably add, but saying that now probably doesn't help you tremendously.
  Reply With Quote
10-10-11, 07:44 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Generally you will get more helpful answers if you explain what your end goal is, rather than only mentioning a specific method you're having trouble with. If something can't be done one way, it may be doable in another way, but without knowing what you actually want to do, it's pretty hard to help.
  Reply With Quote
10-10-11, 10:12 PM   #5
killerpet1986
A Fallenroot Satyr
 
killerpet1986's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2008
Posts: 22
Originally Posted by Phanx View Post
Generally you will get more helpful answers if you explain what your end goal is, rather than only mentioning a specific method you're having trouble with. If something can't be done one way, it may be doable in another way, but without knowing what you actually want to do, it's pretty hard to help.

To be honest, the comments so far have been very helpful. It is possible but just not easy to implement.

To answer your question, what I was interested in was adding color to TinyPad notes. (E.g. If i highlight "test text" i can change it to "|cffXXXXXXtest text|r", where XXXXXX is the color)

At present I can do this using a macro and the :Insert() function. (However, if any text is highlighted then the insert function overwrites that text)

Example on my DK alt:
  Reply With Quote
10-11-11, 05:37 PM   #6
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
If you backup the editbox's text and then call Insert, you can figure out what was selected by finding what got deleted.

I went ahead and got a script working to color highlighted text sort of how a rich text editor might do it, and it works pretty nicely.
lua Code:
  1. --- @return StartPos, EndPos of highlight in this editbox.
  2. local function GetTextHighlight ( self )
  3.   local Text, Cursor = self:GetText(), self:GetCursorPosition();
  4.   self:Insert( "" ); -- Delete selected text
  5.   local TextNew, CursorNew = self:GetText(), self:GetCursorPosition();
  6.   -- Restore previous text
  7.   self:SetText( Text );
  8.   self:SetCursorPosition( Cursor );
  9.   local Start, End = CursorNew, #Text - ( #TextNew - CursorNew );
  10.   self:HighlightText( Start, End );
  11.   return Start, End;
  12. end
  13.  
  14.  
  15. local StripColors;
  16. do
  17.   local CursorPosition, CursorDelta;
  18.   --- Callback for gsub to remove unescaped codes.
  19.   local function StripCodeGsub ( Escapes, Code, End )
  20.     if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code
  21.       if ( CursorPosition and CursorPosition >= End - 1 ) then
  22.         CursorDelta = CursorDelta - #Code;
  23.       end
  24.       return Escapes;
  25.     end
  26.   end
  27.   --- Removes a single escape sequence.
  28.   local function StripCode ( Pattern, Text, OldCursor )
  29.     CursorPosition, CursorDelta = OldCursor, 0;
  30.     return Text:gsub( Pattern, StripCodeGsub ),
  31.     OldCursor and CursorPosition + CursorDelta;
  32.   end
  33.   --- Strips Text of all color escape sequences.
  34.   -- @param Cursor  Optional cursor position to keep track of.
  35.   -- @return Stripped text, and the updated cursor position if Cursor was given.
  36.   function StripColors ( Text, Cursor )
  37.     Text, Cursor = StripCode( "(|*)(|c%x%x%x%x%x%x%x%x)()", Text, Cursor );
  38.     return StripCode( "(|*)(|r)()", Text, Cursor );
  39.   end
  40. end
  41. local COLOR_END = "|r";
  42. --- Wraps this editbox's selected text with the given color.
  43. local function ColorSelection ( self, ColorCode )
  44.   local Start, End = GetTextHighlight( self );
  45.   local Text, Cursor = self:GetText(), self:GetCursorPosition();
  46.   if ( Start == End ) then -- Nothing selected
  47.     --Start, End = Cursor, Cursor; -- Wrap around cursor
  48.     return; -- Wrapping the cursor in a color code and hitting backspace crashes the client!
  49.   end
  50.  
  51.   -- Find active color code at the end of the selection
  52.   local ActiveColor;
  53.   if ( End < #Text ) then -- There is text to color after the selection
  54.     local ActiveEnd;
  55.     local CodeEnd, _, Escapes, Color = 0;
  56.     while ( true ) do
  57.       _, CodeEnd, Escapes, Color = Text:find( "(|*)(|c%x%x%x%x%x%x%x%x)", CodeEnd + 1 );
  58.       if ( not CodeEnd or CodeEnd > End ) then
  59.         break;
  60.       end
  61.       if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code
  62.         ActiveColor, ActiveEnd = Color, CodeEnd;
  63.       end
  64.     end
  65.    
  66.     if ( ActiveColor ) then
  67.       -- Check if color gets terminated before selection ends
  68.       CodeEnd = 0;
  69.       while ( true ) do
  70.         _, CodeEnd, Escapes = Text:find( "(|*)|r", CodeEnd + 1 );
  71.         if ( not CodeEnd or CodeEnd > End ) then
  72.           break;
  73.         end
  74.         if ( CodeEnd > ActiveEnd and #Escapes % 2 == 0 ) then -- Terminates ActiveColor
  75.           ActiveColor = nil;
  76.           break;
  77.         end
  78.       end
  79.     end
  80.   end
  81.  
  82.   local Selection = Text:sub( Start + 1, End );
  83.   -- Remove color codes from the selection
  84.   local Replacement, CursorReplacement = StripColors( Selection, Cursor - Start );
  85.  
  86.   self:SetText( ( "" ):join(
  87.       Text:sub( 1, Start ),
  88.       ColorCode, Replacement, COLOR_END,
  89.       ActiveColor or "", Text:sub( End + 1 )
  90.   ) );
  91.  
  92.   -- Restore cursor and highlight, adjusting for wrapper text
  93.   Cursor = Start + CursorReplacement;
  94.   if ( CursorReplacement > 0 ) then -- Cursor beyond start of color code
  95.     Cursor = Cursor + #ColorCode;
  96.   end
  97.   if ( CursorReplacement >= #Replacement ) then -- Cursor beyond end of color
  98.     Cursor = Cursor + #COLOR_END;
  99.   end
  100.   self:SetCursorPosition( Cursor );
  101.   -- Highlight selection and wrapper
  102.   self:HighlightText( Start, #ColorCode + ( #Replacement - #Selection ) + #COLOR_END + End );
  103. end

You can use it like this:
lua Code:
  1. ColorSelection( _DevPad.GUI.Editor.Edit, IsShiftKeyDown() and "|cffff1111" or "|cff11ff11" ); -- Colors highlighted text in _DevPad green, or red if shift is held.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Getting EditBox Highlighted Text

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off