Title Banner

Previous Book Contents Book Index Next

Newton Developer Technical Information: Newton Programmer's Guide: 2.1 OS Addendum /
Chapter 3 - Word Processing Views / Using Word Processing Views


Handling User Interactions

The TXWord program allows the user to perform several different operations by picking buttons from a button bar. The supported user actions are:

Figure 3-2 shows the TXWord button bar. For more information about button bars, see the newtStatusBar proto in Newton Programmer's Reference. The remainder of this section describes the code used to implement each button bar action in the TXWord program.

Figure 3-2 The TXWord button bar



Changing the Font

The TXWord program allows the user to replace the font used to display the selected range of text. This is handled by the ButtonClickScript and PickActionScript methods of the "Font" button displayed in the TXWord button bar.

Listing 3-10 Changing the font in TXWord

{_proto: protoPopupButton,

text: " Font", ButtonClickScript: func() begin self.font := editor:GetContinuousRun(); // display only fonts popup := MakeFontMenu(font, nil, 'none, 'none); inherited:?ButtonClickScript(); end, PickActionScript: func(index) begin local range := editor:GetHiliteRange(); editor:ChangeRangeRuns(range, {family: popup[index].family}, nil, true); inherited:?PickActionScript(index); end; },

The ButtonClickScript for the "Font" button gets the style run for the current selection and displays a font menu with the style run's font as its initial value. When the user selects a font from the menu, the PickActionScript changes the font attribute of the currently selected text range.

Changing the Font Size

The TXWord program allows the user to replace the font size used to display the selected range of text. This is handled by the ButtonClickScript and PickActionScript methods of the "Size" button displayed in the TXWord button bar.

Listing 3-11 Changing the font size in TXWord

{_proto: protoTextButton,

text: " Size", ButtonClickScript: func() begin self.font := editor:GetContinuousRun(); // display only sizes popup := MakeFontMenu(font, 'none, nil, 'none); inherited:?ButtonClickScript(); end, PickActionScript: func(index) begin local range := editor:GetHiliteRange(); editor:ChangeRangeRuns(range, {size: popup[index].size}, nil, true ); inherited:?PickActionScript(index); end; }

The ButtonClickScript for the "Size" button gets the style run for the current selection and displays a font size menu with the style run's font size as its initial value. When the user selects a size from the menu, the PickActionScript changes the font attribute of the currently selected text range.

Replacing the Selected Text With a Graphic

The TXWord program allows the user to replace the selected text with a shape that indicates the text is censored. The graphic used to indicate that text is censored is stored in a shape object. The Censor method, which is shown in Listing 3-12, is called by the ButtonClickScript of the "Censor" button when the user taps the button.

Listing 3-12 Replacing the selcted text

Censor:

func() begin local graphicSpecForTXView := { class: 'graphics, shape: MakeShape(kJustSayNoPict) }; local range := editor:GetHiliteRange(); editor:Replace(range, graphicSpecForTXView, true); end,

The Censor function uses two of the *protoTXView methods to replace the selected text with a shape:

Converting the Selected Text to Uppercase

The TXWord program allows the user to convert the selected text into all uppercase letters. This is handled by the ButtonClickScript method of the "UpperCase" button displayed in the TXWord button bar.

Listing 3-13 Converting the selcted text to uppercase

{_proto: protoTextButton,

text: "UpperCase", ButtonClickScript: func() begin local range := editor:GetHiliteRange(); local theText:= editor:GetRangeData(range, 'text); local theStyles:= editor:GetRangeData(range, 'styles); Upcase(theText); // if we left the styles slot nil, the styles would be reset // to match the style at the beginning of the run. editor:Replace(range, {text: theText, styles: theStyles}, true); // Ensure the new text is visible and selected editor:SetHiliteRange(range, true, true); inherited:?ButtonClickScript(); end, }

The "UpperCase" button's ButtonClickScript converts the text using the following steps:

Adding a Recognized Word to Your Word-Processing View

Although protoTXView does not handle handwriting recognition, you can use the ViewWordScript to recognize a word and add it to your view. The TXWord implementation of this method is shown in Listing 3-14.

Listing 3-14 Adding a recognized word to a word-processing view

ViewWordScript:

func(unit) begin local words := GetWordArray(unit); local range := editor:GetHiliteRange(); // determine if we need to add a space before or after the word local first := range.first - 1; local last := range.last + 1; local totalChars := :GetCountCharacters(); local textToAdd := ""; if first >= 0 and totalChars > 0 then if :GetRangeData({first: first, last: first + 1}, 'text)[0] <> $ then textToAdd := textToAdd & " "; // insert space before word // add first (most likely) word returned by recognition textToAdd := textToAdd & words[0]; if last < totalChars then if :GetRangeData({first: last, last: last + 1}, 'text)[0] <> $ then textToAdd := textToAdd & " "; // add space after word editor:Replace(range, {text: textToAdd}, true); true; // Return true if input has been completely handled end,

The TXWord implementation of ViewWordScript calls the global function GetWordArray to recognize the input word (unit) and then adds the first word in the returned array -the most likely match- to the word-processing view.

Note
*protoTXView does not provide the same recognition hooks as does clParagraphView. For example, the user does not get the correction picker if he or she double-taps on a word.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
26 APR 1997



Navigation graphic, see text links

Main | Top of Section | What's New | Apple Computer, Inc. | Find It | Feedback | Help