Add simple tests for Scintilla annotations to the stc sample.

Show how add, remove and clear annotations and also how to update the text
width when long annotations are added.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73463 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2013-02-05 20:46:16 +00:00
parent 10d28c972b
commit 2fb4e3cb0a
4 changed files with 121 additions and 3 deletions

View File

@@ -58,6 +58,12 @@ enum {
myID_OVERTYPE, myID_OVERTYPE,
myID_READONLY, myID_READONLY,
myID_WRAPMODEON, myID_WRAPMODEON,
myID_ANNOTATION_ADD,
myID_ANNOTATION_REMOVE,
myID_ANNOTATION_CLEAR,
myID_ANNOTATION_STYLE_HIDDEN,
myID_ANNOTATION_STYLE_STANDARD,
myID_ANNOTATION_STYLE_BOXED,
myID_CHANGECASE, myID_CHANGECASE,
myID_CHANGELOWER, myID_CHANGELOWER,
myID_CHANGEUPPER, myID_CHANGEUPPER,

View File

@@ -28,6 +28,7 @@
// need because it includes almost all 'standard' wxWidgets headers) // need because it includes almost all 'standard' wxWidgets headers)
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/wx.h" #include "wx/wx.h"
#include "wx/textdlg.h"
#endif #endif
//! wxWidgets headers //! wxWidgets headers
@@ -49,6 +50,8 @@
// declarations // declarations
//============================================================================ //============================================================================
// The (uniform) style used for the annotations.
const int ANNOTATION_STYLE = wxSTC_STYLE_LASTPREDEFINED + 1;
//============================================================================ //============================================================================
// implementation // implementation
@@ -93,6 +96,13 @@ BEGIN_EVENT_TABLE (Edit, wxStyledTextCtrl)
EVT_MENU (myID_WRAPMODEON, Edit::OnWrapmodeOn) EVT_MENU (myID_WRAPMODEON, Edit::OnWrapmodeOn)
EVT_MENU (myID_CHARSETANSI, Edit::OnUseCharset) EVT_MENU (myID_CHARSETANSI, Edit::OnUseCharset)
EVT_MENU (myID_CHARSETMAC, 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 // extra
EVT_MENU (myID_CHANGELOWER, Edit::OnChangeCase) EVT_MENU (myID_CHANGELOWER, Edit::OnChangeCase)
EVT_MENU (myID_CHANGEUPPER, Edit::OnChangeCase) EVT_MENU (myID_CHANGEUPPER, Edit::OnChangeCase)
@@ -157,7 +167,10 @@ Edit::Edit (wxWindow *parent, wxWindowID id,
MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK")); MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK"));
MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK")); MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, wxT("BLACK"), wxT("BLACK"));
// miscelaneous // annotations
AnnotationSetVisible(wxSTC_ANNOTATION_BOXED);
// miscellaneous
m_LineNrMargin = TextWidth (wxSTC_STYLE_LINENUMBER, wxT("_999999")); m_LineNrMargin = TextWidth (wxSTC_STYLE_LINENUMBER, wxT("_999999"));
m_FoldingMargin = 16; m_FoldingMargin = 16;
CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key
@@ -312,6 +325,79 @@ void Edit::OnUseCharset (wxCommandEvent &event) {
SetCodePage (charset); 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) { void Edit::OnChangeCase (wxCommandEvent &event) {
switch (event.GetId()) { switch (event.GetId()) {
case myID_CHANGELOWER: { case myID_CHANGELOWER: {
@@ -417,6 +503,12 @@ bool Edit::InitializePrefs (const wxString &name) {
StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE); StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
SetMarginWidth (m_LineNrID, 0); // start out not visible 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! // default fonts for all styles!
int Nr; int Nr;
for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) { for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {

View File

@@ -91,6 +91,11 @@ public:
void OnSetReadOnly (wxCommandEvent &event); void OnSetReadOnly (wxCommandEvent &event);
void OnWrapmodeOn (wxCommandEvent &event); void OnWrapmodeOn (wxCommandEvent &event);
void OnUseCharset (wxCommandEvent &event); void OnUseCharset (wxCommandEvent &event);
// annotations
void OnAnnotationAdd(wxCommandEvent& event);
void OnAnnotationRemove(wxCommandEvent& event);
void OnAnnotationClear(wxCommandEvent& event);
void OnAnnotationStyle(wxCommandEvent& event);
//! extra //! extra
void OnChangeCase (wxCommandEvent &event); void OnChangeCase (wxCommandEvent &event);
void OnConvertEOL (wxCommandEvent &event); void OnConvertEOL (wxCommandEvent &event);

View File

@@ -268,8 +268,6 @@ BEGIN_EVENT_TABLE (AppFrame, wxFrame)
EVT_MENU (wxID_CUT, AppFrame::OnEdit) EVT_MENU (wxID_CUT, AppFrame::OnEdit)
EVT_MENU (wxID_COPY, AppFrame::OnEdit) EVT_MENU (wxID_COPY, AppFrame::OnEdit)
EVT_MENU (wxID_PASTE, AppFrame::OnEdit) EVT_MENU (wxID_PASTE, AppFrame::OnEdit)
EVT_MENU (myID_INDENTINC, AppFrame::OnEdit)
EVT_MENU (myID_INDENTRED, AppFrame::OnEdit)
EVT_MENU (wxID_SELECTALL, AppFrame::OnEdit) EVT_MENU (wxID_SELECTALL, AppFrame::OnEdit)
EVT_MENU (wxID_REDO, AppFrame::OnEdit) EVT_MENU (wxID_REDO, AppFrame::OnEdit)
EVT_MENU (wxID_UNDO, AppFrame::OnEdit) EVT_MENU (wxID_UNDO, AppFrame::OnEdit)
@@ -517,6 +515,20 @@ void AppFrame::CreateMenu ()
menuView->AppendSeparator(); menuView->AppendSeparator();
menuView->Append (myID_USECHARSET, _("Use &code page of .."), menuCharset); menuView->Append (myID_USECHARSET, _("Use &code page of .."), menuCharset);
// Annotations menu
wxMenu* menuAnnotations = new wxMenu;
menuAnnotations->Append(myID_ANNOTATION_ADD, _("&Add or edit an annotation..."),
_("Add an annotation for the current line"));
menuAnnotations->Append(myID_ANNOTATION_REMOVE, _("&Remove annotation"),
_("Remove the annotation for the current line"));
menuAnnotations->Append(myID_ANNOTATION_CLEAR, _("&Clear all annotations"));
wxMenu* menuAnnotationsStyle = new wxMenu;
menuAnnotationsStyle->AppendRadioItem(myID_ANNOTATION_STYLE_HIDDEN, _("&Hidden"));
menuAnnotationsStyle->AppendRadioItem(myID_ANNOTATION_STYLE_STANDARD, _("&Standard"));
menuAnnotationsStyle->AppendRadioItem(myID_ANNOTATION_STYLE_BOXED, _("&Boxed"));
menuAnnotations->AppendSubMenu(menuAnnotationsStyle, "&Style");
// change case submenu // change case submenu
wxMenu *menuChangeCase = new wxMenu; wxMenu *menuChangeCase = new wxMenu;
menuChangeCase->Append (myID_CHANGEUPPER, _("&Upper case")); menuChangeCase->Append (myID_CHANGEUPPER, _("&Upper case"));
@@ -550,10 +562,13 @@ void AppFrame::CreateMenu ()
m_menuBar->Append (menuFile, _("&File")); m_menuBar->Append (menuFile, _("&File"));
m_menuBar->Append (menuEdit, _("&Edit")); m_menuBar->Append (menuEdit, _("&Edit"));
m_menuBar->Append (menuView, _("&View")); m_menuBar->Append (menuView, _("&View"));
m_menuBar->Append (menuAnnotations, _("&Annotations"));
m_menuBar->Append (menuExtra, _("E&xtra")); m_menuBar->Append (menuExtra, _("E&xtra"));
m_menuBar->Append (menuWindow, _("&Window")); m_menuBar->Append (menuWindow, _("&Window"));
m_menuBar->Append (menuHelp, _("&Help")); m_menuBar->Append (menuHelp, _("&Help"));
SetMenuBar (m_menuBar); SetMenuBar (m_menuBar);
m_menuBar->Check(myID_ANNOTATION_STYLE_BOXED, true);
} }
void AppFrame::FileOpen (wxString fname) void AppFrame::FileOpen (wxString fname)