1 |
/* DeskTopPref.m |
2 |
* |
3 |
* Copyright (C) 2003 Free Software Foundation, Inc. |
4 |
* |
5 |
* Author: Enrico Sersale <enrico@imago.ro> |
6 |
* Date: August 2001 |
7 |
* |
8 |
* This file is part of the GNUstep GWorkspace application |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
*/ |
24 |
|
25 |
|
26 |
#include <Foundation/Foundation.h> |
27 |
#include <AppKit/AppKit.h> |
28 |
#ifdef GNUSTEP |
29 |
#include "GWFunctions.h" |
30 |
#include "GWNotifications.h" |
31 |
#else |
32 |
#include <GWorkspace/GWFunctions.h> |
33 |
#include <GWorkspace/GWNotifications.h> |
34 |
#endif |
35 |
#include "DeskTopPref.h" |
36 |
#include "GWorkspace.h" |
37 |
#include "GNUstep.h" |
38 |
|
39 |
static NSString *nibName = @"DeskTopPref"; |
40 |
|
41 |
@implementation ColorView |
42 |
|
43 |
- (void)dealloc |
44 |
{ |
45 |
TEST_RELEASE (color); |
46 |
[super dealloc]; |
47 |
} |
48 |
|
49 |
- (id)init |
50 |
{ |
51 |
self = [super init]; |
52 |
color = nil; |
53 |
return self; |
54 |
} |
55 |
|
56 |
- (void)setColor:(NSColor *)c |
57 |
{ |
58 |
ASSIGN (color, c); |
59 |
} |
60 |
|
61 |
- (void)drawRect:(NSRect)rect |
62 |
{ |
63 |
NSRect bounds = [self bounds]; |
64 |
float x = rect.origin.x + 2; |
65 |
float y = rect.origin.y + 2; |
66 |
float w = rect.size.width - 4; |
67 |
float h = rect.size.height - 4; |
68 |
NSRect colorRect = NSMakeRect(x, y, w, h); |
69 |
|
70 |
[super drawRect: rect]; |
71 |
|
72 |
NSDrawGrayBezel(bounds, rect); |
73 |
|
74 |
if (color != nil) { |
75 |
[color set]; |
76 |
NSRectFill(colorRect); |
77 |
} |
78 |
} |
79 |
|
80 |
@end |
81 |
|
82 |
@implementation DeskTopPref |
83 |
|
84 |
- (void)dealloc |
85 |
{ |
86 |
TEST_RELEASE (prefbox); |
87 |
RELEASE (colorsView); |
88 |
TEST_RELEASE (color); |
89 |
[super dealloc]; |
90 |
} |
91 |
|
92 |
- (id)init |
93 |
{ |
94 |
self = [super init]; |
95 |
|
96 |
if (self) { |
97 |
if ([NSBundle loadNibNamed: nibName owner: self] == NO) { |
98 |
NSLog(@"failed to load %@!", nibName); |
99 |
} else { |
100 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
101 |
NSDictionary *desktopViewPrefs = [defaults dictionaryForKey: @"desktopviewprefs"]; |
102 |
NSString *imagePath = nil; |
103 |
|
104 |
RETAIN (prefbox); |
105 |
RELEASE (win); |
106 |
|
107 |
gw = [GWorkspace gworkspace]; |
108 |
|
109 |
deskactive = [defaults boolForKey: @"desktop"]; |
110 |
|
111 |
if (desktopViewPrefs != nil) { |
112 |
id dictEntry = [desktopViewPrefs objectForKey: @"backcolor"]; |
113 |
|
114 |
imagePath = [desktopViewPrefs objectForKey: @"imagepath"]; |
115 |
|
116 |
if(dictEntry == nil) { |
117 |
ASSIGN (color, [NSColor windowBackgroundColor]); |
118 |
} else { |
119 |
NSString *cs; |
120 |
|
121 |
cs = [dictEntry objectForKey: @"red"]; |
122 |
[redField setStringValue: cs]; |
123 |
r = [cs floatValue]; |
124 |
[redSlider setFloatValue: r]; |
125 |
cs = [dictEntry objectForKey: @"green"]; |
126 |
[greenField setStringValue: cs]; |
127 |
g = [cs floatValue]; |
128 |
[greenSlider setFloatValue: g]; |
129 |
cs = [dictEntry objectForKey: @"blue"]; |
130 |
[blueField setStringValue: cs]; |
131 |
b = [cs floatValue]; |
132 |
[blueSlider setFloatValue: b]; |
133 |
alpha = [[dictEntry objectForKey: @"alpha"] floatValue]; |
134 |
ASSIGN (color, [NSColor colorWithCalibratedRed: r green: g blue: b alpha: alpha]); |
135 |
} |
136 |
} else { |
137 |
imagePath = nil; |
138 |
ASSIGN (color, [NSColor windowBackgroundColor]); |
139 |
} |
140 |
|
141 |
colorsView = [[ColorView alloc] init]; |
142 |
[colorsView setFrame: NSMakeRect(0, 0, 64, 64)]; |
143 |
[colorsView setColor: color]; |
144 |
[colorsBox addSubview: colorsView]; |
145 |
|
146 |
if(imagePath != nil) { |
147 |
[setImageButt setTitle: NSLocalizedString(@"Unset Image", @"")]; |
148 |
[setImageButt setAction: @selector(unsetImage:)]; |
149 |
} else { |
150 |
[setImageButt setTitle: NSLocalizedString(@"Set Image", @"")]; |
151 |
[setImageButt setAction: @selector(chooseImage:)]; |
152 |
} |
153 |
|
154 |
/* Internationalization */ |
155 |
[controlsbox setTitle: NSLocalizedString(@"Desktop Color", @"")]; |
156 |
[setColorButt setTitle: NSLocalizedString(@"Set", @"")]; |
157 |
[redlabel setStringValue: NSLocalizedString(@"red", @"")]; |
158 |
[greenlabel setStringValue: NSLocalizedString(@"green", @"")]; |
159 |
[bluelabel setStringValue: NSLocalizedString(@"blue", @"")]; |
160 |
|
161 |
[(NSButton *)chooseDeskButt setState: deskactive]; |
162 |
[self setDeskState: chooseDeskButt]; |
163 |
} |
164 |
} |
165 |
|
166 |
return self; |
167 |
} |
168 |
|
169 |
- (NSView *)prefView |
170 |
{ |
171 |
return prefbox; |
172 |
} |
173 |
|
174 |
- (NSString *)prefName |
175 |
{ |
176 |
return NSLocalizedString(@"Desktop", @""); |
177 |
} |
178 |
|
179 |
- (IBAction)setDeskState:(id)sender |
180 |
{ |
181 |
if ([sender state] == NSOnState) { |
182 |
deskactive = YES; |
183 |
[chooseDeskButt setTitle: NSLocalizedString(@"Deactivate desktop", @"")]; |
184 |
[redSlider setEnabled: YES]; |
185 |
[greenSlider setEnabled: YES]; |
186 |
[blueSlider setEnabled: YES]; |
187 |
[colorsView setColor: color]; |
188 |
[colorsView setNeedsDisplay: YES]; |
189 |
[setColorButt setEnabled: YES]; |
190 |
[setImageButt setEnabled: YES]; |
191 |
} else { |
192 |
deskactive = NO; |
193 |
[chooseDeskButt setTitle: NSLocalizedString(@"Activate desktop", @"")]; |
194 |
[redSlider setEnabled: NO]; |
195 |
[greenSlider setEnabled: NO]; |
196 |
[blueSlider setEnabled: NO]; |
197 |
[colorsView setColor: [NSColor windowBackgroundColor]]; |
198 |
[colorsView setNeedsDisplay: YES]; |
199 |
[setColorButt setEnabled: NO]; |
200 |
[setImageButt setEnabled: NO]; |
201 |
} |
202 |
|
203 |
[gw showHideDesktop: deskactive]; |
204 |
} |
205 |
|
206 |
- (IBAction)makeColor:(id)sender |
207 |
{ |
208 |
if (sender == redSlider) { |
209 |
r = [sender floatValue]; |
210 |
[redField setStringValue: [NSString stringWithFormat: @"%.2f", r]]; |
211 |
} else if (sender == greenSlider) { |
212 |
g = [sender floatValue]; |
213 |
[greenField setStringValue: [NSString stringWithFormat: @"%.2f", g]]; |
214 |
} else if (sender == blueSlider) { |
215 |
b = [sender floatValue]; |
216 |
[blueField setStringValue: [NSString stringWithFormat: @"%.2f", b]]; |
217 |
} |
218 |
|
219 |
ASSIGN (color, [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1]); |
220 |
[colorsView setColor: color]; |
221 |
[colorsView setNeedsDisplay: YES]; |
222 |
} |
223 |
|
224 |
- (IBAction)setColor:(id)sender |
225 |
{ |
226 |
NSUserDefaults *defaults; |
227 |
NSMutableDictionary *desktopViewPrefs, *colorDict; |
228 |
id dictEntry; |
229 |
|
230 |
defaults = [NSUserDefaults standardUserDefaults]; |
231 |
|
232 |
dictEntry = [defaults dictionaryForKey: @"desktopviewprefs"]; |
233 |
if (dictEntry == nil) { |
234 |
desktopViewPrefs = [[NSMutableDictionary alloc] initWithCapacity: 1]; |
235 |
} else { |
236 |
desktopViewPrefs = [dictEntry mutableCopy]; |
237 |
} |
238 |
|
239 |
colorDict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
240 |
[colorDict setObject: [NSString stringWithFormat: @"%f", r] forKey: @"red"]; |
241 |
[colorDict setObject: [NSString stringWithFormat: @"%f", g] forKey: @"green"]; |
242 |
[colorDict setObject: [NSString stringWithFormat: @"%f", b] forKey: @"blue"]; |
243 |
[colorDict setObject: @"1.0" forKey: @"alpha"]; |
244 |
|
245 |
[desktopViewPrefs setObject: colorDict forKey: @"backcolor"]; |
246 |
[defaults setObject: desktopViewPrefs forKey: @"desktopviewprefs"]; |
247 |
[defaults synchronize]; |
248 |
RELEASE (desktopViewPrefs); |
249 |
|
250 |
[[NSNotificationCenter defaultCenter] |
251 |
postNotificationName: GWDesktopViewColorChangedNotification |
252 |
object: colorDict]; |
253 |
} |
254 |
|
255 |
- (IBAction)chooseImage:(id)sender |
256 |
{ |
257 |
NSOpenPanel *openPanel; |
258 |
NSArray *fileTypes; |
259 |
NSString *imagePath; |
260 |
int result; |
261 |
NSUserDefaults *defaults; |
262 |
NSMutableDictionary *desktopViewPrefs; |
263 |
id dictEntry; |
264 |
|
265 |
fileTypes = [NSArray arrayWithObjects: @"tiff", @"tif", @"TIFF", @"TIFF", @"jpeg", @"jpg", @"JPEG", @"JPG", nil]; |
266 |
|
267 |
openPanel = [NSOpenPanel openPanel]; |
268 |
[openPanel setTitle: @"open"]; |
269 |
[openPanel setAllowsMultipleSelection: NO]; |
270 |
[openPanel setCanChooseFiles: YES]; |
271 |
[openPanel setCanChooseDirectories: NO]; |
272 |
|
273 |
result = [openPanel runModalForDirectory: NSHomeDirectory() file: nil types: fileTypes]; |
274 |
if(result != NSOKButton) { |
275 |
return; |
276 |
} |
277 |
|
278 |
imagePath = [NSString stringWithString: [openPanel filename]]; |
279 |
[setImageButt setTitle: @"Unset Image"]; |
280 |
[setImageButt setAction: @selector(unsetImage:)]; |
281 |
[setImageButt setNeedsDisplay: YES]; |
282 |
|
283 |
defaults = [NSUserDefaults standardUserDefaults]; |
284 |
dictEntry = [defaults dictionaryForKey: @"desktopviewprefs"]; |
285 |
if (dictEntry == nil) { |
286 |
desktopViewPrefs = [[NSMutableDictionary alloc] initWithCapacity: 1]; |
287 |
} else { |
288 |
desktopViewPrefs = [dictEntry mutableCopy]; |
289 |
} |
290 |
[desktopViewPrefs setObject: imagePath forKey: @"imagepath"]; |
291 |
[defaults setObject: desktopViewPrefs forKey: @"desktopviewprefs"]; |
292 |
[defaults synchronize]; |
293 |
RELEASE (desktopViewPrefs); |
294 |
|
295 |
[[NSNotificationCenter defaultCenter] |
296 |
postNotificationName: GWDesktopViewImageChangedNotification |
297 |
object: imagePath]; |
298 |
} |
299 |
|
300 |
- (IBAction)unsetImage:(id)sender |
301 |
{ |
302 |
NSUserDefaults *defaults; |
303 |
NSMutableDictionary *desktopViewPrefs; |
304 |
NSString *imagePath; |
305 |
id dictEntry; |
306 |
|
307 |
defaults = [NSUserDefaults standardUserDefaults]; |
308 |
|
309 |
dictEntry = [defaults dictionaryForKey: @"desktopviewprefs"]; |
310 |
if (dictEntry != nil) { |
311 |
desktopViewPrefs = [dictEntry mutableCopy]; |
312 |
imagePath = [desktopViewPrefs objectForKey: @"imagepath"]; |
313 |
if(imagePath != nil) { |
314 |
[desktopViewPrefs removeObjectForKey: @"imagepath"]; |
315 |
[defaults setObject: desktopViewPrefs forKey: @"desktopviewprefs"]; |
316 |
[defaults synchronize]; |
317 |
} |
318 |
RELEASE (desktopViewPrefs); |
319 |
} |
320 |
|
321 |
[setImageButt setTitle: @"Set Image"]; |
322 |
[setImageButt setAction: @selector(chooseImage:)]; |
323 |
[setImageButt setNeedsDisplay: YES]; |
324 |
|
325 |
[[NSNotificationCenter defaultCenter] |
326 |
postNotificationName: GWDesktopViewUnsetImageNotification |
327 |
object: nil]; |
328 |
} |
329 |
|
330 |
@end |