Migrated darwin/button.m. Yeah, this will do.

This commit is contained in:
Pietro Gagliardi 2015-04-17 18:20:14 -04:00
parent 3f2a0f8dfa
commit 2fa8bfd95b
2 changed files with 50 additions and 37 deletions

View File

@ -1,86 +1,97 @@
// 7 april 2015 // 7 april 2015
#import "uipriv_darwin.h" #import "uipriv_darwin.h"
@interface uiNSButton : NSButton @interface uipButtonDelegate : NSObject {
@property uiButton *uiB; uiButton *b;
@property void (*uiOnClicked)(uiButton *, void *); void (*onClicked)(uiButton *, void *);
@property void *uiOnClickedData; void *onClickedData;
}
- (IBAction)buttonClicked:(id)sender;
- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data;
@end @end
@implementation uiNSButton @implementation uipButtonDelegate
- (void)viewDidMoveToSuperview uiLogObjCClassAllocations
- (IBAction)buttonClicked:(id)sender
{ {
if (uiDarwinControlFreeWhenAppropriate(uiControl(self.uiB), [self superview])) { (*(self->onClicked))(self->b, self->onClickedData);
[self setTarget:nil];
self.uiB = NULL;
}
[super viewDidMoveToSuperview];
} }
- (IBAction)uiButtonClicked:(id)sender - (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data
{ {
(*(self.uiOnClicked))(self.uiB, self.uiOnClickedData); self->onClicked = f;
self->onClickedData = data;
} }
@end @end
struct button {
uiButton b;
NSButton *button;
uipButtonDelegate *delegate;
};
static void defaultOnClicked(uiButton *c, void *data) static void defaultOnClicked(uiButton *c, void *data)
{ {
// do nothing // do nothing
} }
static void destroy(void *data)
{
struct button *b = (struct button *) bb;
[b->button setTarget:nil];
[b->delegate release];
uiFree(b);
}
static char *buttonText(uiButton *bb) static char *buttonText(uiButton *bb)
{ {
uiNSButton *b; struct button *b = (struct button *) bb;
b = (uiNSButton *) uiControlHandle(uiControl(bb)); return uiDarwinNSStringToText([b->button title]);
return uiDarwinNSStringToText([b title]);
} }
static void buttonSetText(uiButton *bb, const char *text) static void buttonSetText(uiButton *bb, const char *text)
{ {
uiNSButton *b; struct button *b = (struct button *) bb;
b = (uiNSButton *) uiControlHandle(uiControl(bb)); [b->button setTitle:toNSString(text)];
[b setTitle:toNSString(text)];
} }
static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data) static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
{ {
uiNSButton *b; struct button *b = (struct button *) bb;
b = (uiNSButton *) uiControlHandle(uiControl(bb)); [b->delegate setOnClicked:f data:data];
b.uiOnClicked = f;
b.uiOnClickedData = data;
} }
uiButton *uiNewButton(const char *text) uiButton *uiNewButton(const char *text)
{ {
uiButton *b; uiButton *b;
uiNSButton *bb;
b = uiNew(uiButton); b = uiNew(uiButton);
uiDarwinNewControl(uiControl(b), [uiNSButton class], NO, NO); uiDarwinNewControl(uiControl(b), [uiNSButton class], NO, NO, destroy, b);
bb = (uiNSButton *) uiControlHandle(uiControl(b));
[bb setTitle:toNSString(text)]; b->button = (NSButton *) VIEW(b);
[bb setButtonType:NSMomentaryPushInButton];
[bb setBordered:YES];
[bb setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont((NSControl *) bb);
[bb setTarget:bb]; [b->button setTitle:toNSString(text)];
[bb setAction:@selector(uiButtonClicked:)]; [b->button setButtonType:NSMomentaryPushInButton];
[b->button setBordered:YES];
[b->button setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont(b->button);
bb.uiOnClicked = defaultOnClicked; b->delegate = [uipButtonDelegate new];
[b->button setTarget:b->delegate];
[b->button setAction:@selector(buttonClicked:)];
[b->delegate setOnClicked:defaultOnClicked data:NULL];
uiButton(b)->Text = buttonText; uiButton(b)->Text = buttonText;
uiButton(b)->SetText = buttonSetText; uiButton(b)->SetText = buttonSetText;
uiButton(b)->OnClicked = buttonOnClicked; uiButton(b)->OnClicked = buttonOnClicked;
bb.uiB = b; return uiButton(b);
return bb.uiB;
} }

View File

@ -24,6 +24,8 @@
fprintf(stderr, "%p free\n", self); \ fprintf(stderr, "%p free\n", self); \
} }
#define VIEW(c) uiControlHandle(uiControl(c))
// init_darwin.m // init_darwin.m
extern NSView *destroyedControlsView; extern NSView *destroyedControlsView;