Skip to content

Commit

Permalink
StringListAdapter implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
buggins committed Dec 15, 2014
1 parent dbac501 commit df8d271
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 9 deletions.
4 changes: 3 additions & 1 deletion dlanguilib.visualdproj
Expand Up @@ -66,7 +66,7 @@
<debuglevel>0</debuglevel>
<debugids />
<versionlevel>0</versionlevel>
<versionids />
<versionids>USE_SDL USE_OPENGL</versionids>
<dump_source>0</dump_source>
<mapverbosity>0</mapverbosity>
<createImplib>1</createImplib>
Expand Down Expand Up @@ -160,6 +160,7 @@
<debuglevel>0</debuglevel>
<debugids />
<versionlevel>0</versionlevel>
<versionids />
<dump_source>0</dump_source>
<mapverbosity>0</mapverbosity>
<createImplib>0</createImplib>
Expand Down Expand Up @@ -327,6 +328,7 @@
</Folder>
</Folder>
<Folder name="widgets">
<File path="src\dlangui\widgets\combobox.d" />
<File path="src\dlangui\widgets\controls.d" />
<File path="src\dlangui\widgets\editors.d" />
<File path="src\dlangui\widgets\grid.d" />
Expand Down
2 changes: 1 addition & 1 deletion examples/example1/example1.visualdproj
Expand Up @@ -66,7 +66,7 @@
<debuglevel>0</debuglevel>
<debugids />
<versionlevel>0</versionlevel>
<versionids />
<versionids>USE_SDL USE_OPENGL</versionids>
<dump_source>0</dump_source>
<mapverbosity>3</mapverbosity>
<createImplib>0</createImplib>
Expand Down
31 changes: 27 additions & 4 deletions examples/example1/src/main.d
Expand Up @@ -375,10 +375,24 @@ extern (C) int UIAppMain(string[] args) {
tabs.addTab(layout, "Tab 1"d);

static if (true) {
ListWidget list = new ListWidget("tab2", Orientation.Vertical);
// two long lists
// left one is list with widgets as items
// right one is list with string list adapter
HorizontalLayout longLists = new HorizontalLayout("tab2");
longLists.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);

ListWidget list = new ListWidget("list1", Orientation.Vertical);
list.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);

StringListAdapter stringList = new StringListAdapter();
WidgetListAdapter listAdapter = new WidgetListAdapter();
for (int i = 0; i < 1000; i++)
listAdapter.widgets.add((new TextWidget()).text("List item "d ~ to!dstring(i)).styleId("LIST_ITEM"));
listAdapter.widgets.add((new TextWidget()).text("This is a list of widgets"d).styleId("LIST_ITEM"));
stringList.items.add("This is a list of strings from StringListAdapter"d);
for (int i = 1; i < 1000; i++) {
dstring label = "List item "d ~ to!dstring(i);
listAdapter.widgets.add((new TextWidget()).text("Widget list - "d ~ label).styleId("LIST_ITEM"));
stringList.items.add("Simple string - "d ~ label);
}
list.ownAdapter = listAdapter;
listAdapter.resetItemState(0, State.Enabled);
listAdapter.resetItemState(5, State.Enabled);
Expand All @@ -388,7 +402,16 @@ extern (C) int UIAppMain(string[] args) {
assert(list.itemEnabled(6) == true);
list.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);
list.selectItem(0);
tabs.addTab(list, "TAB_LONG_LIST"c);

longLists.addChild(list);

ListWidget list2 = new ListWidget("list2");
list2.ownAdapter = stringList;
list2.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);
list2.selectItem(0);
longLists.addChild(list2);

tabs.addTab(longLists, "TAB_LONG_LIST"c);
}

{
Expand Down
115 changes: 112 additions & 3 deletions src/dlangui/core/i18n.d
Expand Up @@ -95,9 +95,112 @@ struct UIString {
alias value this;
}

shared UIStringTranslator i18n;
shared static this() {
i18n = new shared UIStringTranslator();
/** UIString item collection */
struct UIStringCollection {
private UIString[] _items;
private int _length;

/** returns number of items */
@property int length() { return _length; }
/** slice */
UIString[] opIndex() {
return _items[0 .. _length];
}
/** slice */
UIString[] opSlice() {
return _items[0 .. _length];
}
/** slice */
UIString[] opSlice(size_t start, size_t end) {
return _items[start .. end];
}
/** read item by index */
UIString opIndex(size_t index) {
return _items[index];
}
/** modify item by index */
UIString opIndexAssign(UIString value, size_t index) {
_items[index] = value;
return _items[index];
}
/** return unicode string for item by index */
dstring get(size_t index) {
return _items[index].value;
}
/** Assign UIStringCollection */
void opAssign(ref UIStringCollection items) {
clear();
addAll(items);
}
/** Append UIStringCollection */
void addAll(ref UIStringCollection items) {
foreach (UIString item; items) {
add(item);
}
}
/** Assign array of string resource IDs */
void opAssign(string[] items) {
clear();
addAll(items);
}
/** Append array of string resource IDs */
void addAll(string[] items) {
foreach (string item; items) {
add(item);
}
}
/** Assign array of unicode strings */
void opAssign(dstring[] items) {
clear();
addAll(items);
}
/** Append array of unicode strings */
void addAll(dstring[] items) {
foreach (dstring item; items) {
add(item);
}
}
/** remove all items */
void clear() {
_items.length = 0;
_length = 0;
}
/** Insert resource id item into specified position */
void add(string item, int index = -1) {
UIString s;
s = item;
add(s, index);
}
/** Insert unicode string item into specified position */
void add(dstring item, int index = -1) {
UIString s;
s = item;
add(s, index);
}
/** Insert UIString item into specified position */
void add(UIString item, int index = -1) {
if (index < 0 || index > _length)
index = _length;
if (_items.length < _length + 1) {
if (_items.length < 8)
_items.length = 8;
else
_items.length = _items.length * 2;
}
for (size_t i = _length; i > index; i--) {
_items[i] = _items[i + 1];
}
_items[index] = item;
_length++;
}
/** Remove item with specified index */
void remove(int index) {
if (index < 0 || index >= _length)
return;
for (size_t i = index; i < _length - 1; i++)
_items[i] = _items[i + 1];
_length--;
}
}

synchronized class UIStringTranslator {
Expand Down Expand Up @@ -149,6 +252,7 @@ synchronized class UIStringTranslator {
}
return res;
}

/// translate string ID to string (returns "UNTRANSLATED: id" for missing values)
dstring get(string id) {
if (id is null)
Expand Down Expand Up @@ -233,3 +337,8 @@ private shared class UIStringList {
return res;
}
}

shared UIStringTranslator i18n;
shared static this() {
i18n = new shared UIStringTranslator();
}
58 changes: 58 additions & 0 deletions src/dlangui/widgets/combobox.d
@@ -0,0 +1,58 @@
module dlangui.widgets.combobox;

import dlangui.widgets.widget;
import dlangui.widgets.layouts;
import dlangui.widgets.editors;
import dlangui.widgets.lists;
import dlangui.widgets.controls;

class ComboBoxBase : HorizontalLayout, OnClickHandler {
protected Widget _body;
protected ImageButton _button;
protected ListAdapter _adapter;
protected bool _ownAdapter;
protected int _selectedItemIndex;

protected Widget createSelectedItemWidget() {
Widget res;
if (_adapter && _selectedItemIndex < _adapter.itemCount) {
res = _adapter.itemWidget(_selectedItemIndex);
res.id = "COMBOBOX_BODY";
} else {
res = new Widget("COMBOBOX_BODY");
}
res.layoutWidth = FILL_PARENT;
res.layoutHeight = WRAP_CONTENT;
return res;
}

override bool onClick(Widget source) {
// TODO
return true;
}

protected ImageButton createButton() {
ImageButton res = new ImageButton("COMBOBOX_BUTTON");
res.onClickListener = this;
return res;
}

this(string ID, ListAdapter adapter, bool ownAdapter = true) {
super(ID);
_adapter = adapter;
_ownAdapter = ownAdapter;
_body = createSelectedItemWidget();
_button = createButton();
addChild(_body);
addChild(_button);
}
}

class ComboBox : ComboBoxBase {
this(string ID, string[] items) {
super(ID, new StringListAdapter(items), true);
}
this(string ID, dstring[] items) {
super(ID, new StringListAdapter(items), true);
}
}
90 changes: 90 additions & 0 deletions src/dlangui/widgets/lists.d
Expand Up @@ -65,6 +65,96 @@ class WidgetListAdapter : ListAdapter {
}
}

/** List adapter providing strings only. */
class StringListAdapter : ListAdapter {
protected UIStringCollection _items;
protected uint[] _states;
protected TextWidget _widget;
protected int _lastItemIndex;

/** create empty string list adapter. */
this() {
_lastItemIndex = -1;
}

/** Init with array of string resource IDs. */
this(string[] items) {
_items.addAll(items);
_lastItemIndex = -1;
updateStatesLength();
}

/** Init with array of unicode strings. */
this(dstring[] items) {
_items.addAll(items);
_lastItemIndex = -1;
updateStatesLength();
}

/** Access to items collection. */
@property ref UIStringCollection items() { return _items; }

/// returns number of widgets in list
@property override int itemCount() {
return _items.length;
}

protected void updateStatesLength() {
if (_states.length < _items.length) {
int oldlen = _states.length;
_states.length = _items.length;
for (int i = oldlen; i < _items.length; i++)
_states[i] = State.Enabled;
}
}

/// return list item widget by item index
override Widget itemWidget(int index) {
updateStatesLength();
if (_widget is null) {
_widget = new TextWidget("LIST_ITEM");
_widget.styleId = "LIST_ITEM";
} else {
if (index == _lastItemIndex)
return _widget;
}
// update widget
_widget.text = _items.get(index);
_widget.state = _states[index];
_lastItemIndex = index;
return _widget;
}

/// return list item's state flags
override uint itemState(int index) {
updateStatesLength();
return _states[index];
}

/// set one or more list item's state flags, returns updated state
override uint setItemState(int index, uint flags) {
updateStatesLength();
_states[index] |= flags;
if (_widget !is null && _lastItemIndex == index)
_widget.state = _states[index];
return _states[index];
}
/// reset one or more list item's state flags, returns updated state
override uint resetItemState(int index, uint flags) {
updateStatesLength();
_states[index] &= ~flags;
if (_widget !is null && _lastItemIndex == index)
_widget.state = _states[index];
return _states[index];
}

~this() {
//Log.d("Destroying StringListAdapter");
if (_widget)
destroy(_widget);
}
}

/// List
class ListWidget : WidgetGroup, OnScrollHandler {
protected Orientation _orientation = Orientation.Vertical;
Expand Down
Empty file added travis.yml
Empty file.

0 comments on commit df8d271

Please sign in to comment.