2015-08-14 20:46:35 -05:00
|
|
|
// 14 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
struct uiDateTimePicker {
|
|
|
|
uiDarwinControl c;
|
|
|
|
NSDatePicker *dp;
|
2018-02-21 23:47:45 -06:00
|
|
|
void (*onChanged)(uiDateTimePicker *, void *);
|
|
|
|
void *onChangedData;
|
2018-05-13 17:04:57 -05:00
|
|
|
BOOL blockSendOnce;
|
2015-08-14 20:46:35 -05:00
|
|
|
};
|
|
|
|
|
2018-05-13 11:05:09 -05:00
|
|
|
// TODO see if target-action works here or not; I forgot what cody271@ originally said
|
|
|
|
// the primary advantage of the delegate is the ability to reject changes, but libui doesn't support that yet — we should consider that API option as well
|
|
|
|
@interface uiprivDatePickerDelegateClass : NSObject<NSDatePickerCellDelegate> {
|
|
|
|
uiprivMap *pickers;
|
2018-02-21 23:47:45 -06:00
|
|
|
}
|
2018-05-05 23:21:25 -05:00
|
|
|
- (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell validateProposedDateValue:(NSDate **)proposedDateValue timeInterval:(NSTimeInterval *)proposedTimeInterval;
|
2018-02-21 23:47:45 -06:00
|
|
|
- (void)doTimer:(NSTimer *)timer;
|
|
|
|
- (void)registerPicker:(uiDateTimePicker *)b;
|
|
|
|
- (void)unregisterPicker:(uiDateTimePicker *)b;
|
|
|
|
@end
|
|
|
|
|
2018-05-05 23:28:19 -05:00
|
|
|
@implementation uiprivDatePickerDelegateClass
|
2018-02-21 23:47:45 -06:00
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2018-05-12 23:40:47 -05:00
|
|
|
self->pickers = uiprivNewMap();
|
2018-02-21 23:47:45 -06:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2018-05-12 23:40:47 -05:00
|
|
|
uiprivMapDestroy(self->pickers);
|
2018-02-21 23:47:45 -06:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2018-05-13 11:05:09 -05:00
|
|
|
- (void)datePickerCell:(NSDatePickerCell *)cell validateProposedDateValue:(NSDate **)proposedDateValue timeInterval:(NSTimeInterval *)proposedTimeInterval
|
2018-02-21 23:47:45 -06:00
|
|
|
{
|
|
|
|
uiDateTimePicker *d;
|
|
|
|
|
2018-05-13 11:05:09 -05:00
|
|
|
d = (uiDateTimePicker *) uiprivMapGet(self->pickers, cell);
|
2018-02-21 23:47:45 -06:00
|
|
|
[NSTimer scheduledTimerWithTimeInterval:0
|
|
|
|
target:self
|
|
|
|
selector:@selector(doTimer:)
|
|
|
|
userInfo:[NSValue valueWithPointer:d]
|
|
|
|
repeats:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doTimer:(NSTimer *)timer
|
|
|
|
{
|
2018-05-13 11:05:09 -05:00
|
|
|
NSValue *v;
|
2018-02-21 23:47:45 -06:00
|
|
|
uiDateTimePicker *d;
|
|
|
|
|
2018-05-13 11:05:09 -05:00
|
|
|
v = (NSValue *) [timer userInfo];
|
|
|
|
d = (uiDateTimePicker *) [v pointerValue];
|
2018-05-13 17:04:57 -05:00
|
|
|
if (d->blockSendOnce) {
|
|
|
|
d->blockSendOnce = NO;
|
|
|
|
return;
|
|
|
|
}
|
2018-02-21 23:47:45 -06:00
|
|
|
(*(d->onChanged))(d, d->onChangedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerPicker:(uiDateTimePicker *)d
|
|
|
|
{
|
2018-05-12 23:40:47 -05:00
|
|
|
uiprivMapSet(self->pickers, d->dp.cell, d);
|
2018-02-21 23:47:45 -06:00
|
|
|
[d->dp setDelegate:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterPicker:(uiDateTimePicker *)d
|
|
|
|
{
|
|
|
|
[d->dp setDelegate:nil];
|
2018-05-12 23:40:47 -05:00
|
|
|
uiprivMapDelete(self->pickers, d->dp.cell);
|
2018-02-21 23:47:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-05-05 23:28:19 -05:00
|
|
|
static uiprivDatePickerDelegateClass *datePickerDelegate = nil;
|
2018-02-21 23:47:45 -06:00
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinControlAllDefaults(uiDateTimePicker, dp)
|
2015-08-14 20:46:35 -05:00
|
|
|
|
2018-02-21 23:47:45 -06:00
|
|
|
static void defaultOnChanged(uiDateTimePicker *d, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2018-05-13 11:05:09 -05:00
|
|
|
// TODO consider using NSDateComponents iff we ever need the extra accuracy of not using NSTimeInterval
|
2018-02-22 21:20:16 -06:00
|
|
|
void uiDateTimePickerTime(uiDateTimePicker *d, struct tm *time)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
struct tm tmbuf;
|
|
|
|
NSDate *date;
|
|
|
|
|
|
|
|
date = [d->dp dateValue];
|
|
|
|
t = (time_t) [date timeIntervalSince1970];
|
|
|
|
|
2018-05-12 22:03:20 -05:00
|
|
|
// Copy time to minimize a race condition
|
|
|
|
// time.h functions use global non-thread-safe data
|
2018-02-22 21:20:16 -06:00
|
|
|
tmbuf = *localtime(&t);
|
2018-05-13 11:05:09 -05:00
|
|
|
memcpy(time, &tmbuf, sizeof (struct tm));
|
2018-02-22 21:20:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiDateTimePickerSetTime(uiDateTimePicker *d, const struct tm *time)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
struct tm tmbuf;
|
|
|
|
|
2018-05-12 22:03:20 -05:00
|
|
|
// Copy time because mktime() modifies its argument
|
2018-05-13 11:05:09 -05:00
|
|
|
memcpy(&tmbuf, time, sizeof (struct tm));
|
2018-02-22 21:20:16 -06:00
|
|
|
t = mktime(&tmbuf);
|
|
|
|
|
2018-05-13 17:04:57 -05:00
|
|
|
// TODO get rid of the need for this
|
|
|
|
d->blockSendOnce = YES;
|
2018-02-22 21:20:16 -06:00
|
|
|
[d->dp setDateValue:[NSDate dateWithTimeIntervalSince1970:t]];
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiDateTimePickerOnChanged(uiDateTimePicker *d, void (*f)(uiDateTimePicker *, void *), void *data)
|
|
|
|
{
|
|
|
|
d->onChanged = f;
|
|
|
|
d->onChangedData = data;
|
|
|
|
}
|
|
|
|
|
2015-08-14 20:46:35 -05:00
|
|
|
static uiDateTimePicker *finishNewDateTimePicker(NSDatePickerElementFlags elements)
|
|
|
|
{
|
|
|
|
uiDateTimePicker *d;
|
|
|
|
|
2016-04-25 10:54:09 -05:00
|
|
|
uiDarwinNewControl(uiDateTimePicker, d);
|
2015-08-14 20:46:35 -05:00
|
|
|
|
|
|
|
d->dp = [[NSDatePicker alloc] initWithFrame:NSZeroRect];
|
2018-02-21 23:47:45 -06:00
|
|
|
[d->dp setDateValue:[NSDate date]];
|
2015-08-22 11:16:27 -05:00
|
|
|
[d->dp setBordered:NO];
|
|
|
|
[d->dp setBezeled:YES];
|
|
|
|
[d->dp setDrawsBackground:YES];
|
2015-08-14 20:46:35 -05:00
|
|
|
[d->dp setDatePickerStyle:NSTextFieldAndStepperDatePickerStyle];
|
|
|
|
[d->dp setDatePickerElements:elements];
|
|
|
|
[d->dp setDatePickerMode:NSSingleDateMode];
|
2015-08-22 11:16:27 -05:00
|
|
|
uiDarwinSetControlFont(d->dp, NSRegularControlSize);
|
2015-08-14 20:46:35 -05:00
|
|
|
|
2018-02-21 23:47:45 -06:00
|
|
|
if (datePickerDelegate == nil) {
|
2018-05-05 23:28:19 -05:00
|
|
|
datePickerDelegate = [[uiprivDatePickerDelegateClass new] autorelease];
|
2018-05-12 23:40:47 -05:00
|
|
|
[uiprivDelegates addObject:datePickerDelegate];
|
2018-02-21 23:47:45 -06:00
|
|
|
}
|
|
|
|
[datePickerDelegate registerPicker:d];
|
|
|
|
uiDateTimePickerOnChanged(d, defaultOnChanged, NULL);
|
|
|
|
|
2015-08-14 20:46:35 -05:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiDateTimePicker *uiNewDateTimePicker(void)
|
|
|
|
{
|
|
|
|
return finishNewDateTimePicker(NSYearMonthDayDatePickerElementFlag | NSHourMinuteSecondDatePickerElementFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
uiDateTimePicker *uiNewDatePicker(void)
|
|
|
|
{
|
|
|
|
return finishNewDateTimePicker(NSYearMonthDayDatePickerElementFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
uiDateTimePicker *uiNewTimePicker(void)
|
|
|
|
{
|
|
|
|
return finishNewDateTimePicker(NSHourMinuteSecondDatePickerElementFlag);
|
|
|
|
}
|