mirror of https://github.com/buggins/dlangui.git
support loading from files for editor contents; tabs and tree widgets improvements
This commit is contained in:
parent
eef521432f
commit
1d639c66de
|
@ -63,6 +63,7 @@ public import dlangui.widgets.tabs;
|
||||||
public import dlangui.widgets.menu;
|
public import dlangui.widgets.menu;
|
||||||
public import dlangui.widgets.scroll;
|
public import dlangui.widgets.scroll;
|
||||||
public import dlangui.widgets.editors;
|
public import dlangui.widgets.editors;
|
||||||
|
public import dlangui.widgets.srcedit;
|
||||||
public import dlangui.widgets.grid;
|
public import dlangui.widgets.grid;
|
||||||
public import dlangui.widgets.tree;
|
public import dlangui.widgets.tree;
|
||||||
public import dlangui.widgets.combobox;
|
public import dlangui.widgets.combobox;
|
||||||
|
|
|
@ -32,6 +32,7 @@ import dlangui.widgets.menu;
|
||||||
import dlangui.widgets.popup;
|
import dlangui.widgets.popup;
|
||||||
|
|
||||||
import std.algorithm;
|
import std.algorithm;
|
||||||
|
import std.stream;
|
||||||
|
|
||||||
immutable dchar EOL = '\n';
|
immutable dchar EOL = '\n';
|
||||||
|
|
||||||
|
@ -446,6 +447,14 @@ class EditableContent {
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// clear content
|
||||||
|
void clear() {
|
||||||
|
clearUndo();
|
||||||
|
_lines.length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// returns line text
|
/// returns line text
|
||||||
@property int length() { return cast(int)_lines.length; }
|
@property int length() { return cast(int)_lines.length; }
|
||||||
dstring opIndex(int index) {
|
dstring opIndex(int index) {
|
||||||
|
@ -808,6 +817,44 @@ class EditableContent {
|
||||||
void clearUndo() {
|
void clearUndo() {
|
||||||
_undoBuffer.clear();
|
_undoBuffer.clear();
|
||||||
}
|
}
|
||||||
|
/// load content form input stream
|
||||||
|
bool load(InputStream f, string fname = null) {
|
||||||
|
import dlangui.core.linestream;
|
||||||
|
clear();
|
||||||
|
try {
|
||||||
|
LineStream lines = LineStream.create(f, fname);
|
||||||
|
for (;;) {
|
||||||
|
dchar[] s = lines.readLine();
|
||||||
|
if (s is null)
|
||||||
|
break;
|
||||||
|
_lines[_lines.length++] = s.dup;
|
||||||
|
}
|
||||||
|
if (lines.errorCode != 0) {
|
||||||
|
clear();
|
||||||
|
Log.e("Error ", lines.errorCode, " ", lines.errorMessage, " -- at line ", lines.errorLine, " position ", lines.errorPos);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// EOF
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e("Exception while trying to read file ", fname, " ", e.toString);
|
||||||
|
clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// load content from file
|
||||||
|
bool load(string filename) {
|
||||||
|
clear();
|
||||||
|
try {
|
||||||
|
std.stream.File f = new std.stream.File(filename);
|
||||||
|
scope(exit) { f.close(); }
|
||||||
|
return load(f, filename);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e("Exception while trying to read file ", filename, " ", e.toString);
|
||||||
|
clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// base for all editor widgets
|
/// base for all editor widgets
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
// Written in the D programming language.
|
||||||
|
|
||||||
|
/**
|
||||||
|
This module contains implementation of source code editor widget.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Synopsis:
|
||||||
|
|
||||||
|
----
|
||||||
|
import dlangui.widgets.srcedit;
|
||||||
|
|
||||||
|
----
|
||||||
|
|
||||||
|
Copyright: Vadim Lopatin, 2014
|
||||||
|
License: Boost License 1.0
|
||||||
|
Authors: Vadim Lopatin, coolreader.org@gmail.com
|
||||||
|
*/
|
||||||
|
module dlangui.widgets.srcedit;
|
||||||
|
|
||||||
|
import dlangui.graphics.fonts;
|
||||||
|
import dlangui.widgets.editors;
|
||||||
|
import dlangui.widgets.styles;
|
||||||
|
|
||||||
|
class SourceEdit : EditBox {
|
||||||
|
this(string ID) {
|
||||||
|
super(ID);
|
||||||
|
fontFace = "Consolas,Courier New";
|
||||||
|
fontFamily = FontFamily.MonoSpace;
|
||||||
|
fontSize = 18;
|
||||||
|
layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);
|
||||||
|
minFontSize(12).maxFontSize(75); // allow font zoom with Ctrl + MouseWheel
|
||||||
|
}
|
||||||
|
this() {
|
||||||
|
this("SRCEDIT");
|
||||||
|
}
|
||||||
|
protected string _filename;
|
||||||
|
@property string filename() {
|
||||||
|
return _filename;
|
||||||
|
}
|
||||||
|
/// load from file
|
||||||
|
bool load(string fn) {
|
||||||
|
if (content.load(fn)) {
|
||||||
|
_filename = fn;
|
||||||
|
requestLayout();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// failed
|
||||||
|
_filename = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -132,6 +132,13 @@ immutable string STYLE_DOCK_WINDOW_CAPTION_LABEL = "DOCK_WINDOW_CAPTION_LABEL";
|
||||||
/// standard style id for dock window body
|
/// standard style id for dock window body
|
||||||
immutable string STYLE_DOCK_WINDOW_BODY = "DOCK_WINDOW_BODY";
|
immutable string STYLE_DOCK_WINDOW_BODY = "DOCK_WINDOW_BODY";
|
||||||
|
|
||||||
|
/// standard style id for tab control in dock frame
|
||||||
|
immutable string STYLE_TAB_UP_DARK = "TAB_UP_DARK";
|
||||||
|
/// standard style id for tab control tab button in dock frame
|
||||||
|
immutable string STYLE_TAB_UP_BUTTON_DARK = "TAB_UP_BUTTON_DARK";
|
||||||
|
/// standard style id for tab control tab button text in dock frame
|
||||||
|
immutable string STYLE_TAB_UP_BUTTON_DARK_TEXT = "TAB_UP_BUTTON_DARK_TEXT";
|
||||||
|
|
||||||
/// standard style id for toolbars layout
|
/// standard style id for toolbars layout
|
||||||
immutable string STYLE_TOOLBAR_HOST = "TOOLBAR_HOST";
|
immutable string STYLE_TOOLBAR_HOST = "TOOLBAR_HOST";
|
||||||
/// standard style id for toolbars
|
/// standard style id for toolbars
|
||||||
|
|
|
@ -59,6 +59,24 @@ class TabItem {
|
||||||
void updateAccessTs() {
|
void updateAccessTs() {
|
||||||
_lastAccessTs = _lastAccessCounter++; //std.datetime.Clock.currStdTime;
|
_lastAccessTs = _lastAccessCounter++; //std.datetime.Clock.currStdTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Object _objectParam;
|
||||||
|
@property Object objectParam() {
|
||||||
|
return _objectParam;
|
||||||
|
}
|
||||||
|
@property TabItem objectParam(Object value) {
|
||||||
|
_objectParam = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int _intParam;
|
||||||
|
@property int intParam() {
|
||||||
|
return _intParam;
|
||||||
|
}
|
||||||
|
@property TabItem intParam(int value) {
|
||||||
|
_intParam = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// tab item widget - to show tab header
|
/// tab item widget - to show tab header
|
||||||
|
@ -510,6 +528,8 @@ class TabHost : FrameLayout, TabHandler {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// compound widget - contains from TabControl widget (tabs header) and TabHost (content pages)
|
/// compound widget - contains from TabControl widget (tabs header) and TabHost (content pages)
|
||||||
class TabWidget : VerticalLayout, TabHandler {
|
class TabWidget : VerticalLayout, TabHandler {
|
||||||
protected TabControl _tabControl;
|
protected TabControl _tabControl;
|
||||||
|
@ -582,7 +602,8 @@ class TabWidget : VerticalLayout, TabHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// change style ids
|
/// change style ids
|
||||||
void setStyles(string tabStyle, string tabButtonStyle, string tabButtonTextStyle) {
|
void setStyles(string tabWidgetStyle, string tabStyle, string tabButtonStyle, string tabButtonTextStyle) {
|
||||||
|
styleId = tabWidgetStyle;
|
||||||
_tabControl.setStyles(tabStyle, tabButtonStyle, tabButtonTextStyle);
|
_tabControl.setStyles(tabStyle, tabButtonStyle, tabButtonTextStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -203,17 +203,15 @@ class TreeItem {
|
||||||
|
|
||||||
|
|
||||||
protected int _intParam;
|
protected int _intParam;
|
||||||
protected Object _objectParam;
|
|
||||||
|
|
||||||
@property int intParam() {
|
@property int intParam() {
|
||||||
return _intParam;
|
return _intParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
@property TreeItem intParam(int value) {
|
@property TreeItem intParam(int value) {
|
||||||
_intParam = value;
|
_intParam = value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Object _objectParam;
|
||||||
@property Object objectParam() {
|
@property Object objectParam() {
|
||||||
return _objectParam;
|
return _objectParam;
|
||||||
}
|
}
|
||||||
|
@ -294,6 +292,18 @@ class TreeItem {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns item by id, null if not found
|
||||||
|
TreeItem findItemById(string id) {
|
||||||
|
if (_id.equal(id))
|
||||||
|
return this;
|
||||||
|
for (int i = 0; i < childCount; i++) {
|
||||||
|
TreeItem res = child(i).findItemById(id);
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OnTreeContentChangeListener {
|
interface OnTreeContentChangeListener {
|
||||||
|
@ -626,6 +636,11 @@ class TreeWidgetBase : ScrollWidget, OnTreeContentChangeListener, OnTreeStateCh
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns item by id, null if not found
|
||||||
|
TreeItem findItemById(string id) {
|
||||||
|
return _tree.findItemById(id);
|
||||||
|
}
|
||||||
|
|
||||||
override bool onKey(Widget source, KeyEvent event) {
|
override bool onKey(Widget source, KeyEvent event) {
|
||||||
if (event.action == KeyAction.KeyDown) {
|
if (event.action == KeyAction.KeyDown) {
|
||||||
Action action = findKeyAction(event.keyCode, event.flags & (KeyFlag.Shift | KeyFlag.Alt | KeyFlag.Control));
|
Action action = findKeyAction(event.keyCode, event.flags & (KeyFlag.Shift | KeyFlag.Alt | KeyFlag.Control));
|
||||||
|
@ -719,6 +734,19 @@ class TreeWidgetBase : ScrollWidget, OnTreeContentChangeListener, OnTreeStateCh
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void selectItem(TreeItem item, bool makeVisible = true) {
|
||||||
|
if (!item)
|
||||||
|
return;
|
||||||
|
_tree.selectItem(item);
|
||||||
|
if (makeVisible)
|
||||||
|
makeItemVisible(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
void selectItem(string itemId, bool makeVisible = true) {
|
||||||
|
TreeItem item = findItemById(itemId);
|
||||||
|
selectItem(item, makeVisible);
|
||||||
|
}
|
||||||
|
|
||||||
override protected bool handleAction(const Action a) {
|
override protected bool handleAction(const Action a) {
|
||||||
Log.d("tree.handleAction ", a.id);
|
Log.d("tree.handleAction ", a.id);
|
||||||
switch (a.id) {
|
switch (a.id) {
|
||||||
|
|
Loading…
Reference in New Issue