Using WASTE


WASTE is an almost-compatible TextEdit replacement which overcomes some of the limitations of it (like the 32K limit) and provides some extensions (drag and drop, images, undo support). Moreover, it has a much cleaner interface and is therefore easier integrated in Python.

WASTE is written by Marco Piovanelli, <piovanel@kagi.com>, and copyrighted by him. You can always obtain the latest version (for use in C or Pascal programs) and the documentation from <ftp://ftp.dsi.unimi.it/DSI/piovanel/WASTE>. We explain the useage of waste here by showing how to modify the TextEdit based ped.py of the previous example into the waste-based wed.py, so you should have both sources handy.

Functionally, wed.py provides three new things: resizable windows, a horizontal scroll bar and undo.

Let us look at the code, first at the application class Wed. The only real change is that we now handle undo. Aside from enabling it in the creation routine and the addition of a callback routine there is a bit of new code in updatemenubar: Waste not only handles the full details of implementing undo, it will also tell us what the next undo operation will undo (or redo). We use this to our advantage by changing the undo menu label to tell the user.

The WasteWindow has seen a bit more change. Initialization of the waste data structure is a bit different, in that we can specify some options at creation time. Also, waste has no SetText method but a UseText which expects a handle as parameter. We have to be very careful that we keep this handle around, because Python will happily free the handle if we have no more references to it (and I doubt that Waste would like this:-). A final difference in open is that we use a large number for the destination rectangle width, because we will use a horizontal scroll bar.

The idle method is a bit more involved, since we also call WEAdjustCursor to provide the correct cursor based on mouse-position. Users like this.

Getscrollbarvalues is simpler than its' TextEdit counterpart because Waste correctly updates the destination rectangle when the document changes. Also note that waste uses accessor functions to get at internal values, as opposed to direct struct access for TextEdit.

Scrollbar_callback on the other hand is more elaborate (but also provides more functionality). It also handles horizontal scrolls (scrolling one-tenth and half a screenful with the buttons). This function is also "multi-font-ready" in that scrolling one line will do the expected thing in case of multiple fonts. We will implement a multi-font editor later. A minor annoyance of Waste is that is does not provide a pinned scroll, so at the end of our callback routine we have to check that we have not scrolled past the beginning or end of the document, and adjust when needed.

do_update is also changed, because Waste is completely region-based (as opposed to rect-based). Hence, we erase regions here and we can also return immedeately if there is nothing to update.

Do_postresize is new: because Waste uses accessor functions we can now modify the viewRect from Python, which is impossible in the Python TextEdit interface, and hence we can implement resize. The do_key and do_contentclick methods have also seen minor changes, because the corresponding waste routines need a bit more information than their TextEdit counterparts. The Cut/copy/paste code is simplified, because Waste uses the normal desktop scrap.

Implementing undo is a wonder of simplicity: Waste handles all the details for us. Also, the new can_paste method (which controls greying out of the paste menu entry) is an improvement over what ped did: in ped it was possible that paste was enabled but that the data on the scrap was incompatible with TextEdit. No more such problems here.

That is all for now. There is an undocumented extended version of wed, swed.py, which supports multiple fonts, sizes and faces, and uses Waste's tab-calculation to do tab characters "right". There is also an even more elaborate example, htmled.py which extends swed with the ability to import html files, showing the use of color and how to use embedded object (rulers, in this case). These two programs have not been documented yet, though, so you will have to look at them without guidance.


Back to the index to pick another example.