settings editor - edit number; editors - change listener

This commit is contained in:
Vadim Lopatin 2015-03-04 13:28:08 +03:00
parent 9d52514295
commit cbf9b64b0c
4 changed files with 99 additions and 44 deletions

View File

@ -718,47 +718,6 @@ final class Setting {
return result;
}
static long parseLong(inout string v, long defValue = 0) {
int len = cast(int)v.length;
if (len == 0)
return defValue;
int sign = 1;
long value = 0;
int digits = 0;
for (int i = 0; i < len; i++) {
char ch = v[i];
if (ch == '-') {
if (i != 0)
return defValue;
sign = -1;
} else if (ch >= '0' && ch <= '9') {
digits++;
value = value * 10 + (ch - '0');
} else {
return defValue;
}
}
return digits > 0 ? (sign > 0 ? value : -value) : defValue;
}
static ulong parseULong(inout string v, ulong defValue = 0) {
int len = cast(int)v.length;
if (len == 0)
return defValue;
ulong value = 0;
int digits = 0;
for (int i = 0; i < len; i++) {
char ch = v[i];
if (ch >= '0' && ch <= '9') {
digits++;
value = value * 10 + (ch - '0');
} else {
return defValue;
}
}
return digits > 0 ? value : defValue;
}
/// read as long value
@property inout(long) integer() inout {
final switch(_type) with(SettingType) {
@ -1916,3 +1875,45 @@ final class Setting {
}
}
}
long parseLong(inout string v, long defValue = 0) {
int len = cast(int)v.length;
if (len == 0)
return defValue;
int sign = 1;
long value = 0;
int digits = 0;
for (int i = 0; i < len; i++) {
char ch = v[i];
if (ch == '-') {
if (i != 0)
return defValue;
sign = -1;
} else if (ch >= '0' && ch <= '9') {
digits++;
value = value * 10 + (ch - '0');
} else {
return defValue;
}
}
return digits > 0 ? (sign > 0 ? value : -value) : defValue;
}
ulong parseULong(inout string v, ulong defValue = 0) {
int len = cast(int)v.length;
if (len == 0)
return defValue;
ulong value = 0;
int digits = 0;
for (int i = 0; i < len; i++) {
char ch = v[i];
if (ch >= '0' && ch <= '9') {
digits++;
value = value * 10 + (ch - '0');
} else {
return defValue;
}
}
return digits > 0 ? value : defValue;
}

View File

@ -62,6 +62,46 @@ class CheckboxItem : SettingsItem {
}
}
class NumberEditItem : SettingsItem {
protected int _minValue;
protected int _maxValue;
protected int _defaultValue;
this(string id, UIString label, int minValue = int.max, int maxValue = int.max, int defaultValue = 0) {
super(id, label);
_minValue = minValue;
_maxValue = maxValue;
_defaultValue = defaultValue;
}
/// create setting widget
override Widget createWidget(Setting settings) {
HorizontalLayout res = new HorizontalLayout(_id);
TextWidget lbl = new TextWidget(_id ~ "-label", _label);
EditLine ed = new EditLine(_id ~ "-edit", _label);
Setting setting = settings.settingByPath(_id, SettingType.STRING);
int n = cast(int)setting.integerDef(_defaultValue);
if (_minValue != int.max && n < _minValue)
n = _minValue;
if (_maxValue != int.max && n > _maxValue)
n = _maxValue;
setting.integer = cast(long)n;
ed.text = toUTF32(to!string(n));
ed.onContentChangeListener = delegate(EditableContent content) {
long v = parseLong(toUTF8(content.text), long.max);
if (v != long.max) {
if ((_minValue == int.max || v >= _minValue) && (_maxValue == int.max || v <= _maxValue)) {
setting.integer = v;
ed.textColor = 0x000000;
} else {
ed.textColor = 0xFF0000;
}
}
};
res.addChild(lbl);
res.addChild(ed);
return res;
}
}
/// settings page - item of settings tree, can edit several settings
class SettingsPage {
protected SettingsPage _parent;
@ -119,6 +159,13 @@ class SettingsPage {
return res;
}
/// add EditLine to edit number
NumberEditItem addNumberEdit(string id, UIString label, int minValue = int.max, int maxValue = int.max, int defaultValue = 0) {
NumberEditItem res = new NumberEditItem(id, label, minValue, maxValue, defaultValue);
addItem(res);
return res;
}
/// create page widget (default implementation creates empty page)
Widget createWidget(Setting settings) {
VerticalLayout res = new VerticalLayout(_id);
@ -192,7 +239,7 @@ class SettingsDialog : Dialog {
_tree.fontSize = 16;
_frame = new FrameLayout("prop_pages");
_frame.styleId = STYLE_SETTINGS_PAGES;
_frame.minHeight(200).minWidth(100).layoutHeight(FILL_PARENT).layoutHeight(FILL_PARENT);
_frame.layoutHeight(FILL_PARENT).layoutHeight(FILL_PARENT).minHeight(200).minWidth(100);
createControls(_layout, _tree.items);
HorizontalLayout content = new HorizontalLayout("settings_dlg_content");
content.addChild(_tree);
@ -202,7 +249,6 @@ class SettingsDialog : Dialog {
addChild(createButtonsPanel([ACTION_APPLY, ACTION_CANCEL], 0, 0));
if (_layout.childCount > 0)
_tree.selectItem(_layout.child(0).id);
}
}

View File

@ -43,6 +43,11 @@ interface ModifiedStateListener {
void onModifiedStateChange(Widget source, bool modified);
}
/// Modified content listener
interface EditableContentChangeListener {
void onEditableContentChanged(EditableContent source);
}
/// Editor action codes
enum EditorActions : int {
None = 0,
@ -235,6 +240,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
/// Modified state change listener
Signal!ModifiedStateListener onModifiedStateChangeListener;
Signal!EditableContentChangeListener onContentChangeListener;
/// override to support modification of client rect after change, e.g. apply offset
override protected void handleClientRectLayout(ref Rect rc) {
@ -690,6 +696,9 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
onModifiedStateChangeListener(this, content.modified);
requestActionsUpdate();
}
}
if (onContentChangeListener.assigned) {
onContentChangeListener(_content);
}
return;
}

View File

@ -371,7 +371,6 @@
<style id="SETTINGS_PAGES"
margins="0,4,4,4"
padding="4,4,4,4"
backgroundColor="#80FFFFFF"
layoutWidth="FILL_PARENT"
layoutWeight="5"
/>