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 | @implementation NJRVoicePopUpButton
|
---|
12 |
|
---|
13 | - (void)_refreshVoiceList;
|
---|
14 | {
|
---|
15 | NSMenu *menu;
|
---|
16 | NSMenuItem *item;
|
---|
17 | NSArray *voiceNames = [SUSpeaker voiceNames];
|
---|
18 |
|
---|
19 | [self removeAllItems];
|
---|
20 | menu = [self menu];
|
---|
21 | [menu setAutoenablesItems: NO];
|
---|
22 | // XXX would be more elegant with surrogate support like my font popup menu
|
---|
23 | item = [menu addItemWithTitle: @"ÇunknownÈ" action: nil keyEquivalent: @""];
|
---|
24 | [item setEnabled: NO];
|
---|
25 | [menu addItem: [NSMenuItem separatorItem]];
|
---|
26 | if (voiceNames == nil || [voiceNames count] == 0) {
|
---|
27 | item = [menu addItemWithTitle: @"CanÕt locate voices" action: nil keyEquivalent: @""];
|
---|
28 | [item setEnabled: NO];
|
---|
29 | } else {
|
---|
30 | NSEnumerator *e = [voiceNames objectEnumerator];
|
---|
31 | NSString *voiceName;
|
---|
32 | while ( (voiceName = [e nextObject]) != nil) {
|
---|
33 | item = [menu addItemWithTitle: voiceName action: @selector(_previewVoice) keyEquivalent: @""];
|
---|
34 | [item setTarget: self];
|
---|
35 | }
|
---|
36 | }
|
---|
37 | if (_speaker == nil) [self selectItemWithTitle: [SUSpeaker defaultVoice]];
|
---|
38 | }
|
---|
39 |
|
---|
40 | - (id)initWithFrame:(NSRect)frame;
|
---|
41 | {
|
---|
42 | if ( (self = [super initWithFrame: frame]) != nil) {
|
---|
43 | [self _refreshVoiceList];
|
---|
44 | }
|
---|
45 | return self;
|
---|
46 | }
|
---|
47 |
|
---|
48 | - (id)initWithCoder:(NSCoder *)coder;
|
---|
49 | {
|
---|
50 | if ( (self = [super initWithCoder: coder]) != nil) {
|
---|
51 | [self _refreshVoiceList];
|
---|
52 | }
|
---|
53 | return self;
|
---|
54 | }
|
---|
55 |
|
---|
56 | - (SUSpeaker *)_speaker;
|
---|
57 | {
|
---|
58 | if (_speaker == nil) _speaker = [[SUSpeaker alloc] init];
|
---|
59 | return _speaker;
|
---|
60 | }
|
---|
61 |
|
---|
62 | - (void)_invalidateVoiceSelection;
|
---|
63 | {
|
---|
64 | [self _refreshVoiceList];
|
---|
65 | [self selectItemAtIndex: 0];
|
---|
66 | }
|
---|
67 |
|
---|
68 | - (void)setVoice:(NSString *)voice;
|
---|
69 | {
|
---|
70 | int voiceIdx = [self indexOfItemWithTitle: voice];
|
---|
71 | if (voiceIdx == -1) {
|
---|
72 | [self _invalidateVoiceSelection];
|
---|
73 | } else {
|
---|
74 | [self selectItemAtIndex: voiceIdx];
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | - (void)_previewVoice;
|
---|
79 | {
|
---|
80 | NSString *voiceName = [self titleOfSelectedItem];
|
---|
81 | NSString *previewString = nil;
|
---|
82 | VoiceSpec voice;
|
---|
83 | OSStatus err = noErr;
|
---|
84 | VoiceDescription info;
|
---|
85 | short voiceIndex = [[SUSpeaker voiceNames] indexOfObject: voiceName] + 1;
|
---|
86 |
|
---|
87 | [_speaker stopSpeaking];
|
---|
88 |
|
---|
89 | if ( (err = GetIndVoice(voiceIndex, &voice)) != noErr) {
|
---|
90 | NSBeginAlertSheet(@"Voice not available", nil, 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);
|
---|
91 | [self _invalidateVoiceSelection];
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (_delegate != nil && [_delegate respondsToSelector: @selector(voicePopUpButton:previewStringForVoice:)]) {
|
---|
96 | previewString = [_delegate voicePopUpButton: self previewStringForVoice: voiceName];
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (previewString == nil) {
|
---|
100 | err = GetVoiceDescription(&voice, &info, sizeof(info));
|
---|
101 | if (err != noErr || info.comment[0] == 0)
|
---|
102 | previewString = voiceName;
|
---|
103 | else {
|
---|
104 | previewString = (NSString *)CFStringCreateWithPascalString(NULL, info.comment, kCFStringEncodingMacRoman);
|
---|
105 | [previewString autorelease];
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | [[self _speaker] setVoice: voiceIndex];
|
---|
110 | [_speaker speakText: previewString];
|
---|
111 | }
|
---|
112 |
|
---|
113 | - (void)dealloc;
|
---|
114 | {
|
---|
115 | [_speaker release];
|
---|
116 | [super dealloc];
|
---|
117 | }
|
---|
118 |
|
---|
119 | - (IBAction)stopVoicePreview:(id)sender;
|
---|
120 | {
|
---|
121 | [_speaker stopSpeaking];
|
---|
122 | }
|
---|
123 |
|
---|
124 | - (void)setEnabled:(BOOL)flag;
|
---|
125 | {
|
---|
126 | [super setEnabled: flag];
|
---|
127 | if (flag) ; // XXX [self stopVoicePreview: self]; // need to prohibit at startup
|
---|
128 | else [self stopVoicePreview: self];
|
---|
129 | }
|
---|
130 |
|
---|
131 | - (void)setDelegate:(id)delegate;
|
---|
132 | {
|
---|
133 | _delegate = delegate;
|
---|
134 | }
|
---|
135 |
|
---|
136 | - (id)delegate;
|
---|
137 | {
|
---|
138 | return _delegate;
|
---|
139 | }
|
---|
140 |
|
---|
141 | @end |
---|