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_CUT, MyFrame::OnUpdateCutCopy )
EVT_UPDATE_UI ( wxID_COPY, MyFrame::OnUpdateCutCopy ) EVT_UPDATE_UI ( wxID_COPY, MyFrame::OnUpdateCutCopy )
EVT_UPDATE_UI ( ID_FIND_AGAIN, MyFrame::OnUpdateFindAgain ) 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_RANGE ( ID_FIND, ID_EXPORT_MSWORD, MyFrame::OnUpdateDocRange )
EVT_UPDATE_UI ( ID_PREVIOUS_DOCUMENT, MyFrame::OnUpdatePreviousDocument ) EVT_UPDATE_UI ( ID_PREVIOUS_DOCUMENT, MyFrame::OnUpdatePreviousDocument )
EVT_UPDATE_UI ( ID_NEXT_DOCUMENT, MyFrame::OnUpdateNextDocument ) EVT_UPDATE_UI ( ID_NEXT_DOCUMENT, MyFrame::OnUpdateNextDocument )
@ -3699,6 +3700,20 @@ void MyFrame::OnUpdatePaste ( wxUpdateUIEvent& event )
event.Enable ( getActiveDocument() != NULL ); 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 ) void MyFrame::OnUpdatePreviousDocument ( wxUpdateUIEvent& event )
{ {
if ( !getActiveDocument() ) if ( !getActiveDocument() )

View File

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

View File

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

View File

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