Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / NativeWindow.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.air.NativeWindow"></div>/**\r
9  * @class Ext.air.NativeWindow\r
10  * @extends Ext.air.NativeObservable\r
11  * \r
12  * Wraps the AIR NativeWindow class to give an Ext friendly API. <br/><br/>This class also adds \r
13  * automatic state management (position and size) for the window (by id) and it can be used \r
14  * for easily creating "minimize to system tray" for the main window in your application.<br/><br/>\r
15  * \r
16  * Note: Many of the config options for this class can only be applied to NEW windows. Passing \r
17  * in an existing instance of a window along with those config options will have no effect.\r
18  * \r
19  * @constructor\r
20  * @param {Object} config\r
21  */\r
22 Ext.air.NativeWindow = function(config){\r
23         Ext.apply(this, config);\r
24         \r
25         <div id="prop-Ext.air.NativeWindow-id"></div>/**\r
26          * @type String\r
27          */\r
28         this.id = this.id || Ext.uniqueId();\r
29         \r
30         this.addEvents(\r
31                 <div id="event-Ext.air.NativeWindow-close"></div>/**\r
32                  * @event close\r
33                  * @param {Object} e The air event object\r
34                  */\r
35                 'close', \r
36                 <div id="event-Ext.air.NativeWindow-closing"></div>/**\r
37                  * @event closing\r
38                  * @param {Object} e The air event object\r
39                  */\r
40                 'closing',\r
41                 <div id="event-Ext.air.NativeWindow-move"></div>/**\r
42                  * @event move\r
43                  * @param {Object} e The air event object\r
44                  */\r
45                 'move',\r
46                 <div id="event-Ext.air.NativeWindow-moving"></div>/**\r
47                  * @event moving\r
48                  * @param {Object} e The air event object\r
49                  */\r
50                 'moving',\r
51                 <div id="event-Ext.air.NativeWindow-resize"></div>/**\r
52                  * @event resize\r
53                  * @param {Object} e The air event object\r
54                  */\r
55                 'resize',\r
56                 <div id="event-Ext.air.NativeWindow-resizing"></div>/**\r
57                  * @event resizing\r
58                  * @param {Object} e The air event object\r
59                  */\r
60                 'resizing',\r
61                 <div id="event-Ext.air.NativeWindow-displayStateChange"></div>/**\r
62                  * @event displayStateChange\r
63                  * @param {Object} e The air event object\r
64                  */\r
65                 'displayStateChange',\r
66                 <div id="event-Ext.air.NativeWindow-displayStateChanging"></div>/**\r
67                  * @event displayStateChanging\r
68                  * @param {Object} e The air event object\r
69                  */\r
70                 'displayStateChanging'\r
71         );\r
72         \r
73         Ext.air.NativeWindow.superclass.constructor.call(this);\r
74         \r
75         if(!this.instance){\r
76                 var options = new air.NativeWindowInitOptions();\r
77                 options.systemChrome = this.chrome;\r
78                 options.type = this.type;\r
79                 options.resizable = this.resizable;\r
80                 options.minimizable = this.minimizable;\r
81                 options.maximizable = this.maximizable;\r
82                 options.transparent = this.transparent;\r
83                 \r
84                 this.loader = window.runtime.flash.html.HTMLLoader.createRootWindow(false, options, false);\r
85                 this.loader.load(new air.URLRequest(this.file));\r
86         \r
87                 this.instance = this.loader.window.nativeWindow;\r
88         }else{\r
89                 this.loader = this.instance.stage.getChildAt(0);\r
90         }\r
91         \r
92         var provider = Ext.state.Manager;\r
93         var b = air.Screen.mainScreen.visibleBounds;\r
94         \r
95         var state = provider.get(this.id) || {};\r
96         provider.set(this.id, state);\r
97                 \r
98         var win = this.instance;\r
99         \r
100         var width = Math.max(state.width || this.width, 100);\r
101         var height = Math.max(state.height || this.height, 100);\r
102         \r
103         var centerX = b.x + ((b.width/2)-(width/2));\r
104         var centerY = b.y + ((b.height/2)-(height/2));\r
105         \r
106         var x = !Ext.isEmpty(state.x, false) ? state.x : (!Ext.isEmpty(this.x, false) ? this.x : centerX);\r
107         var y = !Ext.isEmpty(state.y, false) ? state.y : (!Ext.isEmpty(this.y, false) ? this.y : centerY);\r
108         \r
109         win.width = width;\r
110         win.height = height;\r
111         win.x = x;\r
112         win.y = y;\r
113         \r
114         win.addEventListener('move', function(){\r
115                 if(win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) {\r
116                         state.x = win.x;\r
117                         state.y = win.y;\r
118                 }\r
119         });     \r
120         win.addEventListener('resize', function(){\r
121                 if (win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) {\r
122                         state.width = win.width;\r
123                         state.height = win.height;\r
124                 }\r
125         });\r
126         \r
127         Ext.air.NativeWindowManager.register(this);\r
128         this.on('close', this.unregister, this);\r
129         \r
130         <div id="cfg-Ext.air.NativeWindow-minimizeToTray"></div>/**\r
131          * @cfg {Boolean} minimizeToTray \r
132          * True to enable minimizing to the system tray. Note: this should only be applied\r
133          * to the primary window in your application. A trayIcon is required.\r
134          */\r
135         if(this.minimizeToTray){\r
136                 this.initMinimizeToTray(this.trayIcon, this.trayMenu);\r
137         }\r
138         \r
139 };\r
140 \r
141 Ext.extend(Ext.air.NativeWindow, Ext.air.NativeObservable, {\r
142         \r
143         <div id="cfg-Ext.air.NativeWindow-instance"></div>/**\r
144          * @cfg {air.NativeWindow} instance \r
145          * The native window instance to wrap. If undefined, a new window will be created.\r
146          */\r
147         \r
148         <div id="cfg-Ext.air.NativeWindow-trayIcon"></div>/**\r
149          * @cfg {String} trayIcon \r
150          * The icon to display when minimized in the system tray\r
151          */\r
152         <div id="cfg-Ext.air.NativeWindow-trayMenu"></div>/**\r
153          * @cfg {NativeMenu} trayMenu \r
154          * Menu to display when the tray icon is right clicked\r
155          */\r
156         <div id="cfg-Ext.air.NativeWindow-trayTip"></div>/**\r
157          * @cfg {String} trayTip \r
158          * Tooltip for the tray icon\r
159          */     \r
160         \r
161         <div id="cfg-Ext.air.NativeWindow-chrome"></div>/**\r
162          * @cfg {String} chrome \r
163          * The native window chrome (defaults to 'standard', can also be 'none').\r
164          */\r
165         chrome: 'standard', // can also be none\r
166         <div id="cfg-Ext.air.NativeWindow-type"></div>/**\r
167          * @cfg {String} type \r
168          * The native window type - normal, utility or lightweight. (defaults to normal)\r
169          */\r
170         type: 'normal', // can be normal, utility or lightweight\r
171         <div id="cfg-Ext.air.NativeWindow-width"></div>/**\r
172          * @cfg {Number} width\r
173          */\r
174         width:600,\r
175         <div id="cfg-Ext.air.NativeWindow-height"></div>/**\r
176          * @cfg {Number} height \r
177          */\r
178         height:400,\r
179         <div id="cfg-Ext.air.NativeWindow-resizable"></div>/**\r
180          * @cfg {Boolean} resizable \r
181          */\r
182         resizable: true,\r
183         <div id="cfg-Ext.air.NativeWindow-minimizable"></div>/**\r
184          * @cfg {Boolean} minimizable \r
185          */\r
186         minimizable: true,\r
187         <div id="cfg-Ext.air.NativeWindow-maximizable"></div>/**\r
188          * @cfg {Boolean} maximizable \r
189          */\r
190         maximizable: true,\r
191         <div id="cfg-Ext.air.NativeWindow-transparent"></div>/**\r
192          * @cfg {Boolean} transparent\r
193          */\r
194         transparent: false,\r
195         \r
196         <div id="method-Ext.air.NativeWindow-getNative"></div>/**\r
197          * Returns the air.NativeWindow instance\r
198          * @return air.NativeWindow\r
199          */\r
200         getNative : function(){\r
201                 return this.instance;\r
202         },\r
203         \r
204         <div id="method-Ext.air.NativeWindow-getCenterXY"></div>/**\r
205          * Returns the x/y coordinates for centering the windw on the screen\r
206          * @return {x: Number, y: Number}\r
207          */\r
208         getCenterXY : function(){\r
209                 var b = air.Screen.mainScreen.visibleBounds;\r
210                 return {\r
211                         x: b.x + ((b.width/2)-(this.width/2)),\r
212                         y: b.y + ((b.height/2)-(this.height/2))\r
213                 };\r
214         },\r
215         \r
216         <div id="method-Ext.air.NativeWindow-show"></div>/**\r
217          * Shows the window\r
218          */\r
219         show :function(){\r
220                 if(this.trayed){\r
221                         Ext.air.SystemTray.hideIcon();\r
222                         this.trayed = false;\r
223                 }\r
224                 this.instance.visible = true;\r
225         },\r
226         \r
227         <div id="method-Ext.air.NativeWindow-activate"></div>/**\r
228          * Shows and activates the window\r
229          */\r
230         activate : function(){\r
231                 this.show();\r
232                 this.instance.activate();\r
233         },\r
234         \r
235         <div id="method-Ext.air.NativeWindow-hide"></div>/**\r
236          * Hides the window\r
237          */\r
238         hide :function(){\r
239                 this.instance.visible = false;\r
240         },\r
241         \r
242         <div id="method-Ext.air.NativeWindow-close"></div>/**\r
243          * Closes the window\r
244          */\r
245         close : function(){\r
246                 this.instance.close();  \r
247         },\r
248         \r
249         <div id="method-Ext.air.NativeWindow-isMinimized"></div>/**\r
250          * Returns true if this window is minimized\r
251          * @return Boolean\r
252          */\r
253         isMinimized :function(){\r
254                 return this.instance.displayState == air.NativeWindowDisplayState.MINIMIZED;\r
255         },\r
256         \r
257         <div id="method-Ext.air.NativeWindow-isMaximized"></div>/**\r
258          * Returns true if this window is maximized\r
259          * @return Boolean\r
260          */\r
261         isMaximized :function(){\r
262                 return this.instance.displayState == air.NativeWindowDisplayState.MAXIMIZED;\r
263         },\r
264         \r
265         <div id="method-Ext.air.NativeWindow-moveTo"></div>/**\r
266          * Moves the window to the passed xy and y coordinates\r
267          * @param {Number} x\r
268          * @param {Number} y\r
269          */\r
270         moveTo : function(x, y){\r
271                 this.x = this.instance.x = x;\r
272                 this.y = this.instance.y = y;   \r
273         },\r
274         \r
275         <div id="method-Ext.air.NativeWindow-resize"></div>/**\r
276          * @param {Number} width\r
277          * @param {Number} height\r
278          */\r
279         resize : function(width, height){\r
280                 this.width = this.instance.width = width;\r
281                 this.height = this.instance.height = height;    \r
282         },\r
283         \r
284         unregister : function(){\r
285                 Ext.air.NativeWindowManager.unregister(this);\r
286         },\r
287         \r
288         initMinimizeToTray : function(icon, menu){\r
289                 var tray = Ext.air.SystemTray;\r
290                 \r
291                 tray.setIcon(icon, this.trayTip);\r
292                 this.on('displayStateChanging', function(e){\r
293                         if(e.afterDisplayState == 'minimized'){\r
294                                 e.preventDefault();\r
295                                 this.hide();\r
296                                 tray.showIcon();\r
297                                 this.trayed = true;\r
298                         }\r
299                 }, this);\r
300                 \r
301                 tray.on('click', function(){\r
302                         this.activate();\r
303                 }, this);\r
304                 \r
305                 if(menu){\r
306                         tray.setMenu(menu);\r
307                 }\r
308         }\r
309 });\r
310 \r
311 <div id="method-Ext.air.NativeWindow-NativeWindow.getRootWindow"></div>/**\r
312  * Returns the first opened window in your application\r
313  * @return air.NativeWindow\r
314  * @static\r
315  */\r
316 Ext.air.NativeWindow.getRootWindow = function(){\r
317         return air.NativeApplication.nativeApplication.openedWindows[0];\r
318 };\r
319 \r
320 <div id="method-Ext.air.NativeWindow-NativeWindow.getRootHtmlWindow"></div>/**\r
321  * Returns the javascript "window" object of the first opened window in your application\r
322  * @return Window\r
323  * @static\r
324  */\r
325 Ext.air.NativeWindow.getRootHtmlWindow = function(){\r
326         return Ext.air.NativeWindow.getRootWindow().stage.getChildAt(0).window;\r
327 };\r
328 \r
329 <div id="cls-Ext.air.NativeWindowGroup"></div>/**\r
330  * @class Ext.air.NativeWindowGroup\r
331  * \r
332  * A collection of NativeWindows.\r
333  */\r
334 Ext.air.NativeWindowGroup = function(){\r
335     var list = {};\r
336 \r
337     return {\r
338                 <div id="method-Ext.air.NativeWindowGroup-register"></div>/**\r
339                  * @param {Object} win\r
340                  */\r
341         register : function(win){\r
342             list[win.id] = win;\r
343         },\r
344 \r
345         <div id="method-Ext.air.NativeWindowGroup-unregister"></div>/**\r
346                  * @param {Object} win\r
347                  */\r
348         unregister : function(win){\r
349             delete list[win.id];\r
350         },\r
351 \r
352         <div id="method-Ext.air.NativeWindowGroup-get"></div>/**\r
353                  * @param {String} id\r
354                  */\r
355         get : function(id){\r
356             return list[id];\r
357         },\r
358 \r
359         <div id="method-Ext.air.NativeWindowGroup-closeAll"></div>/**\r
360                  * Closes all windows\r
361                  */\r
362         closeAll : function(){\r
363             for(var id in list){\r
364                 if(list.hasOwnProperty(id)){\r
365                     list[id].close();\r
366                 }\r
367             }\r
368         },\r
369 \r
370         <div id="method-Ext.air.NativeWindowGroup-each"></div>/**\r
371          * Executes the specified function once for every window in the group, passing each\r
372          * window as the only parameter. Returning false from the function will stop the iteration.\r
373          * @param {Function} fn The function to execute for each item\r
374          * @param {Object} scope (optional) The scope in which to execute the function\r
375          */\r
376         each : function(fn, scope){\r
377             for(var id in list){\r
378                 if(list.hasOwnProperty(id)){\r
379                     if(fn.call(scope || list[id], list[id]) === false){\r
380                         return;\r
381                     }\r
382                 }\r
383             }\r
384         }\r
385     };\r
386 };\r
387 \r
388 <div id="cls-Ext.air.NativeWindowManager"></div>/**\r
389  * @class Ext.air.NativeWindowManager\r
390  * @extends Ext.air.NativeWindowGroup\r
391  * \r
392  * Collection of all NativeWindows created.\r
393  * \r
394  * @singleton\r
395  */\r
396 Ext.air.NativeWindowManager = new Ext.air.NativeWindowGroup();</pre>    \r
397 </body>\r
398 </html>