OSDN Git Service

MacGui: Fix issue where selecting the chapter tab can crash the macgui if no valid...
[handbrake-jp/handbrake-jp-git.git] / macosx / PictureController.mm
1 /* $Id: PictureController.mm,v 1.11 2005/08/01 15:10:44 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "PictureController.h"
8
9 @interface PictureController (Private)
10
11 - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize;
12 - (void)resizeSheetForViewSize: (NSSize)viewSize;
13 - (void)setViewSize: (NSSize)viewSize;
14 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize;
15
16 @end
17
18 @implementation PictureController
19
20 - (id)initWithDelegate:(id)del
21 {
22         if (self = [super init])
23         {
24                 delegate = del;
25         [self loadMyNibFile];
26         fPicturePreviews = [[NSMutableDictionary dictionaryWithCapacity: HB_NUM_HBLIB_PICTURES] retain];
27         }
28         return self;
29 }
30
31 - (void) dealloc
32 {
33     [fPicturePreviews release];
34     [super dealloc];
35 }
36
37 - (void) SetHandle: (hb_handle_t *) handle
38 {
39     fHandle = handle;
40
41     [fWidthStepper  setValueWraps: NO];
42     [fWidthStepper  setIncrement: 16];
43     [fWidthStepper  setMinValue: 64];
44     [fHeightStepper setValueWraps: NO];
45     [fHeightStepper setIncrement: 16];
46     [fHeightStepper setMinValue: 64];
47
48     [fCropTopStepper    setIncrement: 2];
49     [fCropTopStepper    setMinValue:  0];
50     [fCropBottomStepper setIncrement: 2];
51     [fCropBottomStepper setMinValue:  0];
52     [fCropLeftStepper   setIncrement: 2];
53     [fCropLeftStepper   setMinValue:  0];
54     [fCropRightStepper  setIncrement: 2];
55     [fCropRightStepper  setMinValue:  0];
56 }
57
58 - (void) SetTitle: (hb_title_t *) title
59 {
60     hb_job_t * job = title->job;
61
62     fTitle = title;
63
64     [fWidthStepper      setMaxValue: title->width];
65     [fWidthStepper      setIntValue: job->width];
66     [fWidthField        setIntValue: job->width];
67     [fHeightStepper     setMaxValue: title->height];
68     [fHeightStepper     setIntValue: job->height];
69     [fHeightField       setIntValue: job->height];
70     [fRatioCheck        setState:    job->keep_ratio ? NSOnState : NSOffState];
71     [fCropTopStepper    setMaxValue: title->height/2-2];
72     [fCropBottomStepper setMaxValue: title->height/2-2];
73     [fCropLeftStepper   setMaxValue: title->width/2-2];
74     [fCropRightStepper  setMaxValue: title->width/2-2];
75
76     /* Populate the Anamorphic NSPopUp button here */
77     [fAnamorphicPopUp removeAllItems];
78     [fAnamorphicPopUp addItemWithTitle: @"None"];
79     [fAnamorphicPopUp addItemWithTitle: @"Strict"];
80     if (allowLooseAnamorphic)
81     {
82     [fAnamorphicPopUp addItemWithTitle: @"Loose"];
83     }
84     [fAnamorphicPopUp selectItemAtIndex: job->pixel_ratio];
85     
86     /* We initially set the previous state of keep ar to on */
87     keepAspectRatioPreviousState = 1;
88         if (!autoCrop)
89         {
90         [fCropMatrix  selectCellAtRow: 1 column:0];
91         /* If auto, lets set the crop steppers according to current job->crop values */
92         [fCropTopStepper    setIntValue: job->crop[0]];
93         [fCropTopField      setIntValue: job->crop[0]];
94         [fCropBottomStepper setIntValue: job->crop[1]];
95         [fCropBottomField   setIntValue: job->crop[1]];
96         [fCropLeftStepper   setIntValue: job->crop[2]];
97         [fCropLeftField     setIntValue: job->crop[2]];
98         [fCropRightStepper  setIntValue: job->crop[3]];
99         [fCropRightField    setIntValue: job->crop[3]];
100         }
101         else
102         {
103         [fCropMatrix  selectCellAtRow: 0 column:0];
104         }
105         
106         /* Set filters widgets according to the filters struct */
107         [fVFRCheck setState:fPictureFilterSettings.vfr];
108     [fDetelecineCheck setState:fPictureFilterSettings.detelecine];
109     [fDeinterlacePopUp selectItemAtIndex: fPictureFilterSettings.deinterlace];
110     [fDenoisePopUp selectItemAtIndex: fPictureFilterSettings.denoise];
111     [fDeblockCheck setState: fPictureFilterSettings.deblock];
112     
113     fPicture = 0;
114     MaxOutputWidth = title->width - job->crop[2] - job->crop[3];
115     MaxOutputHeight = title->height - job->crop[0] - job->crop[1];
116     [self SettingsChanged: nil];
117 }
118
119 /* we use this to setup the initial picture filters upon first launch, after that their states
120 are maintained across different sources */
121 - (void) setInitialPictureFilters
122 {
123         /* we use a popup to show the deinterlace settings */
124         [fDeinterlacePopUp removeAllItems];
125     [fDeinterlacePopUp addItemWithTitle: @"None"];
126     [fDeinterlacePopUp addItemWithTitle: @"Fast"];
127     [fDeinterlacePopUp addItemWithTitle: @"Slow"];
128         [fDeinterlacePopUp addItemWithTitle: @"Slower"];
129         [fDeinterlacePopUp addItemWithTitle: @"Slowest"];
130     
131         /* Set deinterlaces level according to the integer in the main window */
132         [fDeinterlacePopUp selectItemAtIndex: fPictureFilterSettings.deinterlace];
133
134         /* we use a popup to show the denoise settings */
135         [fDenoisePopUp removeAllItems];
136     [fDenoisePopUp addItemWithTitle: @"None"];
137     [fDenoisePopUp addItemWithTitle: @"Weak"];
138         [fDenoisePopUp addItemWithTitle: @"Medium"];
139     [fDenoisePopUp addItemWithTitle: @"Strong"];
140         /* Set denoises level according to the integer in the main window */
141         [fDenoisePopUp selectItemAtIndex: fPictureFilterSettings.denoise];
142
143 }
144
145 // Adjusts the window to draw the current picture (fPicture) adjusting its size as
146 // necessary to display as much of the picture as possible.
147 - (void) displayPreview
148 {
149     [fPictureView setImage: [self imageForPicture: fPicture]];
150         
151         NSSize displaySize = NSMakeSize( (float)fTitle->width, (float)fTitle->height );
152     /* Set the picture size display fields below the Preview Picture*/
153     if( fTitle->job->pixel_ratio == 1 ) // Original PAR Implementation
154     {
155         output_width = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
156         output_height = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
157         display_width = output_width * fTitle->job->pixel_aspect_width / fTitle->job->pixel_aspect_height;
158         [fInfoField setStringValue:[NSString stringWithFormat:
159                                     @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d",
160                                     fTitle->width, fTitle->height, output_width, output_height, display_width, output_height]];
161         displaySize.width *= ((float)fTitle->job->pixel_aspect_width) / ((float)fTitle->job->pixel_aspect_height);   
162     }
163     else if (fTitle->job->pixel_ratio == 2) // Loose Anamorphic
164     {
165         display_width = output_width * output_par_width / output_par_height;
166         [fInfoField setStringValue:[NSString stringWithFormat:
167                                     @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d",
168                                     fTitle->width, fTitle->height, output_width, output_height, display_width, output_height]];
169         
170         /* FIXME: needs to be fixed so that the picture window does not resize itself on the first
171          anamorphic width drop
172          */
173         if (fTitle->width - 8 < output_width)
174         {
175             displaySize.width *= ((float)output_par_width) / ((float)output_par_height);
176         }
177     }
178     else // No Anamorphic
179     {
180         [fInfoField setStringValue: [NSString stringWithFormat:
181                                      @"Source: %dx%d, Output: %dx%d", fTitle->width, fTitle->height,
182                                      fTitle->job->width, fTitle->job->height]];
183     }
184     
185     NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
186     if( [self viewNeedsToResizeToSize:viewSize] )
187     {
188         [self resizeSheetForViewSize:viewSize];
189         [self setViewSize:viewSize];
190     }
191     
192     // Show the scaled text (use the height to check since the width can vary
193     // with anamorphic video).
194     if( ((int)viewSize.height) != fTitle->height )
195     {
196         float scale = viewSize.width / ((float)fTitle->width);
197         NSString *scaleString = [NSString stringWithFormat:
198                                  NSLocalizedString( @" (Preview scaled to %.0f%% actual size)",
199                                                    @"String shown when a preview is scaled" ),
200                                  scale * 100.0];
201         [fInfoField setStringValue:
202          [[fInfoField stringValue] stringByAppendingString:scaleString]];
203     }
204     
205     [fPrevButton setEnabled: ( fPicture > 0 )];
206     [fNextButton setEnabled: ( fPicture < 9 )];
207 }
208
209 - (IBAction) SettingsChanged: (id) sender
210 {
211     hb_job_t * job = fTitle->job;
212     
213         if( [fAnamorphicPopUp indexOfSelectedItem] > 0 )
214         {
215         if ([fAnamorphicPopUp indexOfSelectedItem] == 2) // Loose anamorphic
216         {
217             job->pixel_ratio = 2;
218             [fWidthStepper setEnabled: YES];
219             [fWidthField setEnabled: YES];
220             /* We set job->width and call hb_set_anamorphic_size in libhb to do a "dry run" to get
221              * the values to be used by libhb for loose anamorphic
222              */
223             job->width       = [fWidthStepper  intValue];
224             hb_set_anamorphic_size(job, &output_width, &output_height, &output_par_width, &output_par_height);
225             [fHeightStepper      setIntValue: output_height];
226             [fHeightField        setIntValue: output_height];
227             job->height      = [fHeightStepper intValue];
228             
229         }
230         else // must be "1" or strict anamorphic
231         {
232             [fWidthStepper      setIntValue: fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3]];
233             [fWidthField        setIntValue: fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3]];
234             
235             /* This will show correct anamorphic height values, but
236              show distorted preview picture ratio */
237             [fHeightStepper      setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
238             [fHeightField        setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
239             job->width       = [fWidthStepper  intValue];
240             job->height      = [fHeightStepper intValue];
241             
242             job->pixel_ratio = 1;
243             [fWidthStepper setEnabled: NO];
244             [fWidthField setEnabled: NO];
245         }
246         
247         /* if the sender is the Anamorphic checkbox, record the state
248          of KeepAspect Ratio so it can be reset if Anamorphic is unchecked again */
249         if (sender == fAnamorphicPopUp)
250         {
251             keepAspectRatioPreviousState = [fRatioCheck state];
252         }
253         [fRatioCheck setState:NSOffState];
254         [fRatioCheck setEnabled: NO];
255         
256         
257         [fHeightStepper setEnabled: NO];
258         [fHeightField setEnabled: NO];
259         
260     }
261     else
262         {
263         job->width       = [fWidthStepper  intValue];
264         job->height      = [fHeightStepper intValue];
265         job->pixel_ratio = 0;
266         [fWidthStepper setEnabled: YES];
267         [fWidthField setEnabled: YES];
268         [fHeightStepper setEnabled: YES];
269         [fHeightField setEnabled: YES];
270         [fRatioCheck setEnabled: YES];
271         /* if the sender is the Anamorphic checkbox, we return the
272          keep AR checkbox to its previous state */
273         if (sender == fAnamorphicPopUp)
274         {
275             [fRatioCheck setState:keepAspectRatioPreviousState];
276         }
277         
278         }
279         
280     
281     job->keep_ratio  = ( [fRatioCheck state] == NSOnState );
282     
283         fPictureFilterSettings.deinterlace = [fDeinterlacePopUp indexOfSelectedItem];
284     /* if the gui deinterlace settings are fast through slowest, the job->deinterlace
285      value needs to be set to one, for the job as well as the previews showing deinterlacing
286      otherwise set job->deinterlace to 0 or "off" */
287     if (fPictureFilterSettings.deinterlace > 0)
288     {
289         job->deinterlace  = 1;
290     }
291     else
292     {
293         job->deinterlace  = 0;
294     }
295     fPictureFilterSettings.denoise     = [fDenoisePopUp indexOfSelectedItem];
296     fPictureFilterSettings.vfr  = [fVFRCheck state];
297     if (fPictureFilterSettings.vfr > 0)
298     {
299         [fDetelecineCheck setState:NSOnState];
300         [fDetelecineCheck setEnabled: NO];
301     }
302     else
303     {
304         [fDetelecineCheck setEnabled: YES];
305     }
306     fPictureFilterSettings.detelecine  = [fDetelecineCheck state];
307     fPictureFilterSettings.deblock  = [fDeblockCheck state];
308         //job->pixel_ratio = ( [fPARCheck state] == NSOnState );
309     
310     autoCrop = ( [fCropMatrix selectedRow] == 0 );
311     [fCropTopStepper    setEnabled: !autoCrop];
312     [fCropBottomStepper setEnabled: !autoCrop];
313     [fCropLeftStepper   setEnabled: !autoCrop];
314     [fCropRightStepper  setEnabled: !autoCrop];
315     
316     if( autoCrop )
317     {
318         memcpy( job->crop, fTitle->crop, 4 * sizeof( int ) );
319     }
320     else
321     {
322         job->crop[0] = [fCropTopStepper    intValue];
323         job->crop[1] = [fCropBottomStepper intValue];
324         job->crop[2] = [fCropLeftStepper   intValue];
325         job->crop[3] = [fCropRightStepper  intValue];
326     }
327     
328     if( job->keep_ratio )
329     {
330         if( sender == fWidthStepper || sender == fRatioCheck ||
331            sender == fCropTopStepper || sender == fCropBottomStepper )
332         {
333             hb_fix_aspect( job, HB_KEEP_WIDTH );
334             if( job->height > fTitle->height )
335             {
336                 job->height = fTitle->height;
337                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
338             }
339         }
340         else
341         {
342             hb_fix_aspect( job, HB_KEEP_HEIGHT );
343             if( job->width > fTitle->width )
344             {
345                 job->width = fTitle->width;
346                 hb_fix_aspect( job, HB_KEEP_WIDTH );
347             }
348         }
349     }
350     
351     [fWidthStepper      setIntValue: job->width];
352     [fWidthField        setIntValue: job->width];
353     if( [fAnamorphicPopUp indexOfSelectedItem] < 2 )
354         {
355         [fHeightStepper     setIntValue: job->height];
356         [fHeightField       setIntValue: job->height];
357     }
358     [fCropTopStepper    setIntValue: job->crop[0]];
359     [fCropTopField      setIntValue: job->crop[0]];
360     [fCropBottomStepper setIntValue: job->crop[1]];
361     [fCropBottomField   setIntValue: job->crop[1]];
362     [fCropLeftStepper   setIntValue: job->crop[2]];
363     [fCropLeftField     setIntValue: job->crop[2]];
364     [fCropRightStepper  setIntValue: job->crop[3]];
365     [fCropRightField    setIntValue: job->crop[3]];
366     /* Sanity Check Here for < 16 px preview to avoid
367      crashing hb_get_preview. In fact, just for kicks
368      lets getting previews at a min limit of 32, since
369      no human can see any meaningful detail below that */
370     if (job->width >= 64 && job->height >= 64)
371     {
372         // Purge the existing picture previews so they get recreated the next time
373         // they are needed.
374         [self purgeImageCache];
375         [self displayPreview];
376     }
377 }
378
379 - (IBAction) PreviousPicture: (id) sender
380 {   
381     if( fPicture <= 0 )
382     {
383         return;
384     }
385     fPicture--;
386     [self displayPreview];
387 }
388
389 - (IBAction) NextPicture: (id) sender
390 {
391     if( fPicture >= 9 )
392     {
393         return;
394     }
395     fPicture++;
396     [self displayPreview];
397 }
398
399 - (IBAction) ClosePanel: (id) sender
400 {
401     if ([delegate respondsToSelector:@selector(pictureSettingsDidChange)])
402         [delegate pictureSettingsDidChange];
403         
404     [NSApp endSheet: fPicturePanel];
405     [fPicturePanel orderOut: self];
406 }
407
408 - (BOOL) autoCrop
409 {
410     return autoCrop;
411 }
412 - (void) setAutoCrop: (BOOL) setting
413 {
414     autoCrop = setting;
415 }
416
417 - (BOOL) allowLooseAnamorphic
418 {
419     return allowLooseAnamorphic;
420 }
421
422 - (void) setAllowLooseAnamorphic: (BOOL) setting
423 {
424     allowLooseAnamorphic = setting;
425 }
426
427 - (int) detelecine
428 {
429     return fPictureFilterSettings.detelecine;
430 }
431
432 - (void) setDetelecine: (int) setting
433 {
434     fPictureFilterSettings.detelecine = setting;
435 }
436
437 - (int) vfr
438 {
439     return fPictureFilterSettings.vfr;
440 }
441
442 - (void) setVFR: (int) setting
443 {
444     fPictureFilterSettings.vfr = setting;
445 }
446
447 - (int) deinterlace
448 {
449     return fPictureFilterSettings.deinterlace;
450 }
451
452 - (void) setDeinterlace: (int) setting {
453     fPictureFilterSettings.deinterlace = setting;
454 }
455
456 - (int) denoise
457 {
458     return fPictureFilterSettings.denoise;
459 }
460
461 - (void) setDenoise: (int) setting
462 {
463     fPictureFilterSettings.denoise = setting;
464 }
465
466 - (int) deblock
467 {
468     return fPictureFilterSettings.deblock;
469 }
470
471 - (void) setDeblock: (int) setting
472 {
473     fPictureFilterSettings.deblock = setting;
474 }
475
476 - (void)showPanelInWindow: (NSWindow *)fWindow forTitle: (hb_title_t *)title
477 {
478     [self SetTitle:title];
479     
480     [NSApp beginSheet:fPicturePanel
481        modalForWindow:fWindow
482         modalDelegate:nil
483        didEndSelector:nil
484           contextInfo:NULL];
485 }
486
487
488 // This function converts an image created by libhb (specified via pictureIndex) into
489 // an NSImage suitable for the GUI code to use. If removeBorders is YES,
490 // makeImageForPicture crops the image generated by libhb stripping off the gray
491 // border around the content. This is the low-level method that generates the image.
492 // -imageForPicture calls this function whenever it can't find an image in its cache.
493 + (NSImage *) makeImageForPicture: (int)pictureIndex
494                 libhb:(hb_handle_t*)handle
495                 title:(hb_title_t*)title
496                 removeBorders:(BOOL)removeBorders
497 {
498     if (removeBorders)
499     {
500         //     |<---------- title->width ----------->|
501         //     |   |<---- title->job->width ---->|   |
502         //     |   |                             |   |
503         //     .......................................
504         //     ....+-----------------------------+....
505         //     ....|                             |....<-- gray border
506         //     ....|                             |....
507         //     ....|                             |....
508         //     ....|                             |<------- image
509         //     ....|                             |....
510         //     ....|                             |....
511         //     ....|                             |....
512         //     ....|                             |....
513         //     ....|                             |....
514         //     ....+-----------------------------+....
515         //     .......................................
516
517         static uint8_t * buffer;
518         static int bufferSize;
519
520         // Make sure we have a big enough buffer to receive the image from libhb. libhb
521         // creates images with a one-pixel border around the original content. Hence we
522         // add 2 pixels horizontally and vertically to the buffer size.
523         int srcWidth = title->width + 2;
524         int srcHeight= title->height + 2;
525         int newSize;
526         newSize = srcWidth * srcHeight * 4;
527         if( bufferSize < newSize )
528         {
529             bufferSize = newSize;
530             buffer     = (uint8_t *) realloc( buffer, bufferSize );
531         }
532
533         hb_get_preview( handle, title, pictureIndex, buffer );
534
535         // Create an NSBitmapImageRep and copy the libhb image into it, converting it from
536         // libhb's format to one suitable for NSImage. Along the way, we'll strip off the
537         // border around libhb's image.
538         
539         // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
540         // Alpha is ignored.
541         
542         int dstWidth = title->job->width;
543         int dstHeight = title->job->height;
544         NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
545         NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
546                 initWithBitmapDataPlanes:nil
547                 pixelsWide:dstWidth
548                 pixelsHigh:dstHeight
549                 bitsPerSample:8
550                 samplesPerPixel:3   // ignore alpha
551                 hasAlpha:NO
552                 isPlanar:NO
553                 colorSpaceName:NSCalibratedRGBColorSpace
554                 bitmapFormat:bitmapFormat
555                 bytesPerRow:dstWidth * 4
556                 bitsPerPixel:32] autorelease];
557
558         int borderTop = (srcHeight - dstHeight) / 2;
559         int borderLeft = (srcWidth - dstWidth) / 2;
560         
561         UInt32 * src = (UInt32 *)buffer;
562         UInt32 * dst = (UInt32 *)[imgrep bitmapData];
563         src += borderTop * srcWidth;    // skip top rows in src to get to first row of dst
564         src += borderLeft;              // skip left pixels in src to get to first pixel of dst
565         for (int r = 0; r < dstHeight; r++)
566         {
567             for (int c = 0; c < dstWidth; c++)
568 #if TARGET_RT_LITTLE_ENDIAN
569                 *dst++ = Endian32_Swap(*src++);
570 #else
571                 *dst++ = *src++;
572 #endif
573             src += (srcWidth - dstWidth);   // skip to next row in src
574         }
575
576         NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(dstWidth, dstHeight)] autorelease];
577         [img addRepresentation:imgrep];
578
579         return img;
580     }
581     else
582     {
583         // Make sure we have big enough buffer
584         static uint8_t * buffer;
585         static int bufferSize;
586
587         int newSize;
588         newSize = ( title->width + 2 ) * (title->height + 2 ) * 4;
589         if( bufferSize < newSize )
590         {
591             bufferSize = newSize;
592             buffer     = (uint8_t *) realloc( buffer, bufferSize );
593         }
594
595         hb_get_preview( handle, title, pictureIndex, buffer );
596
597         // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
598         // We'll copy that into an NSImage swapping it to ARGB in the process. Alpha is
599         // ignored.
600         int width = title->width + 2;      // hblib adds a one-pixel border to the image
601         int height = title->height + 2;
602         int numPixels = width * height;
603         NSBitmapFormat bitmapFormat = (NSBitmapFormat)NSAlphaFirstBitmapFormat;
604         NSBitmapImageRep * imgrep = [[[NSBitmapImageRep alloc]
605                 initWithBitmapDataPlanes:nil
606                 pixelsWide:width
607                 pixelsHigh:height
608                 bitsPerSample:8
609                 samplesPerPixel:3   // ignore alpha
610                 hasAlpha:NO
611                 isPlanar:NO
612                 colorSpaceName:NSCalibratedRGBColorSpace
613                 bitmapFormat:bitmapFormat
614                 bytesPerRow:width * 4
615                 bitsPerPixel:32] autorelease];
616
617         UInt32 * src = (UInt32 *)buffer;
618         UInt32 * dst = (UInt32 *)[imgrep bitmapData];
619         for (int i = 0; i < numPixels; i++)
620 #if TARGET_RT_LITTLE_ENDIAN
621             *dst++ = Endian32_Swap(*src++);
622 #else
623             *dst++ = *src++;
624 #endif
625
626         NSImage * img = [[[NSImage alloc] initWithSize: NSMakeSize(width, height)] autorelease];
627         [img addRepresentation:imgrep];
628
629         return img;
630     }
631 }
632
633 // Returns the preview image for the specified index, retrieving it from its internal
634 // cache or by calling makeImageForPicture if it is not cached. Generally, you should
635 // use imageForPicture so that images are cached. Calling makeImageForPicture will
636 // always generate a new copy of the image.
637 - (NSImage *) imageForPicture: (int) pictureIndex
638 {
639     // The preview for the specified index may not currently exist, so this method
640     // generates it if necessary.
641     NSString * key = [NSString stringWithFormat:@"%d", pictureIndex];
642     NSImage * theImage = [fPicturePreviews objectForKey:key];
643     if (!theImage)
644     {
645         theImage = [PictureController makeImageForPicture:pictureIndex libhb:fHandle title:fTitle removeBorders: NO];
646         [fPicturePreviews setObject:theImage forKey:key];
647     }
648     return theImage;
649 }
650
651 // Purges all images from the cache. The next call to imageForPicture will cause a new
652 // image to be generated.
653 - (void) purgeImageCache
654 {
655     [fPicturePreviews removeAllObjects];
656 }
657
658 - (BOOL) loadMyNibFile
659 {
660     if(![NSBundle loadNibNamed:@"PictureSettings" owner:self])
661     {
662         NSLog(@"Warning! Could not load myNib file.\n");
663         return NO;
664     }
665     
666     return YES;
667 }
668
669 @end
670
671 @implementation PictureController (Private)
672
673 //
674 // -[PictureController(Private) optimalViewSizeForImageSize:]
675 //
676 // Given the size of the preview image to be shown, returns the best possible
677 // size for the OpenGL view.
678 //
679 - (NSSize)optimalViewSizeForImageSize: (NSSize)imageSize
680 {
681     // The min size is 320x240
682     float minWidth = 320.0;
683     float minHeight = 240.0;
684     
685     // The max size of the view is when the sheet is taking up 85% of the screen.
686     NSSize screenSize = [[NSScreen mainScreen] frame].size;
687     NSSize sheetSize = [fPicturePanel frame].size;
688     NSSize viewAreaSize = [fPictureViewArea frame].size;
689     float paddingX = sheetSize.width - viewAreaSize.width;
690     float paddingY = sheetSize.height - viewAreaSize.height;
691     float maxWidth = (0.85 * screenSize.width) - paddingX;
692     float maxHeight = (0.85 * screenSize.height) - paddingY;
693     
694     NSSize resultSize = imageSize;
695     
696     // Its better to have a view that's too small than a view that's too big, so
697     // apply the maximum constraints last.
698     if( resultSize.width < minWidth )
699     {
700         resultSize.height *= (minWidth / resultSize.width);
701         resultSize.width = minWidth;
702     }
703     if( resultSize.height < minHeight )
704     {
705         resultSize.width *= (minHeight / resultSize.height);
706         resultSize.height = minHeight;
707     }
708     if( resultSize.width > maxWidth )
709     {
710         resultSize.height *= (maxWidth / resultSize.width);
711         resultSize.width = maxWidth;
712     }
713     if( resultSize.height > maxHeight )
714     {
715         resultSize.width *= (maxHeight / resultSize.height);
716         resultSize.height = maxHeight;
717     }
718     
719     return resultSize;
720 }
721
722 //
723 // -[PictureController(Private) resizePanelForViewSize:animate:]
724 //
725 // Resizes the entire sheet to accomodate an OpenGL view of a particular size.
726 //
727 - (void)resizeSheetForViewSize: (NSSize)viewSize
728 {
729     // Figure out the deltas for the new frame area
730     NSSize currentSize = [fPictureViewArea frame].size;
731     float deltaX = viewSize.width - currentSize.width;
732     float deltaY = viewSize.height - currentSize.height;
733     
734     // Now resize the whole panel by those same deltas, but don't exceed the min
735     NSRect frame = [fPicturePanel frame];
736     NSSize maxSize = [fPicturePanel maxSize];
737     NSSize minSize = [fPicturePanel minSize];
738     frame.size.width += deltaX;
739     frame.size.height += deltaY;
740     if( frame.size.width < minSize.width )
741     {
742         frame.size.width = minSize.width;
743     }
744     if( frame.size.height < minSize.height )
745     {
746         frame.size.height = minSize.height;
747     }
748
749     // But now the sheet is off-center, so also shift the origin to center it and
750     // keep the top aligned.
751     frame.origin.x -= (deltaX / 2.0);
752     frame.origin.y -= deltaY;
753
754     [fPicturePanel setFrame:frame display:YES animate:YES];
755 }
756
757 //
758 // -[PictureController(Private) setViewSize:]
759 //
760 // Changes the OpenGL view's size and centers it vertially inside of its area.
761 // Assumes resizeSheetForViewSize: has already been called.
762 //
763 - (void)setViewSize: (NSSize)viewSize
764 {
765     [fPictureView setFrameSize:viewSize];
766     
767     // center it vertically
768     NSPoint origin = [fPictureViewArea frame].origin;
769     origin.y += ([fPictureViewArea frame].size.height -
770                  [fPictureView frame].size.height) / 2.0;
771     [fPictureView setFrameOrigin:origin];
772 }
773
774 //
775 // -[PictureController(Private) viewNeedsToResizeToSize:]
776 //
777 // Returns YES if the view will need to resize to match the given size.
778 //
779 - (BOOL)viewNeedsToResizeToSize: (NSSize)newSize
780 {
781     NSSize viewSize = [fPictureView frame].size;
782     return (newSize.width != viewSize.width || newSize.height != viewSize.height);
783 }
784
785
786 @end