Cleaned out some of that gunk. I can't avoid subclassing the standard controls, but making it so that the uiControl returned by uiDarwinNewControl() can be freed by the caller will help.
This commit is contained in:
parent
4ab6251449
commit
941825e190
|
@ -1,7 +1,7 @@
|
|||
// 7 april 2015
|
||||
#import "uipriv_darwin.h"
|
||||
|
||||
@interface uiNSButton : NSButton <uiFreeOnDealloc>
|
||||
@interface uiNSButton : NSButton
|
||||
@property uiControl *uiC;
|
||||
@property void (*uiOnClicked)(uiControl *, void *);
|
||||
@property void *uiOnClickedData;
|
||||
|
@ -10,12 +10,15 @@
|
|||
|
||||
@implementation uiNSButton
|
||||
|
||||
uiLogObjCClassAllocations(uiDoFreeOnDealloc(self.uiFreeList);)
|
||||
uiFreeOnDeallocImpl
|
||||
- (void)dealloc
|
||||
{
|
||||
uiDarwinControlFree(self.uiC);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (IBAction)uiButtonClicked:(id)sender
|
||||
{
|
||||
(*(self.onClicked))(self.c, self.onClickedData);
|
||||
(*(self.uiOnClicked))(self.uiC, self.uiOnClickedData);
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -33,7 +36,7 @@ uiControl *uiNewButton(const char *text)
|
|||
|
||||
c = uiDarwinNewControl([uiNSButton class], NO, NO, NULL);
|
||||
b = (uiNSButton *) uiControlHandle(c);
|
||||
b.c = c;
|
||||
b.uiC = c;
|
||||
|
||||
[b setTitle:toNSString(text)];
|
||||
[b setButtonType:NSMomentaryPushInButton];
|
||||
|
@ -44,7 +47,7 @@ uiControl *uiNewButton(const char *text)
|
|||
[b setTarget:b];
|
||||
[b setAction:@selector(uiButtonClicked:)];
|
||||
|
||||
b.onClicked = defaultOnClicked;
|
||||
b.uiOnClicked = defaultOnClicked;
|
||||
|
||||
return b.c;
|
||||
}
|
||||
|
@ -56,6 +59,6 @@ void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
|
|||
button *b;
|
||||
|
||||
b = (uiNSButton *) uiControlHandle(c);
|
||||
b.onClicked = f;
|
||||
b.onClickedData = data;
|
||||
b.uiOnClicked = f;
|
||||
b.uiOnClickedData = data;
|
||||
}
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
|
||||
@implementation uiApplication
|
||||
|
||||
uiLogObjCClassAllocations()
|
||||
|
||||
// hey look! we're overriding terminate:!
|
||||
// we're going to make sure we can go back to main() whether Cocoa likes it or not!
|
||||
// and just how are we going to do that, hm?
|
||||
|
|
|
@ -8,7 +8,6 @@ struct uiSingleViewControl {
|
|||
NSView *view;
|
||||
NSScrollView *scrollView;
|
||||
NSView *immediate; // the control that is added to the parent container; either view or scrollView
|
||||
void *data;
|
||||
};
|
||||
|
||||
#define S(c) ((uiSingleViewControl *) (c))
|
||||
|
@ -59,12 +58,9 @@ static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, i
|
|||
[S(c)->immediate setFrame:frame];
|
||||
}
|
||||
|
||||
// TODO connect free function
|
||||
|
||||
uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void *data)
|
||||
uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder)
|
||||
{
|
||||
uiSingleViewControl *c;
|
||||
uiFreeOnDealloc *freer;
|
||||
|
||||
c = uiNew(uiSingleViewControl);
|
||||
// thanks to autoxr and arwyn in irc.freenode.net/#macdev
|
||||
|
@ -91,15 +87,10 @@ uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHas
|
|||
c->control.preferredSize = singlePreferredSize;
|
||||
c->control.resize = singleResize;
|
||||
|
||||
c->data = data;
|
||||
|
||||
freer = (uiFreeOnDealloc *) (c->view);
|
||||
[freer uiFreeOnDealloc:c];
|
||||
|
||||
return (uiControl *) c;
|
||||
}
|
||||
|
||||
void *uiDarwinControlData(uiControl *c)
|
||||
void uiDarwinControlFree(uiControl *c)
|
||||
{
|
||||
return S(c)->data;
|
||||
uiFree(c);
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ This file assumes that you have imported <Cocoa/Cocoa.h> and "ui.h" beforehand.
|
|||
// uiDarwinNewControl() creates a new uiControl with the given Cocoa control inside.
|
||||
// The first parameter should come from [RealControlType class].
|
||||
// The two scrollView parameters allow placing scrollbars on the new control.
|
||||
// The data parameter can be accessed with uiDarwinControlData().
|
||||
extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void *data);
|
||||
extern void *uiDarwinControlData(uiControl *c);
|
||||
// Your control must call uiDarwinControlFree() on the returned uiControl in its -[dealloc] method.
|
||||
extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder);
|
||||
extern void uiDarwinControlFree(uiControl *);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -39,17 +39,6 @@ struct uiSizing {
|
|||
|
||||
// util_darwin.m
|
||||
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
|
||||
@interface uiContainer : NSView
|
||||
|
|
|
@ -6,13 +6,3 @@ void setStandardControlFont(NSControl *control)
|
|||
{
|
||||
[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];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue