More OS X uiControl work.

This commit is contained in:
Pietro Gagliardi 2015-07-11 22:56:43 -04:00
parent 89f8bd8643
commit 4234b47f62
3 changed files with 54 additions and 177 deletions

View File

@ -1,41 +0,0 @@
// 7 april 2015
#import "uipriv_darwin.h"
struct button {
uiButton b;
NSButton *button;
buttonDelegate *delegate;
};
static void defaultOnClicked(uiButton *b, void *data)
{
// do nothing
}
static void destroy(void *data)
{
struct button *b = (struct button *) data;
[b->button setTarget:nil];
[b->delegate release];
uiFree(b);
}
uiButton *uiNewButton(const char *text)
{
struct button *b;
b = uiNew(struct button);
uiDarwinMakeControl(uiControl(b), [NSButton class], NO, NO, destroy, b);
b->button = (NSButton *) uiControlHandle(uiControl(b));
uiButton(b)->Text = buttonText;
uiButton(b)->SetText = buttonSetText;
uiButton(b)->OnClicked = buttonOnClicked;
return uiButton(b);
}

View File

@ -1,121 +0,0 @@
// 7 april 2015
#import "uipriv_darwin.h"
@interface checkboxDelegate : NSObject {
uiCheckbox *c;
void (*onToggled)(uiCheckbox *, void *);
void *onToggledData;
}
- (IBAction)checkboxToggled:(id)sender;
- (void)setCheckbox:(uiCheckbox *)newc;
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data;
@end
@implementation checkboxDelegate
- (IBAction)checkboxToggled:(id)sender
{
(*(self->onToggled))(self->c, self->onToggledData);
}
- (void)setCheckbox:(uiCheckbox *)newc
{
self->c = newc;
}
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data
{
self->onToggled = f;
self->onToggledData = data;
}
@end
struct checkbox {
uiCheckbox c;
NSButton *checkbox;
checkboxDelegate *delegate;
};
static void defaultOnToggled(uiCheckbox *c, void *data)
{
// do nothing
}
static void destroy(void *data)
{
struct checkbox *c = (struct checkbox *) data;
[c->checkbox setTarget:nil];
[c->delegate release];
uiFree(c);
}
static char *checkboxText(uiCheckbox *cc)
{
struct checkbox *c = (struct checkbox *) cc;
return uiDarwinNSStringToText([c->checkbox title]);
}
static void checkboxSetText(uiCheckbox *cc, const char *text)
{
struct checkbox *c = (struct checkbox *) cc;
[c->checkbox setTitle:toNSString(text)];
}
static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
{
struct checkbox *c = (struct checkbox *) cc;
[c->delegate setOnToggled:f data:data];
}
static int checkboxChecked(uiCheckbox *cc)
{
struct checkbox *c = (struct checkbox *) cc;
return [c->checkbox state] == NSOnState;
}
static void checkboxSetChecked(uiCheckbox *cc, int checked)
{
struct checkbox *c = (struct checkbox *) cc;
NSInteger state;
state = NSOnState;
if (!checked)
state = NSOffState;
[c->checkbox setState:state];
}
uiCheckbox *uiNewCheckbox(const char *text)
{
struct checkbox *c;
c = uiNew(struct checkbox);
uiDarwinMakeControl(uiControl(c), [NSButton class], NO, NO, destroy, c);
c->checkbox = (NSButton *) uiControlHandle(uiControl(c));
[c->checkbox setTitle:toNSString(text)];
[c->checkbox setButtonType:NSSwitchButton];
[c->checkbox setBordered:NO];
setStandardControlFont(c->checkbox);
c->delegate = [checkboxDelegate new];
[c->checkbox setTarget:c->delegate];
[c->checkbox setAction:@selector(checkboxToggled:)];
[c->delegate setCheckbox:uiCheckbox(c)];
[c->delegate setOnToggled:defaultOnToggled data:NULL];
uiCheckbox(c)->Text = checkboxText;
uiCheckbox(c)->SetText = checkboxSetText;
uiCheckbox(c)->OnToggled = checkboxOnToggled;
uiCheckbox(c)->Checked = checkboxChecked;
uiCheckbox(c)->SetChecked = checkboxSetChecked;
return uiCheckbox(c);
}

View File

@ -1,11 +1,40 @@
// 10 june 2015
#include "uipriv_darwin.h"
struct checkbox {
uiCheckbox c;
OSTYPE *OSHANDLE;
@interface checkboxDelegate : NSObject {
uiCheckbox *c;
void (*onToggled)(uiCheckbox *, void *);
void *onToggledData;
}
- (IBAction)checkboxToggled:(id)sender;
- (void)setCheckbox:(uiCheckbox *)newc;
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data;
@end
@implementation checkboxDelegate
- (IBAction)checkboxToggled:(id)sender
{
(*(self->onToggled))(self->c, self->onToggledData);
}
- (void)setCheckbox:(uiCheckbox *)newc
{
self->c = newc;
}
- (void)setOnToggled:(void (*)(uiCheckbox *, void *))f data:(void *)data
{
self->onToggled = f;
self->onToggledData = data;
}
@end
struct checkbox {
uiCheckbox c;
NSButton *checkbox;
checkboxDelegate *delegate;
};
uiDefineControlType(uiCheckbox, uiTypeCheckbox, struct checkbox)
@ -14,7 +43,7 @@ static uintptr_t checkboxHandle(uiControl *cc)
{
struct checkbox *c = (struct checkbox *) cc;
return (uintptr_t) (c->OSHANDLE);
return (uintptr_t) (c->checkbox);
}
static void defaultOnToggled(uiCheckbox *c, void *data)
@ -26,49 +55,59 @@ static char *checkboxText(uiCheckbox *cc)
{
struct checkbox *c = (struct checkbox *) cc;
return PUT_CODE_HERE;
return uiDarwinNSStringToText([c->checkbox title]);
}
static void checkboxSetText(uiCheckbox *cc, const char *text)
{
struct checkbox *c = (struct checkbox *) cc;
PUT_CODE_HERE;
// changing the text might necessitate a change in the checkbox's size
uiControlQueueResize(uiControl(c));
[c->checkbox setTitle:toNSString(text)];
}
static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
{
struct checkbox *c = (struct checkbox *) cc;
c->onToggled = f;
c->onToggledData = data;
[c->delegate setOnToggled:f data:data];
}
static int checkboxChecked(uiCheckbox *cc)
{
struct checkbox *c = (struct checkbox *) cc;
return PUT_CODE_HERE;
return [c->checkbox state] == NSOnState;
}
static void checkboxSetChecked(uiCheckbox *cc, int checked)
{
struct checkbox *c = (struct checkbox *) cc;
NSInteger state;
PUT_CODE_HERE;
state = NSOnState;
if (!checked)
state = NSOffState;
[c->checkbox setState:state];
}
uiCheckbox *uiNewCheckbox(const char *text)
{
struct checkbox *c;
c = (struct checkbox *) MAKE_CONTROL_INSTANCE(uiTypeCheckbox());
c = (struct checkbox *) uiNewControl(uiTypeCheckbox());
PUT_CODE_HERE;
// TODO make a macro for the below
c->checkbox = [[NSCheckbox alloc] initWithFrame:NSZeroRect];
[c->checkbox setTitle:toNSString(text)];
[c->checkbox setButtonType:NSSwitchButton];
[c->checkbox setBordered:NO];
uiDarwinMakeSingleViewControl(uiControl(c), c->checkbox, YES);
c->onToggled = defaultOnToggled;
c->delegate = [checkboxDelegate new];
[c->checkbox setTarget:c->delegate];
[c->checkbox setAction:@selector(checkboxToggled:)];
[c->delegate setCheckbox:uiCheckbox(c)];
[c->delegate setOnToggled:defaultOnToggled data:NULL];
uiControl(c)->Handle = checkboxHandle;