Made toggling comment work for file types other than XML

This commit is contained in:
Zane U. Ji 2014-05-03 23:11:24 +08:00
parent 19b7a74641
commit a3cfe9b55f
5 changed files with 504 additions and 424 deletions

File diff suppressed because it is too large Load Diff

View File

@ -193,6 +193,7 @@ BEGIN_EVENT_TABLE ( MyFrame, wxFrame )
EVT_UPDATE_UI ( wxID_CUT, MyFrame::OnUpdateCutCopy )
EVT_UPDATE_UI ( wxID_COPY, MyFrame::OnUpdateCutCopy )
EVT_UPDATE_UI ( ID_FIND_AGAIN, MyFrame::OnUpdateFindAgain )
EVT_UPDATE_UI ( ID_TOGGLE_COMMENT, MyFrame::OnUpdateToggleComment )
EVT_UPDATE_UI_RANGE ( ID_FIND, ID_EXPORT_MSWORD, MyFrame::OnUpdateDocRange )
EVT_UPDATE_UI ( ID_PREVIOUS_DOCUMENT, MyFrame::OnUpdatePreviousDocument )
EVT_UPDATE_UI ( ID_NEXT_DOCUMENT, MyFrame::OnUpdateNextDocument )
@ -3699,6 +3700,20 @@ void MyFrame::OnUpdatePaste ( wxUpdateUIEvent& event )
event.Enable ( getActiveDocument() != NULL );
}
void MyFrame::OnUpdateToggleComment ( wxUpdateUIEvent& event )
{
XmlDoc *doc = getActiveDocument();
if ( !doc )
{
event.Enable ( false );
return;
}
int from = doc->GetSelectionStart();
int to = doc->GetSelectionEnd();
event.Enable ( from != to || doc->getType() == FILE_TYPE_XML );
}
void MyFrame::OnUpdatePreviousDocument ( wxUpdateUIEvent& event )
{
if ( !getActiveDocument() )

View File

@ -288,6 +288,7 @@ class MyFrame : public wxFrame
void OnUpdateCutCopy ( wxUpdateUIEvent& event );
void OnUpdateFindAgain ( wxUpdateUIEvent& event );
void OnUpdatePaste ( wxUpdateUIEvent& event );
void OnUpdateToggleComment ( wxUpdateUIEvent& event );
void OnUpdateDocRange ( wxUpdateUIEvent& event );
void OnUpdateReplaceRange ( wxUpdateUIEvent& event );
void OnUpdateReload ( wxUpdateUIEvent& event );

View File

@ -2215,53 +2215,96 @@ void XmlCtrl::OnMiddleDown ( wxMouseEvent& event )
Paste();
}
bool XmlCtrl::selectCurrentElement()
{
if ( type != FILE_TYPE_XML )
return false;
Colourise ( 0, -1 );
int pos = GetCurrentPos();
int style = getLexerStyleAt ( pos ) ;
if ( style == wxSTC_H_COMMENT )
{
int i = pos;
while ( --i >= 0 && getLexerStyleAt ( i ) == wxSTC_H_COMMENT )
continue;
SetSelectionStart ( i + 1 );
int styled = GetEndStyled();
i = pos;
while ( i < styled && getLexerStyleAt ( i ) == wxSTC_H_COMMENT )
i++;
SetSelectionEnd ( i );
}
else
{
// Select current tag
int start = findPreviousStartTag ( pos, 1, '<', pos );
if ( start < 0 )
{
MyFrame *frame = ( MyFrame * ) wxTheApp->GetTopWindow();
frame->statusProgress ( _("Cannot find the start tag") );
return false;
}
int range = GetTextLength() - pos;
int end = findNextEndTag ( pos, 1, '>', range );
if ( end < 0 )
{
MyFrame *frame = ( MyFrame * ) wxTheApp->GetTopWindow();
frame->statusProgress ( _("Cannot find the end tag") );
return false;
}
SetSelection ( start, end );
}
return true;
}
void XmlCtrl::toggleComment()
{
MyFrame *frame = ( MyFrame * ) wxTheApp->GetTopWindow();
frame->statusProgress ( wxEmptyString );
int pos = -1;
wxString text = GetSelectedText();
if ( text.IsEmpty() )
wxString commentStart = _T ( "<!--" );
wxString commentEnd = _T ( "-->" );
// Is there a selection?
int from = GetSelectionStart();
int to = GetSelectionEnd();
switch ( type )
{
if ( type == FILE_TYPE_BINARY )
case FILE_TYPE_BINARY:
return;
case FILE_TYPE_CSS:
if ( from == to )
return;
commentStart = _T ( "/*" );
commentEnd = _T ( "*/" );
break;
case FILE_TYPE_XML:
if ( from != to )
break;
// Select current element
pos = GetCurrentPos();
Colourise ( 0, -1 );
if ( !selectCurrentElement() )
return;
break;
int style = getLexerStyleAt ( pos ) ;
if ( style == wxSTC_H_COMMENT )
{
int i = pos;
while ( --i >= 0 && getLexerStyleAt ( i ) == wxSTC_H_COMMENT )
continue;
SetSelectionStart ( i + 1 );
int styled = GetEndStyled();
i = pos;
while ( i < styled && getLexerStyleAt ( i ) == wxSTC_H_COMMENT )
i++;
SetSelectionEnd ( i );
}
else
{
// Select current tag
int start = findPreviousStartTag ( pos, 1, '<', pos );
if ( start < 0 )
{
wxMessageBox(_T("Cann't find the start tag"));
return;
}
int range = GetTextLength() - pos;
int end = findNextEndTag ( pos, 1, '>', range );
if ( end < 0 )
{
wxMessageBox(_T("Cann't find the end tag"));
return;
}
SetSelection ( start, end );
}
text = GetSelectedText();
default:
if ( from == to )
return;
break;
}
wxString text = GetSelectedText();
wxASSERT ( !text.IsEmpty() );
// Skip leading spaces
wxString::iterator itr, start, end;
itr = start = text.begin();
@ -2269,9 +2312,6 @@ void XmlCtrl::toggleComment()
while ( itr != end && wxIsspace ( *itr ) )
++itr;
const static wxString commentStart = _T ( "<!--" );
const static wxString commentEnd = _T ( "-->" );
size_t startPos = itr - start;
int ret = text.compare ( startPos, commentStart.length(), commentStart );
if ( ret == 0 )
@ -2306,12 +2346,15 @@ void XmlCtrl::toggleComment()
// Comment selection
// "--" is not allowed in comments
const static wxString doubleHyphen = _T ( "--" );
size_t offset = 0;
while ( ( offset = text.find ( doubleHyphen, offset ) ) != wxString::npos )
if ( commentStart == _T ( "<!--" ) )
{
text.replace ( offset, doubleHyphen.length(), _T ( "- -" ) );
offset += 2; // WARNING: Not three!
const static wxString doubleHyphen = _T ( "--" );
size_t offset = 0;
while ( ( offset = text.find ( doubleHyphen, offset ) ) != wxString::npos )
{
text.replace ( offset, doubleHyphen.length(), _T ( "- -" ) );
offset += 2; // WARNING: Not three!
}
}
text = commentStart + text + commentEnd;

View File

@ -158,6 +158,7 @@ class XmlCtrl: public wxStyledTextCtrl
std::string myGetTextRaw(); // alternative to faulty stc implementation
bool getValidationRequired();
void setValidationRequired ( bool b );
bool selectCurrentElement();
void toggleComment();
private:
ValidationThread *validationThread; // used for background validation