-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWBTimeControl.m
775 lines (595 loc) · 23.6 KB
/
WBTimeControl.m
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
/*
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain this list of conditions and the following disclaimer.
The names of its contributors may not be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10/23/05 Jerry Krinock:
Added -setEnabled: method over-riding superclass. Previously, sending setEnabled:NO would not
affect the WBTimeController; it was in effect always enabled.
02/28/04 Jerry Krinock:
1. I changed the WBTimeControl files to remove the "seconds" cell, since I only want hours and
minutes in my interface. That is, I wanted 2 cells instead of 3. There are
a dozen or so such changes, all marked by the comment: // seconds not used //.
2. In lines 96 and 443 of WBTimeControl.m, the divisor was hard-coded to "3", so I changed
it to WBTIMECONTROL_CELL_COUNT.
3. CodeWarrior barfed on the first statement in setDate: until I changed the type declaration
of its argument from (id) to (NSCalendarDate*).
4. Added calls to SSLog (RaisinLand Logging, wrapper for NSLog) for debugging.
5. Changed file extension from ".m" to ".mm", because otherwise, for some reason, the
linker in Xcode cannot find the definition for SSLog, although this problem does not
occur when linking in CodeWarrior.
06/25/03:
Fix a bug in acceptNewValueInSelectedCell
10/16/04:
Fix a bug occurring with double-clicks
Support for proper tabbing
Support enable/disable state
*/
#import "SSApp/SSUtils.h" // for Jerry's Raisinland SSLog
#import "WBTimeControl.h"
#define WBTIMECONTROL_LEFT_OFFSET 3.5
#define WBTIMECONTROL_INTERCELL_SPACE 4
#define WBTIMECONTROL_RIGHT_OFFSET 3
#define WBTIMECONTROL_TOP_OFFSET 3
#define WBTIMECONTROL_BOTTOM_OFFSET 9
extern NSInteger gLogging ;
// seconds not used // int _WBTimeControlMax[WBTIMECONTROL_CELL_COUNT]={23,59,59};
NSInteger _WBTimeControlMax[WBTIMECONTROL_CELL_COUNT]={23,59};
@implementation WBTimeControl
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSInteger i;
NSRect tCellFrame;
// No cell selected
selected=WBTIMECONTROL_HOUR_ID;
isUsingFieldEditor=NO;
// Set the default date
currentDate=[[NSCalendarDate calendarDate] retain];
// Create the 3 cells
for(i=0;i<WBTIMECONTROL_CELL_COUNT;i++)
{
cells[i]=[[NSTextFieldCell alloc] initTextCell:@""];
[(NSTextFieldCell *) cells[i] setDrawsBackground:YES];
[(NSTextFieldCell *) cells[i] setEditable:YES];
[(NSTextFieldCell *) cells[i] setBordered:NO];
[(NSTextFieldCell *) cells[i] setFont:[NSFont labelFontOfSize:13]];
}
// Set the default values
[(NSTextFieldCell *) cells[WBTIMECONTROL_HOUR_ID] setStringValue:[NSString stringWithFormat:@"%02ld",(long)[currentDate hourOfDay]]];
[(NSTextFieldCell *) cells[WBTIMECONTROL_MINUTE_ID] setStringValue:[NSString stringWithFormat:@"%02ld",(long)[currentDate minuteOfHour]]];
// seconds not used //[(NSTextFieldCell *) cells[WBTIMECONTROL_SECOND_ID] setStringValue:[NSString stringWithFormat:@"%02d",[currentDate secondOfMinute]]];
// Create the 2 colon cells
for(i=0;i<(WBTIMECONTROL_CELL_COUNT-1);i++)
{
colonCells[i]=[[NSTextFieldCell alloc] initTextCell:@":"];
[(NSTextFieldCell *) colonCells[i] setDrawsBackground:NO];
[(NSTextFieldCell *) colonCells[i] setEditable:NO];
[(NSTextFieldCell *) colonCells[i] setBordered:NO];
[(NSTextFieldCell *) colonCells[i] setFont:[NSFont labelFontOfSize:13]];
}
// Compute the Cells' frame
tCellFrame.origin.x=WBTIMECONTROL_LEFT_OFFSET;
tCellFrame.origin.y=WBTIMECONTROL_BOTTOM_OFFSET;
tCellFrame.size.width=(NSWidth(frame)-2*WBTIMECONTROL_INTERCELL_SPACE-WBTIMECONTROL_RIGHT_OFFSET-WBTIMECONTROL_LEFT_OFFSET)/WBTIMECONTROL_CELL_COUNT;
tCellFrame.size.height=NSHeight(frame)-WBTIMECONTROL_TOP_OFFSET-WBTIMECONTROL_BOTTOM_OFFSET;
for(i=0;i<WBTIMECONTROL_CELL_COUNT;i++)
{
rects[i]=tCellFrame;
tCellFrame.origin.x=NSMaxX(tCellFrame)+WBTIMECONTROL_INTERCELL_SPACE;
}
// Compute the Colons' frame
for(i=0;i<(WBTIMECONTROL_CELL_COUNT-1);i++)
{
colonRects[i]=tCellFrame;
colonRects[i].origin.x=NSMaxX(rects[i])-3;
colonRects[i].size.width=NSMinX(rects[i+1])-NSMinX(colonRects[i])-1;
}
SSLog(5, "WBTimeController -initWithFrame") ;
}
return self;
}
- (NSCalendarDate*)date // type NSCalendarDate* added by Jerry. Was id
{
SSLog(5, "WBTimeControl -date. Returning %@", [currentDate description]) ;
return [[currentDate retain] autorelease];
}
- (void) setDate:(NSCalendarDate*) aDate // argument type changed by Jerry to satisfy CodeWarrior, from id to NSCalendarDate*.
{
SSLog(5, "WBTimeControl setDate:%@", [aDate description]) ;
if ([currentDate isEqualToDate:aDate]==NO)
{
if (currentDate!=aDate)
{
[currentDate release];
currentDate=[aDate copy];
}
[self editOff];
[cells[WBTIMECONTROL_HOUR_ID] setStringValue:[NSString stringWithFormat:@"%02ld", (long)[self hour]]];
[cells[WBTIMECONTROL_MINUTE_ID] setStringValue:[NSString stringWithFormat:@"%02ld", (long)[self minute]]];
// We don't do seconds
//[cells[WBTIMECONTROL_SECOND_ID] setStringValue:[NSString stringWithFormat:@"%02d",[self second]]];
}
}
#pragma mark * -
- (void) setEnabled:(BOOL) enabled
{
NSInteger i;
for(i=0;i<WBTIMECONTROL_CELL_COUNT;i++)
{
[cells[i] setEnabled:enabled];
}
if (stepper!=nil)
{
[stepper setEnabled:enabled];
}
[super setEnabled:enabled];
}
#pragma mark * -
- (void) setDelegate:(id) aDelegate
{
if (delegate!=nil)
{
[[NSNotificationCenter defaultCenter] removeObserver:delegate
name:nil
object:self];
}
delegate = aDelegate;
if ([delegate respondsToSelector: @selector(controlTextDidEndEditing:)])
{
[[NSNotificationCenter defaultCenter] addObserver: delegate
selector: @selector(controlTextDidEndEditing:)
name: NSControlTextDidEndEditingNotification
object: self];
}
}
- (void)setHour:(NSInteger)aHour
{
SSLog(5, "WBTimeControl setHour:%i", aHour ) ;
NSInteger tHour=[self hour];
if (aHour>=0 && aHour<=_WBTimeControlMax[WBTIMECONTROL_HOUR_ID])
{
NSCalendarDate * tDate;
tDate=[currentDate dateByAddingYears:0
months:0
days:0
hours:aHour-tHour
minutes:0
seconds:0];
[currentDate release];
currentDate=[tDate retain];
stepperMidValue=aHour;
if (isUsingFieldEditor==YES && selected==WBTIMECONTROL_HOUR_ID)
{
[[self currentEditor] setString:[NSString stringWithFormat:@"%02ld", (long)aHour]];
[self editOff];
}
else
{
[cells[WBTIMECONTROL_HOUR_ID] setStringValue:[NSString stringWithFormat:@"%02ld", (long)aHour]];
}
}
}
- (NSInteger)hour
{
SSLog(5, "WBTimeControl -hour. Returning %i", [currentDate hourOfDay]) ;
return [currentDate hourOfDay];
}
- (void)setMinute:(NSInteger)aMinute
{
SSLog(5, "WBTimeControl -setMinute:%i", aMinute ) ;
NSInteger tMinute=[self minute];
if (aMinute>=0 && aMinute<=_WBTimeControlMax[WBTIMECONTROL_MINUTE_ID])
{
NSCalendarDate * tDate;
tDate=[currentDate dateByAddingYears:0
months:0
days:0
hours:0
minutes:aMinute-tMinute
seconds:0];
[currentDate release];
currentDate=[tDate retain];
stepperMidValue=aMinute;
if (isUsingFieldEditor==YES && selected==WBTIMECONTROL_MINUTE_ID)
{
[[self currentEditor] setString:[NSString stringWithFormat:@"%02ld", (long)aMinute]];
[self editOff];
}
else
{
[cells[WBTIMECONTROL_MINUTE_ID] setStringValue:[NSString stringWithFormat:@"%02ld", (long)aMinute]];
}
}
}
- (NSInteger)minute
{
SSLog(5, "WBTimeControl - minute. Returned %i", [currentDate minuteOfHour]) ;
return [currentDate minuteOfHour];
}
// seconds not used //
/*
- (void)setSecond:(int)aSecond
{
int tSecond=[self second];
if (aSecond>=0 && aSecond<=_WBTimeControlMax[WBTIMECONTROL_SECOND_ID])
{
NSCalendarDate * tDate;
tDate=[currentDate dateByAddingYears:0
months:0
days:0
hours:0
minutes:0
seconds:aSecond-tSecond];
[currentDate release];
currentDate=[tDate retain];
stepperMidValue=aSecond;
if (isUsingFieldEditor==YES && selected==WBTIMECONTROL_SECOND_ID)
{
[[self currentEditor] setString:[NSString stringWithFormat:@"%02d",aSecond]];
[self editOff];
}
else
{
[cells[WBTIMECONTROL_SECOND_ID] setStringValue:[NSString stringWithFormat:@"%02d",aSecond]];
}
}
}
- (int)second
{
return [currentDate secondOfMinute];
}
*/
- (NSInteger)selected
{
SSLog(5, "WBTimeControl -selected. Returning %i.", selected) ;
return selected;
}
- (void)setSelected:(NSInteger)aSelected
{
SSLog(5, "WBTimeControl -setSelected:%i.", aSelected) ;
selected=aSelected;
if (stepper!=nil)
{
[stepper setMinValue:0];
[stepper setMaxValue:_WBTimeControlMax[selected]];
switch(selected)
{
case WBTIMECONTROL_HOUR_ID:
SSLog(5, " Setting stepper hour to %i", [self hour]) ;
[stepper setIntegerValue:[self hour]];
break;
case WBTIMECONTROL_MINUTE_ID:
SSLog(5, " Setting stepper minute to %i", [self minute]) ;
[stepper setIntegerValue:[self minute]];
break;
// seconds not used //
//case WBTIMECONTROL_SECOND_ID:
// [stepper setIntValue:[self second]];
// break;
}
stepperMidValue=[stepper doubleValue];
}
[self setNeedsDisplay];
}
- (void)awakeFromNib
{
[self setSelected:WBTIMECONTROL_HOUR_ID] ;
}
- (BOOL) isOpaque
{
return NO;
}
- (void) drawRect:(NSRect) aFrame
{
NSInteger i;
NSRect tBounds=[self bounds];
NSRect tRect;
CGFloat savedLineWidth;
[[NSColor whiteColor] set];
// Draw the background
tRect=tBounds;
tRect.origin.y=WBTIMECONTROL_BOTTOM_OFFSET-3;
tRect.size.height-=tRect.origin.y;
NSRectFill(tRect);
// Draw the Frame
[[NSColor colorWithDeviceWhite:0.3333 alpha:1.0] set];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(tRect)+0.5,NSMinY(tRect)+1)
toPoint:NSMakePoint(NSMinX(tRect)+0.5,NSMaxY(tRect))];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(tRect),NSMaxY(tRect)-0.5)
toPoint:NSMakePoint(NSMaxX(tRect),NSMaxY(tRect)-0.5)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(tRect)+1.5,NSMinY(tRect)+2)
toPoint:NSMakePoint(NSMinX(tRect)+1.5,NSMaxY(tRect)-1)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(tRect)+1,NSMaxY(tRect)-1.5)
toPoint:NSMakePoint(NSMaxX(tRect)-1,NSMaxY(tRect)-1.5)];
[[NSColor colorWithDeviceWhite:0.6667 alpha:1.0] set];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(tRect)+1,NSMinY(tRect)+1.5)
toPoint:NSMakePoint(NSMaxX(tRect)-1,NSMinY(tRect)+1.5)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(tRect)-1.5,NSMinY(tRect)+1)
toPoint:NSMakePoint(NSMaxX(tRect)-1.5,NSMaxY(tRect)-2)];
// Draw the selection thumb
[[NSColor colorWithDeviceWhite:0.5765 alpha:1.0] set];
savedLineWidth=[NSBezierPath defaultLineWidth];
[NSBezierPath setDefaultLineWidth:2];
[NSBezierPath strokeLineFromPoint:NSMakePoint(floor(NSMidX(rects[selected]))-4,WBTIMECONTROL_BOTTOM_OFFSET-8)
toPoint:NSMakePoint(floor(NSMidX(rects[selected])),WBTIMECONTROL_BOTTOM_OFFSET-4)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(floor(NSMidX(rects[selected])),WBTIMECONTROL_BOTTOM_OFFSET-4)
toPoint:NSMakePoint(floor(NSMidX(rects[selected]))+4,WBTIMECONTROL_BOTTOM_OFFSET-8)];
[NSBezierPath setDefaultLineWidth:savedLineWidth];
// Draw the Time Separator
for(i=0;i<(WBTIMECONTROL_CELL_COUNT-1);i++)
{
[colonCells[i] drawWithFrame:colonRects[i] inView:self];
}
// Draw the cells
for(i=0;i<WBTIMECONTROL_CELL_COUNT;i++)
{
[cells[i] drawWithFrame:rects[i] inView:self];
}
}
- (void) mouseDown:(NSEvent *) theEvent
{
if ([self isEnabled]==YES)
{
NSInteger i;
NSPoint tMouseLoc=[self convertPoint:[theEvent locationInWindow] fromView:nil];
NSRect tThumbRect;
NSRect tBounds;
CGFloat tWidth;
// Find where the event occurred
tBounds=[self bounds];
tWidth=NSWidth(tBounds)/3;
// Either in the thumb part
for(i=0;i<WBTIMECONTROL_CELL_COUNT;i++)
{
tThumbRect=NSMakeRect(tWidth*i,0,tWidth,WBTIMECONTROL_BOTTOM_OFFSET-4);
if (NSMouseInRect(tMouseLoc,tThumbRect,[self isFlipped])==YES)
{
if (i!=selected)
{
if (isUsingFieldEditor==YES)
{
[self editOff];
}
[self setSelected:i];
}
break;
}
}
// Or in a cell
for(i=0;i<WBTIMECONTROL_CELL_COUNT;i++)
{
if (NSMouseInRect(tMouseLoc,rects[i],[self isFlipped])==YES)
{
if (i!=selected || isUsingFieldEditor==NO)
{
if (isUsingFieldEditor==YES)
{
[self editOff];
}
else
{
[_window endEditingFor:nil];
[_window makeFirstResponder: self];
}
[self editCell:i];
}
else
{
if (isUsingFieldEditor==NO)
{
[cells[selected] editWithFrame:rects[selected]
inView:self
editor:[self currentEditor]
delegate:self
event:theEvent];
}
}
break;
}
}
}
}
- (BOOL)acceptNewValueInSelectedCell:(id) sender
{
NSString *string;
NSInteger tValue;
SSLog(5, "WBTimeControl -acceptNewValueInSelectedCell with selected=%i", selected) ;
string = [[[[self currentEditor] string] copy] autorelease];
tValue=[string integerValue];
if (tValue<=_WBTimeControlMax[selected])
{
[cells[selected] setStringValue: [NSString stringWithFormat:@"%02ld", (long)tValue]];
// Set the new date
switch(selected)
{
case WBTIMECONTROL_HOUR_ID:
[self setHour:tValue];
break;
case WBTIMECONTROL_MINUTE_ID:
[self setMinute:tValue];
break;
// seconds not used //
//case WBTIMECONTROL_SECOND_ID:
// [self setSecond:tValue];
// break;
}
SSLog(5, " Value accepted.") ;
return YES;
}
SSLog(5, " Value not accepted.") ;
return NO;
}
- (void)editCell:(NSInteger) aSelected
{
SSLog(5, "WBTimeControl -editCell:%i", aSelected) ;
NSText *tObject;
NSText* t = [_window fieldEditor: YES forObject: self];
NSInteger length;
id tCell=cells[aSelected];
length = [[tCell stringValue] length];
tObject = [tCell setUpFieldEditorAttributes: t];
[tCell selectWithFrame: rects[aSelected]
inView: self
editor: tObject
delegate: self
start: 0
length: length];
isUsingFieldEditor=YES;
[self setSelected:aSelected];
}
- (void)editOff
{
if (isUsingFieldEditor==YES)
{
SSLog(5, "WBTimeControl -editOff") ;
[cells[selected] endEditing: [self currentEditor]];
isUsingFieldEditor=NO;
}
}
- (void) textDidEndEditing:(NSNotification *) aNotification
{
NSMutableDictionary * tDictionary;
id textMovement;
BOOL wasAccepted;
SSLog(5, "WBTimeControl -textDidEndEditing.") ;
wasAccepted=[self acceptNewValueInSelectedCell:self];
[cells[selected] endEditing: [aNotification object]];
if (wasAccepted==YES)
{
SSLog(5, "WBTimeControl -textDidEndEditing. Finds accepted=YES") ;
tDictionary = [[[NSMutableDictionary alloc] initWithDictionary:[aNotification userInfo]] autorelease];
[tDictionary setObject: [aNotification object] forKey: @"NSFieldEditor"];
[[NSNotificationCenter defaultCenter] postNotificationName: NSControlTextDidEndEditingNotification
object: self
userInfo: tDictionary];
}
else
SSLog(5, "WBTimeControl -textDidEndEditing. Finds accepted=NO") ;
isUsingFieldEditor=NO;
textMovement = [[aNotification userInfo] objectForKey: @"NSTextMovement"];
if (textMovement)
{
SSLog(5, "WBTimeControl -textDidEndEditing. Detected textMovement") ;
switch ([(NSNumber *)textMovement integerValue])
{
case NSReturnTextMovement:
if ([self sendAction:[self action] to:[self target]] == NO)
{
NSEvent *event = [_window currentEvent];
if ([self performKeyEquivalent: event] == NO
&& [_window performKeyEquivalent: event] == NO)
{
}
}
break;
case NSTabTextMovement:
// seconds not used //
//if (selected<WBTIMECONTROL_SECOND_ID)
if (selected<WBTIMECONTROL_MINUTE_ID)
{
[self editCell:selected+1];
break;
}
[_window selectKeyViewFollowingView: self];
if ([_window firstResponder] == _window)
{
[self editCell:WBTIMECONTROL_HOUR_ID];
}
break;
case NSBacktabTextMovement:
if (selected>WBTIMECONTROL_HOUR_ID)
{
[self editCell:selected-1];
break;
}
[_window selectKeyViewFollowingView: self];
if ([_window firstResponder] == _window)
{
// seconds not used //[self editCell:WBTIMECONTROL_SECOND_ID];
[self editCell:WBTIMECONTROL_MINUTE_ID]; // I want the first responder to be hours
}
break;
}
}
}
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
NSInteger i,tLength;
unichar tUniChar;
SSLog(5, "WBTimeControl -shouldChangeTextInRange(%i,%i), to \"%@\".", affectedCharRange.location, affectedCharRange.length, replacementString) ;
tLength=[replacementString length];
if (affectedCharRange.location>=2 || affectedCharRange.length>2)
{
return NO;
}
for(i=0;i<tLength;i++)
{
tUniChar=[replacementString characterAtIndex:i];
if (tUniChar<'0' || tUniChar>'9')
{
return NO;
}
}
if (stepper!=nil)
{
NSMutableString * tString;
NSInteger tValue;
tString=[[[textView string] mutableCopy] autorelease];
[tString replaceCharactersInRange:affectedCharRange withString:replacementString];
tValue=[tString integerValue];
if (tValue>_WBTimeControlMax[selected])
{
[stepper setDoubleValue:stepperMidValue];
}
else
{
[stepper setIntegerValue:tValue];
}
}
return YES;
}
- (BOOL)acceptsFirstResponder
{
// We want to be able to become first responder
return [self isEnabled];
}
- (BOOL)needsPanelToBecomeKey
{
// We want to get focus via mouse down AND tabbing
return YES;
}
- (BOOL)becomeFirstResponder
{
BOOL result = [super becomeFirstResponder];
if (result)
{
NSSelectionDirection selectionDirection = [[self window] keyViewSelectionDirection];
if (selectionDirection==NSSelectingNext)
{
[self editCell:0];
}
else if (selectionDirection==NSSelectingPrevious)
{
[self editCell:WBTIMECONTROL_CELL_COUNT-1];
}
else
{
// Direct selection by clicking. Let the mouse down code figure out which cell to edit.
}
}
return result;
}
@end