X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c40691a0644cb0e08ec2c0cb7cda1c10b5a84e1b..4d623b67ee74794fbeba5805a269433d030c67d5:/samples/stc/edit.cpp diff --git a/samples/stc/edit.cpp b/samples/stc/edit.cpp index 7a166bbcdd..c220fa7faf 100644 --- a/samples/stc/edit.cpp +++ b/samples/stc/edit.cpp @@ -1,5 +1,5 @@ ////////////////////////////////////////////////////////////////////////////// -// File: edit.cpp +// File: contrib/samples/stc/edit.cpp // Purpose: STC test module // Maintainer: Wyo // Created: 2003-09-01 @@ -17,7 +17,7 @@ // headers //---------------------------------------------------------------------------- -// For compilers that support precompilation, includes . +// For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ @@ -28,6 +28,7 @@ // need because it includes almost all 'standard' wxWidgets headers) #ifndef WX_PRECOMP #include "wx/wx.h" + #include "wx/textdlg.h" #endif //! wxWidgets headers @@ -49,6 +50,8 @@ // declarations //============================================================================ +// The (uniform) style used for the annotations. +const int ANNOTATION_STYLE = wxSTC_STYLE_LASTPREDEFINED + 1; //============================================================================ // implementation @@ -93,6 +96,13 @@ BEGIN_EVENT_TABLE (Edit, wxStyledTextCtrl) EVT_MENU (myID_WRAPMODEON, Edit::OnWrapmodeOn) EVT_MENU (myID_CHARSETANSI, Edit::OnUseCharset) EVT_MENU (myID_CHARSETMAC, Edit::OnUseCharset) + // annotations + EVT_MENU (myID_ANNOTATION_ADD, Edit::OnAnnotationAdd) + EVT_MENU (myID_ANNOTATION_REMOVE, Edit::OnAnnotationRemove) + EVT_MENU (myID_ANNOTATION_CLEAR, Edit::OnAnnotationClear) + EVT_MENU (myID_ANNOTATION_STYLE_HIDDEN, Edit::OnAnnotationStyle) + EVT_MENU (myID_ANNOTATION_STYLE_STANDARD, Edit::OnAnnotationStyle) + EVT_MENU (myID_ANNOTATION_STYLE_BOXED, Edit::OnAnnotationStyle) // extra EVT_MENU (myID_CHANGELOWER, Edit::OnChangeCase) EVT_MENU (myID_CHANGEUPPER, Edit::OnChangeCase) @@ -102,6 +112,7 @@ BEGIN_EVENT_TABLE (Edit, wxStyledTextCtrl) // stc EVT_STC_MARGINCLICK (wxID_ANY, Edit::OnMarginClick) EVT_STC_CHARADDED (wxID_ANY, Edit::OnCharAdded) + EVT_STC_KEY( wxID_ANY , Edit::OnKey ) END_EVENT_TABLE() Edit::Edit (wxWindow *parent, wxWindowID id, @@ -119,6 +130,9 @@ Edit::Edit (wxWindow *parent, wxWindowID id, // initialize language m_language = NULL; + // Use all the bits in the style byte as styles, not indicators. + SetStyleBits(8); + // default font for all styles SetViewEOL (g_CommonPrefs.displayEOLEnable); SetIndentationGuides (g_CommonPrefs.indentGuideEnable); @@ -134,9 +148,9 @@ Edit::Edit (wxWindow *parent, wxWindowID id, StyleSetFont (wxSTC_STYLE_DEFAULT, font); StyleSetForeground (wxSTC_STYLE_DEFAULT, *wxBLACK); StyleSetBackground (wxSTC_STYLE_DEFAULT, *wxWHITE); - StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY"))); + StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (wxT("DARK GREY"))); StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE); - StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY"))); + StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColour (wxT("DARK GREY"))); InitializePrefs (DEFAULT_LANGUAGE); // set visibility @@ -145,16 +159,19 @@ Edit::Edit (wxWindow *parent, wxWindowID id, 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")); + MarkerDefine (wxSTC_MARKNUM_FOLDER, wxSTC_MARK_DOTDOTDOT, wxT("BLACK"), wxT("BLACK")); + MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, wxT("BLACK"), wxT("BLACK")); + MarkerDefine (wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK")); + MarkerDefine (wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_DOTDOTDOT, wxT("BLACK"), wxT("WHITE")); + MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_ARROWDOWN, wxT("BLACK"), wxT("WHITE")); + MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK")); + MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK")); + + // annotations + AnnotationSetVisible(wxSTC_ANNOTATION_BOXED); + + // miscellaneous + m_LineNrMargin = TextWidth (wxSTC_STYLE_LINENUMBER, wxT("_999999")); m_FoldingMargin = 16; CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key SetLayoutCache (wxSTC_CACHE_PAGE); @@ -189,6 +206,11 @@ void Edit::OnEditClear (wxCommandEvent &WXUNUSED(event)) { Clear (); } +void Edit::OnKey (wxStyledTextEvent &WXUNUSED(event)) +{ + wxMessageBox("OnKey"); +} + void Edit::OnEditCut (wxCommandEvent &WXUNUSED(event)) { if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return; Cut (); @@ -303,6 +325,79 @@ void Edit::OnUseCharset (wxCommandEvent &event) { SetCodePage (charset); } +void Edit::OnAnnotationAdd(wxCommandEvent& WXUNUSED(event)) +{ + const int line = GetCurrentLine(); + + wxString ann = AnnotationGetText(line); + ann = wxGetTextFromUser + ( + wxString::Format("Enter annotation for the line %d", line), + "Edit annotation", + ann, + this + ); + if ( ann.empty() ) + return; + + AnnotationSetText(line, ann); + AnnotationSetStyle(line, ANNOTATION_STYLE); + + // Scintilla doesn't update the scroll width for annotations, even with + // scroll width tracking on, so do it manually. + const int width = GetScrollWidth(); + + // NB: The following adjustments are only needed when using + // wxSTC_ANNOTATION_BOXED annotations style, but we apply them always + // in order to make things simpler and not have to redo the width + // calculations when the annotations visibility changes. In a real + // program you'd either just stick to a fixed annotations visibility or + // update the width when it changes. + + // Take into account the fact that the annotation is shown indented, with + // the same indent as the line it's attached to. + int indent = GetLineIndentation(line); + + // This is just a hack to account for the width of the box, there doesn't + // seem to be any way to get it directly from Scintilla. + indent += 3; + + const int widthAnn = TextWidth(ANNOTATION_STYLE, ann + wxString(indent, ' ')); + + if (widthAnn > width) + SetScrollWidth(widthAnn); +} + +void Edit::OnAnnotationRemove(wxCommandEvent& WXUNUSED(event)) +{ + AnnotationSetText(GetCurrentLine(), wxString()); +} + +void Edit::OnAnnotationClear(wxCommandEvent& WXUNUSED(event)) +{ + AnnotationClearAll(); +} + +void Edit::OnAnnotationStyle(wxCommandEvent& event) +{ + int style = 0; + switch (event.GetId()) { + case myID_ANNOTATION_STYLE_HIDDEN: + style = wxSTC_ANNOTATION_HIDDEN; + break; + + case myID_ANNOTATION_STYLE_STANDARD: + style = wxSTC_ANNOTATION_STANDARD; + break; + + case myID_ANNOTATION_STYLE_BOXED: + style = wxSTC_ANNOTATION_BOXED; + break; + } + + AnnotationSetVisible(style); +} + void Edit::OnChangeCase (wxCommandEvent &event) { switch (event.GetId()) { case myID_CHANGELOWER: { @@ -369,8 +464,8 @@ wxString Edit::DeterminePrefs (const wxString &filename) { while (!filepattern.empty()) { wxString cur = filepattern.BeforeFirst (';'); if ((cur == filename) || - (cur == (filename.BeforeLast ('.') + _T(".*"))) || - (cur == (_T("*.") + filename.AfterLast ('.')))) { + (cur == (filename.BeforeLast ('.') + wxT(".*"))) || + (cur == (wxT("*.") + filename.AfterLast ('.')))) { return curInfo->name; } filepattern = filepattern.AfterFirst (';'); @@ -404,10 +499,16 @@ bool Edit::InitializePrefs (const wxString &name) { // set margin for line numbers SetMarginType (m_LineNrID, wxSTC_MARGIN_NUMBER); - StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY"))); + StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (wxT("DARK GREY"))); StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE); SetMarginWidth (m_LineNrID, 0); // start out not visible + // annotations style + StyleSetBackground(ANNOTATION_STYLE, wxColour(244, 220, 220)); + StyleSetForeground(ANNOTATION_STYLE, *wxBLACK); + StyleSetSizeFractional(ANNOTATION_STYLE, + (StyleGetSizeFractional(wxSTC_STYLE_DEFAULT)*4)/5); + // default fonts for all styles! int Nr; for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) { @@ -416,8 +517,8 @@ bool Edit::InitializePrefs (const wxString &name) { } // set common styles - StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColour (_T("DARK GREY"))); - StyleSetForeground (wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY"))); + StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColour (wxT("DARK GREY"))); + StyleSetForeground (wxSTC_STYLE_INDENTGUIDE, wxColour (wxT("DARK GREY"))); // initialize settings if (g_CommonPrefs.syntaxEnable) { @@ -439,7 +540,7 @@ bool Edit::InitializePrefs (const wxString &name) { StyleSetUnderline (Nr, (curType.fontstyle & mySTC_STYLE_UNDERL) > 0); StyleSetVisible (Nr, (curType.fontstyle & mySTC_STYLE_HIDDEN) == 0); StyleSetCase (Nr, curType.lettercase); - const wxChar *pwords = curInfo->styles[Nr].words; + const char *pwords = curInfo->styles[Nr].words; if (pwords) { SetKeyWords (keywordnr, pwords); keywordnr += 1; @@ -461,21 +562,21 @@ bool Edit::InitializePrefs (const wxString &name) { if (g_CommonPrefs.foldEnable) { SetMarginWidth (m_FoldingID, curInfo->folds != 0? m_FoldingMargin: 0); SetMarginSensitive (m_FoldingID, curInfo->folds != 0); - SetProperty (_T("fold"), curInfo->folds != 0? _T("1"): _T("0")); - SetProperty (_T("fold.comment"), - (curInfo->folds & mySTC_FOLD_COMMENT) > 0? _T("1"): _T("0")); - SetProperty (_T("fold.compact"), - (curInfo->folds & mySTC_FOLD_COMPACT) > 0? _T("1"): _T("0")); - SetProperty (_T("fold.preprocessor"), - (curInfo->folds & mySTC_FOLD_PREPROC) > 0? _T("1"): _T("0")); - SetProperty (_T("fold.html"), - (curInfo->folds & mySTC_FOLD_HTML) > 0? _T("1"): _T("0")); - SetProperty (_T("fold.html.preprocessor"), - (curInfo->folds & mySTC_FOLD_HTMLPREP) > 0? _T("1"): _T("0")); - SetProperty (_T("fold.comment.python"), - (curInfo->folds & mySTC_FOLD_COMMENTPY) > 0? _T("1"): _T("0")); - SetProperty (_T("fold.quotes.python"), - (curInfo->folds & mySTC_FOLD_QUOTESPY) > 0? _T("1"): _T("0")); + SetProperty (wxT("fold"), curInfo->folds != 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.comment"), + (curInfo->folds & mySTC_FOLD_COMMENT) > 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.compact"), + (curInfo->folds & mySTC_FOLD_COMPACT) > 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.preprocessor"), + (curInfo->folds & mySTC_FOLD_PREPROC) > 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.html"), + (curInfo->folds & mySTC_FOLD_HTML) > 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.html.preprocessor"), + (curInfo->folds & mySTC_FOLD_HTMLPREP) > 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.comment.python"), + (curInfo->folds & mySTC_FOLD_COMMENTPY) > 0? wxT("1"): wxT("0")); + SetProperty (wxT("fold.quotes.python"), + (curInfo->folds & mySTC_FOLD_QUOTESPY) > 0? wxT("1"): wxT("0")); } SetFoldFlags (wxSTC_FOLDFLAG_LINEBEFORE_CONTRACTED | wxSTC_FOLDFLAG_LINEAFTER_CONTRACTED); @@ -507,8 +608,8 @@ bool Edit::LoadFile () #if wxUSE_FILEDLG // get filname if (!m_filename) { - wxFileDialog dlg (this, _T("Open file"), wxEmptyString, wxEmptyString, - _T("Any file (*)|*"), wxOPEN | wxFILE_MUST_EXIST | wxCHANGE_DIR); + wxFileDialog dlg (this, wxT("Open file"), wxEmptyString, wxEmptyString, + wxT("Any file (*)|*"), wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR); if (dlg.ShowModal() != wxID_OK) return false; m_filename = dlg.GetPath(); } @@ -524,18 +625,6 @@ bool Edit::LoadFile (const wxString &filename) { // load file in edit and clear undo if (!filename.empty()) m_filename = filename; -// wxFile file (m_filename); -// if (!file.IsOpened()) return false; - ClearAll (); -// long lng = file.Length (); -// if (lng > 0) { -// wxString buf; -// wxChar *buff = buf.GetWriteBuf (lng); -// file.Read (buff, lng); -// buf.UngetWriteBuf (); -// InsertText (0, buf); -// } -// file.Close(); wxStyledTextCtrl::LoadFile(m_filename); @@ -556,8 +645,8 @@ bool Edit::SaveFile () // get filname if (!m_filename) { - wxFileDialog dlg (this, _T("Save file"), wxEmptyString, wxEmptyString, _T("Any file (*)|*"), - wxSAVE | wxOVERWRITE_PROMPT); + wxFileDialog dlg (this, wxT("Save file"), wxEmptyString, wxEmptyString, wxT("Any file (*)|*"), + wxFD_SAVE | wxFD_OVERWRITE_PROMPT); if (dlg.ShowModal() != wxID_OK) return false; m_filename = dlg.GetPath(); } @@ -630,14 +719,14 @@ EditProperties::EditProperties (Edit *edit, textinfo->Add (new wxStaticText (this, wxID_ANY, _("Lexer-ID: "), wxDefaultPosition, wxSize(80, wxDefaultCoord)), 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4); - text = wxString::Format (_T("%d"), edit->GetLexer()); + text = wxString::Format (wxT("%d"), edit->GetLexer()); textinfo->Add (new wxStaticText (this, wxID_ANY, text), 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4); wxString EOLtype = wxEmptyString; switch (edit->GetEOLMode()) { - case wxSTC_EOL_CR: {EOLtype = _T("CR (Unix)"); break; } - case wxSTC_EOL_CRLF: {EOLtype = _T("CRLF (Windows)"); break; } - case wxSTC_EOL_LF: {EOLtype = _T("CR (Macintosh)"); break; } + case wxSTC_EOL_CR: {EOLtype = wxT("CR (Unix)"); break; } + case wxSTC_EOL_CRLF: {EOLtype = wxT("CRLF (Windows)"); break; } + case wxSTC_EOL_LF: {EOLtype = wxT("CR (Macintosh)"); break; } } textinfo->Add (new wxStaticText (this, wxID_ANY, _("Line endings"), wxDefaultPosition, wxSize(80, wxDefaultCoord)), @@ -657,25 +746,25 @@ EditProperties::EditProperties (Edit *edit, statistic->Add (new wxStaticText (this, wxID_ANY, _("Total lines"), wxDefaultPosition, wxSize(80, wxDefaultCoord)), 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4); - text = wxString::Format (_T("%d"), edit->GetLineCount()); + text = wxString::Format (wxT("%d"), edit->GetLineCount()); statistic->Add (new wxStaticText (this, wxID_ANY, text), 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4); statistic->Add (new wxStaticText (this, wxID_ANY, _("Total chars"), wxDefaultPosition, wxSize(80, wxDefaultCoord)), 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4); - text = wxString::Format (_T("%d"), edit->GetTextLength()); + text = wxString::Format (wxT("%d"), edit->GetTextLength()); statistic->Add (new wxStaticText (this, wxID_ANY, text), 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4); statistic->Add (new wxStaticText (this, wxID_ANY, _("Current line"), wxDefaultPosition, wxSize(80, wxDefaultCoord)), 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4); - text = wxString::Format (_T("%d"), edit->GetCurrentLine()); + text = wxString::Format (wxT("%d"), edit->GetCurrentLine()); statistic->Add (new wxStaticText (this, wxID_ANY, text), 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4); statistic->Add (new wxStaticText (this, wxID_ANY, _("Current pos"), wxDefaultPosition, wxSize(80, wxDefaultCoord)), 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4); - text = wxString::Format (_T("%d"), edit->GetCurrentPos()); + text = wxString::Format (wxT("%d"), edit->GetCurrentPos()); statistic->Add (new wxStaticText (this, wxID_ANY, text), 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4); @@ -709,7 +798,7 @@ EditProperties::EditProperties (Edit *edit, // EditPrint //---------------------------------------------------------------------------- -EditPrint::EditPrint (Edit *edit, wxChar *title) +EditPrint::EditPrint (Edit *edit, const wxChar *title) : wxPrintout(title) { m_edit = edit; m_printed = 0;