got it working. Looks like the data is what we expect.
[cpuHistory.git] / Preferences.m
blobc0b9917301e527392519fd18d2fbc09fbd1685e0
1 /*
2  *      Memory Monitor
3  *
4  *      Copyright © 2001-2003 Bernhard Baehr
5  *
6  *      Preferences.m - Preferences Controller Class
7  *
8  *      This program is free software; you can redistribute it and/or modify
9  *      it under the terms of the GNU General Public License as published by
10  *      the Free Software Foundation; either version 2 of the License, or
11  *      (at your option) any later version.
12  *
13  *      This program is distributed in the hope that it will be useful,
14  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *      GNU General Public License for more details.
17  *
18  *      You should have received a copy of the GNU General Public License
19  *      along with this program; if not, write to the Free Software
20  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
24 #import "Preferences.h"
27 @implementation Preferences
30 + (NSMutableDictionary *)defaultPreferences
32         return ([[NSMutableDictionary alloc] initWithObjectsAndKeys:
33                 [[NSColor yellowColor] colorWithAlphaComponent:0.8], WIRED_COLOR_KEY,
34                 [[NSColor magentaColor] colorWithAlphaComponent:0.8], ACTIVE_COLOR_KEY,
35                 [[NSColor cyanColor] colorWithAlphaComponent:0.8], INACTIVE_COLOR_KEY,
36                 [[NSColor blueColor] colorWithAlphaComponent:0.8], FREE_COLOR_KEY,
37                 [NSColor whiteColor], PAGEIN_COLOR_KEY,
38                 [NSColor blackColor], PAGEOUT_COLOR_KEY,
39                 [NSNumber numberWithInt:10], UPDATE_FREQUENCY_KEY,      /* unit is 1/10 second */
40                 [NSNumber numberWithInt:250], PAGING_SCALE_MAX_KEY,
41                 [NSNumber numberWithBool:YES], PAGEIN_ATOP_PAGEOUT_KEY,
42                 [NSNumber numberWithBool:YES], SHOW_PAGING_RATE_KEY,
43                 [NSNumber numberWithBool:NO], SHOW_GRAPH_WINDOW_KEY,
44                 [NSNumber numberWithBool:NO], GRAPH_WINDOW_ON_TOP_KEY,
45                 [NSNumber numberWithInt:128], GRAPH_WINDOW_SIZE_KEY,
46                 [NSNumber numberWithFloat:1.0], DOCK_ICON_SIZE_KEY,
47                 nil]);
51 - (id)init
53 #define SCANCOLOR(key)  \
54         if ((obj = [defaults objectForKey:key])) { \
55                 a = transparency; \
56                 sscanf ([obj cString], "%f %f %f %f", &r, &g, &b, &a); \
57                 obj = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a]; \
58                 [currentSettings setObject:obj forKey:key]; \
59         }
60 #define GETNUMBER(key)  \
61         if ((obj = [defaults objectForKey:key])) \
62                 [currentSettings setObject:obj forKey:key];
64         id      obj;
65         float   r, g, b, a, transparency;
67         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
68         
69         self = [super init];
70         currentSettings = [Preferences defaultPreferences];
71         GETNUMBER (OLD_TRANSPARENCY_KEY);
72         transparency = obj ? [obj floatValue] : 0.8;    /* global transparency setting of version 1.1 */
73         SCANCOLOR (WIRED_COLOR_KEY);
74         SCANCOLOR (ACTIVE_COLOR_KEY);
75         SCANCOLOR (INACTIVE_COLOR_KEY);
76         SCANCOLOR (FREE_COLOR_KEY);
77         transparency = 1.0;                     /* paging was drawn without transparency in version 1.1 */
78         SCANCOLOR (PAGEIN_COLOR_KEY);
79         SCANCOLOR (PAGEOUT_COLOR_KEY);
80         GETNUMBER (UPDATE_FREQUENCY_KEY);
81         GETNUMBER (PAGING_SCALE_MAX_KEY);
82         GETNUMBER (PAGEIN_ATOP_PAGEOUT_KEY);
83         GETNUMBER (SHOW_PAGING_RATE_KEY);
84         GETNUMBER (SHOW_GRAPH_WINDOW_KEY);
85         GETNUMBER (GRAPH_WINDOW_ON_TOP_KEY);
86         GETNUMBER (GRAPH_WINDOW_SIZE_KEY);
87         GETNUMBER (DOCK_ICON_SIZE_KEY);
88         [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
89         return (self);
93 - (void)showUpdateFrequency:(int)freq
95         [updateFrequency setStringValue:[NSString
96                 localizedStringWithFormat:NSLocalizedString(@"every\n%.1f sec.", @""), freq / 10.0]];
100 - (void)adjustGraphWindowControls
101 {       
102         id      obj;
103         
104         BOOL enabled = [[currentSettings objectForKey:SHOW_GRAPH_WINDOW_KEY] boolValue];
105         NSColor *color = enabled ? [NSColor controlTextColor] : [NSColor controlHighlightColor];
106         NSEnumerator *enumerator = [[graphWindowOptionsView subviews] objectEnumerator];
107         
108         while ((obj = [enumerator nextObject])) {
109                 if ([obj isMemberOfClass:[NSTextField class]])
110                         [obj setTextColor:color];
111                 else
112                         [obj setEnabled:enabled];
113         }
117 - (IBAction)showPreferences:(id)sender
119         double  freq;
120         
121         if (! panel) {
122                 if ([NSBundle loadNibNamed:@"Preferences" owner:self])
123                         [panel center];
124                 else {
125                         NSLog (@"Failed to load Preferences.nib");
126                         return;
127                 }
128         }
129         [wiredColor setColor:[currentSettings objectForKey:WIRED_COLOR_KEY]];
130         [activeColor setColor:[currentSettings objectForKey:ACTIVE_COLOR_KEY]];
131         [inactiveColor setColor:[currentSettings objectForKey:INACTIVE_COLOR_KEY]];
132         [freeColor setColor:[currentSettings objectForKey:FREE_COLOR_KEY]];
133         [pageinColor setColor:[currentSettings objectForKey:PAGEIN_COLOR_KEY]];
134         [pageoutColor setColor:[currentSettings objectForKey:PAGEOUT_COLOR_KEY]];
135         freq = [[currentSettings objectForKey:UPDATE_FREQUENCY_KEY] floatValue];
136         [self showUpdateFrequency:(int)freq];
137         [updateFrequencySlider setFloatValue:1000.0 * log(freq)];
138         [pagingScale selectItemAtIndex:[pagingScale
139                 indexOfItemWithTag:[[currentSettings objectForKey:PAGING_SCALE_MAX_KEY] intValue]]];
140         [pageinAtopPageout selectCellWithTag:[[currentSettings objectForKey:PAGEIN_ATOP_PAGEOUT_KEY] intValue]];
141         [showPagingRate setState:[[currentSettings objectForKey:SHOW_PAGING_RATE_KEY] boolValue]];
142         [showGraphWindow setState:[[currentSettings objectForKey:SHOW_GRAPH_WINDOW_KEY] boolValue]];
143         [graphWindowOnTop setState:[[currentSettings objectForKey:GRAPH_WINDOW_ON_TOP_KEY] boolValue]];
144         [graphWindowSize setFloatValue:[[currentSettings objectForKey:GRAPH_WINDOW_SIZE_KEY] floatValue]];
145         [dockIconSizeSlider setFloatValue:[[currentSettings objectForKey:DOCK_ICON_SIZE_KEY] floatValue]];
146         [self adjustGraphWindowControls];
147         [panel makeKeyAndOrderFront:nil];
151 - (IBAction)revertToDefaults:(id)sender
153         [currentSettings release];
154         currentSettings = [Preferences defaultPreferences];
155         [self showPreferences:sender];
156         [[NSNotificationCenter defaultCenter] postNotificationName:PREFERENCES_CHANGED object:nil];
160 - (void)savePreferences
162         id              obj;
163         float           r, g, b, a;
164         NSString        *key;
165         
166         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
167         NSEnumerator *enumerator = [currentSettings keyEnumerator];
169         while ((key = [enumerator nextObject])) {
170                 obj = [currentSettings objectForKey:key];
171                 if ([obj isKindOfClass:[NSColor class]]) {
172                         [[obj colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"]
173                                 getRed:&r green:&g blue:&b alpha:&a];
174                         [obj release];
175                         obj = [NSString stringWithFormat:@"%f %f %f %f", r, g, b, a];
176                 }
177                 [defaults setObject:obj forKey:key];
178         }
182 - (id)objectForKey:(id)key
184         return ([currentSettings objectForKey:key]);
188 - (IBAction)preferencesChanged:(id)sender
190         int     freq;
191         
192         [currentSettings setObject:[wiredColor color] forKey:WIRED_COLOR_KEY];
193         [currentSettings setObject:[activeColor color] forKey:ACTIVE_COLOR_KEY];
194         [currentSettings setObject:[inactiveColor color] forKey:INACTIVE_COLOR_KEY];
195         [currentSettings setObject:[freeColor color] forKey:FREE_COLOR_KEY];
196         [currentSettings setObject:[pageinColor color] forKey:PAGEIN_COLOR_KEY];
197         [currentSettings setObject:[pageoutColor color] forKey:PAGEOUT_COLOR_KEY];
198         freq = exp([updateFrequencySlider doubleValue] / 1000.0);       /* 1..600 == 0.1 sec. to 1 min. */
199         [self showUpdateFrequency:freq];
200         [currentSettings setObject:[NSNumber numberWithInt:freq] forKey:UPDATE_FREQUENCY_KEY];
201         [currentSettings setObject:[NSNumber numberWithInt:[[pagingScale selectedItem] tag]] forKey:PAGING_SCALE_MAX_KEY];
202         [currentSettings setObject:[NSNumber numberWithInt:[[pageinAtopPageout selectedCell] tag]] forKey:PAGEIN_ATOP_PAGEOUT_KEY];
203         [currentSettings setObject:[NSNumber numberWithInt:[showPagingRate state]] forKey:SHOW_PAGING_RATE_KEY];
204         [currentSettings setObject:[NSNumber numberWithInt:[showGraphWindow state]] forKey:SHOW_GRAPH_WINDOW_KEY];
205         [currentSettings setObject:[NSNumber numberWithInt:[graphWindowOnTop state]] forKey:GRAPH_WINDOW_ON_TOP_KEY];
206         [currentSettings setObject:[NSNumber numberWithInt:[graphWindowSize intValue]] forKey:GRAPH_WINDOW_SIZE_KEY];
207         [currentSettings setObject:[NSNumber numberWithFloat:[dockIconSizeSlider floatValue]] forKey:DOCK_ICON_SIZE_KEY];
208         [self adjustGraphWindowControls];
209         [[NSNotificationCenter defaultCenter] postNotificationName:PREFERENCES_CHANGED object:nil];
213 - (int)windowNumber
215         return (panel ? [panel windowNumber] : 0);
219 @end