source: trunk/Cocoa/Pester/Source/PSAlarmsController.m@ 111

Last change on this file since 111 was 102, checked in by Nicholas Riley, 21 years ago

Pester 1.1b3

File size: 5.1 KB
RevLine 
[26]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"
[53]11#import "PSAlerts.h"
[102]12#import "NSString-NJRExtensions.h"
[26]13#import "NSTableView-NJRExtensions.h"
[51]14#import "NJRTableView.h"
15#import "NJRTableDelegate.h"
[26]16
17@implementation PSAlarmsController
18
[51]19- (void)alarmsChanged;
20{
[102]21 reorderedAlarms = [[alarmList delegate] reorderedDataForData: [alarms alarms]];
[51]22}
23
[26]24- (id)init;
25{
26 if ( (self = [super initWithWindowNibName: @"Alarms"]) != nil) {
27 alarms = [PSAlarms allAlarms];
[28]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]]])
[51]33 {
[28]34 [[self window] center];
[51]35 }
[26]36 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(alarmsChanged) name: PSAlarmsDidChangeNotification object: alarms];
[102]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];
[51]41 [self alarmsChanged];
[102]42 [[self window] makeFirstResponder: alarmList];
43 [[self window] setResizeIncrements: NSMakeSize(1, [alarmList cellHeight])];
[26]44 }
45 return self;
46}
47
[102]48- (void)dealloc;
49{
50 [reorderedAlarms release];
51 [messageAttributes release];
52 [super dealloc];
53}
[26]54
55- (IBAction)remove:(id)sender;
56{
[102]57 [alarms removeAlarms: [[alarmList delegate] selectedItems]];
[26]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{
[51]71 PSAlarm *alarm = [reorderedAlarms objectAtIndex: row];
[26]72
[102]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 {
[26]78 NSCalendarDate *date = [alarm date];
[28]79 if ([[tableColumn identifier] isEqualToString: @"date"]) return [alarm shortDateString];
[26]80 if ([[tableColumn identifier] isEqualToString: @"time"]) {
81 if (date == nil) return @"ÇexpiredÈ";
[28]82 return [alarm timeString];
[26]83 }
84 }
85 return nil;
86}
87@end
88
[51]89@implementation PSAlarmsController (NJRTableViewDataSource)
90
91- (void)removeSelectedRowsFromTableView:(NSTableView *)aTableView;
92{
93 [self remove: aTableView];
94}
95
[53]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
[51]103@end
104
[26]105@implementation PSAlarmsController (NSTableViewNotifications)
106
107- (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
108{
[102]109 [removeButton setEnabled: ([alarmList numberOfSelectedRows] != 0)];
[26]110}
111
112@end
113
114@implementation PSAlarmsController (NSWindowDelegate)
115
[28]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
[26]126- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
127{
[102]128 NSWindow *window = [alarmList window];
[26]129 NSRect frame = [window frame];
[102]130 NSScrollView *scrollView = [alarmList enclosingScrollView];
[26]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
[102]137 float rowHeight = [alarmList cellHeight];
138 heightChange = (rowHeight * [alarmList numberOfRows]) - displayedHeight;
[26]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
[34]154@end
Note: See TracBrowser for help on using the repository browser.