source: trunk/Cocoa/Pester/Source/NJRVoicePopUpButton.m @ 600

Last change on this file since 600 was 600, checked in by Nicholas Riley, 9 years ago

Prototypes to pacify GCC.

File size: 3.7 KB
Line 
1//
2//  NJRVoicePopUpButton.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 "NJRVoicePopUpButton.h"
10
11@interface NJRVoicePopUpButton (Private)
12- (void)_previewVoice;
13@end
14
15@implementation NJRVoicePopUpButton
16
17- (void)_refreshVoiceList;
18{
19    NSMenu *menu;
20    NSMenuItem *item;
21    NSArray *voices = [NSSpeechSynthesizer availableVoices];
22
23    [self removeAllItems];
24    menu = [self menu];
25    [menu setAutoenablesItems: NO];
26    // XXX would be more elegant with surrogate support like my font popup menu
27    item = [menu addItemWithTitle: @"«unknown»" action: nil keyEquivalent: @""];
28    [item setEnabled: NO];
29    [menu addItem: [NSMenuItem separatorItem]];
30    if (voices == nil || [voices count] == 0) {
31        item = [menu addItemWithTitle: NSLocalizedString(@"Can't locate voices", "Voice popup menu item surrogate for voice list if no voices are found") action: nil keyEquivalent: @""];
32        [item setEnabled: NO];
33    } else {
34        NSEnumerator *e = [voices objectEnumerator];
35        NSString *voice;
36        while ( (voice = [e nextObject]) != nil) {
37            item = [menu addItemWithTitle:
38                    [[NSSpeechSynthesizer attributesForVoice: voice] objectForKey: NSVoiceName]
39                                   action: @selector(_previewVoice) keyEquivalent: @""];
40            [item setRepresentedObject: voice];
41            [item setTarget: self];
42        }
43    }
44    if (_speaker == nil)
45        [self selectItemAtIndex: [menu indexOfItemWithRepresentedObject: [NSSpeechSynthesizer defaultVoice]]];
46}
47
48- (id)initWithFrame:(NSRect)frame;
49{
50    if ( (self = [super initWithFrame: frame]) != nil) {
51        [self _refreshVoiceList];
52    }
53    return self;
54}
55
56- (id)initWithCoder:(NSCoder *)coder;
57{
58    if ( (self = [super initWithCoder: coder]) != nil) {
59        [self _refreshVoiceList];
60    }
61    return self;
62}
63
64- (NSSpeechSynthesizer *)_speaker;
65{
66    if (_speaker == nil) _speaker = [[NSSpeechSynthesizer alloc] initWithVoice: nil];
67    return _speaker;
68}
69
70- (void)_invalidateVoiceSelection;
71{
72    [self _refreshVoiceList];
73    [self selectItemAtIndex: 0];
74}
75
76- (void)setVoice:(NSString *)voice;
77{
78    int voiceIdx = [self indexOfItemWithRepresentedObject: voice];
79    if (voiceIdx == -1) {
80        [self _invalidateVoiceSelection];
81    } else {
82        [self selectItemAtIndex: voiceIdx];
83    }
84}
85
86- (void)_previewVoice;
87{
88    NSString *voice = [[self selectedItem] representedObject];
89    NSString *previewString = nil;
90
91    [_speaker stopSpeaking];
92
93    if (![[self _speaker] setVoice: voice]) {
94        // XXX localize title
95        NSBeginAlertSheet(@"Voice not available", nil, nil, nil, [self window], nil, nil, nil, nil, NSLocalizedString(@"The voice '%@' you selected could not be used.", "Message displayed in alert sheet when -[NSSpeechSynthesizer setVoice:] returns an error"), [self titleOfSelectedItem]);
96        [self _invalidateVoiceSelection];
97        return;
98    }
99
100    if (_delegate != nil && [_delegate respondsToSelector: @selector(voicePopUpButton:previewStringForVoice:)]) {
101        previewString = [_delegate voicePopUpButton: self previewStringForVoice: voice];
102    }
103
104    if (previewString == nil)
105        previewString = [[NSSpeechSynthesizer attributesForVoice: voice] objectForKey: NSVoiceDemoText];
106
107    [_speaker startSpeakingString: previewString];
108}
109
110- (void)dealloc;
111{
112    [_speaker release];
113    [super dealloc];
114}
115
116- (IBAction)stopVoicePreview:(id)sender;
117{
118    [_speaker stopSpeaking];
119}
120
121- (void)setEnabled:(BOOL)flag;
122{
123    [super setEnabled: flag];
124    if (flag) ; // XXX [self stopVoicePreview: self]; // need to prohibit at startup
125    else [self stopVoicePreview: self];
126}
127
128- (void)setDelegate:(id)delegate;
129{
130    _delegate = delegate;
131}
132
133- (id)delegate;
134{
135    return _delegate;
136}
137
138@end
Note: See TracBrowser for help on using the repository browser.