~ubuntu-branches/ubuntu/gutsy/viewpdf.app/gutsy

« back to all changes in this revision

Viewing changes to ExtendedImageView.m

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2004-11-12 22:04:50 UTC
  • Revision ID: james.westby@ubuntu.com-20041112220450-hp9da460x7pvz96z
Tags: upstream-0.9
Import upstream version 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003  Stefan Kleine Stegemann
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "ExtendedImageView.h"
 
20
 
 
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>
 
34
 
 
35
 
 
36
/*
 
37
 * Initialize constants.
 
38
 */
 
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";
 
44
 
 
45
 
 
46
/*
 
47
 * Non-Public methods.
 
48
 */
 
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;
 
58
@end
 
59
 
 
60
 
 
61
/*
 
62
 */
 
63
@implementation ExtendedImageView
 
64
 
 
65
/*
 
66
 * Designated initializer.
 
67
 */
 
68
- (id) initWithFrame: (NSRect)frame
 
69
{
 
70
   if ((self = [super initWithFrame: frame]))
 
71
   {
 
72
      selection = NSMakeRect(0, 0, 0, 0);
 
73
      drawSelection = NO;
 
74
   }
 
75
 
 
76
   return self;
 
77
}
 
78
 
 
79
 
 
80
- (void) dealloc
 
81
{
 
82
   NSLog(@"dealloc ExtendedImageView, retain count is %d", [self retainCount]);
 
83
 
 
84
   [super dealloc];
 
85
}
 
86
 
 
87
 
 
88
- (void) setSelection: (NSRect)aSelection
 
89
{
 
90
   /*
 
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);
 
94
   */
 
95
   NSRect updateRect = NSUnionRect(aSelection, selection);
 
96
   selection = aSelection;
 
97
   drawSelection = YES;
 
98
   [self setNeedsDisplayInRect: updateRect];
 
99
}
 
100
 
 
101
 
 
102
- (void) removeSelection
 
103
{
 
104
   [self setNeedsDisplayInRect: selection];
 
105
   drawSelection = NO;
 
106
}
 
107
 
 
108
 
 
109
- (void) drawRect: (NSRect)aRect
 
110
{
 
111
   [super drawRect: aRect];
 
112
 
 
113
   if (drawSelection)
 
114
   {
 
115
      [[[NSColor blueColor] colorWithAlphaComponent: 0.5] set];
 
116
      [NSBezierPath fillRect: selection];
 
117
   }
 
118
}
 
119
 
 
120
 
 
121
/*
 
122
 * Setup a new selection rectangle. The old selection
 
123
 * will be deleted if present.
 
124
 */
 
125
- (void) mouseDown: (NSEvent*)event
 
126
{
 
127
   if ([event modifierFlags] & NSShiftKeyMask)
 
128
   {
 
129
      [self _selectionMouseDown: event];
 
130
   }
 
131
   else
 
132
   {
 
133
      [self _scrollMouseDown: event];
 
134
   }
 
135
}
 
136
 
 
137
 
 
138
/*
 
139
 *�When the mouse is dragged, the selection will
 
140
 * be extended to where the user drags.
 
141
 */
 
142
- (void) mouseDragged: (NSEvent*)event
 
143
{
 
144
   if ([event modifierFlags] & NSShiftKeyMask)
 
145
   {
 
146
      [self _selectionMouseDragged: event];
 
147
   }
 
148
   else
 
149
   {
 
150
      [self _scrollMouseDragged: event];
 
151
   }
 
152
}
 
153
 
 
154
 
 
155
/*
 
156
 * Finish selection.
 
157
 */
 
158
- (void) mouseUp: (NSEvent*)event
 
159
{
 
160
   if ([event modifierFlags] & NSShiftKeyMask)
 
161
   {
 
162
      [self _selectionMouseUp: event];
 
163
   }
 
164
   else
 
165
   {
 
166
      [self _scrollMouseUp: event];
 
167
   }
 
168
}
 
169
 
 
170
@end
 
171
 
 
172
 
 
173
@implementation ExtendedImageView(Private)
 
174
 
 
175
/*
 
176
 * The following methods implement scrolling by dragging
 
177
 * the mouse.
 
178
 */
 
179
- (void) _scrollMouseDown: (NSEvent*)event
 
180
{
 
181
   // TODO: set grab mouse cursor
 
182
   return;
 
183
}
 
184
 
 
185
 
 
186
- (void) _scrollMouseDragged: (NSEvent*)event
 
187
{
 
188
   NSSize scrollAmount = NSMakeSize(-[event deltaX], -[event deltaY]);
 
189
 
 
190
   [[NSNotificationCenter defaultCenter]
 
191
      postNotificationName: N_ScrollingRequested
 
192
      object: self
 
193
      userInfo: [NSDictionary dictionaryWithObject: [NSValue valueWithSize: scrollAmount]
 
194
                              forKey: UserInfoKeyScrollAmount]];
 
195
}
 
196
 
 
197
 
 
198
- (void) _scrollMouseUp: (NSEvent*)event
 
199
{
 
200
   // TODO: reset mouse cursor
 
201
   return;
 
202
}
 
203
 
 
204
 
 
205
/*
 
206
 * The following methods implement text selection with the mouse
 
207
 * (dragging while holding the shift key)
 
208
 */
 
209
- (void) _selectionMouseDown: (NSEvent*)event
 
210
{
 
211
   [self removeSelection];
 
212
 
 
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
 
219
      object: self];
 
220
      
 
221
   selectionStart = [self convertPoint: [event locationInWindow] fromView: nil];
 
222
}
 
223
 
 
224
 
 
225
- (void) _selectionMouseDragged: (NSEvent*)event
 
226
{
 
227
   NSPoint mouseLocation = [self convertPoint: [event locationInWindow] fromView: nil];
 
228
 
 
229
   NSRect newSelection;
 
230
 
 
231
   if (mouseLocation.x < selectionStart.x)
 
232
   {
 
233
      newSelection.origin.x = mouseLocation.x;
 
234
      newSelection.size.width = selectionStart.x - mouseLocation.x;
 
235
   }
 
236
   else
 
237
   {
 
238
      newSelection.origin.x = selectionStart.x;
 
239
      newSelection.size.width = mouseLocation.x - selectionStart.x;
 
240
   }
 
241
 
 
242
   if (mouseLocation.y < selectionStart.y)
 
243
   {
 
244
      newSelection.origin.y = mouseLocation.y;
 
245
      newSelection.size.height = selectionStart.y - mouseLocation.y;
 
246
   }
 
247
   else
 
248
   {
 
249
      newSelection.origin.y = selectionStart.y;
 
250
      newSelection.size.height = mouseLocation.y - selectionStart.y;
 
251
   }
 
252
 
 
253
   [self setSelection: newSelection];
 
254
 
 
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
 
260
      object: self
 
261
      userInfo: [NSDictionary dictionaryWithObject: [NSValue valueWithRect: newSelection]
 
262
                              forKey: UserInfoKeySelection]];
 
263
}
 
264
 
 
265
 
 
266
- (void) _selectionMouseUp: (NSEvent*)event
 
267
{
 
268
   [self setNeedsDisplay: YES];
 
269
}
 
270
 
 
271
 
 
272
/*
 
273
 * Adjust the scale factor for the image so that the
 
274
 * image will completely fill the image view.
 
275
 */
 
276
- (void) _adjustScaling
 
277
{
 
278
   float   wScale;
 
279
   float   hScale;
 
280
   id      scaleTransform;
 
281
   NSSize  imageSize;
 
282
   NSSize  viewSize;
 
283
 
 
284
   imageSize = [[self image] size];
 
285
   viewSize  = [self frame].size;
 
286
      
 
287
   wScale = viewSize.width / imageSize.width;
 
288
   hScale = viewSize.height / imageSize.height;
 
289
 
 
290
   scaleTransform = [NSAffineTransform transform];
 
291
   [scaleTransform scaleBy: (wScale < hScale ? wScale : hScale)];
 
292
   [scaleTransform concat];
 
293
}
 
294
 
 
295
 
 
296
/*
 
297
 * Look for a superview that responds to setDocumentCursor
 
298
 * and set the document cursor of this view to the specified
 
299
 * cursor.
 
300
 */
 
301
- (void) _setDocumentCursor: (NSCursor*)cursor
 
302
{
 
303
   id view;
 
304
 
 
305
   view = [self superview];
 
306
   while(view)
 
307
   {
 
308
      if ([view respondsToSelector: @selector(setDocumentCursor:)])
 
309
      {
 
310
         [view setDocumentCursor: cursor];
 
311
         [[view window] resetCursorRects];
 
312
         NSLog(@"document cursor set");
 
313
         break;
 
314
      }
 
315
      view = [view superview];
 
316
   }
 
317
  
 
318
   if (!view)
 
319
   {
 
320
      NSLog(@"Warning: Unable to set document cursor");
 
321
   }
 
322
}
 
323
 
 
324
@end
 
325
 
 
326