source: releases/Pester/1.1a5/Source/NJRVoicePopUpButton.m@ 548

Last change on this file since 548 was 53, checked in by Nicholas Riley, 22 years ago

Updated for Pester 1.1a5 (very limited release).

Pester 1.1a4 was never released.

File size: 3.8 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// 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)setVoice:(NSString *)voice;
71{
72 int voiceIdx = [self indexOfItemWithTitle: voice];
73 if (voiceIdx == -1) {
74 [self _invalidateVoiceSelection];
75 } else {
76 [self selectItemAtIndex: voiceIdx];
77 }
78}
79
80- (void)_previewVoice;
81{
82 NSString *voiceName = [self titleOfSelectedItem];
83 NSString *previewString = nil;
84 VoiceSpec voice;
85 OSStatus err = noErr;
86 VoiceDescription info;
87 short voiceIndex = [[SUSpeaker voiceNames] indexOfObject: voiceName] + 1;
88
89 [_speaker stopSpeaking];
90
91 if ( (err = GetIndVoice(voiceIndex, &voice)) != noErr) {
92 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);
93 [self _invalidateVoiceSelection];
94 return;
95 }
96
97 if (_delegate != nil && [_delegate respondsToSelector: @selector(voicePopUpButton:previewStringForVoice:)]) {
98 previewString = [_delegate voicePopUpButton: self previewStringForVoice: voiceName];
99 }
100
101 if (previewString == nil) {
102 err = GetVoiceDescription(&voice, &info, sizeof(info));
103 if (err != noErr || info.comment[0] == 0)
104 previewString = voiceName;
105 else {
106 previewString = (NSString *)CFStringCreateWithPascalString(NULL, info.comment, kCFStringEncodingMacRoman);
107 [previewString autorelease];
108 }
109 }
110
111 [[self _speaker] setVoice: voiceIndex];
112 [_speaker speakText: previewString];
113}
114
115- (void)dealloc;
116{
117 [_speaker release];
118 [super dealloc];
119}
120
121- (IBAction)stopVoicePreview:(id)sender;
122{
123 [_speaker stopSpeaking];
124}
125
126- (void)setEnabled:(BOOL)flag;
127{
128 [super setEnabled: flag];
129 if (flag) ; // XXX [self stopVoicePreview: self]; // need to prohibit at startup
130 else [self stopVoicePreview: self];
131}
132
133- (void)setDelegate:(id)delegate;
134{
135 _delegate = delegate;
136}
137
138- (id)delegate;
139{
140 return _delegate;
141}
142
143@end
Note: See TracBrowser for help on using the repository browser.