~ubuntu-branches/ubuntu/utopic/preview.app/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/728319.patch/Document.m

  • Committer: Package Import Robot
  • Author(s): Federico Gimenez Nieto
  • Date: 2014-06-22 11:18:06 UTC
  • Revision ID: package-import@ubuntu.com-20140622111806-tvjy0bavbdxpoueo
Tags: 0.8.5-10
* 728319.patch by Yavor Doganov <yavor@gnu.org> (Closes: #728319)
* debian/control: bumped Standards-Version: 3.9.5
* debian/copyright: date modified

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
        Document.m
 
3
 
 
4
        Document class & WindowController : This file is part of Preview 
 
5
 
 
6
        Copyright (C) 2003;2004 Fabien VALLON 
 
7
        2003,2004 Alcove ( http://www.alcove.com ) 
 
8
        Additional copyrights here
 
9
 
 
10
        Authors : Fabien VALLON <fabien@sonappart.net>
 
11
        Date:   10 Oct 2003
 
12
 
 
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.
 
17
 
 
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.
 
21
 
 
22
        See the GNU General Public License for more details.
 
23
 
 
24
        You should have received a copy of the GNU General Public
 
25
        License along with this program; if not, write to:
 
26
 
 
27
                Free Software Foundation, Inc.
 
28
                59 Temple Place - Suite 330
 
29
                Boston, MA  02111-1307, USA
 
30
*/
 
31
 
 
32
// See Doumentation/DEVELOPERS
 
33
 
 
34
#include "Document.h"
 
35
 
 
36
#include <Foundation/NSNotification.h>
 
37
 
 
38
#include <AppKit/AppKit.h>
 
39
 
 
40
/*********************************************************************/
 
41
/**************** NSDocument Private methods *************************/
 
42
/*********************************************************************/
 
43
 
 
44
#define HEIGHT_HUNDRED_PER_CENT 0
 
45
#define FOUR_HUNDRED_PER_CENT   1
 
46
#define DOUBLE_SIZE             2
 
47
#define FULL_SIZE               3
 
48
#define HALF_SIZE               4
 
49
#define FIT_WINDOW              5
 
50
#define FIT_WIDTH               6
 
51
 
 
52
#define ZOOM_IN                 0
 
53
#define ZOOM_OUT                1
 
54
 
 
55
#define SCALEFACTOR            0.1
 
56
 
 
57
 
 
58
 
 
59
@interface CheckeredView: NSView
 
60
@end
 
61
 
 
62
 
 
63
@implementation CheckeredView 
 
64
 
 
65
- (void)drawRect:(NSRect)rect
 
66
{
 
67
  NSColor *backColor = [NSColor darkGrayColor];
 
68
  NSColor *color = [NSColor grayColor];
 
69
 
 
70
  [backColor set];
 
71
  NSRectFill(rect);
 
72
  [color set];
 
73
  int i, j;
 
74
  BOOL drawForeground = NO;
 
75
  for(i = 0; i < rect.size.width; i+=10)
 
76
    {
 
77
      drawForeground = i % 20 == 0;
 
78
      for(j = 0; j < rect.size.height; j+=10)
 
79
        {
 
80
          if(drawForeground)
 
81
            {
 
82
              NSRectFill(NSMakeRect(rect.origin.x+i, rect.origin.y+j, 10, 10));
 
83
            }
 
84
          drawForeground = !drawForeground;
 
85
        }
 
86
    }
 
87
 
 
88
}
 
89
 
 
90
@end
 
91
 
 
92
 
 
93
 
 
94
 
 
95
@interface Document  (Private)
 
96
-(void) _setScaleFactor: (double) factor;
 
97
-(double) _scaleFactor;
 
98
-(void) _updateImage;
 
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;
 
105
-(BOOL) _isAlpha;
 
106
@end
 
107
 
 
108
@implementation Document (Private)
 
109
 
 
110
-(void) _updateImage 
 
111
{
 
112
  if ([self _scaleFactor] != 1.0 )
 
113
    {
 
114
      NSSize imageSize;
 
115
      NSAffineTransform *affineTransform;
 
116
 
 
117
      affineTransform = [NSAffineTransform transform];
 
118
      [affineTransform scaleBy:[self _scaleFactor]];
 
119
      imageSize = [imageView frame].size;
 
120
      
 
121
      [imageView  setFrameSize:[affineTransform transformSize:imageSize]];
 
122
      if ( _isAlpha ) 
 
123
        {
 
124
          [checkeredView  setFrameSize:
 
125
                            [affineTransform transformSize:imageSize]];
 
126
        }
 
127
      
 
128
      [imageView setNeedsDisplay:YES];
 
129
 
 
130
    }
 
131
}
 
132
 
 
133
/*
 
134
 * Set the scale factor (zoom).
 
135
 */
 
136
- (void) _setScaleFactor: (double)factor
 
137
{
 
138
  _scaleFactor = factor;
 
139
}
 
140
 
 
141
- (double) _scaleFactor
 
142
{
 
143
   return _scaleFactor;
 
144
}
 
145
 
 
146
-(void) _setOriginalSize: (NSSize) orginalSize
 
147
{
 
148
  _originalSize = orginalSize;
 
149
}
 
150
 
 
151
-(NSSize) _originalSize
 
152
{
 
153
  return _originalSize;
 
154
}
 
155
 
 
156
-(void) _setCurrentItem:(unsigned) tag
 
157
{
 
158
  if (tag <= FIT_WIDTH)
 
159
    _tag = tag;
 
160
}
 
161
 
 
162
-(unsigned) _currentItem
 
163
{
 
164
  return _tag;
 
165
}
 
166
 
 
167
- (void) _notifyDragScroll: (id)notification
 
168
{
 
169
   NSPoint  newOrigin;
 
170
   NSSize   scrollAmount;
 
171
   NSSize   contentSize;
 
172
   NSRect   vRect;
 
173
   
 
174
   scrollAmount =
 
175
     [[[notification userInfo] objectForKey: @"UserInfoKeyScrollAmount"] sizeValue];
 
176
 
 
177
   vRect       = [scrollView documentVisibleRect];
 
178
   contentSize = [scrollView contentSize];
 
179
 
 
180
   newOrigin = NSMakePoint(vRect.origin.x + scrollAmount.width,
 
181
                           vRect.origin.y + scrollAmount.height);
 
182
 
 
183
 
 
184
   [[scrollView contentView] scrollToPoint:
 
185
                                [[scrollView contentView] constrainScrollPoint:newOrigin]];
 
186
}
 
187
 
 
188
 
 
189
-(void) _setIsAlpha:(BOOL) flag
 
190
{
 
191
  _isAlpha = flag;
 
192
}
 
193
 
 
194
-(BOOL) _isAlpha
 
195
{
 
196
  return _isAlpha;
 
197
}
 
198
 
 
199
 
 
200
 
 
201
@end
 
202
 
 
203
 
 
204
/************************************************************/
 
205
/*********** NSDocument subclass methods ********************/
 
206
/************************************************************/
 
207
 
 
208
@implementation Document
 
209
 
 
210
/**
 
211
 * NSDocument subclass method
 
212
 * return the nib (Preview) 
 
213
 */
 
214
- (NSString *) windowNibName
 
215
{
 
216
  return @"Preview";
 
217
}
 
218
 
 
219
/**
 
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)
 
225
 *
 
226
 * 2- set _image into imageView
 
227
 */
 
228
 
 
229
- (void) windowControllerDidLoadNib:(NSWindowController *)windowController
 
230
{
 
231
  // NSSize windowSize;
 
232
 
 
233
  window = [windowController window];
 
234
  
 
235
  if ( ( ! window ) || (!_image )  )
 
236
    return;
 
237
 
 
238
  {
 
239
    BOOL bigger = NO;
 
240
    NSSize imageSize = [_image size];
 
241
    NSSize contentSize;
 
242
    NSSize screenSize = [[NSScreen mainScreen] frame].size;
 
243
    screenSize.width -= 100 + 64;
 
244
    screenSize.height -= 120;
 
245
 
 
246
    if ( screenSize.width > imageSize.width  + [[scrollView verticalScroller] frame].size.width )
 
247
      contentSize.width = imageSize.width  + [[scrollView verticalScroller] frame].size.width;
 
248
    else
 
249
      {
 
250
        contentSize.width = screenSize.width;
 
251
        bigger = YES;
 
252
      }
 
253
    
 
254
    if ( screenSize.height > imageSize.height + [[scrollView horizontalScroller] frame].size.height ) 
 
255
      contentSize.height = imageSize.height +  [[scrollView horizontalScroller] frame].size.height; 
 
256
    else
 
257
      {
 
258
        contentSize.height = screenSize.height;
 
259
        bigger = YES;
 
260
      }
 
261
    
 
262
    if ( [[_image  bestRepresentationForDevice:nil]  hasAlpha] ) 
 
263
      {
 
264
        [self _setIsAlpha: YES];
 
265
        checkeredView = [[CheckeredView alloc] initWithFrame: NSMakeRect(0,0,imageSize.width,imageSize.height)];
 
266
        [imageView retain];
 
267
        [scrollView setDocumentView: checkeredView];
 
268
        [checkeredView addSubview:imageView ];
 
269
      }
 
270
    else 
 
271
      {
 
272
        [self _setIsAlpha: NO];
 
273
      }
 
274
 
 
275
    
 
276
    [imageView setFrame:NSMakeRect(0,0,imageSize.width,imageSize.height)];
 
277
    [imageView setImage:_image];
 
278
    
 
279
    int test = [imageView addTrackingRect:[imageView bounds]
 
280
                          owner:imageView 
 
281
                          userData:nil
 
282
                          assumeInside:YES];
 
283
 
 
284
    [window setContentSize:contentSize];
 
285
    [window setFrameOrigin: NSMakePoint(100,120)];
 
286
  }
 
287
 
 
288
  {
 
289
    [[NSNotificationCenter defaultCenter] addObserver: self
 
290
                                          selector: @selector(_notifyDragScroll:)
 
291
                                          name: @"TEST"
 
292
                                          object: imageView];
 
293
 
 
294
    [[NSNotificationCenter defaultCenter] addObserver: self
 
295
                                          selector: @selector(_notifyMouseDown:)
 
296
                                          name: @"MOUSEDOWN"
 
297
                                          object: nil];
 
298
 
 
299
    [[NSNotificationCenter defaultCenter] addObserver: self
 
300
                                          selector: @selector(_notifyMouseUp:)
 
301
                                          name: @"MOUSEUP"
 
302
                                          object: nil];
 
303
 
 
304
  }
 
305
 
 
306
 
 
307
  //Registering Objects for Services
 
308
  {
 
309
    [NSApp registerServicesMenuSendTypes:[[NSArray alloc] initWithObjects:NSFilenamesPboardType,nil ]
 
310
           returnTypes:nil];
 
311
  }
 
312
  
 
313
}
 
314
 
 
315
 
 
316
/**
 
317
 * NSDocument subclass method
 
318
 * This will create a new document. 
 
319
 * if it the _image is succesfully init it set the size.
 
320
 */
 
321
- (BOOL) loadDataRepresentation: (NSData*)data ofType: (NSString*)docType
 
322
{
 
323
  _image = [[[NSImage alloc] initWithData: data] autorelease];
 
324
  
 
325
  if (! _image )
 
326
    return NO;
 
327
 
 
328
  [self _setOriginalSize: [_image size]];
 
329
  return YES;
 
330
}
 
331
 
 
332
/**
 
333
 * NSDocument subclass method
 
334
 */
 
335
- (NSData *)dataRepresentationOfType:(NSString *)aType 
 
336
{
 
337
  Class imageRepClass = [NSImageRep imageRepClassForFileType:aType];
 
338
 
 
339
  if ( ! imageRepClass )
 
340
    {
 
341
      return nil;
 
342
    }
 
343
    
 
344
  return [_image TIFFRepresentation];
 
345
}
 
346
 
 
347
/**
 
348
 * NSDocument subclass method
 
349
 */
 
350
- (NSString *)fileType
 
351
{
 
352
  return @"tiff";
 
353
}
 
354
 
 
355
 
 
356
- (id)validRequestorForSendType:(NSString *)sendType
 
357
                     returnType:(NSString *)returnType
 
358
{
 
359
  return self;
 
360
}
 
361
 
 
362
 
 
363
- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pasteBoard
 
364
                             types:(NSArray *)types
 
365
{
 
366
  NSArray *pbTypeArray = [[NSArray alloc] initWithObjects:NSStringPboardType,NSFilenamesPboardType,NSTIFFPboardType,nil];
 
367
  BOOL ok = NO;
 
368
  NSData *tiffRep = [_image TIFFRepresentation];
 
369
 
 
370
  [pasteBoard declareTypes:pbTypeArray owner:nil];
 
371
 
 
372
  if ( [types containsObject : NSStringPboardType] ) 
 
373
    {
 
374
       if ( [pasteBoard setString: [super fileName]
 
375
                        forType: NSStringPboardType] ) 
 
376
        ok = YES;
 
377
    }
 
378
  if ( [types containsObject : NSFilenamesPboardType] ) 
 
379
    {
 
380
      if ( [pasteBoard setPropertyList: [NSArray arrayWithObject:[super fileName]]
 
381
                       forType: NSFilenamesPboardType] ) 
 
382
        ok = YES;
 
383
    }
 
384
 
 
385
  if ( [types containsObject : NSTIFFPboardType] ) 
 
386
    {
 
387
      if ([pasteBoard setData: tiffRep forType: NSTIFFPboardType] )
 
388
        ok = YES;
 
389
    }
 
390
 
 
391
  return ok;
 
392
 
 
393
 
 
394
}
 
395
 
 
396
/**
 
397
 *
 
398
 * This method copy NSTIFFPboardType, NSFilenamesPboardType or NSStringPboardType
 
399
 * into the general Pasteboard
 
400
 */
 
401
- (void) copy: (id) sender 
 
402
{  
 
403
  NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
 
404
  NSData *tiffRep = [_image TIFFRepresentation];
 
405
 
 
406
  NSArray *pbTypeArray = [[NSArray alloc] initWithObjects:NSStringPboardType,NSFilenamesPboardType,NSTIFFPboardType,nil];
 
407
  
 
408
  [pasteBoard declareTypes: pbTypeArray owner:self];
 
409
 
 
410
 
 
411
  if ( ! [pasteBoard setPropertyList: [[NSArray alloc] initWithObjects:[super fileName],nil]
 
412
                   forType: NSStringPboardType] ) 
 
413
    NSLog(@"Problem : cannot copy NSStringPboardType");
 
414
 
 
415
 
 
416
  if (! [pasteBoard setPropertyList: [NSArray arrayWithObject:[super fileName]]
 
417
                   forType: NSFilenamesPboardType] ) 
 
418
    NSLog(@"Problem : cannot copy NSFilenamesPboardType");
 
419
  
 
420
  if (! [pasteBoard setData: tiffRep forType: NSTIFFPboardType] )
 
421
    NSLog(@"Problem : cannot copy NSTIFFPboardType");
 
422
}
 
423
 
 
424
 
 
425
/**
 
426
 * Action method
 
427
 *
 
428
 *
 
429
 *
 
430
 */
 
431
-(void) resize: (id) sender
 
432
{
 
433
  unsigned tag;
 
434
  NSSize newSize;
 
435
 
 
436
  //Get Tag (sender comes from popUp or matrix or menu
 
437
  {
 
438
    if ( sender == popUp ) 
 
439
      tag = [sender indexOfSelectedItem]; //popUp
 
440
    else if ( sender == matrix ) 
 
441
      {
 
442
        tag = [[sender selectedCell] tag]; //matrix
 
443
      }
 
444
    else 
 
445
      tag = [sender tag]; //menu
 
446
  }
 
447
  
 
448
 
 
449
  //Deselect matrix cells if popPup is selected
 
450
  {
 
451
    if (tag <= HALF_SIZE) 
 
452
      {
 
453
        if (  [matrix selectedCell] )
 
454
          [matrix deselectAllCells];
 
455
        
 
456
        [imageView setAutoresizingMask: NSViewNotSizable];
 
457
      }
 
458
  }
 
459
 
 
460
  switch (tag)
 
461
    {
 
462
    case HEIGHT_HUNDRED_PER_CENT: 
 
463
      newSize.width = [self _originalSize].width * 8;
 
464
      newSize.height = [self _originalSize].height * 8;
 
465
      break;
 
466
    case FOUR_HUNDRED_PER_CENT: 
 
467
      newSize.width = [self _originalSize].width * 4;
 
468
      newSize.height = [self _originalSize].height * 4;
 
469
      break;
 
470
    case DOUBLE_SIZE:
 
471
      newSize.width = [self _originalSize].width * 2;
 
472
      newSize.height = [self _originalSize].height * 2;
 
473
      break;
 
474
    case FULL_SIZE: 
 
475
      newSize.width = [self _originalSize].width ;
 
476
      newSize.height = [self _originalSize].height;
 
477
      break;
 
478
   case HALF_SIZE:
 
479
      newSize.width = [self _originalSize].width / 2;
 
480
      newSize.height = [self _originalSize].height / 2;
 
481
      break;
 
482
   case FIT_WINDOW:
 
483
     newSize.width = [scrollView contentSize].width;
 
484
     newSize.height = [scrollView contentSize].height;
 
485
     [imageView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
 
486
     if ( [self _isAlpha] ) 
 
487
        {
 
488
          NSLog(@"isAlpha");
 
489
          [checkeredView setAutoresizingMask: 
 
490
                           (NSViewWidthSizable|NSViewHeightSizable)];
 
491
        }
 
492
     break;
 
493
   case FIT_WIDTH:
 
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];
 
499
      break; 
 
500
    default: 
 
501
      printf("problem resize default \n");
 
502
      return;
 
503
    }
 
504
 
 
505
  //set autoresizing Mask
 
506
  if ( tag < FIT_WINDOW ) 
 
507
    {
 
508
      if ( [self  _isAlpha] ) 
 
509
        [checkeredView setAutoresizingMask: NSViewNotSizable];
 
510
 
 
511
      [imageView setAutoresizingMask: NSViewNotSizable];
 
512
    }
 
513
 
 
514
  
 
515
  //Resize
 
516
  if ( [self _isAlpha] ) 
 
517
    {
 
518
      [checkeredView setFrame:NSMakeRect(0,0,newSize.width,newSize.height) ];
 
519
    }
 
520
  
 
521
  NSLog(@"apres newSize %@",NSStringFromSize(newSize));
 
522
 
 
523
  [imageView setFrame: NSMakeRect(0,0,newSize.width,newSize.height)];
 
524
    
 
525
  // why TODO FIXME !!! only need with FIT_WINDOW 
 
526
  [imageView setNeedsDisplay:YES];
 
527
  [checkeredView setNeedsDisplay:YES];
 
528
  [self _setCurrentItem: tag];
 
529
}
 
530
 
 
531
-(void) zoomImage : (id) sender
 
532
{
 
533
  unsigned tag;
 
534
  tag = [sender tag];
 
535
 
 
536
  if ( tag == ZOOM_IN ) 
 
537
    {
 
538
      [self _setScaleFactor: (1 + SCALEFACTOR)];
 
539
      [self _updateImage];
 
540
    }
 
541
  else if ( tag == ZOOM_OUT ) 
 
542
    {
 
543
      [self _setScaleFactor: (1 -SCALEFACTOR)];
 
544
      [self _updateImage];
 
545
    }
 
546
  else 
 
547
    {
 
548
      NSLog(@"zoomImage tag: %i",tag);
 
549
    }
 
550
}
 
551
 
 
552
/**
 
553
 * window delegate method.
 
554
 * The mini icon is generate from _image
 
555
 */
 
556
- (void)windowDidMiniaturize:(NSNotification *)aNotification
 
557
{
 
558
  NSImage *miniImage = _image;
 
559
  [miniImage setSize: NSMakeSize(48,48)];
 
560
  [window setMiniwindowImage:miniImage];
 
561
}
 
562
 
 
563
/**
 
564
 * window delegate method.
 
565
 * This method is used to refresh the horizontalScroller width
 
566
 */
 
567
- (void)windowDidResize:(NSNotification *)aNotification
 
568
{
 
569
  NSRect scrollerRect =   [[scrollView horizontalScroller] frame];
 
570
 
 
571
  scrollerRect.size.width = [window frame].size.width - 135;
 
572
  [[scrollView horizontalScroller] setFrame:scrollerRect];
 
573
}
 
574
 
 
575
 
 
576
//Validate Menu : 
 
577
- (BOOL) validateMenuItem: (id)menuItem
 
578
{
 
579
  SEL action = [menuItem action];
 
580
 
 
581
  if ( sel_isEqual(action,@selector(resize:)) )
 
582
    {
 
583
      if ( [menuItem tag] == [self _currentItem] ) 
 
584
        return NO;      
 
585
    }
 
586
 
 
587
  return YES;
 
588
}
 
589
 
 
590
 
 
591
@end
 
592