From 89f8bd8643ee2e66c1df2982e9e7e43a74d8dc98 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 11 Jul 2015 20:28:04 -0400 Subject: [PATCH] Migrated OS X uiButton. We're off to a meh start... --- darwin/button.m | 61 ------------------------------------------- redo/darwin/button.m | 61 ++++++++++++++++++++++++++++++++++--------- redo/darwin/control.m | 8 +++++- 3 files changed, 55 insertions(+), 75 deletions(-) diff --git a/darwin/button.m b/darwin/button.m index 3b07093e..f22e82cf 100644 --- a/darwin/button.m +++ b/darwin/button.m @@ -1,35 +1,6 @@ // 7 april 2015 #import "uipriv_darwin.h" -@interface buttonDelegate : NSObject { - uiButton *b; - void (*onClicked)(uiButton *, void *); - void *onClickedData; -} -- (IBAction)buttonClicked:(id)sender; -- (void)setButton:(uiButton *)newb; -- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data; -@end - -@implementation buttonDelegate - -- (IBAction)buttonClicked:(id)sender -{ - (*(self->onClicked))(self->b, self->onClickedData); -} - -- (void)setButton:(uiButton *)newb -{ - self->b = newb; -} - -- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data -{ - self->onClicked = f; - self->onClickedData = data; -} - -@end struct button { uiButton b; @@ -51,26 +22,6 @@ static void destroy(void *data) uiFree(b); } -static char *buttonText(uiButton *bb) -{ - struct button *b = (struct button *) bb; - - return uiDarwinNSStringToText([b->button title]); -} - -static void buttonSetText(uiButton *bb, const char *text) -{ - struct button *b = (struct button *) bb; - - [b->button setTitle:toNSString(text)]; -} - -static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data) -{ - struct button *b = (struct button *) bb; - - [b->delegate setOnClicked:f data:data]; -} uiButton *uiNewButton(const char *text) { @@ -82,18 +33,6 @@ uiButton *uiNewButton(const char *text) b->button = (NSButton *) uiControlHandle(uiControl(b)); - [b->button setTitle:toNSString(text)]; - [b->button setButtonType:NSMomentaryPushInButton]; - [b->button setBordered:YES]; - [b->button setBezelStyle:NSRoundedBezelStyle]; - setStandardControlFont(b->button); - - b->delegate = [buttonDelegate new]; - [b->button setTarget:b->delegate]; - [b->button setAction:@selector(buttonClicked:)]; - [b->delegate setButton:uiButton(b)]; - [b->delegate setOnClicked:defaultOnClicked data:NULL]; - uiButton(b)->Text = buttonText; uiButton(b)->SetText = buttonSetText; uiButton(b)->OnClicked = buttonOnClicked; diff --git a/redo/darwin/button.m b/redo/darwin/button.m index 1bdadd43..3675de10 100644 --- a/redo/darwin/button.m +++ b/redo/darwin/button.m @@ -1,11 +1,40 @@ // 10 june 2015 #include "uipriv_darwin.h" -struct button { - uiButton b; - OSTYPE *OSHANDLE; +@interface buttonDelegate : NSObject { + uiButton *b; void (*onClicked)(uiButton *, void *); void *onClickedData; +} +- (IBAction)buttonClicked:(id)sender; +- (void)setButton:(uiButton *)newb; +- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data; +@end + +@implementation buttonDelegate + +- (IBAction)buttonClicked:(id)sender +{ + (*(self->onClicked))(self->b, self->onClickedData); +} + +- (void)setButton:(uiButton *)newb +{ + self->b = newb; +} + +- (void)setOnClicked:(void (*)(uiButton *, void *))f data:(void *)data +{ + self->onClicked = f; + self->onClickedData = data; +} + +@end + +struct button { + uiButton b; + NSButton *button; + buttonDelegate *delegate; }; uiDefineControlType(uiButton, uiTypeButton, struct button) @@ -14,7 +43,7 @@ static uintptr_t buttonHandle(uiControl *c) { struct button *b = (struct button *) c; - return (uintptr_t) (b->OSHANDLE); + return (uintptr_t) (b->button); } static void defaultOnClicked(uiButton *b, void *data) @@ -26,35 +55,41 @@ static char *buttonText(uiButton *bb) { struct button *b = (struct button *) bb; - return PUT_CODE_HERE; + return uiDarwinNSStringToText([b->button title]); } static void buttonSetText(uiButton *bb, const char *text) { struct button *b = (struct button *) bb; - PUT_CODE_HERE; - // changing the text might necessitate a change in the button's size - uiControlQueueResize(uiControl(b)); + [b->button setTitle:toNSString(text)]; } static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data) { struct button *b = (struct button *) bb; - b->onClicked = f; - b->onClickedData = data; + [b->delegate setOnClicked:f data:data]; } uiButton *uiNewButton(const char *text) { struct button *b; - b = (struct button *) MAKE_CONTROL_INSTANCE(uiTypeButton()); + b = (struct button *) uiNewControl(uiTypeButton()); - PUT_CODE_HERE; + b->button = [[NSButton alloc] initWithFrame:NSZeroFrame]; + [b->button setTitle:toNSString(text)]; + [b->button setButtonType:NSMomentaryPushInButton]; + [b->button setBordered:YES]; + [b->button setBezelStyle:NSRoundedBezelStyle]; + uiDarwinMakeSingleViewControl(uiControl(b), b->button, YES); - b->onClicked = defaultOnClicked; + b->delegate = [buttonDelegate new]; + [b->button setTarget:b->delegate]; + [b->button setAction:@selector(buttonClicked:)]; + [b->delegate setButton:uiButton(b)]; + [b->delegate setOnClicked:defaultOnClicked data:NULL]; uiControl(b)->Handle = buttonHandle; diff --git a/redo/darwin/control.m b/redo/darwin/control.m index b4f87e8c..9c0087d3 100644 --- a/redo/darwin/control.m +++ b/redo/darwin/control.m @@ -5,6 +5,9 @@ static void singleViewCommitDestroy(uiControl *c) { + // TODO make this unnecessary? + if ([VIEW(c) respondsToSelector:@selector(delegate)]) + [[VIEW(c) delegate] release]; [VIEW(c) release]; } @@ -102,11 +105,14 @@ static int singleViewHasTabStops(uiControl *c) } // called after creating the control's NSView -void uiDarwinMakeSingleViewControl(uiControl *c, NSView *view) +void uiDarwinMakeSingleViewControl(uiControl *c, NSView *view, BOOL useStandardControlFont) { // we have to retain the view so we can reparent it [view retain]; + if (useStandardControlFont) + setStandardControlFont((NSControl *) view); + uiControl(c)->CommitDestroy = singleViewCommitDestroy; uiControl(c)->CommitSetParent = singleViewCommitSetParent; uiControl(c)->PreferredSize = singleViewPreferredSize;