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