1 |
/* IconsViewerPref.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 "GWLib.h" |
30 |
#include "GWProtocol.h" |
31 |
#include "GWFunctions.h" |
32 |
#include "GWNotifications.h" |
33 |
#else |
34 |
#include <GWorkspace/GWLib.h> |
35 |
#include <GWorkspace/GWProtocol.h> |
36 |
#include <GWorkspace/GWFunctions.h> |
37 |
#include <GWorkspace/GWNotifications.h> |
38 |
#endif |
39 |
#include "IconsViewerPref.h" |
40 |
#include "GNUstep.h" |
41 |
|
42 |
#ifdef GNUSTEP |
43 |
#define BOX_W 197 |
44 |
#define NAME_OR_Y 5 |
45 |
#define NAME_W 16 |
46 |
#define NAME_MARGIN 6 |
47 |
#else |
48 |
#define BOX_W 197 |
49 |
#define NAME_OR_Y 5 |
50 |
#define NAME_W 16 |
51 |
#define NAME_MARGIN 6 |
52 |
#endif |
53 |
|
54 |
#define DEFAULT_ICONS_WIDTH 120 |
55 |
|
56 |
#ifndef max |
57 |
#define max(a,b) ((a) > (b) ? (a):(b)) |
58 |
#endif |
59 |
|
60 |
#ifndef min |
61 |
#define min(a,b) ((a) < (b) ? (a):(b)) |
62 |
#endif |
63 |
|
64 |
static NSString *nibName = @"IconsViewerPref"; |
65 |
|
66 |
@implementation LabelResizer |
67 |
|
68 |
- (void)dealloc |
69 |
{ |
70 |
RELEASE (arrow); |
71 |
[super dealloc]; |
72 |
} |
73 |
|
74 |
- (id)initForController:(id)acontroller |
75 |
withPosition:(ResizerPosition)pos |
76 |
{ |
77 |
self = [super init]; |
78 |
[self setFrame: NSMakeRect(0, 0, 16, 16)]; |
79 |
position = pos; |
80 |
controller = acontroller; |
81 |
|
82 |
if (position == leftarrow) { |
83 |
ASSIGN (arrow, [NSImage imageNamed: @"LeftArr.tiff"]); |
84 |
} else { |
85 |
ASSIGN (arrow, [NSImage imageNamed: @"RightArr.tiff"]); |
86 |
} |
87 |
|
88 |
return self; |
89 |
} |
90 |
|
91 |
- (ResizerPosition)position |
92 |
{ |
93 |
return position; |
94 |
} |
95 |
|
96 |
- (void)mouseDown:(NSEvent *)e |
97 |
{ |
98 |
[controller startMouseEvent: e onResizer: self]; |
99 |
} |
100 |
|
101 |
- (void)drawRect:(NSRect)rect |
102 |
{ |
103 |
[super drawRect: rect]; |
104 |
[arrow compositeToPoint: NSZeroPoint operation: NSCompositeSourceOver]; |
105 |
} |
106 |
|
107 |
@end |
108 |
|
109 |
|
110 |
@implementation IconsViewerPref |
111 |
|
112 |
- (void)dealloc |
113 |
{ |
114 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
115 |
TEST_RELEASE (prefbox); |
116 |
RELEASE (leftResizer); |
117 |
RELEASE (rightResizer); |
118 |
RELEASE (fname); |
119 |
[super dealloc]; |
120 |
} |
121 |
|
122 |
- (id)init |
123 |
{ |
124 |
self = [super init]; |
125 |
|
126 |
if (self) { |
127 |
if ([NSBundle loadNibNamed: nibName owner: self] == NO) { |
128 |
NSLog(@"failed to load %@!", nibName); |
129 |
} else { |
130 |
NSUserDefaults *defaults; |
131 |
NSString *widthStr; |
132 |
NSArray *selPaths; |
133 |
int orx, count; |
134 |
|
135 |
RETAIN (prefbox); |
136 |
RELEASE (win); |
137 |
|
138 |
ws = [NSWorkspace sharedWorkspace]; |
139 |
|
140 |
[imView setImageScaling: NSScaleProportionally]; |
141 |
|
142 |
selPaths = [[GWLib workspaceApp] getSelectedPaths]; |
143 |
count = [selPaths count]; |
144 |
if (count == 1) { |
145 |
NSString *fpath = [selPaths objectAtIndex: 0]; |
146 |
NSString *defApp; |
147 |
NSString *type; |
148 |
|
149 |
ASSIGN (fname, [fpath lastPathComponent]); |
150 |
|
151 |
[ws getInfoForFile: fpath application: &defApp type: &type]; |
152 |
[imView setImage: [GWLib iconForFile: fpath ofType: type]]; |
153 |
} else { |
154 |
fname = [[NSString alloc] initWithFormat: @"%i items", count]; |
155 |
[imView setImage: [NSImage imageNamed: @"MultipleSelection.tiff"]]; |
156 |
} |
157 |
|
158 |
defaults = [NSUserDefaults standardUserDefaults]; |
159 |
widthStr = [defaults objectForKey: @"iconsCellsWidth"]; |
160 |
|
161 |
if (widthStr) { |
162 |
cellsWidth = [widthStr intValue]; |
163 |
} else { |
164 |
cellsWidth = DEFAULT_ICONS_WIDTH; |
165 |
} |
166 |
|
167 |
orx = (int)((BOX_W - cellsWidth) / 2); |
168 |
|
169 |
leftResizer = [[LabelResizer alloc] initForController: self |
170 |
withPosition: leftarrow]; |
171 |
[leftResizer setFrame: NSMakeRect(0, 0, NAME_W, NAME_W)]; |
172 |
[(NSBox *)leftResBox setContentView: leftResizer]; |
173 |
[leftResBox setFrame: NSMakeRect(orx - NAME_W, NAME_OR_Y, NAME_W, NAME_W)]; |
174 |
|
175 |
rightResizer = [[LabelResizer alloc] initForController: self |
176 |
withPosition: rightarrow]; |
177 |
[rightResizer setFrame: NSMakeRect(0, 0, NAME_W, NAME_W)]; |
178 |
[(NSBox *)rightResBox setContentView: rightResizer]; |
179 |
[rightResBox setFrame: NSMakeRect(orx + cellsWidth, NAME_OR_Y, NAME_W, NAME_W)]; |
180 |
|
181 |
[nameField setFrame: NSMakeRect(orx, NAME_OR_Y, cellsWidth, NAME_W)]; |
182 |
[nameField setStringValue: cutFileLabelText(fname, nameField, cellsWidth -NAME_MARGIN)]; |
183 |
|
184 |
[[NSNotificationCenter defaultCenter] addObserver: self |
185 |
selector: @selector(selectionChanged:) |
186 |
name: GWCurrentSelectionChangedNotification |
187 |
object: nil]; |
188 |
|
189 |
/* Internationalization */ |
190 |
[setButt setTitle: NSLocalizedString(@"Use Default Settings", @"")]; |
191 |
[iconbox setTitle: NSLocalizedString(@"Title Width", @"")]; |
192 |
} |
193 |
} |
194 |
|
195 |
return self; |
196 |
} |
197 |
|
198 |
- (NSView *)prefView |
199 |
{ |
200 |
return prefbox; |
201 |
} |
202 |
|
203 |
- (NSString *)prefName |
204 |
{ |
205 |
return NSLocalizedString(@"Icon View", @""); |
206 |
} |
207 |
|
208 |
- (void)tile |
209 |
{ |
210 |
int orx = (int)((BOX_W - cellsWidth) / 2); |
211 |
|
212 |
[nameField setFrame: NSMakeRect(orx, NAME_OR_Y, cellsWidth, NAME_W)]; |
213 |
[nameField setStringValue: cutFileLabelText(fname, nameField, cellsWidth -NAME_MARGIN)]; |
214 |
|
215 |
[leftResBox setFrame: NSMakeRect(orx - NAME_W, NAME_OR_Y, NAME_W, NAME_W)]; |
216 |
[rightResBox setFrame: NSMakeRect(orx + cellsWidth, NAME_OR_Y, NAME_W, NAME_W)]; |
217 |
|
218 |
[iconbox setNeedsDisplay: YES]; |
219 |
} |
220 |
|
221 |
- (void)selectionChanged:(NSNotification *)n |
222 |
{ |
223 |
NSArray *selPaths = [[GWLib workspaceApp] getSelectedPaths]; |
224 |
int count = [selPaths count]; |
225 |
|
226 |
if (count == 1) { |
227 |
NSString *fpath = [selPaths objectAtIndex: 0]; |
228 |
NSString *defApp; |
229 |
NSString *type; |
230 |
|
231 |
ASSIGN (fname, [fpath lastPathComponent]); |
232 |
|
233 |
[ws getInfoForFile: fpath application: &defApp type: &type]; |
234 |
[imView setImage: [GWLib iconForFile: fpath ofType: type]]; |
235 |
} else { |
236 |
fname = [[NSString alloc] initWithFormat: @"%i items", count]; |
237 |
[imView setImage: [NSImage imageNamed: @"MultipleSelection.tiff"]]; |
238 |
} |
239 |
|
240 |
[self tile]; |
241 |
} |
242 |
|
243 |
- (void)startMouseEvent:(NSEvent *)event onResizer:(LabelResizer *)resizer |
244 |
{ |
245 |
NSApplication *app = [NSApplication sharedApplication]; |
246 |
NSDate *farAway = [NSDate distantFuture]; |
247 |
ResizerPosition pos = [resizer position]; |
248 |
int orx = (int)[prefbox convertPoint: [event locationInWindow] fromView: nil].x; |
249 |
NSView *resbox1 = (pos == leftarrow) ? leftResBox : rightResBox; |
250 |
NSView *resbox2 = (pos == leftarrow) ? rightResBox : leftResBox; |
251 |
unsigned int eventMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask; |
252 |
NSEvent *e; |
253 |
|
254 |
[prefbox lockFocus]; |
255 |
[[NSRunLoop currentRunLoop] limitDateForMode: NSEventTrackingRunLoopMode]; |
256 |
|
257 |
e = [app nextEventMatchingMask: eventMask |
258 |
untilDate: farAway |
259 |
inMode: NSEventTrackingRunLoopMode |
260 |
dequeue: YES]; |
261 |
|
262 |
while ([e type] != NSLeftMouseUp) { |
263 |
int x = (int)[prefbox convertPoint: [e locationInWindow] fromView: nil].x; |
264 |
int diff = x - orx; |
265 |
int orx1 = (int)[resbox1 frame].origin.x; |
266 |
int orx2 = (int)[resbox2 frame].origin.x; |
267 |
|
268 |
if ((max(orx1 + diff, orx2 - diff) - min(orx1 + diff, orx2 - diff)) < 160 |
269 |
&& (max(orx1 + diff, orx2 - diff) - min(orx1 + diff, orx2 - diff)) > 70) { |
270 |
int fieldwdt = max(orx1 + diff, orx2 - diff) - min(orx1 + diff, orx2 - diff) - NAME_W; |
271 |
int nameforx = (int)((BOX_W - fieldwdt) / 2); |
272 |
|
273 |
[resbox1 setFrameOrigin: NSMakePoint(orx1 + diff, NAME_OR_Y)]; |
274 |
[resbox2 setFrameOrigin: NSMakePoint(orx2 - diff, NAME_OR_Y)]; |
275 |
|
276 |
[nameField setFrame: NSMakeRect(nameforx, NAME_OR_Y, fieldwdt, NAME_W)]; |
277 |
[nameField setStringValue: cutFileLabelText(fname, nameField, fieldwdt -NAME_MARGIN)]; |
278 |
|
279 |
[iconbox setNeedsDisplay: YES]; |
280 |
|
281 |
orx = x; |
282 |
} |
283 |
e = [app nextEventMatchingMask: eventMask |
284 |
untilDate: farAway |
285 |
inMode: NSEventTrackingRunLoopMode |
286 |
dequeue: YES]; |
287 |
} |
288 |
[prefbox unlockFocus]; |
289 |
|
290 |
[self setNewWidth: (int)[nameField frame].size.width]; |
291 |
|
292 |
[setButt setEnabled: YES]; |
293 |
} |
294 |
|
295 |
- (void)setNewWidth:(int)w |
296 |
{ |
297 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
298 |
|
299 |
[defaults setObject: [NSString stringWithFormat: @"%i", w] |
300 |
forKey: @"iconsCellsWidth"]; |
301 |
[defaults synchronize]; |
302 |
|
303 |
cellsWidth = w; |
304 |
|
305 |
[[NSNotificationCenter defaultCenter] |
306 |
postNotificationName: GWIconsCellsWidthChangedNotification |
307 |
object: [NSNumber numberWithInt: w]]; |
308 |
} |
309 |
|
310 |
- (void)setDefaultWidth:(id)sender |
311 |
{ |
312 |
cellsWidth = DEFAULT_ICONS_WIDTH; |
313 |
[self setNewWidth: cellsWidth]; |
314 |
[self tile]; |
315 |
[setButt setEnabled: NO]; |
316 |
} |
317 |
|
318 |
@end |