From Eric Wing, "Here are some updates for osgsimpleviewerCocoa.
New features and enhancements: - Shared OpenGL contexts - Multithreaded OpenGL Engine support - Localizable string support - Offscreen rendering (to framebuffer object) for: - Copy/Paste (to image) support - Drag-and-drop (as source image) support - Print to Printer/PDF - Dock minimization picture - More robust initializer handling - Fix to better handle hardware surfaces in NSScrollViews/NSSplitViews - Use of respondsToSelector and instancesRespondToSelector to demonstrate runtime feature checking to provide access to newer features while still compiling on and supporting legacy versions The new file "Localizable.strings" needs to be placed inside the English.lproj directory."
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"DragAndDropHere" = "Drag-and-Drop\nyour .osg model here!";
|
||||
@@ -3,7 +3,7 @@
|
||||
// osgsimpleviewerCocoa
|
||||
//
|
||||
// Created by Eric Wing on 11/12/06.
|
||||
// Copyright 2006. All rights reserved.
|
||||
// Copyright 2006. Released under the OSGPL.
|
||||
//
|
||||
/* This is the class interface for a custom NSView that interfaces with an osgViewer.
|
||||
* Because Cocoa is written in Objective-C, but OSG is written in C++, we rely on
|
||||
@@ -48,23 +48,40 @@ namespace osgViewer
|
||||
// If so, remember ref_ptr is an object on the stack and the cdtors option must be activated.
|
||||
// We could also make simpleViewer an object instead of a pointer, but again, turn on the option.
|
||||
osgViewer::SimpleViewer* simpleViewer;
|
||||
|
||||
|
||||
// This timer is used to trigger animation callbacks since everything is event driven.
|
||||
NSTimer* animationTimer;
|
||||
|
||||
// Flags to help track whether ctrl-clicking or option-clicking is being used
|
||||
BOOL isUsingCtrlClick;
|
||||
BOOL isUsingOptionClick;
|
||||
|
||||
// Flag to track whether the OpenGL multithreading engine is enabled or not
|
||||
BOOL isUsingMultithreadedOpenGLEngine;
|
||||
|
||||
}
|
||||
|
||||
// My custom static method to create a basic pixel format
|
||||
+ (NSOpenGLPixelFormat*) basicPixelFormat;
|
||||
|
||||
|
||||
// Official init methods
|
||||
- (id) initWithFrame:(NSRect)frame_rect pixelFormat:(NSOpenGLPixelFormat*)pixel_format;
|
||||
- (id) initWithCoder:(NSCoder*)the_coder;
|
||||
- (id) initWithFrame:(NSRect)frame_rect;
|
||||
|
||||
// Official function, overridden by this class to prevent flashing/tearing when in splitviews, scrollviews, etc.
|
||||
- (void) renewGState;
|
||||
|
||||
// My custom function for minimization.
|
||||
- (void) prepareForMiniaturization:(NSNotification*)notification;
|
||||
|
||||
|
||||
// Custom function to allow users to know if the Multithreaded OpenGL Engine is enabled
|
||||
- (BOOL) isUsingMultithreadedOpenGLEngine;
|
||||
|
||||
// Private init helper methods
|
||||
- (void) initSharedOpenGLContext;
|
||||
- (void) commonInit;
|
||||
- (void) initOSGViewer;
|
||||
- (void) initAnimationTimer;
|
||||
@@ -73,6 +90,7 @@ namespace osgViewer
|
||||
- (void) prepareOpenGL;
|
||||
// Class dealloc method
|
||||
- (void) dealloc;
|
||||
- (void) finalize;
|
||||
|
||||
// Official mouse event methods
|
||||
- (void) mouseDown:(NSEvent*)the_event;
|
||||
@@ -117,6 +135,13 @@ namespace osgViewer
|
||||
- (void) reshape;
|
||||
- (void) drawRect:(NSRect)the_rect;
|
||||
|
||||
// Private helper methods for drawing
|
||||
- (NSBitmapImageRep*) renderOpenGLSceneToFramebuffer;
|
||||
- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height;
|
||||
- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height clearColorRed:(float)clear_red clearColorGreen:(float)clear_green clearColorBlue:(float)clear_blue clearColorAlpha:(float)clear_alpha;
|
||||
- (NSImage*)imageFromBitmapImageRep:(NSBitmapImageRep*)bitmap_image_rep;
|
||||
|
||||
|
||||
// Official methods for drag and drop (view as target)
|
||||
- (unsigned int) draggingEntered:(id <NSDraggingInfo>)the_sender;
|
||||
- (void) draggingExited:(id <NSDraggingInfo>)the_sender;
|
||||
@@ -124,10 +149,19 @@ namespace osgViewer
|
||||
- (BOOL) performDragOperation:(id <NSDraggingInfo>)the_sender;
|
||||
- (void) concludeDragOperation:(id <NSDraggingInfo>)the_sender;
|
||||
|
||||
// Official method for copy (i.e. copy & paste)
|
||||
- (IBAction) copy:(id)sender;
|
||||
|
||||
// Private helper methods for drag and drop and copy/paste (view as source)
|
||||
- (NSData*) dataWithTIFFOfContentView;
|
||||
- (NSData*) contentsAsDataOfType:(NSString *)pboardType;
|
||||
- (void) startDragAndDropAsSource:(NSEvent*)the_event;
|
||||
|
||||
|
||||
// Examples of providing an action to connect to.
|
||||
- (IBAction) resetPosition:(id)the_sender;
|
||||
- (IBAction) takeBackgroundColorFrom:(id)the_sender;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// osgsimpleviewerCocoa
|
||||
//
|
||||
// Created by Eric Wing on 11/12/06.
|
||||
// Copyright 2006. All rights reserved.
|
||||
// Copyright 2006. Released under the OSGPL.
|
||||
//
|
||||
/* This class demonstrates how to subclass NSOpenGLView to integrate with the
|
||||
* osgViewer::SimpleViewer.
|
||||
@@ -17,28 +17,46 @@
|
||||
* Scroll events (Mighty Mouse, Two-finger trackpads)
|
||||
* Keyboard events
|
||||
* Drag and drop (as target)
|
||||
* Resolution Independent UI (maybe, not Leopard tested, only Tiger Quartz Debug)
|
||||
* Drag and drop (as source)
|
||||
* Copy (for copy/paste)
|
||||
* Resolution Independent UI (maybe, not Leopard tested/only Tiger Quartz Debug)
|
||||
* Target-Action (for other widgets to invoke actions on this view)
|
||||
*
|
||||
* Shared OpenGLContexts
|
||||
* Multithreaded OpenGL Engine (with CPU count check)
|
||||
* Getting an image for minimization in the Dock
|
||||
* Using osg::Camera's Framebuffer Objects to generate screen captures
|
||||
* Code to detect drawing to offscreen (e.g. printer)
|
||||
* Localization/Localizable strings (really need some different languages)
|
||||
* Use of respondsToSelector and instancesRespondToSelector to demonstrate
|
||||
* runtime feature checking to provide access to newer features while still
|
||||
* supporting legacy versions.
|
||||
*
|
||||
* Things not demonstrated by this view or application example (but would be interesting):
|
||||
* Cocoa Bindings (highly recommended)
|
||||
* Core Data (works great with Cocoa Bindings)
|
||||
* Custom Interface Builder palette with Inspector configurable options
|
||||
* Shared OpenGLContexts
|
||||
* More PixelFormat options
|
||||
* Fullscreen mode
|
||||
* Low-level CGL access.
|
||||
* Low-level CGL access (kind of have with multithreaded OpenGL engine).
|
||||
* Delegates for your view
|
||||
* Multithreading
|
||||
* Drag-and-drop as a source (similar to the target code)
|
||||
* Copy, Cut, Paste (very similar to drag-and-drop)
|
||||
* Creating (updating) images of the window for the minimized view in the Dock
|
||||
* Printing support
|
||||
* Updating images of the window for the minimized view in the Dock
|
||||
* Full Printing support
|
||||
* Non-view stuff (Application Delegates, models, controllers)
|
||||
* Launching by double-clicking a model or drag-and-drop onto Application Icon (non-view)
|
||||
* Launching via commandline with parameters (non-view)
|
||||
*/
|
||||
|
||||
/* Commentary:
|
||||
* This class packs a lot of functionality that you might not expect from a "SimpleViewer".
|
||||
* The reason is that Mac users have high expectations of their applications, so things
|
||||
* that might not even be considered on other platforms (like drag-and-drop), are almost
|
||||
* a requirment on Mac OS X.
|
||||
* The good news is that this class can almost be used as a template for other purposes.
|
||||
* If you are looking for the bare minimum code needed, focus your attention on
|
||||
* the init* routines and drawRect.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Coding conventions:
|
||||
* My coding style is slightly different than what you normally see in Cocoa.
|
||||
@@ -52,12 +70,15 @@
|
||||
* (I tend to think of the pointer as part of the type.)
|
||||
* For Obj-C named parameters, I tend to keep the namedParameter and the value
|
||||
* together instead of separated by spaces
|
||||
* (e.g. [self initWithX:x_val yVal:y_val zVal:z_val].
|
||||
* e.g. [self initWithX:x_val yVal:y_val zVal:z_val].
|
||||
* (When I was first learning Objective-C, this made it easier for me to
|
||||
* figure out which things * were paired.)
|
||||
* figure out which things were paired.)
|
||||
*/
|
||||
#import "SimpleViewerCocoa.h"
|
||||
|
||||
#include <OpenGL/gl.h>
|
||||
#include <OpenGL/glu.h> // handy for gluErrorString
|
||||
|
||||
#include <osgViewer/SimpleViewer>
|
||||
#include <osgGA/TrackballManipulator>
|
||||
// Needed to explicitly typecast keys to the OSG type
|
||||
@@ -76,8 +97,50 @@
|
||||
// osgViewer itself does not have a dependency on osgText.
|
||||
#include <osg/ref_ptr>
|
||||
#include <osgText/Text>
|
||||
|
||||
#include <osg/Geode>
|
||||
|
||||
// Needed for Multithreaded OpenGL Engine (sysctlbyname for num CPUs)
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <OpenGL/OpenGL.h> // for CoreOpenGL (CGL) for Multithreaded OpenGL Engine
|
||||
|
||||
|
||||
|
||||
// This is optional. This allows memory for things like textures and displaylists to be shared among different contexts.
|
||||
#define SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
#ifdef SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
static NSOpenGLContext* s_sharedOpenGLContext = NULL;
|
||||
#endif // SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
|
||||
// Taken/Adapted from one of the Apple OpenGL developer examples
|
||||
static void Internal_SetAlpha(NSBitmapImageRep *imageRep, unsigned char alpha_value)
|
||||
{
|
||||
register unsigned char * sp = [imageRep bitmapData];
|
||||
register int bytesPerRow = [imageRep bytesPerRow];
|
||||
register int height = [imageRep pixelsHigh];
|
||||
register int width = [imageRep pixelsWide];
|
||||
|
||||
for(int i=0; i<height; i++)
|
||||
{
|
||||
register unsigned int * the_pixel = (unsigned int *) sp;
|
||||
register int w = width;
|
||||
while (w-- > 0)
|
||||
{
|
||||
unsigned char* sp_char = (unsigned char *) the_pixel;
|
||||
// register unsigned char * the_red = sp_char;
|
||||
// register unsigned char * the_green = (sp_char+1);
|
||||
// register unsigned char * the_blue = (sp_char+2);
|
||||
register unsigned char * the_alpha = (sp_char+3);
|
||||
|
||||
*the_alpha = alpha_value;
|
||||
*the_pixel++;
|
||||
}
|
||||
sp += bytesPerRow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@implementation SimpleViewerCocoa
|
||||
|
||||
// My simple pixel format definition
|
||||
@@ -88,11 +151,15 @@
|
||||
NSOpenGLPFAWindow,
|
||||
NSOpenGLPFADoubleBuffer, // double buffered
|
||||
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)32, // depth buffer size in bits
|
||||
// NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24, // Not sure if this helps
|
||||
// NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8, // Not sure if this helps
|
||||
(NSOpenGLPixelFormatAttribute)nil
|
||||
};
|
||||
return [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixel_attributes] autorelease];
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// Init Stuff /////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
@@ -131,18 +198,64 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
/* Some generic code expecting regular NSView's may call this initializer instead of the specialized NSOpenGLView designated initializer.
|
||||
* I override this method here to make sure it does the right thing.
|
||||
*/
|
||||
- (id) initWithFrame:(NSRect)frame_rect
|
||||
{
|
||||
self = [super initWithFrame:frame_rect pixelFormat:[SimpleViewerCocoa basicPixelFormat]];
|
||||
if(self)
|
||||
{
|
||||
[self commonInit];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
// My custom methods to centralize common init stuff
|
||||
- (void) commonInit
|
||||
{
|
||||
isUsingCtrlClick = NO;
|
||||
isUsingOptionClick = NO;
|
||||
|
||||
isUsingMultithreadedOpenGLEngine = NO;
|
||||
|
||||
[self initSharedOpenGLContext];
|
||||
|
||||
[self initOSGViewer];
|
||||
[self initAnimationTimer];
|
||||
|
||||
// Register for Drag and Drop
|
||||
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
|
||||
|
||||
// Add minification observer so we can set the Dock picture since OpenGL views don't do this automatically for us.
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(prepareForMiniaturization:) name:NSWindowWillMiniaturizeNotification object:nil];
|
||||
|
||||
}
|
||||
|
||||
/* Optional: This will setup shared OpenGL contexts so resources like textures, etc, can be shared/reused
|
||||
* by multiple instances of SimpleViwerCocoa views.
|
||||
*/
|
||||
- (void) initSharedOpenGLContext
|
||||
{
|
||||
#ifdef SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
|
||||
NSOpenGLContext* this_views_opengl_context = nil;
|
||||
|
||||
// create a context the first time through
|
||||
if(s_sharedOpenGLContext == NULL)
|
||||
{
|
||||
s_sharedOpenGLContext = [[NSOpenGLContext alloc]
|
||||
initWithFormat:[SimpleViewerCocoa basicPixelFormat]
|
||||
shareContext:nil];
|
||||
|
||||
}
|
||||
|
||||
this_views_opengl_context = [[NSOpenGLContext alloc]
|
||||
initWithFormat:[SimpleViewerCocoa basicPixelFormat]
|
||||
shareContext:s_sharedOpenGLContext];
|
||||
[self setOpenGLContext:this_views_opengl_context];
|
||||
// [this_views_opengl_context makeCurrentContext];
|
||||
#endif // SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
}
|
||||
|
||||
// Allocate a SimpleViewer and do basic initialization. No assumption about having an
|
||||
@@ -151,6 +264,15 @@
|
||||
{
|
||||
// osg::setNotifyLevel( osg::DEBUG_FP );
|
||||
simpleViewer = new osgViewer::SimpleViewer;
|
||||
|
||||
#ifdef SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
// Workaround: osgViewer::SimpleViewer automatically increments its context ID values.
|
||||
// Since we're using a shared context, we want all SimpleViewer's to use the same context ID.
|
||||
// There is no API to avoid this behavior, so we need to undo what SimpleViewer's constructor did.
|
||||
simpleViewer->getSceneView()->getState()->setContextID(0);
|
||||
osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(1);
|
||||
#endif // SIMPLEVIEWER_USE_SHARED_CONTEXTS
|
||||
|
||||
// Cocoa follows the same coordinate convention as OpenGL. osgViewer's default is inverted.
|
||||
simpleViewer->getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);
|
||||
// Use a trackball manipulator...matches nicely with the Mighty Mouse Scrollball.
|
||||
@@ -166,16 +288,16 @@
|
||||
NSInvocation* an_invocation;
|
||||
// animationCallback is my animation callback method
|
||||
the_selector = @selector( animationCallback );
|
||||
a_signature = [SimpleViewerCocoa instanceMethodSignatureForSelector:the_selector];
|
||||
an_invocation = [NSInvocation invocationWithMethodSignature:a_signature] ;
|
||||
[an_invocation setSelector:the_selector];
|
||||
[an_invocation setTarget:self];
|
||||
a_signature = [SimpleViewerCocoa instanceMethodSignatureForSelector:the_selector];
|
||||
an_invocation = [NSInvocation invocationWithMethodSignature:a_signature] ;
|
||||
[an_invocation setSelector:the_selector];
|
||||
[an_invocation setTarget:self];
|
||||
|
||||
animationTimer = [NSTimer
|
||||
animationTimer = [NSTimer
|
||||
scheduledTimerWithTimeInterval:1.0/60.0 // fps
|
||||
invocation:an_invocation
|
||||
repeats:YES];
|
||||
[animationTimer retain];
|
||||
[animationTimer retain];
|
||||
|
||||
// For single threaded apps like this one,
|
||||
// Cocoa seems to block timers or events sometimes. This can be seen
|
||||
@@ -197,9 +319,17 @@
|
||||
[animationTimer invalidate];
|
||||
[animationTimer release];
|
||||
delete simpleViewer;
|
||||
simpleViewer = NULL;
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (void) finalize
|
||||
{
|
||||
delete simpleViewer;
|
||||
simpleViewer = NULL;
|
||||
[super finalize];
|
||||
}
|
||||
|
||||
/* NSOpenGLView defines this method to be called (only once) after the OpenGL
|
||||
* context is created and made the current context. It is intended to be used to setup
|
||||
* your initial OpenGL state. This seems like a good place to initialize the
|
||||
@@ -215,13 +345,51 @@
|
||||
// If 0, flushBuffer will execute as soon as possible.
|
||||
long swap_interval = 1 ;
|
||||
[[self openGLContext] setValues:&swap_interval forParameter:NSOpenGLCPSwapInterval];
|
||||
|
||||
|
||||
|
||||
// Try new multithreaded OpenGL engine?
|
||||
// See Technical Note TN2085 Enabling multi-threaded execution of the OpenGL framework
|
||||
// http://developer.apple.com/technotes/tn2006/tn2085.html
|
||||
uint64_t num_cpus = 0;
|
||||
size_t num_cpus_length = sizeof(num_cpus);
|
||||
// Multithreaded engine only benefits with muliple CPUs, so do CPU count check
|
||||
if(sysctlbyname("hw.activecpu", &num_cpus, &num_cpus_length, NULL, 0) == 0)
|
||||
{
|
||||
// NSLog(@"Num CPUs=%d", num_cpus);
|
||||
if(num_cpus >= 2)
|
||||
{
|
||||
// Cleared to enable multi-threaded engine
|
||||
CGLError error_val = CGLEnable((CGLContextObj)[[self openGLContext] CGLContextObj], kCGLCEMPEngine);
|
||||
if(error_val != 0)
|
||||
{
|
||||
// The likelihood of failure seems quite high on older hardware, at least for now.
|
||||
// NSLog(@"Failed to enable Multithreaded OpenGL Engine: %s", CGLErrorString(error_val));
|
||||
isUsingMultithreadedOpenGLEngine = NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
// NSLog(@"Success! Multithreaded OpenGL Engine activated!");
|
||||
isUsingMultithreadedOpenGLEngine = YES;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
isUsingMultithreadedOpenGLEngine = NO;
|
||||
}
|
||||
}
|
||||
|
||||
// This is also might be a good place to setup OpenGL state that OSG doesn't control.
|
||||
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
|
||||
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
||||
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
||||
|
||||
/*
|
||||
GLint maxbuffers[1];
|
||||
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, maxbuffers);
|
||||
NSLog(@"GL_MAX_COLOR_ATTACHMENTS=%d", maxbuffers[0]);
|
||||
*/
|
||||
|
||||
// We need to tell the osgViewer what the viewport size is
|
||||
[self resizeViewport];
|
||||
|
||||
@@ -230,21 +398,81 @@
|
||||
// This is to setup some default text in the OpenGL view so the
|
||||
// user knows that they should drag and drop a model into the view.
|
||||
osg::ref_ptr<osgText::Text> default_text = new osgText::Text;
|
||||
|
||||
default_text->setAlignment(osgText::Text::CENTER_CENTER);
|
||||
default_text->setBackdropType(osgText::Text::OUTLINE);
|
||||
// default_text->setBackdropImplementation(osgText::Text::POLYGON_OFFSET);
|
||||
default_text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));
|
||||
default_text->setBackdropColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));
|
||||
default_text->setAxisAlignment(osgText::Text::XZ_PLANE);
|
||||
default_text->setText("Drag-and-Drop\nyour .osg model here!");
|
||||
|
||||
// We should use a (Cocoa) localizable string instead of a hard coded string.
|
||||
// default_text->setText("Drag-and-Drop\nyour .osg model here!");
|
||||
// The first string is the key name (you need a Localizable.strings file for your Nib). The second string is just a comment.
|
||||
NSString* localized_string = NSLocalizedString(@"DragAndDropHere", @"Drag-and-Drop\nyour .osg model here!");
|
||||
default_text->setText([localized_string UTF8String]);
|
||||
|
||||
osg::ref_ptr<osg::Geode> the_geode = new osg::Geode;
|
||||
the_geode->addDrawable(default_text.get());
|
||||
simpleViewer->setSceneData(the_geode.get());
|
||||
|
||||
simpleViewer->setSceneData(the_geode.get());
|
||||
}
|
||||
|
||||
/* disableScreenUpdatesUntilFlush was introduced in Tiger. It will prevent
|
||||
* unnecessary screen flashing caused by NSSplitViews or NSScrollviews.
|
||||
* From Apple's release notes:
|
||||
|
||||
NSWindow -disableScreenUpdatesUntilFlush API (Section added since WWDC)
|
||||
|
||||
When a view that renders to a hardware surface (such as an OpenGL view) is placed in an NSScrollView or NSSplitView, there can be a noticeable flicker or lag when the scroll position or splitter position is moved. This happens because each move of the hardware surface takes effect immediately, before the remaining window content has had the chance to draw and flush.
|
||||
|
||||
To enable applications to eliminate this visual artifact, Tiger AppKit provides a new NSWindow message, -disableScreenUpdatesUntilFlush. This message asks a window to suspend screen updates until the window's updated content is subsequently flushed to the screen. This message can be sent from a view that is about to move its hardware surface, to insure that the hardware surface move and window redisplay will be visually synchronized. The window responds by immediately disabling screen updates via a call to NSDisableScreenUpdates(), and setting a flag that will cause it to call NSEnableScreenUpdates() later, when the window flushes. It is permissible to send this message to a given window more than once during a given window update cycle; the window will only suspend and re-enable updates once during that cycle.
|
||||
|
||||
A view class that renders to a hardware surface can send this message from an override of -renewGState (a method that is is invoked immediately before the view's surface is moved) to effectively defer compositing of the moved surface until the window has finished drawing and flushing its remaining content.
|
||||
A -respondsToSelector: check has been used to provide compatibility with previous system releases. On pre-Tiger systems, where NSWindow has no -disableScreenUpdatesUntilFlush method, the -renewGState override will have no effect.
|
||||
*/
|
||||
- (void) renewGState
|
||||
{
|
||||
NSWindow* the_window = [self window];
|
||||
if([the_window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
|
||||
{
|
||||
[the_window disableScreenUpdatesUntilFlush];
|
||||
}
|
||||
[super renewGState];
|
||||
}
|
||||
|
||||
|
||||
/* When you minimize an app, you usually can see its shrunken contents
|
||||
* in the dock. However, an OpenGL view by default only produces a blank
|
||||
* white window. So we use this method to do an image capture of our view
|
||||
* which will be used as its minimized picture.
|
||||
* (A possible enhancement to consider is to update the picture over time.)
|
||||
*/
|
||||
- (void) prepareForMiniaturization:(NSNotification*)notification
|
||||
{
|
||||
NSBitmapImageRep* ns_image_rep = [self renderOpenGLSceneToFramebuffer];
|
||||
if([self lockFocusIfCanDraw])
|
||||
{
|
||||
[ns_image_rep draw];
|
||||
[self unlockFocus];
|
||||
[[self window] flushWindow];
|
||||
}
|
||||
}
|
||||
|
||||
/* Allow people to easily query if the multithreaded OpenGL engine is activated.
|
||||
*/
|
||||
- (BOOL) isUsingMultithreadedOpenGLEngine
|
||||
{
|
||||
return isUsingMultithreadedOpenGLEngine;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// End Init Stuff /////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// Mouse Stuff ////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
@@ -265,6 +493,10 @@
|
||||
[self setIsUsingOptionClick:YES];
|
||||
[self doMiddleMouseButtonDown:the_event];
|
||||
}
|
||||
else if([the_event modifierFlags] & NSCommandKeyMask)
|
||||
{
|
||||
[self startDragAndDropAsSource:the_event];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self doLeftMouseButtonDown:the_event];
|
||||
@@ -293,7 +525,7 @@
|
||||
[self setIsUsingCtrlClick:NO];
|
||||
[self doRightMouseButtonUp:the_event];
|
||||
}
|
||||
if([self isUsingOptionClick] == YES)
|
||||
else if([self isUsingOptionClick] == YES)
|
||||
{
|
||||
[self setIsUsingOptionClick:NO];
|
||||
[self doMiddleMouseButtonUp:the_event];
|
||||
@@ -328,10 +560,6 @@
|
||||
// "otherMouse" seems to capture middle button and any other buttons beyond (4th, etc).
|
||||
- (void) otherMouseDown:(NSEvent*)the_event
|
||||
{
|
||||
// We must convert the mouse event locations from the window coordinate system to the
|
||||
// local view coordinate system.
|
||||
NSPoint the_point = [the_event locationInWindow];
|
||||
NSPoint converted_point = [self convertPoint:the_point fromView:nil];
|
||||
// Button 0 is left
|
||||
// Button 1 is right
|
||||
// Button 2 is middle
|
||||
@@ -400,6 +628,7 @@
|
||||
return isUsingOptionClick;
|
||||
}
|
||||
|
||||
|
||||
- (void) doLeftMouseButtonDown:(NSEvent*)the_event
|
||||
{
|
||||
// We must convert the mouse event locations from the window coordinate system to the
|
||||
@@ -622,10 +851,263 @@
|
||||
// (e.g. No sense drawing when the application is hidden.)
|
||||
- (void) drawRect:(NSRect)the_rect
|
||||
{
|
||||
[[self openGLContext] makeCurrentContext];
|
||||
simpleViewer->frame();
|
||||
[[self openGLContext] flushBuffer];
|
||||
if([[NSGraphicsContext currentContext] isDrawingToScreen])
|
||||
{
|
||||
// [[self openGLContext] makeCurrentContext];
|
||||
simpleViewer->frame();
|
||||
[[self openGLContext] flushBuffer];
|
||||
}
|
||||
else // This is usually the print case
|
||||
{
|
||||
// [[self openGLContext] makeCurrentContext];
|
||||
|
||||
// FIXME: We should be computing a size that fits best to the paper target
|
||||
NSSize size_in_points = [self bounds].size;
|
||||
NSSize size_in_window_coordinates = [self convertSize:size_in_points toView:nil];
|
||||
NSBitmapImageRep * bitmap_image_rep = [self renderOpenGLSceneToFramebufferAsFormat:GL_RGB viewWidth:size_in_window_coordinates.width viewHeight:size_in_window_coordinates.height];
|
||||
|
||||
NSImage* ns_image = [self imageFromBitmapImageRep:bitmap_image_rep];
|
||||
|
||||
if(ns_image)
|
||||
{
|
||||
NSSize image_size = [ns_image size];
|
||||
[ns_image drawAtPoint:NSMakePoint(0.0, 0.0)
|
||||
fromRect: NSMakeRect(0.0, 0.0, image_size.width, image_size.height)
|
||||
// operation: NSCompositeSourceOver
|
||||
operation: NSCompositeCopy
|
||||
fraction: 1.0];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"Image not valid");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Optional render to framebuffer stuff below. The code renders offscreen to assist in screen capture stuff.
|
||||
* This can be useful for things like the Dock minimization picture, drag-and-drop dropImage, copy and paste,
|
||||
* and printing.
|
||||
*/
|
||||
|
||||
// Convenience version. Will use the current view's bounds and produce an RGB image with the current clear color.
|
||||
- (NSBitmapImageRep*) renderOpenGLSceneToFramebuffer
|
||||
{
|
||||
NSSize size_in_points = [self bounds].size;
|
||||
NSSize size_in_window_coordinates = [self convertSize:size_in_points toView:nil];
|
||||
const osg::Vec4& clear_color = simpleViewer->getCamera()->getClearColor();
|
||||
|
||||
return [self renderOpenGLSceneToFramebufferAsFormat:GL_RGB viewWidth:size_in_window_coordinates.width viewHeight:size_in_window_coordinates.height clearColorRed:clear_color[0] clearColorGreen:clear_color[1] clearColorBlue:clear_color[2] clearColorAlpha:clear_color[3]];
|
||||
}
|
||||
|
||||
// Convenience version. Allows you to specify the view and height and format, but uses the current the current clear color.
|
||||
- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height
|
||||
{
|
||||
const osg::Vec4& clear_color = simpleViewer->getCamera()->getClearColor();
|
||||
|
||||
return [self renderOpenGLSceneToFramebufferAsFormat:gl_format viewWidth:view_width viewHeight:view_height clearColorRed:clear_color[0] clearColorGreen:clear_color[1] clearColorBlue:clear_color[2] clearColorAlpha:clear_color[3]];
|
||||
}
|
||||
|
||||
// Renders to an offscreen buffer and returns a copy of the data to an NSBitmapImageRep.
|
||||
// Allows you to specify the gl_format, width and height, and the glClearColor
|
||||
// gl_format is only GL_RGB or GLRGBA.
|
||||
- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height clearColorRed:(float)clear_red clearColorGreen:(float)clear_green clearColorBlue:(float)clear_blue clearColorAlpha:(float)clear_alpha
|
||||
{
|
||||
// Round values and bring to closest integer.
|
||||
int viewport_width = (int)(view_width + 0.5f);
|
||||
int viewport_height = (int)(view_height + 0.5f);
|
||||
|
||||
NSBitmapImageRep* ns_image_rep;
|
||||
osg::ref_ptr<osg::Image> osg_image = new osg::Image;
|
||||
|
||||
if(GL_RGBA == gl_format)
|
||||
{
|
||||
// Introduced in 10.4, but gives much better looking results if you utilize transparency
|
||||
if([NSBitmapImageRep instancesRespondToSelector:@selector(initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bitmapFormat:bytesPerRow:bitsPerPixel:)])
|
||||
{
|
||||
ns_image_rep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
|
||||
pixelsWide:viewport_width
|
||||
pixelsHigh:viewport_height
|
||||
bitsPerSample:8
|
||||
samplesPerPixel:4
|
||||
hasAlpha:YES
|
||||
isPlanar:NO
|
||||
colorSpaceName:NSCalibratedRGBColorSpace
|
||||
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat // 10.4+, gives much better looking results if you utilize transparency
|
||||
bytesPerRow:osg::Image::computeRowWidthInBytes(viewport_width, GL_RGBA, GL_UNSIGNED_BYTE, 1)
|
||||
bitsPerPixel:32]
|
||||
autorelease];
|
||||
}
|
||||
else // fallback for 10.0 to 10.3
|
||||
{
|
||||
ns_image_rep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
|
||||
pixelsWide:viewport_width
|
||||
pixelsHigh:viewport_height
|
||||
bitsPerSample:8
|
||||
samplesPerPixel:4
|
||||
hasAlpha:YES
|
||||
isPlanar:NO
|
||||
colorSpaceName:NSCalibratedRGBColorSpace
|
||||
// bitmapFormat:NSAlphaNonpremultipliedBitmapFormat // 10.4+, gives much better looking results if you utilize transparency
|
||||
bytesPerRow:osg::Image::computeRowWidthInBytes(viewport_width, GL_RGBA, GL_UNSIGNED_BYTE, 1)
|
||||
bitsPerPixel:32]
|
||||
autorelease];
|
||||
}
|
||||
// This is an optimization. Instead of creating data in both an osg::Image and NSBitmapImageRep,
|
||||
// Allocate just the memory in the NSBitmapImageRep and give the osg::Image a reference to the data.
|
||||
// I let NSBitmapImageRep control the memory because I think it will be easier to deal with
|
||||
// memory management in the cases where it must interact with other Cocoa mechanisms like Drag-and-drop
|
||||
// where the memory persistence is less obvious. Make sure that you don't expect to use the osg::Image
|
||||
// outside the scope of this function because there is no telling when the data will be removed out
|
||||
// from under it by Cocoa since osg::Image will not retain.
|
||||
osg_image->setImage([ns_image_rep pixelsWide], [ns_image_rep pixelsHigh], 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, [ns_image_rep bitmapData], osg::Image::NO_DELETE, 1);
|
||||
}
|
||||
else if(GL_RGB == gl_format)
|
||||
{
|
||||
ns_image_rep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
|
||||
pixelsWide:viewport_width
|
||||
pixelsHigh:viewport_height
|
||||
bitsPerSample:8
|
||||
samplesPerPixel:3
|
||||
hasAlpha:NO
|
||||
isPlanar:NO
|
||||
colorSpaceName:NSCalibratedRGBColorSpace
|
||||
// bitmapFormat:(NSBitmapFormat)0 // 10.4+
|
||||
bytesPerRow:osg::Image::computeRowWidthInBytes(viewport_width, GL_RGB, GL_UNSIGNED_BYTE, 1)
|
||||
bitsPerPixel:24]
|
||||
autorelease];
|
||||
|
||||
// This is an optimization. Instead of creating data in both an osg::Image and NSBitmapImageRep,
|
||||
// Allocate just the memory in the NSBitmapImageRep and give the osg::Image a reference to the data.
|
||||
// I let NSBitmapImageRep control the memory because I think it will be easier to deal with
|
||||
// memory management in the cases where it must interact with other Cocoa mechanisms like Drag-and-drop
|
||||
// where the memory persistence is less obvious. Make sure that you don't expect to use the osg::Image
|
||||
// outside the scope of this function because there is no telling when the data will be removed out
|
||||
// from under it by Cocoa since osg::Image will not retain.
|
||||
osg_image->setImage([ns_image_rep pixelsWide], [ns_image_rep pixelsHigh], 1, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, [ns_image_rep bitmapData], osg::Image::NO_DELETE, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"Sorry, unsupported format in renderOpenGLSceneToFramebufferAsFormat");
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Can't find a way to query SimpleViewer for the current values, so recompute current view size.
|
||||
NSSize original_size_in_points = [self bounds].size;
|
||||
NSSize original_size_in_window_coordinates = [self convertSize:original_size_in_points toView:nil];
|
||||
// simpleViewer->getEventQueue()->windowResize(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height);
|
||||
|
||||
simpleViewer->getEventQueue()->windowResize(0, 0, viewport_width, viewport_height);
|
||||
|
||||
/*
|
||||
* I want to use a Framebuffer Object because it seems to be the OpenGL sanctioned way of rendering offscreen.
|
||||
* Also, I want to try to decouple the image capture from the onscreen rendering. This is potentially useful
|
||||
* for two reasons:
|
||||
* 1) You may want to customize the image dimensions to best fit the situation (consider printing to a page to fit)
|
||||
* 2) You may want to customize the scene for the target (consider special output for a printer, or removed data for a thumbnail)
|
||||
* Unfortunately, I have hit two problems.
|
||||
* 1) osg::Camera (which seems to be the way to access Framebuffer Objects in OSG) doesn't seem to capture if it is the root node.
|
||||
* The workaround is to copy the camera attributes into another camera, and then add a second camera node into the scene.
|
||||
* I'm hoping OSG will simplify this in the future.
|
||||
* 2) I may have encountered a bug. Under some circumstances, the offscreen renderbuffer seems to get drawn into the onscreen view
|
||||
* when using a DragImage for drag-and-drop. I reproduced a non-OSG example, but learned I missed two important FBO calls which trigger gl errors.
|
||||
* So I'm wondering if OSG made the same mistake.
|
||||
* But the problem doesn't seem critical. It just looks bad.
|
||||
*/
|
||||
//NSLog(@"Before camera glGetError: %s", gluErrorString(glGetError()));
|
||||
osg::Camera* root_camera = simpleViewer->getCamera();
|
||||
|
||||
// I originally tried the clone() method and the copy construction, but it didn't work right,
|
||||
// so I manually copy the attributes.
|
||||
osg::Camera* the_camera = new osg::Camera;
|
||||
|
||||
the_camera->setClearMask(root_camera->getClearMask());
|
||||
the_camera->setProjectionMatrix(root_camera->getProjectionMatrix());
|
||||
the_camera->setViewMatrix(root_camera->getViewMatrix());
|
||||
the_camera->setViewport(root_camera->getViewport());
|
||||
the_camera->setClearColor(
|
||||
osg::Vec4(
|
||||
clear_red,
|
||||
clear_green,
|
||||
clear_blue,
|
||||
clear_alpha
|
||||
)
|
||||
);
|
||||
|
||||
// This must be ABSOLUTE_RF, and not a copy of the root camera because the transforms are additive.
|
||||
the_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
||||
|
||||
// We need to insert the new (second) camera into the scene (below the root camera) and attach
|
||||
// the scene data to the new camera.
|
||||
osg::ref_ptr<osg::Node> root_node = simpleViewer->getSceneView()->getSceneData();
|
||||
|
||||
the_camera->addChild(root_node.get());
|
||||
// Don't call (bypass) simpleViewer's setSceneData, but the underlying SceneView's setSceneData.
|
||||
// Otherwise, the camera position gets reset to the home position.
|
||||
simpleViewer->getSceneView()->setSceneData(the_camera);
|
||||
|
||||
// set the camera to render before the main camera.
|
||||
the_camera->setRenderOrder(osg::Camera::PRE_RENDER);
|
||||
|
||||
// tell the camera to use OpenGL frame buffer object where supported.
|
||||
the_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
|
||||
|
||||
|
||||
// attach the image so its copied on each frame.
|
||||
the_camera->attach(osg::Camera::COLOR_BUFFER, osg_image.get());
|
||||
|
||||
|
||||
//NSLog(@"Before frame(), glGetError: %s", gluErrorString(glGetError()));
|
||||
|
||||
|
||||
// Render the scene
|
||||
simpleViewer->frame();
|
||||
|
||||
// Not sure if I really need this (seems to work without it), and if so, not sure if I need flush or finish
|
||||
glFlush();
|
||||
// glFinish();
|
||||
|
||||
//NSLog(@"After flush(), glGetError: %s", gluErrorString(glGetError()));
|
||||
|
||||
|
||||
|
||||
// The image is upside-down to Cocoa, so invert it.
|
||||
osg_image.get()->flipVertical();
|
||||
|
||||
// Clean up everything I changed
|
||||
// the_camera->detach(osg::Camera::COLOR_BUFFER);
|
||||
// the_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER);
|
||||
// Don't call (bypass) simpleViewer's setSceneData, but the underlying SceneView's setSceneData.
|
||||
// Otherwise, the camera position gets reset to the home position.
|
||||
simpleViewer->getSceneView()->setSceneData(root_node.get());
|
||||
simpleViewer->getEventQueue()->windowResize(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height);
|
||||
|
||||
|
||||
// Ugh. Because of the bug I mentioned, I'm losing the picture in the display when I print.
|
||||
[self setNeedsDisplay:YES];
|
||||
//NSLog(@"at return, glGetError: %s", gluErrorString(glGetError()));
|
||||
|
||||
return ns_image_rep;
|
||||
}
|
||||
|
||||
// Convenience method
|
||||
- (NSImage*)imageFromBitmapImageRep:(NSBitmapImageRep*)bitmap_image_rep
|
||||
{
|
||||
if(nil == bitmap_image_rep)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
NSImage* image = [[[NSImage alloc] initWithSize:[bitmap_image_rep size]] autorelease];
|
||||
[image addRepresentation:bitmap_image_rep];
|
||||
// This doesn't seem to work as I want it to. The image only gets flipped when rendered in a regular view.
|
||||
// It doesn't flip for the printer view. I must actually invert the pixels.
|
||||
// [image setFlipped:YES];
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// End View and Draw Stuff ////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
@@ -710,9 +1192,124 @@
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// End of drag and drop ///////////////////////
|
||||
/////////////////////////// End of drag and drop (receiver) ////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// For drag and drop and copy/paste (source) ////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
- (IBAction) copy:(id)sender
|
||||
{
|
||||
NSString* type = NSTIFFPboardType;
|
||||
NSData* image_data = [self contentsAsDataOfType:type];
|
||||
|
||||
if(image_data)
|
||||
{
|
||||
NSPasteboard* general_pboard = [NSPasteboard generalPasteboard];
|
||||
[general_pboard declareTypes:[NSArray arrayWithObjects:type, nil] owner: nil];
|
||||
[general_pboard setData:image_data forType:type];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSData*) dataWithTIFFOfContentView
|
||||
{
|
||||
NSBitmapImageRep * image = [self renderOpenGLSceneToFramebuffer];
|
||||
NSData* data = nil;
|
||||
|
||||
if(image != nil)
|
||||
{
|
||||
data = [image TIFFRepresentation];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/* Returns a data object containing the current contents of the receiving window */
|
||||
- (NSData*) contentsAsDataOfType:(NSString *)pboardType
|
||||
{
|
||||
NSData * data = nil;
|
||||
if ([pboardType isEqualToString: NSTIFFPboardType] == YES)
|
||||
{
|
||||
data = [self dataWithTIFFOfContentView];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
- (void) startDragAndDropAsSource:(NSEvent*)the_event
|
||||
{
|
||||
NSPasteboard* drag_paste_board;
|
||||
NSImage* the_image;
|
||||
NSSize the_size;
|
||||
NSPoint the_point;
|
||||
|
||||
NSSize size_in_points = [self bounds].size;
|
||||
NSSize size_in_window_coordinates = [self convertSize:size_in_points toView:nil];
|
||||
|
||||
// Create the image that will be dragged
|
||||
NSString * type = NSTIFFPboardType;
|
||||
|
||||
// I want two images. One to be rendered for the target, and one as the drag-image.
|
||||
// I want the drag-image to be translucent.
|
||||
// I think this is where render GL_COLOR_ATTACHMENTn (COLOR_BUFFERn?) would be handy.
|
||||
// But my hardware only returns 1 for glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, maxbuffers);
|
||||
// So I won't bother and will just render twice.
|
||||
NSBitmapImageRep* bitmap_image_rep = [self renderOpenGLSceneToFramebufferAsFormat:GL_RGB viewWidth:size_in_window_coordinates.width viewHeight:size_in_window_coordinates.height];
|
||||
NSBitmapImageRep* bitmap_image_rep_transparent_copy = [self renderOpenGLSceneToFramebufferAsFormat:GL_RGBA viewWidth:size_in_window_coordinates.width viewHeight:size_in_window_coordinates.height];
|
||||
// NSBitmapImageRep* bitmap_image_rep = [self renderOpenGLSceneToFramebufferAsFormat:GL_RGBA viewWidth:size_in_window_coordinates.width viewHeight:size_in_window_coordinates.height clearColorRed:1.0f clearColorGreen:1.0f clearColorBlue:0.0f clearColorAlpha:0.4f];
|
||||
|
||||
//NSBitmapImageRep* bitmap_image_rep_transparent_copy = bitmap_image_rep;
|
||||
|
||||
// 0x32 is an arbitrary number. Basically, I want something between 0 and 0xFF.
|
||||
Internal_SetAlpha(bitmap_image_rep_transparent_copy, 0x32);
|
||||
|
||||
NSData* image_data = [bitmap_image_rep TIFFRepresentation];
|
||||
|
||||
if(image_data)
|
||||
{
|
||||
|
||||
drag_paste_board = [NSPasteboard pasteboardWithName:NSDragPboard];
|
||||
// is owner:self or nil? (Hillegass says self)
|
||||
[drag_paste_board declareTypes: [NSArray arrayWithObjects: type, nil] owner: self];
|
||||
[drag_paste_board setData:image_data forType: type];
|
||||
|
||||
// create an image from the data
|
||||
the_image = [[NSImage alloc] initWithData:[bitmap_image_rep_transparent_copy TIFFRepresentation]];
|
||||
|
||||
the_point = [self convertPoint:[the_event locationInWindow] fromView:nil];
|
||||
the_size = [the_image size];
|
||||
|
||||
// shift the point to the center of the image
|
||||
the_point.x = the_point.x - the_size.width/2.0;
|
||||
the_point.y = the_point.y - the_size.height/2.0;
|
||||
|
||||
// start drag
|
||||
[self dragImage:the_image
|
||||
at:the_point
|
||||
offset:NSMakeSize(0,0)
|
||||
event:the_event
|
||||
pasteboard:drag_paste_board
|
||||
source:self
|
||||
slideBack:YES];
|
||||
|
||||
[the_image release];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"Error, failed to create image data");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// For drag and drop and copy/paste (source) ////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////// IBAction examples /////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user