Wrote up cleanup gunk on Mac OS X. Really gunk; I don't like what all this does but I don't think there's another way.

This commit is contained in:
Pietro Gagliardi 2015-04-08 03:38:08 -04:00
parent 5597606f18
commit 4ab6251449
7 changed files with 67 additions and 38 deletions

View File

@ -1,30 +1,19 @@
// 7 april 2015 // 7 april 2015
#import "uipriv_darwin.h" #import "uipriv_darwin.h"
#ifdef uiLogAllocations @interface uiNSButton : NSButton <uiFreeOnDealloc>
@interface loggingNSButton : NSButton @property uiControl *uiC;
@property void (*uiOnClicked)(uiControl *, void *);
@property void *uiOnClickedData;
@property NSMutableArray *uiFreeList;
@end @end
@implementation loggingNSButton @implementation uiNSButton
uiLogObjCClassAllocations uiLogObjCClassAllocations(uiDoFreeOnDealloc(self.uiFreeList);)
uiFreeOnDeallocImpl
@end - (IBAction)uiButtonClicked:(id)sender
#else
#define loggingNSButton NSButton
#endif
@interface button : NSObject
@property uiControl *c;
@property void (*onClicked)(uiControl *, void *);
@property void *onClickedData;
@end
@implementation button
uiLogObjCClassAllocations
- (IBAction)buttonClicked:(id)sender
{ {
(*(self.onClicked))(self.c, self.onClickedData); (*(self.onClicked))(self.c, self.onClickedData);
} }
@ -39,21 +28,21 @@ static void defaultOnClicked(uiControl *c, void *data)
// TODO destruction // TODO destruction
uiControl *uiNewButton(const char *text) uiControl *uiNewButton(const char *text)
{ {
button *b; uiControl *c;
NSButton *bb; uiNSButton *b;
b = [button new]; c = uiDarwinNewControl([uiNSButton class], NO, NO, NULL);
b.c = uiDarwinNewControl([loggingNSButton class], NO, NO, b); b = (uiNSButton *) uiControlHandle(c);
b.c = c;
bb = (NSButton *) uiControlHandle(b.c); [b setTitle:toNSString(text)];
[bb setTitle:toNSString(text)]; [b setButtonType:NSMomentaryPushInButton];
[bb setButtonType:NSMomentaryPushInButton]; [b setBordered:YES];
[bb setBordered:YES]; [b setBezelStyle:NSRoundedBezelStyle];
[bb setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont((NSControl *) bb); setStandardControlFont((NSControl *) bb);
[bb setTarget:b]; [b setTarget:b];
[bb setAction:@selector(buttonClicked:)]; [b setAction:@selector(uiButtonClicked:)];
b.onClicked = defaultOnClicked; b.onClicked = defaultOnClicked;
@ -66,7 +55,7 @@ void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
{ {
button *b; button *b;
b = (button *) uiDarwinControlData(c); b = (uiNSButton *) uiControlHandle(c);
b.onClicked = f; b.onClicked = f;
b.onClickedData = data; b.onClickedData = data;
} }

View File

@ -10,7 +10,10 @@
// thanks to mikeash and JtRip in irc.freenode.net/#macdev // thanks to mikeash and JtRip in irc.freenode.net/#macdev
@implementation uiContainer @implementation uiContainer
uiLogObjCClassAllocations uiLogObjCClassAllocations(
if (self.child != NULL)
uiControlDestroy(self.child);
)
- (void)setFrameSize:(NSSize)s - (void)setFrameSize:(NSSize)s
{ {

View File

@ -6,7 +6,7 @@
@implementation uiApplication @implementation uiApplication
uiLogObjCClassAllocations uiLogObjCClassAllocations()
// hey look! we're overriding terminate:! // hey look! we're overriding terminate:!
// we're going to make sure we can go back to main() whether Cocoa likes it or not! // we're going to make sure we can go back to main() whether Cocoa likes it or not!

View File

@ -13,6 +13,11 @@ struct uiSingleViewControl {
#define S(c) ((uiSingleViewControl *) (c)) #define S(c) ((uiSingleViewControl *) (c))
static void singleDestroy(uiControl *c)
{
[S(c)->view release];
}
static uintptr_t singleHandle(uiControl *c) static uintptr_t singleHandle(uiControl *c)
{ {
return (uintptr_t) (S(c)->view); return (uintptr_t) (S(c)->view);
@ -59,6 +64,7 @@ static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, i
uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void *data) uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void *data)
{ {
uiSingleViewControl *c; uiSingleViewControl *c;
uiFreeOnDealloc *freer;
c = uiNew(uiSingleViewControl); c = uiNew(uiSingleViewControl);
// thanks to autoxr and arwyn in irc.freenode.net/#macdev // thanks to autoxr and arwyn in irc.freenode.net/#macdev
@ -79,6 +85,7 @@ uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHas
c->immediate = (NSView *) (c->scrollView); c->immediate = (NSView *) (c->scrollView);
} }
c->control.destroy = singleDestroy;
c->control.handle = singleHandle; c->control.handle = singleHandle;
c->control.setParent = singleSetParent; c->control.setParent = singleSetParent;
c->control.preferredSize = singlePreferredSize; c->control.preferredSize = singlePreferredSize;
@ -86,6 +93,9 @@ uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHas
c->data = data; c->data = data;
freer = (uiFreeOnDealloc *) (c->view);
[freer uiFreeOnDealloc:c];
return (uiControl *) c; return (uiControl *) c;
} }

View File

@ -14,7 +14,7 @@ struct uiSizing {
// TODO see if we can override alloc instead // TODO see if we can override alloc instead
#ifdef uiLogAllocations #ifdef uiLogAllocations
#import <stdio.h> #import <stdio.h>
#define uiLogObjCClassAllocations \ #define uiLogObjCClassAllocations(deallocCode) \
+ (id)alloc \ + (id)alloc \
{ \ { \
id thing; \ id thing; \
@ -24,15 +24,32 @@ struct uiSizing {
} \ } \
- (void)dealloc \ - (void)dealloc \
{ \ { \
deallocCode \
[super dealloc]; \ [super dealloc]; \
fprintf(stderr, "%p free\n", self); \ fprintf(stderr, "%p free\n", self); \
} }
#else #else
#define uiLogObjCClassAllocations #define uiLogObjCClassAllocations(deallocCode) \
- (void)dealloc \
{ \
deallocCode \
[super dealloc]; \
}
#endif #endif
// util_darwin.m // util_darwin.m
extern void setStandardControlFont(NSControl *); extern void setStandardControlFont(NSControl *);
@protocol uiFreeOnDealloc
- (void)uiFreeOnDealloc:(void *)p;
@end
#define uiFreeOnDeallocImpl \
- (void)uiFreeOnDealloc:(void *)p \
{ \
if (self.uiFreeList == nil) \
self.uiFreeList = [NSMutableArray new]; \
[self.uiFreeList addObject:[NSValue valueWIthPointer:p]]; \
}
extern void uiDoFreeOnDealloc(NSMutableArray *);
// container_darwin.m // container_darwin.m
@interface uiContainer : NSView @interface uiContainer : NSView

View File

@ -6,3 +6,13 @@ void setStandardControlFont(NSControl *control)
{ {
[control setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]]; [control setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
} }
void uiDoFreeOnDealloc(NSMutableArray *m)
{
[m enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
NSValue *v = (NSValue *) obj;
uiFree([v pointerValue]);
}];
[m release];
}

View File

@ -10,7 +10,7 @@
@implementation loggingNSWindow @implementation loggingNSWindow
uiLogObjCClassAllocations uiLogObjCClassAllocations()
@end @end
#else #else
@ -25,7 +25,7 @@ uiLogObjCClassAllocations
@implementation uiWindowDelegate @implementation uiWindowDelegate
uiLogObjCClassAllocations uiLogObjCClassAllocations()
- (BOOL)windowShouldClose:(id)win - (BOOL)windowShouldClose:(id)win
{ {