ketmar changes

This commit is contained in:
Adam D. Ruppe 2017-04-19 22:58:50 -04:00
parent 1db72d6b18
commit 0f8df3f6cd
2 changed files with 66 additions and 25 deletions

View File

@ -570,6 +570,8 @@ class Action {
private static Action[int] mapping;
}
KeyEvent accelerator;
///
this(string label, ushort icon = 0, void delegate() triggered = null) {
this.label = label;
@ -3316,6 +3318,7 @@ class Window : Widget {
bool dispatchKeyEvent(KeyEvent ev) {
if(focusedWidget) {
auto event = new Event(ev.pressed ? "keydown" : "keyup", focusedWidget);
event.originalKeyEvent = ev;
event.character = ev.character;
event.key = ev.key;
event.state = ev.modifierState;
@ -3670,6 +3673,10 @@ class MainWindow : Window {
ushort correctIcon = 0; // FIXME
auto action = new Action(memberName, correctIcon, &__traits(getMember, t, memberName));
if(accelerator.keyString.length) {
action.accelerator = KeyEvent.parse(accelerator.keyString);
}
if(toolbar !is .toolbar.init)
toolbarActions ~= action;
if(menu !is .menu.init) {
@ -3696,6 +3703,15 @@ class MainWindow : Window {
auto tb = new ToolBar(toolbarActions, this);
}
void delegate()[string] accelerators;
override void defaultEventHandler_keydown(Event event) {
auto str = event.originalKeyEvent.toStr;
if(auto acl = str in accelerators)
(*acl)();
super.defaultEventHandler_keydown(event);
}
override void defaultEventHandler_mouseover(Event event) {
super.defaultEventHandler_mouseover(event);
if(this.statusBar !is null && event.target.statusTip.length)
@ -4523,6 +4539,10 @@ class MenuItem : MouseActivatedWidget {
painter.outlineColor = Color.black;
painter.fillColor = Color.transparent;
painter.drawText(Point(cast(MenuBar) this.parent ? 4 : 20, 2), label, Point(width, height), TextAlignment.Left);
if(action && action.accelerator !is KeyEvent.init) {
painter.drawText(Point(cast(MenuBar) this.parent ? 4 : 20, 2), action.accelerator.toStr(), Point(width, height), TextAlignment.Right);
}
}
@ -5458,6 +5478,8 @@ class Event {
// for key events
Key key; ///
KeyEvent originalKeyEvent;
// char character events
dchar character; ///
@ -6119,7 +6141,7 @@ http://msdn.microsoft.com/en-us/library/windows/desktop/bb760476%28v=vs.85%29.as
/// This item in the menu will be preceded by a separator line
struct seperator {}
/// Program-wide keyboard shortcut to trigger the action
struct accelerator { string acl; }
struct accelerator { string keyString; }
/// tells which menu the action will be on
struct menu { string name; }
/// Describes which toolbar section the action appears on

View File

@ -3650,7 +3650,7 @@ struct KeyEvent {
}
}
if (!this.key) return null;
if (!this.key && !(this.modifierState&(ModifierState.ctrl|ModifierState.alt|ModifierState.shift|ModifierState.windows))) return null;
// put modifiers
if (this.modifierState&ModifierState.ctrl) put("Ctrl+");
@ -3658,25 +3658,28 @@ struct KeyEvent {
if (this.modifierState&ModifierState.windows) put("Win+");
if (this.modifierState&ModifierState.shift) put("Shift+");
foreach (string kn; __traits(allMembers, Key)) {
if (this.key == __traits(getMember, Key, kn)) {
// HACK!
static if (kn == "N0") put("0");
else static if (kn == "N1") put("1");
else static if (kn == "N2") put("2");
else static if (kn == "N3") put("3");
else static if (kn == "N4") put("4");
else static if (kn == "N5") put("5");
else static if (kn == "N6") put("6");
else static if (kn == "N7") put("7");
else static if (kn == "N8") put("8");
else static if (kn == "N9") put("9");
else put(kn);
return dest[0..dpos];
if (this.key) {
foreach (string kn; __traits(allMembers, Key)) {
if (this.key == __traits(getMember, Key, kn)) {
// HACK!
static if (kn == "N0") put("0");
else static if (kn == "N1") put("1");
else static if (kn == "N2") put("2");
else static if (kn == "N3") put("3");
else static if (kn == "N4") put("4");
else static if (kn == "N5") put("5");
else static if (kn == "N6") put("6");
else static if (kn == "N7") put("7");
else static if (kn == "N8") put("8");
else static if (kn == "N9") put("9");
else put(kn);
return dest[0..dpos];
}
}
put("Unknown");
} else {
if (dpos && dest[dpos-1] == '+') --dpos;
}
put("Unknown");
return dest[0..dpos];
}
@ -3728,13 +3731,29 @@ struct KeyEvent {
KeyEvent res;
res.key = cast(Key)0; // just in case
const(char)[] tk, tkn; // last token
bool allowEmascStyle = true;
tokenloop: for (;;) {
auto tk = getToken();
if (tk is null) break;
if (strEquCI(tk, "C") || strEquCI(tk, "Ctrl")) { res.modifierState |= ModifierState.ctrl; continue tokenloop; }
if (strEquCI(tk, "M") || strEquCI(tk, "Alt")) { res.modifierState |= ModifierState.alt; continue tokenloop; }
if (strEquCI(tk, "H") || strEquCI(tk, "Win") || strEquCI(tk, "Windows")) { res.modifierState |= ModifierState.windows; continue tokenloop; }
if (strEquCI(tk, "S") || strEquCI(tk, "Shift")) { res.modifierState |= ModifierState.shift; continue tokenloop; }
tk = tkn;
tkn = getToken();
//k8: yay, i took "Bloody Mess" trait from Fallout!
if (tkn.length != 0 && tk.length == 0) { tk = tkn; continue tokenloop; }
if (tkn.length == 0 && tk.length == 0) break; // no more tokens
if (allowEmascStyle && tkn.length != 0) {
if (tkn.length == 1) {
if (strEquCI(tk, "C")) { res.modifierState |= ModifierState.ctrl; continue tokenloop; }
if (strEquCI(tk, "M")) { res.modifierState |= ModifierState.alt; continue tokenloop; }
if (strEquCI(tk, "H")) { res.modifierState |= ModifierState.windows; continue tokenloop; }
if (strEquCI(tk, "S")) { res.modifierState |= ModifierState.shift; continue tokenloop; }
} else {
allowEmascStyle = false;
}
}
if (strEquCI(tk, "Ctrl")) { allowEmascStyle = false; res.modifierState |= ModifierState.ctrl; continue tokenloop; }
if (strEquCI(tk, "Alt")) { allowEmascStyle = false; res.modifierState |= ModifierState.alt; continue tokenloop; }
if (strEquCI(tk, "Win") || strEquCI(tk, "Windows")) { allowEmascStyle = false; res.modifierState |= ModifierState.windows; continue tokenloop; }
if (strEquCI(tk, "Shift")) { allowEmascStyle = false; res.modifierState |= ModifierState.shift; continue tokenloop; }
if (tk.length == 0) continue;
// try key name
if (res.key == 0) {
// little hack