r20517: re-add cleaned-up webapps
[kai/samba.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / source / class / qx / ui / form / Button.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
9
10    License:
11      LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
12
13    Authors:
14      * Sebastian Werner (wpbasti)
15      * Andreas Ecker (ecker)
16
17 ************************************************************************ */
18
19 /* ************************************************************************
20
21 #module(ui_form)
22
23 ************************************************************************ */
24
25 qx.OO.defineClass("qx.ui.form.Button", qx.ui.basic.Atom,
26 function(vText, vIcon, vIconWidth, vIconHeight, vFlash)
27 {
28   // ************************************************************************
29   //   INIT
30   // ************************************************************************
31   qx.ui.basic.Atom.call(this, vText, vIcon, vIconWidth, vIconHeight, vFlash);
32
33   // Make focusable
34   this.setTabIndex(1);
35
36
37   // ************************************************************************
38   //   MOUSE EVENTS
39   // ************************************************************************
40   this.addEventListener("mouseover", this._onmouseover);
41   this.addEventListener("mouseout", this._onmouseout);
42   this.addEventListener("mousedown", this._onmousedown);
43   this.addEventListener("mouseup", this._onmouseup);
44
45
46   // ************************************************************************
47   //   KEY EVENTS
48   // ************************************************************************
49   this.addEventListener("keydown", this._onkeydown);
50   this.addEventListener("keyup", this._onkeyup);
51 });
52
53 qx.OO.changeProperty({ name : "appearance", type : "string", defaultValue : "button" });
54
55
56
57 /*
58 ---------------------------------------------------------------------------
59   EVENT HANDLER
60 ---------------------------------------------------------------------------
61 */
62
63 qx.Proto._onmouseover = function(e)
64 {
65   if (e.getTarget() != this) {
66     return;
67   }
68
69   if (this.hasState("abandoned"))
70   {
71     this.removeState("abandoned");
72     this.addState("pressed");
73   }
74
75   this.addState("over");
76 }
77
78 qx.Proto._onmouseout = function(e)
79 {
80   if (e.getTarget() != this) {
81     return;
82   }
83
84   this.removeState("over");
85
86   if (this.hasState("pressed"))
87   {
88     // Activate capturing if the button get a mouseout while
89     // the button is pressed.
90     this.setCapture(true);
91
92     this.removeState("pressed");
93     this.addState("abandoned");
94   }
95 }
96
97 qx.Proto._onmousedown = function(e)
98 {
99   if (e.getTarget() != this || !e.isLeftButtonPressed()) {
100     return;
101   }
102
103   this.removeState("abandoned");
104   this.addState("pressed");
105 }
106
107 qx.Proto._onmouseup = function(e)
108 {
109   this.setCapture(false);
110
111   // We must remove the states before executing the command
112   // because in cases were the window lost the focus while
113   // executing we get the capture phase back (mouseout).
114   var hasPressed = this.hasState("pressed");
115   var hasAbandoned = this.hasState("abandoned");
116
117   if (hasPressed) {
118     this.removeState("pressed");
119   }
120
121   if (hasAbandoned) {
122     this.removeState("abandoned");
123   }
124
125   if (!hasAbandoned)
126   {
127     this.addState("over");
128
129     if (hasPressed) {
130       this.execute();
131     }
132   }
133 }
134
135 qx.Proto._onkeydown = function(e)
136 {
137   switch(e.getKeyIdentifier())
138   {
139     case "Enter":
140     case "Space":
141       this.removeState("abandoned");
142       this.addState("pressed");
143   }
144 }
145
146 qx.Proto._onkeyup = function(e)
147 {
148   switch(e.getKeyIdentifier())
149   {
150     case "Enter":
151     case "Space":
152       if (this.hasState("pressed"))
153       {
154         this.removeState("abandoned");
155         this.removeState("pressed");
156         this.execute();
157       }
158   }
159 }
160
161
162
163
164
165
166
167
168 /*
169 ---------------------------------------------------------------------------
170   DISPOSER
171 ---------------------------------------------------------------------------
172 */
173
174 qx.Proto.dispose = function()
175 {
176   if (this.getDisposed()) {
177     return;
178   }
179
180   // ************************************************************************
181   //   MOUSE EVENTS
182   // ************************************************************************
183   this.removeEventListener("mouseover", this._onmouseover, this);
184   this.removeEventListener("mouseout", this._onmouseout, this);
185   this.removeEventListener("mousedown", this._onmousedown, this);
186   this.removeEventListener("mouseup", this._onmouseup, this);
187
188
189   // ************************************************************************
190   //   KEY EVENTS
191   // ************************************************************************
192   this.removeEventListener("keydown", this._onkeydown, this);
193   this.removeEventListener("keyup", this._onkeyup, this);
194
195
196   // ************************************************************************
197   //   SUPER CLASS
198   // ************************************************************************
199   return qx.ui.basic.Atom.prototype.dispose.call(this);
200 }