# HG changeset patch # User mhessling@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1300406313 0 # Node ID 2aaa3f67cfb174707cb466803a9b5c3180ffac6d # Parent 39d848c20c054e55d3ec56404f681d2e9fee7618 Fix for crashes with generating images from data diff -r 39d848c20c05 -r 2aaa3f67cfb1 mac/dw.m --- a/mac/dw.m Thu Mar 17 23:57:49 2011 +0000 +++ b/mac/dw.m Thu Mar 17 23:58:33 2011 +0000 @@ -82,7 +82,7 @@ int id; void *signalfunction; void *data; - + } SignalHandler; SignalHandler *Root = NULL; @@ -95,7 +95,7 @@ SignalHandler *_get_handler(HWND window, int messageid) { SignalHandler *tmp = Root; - + /* Find any callbacks for this function */ while(tmp) { @@ -112,7 +112,7 @@ { ULONG message; char name[30]; - + } SignalList; /* List of signals */ @@ -141,7 +141,7 @@ { SignalHandler *handler = _get_handler(object, message); /* NSLog(@"Event handler - type %d\n", message); */ - + if(handler) { switch(message) @@ -150,7 +150,7 @@ case 0: { int (* API timerfunc)(void *) = (int (* API)(void *))handler->signalfunction; - + if(!timerfunc(handler->data)) dw_timer_disconnect(handler->id); return 0; @@ -160,7 +160,7 @@ { int (*sizefunc)(HWND, int, int, void *) = handler->signalfunction; NSSize size; - + if([object isMemberOfClass:[NSWindow class]]) { NSWindow *window = object; @@ -171,13 +171,13 @@ NSView *view = object; size = [view frame].size; } - + if(size.width > 0 && size.height > 0) { return sizefunc(object, size.width, size.height, handler->data); } return 0; - } + } case 2: { int (*keypressfunc)(HWND, char, int, int, void *) = handler->signalfunction; @@ -205,7 +205,7 @@ int (* API buttonfunc)(HWND, int, int, int, void *) = (int (* API)(HWND, int, int, int, void *))handler->signalfunction; int button = (int)event; LONG x,y; - + dw_pointer_query_pos(&x, &y); return buttonfunc(object, (int)x, (int)y, button, handler->data); @@ -222,7 +222,7 @@ DWExpose exp; int (* API exposefunc)(HWND, DWExpose *, void *) = (int (* API)(HWND, DWExpose *, void *))handler->signalfunction; NSRect rect = [object frame]; - + exp.x = rect.origin.x; exp.y = rect.origin.y; exp.width = rect.size.width; @@ -252,9 +252,9 @@ char *text = (char *)event; void *user = NULL; LONG x,y; - + dw_pointer_query_pos(&x, &y); - + return containercontextfunc(handler->window, text, (int)x, (int)y, handler->data, user); } /* Generic selection changed event for several classes */ @@ -295,7 +295,7 @@ } return result; } - + return treeselectfunc(handler->window, item, text, handler->data, user); } /* Set Focus event */ @@ -421,7 +421,7 @@ NSValue *v = (NSValue *)param; void **params = (void **)[v pointerValue]; void (* windowfunc)(void *); - + if(params) { windowfunc = params[0]; @@ -429,7 +429,7 @@ { windowfunc(params[1]); } - } + } } @end @@ -510,7 +510,7 @@ -(void)setColor:(unsigned long)input { id orig = bgcolor; - + if(input == _colors[DW_CLR_DEFAULT]) { bgcolor = nil; @@ -678,7 +678,7 @@ { id object = [notepage view]; DWNotebookPage *page = (DWNotebookPage *)notepage; - + if([object isMemberOfClass:[DWBox class]]) { DWBox *view = object; @@ -687,7 +687,7 @@ _do_resize(box, size.width, size.height); } _event_handler(self, (void *)[page pageid], 15); -} +} -(void)dealloc { UserData *root = userdata; _remove_userdata(&root, NULL, TRUE); [super dealloc]; } @end @@ -738,7 +738,7 @@ { NSArray *views = [self subviews]; id object; - + for(object in views) { if([object isMemberOfClass:[DWBox class]]) @@ -748,7 +748,7 @@ NSSize size = [view frame].size; _do_resize(box, size.width, size.height); } - } + } } -(void *)userdata { return userdata; } -(void)setUserdata:(void *)input { userdata = input; } @@ -962,7 +962,7 @@ { int z, col = -1; int count = (int)[tvcols count]; - + for(z=0;zmessage = message; new->window = window; new->id = msgid; new->signalfunction = signalfunction; new->data = data; new->next = NULL; - + if (!Root) Root = new; else @@ -1484,7 +1484,7 @@ ULONG _findsigmessage(char *signame) { int z; - + for(z=0;zpad * 2); (*usedy) += (thisbox->pad * 2); @@ -1810,7 +1810,7 @@ DWNotebook *notebook = (DWNotebook *)handle; DWNotebookPage *notepage = (DWNotebookPage *)[notebook selectedTabViewItem]; DWBox *view = [notepage view]; - + if(view != nil) { Box *box = [view box]; @@ -1837,14 +1837,14 @@ { DWSplitBar *split = (DWSplitBar *)handle; float percent = [split percent]; - + if(percent > 0) { dw_splitbar_set(handle, percent); [split setPercent:0]; } } - + if(thisbox->type == DW_HORZ) currentx += width + vectorx + (pad * 2); if(thisbox->type == DW_VERT) @@ -1881,7 +1881,7 @@ NSMenu *_generate_main_menu() { NSString *applicationName = nil; - + /* This only works on 10.6 so we have a backup method */ #ifdef BUILDING_FOR_SNOW_LEOPARD applicationName = [[NSRunningApplication currentApplication] localizedName]; @@ -1890,57 +1890,57 @@ { applicationName = [[NSProcessInfo processInfo] processName]; } - + /* Create the main menu */ NSMenu * mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease]; - + NSMenuItem * mitem = [mainMenu addItemWithTitle:@"Apple" action:NULL keyEquivalent:@""]; NSMenu * menu = [[[NSMenu alloc] initWithTitle:@"Apple"] autorelease]; - + [DWApp performSelector:@selector(setAppleMenu:) withObject:menu]; - + /* Setup the Application menu */ NSMenuItem * item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"About", nil), applicationName] action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; [item setTarget:DWApp]; - + [menu addItem:[NSMenuItem separatorItem]]; - + item = [menu addItemWithTitle:NSLocalizedString(@"Services", nil) action:NULL keyEquivalent:@""]; NSMenu * servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease]; [menu setSubmenu:servicesMenu forItem:item]; [DWApp setServicesMenu:servicesMenu]; - + [menu addItem:[NSMenuItem separatorItem]]; - + item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Hide", nil), applicationName] action:@selector(hide:) keyEquivalent:@"h"]; [item setTarget:DWApp]; - + item = [menu addItemWithTitle:NSLocalizedString(@"Hide Others", nil) action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; [item setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask]; [item setTarget:DWApp]; - + item = [menu addItemWithTitle:NSLocalizedString(@"Show All", nil) action:@selector(unhideAllApplications:) keyEquivalent:@""]; [item setTarget:DWApp]; - + [menu addItem:[NSMenuItem separatorItem]]; - + item = [menu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Quit", nil), applicationName] action:@selector(terminate:) keyEquivalent:@"q"]; [item setTarget:DWApp]; - + [mainMenu setSubmenu:menu forItem:mitem]; - + return mainMenu; } @@ -2058,11 +2058,11 @@ char *dw_user_dir(void) { static char _user_dir[1024] = ""; - + if(!_user_dir[0]) { char *home = getenv("HOME"); - + if(home) strcpy(_user_dir, home); else @@ -2091,7 +2091,7 @@ va_start(args, format); vsprintf(outbuf, format, args); va_end(args); - + if(flags & DW_MB_OKCANCEL) { button2 = @"Cancel"; @@ -2107,7 +2107,7 @@ button2 = @"No"; button3 = @"Cancel"; } - + if(flags & DW_MB_ERROR) { iResponse = (int) @@ -2121,7 +2121,7 @@ [ NSString stringWithUTF8String:outbuf ], button1, button2, button3); } - + switch(iResponse) { case NSAlertDefaultReturn: /* user pressed OK */ @@ -2162,14 +2162,14 @@ { /* Create the File Open Dialog class. */ NSOpenPanel* openDlg = [NSOpenPanel openPanel]; - + /* Enable the selection of files in the dialog. */ [openDlg setCanChooseFiles:YES]; [openDlg setCanChooseDirectories:NO]; - + /* Disable multiple selection */ [openDlg setAllowsMultipleSelection:NO]; - + /* Display the dialog. If the OK button was pressed, * process the files. */ @@ -2180,17 +2180,17 @@ */ NSArray* files = [openDlg filenames]; NSString* fileName = [files objectAtIndex:0]; - return strdup([ fileName UTF8String ]); + return strdup([ fileName UTF8String ]); } } else { /* Create the File Save Dialog class. */ NSSavePanel* saveDlg = [NSSavePanel savePanel]; - + /* Enable the creation of directories in the dialog. */ [saveDlg setCanCreateDirectories:YES]; - + /* Display the dialog. If the OK button was pressed, * process the files. */ @@ -2200,8 +2200,8 @@ * files and directories selected. */ NSString* fileName = [saveDlg filename]; - return strdup([ fileName UTF8String ]); - } + return strdup([ fileName UTF8String ]); + } } return NULL; @@ -2234,13 +2234,13 @@ void dw_clipboard_set_text( char *str, int len) { NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; - + /* Only in Snow Leopard */ if(DWOSMinor > 5) { [pasteboard clearContents]; } - + [pasteboard setString:[ NSString stringWithUTF8String:str ] forType:NSStringPboardType]; } @@ -2253,7 +2253,7 @@ DWDialog * API dw_dialog_new(void *data) { DWDialog *tmp = malloc(sizeof(DWDialog)); - + if(tmp) { tmp->eve = dw_event_new(); @@ -2262,7 +2262,7 @@ tmp->done = FALSE; tmp->result = NULL; } - return tmp; + return tmp; } /* @@ -2289,7 +2289,7 @@ void * API dw_dialog_wait(DWDialog *dialog) { void *tmp; - + while(!dialog->done) { _dw_main_iteration([NSDate dateWithTimeIntervalSinceNow:0.01]); @@ -2404,7 +2404,7 @@ NSWindow *window = box; view = [window contentView]; } - + thisbox = [view box]; thisitem = thisbox->items; object = item; @@ -2462,7 +2462,7 @@ /* Add the item to the box */ [view addSubview:this]; - + /* Free the old data */ if(thisbox->count) free(thisitem); @@ -2497,7 +2497,7 @@ NSWindow *window = box; view = [window contentView]; } - + thisbox = [view box]; thisitem = thisbox->items; object = item; @@ -2555,7 +2555,7 @@ /* Add the item to the box */ [view addSubview:this]; - + /* Free the old data */ if(thisbox->count) free(thisitem); @@ -3039,11 +3039,11 @@ int _locked_by_me = FALSE; DW_MUTEX_LOCK; id object = handle; - + if([object isMemberOfClass:[DWComboBox class]]) { DWComboBox *combo = handle; - + [combo addItemWithObjectValue:[ NSString stringWithUTF8String:text ]]; } else if([object isMemberOfClass:[DWContainer class]]) @@ -3073,11 +3073,11 @@ int _locked_by_me = FALSE; DW_MUTEX_LOCK; id object = handle; - + if([object isMemberOfClass:[DWComboBox class]]) { DWComboBox *combo = handle; - + [combo insertItemWithObjectValue:[ NSString stringWithUTF8String:text ] atIndex:pos]; } else if([object isMemberOfClass:[DWContainer class]]) @@ -3104,14 +3104,14 @@ int _locked_by_me = FALSE; DW_MUTEX_LOCK; id object = handle; - + if([object isMemberOfClass:[DWComboBox class]]) { DWComboBox *combo = handle; int z; - + for(z=0;zwidth = width; @@ -5048,7 +5048,7 @@ HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename) { HPIXMAP pixmap; - + if(!(pixmap = calloc(1,sizeof(struct _hpixmap)))) return NULL; NSString *nstr = [ NSString stringWithUTF8String:filename ]; @@ -5080,10 +5080,10 @@ HPIXMAP API dw_pixmap_new_from_data(HWND handle, char *data, int len) { HPIXMAP pixmap; - + if(!(pixmap = calloc(1,sizeof(struct _hpixmap)))) return NULL; - NSData *thisdata = [[NSData alloc] dataWithBytes:data length:len]; + NSData *thisdata = [NSData dataWithBytes:data length:len]; NSImage *image = [[NSImage alloc] initWithData:thisdata]; NSSize size = [image size]; pixmap->width = size.width; @@ -5113,7 +5113,7 @@ HPIXMAP API dw_pixmap_grab(HWND handle, ULONG resid) { HPIXMAP pixmap; - + if(!(pixmap = calloc(1,sizeof(struct _hpixmap)))) return NULL; @@ -5212,9 +5212,9 @@ DWCalendar *calendar = handle; NSDate *date; char buffer[100]; - + sprintf(buffer, "%04d-%02d-%02d 00:00:00 +0600", year, month, day); - + date = [[NSDate alloc] initWithString:[ NSString stringWithUTF8String:buffer ]]; [calendar setDateValue:date]; [date release]; @@ -5235,10 +5235,10 @@ sscanf([ nstr UTF8String ], "%d/%d/%d", month, day, year); if(*year < 70) { - *year += 2000; + *year += 2000; } else if(*year < 100) - { + { *year += 1900; } [df release]; @@ -5425,7 +5425,7 @@ { int z, cur=0; char accel = '\0'; - + for(z=0;z 5 && [object isMemberOfClass:[NSWindow class]]) { NSWindow *window = object; int currentstyle = (int)[window styleMask]; int tmp; - + tmp = currentstyle | (int)mask; tmp ^= mask; tmp |= style; - + [window setStyleMask:tmp]; } else if([object isKindOfClass:[NSTextField class]]) { NSTextField *tf = object; - + /* TODO: See if we need to switch to a bitmask */ [[tf cell] setAlignment:style]; } @@ -6041,7 +6041,7 @@ char *fontcopy = strdup(fontname); char *name = strchr(fontcopy, '.'); NSFont *font = nil; - + if(name) { int size = atoi(fontcopy); @@ -6062,7 +6062,7 @@ int API dw_window_set_font(HWND handle, char *fontname) { NSFont *font = _dw_font_by_name(fontname); - + if(font) { id object = handle; @@ -6080,7 +6080,7 @@ else if([object isMemberOfClass:[DWRender class]]) { DWRender *render = object; - + [render setFont:font]; } } @@ -6095,7 +6095,7 @@ char * API dw_window_get_font(HWND handle) { id object = handle; - + if([object isKindOfClass:[NSControl class]] || [object isMemberOfClass:[DWRender class]]) { NSFont *font = [object font]; @@ -6116,7 +6116,7 @@ int _locked_by_me = FALSE; DW_MUTEX_LOCK; NSObject *object = handle; - + if([ object isKindOfClass:[ NSWindow class ] ]) { NSWindow *window = handle; @@ -6136,19 +6136,19 @@ char * API dw_window_get_text(HWND handle) { NSObject *object = handle; - + if([ object isKindOfClass:[ NSControl class ] ]) { NSControl *control = handle; NSString *nsstr = [ control stringValue]; - + return strdup([ nsstr UTF8String ]); } else if([ object isKindOfClass:[ NSWindow class ] ]) { NSWindow *window = handle; NSString *nsstr = [ window title]; - + return strdup([ nsstr UTF8String ]); } return NULL; @@ -6165,7 +6165,7 @@ int _locked_by_me = FALSE; DW_MUTEX_LOCK; NSObject *object = handle; - + if([ object isKindOfClass:[ NSControl class ] ]) { NSControl *control = handle; @@ -6225,9 +6225,9 @@ if([ object isKindOfClass:[ NSImageView class ] ]) { NSImageView *iv = handle; - NSData *thisdata = [[NSData alloc] dataWithBytes:data length:len]; + NSData *thisdata = [NSData dataWithBytes:data length:len]; NSImage *pixmap = [[NSImage alloc] initWithData:thisdata]; - + if(pixmap) { [iv setImage:pixmap]; @@ -6262,7 +6262,7 @@ { bitmap = dw_icon_load(0, resid); } - + if(bitmap) { [iv setImage:bitmap]; @@ -6360,7 +6360,7 @@ NSSize size; size.width = width; size.height = height; - + if([ object isKindOfClass:[ NSWindow class ] ]) { NSWindow *window = handle; @@ -6384,7 +6384,7 @@ NSPoint point; point.x = x; point.y = y; - + if([ object isKindOfClass:[ NSWindow class ] ]) { NSWindow *window = handle; @@ -6420,7 +6420,7 @@ void API dw_window_get_pos_size(HWND handle, LONG *x, LONG *y, ULONG *width, ULONG *height) { NSObject *object = handle; - + if([ object isKindOfClass:[ NSWindow class ] ]) { NSWindow *window = handle; @@ -6470,16 +6470,16 @@ void dw_environment_query(DWEnv *env) { struct utsname name; - + uname(&name); strcpy(env->osName, "MacOS"); - + strcpy(env->buildDate, __DATE__); strcpy(env->buildTime, __TIME__); env->DWMajorVersion = DW_MAJOR_VERSION; env->DWMinorVersion = DW_MINOR_VERSION; env->DWSubVersion = DW_SUB_VERSION; - + env->MajorVersion = DWOSMajor; env->MinorVersion = DWOSMinor; env->MajorBuild = DWOSBuild; @@ -6512,7 +6512,7 @@ UserData *_find_userdata(UserData **root, char *varname) { UserData *tmp = *root; - + while(tmp) { if(strcasecmp(tmp->varname, varname) == 0) @@ -6525,7 +6525,7 @@ int _new_userdata(UserData **root, char *varname, void *data) { UserData *new = _find_userdata(root, varname); - + if(new) { new->data = data; @@ -6538,9 +6538,9 @@ { new->varname = strdup(varname); new->data = data; - + new->next = NULL; - + if (!*root) *root = new; else @@ -6565,7 +6565,7 @@ int _remove_userdata(UserData **root, char *varname, int all) { UserData *prev = NULL, *tmp = *root; - + while(tmp) { if(all || strcasecmp(tmp->varname, varname) == 0) @@ -6620,16 +6620,16 @@ object = [sv documentView]; } WindowData *blah = (WindowData *)[object userdata]; - + if(!blah) { if(!dataname) return; - + blah = calloc(1, sizeof(WindowData)); [object setUserdata:blah]; } - + if(data) _new_userdata(&(blah->root), dataname, data); else @@ -6662,7 +6662,7 @@ object = [sv documentView]; } WindowData *blah = (WindowData *)[object userdata]; - + if(blah && blah->root && dataname) { UserData *ud = _find_userdata(&(blah->root), dataname); @@ -6687,7 +6687,7 @@ int API dw_timer_connect(int interval, void *sigfunc, void *data) { int z; - + for(z=0;zid == timerid && tmp->window == thistimer) @@ -6780,10 +6780,10 @@ { SignalHandler *prev = NULL, *tmp = Root; ULONG message; - + if(!window || !signame || (message = _findsigmessage(signame)) == 0) return; - + while(tmp) { if(tmp->window == window && tmp->message == message) @@ -6817,7 +6817,7 @@ void API dw_signal_disconnect_by_window(HWND window) { SignalHandler *prev = NULL, *tmp = Root; - + while(tmp) { if(tmp->window == window) @@ -6852,7 +6852,7 @@ void API dw_signal_disconnect_by_data(HWND window, void *data) { SignalHandler *prev = NULL, *tmp = Root; - + while(tmp) { if(tmp->window == window && tmp->data == data) @@ -7314,7 +7314,7 @@ free(tmpsock); return NULL; } - + tmpsock[0] = socket(AF_UNIX, SOCK_STREAM, 0); ev = socket(AF_UNIX, SOCK_STREAM, 0); memset(&un, 0, sizeof(un)); @@ -7522,7 +7522,7 @@ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; pthread_setspecific(_dw_pool_key, pool); #endif - + threadfunc = (void (*)(void *))tmp[0]; threadfunc(tmp[1]); @@ -7738,7 +7738,7 @@ int dw_exec(char *program, int type, char **params) { int ret = -1; - + if(type == DW_EXEC_GUI) { if(params && params[0] && params[1]) @@ -7752,19 +7752,19 @@ } return 0; } - + if((ret = fork()) == 0) { int i; - + for (i = 3; i < 256; i++) close(i); setsid(); - + if(type == DW_EXEC_CON) { char **tmpargs; - + if(!params) { tmpargs = malloc(sizeof(char *)); @@ -7773,7 +7773,7 @@ else { int z = 0; - + while(params[z]) { z++;