[34] | 1 | //
|
---|
| 2 | // NJRVoicePopUpButton.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Sat Oct 26 2002.
|
---|
[42] | 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
[34] | 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "NJRVoicePopUpButton.h"
|
---|
| 10 |
|
---|
[600] | 11 | @interface NJRVoicePopUpButton (Private)
|
---|
| 12 | - (void)_previewVoice;
|
---|
| 13 | @end
|
---|
| 14 |
|
---|
[34] | 15 | @implementation NJRVoicePopUpButton
|
---|
| 16 |
|
---|
| 17 | - (void)_refreshVoiceList;
|
---|
| 18 | {
|
---|
| 19 | NSMenu *menu;
|
---|
| 20 | NSMenuItem *item;
|
---|
[364] | 21 | NSArray *voices = [NSSpeechSynthesizer availableVoices];
|
---|
[34] | 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
|
---|
[355] | 27 | item = [menu addItemWithTitle: @"«unknown»" action: nil keyEquivalent: @""];
|
---|
[34] | 28 | [item setEnabled: NO];
|
---|
| 29 | [menu addItem: [NSMenuItem separatorItem]];
|
---|
[364] | 30 | if (voices == nil || [voices count] == 0) {
|
---|
[103] | 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: @""];
|
---|
[34] | 32 | [item setEnabled: NO];
|
---|
| 33 | } else {
|
---|
[364] | 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];
|
---|
[34] | 41 | [item setTarget: self];
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
[364] | 44 | if (_speaker == nil)
|
---|
| 45 | [self selectItemAtIndex: [menu indexOfItemWithRepresentedObject: [NSSpeechSynthesizer defaultVoice]]];
|
---|
[34] | 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 |
|
---|
[364] | 64 | - (NSSpeechSynthesizer *)_speaker;
|
---|
[34] | 65 | {
|
---|
[364] | 66 | if (_speaker == nil) _speaker = [[NSSpeechSynthesizer alloc] initWithVoice: nil];
|
---|
[34] | 67 | return _speaker;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | - (void)_invalidateVoiceSelection;
|
---|
| 71 | {
|
---|
| 72 | [self _refreshVoiceList];
|
---|
| 73 | [self selectItemAtIndex: 0];
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[53] | 76 | - (void)setVoice:(NSString *)voice;
|
---|
| 77 | {
|
---|
[364] | 78 | int voiceIdx = [self indexOfItemWithRepresentedObject: voice];
|
---|
[53] | 79 | if (voiceIdx == -1) {
|
---|
| 80 | [self _invalidateVoiceSelection];
|
---|
| 81 | } else {
|
---|
| 82 | [self selectItemAtIndex: voiceIdx];
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[34] | 86 | - (void)_previewVoice;
|
---|
| 87 | {
|
---|
[364] | 88 | NSString *voice = [[self selectedItem] representedObject];
|
---|
[34] | 89 | NSString *previewString = nil;
|
---|
| 90 |
|
---|
| 91 | [_speaker stopSpeaking];
|
---|
| 92 |
|
---|
[364] | 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]);
|
---|
[34] | 96 | [self _invalidateVoiceSelection];
|
---|
| 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | if (_delegate != nil && [_delegate respondsToSelector: @selector(voicePopUpButton:previewStringForVoice:)]) {
|
---|
[364] | 101 | previewString = [_delegate voicePopUpButton: self previewStringForVoice: voice];
|
---|
[34] | 102 | }
|
---|
| 103 |
|
---|
[364] | 104 | if (previewString == nil)
|
---|
| 105 | previewString = [[NSSpeechSynthesizer attributesForVoice: voice] objectForKey: NSVoiceDemoText];
|
---|
[34] | 106 |
|
---|
[364] | 107 | [_speaker startSpeakingString: previewString];
|
---|
[34] | 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 |
|
---|
[43] | 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 |
|
---|
[34] | 128 | - (void)setDelegate:(id)delegate;
|
---|
| 129 | {
|
---|
| 130 | _delegate = delegate;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | - (id)delegate;
|
---|
| 134 | {
|
---|
| 135 | return _delegate;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | @end |
---|