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
38 #if !defined(MAC_OS_X_VERSION_10_12) || (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12)
39 // replacements for macOS versions before 10.12
40 #define NSWindowStyleMaskResizable NSResizableWindowMask
41 #define NSWindowStyleMaskClosable NSClosableWindowMask
42 #define NSWindowStyleMaskTitled NSTitledWindowMask
45 static Standard_Integer getScreenBottom()
47 Cocoa_LocalPool aLocalPool;
48 NSArray* aScreens = [NSScreen screens];
49 if (aScreens == NULL || [aScreens count] == 0)
54 NSScreen* aScreen = (NSScreen* )[aScreens objectAtIndex: 0];
55 NSDictionary* aDict = [aScreen deviceDescription];
56 NSNumber* aNumber = [aDict objectForKey: @"NSScreenNumber"];
58 || [aNumber isKindOfClass: [NSNumber class]] == NO)
63 CGDirectDisplayID aDispId = [aNumber unsignedIntValue];
64 CGRect aRect = CGDisplayBounds(aDispId);
65 return Standard_Integer(aRect.origin.y + aRect.size.height);
69 // =======================================================================
70 // function : Cocoa_Window
72 // =======================================================================
73 Cocoa_Window::Cocoa_Window (const Standard_CString theTitle,
74 const Standard_Integer thePxLeft,
75 const Standard_Integer thePxTop,
76 const Standard_Integer thePxWidth,
77 const Standard_Integer thePxHeight)
79 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
85 myXRight (thePxLeft + thePxWidth),
86 myYBottom (thePxTop + thePxHeight)
88 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
91 if (thePxWidth <= 0 || thePxHeight <= 0)
93 throw Aspect_WindowDefinitionError("Coordinate(s) out of range");
95 else if (NSApp == NULL)
97 throw Aspect_WindowDefinitionError("Cocoa application should be instantiated before window");
101 // convert top-bottom coordinates to bottom-top (Cocoa)
102 myYTop = getScreenBottom() - myYBottom;
103 myYBottom = myYTop + thePxHeight;
105 Cocoa_LocalPool aLocalPool;
106 NSUInteger aWinStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable;
107 NSRect aRectNs = NSMakeRect (float(myXLeft), float(myYTop), float(thePxWidth), float(thePxHeight));
108 myHWindow = [[NSWindow alloc] initWithContentRect: aRectNs
110 backing: NSBackingStoreBuffered
112 if (myHWindow == NULL)
114 throw Aspect_WindowDefinitionError("Unable to create window");
116 myHView = [[myHWindow contentView] retain];
118 NSString* aTitleNs = [[NSString alloc] initWithUTF8String: theTitle];
119 [myHWindow setTitle: aTitleNs];
122 // do not destroy NSWindow on close - we didn't handle it!
123 [myHWindow setReleasedWhenClosed: NO];
127 // =======================================================================
128 // function : Cocoa_Window
130 // =======================================================================
131 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
132 Cocoa_Window::Cocoa_Window (UIView* theViewNS)
135 Cocoa_Window::Cocoa_Window (NSView* theViewNS)
145 #if defined(HAVE_OBJC_ARC)
148 myHView = [theViewNS retain];
153 // =======================================================================
154 // function : ~Cocoa_Window
156 // =======================================================================
157 Cocoa_Window::~Cocoa_Window()
159 #if !defined(HAVE_OBJC_ARC)
160 Cocoa_LocalPool aLocalPool;
162 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
163 if (myHWindow != NULL)
165 #if !defined(HAVE_OBJC_ARC)
174 #if !defined(HAVE_OBJC_ARC)
181 // =======================================================================
182 // function : SetHView
184 // =======================================================================
185 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
186 void Cocoa_Window::SetHView (UIView* theView)
189 void Cocoa_Window::SetHView (NSView* theView)
191 if (myHWindow != NULL)
193 [myHWindow setContentView: theView];
197 #if defined(HAVE_OBJC_ARC)
205 myHView = [theView retain];
209 // =======================================================================
210 // function : IsMapped
212 // =======================================================================
213 Standard_Boolean Cocoa_Window::IsMapped() const
217 return Standard_True;
220 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
221 return myHView != NULL;
223 return myHView != NULL
224 && [[myHView window] isVisible];
228 // =======================================================================
231 // =======================================================================
232 void Cocoa_Window::Map() const
241 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
244 [[myHView window] orderFront: NULL];
249 // =======================================================================
252 // =======================================================================
253 void Cocoa_Window::Unmap() const
257 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
260 [[myHView window] orderOut: NULL];
265 // =======================================================================
266 // function : DoResize
268 // =======================================================================
269 Aspect_TypeOfResize Cocoa_Window::DoResize() const
273 return Aspect_TOR_UNKNOWN;
276 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
277 CGRect aBounds = [myHView bounds];
279 NSRect aBounds = [myHView bounds];
281 Standard_Integer aMask = 0;
282 Aspect_TypeOfResize aMode = Aspect_TOR_UNKNOWN;
284 if (Abs ((Standard_Integer )aBounds.origin.x - myXLeft ) > 2) aMask |= 1;
285 if (Abs ((Standard_Integer )(aBounds.origin.x + aBounds.size.width) - myXRight ) > 2) aMask |= 2;
286 if (Abs ((Standard_Integer )aBounds.origin.y - myYTop ) > 2) aMask |= 4;
287 if (Abs ((Standard_Integer )(aBounds.origin.y + aBounds.size.height) - myYBottom) > 2) aMask |= 8;
290 case 0: aMode = Aspect_TOR_NO_BORDER; break;
291 case 1: aMode = Aspect_TOR_LEFT_BORDER; break;
292 case 2: aMode = Aspect_TOR_RIGHT_BORDER; break;
293 case 4: aMode = Aspect_TOR_TOP_BORDER; break;
294 case 5: aMode = Aspect_TOR_LEFT_AND_TOP_BORDER; break;
295 case 6: aMode = Aspect_TOR_TOP_AND_RIGHT_BORDER; break;
296 case 8: aMode = Aspect_TOR_BOTTOM_BORDER; break;
297 case 9: aMode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER; break;
298 case 10: aMode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER; break;
302 *((Standard_Integer* )&myXLeft ) = (Standard_Integer )aBounds.origin.x;
303 *((Standard_Integer* )&myXRight ) = (Standard_Integer )(aBounds.origin.x + aBounds.size.width);
304 *((Standard_Integer* )&myYTop ) = (Standard_Integer )aBounds.origin.y;
305 *((Standard_Integer* )&myYBottom ) = (Standard_Integer )(aBounds.origin.y + aBounds.size.height);
309 // =======================================================================
310 // function : DoMapping
312 // =======================================================================
313 Standard_Boolean Cocoa_Window::DoMapping() const
315 return Standard_True;
318 // =======================================================================
321 // =======================================================================
322 Standard_Real Cocoa_Window::Ratio() const
329 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
330 CGRect aBounds = [myHView bounds];
332 NSRect aBounds = [myHView bounds];
334 return Standard_Real (aBounds.size.width / aBounds.size.height);
337 // =======================================================================
338 // function : Position
340 // =======================================================================
341 void Cocoa_Window::Position (Standard_Integer& X1, Standard_Integer& Y1,
342 Standard_Integer& X2, Standard_Integer& Y2) const
344 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
345 CGRect aBounds = [myHView bounds];
348 X2 = (Standard_Integer )aBounds.size.width;
349 Y2 = (Standard_Integer )aBounds.size.height;
351 NSWindow* aWindow = [myHView window];
352 NSRect aWindowRect = [aWindow frame];
353 X1 = (Standard_Integer) aWindowRect.origin.x;
354 Y1 = getScreenBottom() - (Standard_Integer) aWindowRect.origin.y - (Standard_Integer) aWindowRect.size.height;
355 X2 = X1 + (Standard_Integer) aWindowRect.size.width;
356 Y2 = Y1 + (Standard_Integer) aWindowRect.size.height;
360 // =======================================================================
363 // =======================================================================
364 void Cocoa_Window::Size (Standard_Integer& theWidth,
365 Standard_Integer& theHeight) const
372 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
373 CGRect aBounds = [myHView bounds];
375 NSRect aBounds = [myHView bounds];
377 theWidth = (Standard_Integer )aBounds.size.width;
378 theHeight = (Standard_Integer )aBounds.size.height;