4
Document class & WindowController : This file is part of Preview
6
Copyright (C) 2003;2004 Fabien VALLON
7
2003,2004 Alcove ( http://www.alcove.com )
8
Additional copyrights here
10
Authors : Fabien VALLON <fabien@sonappart.net>
13
This program is free software; you can redistribute it and/or
14
modify it under the terms of the GNU General Public License as
15
published by the Free Software Foundation; either version 2 of
16
the License, or (at your option) any later version.
18
This program is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
See the GNU General Public License for more details.
24
You should have received a copy of the GNU General Public
25
License along with this program; if not, write to:
27
Free Software Foundation, Inc.
28
59 Temple Place - Suite 330
29
Boston, MA 02111-1307, USA
32
// See Doumentation/DEVELOPERS
36
#include <Foundation/NSNotification.h>
38
#include <AppKit/AppKit.h>
40
/*********************************************************************/
41
/**************** NSDocument Private methods *************************/
42
/*********************************************************************/
44
#define HEIGHT_HUNDRED_PER_CENT 0
45
#define FOUR_HUNDRED_PER_CENT 1
55
#define SCALEFACTOR 0.1
59
@interface CheckeredView: NSView
63
@implementation CheckeredView
65
- (void)drawRect:(NSRect)rect
67
NSColor *backColor = [NSColor darkGrayColor];
68
NSColor *color = [NSColor grayColor];
74
BOOL drawForeground = NO;
75
for(i = 0; i < rect.size.width; i+=10)
77
drawForeground = i % 20 == 0;
78
for(j = 0; j < rect.size.height; j+=10)
82
NSRectFill(NSMakeRect(rect.origin.x+i, rect.origin.y+j, 10, 10));
84
drawForeground = !drawForeground;
95
@interface Document (Private)
96
-(void) _setScaleFactor: (double) factor;
97
-(double) _scaleFactor;
99
-(void) _setOriginalSize: (NSSize) originalSize;
100
-(NSSize) _originalSize;
101
-(void) _setCurrentItem:(unsigned) tag;
102
-(unsigned) _currentItem;
103
-(void) _notifyDragScroll: (id)notification;
104
-(void) _setIsAlpha:(BOOL) flag;
108
@implementation Document (Private)
112
if ([self _scaleFactor] != 1.0 )
115
NSAffineTransform *affineTransform;
117
affineTransform = [NSAffineTransform transform];
118
[affineTransform scaleBy:[self _scaleFactor]];
119
imageSize = [imageView frame].size;
121
[imageView setFrameSize:[affineTransform transformSize:imageSize]];
124
[checkeredView setFrameSize:
125
[affineTransform transformSize:imageSize]];
128
[imageView setNeedsDisplay:YES];
134
* Set the scale factor (zoom).
136
- (void) _setScaleFactor: (double)factor
138
_scaleFactor = factor;
141
- (double) _scaleFactor
146
-(void) _setOriginalSize: (NSSize) orginalSize
148
_originalSize = orginalSize;
151
-(NSSize) _originalSize
153
return _originalSize;
156
-(void) _setCurrentItem:(unsigned) tag
158
if (tag <= FIT_WIDTH)
162
-(unsigned) _currentItem
167
- (void) _notifyDragScroll: (id)notification
175
[[[notification userInfo] objectForKey: @"UserInfoKeyScrollAmount"] sizeValue];
177
vRect = [scrollView documentVisibleRect];
178
contentSize = [scrollView contentSize];
180
newOrigin = NSMakePoint(vRect.origin.x + scrollAmount.width,
181
vRect.origin.y + scrollAmount.height);
184
[[scrollView contentView] scrollToPoint:
185
[[scrollView contentView] constrainScrollPoint:newOrigin]];
189
-(void) _setIsAlpha:(BOOL) flag
204
/************************************************************/
205
/*********** NSDocument subclass methods ********************/
206
/************************************************************/
208
@implementation Document
211
* NSDocument subclass method
212
* return the nib (Preview)
214
- (NSString *) windowNibName
220
* NSDocument subclass method
221
* 1- set the window frame:
222
* window origin is set to (120,100) == (WINDOW_ORIGIN_X,WINDOW_ORIGIN_Y)
223
* the windowSize have a Minsize (set in Preview.gorm)
224
* the window size is not bigger than the NSScreen:visibleFrame (-origin)
226
* 2- set _image into imageView
229
- (void) windowControllerDidLoadNib:(NSWindowController *)windowController
231
// NSSize windowSize;
233
window = [windowController window];
235
if ( ( ! window ) || (!_image ) )
240
NSSize imageSize = [_image size];
242
NSSize screenSize = [[NSScreen mainScreen] frame].size;
243
screenSize.width -= 100 + 64;
244
screenSize.height -= 120;
246
if ( screenSize.width > imageSize.width + [[scrollView verticalScroller] frame].size.width )
247
contentSize.width = imageSize.width + [[scrollView verticalScroller] frame].size.width;
250
contentSize.width = screenSize.width;
254
if ( screenSize.height > imageSize.height + [[scrollView horizontalScroller] frame].size.height )
255
contentSize.height = imageSize.height + [[scrollView horizontalScroller] frame].size.height;
258
contentSize.height = screenSize.height;
262
if ( [[_image bestRepresentationForDevice:nil] hasAlpha] )
264
[self _setIsAlpha: YES];
265
checkeredView = [[CheckeredView alloc] initWithFrame: NSMakeRect(0,0,imageSize.width,imageSize.height)];
267
[scrollView setDocumentView: checkeredView];
268
[checkeredView addSubview:imageView ];
272
[self _setIsAlpha: NO];
276
[imageView setFrame:NSMakeRect(0,0,imageSize.width,imageSize.height)];
277
[imageView setImage:_image];
279
int test = [imageView addTrackingRect:[imageView bounds]
284
[window setContentSize:contentSize];
285
[window setFrameOrigin: NSMakePoint(100,120)];
289
[[NSNotificationCenter defaultCenter] addObserver: self
290
selector: @selector(_notifyDragScroll:)
294
[[NSNotificationCenter defaultCenter] addObserver: self
295
selector: @selector(_notifyMouseDown:)
299
[[NSNotificationCenter defaultCenter] addObserver: self
300
selector: @selector(_notifyMouseUp:)
307
//Registering Objects for Services
309
[NSApp registerServicesMenuSendTypes:[[NSArray alloc] initWithObjects:NSFilenamesPboardType,nil ]
317
* NSDocument subclass method
318
* This will create a new document.
319
* if it the _image is succesfully init it set the size.
321
- (BOOL) loadDataRepresentation: (NSData*)data ofType: (NSString*)docType
323
_image = [[[NSImage alloc] initWithData: data] autorelease];
328
[self _setOriginalSize: [_image size]];
333
* NSDocument subclass method
335
- (NSData *)dataRepresentationOfType:(NSString *)aType
337
Class imageRepClass = [NSImageRep imageRepClassForFileType:aType];
339
if ( ! imageRepClass )
344
return [_image TIFFRepresentation];
348
* NSDocument subclass method
350
- (NSString *)fileType
356
- (id)validRequestorForSendType:(NSString *)sendType
357
returnType:(NSString *)returnType
363
- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pasteBoard
364
types:(NSArray *)types
366
NSArray *pbTypeArray = [[NSArray alloc] initWithObjects:NSStringPboardType,NSFilenamesPboardType,NSTIFFPboardType,nil];
368
NSData *tiffRep = [_image TIFFRepresentation];
370
[pasteBoard declareTypes:pbTypeArray owner:nil];
372
if ( [types containsObject : NSStringPboardType] )
374
if ( [pasteBoard setString: [super fileName]
375
forType: NSStringPboardType] )
378
if ( [types containsObject : NSFilenamesPboardType] )
380
if ( [pasteBoard setPropertyList: [NSArray arrayWithObject:[super fileName]]
381
forType: NSFilenamesPboardType] )
385
if ( [types containsObject : NSTIFFPboardType] )
387
if ([pasteBoard setData: tiffRep forType: NSTIFFPboardType] )
398
* This method copy NSTIFFPboardType, NSFilenamesPboardType or NSStringPboardType
399
* into the general Pasteboard
401
- (void) copy: (id) sender
403
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
404
NSData *tiffRep = [_image TIFFRepresentation];
406
NSArray *pbTypeArray = [[NSArray alloc] initWithObjects:NSStringPboardType,NSFilenamesPboardType,NSTIFFPboardType,nil];
408
[pasteBoard declareTypes: pbTypeArray owner:self];
411
if ( ! [pasteBoard setPropertyList: [[NSArray alloc] initWithObjects:[super fileName],nil]
412
forType: NSStringPboardType] )
413
NSLog(@"Problem : cannot copy NSStringPboardType");
416
if (! [pasteBoard setPropertyList: [NSArray arrayWithObject:[super fileName]]
417
forType: NSFilenamesPboardType] )
418
NSLog(@"Problem : cannot copy NSFilenamesPboardType");
420
if (! [pasteBoard setData: tiffRep forType: NSTIFFPboardType] )
421
NSLog(@"Problem : cannot copy NSTIFFPboardType");
431
-(void) resize: (id) sender
436
//Get Tag (sender comes from popUp or matrix or menu
438
if ( sender == popUp )
439
tag = [sender indexOfSelectedItem]; //popUp
440
else if ( sender == matrix )
442
tag = [[sender selectedCell] tag]; //matrix
445
tag = [sender tag]; //menu
449
//Deselect matrix cells if popPup is selected
451
if (tag <= HALF_SIZE)
453
if ( [matrix selectedCell] )
454
[matrix deselectAllCells];
456
[imageView setAutoresizingMask: NSViewNotSizable];
462
case HEIGHT_HUNDRED_PER_CENT:
463
newSize.width = [self _originalSize].width * 8;
464
newSize.height = [self _originalSize].height * 8;
466
case FOUR_HUNDRED_PER_CENT:
467
newSize.width = [self _originalSize].width * 4;
468
newSize.height = [self _originalSize].height * 4;
471
newSize.width = [self _originalSize].width * 2;
472
newSize.height = [self _originalSize].height * 2;
475
newSize.width = [self _originalSize].width ;
476
newSize.height = [self _originalSize].height;
479
newSize.width = [self _originalSize].width / 2;
480
newSize.height = [self _originalSize].height / 2;
483
newSize.width = [scrollView contentSize].width;
484
newSize.height = [scrollView contentSize].height;
485
[imageView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
486
if ( [self _isAlpha] )
489
[checkeredView setAutoresizingMask:
490
(NSViewWidthSizable|NSViewHeightSizable)];
494
newSize.width = [scrollView contentSize].width;
495
newSize.height = [imageView frame].size.height;
496
[imageView setAutoresizingMask: (NSViewWidthSizable)];
497
if ( [self _isAlpha] )
498
[checkeredView setAutoresizingMask: NSViewWidthSizable];
501
printf("problem resize default \n");
505
//set autoresizing Mask
506
if ( tag < FIT_WINDOW )
508
if ( [self _isAlpha] )
509
[checkeredView setAutoresizingMask: NSViewNotSizable];
511
[imageView setAutoresizingMask: NSViewNotSizable];
516
if ( [self _isAlpha] )
518
[checkeredView setFrame:NSMakeRect(0,0,newSize.width,newSize.height) ];
521
NSLog(@"apres newSize %@",NSStringFromSize(newSize));
523
[imageView setFrame: NSMakeRect(0,0,newSize.width,newSize.height)];
525
// why TODO FIXME !!! only need with FIT_WINDOW
526
[imageView setNeedsDisplay:YES];
527
[checkeredView setNeedsDisplay:YES];
528
[self _setCurrentItem: tag];
531
-(void) zoomImage : (id) sender
536
if ( tag == ZOOM_IN )
538
[self _setScaleFactor: (1 + SCALEFACTOR)];
541
else if ( tag == ZOOM_OUT )
543
[self _setScaleFactor: (1 -SCALEFACTOR)];
548
NSLog(@"zoomImage tag: %i",tag);
553
* window delegate method.
554
* The mini icon is generate from _image
556
- (void)windowDidMiniaturize:(NSNotification *)aNotification
558
NSImage *miniImage = _image;
559
[miniImage setSize: NSMakeSize(48,48)];
560
[window setMiniwindowImage:miniImage];
564
* window delegate method.
565
* This method is used to refresh the horizontalScroller width
567
- (void)windowDidResize:(NSNotification *)aNotification
569
NSRect scrollerRect = [[scrollView horizontalScroller] frame];
571
scrollerRect.size.width = [window frame].size.width - 135;
572
[[scrollView horizontalScroller] setFrame:scrollerRect];
577
- (BOOL) validateMenuItem: (id)menuItem
579
SEL action = [menuItem action];
581
if ( sel_isEqual(action,@selector(resize:)) )
583
if ( [menuItem tag] == [self _currentItem] )