⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textinput.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

#include "stdafx.h"
#include "TextInput.h"
#include "Extern.h"
#include "MudWindow.h"
#include "TabList.h"
#include "Keyboard.h"

CTextInput::CTextInput(int nXPos, int nYPos, int nWidth, WORD wAttr,
	int nMaxLen)
{
	m_nXPos = nXPos;
	m_nYPos = nYPos;
	m_nWidth = nWidth;
	m_nMaxLen = nMaxLen;
	m_nMaxVisible = m_nWidth-1;
	m_nScrollPos = m_nXPos+m_nWidth-1;

	m_bLineReady = FALSE;

	m_nTextIndex = 0;
	m_strText = "";

	FillChar(m_nXPos,m_nYPos,m_nWidth,' ',wAttr);
	SetColor(wAttr);
	m_nFore = 15;
	m_nBack = 1;

	m_dwCursor.X = m_nXPos;
	m_dwCursor.Y = m_nYPos;

	m_bInsert = TRUE;

	m_nTabPressed = 0;
}

CTextInput::CTextInput()
{
	m_nTextIndex = 0;
	m_strText = "";

	m_bLineReady = FALSE;
	
	m_bInsert = TRUE;
}

void CTextInput::Init(int nXPos, int nYPos, int nWidth, WORD wAttr,
	int nMaxLen)
{
	m_nXPos = nXPos;
	m_nYPos = nYPos;
	m_nWidth = nWidth;
	m_nMaxLen = nMaxLen;
	m_nScrollPos = m_nXPos+m_nWidth-1;
	m_nMaxVisible = m_nWidth-1;

	FillChar(m_nXPos,m_nYPos,m_nWidth,' ',wAttr);
	SetColor(wAttr);

	m_nFore = 15;
	m_nBack = 1;

	m_dwCursor.X = m_nXPos;
	m_dwCursor.Y = m_nYPos;
}

void CTextInput::Move(int nXPos, int nYPos, int nWidth)
{
	m_nXPos = nXPos;
	m_nYPos = nYPos;
	m_nWidth = nWidth;
	
	m_nMaxVisible = m_nWidth-1;
	m_nScrollPos = m_nXPos+m_nWidth-1;

	Redraw();
}

void CTextInput::ControlColor(WORD wAttr)
{
	SetColor(wAttr);
}

void CTextInput::Redraw()
{
	int i;
	int nStartLine, nEndLine;
	int nLen = m_strText.GetLength();

	HideCursor();

	nStartLine = m_nYPos;
	nEndLine = m_nYPos;
			
	for (i=nStartLine;i<=nEndLine;i++)
		FillChar(m_nXPos,i,m_nWidth,' ',m_wAttr);

	// Make sure the cursor is in the visible area in case the
	// size was just changed.
	if (m_dwCursor.X > m_nScrollPos)
		m_dwCursor.X = m_nScrollPos;	

	WORD wOldX = m_dwCursor.X;
	// Draw all the text that will fit on the left up to the
	// cursor position.
	int nSpaceLeft = m_dwCursor.X - m_nXPos;
	CString strTemp;
	strTemp = m_strText.Mid(m_nTextIndex-nSpaceLeft,nSpaceLeft);

	// If we are not at the ent of the text buffer and we are not
	// at the enf of the visible line, we have some stuff to 
	// print to the right of the cursor.
	if (m_nTextIndex != nLen && m_dwCursor.X < m_nScrollPos)
	{
		int nSpaceRight = m_nScrollPos - m_dwCursor.X;
		strTemp += m_strText.Mid(m_nTextIndex,nSpaceRight);
	}
	Gotoxy(m_nXPos,m_nYPos);
	Print(strTemp);

	m_dwCursor.X = wOldX;
	SetFocus();
	ShowCursor();
}

BOOL CTextInput::IsProcessedKey(KEYBOARDCHAR kbch)
{
	if (kbch.bSpecial && kbch.bEnhanced)
	{
		if (!kbch.bShift && !kbch.bControl && !kbch.bAlt)
		{
			switch(kbch.wKeyCode)
			{
				case VK_LEFT :	
				case VK_RIGHT :
				case VK_HOME :
				case VK_END :
				case VK_INSERT :
				case VK_DELETE :
					return(TRUE);
			}
		}

		if (kbch.bControl && !kbch.bShift && !kbch.bAlt)
		{
			switch(kbch.wKeyCode)
			{
				case VK_LEFT :
				case VK_RIGHT :
					return(TRUE);
					break;
			}
		}
	}

	if (!kbch.bSpecial)
	{
		if (kbch.chKey == 8 || kbch.chKey == '\r' || 
			(kbch.chKey >= ' ' && kbch.chKey <= '~') || 
			kbch.chKey == 9 || kbch.chKey == 3 || kbch.chKey == 0x1d)
			return(TRUE);
	}

	return(FALSE);
}

void CTextInput::GetText(CString &strText)
{
	FillChar(m_nXPos,m_nYPos,m_nWidth,' ',m_wAttr);
	m_bLineReady = FALSE;
	strText = m_strText;
	m_strText = "";
	m_nTextIndex = 0;
	Gotoxy(m_nXPos,m_nYPos);
}

void CTextInput::ProcessKey(KEYBOARDCHAR kbch)
{
	SetColor(m_wAttr);
	if (!kbch.bSpecial)
	{
		if (kbch.chKey == 9)
		{
			TabList();
			return;
		}
		else
			m_nTabPressed = 0;

		if (kbch.chKey == 8 || (kbch.chKey >= ' ' && kbch.chKey <= '~') || 
			kbch.chKey == 3 || kbch.chKey == 0x1d)
		{
			InsertChar(kbch.chKey);
			return;
		}

		if (kbch.chKey == '\r')
		{
			m_bLineReady = TRUE;
			return;
		}
	}
	else
	{
		if (kbch.bEnhanced && !kbch.bAlt && !kbch.bShift && kbch.bControl)
		{
			switch(kbch.wKeyCode)
			{
				case VK_RIGHT : // Right Arrow.
					WordRight();
					break;

				case VK_LEFT : // Left Arrow.
					WordLeft();
					break;
			}
			return;
		}

		if (kbch.bEnhanced && !kbch.bAlt && !kbch.bShift && !kbch.bControl)
		{
			switch(kbch.wKeyCode)
			{
				case VK_RIGHT : // Right Arrow.
					ArrowRight();
					break;

				case VK_LEFT : // Left Arrow.
					ArrowLeft();
					break;

				case VK_HOME : // Home.
					Home();
					break;

				case VK_END : // End.
					End();
					break;

				case VK_INSERT : // Insert.
					if (m_bInsert)
						m_bInsert = FALSE;
					else
						m_bInsert = TRUE;
					break;

				case VK_DELETE : // Delete.
					Delete();
					break;
			}
			return;
		}
	}
}

void CTextInput::InsertChar(unsigned char ch)
{
	int nTextLen = m_strText.GetLength();

	if (ch == 8)	// Backspace
	{
		// At the head of the buffer already.
		if (!m_nTextIndex)
			return;

		// At the end of the line.
		if (m_nTextIndex == nTextLen)
		{
			m_strText = m_strText.Left(nTextLen-1);
			m_nTextIndex--;
			Gotoxy(m_dwCursor.X-1,m_nYPos);
			PrintCh(' ');
			Gotoxy(m_dwCursor.X-1,m_nYPos);

			// If that left us at the first column and we are not
			// at the head of the buffer, scroll some more text into
			// view.
			if (m_dwCursor.X == m_nXPos && m_nTextIndex)
			{
				HideCursor();
				nTextLen--;
				int nStart = m_nTextIndex - m_nMaxVisible;
				int nNum = m_nMaxVisible;
				if (nStart < 0)
				{
					nStart = 0;
					nNum = nTextLen;
				}
				CString strTemp(m_strText.Mid(nStart,nNum));
				CString strPadding(' ',m_nMaxVisible-nNum);
				Gotoxy(m_nXPos,m_nYPos);
				Print(strTemp+strPadding);
				Gotoxy(m_nXPos+nNum,m_nYPos);
				ShowCursor();
			}
			return;
		}
	
		// Backspace anywhere but at the end would be the same
		// as pressing delete 1 char back.  So set the index to
		// 1 less and call delete.
		m_nTextIndex--;
		Gotoxy(m_dwCursor.X-1,m_nYPos);
		Delete();
		return;
	}

	if (m_bInsert && nTextLen+1 > m_nMaxLen)
		return;

	if (!m_bInsert && m_nTextIndex == nTextLen && nTextLen+1 > m_nMaxLen)
		return;

	// At the end of the line, tack it on.
	if (nTextLen == m_nTextIndex)
	{
		// If the cursor is in the last visible character position
		// need to scroll the text.
		if (m_dwCursor.X == m_nScrollPos)
			ScrollLeft();

		PrintCh(ch);
		m_strText += ch;
		m_nTextIndex++;

		return;
	}

	if (m_bInsert)
	{
		// Insert the character in to the string.
		CString strRight(m_strText.Right(nTextLen-m_nTextIndex));
		m_strText = m_strText.Left(m_nTextIndex) + CString(ch) + strRight;

		if (m_dwCursor.X == m_nScrollPos)
		{
			ScrollLeft();
			m_nTextIndex++;
		}
		else
		{
			// Grab a chunk of text to move to the right in the control.
			nTextLen++;
			int nStart = m_nTextIndex+1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -