<commit_before>/*=========================================================================

Program:   Visualization Toolkit
Module:    vtkCocoaRenderWindow.mm

Copyright (c) <NAME>, <NAME>, <NAME>
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtk_glew.h"

#include "vtkOpenGLRenderWindow.h"
#import <Cocoa/Cocoa.h>
#import "vtkCocoaMacOSXSDKCompatibility.h" // Needed to support old SDKs

#import "vtkOpenGL.h"
#import "vtkCocoaRenderWindow.h"
#import "vtkRenderWindowInteractor.h"
#import "vtkCommand.h"
#import "vtkIdList.h"
#import "vtkObjectFactory.h"
#import "vtkRendererCollection.h"
#import "vtkCocoaGLView.h"


#import <sstream>

vtkStandardNewMacro(vtkCocoaRenderWindow);

//----------------------------------------------------------------------------
// This is a private class and an implementation detail, do not use it.
// For fullscreen, an NSWindow that captures key events even when borderless
@interface vtkCocoaFullScreenWindow : NSWindow
{
}
@end

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

//----------------------------------------------------------------------------
// This is a private class and an implementation detail, do not use it.
// It manages the NSWindow of a "pure VTK application",
// as opposed to a regular Mac app that happens to use VTK.
//----------------------------------------------------------------------------
@interface vtkCocoaServer : NSObject
{
  @private
  vtkCocoaRenderWindow *_renWin;
}

// Designated initializer
- (id)initWithRenderWindow:(vtkCocoaRenderWindow *)inRenderWindow;

- (void)startObservations;
- (void)stopObservations;

@end

//----------------------------------------------------------------------------
@implementation vtkCocoaServer

//----------------------------------------------------------------------------
- (id)initWithRenderWindow:(vtkCocoaRenderWindow *)inRenderWindow
{
  self = [super init];
  if (self)
    {
    _renWin = inRenderWindow;
    }
  return self;
}

//----------------------------------------------------------------------------
- (void)startObservations
{
  assert(_renWin);

  int windowCreated = _renWin->GetWindowCreated();
  NSWindow *win = reinterpret_cast<NSWindow *>(_renWin->GetRootWindow());
  if (windowCreated && win)
    {
    // Receive notifications of this, and only this, window's closing.
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(windowWillClose:)
               name:NSWindowWillCloseNotification
             object:win];
    }

  NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
  int viewCreated = _renWin->GetViewCreated();
  if (viewCreated && view)
    {
    // Receive notifications of this, and only this, view's frame changing.
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(viewFrameDidChange:)
               name:NSViewFrameDidChangeNotification
             object:view];
    }
}

//----------------------------------------------------------------------------
- (void)stopObservations
{
  assert(_renWin);

  int windowCreated = _renWin->GetWindowCreated();
  NSWindow *win = reinterpret_cast<NSWindow *>(_renWin->GetRootWindow());
  if (windowCreated && win)
    {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self
                  name:NSWindowWillCloseNotification
                object:win];
    }

  NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
  int viewCreated = _renWin->GetViewCreated();
  if (viewCreated && view)
    {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self
                  name:NSViewFrameDidChangeNotification
                object:view];
    }
}

//----------------------------------------------------------------------------
- (void)windowWillClose:(NSNotification *)aNotification
{
  // We should only get here if it was us that created the NSWindow.
  assert(_renWin);
  assert(_renWin->GetWindowCreated());

  // We should only have observed our own NSWindow.
  assert([aNotification object] == _renWin->GetRootWindow());
  (void)aNotification;

  // Stop observing because the window is closing.
  [self stopObservations];

  // The NSWindow is closing, so prevent anyone from accidentally using it.
  _renWin->SetRootWindow(NULL);

  // Tell interactor to stop the NSApplication's run loop
  vtkRenderWindowInteractor *interactor = _renWin->GetInteractor();
  if (interactor)
    {
    interactor->TerminateApp();
    }
}

//----------------------------------------------------------------------------
- (void)viewFrameDidChange:(NSNotification *)aNotification
{
  // We should only get here if it was us that created the NSView.
  assert(_renWin);
  assert(_renWin->GetViewCreated());

  // We should only have observed our own NSView.
  assert([aNotification object] == _renWin->GetWindowId());
  (void)aNotification;

  // Retrieve the Interactor.
  vtkRenderWindowInteractor *interactor = _renWin->GetInteractor();
  if (!interactor || !interactor->GetEnabled())
    {
    return;
    }

  // Get the NSView's new frame size.
  NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
  assert(view);
  NSRect frameRect = [view frame];
  int width = (int)round(NSWidth(frameRect));
  int height = (int)round(NSHeight(frameRect));

  // Get the interactor's current cache of the size.
  int size[2];
  interactor->GetSize(size);

  if (width != size[0] || height != size[1])
    {
    // Send ConfigureEvent from the Interactor.
    interactor->UpdateSize(width, height);
    interactor->InvokeEvent(vtkCommand::ConfigureEvent, NULL);
    }
}

@end

//----------------------------------------------------------------------------
vtkCocoaRenderWindow::vtkCocoaRenderWindow()
{
  // First, create the cocoa objects manager. The dictionary is empty so
  // essentially all objects are initialized to NULL.
  NSMutableDictionary *cocoaManager = [NSMutableDictionary dictionary];

  // SetCocoaManager works like an Obj-C setter, so do like Obj-C and
  // init the ivar to null first.
  this->CocoaManager = NULL;
  this->SetCocoaManager(reinterpret_cast<void *>(cocoaManager));
  [cocoaManager self]; // prevent premature collection under GC.

  this->WindowCreated = 0;
  this->ViewCreated = 0;
  this->SetWindowName("Visualization Toolkit - Cocoa");
  this->CursorHidden = 0;
  this->ForceMakeCurrent = 0;
  this->Capabilities = 0;
  this->OnScreenInitialized = 0;
  this->OffScreenInitialized = 0;
}

//----------------------------------------------------------------------------
vtkCocoaRenderWindow::~vtkCocoaRenderWindow()
{
  if (this->CursorHidden)
    {
    this->ShowCursor();
    }
  this->Finalize();

  vtkRenderer *ren;
  vtkCollectionSimpleIterator rit;
  this->Renderers->InitTraversal(rit);
  while ( (ren = this->Renderers->GetNextRenderer(rit)) )
    {
    ren->SetRenderWindow(NULL);
    }

  delete[] this->Capabilities;
  this->Capabilities = 0;

  this->SetContextId(NULL);
  this->SetPixelFormat(NULL);
  this->SetCocoaServer(NULL);
  this->SetRootWindow(NULL);
  this->SetWindowId(NULL);
  this->SetParentId(NULL);

  // Release the cocoa object manager.
  this->SetCocoaManager(NULL);
}

//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::Finalize()
{
  if(this->OffScreenInitialized)
    {
    this->OffScreenInitialized = 0;
    this->DestroyOffScreenWindow();
    }
  if(this->OnScreenInitialized)
    {
    this->OnScreenInitialized = 0;
    this->DestroyWindow();
    }
}

//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::DestroyWindow()
{
  // finish OpenGL rendering
  if (this->OwnContext && this->GetContextId())
    {
    this->MakeCurrent();

    // tell each of the renderers that this render window/graphics context
    // is being removed (the RendererCollection is removed by vtkRenderWindow's
    // destructor)
    vtkCollectionSimpleIterator rsit;
    vtkRenderer *ren;
    for ( this->Renderers->InitTraversal(rsit);
          (ren = this->Renderers->GetNextRenderer(rsit));)
      {
      ren->SetRenderWindow(NULL);
      ren->SetRenderWindow(this);
      }
  }
  this->SetContextId(NULL);
  this->SetPixelFormat(NULL);

  vtkCocoaServer *server = (vtkCocoaServer *)this->GetCocoaServer();
  [server stopObservations];
  this->SetCocoaServer(NULL);

  // If we created it, close the NSWindow.
  if (this->WindowCreated)
    {
    NSWindow *window = (NSWindow*)this->GetRootWindow();
    [window close];
    }

  this->SetWindowId(NULL);
  this->SetParentId(NULL);
  this->SetRootWindow(NULL);
  this->WindowCreated = 0;
  this->ViewCreated = 0;
}

//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetWindowName( const char * _arg )
{
  vtkWindow::SetWindowName(_arg);<commit_msg>Add missing call to ReleaseGraphicsResources<commit_after>/*=========================================================================

Program:   Visualization Toolkit
Module:    vtkCocoaRenderWindow.mm

Copyright (c) <NAME>, <NAME>, <NAME>
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtk_glew.h"

#include "vtkOpenGLRenderWindow.h"
#import <Cocoa/Cocoa.h>
#import "vtkCocoaMacOSXSDKCompatibility.h" // Needed to support old SDKs

#import "vtkOpenGL.h"
#import "vtkCocoaRenderWindow.h"
#import "vtkRenderWindowInteractor.h"
#import "vtkCommand.h"
#import "vtkIdList.h"
#import "vtkObjectFactory.h"
#import "vtkRendererCollection.h"
#import "vtkCocoaGLView.h"


#import <sstream>

vtkStandardNewMacro(vtkCocoaRenderWindow);

//----------------------------------------------------------------------------
// This is a private class and an implementation detail, do not use it.
// For fullscreen, an NSWindow that captures key events even when borderless
@interface vtkCocoaFullScreenWindow : NSWindow
{
}
@end

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

//----------------------------------------------------------------------------
// This is a private class and an implementation detail, do not use it.
// It manages the NSWindow of a "pure VTK application",
// as opposed to a regular Mac app that happens to use VTK.
//----------------------------------------------------------------------------
@interface vtkCocoaServer : NSObject
{
  @private
  vtkCocoaRenderWindow *_renWin;
}

// Designated initializer
- (id)initWithRenderWindow:(vtkCocoaRenderWindow *)inRenderWindow;

- (void)startObservations;
- (void)stopObservations;

@end

//----------------------------------------------------------------------------
@implementation vtkCocoaServer

//----------------------------------------------------------------------------
- (id)initWithRenderWindow:(vtkCocoaRenderWindow *)inRenderWindow
{
  self = [super init];
  if (self)
    {
    _renWin = inRenderWindow;
    }
  return self;
}

//----------------------------------------------------------------------------
- (void)startObservations
{
  assert(_renWin);

  int windowCreated = _renWin->GetWindowCreated();
  NSWindow *win = reinterpret_cast<NSWindow *>(_renWin->GetRootWindow());
  if (windowCreated && win)
    {
    // Receive notifications of this, and only this, window's closing.
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(windowWillClose:)
               name:NSWindowWillCloseNotification
             object:win];
    }

  NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
  int viewCreated = _renWin->GetViewCreated();
  if (viewCreated && view)
    {
    // Receive notifications of this, and only this, view's frame changing.
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(viewFrameDidChange:)
               name:NSViewFrameDidChangeNotification
             object:view];
    }
}

//----------------------------------------------------------------------------
- (void)stopObservations
{
  assert(_renWin);

  int windowCreated = _renWin->GetWindowCreated();
  NSWindow *win = reinterpret_cast<NSWindow *>(_renWin->GetRootWindow());
  if (windowCreated && win)
    {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self
                  name:NSWindowWillCloseNotification
                object:win];
    }

  NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
  int viewCreated = _renWin->GetViewCreated();
  if (viewCreated && view)
    {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self
                  name:NSViewFrameDidChangeNotification
                object:view];
    }
}

//----------------------------------------------------------------------------
- (void)windowWillClose:(NSNotification *)aNotification
{
  // We should only get here if it was us that created the NSWindow.
  assert(_renWin);
  assert(_renWin->GetWindowCreated());

  // We should only have observed our own NSWindow.
  assert([aNotification object] == _renWin->GetRootWindow());
  (void)aNotification;

  // Stop observing because the window is closing.
  [self stopObservations];

  // The NSWindow is closing, so prevent anyone from accidentally using it.
  _renWin->SetRootWindow(NULL);

  // Tell interactor to stop the NSApplication's run loop
  vtkRenderWindowInteractor *interactor = _renWin->GetInteractor();
  if (interactor)
    {
    interactor->TerminateApp();
    }
}

//----------------------------------------------------------------------------
- (void)viewFrameDidChange:(NSNotification *)aNotification
{
  // We should only get here if it was us that created the NSView.
  assert(_renWin);
  assert(_renWin->GetViewCreated());

  // We should only have observed our own NSView.
  assert([aNotification object] == _renWin->GetWindowId());
  (void)aNotification;

  // Retrieve the Interactor.
  vtkRenderWindowInteractor *interactor = _renWin->GetInteractor();
  if (!interactor || !interactor->GetEnabled())
    {
    return;
    }

  // Get the NSView's new frame size.
  NSView *view = reinterpret_cast<NSView *>(_renWin->GetWindowId());
  assert(view);
  NSRect frameRect = [view frame];
  int width = (int)round(NSWidth(frameRect));
  int height = (int)round(NSHeight(frameRect));

  // Get the interactor's current cache of the size.
  int size[2];
  interactor->GetSize(size);

  if (width != size[0] || height != size[1])
    {
    // Send ConfigureEvent from the Interactor.
    interactor->UpdateSize(width, height);
    interactor->InvokeEvent(vtkCommand::ConfigureEvent, NULL);
    }
}

@end

//----------------------------------------------------------------------------
vtkCocoaRenderWindow::vtkCocoaRenderWindow()
{
  // First, create the cocoa objects manager. The dictionary is empty so
  // essentially all objects are initialized to NULL.
  NSMutableDictionary *cocoaManager = [NSMutableDictionary dictionary];

  // SetCocoaManager works like an Obj-C setter, so do like Obj-C and
  // init the ivar to null first.
  this->CocoaManager = NULL;
  this->SetCocoaManager(reinterpret_cast<void *>(cocoaManager));
  [cocoaManager self]; // prevent premature collection under GC.

  this->WindowCreated = 0;
  this->ViewCreated = 0;
  this->SetWindowName("Visualization Toolkit - Cocoa");
  this->CursorHidden = 0;
  this->ForceMakeCurrent = 0;
  this->Capabilities = 0;
  this->OnScreenInitialized = 0;
  this->OffScreenInitialized = 0;
}

//----------------------------------------------------------------------------
vtkCocoaRenderWindow::~vtkCocoaRenderWindow()
{
  if (this->CursorHidden)
    {
    this->ShowCursor();
    }
  this->Finalize();

  vtkRenderer *ren;
  vtkCollectionSimpleIterator rit;
  this->Renderers->InitTraversal(rit);
  while ( (ren = this->Renderers->GetNextRenderer(rit)) )
    {
    ren->SetRenderWindow(NULL);
    }

  delete[] this->Capabilities;
  this->Capabilities = 0;

  this->SetContextId(NULL);
  this->SetPixelFormat(NULL);
  this->SetCocoaServer(NULL);
  this->SetRootWindow(NULL);
  this->SetWindowId(NULL);
  this->SetParentId(NULL);

  // Release the cocoa object manager.
  this->SetCocoaManager(NULL);
}

//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::Finalize()
{
  if(this->OffScreenInitialized)
    {
    this->OffScreenInitialized = 0;
    this->DestroyOffScreenWindow();
    }
  if(this->OnScreenInitialized)
    {
    this->OnScreenInitialized = 0;
    this->DestroyWindow();
    }
}

//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::DestroyWindow()
{
  // finish OpenGL rendering
  if (this->OwnContext && this->GetContextId())
    {
    this->MakeCurrent();

    // tell each of the renderers that this render window/graphics context
    // is being removed (the RendererCollection is removed by vtkRenderWindow's
    // destructor)
    vtkCollectionSimpleIterator rsit;
    vtkRenderer *ren;
    for ( this->Renderers->InitTraversal(rsit);
          (ren = this->Renderers->GetNextRenderer(rsit));)
      {
      ren->SetRenderWindow(NULL);
      ren->SetRenderWindow(this);
      }
    this->ReleaseGraphicsResources();
  }
  this->SetContextId(NULL);
  this->SetPixelFormat(NULL);

  vtkCocoaServer *server = (vtkCocoaServer *)this->GetCocoaServer();
  [server stopObservations];
  this->SetCocoaServer(NULL);

  // If we created it, close the NSWindow.
  if (this->WindowCreated)
    {
    NSWindow *window = (NSWindow*)this->GetRootWindow();
    [window close];
    }

  this->SetWindowId(NULL);
  this->SetParentId(NULL);
  this->SetRootWindow(NULL);
  this->WindowCreated = 0;
  this->ViewCreated = 0;
}

//----------------------------------------------------------------------------
void vtkCocoaRenderWindow::SetWindowName( const char * _arg )
{
  vtkWindow::SetWindowName(_arg);
{'fraction_non_alphanumeric': 0.2319420011727704, 'fraction_numerical': 0.0015992323684631377, 'mean_word_length': 2.42648401826484, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 174, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1070932', 'n_tokens_mistral': 5275, 'n_tokens_neox': 5126, 'n_words': 1212}