1 // Created on: 2012-11-12
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #import <Cocoa/Cocoa.h>
22 #include <Cocoa_Window.hxx>
24 #include <Cocoa_LocalPool.hxx>
26 #include <Image_AlienPixMap.hxx>
27 #include <Aspect_Convert.hxx>
28 #include <Aspect_GraphicDevice.hxx>
29 #include <Aspect_WindowDefinitionError.hxx>
31 IMPLEMENT_STANDARD_HANDLE (Cocoa_Window, Aspect_Window)
32 IMPLEMENT_STANDARD_RTTIEXT(Cocoa_Window, Aspect_Window)
34 //! Dummy device class implementation
35 class Cocoa_GraphicDevice : public Aspect_GraphicDevice
40 virtual Handle_Aspect_GraphicDriver GraphicDriver() const
45 DEFINE_STANDARD_RTTI(Cocoa_GraphicDevice)
49 DEFINE_STANDARD_HANDLE(Cocoa_GraphicDevice, Aspect_GraphicDevice)
51 IMPLEMENT_STANDARD_HANDLE (Cocoa_GraphicDevice, Aspect_GraphicDevice)
52 IMPLEMENT_STANDARD_RTTIEXT(Cocoa_GraphicDevice, Aspect_GraphicDevice)
54 static Standard_Integer getScreenBottom()
56 Cocoa_LocalPool aLocalPool;
57 NSArray* aScreens = [NSScreen screens];
58 if (aScreens == NULL || [aScreens count] == 0)
63 NSScreen* aScreen = (NSScreen* )[aScreens objectAtIndex: 0];
64 NSDictionary* aDict = [aScreen deviceDescription];
65 NSNumber* aNumber = [aDict objectForKey: @"NSScreenNumber"];
67 || [aNumber isKindOfClass: [NSNumber class]] == NO)
72 CGDirectDisplayID aDispId = [aNumber unsignedIntValue];
73 CGRect aRect = CGDisplayBounds(aDispId);
74 return Standard_Integer(aRect.origin.y + aRect.size.height);
77 // =======================================================================
78 // function : Cocoa_Window
80 // =======================================================================
81 Cocoa_Window::Cocoa_Window (const Standard_CString theTitle,
82 const Standard_Integer thePxLeft,
83 const Standard_Integer thePxTop,
84 const Standard_Integer thePxWidth,
85 const Standard_Integer thePxHeight)
86 : Aspect_Window (new Cocoa_GraphicDevice()),
91 myXRight (thePxLeft + thePxWidth),
92 myYBottom (thePxTop + thePxHeight)
94 if (thePxWidth <= 0 || thePxHeight <= 0)
96 Aspect_WindowDefinitionError::Raise ("Coordinate(s) out of range");
98 else if (NSApp == NULL)
100 Aspect_WindowDefinitionError::Raise ("Cocoa application should be instantiated before window");
104 // convert top-bottom coordinates to bottom-top (Cocoa)
105 myYTop = getScreenBottom() - myYBottom;
106 myYBottom = myYTop + thePxHeight;
108 Cocoa_LocalPool aLocalPool;
109 NSUInteger aWinStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
110 NSRect aRectNs = NSMakeRect (float(myXLeft), float(myYTop), float(thePxWidth), float(thePxHeight));
111 myHWindow = [[NSWindow alloc] initWithContentRect: aRectNs
113 backing: NSBackingStoreBuffered
115 if (myHWindow == NULL)
117 Aspect_WindowDefinitionError::Raise ("Unable to create window");
119 myHView = [[myHWindow contentView] retain];
121 NSString* aTitleNs = [[NSString alloc] initWithUTF8String: theTitle];
122 [myHWindow setTitle: aTitleNs];
125 // do not destroy NSWindow on close - we didn't handle it!
126 [myHWindow setReleasedWhenClosed: NO];
129 // =======================================================================
130 // function : Cocoa_Window
132 // =======================================================================
133 Cocoa_Window::Cocoa_Window (NSView* theViewNS)
134 : Aspect_Window (new Cocoa_GraphicDevice()),
136 myHView ([theViewNS retain]),
145 // =======================================================================
146 // function : Destroy
148 // =======================================================================
149 void Cocoa_Window::Destroy()
151 Cocoa_LocalPool aLocalPool;
152 if (myHWindow != NULL)
165 // =======================================================================
168 // =======================================================================
169 NSView* Cocoa_Window::HView() const
174 // =======================================================================
175 // function : SetHView
177 // =======================================================================
178 void Cocoa_Window::SetHView (NSView* theView)
180 if (myHWindow != NULL)
182 [myHWindow setContentView: theView];
189 myHView = [theView retain];
192 // =======================================================================
193 // function : DoubleBuffer
195 // =======================================================================
196 Standard_Boolean Cocoa_Window::DoubleBuffer() const
198 return Standard_True;
201 // =======================================================================
202 // function : SetBackground
204 // =======================================================================
205 void Cocoa_Window::SetBackground (const Aspect_Background& theBackground)
207 SetBackground (theBackground.Color());
210 // =======================================================================
211 // function : SetBackground
213 // =======================================================================
214 void Cocoa_Window::SetBackground (const Quantity_NameOfColor theBackColor)
216 SetBackground (Quantity_Color (theBackColor));
219 // =======================================================================
220 // function : SetBackground
222 // =======================================================================
223 void Cocoa_Window::SetBackground (const Aspect_Handle theBackPixmap)
228 // =======================================================================
229 // function : SetBackground
231 // =======================================================================
232 Standard_Boolean Cocoa_Window::SetBackground (const Standard_CString theFileName,
233 const Aspect_FillMethod theMethod)
235 return Standard_False;
238 // =======================================================================
239 // function : SetBackground
241 // =======================================================================
242 void Cocoa_Window::SetBackground (const Quantity_Color& theColor)
247 // =======================================================================
248 // function : SetBackground
250 // =======================================================================
251 void Cocoa_Window::SetBackground (const Aspect_GradientBackground& theGrBackground)
253 Quantity_Color aColor1, aColor2;
254 theGrBackground.Colors (aColor1, aColor2);
255 SetBackground (aColor1, aColor2, theGrBackground.BgGradientFillMethod());
258 // =======================================================================
259 // function : SetBackground
261 // =======================================================================
262 void Cocoa_Window::SetBackground (const Quantity_Color& theColor1,
263 const Quantity_Color& theColor2,
264 const Aspect_GradientFillMethod theMethod)
269 // =======================================================================
270 // function : SetDoubleBuffer
272 // =======================================================================
273 void Cocoa_Window::SetDoubleBuffer (const Standard_Boolean )
278 // =======================================================================
281 // =======================================================================
282 void Cocoa_Window::Flush() const
287 // =======================================================================
288 // function : IsMapped
290 // =======================================================================
291 Standard_Boolean Cocoa_Window::IsMapped() const
295 return Standard_True;
298 return (myHView != NULL) && [[myHView window] isVisible];
301 // =======================================================================
304 // =======================================================================
305 void Cocoa_Window::Map() const
314 [[myHView window] orderFront: NULL];
318 // =======================================================================
321 // =======================================================================
322 void Cocoa_Window::Unmap() const
326 [[myHView window] orderOut: NULL];
330 // =======================================================================
331 // function : DoResize
333 // =======================================================================
334 Aspect_TypeOfResize Cocoa_Window::DoResize() const
338 return Aspect_TOR_UNKNOWN;
341 NSRect aBounds = [myHView bounds];
342 Standard_Integer aMask = 0;
343 Aspect_TypeOfResize aMode = Aspect_TOR_UNKNOWN;
345 if (Abs ((Standard_Integer )aBounds.origin.x - myXLeft ) > 2) aMask |= 1;
346 if (Abs ((Standard_Integer )(aBounds.origin.x + aBounds.size.width) - myXRight ) > 2) aMask |= 2;
347 if (Abs ((Standard_Integer )aBounds.origin.y - myYTop ) > 2) aMask |= 4;
348 if (Abs ((Standard_Integer )(aBounds.origin.y + aBounds.size.height) - myYBottom) > 2) aMask |= 8;
351 case 0: aMode = Aspect_TOR_NO_BORDER; break;
352 case 1: aMode = Aspect_TOR_LEFT_BORDER; break;
353 case 2: aMode = Aspect_TOR_RIGHT_BORDER; break;
354 case 4: aMode = Aspect_TOR_TOP_BORDER; break;
355 case 5: aMode = Aspect_TOR_LEFT_AND_TOP_BORDER; break;
356 case 6: aMode = Aspect_TOR_TOP_AND_RIGHT_BORDER; break;
357 case 8: aMode = Aspect_TOR_BOTTOM_BORDER; break;
358 case 9: aMode = Aspect_TOR_BOTTOM_AND_LEFT_BORDER; break;
359 case 10: aMode = Aspect_TOR_RIGHT_AND_BOTTOM_BORDER; break;
363 *((Standard_Integer* )&myXLeft ) = (Standard_Integer )aBounds.origin.x;
364 *((Standard_Integer* )&myXRight ) = (Standard_Integer )(aBounds.origin.x + aBounds.size.width);
365 *((Standard_Integer* )&myYTop ) = (Standard_Integer )aBounds.origin.y;
366 *((Standard_Integer* )&myYBottom ) = (Standard_Integer )(aBounds.origin.y + aBounds.size.height);
370 // =======================================================================
371 // function : DoMapping
373 // =======================================================================
374 Standard_Boolean Cocoa_Window::DoMapping() const
376 return Standard_True;
379 // =======================================================================
382 // =======================================================================
383 void Cocoa_Window::Clear() const
388 // =======================================================================
389 // function : ClearArea
391 // =======================================================================
392 void Cocoa_Window::ClearArea (const Standard_Integer Xc,
393 const Standard_Integer Yc,
394 const Standard_Integer Width,
395 const Standard_Integer Height) const
400 // =======================================================================
401 // function : Restore
403 // =======================================================================
404 void Cocoa_Window::Restore() const
409 // =======================================================================
410 // function : RestoreArea
412 // =======================================================================
413 void Cocoa_Window::RestoreArea (const Standard_Integer Xc,
414 const Standard_Integer Yc,
415 const Standard_Integer Width,
416 const Standard_Integer Height) const
421 // =======================================================================
424 // =======================================================================
425 Standard_Boolean Cocoa_Window::Dump (const Standard_CString theFilename,
426 const Standard_Real theGammaValue) const
428 /*Image_AlienPixMap anImg;
429 if (!ToPixMap (anImg) || anImg.IsEmpty())
431 return Standard_False;
433 if (Abs (theGammaValue - 1.0) > 0.001)
435 anImg.AdjustGamma (theGammaValue);
437 return anImg.Save (theFilename);*/
438 return Standard_False;
441 // =======================================================================
442 // function : DumpArea
444 // =======================================================================
445 Standard_Boolean Cocoa_Window::DumpArea (const Standard_CString theFilename,
446 const Standard_Integer theCenterX,
447 const Standard_Integer theCenterY,
448 const Standard_Integer theWidth,
449 const Standard_Integer theHeight,
450 const Standard_Real theGammaValue) const
452 return Standard_False;
455 // =======================================================================
456 // function : ToPixMap
458 // =======================================================================
459 /*Standard_Boolean Cocoa_Window::ToPixMap (Image_PixMap& thePixMap) const
461 return Standard_False;
464 // =======================================================================
467 // =======================================================================
468 Standard_Boolean Cocoa_Window::Load (const Standard_CString theFilename) const
470 return Standard_False;
473 // =======================================================================
474 // function : LoadArea
476 // =======================================================================
477 Standard_Boolean Cocoa_Window::LoadArea (const Standard_CString theFilename,
478 const Standard_Integer theCenterX,
479 const Standard_Integer theCenterY,
480 const Standard_Integer theWidth,
481 const Standard_Integer theHeight) const
483 return Standard_False;
486 // =======================================================================
487 // function : BackingStore
489 // =======================================================================
490 Standard_Boolean Cocoa_Window::BackingStore() const
492 return Standard_False;
495 // =======================================================================
498 // =======================================================================
499 Quantity_Ratio Cocoa_Window::Ratio() const
506 NSRect aBounds = [myHView bounds];
507 return Quantity_Ratio (aBounds.size.width / aBounds.size.height);
510 // =======================================================================
511 // function : Position
513 // =======================================================================
514 void Cocoa_Window::Position (Quantity_Parameter& X1, Quantity_Parameter& Y1,
515 Quantity_Parameter& X2, Quantity_Parameter& Y2) const
520 // =======================================================================
521 // function : Position
523 // =======================================================================
524 void Cocoa_Window::Position (Standard_Integer& X1, Standard_Integer& Y1,
525 Standard_Integer& X2, Standard_Integer& Y2) const
530 // =======================================================================
533 // =======================================================================
534 void Cocoa_Window::Size (Quantity_Parameter& theWidth,
535 Quantity_Parameter& theHeight) const
540 // =======================================================================
543 // =======================================================================
544 void Cocoa_Window::Size (Standard_Integer& theWidth,
545 Standard_Integer& theHeight) const
552 NSRect aBounds = [myHView bounds];
553 theWidth = (Standard_Integer )aBounds.size.width;
554 theHeight = (Standard_Integer )aBounds.size.height;
557 // =======================================================================
560 // =======================================================================
561 void Cocoa_Window::MMSize (Standard_Real& theWidth,
562 Standard_Real& theHeight) const
567 // =======================================================================
568 // function : Convert
570 // =======================================================================
571 Quantity_Parameter Cocoa_Window::Convert (const Standard_Integer PV) const
576 // =======================================================================
577 // function : Convert
579 // =======================================================================
580 Standard_Integer Cocoa_Window::Convert (const Quantity_Parameter DV) const
585 // =======================================================================
586 // function : Convert
588 // =======================================================================
589 void Cocoa_Window::Convert (const Standard_Integer PX,
590 const Standard_Integer PY,
591 Quantity_Parameter& DX,
592 Quantity_Parameter& DY) const
597 // =======================================================================
598 // function : Convert
600 // =======================================================================
601 void Cocoa_Window::Convert (const Quantity_Parameter DX,
602 const Quantity_Parameter DY,
603 Standard_Integer& PX,
604 Standard_Integer& PY) const