File: Document.m

package info (click to toggle)
preview.app 0.8.5-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 508 kB
  • sloc: objc: 487; makefile: 57; sh: 17
file content (582 lines) | stat: -rw-r--r-- 13,588 bytes parent folder | download | duplicates (2)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
 /*
        Document.m

	Document class & WindowController : This file is part of Preview 

	Copyright (C) 2003;2004 Fabien VALLON 
	2003,2004 Alcove ( http://www.alcove.com ) 
	Additional copyrights here

	Authors : Fabien VALLON <fabien@sonappart.net>
	Date:	10 Oct 2003

	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:

		Free Software Foundation, Inc.
		59 Temple Place - Suite 330
		Boston, MA  02111-1307, USA
*/

// See Doumentation/DEVELOPERS

#include "Document.h"

#include <Foundation/NSNotification.h>

#include <AppKit/AppKit.h>

/*********************************************************************/
/**************** NSDocument Private methods *************************/
/*********************************************************************/

#define HEIGHT_HUNDRED_PER_CENT 0
#define FOUR_HUNDRED_PER_CENT   1
#define DOUBLE_SIZE             2
#define FULL_SIZE               3
#define HALF_SIZE               4
#define FIT_WINDOW              5
#define FIT_WIDTH               6

#define ZOOM_IN                 0
#define ZOOM_OUT                1

#define SCALEFACTOR            0.1



@interface CheckeredView: NSView
@end


@implementation CheckeredView 

- (void)drawRect:(NSRect)rect
{
  NSColor *backColor = [NSColor darkGrayColor];
  NSColor *color = [NSColor grayColor];

  [backColor set];
  NSRectFill(rect);
  [color set];
  int i, j;
  BOOL drawForeground = NO;
  for(i = 0; i < rect.size.width; i+=10)
    {
      drawForeground = i % 20 == 0;
      for(j = 0; j < rect.size.height; j+=10)
        {
	  if(drawForeground)
            {
	      NSRectFill(NSMakeRect(rect.origin.x+i, rect.origin.y+j, 10, 10));
            }
	  drawForeground = !drawForeground;
        }
    }

}

@end




@interface Document  (Private)
-(void) _setScaleFactor: (double) factor;
-(double) _scaleFactor;
-(void) _updateImage;
-(void) _setOriginalSize: (NSSize) originalSize;
-(NSSize) _originalSize;
-(void) _setCurrentItem:(unsigned) tag;
-(unsigned) _currentItem;
-(void) _notifyDragScroll: (id)notification;
-(void) _setIsAlpha:(BOOL) flag;
-(BOOL) _isAlpha;
@end

@implementation Document (Private)

-(void) _updateImage 
{
  if ([self _scaleFactor] != 1.0 )
    {
      NSSize imageSize;
      NSAffineTransform *affineTransform;

      affineTransform = [NSAffineTransform transform];
      [affineTransform scaleBy:[self _scaleFactor]];
      imageSize = [imageView frame].size;
      
      [imageView  setFrameSize:[affineTransform transformSize:imageSize]];
      if ( _isAlpha ) 
	{
	  [checkeredView  setFrameSize:
			    [affineTransform transformSize:imageSize]];
	}
      
      [imageView setNeedsDisplay:YES];

    }
}

/*
 * Set the scale factor (zoom).
 */
- (void) _setScaleFactor: (double)factor
{
  _scaleFactor = factor;
}

- (double) _scaleFactor
{
   return _scaleFactor;
}

-(void) _setOriginalSize: (NSSize) orginalSize
{
  _originalSize = orginalSize;
}

-(NSSize) _originalSize
{
  return _originalSize;
}

-(void) _setCurrentItem:(unsigned) tag
{
  if (tag <= FIT_WIDTH)
    _tag = tag;
}

-(unsigned) _currentItem
{
  return _tag;
}

- (void) _notifyDragScroll: (id)notification
{
   NSPoint  newOrigin;
   NSSize   scrollAmount;
   NSSize   contentSize;
   NSRect   vRect;
   
   scrollAmount =
     [[[notification userInfo] objectForKey: @"UserInfoKeyScrollAmount"] sizeValue];

   vRect       = [scrollView documentVisibleRect];
   contentSize = [scrollView contentSize];

   newOrigin = NSMakePoint(vRect.origin.x + scrollAmount.width,
                           vRect.origin.y + scrollAmount.height);


   [[scrollView contentView] scrollToPoint:
			        [[scrollView contentView] constrainScrollPoint:newOrigin]];
}


-(void) _setIsAlpha:(BOOL) flag
{
  _isAlpha = flag;
}

-(BOOL) _isAlpha
{
  return _isAlpha;
}



@end


/************************************************************/
/*********** NSDocument subclass methods ********************/
/************************************************************/

@implementation Document

/**
 * NSDocument subclass method
 * return the nib (Preview) 
 */
- (NSString *) windowNibName
{
  return @"Preview";
}

/**
 * NSDocument subclass method
 * 1- set the window frame: 
 * window origin is set to (120,100) == (WINDOW_ORIGIN_X,WINDOW_ORIGIN_Y)
 * the windowSize have a Minsize (set in Preview.gorm)
 * the window size is not bigger than the NSScreen:visibleFrame (-origin)
 *
 * 2- set _image into imageView
 */

- (void) windowControllerDidLoadNib:(NSWindowController *)windowController
{
  // NSSize windowSize;

  window = [windowController window];
  
  if ( ( ! window ) || (!_image )  )
    return;

  {
    BOOL bigger = NO;
    NSSize imageSize = [_image size];
    NSSize contentSize;
    NSSize screenSize = [[NSScreen mainScreen] frame].size;
    screenSize.width -= 100 + 64;
    screenSize.height -= 120;

    if ( screenSize.width > imageSize.width  + [[scrollView verticalScroller] frame].size.width )
      contentSize.width = imageSize.width  + [[scrollView verticalScroller] frame].size.width;
    else
      {
	contentSize.width = screenSize.width;
	bigger = YES;
      }
    
    if ( screenSize.height > imageSize.height + [[scrollView horizontalScroller] frame].size.height ) 
      contentSize.height = imageSize.height +  [[scrollView horizontalScroller] frame].size.height; 
    else
      {
	contentSize.height = screenSize.height;
	bigger = YES;
      }
    
    if ( [[_image  bestRepresentationForDevice:nil]  hasAlpha] ) 
      {
	[self _setIsAlpha: YES];
	checkeredView = [[CheckeredView alloc] initWithFrame: NSMakeRect(0,0,imageSize.width,imageSize.height)];
	[imageView retain];
	[scrollView setDocumentView: checkeredView];
 	[checkeredView addSubview:imageView ];
      }
    else 
      {
	[self _setIsAlpha: NO];
      }

    
    [imageView setFrame:NSMakeRect(0,0,imageSize.width,imageSize.height)];
    [imageView setImage:_image];
    
    int test = [imageView addTrackingRect:[imageView bounds]
			  owner:imageView 
			  userData:nil
			  assumeInside:YES];

    [window setContentSize:contentSize];
    [window setFrameOrigin: NSMakePoint(100,120)];
  }

  {
    [[NSNotificationCenter defaultCenter] addObserver: self
					  selector: @selector(_notifyDragScroll:)
					  name: @"TEST"
					  object: imageView];

  }


  //Registering Objects for Services
  {
    [NSApp registerServicesMenuSendTypes:[[NSArray alloc] initWithObjects:NSFilenamesPboardType,nil ]
	   returnTypes:nil];
  }
  
}


/**
 * NSDocument subclass method
 * This will create a new document. 
 * if it the _image is succesfully init it set the size.
 */
- (BOOL) loadDataRepresentation: (NSData*)data ofType: (NSString*)docType
{
  _image = [[[NSImage alloc] initWithData: data] autorelease];
  
  if (! _image )
    return NO;

  [self _setOriginalSize: [_image size]];
  return YES;
}

/**
 * NSDocument subclass method
 */
- (NSData *)dataRepresentationOfType:(NSString *)aType 
{
  Class imageRepClass = [NSImageRep imageRepClassForFileType:aType];

  if ( ! imageRepClass )
    {
      return nil;
    }
    
  return [_image TIFFRepresentation];
}

/**
 * NSDocument subclass method
 */
- (NSString *)fileType
{
  return @"tiff";
}


- (id)validRequestorForSendType:(NSString *)sendType
		     returnType:(NSString *)returnType
{
  return self;
}


- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pasteBoard
			     types:(NSArray *)types
{
  NSArray *pbTypeArray = [[NSArray alloc] initWithObjects:NSStringPboardType,NSFilenamesPboardType,NSTIFFPboardType,nil];
  BOOL ok = NO;
  NSData *tiffRep = [_image TIFFRepresentation];

  [pasteBoard declareTypes:pbTypeArray owner:nil];

  if ( [types containsObject : NSStringPboardType] ) 
    {
       if ( [pasteBoard setString: [super fileName]
			forType: NSStringPboardType] ) 
 	ok = YES;
    }
  if ( [types containsObject : NSFilenamesPboardType] ) 
    {
      if ( [pasteBoard setPropertyList: [NSArray arrayWithObject:[super fileName]]
		       forType: NSFilenamesPboardType] ) 
	ok = YES;
    }

  if ( [types containsObject : NSTIFFPboardType] ) 
    {
      if ([pasteBoard setData: tiffRep forType: NSTIFFPboardType] )
	ok = YES;
    }

  return ok;


}

/**
 *
 * This method copy NSTIFFPboardType, NSFilenamesPboardType or NSStringPboardType
 * into the general Pasteboard
 */
- (void) copy: (id) sender 
{  
  NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
  NSData *tiffRep = [_image TIFFRepresentation];

  NSArray *pbTypeArray = [[NSArray alloc] initWithObjects:NSStringPboardType,NSFilenamesPboardType,NSTIFFPboardType,nil];
  
  [pasteBoard declareTypes: pbTypeArray owner:self];


  if ( ! [pasteBoard setPropertyList: [[NSArray alloc] initWithObjects:[super fileName],nil]
		   forType: NSStringPboardType] ) 
    NSLog(@"Problem : cannot copy NSStringPboardType");


  if (! [pasteBoard setPropertyList: [NSArray arrayWithObject:[super fileName]]
		   forType: NSFilenamesPboardType] ) 
    NSLog(@"Problem : cannot copy NSFilenamesPboardType");
  
  if (! [pasteBoard setData: tiffRep forType: NSTIFFPboardType] )
    NSLog(@"Problem : cannot copy NSTIFFPboardType");
}


/**
 * Action method
 *
 *
 *
 */
-(void) resize: (id) sender
{
  unsigned tag;
  NSSize newSize;

  //Get Tag (sender comes from popUp or matrix or menu
  {
    if ( sender == popUp ) 
      tag = [sender indexOfSelectedItem]; //popUp
    else if ( sender == matrix ) 
      {
	tag = [[sender selectedCell] tag]; //matrix
      }
    else 
      tag = [sender tag]; //menu
  }
  

  //Deselect matrix cells if popPup is selected
  {
    if (tag <= HALF_SIZE) 
      {
	if (  [matrix selectedCell] )
	  [matrix deselectAllCells];
	
	[imageView setAutoresizingMask: NSViewNotSizable];
      }
  }

  switch (tag)
    {
    case HEIGHT_HUNDRED_PER_CENT: 
      newSize.width = [self _originalSize].width * 8;
      newSize.height = [self _originalSize].height * 8;
      break;
    case FOUR_HUNDRED_PER_CENT: 
      newSize.width = [self _originalSize].width * 4;
      newSize.height = [self _originalSize].height * 4;
      break;
    case DOUBLE_SIZE:
      newSize.width = [self _originalSize].width * 2;
      newSize.height = [self _originalSize].height * 2;
      break;
    case FULL_SIZE: 
      newSize.width = [self _originalSize].width ;
      newSize.height = [self _originalSize].height;
      break;
   case HALF_SIZE:
      newSize.width = [self _originalSize].width / 2;
      newSize.height = [self _originalSize].height / 2;
      break;
   case FIT_WINDOW:
     newSize.width = [scrollView contentSize].width;
     newSize.height = [scrollView contentSize].height;
     [imageView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
     if ( [self _isAlpha] ) 
	{
	  NSLog(@"isAlpha");
	  [checkeredView setAutoresizingMask: 
			   (NSViewWidthSizable|NSViewHeightSizable)];
	}
     break;
   case FIT_WIDTH:
      newSize.width = [scrollView contentSize].width;
      newSize.height = [imageView frame].size.height;
      [imageView setAutoresizingMask: (NSViewWidthSizable)];
      if ( [self _isAlpha] ) 
	[checkeredView setAutoresizingMask: NSViewWidthSizable];
      break; 
    default: 
      printf("problem resize default \n");
      return;
    }

  //set autoresizing Mask
  if ( tag < FIT_WINDOW ) 
    {
      if ( [self  _isAlpha] ) 
	[checkeredView setAutoresizingMask: NSViewNotSizable];

      [imageView setAutoresizingMask: NSViewNotSizable];
    }

  
  //Resize
  if ( [self _isAlpha] ) 
    {
      [checkeredView setFrame:NSMakeRect(0,0,newSize.width,newSize.height) ];
    }
  
  NSLog(@"apres newSize %@",NSStringFromSize(newSize));

  [imageView setFrame: NSMakeRect(0,0,newSize.width,newSize.height)];
    
  // why TODO FIXME !!! only need with FIT_WINDOW 
  [imageView setNeedsDisplay:YES];
  [checkeredView setNeedsDisplay:YES];
  [self _setCurrentItem: tag];
}

-(void) zoomImage : (id) sender
{
  unsigned tag;
  tag = [sender tag];

  if ( tag == ZOOM_IN ) 
    {
      [self _setScaleFactor: (1 + SCALEFACTOR)];
      [self _updateImage];
    }
  else if ( tag == ZOOM_OUT ) 
    {
      [self _setScaleFactor: (1 -SCALEFACTOR)];
      [self _updateImage];
    }
  else 
    {
      NSLog(@"zoomImage tag: %i",tag);
    }
}

/**
 * window delegate method.
 * The mini icon is generate from _image
 */
- (void)windowDidMiniaturize:(NSNotification *)aNotification
{
  NSImage *miniImage = _image;
  [miniImage setSize: NSMakeSize(48,48)];
  [window setMiniwindowImage:miniImage];
}

/**
 * window delegate method.
 * This method is used to refresh the horizontalScroller width
 */
- (void)windowDidResize:(NSNotification *)aNotification
{
  NSRect scrollerRect =   [[scrollView horizontalScroller] frame];

  scrollerRect.size.width = [window frame].size.width - 135;
  [[scrollView horizontalScroller] setFrame:scrollerRect];
}


//Validate Menu : 
- (BOOL) validateMenuItem: (id)menuItem
{
  SEL action = [menuItem action];

  if ( sel_isEqual(action,@selector(resize:)) )
    {
      if ( [menuItem tag] == [self _currentItem] ) 
	return NO;	
    }

  return YES;
}


@end