dropdown fixes

This commit is contained in:
Adam D. Ruppe 2022-03-07 16:24:57 -05:00
parent 8ee2fb129e
commit 82b91d2af9
1 changed files with 52 additions and 6 deletions

View File

@ -2141,20 +2141,38 @@ abstract class ComboboxBase : Widget {
} }
else static assert(false); else static assert(false);
private string[] options; /++
Returns the current list of options in the selection.
History:
Property accessor added March 1, 2022 (dub v10.7). Prior to that, it was private.
+/
final @property string[] options() const {
return options_;
}
private string[] options_;
private int selection_ = -1; private int selection_ = -1;
/++
Adds an option to the end of options array.
+/
void addOption(string s) { void addOption(string s) {
options ~= s; options_ ~= s;
version(win32_widgets) version(win32_widgets)
SendMessageW(hwnd, 323 /*CB_ADDSTRING*/, 0, cast(LPARAM) toWstringzInternal(s)); SendMessageW(hwnd, 323 /*CB_ADDSTRING*/, 0, cast(LPARAM) toWstringzInternal(s));
} }
/++
Gets the current selection as an index into the [options] array. Returns -1 if nothing is selected.
+/
int getSelection() { int getSelection() {
return selection_; return selection_;
} }
/++ /++
Returns the current selection as a string.
History: History:
Added November 17, 2021 Added November 17, 2021
+/ +/
@ -2162,19 +2180,47 @@ abstract class ComboboxBase : Widget {
return selection_ == -1 ? null : options[selection_]; return selection_ == -1 ? null : options[selection_];
} }
void setSelection(int idx) { /++
Sets the current selection to an index in the options array, or to the given option if present.
Please note that the string version may do a linear lookup.
Returns:
the index you passed in
History:
The `string` based overload was added on March 1, 2022 (dub v10.7).
The return value was `void` prior to March 1, 2022.
+/
int setSelection(int idx) {
selection_ = idx; selection_ = idx;
version(win32_widgets) version(win32_widgets)
SendMessageW(hwnd, 334 /*CB_SETCURSEL*/, idx, 0); SendMessageW(hwnd, 334 /*CB_SETCURSEL*/, idx, 0);
auto t = new SelectionChangedEvent(this, selection_, selection_ == -1 ? null : options[selection_]); auto t = new SelectionChangedEvent(this, selection_, selection_ == -1 ? null : options[selection_]);
t.dispatch(); t.dispatch();
return idx;
} }
static class SelectionChangedEvent : Event { /// ditto
enum EventString = "change"; void setSelection(string s) {
if(s !is null)
foreach(idx, item; options)
if(item == s) {
return setSelection(cast(int) idx);
}
return setSelection(-1);
}
/++
This event is fired when the selection changes. Note it inherits
from ChangeEvent!string, meaning you can use that as well, and it also
fills in [Event.intValue].
+/
static class SelectionChangedEvent : ChangeEvent!string {
this(Widget target, int iv, string sv) { this(Widget target, int iv, string sv) {
super("change", target); super(target, &stringValue);
this.iv = iv; this.iv = iv;
this.sv = sv; this.sv = sv;
} }