source: releases/Pester/1.1b1/Source/PSAlarmNotifierController.m@ 556

Last change on this file since 556 was 53, checked in by Nicholas Riley, 22 years ago

Updated for Pester 1.1a5 (very limited release).

Pester 1.1a4 was never released.

File size: 5.5 KB
Line 
1//
2// PSAlarmNotifierController.m
3// Pester
4//
5// Created by Nicholas Riley on Tue Oct 08 2002.
6// Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import "PSAlarmNotifierController.h"
10#import "PSAlarmAlertController.h"
11#import "PSAlarm.h"
12#import "PSNotifierAlert.h"
13#import "NJRIntervalField.h"
14
15static NSString * const PSAlarmSnoozeInterval = @"Pester alarm snooze interval"; // NSUserDefaults key
16
17@interface PSAlarmNotifierController (Private)
18
19- (void)update:(id)sender;
20- (void)updateNextDateDisplay:(id)sender;
21
22@end
23
24@implementation PSAlarmNotifierController
25
26// XXX should use NSNonactivatingPanelMask on 10.2
27
28- (id)initWithAlarm:(PSAlarm *)anAlarm;
29{
30 if ([self initWithWindowNibName: @"Notifier"]) {
31 NSWindow *window = [self window];
32 NSRect frameRect = [window frame];
33 alarm = [anAlarm retain];
34 [messageField setStringValue: [alarm message]];
35 [dateField setStringValue: [alarm dateTimeString]];
36 if (![snoozeIntervalField setInterval: [alarm snoozeInterval]] &&
37 ![snoozeIntervalField setInterval: [[[NSUserDefaults standardUserDefaults] objectForKey: PSAlarmSnoozeInterval] doubleValue]])
38 [snoozeIntervalField setInterval: 15 * 60]; // 15 minutes
39 if ([alarm isRepeating]) {
40 [intervalField setStringValue:
41 [NSString stringWithFormat: @"every %@", [[alarm intervalString] lowercaseString]]];
42 [self updateNextDateDisplay: nil];
43 updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateNextDateDisplay:) userInfo: nil repeats: YES];
44 frameRect.size = [window maxSize];
45 } else {
46 frameRect.size = [window minSize];
47 }
48 [window setFrame: frameRect display: NO];
49 [window center];
50 [window makeKeyAndOrderFront: nil];
51 [window orderFrontRegardless];
52 }
53 return self;
54}
55
56- (void)dealloc;
57{
58 [alarm release];
59 [updateTimer invalidate]; updateTimer = nil;
60 [super dealloc];
61}
62
63- (void)updateNextDateDisplay:(id)sender;
64{
65 if (!canSnooze) {
66 NSString *nextDateTimeString = [alarm nextDateTimeString];
67 if (nextDateTimeString == nil) { // no longer repeating
68 [updateTimer invalidate]; updateTimer = nil;
69 } else {
70 [nextDateField setStringValue: nextDateTimeString];
71 }
72 }
73}
74
75- (void)update:(id)sender;
76{
77 canSnooze = [snoozeIntervalField interval] != 0;
78 if (canSnooze) [nextDateField setStringValue: @"after snooze"];
79 [snoozeButton setEnabled: canSnooze];
80 [canSnooze ? snoozeButton : okButton setKeyEquivalent: @"\r"];
81 [canSnooze ? okButton : snoozeButton setKeyEquivalent: @""];
82}
83
84- (IBAction)close:(id)sender;
85{
86 [PSAlarmAlertController stopAlerts: sender];
87 [self close];
88 [[PSNotifierAlert alert] completedForAlarm: alarm];
89}
90
91- (IBAction)snooze:(NSButton *)sender;
92{
93 NSTimeInterval snoozeInterval = [snoozeIntervalField interval];
94 [alarm setSnoozeInterval: snoozeInterval];
95 [[NSUserDefaults standardUserDefaults] setObject: [NSNumber numberWithDouble: snoozeInterval] forKey: PSAlarmSnoozeInterval];
96 [self close: sender];
97}
98
99- (IBAction)stopRepeating:(NSButton *)sender;
100{
101 NSWindow *window = [self window];
102 NSRect frameRect = [window frame];
103 NSSize newSize = [window minSize];
104
105 [alarm setRepeating: NO];
106 [sender setEnabled: NO];
107 frameRect.origin.y += frameRect.size.height - newSize.height;
108 frameRect.size = newSize;
109 [window setFrame: frameRect display: YES animate: YES];
110}
111
112@end
113
114@implementation PSAlarmNotifierController (NSControlSubclassDelegate)
115
116- (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error;
117{
118 if (control == snoozeIntervalField)
119 [snoozeIntervalField handleDidFailToFormatString: string errorDescription: error label: @"snooze interval"];
120 return NO;
121}
122
123- (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
124{
125 // NSLog(@"UPDATING FROM validation");
126 [self update: control]; // switch to snooze if someone types something weird...
127}
128
129- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector;
130{
131 // NSLog(@"UPDATING from textView: %@", NSStringFromSelector(commandSelector));
132 if (commandSelector == @selector(cancel:)) {
133 // if someone just wants the stupid thing to go away and presses escape, donÕt hinder them
134 [self close: control];
135 return YES;
136 }
137 // if someone invokes the default button or switches fields, donÕt override it
138 if (commandSelector == @selector(insertNewline:) ||
139 commandSelector == @selector(insertTab:) ||
140 commandSelector == @selector(insertBacktab:)) return NO;
141 [self update: control]; // ...or if they type a navigation key...
142 return NO; // we donÕt handle it
143}
144
145@end
146
147@implementation PSAlarmNotifierController (NSControlSubclassNotifications)
148
149- (void)controlTextDidChange:(NSNotification *)notification;
150{
151 // NSLog(@"UPDATING FROM controlTextDidChange: %@", [notification object]);
152 [self update: [notification object]]; // ...or if they modify the snooze interval
153}
154
155@end
156
157@implementation PSAlarmNotifierController (NSWindowNotifications)
158
159- (void)windowWillClose:(NSNotification *)notification;
160{
161 // canÕt rely on dealloc to invalidate the timer, because it retains this object
162 [updateTimer invalidate]; updateTimer = nil;
163 [self release]; // in non-document-based apps, this is needed; see docs
164}
165
166@end
Note: See TracBrowser for help on using the repository browser.