1 // Created on: 2012-11-12
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #import <TargetConditionals.h>
18 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
19 #import <UIKit/UIKit.h>
21 #import <Cocoa/Cocoa.h>
24 #include <Cocoa_Window.hxx>
26 #include <Cocoa_LocalPool.hxx>
28 #include <Image_AlienPixMap.hxx>
29 #include <Aspect_Convert.hxx>
30 #include <Aspect_WindowDefinitionError.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(Cocoa_Window,Aspect_Window)
34 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
37 static Standard_Integer getScreenBottom()
39 Cocoa_LocalPool aLocalPool;
40 NSArray* aScreens = [NSScreen screens];
41 if (aScreens == NULL || [aScreens count] == 0)
46 NSScreen* aScreen = (NSScreen* )[aScreens objectAtIndex: 0];
47 NSDictionary* aDict = [aScreen deviceDescription];
48 NSNumber* aNumber = [aDict objectForKey: @"NSScreenNumber"];
50 || [aNumber isKindOfClass: [NSNumber class]] == NO)
55 CGDirectDisplayID aDispId = [aNumber unsignedIntValue];
56 CGRect aRect = CGDisplayBounds(aDispId);
57 return Standard_Integer(aRect.origin.y + aRect.size.height);
61 // =======================================================================
62 // function : Cocoa_Window
64 // =======================================================================
65 Cocoa_Window::Cocoa_Window (const Standard_CString theTitle,
66 const Standard_Integer thePxLeft,
67 const Standard_Integer thePxTop,
68 const Standard_Integer thePxWidth,
69 const Standard_Integer thePxHeight)
71 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
77 myXRight (thePxLeft + thePxWidth),
78 myYBottom (thePxTop + thePxHeight)
80 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
83 if (thePxWidth <= 0 || thePxHeight <= 0)
85 throw Aspect_WindowDefinitionError("Coordinate(s) out of range");
87 else if (NSApp == NULL)
89 throw Aspect_WindowDefinitionError("Cocoa application should be instantiated before window");
93 // convert top-bottom coordinates to bottom-top (Cocoa)
94 myYTop = getScreenBottom() - myYBottom;
95 myYBottom = myYTop + thePxHeight;
97 Cocoa_LocalPool aLocalPool;
98 NSUInteger aWinStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
99 NSRect aRectNs = NSMakeRect (float(myXLeft), float(myYTop), float(thePxWidth), float(thePxHeight));
100 myHWindow = [[NSWindow alloc] initWithContentRect: aRectNs
102 backing: NSBackingStoreBuffered
104 if (myHWindow == NULL)
106 throw Aspect_WindowDefinitionError("Unable to create window");
108 myHView = [[myHWindow contentView] retain];
110 NSString* aTitleNs = [[NSString alloc] initWithUTF8String: theTitle];
111 [myHWindow setTitle: aTitleNs];
114 // do not destroy NSWindow on close - we didn't handle it!
115 [myHWindow setReleasedWhenClosed: NO];
119 // =======================================================================
120 // function : Cocoa_Window
122 // =======================================================================
123 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
124 Cocoa_Window::Cocoa_Window (UIView* theViewNS)
127 Cocoa_Window::Cocoa_Window (NSView* theViewNS)
137 #if defined(HAVE_OBJC_ARC)
140 myHView = [theViewNS retain];
145 // =======================================================================
146 // function : ~Cocoa_Window
148 // =======================================================================
149 Cocoa_Window::~Cocoa_Window()
151 #if !defined(HAVE_OBJC_ARC)
152 Cocoa_LocalPool aLocalPool;
154 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
155 if (myHWindow != NULL)
157 #if !defined(HAVE_OBJC_ARC)
166 #if !defined(HAVE_OBJC_ARC)
173 // =======================================================================
174 // function : SetHView
176 // =======================================================================
177 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
178 void Cocoa_Window::SetHView (UIView* theView)
181 void Cocoa_Window::SetHView (NSView* theView)
183 if (myHWindow != NULL)
185 [myHWindow setContentView: theView];
189 #if defined(HAVE_OBJC_ARC)
197 myHView = [theView retain];
201 // =======================================================================
202 // function : IsMapped
204 // =======================================================================
205 Standard_Boolean Cocoa_Window::IsMapped() const
209 return Standard_True;
212 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
213 return myHView != NULL;
215 return myHView != NULL
216 && [[myHView window] isVisible];
220 // =======================================================================
223 // =======================================================================
224 void Cocoa_Window::Map() const
233 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
236 [[myHView window] orderFront: NULL];
241 // =======================================================================
244 // =======================================================================
245 void Cocoa_Window::Unmap() const
249 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
252 [[myHView window] orderOut: NULL];
257 // =======================================================================
258 // function : DoResize
260 // =======================================================================
261 Aspect_TypeOfResize Cocoa_Window::DoResize() const
265 return Aspect_TOR_UNKNOWN;
268 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
269 CGRect aBounds = [myHView bounds];
271 NSRect aBounds = [myHView bounds];
273 Standard_Integer aMask = 0;
274 Aspect_TypeOfResize aMode = Aspect_TOR_UNKNOWN;
276 if (Abs ((Standard_Integer )aBounds.origin.x - myXLeft ) > 2) aMask |= 1;
277 if (Abs ((Standard_Integer )(aBounds.origin.x + aBounds.size.width) - myXRight ) > 2) aMask |= 2;
278 if (Abs ((Standard_Integer )aBounds.origin.y - myYTop ) > 2) aMask |= 4;
279 if (Abs ((Standard_Integer )(aBounds.origin.y + aBounds.size.height) - myYBottom) > 2) aMask |= 8;
282 case 0: aMode = Aspect_TOR_NO_BORDER; break;
283 case 1: aMode = Aspect_TOR_LEFT_BORDER; break;
284 case 2: aMode = Aspect_TOR_RIGHT_BORDER; break;
285 case 4: aMode = Aspect_TOR_TOP_BORDER; break;
286 case 5: aMode = Aspect_TOR_LEFT_AND_TOP_BORDER; break;
287 case 6: aMode = Aspect_TOR_TOP_AND_RIGHT_BORDER; break;
288 case 8: aMode = Aspect_TOR_BOTTOM_BORDER; break;
289 case 9: aMode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER; break;
290 case 10: aMode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER; break;
294 *((Standard_Integer* )&myXLeft ) = (Standard_Integer )aBounds.origin.x;
295 *((Standard_Integer* )&myXRight ) = (Standard_Integer )(aBounds.origin.x + aBounds.size.width);
296 *((Standard_Integer* )&myYTop ) = (Standard_Integer )aBounds.origin.y;
297 *((Standard_Integer* )&myYBottom ) = (Standard_Integer )(aBounds.origin.y + aBounds.size.height);
301 // =======================================================================
302 // function : DoMapping
304 // =======================================================================
305 Standard_Boolean Cocoa_Window::DoMapping() const
307 return Standard_True;
310 // =======================================================================
313 // =======================================================================
314 Quantity_Ratio Cocoa_Window::Ratio() const
321 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
322 CGRect aBounds = [myHView bounds];
324 NSRect aBounds = [myHView bounds];
326 return Quantity_Ratio (aBounds.size.width / aBounds.size.height);
329 // =======================================================================
330 // function : Position
332 // =======================================================================
333 void Cocoa_Window::Position (Standard_Integer& X1, Standard_Integer& Y1,
334 Standard_Integer& X2, Standard_Integer& Y2) const
336 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
337 CGRect aBounds = [myHView bounds];
340 X2 = (Standard_Integer )aBounds.size.width;
341 Y2 = (Standard_Integer )aBounds.size.height;
343 NSWindow* aWindow = [myHView window];
344 NSRect aWindowRect = [aWindow frame];
345 X1 = (Standard_Integer) aWindowRect.origin.x;
346 Y1 = getScreenBottom() - (Standard_Integer) aWindowRect.origin.y - (Standard_Integer) aWindowRect.size.height;
347 X2 = X1 + (Standard_Integer) aWindowRect.size.width;
348 Y2 = Y1 + (Standard_Integer) aWindowRect.size.height;
352 // =======================================================================
355 // =======================================================================
356 void Cocoa_Window::Size (Standard_Integer& theWidth,
357 Standard_Integer& theHeight) const
364 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
365 CGRect aBounds = [myHView bounds];
367 NSRect aBounds = [myHView bounds];
369 theWidth = (Standard_Integer )aBounds.size.width;
370 theHeight = (Standard_Integer )aBounds.size.height;