Debian Codegraph @

demo/browser/xbStyle.js

raw
1 /*
2 * xbStyle.js
3 * $Revision: 1.2 $ $Date: 2003/02/07 16:04:22 $
4 */
5
6 /* ***** BEGIN LICENSE BLOCK *****
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8 *
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
13 *
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
18 *
19 * The Original Code is Netscape code.
20 *
21 * The Initial Developer of the Original Code is
22 * Netscape Corporation.
23 * Portions created by the Initial Developer are Copyright (C) 2001
24 * the Initial Developer. All Rights Reserved.
25 *
26 * Contributor(s): Bob Clary <[email protected]>
27 *
28 * ***** END LICENSE BLOCK ***** */
29
30 function xbStyleNotSupported() {}
31
32 function xbStyleNotSupportStringValue(propname) { xbDEBUG.dump(propname + ' is not supported in this browser'); return '';};
33
34 /////////////////////////////////////////////////////////////
35 // xbClipRect
36
37 function xbClipRect(a1, a2, a3, a4)
38 {
39 this.top = 0;
40 this.right = 0;
41 this.bottom = 0;
42 this.left = 0;
43
44 if (typeof(a1) == 'string')
45 {
46 var val;
47 var ca;
48 var i;
49
50 if (a1.indexOf('rect(') == 0)
51 {
52 // I would have preferred [0-9]+[a-zA-Z]+ for a regexp
53 // but NN4 returns null for that.
54 ca = a1.substring(5, a1.length-1).match(/-?[0-9a-zA-Z]+/g);
55 for (i = 0; i < 4; ++i)
56 {
57 val = xbToInt(ca[i]);
58 if (val != 0 && ca[i].indexOf('px') == -1)
59 {
60 xbDEBUG.dump('xbClipRect: A clipping region ' + a1 + ' was detected that did not use pixels as units. Click Ok to continue, Cancel to Abort');
61 return;
62 }
63 ca[i] = val;
64 }
65 this.top = ca[0];
66 this.right = ca[1];
67 this.bottom = ca[2];
68 this.left = ca[3];
69 }
70 }
71 else if (typeof(a1) == 'number' && typeof(a2) == 'number' && typeof(a3) == 'number' && typeof(a4) == 'number')
72 {
73 this.top = a1;
74 this.right = a2;
75 this.bottom = a3;
76 this.left = a4;
77 }
78 }
79
80 xbClipRect.prototype.top = 0;
81 xbClipRect.prototype.right = 0;
82 xbClipRect.prototype.bottom = 0;
83 xbClipRect.prototype.left = 0;
84
85
86 function xbClipRectGetWidth()
87 {
88 return this.right - this.left;
89 }
90 xbClipRect.prototype.getWidth = xbClipRectGetWidth;
91
92 function xbClipRectSetWidth(width)
93 {
94 this.right = this.left + width;
95 }
96 xbClipRect.prototype.setWidth = xbClipRectSetWidth;
97
98 function xbClipRectGetHeight()
99 {
100 return this.bottom - this.top;
101 }
102 xbClipRect.prototype.getHeight = xbClipRectGetHeight;
103
104 function xbClipRectSetHeight(height)
105 {
106 this.bottom = this.top + height;
107 }
108 xbClipRect.prototype.setHeight = xbClipRectSetHeight;
109
110 function xbClipRectToString()
111 {
112 return 'rect(' + this.top + 'px ' + this.right + 'px ' + this.bottom + 'px ' + this.left + 'px )' ;
113 }
114 xbClipRect.prototype.toString = xbClipRectToString;
115
116 /////////////////////////////////////////////////////////////
117 // xbStyle
118 //
119 // Note Opera violates the standard by cascading the effective values
120 // into the HTMLElement.style object. We can use IE's HTMLElement.currentStyle
121 // to get the effective values. In Gecko we will use the W3 DOM Style Standard getComputedStyle
122
123 function xbStyle(obj, win, position)
124 {
125 if (typeof(obj) == 'object' && typeof(obj.style) != 'undefined')
126 this.styleObj = obj.style;
127 else if (document.layers) // NN4
128 {
129 if (typeof(position) == 'undefined')
130 position = '';
131
132 this.styleObj = obj;
133 this.styleObj.position = position;
134 }
135 this.object = obj;
136 this.window = win ? win : window;
137 }
138
139 xbStyle.prototype.styleObj = null;
140 xbStyle.prototype.object = null;
141
142 /////////////////////////////////////////////////////////////
143 // xbStyle.getEffectiveValue()
144 // note that xbStyle's constructor uses the currentStyle object
145 // for IE5+ and that Opera's style object contains computed values
146 // already. Netscape Navigator's layer object also contains the
147 // computed values as well. Note that IE4 will not return the
148 // computed values.
149
150 function xbStyleGetEffectiveValue(propname)
151 {
152 var value = null;
153
154 if (this.window.document.defaultView && this.window.document.defaultView.getComputedStyle)
155 {
156 // W3
157 // Note that propname is the name of the property in the CSS Style
158 // Object. However the W3 method getPropertyValue takes the actual
159 // property name from the CSS Style rule, i.e., propname is
160 // 'backgroundColor' but getPropertyValue expects 'background-color'.
161
162 var capIndex;
163 var cappropname = propname;
164
165 while ( (capIndex = cappropname.search(/[A-Z]/)) != -1)
166 {
167 if (capIndex != -1)
168 {
169 cappropname = cappropname.substring(0, capIndex) + '-' + cappropname.substring(capIndex, capIndex+1).toLowerCase() + cappropname.substr(capIndex+1);
170 }
171 }
172
173 value = this.window.document.defaultView.getComputedStyle(this.object, '').getPropertyValue(cappropname);
174
175 // xxxHack for Gecko:
176 if (!value && this.styleObj[propname])
177 {
178 value = this.styleObj[propname];
179 }
180 }
181 else if (typeof(this.styleObj[propname]) == 'undefined')
182 {
183 value = xbStyleNotSupportStringValue(propname);
184 }
185 else if (typeof(this.object.currentStyle) != 'undefined')
186 {
187 // IE5+
188 value = this.object.currentStyle[propname];
189 if (!value)
190 {
191 value = this.styleObj[propname];
192 }
193
194 if (propname == 'clip' && !value)
195 {
196 // clip is not stored in IE5/6 handle separately
197 value = 'rect(' + this.object.currentStyle.clipTop + ', ' + this.object.currentStyle.clipRight + ', ' + this.object.currentStyle.clipBottom + ', ' + this.object.currentStyle.clipLeft + ')';
198 }
199 }
200 else
201 {
202 // IE4+, Opera, NN4
203 value = this.styleObj[propname];
204 }
205
206 return value;
207 }
208
209 /////////////////////////////////////////////////////////////////////////////
210 // xbStyle.moveAbove()
211
212 function xbStyleMoveAbove(cont)
213 {
214 this.setzIndex(cont.getzIndex()+1);
215 }
216
217 /////////////////////////////////////////////////////////////////////////////
218 // xbStyle.moveBelow()
219
220 function xbStyleMoveBelow(cont)
221 {
222 var zindex = cont.getzIndex() - 1;
223
224 this.setzIndex(zindex);
225 }
226
227 /////////////////////////////////////////////////////////////////////////////
228 // xbStyle.moveBy()
229
230 function xbStyleMoveBy(deltaX, deltaY)
231 {
232 this.moveTo(this.getLeft() + deltaX, this.getTop() + deltaY);
233 }
234
235 /////////////////////////////////////////////////////////////////////////////
236 // xbStyle.moveTo()
237
238 function xbStyleMoveTo(x, y)
239 {
240 this.setLeft(x);
241 this.setTop(y);
242 }
243
244 /////////////////////////////////////////////////////////////////////////////
245 // xbStyle.moveToAbsolute()
246
247 function xbStyleMoveToAbsolute(x, y)
248 {
249 this.setPageX(x);
250 this.setPageY(y);
251 }
252
253 /////////////////////////////////////////////////////////////////////////////
254 // xbStyle.resizeBy()
255
256 function xbStyleResizeBy(deltaX, deltaY)
257 {
258 this.setWidth( this.getWidth() + deltaX );
259 this.setHeight( this.getHeight() + deltaY );
260 }
261
262 /////////////////////////////////////////////////////////////////////////////
263 // xbStyle.resizeTo()
264
265 function xbStyleResizeTo(x, y)
266 {
267 this.setWidth(x);
268 this.setHeight(y);
269 }
270
271 ////////////////////////////////////////////////////////////////////////
272
273 xbStyle.prototype.getEffectiveValue = xbStyleGetEffectiveValue;
274 xbStyle.prototype.moveAbove = xbStyleMoveAbove;
275 xbStyle.prototype.moveBelow = xbStyleMoveBelow;
276 xbStyle.prototype.moveBy = xbStyleMoveBy;
277 xbStyle.prototype.moveTo = xbStyleMoveTo;
278 xbStyle.prototype.moveToAbsolute = xbStyleMoveToAbsolute;
279 xbStyle.prototype.resizeBy = xbStyleResizeBy;
280 xbStyle.prototype.resizeTo = xbStyleResizeTo;
281
282 if (document.all || document.getElementsByName)
283 {
284 xblibrary.loadScript('xbStyle-css.js');
285 }
286 else if (document.layers)
287 {
288 xblibrary.loadScript('xbStyle-nn4.js');
289 }
290 else
291 {
292 xblibrary.loadScript('xbStyle-not-supported.js');
293 }
294
295