libui/darwin/entry.m

167 lines
3.5 KiB
Mathematica
Raw Normal View History

2015-08-14 21:50:20 -05:00
// 14 august 2015
#import "uipriv_darwin.h"
2015-08-21 16:16:52 -05:00
// Text fields for entering text have no intrinsic width; we'll use the default Interface Builder width for them.
#define textfieldWidth 96
@interface libui_intrinsicWidthNSTextField : NSTextField
@end
2015-08-21 16:16:52 -05:00
@implementation libui_intrinsicWidthNSTextField
- (NSSize)intrinsicContentSize
{
NSSize s;
s = [super intrinsicContentSize];
s.width = textfieldWidth;
return s;
}
@end
2015-08-14 21:50:20 -05:00
struct uiEntry {
uiDarwinControl c;
NSTextField *textfield;
void (*onChanged)(uiEntry *, void *);
void *onChangedData;
};
@interface entryDelegateClass : NSObject<NSTextFieldDelegate> {
struct mapTable *entries;
2015-08-14 21:50:20 -05:00
}
- (void)controlTextDidChange:(NSNotification *)note;
- (void)registerEntry:(uiEntry *)e;
- (void)unregisterEntry:(uiEntry *)e;
@end
@implementation entryDelegateClass
- (id)init
{
self = [super init];
if (self)
self->entries = newMap();
2015-08-14 21:50:20 -05:00
return self;
}
- (void)dealloc
{
mapDestroy(self->entries);
2015-08-14 21:50:20 -05:00
[super dealloc];
}
- (void)controlTextDidChange:(NSNotification *)note
{
uiEntry *e;
e = (uiEntry *) mapGet(self->entries, [note object]);
2015-08-14 21:50:20 -05:00
(*(e->onChanged))(e, e->onChangedData);
}
- (void)registerEntry:(uiEntry *)e
{
mapSet(self->entries, e->textfield, e);
[e->textfield setDelegate:self];
2015-08-14 21:50:20 -05:00
}
- (void)unregisterEntry:(uiEntry *)e
{
[e->textfield setDelegate:nil];
mapDelete(self->entries, e->textfield);
2015-08-14 21:50:20 -05:00
}
@end
static entryDelegateClass *entryDelegate = nil;
uiDarwinDefineControlWithOnDestroy(
uiEntry, // type name
uiEntryType, // type function
textfield, // handle
[entryDelegate unregisterEntry:this]; // on destroy
)
char *uiEntryText(uiEntry *e)
{
return uiDarwinNSStringToText([e->textfield stringValue]);
}
void uiEntrySetText(uiEntry *e, const char *text)
{
[e->textfield setStringValue:toNSString(text)];
// don't queue the control for resize; entry sizes are independent of their contents
}
void uiEntryOnChanged(uiEntry *e, void (*f)(uiEntry *, void *), void *data)
{
e->onChanged = f;
e->onChangedData = data;
}
int uiEntryReadOnly(uiEntry *e)
{
return [e->textfield isEditable] == NO;
}
void uiEntrySetReadOnly(uiEntry *e, int readonly)
{
BOOL editable;
editable = YES;
if (readonly)
editable = NO;
[e->textfield setEditable:editable];
}
static void defaultOnChanged(uiEntry *e, void *data)
{
// do nothing
}
2015-08-14 21:50:20 -05:00
// 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)
{
uiDarwinSetControlFont(t, NSRegularControlSize);
// 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];
}
NSTextField *newEditableTextField(void)
{
NSTextField *tf;
tf = [[libui_intrinsicWidthNSTextField alloc] initWithFrame:NSZeroRect];
[tf setSelectable:YES]; // otherwise the setting is masked by the editable default of YES
finishNewTextField(tf, YES);
return tf;
}
2015-08-14 21:50:20 -05:00
uiEntry *uiNewEntry(void)
{
uiEntry *e;
e = (uiEntry *) uiNewControl(uiEntryType());
e->textfield = newEditableTextField();
2015-08-14 21:50:20 -05:00
2015-08-17 11:57:45 -05:00
if (entryDelegate == nil) {
2015-08-14 21:50:20 -05:00
entryDelegate = [entryDelegateClass new];
2015-08-17 11:57:45 -05:00
[delegates addObject:entryDelegate];
}
2015-08-14 21:50:20 -05:00
[entryDelegate registerEntry:e];
uiEntryOnChanged(e, defaultOnChanged, NULL);
uiDarwinFinishNewControl(e, uiEntry);
return e;
}