Examples

Change Sort Order

We want to provide a way to sort by name, by title, or by acquire date. In order to change the sort order, we'll be dynamically changing the soupQuery in the allSoups.mySoup frame. A QuickTime movie of this example is available.

1. Add a SortBy slot to the newtApplication template which will be responsible for actually changing the sort order:

func(newSortSymbol)
begin
   // if already using that sort order, do nothing
   if allSoups.mySoup.soupQuery.indexPath =
      newSortSymbol then
      return;

   // since we'll be writing to the mySoup frame
   // we need to make sure it's in the frames heap
   // proto to the template version (if necessary)
   if IsReadOnly(allSoups) then
      self.allSoups := {_proto: allSoups};
   if IsReadOnly(allSoups.mySoup) then
      allSoups.mySoup := 
         {_proto: allSoups.mySoup};

   // change the soupQuery to the new sort order
   allSoups.mySoup.soupQuery := 
      {indexPath: newSortSymbol};
   // redo the query
   allSoups.mySoup:SetupCursor();
   
   // recreate children which will re-read from 
   // cursor
   :RedoChildren();
end;
2. Create a button to add to the status bar. The button will provide three choices: "Sort by author", "Sort by Title", and "Sort by acquire date". Start by creating a new layout file.

3. Draw within it a protoPopupButton (see Newton Programmer's Guide: System for complete information on protoPopupButton) and edit the following slots in the popup button:

text
" Sort" // two spaces at the
// beginning of the string to
// leave room for the diamond
// picker

popup
[
"by author",
"by title",
"by acquire date",
]

PickActionScript
func(itemSelected)
begin
GetRoot().(kAppSymbol):SortBy(
sortSymbols[itemSelected]);

// call the inherited version to
// unhilite the button
inherited:?PickActionScript(
itemSelected);
end

sortSymbols
[
'author,
'title,
'acquireDate,
]


Note:For a protoPopupButton, the PickActionScript is called when the user chooses an item from the popup.


4. Save the layout file as "sortButton.t" and add it to the project. Make sure to change the build order so it is built before "Main.t".

5. To display the button in the statusBar, modify the menuLeftButtons slot of the newtStatusBar to:

[
   newtInfoButton,
   GetLayout("sortButton.t"),
]
At this point, you can test the application. The sort picker works, as you can tell, by changing the sort order in both the overview and the default view (see FIGURE 10.13).

FIGURE 10.13 : The working sort picker.


The current sort order is not displayed to the user, however. To fix this problem, we'll modify the picker to show a checkmark next to the current sort order. A QuickTime movie of this example is available. The popup array in the protoPopupButton contains the strings which will be displayed in the picker. The popup array can also hold a frame with a checkmark instead of just a string. To get this to happen we will dynamically construct the popup array when the button is tapped.

6. First, we'll add a GetSortBy method to the newtApplication template. It will return the current sort order:

func()
begin
   return allSoups.mySoup.soupQuery.indexPath;
end;
7. In the protoPopupButton, rename the popup slot to plainPopup (we'll use this to store the original strings).

8. Add a buttonClickScript to the protoPopupButton. This is called when the user taps on the button:

func()
begin
   // make writable copy of array
   self.popup := Clone(plainPopup);
   
   // what is the index of the current sort order?
   local index := ArrayPos(sortSymbols, 
      GetRoot().(kAppSymbol):GetSortBy(), 0, nil);
   
   // make this item be checkmarked.
   popup[index] := {mark: kCheckMarkChar,
             item: plainPopup[index],
            };
   
   // call the inherited version to do the popup
   inherited:?ButtonClickScript();
end
Now when we build and test the application, the picker correctly displays a checkmark (see FIGURE 10.14).

FIGURE 10.14 : The working sort picker with a checkmark.


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996