📄 findreplacedlg.cpp
字号:
int nbMarked = processAll(MARK_ALL);
char result[64];
if (nbMarked < 0)
strcpy(result, "The regular expression to search is formed badly");
else
{
itoa(nbMarked, result, 10);
strcat(result, " tokens are found and marked");
}
::MessageBox(_hSelf, result, "", MB_OK);
return TRUE;
}
case IDC_CLEAR_ALL :
{
LangType lt = (*_ppEditView)->getCurrentDocType();
if (lt == L_TXT)
(*_ppEditView)->defineDocType(L_CPP);
(*_ppEditView)->defineDocType(lt);
(*_ppEditView)->execute(SCI_MARKERDELETEALL, MARK_SYMBOLE);
return TRUE;
}
case IDCCOUNTALL :
{
int nbCounted = processAll(COUNT_ALL);
char result[64];
if (nbCounted < 0)
strcpy(result, "The regular expression to search is formed badly");
else
{
itoa(nbCounted, result, 10);
strcat(result, " tokens are found.");
}
::MessageBox(_hSelf, result, "", MB_OK);
return TRUE;
}
case IDWHOLEWORD :
_options._isWholeWord = isCheckedOrNot(IDWHOLEWORD);
return TRUE;
case IDMATCHCASE :
_options._isMatchCase = isCheckedOrNot(IDMATCHCASE);
return TRUE;
case IDREGEXP :
_options._isRegExp = isCheckedOrNot(IDREGEXP);
if (_options._isRegExp)
_options._isWholeWord = false;
::SendDlgItemMessage(_hSelf, IDWHOLEWORD, BM_SETCHECK, _options._isWholeWord?BST_CHECKED:BST_UNCHECKED, 0);
::EnableWindow(::GetDlgItem(_hSelf, IDWHOLEWORD), (BOOL)!_options._isRegExp);
::SendDlgItemMessage(_hSelf, IDDIRECTIONUP, BM_SETCHECK, BST_UNCHECKED, 0);
::EnableWindow(::GetDlgItem(_hSelf, IDDIRECTIONUP), (BOOL)!_options._isRegExp);
::SendDlgItemMessage(_hSelf, IDDIRECTIONDOWN, BM_SETCHECK, BST_CHECKED, 0);
_options._whichDirection = DIR_DOWN;
return TRUE;
case IDWRAP :
_options._isWrapAround = isCheckedOrNot(IDWRAP);
return TRUE;
case IDDIRECTIONUP :
case IDDIRECTIONDOWN :
_options._whichDirection = (BST_CHECKED == ::SendMessage(::GetDlgItem(_hSelf, IDDIRECTIONDOWN), BM_GETCHECK, BST_CHECKED, 0));
::EnableWindow(::GetDlgItem(_hSelf, IDC_DISPLAYPOS_STATIC), (BOOL)(_options._whichDirection == DIR_DOWN));
::EnableWindow(::GetDlgItem(_hSelf, IDC_DISPLAYPOS_TOP), (BOOL)(_options._whichDirection == DIR_DOWN));
::EnableWindow(::GetDlgItem(_hSelf, IDC_DISPLAYPOS_MIDDLE), (BOOL)(_options._whichDirection == DIR_DOWN));
::EnableWindow(::GetDlgItem(_hSelf, IDC_DISPLAYPOS_BOTTOM), (BOOL)(_options._whichDirection == DIR_DOWN));
return TRUE;
case IDC_PURGE_CHECK :
_doPurge = isCheckedOrNot(IDC_PURGE_CHECK);
return TRUE;
case IDC_MARKLINE_CHECK :
_doMarkLine = isCheckedOrNot(IDC_MARKLINE_CHECK);
::EnableWindow(::GetDlgItem(_hSelf, IDCMARKALL), (_doMarkLine || _doStyleFoundToken));
return TRUE;
case IDC_STYLEFOUND_CHECK :
_doStyleFoundToken = isCheckedOrNot(IDC_STYLEFOUND_CHECK);
::EnableWindow(::GetDlgItem(_hSelf, IDCMARKALL), (_doMarkLine || _doStyleFoundToken));
return TRUE;
case IDC_IN_SELECTION_CHECK :
_isInSelection = isCheckedOrNot(IDC_IN_SELECTION_CHECK);
return TRUE;
case IDC_TRANSPARENT_CHECK :
{
bool isChecked = isCheckedOrNot(IDC_TRANSPARENT_CHECK);
if (isChecked)
{
int percent = ::SendDlgItemMessage(_hSelf, IDC_PERCENTAGE_SLIDER, TBM_GETPOS, 0, 0);
(NppParameters::getInstance())->SetTransparent(_hSelf, percent);
}
else
(NppParameters::getInstance())->removeTransparent(_hSelf);
::EnableWindow(::GetDlgItem(_hSelf, IDC_PERCENTAGE_SLIDER), isChecked);
return TRUE;
}
default :
break;
}
}
}
return FALSE;
}
// return value :
// true : the text2find is found
// false : the text2find is not found
bool FindReplaceDlg::processFindNext(const char *txt2find, FindOption *options)
{
bool processFromParams = txt2find && options && txt2find[0];
const char *pText;
FindOption *pOptions;
if (!processFromParams)
{
if (!isCreated()) return false;
getSearchTexts();
addText2Combo(_text2Find, ::GetDlgItem(_hSelf, IDFINDWHAT));
pText = _text2Find;
pOptions = &_options;
}
else
{
pText = txt2find;
pOptions = options;
}
int docLength = int((*_ppEditView)->execute(SCI_GETLENGTH));
CharacterRange cr = (*_ppEditView)->getSelection();
int startPosition = cr.cpMax;
int endPosition = docLength;
if (pOptions->_whichDirection == DIR_UP)
{
startPosition = cr.cpMin - 1;
endPosition = 0;
}
int flags = (pOptions->_isWholeWord ? SCFIND_WHOLEWORD : 0) |
(pOptions->_isMatchCase ? SCFIND_MATCHCASE : 0) |
(pOptions->_isRegExp ? SCFIND_REGEXP|SCFIND_POSIX : 0);
(*_ppEditView)->execute(SCI_SETTARGETSTART, startPosition);
(*_ppEditView)->execute(SCI_SETTARGETEND, endPosition);
(*_ppEditView)->execute(SCI_SETSEARCHFLAGS, flags);
//char translatedText[FIND_REPLACE_STR_MAX];
/*
if (_isRegExp)
{
formatType f = (*_ppEditView)->getCurrentBuffer().getFormat();
pText = translate2SlashN(translatedText, f);
}
*/
int posFind = int((*_ppEditView)->execute(SCI_SEARCHINTARGET, strlen(pText), (LPARAM)pText));
if (posFind == -1) //return;
{
if (pOptions->_isWrapAround)
{
if (pOptions->_whichDirection == DIR_DOWN)
{
startPosition = 0;
endPosition = docLength;
}
else
{
startPosition = docLength;
endPosition = 0;
}
(*_ppEditView)->execute(SCI_SETTARGETSTART, startPosition);
(*_ppEditView)->execute(SCI_SETTARGETEND, endPosition);
int posFind = int((*_ppEditView)->execute(SCI_SEARCHINTARGET, strlen(pText), (LPARAM)pText));
if (posFind == -1)
{
::MessageBox(_hSelf, "Can't find the word", "Find", MB_OK);
// if the dialog is not shown, pass the focus to his parent(ie. Notepad++)
if (!::IsWindowVisible(_hSelf))
::SetFocus((*_ppEditView)->getHSelf());
return false;
}
int start = int((*_ppEditView)->execute(SCI_GETTARGETSTART));
int end = int((*_ppEditView)->execute(SCI_GETTARGETEND));
(*_ppEditView)->execute(SCI_SETSEL, start, end);
}
else
{
::MessageBox(_hSelf, "Can't find the word", "Find", MB_OK);
// if the dialog is not shown, pass the focus to his parent(ie. Notepad++)
if (!::IsWindowVisible(_hSelf))
::SetFocus((*_ppEditView)->getHSelf());
return false;
}
}
int start = int((*_ppEditView)->execute(SCI_GETTARGETSTART));
int end = int((*_ppEditView)->execute(SCI_GETTARGETEND));
int displayPos = getDisplayPos();
(*_ppEditView)->execute(SCI_SETSEL, start, end);
if ((displayPos != DISPLAY_POS_BOTTOM) && (_options._whichDirection == DIR_DOWN))
{
int firstVisibleLine = (*_ppEditView)->execute(EM_GETFIRSTVISIBLELINE);
int currentlineNumber = (*_ppEditView)->execute(SCI_LINEFROMPOSITION, posFind);
int nbColumn2Scroll;
if (displayPos == DISPLAY_POS_TOP)
nbColumn2Scroll = currentlineNumber-firstVisibleLine;
else //(displayPos == DISPLAY_POS_MIDDLE)
nbColumn2Scroll = (currentlineNumber-firstVisibleLine)/2;
(*_ppEditView)->scroll(0, nbColumn2Scroll);
}
return true;
}
// return value :
// true : the text is replaced, and find the next occurrence
// false : the text2find is not found, so the text is NOT replace
// || the text is replaced, and do NOT find the next occurrence
bool FindReplaceDlg::processReplace()
{
if ((*_ppEditView)->getCurrentBuffer().isReadOnly()) return false;
getSearchTexts() ;
getReplaceTexts();
int flags = (_options._isWholeWord ? SCFIND_WHOLEWORD : 0) |
(_options._isMatchCase ? SCFIND_MATCHCASE : 0) |
(_options._isRegExp ? SCFIND_REGEXP|SCFIND_POSIX : 0);
CharacterRange cr = (*_ppEditView)->getSelection();
(*_ppEditView)->execute(SCI_SETTARGETSTART, cr.cpMin);
(*_ppEditView)->execute(SCI_SETTARGETEND, cr.cpMax);
(*_ppEditView)->execute(SCI_SETSEARCHFLAGS, flags);
int posFind = int((*_ppEditView)->execute(SCI_SEARCHINTARGET, (WPARAM)strlen(_text2Find), (LPARAM)_text2Find));
if (posFind != -1)
{
if (_options._isRegExp)
{
//For the rare re exp case. ex: replace ^ by AAA
int start = int((*_ppEditView)->execute(SCI_GETTARGETSTART));
int end = int((*_ppEditView)->execute(SCI_GETTARGETEND));
int foundTextLen = (end >= start)?end - start:start - end;
int replacedLen = (*_ppEditView)->execute(SCI_REPLACETARGETRE, strlen(_replaceText), (LPARAM)_replaceText);
if (!foundTextLen)
(*_ppEditView)->execute(SCI_SETSEL, start, start + replacedLen);
}
else
{
(*_ppEditView)->execute(SCI_REPLACESEL, strlen(_replaceText), (LPARAM)_replaceText);
}
}
return processFindNext();
}
int FindReplaceDlg::processAll(int op, bool isEntire, const char *dir2search)
{
int nbReplaced = 0;
if (!isCreated()) return nbReplaced;
if ((op == REPLACE_ALL) && (*_ppEditView)->getCurrentBuffer().isReadOnly())
return nbReplaced;
FindOption *pOptions = &_options;
getSearchTexts();
getReplaceTexts() ;
int docLength = int((*_ppEditView)->execute(SCI_GETLENGTH));
CharacterRange cr = (*_ppEditView)->getSelection();
// Par default :
// direction : bas
// commence par : cursor pos
// fini par : fin doc
int startPosition = cr.cpMin;
int endPosition = docLength;
if (pOptions->_whichDirection == DIR_UP)
{
startPosition = cr.cpMax;
endPosition = 0;
}
bool direction = pOptions->_whichDirection;
if ((pOptions->_isWrapAround || isEntire) || (op == COUNT_ALL))
{
startPosition = 0;
endPosition = docLength;
direction = DIR_DOWN;
}
if ((_isInSelection) && ((op == MARK_ALL) || ((op == REPLACE_ALL) && (!isEntire))))
{
CharacterRange cr = (*_ppEditView)->getSelection();
startPosition = cr.cpMin;
endPosition = cr.cpMax;
}
int flags = (pOptions->_isWholeWord ? SCFIND_WHOLEWORD : 0) |
(pOptions->_isMatchCase ? SCFIND_MATCHCASE : 0) |
(pOptions->_isRegExp ? SCFIND_REGEXP|SCFIND_POSIX : 0);
(*_ppEditView)->execute(SCI_SETTARGETSTART, startPosition);
(*_ppEditView)->execute(SCI_SETTARGETEND, endPosition);
(*_ppEditView)->execute(SCI_SETSEARCHFLAGS, flags);
addText2Combo(_text2Find, ::GetDlgItem(_hSelf, IDFINDWHAT));
if (!_text2Find[0])
return nbReplaced;
if (op == MARK_ALL)
{
if (_doStyleFoundToken)
{
if (_doPurge)
{
LangType lt = (*_ppEditView)->getCurrentDocType();
if (lt == L_TXT)
(*_ppEditView)->defineDocType(L_CPP);
(*_ppEditView)->defineDocType(lt);
}
(*_ppEditView)->execute(SCI_SETLEXER, SCLEX_NULL);
}
if ((_doMarkLine) && (_doPurge))
{
(*_ppEditView)->execute(SCI_MARKERDELETEALL, MARK_SYMBOLE);
}
}
int posFind = int((*_ppEditView)->execute(SCI_SEARCHINTARGET, (WPARAM)strlen(_text2Find), (LPARAM)_text2Find));
while (posFind != -1)
{
int posFindBefore = posFind;
int start = int((*_ppEditView)->execute(SCI_GETTARGETSTART));
int end = int((*_ppEditView)->execute(SCI_GETTARGETEND));
int foundTextLen = (end >= start)?end - start:start - end;
// Si on a trouv
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -