Implement uiDateTimePickerTime() for OS X

This commit is contained in:
cody271 2018-02-21 21:47:45 -08:00
parent 3e9bdd26f1
commit 04ce39a941
1 changed files with 101 additions and 0 deletions

View File

@ -4,10 +4,82 @@
struct uiDateTimePicker { struct uiDateTimePicker {
uiDarwinControl c; uiDarwinControl c;
NSDatePicker *dp; NSDatePicker *dp;
void (*onChanged)(uiDateTimePicker *, void *);
void *onChangedData;
}; };
@interface datePickerDelegateClass : NSObject <NSDatePickerCellDelegate> {
struct mapTable *pickers;
}
- (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell
validateProposedDateValue:(NSDate **)proposedDateValue
timeInterval:(NSTimeInterval *)proposedTimeInterval;
- (void)doTimer:(NSTimer *)timer;
- (void)registerPicker:(uiDateTimePicker *)b;
- (void)unregisterPicker:(uiDateTimePicker *)b;
@end
@implementation datePickerDelegateClass
- (id)init
{
self = [super init];
if (self)
self->pickers = newMap();
return self;
}
- (void)dealloc
{
mapDestroy(self->pickers);
[super dealloc];
}
- (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell
validateProposedDateValue:(NSDate **)proposedDateValue
timeInterval:(NSTimeInterval *)proposedTimeInterval
{
uiDateTimePicker *d;
d = (uiDateTimePicker *) mapGet(self->pickers, aDatePickerCell);
[NSTimer scheduledTimerWithTimeInterval:0
target:self
selector:@selector(doTimer:)
userInfo:[NSValue valueWithPointer:d]
repeats:NO];
}
- (void)doTimer:(NSTimer *)timer
{
uiDateTimePicker *d;
d = (uiDateTimePicker*) [[timer userInfo] pointerValue];
(*(d->onChanged))(d, d->onChangedData);
}
- (void)registerPicker:(uiDateTimePicker *)d
{
mapSet(self->pickers, d->dp.cell, d);
[d->dp setDelegate:self];
}
- (void)unregisterPicker:(uiDateTimePicker *)d
{
[d->dp setDelegate:nil];
mapDelete(self->pickers, d->dp.cell);
}
@end
static datePickerDelegateClass *datePickerDelegate = nil;
uiDarwinControlAllDefaults(uiDateTimePicker, dp) uiDarwinControlAllDefaults(uiDateTimePicker, dp)
static void defaultOnChanged(uiDateTimePicker *d, void *data)
{
// do nothing
}
static uiDateTimePicker *finishNewDateTimePicker(NSDatePickerElementFlags elements) static uiDateTimePicker *finishNewDateTimePicker(NSDatePickerElementFlags elements)
{ {
uiDateTimePicker *d; uiDateTimePicker *d;
@ -15,6 +87,7 @@ static uiDateTimePicker *finishNewDateTimePicker(NSDatePickerElementFlags elemen
uiDarwinNewControl(uiDateTimePicker, d); uiDarwinNewControl(uiDateTimePicker, d);
d->dp = [[NSDatePicker alloc] initWithFrame:NSZeroRect]; d->dp = [[NSDatePicker alloc] initWithFrame:NSZeroRect];
[d->dp setDateValue:[NSDate date]];
[d->dp setBordered:NO]; [d->dp setBordered:NO];
[d->dp setBezeled:YES]; [d->dp setBezeled:YES];
[d->dp setDrawsBackground:YES]; [d->dp setDrawsBackground:YES];
@ -23,6 +96,13 @@ static uiDateTimePicker *finishNewDateTimePicker(NSDatePickerElementFlags elemen
[d->dp setDatePickerMode:NSSingleDateMode]; [d->dp setDatePickerMode:NSSingleDateMode];
uiDarwinSetControlFont(d->dp, NSRegularControlSize); uiDarwinSetControlFont(d->dp, NSRegularControlSize);
if (datePickerDelegate == nil) {
datePickerDelegate = [[datePickerDelegateClass new] autorelease];
[delegates addObject:datePickerDelegate];
}
[datePickerDelegate registerPicker:d];
uiDateTimePickerOnChanged(d, defaultOnChanged, NULL);
return d; return d;
} }
@ -43,12 +123,33 @@ uiDateTimePicker *uiNewTimePicker(void)
void uiDateTimePickerTime(uiDateTimePicker *d, struct tm *time) void uiDateTimePickerTime(uiDateTimePicker *d, struct tm *time)
{ {
time_t t;
struct tm tmbuf;
NSDate *date;
NSTimeInterval interval;
date = [d->dp dateValue];
interval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:date];
date = [date dateByAddingTimeInterval:interval];
t = (time_t) [date timeIntervalSince1970];
tmbuf = *gmtime(&t);
memcpy(time, &tmbuf, sizeof(struct tm));
} }
void uiDateTimePickerSetTime(uiDateTimePicker *d, const struct tm *time) void uiDateTimePickerSetTime(uiDateTimePicker *d, const struct tm *time)
{ {
time_t t;
struct tm tmbuf;
memcpy(&tmbuf, time, sizeof(struct tm));
t = mktime(&tmbuf);
[d->dp setDateValue:[NSDate dateWithTimeIntervalSince1970:t]];
} }
void uiDateTimePickerOnChanged(uiDateTimePicker *d, void (*f)(uiDateTimePicker *, void *), void *data) void uiDateTimePickerOnChanged(uiDateTimePicker *d, void (*f)(uiDateTimePicker *, void *), void *data)
{ {
d->onChanged = f;
d->onChangedData = data;
} }