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 "DocumentWindow.h"
20
#include "DocumentWindowController.h"
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>
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";
49
* Non-Public methods of the DocumentView class.
51
@interface DocumentWindow(Private)
52
- (void) _createContentView;
53
- (void) _createDocumentView;
54
- (void) _createToolbar;
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.
63
@implementation DocumentWindow
66
* Designated initializer.
72
contentRect = NSMakeRect(100, 100, 400, 200);
74
if ((self = [super initWithContentRect: contentRect
75
styleMask: (NSTitledWindowMask
76
| NSClosableWindowMask
77
| NSMiniaturizableWindowMask
78
| NSResizableWindowMask)
79
backing: NSBackingStoreBuffered
88
[self setTitle: @"ViewPDF Window"];
89
[self setMinSize: NSMakeSize(100, 100)];
91
[self _createContentView];
92
[self _createDocumentView];
94
[[[self documentView] scrollView] setDisplayPageControls: YES];
102
* Set the windows controller.
104
- (void) setController: (DocumentWindowController*)controller
106
myController = controller;
107
// myController needs not to be retained because this
108
// is done by the document-based application model
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];
123
* Invoked by the controller when the controller has been
124
* initialized with this window.
126
- (void) windowInitializedWithController
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];
138
NSLog(@"dealloc DocumentWindow, retain count is %d", [self retainCount]);
140
RELEASE(fieldEditor);
147
* Catch control-keys.
149
- (void) keyDown: (NSEvent*)theEvent
151
NSString* chars = [theEvent characters];
152
unichar firstChar = 0;
153
BOOL shiftKey = [theEvent modifierFlags] & NSShiftKeyMask;
155
if ([chars length] > 0)
157
firstChar = [chars characterAtIndex: 0];
160
case NSLeftArrowFunctionKey:
163
[myController scrollHorizontal: self direction: SCROLL_LEFT];
167
[myController previousPage: self];
171
case NSRightArrowFunctionKey:
174
[myController scrollHorizontal: self direction: SCROLL_RIGHT];
178
[myController nextPage: self];
182
case NSUpArrowFunctionKey:
183
[myController scrollVertical: self direction: SCROLL_UP];
186
case NSDownArrowFunctionKey:
187
[myController scrollVertical: self direction: SCROLL_DOWN];
190
case NSPageUpFunctionKey:
191
[myController scrollPage: self direction: SCROLL_UP];
194
case NSPageDownFunctionKey:
195
[myController scrollPage: self direction: SCROLL_DOWN];
201
[myController scrollPage: self direction: SCROLL_UP];
205
[myController scrollPage: self direction: SCROLL_DOWN];
210
if ([self isInFindMode])
221
* Get a field editor that is able to catch the ESC key.
223
- (NSText*) fieldEditor: (BOOL)createFlag forObject: (id)anObject
225
if (!fieldEditor && createFlag)
227
fieldEditor = [[ExtendedNSTextView alloc] init];
228
[fieldEditor setFieldEditor: YES];
229
[fieldEditor setWindow: self];
236
- (PDFContentView*) documentView
242
- (void) toggleToolbar
244
[super toggleToolbarShown: nil];
246
[[NSUserDefaults standardUserDefaults] setBool: [toolbar isVisible]
247
forKey: PrefsToolbarVisible];
251
- (void) enterFindMode
257
if ([self isInFindMode])
262
contentFrame = [[self contentView] frame];
264
fvFrame = NSMakeRect(0, 0, NSWidth(contentFrame), 40);
268
findView = [[FindView alloc] initWithFrame: fvFrame];
272
[findView setFrame: fvFrame];
275
docFrame = [[self documentView] frame];
276
[[self documentView] setFrame:
277
NSMakeRect(NSMinX(docFrame),
280
NSHeight(docFrame) - NSHeight(fvFrame))];
282
[[self contentView] addSubview: findView];
283
[[self documentView] setNeedsDisplay: YES];
291
- (void) exitFindMode
295
NSAssert([self isInFindMode], @"not in find mode");
299
[findView deactivate];
301
contentFrame = [[self contentView] frame];
303
[findView removeFromSuperview];
304
[[self documentView] setFrame:
305
NSMakeRect(0, 0, NSWidth(contentFrame), NSHeight(contentFrame))];
307
[[self documentView] setNeedsDisplay: YES];
311
- (BOOL) isInFindMode
319
/* ----------------------------------------------------- */
320
/* Category NSToolbarDelegate */
321
/* ----------------------------------------------------- */
323
@implementation DocumentWindow (NSToolbarDelegate)
325
- (NSToolbarItem*)toolbar: (NSToolbar*)toolbar
326
itemForItemIdentifier: (NSString*)itemIdentifier
327
willBeInsertedIntoToolbar: (BOOL)flag
329
NSToolbarItem* theItem;
331
NSAssert(myController, @"cannot create toolbar item with no window controller set");
333
theItem = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
334
AUTORELEASE(theItem);
336
if ([itemIdentifier isEqualToString: TbItemPrevPage])
338
[theItem setLabel: @"Previous Page"];
339
[theItem setImage: [NSImage imageNamed: @"tb_prev_page.tiff"]];
340
[theItem setTarget: myController];
341
[theItem setAction: @selector(previousPage:)];
343
else if ([itemIdentifier isEqualToString: TbItemNextPage])
345
[theItem setLabel: @"Next Page"];
346
[theItem setImage: [NSImage imageNamed: @"tb_next_page.tiff"]];
347
[theItem setTarget: myController];
348
[theItem setAction: @selector(nextPage:)];
350
else if ([itemIdentifier isEqualToString: TbItemZoomIn])
352
[theItem setLabel: @"Zoom In"];
353
[theItem setImage: [NSImage imageNamed: @"zoom_in.tiff"]];
354
[theItem setTarget: myController];
355
[theItem setAction: @selector(zoomIn:)];
357
else if ([itemIdentifier isEqualToString: TbItemZoomOut])
359
[theItem setLabel: @"Zoom Out"];
360
[theItem setImage: [NSImage imageNamed: @"zoom_out.tiff"]];
361
[theItem setTarget: myController];
362
[theItem setAction: @selector(zoomOut:)];
364
else if ([itemIdentifier isEqualToString: TbItemZoomToRealSize])
366
[theItem setLabel: @"1:1"];
367
[theItem setImage: [NSImage imageNamed: @"zoom_11.tiff"]];
368
[theItem setTarget: myController];
369
[theItem setAction: @selector(zoomToRealSize:)];
371
else if ([itemIdentifier isEqualToString: TbItemSizeToFit])
373
[theItem setLabel: @"Fit Window"];
374
[theItem setImage: [NSImage imageNamed: @"fit_page.tiff"]];
375
[theItem setTarget: myController];
376
[theItem setAction: @selector(sizePageToFit:)];
378
else if ([itemIdentifier isEqualToString: TbItemSizeToFitWidth])
380
[theItem setLabel: @"Fit Width"];
381
[theItem setImage: [NSImage imageNamed: @"fit_width.tiff"]];
382
[theItem setTarget: myController];
383
[theItem setAction: @selector(sizePageToFitWidth:)];
390
- (NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar*)aToolbar
392
NSMutableArray* items = [[NSMutableArray alloc] init];
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];
407
- (NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar*)aToolbar
409
return [self toolbarAllowedItemIdentifiers: aToolbar];
413
- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
415
return [NSArray array];
421
/* ----------------------------------------------------- */
422
/* Category Private */
423
/* ----------------------------------------------------- */
425
@implementation DocumentWindow (Private)
428
* Create the window's content view.
430
- (void) _createContentView
434
NSRect contentViewFrame;
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)];
445
[self setContentView: contentView];
447
RELEASE(contentView);
452
* Create the image view and add it to the window.
454
- (void) _createDocumentView
458
frame = NSMakeRect(0,
460
NSWidth([[self contentView] frame]),
461
NSHeight([[self contentView] frame]));
463
documentView = [[PDFContentView alloc] initWithFrame: frame];
464
[[self contentView] addSubview: documentView];
466
RELEASE(documentView);
471
* Create the window's toolbar.
473
- (void) _createToolbar
477
toolbar = [[NSToolbar alloc] initWithIdentifier: @"ViewPDFToolbar"];
478
[toolbar setDelegate: self];
479
[toolbar setAllowsUserCustomization: NO];
480
[self setToolbar: toolbar];
483
isVisible = [[NSUserDefaults standardUserDefaults] boolForKey: PrefsToolbarVisible];
484
[toolbar setVisible: isVisible];
490
/* ----------------------------------------------------- */
491
/* ExtendedNSTextView implementation */
492
/* ----------------------------------------------------- */
494
@implementation ExtendedNSTextView
496
- (id) initWithFrame: (NSRect)aFrame
497
textContainer: (NSTextContainer*)aTextContainer
499
if ((self = [super initWithFrame: aFrame textContainer: aTextContainer]))
514
- (void) setWindow: (DocumentWindow*)aWindow
520
- (void) keyDown: (NSEvent*)theEvent
522
NSString* chars = [theEvent characters];
523
unichar firstChar = 0;
525
if ([chars length] > 0)
527
firstChar = [chars characterAtIndex: 0];
528
if ((firstChar == 27) && [window isInFindMode])
530
[window exitFindMode];
535
[super keyDown: theEvent];