API  0.9.8
 All Classes Files Functions Variables Typedefs Macros Groups Pages
CPPlatformWindow.j
Go to the documentation of this file.
1 /*
2  * CPPlatformWindow.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2010, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 @global CPApp
26 
27 @typedef DOMWindow
28 
30 
31 @implementation CPPlatformWindow : CPObject
32 {
33  CGRect _contentRect;
34 
35  CPInteger _level;
36  BOOL _hasShadow;
37  unsigned _shadowStyle;
38  CPString _title;
39  BOOL _shouldUpdateContentRect;
40  BOOL _hasInitializeInstanceWithWindow;
41 
42 #if PLATFORM(DOM)
43  DOMWindow _DOMWindow;
44 
45  DOMElement _DOMBodyElement;
46  DOMElement _DOMFocusElement;
47  DOMElement _DOMEventGuard;
48  DOMElement _DOMScrollingElement;
49  id _hideDOMScrollingElementTimeout;
50 
51  CPArray _windowLevels;
52  CPDictionary _windowLayers;
53 
54  BOOL _mouseIsDown;
55  BOOL _mouseDownIsRightClick;
56  CGPoint _lastMouseEventLocation;
57  CPWindow _mouseDownWindow;
58  CPTimeInterval _lastMouseUp;
59  CPTimeInterval _lastMouseDown;
60 
61  Object _charCodes;
62  unsigned _keyCode;
63  unsigned _lastKey;
64  BOOL _capsLockActive;
65 
66  BOOL _DOMEventMode;
67 
68  CPPlatformPasteboard _platformPasteboard;
69 
70  CPString _overriddenEventType;
71 #endif
72 }
73 
74 + (CPSet)visiblePlatformWindows
75 {
76  return [CPSet set];
77 }
78 
79 + (BOOL)supportsMultipleInstances
80 {
81 #if PLATFORM(DOM)
83 #else
84  return NO;
85 #endif
86 }
87 
88 + (CPPlatformWindow)primaryPlatformWindow
89 {
90  return PrimaryPlatformWindow;
91 }
92 
93 + (void)setPrimaryPlatformWindow:(CPPlatformWindow)aPlatformWindow
94 {
95  PrimaryPlatformWindow = aPlatformWindow;
96 }
97 
98 - (id)initWithContentRect:(CGRect)aRect
99 {
100  self = [super init];
101 
102  if (self)
103  {
104  _contentRect = CGRectMakeCopy(aRect);
105 
106 #if PLATFORM(DOM)
107  _windowLevels = [];
108  _windowLayers = @{};
109 
110  _charCodes = {};
111 #endif
112  }
113 
114  return self;
115 }
116 
117 - (id)initWithWindow:(CPWindow)aWindow
118 {
119  self = [self initWithContentRect:CGRectMakeCopy([aWindow frame])];
120 
121  _hasInitializeInstanceWithWindow = YES;
122  [aWindow setPlatformWindow:self];
123  [aWindow setFullPlatformWindow:YES];
124 
125  return self;
126 }
127 
128 - (id)init
129 {
130  return [self initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 500.0)];
131 }
132 
133 - (CGRect)contentRect
134 {
135  return CGRectMakeCopy(_contentRect);
136 }
137 
138 - (CGRect)contentBounds
139 {
140  var contentBounds = [self contentRect];
141 
142  contentBounds.origin = CGPointMakeZero();
143 
144  return contentBounds;
145 }
146 
147 - (CGRect)visibleFrame
148 {
149  var frame = [self contentBounds];
150 
151  frame.origin = CGPointMakeZero();
152 
153  if ([CPMenu menuBarVisible] && [CPPlatformWindow primaryPlatformWindow] === self)
154  {
155  var menuBarHeight = [[CPApp mainMenu] menuBarHeight];
156 
157  frame.origin.y += menuBarHeight;
158  frame.size.height -= menuBarHeight;
159  }
160 
161  return frame;
162 }
163 
164 - (CGRect)usableContentFrame
165 {
166  return [self visibleFrame];
167 }
168 
169 - (void)setContentRect:(CGRect)aRect
170 {
171  if (!aRect || CGRectEqualToRect(_contentRect, aRect))
172  return;
173 
174  _contentRect = CGRectMakeCopy(aRect);
175 
176 #if PLATFORM(DOM)
177  [self updateNativeContentRect];
178 #endif
179 }
180 
181 - (void)updateFromNativeContentRect
182 {
183  [self setContentRect:[self nativeContentRect]];
184 }
185 
186 - (CGPoint)convertBaseToScreen:(CGPoint)aPoint
187 {
188  var contentRect = [self contentRect];
189 
190  return CGPointMake(aPoint.x + CGRectGetMinX(contentRect), aPoint.y + CGRectGetMinY(contentRect));
191 }
192 
193 - (CGPoint)convertScreenToBase:(CGPoint)aPoint
194 {
195  var contentRect = [self contentRect];
196 
197  return CGPointMake(aPoint.x - CGRectGetMinX(contentRect), aPoint.y - CGRectGetMinY(contentRect));
198 }
199 
200 - (BOOL)isVisible
201 {
202 #if PLATFORM(DOM)
203  return _DOMWindow !== NULL;
204 #else
205  return NO;
206 #endif
207 }
208 
209 - (void)deminiaturize:(id)sender
210 {
211 #if PLATFORM(DOM)
212  if (_DOMWindow && typeof _DOMWindow["cpDeminiaturize"] === "function")
213  _DOMWindow.cpDeminiaturize();
214 #endif
215 }
216 
217 - (void)miniaturize:(id)sender
218 {
219 #if PLATFORM(DOM)
220  if (_DOMWindow && typeof _DOMWindow["cpMiniaturize"] === "function")
221  _DOMWindow.cpMiniaturize();
222 #endif
223 }
224 
225 - (void)moveWindow:(CPWindow)aWindow fromLevel:(int)fromLevel toLevel:(int)toLevel
226 {
227 #if PLATFORM(DOM)
228  if (!aWindow._isVisible)
229  return;
230 
231  var fromLayer = [self layerAtLevel:fromLevel create:NO],
232  toLayer = [self layerAtLevel:toLevel create:YES];
233 
234  [fromLayer removeWindow:aWindow];
235  [toLayer insertWindow:aWindow atIndex:CPNotFound];
236 #endif
237 }
238 
239 - (void)setLevel:(CPInteger)aLevel
240 {
241  _level = aLevel;
242 
243 #if PLATFORM(DOM)
244  if (_DOMWindow && _DOMWindow.cpSetLevel)
245  _DOMWindow.cpSetLevel(aLevel);
246 #endif
247 }
248 
249 - (void)setHasShadow:(BOOL)shouldHaveShadow
250 {
251  _hasShadow = shouldHaveShadow;
252 
253 #if PLATFORM(DOM)
254  if (_DOMWindow && _DOMWindow.cpSetHasShadow)
255  _DOMWindow.cpSetHasShadow(shouldHaveShadow);
256 #endif
257 }
258 
259 - (void)setShadowStyle:(int)aStyle
260 {
261  _shadowStyle = aStyle;
262 
263 #if PLATFORM(DOM)
264  if (_DOMWindow && _DOMWindow.cpSetShadowStyle)
265  _shadowStyle.cpSetShadowStyle(aStyle);
266 #endif
267 }
268 
269 - (BOOL)supportsFullPlatformWindows
270 {
271  return [CPPlatform isBrowser];
272 }
273 
274 - (void)_setTitle:(CPString)aTitle window:(CPWindow)aWindow
275 {
276  _title = aTitle;
277 
278 #if PLATFORM(DOM)
279  if (_DOMWindow &&
280  _DOMWindow.document &&
281  ([aWindow isFullPlatformWindow]))
282  {
283  _DOMWindow.document.title = _title;
284  }
285 #endif
286 }
287 
288 - (CPString)title
289 {
290  return _title;
291 }
292 
293 - (BOOL)_canUpdateContentRect
294 {
295  // We onyl update the contentRect with the frame of the bridgeless window if we have initialized the platform with the method initWithWindow:
296  return _shouldUpdateContentRect && _hasInitializeInstanceWithWindow;
297 }
298 
299 - (BOOL)_hasInitializeInstanceWithWindow
300 {
301  return _hasInitializeInstanceWithWindow;
302 }
303 
304 - (void)_setShouldUpdateContentRect:(BOOL)aBoolean
305 {
306  _shouldUpdateContentRect = aBoolean;
307 }
308 
309 @end
310 
311 #if PLATFORM(BROWSER)
312 #endif