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

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

Changes for Pester 1.1d1.

File size: 3.3 KB
Line 
1//
2//  NJRVoicePopUpButton.m
3//  Pester
4//
5//  Created by Nicholas Riley on Sat Oct 26 2002.
6//  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7//
8
9#import "NJRVoicePopUpButton.h"
10
11
12@implementation NJRVoicePopUpButton
13
14- (void)_refreshVoiceList;
15{
16    NSMenu *menu;
17    NSMenuItem *item;
18    NSArray *voiceNames = [SUSpeaker voiceNames];
19
20    [self removeAllItems];
21    menu = [self menu];
22    [menu setAutoenablesItems: NO];
23    // XXX would be more elegant with surrogate support like my font popup menu
24    item = [menu addItemWithTitle: @"ÇunknownÈ" action: nil keyEquivalent: @""];
25    [item setEnabled: NO];
26    [menu addItem: [NSMenuItem separatorItem]];
27    if (voiceNames == nil || [voiceNames count] == 0) {
28        item = [menu addItemWithTitle: @"CanÕt locate voices" action: nil keyEquivalent: @""];
29        [item setEnabled: NO];
30    } else {
31        NSEnumerator *e = [voiceNames objectEnumerator];
32        NSString *voiceName;
33        while ( (voiceName = [e nextObject]) != nil) {
34            item = [menu addItemWithTitle: voiceName action: @selector(_previewVoice) keyEquivalent: @""];
35            [item setTarget: self];
36        }
37    }
38    if (_speaker == nil) [self selectItemWithTitle: [SUSpeaker defaultVoice]];
39}
40
41- (id)initWithFrame:(NSRect)frame;
42{
43    if ( (self = [super initWithFrame: frame]) != nil) {
44        [self _refreshVoiceList];
45    }
46    return self;
47}
48
49- (id)initWithCoder:(NSCoder *)coder;
50{
51    if ( (self = [super initWithCoder: coder]) != nil) {
52        [self _refreshVoiceList];
53    }
54    return self;
55}
56
57- (SUSpeaker *)_speaker;
58{
59    if (_speaker == nil) _speaker = [[SUSpeaker alloc] init];
60    return _speaker;
61}
62
63- (void)_invalidateVoiceSelection;
64{
65    [self _refreshVoiceList];
66    [self selectItemAtIndex: 0];
67}
68
69- (void)_previewVoice;
70{
71    NSString *voiceName = [self titleOfSelectedItem];
72    NSString *previewString = nil;
73    VoiceSpec voice;
74    OSStatus err = noErr;
75    VoiceDescription info;
76    short voiceIndex = [[SUSpeaker voiceNames] indexOfObject: voiceName] + 1;
77
78    [_speaker stopSpeaking];
79
80    if ( (err = GetIndVoice(voiceIndex, &voice)) != noErr) {
81        NSBeginAlertSheet(@"Voice not available", @"OK", nil, nil, [self window], nil, nil, nil, nil, @"The voice Ò%@Ó you selected could not be used.  An error of type %ld occurred while attempting to retrieve voice information.", voiceName, err);
82        [self _invalidateVoiceSelection];
83        return;
84    }
85
86    if (_delegate != nil && [_delegate respondsToSelector: @selector(voicePopUpButton:previewStringForVoice:)]) {
87        previewString = [_delegate voicePopUpButton: self previewStringForVoice: voiceName];
88    }
89
90    if (previewString == nil) {
91        err = GetVoiceDescription(&voice, &info, sizeof(info));
92        if (err != noErr || info.comment[0] == 0)
93            previewString = voiceName;
94        else {
95            previewString = (NSString *)CFStringCreateWithPascalString(NULL, info.comment, kCFStringEncodingMacRoman);
96            [previewString autorelease];
97        }
98    }
99
100    [[self _speaker] setVoice: voiceIndex];
101    [_speaker speakText: previewString];
102}
103
104- (void)dealloc;
105{
106    [_speaker release];
107    [super dealloc];
108}
109
110- (IBAction)stopVoicePreview:(id)sender;
111{
112    [_speaker stopSpeaking];
113}
114
115- (void)setDelegate:(id)delegate;
116{
117    _delegate = delegate;
118}
119
120- (id)delegate;
121{
122    return _delegate;
123}
124
125@end
Note: See TracBrowser for help on using the repository browser.