0023682: Implement Draw_Window with Cocoa framework
authordbv <dbv@opencascade.com>
Fri, 18 Jan 2013 11:04:50 +0000 (15:04 +0400)
committerdbv <dbv@opencascade.com>
Fri, 18 Jan 2013 11:04:50 +0000 (15:04 +0400)
Implemented most Draw_Window functions with Cocoa. Still should be implemented: drawing rectangle when zooming, window screenshot function in QAxwd
Added workaround for drawing rectangle when zooming
Added small fixes

src/Draw/Draw_Viewer.cxx
src/Draw/Draw_Window.cxx
src/Draw/Draw_Window.hxx
src/Draw/Draw_Window_1.mm [new file with mode: 0644]
src/Draw/EXTERNLIB
src/Draw/FILES
src/TKDraw/EXTERNLIB

index c48f5cc..699fc59 100755 (executable)
@@ -77,7 +77,7 @@ class Draw_View : public Draw_Window {
              Standard_Integer W,
              Standard_Integer H);
 
-#ifdef WNT
+#if defined(_WIN32) || defined(__WIN32__)
     Draw_View(Standard_Integer i,
              Draw_Viewer* v,
              Standard_Integer X,
@@ -85,6 +85,14 @@ class Draw_View : public Draw_Window {
              Standard_Integer W,
              Standard_Integer H,
         HWND win);
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+  Draw_View(Standard_Integer i,
+            Draw_Viewer* v,
+            Standard_Integer X,
+            Standard_Integer Y,
+            Standard_Integer W,
+            Standard_Integer H,
+            NSWindow* theWindow);
 #endif
 
   Draw_View(Standard_Integer i,
@@ -139,7 +147,7 @@ Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v,
   Framex0=Framey0=Framex1=Framey1=0;
 }
 
-#ifdef WNT
+#if defined(_WIN32) || defined(__WIN32__)
 //=======================================================================
 //function : Draw_View
 //purpose  :
@@ -154,16 +162,24 @@ Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v,
 {
   Framex0=Framey0=Framex1=Framey1=0;
 }
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v,
+                     Standard_Integer X,
+                     Standard_Integer Y,
+                     Standard_Integer W,
+                     Standard_Integer H,
+                     NSWindow* theWindow) :
+Draw_Window(theWindow, "Win", X, Y, W, H), id(i), viewer(v)
+{
+  Framex0=Framey0=Framex1=Framey1=0;
+}
 #endif
 
 //=======================================================================
 //function : Draw_View
 //purpose  :
 //=======================================================================
-
-// Portage WNT
-
-#ifdef WNT
+#if defined(_WIN32) || defined (__WIN32__) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
 Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v, const char* w)
 #else
 Draw_View::Draw_View(Standard_Integer i, Draw_Viewer* v, const char* w) :
@@ -1034,9 +1050,9 @@ unsigned long Draw_Viewer::GetWindow (const Standard_Integer id) const
 {
   if (Draw_Batch) return 0;
   if (myViews[id]) {
-    #ifdef WNT
+  #if defined(_WIN32) || defined(__WIN32__)
     return (unsigned long)(myViews[id]->win);
-       #else
+       #elif !defined(__APPLE__) || defined(MACOSX_USE_GLX)
     return myViews[id]->win;
        #endif
   }
@@ -1240,7 +1256,7 @@ void Draw_Viewer::Select (Standard_Integer& id, Standard_Integer& X, Standard_In
 {
   if (Draw_Batch) return;
   Flush();
-#ifndef WNT
+#if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
   if (!wait) {
     if (id >=0 && id < MAXVIEW) {
       if (myViews[id]) myViews[id]->Wait(wait);
@@ -1298,6 +1314,31 @@ void Draw_Viewer::Select (Standard_Integer& id, Standard_Integer& X, Standard_In
     Y = -Y - myViews[id]->dY;
   }
   if (!wait) myViews[id]->Wait(!wait);
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+  Standard_Integer aWindowNumber;
+
+  id = MAXVIEW;
+  while (id >= MAXVIEW)
+  {
+    GetNextEvent(wait, aWindowNumber, X, Y, Button);
+    
+    if (Y < 0)
+    {
+      continue; // mouse clicked on window title
+    }
+
+    for (Standard_Integer anIter = 0; anIter < MAXVIEW; anIter++)
+    {
+      if (myViews[anIter] && myViews[anIter]->IsEqualWindows (aWindowNumber))
+      {
+        id = anIter;
+      }
+    }
+  }
+
+  X =  X - myViews[id]->dX;
+  Y = -Y - myViews[id]->dY;
+
 #else
   HANDLE hWnd;
 
index 8710e73..ab3c504 100755 (executable)
@@ -92,7 +92,7 @@ defaultPrompt:
     }
 }
 
-#ifndef WNT
+#if !defined(_WIN32) && !defined(__WIN32__)
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -181,7 +181,7 @@ Standard_Boolean Draw_BlackBackGround = Standard_True;
 //======================================================
 Draw_Window* Draw_Window::firstWindow = NULL;
 
-
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
 //=======================================================================
 //function : Draw_Window
 //purpose  :
@@ -750,6 +750,25 @@ Standard_Boolean Draw_Window::Save (const char* theFileName) const
   return anImage.Save (theFileName);
 }
 
+//=======================================================================
+//function : Wait
+//purpose  :
+//=======================================================================
+
+void Draw_Window::Wait (Standard_Boolean wait)
+{
+  Flush();
+  if (!wait) {
+    XSelectInput(Draw_WindowDisplay,win,
+                 ButtonPressMask|ExposureMask | StructureNotifyMask |
+                 PointerMotionMask);
+  }
+  else {
+    XSelectInput(Draw_WindowDisplay,win,
+                 ButtonPressMask|ExposureMask | StructureNotifyMask);
+  }
+}
+
 //=======================================================================
 //function : ProcessEvent
 //purpose  :
@@ -878,25 +897,6 @@ void Draw_Window::WConfigureNotify(const Standard_Integer,
 {
 }
 
-//=======================================================================
-//function : Wait
-//purpose  :
-//=======================================================================
-
-void Draw_Window::Wait (Standard_Boolean wait)
-{
-  Flush();
-  if (!wait) {
-        XSelectInput(Draw_WindowDisplay,win,
-                     ButtonPressMask|ExposureMask | StructureNotifyMask |
-                     PointerMotionMask);
-  }
-  else {
-        XSelectInput(Draw_WindowDisplay,win,
-                     ButtonPressMask|ExposureMask | StructureNotifyMask);
-  }
-}
-
 //=======================================================================
 //function : WUnmapNotify
 //purpose  :
@@ -940,6 +940,35 @@ static void ProcessEvents(ClientData,int)
   }
 }
 
+//======================================================
+// funtion : GetNextEvent()
+// purpose :
+//======================================================
+void GetNextEvent(Event& ev)
+{
+  XEvent xev;
+  XNextEvent(Draw_WindowDisplay, &xev);
+  switch(xev.type)
+  {
+    case ButtonPress :
+      ev.type = 4;
+      ev.window = xev.xbutton.window;
+      ev.button = xev.xbutton.button;
+      ev.x = xev.xbutton.x;
+      ev.y = xev.xbutton.y;
+      break;
+
+    case MotionNotify :
+      ev.type = 6;
+      ev.window = xev.xmotion.window;
+      ev.button = 0;
+      ev.x = xev.xmotion.x;
+      ev.y = xev.xmotion.y;
+      break;
+  }
+}
+#endif //__APPLE__
+
 //======================================================
 // funtion :Run_Appli
 // purpose :
@@ -974,6 +1003,7 @@ void Run_Appli(Standard_Boolean (*interprete) (const char*))
   // ConnectionNumber(Draw_WindowDisplay) is an int 32 bits
   //                    (void*) is a pointer      64 bits ???????
 
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
 #if TCL_MAJOR_VERSION  < 8
     Tk_CreateFileHandler((void*) ConnectionNumber(Draw_WindowDisplay),
                          TK_READABLE, ProcessEvents,(ClientData) 0 );
@@ -981,6 +1011,7 @@ void Run_Appli(Standard_Boolean (*interprete) (const char*))
     Tk_CreateFileHandler(ConnectionNumber(Draw_WindowDisplay),
                          TK_READABLE, ProcessEvents,(ClientData) 0 );
 #endif
+#endif // __APPLE__
 
 #endif
 
@@ -1059,6 +1090,7 @@ Standard_Boolean Init_Appli()
 
   Tk_GeometryRequest(mainWindow, 200, 200);
 
+#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
   if (Draw_WindowDisplay == NULL) {
     Draw_WindowDisplay = XOpenDisplay(NULL);
     // Replaced Tk_Display(mainWindow) with XOpenDisplay; On Mac OS X Tk_Display
@@ -1083,6 +1115,8 @@ Standard_Boolean Init_Appli()
   Draw_WindowScreen   = DefaultScreen(Draw_WindowDisplay);
   Draw_WindowColorMap = DefaultColormap(Draw_WindowDisplay,
                                         Draw_WindowScreen);
+#endif // __APPLE__
+
   tty = isatty(0);
   Tcl_SetVar(interp,"tcl_interactive",(char*)(tty ? "1" : "0"), TCL_GLOBAL_ONLY);
 //  Tcl_SetVar(interp,"tcl_interactive",tty ? "1" : "0", TCL_GLOBAL_ONLY);
@@ -1098,34 +1132,6 @@ void Destroy_Appli()
   //XCloseDisplay(Draw_WindowDisplay);
 }
 
-//======================================================
-// funtion : GetNextEvent()
-// purpose :
-//======================================================
-void GetNextEvent(Event& ev)
-{
-  XEvent xev;
-  XNextEvent(Draw_WindowDisplay, &xev);
-  switch(xev.type)
-  {
-      case ButtonPress :
-           ev.type = 4;
-           ev.window = xev.xbutton.window;
-           ev.button = xev.xbutton.button;
-           ev.x = xev.xbutton.x;
-           ev.y = xev.xbutton.y;
-           break;
-
-      case MotionNotify :
-           ev.type = 6;
-           ev.window = xev.xmotion.window;
-           ev.button = 0;
-           ev.x = xev.xmotion.x;
-           ev.y = xev.xmotion.y;
-           break;
-   }
-}
-
 /*
  *----------------------------------------------------------------------
  *
@@ -2024,7 +2030,7 @@ static DWORD WINAPI readStdinThreadFunc(VOID)
     while (console_semaphore != WAIT_CONSOLE_COMMAND)
       Sleep(100);
     //if (gets(console_command))
-       if (fgets(console_command,COMMAND_SIZE,stdin)) 
+       if (fgets(console_command,COMMAND_SIZE,stdin))
       {
         console_semaphore = HAS_CONSOLE_COMMAND;
       }
index 0278343..33bb1ac 100755 (executable)
@@ -25,7 +25,7 @@
 #include <Standard_Boolean.hxx>
 #include <Standard_Integer.hxx>
 
-#ifndef WNT
+#if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
 
 const Standard_Integer MAXCOLOR = 15;
 
@@ -189,7 +189,134 @@ void Destroy_Appli();
 //======================================================
 void GetNextEvent(Event&);
 
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+
+const Standard_Integer MAXCOLOR = 15;
+
+struct Segment
+{
+  Standard_Integer myXStart;
+  Standard_Integer myYStart;
+  Standard_Integer myXEnd;
+  Standard_Integer myYEnd;
+
+  void Init(short theXStart, short theYStart, short theXEnd, short theYEnd) {
+    myXStart = theXStart; myYStart = theYStart; myXEnd = theXEnd; myYEnd = theYEnd;
+  }
+
+};
+
+#ifdef __OBJC__
+  @class NSView;
+  @class NSWindow;
+  @class NSImage;
+  @class Draw_CocoaView;
 #else
+  struct NSView;
+  struct NSWindow;
+  struct NSImage;
+  struct Draw_CocoaView;
+#endif
+
+
+class Draw_Window
+{
+  public :
+
+  Draw_Window (); // the window is not initialized
+  Draw_Window (Standard_CString        theTitle,
+               const Standard_Integer& theXLeft = 0,  const Standard_Integer& theYTop   = 0,
+               const Standard_Integer& theWidth = 50, const Standard_Integer& theHeight = 50);
+
+  Draw_Window (NSWindow*               theWindow,     Standard_CString        theTitle,
+               const Standard_Integer& theXLeft = 0,  const Standard_Integer& theYTop   = 0,
+               const Standard_Integer& theWidth = 50, const Standard_Integer& theHeight = 50);
+
+  void Init (const Standard_Integer& theXLeft = 0,  const Standard_Integer& theYLeft  = 0,
+             const Standard_Integer& theWidth = 50, const Standard_Integer& theHeight = 50);
+
+  virtual ~Draw_Window ();
+
+  void SetPosition (const Standard_Integer& theNewXpos,
+                    const Standard_Integer& theNewYpos);
+
+  void SetDimension (const Standard_Integer& theNewWidth,
+                     const Standard_Integer& theNewHeight);
+
+  void GetPosition (Standard_Integer &thePosX,
+                    Standard_Integer &thePosY);
+
+  Standard_Integer HeightWin() const;
+  Standard_Integer WidthWin()  const;
+
+  void  SetTitle (Standard_CString theTitle);
+  Standard_CString GetTitle ();
+
+  void DisplayWindow();
+  void Hide();
+  void Destroy();
+  void Clear();
+
+  void InitBuffer();
+
+  static Standard_Boolean DefineColor (const Standard_Integer&, Standard_CString);
+  void SetColor     (const Standard_Integer& theColor);
+  void SetMode      (const Standard_Integer& theMode);
+  void DrawString   (const Standard_Integer& theX, const Standard_Integer& theY, char* theText);
+  void DrawSegments (Segment* theSegment, const Standard_Integer& theNumberOfElements);
+  void Redraw();
+  static void Flush();
+  
+  // save snapshot
+  Standard_Boolean Save (Standard_CString theFileName) const;
+  
+  Standard_Boolean IsEqualWindows (const Standard_Integer& theWindowNumber);
+
+private:
+  NSWindow*        myWindow;
+  Draw_CocoaView*  myView;
+  NSImage*         myImageBuffer;
+  Standard_Boolean myUseBuffer;
+  Standard_Integer myCurrentColor;
+
+  static Draw_Window* firstWindow;
+  Draw_Window*        nextWindow;
+  Draw_Window*        previousWindow;
+
+};
+
+//======================================================
+// funtion : Run_Appli
+// purpose : run the application
+//           interp will be called to interpret a command
+//           and return True if the command is complete
+//======================================================
+
+void Run_Appli(Standard_Boolean (*inteprete) (const char*));
+
+//======================================================
+// funtion : Init_Appli
+// purpose :
+//======================================================
+Standard_Boolean Init_Appli();
+
+//======================================================
+// funtion : Destroy_Appli()
+// purpose :
+//======================================================
+void Destroy_Appli();
+
+//======================================================
+// funtion : GetNextEvent()
+// purpose :
+//======================================================
+void GetNextEvent (Standard_Boolean  theWait,
+                   Standard_Integer& theWindowNumber,
+                   Standard_Integer& theX,
+                   Standard_Integer& theY,
+                   Standard_Integer& theButton);
+#else
+
 // Specifique WNT
 
 #include <windows.h>
diff --git a/src/Draw/Draw_Window_1.mm b/src/Draw/Draw_Window_1.mm
new file mode 100644 (file)
index 0000000..22c0f1d
--- /dev/null
@@ -0,0 +1,612 @@
+// Copyright 2012 OPEN CASCADE SAS
+//
+// The content of this file is subject to the Open CASCADE Technology Public
+// License Version 6.5 (the "License"). You may not use the content of this file
+// except in compliance with the License. Please obtain a copy of the License
+// at http://www.opencascade.org and read it completely before using this file.
+//
+// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
+// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+//
+// The Original Code and all software distributed under the License is
+// distributed on an "AS IS" basis, without warranty of any kind, and the
+// Initial Developer hereby disclaims all such warranties, including without
+// limitation, any warranties of merchantability, fitness for a particular
+// purpose or non-infringement. Please see the License for the specific terms
+// and conditions governing the rights and limitations under the License.
+
+#if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+
+#import <Cocoa/Cocoa.h>
+
+#include <Draw_Window.hxx>
+#include <Cocoa_LocalPool.hxx>
+
+@interface Draw_CocoaView : NSView
+{
+  NSImage* myImage;
+}
+
+- (void )setImage: (NSImage* )theImage;
+- (void )redraw;
+@end
+
+@implementation Draw_CocoaView
+
+- (void )setImage: (NSImage* )theImage
+{
+  [theImage retain];
+  [myImage release];
+  myImage = theImage;
+}
+
+- (BOOL )isFlipped
+{
+  return YES; // for drawing image from left-top corner
+}
+
+- (void )redraw
+{
+  [self setNeedsDisplay: YES];
+}
+
+- (void )drawRect: (NSRect )theRect
+{
+
+  NSRect aBounds = NSMakeRect (0.0, 0.0, myImage.size.width, myImage.size.height);
+
+  [myImage drawInRect: aBounds
+             fromRect: NSZeroRect
+            operation: NSCompositeSourceOver
+             fraction: 1
+       respectFlipped: YES
+                hints: nil];
+}
+
+- (void )dealloc
+{
+  [myImage release];
+  [super dealloc];
+}
+@end
+
+static Standard_Integer getScreenBottom()
+{
+  NSRect aRect = [[[NSScreen screens] objectAtIndex:0] frame];
+  Standard_Integer aScreenBottom = Standard_Integer(aRect.size.height + aRect.origin.y);
+  return aScreenBottom;
+}
+
+extern Standard_Boolean Draw_VirtualWindows;
+static Standard_Boolean Draw_IsInZoomingMode = Standard_False;
+
+Standard_Real Draw_RGBColorsArray[MAXCOLOR][3] = {{1.0,  1.0,  1.0},
+                                                  {1.0,  0.0,  0.0},
+                                                  {0.0,  1.0,  0.0},
+                                                  {0.0,  0.0,  1.0},
+                                                  {0.0,  1.0,  1.0},
+                                                  {1.0,  0.84, 0.0},
+                                                  {1.0,  0.0,  1.0},
+                                                  {1.0,  0.2,  0.7},
+                                                  {1.0,  0.65, 0.0},
+                                                  {1.0,  0.89, 0.88},
+                                                  {1.0,  0.63, 0.48},
+                                                  {0.78, 0.08, 0.52},
+                                                  {1.0,  1.0,  0.0},
+                                                  {0.94, 0.9,  0.55},
+                                                  {1.0,  0.5,  0.31}};
+
+//=======================================================================
+//function : Draw_Window
+//purpose  :
+//=======================================================================
+Draw_Window::Draw_Window() :
+  myWindow (NULL),
+  myView (NULL),
+  myImageBuffer (NULL),
+  myUseBuffer (Standard_False),
+  nextWindow (firstWindow),
+  previousWindow (NULL)
+{
+  if (firstWindow != NULL) firstWindow->previousWindow = this;
+  firstWindow = this;
+}
+
+//=======================================================================
+//function : Draw_Window
+//purpose  :
+//=======================================================================
+Draw_Window::Draw_Window (Standard_CString theTitle,
+                          const Standard_Integer& theXLeft, const Standard_Integer& theYTop,
+                          const Standard_Integer& theWidth, const Standard_Integer& theHeight):
+  myWindow (NULL),
+  myView (NULL),
+  myImageBuffer (NULL),
+  myUseBuffer (Standard_False),
+  nextWindow (firstWindow),
+  previousWindow (NULL)
+{
+  if (firstWindow != NULL) firstWindow->previousWindow = this;
+  firstWindow = this;
+  Init (theXLeft, theYTop, theWidth, theHeight);
+  SetTitle (theTitle);
+}
+
+Draw_Window::Draw_Window (NSWindow*               theWindow, Standard_CString        theTitle,
+                          const Standard_Integer& theXLeft,  const Standard_Integer& theYTop,
+                          const Standard_Integer& theWidth,  const Standard_Integer& theHeight):
+  myWindow (NULL),
+  myView (NULL),
+  myImageBuffer (NULL),
+  myUseBuffer (Standard_False),
+  nextWindow (firstWindow),
+  previousWindow (NULL)
+{
+  myWindow = [theWindow retain];
+  if (firstWindow != NULL) firstWindow->previousWindow = this;
+  firstWindow = this;
+  Init (theXLeft, theYTop, theWidth, theHeight);
+  SetTitle (theTitle);
+}
+
+//=======================================================================
+//function : ~Draw_Window
+//purpose  :
+//=======================================================================
+Draw_Window::~Draw_Window()
+{
+  if (previousWindow != NULL)
+  {
+    previousWindow->nextWindow = nextWindow;
+  }
+  else
+  {
+    firstWindow = nextWindow;
+  }
+
+  if (nextWindow != NULL)
+  {
+    nextWindow->previousWindow = previousWindow;
+  }
+
+  if (myWindow != NULL)
+  { 
+    [myWindow release];
+    myWindow = NULL;
+  }
+
+  if (myView != NULL)
+  {
+    [myView release];
+    myView = NULL;
+  }
+
+  if (myImageBuffer != NULL)
+  {
+    [myImageBuffer release];
+    myImageBuffer = NULL;
+  }
+}
+
+//=======================================================================
+//function : Init
+//purpose  :
+//=======================================================================
+void Draw_Window::Init (const Standard_Integer& theXLeft, const Standard_Integer& theYTop,
+                        const Standard_Integer& theWidth, const Standard_Integer& theHeight)
+{
+  Cocoa_LocalPool aLocalPool;
+
+  // converting left-bottom coordinate to left-top coordinate
+  Standard_Integer anYTop = getScreenBottom() - theYTop - theHeight;
+
+  if (myWindow == NULL)
+  {
+    NSRect     aRectNs   = NSMakeRect (theXLeft, anYTop, theWidth, theHeight);
+    NSUInteger aWinStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
+
+    myWindow = [[NSWindow alloc] initWithContentRect: aRectNs
+                                           styleMask: aWinStyle
+                                             backing: NSBackingStoreBuffered
+                                               defer: NO];
+  }
+
+  if (myView == NULL)
+  {
+    NSRect aBounds = [[myWindow contentView] bounds];
+    
+    myView = [[Draw_CocoaView alloc] initWithFrame: aBounds];
+    [myWindow setContentView: myView];
+  }
+
+  if (myImageBuffer == NULL)
+  {
+    NSRect aRectNs = [myView bounds];
+    myImageBuffer  = [[NSImage alloc] initWithSize: aRectNs.size];
+  }
+
+  [myView setImage: myImageBuffer];
+
+  myUseBuffer = Draw_VirtualWindows;
+
+  myCurrentColor = 3;
+
+  [myWindow setBackgroundColor: NSColor.blackColor];
+  [myWindow setReleasedWhenClosed: NO];
+}
+
+//=======================================================================
+//function : InitBuffer
+//purpose  :
+//=======================================================================
+void Draw_Window::InitBuffer()
+{
+  //
+}
+
+//=======================================================================
+//function : SetPosition
+//purpose  :
+//=======================================================================
+void Draw_Window::SetPosition (const Standard_Integer& theNewXpos,
+                               const Standard_Integer& theNewYpos)
+{
+  NSPoint aNewPosition = NSMakePoint (theNewXpos, theNewYpos);
+  [myWindow setFrameTopLeftPoint: aNewPosition];
+}
+
+//=======================================================================
+//function : SetDimension
+//purpose  :
+//=======================================================================
+void Draw_Window::SetDimension (const Standard_Integer& theNewWidth,
+                                const Standard_Integer& theNewHeight)
+{
+  NSRect aWindowRect = [myWindow frame];
+  Standard_Integer aNewY = aWindowRect.origin.y + aWindowRect.size.height - theNewHeight;
+  NSRect aNewContentRect = NSMakeRect (aWindowRect.origin.x, aNewY,
+                                       theNewWidth, theNewHeight);
+  [myWindow setFrame: aNewContentRect display: YES];
+}
+
+//=======================================================================
+//function : GetPosition
+//purpose  :
+//=======================================================================
+void Draw_Window::GetPosition (Standard_Integer &thePosX,
+                               Standard_Integer &thePosY)
+{
+  NSRect aWindowRect = [myWindow frame];
+  thePosX = aWindowRect.origin.x;
+  thePosY = getScreenBottom() - aWindowRect.origin.y - aWindowRect.size.height;
+}
+
+//=======================================================================
+//function : HeightWin
+//purpose  :
+//=======================================================================
+Standard_Integer Draw_Window::HeightWin() const
+{
+  NSRect aViewBounds = [myView bounds];
+  return aViewBounds.size.height;
+}
+
+//=======================================================================
+//function : WidthWin
+//purpose  :
+//=======================================================================
+Standard_Integer Draw_Window::WidthWin() const
+{
+  NSRect aViewBounds = [myView bounds];
+  return aViewBounds.size.width;
+}
+
+//=======================================================================
+//function : SetTitle
+//purpose  :
+//=======================================================================
+void Draw_Window::SetTitle (Standard_CString theTitle)
+{
+  NSString* aTitleNs = [[NSString alloc] initWithUTF8String: theTitle];
+  [myWindow setTitle: aTitleNs];
+  [aTitleNs release];
+}
+
+//=======================================================================
+//function : GetTitle
+//purpose  :
+//=======================================================================
+Standard_CString Draw_Window::GetTitle()
+{
+  Standard_CString aTitle = [[myWindow title] UTF8String];
+  return aTitle;
+}
+
+//=======================================================================
+//function :DefineColor
+//purpose  :
+//=======================================================================
+Standard_Boolean Draw_Window::DefineColor (const Standard_Integer&, Standard_CString)
+{
+  return Standard_True; // unused
+}
+
+//=======================================================================
+//function : DisplayWindow
+//purpose  :
+//=======================================================================
+void Draw_Window::DisplayWindow()
+{
+  if (Draw_VirtualWindows)
+  {
+    return;
+  }
+
+  if (myWindow != NULL)
+  {
+    [myWindow orderFront: NULL];
+  }
+}
+
+//=======================================================================
+//function : Hide
+//purpose  :
+//=======================================================================
+void Draw_Window::Hide()
+{
+  if (myWindow != NULL)
+  {
+    [myWindow orderOut: NULL];
+  }
+}
+
+//=======================================================================
+//function : Destroy
+//purpose  :
+//=======================================================================
+void Draw_Window::Destroy()
+{  
+  if (myWindow != NULL)
+  { 
+    [myWindow release];
+    myWindow = NULL;
+  }
+
+  if (myView != NULL)
+  {
+    [myView release];
+    myView = NULL;
+  }
+
+  if (myImageBuffer != NULL)
+  {
+    [myImageBuffer release];
+    myImageBuffer = NULL;
+  }
+}
+
+//=======================================================================
+//function : Clear
+//purpose  :
+//=======================================================================
+void Draw_Window::Clear()
+{
+  [myImageBuffer lockFocus];
+  [[NSColor blackColor] set];
+  NSRect anImageBounds = NSMakeRect (0.0, 0.0, myImageBuffer.size.width, myImageBuffer.size.height);
+  NSRectFill (anImageBounds);
+  [myImageBuffer unlockFocus];
+
+  if (!myUseBuffer)
+  {
+    [myView redraw];
+  }
+}
+
+//=======================================================================
+//function : Flush
+//purpose  :
+//=======================================================================
+void Draw_Window::Flush()
+{
+  //
+}
+
+//=======================================================================
+//function : DrawString
+//purpose  :
+//=======================================================================
+void Draw_Window::DrawString (const Standard_Integer& theXLeft, const Standard_Integer& theYTop, char* theText)
+{
+  Cocoa_LocalPool aLocalPool;
+
+  NSString* aTextNs = [[[NSString alloc] initWithUTF8String: theText] autorelease];
+  NSColor*  aColor  = [NSColor colorWithDeviceRed: Draw_RGBColorsArray[myCurrentColor][0]
+                                            green: Draw_RGBColorsArray[myCurrentColor][1]
+                                             blue: Draw_RGBColorsArray[myCurrentColor][2]
+                                            alpha: 1.0f];
+  NSDictionary* anAttributes = [[[NSDictionary alloc] initWithObjectsAndKeys: aColor, NSForegroundColorAttributeName, nil] autorelease];
+
+  [myImageBuffer lockFocus];
+  [aTextNs drawAtPoint: NSMakePoint (theXLeft, myImageBuffer.size.height - theYTop) withAttributes: anAttributes];
+  [myImageBuffer unlockFocus];
+
+  if (!myUseBuffer)
+  {
+    [myView redraw];
+  }
+}
+
+//=======================================================================
+//function : DrawSegments
+//purpose  :
+//=======================================================================
+void Draw_Window::DrawSegments (Segment *theSegment, const Standard_Integer& theNumberOfElements)
+{
+  Cocoa_LocalPool aLocalPool;
+
+  NSBezierPath* aPath = [[[NSBezierPath alloc] init] autorelease];
+
+  NSImage* anImage;
+  Standard_Integer anIter = 0;
+  
+  if (Draw_IsInZoomingMode)
+  {
+    // workaround for rectangle drawing when zooming
+    anImage = [[myImageBuffer copy] autorelease];
+    anIter  = 4;
+  }
+  else
+  {
+    anImage = myImageBuffer;
+  }
+
+
+  for (; anIter < theNumberOfElements; anIter++)
+  {
+    NSPoint aPoint = NSMakePoint (theSegment[anIter].myXStart, myImageBuffer.size.height - theSegment[anIter].myYStart);
+    [aPath moveToPoint: aPoint];
+    aPoint = NSMakePoint (theSegment[anIter].myXEnd, myImageBuffer.size.height - theSegment[anIter].myYEnd);
+    [aPath lineToPoint: aPoint];
+  }
+
+  [anImage lockFocus];
+  NSColor* aColor = [NSColor colorWithDeviceRed: Draw_RGBColorsArray[myCurrentColor][0]
+                                          green: Draw_RGBColorsArray[myCurrentColor][1]
+                                           blue: Draw_RGBColorsArray[myCurrentColor][2]
+                                          alpha: 1.0f];
+  [aColor set];
+  [aPath stroke];
+  [anImage unlockFocus];
+
+  if (!myUseBuffer)
+  {
+    [myView setImage: anImage];
+    [myView redraw];
+  }
+  
+  Draw_IsInZoomingMode = Standard_False;
+}
+
+//=======================================================================
+//function : Redraw
+//purpose  :
+//=======================================================================
+void Draw_Window::Redraw()
+{
+  if (myUseBuffer)
+  {
+    [myView redraw];
+  }
+}
+
+//=======================================================================
+//function : SetColor
+//purpose  :
+//=======================================================================
+void Draw_Window::SetColor (const Standard_Integer& theColor)
+{
+  myCurrentColor = theColor;
+}
+
+//=======================================================================
+//function : SetMode
+//purpose  :
+//=======================================================================
+void Draw_Window::SetMode (const Standard_Integer& theMode)
+{
+  //
+}
+
+//=======================================================================
+//function : Save
+//purpose  :
+//=======================================================================
+Standard_Boolean Draw_Window::Save (Standard_CString theFileName) const
+{
+  Cocoa_LocalPool aLocalPool;
+
+  NSString* aFileName = [[[NSString alloc] initWithUTF8String: theFileName] autorelease];
+  NSString* aFileExtension = [[aFileName pathExtension] lowercaseString];
+
+  NSDictionary* aFileTypeDict = [NSDictionary dictionaryWithObjectsAndKeys:
+                                  [NSNumber numberWithInt: NSPNGFileType],  @"png",
+                                  [NSNumber numberWithInt: NSBMPFileType],  @"bmp",
+                                  [NSNumber numberWithInt: NSJPEGFileType], @"jpg",
+                                  [NSNumber numberWithInt: NSGIFFileType],  @"gif",
+                                  nil];
+
+  NSBitmapImageFileType aFileType;
+
+  if ([aFileTypeDict valueForKey: aFileExtension] != nil)
+  {
+    aFileType = [[aFileTypeDict valueForKey: aFileExtension] intValue];
+  }
+  else
+  {
+    return Standard_False; // unsupported image extension
+  }
+
+  NSBitmapImageRep* anImageRep = [NSBitmapImageRep imageRepWithData: [myImageBuffer TIFFRepresentation]];
+
+  NSData* aData = [anImageRep representationUsingType: aFileType 
+                                           properties: nil];
+
+  Standard_Boolean isSuccess = [aData writeToFile: aFileName
+                                       atomically: NO];
+
+  return isSuccess;
+}
+
+Standard_Boolean Draw_Window::IsEqualWindows (const Standard_Integer& theWindowNumber)
+{
+  return ([myWindow windowNumber] == theWindowNumber);
+}
+
+void GetNextEvent (Standard_Boolean  theWait,
+                   Standard_Integer& theWindowNumber,
+                   Standard_Integer& theX,
+                   Standard_Integer& theY,
+                   Standard_Integer& theButton)
+{
+  Cocoa_LocalPool aLocalPool;
+
+  unsigned int anEventMatchMask = NSLeftMouseDownMask | NSRightMouseDownMask;
+
+  if (!theWait)
+  {
+    anEventMatchMask = anEventMatchMask | NSMouseMovedMask | NSLeftMouseDraggedMask;
+    Draw_IsInZoomingMode = Standard_True;
+  }
+
+  NSEvent* anEvent = [NSApp nextEventMatchingMask: anEventMatchMask
+                                        untilDate: [NSDate distantFuture]
+                                           inMode: NSEventTrackingRunLoopMode
+                                          dequeue: YES];
+
+  NSWindow* aWindow = [anEvent window];
+  NSView*   aView   = [aWindow contentView];
+  theWindowNumber   = [aWindow windowNumber];
+
+  NSPoint aMouseLoc = [aView convertPoint: [anEvent locationInWindow] fromView: nil];
+  NSRect  aBounds   = [aView bounds];
+
+  theX = Standard_Integer (aMouseLoc.x);
+  theY = Standard_Integer (aMouseLoc.y);
+
+  NSEventType anEventType = [anEvent type];
+
+  if (anEventType == NSLeftMouseDown)
+  {
+    theButton = 1;
+  }
+  else if (anEventType == NSRightMouseDown)
+  {
+    theButton = 3;
+  }
+  else if ((anEventType == NSMouseMoved || anEventType == NSLeftMouseDragged) && !theWait)
+  {
+    theButton = 0;
+  }
+}
+#endif // __APPLE__
index 923d2d7..7f11200 100755 (executable)
@@ -4,3 +4,6 @@ CSF_gdi32
 CSF_advapi32
 CSF_user32
 CSF_TBB
+CSF_objc
+CSF_Appkit
+CSF_IOKit
index f8b7408..2e588e8 100755 (executable)
@@ -27,3 +27,4 @@ Draw_Main.hxx
 Draw_Main.cxx
 Draw_PluginMacro.hxx
 Draw_PloadCommands.cxx
+Draw_Window_1.mm
index 37eaf81..4eabe06 100755 (executable)
@@ -15,3 +15,6 @@ CSF_gdi32
 CSF_advapi32
 CSF_user32
 CSF_TBB
+CSF_objc
+CSF_Appkit
+CSF_IOKit