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_HANDLE (Cocoa_Window, Aspect_Window)
33 IMPLEMENT_STANDARD_RTTIEXT(Cocoa_Window, Aspect_Window)
35 #if defined(__clang__) && (__clang_major__ >= 4) && __has_feature(objc_arc)
39 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
42 static Standard_Integer getScreenBottom()
44 Cocoa_LocalPool aLocalPool;
45 NSArray* aScreens = [NSScreen screens];
46 if (aScreens == NULL || [aScreens count] == 0)
51 NSScreen* aScreen = (NSScreen* )[aScreens objectAtIndex: 0];
52 NSDictionary* aDict = [aScreen deviceDescription];
53 NSNumber* aNumber = [aDict objectForKey: @"NSScreenNumber"];
55 || [aNumber isKindOfClass: [NSNumber class]] == NO)
60 CGDirectDisplayID aDispId = [aNumber unsignedIntValue];
61 CGRect aRect = CGDisplayBounds(aDispId);
62 return Standard_Integer(aRect.origin.y + aRect.size.height);
66 // =======================================================================
67 // function : Cocoa_Window
69 // =======================================================================
70 Cocoa_Window::Cocoa_Window (const Standard_CString theTitle,
71 const Standard_Integer thePxLeft,
72 const Standard_Integer thePxTop,
73 const Standard_Integer thePxWidth,
74 const Standard_Integer thePxHeight)
76 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
82 myXRight (thePxLeft + thePxWidth),
83 myYBottom (thePxTop + thePxHeight)
85 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
88 if (thePxWidth <= 0 || thePxHeight <= 0)
90 Aspect_WindowDefinitionError::Raise ("Coordinate(s) out of range");
92 else if (NSApp == NULL)
94 Aspect_WindowDefinitionError::Raise ("Cocoa application should be instantiated before window");
98 // convert top-bottom coordinates to bottom-top (Cocoa)
99 myYTop = getScreenBottom() - myYBottom;
100 myYBottom = myYTop + thePxHeight;
102 Cocoa_LocalPool aLocalPool;
103 NSUInteger aWinStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
104 NSRect aRectNs = NSMakeRect (float(myXLeft), float(myYTop), float(thePxWidth), float(thePxHeight));
105 myHWindow = [[NSWindow alloc] initWithContentRect: aRectNs
107 backing: NSBackingStoreBuffered
109 if (myHWindow == NULL)
111 Aspect_WindowDefinitionError::Raise ("Unable to create window");
113 myHView = [[myHWindow contentView] retain];
115 NSString* aTitleNs = [[NSString alloc] initWithUTF8String: theTitle];
116 [myHWindow setTitle: aTitleNs];
119 // do not destroy NSWindow on close - we didn't handle it!
120 [myHWindow setReleasedWhenClosed: NO];
124 // =======================================================================
125 // function : Cocoa_Window
127 // =======================================================================
128 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
129 Cocoa_Window::Cocoa_Window (UIView* theViewNS)
132 Cocoa_Window::Cocoa_Window (NSView* theViewNS)
142 #if defined(HAVE_OBJC_ARC)
145 myHView = [theViewNS retain];
150 // =======================================================================
151 // function : Destroy
153 // =======================================================================
154 void Cocoa_Window::Destroy()
156 #if !defined(HAVE_OBJC_ARC)
157 Cocoa_LocalPool aLocalPool;
159 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
160 if (myHWindow != NULL)
162 #if !defined(HAVE_OBJC_ARC)
171 #if !defined(HAVE_OBJC_ARC)
178 // =======================================================================
179 // function : SetHView
181 // =======================================================================
182 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
183 void Cocoa_Window::SetHView (UIView* theView)
186 void Cocoa_Window::SetHView (NSView* theView)
188 if (myHWindow != NULL)
190 [myHWindow setContentView: theView];
194 #if defined(HAVE_OBJC_ARC)
202 myHView = [theView retain];
206 // =======================================================================
207 // function : IsMapped
209 // =======================================================================
210 Standard_Boolean Cocoa_Window::IsMapped() const
214 return Standard_True;
217 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
218 return myHView != NULL;
220 return myHView != NULL
221 && [[myHView window] isVisible];
225 // =======================================================================
228 // =======================================================================
229 void Cocoa_Window::Map() const
238 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
241 [[myHView window] orderFront: NULL];
246 // =======================================================================
249 // =======================================================================
250 void Cocoa_Window::Unmap() const
254 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
257 [[myHView window] orderOut: NULL];
262 // =======================================================================
263 // function : DoResize
265 // =======================================================================
266 Aspect_TypeOfResize Cocoa_Window::DoResize() const
270 return Aspect_TOR_UNKNOWN;
273 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
274 CGRect aBounds = [myHView bounds];
276 NSRect aBounds = [myHView bounds];
278 Standard_Integer aMask = 0;
279 Aspect_TypeOfResize aMode = Aspect_TOR_UNKNOWN;
281 if (Abs ((Standard_Integer )aBounds.origin.x - myXLeft ) > 2) aMask |= 1;
282 if (Abs ((Standard_Integer )(aBounds.origin.x + aBounds.size.width) - myXRight ) > 2) aMask |= 2;
283 if (Abs ((Standard_Integer )aBounds.origin.y - myYTop ) > 2) aMask |= 4;
284 if (Abs ((Standard_Integer )(aBounds.origin.y + aBounds.size.height) - myYBottom) > 2) aMask |= 8;
287 case 0: aMode = Aspect_TOR_NO_BORDER; break;
288 case 1: aMode = Aspect_TOR_LEFT_BORDER; break;
289 case 2: aMode = Aspect_TOR_RIGHT_BORDER; break;
290 case 4: aMode = Aspect_TOR_TOP_BORDER; break;
291 case 5: aMode = Aspect_TOR_LEFT_AND_TOP_BORDER; break;
292 case 6: aMode = Aspect_TOR_TOP_AND_RIGHT_BORDER; break;
293 case 8: aMode = Aspect_TOR_BOTTOM_BORDER; break;
294 case 9: aMode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER; break;
295 case 10: aMode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER; break;
299 *((Standard_Integer* )&myXLeft ) = (Standard_Integer )aBounds.origin.x;
300 *((Standard_Integer* )&myXRight ) = (Standard_Integer )(aBounds.origin.x + aBounds.size.width);
301 *((Standard_Integer* )&myYTop ) = (Standard_Integer )aBounds.origin.y;
302 *((Standard_Integer* )&myYBottom ) = (Standard_Integer )(aBounds.origin.y + aBounds.size.height);
306 // =======================================================================
307 // function : DoMapping
309 // =======================================================================
310 Standard_Boolean Cocoa_Window::DoMapping() const
312 return Standard_True;
315 // =======================================================================
318 // =======================================================================
319 Quantity_Ratio Cocoa_Window::Ratio() const
326 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
327 CGRect aBounds = [myHView bounds];
329 NSRect aBounds = [myHView bounds];
331 return Quantity_Ratio (aBounds.size.width / aBounds.size.height);
334 // =======================================================================
335 // function : Position
337 // =======================================================================
338 void Cocoa_Window::Position (Standard_Integer& X1, Standard_Integer& Y1,
339 Standard_Integer& X2, Standard_Integer& Y2) const
341 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
342 CGRect aBounds = [myHView bounds];
345 X2 = (Standard_Integer )aBounds.size.width;
346 Y2 = (Standard_Integer )aBounds.size.height;
348 NSWindow* aWindow = [myHView window];
349 NSRect aWindowRect = [aWindow frame];
350 X1 = (Standard_Integer) aWindowRect.origin.x;
351 Y1 = getScreenBottom() - (Standard_Integer) aWindowRect.origin.y - (Standard_Integer) aWindowRect.size.height;
352 X2 = X1 + (Standard_Integer) aWindowRect.size.width;
353 Y2 = Y1 + (Standard_Integer) aWindowRect.size.height;
357 // =======================================================================
360 // =======================================================================
361 void Cocoa_Window::Size (Standard_Integer& theWidth,
362 Standard_Integer& theHeight) const
369 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
370 CGRect aBounds = [myHView bounds];
372 NSRect aBounds = [myHView bounds];
374 theWidth = (Standard_Integer )aBounds.size.width;
375 theHeight = (Standard_Integer )aBounds.size.height;