📄 edit.cpp
字号:
//////////////////////////////////////////////////////////////////////////////
// File: contrib/samples/stc/edit.cpp
// Purpose: STC test module
// Maintainer: Wyo
// Created: 2003-09-01
// RCS-ID: $Id: edit.cpp,v 1.12 2006/05/28 23:31:30 VZ Exp $
// Copyright: (c) wxGuide
// Licence: wxWindows licence
//////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
// informations
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// headers
//----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all 'standard' wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
//! wxWidgets headers
#include "wx/file.h" // raw file io support
#include "wx/filename.h" // filename support
//! application headers
#include "defsext.h" // additional definitions
#include "edit.h" // edit module
//----------------------------------------------------------------------------
// resources
//----------------------------------------------------------------------------
//============================================================================
// declarations
//============================================================================
//============================================================================
// implementation
//============================================================================
//----------------------------------------------------------------------------
// Edit
//----------------------------------------------------------------------------
BEGIN_EVENT_TABLE (Edit, wxStyledTextCtrl)
// common
EVT_SIZE ( Edit::OnSize)
// edit
EVT_MENU (wxID_CLEAR, Edit::OnEditClear)
EVT_MENU (wxID_CUT, Edit::OnEditCut)
EVT_MENU (wxID_COPY, Edit::OnEditCopy)
EVT_MENU (wxID_PASTE, Edit::OnEditPaste)
EVT_MENU (myID_INDENTINC, Edit::OnEditIndentInc)
EVT_MENU (myID_INDENTRED, Edit::OnEditIndentRed)
EVT_MENU (wxID_SELECTALL, Edit::OnEditSelectAll)
EVT_MENU (myID_SELECTLINE, Edit::OnEditSelectLine)
EVT_MENU (wxID_REDO, Edit::OnEditRedo)
EVT_MENU (wxID_UNDO, Edit::OnEditUndo)
// find
EVT_MENU (wxID_FIND, Edit::OnFind)
EVT_MENU (myID_FINDNEXT, Edit::OnFindNext)
EVT_MENU (myID_REPLACE, Edit::OnReplace)
EVT_MENU (myID_REPLACENEXT, Edit::OnReplaceNext)
EVT_MENU (myID_BRACEMATCH, Edit::OnBraceMatch)
EVT_MENU (myID_GOTO, Edit::OnGoto)
// view
EVT_MENU_RANGE (myID_HILIGHTFIRST, myID_HILIGHTLAST,
Edit::OnHilightLang)
EVT_MENU (myID_DISPLAYEOL, Edit::OnDisplayEOL)
EVT_MENU (myID_INDENTGUIDE, Edit::OnIndentGuide)
EVT_MENU (myID_LINENUMBER, Edit::OnLineNumber)
EVT_MENU (myID_LONGLINEON, Edit::OnLongLineOn)
EVT_MENU (myID_WHITESPACE, Edit::OnWhiteSpace)
EVT_MENU (myID_FOLDTOGGLE, Edit::OnFoldToggle)
EVT_MENU (myID_OVERTYPE, Edit::OnSetOverType)
EVT_MENU (myID_READONLY, Edit::OnSetReadOnly)
EVT_MENU (myID_WRAPMODEON, Edit::OnWrapmodeOn)
EVT_MENU (myID_CHARSETANSI, Edit::OnUseCharset)
EVT_MENU (myID_CHARSETMAC, Edit::OnUseCharset)
// extra
EVT_MENU (myID_CHANGELOWER, Edit::OnChangeCase)
EVT_MENU (myID_CHANGEUPPER, Edit::OnChangeCase)
EVT_MENU (myID_CONVERTCR, Edit::OnConvertEOL)
EVT_MENU (myID_CONVERTCRLF, Edit::OnConvertEOL)
EVT_MENU (myID_CONVERTLF, Edit::OnConvertEOL)
// stc
EVT_STC_MARGINCLICK (wxID_ANY, Edit::OnMarginClick)
EVT_STC_CHARADDED (wxID_ANY, Edit::OnCharAdded)
END_EVENT_TABLE()
Edit::Edit (wxWindow *parent, wxWindowID id,
const wxPoint &pos,
const wxSize &size,
long style)
: wxStyledTextCtrl (parent, id, pos, size, style) {
m_filename = wxEmptyString;
m_LineNrID = 0;
m_DividerID = 1;
m_FoldingID = 2;
// initialize language
m_language = NULL;
// default font for all styles
SetViewEOL (g_CommonPrefs.displayEOLEnable);
SetIndentationGuides (g_CommonPrefs.indentGuideEnable);
SetEdgeMode (g_CommonPrefs.longLineOnEnable?
wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
SetViewWhiteSpace (g_CommonPrefs.whiteSpaceEnable?
wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
SetOvertype (g_CommonPrefs.overTypeInitial);
SetReadOnly (g_CommonPrefs.readOnlyInitial);
SetWrapMode (g_CommonPrefs.wrapModeInitial?
wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
wxFont font (10, wxMODERN, wxNORMAL, wxNORMAL);
StyleSetFont (wxSTC_STYLE_DEFAULT, font);
StyleSetForeground (wxSTC_STYLE_DEFAULT, *wxBLACK);
StyleSetBackground (wxSTC_STYLE_DEFAULT, *wxWHITE);
StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY")));
StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY")));
InitializePrefs (DEFAULT_LANGUAGE);
// set visibility
SetVisiblePolicy (wxSTC_VISIBLE_STRICT|wxSTC_VISIBLE_SLOP, 1);
SetXCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
SetYCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
// markers
MarkerDefine (wxSTC_MARKNUM_FOLDER, wxSTC_MARK_DOTDOTDOT, _T("BLACK"), _T("BLACK"));
MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, _T("BLACK"), _T("BLACK"));
MarkerDefine (wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, _T("BLACK"), _T("BLACK"));
MarkerDefine (wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_DOTDOTDOT, _T("BLACK"), _T("WHITE"));
MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_ARROWDOWN, _T("BLACK"), _T("WHITE"));
MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, _T("BLACK"), _T("BLACK"));
MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, _T("BLACK"), _T("BLACK"));
// miscelaneous
m_LineNrMargin = TextWidth (wxSTC_STYLE_LINENUMBER, _T("_999999"));
m_FoldingMargin = 16;
CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key
SetLayoutCache (wxSTC_CACHE_PAGE);
}
Edit::~Edit () {}
//----------------------------------------------------------------------------
// common event handlers
void Edit::OnSize( wxSizeEvent& event ) {
int x = GetClientSize().x +
(g_CommonPrefs.lineNumberEnable? m_LineNrMargin: 0) +
(g_CommonPrefs.foldEnable? m_FoldingMargin: 0);
if (x > 0) SetScrollWidth (x);
event.Skip();
}
// edit event handlers
void Edit::OnEditRedo (wxCommandEvent &WXUNUSED(event)) {
if (!CanRedo()) return;
Redo ();
}
void Edit::OnEditUndo (wxCommandEvent &WXUNUSED(event)) {
if (!CanUndo()) return;
Undo ();
}
void Edit::OnEditClear (wxCommandEvent &WXUNUSED(event)) {
if (GetReadOnly()) return;
Clear ();
}
void Edit::OnEditCut (wxCommandEvent &WXUNUSED(event)) {
if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return;
Cut ();
}
void Edit::OnEditCopy (wxCommandEvent &WXUNUSED(event)) {
if (GetSelectionEnd()-GetSelectionStart() <= 0) return;
Copy ();
}
void Edit::OnEditPaste (wxCommandEvent &WXUNUSED(event)) {
if (!CanPaste()) return;
Paste ();
}
void Edit::OnFind (wxCommandEvent &WXUNUSED(event)) {
}
void Edit::OnFindNext (wxCommandEvent &WXUNUSED(event)) {
}
void Edit::OnReplace (wxCommandEvent &WXUNUSED(event)) {
}
void Edit::OnReplaceNext (wxCommandEvent &WXUNUSED(event)) {
}
void Edit::OnBraceMatch (wxCommandEvent &WXUNUSED(event)) {
int min = GetCurrentPos ();
int max = BraceMatch (min);
if (max > (min+1)) {
BraceHighlight (min+1, max);
SetSelection (min+1, max);
}else{
BraceBadLight (min);
}
}
void Edit::OnGoto (wxCommandEvent &WXUNUSED(event)) {
}
void Edit::OnEditIndentInc (wxCommandEvent &WXUNUSED(event)) {
CmdKeyExecute (wxSTC_CMD_TAB);
}
void Edit::OnEditIndentRed (wxCommandEvent &WXUNUSED(event)) {
CmdKeyExecute (wxSTC_CMD_DELETEBACK);
}
void Edit::OnEditSelectAll (wxCommandEvent &WXUNUSED(event)) {
SetSelection (0, GetTextLength ());
}
void Edit::OnEditSelectLine (wxCommandEvent &WXUNUSED(event)) {
int lineStart = PositionFromLine (GetCurrentLine());
int lineEnd = PositionFromLine (GetCurrentLine() + 1);
SetSelection (lineStart, lineEnd);
}
void Edit::OnHilightLang (wxCommandEvent &event) {
InitializePrefs (g_LanguagePrefs [event.GetId() - myID_HILIGHTFIRST].name);
}
void Edit::OnDisplayEOL (wxCommandEvent &WXUNUSED(event)) {
SetViewEOL (!GetViewEOL());
}
void Edit::OnIndentGuide (wxCommandEvent &WXUNUSED(event)) {
SetIndentationGuides (!GetIndentationGuides());
}
void Edit::OnLineNumber (wxCommandEvent &WXUNUSED(event)) {
SetMarginWidth (m_LineNrID,
GetMarginWidth (m_LineNrID) == 0? m_LineNrMargin: 0);
}
void Edit::OnLongLineOn (wxCommandEvent &WXUNUSED(event)) {
SetEdgeMode (GetEdgeMode() == 0? wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
}
void Edit::OnWhiteSpace (wxCommandEvent &WXUNUSED(event)) {
SetViewWhiteSpace (GetViewWhiteSpace() == 0?
wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
}
void Edit::OnFoldToggle (wxCommandEvent &WXUNUSED(event)) {
ToggleFold (GetFoldParent(GetCurrentLine()));
}
void Edit::OnSetOverType (wxCommandEvent &WXUNUSED(event)) {
SetOvertype (!GetOvertype());
}
void Edit::OnSetReadOnly (wxCommandEvent &WXUNUSED(event)) {
SetReadOnly (!GetReadOnly());
}
void Edit::OnWrapmodeOn (wxCommandEvent &WXUNUSED(event)) {
SetWrapMode (GetWrapMode() == 0? wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
}
void Edit::OnUseCharset (wxCommandEvent &event) {
int Nr;
int charset = GetCodePage();
switch (event.GetId()) {
case myID_CHARSETANSI: {charset = wxSTC_CHARSET_ANSI; break;}
case myID_CHARSETMAC: {charset = wxSTC_CHARSET_ANSI; break;}
}
for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
StyleSetCharacterSet (Nr, charset);
}
SetCodePage (charset);
}
void Edit::OnChangeCase (wxCommandEvent &event) {
switch (event.GetId()) {
case myID_CHANGELOWER: {
CmdKeyExecute (wxSTC_CMD_LOWERCASE);
break;
}
case myID_CHANGEUPPER: {
CmdKeyExecute (wxSTC_CMD_UPPERCASE);
break;
}
}
}
void Edit::OnConvertEOL (wxCommandEvent &event) {
int eolMode = GetEOLMode();
switch (event.GetId()) {
case myID_CONVERTCR: { eolMode = wxSTC_EOL_CR; break;}
case myID_CONVERTCRLF: { eolMode = wxSTC_EOL_CRLF; break;}
case myID_CONVERTLF: { eolMode = wxSTC_EOL_LF; break;}
}
ConvertEOLs (eolMode);
SetEOLMode (eolMode);
}
//! misc
void Edit::OnMarginClick (wxStyledTextEvent &event) {
if (event.GetMargin() == 2) {
int lineClick = LineFromPosition (event.GetPosition());
int levelClick = GetFoldLevel (lineClick);
if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) {
ToggleFold (lineClick);
}
}
}
void Edit::OnCharAdded (wxStyledTextEvent &event) {
char chr = (char)event.GetKey();
int currentLine = GetCurrentLine();
// Change this if support for mac files with \r is needed
if (chr == '\n') {
int lineInd = 0;
if (currentLine > 0) {
lineInd = GetLineIndentation(currentLine - 1);
}
if (lineInd == 0) return;
SetLineIndentation (currentLine, lineInd);
GotoPos(PositionFromLine (currentLine) + lineInd);
}
}
//----------------------------------------------------------------------------
// private functions
wxString Edit::DeterminePrefs (const wxString &filename) {
LanguageInfo const* curInfo;
// determine language from filepatterns
int languageNr;
for (languageNr = 0; languageNr < g_LanguagePrefsSize; languageNr++) {
curInfo = &g_LanguagePrefs [languageNr];
wxString filepattern = curInfo->filepattern;
filepattern.Lower();
while (!filepattern.empty()) {
wxString cur = filepattern.BeforeFirst (';');
if ((cur == filename) ||
(cur == (filename.BeforeLast ('.') + _T(".*"))) ||
(cur == (_T("*.") + filename.AfterLast ('.')))) {
return curInfo->name;
}
filepattern = filepattern.AfterFirst (';');
}
}
return wxEmptyString;
}
bool Edit::InitializePrefs (const wxString &name) {
// initialize styles
StyleClearAll();
LanguageInfo const* curInfo = NULL;
// determine language
bool found = false;
int languageNr;
for (languageNr = 0; languageNr < g_LanguagePrefsSize; languageNr++) {
curInfo = &g_LanguagePrefs [languageNr];
if (curInfo->name == name) {
found = true;
break;
}
}
if (!found) return false;
// set lexer and language
SetLexer (curInfo->lexer);
m_language = curInfo;
// set margin for line numbers
SetMarginType (m_LineNrID, wxSTC_MARGIN_NUMBER);
StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY")));
StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
SetMarginWidth (m_LineNrID, 0); // start out not visible
// default fonts for all styles!
int Nr;
for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
wxFont font (10, wxMODERN, wxNORMAL, wxNORMAL);
StyleSetFont (Nr, font);
}
// set common styles
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -