More OS X uiControl work, finally.

This commit is contained in:
Pietro Gagliardi 2015-07-22 23:03:26 -04:00
parent 4234b47f62
commit d5744df476
3 changed files with 70 additions and 146 deletions

View File

@ -1,134 +0,0 @@
// 9 april 2015
#import "uipriv_darwin.h"
@interface entryDelegate : NSObject <NSTextFieldDelegate> {
uiEntry *e;
void (*onChanged)(uiEntry *, void *);
void *onChangedData;
}
- (void)controlTextDidChange:(NSNotification *)note;
- (void)setEntry:(uiEntry *)newe;
- (void)setOnChanged:(void (*)(uiEntry *, void *))f data:(void *)data;
@end
@implementation entryDelegate
- (void)controlTextDidChange:(NSNotification *)note
{
(*(self->onChanged))(self->e, self->onChangedData);
}
- (void)setEntry:(uiEntry *)newe
{
self->e = newe;
}
- (void)setOnChanged:(void (*)(uiEntry *, void *))f data:(void *)data
{
self->onChanged = f;
self->onChangedData = data;
}
@end
struct entry {
uiEntry e;
NSTextField *textfield;
entryDelegate *delegate;
};
static void defaultOnChanged(uiEntry *e, void *data)
{
// do nothing
}
static void destroy(void *data)
{
struct entry *e = (struct entry *) data;
[e->textfield setDelegate:nil];
[e->delegate release];
uiFree(e);
}
static char *entryText(uiEntry *ee)
{
struct entry *e = (struct entry *) ee;
return uiDarwinNSStringToText([e->textfield stringValue]);
}
static void entrySetText(uiEntry *ee, const char *text)
{
struct entry *e = (struct entry *) ee;
[e->textfield setStringValue:toNSString(text)];
}
static void entryOnChanged(uiEntry *ee, void (*f)(uiEntry *, void *), void *data)
{
struct entry *e = (struct entry *) ee;
[e->delegate setOnChanged:f data:data];
}
static int entryReadOnly(uiEntry *ee)
{
struct entry *e = (struct entry *) ee;
return [e->textfield isEditable] == NO;
}
static void entrySetReadOnly(uiEntry *ee, int readonly)
{
struct entry *e = (struct entry *) ee;
BOOL editable;
editable = YES;
if (readonly)
editable = NO;
[e->textfield setEditable:editable];
}
// these are based on interface builder defaults; my comments in the old code weren't very good so I don't really know what talked about what, sorry :/
void finishNewTextField(NSTextField *t, BOOL isEntry)
{
setStandardControlFont(t);
// THE ORDER OF THESE CALLS IS IMPORTANT; CHANGE IT AND THE BORDERS WILL DISAPPEAR
[t setBordered:NO];
[t setBezelStyle:NSTextFieldSquareBezel];
[t setBezeled:isEntry];
// we don't need to worry about substitutions/autocorrect here; see window_darwin.m for details
[[t cell] setLineBreakMode:NSLineBreakByClipping];
[[t cell] setScrollable:YES];
}
uiEntry *uiNewEntry(void)
{
struct entry *e;
e = uiNew(struct entry);
uiDarwinMakeControl(uiControl(e), [NSTextField class], NO, NO, destroy, e);
e->textfield = (NSTextField *) uiControlHandle(uiControl(e));
[e->textfield setSelectable:YES]; // otherwise the setting is masked by the editable default of YES
finishNewTextField(e->textfield, YES);
e->delegate = [entryDelegate new];
[e->textfield setDelegate:e->delegate];
[e->delegate setEntry:uiEntry(e)];
[e->delegate setOnChanged:defaultOnChanged data:NULL];
uiEntry(e)->Text = entryText;
uiEntry(e)->SetText = entrySetText;
uiEntry(e)->OnChanged = entryOnChanged;
uiEntry(e)->ReadOnly = entryReadOnly;
uiEntry(e)->SetReadOnly = entrySetReadOnly;
return uiEntry(e);
}

View File

@ -1,6 +1,8 @@
// 10 june 2015 // 10 june 2015
#include "uipriv_darwin.h" #include "uipriv_darwin.h"
// TODO reimplement CommitDestroy() on all of these
@interface buttonDelegate : NSObject { @interface buttonDelegate : NSObject {
uiButton *b; uiButton *b;
void (*onClicked)(uiButton *, void *); void (*onClicked)(uiButton *, void *);

View File

@ -1,11 +1,40 @@
// 11 june 2015 // 11 june 2015
#include "uipriv_darwin.h" #include "uipriv_darwin.h"
struct entry { @interface entryDelegate : NSObject <NSTextFieldDelegate> {
uiEntry e; uiEntry *e;
OSTYPE *OSHANDLE;
void (*onChanged)(uiEntry *, void *); void (*onChanged)(uiEntry *, void *);
void *onChangedData; void *onChangedData;
}
- (void)controlTextDidChange:(NSNotification *)note;
- (void)setEntry:(uiEntry *)newe;
- (void)setOnChanged:(void (*)(uiEntry *, void *))f data:(void *)data;
@end
@implementation entryDelegate
- (void)controlTextDidChange:(NSNotification *)note
{
(*(self->onChanged))(self->e, self->onChangedData);
}
- (void)setEntry:(uiEntry *)newe
{
self->e = newe;
}
- (void)setOnChanged:(void (*)(uiEntry *, void *))f data:(void *)data
{
self->onChanged = f;
self->onChangedData = data;
}
@end
struct entry {
uiEntry e;
NSTextField *textfield;
entryDelegate *delegate;
}; };
uiDefineControlType(uiEntry, uiTypeEntry, struct entry) uiDefineControlType(uiEntry, uiTypeEntry, struct entry)
@ -14,7 +43,7 @@ static uintptr_t entryHandle(uiControl *c)
{ {
struct entry *e = (struct entry *) c; struct entry *e = (struct entry *) c;
return (uintptr_t) (e->OSHANDLE); return (uintptr_t) (e->textfield);
} }
static void defaultOnChanged(uiEntry *e, void *data) static void defaultOnChanged(uiEntry *e, void *data)
@ -26,14 +55,14 @@ static char *entryText(uiEntry *ee)
{ {
struct entry *e = (struct entry *) ee; struct entry *e = (struct entry *) ee;
return PUT_CODE_HERE; return uiDarwinNSStringToText([e->textfield stringValue]);
} }
static void entrySetText(uiEntry *ee, const char *text) static void entrySetText(uiEntry *ee, const char *text)
{ {
struct entry *e = (struct entry *) ee; struct entry *e = (struct entry *) ee;
PUT_CODE_HERE; [e->textfield setStringValue:toNSString(text)];
// don't queue the control for resize; entry sizes are independent of their contents // don't queue the control for resize; entry sizes are independent of their contents
} }
@ -41,31 +70,58 @@ static void entryOnChanged(uiEntry *ee, void (*f)(uiEntry *, void *), void *data
{ {
struct entry *e = (struct entry *) ee; struct entry *e = (struct entry *) ee;
e->onChanged = f; [e->delegate setOnChanged:f data:data];
e->onChangedData = data;
} }
static int entryReadOnly(uiEntry *ee) static int entryReadOnly(uiEntry *ee)
{ {
struct entry *e = (struct entry *) ee; struct entry *e = (struct entry *) ee;
return PUT_CODE_HERE; return [e->textfield isEditable] == NO;
} }
static void entrySetReadOnly(uiEntry *ee, int readonly) static void entrySetReadOnly(uiEntry *ee, int readonly)
{ {
struct entry *e = (struct entry *) ee; struct entry *e = (struct entry *) ee;
BOOL editable;
PUT_CODE_HERE; editable = YES;
if (readonly)
editable = NO;
[e->textfield setEditable:editable];
}
// these are based on interface builder defaults; my comments in the old code weren't very good so I don't really know what talked about what, sorry :/
void finishNewTextField(uiControl *tt, NSTextField *t, BOOL isEntry)
{
uiDarwinMakeSingleViewControl(tt, t, YES);
// THE ORDER OF THESE CALLS IS IMPORTANT; CHANGE IT AND THE BORDERS WILL DISAPPEAR
[t setBordered:NO];
[t setBezelStyle:NSTextFieldSquareBezel];
[t setBezeled:isEntry];
// we don't need to worry about substitutions/autocorrect here; see window_darwin.m for details
[[t cell] setLineBreakMode:NSLineBreakByClipping];
[[t cell] setScrollable:YES];
} }
uiEntry *uiNewEntry(void) uiEntry *uiNewEntry(void)
{ {
struct entry *e; struct entry *e;
e = (struct entry *) MAKE_CONTROL_INSTANCE(uiTypeEntry()); e = (struct entry *) uiNewControl(uiTypeEntry());
PUT_CODE_HERE; e->textfield = [[NSTextField alloc] initWithFrame:NSZeroRect];
[e->textfield setSelectable:YES]; // otherwise the setting is masked by the editable default of YES
finishNewTextField(uiControl(e), e->textfield, YES);
e->delegate = [entryDelegate new];
[e->textfield setDelegate:e->delegate];
[e->delegate setEntry:uiEntry(e)];
[e->delegate setOnChanged:defaultOnChanged data:NULL];
e->onChanged = defaultOnChanged; e->onChanged = defaultOnChanged;