Ejemplo n.º 1
0
BOOL ClipboardCut(void)
{
	if (!ClipboardCopy()) return (FALSE);
	DeleteSelected(17);
	GetXPos(&CurrCol);
	ShowEditCaret();
	return (TRUE);
}
Ejemplo n.º 2
0
void cGUIEdit::OnUpdate()
{
	iRect rc = scrRect();
	int mx = g_gui->m_mouse.x - rc.p1.x;
	bool shift = einput->shift();
	bool ctrl = einput->ctrl();
	bool enter = einput->isKeyDown(DIK_RETURN) || einput->isKeyDown(DIK_NUMPADENTER);
	
	int width = rect.Width();
	int height = rect.Height();
	int cursorx = Chr2Pos(m_sel2);
	int deltax = 0;
	if(cursorx>width-height/2) deltax = -(cursorx-(width-height/2));

	BOOL captured = (g_gui->m_capture == this);
	BOOL clicked = einput->isMouseDown(0);
	BOOL clickedin = clicked && m_mousein;

	if(clicked)
	{
		if(clickedin)
		{
			if(!m_edit) m_bktxt = txt;
			Capture(true);
			m_edit = true;
			m_sel1 = m_sel2 = Pos2Chr(mx-deltax);
		
			// double click
			static int dblclicktick = 0;
			int tick = GetTickCount();
			if(tick-dblclicktick<500) // select all
			{
				m_sel1=0; m_sel2=txt.size();
				Capture(false);
			}
			dblclicktick = tick;
		}
		else
		{
			if(m_edit)
			{
				Action(0);
				m_edit = false;
				m_sel1 = m_sel2 = 0;
			}
		}
	}
	else
	if(captured)
	{
		m_sel2 = Pos2Chr(mx-deltax);
		if( !einput->mouseValue(0) ) // for selections
		{
			Capture(false);
		}
	}

	int s1 = sel1(), s2 = sel2();

	if(m_edit)
	{
		if(einput->isKeyDown(DIK_ESCAPE))
		{
			if(!m_bktxt.empty()) txt = m_bktxt; // restore
			m_edit = false;
			m_sel1 = m_sel2 = 0;
			return;
		}

		if(enter || einput->isKeyDown(DIK_TAB))
		{
			Action(enter ? 1 : 3);
			m_edit = false;
			m_sel1 = m_sel2 = 0;
			return;
		}

		if(einput->isKeyDown(DIK_DELETE))
		{
			if(s1!=s2)
				ShiftLeft(s2,s2-s1); // selection cut
			else
				ShiftLeft(s1+1,1);
			m_sel2 = m_sel1 = s1;
			return;
		}	

		if(einput->isKeyDown(DIK_BACK))
		{
			if(s1!=s2)
				ShiftLeft(s2,s2-s1); // selection cut
			else
				ShiftLeft(s1,1);
			s1--;
			if(s1<0) s1 = 0;
			m_sel2 = m_sel1 = s1;
			return;
		}	
		
		if(einput->isKeyDown(DIK_HOME))
		{
			m_sel2 = 0;
			if(!shift) m_sel1 = m_sel2;
			return;
		}
		
		if(einput->isKeyDown(DIK_END))
		{
			m_sel2 = txt.size();
			if(!shift) m_sel1 = m_sel2;
			return;
		}
		
		if(einput->isKeyDown(DIK_LEFT))
		{
			if(m_sel1==m_sel2 || shift)
			{
				m_sel2--;
				if(m_sel2<0) m_sel2 = 0;
				if(!shift) m_sel1 = m_sel2;
			}
			else // break selection
			{
				if(m_sel2<m_sel1) 
					m_sel1 = m_sel2;
				else 
					m_sel2 = m_sel1;
			}
			return;
		}
		
		if(einput->isKeyDown(DIK_RIGHT))
		{
			if(m_sel1==m_sel2 || shift)
			{
				m_sel2++;
				if(m_sel2>txt.size()) m_sel2 = txt.size();
				if(!shift) m_sel1 = m_sel2;
			}
			else // break selection
			{
				if(m_sel2<m_sel1) 
					m_sel2 = m_sel1;
				else 
					m_sel1 = m_sel2;
			}
			return;
		}

		if(ctrl && einput->isKeyDown(DIK_C))
		{
			ClipboardCopy();
			return;
		}
		
		if(ctrl && einput->isKeyDown(DIK_V))
		{
			ClipboardPaste();
			return;
		}

		if(ctrl && einput->isKeyDown(DIK_X))
		{
			ClipboardCopy();
			SelectionCut();
			return;
		}

		if(!einput->keyQueue.empty())
		{
			for(auto ch: einput->keyQueue)
			{
				if(std::isprint(ch))
				{
					if(s1!=s2)
						ShiftLeft(s2,s2-s1); // selection cut
					m_sel2 = m_sel1 = s1;
					txt = txt.substr(0, s1) + ch + txt.substr(s1);
					m_sel1++;
					s1 = s2 = m_sel2 = m_sel1;
				}
			}
			einput->keyQueue.clear();
		}
	}

	// right click action
	if(!IsCaptured() && m_mousein && einput->isMouseDown(1))
		Action(2);
}