Files
geant4/source/externals/g4tools/include/tools/Cocoa/sg_viewer.hm
T
2021-06-25 16:12:29 +02:00

164 lines
4.4 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_Cocoa_sg_viewer_hm
#define tools_Cocoa_sg_viewer_hm
#include "../sg/GL_viewer"
#include "session.hm"
@interface tools_Cocoa_sg_viewer_GLView : NSOpenGLView {
tools::sg::GL_viewer* m_viewer;
}
- (id)init:(NSRect)rect viewer:(tools::sg::GL_viewer*)a_viewer;
- (void)dealloc;
- (void)drawRect:(NSRect)a_rect;
@end
@implementation tools_Cocoa_sg_viewer_GLView
- (id)init:(NSRect)a_rect viewer:(tools::sg::GL_viewer*)a_viewer {
NSOpenGLPixelFormatAttribute att[32];
int i = 0;
att[i++] = NSOpenGLPFADoubleBuffer;
att[i++] = NSOpenGLPFAAccelerated;
att[i++] = NSOpenGLPFAAccumSize;
att[i++] = (NSOpenGLPixelFormatAttribute)32;
att[i++] = NSOpenGLPFAColorSize;
att[i++] = (NSOpenGLPixelFormatAttribute)32;
att[i++] = NSOpenGLPFADepthSize;
att[i++] = (NSOpenGLPixelFormatAttribute)32;
// Antialiasing :
att[i++] = NSOpenGLPFASampleBuffers;
att[i++] = 1;
att[i++] = NSOpenGLPFASamples;
att[i++] = 2;
att[i++] = NSOpenGLPFANoRecovery;
att[i] = (NSOpenGLPixelFormatAttribute)0;
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:att];
if(self = [super initWithFrame:a_rect pixelFormat:pixelFormat]) {
// flush buffer only during the vertical retrace of the monitor
const GLint vals[1] = {1};
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1014
[[self openGLContext] setValues:vals forParameter:NSOpenGLCPSwapInterval];
#else
[[self openGLContext] setValues:vals forParameter:NSOpenGLContextParameterSwapInterval];
#endif
}
[pixelFormat release];
m_viewer = a_viewer;
return self;
}
- (void)dealloc {[super dealloc];}
- (void)drawRect:(NSRect)a_rect {
if(!m_viewer) return;
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101500
int w = a_rect.size.width;
int h = a_rect.size.height;
#else
NSRect backing_rect = [self convertRectToBacking:a_rect];
int w = (int)(backing_rect.size.width);
int h = (int)(backing_rect.size.height);
#endif
m_viewer->set_size(w,h);
[[self openGLContext] makeCurrentContext];
m_viewer->render();
[[self openGLContext] flushBuffer];
}
@end
#if MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
@interface tools_Cocoa_sg_viewer_win_delegate : NSObject {
#else
@interface tools_Cocoa_sg_viewer_win_delegate : NSObject<NSWindowDelegate> {
#endif
tools::Cocoa::session* m_session;
}
- (id)init:(tools::Cocoa::session*)a_session;
- (void)dealloc;
- (void)windowWillClose:(NSNotification*)a_not;
@end
@implementation tools_Cocoa_sg_viewer_win_delegate
- (id)init:(tools::Cocoa::session*)a_session {
if(self = [super init]) {}
m_session = a_session;
return self;
}
- (void)dealloc {[super dealloc];}
- (void)windowWillClose:(NSNotification*)a_not {
m_session->set_to_exit();
}
@end
namespace tools {
namespace Cocoa {
class sg_viewer : public sg::GL_viewer {
TOOLS_HEADER(sg_viewer,tools::Cocoa::sg_viewer,sg::GL_viewer)
public:
sg_viewer(session& a_session,
int a_x = 0,int a_y = 0,
unsigned int a_width = 500,unsigned int a_height = 500,
const std::string& a_win_title = "")
:parent(a_session.out(),a_width,a_height)
,m_session(a_session)
,m_win(0),m_GLView(0)
{
m_win = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height);
if(!m_win) return; //throw
tools_Cocoa_sg_viewer_win_delegate* _win_delegate = [[tools_Cocoa_sg_viewer_win_delegate alloc] init:&a_session];
[m_win setDelegate:_win_delegate];
NSRect rect;
rect = [m_win frame];
rect = [m_win contentRectForFrameRect:rect]; //window content true size.
m_GLView = [[tools_Cocoa_sg_viewer_GLView alloc] init:rect viewer:this];
[m_win setContentView:m_GLView];
[m_GLView release];
}
virtual ~sg_viewer() {
if(m_win) {
m_session.delete_window(m_win);
//m_session.sync();
}
}
protected:
sg_viewer(const sg_viewer& a_from)
:parent(a_from)
,m_session(a_from.m_session)
,m_win(0),m_GLView(0)
{}
sg_viewer& operator=(const sg_viewer& a_from) {
parent::operator=(a_from);
return *this;
}
public:
bool has_window() const {return m_win?true:false;}
bool show() {
if(!m_win) return false;
m_session.show_window(m_win);
return true;
}
void win_render() {
[[m_GLView openGLContext] makeCurrentContext];
render();
[[m_GLView openGLContext] flushBuffer];
}
protected:
session& m_session;
NSWindow* m_win;
tools_Cocoa_sg_viewer_GLView* m_GLView;
};
}}
#endif