1// 2// PRWindowController.m 3// PRICE 4// 5// Created by Riccardo Mottola on Thu Dec 12 2002. 6// Copyright (c) 2002-2014 Carduus. All rights reserved. 7// 8// This application 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. 9// 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. 10 11#import "PRWindowController.h" 12#import "MyDocument.h" 13 14 15@implementation PRWindowController 16 17- (void)dealloc 18{ 19 /* remove observer for the window resize */ 20 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:[self window]]; 21 22 [super dealloc]; 23} 24 25 26- (void)scaleFromMenu:(id)sender 27{ 28 int tag; 29 NSSize currViewSize; 30 NSSize imgSize; 31 float hscale, vscale; 32 33 tag = [[scalePopUp selectedItem] tag]; 34 currViewSize = [[[view enclosingScrollView] contentView] visibleRect].size; 35 imgSize = [[[self document] activeImage] size]; 36 switch (tag) 37 { 38 case -4: 39 [scalePanel makeKeyAndOrderFront:nil]; 40 break; 41 case -3: 42 vscale = currViewSize.height / imgSize.height; 43 hscale = currViewSize.width / imgSize.width; 44 if (hscale > vscale) 45 [self scaleImageTo:vscale]; 46 else 47 [self scaleImageTo:hscale]; 48 break; 49 case -2: 50 hscale = currViewSize.width / imgSize.width; 51 NSLog(@"%f %f", currViewSize.width, imgSize.width); 52 [self scaleImageTo:hscale]; 53 break; 54 case -1: 55 vscale = currViewSize.height / imgSize.height; 56 NSLog(@"%f %f", currViewSize.height, imgSize.height); 57 [self scaleImageTo:vscale]; 58 break; 59 case 12: 60 [self scaleImageTo:0.125]; 61 break; 62 case 25: 63 [self scaleImageTo:0.25]; 64 break; 65 case 50: 66 [self scaleImageTo:0.5]; 67 break; 68 case 75: 69 [self scaleImageTo:0.75]; 70 break; 71 case 100: 72 [self scaleImageTo:1.0]; 73 break; 74 case 150: 75 [self scaleImageTo:1.55]; 76 break; 77 case 200: 78 [self scaleImageTo:2.0]; 79 break; 80 case 400: 81 [self scaleImageTo:4.0]; 82 break; 83 default: 84 NSLog(@"unexpected case in scale menu selection"); 85 break; 86 } 87} 88 89- (void)scalePanelOk:(id)sender 90{ 91 float percent; 92 93 percent = [scalePanelScaleField floatValue]; 94 [scalePanel performClose:nil]; 95 [self scaleImageTo:percent/100.0]; 96} 97 98- (void)scalePanelCancel:(id)sender 99{ 100 [scalePanel performClose:nil]; 101} 102 103 104/* stores the given value as a scale factor */ 105- (void)scaleImageTo:(float)internal_scale 106{ 107 if (internal_scale > 0) 108 scale = internal_scale; 109 [self scaleImage]; 110} 111 112/* scales the image by the current scale factor */ 113- (void)scaleImage 114{ 115 [view scaleFrameBy:scale]; 116} 117 118/** method called as a notification from the window resize 119or if scale preferences changed */ 120- (void)_windowDidResize :(NSNotification *)notif 121{ 122 int tag; 123 NSSize currViewSize; 124 NSSize imgSize; 125 float hscale, vscale; 126 127 tag = [[scalePopUp selectedItem] tag]; 128 currViewSize = [[[view enclosingScrollView] contentView] visibleRect].size; 129 imgSize = [[[self document] activeImage] pixelSize]; 130 switch (tag) 131 { 132 case -3: 133 vscale = currViewSize.height / imgSize.height; 134 hscale = currViewSize.width / imgSize.width; 135 if (hscale > vscale) 136 [self scaleImageTo:vscale]; 137 else 138 [self scaleImageTo:hscale]; 139 break; 140 case -2: 141 hscale = currViewSize.width / imgSize.width; 142 NSLog(@"%f %f", currViewSize.width, imgSize.width); 143 [self scaleImageTo:hscale]; 144 break; 145 case -1: 146 vscale = currViewSize.height / imgSize.height; 147 NSLog(@"%f %f", currViewSize.height, imgSize.height); 148 [self scaleImageTo:vscale]; 149 break; 150 default: 151 break; 152 } 153} 154 155- (void)windowDidLoad 156/* some initialization stuff */ 157{ 158 NSWindow *currWin; 159 NSRect currentWinContentRect; 160 NSSize currentWindowSize; 161 NSSize currViewSize; 162 NSSize currImageSize; 163 NSSize windowToViewBorder; 164 NSSize newWinSize; 165 NSSize minWinSize; 166 NSSize screenFrameSize; 167 PRImage *currImage; 168 BOOL shouldResize; 169 170 shouldResize = NO; 171 172 /* views coming froma NIB window need a retain, we need this for the save panel */ 173 [saveOptionsView retain]; 174 [fileTypePopUp setTarget: [self document]]; 175 [fileTypePopUp setAction: @selector(changeSaveType:)]; 176 177 /* display the data by MyDocument's activeImage method */ 178 currImage = [[self document] activeImage]; 179 180 [self setImageToDraw:currImage]; 181 [self scaleImageTo:1.0]; 182 183 /* resize the window accordingly to the image's size */ 184 screenFrameSize = [[NSScreen mainScreen] visibleFrame].size; 185 NSLog(@"current screen size: %f x %f", screenFrameSize.width, screenFrameSize.height); 186 187 /* get the current window size and limits */ 188 currWin = [self window]; 189 currentWinContentRect = [NSWindow contentRectForFrameRect:[currWin frame] styleMask:[currWin styleMask]]; 190 currentWindowSize = currentWinContentRect.size; 191 minWinSize = [currWin minSize]; 192 NSLog(@"current window size: %f x %f", currentWindowSize.width, currentWindowSize.height); 193 newWinSize = currentWindowSize; 194 195 /* get size of the visible part of the view */ 196 currViewSize = [[view enclosingScrollView] contentSize]; 197 198 /* calculate the difference that makes up the border */ 199 windowToViewBorder.width = currentWindowSize.width - currViewSize.width; 200 windowToViewBorder.height = currentWindowSize.height - currViewSize.height; 201 202 /* get the image size */ 203 currImageSize = [currImage size]; 204 205 /* resize if image is smaller */ 206 if (currImageSize.width < currViewSize.width) 207 { 208 float newViewWidth; 209 210 shouldResize = YES; 211 if (currImageSize.width > minWinSize.width) 212 newViewWidth = currImageSize.width; 213 else 214 newViewWidth = minWinSize.width; 215 newWinSize.width = newViewWidth + windowToViewBorder.width; 216 } 217 if (currImageSize.height < currViewSize.height) 218 { 219 float newViewHeight; 220 221 shouldResize = YES; 222 if (currImageSize.height > minWinSize.height) 223 newViewHeight = currImageSize.height; 224 else 225 newViewHeight = minWinSize.height; 226 newWinSize.height = newViewHeight + windowToViewBorder.height; 227 } 228 229 /* resize if image is larger */ 230 if ([[NSApp delegate] prefEnlargeWindows]) 231 { 232 if (currImageSize.width > currViewSize.width) 233 { 234 float newViewWidth; 235 236 shouldResize = YES; 237 if (currImageSize.width < screenFrameSize.width) 238 newViewWidth = currImageSize.width; 239 else 240 newViewWidth = screenFrameSize.width - currentWinContentRect.origin.x - windowToViewBorder.width; 241 newWinSize.width = newViewWidth + windowToViewBorder.width; 242 } 243 if (currImageSize.height > currViewSize.height) 244 { 245 float newViewHeight; 246 247 shouldResize = YES; 248 if (currImageSize.height < screenFrameSize.height) 249 newViewHeight = currImageSize.height; 250 else 251 newViewHeight = screenFrameSize.height - windowToViewBorder.height; 252 newWinSize.height = newViewHeight + windowToViewBorder.height; 253 } 254 } 255 256 if (shouldResize) 257 { 258 [currWin setContentSize:newWinSize]; 259 } 260 261 /* add an observer for the window resize */ 262 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowDidResize:) name:NSWindowDidResizeNotification object:currWin]; 263} 264 265/* view accessor */ 266- (PRImageView *)view 267{ 268 return view; 269} 270 271 272- (void)setImageToDraw:(PRImage *)image 273{ 274 [view setImage:image]; 275 [self setImageInfo:image]; 276} 277 278- (void)setImageInfo:(PRImage *)image 279{ 280 [imageInfoLine setStringValue: [NSString stringWithFormat: 281 @"%ld x %ld, %ldx%ld bpp", 282 (long int)[image width], 283 (long int)[image height], 284 (long int)[image bitsPerSample], 285 (long int)[image samplesPerPixel] 286 ]]; 287} 288 289/** Sets the FileType popup content*/ 290- (void)setWritableFileTypes:(NSArray *)types 291{ 292 NSString *type, *title; 293 unsigned i; 294 unsigned count = [types count]; 295 296 [fileTypePopUp removeAllItems]; 297 for (i = 0; i < count; i++) 298 { 299 type = [types objectAtIndex: i]; 300 title = [[NSDocumentController sharedDocumentController] displayNameForType: type]; 301 [fileTypePopUp addItemWithTitle: title]; 302 [[fileTypePopUp itemAtIndex: i] setRepresentedObject: type]; 303 } 304} 305 306 307/* ===== delegates =====*/ 308 309- (BOOL) prepareSavePanel:(NSSavePanel *) panel 310{ 311 [panel setAccessoryView:saveOptionsView]; 312 313 return YES; 314} 315 316- (void)changeSaveType:(id)sender 317{ 318 NSLog(@"changeSaveType type:%@", [[sender selectedItem] representedObject]); 319 [self setCompressionType:[[sender selectedItem] representedObject]]; 320} 321 322/* save panel delegates */ 323/** change the file type */ 324- (void)setCompressionType:(NSString *)type 325{ 326 NSString *title; 327 328 title = [[NSDocumentController sharedDocumentController] displayNameForType: type]; 329 330 if ([fileTypePopUp itemWithTitle:title] == nil) 331 { 332 NSLog(@"%@ type not found, defaulting to TIFF", type); 333 type = @"TIFF"; 334 title = [[NSDocumentController sharedDocumentController] displayNameForType: type]; 335 } 336 [fileTypePopUp selectItemWithTitle: title]; 337 338 if ([type isEqualToString:@"TIFF"]) 339 { 340 NSLog(@"set type to Tiff"); 341 [jpegCompressionSlider setEnabled:NO]; 342 [jpegCompressionField setEnabled:NO]; 343 } 344 else if ([type isEqualToString:@"JPEG"]) 345 { 346 NSLog(@"set type to Jpeg"); 347 [jpegCompressionSlider setEnabled:YES]; 348 [jpegCompressionField setEnabled:YES]; 349 /* simulate click to make interface consistent */ 350 [jpegCompressionSlider performClick:nil]; 351 } 352} 353 354/** keep the slider and the text view of the compression level in sync */ 355- (IBAction)setCompressionLevel:(id)sender 356{ 357 if (sender == jpegCompressionField) 358 [jpegCompressionSlider takeFloatValueFrom:sender]; 359 else 360 [jpegCompressionField takeFloatValueFrom:sender]; 361 compressionLevel = [sender floatValue] / 100; 362} 363 364- (float)compressionLevel 365{ 366 return compressionLevel; 367} 368 369 370@end 371