1 | //
|
---|
2 | // PSAlarmsController.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Fri Oct 11 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSAlarmsController.h"
|
---|
10 | #import "PSAlarm.h"
|
---|
11 | #import "PSAlerts.h"
|
---|
12 | #import "NSString-NJRExtensions.h"
|
---|
13 | #import "NSTableView-NJRExtensions.h"
|
---|
14 | #import "NJRTableView.h"
|
---|
15 | #import "NJRTableDelegate.h"
|
---|
16 |
|
---|
17 | @implementation PSAlarmsController
|
---|
18 |
|
---|
19 | - (void)alarmsChanged;
|
---|
20 | {
|
---|
21 | reorderedAlarms = [[alarmList delegate] reorderedDataForData: [alarms alarms]];
|
---|
22 | }
|
---|
23 |
|
---|
24 | - (id)init;
|
---|
25 | {
|
---|
26 | if ( (self = [super initWithWindowNibName: @"Alarms"]) != nil) {
|
---|
27 | alarms = [PSAlarms allAlarms];
|
---|
28 | // XXX workaround for bug in 10.2.1, 10.1.5: autosave name set in IB doesn't show up
|
---|
29 | [self setWindowFrameAutosaveName: @"Pester alarm list"];
|
---|
30 | // Apple documents the NSUserDefaults key, so we can rely on it hopefully.
|
---|
31 | if (nil == [[NSUserDefaults standardUserDefaults] objectForKey:
|
---|
32 | [@"NSWindow Frame " stringByAppendingString: [[self window] frameAutosaveName]]])
|
---|
33 | {
|
---|
34 | [[self window] center];
|
---|
35 | }
|
---|
36 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(alarmsChanged) name: PSAlarmsDidChangeNotification object: alarms];
|
---|
37 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(tableViewSelectionDidChange:) name: NSTableViewSelectionDidChangeNotification object: alarmList];
|
---|
38 | messageAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: [[[alarmList tableColumnWithIdentifier: @"message"] dataCell] font], NSFontAttributeName, nil];
|
---|
39 | [alarmList setAutosaveName: @"Alarm list"];
|
---|
40 | [alarmList setAutosaveTableColumns: YES];
|
---|
41 | [self alarmsChanged];
|
---|
42 | [[self window] makeFirstResponder: alarmList];
|
---|
43 | [[self window] setResizeIncrements: NSMakeSize(1, [alarmList cellHeight])];
|
---|
44 | }
|
---|
45 | return self;
|
---|
46 | }
|
---|
47 |
|
---|
48 | - (void)dealloc;
|
---|
49 | {
|
---|
50 | [reorderedAlarms release];
|
---|
51 | [messageAttributes release];
|
---|
52 | [super dealloc];
|
---|
53 | }
|
---|
54 |
|
---|
55 | - (IBAction)remove:(id)sender;
|
---|
56 | {
|
---|
57 | [alarms removeAlarms: [[alarmList delegate] selectedItems]];
|
---|
58 | }
|
---|
59 |
|
---|
60 | @end
|
---|
61 |
|
---|
62 | @implementation PSAlarmsController (NSTableDataSource)
|
---|
63 |
|
---|
64 | - (int)numberOfRowsInTableView:(NSTableView *)tableView;
|
---|
65 | {
|
---|
66 | return [alarms alarmCount];
|
---|
67 | }
|
---|
68 |
|
---|
69 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
|
---|
70 | {
|
---|
71 | PSAlarm *alarm = [reorderedAlarms objectAtIndex: row];
|
---|
72 |
|
---|
73 | if ([[tableColumn identifier] isEqualToString: @"message"]) {
|
---|
74 | NSMutableString *message = [[alarm message] mutableCopy];
|
---|
75 | [message truncateToWidth: [tableView frameOfCellAtColumn: 0 row: row].size.width by: NSLineBreakByTruncatingTail withAttributes: messageAttributes];
|
---|
76 | return [message autorelease];
|
---|
77 | } else {
|
---|
78 | NSCalendarDate *date = [alarm date];
|
---|
79 | if ([[tableColumn identifier] isEqualToString: @"date"]) return [alarm shortDateString];
|
---|
80 | if ([[tableColumn identifier] isEqualToString: @"time"]) {
|
---|
81 | if (date == nil) return @"ÇexpiredÈ";
|
---|
82 | return [alarm timeString];
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return nil;
|
---|
86 | }
|
---|
87 | @end
|
---|
88 |
|
---|
89 | @implementation PSAlarmsController (NJRTableViewDataSource)
|
---|
90 |
|
---|
91 | - (void)removeSelectedRowsFromTableView:(NSTableView *)aTableView;
|
---|
92 | {
|
---|
93 | [self remove: aTableView];
|
---|
94 | }
|
---|
95 |
|
---|
96 | - (NSString *)tableView:(NSTableView *)aTableView toolTipForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
|
---|
97 | {
|
---|
98 | PSAlarm *alarm = [reorderedAlarms objectAtIndex: rowIndex];
|
---|
99 |
|
---|
100 | return [[alarm prettyDescription] string];
|
---|
101 | }
|
---|
102 |
|
---|
103 | @end
|
---|
104 |
|
---|
105 | @implementation PSAlarmsController (NSTableViewNotifications)
|
---|
106 |
|
---|
107 | - (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
|
---|
108 | {
|
---|
109 | [removeButton setEnabled: ([alarmList numberOfSelectedRows] != 0)];
|
---|
110 | }
|
---|
111 |
|
---|
112 | @end
|
---|
113 |
|
---|
114 | @implementation PSAlarmsController (NSWindowDelegate)
|
---|
115 |
|
---|
116 | // XXX workaround for bug in 10.1.5, 10.2.1 (and earlier?): no autosave on window move
|
---|
117 | - (void)windowDidMove:(NSNotification *)aNotification
|
---|
118 | {
|
---|
119 | NSString *autosaveName = [[self window] frameAutosaveName];
|
---|
120 | // on initial display, we get a notification inside -[NSWindow setFrameAutosaveName]!
|
---|
121 | if (autosaveName != nil) {
|
---|
122 | [[self window] saveFrameUsingName: autosaveName];
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
|
---|
127 | {
|
---|
128 | NSWindow *window = [alarmList window];
|
---|
129 | NSRect frame = [window frame];
|
---|
130 | NSScrollView *scrollView = [alarmList enclosingScrollView];
|
---|
131 | float displayedHeight = [[scrollView contentView] bounds].size.height;
|
---|
132 | float heightChange = [[scrollView documentView] bounds].size.height - displayedHeight;
|
---|
133 | float heightExcess;
|
---|
134 |
|
---|
135 | if (heightChange >= 0 && heightChange <= 1) {
|
---|
136 | // either the window is already optimal size, or it's too big
|
---|
137 | float rowHeight = [alarmList cellHeight];
|
---|
138 | heightChange = (rowHeight * [alarmList numberOfRows]) - displayedHeight;
|
---|
139 | }
|
---|
140 |
|
---|
141 | frame.size.height += heightChange;
|
---|
142 |
|
---|
143 | if ( (heightExcess = [window minSize].height - frame.size.height) > 1 ||
|
---|
144 | (heightExcess = [window maxSize].height - frame.size.height) < 1) {
|
---|
145 | heightChange += heightExcess;
|
---|
146 | frame.size.height += heightExcess;
|
---|
147 | }
|
---|
148 |
|
---|
149 | frame.origin.y -= heightChange;
|
---|
150 |
|
---|
151 | return frame;
|
---|
152 | }
|
---|
153 |
|
---|
154 | @end |
---|