📄 wxscintilla.cpp
字号:
////////////////////////////////////////////////////////////////////////////// Name: wxscintilla.cpp// Purpose: A wxWidgets implementation of Scintilla. This class is the// one meant to be used directly by wx applications. It does not// derive directly from the Scintilla classes, but instead// delegates most things to the real Scintilla class.// This allows the use of Scintilla without polluting the// namespace with all the classes and identifiers from Scintilla.//// Author: Robin Dunn//// Created: 13-Jan-2000// RCS-ID: $Id: wxscintilla.cpp,v 1.1 2008/02/24 16:18:07 rollmark Exp $// Copyright: (c) 2004 wxCode// Licence: wxWindows/////////////////////////////////////////////////////////////////////////////#include <ctype.h>#include "ScintillaWX.h"#include "wx/wxscintilla.h"#include <wx/wx.h>#include <wx/tokenzr.h>#include <wx/mstream.h>#include <wx/image.h>#include <wx/file.h>//----------------------------------------------------------------------const wxChar* wxSCINameStr = _T("SCIwindow");#ifdef MAKELONG#undef MAKELONG#endif#define MAKELONG(a, b) ((a) | ((b) << 16))static long wxColourAsLong(const wxColour& co) { return (((long)co.Blue() << 16) | ((long)co.Green() << 8) | ((long)co.Red()));}static wxColour wxColourFromLong(long c) { wxColour clr; clr.Set((unsigned char)(c & 0xff), (unsigned char)((c >> 8) & 0xff), (unsigned char)((c >> 16) & 0xff)); return clr;}static wxColour wxColourFromSpec(const wxString& spec) { // spec should be a colour name or "#RRGGBB" if (spec.GetChar(0) == _T('#')) { long red, green, blue; red = green = blue = 0; spec.Mid(1,2).ToLong(&red, 16); spec.Mid(3,2).ToLong(&green, 16); spec.Mid(5,2).ToLong(&blue, 16); return wxColour((unsigned char)red, (unsigned char)green, (unsigned char)blue); }else{ return wxColour(spec); }}//----------------------------------------------------------------------DEFINE_EVENT_TYPE( wxEVT_SCI_CHANGE )DEFINE_EVENT_TYPE( wxEVT_SCI_STYLENEEDED )DEFINE_EVENT_TYPE( wxEVT_SCI_CHARADDED )DEFINE_EVENT_TYPE( wxEVT_SCI_SAVEPOINTREACHED )DEFINE_EVENT_TYPE( wxEVT_SCI_SAVEPOINTLEFT )DEFINE_EVENT_TYPE( wxEVT_SCI_ROMODIFYATTEMPT )DEFINE_EVENT_TYPE( wxEVT_SCI_KEY )DEFINE_EVENT_TYPE( wxEVT_SCI_DOUBLECLICK )DEFINE_EVENT_TYPE( wxEVT_SCI_UPDATEUI )DEFINE_EVENT_TYPE( wxEVT_SCI_MODIFIED )DEFINE_EVENT_TYPE( wxEVT_SCI_MACRORECORD )DEFINE_EVENT_TYPE( wxEVT_SCI_MARGINCLICK )DEFINE_EVENT_TYPE( wxEVT_SCI_NEEDSHOWN )DEFINE_EVENT_TYPE( wxEVT_SCI_PAINTED )DEFINE_EVENT_TYPE( wxEVT_SCI_USERLISTSELECTION )DEFINE_EVENT_TYPE( wxEVT_SCI_URIDROPPED )DEFINE_EVENT_TYPE( wxEVT_SCI_DWELLSTART )DEFINE_EVENT_TYPE( wxEVT_SCI_DWELLEND )DEFINE_EVENT_TYPE( wxEVT_SCI_START_DRAG )DEFINE_EVENT_TYPE( wxEVT_SCI_DRAG_OVER )DEFINE_EVENT_TYPE( wxEVT_SCI_DO_DROP )DEFINE_EVENT_TYPE( wxEVT_SCI_ZOOM )DEFINE_EVENT_TYPE( wxEVT_SCI_HOTSPOT_CLICK )DEFINE_EVENT_TYPE( wxEVT_SCI_HOTSPOT_DCLICK )DEFINE_EVENT_TYPE( wxEVT_SCI_CALLTIP_CLICK )DEFINE_EVENT_TYPE( wxEVT_SCI_AUTOCOMP_SELECTION )BEGIN_EVENT_TABLE(wxScintilla, wxControl) EVT_PAINT (wxScintilla::OnPaint) EVT_SCROLLWIN (wxScintilla::OnScrollWin) EVT_SCROLL (wxScintilla::OnScroll) EVT_SIZE (wxScintilla::OnSize) EVT_LEFT_DOWN (wxScintilla::OnMouseLeftDown) // Let Scintilla see the double click as a second click EVT_LEFT_DCLICK (wxScintilla::OnMouseLeftDown) EVT_MOTION (wxScintilla::OnMouseMove) EVT_LEFT_UP (wxScintilla::OnMouseLeftUp)#if defined(__WXGTK__) || defined(__WXMAC__) EVT_RIGHT_UP (wxScintilla::OnMouseRightUp)#else EVT_CONTEXT_MENU (wxScintilla::OnContextMenu)#endif EVT_MOUSEWHEEL (wxScintilla::OnMouseWheel) EVT_MIDDLE_UP (wxScintilla::OnMouseMiddleUp) EVT_CHAR (wxScintilla::OnChar) EVT_KEY_DOWN (wxScintilla::OnKeyDown) EVT_KILL_FOCUS (wxScintilla::OnLoseFocus) EVT_SET_FOCUS (wxScintilla::OnGainFocus) EVT_SYS_COLOUR_CHANGED (wxScintilla::OnSysColourChanged) EVT_ERASE_BACKGROUND (wxScintilla::OnEraseBackground) EVT_MENU_RANGE (10, 16, wxScintilla::OnMenu) EVT_LISTBOX_DCLICK (wxID_ANY, wxScintilla::OnListBox)END_EVENT_TABLE()IMPLEMENT_CLASS(wxScintilla, wxControl)IMPLEMENT_DYNAMIC_CLASS(wxScintillaEvent, wxCommandEvent)#ifdef LINK_LEXERS// forces the linking of the lexer modulesint Scintilla_LinkLexers();#endif//----------------------------------------------------------------------// Constructor and DestructorwxScintilla::wxScintilla (wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name) { m_swx = NULL; Create (parent, id, pos, size, style, name);}bool wxScintilla::Create (wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name) {#ifdef __WXMAC__ style |= wxVSCROLL | wxHSCROLL;#endif if (!wxControl::Create (parent, id, pos, size, style | wxWANTS_CHARS | wxCLIP_CHILDREN, wxDefaultValidator, name)) { return false; }#ifdef LINK_LEXERS Scintilla_LinkLexers();#endif m_swx = new ScintillaWX(this); m_stopWatch.Start(); m_lastKeyDownConsumed = FALSE; m_vScrollBar = NULL; m_hScrollBar = NULL;#if wxUSE_UNICODE // Put Scintilla into unicode (UTF-8) mode SetCodePage(wxSCI_CP_UTF8);#endif#if wxCHECK_VERSION(2, 5, 0) // Reduces flicker on GTK+/X11 SetBackgroundStyle(wxBG_STYLE_CUSTOM); SetInitialSize(size);#endif return false;}wxScintilla::~wxScintilla() { delete m_swx;}//----------------------------------------------------------------------// Send message to Scintillalong wxScintilla::SendMsg (int msg, long wp, long lp) { return m_swx->WndProc (msg, wp, lp);}//----------------------------------------------------------------------// Set the vertical scrollbar to use instead of the ont that's built-in.void wxScintilla::SetVScrollBar (wxScrollBar* bar) { m_vScrollBar = bar; if (bar != NULL) { // ensure that the built-in scrollbar is not visible SetScrollbar(wxVERTICAL, 0, 0, 0); }}// Set the horizontal scrollbar to use instead of the ont that's built-in.void wxScintilla::SetHScrollBar (wxScrollBar* bar) { m_hScrollBar = bar; if (bar != NULL) { // ensure that the built-in scrollbar is not visible SetScrollbar(wxHORIZONTAL, 0, 0, 0); }}//----------------------------------------------------------------------// BEGIN generated section. The following code is automatically generated// by gen_iface.py from the contents of Scintilla.iface. Do not edit// this file. Edit wxscintilla.cpp.in or gen_iface.py instead and regenerate.// Add text to the document at current position.void wxScintilla::AddText (const wxString& text) { wxWX2MBbuf buf = (wxWX2MBbuf)wx2sci(text); SendMsg (SCI_ADDTEXT, strlen(buf), (long)(const char*)buf);}// Add text to the document w/length parameter, this allows for binary data to be added.void wxScintilla::AddText (const int length, const wxString& text) { wxWX2MBbuf buf = (wxWX2MBbuf)wx2sci(text); SendMsg (SCI_ADDTEXT, length, (long)(const char*)buf);}// Add array of cells to document.void wxScintilla::AddStyledText (const wxMemoryBuffer& data) { SendMsg (SCI_ADDSTYLEDTEXT, data.GetDataLen(), (long)data.GetData());}// Insert string at a position.void wxScintilla::InsertText (int pos, const wxString& text) { SendMsg (SCI_INSERTTEXT, pos, (long)(const char*)wx2sci(text));}// Delete all text in the document.void wxScintilla::ClearAll() { SendMsg (SCI_CLEARALL, 0, 0);}// Set all style bytes to 0, remove all folding information.void wxScintilla::ClearDocumentStyle() { SendMsg (SCI_CLEARDOCUMENTSTYLE, 0, 0);}// Returns the number of characters in the document.int wxScintilla::GetLength() { return SendMsg (SCI_GETLENGTH, 0, 0);}// Returns the character byte at the position.int wxScintilla::GetCharAt (int pos) { return (unsigned char)SendMsg (SCI_GETCHARAT, pos, 0);}// Returns the position of the caret.int wxScintilla::GetCurrentPos() { return SendMsg (SCI_GETCURRENTPOS, 0, 0);}// Returns the position of the opposite end of the selection to the caret.int wxScintilla::GetAnchor() { return SendMsg (SCI_GETANCHOR, 0, 0);}// Returns the style byte at the position.int wxScintilla::GetStyleAt (int pos) { return (unsigned char)SendMsg (SCI_GETSTYLEAT, pos, 0);}// Redoes the next action on the undo history.void wxScintilla::Redo() { SendMsg (SCI_REDO, 0, 0);}// Choose between collecting actions into the undo// history and discarding them.void wxScintilla::SetUndoCollection (bool collectUndo) { SendMsg (SCI_SETUNDOCOLLECTION, collectUndo, 0);}// Select all the text in the document.void wxScintilla::SelectAll() { SendMsg (SCI_SELECTALL, 0, 0);}// Remember the current position in the undo history as the position// at which the document was saved.void wxScintilla::SetSavePoint() { SendMsg (SCI_SETSAVEPOINT, 0, 0);}// Retrieve a buffer of cells.wxMemoryBuffer wxScintilla::GetStyledText (int startPos, int endPos) { wxMemoryBuffer buf; if (endPos < startPos) { int temp = startPos; startPos = endPos; endPos = temp; } int len = endPos - startPos; if (!len) return buf; TextRange tr; tr.lpstrText = (char*)buf.GetWriteBuf(len*2+1); tr.chrg.cpMin = startPos; tr.chrg.cpMax = endPos; len = SendMsg (SCI_GETSTYLEDTEXT, 0, (long)&tr); buf.UngetWriteBuf(len); return buf;}// Are there any redoable actions in the undo history?bool wxScintilla::CanRedo() { return SendMsg (SCI_CANREDO, 0, 0) != 0;}// Retrieve the line number at which a particular marker is located.int wxScintilla::MarkerLineFromHandle (int handle) { return SendMsg (SCI_MARKERLINEFROMHANDLE, handle, 0);}// Delete a marker.void wxScintilla::MarkerDeleteHandle (int handle) { SendMsg (SCI_MARKERDELETEHANDLE, handle, 0);}// Is undo history being collected?bool wxScintilla::GetUndoCollection() { return SendMsg (SCI_GETUNDOCOLLECTION, 0, 0) != 0;}// Are white space characters currently visible?// Returns one of SCWS_* constants.int wxScintilla::GetViewWhiteSpace() { return SendMsg (SCI_GETVIEWWS, 0, 0);}// Make white space characters invisible, always visible or visible outside indentation.void wxScintilla::SetViewWhiteSpace (int viewWS) { SendMsg (SCI_SETVIEWWS, viewWS, 0);}// Find the position from a point within the window.int wxScintilla::PositionFromPoint (wxPoint pt) { return SendMsg (SCI_POSITIONFROMPOINT, pt.x, pt.y);}// Find the position from a point within the window but return// INVALID_POSITION if not close to text.int wxScintilla::PositionFromPointClose (int x, int y) { return SendMsg (SCI_POSITIONFROMPOINTCLOSE, x, y);}// Set caret to start of a line and ensure it is visible.void wxScintilla::GotoLine (int line) { SendMsg (SCI_GOTOLINE, line, 0);}// Set caret to a position and ensure it is visible.void wxScintilla::GotoPos (int pos) { SendMsg (SCI_GOTOPOS, pos, 0);}// Set the selection anchor to a position. The anchor is the opposite// end of the selection from the caret.void wxScintilla::SetAnchor (int posAnchor) { SendMsg (SCI_SETANCHOR, posAnchor, 0);}// Retrieve the text of the line containing the caret.// Returns the index of the caret on the line.wxString wxScintilla::GetCurLine (int* linePos) { int len = LineLength(GetCurrentLine()); if (!len) { if (linePos) *linePos = 0; return wxEmptyString; } wxMemoryBuffer mbuf(len+1); char* buf = (char*)mbuf.GetWriteBuf(len+1); int pos = SendMsg (SCI_GETCURLINE, len+1, (long)buf); mbuf.UngetWriteBuf(len); mbuf.AppendByte(0); if (linePos) *linePos = pos; return sci2wx(buf);}// Retrieve the position of the last correctly styled character.int wxScintilla::GetEndStyled() { return SendMsg (SCI_GETENDSTYLED, 0, 0);}// Convert all line endings in the document to one mode.void wxScintilla::ConvertEOLs (int eolMode) { SendMsg (SCI_CONVERTEOLS, eolMode, 0);}// Retrieve the current end of line mode - one of CRLF, CR, or LF.int wxScintilla::GetEOLMode() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -