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