/* File: SpeechDrvr.c Author: Michael j. Norton Parent File: BasicCarbEvents.c Note: File as shipped with Apple CarbonLib_1.1d11_SDK Sampel Code. Code added to example to demonstarte Speech recognition manager. Search for string "Speech Recognition" in comments to locate changes. Sample Code file BasicCarbEvents.c renamed to SpeechDrvr.c */ #define TARGET_API_MAC_CARBON 1 #include #include #include #include #include "LangModel.h" #include "SpeechRecLib.h" #include "SpeechEvent.h" static OSStatus InstallStandardMenuBar(void); static pascal OSStatus MyWindowEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData); static pascal OSStatus MyAppleEventHandler (EventHandlerCallRef mySpeechHandler, EventRef inEvent, void* userData); static void HandleWindowUpdate(WindowRef window); static pascal OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, UInt32 refcon); #define mFile 129 #define iQuit 1 void main(void) { Rect bounds = { 100, 100, 300, 500 }; WindowRef window; OSStatus err; EventHandlerRef ref; EventTypeSpec list[] = { {kEventClassWindow, kEventWindowClose }, { kEventClassWindow, kEventWindowDrawContent } }; EventTypeSpec aelist[] = { {kEventClassEPPC, kEventHighLevelEvent} }; /* added for Speech Recognition */ InitCursor(); err = InstallStandardMenuBar(); err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(QuitAppleEventHandler), 0, false); err = CreateNewWindow(kDocumentWindowClass, kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute, &bounds, &window); InstallWindowEventHandler(window, NewEventHandlerUPP(MyWindowEventHandler), 2, list, 0, &ref); ShowWindow(window); /* start Speech Recogniton Manager initialization */ InitSpeech (); InitLanguageModel(kLangModelName, kRecWordList, kTopLMRefcon, kHelloRefCon ); EnableSpeechModel(); /* end Speech Recogniton Manager initialization */ InstallEventHandler( GetApplicationEventTarget(), NewEventHandlerUPP( MyAppleEventHandler ), 1, aelist, 0, NULL ); /* install an event handler for the Speech Recognition recognizer events */ AEInstallEventHandler (kAESpeechSuite, kAESpeechDone, NewAEEventHandlerUPP(MyHandleAESpeechDone), 0, false); RunApplicationEventLoop(); } static pascal OSStatus MyWindowEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData) { WindowRef window; OSStatus result = eventNotHandledErr; GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(window), NULL, &window); if (GetEventKind(event) == kEventWindowDrawContent) { HandleWindowUpdate(window); result = noErr; } else if (GetEventKind(event) == kEventWindowClose) { CloseSpeech(); DisposeWindow(window); QuitApplicationEventLoop(); result = noErr; } return result; } static void HandleWindowUpdate(WindowRef window) { Rect bounds; SInt16 textWidth; Str255 text = "\pHello World!"; SetPortWindowPort(window); EraseRect(GetWindowPortBounds(window, &bounds)); TextSize(36); textWidth = StringWidth(text); MoveTo(((bounds.right - bounds.left) - textWidth) / 2, (bounds.bottom - bounds.top) / 2); // Preferably we would use a CFString for the string handling, in this // case in conjunction with TXNDrawUnicodeTextBox. DrawString(text); // SetRect(&bounds, 50,50,150,150); // TXNDrawCFStringTextBox(CFSTR("Hello World!"), &bounds, NULL, NULL); } static OSStatus InstallStandardMenuBar(void) { Handle menuBar; MenuRef menu; long response; OSStatus err = noErr; menuBar = GetNewMBar(128); if (menuBar) { SetMenuBar(menuBar); // check to see if we should modify the File menu's quit item in // order to follow the Aqua HI quit guideline err = Gestalt(gestaltMenuMgrAttr, &response); if ((err == noErr) && (response & gestaltMenuMgrAquaLayoutMask)) { menu = GetMenuHandle(mFile); DeleteMenuItem(menu, iQuit); } DrawMenuBar(); } return err; } static pascal OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, UInt32 refcon) { CloseSpeech(); QuitApplicationEventLoop(); ExitToShell(); } static pascal OSStatus MyAppleEventHandler (EventHandlerCallRef myAEHandler, EventRef inEvent, void* userData) { OSStatus myErr; if ( GetEventClass( inEvent ) == kEventClassEPPC && GetEventKind( inEvent ) == kEventHighLevelEvent ) { EventRecord er; ConvertEventRefToEventRecord( inEvent, &er ); return AEProcessAppleEvent( &er ); } return myErr; }