<commit_before>/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <vrj/Draw/OGL/Config.h>

#include <boost/bind.hpp>

#import <Foundation/NSArray.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSAutoreleasePool.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSOpenGL.h>
#import <AppKit/NSOpenGLView.h>
#import <AppKit/NSScreen.h>
#import <AppKit/NSApplication.h>
#import <OpenGL/GL.h>

#include <vpr/Thread/Thread.h>
#include <jccl/Config/ConfigElement.h>

#include <vrj/Display/DisplayManager.h>
#include <vrj/Util/Debug.h>
#import <vrj/Draw/OGL/GlViewCocoa.h>
#include <vrj/Draw/OGL/GlWindowCocoa.h>


/**
 * A simple subclass of NSWindow that overrides -canBecomeKeyWindow so that
 * it returns YES.
 */
@interface VrjCustomWindow : NSWindow
@end

@implementation VrjCustomWindow
   -(BOOL) canBecomeKeyWindow
   {
      return YES;
   }
@end

/**
 * An instance of this class is set as the delegate for the NSWindow object
 * assocated with each vrj::GlWindowCocoa instance.
 */
@interface GlWindowDelegate : NSObject
{
   vrj::GlWindowCocoa* mWindow;
}

   -(id) initWithWindow:(vrj::GlWindowCocoa*) window;
@end

@implementation GlWindowDelegate
   -(id) initWithWindow:(vrj::GlWindowCocoa*) window
   {
      mWindow = window;
      return [super init];
   }

   /**
    * Invoked when the Cocoa window is being closed. We tell our
    * vrj::GlWindowCocoa instance that it is being closed so that it does not
    * try to close itself again later. The big issue that this handles is that
    * the NSWindow instance is deallocated when the window gets closed. Hence,
    * the vrj::GlWindowCocoa instance cannot perform any further operations on
    * the window after this point.
    */
   -(void) windowWillClose:(NSNotification*) aNotification
   {
      mWindow->setWindowOpen(false);
   }

   /**
    * Invoked after the Cocoa window is moved.
    */
   -(void) windowDidMove:(NSNotification*) aNotification
   {
      // Inform our input area of the change in our bounds.
      NSRect b = [[aNotification object] frame];
      mWindow->updateBounds(b.origin.x, b.origin.y, b.size.width,
                            b.size.height);
   }
@end

namespace vrj
{

GlWindowCocoa::GlWindowCocoa()
   : vrj::GlWindow()
   , gadget::InputAreaCocoa()
   , mThread(NULL)
   , mScreen(0)
   , mWindowName(@"Cocoa OpenGL Window")
   , mRenderLock(nil)
   , mGlView(nil)
{
   /* Do nothing. */ ;
}

GlWindowCocoa::~GlWindowCocoa()
{
   if ( NULL != mThread )
   {
      delete mThread;
      mThread = NULL;
   }
}

bool GlWindowCocoa::open()
{
   if ( mWindowIsOpen )
   {
      return true;
   }

   if ( mWindowWidth == -1 )
   {
      vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
         << clrOutNORM(clrRED, "ERROR")
         << ": [vrj::GlWindowCocoa::open()] Window has not been configured\n"
         << vprDEBUG_FLUSH;
      return false;
   }

   vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_STATE_LVL)
      << "[vrj::GlWindowCocoa::open()] Open window\n" << vprDEBUG_FLUSH;

   mRenderLock = [[NSLock alloc] init];

   bool opened(false);

   @try
   {
      unsigned int style_mask(0);

      // If the window is borderless, set up the style mask to reflect that.
      // If the window is to be full screen, then we force it to be
      // borderless. This is to make Aqua happy.
      if ( ! mHasBorder || mIsFullScreen )
      {
         style_mask = NSBorderlessWindowMask;
      }
      else
      {
         // XXX: Resizing VR Juggler OpenGL windows crashes the window server.
//         style_mask = NSTitledWindowMask | NSResizableWindowMask |
//                         NSMiniaturizableWindowMask;
         style_mask = NSTitledWindowMask | NSMiniaturizableWindowMask;
      }

      NSRect content_rect = { {mOriginX, mOriginY},
                              {mWindowWidth, mWindowHeight} };

      // mScreen is already known to be a valid index.
      NSScreen* screen = [[NSScreen screens] objectAtIndex:mScreen];

      // Creating the window and its view does not open the window. That
      // happens when the -setContentView: message is delivered to
      // mCocoaWindow in finishOpen().
      mCocoaWindow =
         [[VrjCustomWindow alloc] initWithContentRect:content_rect
                                            styleMask:style_mask
                                              backing:NSBackingStoreBuffered
                                                defer:NO
                                               screen:screen];
      [mCocoaWindow setOneShot:YES];

      // Set the delegate for mCocoaWindow. In particular, we do this so that
      // we can be told if the window gets closed by some external entity.
      id delegate =
         [[[GlWindowDelegate alloc] initWithWindow:this] autorelease];
      [mCocoaWindow setDelegate:delegate];<commit_msg>MFT r20984: Resizing OpenGL windows works fine on Mac OS X Leopard.<commit_after>/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <vrj/Draw/OGL/Config.h>

#include <boost/bind.hpp>

#import <Foundation/NSArray.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSAutoreleasePool.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSOpenGL.h>
#import <AppKit/NSOpenGLView.h>
#import <AppKit/NSScreen.h>
#import <AppKit/NSApplication.h>
#import <OpenGL/GL.h>

#include <vpr/vpr.h>
#include <vpr/Thread/Thread.h>
#include <jccl/Config/ConfigElement.h>

#include <vrj/Display/DisplayManager.h>
#include <vrj/Util/Debug.h>
#import <vrj/Draw/OGL/GlViewCocoa.h>
#include <vrj/Draw/OGL/GlWindowCocoa.h>


/**
 * A simple subclass of NSWindow that overrides -canBecomeKeyWindow so that
 * it returns YES.
 */
@interface VrjCustomWindow : NSWindow
@end

@implementation VrjCustomWindow
   -(BOOL) canBecomeKeyWindow
   {
      return YES;
   }
@end

/**
 * An instance of this class is set as the delegate for the NSWindow object
 * assocated with each vrj::GlWindowCocoa instance.
 */
@interface GlWindowDelegate : NSObject
{
   vrj::GlWindowCocoa* mWindow;
}

   -(id) initWithWindow:(vrj::GlWindowCocoa*) window;
@end

@implementation GlWindowDelegate
   -(id) initWithWindow:(vrj::GlWindowCocoa*) window
   {
      mWindow = window;
      return [super init];
   }

   /**
    * Invoked when the Cocoa window is being closed. We tell our
    * vrj::GlWindowCocoa instance that it is being closed so that it does not
    * try to close itself again later. The big issue that this handles is that
    * the NSWindow instance is deallocated when the window gets closed. Hence,
    * the vrj::GlWindowCocoa instance cannot perform any further operations on
    * the window after this point.
    */
   -(void) windowWillClose:(NSNotification*) aNotification
   {
      mWindow->setWindowOpen(false);
   }

   /**
    * Invoked after the Cocoa window is moved.
    */
   -(void) windowDidMove:(NSNotification*) aNotification
   {
      // Inform our input area of the change in our bounds.
      NSRect b = [[aNotification object] frame];
      mWindow->updateBounds(b.origin.x, b.origin.y, b.size.width,
                            b.size.height);
   }
@end

namespace vrj
{

GlWindowCocoa::GlWindowCocoa()
   : vrj::GlWindow()
   , gadget::InputAreaCocoa()
   , mThread(NULL)
   , mScreen(0)
   , mWindowName(@"Cocoa OpenGL Window")
   , mRenderLock(nil)
   , mGlView(nil)
{
   /* Do nothing. */ ;
}

GlWindowCocoa::~GlWindowCocoa()
{
   if ( NULL != mThread )
   {
      delete mThread;
      mThread = NULL;
   }
}

bool GlWindowCocoa::open()
{
   if ( mWindowIsOpen )
   {
      return true;
   }

   if ( mWindowWidth == -1 )
   {
      vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
         << clrOutNORM(clrRED, "ERROR")
         << ": [vrj::GlWindowCocoa::open()] Window has not been configured\n"
         << vprDEBUG_FLUSH;
      return false;
   }

   vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_STATE_LVL)
      << "[vrj::GlWindowCocoa::open()] Open window\n" << vprDEBUG_FLUSH;

   mRenderLock = [[NSLock alloc] init];

   bool opened(false);

   @try
   {
      unsigned int style_mask(0);

      // If the window is borderless, set up the style mask to reflect that.
      // If the window is to be full screen, then we force it to be
      // borderless. This is to make Aqua happy.
      if ( ! mHasBorder || mIsFullScreen )
      {
         style_mask = NSBorderlessWindowMask;
      }
      else
      {
#if VPR_OS_RELEASE_MAJOR >= 9
         style_mask = NSTitledWindowMask | NSResizableWindowMask |
                         NSMiniaturizableWindowMask;
#else
         // Resizing VR Juggler OpenGL windows crashes the window server on
         // Mac OS X 10.4 (Tiger).
         style_mask = NSTitledWindowMask | NSMiniaturizableWindowMask;
#endif
      }

      NSRect content_rect = { {mOriginX, mOriginY},
                              {mWindowWidth, mWindowHeight} };

      // mScreen is already known to be a valid index.
      NSScreen* screen = [[NSScreen screens] objectAtIndex:mScreen];

      // Creating the window and its view does not open the window. That
      // happens when the -setContentView: message is delivered to
      // mCocoaWindow in finishOpen().
      mCocoaWindow =
         [[VrjCustomWindow alloc] initWithContentRect:content_rect
                                            styleMask:style_mask
                                              backing:NSBackingStoreBuffered
                                                defer:NO
                                               screen:screen];
      [mCocoaWindow setOneShot:YES];

      // Set the delegate for mCocoaWindow. In particular, we do this so that
      // we can be told if the window gets closed by some external entity.
      id delegate =
         [[[GlWindowDelegate alloc] initWithWindow:this] autorelease];
      [mCocoaWindow setDelegate:delegate];
{'fraction_non_alphanumeric': 0.10614897517080486, 'fraction_numerical': 0.005082486252291285, 'mean_word_length': 2.728797763280522, 'pattern_counts': {'":': 2, '<': 80, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '126819', 'n_tokens_mistral': 3752, 'n_tokens_neox': 3628, 'n_words': 1124}