Skip to content
Snippets Groups Projects
Commit e36769aa authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Modernize objc a bit by replacing NSAutoReplacePool with @autoreplacepool

parent ee20c271
No related branches found
No related tags found
No related merge requests found
...@@ -207,57 +207,61 @@ attributedStringInfo getSegments(NSAttributedString *str) ...@@ -207,57 +207,61 @@ attributedStringInfo getSegments(NSAttributedString *str)
- (id) initWithFrame:(NSRect)frame withSamples:(NSUInteger)samples andVsync:(BOOL)vsync - (id) initWithFrame:(NSRect)frame withSamples:(NSUInteger)samples andVsync:(BOOL)vsync
{ {
[self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]]; self = [super initWithFrame:frame];
[self initWithFrame:frame]; if (!self) { return self; } // Despite what this may look like, returning nil self is a-ok.
@autoreleasepool {
// Initialize with a default "safe" pixel format that will work with versions dating back to OS X 10.6. [self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
// Any specialized pixel formats, i.e. a core profile pixel format, should be initialized through rebuildContextWithFormat.
// 10.7 and 10.8 don't really care if we're defining a profile or not. If we don't explicitly request a core or legacy profile, it'll always assume a legacy profile (for compatibility reasons). // Initialize with a default "safe" pixel format that will work with versions dating back to OS X 10.6.
NSOpenGLPixelFormatAttribute attrs[] = { // Any specialized pixel formats, i.e. a core profile pixel format, should be initialized through rebuildContextWithFormat.
NSOpenGLPFANoRecovery, // 10.7 and 10.8 don't really care if we're defining a profile or not. If we don't explicitly request a core or legacy profile, it'll always assume a legacy profile (for compatibility reasons).
NSOpenGLPFADoubleBuffer, NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFAClosestPolicy, NSOpenGLPFANoRecovery,
NSOpenGLPFAAccelerated, NSOpenGLPFADoubleBuffer,
NSOpenGLPFASampleBuffers, static_cast<NSOpenGLPixelFormatAttribute>(samples > 0 ? 1 : 0), NSOpenGLPFAClosestPolicy,
NSOpenGLPFASamples, static_cast<NSOpenGLPixelFormatAttribute>(samples), NSOpenGLPFAAccelerated,
NSOpenGLPFAStencilSize, 8, NSOpenGLPFAMultisample,
NSOpenGLPFADepthSize, 24, NSOpenGLPFASampleBuffers, static_cast<NSOpenGLPixelFormatAttribute>((samples > 0 ? 1 : 0)),
NSOpenGLPFAAlphaSize, 8, NSOpenGLPFASamples, static_cast<NSOpenGLPixelFormatAttribute>(samples),
NSOpenGLPFAColorSize, 24, NSOpenGLPFAStencilSize, static_cast<NSOpenGLPixelFormatAttribute>(8),
0 NSOpenGLPFADepthSize, static_cast<NSOpenGLPixelFormatAttribute>(24),
}; NSOpenGLPFAAlphaSize, static_cast<NSOpenGLPixelFormatAttribute>(8),
NSOpenGLPFAColorSize, static_cast<NSOpenGLPixelFormatAttribute>(24),
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease]; 0
};
if (pixelFormat == nil)
{ NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
NSLog(@"Failed to create pixel format!", nil);
return nil; if (pixelFormat == nil)
} {
NSLog(@"Failed to create pixel format!", nil);
NSOpenGLContext *glContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil]; return nil;
}
if (glContext == nil)
{ NSOpenGLContext *glContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
NSLog(@"Failed to create OpenGL context!", nil);
return nil; if (glContext == nil)
} {
NSLog(@"Failed to create OpenGL context!", nil);
//for retina support return nil;
[self setWantsBestResolutionOpenGLSurface:YES]; }
[self setPixelFormat:pixelFormat]; //for retina support
[self setWantsBestResolutionOpenGLSurface:YES];
[self setPixelFormat:pixelFormat];
[self setOpenGLContext:glContext];
[glContext setView:self];
[glContext makeCurrentContext];
GLint glVsync = vsync ? 1 : 0;
[glContext setValues:&glVsync forParameter:NSOpenGLCPSwapInterval];
[self setOpenGLContext:glContext]; }
[glContext setView:self];
[glContext makeCurrentContext];
GLint glVsync = vsync ? 1 : 0;
[glContext setValues:&glVsync forParameter:NSOpenGLCPSwapInterval];
return self; return self;
} }
...@@ -268,20 +272,23 @@ attributedStringInfo getSegments(NSAttributedString *str) ...@@ -268,20 +272,23 @@ attributedStringInfo getSegments(NSAttributedString *str)
- (BOOL) rebuildContextWithFormat:(NSOpenGLPixelFormat *)format - (BOOL) rebuildContextWithFormat:(NSOpenGLPixelFormat *)format
{ {
NSOpenGLContext *ctx = [self openGLContext]; @autoreleasepool {
NSOpenGLContext *ctx = [self openGLContext];
[ctx clearDrawable];
[ctx initWithFormat:format shareContext:nil]; [ctx clearDrawable];
ctx = [[[NSOpenGLContext alloc] initWithFormat:format shareContext:nil] autorelease];
if (ctx == nil)
{ if (ctx == nil)
NSLog(@"Failed to create OpenGL context!", nil); {
return false; NSLog(@"Failed to create OpenGL context!", nil);
} return false;
}
[self setOpenGLContext:ctx];
[ctx setView:self]; [self setOpenGLContext:ctx];
[ctx makeCurrentContext]; [ctx setView:self];
[ctx makeCurrentContext];
}
return true; return true;
} }
......
...@@ -49,14 +49,12 @@ void setupCocoa() ...@@ -49,14 +49,12 @@ void setupCocoa()
if(!inited) if(!inited)
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @autoreleasepool {
// The following prevents the Cocoa command line parser from trying to open 'unknown' arguements as documents.
// The following prevents the Cocoa command line parser from trying to open 'unknown' arguements as documents. // ie. running './secondlife -set Language fr' would cause a pop-up saying can't open document 'fr'
// ie. running './secondlife -set Language fr' would cause a pop-up saying can't open document 'fr' // when init'ing the Cocoa App window.
// when init'ing the Cocoa App window. [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"];
[[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"]; }
[pool release];
inited = true; inited = true;
} }
...@@ -102,21 +100,19 @@ unsigned short *copyFromPBoard() ...@@ -102,21 +100,19 @@ unsigned short *copyFromPBoard()
CursorRef createImageCursor(const char *fullpath, int hotspotX, int hotspotY) CursorRef createImageCursor(const char *fullpath, int hotspotX, int hotspotY)
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @autoreleasepool {
// extra retain on the NSCursor since we want it to live for the lifetime of the app.
// extra retain on the NSCursor since we want it to live for the lifetime of the app. NSCursor *cursor =
NSCursor *cursor = [[[NSCursor alloc]
[[[NSCursor alloc] initWithImage:
initWithImage: [[[NSImage alloc] initWithContentsOfFile:
[[[NSImage alloc] initWithContentsOfFile: [NSString stringWithFormat:@"%s", fullpath]
[NSString stringWithFormat:@"%s", fullpath] ]autorelease]
]autorelease] hotSpot:NSMakePoint(hotspotX, hotspotY)
hotSpot:NSMakePoint(hotspotX, hotspotY) ] retain];
]retain];
return (CursorRef)cursor;
[pool release]; }
return (CursorRef)cursor;
} }
void setArrowCursor() void setArrowCursor()
...@@ -176,10 +172,10 @@ OSErr releaseImageCursor(CursorRef ref) ...@@ -176,10 +172,10 @@ OSErr releaseImageCursor(CursorRef ref)
{ {
if( ref != NULL ) if( ref != NULL )
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @autoreleasepool {
NSCursor *cursor = (NSCursor*)ref; NSCursor *cursor = (NSCursor*)ref;
[cursor release]; [cursor release];
[pool release]; }
} }
else else
{ {
...@@ -193,10 +189,10 @@ OSErr setImageCursor(CursorRef ref) ...@@ -193,10 +189,10 @@ OSErr setImageCursor(CursorRef ref)
{ {
if( ref != NULL ) if( ref != NULL )
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @autoreleasepool {
NSCursor *cursor = (NSCursor*)ref; NSCursor *cursor = (NSCursor*)ref;
[cursor set]; [cursor set];
[pool release]; }
} }
else else
{ {
...@@ -395,46 +391,47 @@ void requestUserAttention() ...@@ -395,46 +391,47 @@ void requestUserAttention()
long showAlert(std::string text, std::string title, int type) long showAlert(std::string text, std::string title, int type)
{ {
NSAlert *alert = [[NSAlert alloc] init]; @autoreleasepool {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:[NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]]];
[alert setInformativeText:[NSString stringWithCString:text.c_str() encoding:[NSString defaultCStringEncoding]]]; [alert setMessageText:[NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]]];
if (type == 0) [alert setInformativeText:[NSString stringWithCString:text.c_str() encoding:[NSString defaultCStringEncoding]]];
{ if (type == 0)
[alert addButtonWithTitle:@"Okay"]; {
} else if (type == 1) [alert addButtonWithTitle:@"Okay"];
{ } else if (type == 1)
[alert addButtonWithTitle:@"Okay"];
[alert addButtonWithTitle:@"Cancel"];
} else if (type == 2)
{
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
}
long ret = [alert runModal];
[alert dealloc];
if (ret == NSAlertFirstButtonReturn)
{
if (type == 1)
{ {
ret = 3; [alert addButtonWithTitle:@"Okay"];
[alert addButtonWithTitle:@"Cancel"];
} else if (type == 2) } else if (type == 2)
{ {
ret = 0; [alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
} }
} else if (ret == NSAlertSecondButtonReturn) long ret = [alert runModal];
{
if (type == 0 || type == 1) if (ret == NSAlertFirstButtonReturn)
{ {
ret = 2; if (type == 1)
} else if (type == 2) {
ret = 3;
} else if (type == 2)
{
ret = 0;
}
} else if (ret == NSAlertSecondButtonReturn)
{ {
ret = 1; if (type == 0 || type == 1)
{
ret = 2;
} else if (type == 2)
{
ret = 1;
}
} }
return ret;
} }
return ret;
} }
unsigned int getModifiers() unsigned int getModifiers()
...@@ -444,10 +441,10 @@ unsigned int getModifiers() ...@@ -444,10 +441,10 @@ unsigned int getModifiers()
void setTitle(const std::string& title) void setTitle(const std::string& title)
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @autoreleasepool {
LLNSWindow *winRef = [(LLAppDelegate*)[[LLApplication sharedApplication] delegate] window]; LLNSWindow *winRef = [(LLAppDelegate*)[[LLApplication sharedApplication] delegate] window];
NSString *nsTitle = [NSString stringWithUTF8String:title.c_str()]; NSString *nsTitle = [NSString stringWithUTF8String:title.c_str()];
[winRef setTitle:nsTitle]; [winRef setTitle:nsTitle];
[pool release]; }
} }
...@@ -358,7 +358,7 @@ struct AttachmentInfo ...@@ -358,7 +358,7 @@ struct AttachmentInfo
} }
else else
{ {
[super sendEvent:event]; [super sendEvent:event];
} }
} }
......
...@@ -35,39 +35,35 @@ ...@@ -35,39 +35,35 @@
void launchApplication(const std::string* app_name, const std::vector<std::string>* args) void launchApplication(const std::string* app_name, const std::vector<std::string>* args)
{ {
@autoreleasepool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; {
if (app_name->empty()) return;
if (app_name->empty()) return;
NSMutableString* app_name_ns = [NSMutableString stringWithString:[[NSBundle mainBundle] resourcePath]]; //Path to resource dir
NSMutableString* app_name_ns = [NSMutableString stringWithString:[[NSBundle mainBundle] resourcePath]]; //Path to resource dir [app_name_ns appendFormat:@"/%@", [NSString stringWithCString:app_name->c_str()
[app_name_ns appendFormat:@"/%@", [NSString stringWithCString:app_name->c_str() encoding:[NSString defaultCStringEncoding]]];
encoding:[NSString defaultCStringEncoding]]];
NSMutableArray *args_ns = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *args_ns = nil;
args_ns = [[NSMutableArray alloc] init]; for (int i=0; i < args->size(); ++i)
{
for (int i=0; i < args->size(); ++i) NSLog(@"Adding string %s", (*args)[i].c_str());
{ [args_ns addObject:
NSLog(@"Adding string %s", (*args)[i].c_str()); [NSString stringWithCString:(*args)[i].c_str()
[args_ns addObject: encoding:[NSString defaultCStringEncoding]]];
[NSString stringWithCString:(*args)[i].c_str() }
encoding:[NSString defaultCStringEncoding]]];
} NSTask *task = [[[NSTask alloc] init] autorelease];
NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:app_name_ns]];
NSTask *task = [[NSTask alloc] init]; [task setLaunchPath:[bundle executablePath]];
NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:app_name_ns]]; [task setArguments:args_ns];
[task setLaunchPath:[bundle executablePath]]; [task launch];
[task setArguments:args_ns];
[task launch]; // NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
// NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:app_name_ns]];
// NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; //
// NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:app_name_ns]]; // NSError *error = nil;
// // [workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:args_ns forKey:NSWorkspaceLaunchConfigurationArguments] error:&error];
// NSError *error = nil; //TODO Handle error
// [workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:args_ns forKey:NSWorkspaceLaunchConfigurationArguments] error:&error]; }
//TODO Handle error
[pool release];
return;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment