File: SinglePageView.m

package info (click to toggle)
viewpdf.app 1%3A0.2dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 512 kB
  • sloc: objc: 2,436; makefile: 18; sh: 9
file content (385 lines) | stat: -rw-r--r-- 9,413 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright (C) 2005  Stefan Kleine Stegemann
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "SinglePageView.h"
#import "NSView+Scrolling.h"
#import "ExtendedScrollView.h"
#import "Preferences.h"
#include <float.h>

/**
 * Non-Public methods.
 */
@interface SinglePageView (Private)
- (NSSize) _zoomedSize;
- (void) _drawPageBoundaries;
- (void) _applyResizePolicy;
- (NSArray*) _buildZoomFactors;
- (void) _documentPageDidChange: (NSNotification*)aNotification;
@end


@implementation SinglePageView

- (id) initWithFrame: (NSRect)aFrame
{
   self = [super initWithFrame: aFrame];
   if (self)
   {
      document   = nil;
      zoom       = [[ZoomFactorRange alloc] initWithFactors: [self _buildZoomFactors]];
      [zoom setDelegate: self];
      resizePolicy = ResizePolicyNone;
      paperColor = nil;
      [self setPaperColor: [NSColor whiteColor]];
   }

   return self;
}

- (void) dealloc
{
   NSLog(@"dealloc SinglePageView");
   [self setDocument: nil];
   [self setPaperColor: nil];
   [zoom release];
   [super dealloc];
}

- (void) setDocument: (Document*)aDocument
{
   [[NSNotificationCenter defaultCenter] removeObserver: self];
   [document release];
   
   document = [aDocument retain];
   
   if (document)
   {
      [self setFrameSize: [self _zoomedSize]];
      [self displayContentTop];
      
      [[NSNotificationCenter defaultCenter]
            addObserver: self
               selector: @selector(_documentPageDidChange:)
                   name: kDocumentPageChangedNotification
                 object: document];
   }
}

- (NSSize) preferredSize
{
   return [self _zoomedSize];
}

- (void) setPaperColor: (NSColor*)aColor
{
   [paperColor release];
   paperColor = [aColor retain];
   [self setNeedsDisplay: YES];
}

- (NSColor*) paperColor
{
   return paperColor;
}

- (void) setZoom: (float)aFactor
{
   [zoom setFactor: [ZoomFactor factorWithValue: aFactor]];
}

- (void) zoomIn
{
   [zoom increment];
}

- (void) zoomOut
{
   [zoom decrement];
}

- (float) zoom
{
   return [[zoom factor] value];
}

- (NSSize) zoomContentToFit: (NSSize)aSize
{
   NSSize normalized = [document pageSize];

   float xFactor = aSize.width / normalized.width;
   float yFactor = aSize.height / normalized.height;
   
   float factor = (xFactor < yFactor ? xFactor : yFactor);
   [zoom setFactor: [ZoomFactor factorWithValue: 100.0 * factor]];

   return [self _zoomedSize];
}

- (void) setResizePolicy: (ResizePolicy)aPolicy
{
   resizePolicy = aPolicy;
   [self _applyResizePolicy];
}

- (ResizePolicy) resizePolicy
{
   return resizePolicy;
}

- (void) scrollUpOnePage
{
   if (![self scrollPageUp])
   {
      if ([document previousPage])
      {
         [self scrollToBottom];
      }
   }
}

- (void) scrollDownOnePage
{
   if (![self scrollPageDown])
   {
      [document nextPage];
   }
}

- (void) scrollUpOneLine
{
   [self scrollLineUp]; // NSView+Scrolling
}

- (void) scrollDownOneLine
{
   [self scrollLineDown]; // NSView+Scrolling
}

- (void) scrollLeftOneLine
{
   [self scrollLineLeft]; // NSView+Scrolling
}

- (void) scrollRightOneLine
{
   [self scrollLineRight]; // NSView+Scrolling
}

- (void) displayContentTop
{
   [self scrollToTop]; // NSView+Scrolling
}

- (void) displayContentBottom
{
   [self scrollToBottom]; // NSView+Scrolling
}

- (void) displayContentLeft
{
   [self scrollToLeftEdge]; // NSView+Scrolling
}

- (void) displayContentRight
{
   [self scrollToRightEdge]; // NSView+Scrolling
}

- (void) update
{
   [self setNeedsDisplay: YES];
   [[self enclosingScrollView] setNeedsDisplay: YES];
}

- (BOOL) isOpaque
{
   return ([self paperColor] != nil);
}

- (void) drawRect: (NSRect)aRect
{
   NSRect contentRect;
   
   contentRect = NSMakeRect(0,  0, [self frame].size.width, [self frame].size.height);

   // background
   if ([self isOpaque])
   {
      [[self paperColor] set];
      [NSBezierPath fillRect: contentRect];
   }

   // page   
   [document drawPageAtPoint: contentRect.origin scale: [[zoom factor] asScale]];

   // mark page boundaries
   if ([[Preferences sharedPrefs] markPageBoundaries])
   {
      [self _drawPageBoundaries];
   }
}

- (void) viewDidMoveToSuperview
{
   if ([self enclosingScrollView])
   {
      [[self enclosingScrollView] setVerticalPageScroll: 200];
      // TODO: possibly use a ratio that is proportional to
      //       the size of the page?
   }
}

- (void) scrollViewDidResize: (NSScrollView*)aScrollView
{
   NSLog(@"scrollViewDidResize");
   [self _applyResizePolicy];
}

- (void) zoomFactorChanged: (ZoomFactorRange*)aRange
             withOldFactor: (ZoomFactor*)anOldFactor
{
   ZoomFactor* oldFactor = (anOldFactor != nil ? anOldFactor : [aRange factor]);
   
   NSRect oldVRect = [[self enclosingScrollView] documentVisibleRect];
   NSRect normRect = [oldFactor normalizeRect: oldVRect];

   [self setFrameSize: [self _zoomedSize]];

   NSRect scaledRect = [[zoom factor] translateRect: normRect];
   NSRect vRect = [[self enclosingScrollView] documentVisibleRect];

   NSPoint origin = NSMakePoint(NSMidX(scaledRect) - (NSWidth(vRect) / 2),
                                NSMidY(scaledRect) - (NSHeight(vRect) / 2));
   [self scrollPoint: origin];

   [[NSNotificationCenter defaultCenter]
         postNotificationName: kZoomFactorChangedNotification
                       object: self];
}

@end

/* ----------------------------------------------------- */
/*  Category Private                                     */
/* ----------------------------------------------------- */

@implementation SinglePageView (Private)

- (NSSize) _zoomedSize
{
   return [[zoom factor] translateSize: [document pageSize]];
}

- (void) _drawPageBoundaries
{
   // draw only if the enclosing clipview is bigger than
   // this view
   NSRect clipRect = [[[self enclosingScrollView] contentView] bounds];
   NSRect selfRect = [self frame];
   float diffWidth = NSWidth(clipRect) - NSWidth(selfRect);
   float diffHeight = NSHeight(clipRect) - NSHeight(selfRect);
   if ((diffWidth <= 2) && (diffHeight <= 2))
   {
      return;
   }
   
   // draw edges
   [[NSColor lightGrayColor] set];

   NSSize pageSize = [self _zoomedSize];
   float minX = 1.0;
   float minY = 1.0;
   float maxX = (minX + pageSize.width) - 2.0;
   float maxY = (minY + pageSize.height) - 2.0;

   NSBezierPath* path = [NSBezierPath bezierPath];
   [path setLineWidth: 1.0];

   // lower left edge
   [path moveToPoint: NSMakePoint(minX, minY + 10)];
   [path lineToPoint: NSMakePoint(minX, minY)];
   [path lineToPoint: NSMakePoint(minX + 10, minY)];
   // lower right edge
   [path moveToPoint: NSMakePoint(maxX - 10, minY)];
   [path lineToPoint: NSMakePoint(maxX, minY)];
   [path lineToPoint: NSMakePoint(maxX, minY + 10)];
   // upper left edge
   [path moveToPoint: NSMakePoint(minX, maxY - 10)];
   [path lineToPoint: NSMakePoint(minX, maxY)];
   [path lineToPoint: NSMakePoint(minX + 10, maxY)];
   // upper right edge
   [path moveToPoint: NSMakePoint(maxX, maxY - 10)];
   [path lineToPoint: NSMakePoint(maxX, maxY)];
   [path lineToPoint: NSMakePoint(maxX - 10, maxY)];

   [path stroke];
}

- (void) _applyResizePolicy
{
   NSSize contentSize = [[self enclosingScrollView] contentSize];

   switch (resizePolicy)
   {
      case ResizePolicyFitWidth:
         [self zoomContentToFit: NSMakeSize(contentSize.width, FLT_MAX)];
         break;
      case ResizePolicyFitHeight:
         [self zoomContentToFit: NSMakeSize(FLT_MAX, contentSize.height)];
         break;
      case ResizePolicyFitPage:
         [self zoomContentToFit: contentSize];
         break;
      case ResizePolicyNone:
      default:
         // do nothing
         break;
   }
}

- (NSArray*) _buildZoomFactors
{
   return [NSArray arrayWithObjects:
              [ZoomFactor factorWithValue: 10.0],
              [ZoomFactor factorWithValue: 25.0],
              [ZoomFactor factorWithValue: 33.0],
              [ZoomFactor factorWithValue: 50.0],
              [ZoomFactor factorWithValue: 66.0],
              [ZoomFactor factorWithValue: 75.0],
              [ZoomFactor factorWithValue: 88.0],
              [ZoomFactor factorWithValue: 100.0],
              [ZoomFactor factorWithValue: 115.0],
              [ZoomFactor factorWithValue: 125.0],
              [ZoomFactor factorWithValue: 150.0],
              [ZoomFactor factorWithValue: 175.0],
              [ZoomFactor factorWithValue: 200.0],
              [ZoomFactor factorWithValue: 250.0],
              [ZoomFactor factorWithValue: 300.0],
              [ZoomFactor factorWithValue: 350.0],
              nil];
}

- (void) _documentPageDidChange: (NSNotification*)aNotification
{
   // scroll to the top of the page whenever a new page
   // is displayed
   [self displayContentTop];
}

@end