Dynamically Adding to ProtoTextList Confuses Scrolling

One of the Newton 2.x OS Q&As
Copyright © 1997 Newton, Inc. All Rights Reserved. Newton, Newton Technology, Newton Works, the Newton, Inc. logo, the Newton Technology logo, the Light Bulb logo and MessagePad are trademarks of Newton, Inc. and may be registered in the U.S.A. and other countries. Windows is a registered trademark of Microsoft Corp. All other trademarks and company names are the intellectual property of their respective owners.


For the most recent version of the Q&As on the World Wide Web, check the URL: http://www.newton-inc.com/dev/techinfo/qa/qa.htm
If you've copied this file locally, click here to go to the main Newton Q&A page.
This document was exported on 7/23/97.


Dynamically Adding to ProtoTextList Confuses Scrolling (1/15/97)

Q: I am adding items to a protoTextList after it is displayed. I add an item and scroll to highlight that item. However, the state of the scroll arrows does not correctly get updated. Sometimes it will indicate that there are more items to scroll when it is really at the end of the list.

A: There is a problem with Newton 2.0 OS devices (although not Newton 2.1 OS devices) that causes the protoTextList to reset the scroll distance when you update the listItems array. The workaround is to always scroll the list to the top before calling SetupList when you add items. Then you can scroll the list to where you want it. Note that this workaround is safe to use in Newton 2.1 OS as well. In other words, if you are adding items to a protoTextList, use this workaround unless your application is Newton 2.1 OS-specific. This method will add a single item to the protoTextList, set the highlighted item to the new item and scroll if required. It will also make sure the item is unique.

    AddListItem := func(newItem)
    begin
         // Insert the item if not already in Array
        local index := BInsert(listItems, item, '|str<|, nil, true);

        // item must be in the array and index will point to the item.

        if NOT index then
        begin
            :Notify(kNotifyAlert, kAppName, "Duplicate entry.");
            return nil;
        end;

        // workaround a bug in 2.0 that causes the
        // scroll arrows to get out of sync
        // do this by scrolling to the top
        :DoScrollScript(-viewOriginY) ;

        self:SetUpList();

        // Setting the selection slot will highlight the item
        selection := index;

        // scroll to show the new item
        if index >= viewLines then
            :DoScrollScript((index - viewLines + 1) * lineHeight) ;

        self:RedoChildren();

        return true;
    end ;