libui/redo/darwin/entry.m

136 lines
3.2 KiB
Mathematica
Raw Normal View History

// 11 june 2015
2015-07-11 16:02:01 -05:00
#include "uipriv_darwin.h"
2015-07-22 22:03:26 -05:00
@interface entryDelegate : NSObject <NSTextFieldDelegate> {
uiEntry *e;
void (*onChanged)(uiEntry *, void *);
void *onChangedData;
2015-07-22 22:03:26 -05:00
}
- (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)
static uintptr_t entryHandle(uiControl *c)
{
struct entry *e = (struct entry *) c;
2015-07-22 22:03:26 -05:00
return (uintptr_t) (e->textfield);
}
static void defaultOnChanged(uiEntry *e, void *data)
{
// do nothing
}
static char *entryText(uiEntry *ee)
{
struct entry *e = (struct entry *) ee;
2015-07-22 22:03:26 -05:00
return uiDarwinNSStringToText([e->textfield stringValue]);
}
static void entrySetText(uiEntry *ee, const char *text)
{
struct entry *e = (struct entry *) ee;
2015-07-22 22:03:26 -05:00
[e->textfield setStringValue:toNSString(text)];
// don't queue the control for resize; entry sizes are independent of their contents
}
static void entryOnChanged(uiEntry *ee, void (*f)(uiEntry *, void *), void *data)
{
struct entry *e = (struct entry *) ee;
2015-07-22 22:03:26 -05:00
[e->delegate setOnChanged:f data:data];
}
static int entryReadOnly(uiEntry *ee)
{
struct entry *e = (struct entry *) ee;
2015-07-22 22:03:26 -05:00
return [e->textfield isEditable] == NO;
}
static void entrySetReadOnly(uiEntry *ee, int readonly)
{
struct entry *e = (struct entry *) ee;
2015-07-22 22:03:26 -05:00
BOOL editable;
editable = YES;
if (readonly)
editable = NO;
[e->textfield setEditable:editable];
}
2015-07-22 22:03:26 -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(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)
{
struct entry *e;
2015-07-22 22:03:26 -05:00
e = (struct entry *) uiNewControl(uiTypeEntry());
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);
2015-07-22 22:03:26 -05:00
e->delegate = [entryDelegate new];
[e->textfield setDelegate:e->delegate];
[e->delegate setEntry:uiEntry(e)];
[e->delegate setOnChanged:defaultOnChanged data:NULL];
uiControl(e)->Handle = entryHandle;
uiEntry(e)->Text = entryText;
uiEntry(e)->SetText = entrySetText;
uiEntry(e)->OnChanged = entryOnChanged;
uiEntry(e)->ReadOnly = entryReadOnly;
uiEntry(e)->SetReadOnly = entrySetReadOnly;
return uiEntry(e);
}