source: trunk/Cocoa/Pester/Source/NJRSoundPopUpButton.m @ 34

Last change on this file since 34 was 34, checked in by Nicholas Riley, 18 years ago

Changes for Pester 1.1d1.

File size: 5.8 KB
Line 
1//
2//  NJRQTMediaPopUpButton.m
3//  Pester
4//
5//  Created by Nicholas Riley on Sat Oct 26 2002.
6//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import "NJRQTMediaPopUpButton.h"
10#import "SoundFileManager.h"
11#import "NSMovie-NJRExtensions.h"
12#import "NSImage-NJRExtensions.h"
13
14@interface NJRQTMediaPopUpButton (Private)
15- (BOOL)_previewSound;
16@end
17
18@implementation NJRQTMediaPopUpButton
19
20// XXX handle refreshing sound list on resume
21// XXX don't add icons on Puma
22// XXX add saving of recently selected media
23// XXX launch preview on a separate thread (if movies take too long to load, they inhibit the interface)
24
25- (void)awakeFromNib;
26{
27    NSMenu *menu;
28    NSMenuItem *item;
29    SoundFileManager *sfm = [SoundFileManager sharedSoundFileManager];
30    int soundCount = [sfm count];
31   
32    [self removeAllItems];
33    menu = [self menu];
34    item = [menu addItemWithTitle: @"Alert sound" action: @selector(_beepSelected:) keyEquivalent: @""];
35    [item setTarget: self];
36    [menu addItem: [NSMenuItem separatorItem]];
37    if (soundCount == 0) {
38        item = [menu addItemWithTitle: @"CanÕt locate alert sounds" action: nil keyEquivalent: @""];
39        [item setEnabled: NO];
40    } else {
41        SoundFile *sf;
42        int i;
43        [sfm sortByName];
44        for (i = 0 ; i < soundCount ; i++) {
45            sf = [sfm soundFileAtIndex: i];
46            item = [menu addItemWithTitle: [sf name] action: @selector(_soundFileSelected:) keyEquivalent: @""];
47            [item setTarget: self];
48            [item setRepresentedObject: sf];
49            [item setImage: [[[NSWorkspace sharedWorkspace] iconForFile: [sf path]] bestFitImageForSize: NSMakeSize(16, 16)]];
50        }
51    }
52    [menu addItem: [NSMenuItem separatorItem]];
53    item = [menu addItemWithTitle: @"OtherÉ" action: @selector(select:) keyEquivalent: @""];
54    [item setTarget: self];
55}
56
57- (IBAction)select:(id)sender;
58{
59    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
60    NSString *path = [selectedAlias fullPath];
61    [openPanel setAllowsMultipleSelection: NO];
62    [openPanel setCanChooseDirectories: NO];
63    [openPanel setCanChooseFiles: YES];
64    [openPanel beginSheetForDirectory: [path stringByDeletingLastPathComponent]
65                                 file: [path lastPathComponent]
66                                types: nil // XXX fix for QuickTime!
67                       modalForWindow: [self window]
68                        modalDelegate: self
69                       didEndSelector: @selector(openPanelDidEnd:returnCode:contextInfo:)
70                          contextInfo: nil];
71}
72
73- (void)setAlias:(BDAlias *)alias;
74{
75    if (selectedAlias != alias) {
76        [selectedAlias release];
77        selectedAlias = [alias retain];
78    }
79}
80
81- (void)setPath:(NSString *)path;
82{
83    [self setAlias: [BDAlias aliasWithPath: path]];
84}
85
86- (BDAlias *)selectedAlias;
87{
88    return selectedAlias;
89}
90
91
92- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
93{
94    [sheet close];
95
96    if (returnCode == NSOKButton) {
97        NSArray *files = [sheet filenames];
98        NSAssert1([files count] == 1, @"%d items returned, only one expected", [files count]);
99        [self setPath: [files objectAtIndex: 0]];
100        if ([self _previewSound]) {
101            NSString *path = [selectedAlias fullPath];
102            SoundFile *sf = [[SoundFileManager sharedSoundFileManager] soundFileFromPath: path];
103            if (sf != nil) {
104                [self selectItemAtIndex: [self indexOfItemWithRepresentedObject: sf]];
105            } else {
106                NSString *title = [[NSFileManager defaultManager] displayNameAtPath: path];
107                NSMenuItem *item = [[self menu] addItemWithTitle: title action: @selector(_aliasSelected:) keyEquivalent: @""];
108                [item setTarget: self];
109                [item setRepresentedObject: selectedAlias];
110                [item setImage: [[[NSWorkspace sharedWorkspace] iconForFile: path] bestFitImageForSize: NSMakeSize(16, 16)]];
111                [self selectItem: item];
112            }
113        }
114    }
115}
116
117- (void)_beepSelected:(NSMenuItem *)sender;
118{
119    [self setAlias: nil];
120    [self _previewSound];
121}
122
123- (void)_soundFileSelected:(NSMenuItem *)sender;
124{
125    [self setPath: [(SoundFile *)[sender representedObject] path]];
126    [self _previewSound];
127}
128
129- (void)_aliasSelected:(NSMenuItem *)sender;
130{
131    [self setAlias: [sender representedObject]];
132    [self _previewSound];
133}
134
135- (void)_invalidateSoundSelection;
136{
137    [self setAlias: nil];
138    [self selectItemAtIndex: 0];
139}
140
141- (IBAction)stopSoundPreview:(id)sender;
142{
143    [preview stop: self];
144}
145
146- (BOOL)_previewSound;
147{
148    [preview stop: self];
149    if (selectedAlias == nil) {
150        [preview setMovie: nil];
151        NSBeep();
152    } else {
153        NSMovie *movie = [[NSMovie alloc] initWithURL: [NSURL fileURLWithPath: [selectedAlias fullPath]] byReference: YES];
154        if ([movie hasAudio])
155            [preview setMovie: movie];
156        else {
157            [preview setMovie: nil];
158            if (movie == nil) {
159                NSBeginAlertSheet(@"Format not recognized", @"OK", nil, nil, [self window], nil, nil, nil, nil, @"The item you selected isnÕt a sound or movie recognized by QuickTime.  Please select a different item.");
160                [self _invalidateSoundSelection];
161                return NO;
162            }
163            if (![movie hasAudio] && ![movie hasVideo]) {
164                NSBeginAlertSheet(@"No video or audio", @"OK", nil, nil, [self window], nil, nil, nil, nil, @"Ò%@Ó contains neither audio nor video content playable by QuickTime.  Please select a different item.", [[NSFileManager defaultManager] displayNameAtPath: [selectedAlias fullPath]]);
165                [self _invalidateSoundSelection];
166                [movie release];
167                return NO;
168            }
169        }
170        [movie release];
171        [preview start: self];
172    }
173    return YES;
174}
175
176@end
Note: See TracBrowser for help on using the repository browser.