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