Creating a Works Word Processor Document with Data

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.


NEW: Creating a Works Word Processor Document with Data (6/9/97)

Q: How do I create a new Works paper document with some initial data? I'd like something like the Notes application's MakeTextNote function.

A: First, review the Q&A entitled "How to Create Newton Works Documents" for details on actually creating the document.

To create initial data for a word processor document, the simplest thing to do is to create a dummy view based on protoTXView. Use the protoTXView methods to add data to that view. When done, use the Externalize method to get the data in a form suitable for saving in the Works soup.

When creating your dummy protoTXView, it's imperative that you call the SetStore method so that the data is created on the user store rather than the NS heap. Different formats are used for store-backed and heap-backed protoTXViews, and the type of backing is carried into the Externalized data. As a result, failure to use SetStore would cause you to create a Works document that was not backed by the user store and which could eventually result in out-of-memory errors when the user added sufficient data to the document.

Here's an example of how to create a dummy text view and populate it with some initial data. You may wish to vary the store passed to SetStore in the viewSetupFormScript and AdoptEntryFromStationery, or the intial text specified in the 2nd paramater to Replace. (Notably, you may wish to provide styles for the text, see the Newton 2.1 OS documentation on the protoTXView method Replace.)

    // create and populate a dummy protoTXView
    local textView := BuildContext(
        {
            _proto: protoTXView,
            viewBounds: SetBounds(0, 0, 0, 0),
            viewFlags: 0,
            ReorientToScreen: ROM_DefRotateFunc,
            viewSetupFormScript: func() begin
                inherited:?viewSetupFormScript();
                self:SetStore(GetDefaultStore());
            end,
        });
    textView:Open();
    textView:Replace({first: 0, last: 0}, {text: "Some initial text"}, nil);

    // get the data in an external form for the Works soup
    local saveData := textView:Externalize();
    textView:Close();

    // Create a new Works document from the data
    GetRoot().NewtWorks:AdoptEntryFromStationery(
        {
            title: "Initial Title",
            saveData: saveData,
            hiliteRange: {first: 0, last: 0},
            margins: {top: 72, left: 72, right: 72, bottom: 72},
        }, 'paper, GetDefaultStore());