tab and tree controls fixes

This commit is contained in:
Vadim Lopatin 2015-01-26 16:07:35 +03:00
parent 90e6d7e49c
commit f11435e4b6
6 changed files with 78 additions and 25 deletions

View File

@ -171,6 +171,14 @@ class Action {
_id = newId; _id = newId;
return this; return this;
} }
/// compares id of this action with another action id
bool opEquals(int anotherActionId) const {
return _id == anotherActionId;
}
/// compares id of this action with another action id
bool opEquals(const Action action) const {
return _id == action._id;
}
/// sets label string resource id /// sets label string resource id
@property Action label(string resourceId) { @property Action label(string resourceId) {
_label = resourceId; _label = resourceId;

View File

@ -30,29 +30,18 @@ enum StandardAction : int {
Ignore, Ignore,
Open, Open,
Save, Save,
DiscardChanges,
} }
const Action ACTION_OK; const Action ACTION_OK = new Action(StandardAction.Ok, "ACTION_OK"c);
const Action ACTION_CANCEL; const Action ACTION_CANCEL = new Action(StandardAction.Cancel, "ACTION_CANCEL"c);
const Action ACTION_YES; const Action ACTION_YES = new Action(StandardAction.Yes, "ACTION_YES"c);
const Action ACTION_NO; const Action ACTION_NO = new Action(StandardAction.No, "ACTION_NO"c);
const Action ACTION_CLOSE; const Action ACTION_CLOSE = new Action(StandardAction.Close, "ACTION_CLOSE"c);
const Action ACTION_ABORT; const Action ACTION_ABORT = new Action(StandardAction.Abort, "ACTION_ABORT"c);
const Action ACTION_RETRY; const Action ACTION_RETRY = new Action(StandardAction.Retry, "ACTION_RETRY"c);
const Action ACTION_IGNORE; const Action ACTION_IGNORE = new Action(StandardAction.Ignore, "ACTION_IGNORE"c);
const Action ACTION_OPEN; const Action ACTION_OPEN = new Action(StandardAction.Open, "ACTION_OPEN"c);
const Action ACTION_SAVE; const Action ACTION_SAVE = new Action(StandardAction.Save, "ACTION_SAVE"c);
const Action ACTION_DISCARD_CHANGES = new Action(StandardAction.DiscardChanges, "ACTION_DISCARD_CHANGES"c);
static this()
{
ACTION_OK = new Action(StandardAction.Ok, "ACTION_OK"c);
ACTION_CANCEL = cast(immutable(Action)) new Action(StandardAction.Cancel, "ACTION_CANCEL"c);
ACTION_YES = cast(immutable(Action)) new Action(StandardAction.Yes, "ACTION_YES"c);
ACTION_NO = cast(immutable(Action)) new Action(StandardAction.No, "ACTION_NO"c);
ACTION_CLOSE = cast(immutable(Action)) new Action(StandardAction.Close, "ACTION_CLOSE"c);
ACTION_ABORT = cast(immutable(Action)) new Action(StandardAction.Abort, "ACTION_ABORT"c);
ACTION_RETRY = cast(immutable(Action)) new Action(StandardAction.Retry, "ACTION_RETRY"c);
ACTION_IGNORE = cast(immutable(Action)) new Action(StandardAction.Ignore, "ACTION_IGNORE"c);
ACTION_OPEN = cast(immutable(Action)) new Action(StandardAction.Open, "ACTION_OPEN"c);
ACTION_SAVE = cast(immutable(Action)) new Action(StandardAction.Save, "ACTION_SAVE"c);
}

View File

@ -26,10 +26,18 @@ import dlangui.core.signals;
import dlangui.widgets.layouts; import dlangui.widgets.layouts;
import dlangui.widgets.controls; import dlangui.widgets.controls;
import std.algorithm;
/// current tab is changed handler
interface TabHandler { interface TabHandler {
void onTabChanged(string newActiveTabId, string previousTabId); void onTabChanged(string newActiveTabId, string previousTabId);
} }
/// tab close button pressed handler
interface TabCloseHandler {
void onTabClose(string tabId);
}
/// tab item metadata /// tab item metadata
class TabItem { class TabItem {
@ -86,6 +94,7 @@ class TabItemWidget : HorizontalLayout {
private ImageButton _closeButton; private ImageButton _closeButton;
private TabItem _item; private TabItem _item;
private bool _enableCloseButton; private bool _enableCloseButton;
Signal!TabCloseHandler onTabCloseListener;
@property TabItem tabItem() { return _item; } @property TabItem tabItem() { return _item; }
@property TabControl tabControl() { return cast(TabControl)parent; } @property TabControl tabControl() { return cast(TabControl)parent; }
this(TabItem item, bool enableCloseButton = true) { this(TabItem item, bool enableCloseButton = true) {
@ -119,6 +128,8 @@ class TabItemWidget : HorizontalLayout {
protected bool onClick(Widget source) { protected bool onClick(Widget source) {
if (source.compareId("CLOSE")) { if (source.compareId("CLOSE")) {
Log.d("tab close button pressed"); Log.d("tab close button pressed");
if (onTabCloseListener.assigned)
onTabCloseListener(_item.id);
} }
return true; return true;
} }
@ -217,6 +228,9 @@ class TabControl : WidgetGroupDefaultDrawing {
/// signal of tab change (e.g. by clicking on tab header) /// signal of tab change (e.g. by clicking on tab header)
Signal!TabHandler onTabChangedListener; Signal!TabHandler onTabChangedListener;
/// signal on tab close button
Signal!TabCloseHandler onTabCloseListener;
/// empty parameter list constructor - for usage by factory /// empty parameter list constructor - for usage by factory
this() { this() {
this(null); this(null);
@ -299,12 +313,29 @@ class TabControl : WidgetGroupDefaultDrawing {
/// remove tab /// remove tab
TabControl removeTab(string id) { TabControl removeTab(string id) {
string nextId;
if (id.equal(_selectedTabId)) {
// current tab is being closed: remember next tab id
int nextIndex = getNextItemIndex(1);
if (nextIndex < 0)
nextIndex = getNextItemIndex(-1);
if (nextIndex >= 0)
nextId = _items[nextIndex].id;
}
int index = _items.indexById(id); int index = _items.indexById(id);
if (index >= 0) { if (index >= 0) {
_children.remove(index + 1); _children.remove(index + 1);
_items.remove(index); _items.remove(index);
if (id.equal(_selectedTabId))
_selectedTabId = null;
requestLayout(); requestLayout();
} }
if (nextId) {
index = _items.indexById(nextId);
if (index >= 0) {
selectTab(index, true);
}
}
return this; return this;
} }
@ -329,6 +360,10 @@ class TabControl : WidgetGroupDefaultDrawing {
} }
} }
protected void onTabClose(string tabId) {
if (onTabCloseListener.assigned)
onTabCloseListener(tabId);
}
/// add new tab /// add new tab
TabControl addTab(TabItem item, int index = -1, bool enableCloseButton = false) { TabControl addTab(TabItem item, int index = -1, bool enableCloseButton = false) {
@ -337,6 +372,7 @@ class TabControl : WidgetGroupDefaultDrawing {
widget.parent = this; widget.parent = this;
widget.onClickListener = &onClick; widget.onClickListener = &onClick;
widget.setStyles(_tabButtonStyle, _tabButtonTextStyle); widget.setStyles(_tabButtonStyle, _tabButtonTextStyle);
widget.onTabCloseListener = &onTabClose;
_children.insert(widget, index); _children.insert(widget, index);
updateTabs(); updateTabs();
requestLayout(); requestLayout();
@ -440,6 +476,10 @@ class TabControl : WidgetGroupDefaultDrawing {
protected string _selectedTabId; protected string _selectedTabId;
@property string selectedTabId() const {
return _selectedTabId;
}
void updateAccessTs() { void updateAccessTs() {
int index = _items.indexById(_selectedTabId); int index = _items.indexById(_selectedTabId);
if (index >= 0) if (index >= 0)
@ -566,7 +606,7 @@ 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, TabCloseHandler {
protected TabControl _tabControl; protected TabControl _tabControl;
protected TabHost _tabHost; protected TabHost _tabHost;
/// empty parameter list constructor - for usage by factory /// empty parameter list constructor - for usage by factory
@ -579,6 +619,7 @@ class TabWidget : VerticalLayout, TabHandler {
_tabControl = new TabControl("TAB_CONTROL"); _tabControl = new TabControl("TAB_CONTROL");
_tabHost = new TabHost("TAB_HOST", _tabControl); _tabHost = new TabHost("TAB_HOST", _tabControl);
_tabControl.onTabChangedListener.connect(this); _tabControl.onTabChangedListener.connect(this);
_tabControl.onTabCloseListener.connect(this);
styleId = STYLE_TAB_WIDGET; styleId = STYLE_TAB_WIDGET;
addChild(_tabControl); addChild(_tabControl);
addChild(_tabHost); addChild(_tabHost);
@ -587,6 +628,13 @@ class TabWidget : VerticalLayout, TabHandler {
/// signal of tab change (e.g. by clicking on tab header) /// signal of tab change (e.g. by clicking on tab header)
Signal!TabHandler onTabChangedListener; Signal!TabHandler onTabChangedListener;
/// signal on tab close button
Signal!TabCloseHandler onTabCloseListener;
protected override void onTabClose(string tabId) {
if (onTabCloseListener.assigned)
onTabCloseListener(tabId);
}
protected override void onTabChanged(string newActiveTabId, string previousTabId) { protected override void onTabChanged(string newActiveTabId, string previousTabId) {
// forward to listener // forward to listener

View File

@ -738,9 +738,15 @@ class TreeWidgetBase : ScrollWidget, OnTreeContentChangeListener, OnTreeStateCh
} }
} }
void clearSelection() {
_tree.selectItem(null);
}
void selectItem(TreeItem item, bool makeVisible = true) { void selectItem(TreeItem item, bool makeVisible = true) {
if (!item) if (!item) {
clearSelection();
return; return;
}
_tree.selectItem(item); _tree.selectItem(item);
if (makeVisible) if (makeVisible)
makeItemVisible(item); makeItemVisible(item);

View File

@ -10,3 +10,4 @@ ACTION_RETRY=Retry
ACTION_IGNORE=Ignore ACTION_IGNORE=Ignore
ACTION_OPEN=Open ACTION_OPEN=Open
ACTION_SAVE=Save ACTION_SAVE=Save
ACTION_DISCARD_CHANGES=Discard changes

View File

@ -111,6 +111,7 @@
> >
</style> </style>
<style id="TAB_UP_BUTTON_DARK" <style id="TAB_UP_BUTTON_DARK"
padding="5,1,1,1"
backgroundImageId="tab_btn_dark_up" backgroundImageId="tab_btn_dark_up"
/> />
<style id="TAB_UP_BUTTON_DARK_TEXT" <style id="TAB_UP_BUTTON_DARK_TEXT"