qstescintillabase.cpp

来自「porting scintilla to qt」· C++ 代码 · 共 713 行 · 第 1/2 页

CPP
713
字号
#include <qapplication.h>#include <qevent.h>#include <qpainter.h>#include <qscrollbar.h>#include <qstring.h>#include <qbytearray.h>#include <qdrag.h>#include <qmimedata.h>#include <qclipboard.h>#include <qcolor.h>#include <qscrollbar.h>#include <QContextMenuEvent>#include <QDragEnterEvent>#include <QDragMoveEvent>#include <QDropEvent>#include <QDragLeaveEvent>#include <QFocusEvent>#include <QKeyEvent>#include <QList>#include <QMouseEvent>#include <QPaintEvent>#include "constants.h"#include "Platform.h"#include "Scintilla.h"#include "SVector.h"#include "SplitVector.h"#include "Partitioning.h"#include "CellBuffer.h"#include "CharClassify.h"#include "Document.h"#include "Style.h"#include "XPM.h"#include "LineMarker.h"#include "Indicator.h"#include "ViewStyle.h"#include "KeyMap.h"#include "ContractionState.h"#include "Editor.h"#include "AutoComplete.h"#include "CallTip.h"#include "SString.h"#include "PropSet.h"#include "Accessor.h"#include "KeyWords.h"#include "Partitioning.h"#include "ExternalLexer.h"#include "ScintillaBase.h"#include "qstescintillabase.h"#include "qsteclasses.h"QSteScintillaBase::QSteScintillaBase(QWidget *parent):ScintillaBase(),QAbstractScrollArea(parent),capturedMouse(false){	//init QAbstractScrollArea	connect(verticalScrollBar(),SIGNAL(valueChanged(int)),SLOT(handleVSb(int)));	connect(horizontalScrollBar(),SIGNAL(valueChanged(int)),SLOT(handleHSb(int)));	setAcceptDrops(true);	setFocusPolicy(Qt::WheelFocus);	setAttribute(Qt::WA_KeyCompression);	viewport()->setBackgroundRole(QPalette::Base);	viewport()->setMouseTracking(true);	triple_click.setSingleShot(true);    connect(&qtimer, SIGNAL(timeout()), SLOT(handleTimer()));	sendMsgToSci(SCI_SETCARETPERIOD,QApplication::cursorFlashTime()/2);	QClipboard *cb = QApplication::clipboard();	if(cb->supportsSelection())		connect(cb,SIGNAL(selectionChanged()),SLOT(handleSelection()));	//init scintilla	wMain = viewport(); 	Initialise();}QSteScintillaBase::~QSteScintillaBase(){}//send message interfacelong QSteScintillaBase::sendMsgToSci(unsigned int msg,unsigned long wparam,long lparam){	return WndProc(msg,wparam,lparam);}long QSteScintillaBase::sendMsgToSci(unsigned int msg,unsigned long wparam,const char *lparam){	return sendMsgToSci(msg,wparam,reinterpret_cast<long>(lparam));}long QSteScintillaBase::sendMsgToSci(unsigned int msg,const char *lparam){	return sendMsgToSci(msg,0UL,reinterpret_cast<long>(lparam));}long QSteScintillaBase::sendMsgToSci(unsigned int msg,const char *wparam,const char *lparam){	return sendMsgToSci(msg,reinterpret_cast<unsigned long>(wparam),reinterpret_cast<long>(lparam));}long QSteScintillaBase::sendMsgToSci(unsigned int msg,long wparam){	return sendMsgToSci(msg,static_cast<unsigned long>(wparam),0L);}long QSteScintillaBase::sendMsgToSci(unsigned int msg,int wparam){	return sendMsgToSci(msg,static_cast<unsigned long>(wparam),0L);}long QSteScintillaBase::sendMsgToSci(unsigned int msg,long cpMin, long cpMax,char *lpstrText){	TextRange text;	text.chrg.cpMin = cpMin;	text.chrg.cpMax = cpMax;	text.lpstrText = lpstrText;	return WndProc(msg,0,reinterpret_cast<long>(&text));}long QSteScintillaBase::sendMsgToSci(unsigned int msg,unsigned long wparam,const QColor &col){	if(QApplication::colorSpec() == 32){	}	long lparam = (col.blue() << 16) | (col.green() << 8) | col.red();	return WndProc(msg,wparam,lparam);}long QSteScintillaBase::sendMsgToSci(unsigned int msg,const QColor &col){	if(QApplication::colorSpec() == 32){	}	long wparam = (col.blue() << 16) | (col.green() << 8) | col.red();	return WndProc(msg,wparam,0L);}long QSteScintillaBase::sendMsgToSci(unsigned int msg,unsigned long wparam,QPainter *hdc,const QRect &rc,long cpmin,long cpmax){	RangeToFormat rtf;	rtf.hdc = rtf.hdcTarget = reinterpret_cast<SurfaceID>(hdc);	rtf.rc.left = rc.left();	rtf.rc.top = rc.top();	rtf.rc.right = rc.right() + 1;	rtf.rc.bottom = rc.bottom() + 1;	rtf.chrg.cpMin = cpmin;	rtf.chrg.cpMax = cpmax;	return WndProc(msg,wparam,reinterpret_cast<long>(&rtf));}long QSteScintillaBase::sendMsgToSci(unsigned int msg,unsigned long wparam,const QPixmap &lparam){	return sendMsgToSci(msg,wparam,reinterpret_cast<long>(&lparam));}//reimplemented to handle the context menuvoid QSteScintillaBase::contextMenuEvent(QContextMenuEvent *event){	ContextMenu(Point(event->globalX(),event->globalY()));//ContextMenu inherits from ScintillaBase}//reimplemented to handle drag entersvoid QSteScintillaBase::dragEnterEvent(QDragEnterEvent *event){	dragMoveEvent(event);}//reimplemented to handle drag movevoid QSteScintillaBase::dragMoveEvent(QDragMoveEvent *event){	SetDragPosition(PositionFromLocation(Point(event->pos().x(),event->pos().y())));	if(pdoc->IsReadOnly() || !event->mimeData()->hasText()){		event->ignore();		return;	}	event->acceptProposedAction();}//reimplemented to handle drag leavesvoid QSteScintillaBase::dragLeaveEvent(QDragLeaveEvent *event){	SetDragPosition(-1);}//reimplemented to handle drop eventvoid QSteScintillaBase::dropEvent(QDropEvent *event){	bool moving;	const char *s;	if(pdoc->IsReadOnly() || !event->mimeData()->hasText()){		event->ignore();		return;	}	event->acceptProposedAction();	moving = (event->dropAction() == Qt::MoveAction && (event->source() == this || event->source() == viewport()));	sendMsgToSci(SCI_DELETEBACK);	QByteArray ba;	if(IsUnicodeMode())		ba = event->mimeData()->text().toUtf8();	else		ba = event->mimeData()->text().toLatin1();	s = ba.data();	DropAt(posDrop,s,moving,false);	Redraw();}//reimplemented to handle get focusvoid QSteScintillaBase::focusInEvent(QFocusEvent *event){	SetFocusState(true);//SetFocusState inherits from Editor}//reimplemented to handle lost focusvoid QSteScintillaBase::focusOutEvent(QFocusEvent *event){	if(!sendMsgToSci(SCI_AUTOCACTIVE))		SetFocusState(false);//SetFocusState inherits from Editor}//reimplemented to allow tabs to be entered as textbool QSteScintillaBase::focusNextPrevChild(bool next){	if(!pdoc->IsReadOnly())		return false;    return QAbstractScrollArea::focusNextPrevChild(next);}//reimplemented to handle key pressesvoid QSteScintillaBase::keyPressEvent(QKeyEvent *event){	unsigned key;	QByteArray utf8;	bool shift = event->modifiers() & Qt::ShiftModifier;	bool ctrl = event->modifiers() & Qt::ControlModifier;	bool alt = event->modifiers() & Qt::AltModifier;	switch(event->key()){		case Qt::Key_Down:			key = SCK_DOWN;			break;		case Qt::Key_Up:			key = SCK_UP;			break;		case Qt::Key_Left:			key = SCK_LEFT;			break;		case Qt::Key_Right:			key = SCK_RIGHT;			break;		case Qt::Key_Home:			key = SCK_HOME;			break;		case Qt::Key_End:			key = SCK_END;			break;		case Qt::Key_PageUp:			key = SCK_PRIOR;			break;		case Qt::Key_PageDown:			key = SCK_NEXT;			break;		case Qt::Key_Delete:			key = SCK_DELETE;			break;    	case Qt::Key_Insert:        	key = SCK_INSERT;	        break;	    case Qt::Key_Escape:    	    key = SCK_ESCAPE;        	break;	    case Qt::Key_Backspace:			printf("key backspace press\n");    	    key = SCK_BACK;        	break;	    case Qt::Key_Tab:    	    key = SCK_TAB;        	break;	    case Qt::Key_Return:    	case Qt::Key_Enter:        	key = SCK_RETURN;	        break;	    case Qt::Key_Super_L:    	    key = SCK_WIN;        	break;	    case Qt::Key_Super_R:    	    key = SCK_RWIN;        	break;	    case Qt::Key_Menu:    	    key = SCK_MENU;        	break;		default:			utf8 = event->text().toUtf8();			if(utf8.length() != 1)				key = 0;			else if((key = utf8[0]) >= 0x80)				key = 0;			else if(key >= 0x01 && key <= 0x1a)				key += 0x40;			else if(key >= 'A' && key <= 'Z')				shift = true;			else if(key >= 'a' && key <= 'z'){				key -= 0x20;				shift = false;			}	}	//deal user's keybinds here	//for(QSteKeyBinds	if(1){		//auto complete		if(ctrl && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)){			startAutoComplete();			return;		}		//brace match		if(ctrl && shift && (event->key() == Qt::Key_E)){			selectBraceMatch();			return;		}		//brace match		if(ctrl && (event->key() == Qt::Key_E)){			gotoBraceMatch();			return;		}		//abbrev		if(ctrl && (event->key() == Qt::Key_B)){			expandAbbreviation();			return;		}	}	if(key){		bool consumed = false;		KeyDown(key,shift,ctrl,alt,&consumed);//herits from Editor		if(consumed){			event->accept();		}	}	if(utf8.length() > 0 && (IsUnicodeMode() || utf8.length() == 1)){		AddCharUTF(utf8.data(),utf8.length());		event->accept();	}else{//		QAbstractScrollArea::keyPressEvent(event);	}}//reimplemented to handle input method charactersvoid QSteScintillaBase::inputMethodEvent(QInputMethodEvent *event){	QByteArray utf8 = event->commitString().toUtf8();	AddCharUTF(utf8.data(),utf8.length());	event->accept();}//reimplemented to handle mouse double clicksvoid QSteScintillaBase::mouseDoubleClickEvent(QMouseEvent *event){	if(event->button() != Qt::LeftButton){		event->ignore();		return;	}	setFocus();	unsigned clicktime = lastClickTime + Platform::DoubleClickTime() - 1;	bool shift = event->modifiers() & Qt::ShiftModifier;	bool ctrl = event->modifiers() & Qt::ControlModifier;	bool alt = event->modifiers() & Qt::AltModifier;	ButtonDown(Point(event->x(),event->y()),clicktime,shift,ctrl,alt);	triple_click_at = event->globalPos();	triple_click.start(QApplication::doubleClickInterval());}//reimplemented to handle mouse movesvoid QSteScintillaBase::mouseMoveEvent(QMouseEvent *event){	ButtonMove(Point(event->x(),event->y()));}//reimplemented to handle mouse pressesvoid QSteScintillaBase::mousePressEvent(QMouseEvent *event){	setFocus();

⌨️ 快捷键说明

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