/Mac/Demo/textedit.html

http://unladen-swallow.googlecode.com/ · HTML · 150 lines · 125 code · 25 blank · 0 comment · 0 complexity · f62ea75c0613bc997712d944676529cf MD5 · raw file

  1. <HTML><HEAD><TITLE>Using FrameWork and TextEdit</TITLE></HEAD>
  2. <BODY>
  3. <H1>Using FrameWork and TextEdit</H1>
  4. <HR>
  5. In this document we use the <CODE>FrameWork</CODE> and <CODE>TextEdit</CODE>
  6. modules to create a simple text editor. The functionality
  7. of the editor is very basic: you can open multiple files, type text and use
  8. cut/copy/paste. The main intention is to explain the use of FrameWork, really. <p>
  9. <H2>FrameWork</H2>
  10. The FrameWork module provides you with a skeleton application. It declares a
  11. number of classes suitable for subclassing in your application, thereby
  12. releaving you of the burden of doing all event handling, etc. yourself. For a
  13. real understanding you will have to browse the source. Here is a short overview
  14. of the classes and what functionality they provide.
  15. <dl>
  16. <dt> <CODE>Application</CODE>
  17. <dd>
  18. This is the toplevel class you will override. It maintains the menu bar and contains
  19. the main event handling code. Normal use is to override the <code>__init__</code> routine
  20. to do your own initializations and override <code>makeusermenus</code> to create your menus
  21. (your menu callback routines may be here too, but this is by no means necessary).
  22. The event handling code can be overridden at various levels, from very low-level (the
  23. <code>dispatch</code> method) to intermedeate level (<code>do_keyDown</code>, for instance)
  24. to high-level (<code>do_key</code>). The application class knows about the <code>Window</code>
  25. objects you create, and will forward events to the appropriate window (So, normally you
  26. would have a <code>do_key</code> method in your window object, not your application object).
  27. <dt> <CODE>MenuBar</CODE>, <CODE>Menu</CODE> and <CODE>MenuItem</CODE>
  28. <dd>
  29. These classes (and a few friends like <CODE>SubMenu</CODE>) handle your menus. You would not
  30. normally override them but use them as-is. The idiom for creating menus is a bit strange,
  31. see the test code at the bottom of FrameWork for sample use. The apple menu is handled for you
  32. by <CODE>MenuBar</CODE> and <CODE>Application</CODE>.
  33. <dt> <CODE>Window</CODE>
  34. <dd>
  35. The basic window. Again, a class that you normally subclass in your application, possibly
  36. multiple times if you have different types of windows. The init call instantiates the data
  37. structure but actually opening the window is delayed until you call <code>open</code>. Your
  38. open method should call <code>do_postopen</code> to let the base class handle linking in to
  39. the application object. Similarly with <code>close</code> and <code>do_postclose</code>. The
  40. rest of the code is mainly event-oriented: you override <code>do_postresize</code>,
  41. <code>do_contentclick</code>, <code>do_update</code>, <code>do_activate</code>
  42. and <code>do_key</code> to "do your thing". When these methods are called the relevant environment
  43. has been setup (like <code>BeginDrawing</code> has been called for updates, etc).
  44. <dt> <CODE>windowbounds</CODE>
  45. <dd>
  46. Not a class but a function: you pass it a width and height and it will return you a rectangle
  47. you can use to create your window. It will take care of staggering windows and it will try
  48. to fit the window on the screen (but the resulting rect will <em>always</em> have the size you
  49. specify).
  50. <dt> <CODE>ControlsWindow</CODE>
  51. <dd>
  52. A subclass of Window which automatically handles drawing and clicking for controls. You override
  53. the same methods as for Window (if you need to: control-related things are done automatically) and
  54. <code>do_controlhit</code>.
  55. <dt> <CODE>ScrolledWindow</CODE>
  56. <dd>
  57. A subclass of ControlsWindow, a window with optional scrollbars. If you override <code>do_activate</code>
  58. or <code>do_postresize</code> you must call the ScrolledWindow methods at the end of your override.
  59. You call <code>scrollbars</code> to enable/disable scrollbars and <code>updatescrollbars</code> to
  60. update them. You provide <code>getscrollbarvalues</code> to return the current x/y values (a helper
  61. method <code>scalebarvalues</code> is available) and <code>scrollbarcallback</code> to update your
  62. display after the user has used the scrollbars.
  63. <dt> <CODE>DialogWindow</CODE>
  64. <dd>
  65. A modeless dialog window initialized from a DLOG resource. See the
  66. <A HREF="example2.html">second Interslip example</A> for its useage.
  67. </dl>
  68. <H2>A sample text editor</H2>
  69. Let us have a look at <A HREF="textedit/ped.py">ped.py</A> (in the Demo:textedit folder), the Pathetic
  70. EDitor. It has multiple windows, cut/copy/paste and keyboard input, but that is about all. It looks
  71. as if you can resize the window but it does not work. Still, it serves as an example.
  72. Ped creates two classes, <code>TEWindow</code> and <code>Ped</code>. Let us start with the latter one,
  73. which is a subclass of <code>FrameWork.Application</code> and our main application. The init function
  74. has little to do aside from the standard init: it remembers a window sequence number (for untitled windows),
  75. and sets things up for menu disable to work. Remember, the <code>makeusermenus</code> is called
  76. automatically. <p>
  77. <code>Makeusermenus</code> creates the <code>File</code> and <code>Edit</code> menus. It also initializes
  78. a couple of lists that are used later to correctly enable and disable menu items (and complete menus) depending
  79. on whether a window is open, text is selected, etc. The callback functions for the menu items are
  80. all methods of this class. <p>
  81. <code>Updatemenubar</code> handles greying out (and re-enabling) of menu items depending on whether there
  82. is a current window and its state. <p>
  83. The rest of the methods are all callbacks and simple to understand. They check whether there is an active
  84. window (and complain loudly if there is none: the corresponding menu entry should have been disabled
  85. in that case!) and call the appropriate window method. Only the <code>_open</code> method (the common code
  86. for <code>Open</code> and <code>New</code>) deserves some mention. It instantiates a <code>TEWindow</code>
  87. object and opens it with the title, filename and contents of the file to edit. Note that FrameWork takes
  88. care of remembering the window object. A minor note on opening the file in binary mode: this is because
  89. TextEdit expects MacOS style carriage-return terminated lines, not python/unix/C style newline-terminated
  90. lines. <p>
  91. Oh yes: the <code>quit</code> callback does a little magic too. It closes all windows, and only if this
  92. succeeds it actually quits. This gives the user a chance to cancel the operation if some files are unsaved.
  93. <p>
  94. Lastly, there is the <code>idle</code> method, called by the Application base class when no event
  95. is available. It is forwarded to the active window, so it can blink the text caret. <p>
  96. The <code>TEWindow</code> object handles a single window. Due to this structuring it is absolutely no
  97. problem to have multiple windows open at the same time (although a real application should exercise care when
  98. two windows refer to the same document). TEWindow uses the standard init code inherited from
  99. <code>ScrolledWindow</code>, and sets itself up at the time of the <code>open</code> call. It obtains screen
  100. coordinates, opens the window, creates rectangles for TextEdit to work in (the magical number <code>15</code>
  101. here is the size of a normal scroll bar: unfortunately there is no symbolic constant for it),
  102. creates the TextEdit object and initializes it with our data. Finally, the scroll bars are created (the
  103. initial values will be obtained automatically through <code>getscrollbarvalues</code>) and we activate
  104. ourselves (this is unfortunately not done automatically by the MacOS event handling code). <p>
  105. <code>Do_idle</code> simply calls the TextEdit routine that blinks the cursor. <code>Getscrollbarvalues</code>
  106. returns the current X and Y scrollbar values, scaled to <code>0..32767</code>. For X we return <code>None</code>,
  107. which means "no scrollbar, please", for Y we use the scaler provided by <code>ScrolledWindow</code>. <p>
  108. <code>Scrollbar_callback</code> is called when the user uses the scrollbar. It is passed a string <code>'x'</code>
  109. or <code>'y'</code>, one of <code>'set', '-', '--', '+', '++'</code> and (for <code>set</code>) an absolute
  110. value. Note that the sign of the value passed to <code>TEPinScroll</code> is counter-intuitive. <p>
  111. <code>do_activate</code> (de)activates the scrollbars and calls the relevant TextEdit routine. Moreover, it
  112. tells the application object if we are now the active window, and updates the menubar. The next few methods
  113. are update and menu callbacks, and pretty straightforward. Note that <code>do_close</code> can
  114. return without closing the window (if the document is changed and the users cancels out of the operation).
  115. Also note the "magic" in <code>menu_save_as</code>
  116. that set the correct window title. <p>
  117. Things get moderately interesting again at the cut/copy/paste handling, since the TextEdit scrap is
  118. separate from the desktop scrap. For that reason there are various calls to routines that move the scrap
  119. back and forth. <code>Have_selection</code> is called by the menubar update code to determine whether cut and
  120. copy should be enabled. <p>
  121. Understanding the main program is left as an exercise to the reader. <p>
  122. <hr>
  123. That's all for this example, you could now continue with the <A HREF="waste.html">next example</A>, where we use WASTE, a more-or-less
  124. TextEdit compatible library with more functionality, to rebuild our editor. Or you can
  125. return to the <A HREF="index.html">table of contents</A> to pick another topic. <p>