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 "NSTableView-NJRExtensions.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | @implementation PSAlarmsController
|
---|
15 |
|
---|
16 | - (id)init;
|
---|
17 | {
|
---|
18 | if ( (self = [super initWithWindowNibName: @"Alarms"]) != nil) {
|
---|
19 | alarms = [PSAlarms allAlarms];
|
---|
20 | // XXX workaround for bug in 10.2.1, 10.1.5: autosave name set in IB doesn't show up
|
---|
21 | [self setWindowFrameAutosaveName: @"Pester alarm list"];
|
---|
22 | // Apple documents the NSUserDefaults key, so we can rely on it hopefully.
|
---|
23 | if (nil == [[NSUserDefaults standardUserDefaults] objectForKey:
|
---|
24 | [@"NSWindow Frame " stringByAppendingString: [[self window] frameAutosaveName]]])
|
---|
25 | {
|
---|
26 | [[self window] center];
|
---|
27 | }
|
---|
28 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(alarmsChanged) name: PSAlarmsDidChangeNotification object: alarms];
|
---|
29 | [tableView setAutosaveName: @"Alarm list"];
|
---|
30 | [tableView setAutosaveTableColumns: YES];
|
---|
31 | [tableView noteNumberOfRowsChanged];
|
---|
32 | [[self window] makeFirstResponder: tableView];
|
---|
33 | }
|
---|
34 | return self;
|
---|
35 | }
|
---|
36 |
|
---|
37 | - (void)alarmsChanged; // XXX fix autoselection to be more reasonable, see whatever I did in that _Learning Cocoa_ project I think
|
---|
38 | {
|
---|
39 | [tableView reloadData];
|
---|
40 | [tableView deselectAll: self];
|
---|
41 | }
|
---|
42 |
|
---|
43 | - (IBAction)remove:(id)sender;
|
---|
44 | {
|
---|
45 | [alarms removeAlarmsAtIndices: [[tableView selectedRowEnumerator] allObjects]];
|
---|
46 | }
|
---|
47 |
|
---|
48 | @end
|
---|
49 |
|
---|
50 | @implementation PSAlarmsController (NSTableDataSource)
|
---|
51 |
|
---|
52 | - (int)numberOfRowsInTableView:(NSTableView *)tableView;
|
---|
53 | {
|
---|
54 | return [alarms alarmCount];
|
---|
55 | }
|
---|
56 |
|
---|
57 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
|
---|
58 | {
|
---|
59 | PSAlarm *alarm = [alarms alarmAtIndex: row];
|
---|
60 |
|
---|
61 | if ([[tableColumn identifier] isEqualToString: @"message"]) return [alarm message];
|
---|
62 | else {
|
---|
63 | NSCalendarDate *date = [alarm date];
|
---|
64 | if ([[tableColumn identifier] isEqualToString: @"date"]) return [alarm shortDateString];
|
---|
65 | if ([[tableColumn identifier] isEqualToString: @"time"]) {
|
---|
66 | if (date == nil) return @"ÇexpiredÈ";
|
---|
67 | return [alarm timeString];
|
---|
68 | }
|
---|
69 | }
|
---|
70 | return nil;
|
---|
71 | }
|
---|
72 | @end
|
---|
73 |
|
---|
74 | @implementation PSAlarmsController (NSTableViewNotifications)
|
---|
75 |
|
---|
76 | - (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
|
---|
77 | {
|
---|
78 | [removeButton setEnabled: ([tableView numberOfSelectedRows] != 0)];
|
---|
79 | }
|
---|
80 |
|
---|
81 | @end
|
---|
82 |
|
---|
83 | @implementation PSAlarmsController (NSWindowDelegate)
|
---|
84 |
|
---|
85 | // XXX workaround for bug in 10.1.5, 10.2.1 (and earlier?): no autosave on window move
|
---|
86 | - (void)windowDidMove:(NSNotification *)aNotification
|
---|
87 | {
|
---|
88 | NSString *autosaveName = [[self window] frameAutosaveName];
|
---|
89 | // on initial display, we get a notification inside -[NSWindow setFrameAutosaveName]!
|
---|
90 | if (autosaveName != nil) {
|
---|
91 | [[self window] saveFrameUsingName: autosaveName];
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
|
---|
96 | {
|
---|
97 | NSWindow *window = [tableView window];
|
---|
98 | NSRect frame = [window frame];
|
---|
99 | NSScrollView *scrollView = [tableView enclosingScrollView];
|
---|
100 | float displayedHeight = [[scrollView contentView] bounds].size.height;
|
---|
101 | float heightChange = [[scrollView documentView] bounds].size.height - displayedHeight;
|
---|
102 | float heightExcess;
|
---|
103 |
|
---|
104 | if (heightChange >= 0 && heightChange <= 1) {
|
---|
105 | // either the window is already optimal size, or it's too big
|
---|
106 | float rowHeight = [tableView cellHeight];
|
---|
107 | heightChange = (rowHeight * [tableView numberOfRows]) - displayedHeight;
|
---|
108 | }
|
---|
109 |
|
---|
110 | frame.size.height += heightChange;
|
---|
111 |
|
---|
112 | if ( (heightExcess = [window minSize].height - frame.size.height) > 1 ||
|
---|
113 | (heightExcess = [window maxSize].height - frame.size.height) < 1) {
|
---|
114 | heightChange += heightExcess;
|
---|
115 | frame.size.height += heightExcess;
|
---|
116 | }
|
---|
117 |
|
---|
118 | frame.origin.y -= heightChange;
|
---|
119 |
|
---|
120 | return frame;
|
---|
121 | }
|
---|
122 |
|
---|
123 | @end
|
---|
124 |
|
---|