support loading from files for editor contents; tabs and tree widgets improvements

This commit is contained in:
Vadim Lopatin 2015-01-19 11:40:00 +03:00
parent eef521432f
commit 1d639c66de
6 changed files with 160 additions and 4 deletions

View File

@ -63,6 +63,7 @@ public import dlangui.widgets.tabs;
public import dlangui.widgets.menu;
public import dlangui.widgets.scroll;
public import dlangui.widgets.editors;
public import dlangui.widgets.srcedit;
public import dlangui.widgets.grid;
public import dlangui.widgets.tree;
public import dlangui.widgets.combobox;

View File

@ -32,6 +32,7 @@ import dlangui.widgets.menu;
import dlangui.widgets.popup;
import std.algorithm;
import std.stream;
immutable dchar EOL = '\n';
@ -446,6 +447,14 @@ class EditableContent {
}
return this;
}
/// clear content
void clear() {
clearUndo();
_lines.length = 0;
}
/// returns line text
@property int length() { return cast(int)_lines.length; }
dstring opIndex(int index) {
@ -808,6 +817,44 @@ class EditableContent {
void clearUndo() {
_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

View File

@ -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;
}
}

View File

@ -132,6 +132,13 @@ immutable string STYLE_DOCK_WINDOW_CAPTION_LABEL = "DOCK_WINDOW_CAPTION_LABEL";
/// standard style id for 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
immutable string STYLE_TOOLBAR_HOST = "TOOLBAR_HOST";
/// standard style id for toolbars

View File

@ -59,6 +59,24 @@ class TabItem {
void updateAccessTs() {
_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
@ -510,6 +528,8 @@ class TabHost : FrameLayout, TabHandler {
}
/// compound widget - contains from TabControl widget (tabs header) and TabHost (content pages)
class TabWidget : VerticalLayout, TabHandler {
protected TabControl _tabControl;
@ -582,7 +602,8 @@ class TabWidget : VerticalLayout, TabHandler {
}
/// 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);
}

View File

@ -203,17 +203,15 @@ class TreeItem {
protected int _intParam;
protected Object _objectParam;
@property int intParam() {
return _intParam;
}
@property TreeItem intParam(int value) {
_intParam = value;
return this;
}
protected Object _objectParam;
@property Object objectParam() {
return _objectParam;
}
@ -294,6 +292,18 @@ class TreeItem {
}
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 {
@ -626,6 +636,11 @@ class TreeWidgetBase : ScrollWidget, OnTreeContentChangeListener, OnTreeStateCh
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) {
if (event.action == KeyAction.KeyDown) {
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) {
Log.d("tree.handleAction ", a.id);
switch (a.id) {