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
|
// 7 april 2015
|
||||||
#import "uipriv_darwin.h"
|
#import "uipriv_darwin.h"
|
||||||
|
|
||||||
@interface uiNSButton : NSButton <uiFreeOnDealloc>
|
@interface uiNSButton : NSButton
|
||||||
@property uiControl *uiC;
|
@property uiControl *uiC;
|
||||||
@property void (*uiOnClicked)(uiControl *, void *);
|
@property void (*uiOnClicked)(uiControl *, void *);
|
||||||
@property void *uiOnClickedData;
|
@property void *uiOnClickedData;
|
||||||
|
@ -10,12 +10,15 @@
|
||||||
|
|
||||||
@implementation uiNSButton
|
@implementation uiNSButton
|
||||||
|
|
||||||
uiLogObjCClassAllocations(uiDoFreeOnDealloc(self.uiFreeList);)
|
- (void)dealloc
|
||||||
uiFreeOnDeallocImpl
|
{
|
||||||
|
uiDarwinControlFree(self.uiC);
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
- (IBAction)uiButtonClicked:(id)sender
|
- (IBAction)uiButtonClicked:(id)sender
|
||||||
{
|
{
|
||||||
(*(self.onClicked))(self.c, self.onClickedData);
|
(*(self.uiOnClicked))(self.uiC, self.uiOnClickedData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -33,7 +36,7 @@ uiControl *uiNewButton(const char *text)
|
||||||
|
|
||||||
c = uiDarwinNewControl([uiNSButton class], NO, NO, NULL);
|
c = uiDarwinNewControl([uiNSButton class], NO, NO, NULL);
|
||||||
b = (uiNSButton *) uiControlHandle(c);
|
b = (uiNSButton *) uiControlHandle(c);
|
||||||
b.c = c;
|
b.uiC = c;
|
||||||
|
|
||||||
[b setTitle:toNSString(text)];
|
[b setTitle:toNSString(text)];
|
||||||
[b setButtonType:NSMomentaryPushInButton];
|
[b setButtonType:NSMomentaryPushInButton];
|
||||||
|
@ -44,7 +47,7 @@ uiControl *uiNewButton(const char *text)
|
||||||
[b setTarget:b];
|
[b setTarget:b];
|
||||||
[b setAction:@selector(uiButtonClicked:)];
|
[b setAction:@selector(uiButtonClicked:)];
|
||||||
|
|
||||||
b.onClicked = defaultOnClicked;
|
b.uiOnClicked = defaultOnClicked;
|
||||||
|
|
||||||
return b.c;
|
return b.c;
|
||||||
}
|
}
|
||||||
|
@ -56,6 +59,6 @@ void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
|
||||||
button *b;
|
button *b;
|
||||||
|
|
||||||
b = (uiNSButton *) uiControlHandle(c);
|
b = (uiNSButton *) uiControlHandle(c);
|
||||||
b.onClicked = f;
|
b.uiOnClicked = f;
|
||||||
b.onClickedData = data;
|
b.uiOnClickedData = data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
|
|
||||||
@implementation uiApplication
|
@implementation uiApplication
|
||||||
|
|
||||||
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!
|
||||||
// and just how are we going to do that, hm?
|
// and just how are we going to do that, hm?
|
||||||
|
|
|
@ -8,7 +8,6 @@ struct uiSingleViewControl {
|
||||||
NSView *view;
|
NSView *view;
|
||||||
NSScrollView *scrollView;
|
NSScrollView *scrollView;
|
||||||
NSView *immediate; // the control that is added to the parent container; either view or scrollView
|
NSView *immediate; // the control that is added to the parent container; either view or scrollView
|
||||||
void *data;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define S(c) ((uiSingleViewControl *) (c))
|
#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];
|
[S(c)->immediate setFrame:frame];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO connect free function
|
uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder)
|
||||||
|
|
||||||
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
|
||||||
|
@ -91,15 +87,10 @@ uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHas
|
||||||
c->control.preferredSize = singlePreferredSize;
|
c->control.preferredSize = singlePreferredSize;
|
||||||
c->control.resize = singleResize;
|
c->control.resize = singleResize;
|
||||||
|
|
||||||
c->data = data;
|
|
||||||
|
|
||||||
freer = (uiFreeOnDealloc *) (c->view);
|
|
||||||
[freer uiFreeOnDealloc:c];
|
|
||||||
|
|
||||||
return (uiControl *) 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.
|
// uiDarwinNewControl() creates a new uiControl with the given Cocoa control inside.
|
||||||
// The first parameter should come from [RealControlType class].
|
// The first parameter should come from [RealControlType class].
|
||||||
// The two scrollView parameters allow placing scrollbars on the new control.
|
// The two scrollView parameters allow placing scrollbars on the new control.
|
||||||
// The data parameter can be accessed with uiDarwinControlData().
|
// Your control must call uiDarwinControlFree() on the returned uiControl in its -[dealloc] method.
|
||||||
extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void *data);
|
extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder);
|
||||||
extern void *uiDarwinControlData(uiControl *c);
|
extern void uiDarwinControlFree(uiControl *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -39,17 +39,6 @@ struct uiSizing {
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -6,13 +6,3 @@ 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];
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue