menus, part 1

This commit is contained in:
Vadim Lopatin 2014-04-11 18:24:31 +04:00
parent 6e7d9b7cd8
commit dce7eec7a8
6 changed files with 137 additions and 1 deletions

View File

@ -328,6 +328,7 @@
<File path="src\dlangui\widgets\controls.d" />
<File path="src\dlangui\widgets\layouts.d" />
<File path="src\dlangui\widgets\lists.d" />
<File path="src\dlangui\widgets\menu.d" />
<File path="src\dlangui\widgets\styles.d" />
<File path="src\dlangui\widgets\tabs.d" />
<File path="src\dlangui\widgets\widget.d" />

View File

@ -48,6 +48,15 @@ extern (C) int UIAppMain(string[] args) {
Window window = Platform.instance().createWindow("My Window", null);
static if (true) {
VerticalLayout contentLayout = new VerticalLayout();
MenuItem mainMenuItems = new MenuItem();
mainMenuItems.add(new Action(1, "File"d));
mainMenuItems.add(new Action(2, "Edit"d));
mainMenuItems.add(new Action(3, "Window"d));
mainMenuItems.add(new Action(4, "Help"d));
MainMenu mainMenu = new MainMenu(mainMenuItems);
contentLayout.addChild(mainMenu);
TabWidget tabs = new TabWidget("TABS");
tabs.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);
@ -117,7 +126,8 @@ extern (C) int UIAppMain(string[] args) {
tabs.selectTab("tab1");
window.mainWidget = tabs;
contentLayout.addChild(tabs);
window.mainWidget = contentLayout;
} else {
window.mainWidget = (new Button()).text("sample button");
}

View File

@ -9,5 +9,6 @@ public import dlangui.widgets.controls;
public import dlangui.widgets.layouts;
public import dlangui.widgets.lists;
public import dlangui.widgets.tabs;
public import dlangui.widgets.menu;
public import dlangui.graphics.fonts;
public import dlangui.core.i18n;

View File

@ -1,7 +1,55 @@
module dlangui.core.events;
import dlangui.core.i18n;
import std.conv;
/// UI action
class Action {
protected int _id;
protected UIString _label;
protected string _iconId;
this(int id, string labelResourceId, string iconResourceId = null) {
_id = id;
_label = labelResourceId;
_iconId = iconResourceId;
}
this(int id, dstring label, string iconResourceId = null) {
_id = id;
_label = label;
_iconId = iconResourceId;
}
@property int id() const {
return _id;
}
@property Action id(int newId) {
_id = newId;
return this;
}
@property Action label(string resourceId) {
_label = resourceId;
return this;
}
@property Action label(dstring text) {
_label = text;
return this;
}
@property dstring label() const {
return _label.value;
}
@property ref const (UIString) labelValue() const {
return _label;
}
@property string iconId() const {
return _iconId;
}
@property Action iconId(string id) {
_iconId = id;
return this;
}
}
enum MouseAction : ubyte {
Cancel, // button down handling is cancelled
ButtonDown, // button is down

View File

@ -0,0 +1,71 @@
module dlangui.widgets.menu;
import dlangui.core.events;
import dlangui.widgets.controls;
import dlangui.widgets.layouts;
class MenuItem {
bool _checkable;
bool _checked;
bool _enabled;
protected Action _action;
MenuItem[] _subitems;
@property int subitemCount() {
return cast(int)_subitems.length;
}
MenuItem subitem(int index) {
return _subitems[index];
}
MenuItem add(MenuItem subitem) {
_subitems ~= subitem;
return this;
}
MenuItem add(Action subitemAction) {
_subitems ~= new MenuItem(subitemAction);
return this;
}
@property bool isSubmenu() {
return _subitems.length > 0;
}
@property UIString label() {
return _action.labelValue;
}
@property const(Action) action() const { return _action; }
@property MenuItem action(Action a) { _action = a; return this; }
this() {
_enabled = true;
}
this(Action action) {
_action = action;
_enabled = true;
}
~this() {
// TODO
}
}
class MenuItemWidget : HorizontalLayout {
protected MenuItem _item;
protected TextWidget _label;
this(MenuItem item) {
_item = item;
styleId = "MENU_ITEM";
_label = new TextWidget("MENU_LABEL");
_label.text = _item.label;
addChild(_label);
trackHover = true;
}
}
class MainMenu : HorizontalLayout {
MenuItem _item;
this(MenuItem item) {
id = "MAIN_MENU";
styleId = "MAIN_MENU";
_item = item;
for (int i = 0; i < item.subitemCount; i++) {
addChild(new MenuItemWidget(item.subitem(i)));
}
addChild((new Widget()).layoutWidth(FILL_PARENT));
}
}

View File

@ -662,6 +662,11 @@ Theme createDefaultTheme() {
tabWidget.padding(Rect(3,3,3,3)).backgroundColor(0xEEEEEE);
//tabWidget.backgroundImageId("frame_blue");
//res.dumpStats();
Style mainMenu = res.createSubstyle("MAIN_MENU").backgroundColor(0xE0E0E0).layoutWidth(FILL_PARENT);
Style menuItem = res.createSubstyle("MENU_ITEM").padding(Rect(4,2,4,2)).backgroundColor(0xE0E0E0) ;
menuItem.createState(State.Hovered, State.Hovered).backgroundColor(0x80E0E000);
return res;
}