001package org.vishia.gral.swt;
002
003import org.eclipse.swt.SWT;
004import org.vishia.util.KeyCode;
005
006/**Adaption from SWT-KeyCodes to universal vishia key codes.
007 * @author Hartmut Schorrig
008 *
009 */
010public class SwtGralKey extends KeyCode
011{
012  
013  /**Version, history and license
014   * <ul>
015   * <li>2015-08-29 Hartmut chg: Mouse handling: mouse buttons: The usual right mouse button is the Button2. The middle button is number 3.
016   * <li>2015-08-29 Hartmut chg: {@link #convertFromSwt(int, int, char)} now uses the character key information of the event too.
017   *   Elsewhere it is not possible to use the keyboard layout from the operation system to detect keys with sh. 
018   *   All keys with shift, which are character keys, are returned without {@link KeyCode#shift} designation now.
019   * <li>2013-11-16 Hartmut new {@link #convertMouseKey(int, MouseAction, int)}
020   * <li>2011-10-02 Hartmut created for Key handling in the GRAL. 
021   * </ul>
022   * 
023   * <b>Copyright/Copyleft</b>:
024   * For this source the LGPL Lesser General Public License,
025   * published by the Free Software Foundation is valid.
026   * It means:
027   * <ol>
028   * <li> You can use this source without any restriction for any desired purpose.
029   * <li> You can redistribute copies of this source to everybody.
030   * <li> Every user of this source, also the user of redistribute copies
031   *    with or without payment, must accept this license for further using.
032   * <li> But the LPGL is not appropriate for a whole software product,
033   *    if this source is only a part of them. It means, the user
034   *    must publish this part of source,
035   *    but don't need to publish the whole source of the own product.
036   * <li> You can study and modify (improve) this source
037   *    for own using or for redistribution, but you have to license the
038   *    modified sources likewise under this LGPL Lesser General Public License.
039   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
040   * </ol>
041   * If you are intent to use this sources without publishing its usage, you can get
042   * a second license subscribing a special contract with the author. 
043   * 
044   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
045   * 
046   */
047  @SuppressWarnings("hiding")
048  public final static int version = 20131116;
049
050  /**This class isn't instantiated. Only the static method and the static definitions of KeyCode
051   * are used.  */
052  private SwtGralKey()
053  { super(0);
054  }
055  
056  public static int convertFromSwt(int keyCode, int stateMask, char characterKey)
057  {
058    final int stateKeys, key;    
059    if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.CONTROL + SWT.ALT + SWT.SHIFT){
060      stateKeys = ctrl + alt + shift;
061    } else if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.ALT + SWT.CONTROL){
062      stateKeys = ctrl + alt;
063    } else if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.CONTROL + SWT.SHIFT){
064      stateKeys = ctrl + shift;
065    } else if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.ALT + SWT.SHIFT){
066      stateKeys = alt + shift;
067    } else if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.CONTROL){
068      stateKeys = ctrl;
069    } else if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.ALT){
070      stateKeys = alt;
071    } else if((stateMask & (SWT.CONTROL + SWT.ALT + SWT.SHIFT)) == SWT.SHIFT){
072      stateKeys = shift;
073    } else{
074      stateKeys = 0;
075    } 
076    switch(keyCode){
077      case SWT.ARROW_LEFT:  key = left; break;
078      case SWT.ARROW_RIGHT: key = right; break;
079      case SWT.ARROW_UP:    key = up; break;
080      case SWT.ARROW_DOWN:  key = dn; break;
081      case SWT.PAGE_UP:     key = pgup; break;
082      case SWT.PAGE_DOWN:   key = pgdn; break;
083      case SWT.F1:          key = F1; break;
084      case SWT.F2:          key = F2; break;
085      case SWT.F3:          key = F3; break;
086      case SWT.F4:          key = F4; break;
087      case SWT.F5:          key = F5; break;
088      case SWT.F6:          key = F6; break;
089      case SWT.F7:          key = F7; break;
090      case SWT.F8:          key = F8; break;
091      case SWT.F9:          key = F9; break;
092      case SWT.F10:         key = F10; break;
093      case SWT.F11:         key = F11; break;
094      case SWT.F12:         key = F12; break;
095      case SWT.HOME:        key = home; break;
096      case SWT.END:         key = end; break;
097      case SWT.INSERT:      key = ins; break;
098      case SWT.DEL:         key = del; break;
099      case SWT.TAB:         key = tab; break;
100      case 0x0d:            key = enter; break;
101      case 0x08:            key = back; break;
102      case 0x1b:            key = esc; break;
103      default:              key = keyCode;
104    }
105    if(stateKeys == shift && characterKey >= ' ') {
106      return characterKey;
107    } else {
108      return stateKeys + key;
109    }
110  }
111  
112  
113  
114  public enum MouseAction{
115    down, up, upMovedOutside, doubleClick
116  };
117  
118  
119  public static int convertMouseKey(int button, MouseAction action, int stateMask){
120    final int keyMouse;
121    switch(action){
122      case down:{
123        switch(button){ 
124          case 1: keyMouse = KeyCode.mouse1Down; break; 
125          case 3: keyMouse = KeyCode.mouse2Down; break;
126          case 2: keyMouse = KeyCode.mouse3Down; break;
127          default: keyMouse = KeyCode.mouse3Down; break;  //other key
128        }
129
130      } break;
131      case up:{
132        switch(button){ 
133          case   1: keyMouse = KeyCode.mouse1Up; break; 
134          case   3: keyMouse = KeyCode.mouse2Up; break;
135          case   2: keyMouse = KeyCode.mouse3Up; break;
136          default: keyMouse = KeyCode.mouse3Up; break;  //other key
137        }
138        
139      } break;
140      case upMovedOutside:{
141        switch(button){ 
142          case   1: keyMouse = KeyCode.mouse1UpMoved; break; 
143          case   3: keyMouse = KeyCode.mouse2UpMoved; break;
144          case   2: keyMouse = KeyCode.mouse3Up; break;
145          default: keyMouse = KeyCode.mouse3Up; break;  //other key
146        }
147        
148      } break;
149      case doubleClick:{
150        switch(button){ 
151          case 1: keyMouse = KeyCode.mouse1Double; break; 
152          case 3: keyMouse = KeyCode.mouse2Double; break;
153          case 2: keyMouse = KeyCode.mouse3Down; break;
154          default: keyMouse = KeyCode.mouse3Down; break;  //other key
155        }
156        
157      } break;
158      default: keyMouse = 0;  //unused, only because compiling error
159    }
160    final int keyCode = SwtGralKey.convertFromSwt(keyMouse, stateMask, '\0');
161    return keyCode;
162  }
163  
164  
165}