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