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
- changing the font used in the word-processing view
- changing the font size used in the word-processing view
- capitalizing the selected text range
- censoring the selected text range by replacing the text with a graphic image
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 theButtonClickScript
andPickActionScript
methods of the "Font" button displayed in the TXWord button bar.Listing 3-10 Changing the font in TXWord
The{_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; },
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, thePickActionScript
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 theButtonClickScript
andPickActionScript
methods of the "Size" button displayed in the TXWord button bar.Listing 3-11 Changing the font size in TXWord
The{_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; }
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, thePickActionScript
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. TheCensor
method, which is shown in Listing 3-12, is called by theButtonClickScript
of the "Censor" button when the user taps the button.Listing 3-12 Replacing the selcted text
TheCensor:
func() begin local graphicSpecForTXView := { class: 'graphics, shape: MakeShape(kJustSayNoPict) }; local range := editor:GetHiliteRange(); editor:Replace(range, graphicSpecForTXView, true); end,
Censor
function uses two of the *protoTXView
methods to replace the selected text with a shape:
- it calls the *
GetHiliteRange
method to retrieve the currently selected text range- it calls the *
Replace
method to replace that range with a shape. The shape,graphicSpecForTXView
, is a graphic shape that TXWord creates from a picture that it reads from its resources at program initialization time.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 theButtonClickScript
method of the "UpperCase" button displayed in the TXWord button bar.Listing 3-13 Converting the selcted text to uppercase
The "UpperCase" button's{_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, }
ButtonClickScript
converts the text using the following steps:
- It retrieves the range and styles for the selected text.
- Itapplies the
Upcase
function to the text range.- It resets the styles slot of the selected range.
- It calls the *
SetHiliteRange
method to ensure that the new text is visible and selected.Adding a Recognized Word to Your Word-Processing View
Although protoTXView does not handle handwriting recognition, you can use theViewWordScript
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
The TXWord implementation ofViewWordScript:
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,
ViewWordScript
calls the global functionGetWordArray
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 doesclParagraphView
. For example, the user does not get the correction picker if he or she double-taps on a word.![]()
Main | Top of Section | What's New | Apple Computer, Inc. | Find It | Feedback | Help