//--------------------------------------------------------------------- // Creates one of our simple document windows and returns a ref to it. // WindowRef MyCreateNewDocumentWindow(void) { IBNibRef nib; WindowRef window = NULL; OSStatus status; // Create one of our special main document windows status = CreateNibReference(CFSTR("main"), &nib); require_noerr( status, CantGetNIBRef ); status = CreateWindowFromNib(nib, CFSTR("MainDocumentWindow"), &window); require_noerr( status, CantCreateWindow ); // Set its default options status = MySetTextViewOptions(GetTextViewFromWindow(window)); check_noerr( status ); // Make the window transparent status = MakeWindowTransparent(window); check_noerr( status ); // Make the TextView partially transparent status = TextViewSetAlpha(GetTextViewFromWindow(window), 0.25); check_noerr( status ); // Initialize the window title and proxy icon status = SetWindowTitleWithCFString(window, CFSTR("Untitled")); check_noerr( status ); status = SetWindowProxyCreatorAndType(window, kUnknownType, kUnknownType, kOnSystemDisk); check_noerr( status ); // Show the window ShowWindow(window); // Cleanup CantGetViewRef: CantCreateWindow: DisposeNibReference(nib); CantGetNIBRef: return window; }
// Event handling for window focus acquired events pascal OSStatus WindowFocusAcquired(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData) { #pragma unused (nextHandler, userData) WindowRef window; OSStatus status = noErr; GetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window); require_noerr( status, CantGetEventParameter); if ( window != NULL ) { HIViewRef textView; status = GetTextViewFromWindow(window, textView); if ( status == noErr ) { UniChar mark; // For checking menu items // Handle font panel menu item and palette if ( TextViewIsFontPanelSupportEnabled(textView) ) { EnableMenuCommand(NULL, kHICommandShowHideFontPanel); if ( ! FPIsFontPanelVisible() ) verify_noerr( FPShowHideFontPanel() ); } else { DisableMenuCommand(NULL, kHICommandShowHideFontPanel); if ( FPIsFontPanelVisible() ) verify_noerr( FPShowHideFontPanel() ); } // Handle spelling menu items if ( TextViewIsSpellingSupportEnabled(textView) ) { EnableMenuCommand(NULL, kHICommandShowSpellingPanel); EnableMenuCommand(NULL, kToggleAutoSpellcheckCommand); MyShowSpellCheckPanel(); } else { DisableMenuCommand(NULL, kHICommandShowSpellingPanel); DisableMenuCommand(NULL, kToggleAutoSpellcheckCommand); } // Auto-spellcheck menu item mark = ( TXNGetSpellCheckAsYouType(HITextViewGetTXNObject(textView)) ) ? kMenuCheckmarkGlyph : kMenuNullGlyph; verify_noerr( SetMenuCommandMark(NULL, kToggleAutoSpellcheckCommand, mark) ); } } CantGetEventParameter: return status; }
OSStatus TextViewFocusInWindow( WindowRef window ) { OSStatus status = paramErr; HIViewRef textView = NULL; HIViewRef scrollView = NULL; // Get the HITextView from the window status = GetTextViewFromWindow( window, textView ); require_action( textView != NULL, EXIT, status = paramErr ); scrollView = HIViewGetSuperview( textView ); require_action( scrollView != NULL, EXIT, status = paramErr ); HIViewSetFirstSubViewFocus( HIViewGetRoot(window), textView ); status = HIViewSetNextFocus( scrollView, NULL ); status = HIViewAdvanceFocus ( scrollView, 0 /*modifiers*/ ); EXIT: return status; }
// Resets the state of the controls in a window based on the state of the TextView OSStatus UpdateControlsFromTextViewWindow( WindowRef window ) { OSStatus status; HIViewRef textView; ControlRef control; SInt32 newValue; UInt32 unsignedValue; status = GetTextViewFromWindow(window, textView); require_noerr( status, CantGetTextView ); // Font panel checkbox status = GetControlByID(window, &kFontPanelCheckboxControl, &control); require_noerr( status, CantGetControl ); newValue = TextViewIsFontPanelSupportEnabled(textView) ? kControlCheckBoxCheckedValue : kControlCheckBoxUncheckedValue; SetControl32BitValue(control, newValue); // Spelling checkbox status = GetControlByID(window, &kSpellCheckboxControl, &control); require_noerr( status, CantGetControl ); newValue = TextViewIsSpellingSupportEnabled(textView) ? kControlCheckBoxCheckedValue : kControlCheckBoxUncheckedValue; SetControl32BitValue(control, newValue); // Autoscroll radio button status = GetControlByID(window, &kAutoScrollRadioButtonControl, &control); require_noerr( status, CantGetControl ); status = TextViewGetObjectControlData( textView, kTXNAutoScrollBehaviorTag, kUnsigned, newValue, unsignedValue ); require_noerr( status, CantGetValue ); newValue += 1; // Autoscroll constants are zero-based, radio button state is one-based SetControl32BitValue(control, newValue); CantGetValue: CantGetControl: CantGetTextView: return status; }
// Each time a new window is first opened, call this one time function to set up // the text view OSStatus SetUpTheTextView( WindowRef window ) { OSStatus status = noErr; HIViewRef textView = NULL; HIViewRef scrollView = NULL; HIViewRef scrollParentView = NULL; CMLTEViewData* mlteData = NULL; // Get the HITextView from the window status = GetTextViewFromWindow( window, textView ); require_action( textView != NULL, EXIT, status = paramErr ); // make a new custom C++ object to hold MLTE related data mlteData = new CMLTEViewData(); // DON'T FORGET TO DISPOSE THIS WHEN HITextView destructs!!! // put the custom object in the HITextView for this window // as a control property so that we can retrieve it later when we need it. status = TextViewStoreMLTEInstanceData( textView, mlteData ); require_action( textView != NULL, EXIT, status = paramErr ); // Now set the text view as we like it status = TextViewDefaultSettings( textView ); check_noerr( status ); status = TextViewAddActionNameMapper( textView ); check_noerr( status ); status = TextViewSetMargins( textView, 0 /*top*/, 0 /*left*/, 0 /*right*/ ); check_noerr( status ); // get fancy - try to add a picture behind the textView scrollView = HIViewGetSuperview( textView ); scrollParentView = HIViewGetSuperview( scrollView ); if( scrollParentView != NULL ) { HIViewRef imageViewRef = AddImage( scrollParentView ); if( imageViewRef != NULL ) { HIRect scrollFrame; HIViewGetFrame( scrollView, &scrollFrame ); HIViewSetFrame( imageViewRef, &scrollFrame ); status = HIViewSetZOrder( scrollView, kHIViewZOrderAbove, imageViewRef ); check_noerr( status ); HIViewSetVisible(imageViewRef, true); status = TextViewSetBGAlpha( textView, 0.75 ); check_noerr( status ); } } // register for menu handing status = TextViewInstallMenuHandlers( textView ); EXIT: return status; }
// Event handling for HICommand events pascal OSStatus CommandProcess(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData) { #pragma unused (nextHandler, userData) WindowRef window; HIViewRef textView; HICommandExtended inCommand; OSStatus status = noErr; status = GetEventParameter(theEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(HICommandExtended), NULL, &inCommand); require_noerr( status, CantGetEventParameter); // Check to see if this came from one of the buttons if ( inCommand.attributes & kHICommandFromControl ) { ControlRef hitControl; // Get the control and the window & text view associated with it hitControl = inCommand.source.control; window = HIViewGetWindow(hitControl); status = GetTextViewFromWindow(window, textView); if ( status != noErr ) return status; // The button controls need the text view to be the active focus // in order for the HICommands to be correctly dispatched. // So we will just force the TextView to be the focus before handling. // the HICommand. status = TextViewFocusInWindow( HIViewGetWindow(hitControl) ); if ( status != noErr ) return status; switch (inCommand.commandID) { case kDefaultHITextViewCommand: // **** Reset button status = TextViewDefaultSettings( textView ); if (status == noErr) status = UpdateControlsFromTextViewWindow(window); if (status == noErr) status = TextViewFocusInWindow( window ); break; case kFontPanelSupportCommand: // **** Checkboxes if ( GetControl32BitValue(hitControl) == kControlCheckBoxCheckedValue ) status = TextViewDemoFontPanelSupport( textView ); else status = TextViewFontPanelSupport(textView, false); break; case kDemoSpellingSupportCommand: if ( GetControl32BitValue(hitControl) == kControlCheckBoxCheckedValue ) status = TextViewDemoSpellingSupport( textView ); else status = TextViewSpellingSupport(textView, false); break; case kDemoAutoScrollCommand: // **** Radio button // The radio button values correspond to the MLTE auto scroll constants // plus one (the constants start from zero, the radio button values // start from one): // // (radio button 1) kTXNAutoScrollInsertionIntoView == 0 // (radio button 2) kTXNAutoScrollNever == 1 // (radio button 3) kTXNAutoScrollWhenInsertionVisible == 2 // // See the definition of the type TXNAutoScrollBehavior for // more information on each constant and its associated behavior. status = TextViewScrollingOptions( textView, (UInt32)(GetControl32BitValue(hitControl) - 1) ); break; case kDemoActionGrouping: // **** Demo actions status = TextViewDemoActionGroup( textView ); break; case kHICommandSave: case kDemoCFURLWriteCommand: status = TextViewDemoWriteToCFURL( textView ); break; case kDemoCFURLReadCommand: status = TextViewDemoReadFromCFURL( textView ); break; default: status = eventNotHandledErr; break; } } else { switch (inCommand.commandID) { case kHICommandNew: status = NewWindow(); break; case kHICommandOpen: window = GetFrontWindowOfClass(kDocumentWindowClass, true); if ( window != NULL ) { status = GetTextViewFromWindow(window, textView); if ( status == noErr ) status = TextViewDemoReadFromCFURL( textView ); } break; default: status = eventNotHandledErr; break; } } CantGetEventParameter: return status; }