📄 stc.cpp
字号:
////////////////////////////////////////////////////////////////////////////// Name: stc.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: stc.cpp,v 1.104 2006/10/31 08:50:15 RD Exp $// Copyright: (c) 2000 by Total Control Software// Licence: wxWindows license/////////////////////////////////////////////////////////////////////////////#include <ctype.h>#include "wx/wx.h"#include "wx/tokenzr.h"#include "wx/mstream.h"#include "wx/image.h"#include "wx/file.h"#include "wx/stc/stc.h"#include "ScintillaWX.h"//----------------------------------------------------------------------const wxChar* wxSTCNameStr = wxT("stcwindow");#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) == wxT('#')) { 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_STC_CHANGE )DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED )DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED )DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED )DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT )DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT )DEFINE_EVENT_TYPE( wxEVT_STC_KEY )DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK )DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI )DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED )DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD )DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK )DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN )DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED )DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION )DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED )DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART )DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND )DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG )DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER )DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP )DEFINE_EVENT_TYPE( wxEVT_STC_ZOOM )DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_CLICK )DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_DCLICK )DEFINE_EVENT_TYPE( wxEVT_STC_CALLTIP_CLICK )DEFINE_EVENT_TYPE( wxEVT_STC_AUTOCOMP_SELECTION ) BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl) EVT_PAINT (wxStyledTextCtrl::OnPaint) EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin) EVT_SCROLL (wxStyledTextCtrl::OnScroll) EVT_SIZE (wxStyledTextCtrl::OnSize) EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown) // Let Scintilla see the double click as a second click EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown) EVT_MOTION (wxStyledTextCtrl::OnMouseMove) EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)#if defined(__WXGTK__) || defined(__WXMAC__) EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp)#else EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu)#endif EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel) EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp) EVT_CHAR (wxStyledTextCtrl::OnChar) EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown) EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus) EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus) EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged) EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground) EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu) EVT_LISTBOX_DCLICK (wxID_ANY, wxStyledTextCtrl::OnListBox)END_EVENT_TABLE()IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)#ifdef LINK_LEXERS// forces the linking of the lexer modulesint Scintilla_LinkLexers();#endif//----------------------------------------------------------------------// Constructor and DestructorwxStyledTextCtrl::wxStyledTextCtrl(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 wxStyledTextCtrl::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(wxSTC_CP_UTF8);#endif SetInitialSize(size); // Reduces flicker on GTK+/X11 SetBackgroundStyle(wxBG_STYLE_CUSTOM); return true;}wxStyledTextCtrl::~wxStyledTextCtrl() { delete m_swx;}//----------------------------------------------------------------------long wxStyledTextCtrl::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 wxStyledTextCtrl::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 wxStyledTextCtrl::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 stc.cpp.in or gen_iface.py instead and regenerate.// Add text to the document at current position.void wxStyledTextCtrl::AddText(const wxString& text) { wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text); SendMsg(2001, strlen(buf), (long)(const char*)buf);}// Add array of cells to document.void wxStyledTextCtrl::AddStyledText(const wxMemoryBuffer& data) { SendMsg(2002, data.GetDataLen(), (long)data.GetData());}// Insert string at a position.void wxStyledTextCtrl::InsertText(int pos, const wxString& text) { SendMsg(2003, pos, (long)(const char*)wx2stc(text));}// Delete all text in the document.void wxStyledTextCtrl::ClearAll() { SendMsg(2004, 0, 0);}// Set all style bytes to 0, remove all folding information.void wxStyledTextCtrl::ClearDocumentStyle() { SendMsg(2005, 0, 0);}// Returns the number of characters in the document.int wxStyledTextCtrl::GetLength() { return SendMsg(2006, 0, 0);}// Returns the character byte at the position.int wxStyledTextCtrl::GetCharAt(int pos) { return (unsigned char)SendMsg(2007, pos, 0);}// Returns the position of the caret.int wxStyledTextCtrl::GetCurrentPos() { return SendMsg(2008, 0, 0);}// Returns the position of the opposite end of the selection to the caret.int wxStyledTextCtrl::GetAnchor() { return SendMsg(2009, 0, 0);}// Returns the style byte at the position.int wxStyledTextCtrl::GetStyleAt(int pos) { return (unsigned char)SendMsg(2010, pos, 0);}// Redoes the next action on the undo history.void wxStyledTextCtrl::Redo() { SendMsg(2011, 0, 0);}// Choose between collecting actions into the undo// history and discarding them.void wxStyledTextCtrl::SetUndoCollection(bool collectUndo) { SendMsg(2012, collectUndo, 0);}// Select all the text in the document.void wxStyledTextCtrl::SelectAll() { SendMsg(2013, 0, 0);}// Remember the current position in the undo history as the position// at which the document was saved.void wxStyledTextCtrl::SetSavePoint() { SendMsg(2014, 0, 0);}// Retrieve a buffer of cells.wxMemoryBuffer wxStyledTextCtrl::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(2015, 0, (long)&tr); buf.UngetWriteBuf(len); return buf;}// Are there any redoable actions in the undo history?bool wxStyledTextCtrl::CanRedo() { return SendMsg(2016, 0, 0) != 0;}// Retrieve the line number at which a particular marker is located.int wxStyledTextCtrl::MarkerLineFromHandle(int handle) { return SendMsg(2017, handle, 0);}// Delete a marker.void wxStyledTextCtrl::MarkerDeleteHandle(int handle) { SendMsg(2018, handle, 0);}// Is undo history being collected?bool wxStyledTextCtrl::GetUndoCollection() { return SendMsg(2019, 0, 0) != 0;}// Are white space characters currently visible?// Returns one of SCWS_* constants.int wxStyledTextCtrl::GetViewWhiteSpace() { return SendMsg(2020, 0, 0);}// Make white space characters invisible, always visible or visible outside indentation.void wxStyledTextCtrl::SetViewWhiteSpace(int viewWS) { SendMsg(2021, viewWS, 0);}// Find the position from a point within the window.int wxStyledTextCtrl::PositionFromPoint(wxPoint pt) { return SendMsg(2022, pt.x, pt.y);}// Find the position from a point within the window but return// INVALID_POSITION if not close to text.int wxStyledTextCtrl::PositionFromPointClose(int x, int y) { return SendMsg(2023, x, y);}// Set caret to start of a line and ensure it is visible.void wxStyledTextCtrl::GotoLine(int line) { SendMsg(2024, line, 0);}// Set caret to a position and ensure it is visible.void wxStyledTextCtrl::GotoPos(int pos) { SendMsg(2025, pos, 0);}// Set the selection anchor to a position. The anchor is the opposite// end of the selection from the caret.void wxStyledTextCtrl::SetAnchor(int posAnchor) { SendMsg(2026, posAnchor, 0);}// Retrieve the text of the line containing the caret.// Returns the index of the caret on the line.wxString wxStyledTextCtrl::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(2027, len+1, (long)buf); mbuf.UngetWriteBuf(len); mbuf.AppendByte(0); if (linePos) *linePos = pos; return stc2wx(buf);}// Retrieve the position of the last correctly styled character.int wxStyledTextCtrl::GetEndStyled() { return SendMsg(2028, 0, 0);}// Convert all line endings in the document to one mode.void wxStyledTextCtrl::ConvertEOLs(int eolMode) { SendMsg(2029, eolMode, 0);}// Retrieve the current end of line mode - one of CRLF, CR, or LF.int wxStyledTextCtrl::GetEOLMode() { return SendMsg(2030, 0, 0);}// Set the current end of line mode.void wxStyledTextCtrl::SetEOLMode(int eolMode) { SendMsg(2031, eolMode, 0);}// Set the current styling position to pos and the styling mask to mask.// The styling mask can be used to protect some bits in each styling byte from modification.void wxStyledTextCtrl::StartStyling(int pos, int mask) { SendMsg(2032, pos, mask);}// Change style from current styling position for length characters to a style// and move the current styling position to after this newly styled segment.void wxStyledTextCtrl::SetStyling(int length, int style) { SendMsg(2033, length, style);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -