Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / widgets / Window.js
1 /*!
2  * Ext JS Library 3.0.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.Window
9  * @extends Ext.Panel
10  * <p>A specialized panel intended for use as an application window.  Windows are floated, {@link #resizable}, and
11  * {@link #draggable} by default.  Windows can be {@link #maximizable maximized} to fill the viewport,
12  * restored to their prior size, and can be {@link #minimize}d.</p>
13  * <p>Windows can also be linked to a {@link Ext.WindowGroup} or managed by the {@link Ext.WindowMgr} to provide 
14  * grouping, activation, to front, to back and other application-specific behavior.</p>
15  * <p>By default, Windows will be rendered to document.body. To {@link #constrain} a Window to another element
16  * specify {@link Ext.Component#renderTo renderTo}.</p>
17  * <p><b>Note:</b> By default, the <code>{@link #closable close}</code> header tool <i>destroys</i> the Window resulting in
18  * destruction of any child Components. This makes the Window object, and all its descendants <b>unusable</b>. To enable
19  * re-use of a Window, use <b><code>{@link #closeAction closeAction: 'hide'}</code></b>.</p>
20  * @constructor
21  * @param {Object} config The config object
22  * @xtype window
23  */
24 Ext.Window = Ext.extend(Ext.Panel, {
25     /**
26      * @cfg {Number} x
27      * The X position of the left edge of the window on initial showing. Defaults to centering the Window within
28      * the width of the Window's container {@link Ext.Element Element) (The Element that the Window is rendered to).
29      */
30     /**
31      * @cfg {Number} y
32      * The Y position of the top edge of the window on initial showing. Defaults to centering the Window within
33      * the height of the Window's container {@link Ext.Element Element) (The Element that the Window is rendered to).
34      */
35     /**
36      * @cfg {Boolean} modal
37      * True to make the window modal and mask everything behind it when displayed, false to display it without
38      * restricting access to other UI elements (defaults to false).
39      */
40     /**
41      * @cfg {String/Element} animateTarget
42      * Id or element from which the window should animate while opening (defaults to null with no animation).
43      */
44     /**
45      * @cfg {String} resizeHandles
46      * A valid {@link Ext.Resizable} handles config string (defaults to 'all').  Only applies when resizable = true.
47      */
48     /**
49      * @cfg {Ext.WindowGroup} manager
50      * A reference to the WindowGroup that should manage this window (defaults to {@link Ext.WindowMgr}).
51      */
52     /**
53     * @cfg {String/Number/Button} defaultButton
54     * The id / index of a button or a button instance to focus when this window received the focus.
55     */
56     /**
57     * @cfg {Function} onEsc
58     * Allows override of the built-in processing for the escape key. Default action
59     * is to close the Window (performing whatever action is specified in {@link #closeAction}.
60     * To prevent the Window closing when the escape key is pressed, specify this as
61     * Ext.emptyFn (See {@link Ext#emptyFn}).
62     */
63     /**
64      * @cfg {Boolean} collapsed
65      * True to render the window collapsed, false to render it expanded (defaults to false). Note that if 
66      * {@link #expandOnShow} is true (the default) it will override the <tt>collapsed</tt> config and the window 
67      * will always be expanded when shown.
68      */
69     /**
70      * @cfg {Boolean} maximized
71      * True to initially display the window in a maximized state. (Defaults to false).
72      */
73     
74     /**
75     * @cfg {String} baseCls
76     * The base CSS class to apply to this panel's element (defaults to 'x-window').
77     */
78     baseCls : 'x-window',
79     /**
80      * @cfg {Boolean} resizable
81      * True to allow user resizing at each edge and corner of the window, false to disable resizing (defaults to true).
82      */
83     resizable : true,
84     /**
85      * @cfg {Boolean} draggable
86      * True to allow the window to be dragged by the header bar, false to disable dragging (defaults to true).  Note
87      * that by default the window will be centered in the viewport, so if dragging is disabled the window may need
88      * to be positioned programmatically after render (e.g., myWindow.setPosition(100, 100);).
89      */
90     draggable : true,
91     /**
92      * @cfg {Boolean} closable
93      * <p>True to display the 'close' tool button and allow the user to close the window, false to
94      * hide the button and disallow closing the window (defaults to true).</p>
95      * <p>By default, when close is requested by either clicking the close button in the header
96      * or pressing ESC when the Window has focus, the {@link #close} method will be called. This
97      * will <i>{@link Ext.Component#destroy destroy}</i> the Window and its content meaning that
98      * it may not be reused.</p>
99      * <p>To make closing a Window <i>hide</i> the Window so that it may be reused, set
100      * {@link #closeAction} to 'hide'.
101      */
102     closable : true,
103     /**
104      * @cfg {String} closeAction
105      * <p>The action to take when the close header tool is clicked:
106      * <div class="mdetail-params"><ul>
107      * <li><b><code>'{@link #close}'</code></b> : <b>Default</b><div class="sub-desc">
108      * {@link #close remove} the window from the DOM and {@link Ext.Component#destroy destroy}
109      * it and all descendant Components. The window will <b>not</b> be available to be
110      * redisplayed via the {@link #show} method.
111      * </div></li>
112      * <li><b><code>'{@link #hide}'</code></b> : <div class="sub-desc">
113      * {@link #hide} the window by setting visibility to hidden and applying negative offsets.
114      * The window will be available to be redisplayed via the {@link #show} method.
115      * </div></li>
116      * </ul></div>
117      * <p><b>Note:</b> This setting does not affect the {@link #close} method
118      * which will always {@link Ext.Component#destroy destroy} the window. To
119      * programatically <i>hide</i> a window, call {@link #hide}.</p>
120      */
121     closeAction : 'close',
122     /**
123      * @cfg {Boolean} constrain
124      * True to constrain the window within its containing element, false to allow it to fall outside of its
125      * containing element. By default the window will be rendered to document.body.  To render and constrain the 
126      * window within another element specify {@link #renderTo}.
127      * (defaults to false).  Optionally the header only can be constrained using {@link #constrainHeader}.
128      */
129     constrain : false,
130     /**
131      * @cfg {Boolean} constrainHeader
132      * True to constrain the window header within its containing element (allowing the window body to fall outside 
133      * of its containing element) or false to allow the header to fall outside its containing element (defaults to 
134      * false). Optionally the entire window can be constrained using {@link #constrain}.
135      */
136     constrainHeader : false,
137     /**
138      * @cfg {Boolean} plain
139      * True to render the window body with a transparent background so that it will blend into the framing
140      * elements, false to add a lighter background color to visually highlight the body element and separate it
141      * more distinctly from the surrounding frame (defaults to false).
142      */
143     plain : false,
144     /**
145      * @cfg {Boolean} minimizable
146      * True to display the 'minimize' tool button and allow the user to minimize the window, false to hide the button
147      * and disallow minimizing the window (defaults to false).  Note that this button provides no implementation --
148      * the behavior of minimizing a window is implementation-specific, so the minimize event must be handled and a
149      * custom minimize behavior implemented for this option to be useful.
150      */
151     minimizable : false,
152     /**
153      * @cfg {Boolean} maximizable
154      * True to display the 'maximize' tool button and allow the user to maximize the window, false to hide the button
155      * and disallow maximizing the window (defaults to false).  Note that when a window is maximized, the tool button
156      * will automatically change to a 'restore' button with the appropriate behavior already built-in that will
157      * restore the window to its previous size.
158      */
159     maximizable : false,
160     /**
161      * @cfg {Number} minHeight
162      * The minimum height in pixels allowed for this window (defaults to 100).  Only applies when resizable = true.
163      */
164     minHeight : 100,
165     /**
166      * @cfg {Number} minWidth
167      * The minimum width in pixels allowed for this window (defaults to 200).  Only applies when resizable = true.
168      */
169     minWidth : 200,
170     /**
171      * @cfg {Boolean} expandOnShow
172      * True to always expand the window when it is displayed, false to keep it in its current state (which may be
173      * {@link #collapsed}) when displayed (defaults to true).
174      */
175     expandOnShow : true,
176
177     // inherited docs, same default
178     collapsible : false,
179
180     /**
181      * @cfg {Boolean} initHidden
182      * True to hide the window until show() is explicitly called (defaults to true).
183      */
184     initHidden : true,
185     /**
186     * @cfg {Boolean} monitorResize @hide
187     * This is automatically managed based on the value of constrain and constrainToHeader
188     */
189     monitorResize : true,
190
191     // The following configs are set to provide the basic functionality of a window.
192     // Changing them would require additional code to handle correctly and should
193     // usually only be done in subclasses that can provide custom behavior.  Changing them
194     // may have unexpected or undesirable results.
195     /** @cfg {String} elements @hide */
196     elements : 'header,body',
197     /** @cfg {Boolean} frame @hide */
198     frame : true,
199     /** @cfg {Boolean} floating @hide */
200     floating : true,
201
202     // private
203     initComponent : function(){
204         Ext.Window.superclass.initComponent.call(this);
205         this.addEvents(
206             /**
207              * @event activate
208              * Fires after the window has been visually activated via {@link setActive}.
209              * @param {Ext.Window} this
210              */
211             /**
212              * @event deactivate
213              * Fires after the window has been visually deactivated via {@link setActive}.
214              * @param {Ext.Window} this
215              */
216             /**
217              * @event resize
218              * Fires after the window has been resized.
219              * @param {Ext.Window} this
220              * @param {Number} width The window's new width
221              * @param {Number} height The window's new height
222              */
223             'resize',
224             /**
225              * @event maximize
226              * Fires after the window has been maximized.
227              * @param {Ext.Window} this
228              */
229             'maximize',
230             /**
231              * @event minimize
232              * Fires after the window has been minimized.
233              * @param {Ext.Window} this
234              */
235             'minimize',
236             /**
237              * @event restore
238              * Fires after the window has been restored to its original size after being maximized.
239              * @param {Ext.Window} this
240              */
241             'restore'
242         );
243         if(this.initHidden === false){
244             this.show();
245         }else{
246             this.hidden = true;
247         }
248     },
249
250     // private
251     getState : function(){
252         return Ext.apply(Ext.Window.superclass.getState.call(this) || {}, this.getBox(true));
253     },
254
255     // private
256     onRender : function(ct, position){
257         Ext.Window.superclass.onRender.call(this, ct, position);
258
259         if(this.plain){
260             this.el.addClass('x-window-plain');
261         }
262
263         // this element allows the Window to be focused for keyboard events
264         this.focusEl = this.el.createChild({
265                     tag: 'a', href:'#', cls:'x-dlg-focus',
266                     tabIndex:'-1', html: '&#160;'});
267         this.focusEl.swallowEvent('click', true);
268
269         this.proxy = this.el.createProxy('x-window-proxy');
270         this.proxy.enableDisplayMode('block');
271
272         if(this.modal){
273             this.mask = this.container.createChild({cls:'ext-el-mask'}, this.el.dom);
274             this.mask.enableDisplayMode('block');
275             this.mask.hide();
276             this.mon(this.mask, 'click', this.focus, this);
277         }
278         this.initTools();
279     },
280
281     // private
282     initEvents : function(){
283         Ext.Window.superclass.initEvents.call(this);
284         if(this.animateTarget){
285             this.setAnimateTarget(this.animateTarget);
286         }
287
288         if(this.resizable){
289             this.resizer = new Ext.Resizable(this.el, {
290                 minWidth: this.minWidth,
291                 minHeight:this.minHeight,
292                 handles: this.resizeHandles || 'all',
293                 pinned: true,
294                 resizeElement : this.resizerAction
295             });
296             this.resizer.window = this;
297             this.mon(this.resizer, 'beforeresize', this.beforeResize, this);
298         }
299
300         if(this.draggable){
301             this.header.addClass('x-window-draggable');
302         }
303         this.mon(this.el, 'mousedown', this.toFront, this);
304         this.manager = this.manager || Ext.WindowMgr;
305         this.manager.register(this);
306         if(this.maximized){
307             this.maximized = false;
308             this.maximize();
309         }
310         if(this.closable){
311             var km = this.getKeyMap();
312             km.on(27, this.onEsc, this);
313             km.disable();
314         }
315     },
316
317     initDraggable : function(){
318         /**
319          * If this Window is configured {@link #draggable}, this property will contain
320          * an instance of {@link Ext.dd.DD} which handles dragging the Window's DOM Element.
321          * @type Ext.dd.DD
322          * @property dd
323          */
324         this.dd = new Ext.Window.DD(this);
325     },
326
327    // private
328     onEsc : function(){
329         this[this.closeAction]();
330     },
331
332     // private
333     beforeDestroy : function(){
334         if (this.rendered){
335             this.hide();
336           if(this.doAnchor){
337                 Ext.EventManager.removeResizeListener(this.doAnchor, this);
338               Ext.EventManager.un(window, 'scroll', this.doAnchor, this);
339             }
340             Ext.destroy(
341                 this.focusEl,
342                 this.resizer,
343                 this.dd,
344                 this.proxy,
345                 this.mask
346             );
347         }
348         Ext.Window.superclass.beforeDestroy.call(this);
349     },
350
351     // private
352     onDestroy : function(){
353         if(this.manager){
354             this.manager.unregister(this);
355         }
356         Ext.Window.superclass.onDestroy.call(this);
357     },
358
359     // private
360     initTools : function(){
361         if(this.minimizable){
362             this.addTool({
363                 id: 'minimize',
364                 handler: this.minimize.createDelegate(this, [])
365             });
366         }
367         if(this.maximizable){
368             this.addTool({
369                 id: 'maximize',
370                 handler: this.maximize.createDelegate(this, [])
371             });
372             this.addTool({
373                 id: 'restore',
374                 handler: this.restore.createDelegate(this, []),
375                 hidden:true
376             });
377             this.mon(this.header, 'dblclick', this.toggleMaximize, this);
378         }
379         if(this.closable){
380             this.addTool({
381                 id: 'close',
382                 handler: this[this.closeAction].createDelegate(this, [])
383             });
384         }
385     },
386
387     // private
388     resizerAction : function(){
389         var box = this.proxy.getBox();
390         this.proxy.hide();
391         this.window.handleResize(box);
392         return box;
393     },
394
395     // private
396     beforeResize : function(){
397         this.resizer.minHeight = Math.max(this.minHeight, this.getFrameHeight() + 40); // 40 is a magic minimum content size?
398         this.resizer.minWidth = Math.max(this.minWidth, this.getFrameWidth() + 40);
399         this.resizeBox = this.el.getBox();
400     },
401
402     // private
403     updateHandles : function(){
404         if(Ext.isIE && this.resizer){
405             this.resizer.syncHandleHeight();
406             this.el.repaint();
407         }
408     },
409
410     // private
411     handleResize : function(box){
412         var rz = this.resizeBox;
413         if(rz.x != box.x || rz.y != box.y){
414             this.updateBox(box);
415         }else{
416             this.setSize(box);
417         }
418         this.focus();
419         this.updateHandles();
420         this.saveState();
421         this.doLayout();
422         this.fireEvent('resize', this, box.width, box.height);
423     },
424
425     /**
426      * Focuses the window.  If a defaultButton is set, it will receive focus, otherwise the
427      * window itself will receive focus.
428      */
429     focus : function(){
430         var f = this.focusEl, db = this.defaultButton, t = typeof db;
431         if(t != 'undefined'){
432             if(t == 'number' && this.fbar){
433                 f = this.fbar.items.get(db);
434             }else if(t == 'string'){
435                 f = Ext.getCmp(db);
436             }else{
437                 f = db;
438             }
439         }
440         f = f || this.focusEl;
441         f.focus.defer(10, f);
442     },
443
444     /**
445      * Sets the target element from which the window should animate while opening.
446      * @param {String/Element} el The target element or id
447      */
448     setAnimateTarget : function(el){
449         el = Ext.get(el);
450         this.animateTarget = el;
451     },
452
453     // private
454     beforeShow : function(){
455         delete this.el.lastXY;
456         delete this.el.lastLT;
457         if(this.x === undefined || this.y === undefined){
458             var xy = this.el.getAlignToXY(this.container, 'c-c');
459             var pos = this.el.translatePoints(xy[0], xy[1]);
460             this.x = this.x === undefined? pos.left : this.x;
461             this.y = this.y === undefined? pos.top : this.y;
462         }
463         this.el.setLeftTop(this.x, this.y);
464
465         if(this.expandOnShow){
466             this.expand(false);
467         }
468
469         if(this.modal){
470             Ext.getBody().addClass('x-body-masked');
471             this.mask.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
472             this.mask.show();
473         }
474     },
475
476     /**
477      * Shows the window, rendering it first if necessary, or activates it and brings it to front if hidden.
478      * @param {String/Element} animateTarget (optional) The target element or id from which the window should
479      * animate while opening (defaults to null with no animation)
480      * @param {Function} callback (optional) A callback function to call after the window is displayed
481      * @param {Object} scope (optional) The scope in which to execute the callback
482      * @return {Ext.Window} this
483      */
484     show : function(animateTarget, cb, scope){
485         if(!this.rendered){
486             this.render(Ext.getBody());
487         }
488         if(this.hidden === false){
489             this.toFront();
490             return this;
491         }
492         if(this.fireEvent('beforeshow', this) === false){
493             return this;
494         }
495         if(cb){
496             this.on('show', cb, scope, {single:true});
497         }
498         this.hidden = false;
499         if(animateTarget !== undefined){
500             this.setAnimateTarget(animateTarget);
501         }
502         this.beforeShow();
503         if(this.animateTarget){
504             this.animShow();
505         }else{
506             this.afterShow();
507         }
508         return this;
509     },
510
511     // private
512     afterShow : function(isAnim){
513         this.proxy.hide();
514         this.el.setStyle('display', 'block');
515         this.el.show();
516         if(this.maximized){
517             this.fitContainer();
518         }
519         if(Ext.isMac && Ext.isGecko){ // work around stupid FF 2.0/Mac scroll bar bug
520             this.cascade(this.setAutoScroll);
521         }
522
523         if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
524             Ext.EventManager.onWindowResize(this.onWindowResize, this);
525         }
526         this.doConstrain();
527         this.doLayout();
528         if(this.keyMap){
529             this.keyMap.enable();
530         }
531         this.toFront();
532         this.updateHandles();
533         if(isAnim && (Ext.isIE || Ext.isWebKit)){
534             var sz = this.getSize();
535             this.onResize(sz.width, sz.height);
536         }
537         this.fireEvent('show', this);
538     },
539
540     // private
541     animShow : function(){
542         this.proxy.show();
543         this.proxy.setBox(this.animateTarget.getBox());
544         this.proxy.setOpacity(0);
545         var b = this.getBox(false);
546         b.callback = this.afterShow.createDelegate(this, [true], false);
547         b.scope = this;
548         b.duration = 0.25;
549         b.easing = 'easeNone';
550         b.opacity = 0.5;
551         b.block = true;
552         this.el.setStyle('display', 'none');
553         this.proxy.shift(b);
554     },
555
556     /**
557      * Hides the window, setting it to invisible and applying negative offsets.
558      * @param {String/Element} animateTarget (optional) The target element or id to which the window should
559      * animate while hiding (defaults to null with no animation)
560      * @param {Function} callback (optional) A callback function to call after the window is hidden
561      * @param {Object} scope (optional) The scope in which to execute the callback
562      * @return {Ext.Window} this
563      */
564     hide : function(animateTarget, cb, scope){
565         if(this.hidden || this.fireEvent('beforehide', this) === false){
566             return this;
567         }
568         if(cb){
569             this.on('hide', cb, scope, {single:true});
570         }
571         this.hidden = true;
572         if(animateTarget !== undefined){
573             this.setAnimateTarget(animateTarget);
574         }
575         if(this.modal){
576             this.mask.hide();
577             Ext.getBody().removeClass('x-body-masked');
578         }
579         if(this.animateTarget){
580             this.animHide();
581         }else{
582             this.el.hide();
583             this.afterHide();
584         }
585         return this;
586     },
587
588     // private
589     afterHide : function(){
590         this.proxy.hide();
591         if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
592             Ext.EventManager.removeResizeListener(this.onWindowResize, this);
593         }
594         if(this.keyMap){
595             this.keyMap.disable();
596         }
597         this.fireEvent('hide', this);
598     },
599
600     // private
601     animHide : function(){
602         this.proxy.setOpacity(0.5);
603         this.proxy.show();
604         var tb = this.getBox(false);
605         this.proxy.setBox(tb);
606         this.el.hide();
607         var b = this.animateTarget.getBox();
608         b.callback = this.afterHide;
609         b.scope = this;
610         b.duration = 0.25;
611         b.easing = 'easeNone';
612         b.block = true;
613         b.opacity = 0;
614         this.proxy.shift(b);
615     },
616
617     // private
618     onWindowResize : function(){
619         if(this.maximized){
620             this.fitContainer();
621         }
622         if(this.modal){
623             this.mask.setSize('100%', '100%');
624             var force = this.mask.dom.offsetHeight;
625             this.mask.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
626         }
627         this.doConstrain();
628     },
629
630     // private
631     doConstrain : function(){
632         if(this.constrain || this.constrainHeader){
633             var offsets;
634             if(this.constrain){
635                 offsets = {
636                     right:this.el.shadowOffset,
637                     left:this.el.shadowOffset,
638                     bottom:this.el.shadowOffset
639                 };
640             }else {
641                 var s = this.getSize();
642                 offsets = {
643                     right:-(s.width - 100),
644                     bottom:-(s.height - 25)
645                 };
646             }
647
648             var xy = this.el.getConstrainToXY(this.container, true, offsets);
649             if(xy){
650                 this.setPosition(xy[0], xy[1]);
651             }
652         }
653     },
654
655     // private - used for dragging
656     ghost : function(cls){
657         var ghost = this.createGhost(cls);
658         var box = this.getBox(true);
659         ghost.setLeftTop(box.x, box.y);
660         ghost.setWidth(box.width);
661         this.el.hide();
662         this.activeGhost = ghost;
663         return ghost;
664     },
665
666     // private
667     unghost : function(show, matchPosition){
668         if(!this.activeGhost) {
669             return;
670         }
671         if(show !== false){
672             this.el.show();
673             this.focus();
674             if(Ext.isMac && Ext.isGecko){ // work around stupid FF 2.0/Mac scroll bar bug
675                 this.cascade(this.setAutoScroll);
676             }
677         }
678         if(matchPosition !== false){
679             this.setPosition(this.activeGhost.getLeft(true), this.activeGhost.getTop(true));
680         }
681         this.activeGhost.hide();
682         this.activeGhost.remove();
683         delete this.activeGhost;
684     },
685
686     /**
687      * Placeholder method for minimizing the window.  By default, this method simply fires the {@link #minimize} event
688      * since the behavior of minimizing a window is application-specific.  To implement custom minimize behavior,
689      * either the minimize event can be handled or this method can be overridden.
690      * @return {Ext.Window} this
691      */
692     minimize : function(){
693         this.fireEvent('minimize', this);
694         return this;
695     },
696
697     /**
698      * <p>Closes the Window, removes it from the DOM, {@link Ext.Component#destroy destroy}s
699      * the Window object and all its descendant Components. The {@link Ext.Panel#beforeclose beforeclose}
700      * event is fired before the close happens and will cancel the close action if it returns false.<p>
701      * <p><b>Note:</b> This method is not affected by the {@link #closeAction} setting which
702      * only affects the action triggered when clicking the {@link #closable 'close' tool in the header}.
703      * To hide the Window without destroying it, call {@link #hide}.</p>
704      */
705     close : function(){
706         if(this.fireEvent('beforeclose', this) !== false){
707             this.hide(null, function(){
708                 this.fireEvent('close', this);
709                 this.destroy();
710             }, this);
711         }
712     },
713
714     /**
715      * Fits the window within its current container and automatically replaces
716      * the {@link #maximizable 'maximize' tool button} with the 'restore' tool button.
717      * Also see {@link #toggleMaximize}.
718      * @return {Ext.Window} this
719      */
720     maximize : function(){
721         if(!this.maximized){
722             this.expand(false);
723             this.restoreSize = this.getSize();
724             this.restorePos = this.getPosition(true);
725             if (this.maximizable){
726                 this.tools.maximize.hide();
727                 this.tools.restore.show();
728             }
729             this.maximized = true;
730             this.el.disableShadow();
731
732             if(this.dd){
733                 this.dd.lock();
734             }
735             if(this.collapsible){
736                 this.tools.toggle.hide();
737             }
738             this.el.addClass('x-window-maximized');
739             this.container.addClass('x-window-maximized-ct');
740
741             this.setPosition(0, 0);
742             this.fitContainer();
743             this.fireEvent('maximize', this);
744         }
745         return this;
746     },
747
748     /**
749      * Restores a {@link #maximizable maximized}  window back to its original
750      * size and position prior to being maximized and also replaces
751      * the 'restore' tool button with the 'maximize' tool button.
752      * Also see {@link #toggleMaximize}.
753      * @return {Ext.Window} this
754      */
755     restore : function(){
756         if(this.maximized){
757             this.el.removeClass('x-window-maximized');
758             this.tools.restore.hide();
759             this.tools.maximize.show();
760             this.setPosition(this.restorePos[0], this.restorePos[1]);
761             this.setSize(this.restoreSize.width, this.restoreSize.height);
762             delete this.restorePos;
763             delete this.restoreSize;
764             this.maximized = false;
765             this.el.enableShadow(true);
766
767             if(this.dd){
768                 this.dd.unlock();
769             }
770             if(this.collapsible){
771                 this.tools.toggle.show();
772             }
773             this.container.removeClass('x-window-maximized-ct');
774
775             this.doConstrain();
776             this.fireEvent('restore', this);
777         }
778         return this;
779     },
780
781     /**
782      * A shortcut method for toggling between {@link #maximize} and {@link #restore} based on the current maximized
783      * state of the window.
784      * @return {Ext.Window} this
785      */
786     toggleMaximize : function(){
787         return this[this.maximized ? 'restore' : 'maximize']();
788     },
789
790     // private
791     fitContainer : function(){
792         var vs = this.container.getViewSize();
793         this.setSize(vs.width, vs.height);
794     },
795
796     // private
797     // z-index is managed by the WindowManager and may be overwritten at any time
798     setZIndex : function(index){
799         if(this.modal){
800             this.mask.setStyle('z-index', index);
801         }
802         this.el.setZIndex(++index);
803         index += 5;
804
805         if(this.resizer){
806             this.resizer.proxy.setStyle('z-index', ++index);
807         }
808
809         this.lastZIndex = index;
810     },
811
812     /**
813      * Aligns the window to the specified element
814      * @param {Mixed} element The element to align to.
815      * @param {String} position The position to align to (see {@link Ext.Element#alignTo} for more details).
816      * @param {Array} offsets (optional) Offset the positioning by [x, y]
817      * @return {Ext.Window} this
818      */
819     alignTo : function(element, position, offsets){
820         var xy = this.el.getAlignToXY(element, position, offsets);
821         this.setPagePosition(xy[0], xy[1]);
822         return this;
823     },
824
825     /**
826      * Anchors this window to another element and realigns it when the window is resized or scrolled.
827      * @param {Mixed} element The element to align to.
828      * @param {String} position The position to align to (see {@link Ext.Element#alignTo} for more details)
829      * @param {Array} offsets (optional) Offset the positioning by [x, y]
830      * @param {Boolean/Number} monitorScroll (optional) true to monitor body scroll and reposition. If this parameter
831      * is a number, it is used as the buffer delay (defaults to 50ms).
832      * @return {Ext.Window} this
833      */
834     anchorTo : function(el, alignment, offsets, monitorScroll){
835       if(this.doAnchor){
836           Ext.EventManager.removeResizeListener(this.doAnchor, this);
837           Ext.EventManager.un(window, 'scroll', this.doAnchor, this);
838       }
839       this.doAnchor = function(){
840           this.alignTo(el, alignment, offsets);
841       };
842       Ext.EventManager.onWindowResize(this.doAnchor, this);
843       
844       var tm = typeof monitorScroll;
845       if(tm != 'undefined'){
846           Ext.EventManager.on(window, 'scroll', this.doAnchor, this,
847               {buffer: tm == 'number' ? monitorScroll : 50});
848       }
849       this.doAnchor();
850       return this;
851     },
852
853     /**
854      * Brings this window to the front of any other visible windows
855      * @param {Boolean} e (optional) Specify <tt>false</tt> to prevent the window from being focused.
856      * @return {Ext.Window} this
857      */
858     toFront : function(e){
859         if(this.manager.bringToFront(this)){
860             if(!e || !e.getTarget().focus){
861                 this.focus();
862             }
863         }
864         return this;
865     },
866
867     /**
868      * Makes this the active window by showing its shadow, or deactivates it by hiding its shadow.  This method also
869      * fires the {@link #activate} or {@link #deactivate} event depending on which action occurred.
870      * @param {Boolean} active True to activate the window, false to deactivate it (defaults to false)
871      */
872     setActive : function(active){
873         if(active){
874             if(!this.maximized){
875                 this.el.enableShadow(true);
876             }
877             this.fireEvent('activate', this);
878         }else{
879             this.el.disableShadow();
880             this.fireEvent('deactivate', this);
881         }
882     },
883
884     /**
885      * Sends this window to the back of (lower z-index than) any other visible windows
886      * @return {Ext.Window} this
887      */
888     toBack : function(){
889         this.manager.sendToBack(this);
890         return this;
891     },
892
893     /**
894      * Centers this window in the viewport
895      * @return {Ext.Window} this
896      */
897     center : function(){
898         var xy = this.el.getAlignToXY(this.container, 'c-c');
899         this.setPagePosition(xy[0], xy[1]);
900         return this;
901     }
902
903     /**
904      * @cfg {Boolean} autoWidth @hide
905      **/
906 });
907 Ext.reg('window', Ext.Window);
908
909 // private - custom Window DD implementation
910 Ext.Window.DD = function(win){
911     this.win = win;
912     Ext.Window.DD.superclass.constructor.call(this, win.el.id, 'WindowDD-'+win.id);
913     this.setHandleElId(win.header.id);
914     this.scroll = false;
915 };
916
917 Ext.extend(Ext.Window.DD, Ext.dd.DD, {
918     moveOnly:true,
919     headerOffsets:[100, 25],
920     startDrag : function(){
921         var w = this.win;
922         this.proxy = w.ghost();
923         if(w.constrain !== false){
924             var so = w.el.shadowOffset;
925             this.constrainTo(w.container, {right: so, left: so, bottom: so});
926         }else if(w.constrainHeader !== false){
927             var s = this.proxy.getSize();
928             this.constrainTo(w.container, {right: -(s.width-this.headerOffsets[0]), bottom: -(s.height-this.headerOffsets[1])});
929         }
930     },
931     b4Drag : Ext.emptyFn,
932
933     onDrag : function(e){
934         this.alignElWithMouse(this.proxy, e.getPageX(), e.getPageY());
935     },
936
937     endDrag : function(e){
938         this.win.unghost();
939         this.win.saveState();
940     }
941 });