1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<HTML><HEAD><TITLE>Using WASTE</TITLE></HEAD>
<BODY>
<H1>Using WASTE</H1>
<HR>
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. <p>
WASTE is written by Marco Piovanelli, <A HREF="mailto:piovanel@kagi.com"><piovanel@kagi.com></A>,
and copyrighted by him. You can always obtain the latest version (for use in C
or Pascal programs) and the documentation from
<A HREF="ftp://ftp.dsi.unimi.it/DSI/piovanel/WASTE"><ftp://ftp.dsi.unimi.it/DSI/piovanel/WASTE></A>.
We explain the useage of waste here by showing how to modify the TextEdit based
<A HREF="textedit/ped.py">ped.py</A> of the
<A HREF="textedit.html">previous example</A> into the waste-based <A HREF="waste/wed.py">wed.py</A>,
so you should have both sources handy. <p>
Functionally, <code>wed.py</code> provides three new things: resizable windows, a horizontal
scroll bar and undo. <p>
Let us look at the code, first at the application class <code>Wed</code>. The only real change is that
we now handle <code>undo</code>. Aside from enabling it in the creation routine and the addition of
a callback routine there is a bit of new code in <code>updatemenubar</code>: 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. <p>
The <code>WasteWindow</code> 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 <code>SetText</code>
method but a <code>UseText</code> which expects a handle as parameter. We have to be <EM>very</EM> 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 <code>open</code>
is that we use a large number for the destination rectangle width, because we will use a horizontal scroll
bar. <p>
The <code>idle</code> method is a bit more involved, since we also call <code>WEAdjustCursor</code> to
provide the correct cursor based on mouse-position. Users like this. <p>
<code>Getscrollbarvalues</code> 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. <p>
<code>Scrollbar_callback</code> 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. <p>
<code>do_update</code> 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. <p>
<code>Do_postresize</code> 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
<code>do_key</code> and <code>do_contentclick</code> 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. <p>
Implementing undo is a wonder of simplicity: Waste handles all the details for us. Also, the new
<code>can_paste</code> method (which controls greying out of the paste menu entry) is an improvement
over what <code>ped</code> 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. <p>
<hr>
That is all for now. At some point in the future I will extend this editor to incorporate multiple fonts and
pointsizes, pictures and possibly drag-and-drop editing but I will first have to write the code for it.
For now you will have to go back to the <A HREF="index.html">index</A> to pick another example.
|