1 |
/* RecyclerIcon.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 "GWFunctions.h" |
31 |
#else |
32 |
#include <GWorkspace/GWLib.h> |
33 |
#include <GWorkspace/GWFunctions.h> |
34 |
#endif |
35 |
#include "RecyclerIcon.h" |
36 |
#include "RecyclerViews.h" |
37 |
#include "GWorkspace.h" |
38 |
#include "GNUstep.h" |
39 |
|
40 |
@implementation RecyclerIcon |
41 |
|
42 |
- (void)dealloc |
43 |
{ |
44 |
RELEASE (path); |
45 |
RELEASE (name); |
46 |
RELEASE (icon); |
47 |
RELEASE (highlight); |
48 |
[super dealloc]; |
49 |
} |
50 |
|
51 |
- (id)initWithPath:(NSString *)apath inIconsView:(id)aview |
52 |
{ |
53 |
self = [super init]; |
54 |
if (self) { |
55 |
NSFont *font; |
56 |
NSString *defApp; |
57 |
NSString *type; |
58 |
|
59 |
ws = [NSWorkspace sharedWorkspace]; |
60 |
|
61 |
[self setFrame: NSMakeRect(0, 0, 64, 52)]; |
62 |
|
63 |
ASSIGN (path, apath); |
64 |
ASSIGN (name, [path lastPathComponent]); |
65 |
[ws getInfoForFile: path application: &defApp type: &type]; |
66 |
ASSIGN (icon, [[GWorkspace gworkspace] iconForFile: path ofType: type]); |
67 |
ASSIGN (highlight, [NSImage imageNamed: @"CellHighlight.tiff"]); |
68 |
iconsView = (IconsView *)aview; |
69 |
|
70 |
namelabel = [NSTextField new]; |
71 |
AUTORELEASE (namelabel); |
72 |
|
73 |
labelWidth = [iconsView cellsWidth] - 4; |
74 |
font = [NSFont systemFontOfSize: 12]; |
75 |
[namelabel setFont: font]; |
76 |
[namelabel setBezeled: NO]; |
77 |
[namelabel setEditable: NO]; |
78 |
[namelabel setSelectable: NO]; |
79 |
[namelabel setAlignment: NSCenterTextAlignment]; |
80 |
[namelabel setBackgroundColor: [NSColor windowBackgroundColor]]; |
81 |
[namelabel setTextColor: [NSColor blackColor]]; |
82 |
[self setLabelWidth]; |
83 |
|
84 |
isSelect = NO; |
85 |
} |
86 |
return self; |
87 |
} |
88 |
|
89 |
- (void)select |
90 |
{ |
91 |
isSelect = YES; |
92 |
[namelabel setTextColor: [NSColor blackColor]]; |
93 |
[iconsView unselectOtherIcons: self]; |
94 |
[iconsView setCurrentSelection: path]; |
95 |
[self display]; |
96 |
} |
97 |
|
98 |
- (void)unselect |
99 |
{ |
100 |
isSelect = NO; |
101 |
[namelabel setTextColor: [NSColor blackColor]]; |
102 |
[self display]; |
103 |
} |
104 |
|
105 |
- (void)setLabelWidth |
106 |
{ |
107 |
NSFont *font = [NSFont systemFontOfSize: 12]; |
108 |
NSRect rect = [namelabel frame]; |
109 |
labelWidth = [iconsView cellsWidth] - 8; |
110 |
|
111 |
if (isSelect == YES) { |
112 |
[namelabel setFrame: NSMakeRect(0, 0, [font widthOfString: name] + 8, 14)]; |
113 |
[namelabel setStringValue: name]; |
114 |
} else { |
115 |
int width = (int)[[namelabel font] widthOfString: name] + 8; |
116 |
if (width > labelWidth) { |
117 |
width = labelWidth; |
118 |
} |
119 |
[namelabel setFrame: NSMakeRect(0, 0, width, 14)]; |
120 |
[namelabel setStringValue: cutFileLabelText(name, namelabel, width - 8)]; |
121 |
} |
122 |
|
123 |
[(NSView *)iconsView setNeedsDisplayInRect: rect]; |
124 |
} |
125 |
|
126 |
- (NSTextField *)label |
127 |
{ |
128 |
return namelabel; |
129 |
} |
130 |
|
131 |
- (NSString *)path |
132 |
{ |
133 |
return path; |
134 |
} |
135 |
|
136 |
- (NSString *)name |
137 |
{ |
138 |
return name; |
139 |
} |
140 |
|
141 |
- (BOOL)isSelect |
142 |
{ |
143 |
return isSelect; |
144 |
} |
145 |
|
146 |
- (void)mouseDown:(NSEvent *)theEvent |
147 |
{ |
148 |
if (isSelect == NO) { |
149 |
[self select]; |
150 |
} |
151 |
} |
152 |
|
153 |
- (void)mouseDragged:(NSEvent *)theEvent |
154 |
{ |
155 |
if(dragdelay < 5) { |
156 |
dragdelay++; |
157 |
return; |
158 |
} |
159 |
|
160 |
[self startExternalDragOnEvent: theEvent]; |
161 |
} |
162 |
|
163 |
- (void)drawRect:(NSRect)rect |
164 |
{ |
165 |
NSPoint p; |
166 |
NSSize s; |
167 |
|
168 |
if(isSelect) { |
169 |
s = [highlight size]; |
170 |
p = NSMakePoint((rect.size.width - s.width) / 2, (rect.size.height - s.height) / 2); |
171 |
[highlight compositeToPoint: p operation: NSCompositeSourceOver]; |
172 |
} |
173 |
|
174 |
s = [icon size]; |
175 |
p = NSMakePoint((rect.size.width - s.width) / 2, (rect.size.height - s.height) / 2); |
176 |
[icon compositeToPoint: p operation: NSCompositeSourceOver]; |
177 |
} |
178 |
|
179 |
- (id)delegate |
180 |
{ |
181 |
return delegate; |
182 |
} |
183 |
|
184 |
- (void)setDelegate:(id)aDelegate |
185 |
{ |
186 |
ASSIGN (delegate, aDelegate); |
187 |
AUTORELEASE (delegate); |
188 |
} |
189 |
|
190 |
@end |
191 |
|
192 |
@implementation RecyclerIcon (DraggingSource) |
193 |
|
194 |
- (void)startExternalDragOnEvent:(NSEvent *)event |
195 |
{ |
196 |
NSEvent *nextEvent; |
197 |
NSPoint dragPoint; |
198 |
NSPasteboard *pb; |
199 |
NSString *defApp; |
200 |
NSString *type; |
201 |
NSImage *dragIcon; |
202 |
|
203 |
nextEvent = [[self window] nextEventMatchingMask: |
204 |
NSLeftMouseUpMask | NSLeftMouseDraggedMask]; |
205 |
|
206 |
if([nextEvent type] != NSLeftMouseDragged) { |
207 |
return; |
208 |
} |
209 |
|
210 |
dragPoint = [nextEvent locationInWindow]; |
211 |
dragPoint = [self convertPoint: dragPoint fromView: nil]; |
212 |
|
213 |
pb = [NSPasteboard pasteboardWithName: NSDragPboard]; |
214 |
[self declareAndSetShapeOnPasteboard: pb]; |
215 |
|
216 |
dragdelay = 0; |
217 |
|
218 |
[ws getInfoForFile: path application: &defApp type: &type]; |
219 |
dragIcon = [[GWorkspace gworkspace] iconForFile: path ofType: type]; |
220 |
|
221 |
[self dragImage: dragIcon |
222 |
at: dragPoint |
223 |
offset: NSZeroSize |
224 |
event: event |
225 |
pasteboard: pb |
226 |
source: self |
227 |
slideBack: NO]; |
228 |
} |
229 |
|
230 |
- (void)declareAndSetShapeOnPasteboard:(NSPasteboard *)pb |
231 |
{ |
232 |
NSArray *dndtypes; |
233 |
NSArray *selection; |
234 |
|
235 |
dndtypes = [NSArray arrayWithObject: NSFilenamesPboardType]; |
236 |
[pb declareTypes: dndtypes owner: nil]; |
237 |
selection = [NSArray arrayWithObjects: path, nil]; |
238 |
|
239 |
if ([pb setPropertyList: selection forType: NSFilenamesPboardType] == NO) { |
240 |
return; |
241 |
} |
242 |
} |
243 |
|
244 |
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)flag |
245 |
{ |
246 |
return NSDragOperationAll; |
247 |
} |
248 |
|
249 |
@end |