editor readonly mode support

This commit is contained in:
Vadim Lopatin 2014-04-24 14:37:53 +04:00
parent 72ddb2efb7
commit f5bf1bb9ec
6 changed files with 60 additions and 32 deletions

View File

@ -11,6 +11,7 @@ Widget createEditorSettingsControl(EditWidgetBase editor) {
HorizontalLayout res = new HorizontalLayout("editor_options"); HorizontalLayout res = new HorizontalLayout("editor_options");
res.addChild((new CheckBox("wantTabs", "wantTabs"d)).checked(editor.wantTabs).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.wantTabs = checked; return true;})); res.addChild((new CheckBox("wantTabs", "wantTabs"d)).checked(editor.wantTabs).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.wantTabs = checked; return true;}));
res.addChild((new CheckBox("useSpacesForTabs", "useSpacesForTabs"d)).checked(editor.useSpacesForTabs).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.useSpacesForTabs = checked; return true;})); res.addChild((new CheckBox("useSpacesForTabs", "useSpacesForTabs"d)).checked(editor.useSpacesForTabs).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.useSpacesForTabs = checked; return true;}));
res.addChild((new CheckBox("readOnly", "readOnly"d)).checked(editor.readOnly).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.readOnly = checked; return true;}));
res.addChild((new CheckBox("fixedFont", "fixedFont"d)).checked(editor.fontFamily == FontFamily.MonoSpace).addOnCheckChangeListener(delegate(Widget, bool checked) { res.addChild((new CheckBox("fixedFont", "fixedFont"d)).checked(editor.fontFamily == FontFamily.MonoSpace).addOnCheckChangeListener(delegate(Widget, bool checked) {
if (checked) if (checked)
editor.fontFamily(FontFamily.MonoSpace).fontFace("Courier New"); editor.fontFamily(FontFamily.MonoSpace).fontFace("Courier New");

View File

@ -18,7 +18,9 @@
*/ */
--> -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/editbox_background_disabled_focus_yellow" />
<item android:state_focused="true" android:drawable="@drawable/editbox_background_focus_yellow" /> <item android:state_focused="true" android:drawable="@drawable/editbox_background_focus_yellow" />
<item android:state_enabled="false" android:drawable="@drawable/editbox_background_disabled" />
<item android:drawable="@drawable/editbox_background_normal" /> <item android:drawable="@drawable/editbox_background_normal" />
</selector> </selector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -659,8 +659,8 @@ int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int
Platform.setInstance(platform); Platform.setInstance(platform);
if (true) { /// testing freetype font manager
/// testing freetype font manager if (false) {
import dlangui.graphics.ftfonts; import dlangui.graphics.ftfonts;
import win32.shlobj; import win32.shlobj;
FreeTypeFontManager ftfontMan = new FreeTypeFontManager(); FreeTypeFontManager ftfontMan = new FreeTypeFontManager();

View File

@ -224,9 +224,9 @@ class UndoBuffer {
/// adds undo operation /// adds undo operation
void saveForUndo(EditOperation op) { void saveForUndo(EditOperation op) {
_redoList.clear();
if (!_undoList.empty) { if (!_undoList.empty) {
if (_undoList.back.merge(op)) { if (_undoList.back.merge(op)) {
_redoList.clear();
return; // merged - no need to add new operation return; // merged - no need to add new operation
} }
} }
@ -589,6 +589,9 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
protected bool _wantTabs = true; protected bool _wantTabs = true;
protected bool _useSpacesForTabs = false; protected bool _useSpacesForTabs = false;
protected bool _replaceMode;
protected bool _readOnly;
this(string ID) { this(string ID) {
super(ID); super(ID);
@ -665,6 +668,34 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
return this; return this;
} }
/// readonly flag (when true, user cannot change content of editor)
@property bool readOnly() {
return _readOnly;
}
/// sets readonly flag
@property EditWidgetBase readOnly(bool readOnly) {
_readOnly = readOnly;
if (_readOnly)
resetState(State.Enabled);
else
setState(State.Enabled);
invalidate();
return this;
}
/// replace mode flag (when true, entered character replaces character under cursor)
@property bool replaceMode() {
return _replaceMode;
}
/// sets replace mode flag
@property EditWidgetBase replaceMode(bool replaceMode) {
_replaceMode = replaceMode;
invalidate();
return this;
}
/// when true, spaces will be inserted instead of tabs /// when true, spaces will be inserted instead of tabs
@property bool useSpacesForTabs() { @property bool useSpacesForTabs() {
return _useSpacesForTabs; return _useSpacesForTabs;
@ -902,6 +933,8 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
} }
return true; return true;
case EditorActions.DelPrevChar: case EditorActions.DelPrevChar:
if (_readOnly)
return true;
if (!_selectionRange.empty) { if (!_selectionRange.empty) {
// clear selection // clear selection
EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [""d]); EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [""d]);
@ -927,6 +960,8 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
} }
return true; return true;
case EditorActions.DelNextChar: case EditorActions.DelNextChar:
if (_readOnly)
return true;
if (!_selectionRange.empty) { if (!_selectionRange.empty) {
EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [""d]); EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [""d]);
_content.performOperation(op); _content.performOperation(op);
@ -958,12 +993,16 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
if (!_selectionRange.empty) { if (!_selectionRange.empty) {
dstring selectionText = concatDStrings(_content.rangeText(_selectionRange)); dstring selectionText = concatDStrings(_content.rangeText(_selectionRange));
platform.setClipboardText(selectionText); platform.setClipboardText(selectionText);
if (_readOnly)
return true;
EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [""d]); EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [""d]);
_content.performOperation(op); _content.performOperation(op);
} }
return true; return true;
case EditorActions.Paste: case EditorActions.Paste:
{ {
if (_readOnly)
return true;
dstring selectionText = platform.getClipboardText(); dstring selectionText = platform.getClipboardText();
dstring[] lines; dstring[] lines;
if (_content.multiline) { if (_content.multiline) {
@ -977,16 +1016,22 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
return true; return true;
case EditorActions.Undo: case EditorActions.Undo:
{ {
if (_readOnly)
return true;
_content.undo(); _content.undo();
} }
return true; return true;
case EditorActions.Redo: case EditorActions.Redo:
{ {
if (_readOnly)
return true;
_content.redo(); _content.redo();
} }
return true; return true;
case EditorActions.Tab: case EditorActions.Tab:
{ {
if (_readOnly)
return true;
if (_selectionRange.empty) { if (_selectionRange.empty) {
if (_useSpacesForTabs) { if (_useSpacesForTabs) {
// insert one or more spaces to // insert one or more spaces to
@ -1019,6 +1064,8 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
return true; return true;
case EditorActions.BackTab: case EditorActions.BackTab:
{ {
if (_readOnly)
return true;
if (_selectionRange.empty) { if (_selectionRange.empty) {
// remove spaces before caret // remove spaces before caret
TextRange r = spaceBefore(_caretPos); TextRange r = spaceBefore(_caretPos);
@ -1148,27 +1195,23 @@ class EditWidgetBase : WidgetGroup, EditableContentListener {
/// map key to action /// map key to action
override protected Action findKeyAction(uint keyCode, uint flags) { override protected Action findKeyAction(uint keyCode, uint flags) {
// don't handle tabs when disabled // don't handle tabs when disabled
if (keyCode == KeyCode.TAB && (flags == 0 || flags == KeyFlag.Shift) && !_wantTabs) if (keyCode == KeyCode.TAB && (flags == 0 || flags == KeyFlag.Shift) && (!_wantTabs || _readOnly))
return null; return null;
return super.findKeyAction(keyCode, flags); return super.findKeyAction(keyCode, flags);
} }
/// handle keys /// handle keys
override bool onKeyEvent(KeyEvent event) { override bool onKeyEvent(KeyEvent event) {
// if (event.action == KeyAction.Text && event.text.length) {
if (event.action == KeyAction.KeyDown) {
//EditorAction a = keyToAction(event.keyCode, event.flags & (KeyFlag.Shift | KeyFlag.Alt | KeyFlag.Ctrl));
//switch(event.keyCode) {
//
//}
} else if (event.action == KeyAction.Text && event.text.length) {
Log.d("text entered: ", event.text); Log.d("text entered: ", event.text);
if (_readOnly)
return true;
dchar ch = event.text[0]; dchar ch = event.text[0];
if (ch >= 32) { // ignore Backspace and Return if (ch >= 32) { // ignore Backspace and Return
EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [event.text]); EditOperation op = new EditOperation(EditAction.Replace, _selectionRange, [event.text]);
_content.performOperation(op); _content.performOperation(op);
return true;
} }
return true;
} }
return super.onKeyEvent(event); return super.onKeyEvent(event);
} }
@ -1286,24 +1329,6 @@ class EditLine : EditWidgetBase {
override protected bool handleAction(Action a) { override protected bool handleAction(Action a) {
switch (a.id) { switch (a.id) {
/*
case EditorActions.DelPrevChar:
if (_caretPos.pos > 0) {
TextRange range = TextRange(_caretPos, _caretPos);
range.start.pos--;
EditOperation op = new EditOperation(EditAction.Replace, range, [""d]);
_content.performOperation(op);
}
return true;
case EditorActions.DelNextChar:
if (_caretPos.pos < _measuredText.length) {
TextRange range = TextRange(_caretPos, _caretPos);
range.end.pos++;
EditOperation op = new EditOperation(EditAction.Replace, range, [""d]);
_content.performOperation(op);
}
return true;
*/
case EditorActions.Up: case EditorActions.Up:
break; break;
case EditorActions.Down: case EditorActions.Down:
@ -1372,8 +1397,8 @@ class EditLine : EditWidgetBase {
super.onDraw(buf); super.onDraw(buf);
Rect rc = _pos; Rect rc = _pos;
applyMargins(rc); applyMargins(rc);
auto saver = ClipRectSaver(buf, rc);
applyPadding(rc); applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
FontRef font = font(); FontRef font = font();
dstring txt = text; dstring txt = text;
Point sz = font.textSize(txt); Point sz = font.textSize(txt);
@ -1385,7 +1410,7 @@ class EditLine : EditWidgetBase {
visibleRect.left = _clientRc.left; visibleRect.left = _clientRc.left;
visibleRect.right = _clientRc.right; visibleRect.right = _clientRc.right;
drawLineBackground(buf, lineRect, visibleRect); drawLineBackground(buf, lineRect, visibleRect);
font.drawText(buf, rc.left - _scrollPos.x, rc.top + sz.y / 10, txt, textColor, tabSize); font.drawText(buf, rc.left - _scrollPos.x, rc.top, txt, textColor, tabSize);
if (focused) { if (focused) {
// draw caret // draw caret
Rect caretRc = textPosToClient(_caretPos); Rect caretRc = textPosToClient(_caretPos);