forked from Me1000/Cappuccino-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatePicker.j
1208 lines (987 loc) · 38.9 KB
/
DatePicker.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DatePicker.j
*
* Created by Randall Luecke.
* Copyright 2009, RCL Concepts.
* http://www.rclconcepts.com/
* http://www.timetableapp.com/
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPControl.j>
@import <AppKit/CPTextField.j>
@import <AppKit/CPView.j>
@import "Stepper.j"
var datePickerDelegate = @"datePickerDelegateIdentifier",
datePickerSegments = @"datePickerSegmentsIdentifier",
datePickerView = @"datePickerViewIdentifier",
datePickerStepper = @"datePickerStepperIdentifier",
datePickerDate = @"datePickerDateIdentifier";
CPLogRegister(CPLogConsole);
@implementation DatePicker : CPControl
{
CPView _theView @accessors;
Stepper _theStepper @accessors;
CPArray segments @accessors;
id superController @accessors;
CPDate _date;
CPDate _minDate;
CPDate _maxDate;
id bezel;
id bezelFocused;
id dateSegmentFocused;
BOOL focused @accessors;
id inputManager @accessors;
id currentFocusedSegment @accessors;
id lastFocusedSegment @accessors; //not used any longer I don't believe
id _delegate;
//used from the input manager migrations
id activeDateSegment @accessors;
id prevActiveDateSegment @accessors;
id superController @accessors;
BOOL _dontsetfirsttome;
}
- (id)initWithFrame:aFrame
{
self = [super initWithFrame:aFrame];
if(self){
_theView = [[CPView alloc] initWithFrame:CGRectMake(3, 3, CGRectGetWidth(aFrame) - 20, 23)];
//bezel = [CPColor whiteColor];
//bezelFocused = [CPColor whiteColor];
bezel = [CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:
[
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-0.png" size:CGSizeMake(3.0, 4.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-1.png" size:CGSizeMake(1.0, 4.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-2.png" size:CGSizeMake(3.0, 4.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-3.png" size:CGSizeMake(3.0, 1.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-4.png" size:CGSizeMake(1.0, 1.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-5.png" size:CGSizeMake(3.0, 1.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-6.png" size:CGSizeMake(3.0, 4.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-7.png" size:CGSizeMake(1.0, 4.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-8.png" size:CGSizeMake(3.0, 4.0)]
]]];
bezelFocused = [CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:
[
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-0.png" size:CGSizeMake(7.0, 7.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-1.png" size:CGSizeMake(1.0, 7.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-2.png" size:CGSizeMake(7.0, 7.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-3.png" size:CGSizeMake(7.0, 1.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-4.png" size:CGSizeMake(1.0, 1.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-5.png" size:CGSizeMake(7.0, 1.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-6.png" size:CGSizeMake(7.0, 7.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-7.png" size:CGSizeMake(1.0, 7.0)],
[[CPImage alloc] initWithContentsOfFile:"Frameworks/AppKit/Resources/Aristo2.blend/Resources/textfield-bezel-square-focused-8.png" size:CGSizeMake(7.0, 7.0)]
]]];
[_theView setBackgroundColor:bezel];
inputManager = self;//[[DatePickerInputManager alloc] init];
[inputManager setSuperController:self];
_theStepper = [[Stepper alloc] initWithFrame:CGRectMake(aFrame.size.width -13, 4, 13, 25)];
[_theStepper setTarget:self];
[_theStepper setAction:@selector(stepperAction:)];
[self addSubview:_theView];
[self addSubview:_theStepper];
_date = [CPDate dateWithTimeIntervalSinceNow:0];
_maxDate = [CPDate distantFuture];
_minDate = [CPDate distantPast];
segments = [[CPArray alloc] init];
focused = NO;
[_theStepper setEnabled:NO];
[_theStepper setMaxValue:9999];
[_theStepper setMinValue:-1];
}
return self;
}
-(void)setDelegate:(id)aDelegate
{
var defaultCenter = [CPNotificationCenter defaultCenter];
//unsubscribe the existing delegate if it exists
if (_delegate)
{
[defaultCenter removeObserver:_delegate name:"datePickerDidChangeNotification" object:self];
}
_delegate = aDelegate;
if ([_delegate respondsToSelector:@selector(datePickerDidChange:)]){
[defaultCenter addObserver:_delegate selector:@selector(datePickerDidChange:) name:"datePickerDidChangeNotification" object:self];
}
if ([_delegate respondsToSelector:@selector(datePickerDidLoseFocus:)]){
[defaultCenter addObserver:_delegate selector:@selector(datePickerDidLoseFocus:) name:"datePickerDidLoseFocusNotification" object:self];
}
}
//this method doesn't actually work... yet...
- (void)addDateSegmentOfType:(id)segmentType withInitialValue:(CPString)stringValue withSeperatorAtEnd:(CPString)seperator
{
// var aFrameSize = [self makeFrameForType:segmentType];
//
// var newSegment = [[DateSegment alloc] initWithFrame:CGRectMake(6, 7, 20, 18)];
// [dateSegmentes addObject:newSegment];
// [self addSubview: newSegment];
}
- (void)displayPreset:(id)type
{
if(type == 1){ //type is stardard date
var _theMonthField = [[DateSegment alloc] initWithFrame:CGRectMake(28, 7, 20, 18)];
[_theMonthField setStringValue:@"00"];
[_theMonthField sizeToFit];
[_theMonthField setDelegate:self];
[_theMonthField setInputManager: self];
[_theMonthField setSuperController:self];
[_theMonthField setDateType:1];
//[_theMonthField setStringValue: [self _mutableDate].getMonth() + 1];
if(CGRectGetHeight([self frame]) - CGRectGetHeight([_theMonthField frame]) < 14)
{
[self setFrame:CGRectMake(CGRectGetMinX([self frame]), CGRectGetMinY([self frame]), CGRectGetWidth([self frame]), CGRectGetHeight([_theMonthField frame]) + 14)];
[_theView setFrame:CGRectMake(CGRectGetMinX([_theView frame]), CGRectGetMinY([_theView frame]), CGRectGetWidth([_theView frame]), CGRectGetHeight([_theMonthField frame]) + 6)];
}
var _theDayField = [[DateSegment alloc] initWithFrame:CGRectMake(6, 7, 20, 18)];
[_theDayField setStringValue:@"00"];
[_theDayField sizeToFit];
[_theDayField setDelegate:self];
[_theDayField setInputManager: self];
[_theDayField setSuperController:self];
[_theDayField setDateType:2];
//[_theDayField setStringValue: [self _mutableDate].getDate()];
var _theYearField = [[DateSegment alloc] initWithFrame:CGRectMake(50, 7, 35, 18)];
[_theYearField setStringValue:@"0000"];
[_theYearField sizeToFit];
[_theYearField setDelegate:self];
[_theYearField setInputManager: self];
[_theYearField setSuperController:self];
[_theYearField setDateType:3];
//[_theYearField setStringValue: [self _mutableDate].getFullYear()];
[self addSubview: _theMonthField];
[self addSubview: _theDayField];
[self addSubview: _theYearField];
[segments addObject:_theMonthField];
[segments addObject:_theDayField];
[segments addObject:_theYearField];
var slash1 = [[CPTextField alloc] initWithFrame:CGRectMake(23, 7, 15, 18)];
[slash1 setStringValue:@"/"];
[slash1 sizeToFit];
var slash2 = [[CPTextField alloc] initWithFrame:CGRectMake(44, 7, 15, 18)];
[slash2 setStringValue:@"/"];
[slash2 sizeToFit];
[self addSubview:slash1];
[self addSubview:slash2];
}else if(type == 2){ //type is stardard time
var hoursField = [[DateSegment alloc] initWithFrame:CGRectMake(6, 7, 20, 18)];
[hoursField setStringValue:@"00"];
[hoursField sizeToFit];
[hoursField setDelegate:self];
[hoursField setInputManager: self];
[hoursField setSuperController:self];
[hoursField setDateType:9];
if(CGRectGetHeight([self frame]) - CGRectGetHeight([hoursField frame]) < 14)
{
[self setFrame:CGRectMake(CGRectGetMinX([self frame]), CGRectGetMinY([self frame]), CGRectGetWidth([self frame]), CGRectGetHeight([hoursField frame]) + 14)];
[_theView setFrame:CGRectMake(CGRectGetMinX([_theView frame]), CGRectGetMinY([_theView frame]), CGRectGetWidth([_theView frame]), CGRectGetHeight([hoursField frame]) + 6)];
}
/*if([self _mutableDate].getHours() > 12){
var shrotHurStr = [self _mutableDate].getHours() - 12;
}else if([self _mutableDate].getHours() < 1){
shrotHurStr = 12;
}else{
var shrotHurStr = [self _mutableDate].getHours();
}*/
//[hoursField setStringValue:shrotHurStr];
var minutesField = [[DateSegment alloc] initWithFrame:CGRectMake(28, 7, 20, 18)];
[minutesField setStringValue:@"00"];
[minutesField sizeToFit];
[minutesField setDelegate:self];
[minutesField setInputManager: self];
[minutesField setSuperController:self];
[minutesField setDateType:8];
//[minutesField setStringValue: [self _mutableDate].getMinutes()];
//[minutesField setStringValue: [minutesField doubleNumber:[minutesField stringValue]]];
var AMPMField = [[DateSegment alloc] initWithFrame:CGRectMake(45, 7, 20, 18)];
[AMPMField setStringValue:@"AM"];
[AMPMField sizeToFit];
[AMPMField setDelegate:self];
[AMPMField setInputManager: self];
[AMPMField setSuperController:self];
[AMPMField setDateType:10];
//var ampmstr = ([self _mutableDate].getHours() > 11) ? @"PM" : "AM";
//[AMPMField setStringValue: ampmstr];
[self addSubview: hoursField];
[self addSubview: minutesField];
[self addSubview: AMPMField];
[segments addObject: hoursField];
[segments addObject: minutesField];
[segments addObject: AMPMField];
var slash1 = [[CPTextField alloc] initWithFrame:CGRectMake(23, 7, 15, 18)];
[slash1 setStringValue:@":"];
//var slash2 = [[CPTextField alloc] initWithFrame:CGRectMake(44, 7, 15, 18)];
//[slash2 setStringValue:@":"];
[self addSubview:slash1];
//[self addSubview:slash2];
}
[self updatePickerDisplay];
}
- (CPDate)minDate
{
return _minDate;
}
- (void)setMinDate:(CPDate)aDate
{
_minDate = [aDate copy];
if([self _mutableDate] < [self minDate]){
[self setDate: [self minDate]];
}
}
- (CPDate)maxDate
{
return _maxDate;
}
- (void)setMaxDate:(CPDate)aDate
{
_maxDate = [aDate copy];
if([self _mutableDate] < [self maxDate]){
[self setDate: [self maxDate]];
}
}
- (CPDate)date
{
return [_date copy];
}
- (CPDate)_mutableDate
{
return _date;
}
- (void)setDate:(CPDate)aDate
{
_date = [aDate copy];
[self updatePickerDisplay];
}
- (void)updatePickerDisplay
{
for(var i = 0; i < [segments count]; i++)
{
var segment = [segments objectAtIndex:i];
//console.log(segment);
if([segment dateType] == 1){
var strValue = [self _mutableDate].getMonth() + 1;
[segment setStringValue:[segment singleNumber:strValue]];
}else if([segment dateType] == 2){
var strValue = [self _mutableDate].getDate();
[segment setStringValue:[segment doubleNumber:strValue]];
}else if([segment dateType] == 3){
var strValue = [self _mutableDate].getFullYear();
[segment setStringValue:[segment quadNumber:strValue]];
}else if([segment dateType] == 8){
var strValue = [self _mutableDate].getMinutes();
[segment setStringValue:[segment doubleNumber:strValue]];
}else if([segment dateType] == 9){
var strValue = [self _mutableDate].getHours();
//console.log([self _mutableDate]);
if(strValue > 12){
strValue = strValue - 12;
}else if([self _mutableDate].getHours() == 0){
strValue = 12;
}
[segment setStringValue:[segment singleNumber:strValue]];
}else if([segment dateType] == 10){
var strValue = [self _mutableDate].getHours();
if(strValue > 11){
strValue = @"PM";
}else{
strValue = @"AM";
}
[segment setStringValue:[segment singleNumber:strValue]];
}
}
}
- (BOOL)becomeFirstResponder
{
if(!currentFocusedSegment)
[self setActiveDateSegment: [segments objectAtIndex:0]];
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)resignFirstResponder
{
//[super resignFirstResponder];
//[self setFocused:NO];
// console.log("resign");
// [[self window] selectNextKeyView:self];
//[[self window] sendEvent:anEvent]; //it doesn't work unless the event is sent twice... idk why.
[self setPrevActiveDateSegment:[self activeDateSegment]];
[self setActiveDateSegment:nil];
//_dontsetfirsttome = NO;
[[CPNotificationCenter defaultCenter] postNotificationName:"datePickerDidLoseFocusNotification" object:superController userInfo:nil];
return YES;
}
- (void)mouseDown:(CPEvent)anEvent
{
[super mouseDown:anEvent];
//[self setFocused:YES];
if(!currentFocusedSegment){
[self setActiveDateSegment:[segments objectAtIndex:0]];
[[self window] makeFirstResponder:self];
}
}
- (void)setFocused:(BOOL)val
{
if(focused === val)
return;
focused = val;
if(focused){
[_theView setFrame:CGRectMake([_theView frame].origin.x - 4, [_theView frame].origin.y - 3, [_theView frame].size.width + 8, [_theView frame].size.height + 6)];
[_theView setBackgroundColor:bezelFocused];
//[inputManager setActiveDateSegment:[segments objectAtIndex:0]];
}else{
[_theView setBackgroundColor:bezel];
[_theView setFrame:CGRectMake([_theView frame].origin.x + 4, [_theView frame].origin.y + 3, [_theView frame].size.width - 8, [_theView frame].size.height - 6)];
}
}
- (int)maxDays
{
//check the month and find the max number of days for that month and return it.
if([self _mutableDate].getFullYear() >= [self maxYear] && [self _mutableDate].getMonth() >= [self maxMonth]){
//console.log("bug in MaxDay");
return [self maxDate].getDate();
}
//should also check for the max date
var month = [self _mutableDate].getMonth();
var days = nil;
if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
days = 31;
}else if(month == 1){
//check for leap year
days = 28;
if([self isLeapYear]){
days++;
}
}else if(month == 3 || month == 5 || month == 8 || month == 10){
days = 30;
}else{
days = 0;
}
return days;
}
- (int)maxMonth
{
//default would be 12 but check for the max date
if([self _mutableDate].getFullYear() >= [self maxYear]){
//console.log("bug in maxMonth");
return [self maxDate].getMonth() + 1;
}else{
return 12;
}
}
- (int)maxYear
{
//check for the max year
if([self maxDate]){
//console.log("bug in maxYear");
return [self maxDate].getFullYear();
}else{
return 9999;
}
}
- (int)minDays
{
//check the month and find the max number of days for that month and return it.
if([self _mutableDate].getFullYear() <= [self minYear] && [self _mutableDate].getMonth() <= [self minMonth]){
return [self minDate].getDate();
}
//should also check for the max date
var days = 1;
return days;
}
- (int)minMonth
{
//default would be 12 but check for the max date
if([self _mutableDate].getFullYear() <= [self minYear]){
return [self minDate].getMonth() + 1;
}else{
return 1;
}
}
- (int)minYear
{
//check for the max year
var theMinYear = [self minDate].getFullYear();
if([self minDate]){
return [self minDate].getFullYear();
}else{
return 1;
}
}
- (int)maxHours
{
return 12;
}
- (int)maxMins
{
return 59;
}
- (int)maxAMPM
{
return 1;
}
- (void)minHours
{
return 1;
}
- (void)minMins
{
return 0;
}
- (void)minAMPM
{
return 0;
}
- (BOOL)isLeapYear
{
var yr = [self _mutableDate].getFullYear();
return !(yr % 4) && (yr % 100) || !(yr % 400) ? true : false;
}
- (void)setActiveDateSegment:(id)newSegment
{
if(activeDateSegment === newSegment)
return;
if([self activeDateSegment]){
[self setLastFocusedSegment:activeDateSegment];
[activeDateSegment makeInactive];
}
activeDateSegment = newSegment;
if(activeDateSegment){
[self setFocused:YES];
[activeDateSegment makeActive];
[self setCurrentFocusedSegment:activeDateSegment]; //must be called before resetting the first responder.
[[self window] makeFirstResponder:self];
//}
}else{
[self setFocused:NO];
[self setCurrentFocusedSegment:activeDateSegment];
}
}
- (void)keyDown:(id)anEvent
{
[self interpretKeyEvents:anEvent];
}
-(void)interpretKeyEvents:(id)anEvent
{
var key = [anEvent keyCode];
if(key == CPTabKeyCode){
if([self activeDateSegment] == [segments objectAtIndex:[[superController segments] count] -1]){
// [[self window] makeFirstResponder: [superController nextResponder]];
//[superController slectNextFirstResponder:anEvent];
//alert([[self window] firstResponder]);
[[self window] selectNextKeyView:self];
//alert([[self window] firstResponder]);
/*[[self window] sendEvent:anEvent];
if([[[self window] firstResponder] class] == [self class])
[[self window] sendEvent:anEvent];*/
return;
}else{
var i = [segments indexOfObject:[self activeDateSegment]];
i++;
i = [segments objectAtIndex:i];
}
[self setActiveDateSegment:i];
}else if(key == CPRightArrowKeyCode || key == 189 || key == 188 || key == 190 || key == 191 || key == 186){
if([self activeDateSegment] == [segments objectAtIndex:[segments count] -1]){
i = [segments objectAtIndex:0];
}else{
var i = [segments indexOfObject:[self activeDateSegment]];
i++;
i = [segments objectAtIndex:i];
}
[self setActiveDateSegment:i];
}else if(key == CPLeftArrowKeyCode){
if([self activeDateSegment] == [segments objectAtIndex:0]){
i = [segments objectAtIndex:[segments count] -1];
}else{
var i = [segments indexOfObject:[self activeDateSegment]];
i--;
i = [segments objectAtIndex:i];
}
[self setActiveDateSegment:i];
}else if(key == CPDeleteKeyCode && [activeDateSegment dateType] !== 10){
//send delete key
[activeDateSegment removeLastChar];
}else if(key == CPUpArrowKeyCode){
//send increment
[activeDateSegment increment];
}else if(key == CPDownArrowKeyCode){
//send decrement
[activeDateSegment decrement];
}else if(key == 48 || key == 96){
[activeDateSegment sendNewInput: @"0"];
}else if(key == 49 || key == 97){
[activeDateSegment sendNewInput: @"1"];
}else if(key == 50 || key == 98){
[activeDateSegment sendNewInput: @"2"];
}else if(key == 51 || key == 99){
[activeDateSegment sendNewInput: @"3"];
}else if(key == 52 || key == 100){
[activeDateSegment sendNewInput: @"4"];
}else if(key == 53 || key == 101){
[activeDateSegment sendNewInput: @"5"];
}else if(key == 54 || key == 102){
[activeDateSegment sendNewInput: @"6"];
}else if(key == 55 || key == 103){
[activeDateSegment sendNewInput: @"7"];
}else if(key == 56 || key == 104){
[activeDateSegment sendNewInput: @"8"];
}else if(key == 57 || key == 105){
[activeDateSegment sendNewInput: @"9"];
}else if(key == 65 && [activeDateSegment dateType] == 10){
[activeDateSegment sendNewInput: @"A"];
}else if(key == 80 && [activeDateSegment dateType] == 10){
[activeDateSegment sendNewInput: @"P"];
}
}
- (void)stepperAction:(id)sender
{
//if the stepper is clicked and the date picker isn't active make it active.
//if the date picker was previously selected then select that segment
//otherwise select the first one.
//FIX ME: the stepper needs to be clicked twice before the value changes.
if(!activeDateSegment)
[self setActiveDateSegment:(prevActiveDateSegment) ? prevActiveDateSegment : [segments objectAtIndex:1]];
var newValue = [_theStepper intValue];
//[self setActiveDateSegment:[superController currentFocusedSegment]];
if([activeDateSegment dateType] == 1){
var maxValue = [self maxMonth];
var minValue = [self minMonth];
if(newValue > maxValue){
newValue = minValue;
}else if(newValue < minValue){
newValue = maxValue;
}
/*if([superController _mutableDate].getDate() > [superController maxDays]){
[superController _mutableDate].setDate([superController maxDays]);
[superController updatePickerDisplay];
}*/
[self _mutableDate].setMonth(newValue - 1);
}else if([activeDateSegment dateType] == 2){
var maxValue = [self maxDays];
var minValue = [self minDays];
if(newValue > maxValue){
newValue = minValue;
}else if(newValue < minValue){
newValue = maxValue;
}
[superController _mutableDate].setDate(newValue);
newValue = [activeDateSegment doubleNumber:newValue];
}else if([activeDateSegment dateType] == 3){
var maxValue = [self maxYear];
var minValue = [self minYear];
if(newValue > maxValue){
newValue = minValue;
}else if(newValue < minValue){
newValue = maxValue;
}
[superController _mutableDate].setFullYear(newValue);
}else if([activeDateSegment dateType] == 8){ //THIS BEGINS THE TIME PORTION
var maxValue = [self maxMins];
var minValue = [self minMins];
if(newValue > maxValue){
newValue = minValue;
}else if(newValue < minValue){
newValue = maxValue;
}
[superController _mutableDate].setMinutes(newValue);
newValue = [activeDateSegment doubleNumber:newValue];
}else if([activeDateSegment dateType] == 9){
var maxValue = [self maxHours];
var minValue = [self minHours];
if(newValue > maxValue){
newValue = minValue;
}else if(newValue < minValue){
newValue = maxValue;
}
var oldValue = newValue;
var ampm = [segments objectAtIndex:[segments count] - 1];
if([ampm stringValue] === "PM" && newValue !== 12){
newValue = newValue + 12;
}else if([ampm stringValue] === "AM" && newValue == 12){
newValue = 0;
}
[self _mutableDate].setHours(newValue);
newValue = oldValue;
}else if([activeDateSegment dateType] == 10){
var maxValue = [self maxAMPM];
var minValue = [self minAMPM];
if(newValue == maxValue || newValue < minValue){ //if it's PM
newValue = maxValue;
var strValue = @"PM"
var newHrs = [self _mutableDate].getHours();
if(newHrs < 12){
newHrs = newHrs + 12;
}
}else if(newValue == minValue || newValue > maxValue){ //it's AM
newValue = minValue;
var strValue = @"AM"
var newHrs = [self _mutableDate].getHours();
if(newHrs > 11){
newHrs = newHrs - 12;
}
}
[self _mutableDate].setHours(newHrs);
[activeDateSegment setStringValue:strValue];
[[self _theStepper] setIntValue:newValue];
[[CPNotificationCenter defaultCenter] postNotificationName:"datePickerDidChangeNotification" object:superController userInfo:nil];
return; //early return
}
[activeDateSegment setStringValue:newValue];
[_theStepper setIntValue:[activeDateSegment stringValue]];
[[CPNotificationCenter defaultCenter] postNotificationName:"datePickerDidChangeNotification" object:superController userInfo:nil];
// [superController updatePickerDisplay];
}
@end
/*Custom CPTextField for a segment of the date*/
@implementation DateSegment : CPTextField
{
id inputManager @accessors; //the input manager handels inputs... will probably be a textField but it's defined in DatePicker not this class.
id superController @accessors; //the input manager is now part of superController
id dateType @accessors;
/**********************
Date Type enum:
1. number month field
2. number day field
3. full number year field
4. short name month field
5. short year (last two digits) ??
6. day of week short
7. day of week long
8. minutes
9. hours
10. APMP
11. seconds ??
**********************/
CPColor focusedBackground;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if(self){
var mainBundle = [CPBundle mainBundle];
focusedBackground = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:
[
[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"DatePicker/date-segment-left.png"] size:CGSizeMake(4.0, 18.0)],
[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"DatePicker/date-segment-center.png"] size:CGSizeMake(1.0, 18.0)],
[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"DatePicker/date-segment-right.png"] size:CGSizeMake(4.0, 18.0)]
] isVertical:NO]];
[self setValue:CPRightTextAlignment forThemeAttribute:@"alignment"];
}
return self;
}
- (void)mouseDown:(CPEvent)anEvent
{
[inputManager setActiveDateSegment:self];
//[[self window] makeFirstResponder:[inputManager inputManager]];
[[superController _theStepper] setIntValue:[self stringValue]];
if([self dateType] == 10 && [self stringValue] === @"AM"){
[[superController _theStepper] setIntValue:0];
}else if([self dateType] == 10 && [self stringValue] === @"PM"){
[[superController _theStepper] setIntValue:1];
}
}
- (void)setStringValue:(id)aValue
{
[super setStringValue:aValue];
if ([inputManager activeDateSegment] === self)
{
[[superController _theStepper] setDoubleValue:[self intValue]];
if([self dateType] == 10 && [self stringValue] == @"PM"){
[[superController _theStepper] setDoubleValue:1];
}
}
}
- (void)sendNewInput:(id)anInput
{
//console.log([superController _mutableDate]);
//validate the input
var newValue = nil;
if([self dateType] == 1){
newValue = [self numMonthDateInput:anInput];
[superController _mutableDate].setMonth(newValue - 1);
}else if([self dateType] == 2){
newValue = [self numDayDateInput:anInput];
[superController _mutableDate].setDate(newValue);
}else if([self dateType] == 3){
newValue = [self fullYearDateInput:anInput];
[superController _mutableDate].setFullYear(newValue);
}else if([self dateType] == 8){
newValue = [self fullMinutesInput:anInput];
[superController _mutableDate].setMinutes(newValue);
}else if([self dateType] == 9){
newValue = [self fullHoursInput:anInput];
var ampm = [[superController segments] objectAtIndex:[[superController segments] count] - 1];
if([ampm stringValue] === "PM" && newValue !== 12){
var hrs = newValue + 12;
}else if([ampm stringValue] === "AM" && newValue == 12){
var hrs = 0;
}else{
hrs = newValue;
}
[superController _mutableDate].setHours(hrs);
}else if([self dateType] == 10){
newValue = [self fullAMPMInput:anInput];
var hours = [[[superController segments] objectAtIndex:0] intValue] - 1;
if(newValue == "PM"){
hours = hours + 12;
}
[superController _mutableDate].setHours(hours);
}
//for now just update the string value
[self setStringValue: newValue];
[[CPNotificationCenter defaultCenter] postNotificationName:"datePickerDidChangeNotification" object:superController userInfo:nil];
}
- (void)makeActive
{
[self setBackgroundColor:focusedBackground];
[[superController _theStepper] setDoubleValue:[self intValue]];
}
- (void)makeInactive
{
[self setBackgroundColor:[CPColor whiteColor]];
if([self dateType] == 10)
return;
//now format the number like it should look...
var strValue = [self stringValue];
if(strValue == 0 && [self dateType] !== 8){ //minutes are allowed to be zero, everything else bust be greater than zero.
strValue = 1;
}
if([self dateType] == 1){
[self setStringValue:[self singleNumber:strValue]];
}else if([self dateType] == 2){
[self setStringValue:[self doubleNumber:strValue]];
}else if([self dateType] == 3){
[self setStringValue:[self quadNumber:strValue]];
}else if([self dateType] == 8){
[self setStringValue:[self doubleNumber:strValue]];
}else if([self dateType] == 9){
[self setStringValue:[self singleNumber:strValue]];
}
}
- (void)increment
{
if([self dateType] == 10){ // if date segment is AMPM type
if([self stringValue] == "AM"){
//[self setStringValue: @"PM"];
[[superController _theStepper] setDoubleValue:1];
}else{
//[self setStringValue: @"AM"];
[[superController _theStepper] setDoubleValue:0];
}
}else{
[[superController _theStepper] setDoubleValue:[self intValue] + 1];
}
[[superController _theStepper] sendAction:@selector(stepperAction:) to:inputManager];
}
- (void)decrement
{
if([self dateType] == 10){
if([self stringValue] == "AM"){
[self setStringValue: @"PM"];
[[superController _theStepper] setDoubleValue:1];
}else{
[self setStringValue: @"AM"];
[[superController _theStepper] setDoubleValue:0];
}
}else{
[[superController _theStepper] setDoubleValue:[self intValue] - 1];
}
[[superController _theStepper] sendAction:@selector(stepperAction:) to:inputManager];
}
- (void)removeLastChar
{
var stringValue = [self stringValue];
if([stringValue length] > 0){
var newString = [stringValue stringByPaddingToLength:[stringValue length] -1 withString:@"" startingAtIndex:1];
[self setStringValue: newString];
}
}
- (CPString)description
{
switch([self dateType])
{
case 1: return @"number month field";
case 2: return @"number day field";
case 3: return @"full number year field";
case 4: return @"short name month field";
case 5: return @"short year field";
case 6: return @"day of week short";
case 7: return @"day of week long";
case 8: return @"minutes field";
case 9: return @"hours field";
case 10: return @"am-pm field";
case 11: return @"seconds field";
default: return "date segment field";
}
}
@end
@implementation DateSegment (filterData)