select word by double click in editors

This commit is contained in:
Vadim Lopatin 2015-02-10 17:57:33 +03:00
parent 02526cbbb3
commit 9fd6a1e513
6 changed files with 69 additions and 10 deletions

View File

@ -141,8 +141,8 @@
<compiler>0</compiler>
<otherDMD>0</otherDMD>
<program>$(DMDInstallDir)windows\bin\dmd.exe</program>
<imppath>3rdparty ../DerelictGL3/source $(SolutionDir)/../dlib</imppath>
<fileImppath />
<imppath>3rdparty ../DerelictGL3/source ../DerelictUtil/source ../DerelictFT/source ../de_image/source/interfaces ../de_image/source/png $(SolutionDir)/../dlib</imppath>
<fileImppath>$(SolutionDir)/views $(SolutionDir)/views/res $(SolutionDir)/views/res/i18n $(SolutionDir)/views/res/mdpi</fileImppath>
<outdir>$(ConfigurationName)</outdir>
<objdir>$(OutDir)</objdir>
<objname />
@ -160,7 +160,7 @@
<debuglevel>0</debuglevel>
<debugids />
<versionlevel>0</versionlevel>
<versionids>Unicode</versionids>
<versionids>EmbedStandardResources Unicode USE_FREETYPE</versionids>
<dump_source>0</dump_source>
<mapverbosity>0</mapverbosity>
<createImplib>0</createImplib>

View File

@ -141,8 +141,8 @@
<compiler>0</compiler>
<otherDMD>0</otherDMD>
<program>$(DMDInstallDir)windows\bin\dmd.exe</program>
<imppath>../../src ../../3rdparty ../../3rdparty/libpng/source $(SolutionDir)/../dlib</imppath>
<fileImppath />
<imppath>$(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(SolutionDir)/../dlib</imppath>
<fileImppath>views views/res views/res/i18n views/res/mdpi</fileImppath>
<outdir>$(ConfigurationName)</outdir>
<objdir>$(OutDir)</objdir>
<objname />
@ -160,7 +160,7 @@
<debuglevel>0</debuglevel>
<debugids />
<versionlevel>0</versionlevel>
<versionids>Unicode</versionids>
<versionids>Unicode USE_FREETYPE</versionids>
<dump_source>0</dump_source>
<mapverbosity>0</mapverbosity>
<createImplib>0</createImplib>

View File

@ -141,8 +141,8 @@
<compiler>0</compiler>
<otherDMD>0</otherDMD>
<program>$(DMDInstallDir)windows\bin\dmd.exe</program>
<imppath>../../src ../../3rdparty ../../3rdparty/libpng/source $(SolutionDir)/../dlib</imppath>
<fileImppath />
<imppath>$(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(SolutionDir)/../dlib</imppath>
<fileImppath>views views/res views/mdpi views/i18n</fileImppath>
<outdir>$(ConfigurationName)</outdir>
<objdir>$(OutDir)</objdir>
<objname />

View File

@ -577,6 +577,45 @@ class EditableContent {
performOperation(op, this);
}
static bool isAlphaForWordSelection(dchar ch) {
return ch == '_' || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}
/// get word bounds by position
TextRange wordBounds(TextPosition pos) {
TextRange res;
res.start = pos;
res.end = pos;
if (pos.line < 0 || pos.line >= _lines.length)
return res;
dstring s = line(pos.line);
int p = pos.pos;
if (p < 0 || p > s.length)
return res;
dchar leftChar = p > 0 ? s[p - 1] : 0;
dchar rightChar = p < s.length - 1 ? s[p + 1] : 0;
dchar centerChar = p < s.length ? s[p] : 0;
if (isAlphaForWordSelection(centerChar)) {
// ok
} else if (isAlphaForWordSelection(leftChar)) {
p--;
} else if (isAlphaForWordSelection(rightChar)) {
p++;
} else {
return res;
}
int start = p;
int end = p;
while (start > 0 && isAlphaForWordSelection(s[start - 1]))
start--;
while (end + 1 < s.length && isAlphaForWordSelection(s[end + 1]))
end++;
end++;
res.start.pos = start;
res.end.pos = end;
return res;
}
/// call listener to say that whole content is replaced e.g. by loading from file
void notifyContentReplaced() {
clearEditMarks();

View File

@ -1213,8 +1213,9 @@ mixin template APP_ENTRY_POINT() {
char* lpCmdLine, int nCmdShow)
{
try {
return DLANGUIWinMain(hInstance, hPrevInstance,
int res = DLANGUIWinMain(hInstance, hPrevInstance,
lpCmdLine, nCmdShow);
return res;
} catch (Exception e) {
Log.e("Exception: ", e);
return 1;

View File

@ -851,6 +851,21 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
requestActionsUpdate();
}
protected void selectWordByMouse(int x, int y) {
TextPosition oldCaretPos = _caretPos;
TextPosition newPos = clientToTextPos(Point(x,y));
TextRange r = content.wordBounds(newPos);
if (r.start < r.end) {
_selectionRange = r;
_caretPos = r.end;
invalidate();
requestActionsUpdate();
} else {
_caretPos = newPos;
updateSelectionAfterCursorMovement(oldCaretPos, false);
}
}
protected void updateCaretPositionByMouse(int x, int y, bool selecting) {
TextPosition oldCaretPos = _caretPos;
TextPosition newPos = clientToTextPos(Point(x,y));
@ -1362,7 +1377,11 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Left) {
setFocus();
startCaretBlinking();
updateCaretPositionByMouse(event.x - _clientRect.left, event.y - _clientRect.top, false);
if (event.doubleClick) {
selectWordByMouse(event.x - _clientRect.left, event.y - _clientRect.top);
} else {
updateCaretPositionByMouse(event.x - _clientRect.left, event.y - _clientRect.top, false);
}
invalidate();
return true;
}