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

« back to all changes in this revision

Viewing changes to DocumentWindow.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 "DocumentWindow.h"
 
20
#include "DocumentWindowController.h"
 
21
#include "ViewPDF.h"
 
22
 
 
23
#include <Foundation/NSString.h>
 
24
#include <Foundation/NSException.h>
 
25
#include <Foundation/NSArray.h>
 
26
#include <Foundation/NSEnumerator.h>
 
27
#include <Foundation/NSUserDefaults.h>
 
28
#include <AppKit/NSButton.h>
 
29
#include <AppKit/NSEvent.h>
 
30
#include <AppKit/NSImage.h>
 
31
#include <AppKit/NSTextField.h>
 
32
#include <AppKit/NSFont.h>
 
33
#include <AppKit/NSProgressIndicator.h>
 
34
#include <AppKit/NSWindow+Toolbar.h>
 
35
#include <AppKit/NSToolbarItem.h>
 
36
#include <AppKit/NSText.h>
 
37
 
 
38
/* Toolbar Item identifiers */
 
39
static NSString* TbItemNextPage       = @"NextPage";
 
40
static NSString* TbItemPrevPage       = @"PrevPage";
 
41
static NSString* TbItemZoomIn         = @"ZoomIn";
 
42
static NSString* TbItemZoomOut        = @"ZoomOut";
 
43
static NSString* TbItemZoomToRealSize = @"SizeToRealSize";
 
44
static NSString* TbItemSizeToFit      = @"SizeToFit";
 
45
static NSString* TbItemSizeToFitWidth = @"SizeToFitWidth";
 
46
 
 
47
 
 
48
/*
 
49
 * Non-Public methods of the DocumentView class.
 
50
 */
 
51
@interface DocumentWindow(Private)
 
52
- (void) _createContentView;
 
53
- (void) _createDocumentView;
 
54
- (void) _createToolbar;
 
55
@end
 
56
 
 
57
/*
 
58
 * A window that displays a ViewableDocument and provides
 
59
 * some controls to control the view (eg. zoom controls).
 
60
 * A DocumentWindow is controlled by an instance of
 
61
 * DocumentWindowController. 
 
62
 */
 
63
@implementation DocumentWindow
 
64
 
 
65
/*
 
66
 * Designated initializer.
 
67
 */
 
68
- (id) init
 
69
{
 
70
   NSRect  contentRect;
 
71
 
 
72
   contentRect = NSMakeRect(100, 100, 400, 200);
 
73
 
 
74
   if ((self = [super initWithContentRect: contentRect
 
75
                     styleMask: (NSTitledWindowMask
 
76
                                 | NSClosableWindowMask
 
77
                                 | NSMiniaturizableWindowMask
 
78
                                 | NSResizableWindowMask)
 
79
                     backing: NSBackingStoreBuffered
 
80
                      defer: NO]))
 
81
   {
 
82
      myController = nil;
 
83
      toolbar = nil;
 
84
      findView = nil;
 
85
      isInFindMode = NO;
 
86
      fieldEditor = nil;
 
87
 
 
88
      [self setTitle: @"ViewPDF Window"];
 
89
      [self setMinSize: NSMakeSize(100, 100)];
 
90
      
 
91
      [self _createContentView];
 
92
      [self _createDocumentView];
 
93
 
 
94
      [[[self documentView] scrollView] setDisplayPageControls: YES];
 
95
   }
 
96
 
 
97
   return self;
 
98
}
 
99
 
 
100
 
 
101
/*
 
102
 * Set the windows controller.
 
103
 */
 
104
- (void) setController: (DocumentWindowController*)controller
 
105
{
 
106
   myController = controller;
 
107
   // myController needs not to be retained because this
 
108
   // is done by the document-based application model
 
109
 
 
110
   [[[documentView scrollView] firstPageButton] setAction: @selector(firstPage:)];
 
111
   [[[documentView scrollView] firstPageButton] setTarget: myController];
 
112
   [[[documentView scrollView] prevPageButton] setAction: @selector(previousPage:)];
 
113
   [[[documentView scrollView] prevPageButton] setTarget: myController];
 
114
   [[[documentView scrollView] nextPageButton] setAction: @selector(nextPage:)];
 
115
   [[[documentView scrollView] nextPageButton] setTarget: myController];   
 
116
   [[[documentView scrollView] lastPageButton] setAction: @selector(lastPage:)];
 
117
   [[[documentView scrollView] lastPageButton] setTarget: myController];
 
118
   [[[documentView scrollView] currentPageField] setDelegate: myController];
 
119
}
 
120
 
 
121
 
 
122
/*
 
123
 * Invoked by the controller when the controller has been
 
124
 * initialized with this window.
 
125
 */
 
126
- (void) windowInitializedWithController
 
127
{
 
128
   /* we initialize the toolbar here because the window
 
129
      controller set's the window size and calls this
 
130
      method afterwards.  */
 
131
   [self _createToolbar];
 
132
}
 
133
 
 
134
 
 
135
 
 
136
- (void) dealloc
 
137
{
 
138
   NSLog(@"dealloc DocumentWindow, retain count is %d", [self retainCount]);
 
139
   RELEASE(findView);
 
140
   RELEASE(fieldEditor);
 
141
 
 
142
   [super dealloc];
 
143
}
 
144
 
 
145
 
 
146
/*
 
147
 * Catch control-keys.
 
148
 */
 
149
- (void) keyDown: (NSEvent*)theEvent
 
150
{
 
151
   NSString* chars = [theEvent characters];
 
152
   unichar   firstChar = 0;
 
153
   BOOL      shiftKey = [theEvent modifierFlags] & NSShiftKeyMask;
 
154
   
 
155
   if ([chars length] > 0)
 
156
   {
 
157
      firstChar = [chars characterAtIndex: 0];
 
158
      switch (firstChar)
 
159
      {
 
160
         case NSLeftArrowFunctionKey:
 
161
            if (!shiftKey)
 
162
            {
 
163
               [myController scrollHorizontal: self direction: SCROLL_LEFT];
 
164
            }
 
165
            else
 
166
            {
 
167
               [myController previousPage: self];
 
168
            }
 
169
            break;
 
170
 
 
171
         case NSRightArrowFunctionKey:
 
172
            if (!shiftKey)
 
173
            {
 
174
               [myController scrollHorizontal: self direction: SCROLL_RIGHT];
 
175
            }
 
176
            else
 
177
            {
 
178
               [myController nextPage: self];
 
179
            }
 
180
            break;
 
181
 
 
182
         case NSUpArrowFunctionKey:
 
183
            [myController scrollVertical: self direction: SCROLL_UP];
 
184
            break;
 
185
 
 
186
         case NSDownArrowFunctionKey:
 
187
            [myController scrollVertical: self direction: SCROLL_DOWN];
 
188
            break;
 
189
 
 
190
         case NSPageUpFunctionKey:
 
191
            [myController scrollPage: self direction: SCROLL_UP];
 
192
            break;
 
193
 
 
194
         case NSPageDownFunctionKey:
 
195
            [myController scrollPage: self direction: SCROLL_DOWN];
 
196
            break;
 
197
 
 
198
         case 0x20:
 
199
            if (shiftKey)
 
200
            {
 
201
               [myController scrollPage: self direction: SCROLL_UP];
 
202
            }
 
203
            else
 
204
            {
 
205
               [myController scrollPage: self direction: SCROLL_DOWN];
 
206
            }
 
207
            break;
 
208
 
 
209
         case 27: // ESC
 
210
            if ([self isInFindMode])
 
211
            {
 
212
               [self exitFindMode];
 
213
            }
 
214
            break;
 
215
      }
 
216
   }
 
217
}
 
218
 
 
219
 
 
220
/*
 
221
 * Get a field editor that is able to catch the ESC key.
 
222
 */
 
223
- (NSText*) fieldEditor: (BOOL)createFlag forObject: (id)anObject
 
224
{
 
225
   if (!fieldEditor && createFlag)
 
226
   {
 
227
      fieldEditor = [[ExtendedNSTextView alloc] init];
 
228
      [fieldEditor setFieldEditor: YES];
 
229
      [fieldEditor setWindow: self];
 
230
   }
 
231
 
 
232
   return fieldEditor;
 
233
}
 
234
 
 
235
 
 
236
- (PDFContentView*) documentView
 
237
{
 
238
   return documentView;
 
239
}
 
240
 
 
241
 
 
242
- (void) toggleToolbar
 
243
{
 
244
   [super toggleToolbarShown: nil];
 
245
 
 
246
   [[NSUserDefaults standardUserDefaults] setBool: [toolbar isVisible]
 
247
                                           forKey: PrefsToolbarVisible];
 
248
}
 
249
 
 
250
 
 
251
- (void) enterFindMode
 
252
{
 
253
   NSRect contentFrame;
 
254
   NSRect fvFrame;
 
255
   NSRect docFrame;
 
256
 
 
257
   if ([self isInFindMode])
 
258
   {
 
259
      return;
 
260
   }
 
261
 
 
262
   contentFrame = [[self contentView] frame];
 
263
 
 
264
   fvFrame = NSMakeRect(0, 0, NSWidth(contentFrame), 40);
 
265
 
 
266
   if (findView == nil)
 
267
   {
 
268
      findView = [[FindView alloc] initWithFrame: fvFrame];
 
269
   }
 
270
   else
 
271
   {
 
272
      [findView setFrame: fvFrame];
 
273
   }
 
274
 
 
275
   docFrame = [[self documentView] frame];
 
276
   [[self documentView] setFrame:
 
277
      NSMakeRect(NSMinX(docFrame),
 
278
                 NSHeight(fvFrame),
 
279
                 NSWidth(docFrame),
 
280
                 NSHeight(docFrame) - NSHeight(fvFrame))];
 
281
 
 
282
   [[self contentView] addSubview: findView];
 
283
   [[self documentView] setNeedsDisplay: YES];
 
284
 
 
285
   [findView activate];
 
286
 
 
287
   isInFindMode = YES;
 
288
}
 
289
 
 
290
 
 
291
- (void) exitFindMode
 
292
{
 
293
   NSRect contentFrame;
 
294
 
 
295
   NSAssert([self isInFindMode], @"not in find mode");
 
296
 
 
297
   isInFindMode = NO;
 
298
 
 
299
   [findView deactivate];
 
300
 
 
301
   contentFrame = [[self contentView] frame];
 
302
 
 
303
   [findView removeFromSuperview];
 
304
   [[self documentView] setFrame:
 
305
        NSMakeRect(0, 0, NSWidth(contentFrame), NSHeight(contentFrame))];
 
306
 
 
307
   [[self documentView] setNeedsDisplay: YES];
 
308
}
 
309
 
 
310
 
 
311
- (BOOL) isInFindMode
 
312
{
 
313
   return isInFindMode;
 
314
}
 
315
 
 
316
@end
 
317
 
 
318
 
 
319
/* ----------------------------------------------------- */
 
320
/*  Category NSToolbarDelegate                           */
 
321
/* ----------------------------------------------------- */
 
322
 
 
323
@implementation DocumentWindow (NSToolbarDelegate)
 
324
 
 
325
- (NSToolbarItem*)toolbar: (NSToolbar*)toolbar
 
326
    itemForItemIdentifier: (NSString*)itemIdentifier
 
327
willBeInsertedIntoToolbar: (BOOL)flag
 
328
{
 
329
   NSToolbarItem* theItem;
 
330
 
 
331
   NSAssert(myController, @"cannot create toolbar item with no window controller set");
 
332
 
 
333
   theItem = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
 
334
   AUTORELEASE(theItem);
 
335
 
 
336
   if ([itemIdentifier isEqualToString: TbItemPrevPage])
 
337
   {
 
338
      [theItem setLabel: @"Previous Page"];
 
339
      [theItem setImage: [NSImage imageNamed: @"tb_prev_page.tiff"]];
 
340
      [theItem setTarget: myController];
 
341
      [theItem setAction: @selector(previousPage:)];
 
342
   }
 
343
   else if ([itemIdentifier isEqualToString: TbItemNextPage])
 
344
   {
 
345
      [theItem setLabel: @"Next Page"];
 
346
      [theItem setImage: [NSImage imageNamed: @"tb_next_page.tiff"]];
 
347
      [theItem setTarget: myController];
 
348
      [theItem setAction: @selector(nextPage:)];
 
349
   }
 
350
   else if ([itemIdentifier isEqualToString: TbItemZoomIn])
 
351
   {
 
352
      [theItem setLabel: @"Zoom In"];
 
353
      [theItem setImage: [NSImage imageNamed: @"zoom_in.tiff"]];
 
354
      [theItem setTarget: myController];
 
355
      [theItem setAction: @selector(zoomIn:)];
 
356
   }
 
357
   else if ([itemIdentifier isEqualToString: TbItemZoomOut])
 
358
   {
 
359
      [theItem setLabel: @"Zoom Out"];
 
360
      [theItem setImage: [NSImage imageNamed: @"zoom_out.tiff"]];
 
361
      [theItem setTarget: myController];
 
362
      [theItem setAction: @selector(zoomOut:)];
 
363
   }
 
364
   else if ([itemIdentifier isEqualToString: TbItemZoomToRealSize])
 
365
   {
 
366
      [theItem setLabel: @"1:1"];
 
367
      [theItem setImage: [NSImage imageNamed: @"zoom_11.tiff"]];
 
368
      [theItem setTarget: myController];
 
369
      [theItem setAction: @selector(zoomToRealSize:)];
 
370
   }
 
371
   else if ([itemIdentifier isEqualToString: TbItemSizeToFit])
 
372
   {
 
373
      [theItem setLabel: @"Fit Window"];
 
374
      [theItem setImage: [NSImage imageNamed: @"fit_page.tiff"]];
 
375
      [theItem setTarget: myController];
 
376
      [theItem setAction: @selector(sizePageToFit:)];
 
377
   }
 
378
   else if ([itemIdentifier isEqualToString: TbItemSizeToFitWidth])
 
379
   {
 
380
      [theItem setLabel: @"Fit Width"];
 
381
      [theItem setImage: [NSImage imageNamed: @"fit_width.tiff"]];
 
382
      [theItem setTarget: myController];
 
383
      [theItem setAction: @selector(sizePageToFitWidth:)];
 
384
   }
 
385
 
 
386
   return theItem;
 
387
}
 
388
 
 
389
 
 
390
- (NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar*)aToolbar
 
391
{
 
392
   NSMutableArray* items = [[NSMutableArray alloc] init];
 
393
   AUTORELEASE(items);
 
394
 
 
395
   [items addObject: TbItemPrevPage];
 
396
   [items addObject: TbItemNextPage];
 
397
   [items addObject: TbItemZoomIn];
 
398
   [items addObject: TbItemZoomOut];
 
399
   [items addObject: TbItemZoomToRealSize];
 
400
   [items addObject: TbItemSizeToFit];
 
401
   [items addObject: TbItemSizeToFitWidth];
 
402
 
 
403
   return items;
 
404
}
 
405
 
 
406
 
 
407
- (NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar*)aToolbar
 
408
{
 
409
   return [self toolbarAllowedItemIdentifiers: aToolbar];
 
410
}
 
411
 
 
412
 
 
413
- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
 
414
{
 
415
   return [NSArray array];
 
416
}
 
417
 
 
418
@end
 
419
 
 
420
 
 
421
/* ----------------------------------------------------- */
 
422
/*  Category Private                                     */
 
423
/* ----------------------------------------------------- */
 
424
 
 
425
@implementation DocumentWindow (Private)
 
426
 
 
427
/*
 
428
 * Create the window's content view.
 
429
 */
 
430
- (void) _createContentView
 
431
{
 
432
   NSView* contentView;
 
433
   NSRect contentRect;
 
434
   NSRect contentViewFrame;
 
435
 
 
436
   contentRect = [[self contentView] frame];
 
437
   contentViewFrame = NSMakeRect(0, 0, 
 
438
                                 contentRect.size.width,
 
439
                                 contentRect.size.height);
 
440
   contentView = [[NSView alloc] initWithFrame: contentViewFrame];
 
441
   [contentView setAutoresizesSubviews: YES];
 
442
   [contentView setAutoresizingMask: (NSViewWidthSizable |
 
443
                                      NSViewHeightSizable)];
 
444
   
 
445
   [self setContentView: contentView];
 
446
 
 
447
   RELEASE(contentView);
 
448
}
 
449
 
 
450
 
 
451
/*
 
452
 * Create the image view and add it to the window.
 
453
 */
 
454
- (void) _createDocumentView
 
455
{
 
456
   NSRect frame;
 
457
 
 
458
   frame = NSMakeRect(0,
 
459
                      0,
 
460
                      NSWidth([[self contentView] frame]),
 
461
                      NSHeight([[self contentView] frame]));
 
462
 
 
463
   documentView = [[PDFContentView alloc] initWithFrame: frame];
 
464
   [[self contentView] addSubview: documentView];
 
465
   
 
466
   RELEASE(documentView);
 
467
}
 
468
 
 
469
 
 
470
/*
 
471
 * Create the window's toolbar.
 
472
 */
 
473
- (void) _createToolbar
 
474
{
 
475
   BOOL isVisible;
 
476
 
 
477
   toolbar = [[NSToolbar alloc] initWithIdentifier: @"ViewPDFToolbar"];
 
478
   [toolbar setDelegate: self];
 
479
   [toolbar setAllowsUserCustomization: NO];
 
480
   [self setToolbar: toolbar];
 
481
   RELEASE(toolbar);
 
482
 
 
483
   isVisible = [[NSUserDefaults standardUserDefaults] boolForKey: PrefsToolbarVisible];
 
484
   [toolbar setVisible: isVisible];
 
485
}
 
486
 
 
487
@end
 
488
 
 
489
 
 
490
/* ----------------------------------------------------- */
 
491
/*  ExtendedNSTextView implementation                    */
 
492
/* ----------------------------------------------------- */
 
493
 
 
494
@implementation ExtendedNSTextView
 
495
 
 
496
- (id) initWithFrame: (NSRect)aFrame
 
497
       textContainer: (NSTextContainer*)aTextContainer
 
498
{
 
499
   if ((self = [super initWithFrame: aFrame textContainer: aTextContainer]))
 
500
   {
 
501
      window = nil;
 
502
   }
 
503
 
 
504
   return self;
 
505
}
 
506
 
 
507
 
 
508
- (void) dealloc
 
509
{
 
510
   [super dealloc];
 
511
}
 
512
 
 
513
 
 
514
- (void) setWindow: (DocumentWindow*)aWindow
 
515
{
 
516
   window = aWindow;
 
517
}
 
518
 
 
519
 
 
520
- (void) keyDown: (NSEvent*)theEvent
 
521
{
 
522
   NSString* chars = [theEvent characters];
 
523
   unichar   firstChar = 0;
 
524
 
 
525
   if ([chars length] > 0)
 
526
   {
 
527
      firstChar = [chars characterAtIndex: 0];
 
528
      if ((firstChar == 27) && [window isInFindMode])
 
529
      {
 
530
         [window exitFindMode];
 
531
         return;
 
532
      }
 
533
   }
 
534
 
 
535
   [super keyDown: theEvent];
 
536
}
 
537
 
 
538
@end
 
539