/Doc/library/framework.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 340 lines · 195 code · 145 blank · 0 comment · 0 complexity · affe29f8d53e549a0f9679eb94ae5232 MD5 · raw file

  1. :mod:`FrameWork` --- Interactive application framework
  2. ======================================================
  3. .. module:: FrameWork
  4. :platform: Mac
  5. :synopsis: Interactive application framework.
  6. :deprecated:
  7. The :mod:`FrameWork` module contains classes that together provide a framework
  8. for an interactive Macintosh application. The programmer builds an application
  9. by creating subclasses that override various methods of the bases classes,
  10. thereby implementing the functionality wanted. Overriding functionality can
  11. often be done on various different levels, i.e. to handle clicks in a single
  12. dialog window in a non-standard way it is not necessary to override the complete
  13. event handling.
  14. .. note::
  15. This module has been removed in Python 3.x.
  16. Work on the :mod:`FrameWork` has pretty much stopped, now that :mod:`PyObjC` is
  17. available for full Cocoa access from Python, and the documentation describes
  18. only the most important functionality, and not in the most logical manner at
  19. that. Examine the source or the examples for more details. The following are
  20. some comments posted on the MacPython newsgroup about the strengths and
  21. limitations of :mod:`FrameWork`:
  22. .. epigraph::
  23. The strong point of :mod:`FrameWork` is that it allows you to break into the
  24. control-flow at many different places. :mod:`W`, for instance, uses a different
  25. way to enable/disable menus and that plugs right in leaving the rest intact.
  26. The weak points of :mod:`FrameWork` are that it has no abstract command
  27. interface (but that shouldn't be difficult), that its dialog support is minimal
  28. and that its control/toolbar support is non-existent.
  29. The :mod:`FrameWork` module defines the following functions:
  30. .. function:: Application()
  31. An object representing the complete application. See below for a description of
  32. the methods. The default :meth:`__init__` routine creates an empty window
  33. dictionary and a menu bar with an apple menu.
  34. .. function:: MenuBar()
  35. An object representing the menubar. This object is usually not created by the
  36. user.
  37. .. function:: Menu(bar, title[, after])
  38. An object representing a menu. Upon creation you pass the ``MenuBar`` the menu
  39. appears in, the *title* string and a position (1-based) *after* where the menu
  40. should appear (default: at the end).
  41. .. function:: MenuItem(menu, title[, shortcut, callback])
  42. Create a menu item object. The arguments are the menu to create, the item title
  43. string and optionally the keyboard shortcut and a callback routine. The callback
  44. is called with the arguments menu-id, item number within menu (1-based), current
  45. front window and the event record.
  46. Instead of a callable object the callback can also be a string. In this case
  47. menu selection causes the lookup of a method in the topmost window and the
  48. application. The method name is the callback string with ``'domenu_'``
  49. prepended.
  50. Calling the ``MenuBar`` :meth:`fixmenudimstate` method sets the correct dimming
  51. for all menu items based on the current front window.
  52. .. function:: Separator(menu)
  53. Add a separator to the end of a menu.
  54. .. function:: SubMenu(menu, label)
  55. Create a submenu named *label* under menu *menu*. The menu object is returned.
  56. .. function:: Window(parent)
  57. Creates a (modeless) window. *Parent* is the application object to which the
  58. window belongs. The window is not displayed until later.
  59. .. function:: DialogWindow(parent)
  60. Creates a modeless dialog window.
  61. .. function:: windowbounds(width, height)
  62. Return a ``(left, top, right, bottom)`` tuple suitable for creation of a window
  63. of given width and height. The window will be staggered with respect to previous
  64. windows, and an attempt is made to keep the whole window on-screen. However, the
  65. window will however always be the exact size given, so parts may be offscreen.
  66. .. function:: setwatchcursor()
  67. Set the mouse cursor to a watch.
  68. .. function:: setarrowcursor()
  69. Set the mouse cursor to an arrow.
  70. .. _application-objects:
  71. Application Objects
  72. -------------------
  73. Application objects have the following methods, among others:
  74. .. method:: Application.makeusermenus()
  75. Override this method if you need menus in your application. Append the menus to
  76. the attribute :attr:`menubar`.
  77. .. method:: Application.getabouttext()
  78. Override this method to return a text string describing your application.
  79. Alternatively, override the :meth:`do_about` method for more elaborate "about"
  80. messages.
  81. .. method:: Application.mainloop([mask[, wait]])
  82. This routine is the main event loop, call it to set your application rolling.
  83. *Mask* is the mask of events you want to handle, *wait* is the number of ticks
  84. you want to leave to other concurrent application (default 0, which is probably
  85. not a good idea). While raising *self* to exit the mainloop is still supported
  86. it is not recommended: call ``self._quit()`` instead.
  87. The event loop is split into many small parts, each of which can be overridden.
  88. The default methods take care of dispatching events to windows and dialogs,
  89. handling drags and resizes, Apple Events, events for non-FrameWork windows, etc.
  90. In general, all event handlers should return ``1`` if the event is fully handled
  91. and ``0`` otherwise (because the front window was not a FrameWork window, for
  92. instance). This is needed so that update events and such can be passed on to
  93. other windows like the Sioux console window. Calling :func:`MacOS.HandleEvent`
  94. is not allowed within *our_dispatch* or its callees, since this may result in an
  95. infinite loop if the code is called through the Python inner-loop event handler.
  96. .. method:: Application.asyncevents(onoff)
  97. Call this method with a nonzero parameter to enable asynchronous event handling.
  98. This will tell the inner interpreter loop to call the application event handler
  99. *async_dispatch* whenever events are available. This will cause FrameWork window
  100. updates and the user interface to remain working during long computations, but
  101. will slow the interpreter down and may cause surprising results in non-reentrant
  102. code (such as FrameWork itself). By default *async_dispatch* will immediately
  103. call *our_dispatch* but you may override this to handle only certain events
  104. asynchronously. Events you do not handle will be passed to Sioux and such.
  105. The old on/off value is returned.
  106. .. method:: Application._quit()
  107. Terminate the running :meth:`mainloop` call at the next convenient moment.
  108. .. method:: Application.do_char(c, event)
  109. The user typed character *c*. The complete details of the event can be found in
  110. the *event* structure. This method can also be provided in a ``Window`` object,
  111. which overrides the application-wide handler if the window is frontmost.
  112. .. method:: Application.do_dialogevent(event)
  113. Called early in the event loop to handle modeless dialog events. The default
  114. method simply dispatches the event to the relevant dialog (not through the
  115. ``DialogWindow`` object involved). Override if you need special handling of
  116. dialog events (keyboard shortcuts, etc).
  117. .. method:: Application.idle(event)
  118. Called by the main event loop when no events are available. The null-event is
  119. passed (so you can look at mouse position, etc).
  120. .. _window-objects:
  121. Window Objects
  122. --------------
  123. Window objects have the following methods, among others:
  124. .. method:: Window.open()
  125. Override this method to open a window. Store the Mac OS window-id in
  126. :attr:`self.wid` and call the :meth:`do_postopen` method to register the window
  127. with the parent application.
  128. .. method:: Window.close()
  129. Override this method to do any special processing on window close. Call the
  130. :meth:`do_postclose` method to cleanup the parent state.
  131. .. method:: Window.do_postresize(width, height, macoswindowid)
  132. Called after the window is resized. Override if more needs to be done than
  133. calling ``InvalRect``.
  134. .. method:: Window.do_contentclick(local, modifiers, event)
  135. The user clicked in the content part of a window. The arguments are the
  136. coordinates (window-relative), the key modifiers and the raw event.
  137. .. method:: Window.do_update(macoswindowid, event)
  138. An update event for the window was received. Redraw the window.
  139. .. method:: Window.do_activate(activate, event)
  140. The window was activated (``activate == 1``) or deactivated (``activate == 0``).
  141. Handle things like focus highlighting, etc.
  142. .. _controlswindow-object:
  143. ControlsWindow Object
  144. ---------------------
  145. ControlsWindow objects have the following methods besides those of ``Window``
  146. objects:
  147. .. method:: ControlsWindow.do_controlhit(window, control, pcode, event)
  148. Part *pcode* of control *control* was hit by the user. Tracking and such has
  149. already been taken care of.
  150. .. _scrolledwindow-object:
  151. ScrolledWindow Object
  152. ---------------------
  153. ScrolledWindow objects are ControlsWindow objects with the following extra
  154. methods:
  155. .. method:: ScrolledWindow.scrollbars([wantx[, wanty]])
  156. Create (or destroy) horizontal and vertical scrollbars. The arguments specify
  157. which you want (default: both). The scrollbars always have minimum ``0`` and
  158. maximum ``32767``.
  159. .. method:: ScrolledWindow.getscrollbarvalues()
  160. You must supply this method. It should return a tuple ``(x, y)`` giving the
  161. current position of the scrollbars (between ``0`` and ``32767``). You can return
  162. ``None`` for either to indicate the whole document is visible in that direction.
  163. .. method:: ScrolledWindow.updatescrollbars()
  164. Call this method when the document has changed. It will call
  165. :meth:`getscrollbarvalues` and update the scrollbars.
  166. .. method:: ScrolledWindow.scrollbar_callback(which, what, value)
  167. Supplied by you and called after user interaction. *which* will be ``'x'`` or
  168. ``'y'``, *what* will be ``'-'``, ``'--'``, ``'set'``, ``'++'`` or ``'+'``. For
  169. ``'set'``, *value* will contain the new scrollbar position.
  170. .. method:: ScrolledWindow.scalebarvalues(absmin, absmax, curmin, curmax)
  171. Auxiliary method to help you calculate values to return from
  172. :meth:`getscrollbarvalues`. You pass document minimum and maximum value and
  173. topmost (leftmost) and bottommost (rightmost) visible values and it returns the
  174. correct number or ``None``.
  175. .. method:: ScrolledWindow.do_activate(onoff, event)
  176. Takes care of dimming/highlighting scrollbars when a window becomes frontmost.
  177. If you override this method, call this one at the end of your method.
  178. .. method:: ScrolledWindow.do_postresize(width, height, window)
  179. Moves scrollbars to the correct position. Call this method initially if you
  180. override it.
  181. .. method:: ScrolledWindow.do_controlhit(window, control, pcode, event)
  182. Handles scrollbar interaction. If you override it call this method first, a
  183. nonzero return value indicates the hit was in the scrollbars and has been
  184. handled.
  185. .. _dialogwindow-objects:
  186. DialogWindow Objects
  187. --------------------
  188. DialogWindow objects have the following methods besides those of ``Window``
  189. objects:
  190. .. method:: DialogWindow.open(resid)
  191. Create the dialog window, from the DLOG resource with id *resid*. The dialog
  192. object is stored in :attr:`self.wid`.
  193. .. method:: DialogWindow.do_itemhit(item, event)
  194. Item number *item* was hit. You are responsible for redrawing toggle buttons,
  195. etc.