3 * Christopher Bowns, 2008
5 * Formerly: Memory Monitor, by Bernhard Baehr
7 * Copyright © 2001-2003 Bernhard Baehr
9 * Preferences.m - Preferences Controller Class
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #import "Preferences.h"
30 @implementation Preferences
33 + (NSMutableDictionary *)defaultPreferences
35 return ([[NSMutableDictionary alloc] initWithObjectsAndKeys:
36 /* [[NSColor yellowColor] colorWithAlphaComponent:0.8], WIRED_COLOR_KEY,
37 [[NSColor magentaColor] colorWithAlphaComponent:0.8], ACTIVE_COLOR_KEY,
38 [[NSColor cyanColor] colorWithAlphaComponent:0.8], INACTIVE_COLOR_KEY,
39 [[NSColor blueColor] colorWithAlphaComponent:0.8], FREE_COLOR_KEY,
41 [NSColor whiteColor], PAGEIN_COLOR_KEY,
42 [NSColor blackColor], PAGEOUT_COLOR_KEY,
43 [NSNumber numberWithInt:250], PAGING_SCALE_MAX_KEY,
44 [NSNumber numberWithBool:YES], PAGEIN_ATOP_PAGEOUT_KEY,
45 [NSNumber numberWithBool:YES], SHOW_PAGING_RATE_KEY,
48 // Colors taken from samples of 10.5's Activity Monitor
49 [NSColor colorWithCalibratedRed:0.304875 green:0.931411 blue:0.294072 alpha:1.0 ], USER_COLOR_KEY,
50 [NSColor colorWithCalibratedRed:0.933211 green:0.219913 blue:0.200565 alpha:1.0 ], SYS_COLOR_KEY,
51 [NSColor colorWithCalibratedRed:0.200638 green:0.000533 blue:1.0 alpha:1.0 ], NICE_COLOR_KEY,
52 [[NSColor blackColor] colorWithAlphaComponent:1.0], IDLE_COLOR_KEY,
55 [NSNumber numberWithInt:10], UPDATE_FREQUENCY_KEY, /* unit is 1/10 second */
56 [NSNumber numberWithBool:NO], SHOW_GRAPH_WINDOW_KEY,
57 [NSNumber numberWithBool:NO], GRAPH_WINDOW_ON_TOP_KEY,
58 [NSNumber numberWithInt:128], GRAPH_WINDOW_SIZE_KEY,
59 [NSNumber numberWithFloat:1.0], DOCK_ICON_SIZE_KEY,
60 [NSNumber numberWithInt:4], BAR_WIDTH_SIZE_KEY,
67 #define SCANCOLOR(key) \
68 if ((obj = [defaults objectForKey:key])) { \
70 sscanf ([obj cString], "%f %f %f %f", &r, &g, &b, &a); \
71 obj = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a]; \
72 [currentSettings setObject:obj forKey:key]; \
74 #define GETNUMBER(key) \
75 if ((obj = [defaults objectForKey:key])) \
76 [currentSettings setObject:obj forKey:key];
79 float r, g, b, a, transparency = 0.0;
81 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
84 currentSettings = [Preferences defaultPreferences];
85 /* SCANCOLOR (WIRED_COLOR_KEY);
86 SCANCOLOR (ACTIVE_COLOR_KEY);
87 SCANCOLOR (INACTIVE_COLOR_KEY);
88 SCANCOLOR (FREE_COLOR_KEY);
89 SCANCOLOR (PAGEIN_COLOR_KEY);
90 SCANCOLOR (PAGEOUT_COLOR_KEY);
91 GETNUMBER (PAGING_SCALE_MAX_KEY);
92 GETNUMBER (PAGEIN_ATOP_PAGEOUT_KEY);
93 GETNUMBER (SHOW_PAGING_RATE_KEY);
95 // transparency = 1.0; /* paging was drawn without transparency in version 1.1 */
96 // GETNUMBER (OLD_TRANSPARENCY_KEY);
98 SCANCOLOR (USER_COLOR_KEY);
99 SCANCOLOR (SYS_COLOR_KEY);
100 SCANCOLOR (NICE_COLOR_KEY);
101 SCANCOLOR (IDLE_COLOR_KEY);
102 GETNUMBER (UPDATE_FREQUENCY_KEY);
103 GETNUMBER (SHOW_GRAPH_WINDOW_KEY);
104 GETNUMBER (GRAPH_WINDOW_ON_TOP_KEY);
105 GETNUMBER (GRAPH_WINDOW_SIZE_KEY);
106 GETNUMBER (DOCK_ICON_SIZE_KEY);
107 GETNUMBER (BAR_WIDTH_SIZE_KEY);
108 transparency = obj ? [obj floatValue] : 0.8; /* global transparency setting of version 1.1 */
109 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
114 - (void)showUpdateFrequency:(int)freq
116 [updateFrequency setStringValue:[NSString
117 localizedStringWithFormat:NSLocalizedString(@"every\n%.1f sec.", @""), freq / 10.0]];
121 - (void)adjustGraphWindowControls
125 BOOL enabled = [[currentSettings objectForKey:SHOW_GRAPH_WINDOW_KEY] boolValue];
126 NSColor *color = enabled ? [NSColor controlTextColor] : [NSColor controlHighlightColor];
127 NSEnumerator *enumerator = [[graphWindowOptionsView subviews] objectEnumerator];
129 while ((obj = [enumerator nextObject])) {
130 if ([obj isMemberOfClass:[NSTextField class]])
131 [obj setTextColor:color];
133 [obj setEnabled:enabled];
138 - (IBAction)showPreferences:(id)sender
143 if ([NSBundle loadNibNamed:@"Preferences" owner:self])
146 NSLog (@"Failed to load Preferences.nib");
151 /* [wiredColor setColor:[currentSettings objectForKey:WIRED_COLOR_KEY]];
152 [activeColor setColor:[currentSettings objectForKey:ACTIVE_COLOR_KEY]];
153 [inactiveColor setColor:[currentSettings objectForKey:INACTIVE_COLOR_KEY]];
154 [freeColor setColor:[currentSettings objectForKey:FREE_COLOR_KEY]];
155 [pageinColor setColor:[currentSettings objectForKey:PAGEIN_COLOR_KEY]];
156 [pageoutColor setColor:[currentSettings objectForKey:PAGEOUT_COLOR_KEY]];
157 [pagingScale selectItemAtIndex:[pagingScale
158 indexOfItemWithTag:[[currentSettings objectForKey:PAGING_SCALE_MAX_KEY] intValue]]];
159 [pageinAtopPageout selectCellWithTag:[[currentSettings objectForKey:PAGEIN_ATOP_PAGEOUT_KEY] intValue]];
160 [showPagingRate setState:[[currentSettings objectForKey:SHOW_PAGING_RATE_KEY] boolValue]];
163 [userColor setColor:[currentSettings objectForKey:USER_COLOR_KEY]];
164 [sysColor setColor:[currentSettings objectForKey:SYS_COLOR_KEY]];
165 [niceColor setColor:[currentSettings objectForKey:NICE_COLOR_KEY]];
166 [idleColor setColor:[currentSettings objectForKey:IDLE_COLOR_KEY]];
168 freq = [[currentSettings objectForKey:UPDATE_FREQUENCY_KEY] floatValue];
169 [self showUpdateFrequency:(int)freq];
170 [updateFrequencySlider setFloatValue:1000.0 * log(freq)];
171 [showGraphWindow setState:[[currentSettings objectForKey:SHOW_GRAPH_WINDOW_KEY] boolValue]];
172 [graphWindowOnTop setState:[[currentSettings objectForKey:GRAPH_WINDOW_ON_TOP_KEY] boolValue]];
173 [graphWindowSize setFloatValue:[[currentSettings objectForKey:GRAPH_WINDOW_SIZE_KEY] floatValue]];
174 [dockIconSizeSlider setFloatValue:[[currentSettings objectForKey:DOCK_ICON_SIZE_KEY] floatValue]];
175 [barWidthSlider setFloatValue:[[currentSettings objectForKey:BAR_WIDTH_SIZE_KEY] floatValue]];
176 [self adjustGraphWindowControls];
177 [panel makeKeyAndOrderFront:nil];
181 - (IBAction)revertToDefaults:(id)sender
183 [currentSettings release];
184 currentSettings = [Preferences defaultPreferences];
185 [self showPreferences:sender];
186 [[NSNotificationCenter defaultCenter] postNotificationName:PREFERENCES_CHANGED object:nil];
190 - (void)savePreferences
196 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
197 NSEnumerator *enumerator = [currentSettings keyEnumerator];
199 while ((key = [enumerator nextObject])) {
200 obj = [currentSettings objectForKey:key];
201 if ([obj isKindOfClass:[NSColor class]]) {
202 [[obj colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"]
203 getRed:&r green:&g blue:&b alpha:&a];
205 obj = [NSString stringWithFormat:@"%f %f %f %f", r, g, b, a];
207 [defaults setObject:obj forKey:key];
212 - (id)objectForKey:(id)key
214 return ([currentSettings objectForKey:key]);
218 - (IBAction)preferencesChanged:(id)sender
222 /* [currentSettings setObject:[wiredColor color] forKey:WIRED_COLOR_KEY];
223 [currentSettings setObject:[activeColor color] forKey:ACTIVE_COLOR_KEY];
224 [currentSettings setObject:[inactiveColor color] forKey:INACTIVE_COLOR_KEY];
225 [currentSettings setObject:[freeColor color] forKey:FREE_COLOR_KEY];
226 [currentSettings setObject:[pageinColor color] forKey:PAGEIN_COLOR_KEY];
227 [currentSettings setObject:[pageoutColor color] forKey:PAGEOUT_COLOR_KEY];
228 [currentSettings setObject:[NSNumber numberWithInt:[[pagingScale selectedItem] tag]] forKey:PAGING_SCALE_MAX_KEY];
229 [currentSettings setObject:[NSNumber numberWithInt:[[pageinAtopPageout selectedCell] tag]] forKey:PAGEIN_ATOP_PAGEOUT_KEY];
230 [currentSettings setObject:[NSNumber numberWithInt:[showPagingRate state]] forKey:SHOW_PAGING_RATE_KEY];
235 [currentSettings setObject:[userColor color] forKey:USER_COLOR_KEY];
236 [currentSettings setObject:[sysColor color] forKey:SYS_COLOR_KEY];
237 [currentSettings setObject:[niceColor color] forKey:NICE_COLOR_KEY];
238 [currentSettings setObject:[idleColor color] forKey:IDLE_COLOR_KEY];
240 freq = exp([updateFrequencySlider doubleValue] / 1000.0); /* 1..600 == 0.1 sec. to 1 min. */
241 [self showUpdateFrequency:freq];
242 [currentSettings setObject:[NSNumber numberWithInt:freq] forKey:UPDATE_FREQUENCY_KEY];
243 [currentSettings setObject:[NSNumber numberWithInt:[showGraphWindow state]] forKey:SHOW_GRAPH_WINDOW_KEY];
244 [currentSettings setObject:[NSNumber numberWithInt:[graphWindowOnTop state]] forKey:GRAPH_WINDOW_ON_TOP_KEY];
245 [currentSettings setObject:[NSNumber numberWithInt:[graphWindowSize intValue]] forKey:GRAPH_WINDOW_SIZE_KEY];
246 [currentSettings setObject:[NSNumber numberWithFloat:[dockIconSizeSlider floatValue]] forKey:DOCK_ICON_SIZE_KEY];
247 [currentSettings setObject:[NSNumber numberWithInt:[barWidthSlider intValue]] forKey:BAR_WIDTH_SIZE_KEY];
248 [self adjustGraphWindowControls];
249 [[NSNotificationCenter defaultCenter] postNotificationName:PREFERENCES_CHANGED object:nil];
255 return (panel ? [panel windowNumber] : 0);