2
* Copyright (C) 2003 Stefan Kleine Stegemann
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
#include "ExtendedImageView.h"
21
#include <Foundation/NSString.h>
22
#include <Foundation/NSException.h>
23
#include <Foundation/NSNotification.h>
24
#include <Foundation/NSValue.h>
25
#include <Foundation/NSDictionary.h>
26
#include <AppKit/NSAffineTransform.h>
27
#include <AppKit/NSColor.h>
28
#include <AppKit/NSImage.h>
29
#include <AppKit/NSImageRep.h>
30
#include <AppKit/NSBezierPath.h>
31
#include <AppKit/NSCursor.h>
32
#include <AppKit/NSScrollView.h>
33
#include <AppKit/NSWindow.h>
37
* Initialize constants.
39
NSString* N_SelectionChanged = @"N_SelectionChanged";
40
NSString* N_SelectionRemoved = @"N_SelectionRemoved";
41
NSString* N_ScrollingRequested = @"N_ScrollingRequested";
42
NSString* UserInfoKeySelection = @"Selection";
43
NSString* UserInfoKeyScrollAmount = @"ScrollAmount";
49
@interface ExtendedImageView(Private)
50
- (void) _adjustScaling;
51
- (void) _scrollMouseDown: (NSEvent*)event;
52
- (void) _scrollMouseDragged: (NSEvent*)event;
53
- (void) _scrollMouseUp: (NSEvent*)event;
54
- (void) _selectionMouseDown: (NSEvent*)event;
55
- (void) _selectionMouseDragged: (NSEvent*)event;
56
- (void) _selectionMouseUp: (NSEvent*)event;
57
- (void) _setDocumentCursor: (NSCursor*)cursor;
63
@implementation ExtendedImageView
66
* Designated initializer.
68
- (id) initWithFrame: (NSRect)frame
70
if ((self = [super initWithFrame: frame]))
72
selection = NSMakeRect(0, 0, 0, 0);
82
NSLog(@"dealloc ExtendedImageView, retain count is %d", [self retainCount]);
88
- (void) setSelection: (NSRect)aSelection
91
NSLog(@"set selection to x:%f, y:%f, w:%f, h:%f",
92
aSelection.origin.x, aSelection.origin.y,
93
aSelection.size.width, aSelection.size.height);
95
NSRect updateRect = NSUnionRect(aSelection, selection);
96
selection = aSelection;
98
[self setNeedsDisplayInRect: updateRect];
102
- (void) removeSelection
104
[self setNeedsDisplayInRect: selection];
109
- (void) drawRect: (NSRect)aRect
111
[super drawRect: aRect];
115
[[[NSColor blueColor] colorWithAlphaComponent: 0.5] set];
116
[NSBezierPath fillRect: selection];
122
* Setup a new selection rectangle. The old selection
123
* will be deleted if present.
125
- (void) mouseDown: (NSEvent*)event
127
if ([event modifierFlags] & NSShiftKeyMask)
129
[self _selectionMouseDown: event];
133
[self _scrollMouseDown: event];
139
*�When the mouse is dragged, the selection will
140
* be extended to where the user drags.
142
- (void) mouseDragged: (NSEvent*)event
144
if ([event modifierFlags] & NSShiftKeyMask)
146
[self _selectionMouseDragged: event];
150
[self _scrollMouseDragged: event];
158
- (void) mouseUp: (NSEvent*)event
160
if ([event modifierFlags] & NSShiftKeyMask)
162
[self _selectionMouseUp: event];
166
[self _scrollMouseUp: event];
173
@implementation ExtendedImageView(Private)
176
* The following methods implement scrolling by dragging
179
- (void) _scrollMouseDown: (NSEvent*)event
181
// TODO: set grab mouse cursor
186
- (void) _scrollMouseDragged: (NSEvent*)event
188
NSSize scrollAmount = NSMakeSize(-[event deltaX], -[event deltaY]);
190
[[NSNotificationCenter defaultCenter]
191
postNotificationName: N_ScrollingRequested
193
userInfo: [NSDictionary dictionaryWithObject: [NSValue valueWithSize: scrollAmount]
194
forKey: UserInfoKeyScrollAmount]];
198
- (void) _scrollMouseUp: (NSEvent*)event
200
// TODO: reset mouse cursor
206
* The following methods implement text selection with the mouse
207
* (dragging while holding the shift key)
209
- (void) _selectionMouseDown: (NSEvent*)event
211
[self removeSelection];
213
// post a notification to observers that the selection has changed
214
// (doing this in removeSelection will cause unwanted loops because
215
// removeSelection can also be called from outside).
216
// post a notification to observers that the selection has been removed
217
[[NSNotificationCenter defaultCenter]
218
postNotificationName: N_SelectionRemoved
221
selectionStart = [self convertPoint: [event locationInWindow] fromView: nil];
225
- (void) _selectionMouseDragged: (NSEvent*)event
227
NSPoint mouseLocation = [self convertPoint: [event locationInWindow] fromView: nil];
231
if (mouseLocation.x < selectionStart.x)
233
newSelection.origin.x = mouseLocation.x;
234
newSelection.size.width = selectionStart.x - mouseLocation.x;
238
newSelection.origin.x = selectionStart.x;
239
newSelection.size.width = mouseLocation.x - selectionStart.x;
242
if (mouseLocation.y < selectionStart.y)
244
newSelection.origin.y = mouseLocation.y;
245
newSelection.size.height = selectionStart.y - mouseLocation.y;
249
newSelection.origin.y = selectionStart.y;
250
newSelection.size.height = mouseLocation.y - selectionStart.y;
253
[self setSelection: newSelection];
255
// post a notification to observers that the selection has changed
256
// (doing this in setSelection will cause unwanted loops because
257
// setSelection can also be called from outside).
258
[[NSNotificationCenter defaultCenter]
259
postNotificationName: N_SelectionChanged
261
userInfo: [NSDictionary dictionaryWithObject: [NSValue valueWithRect: newSelection]
262
forKey: UserInfoKeySelection]];
266
- (void) _selectionMouseUp: (NSEvent*)event
268
[self setNeedsDisplay: YES];
273
* Adjust the scale factor for the image so that the
274
* image will completely fill the image view.
276
- (void) _adjustScaling
284
imageSize = [[self image] size];
285
viewSize = [self frame].size;
287
wScale = viewSize.width / imageSize.width;
288
hScale = viewSize.height / imageSize.height;
290
scaleTransform = [NSAffineTransform transform];
291
[scaleTransform scaleBy: (wScale < hScale ? wScale : hScale)];
292
[scaleTransform concat];
297
* Look for a superview that responds to setDocumentCursor
298
* and set the document cursor of this view to the specified
301
- (void) _setDocumentCursor: (NSCursor*)cursor
305
view = [self superview];
308
if ([view respondsToSelector: @selector(setDocumentCursor:)])
310
[view setDocumentCursor: cursor];
311
[[view window] resetCursorRects];
312
NSLog(@"document cursor set");
315
view = [view superview];
320
NSLog(@"Warning: Unable to set document cursor");