1 /* ************************************************************************
3 qooxdoo - the new era of web development
8 2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
11 LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
14 * Sebastian Werner (wpbasti)
15 * Andreas Ecker (ecker)
16 * Fabian Jakobs (fjakobs)
18 ************************************************************************ */
20 /* ************************************************************************
24 ************************************************************************ */
27 * This contains a command with shortcut.
29 * Each command could be assigned to multiple widgets.
31 * @event execute {qx.event.type.DataEvent} when the command is executed.
33 * @param vShortcut (string) shortcuts can be composed of optional modifier
34 * keys Control, Alt, Shift, Meta and a non modifier key.
35 * If no non modifier key is specified, the second paramater is evaluated.
36 * The key must be seperated by a ''+'' or ''-'' character.
37 * Examples: Alt+F1, Control+C, Control+Alt+Enf
39 * @param vKeyCodeOrIdentifier (int) Additional key of the command. It is interpreted as a
40 * keyIdentifier if it is given as integer. Otherwhise it is interpreted as keyCode.
42 qx.OO.defineClass("qx.client.Command", qx.core.Target,
43 function(vShortcut, vKeyCodeOrIdentifier)
45 qx.core.Target.call(this);
50 if (qx.util.Validation.isValid(vShortcut)) {
51 this.setShortcut(vShortcut);
54 if (qx.util.Validation.isValid(vKeyCodeOrIdentifier))
56 if (qx.util.Validation.isValidString(vKeyCodeOrIdentifier))
58 this.setKeyIdentifier(vKeyCodeOrIdentifier);
60 else if (qx.util.Validation.isValidNumber(vKeyCodeOrIdentifier))
62 this.warn("The use of keyCode in command is deprecated. Use keyIdentifier instead.");
63 this.setKeyCode(vKeyCodeOrIdentifier);
67 var msg = "vKeyCodeOrIdentifier must be of type string or number: " + vKeyCodeOrIdentifier;
73 // OSX warning for Alt key combinations
74 if (this._modifier.Alt && this._key && this._key.length == 1) {
76 (this._key >= "A" && this._key <= "Z") ||
77 (this._key >= "0" && this._key <= "9")
79 this.warn("A shortcut containing Alt and a letter or number will not work under OS X!");
82 qx.event.handler.EventHandler.getInstance().addCommand(this);
86 /** the command shortcut */
87 qx.OO.addProperty({ name : "shortcut", type : "string" });
90 * keyCode (Deprecated)
91 * Still there for compatibility with the old key handler/commands
93 qx.OO.addProperty({ name : "keyCode", type : "number" });
96 qx.OO.addProperty({ name : "keyIdentifier", type : "string" });
101 ---------------------------------------------------------------------------
103 ---------------------------------------------------------------------------
107 * Fire the "execute" event on this command.
109 * @param vTarget (Object)
111 qx.Proto.execute = function(vTarget)
113 if (this.hasEventListeners("execute")) {
114 this.dispatchEvent(new qx.event.type.DataEvent("execute", vTarget), true);
123 ---------------------------------------------------------------------------
125 ---------------------------------------------------------------------------
128 qx.Proto._modifyShortcut = function(propValue, propOldValue, propData)
135 // split string to get each key which must be pressed
136 // build a hash with active keys
137 var a = propValue.split(/[-+\s]+/);
140 for (var i=0; i<al; i++)
142 var identifier = qx.event.handler.KeyEventHandler.getInstance().oldKeyNameToKeyIdentifier(a[i]);
150 this._modifier[identifier] = true;
154 var msg = "Not a valid key name for a command: " + a[i];
160 var msg = "You can only specify one non modifier key!";
164 this._key = identifier;
174 ---------------------------------------------------------------------------
175 INTERNAL MATCHING LOGIC
176 ---------------------------------------------------------------------------
180 * Checks wether the given key event matches the command's shortcut
182 * @param e (qx.event.type.KeyEvent) the key event object
183 * @return (boolean) wether the commands shortcut matches the key event
185 qx.Proto._matchesKeyEvent = function(e)
187 var key = this._key || this.getKeyIdentifier();
188 if (!key && !this.getKeyCode()) {
189 // no shortcut defined.
193 // pre-check for check special keys
194 // we handle this here to omit to check this later again.
196 (this._modifier.Shift && !e.getShiftKey()) ||
197 (this._modifier.Control && !e.getCtrlKey()) ||
198 // (this._modifier.Meta && !e.getCtrlKey()) ||
199 (this._modifier.Alt && !e.getAltKey())
206 if (key == e.getKeyIdentifier()) {
212 if (this.getKeyCode() == e.getKeyCode()) {
223 ---------------------------------------------------------------------------
225 ---------------------------------------------------------------------------
229 * Returns the shortcut as string
231 * @return (string) shortcut
233 qx.Proto.toString = function()
235 var vShortcut = this.getShortcut();
236 var vKeyCode = this.getKeyCode();
238 var vKeyIdentifier = this._key || this.getKeyIdentifier();
241 if (qx.util.Validation.isValidString(vKeyIdentifier))
243 vKeyString = vKeyIdentifier;
245 else if (qx.util.Validation.isValidNumber(vKeyCode))
247 var vTemp = qx.event.type.KeyEvent.codes[vKeyCode];
248 vKeyString = vTemp ? qx.lang.String.toFirstUp(vTemp) : String(vKeyCode);
251 if (qx.util.Validation.isValidString(vShortcut))
253 vString = vShortcut + "+" + vKeyString;
255 else if (qx.util.Validation.isValidNumber(vKeyCode))
257 vString = vKeyString;
266 ---------------------------------------------------------------------------
268 ---------------------------------------------------------------------------
274 qx.Proto.dispose = function()
276 if (this.getDisposed()) {
280 this._shortcutParts = null;
282 var vMgr = qx.event.handler.EventHandler.getInstance();
284 vMgr.removeCommand(this);
287 return qx.core.Target.prototype.dispose.call(this);