Thread Tools Display Modes
03-29-13, 07:57 AM   #1
Ghost Dancer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 5
Editbox Right Click

I want to add some text formatting options to my Noteworthy addon - the options will be accessed via its right-click context menu. However, right clicking in an editbox clears the current selected text and just sets a new cursor position (i.e. the same as left clicking).

Is there any way of preventing this happening so the selection remains when my context menu is shown? Bear in mind that I need to detect a right-click to open the context menu and I also need to keep the default left click functionality so the user can still click, select text etc.
  Reply With Quote
03-29-13, 04:18 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Post the code you're using the create the current editbox.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
03-30-13, 01:18 PM   #3
Ghost Dancer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 5
I'm using the following template for the editbox:

Code:
<EditBox name="$parentEditBox" letters="5000" multiLine="true" enableMouse="true" autoFocus="false">
	<Size>
		<AbsDimension x="315" y="315" />
	</Size>
	
	<Scripts>
		<OnEscapePressed>self:ClearFocus();</OnEscapePressed>
		<OnMouseUp>
			if button == "LeftButton" then
				Noteworthy_SetTextFocus();
			else
				Noteworthy_EditContextMenu();
			end
		</OnMouseUp>
		<OnCursorChanged>Noteworthy_CheckLink(self);</OnCursorChanged>
		<OnReceiveDrag>Noteworthy_Drag();</OnReceiveDrag>
	</Scripts>
	
	<FontString inherits="ChatFontNormal" />
</EditBox>
  Reply With Quote
03-31-13, 12:22 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Based on some quick testing, it looks like there is no way to prevent the default behavior of the editbox highlight, so you'd need to store the highlight and cursor positions yourself in OnMouseUp with the LeftButton, then use editbox:ClearFocus() on OnMouseDown and OnMouseUp with the RightButton, and finally restore the cursor and highlight positions when your menu closes.

It might be easier to add control buttons for the formatting options, so you're not wrestling with the baked-in highlighting behavior.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
03-31-13, 05:59 AM   #5
Ghost Dancer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 5
Yeah, I had a feeling that might be the case but thought it was worth asking. Thanks for replying. I think a separate button is the way to go - even if I store the highlight position, it would be confusing to the user I think.

Last edited by Ghost Dancer : 03-31-13 at 02:28 PM.
  Reply With Quote
03-31-13, 02:28 PM   #6
Ghost Dancer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 5
After some further thought on this I've managed to code a solution by saving and restoring the selection. I'll post the code once I've cleaned it up a bit (its a bit messy ATM).
  Reply With Quote
04-01-13, 03:50 AM   #7
Ghost Dancer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 5
OK, here's the XML code:

Code:
<EditBox name="$parentEditBox" letters="5000" multiLine="true" enableMouse="true" autoFocus="false">
	<Size>
		<AbsDimension x="315" y="315" />
	</Size>
	
	<Scripts>
		<OnCursorChanged>MyAddon_EditBoxCursor(self);</OnCursorChanged>
		<OnHide>MyAddon_CurrentEditBox = nil;</OnHide>
		<OnMouseDown>MyAddon_MouseButton = button;</OnMouseDown>
		<OnMouseUp>
			if button == "RightButton" then
				MyAddon_RestoreHighlight()
				MyAddon_EditContextMenu()
			end;
		</OnMouseUp>
	</Scripts>
</EditBox>
And the Lua:

Code:
function MyAddon_EditBoxCursor(self)
	if MyAddon_MouseButton == "LeftButton" then
		local cursorPos = self:GetCursorPosition();
		
		if (cursorPos ~= MyAddon_CursorPos) then
			MyAddon_CurrentEditBox = self;
			
			local Text = self:GetText();
			self:Insert(""); -- Delete selected text
			
			local TextNew = self:GetText();
			local CursorNew = self:GetCursorPosition();
			
			MyAddon_HighlightStart = CursorNew;
			MyAddon_HighlightEnd = #Text - (#TextNew - CursorNew);
			
			-- Restore previous text
			self:SetText(Text);
			MyAddon_RestoreHighlight();
		end
	end
end


function MyAddon_RestoreHighlight()
	if Zath_CurrentEditBox and Zath_HighlightStart ~= nil then
		MyAddon_CurrentEditBox:SetCursorPosition(MyAddon_CursorPos);
		MyAddon_CurrentEditBox:HighlightText(MyAddon_HighlightStart, MyAddon_HighlightEnd);
	end
end
Edit: Fixed a bug in the RestoreHighlight function so it doesn't incorrectly select the entire text if selection hasn't been stored.

Last edited by Ghost Dancer : 04-01-13 at 09:09 AM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Editbox Right Click


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