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(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
38 static Standard_Integer getScreenBottom()
40 Cocoa_LocalPool aLocalPool;
41 NSArray* aScreens = [NSScreen screens];
42 if (aScreens == NULL || [aScreens count] == 0)
47 NSScreen* aScreen = (NSScreen* )[aScreens objectAtIndex: 0];
48 NSDictionary* aDict = [aScreen deviceDescription];
49 NSNumber* aNumber = [aDict objectForKey: @"NSScreenNumber"];
51 || [aNumber isKindOfClass: [NSNumber class]] == NO)
56 CGDirectDisplayID aDispId = [aNumber unsignedIntValue];
57 CGRect aRect = CGDisplayBounds(aDispId);
58 return Standard_Integer(aRect.origin.y + aRect.size.height);
62 // =======================================================================
63 // function : Cocoa_Window
65 // =======================================================================
66 Cocoa_Window::Cocoa_Window (const Standard_CString theTitle,
67 const Standard_Integer thePxLeft,
68 const Standard_Integer thePxTop,
69 const Standard_Integer thePxWidth,
70 const Standard_Integer thePxHeight)
72 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
78 myXRight (thePxLeft + thePxWidth),
79 myYBottom (thePxTop + thePxHeight)
81 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
84 if (thePxWidth <= 0 || thePxHeight <= 0)
86 Aspect_WindowDefinitionError::Raise ("Coordinate(s) out of range");
88 else if (NSApp == NULL)
90 Aspect_WindowDefinitionError::Raise ("Cocoa application should be instantiated before window");
94 // convert top-bottom coordinates to bottom-top (Cocoa)
95 myYTop = getScreenBottom() - myYBottom;
96 myYBottom = myYTop + thePxHeight;
98 Cocoa_LocalPool aLocalPool;
99 NSUInteger aWinStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
100 NSRect aRectNs = NSMakeRect (float(myXLeft), float(myYTop), float(thePxWidth), float(thePxHeight));
101 myHWindow = [[NSWindow alloc] initWithContentRect: aRectNs
103 backing: NSBackingStoreBuffered
105 if (myHWindow == NULL)
107 Aspect_WindowDefinitionError::Raise ("Unable to create window");
109 myHView = [[myHWindow contentView] retain];
111 NSString* aTitleNs = [[NSString alloc] initWithUTF8String: theTitle];
112 [myHWindow setTitle: aTitleNs];
115 // do not destroy NSWindow on close - we didn't handle it!
116 [myHWindow setReleasedWhenClosed: NO];
120 // =======================================================================
121 // function : Cocoa_Window
123 // =======================================================================
124 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
125 Cocoa_Window::Cocoa_Window (UIView* theViewNS)
128 Cocoa_Window::Cocoa_Window (NSView* theViewNS)
138 #if defined(HAVE_OBJC_ARC)
141 myHView = [theViewNS retain];
146 // =======================================================================
147 // function : Destroy
149 // =======================================================================
150 void Cocoa_Window::Destroy()
152 #if !defined(HAVE_OBJC_ARC)
153 Cocoa_LocalPool aLocalPool;
155 #if !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
156 if (myHWindow != NULL)
158 #if !defined(HAVE_OBJC_ARC)
167 #if !defined(HAVE_OBJC_ARC)
174 // =======================================================================
175 // function : SetHView
177 // =======================================================================
178 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
179 void Cocoa_Window::SetHView (UIView* theView)
182 void Cocoa_Window::SetHView (NSView* theView)
184 if (myHWindow != NULL)
186 [myHWindow setContentView: theView];
190 #if defined(HAVE_OBJC_ARC)
198 myHView = [theView retain];
202 // =======================================================================
203 // function : IsMapped
205 // =======================================================================
206 Standard_Boolean Cocoa_Window::IsMapped() const
210 return Standard_True;
213 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
214 return myHView != NULL;
216 return myHView != NULL
217 && [[myHView window] isVisible];
221 // =======================================================================
224 // =======================================================================
225 void Cocoa_Window::Map() const
234 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
237 [[myHView window] orderFront: NULL];
242 // =======================================================================
245 // =======================================================================
246 void Cocoa_Window::Unmap() const
250 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
253 [[myHView window] orderOut: NULL];
258 // =======================================================================
259 // function : DoResize
261 // =======================================================================
262 Aspect_TypeOfResize Cocoa_Window::DoResize() const
266 return Aspect_TOR_UNKNOWN;
269 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
270 CGRect aBounds = [myHView bounds];
272 NSRect aBounds = [myHView bounds];
274 Standard_Integer aMask = 0;
275 Aspect_TypeOfResize aMode = Aspect_TOR_UNKNOWN;
277 if (Abs ((Standard_Integer )aBounds.origin.x - myXLeft ) > 2) aMask |= 1;
278 if (Abs ((Standard_Integer )(aBounds.origin.x + aBounds.size.width) - myXRight ) > 2) aMask |= 2;
279 if (Abs ((Standard_Integer )aBounds.origin.y - myYTop ) > 2) aMask |= 4;
280 if (Abs ((Standard_Integer )(aBounds.origin.y + aBounds.size.height) - myYBottom) > 2) aMask |= 8;
283 case 0: aMode = Aspect_TOR_NO_BORDER; break;
284 case 1: aMode = Aspect_TOR_LEFT_BORDER; break;
285 case 2: aMode = Aspect_TOR_RIGHT_BORDER; break;
286 case 4: aMode = Aspect_TOR_TOP_BORDER; break;
287 case 5: aMode = Aspect_TOR_LEFT_AND_TOP_BORDER; break;
288 case 6: aMode = Aspect_TOR_TOP_AND_RIGHT_BORDER; break;
289 case 8: aMode = Aspect_TOR_BOTTOM_BORDER; break;
290 case 9: aMode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER; break;
291 case 10: aMode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER; break;
295 *((Standard_Integer* )&myXLeft ) = (Standard_Integer )aBounds.origin.x;
296 *((Standard_Integer* )&myXRight ) = (Standard_Integer )(aBounds.origin.x + aBounds.size.width);
297 *((Standard_Integer* )&myYTop ) = (Standard_Integer )aBounds.origin.y;
298 *((Standard_Integer* )&myYBottom ) = (Standard_Integer )(aBounds.origin.y + aBounds.size.height);
302 // =======================================================================
303 // function : DoMapping
305 // =======================================================================
306 Standard_Boolean Cocoa_Window::DoMapping() const
308 return Standard_True;
311 // =======================================================================
314 // =======================================================================
315 Quantity_Ratio Cocoa_Window::Ratio() const
322 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
323 CGRect aBounds = [myHView bounds];
325 NSRect aBounds = [myHView bounds];
327 return Quantity_Ratio (aBounds.size.width / aBounds.size.height);
330 // =======================================================================
331 // function : Position
333 // =======================================================================
334 void Cocoa_Window::Position (Standard_Integer& X1, Standard_Integer& Y1,
335 Standard_Integer& X2, Standard_Integer& Y2) const
337 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
338 CGRect aBounds = [myHView bounds];
341 X2 = (Standard_Integer )aBounds.size.width;
342 Y2 = (Standard_Integer )aBounds.size.height;
344 NSWindow* aWindow = [myHView window];
345 NSRect aWindowRect = [aWindow frame];
346 X1 = (Standard_Integer) aWindowRect.origin.x;
347 Y1 = getScreenBottom() - (Standard_Integer) aWindowRect.origin.y - (Standard_Integer) aWindowRect.size.height;
348 X2 = X1 + (Standard_Integer) aWindowRect.size.width;
349 Y2 = Y1 + (Standard_Integer) aWindowRect.size.height;
353 // =======================================================================
356 // =======================================================================
357 void Cocoa_Window::Size (Standard_Integer& theWidth,
358 Standard_Integer& theHeight) const
365 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
366 CGRect aBounds = [myHView bounds];
368 NSRect aBounds = [myHView bounds];
370 theWidth = (Standard_Integer )aBounds.size.width;
371 theHeight = (Standard_Integer )aBounds.size.height;