blob: edc76c107b439de8571223b33a5adb73f3849902 [file] [log] [blame]
Fred Drakec817e271998-08-10 18:40:22 +00001\section{\module{FrameWork} ---
Fred Drakef6863c11999-03-02 16:37:17 +00002 Interactive application framework}
Fred Drakefe7f3bc1998-07-23 17:55:31 +00003
Fred Drakef6863c11999-03-02 16:37:17 +00004\declaremodule{standard}{FrameWork}
5 \platform{Mac}
Fred Drakefe7f3bc1998-07-23 17:55:31 +00006\modulesynopsis{Interactive application framework.}
7
Fred Drakeb4efb6c1998-02-18 17:19:11 +00008
Fred Drake41788db1998-04-04 06:23:02 +00009The \module{FrameWork} module contains classes that together provide a
Fred Drakeb4efb6c1998-02-18 17:19:11 +000010framework for an interactive Macintosh application. The programmer
11builds an application by creating subclasses that override various
12methods of the bases classes, thereby implementing the functionality
13wanted. Overriding functionality can often be done on various
14different levels, i.e. to handle clicks in a single dialog window in a
15non-standard way it is not necessary to override the complete event
16handling.
17
Brett Cannon7706c2d2005-02-13 22:50:04 +000018Work on the \module{FrameWork} has pretty much stopped, now that
19\module{PyObjC} is available for full Cocoa access from Python, and the
Fred Drakeb4efb6c1998-02-18 17:19:11 +000020documentation describes only the most important functionality, and not
21in the most logical manner at that. Examine the source or the examples
Fred Drake658865c2000-10-14 04:53:31 +000022for more details. The following are some comments posted on the
23MacPython newsgroup about the strengths and limitations of
24\module{FrameWork}:
25
26\begin{quotation}
27The strong point of \module{FrameWork} is that it allows you to break
28into the control-flow at many different places. \refmodule{W}, for
29instance, uses a different way to enable/disable menus and that plugs
30right in leaving the rest intact. The weak points of
31\module{FrameWork} are that it has no abstract command interface (but
Neil Schemenauerc4932292005-06-18 17:54:13 +000032that shouldn't be difficult), that its dialog support is minimal and
33that its control/toolbar support is non-existent.
Fred Drake658865c2000-10-14 04:53:31 +000034\end{quotation}
35
Fred Drakeb4efb6c1998-02-18 17:19:11 +000036
Fred Drake41788db1998-04-04 06:23:02 +000037The \module{FrameWork} module defines the following functions:
Fred Drakeb4efb6c1998-02-18 17:19:11 +000038
Fred Drakeb4efb6c1998-02-18 17:19:11 +000039
40\begin{funcdesc}{Application}{}
41An object representing the complete application. See below for a
Fred Drake41788db1998-04-04 06:23:02 +000042description of the methods. The default \method{__init__()} routine
Fred Drakeb4efb6c1998-02-18 17:19:11 +000043creates an empty window dictionary and a menu bar with an apple menu.
44\end{funcdesc}
45
46\begin{funcdesc}{MenuBar}{}
47An object representing the menubar. This object is usually not created
48by the user.
49\end{funcdesc}
50
Fred Drakecce10901998-03-17 06:33:25 +000051\begin{funcdesc}{Menu}{bar, title\optional{, after}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +000052An object representing a menu. Upon creation you pass the
53\code{MenuBar} the menu appears in, the \var{title} string and a
54position (1-based) \var{after} where the menu should appear (default:
55at the end).
56\end{funcdesc}
57
Fred Drakecce10901998-03-17 06:33:25 +000058\begin{funcdesc}{MenuItem}{menu, title\optional{, shortcut, callback}}
Fred Drake658865c2000-10-14 04:53:31 +000059Create a menu item object. The arguments are the menu to create, the
Raymond Hettinger92016dc2003-09-22 15:27:11 +000060item title string and optionally the keyboard shortcut
Fred Drakeb4efb6c1998-02-18 17:19:11 +000061and a callback routine. The callback is called with the arguments
62menu-id, item number within menu (1-based), current front window and
63the event record.
64
Fred Drake658865c2000-10-14 04:53:31 +000065Instead of a callable object the callback can also be a string. In
Fred Drakeb4efb6c1998-02-18 17:19:11 +000066this case menu selection causes the lookup of a method in the topmost
67window and the application. The method name is the callback string
68with \code{'domenu_'} prepended.
69
Fred Drakef6863c11999-03-02 16:37:17 +000070Calling the \code{MenuBar} \method{fixmenudimstate()} method sets the
Fred Drakeb4efb6c1998-02-18 17:19:11 +000071correct dimming for all menu items based on the current front window.
72\end{funcdesc}
73
74\begin{funcdesc}{Separator}{menu}
75Add a separator to the end of a menu.
76\end{funcdesc}
77
Fred Drakecce10901998-03-17 06:33:25 +000078\begin{funcdesc}{SubMenu}{menu, label}
Fred Drakeb4efb6c1998-02-18 17:19:11 +000079Create a submenu named \var{label} under menu \var{menu}. The menu
80object is returned.
81\end{funcdesc}
82
83\begin{funcdesc}{Window}{parent}
84Creates a (modeless) window. \var{Parent} is the application object to
85which the window belongs. The window is not displayed until later.
86\end{funcdesc}
87
88\begin{funcdesc}{DialogWindow}{parent}
89Creates a modeless dialog window.
90\end{funcdesc}
91
Fred Drakecce10901998-03-17 06:33:25 +000092\begin{funcdesc}{windowbounds}{width, height}
Fred Drakef6863c11999-03-02 16:37:17 +000093Return a \code{(\var{left}, \var{top}, \var{right}, \var{bottom})}
94tuple suitable for creation of a window of given width and height. The
95window will be staggered with respect to previous windows, and an
Fred Drake658865c2000-10-14 04:53:31 +000096attempt is made to keep the whole window on-screen. However, the window will
97however always be the exact size given, so parts may be offscreen.
Fred Drakeb4efb6c1998-02-18 17:19:11 +000098\end{funcdesc}
99
100\begin{funcdesc}{setwatchcursor}{}
101Set the mouse cursor to a watch.
102\end{funcdesc}
103
104\begin{funcdesc}{setarrowcursor}{}
105Set the mouse cursor to an arrow.
106\end{funcdesc}
107
Fred Drakef6863c11999-03-02 16:37:17 +0000108
109\subsection{Application Objects \label{application-objects}}
Fred Drake41788db1998-04-04 06:23:02 +0000110
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000111Application objects have the following methods, among others:
112
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000113
Fred Drakef6863c11999-03-02 16:37:17 +0000114\begin{methoddesc}[Application]{makeusermenus}{}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000115Override this method if you need menus in your application. Append the
Fred Drake41788db1998-04-04 06:23:02 +0000116menus to the attribute \member{menubar}.
Fred Drakef6863c11999-03-02 16:37:17 +0000117\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000118
Fred Drakef6863c11999-03-02 16:37:17 +0000119\begin{methoddesc}[Application]{getabouttext}{}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000120Override this method to return a text string describing your
Fred Drake41788db1998-04-04 06:23:02 +0000121application. Alternatively, override the \method{do_about()} method
122for more elaborate ``about'' messages.
Fred Drakef6863c11999-03-02 16:37:17 +0000123\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000124
Fred Drakef6863c11999-03-02 16:37:17 +0000125\begin{methoddesc}[Application]{mainloop}{\optional{mask\optional{, wait}}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000126This routine is the main event loop, call it to set your application
127rolling. \var{Mask} is the mask of events you want to handle,
128\var{wait} is the number of ticks you want to leave to other
129concurrent application (default 0, which is probably not a good
Fred Drakef6863c11999-03-02 16:37:17 +0000130idea). While raising \var{self} to exit the mainloop is still
131supported it is not recommended: call \code{self._quit()} instead.
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000132
133The event loop is split into many small parts, each of which can be
134overridden. The default methods take care of dispatching events to
135windows and dialogs, handling drags and resizes, Apple Events, events
136for non-FrameWork windows, etc.
137
Fred Drake41788db1998-04-04 06:23:02 +0000138In general, all event handlers should return \code{1} if the event is fully
139handled and \code{0} otherwise (because the front window was not a FrameWork
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000140window, for instance). This is needed so that update events and such
141can be passed on to other windows like the Sioux console window.
Fred Drake41788db1998-04-04 06:23:02 +0000142Calling \function{MacOS.HandleEvent()} is not allowed within
143\var{our_dispatch} or its callees, since this may result in an
144infinite loop if the code is called through the Python inner-loop
145event handler.
Fred Drakef6863c11999-03-02 16:37:17 +0000146\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000147
Fred Drakef6863c11999-03-02 16:37:17 +0000148\begin{methoddesc}[Application]{asyncevents}{onoff}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000149Call this method with a nonzero parameter to enable
150asynchronous event handling. This will tell the inner interpreter loop
151to call the application event handler \var{async_dispatch} whenever events
152are available. This will cause FrameWork window updates and the user
153interface to remain working during long computations, but will slow the
154interpreter down and may cause surprising results in non-reentrant code
Raymond Hettinger68804312005-01-01 00:28:46 +0000155(such as FrameWork itself). By default \var{async_dispatch} will immediately
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000156call \var{our_dispatch} but you may override this to handle only certain
157events asynchronously. Events you do not handle will be passed to Sioux
158and such.
159
160The old on/off value is returned.
Fred Drakef6863c11999-03-02 16:37:17 +0000161\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000162
Fred Drakef6863c11999-03-02 16:37:17 +0000163\begin{methoddesc}[Application]{_quit}{}
Fred Drake41788db1998-04-04 06:23:02 +0000164Terminate the running \method{mainloop()} call at the next convenient
165moment.
Fred Drakef6863c11999-03-02 16:37:17 +0000166\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000167
Fred Drakef6863c11999-03-02 16:37:17 +0000168\begin{methoddesc}[Application]{do_char}{c, event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000169The user typed character \var{c}. The complete details of the event
170can be found in the \var{event} structure. This method can also be
171provided in a \code{Window} object, which overrides the
172application-wide handler if the window is frontmost.
Fred Drakef6863c11999-03-02 16:37:17 +0000173\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000174
Fred Drakef6863c11999-03-02 16:37:17 +0000175\begin{methoddesc}[Application]{do_dialogevent}{event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000176Called early in the event loop to handle modeless dialog events. The
177default method simply dispatches the event to the relevant dialog (not
Raymond Hettingerc7a26562003-08-12 00:01:17 +0000178through the \code{DialogWindow} object involved). Override if you
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000179need special handling of dialog events (keyboard shortcuts, etc).
Fred Drakef6863c11999-03-02 16:37:17 +0000180\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000181
Fred Drakef6863c11999-03-02 16:37:17 +0000182\begin{methoddesc}[Application]{idle}{event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000183Called by the main event loop when no events are available. The
184null-event is passed (so you can look at mouse position, etc).
Fred Drakef6863c11999-03-02 16:37:17 +0000185\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000186
Fred Drakef6863c11999-03-02 16:37:17 +0000187
188\subsection{Window Objects \label{window-objects}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000189
190Window objects have the following methods, among others:
191
Fred Drakef6863c11999-03-02 16:37:17 +0000192\begin{methoddesc}[Window]{open}{}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000193Override this method to open a window. Store the MacOS window-id in
Fred Drakef6863c11999-03-02 16:37:17 +0000194\member{self.wid} and call the \method{do_postopen()} method to
195register the window with the parent application.
196\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000197
Fred Drakef6863c11999-03-02 16:37:17 +0000198\begin{methoddesc}[Window]{close}{}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000199Override this method to do any special processing on window
Fred Drakef6863c11999-03-02 16:37:17 +0000200close. Call the \method{do_postclose()} method to cleanup the parent
201state.
202\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000203
Fred Drakef6863c11999-03-02 16:37:17 +0000204\begin{methoddesc}[Window]{do_postresize}{width, height, macoswindowid}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000205Called after the window is resized. Override if more needs to be done
206than calling \code{InvalRect}.
Fred Drakef6863c11999-03-02 16:37:17 +0000207\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000208
Fred Drakef6863c11999-03-02 16:37:17 +0000209\begin{methoddesc}[Window]{do_contentclick}{local, modifiers, event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000210The user clicked in the content part of a window. The arguments are
211the coordinates (window-relative), the key modifiers and the raw
212event.
Fred Drakef6863c11999-03-02 16:37:17 +0000213\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000214
Fred Drakef6863c11999-03-02 16:37:17 +0000215\begin{methoddesc}[Window]{do_update}{macoswindowid, event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000216An update event for the window was received. Redraw the window.
Fred Drakef6863c11999-03-02 16:37:17 +0000217\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000218
Guido van Rossumd8faa362007-04-27 19:54:29 +0000219\begin{methoddesc}[Window]{do_activate}{activate, event}
Fred Drakef6863c11999-03-02 16:37:17 +0000220The window was activated (\code{\var{activate} == 1}) or deactivated
221(\code{\var{activate} == 0}). Handle things like focus highlighting,
222etc.
223\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000224
Fred Drakef6863c11999-03-02 16:37:17 +0000225
226\subsection{ControlsWindow Object \label{controlswindow-object}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000227
228ControlsWindow objects have the following methods besides those of
229\code{Window} objects:
230
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000231
Fred Drakef6863c11999-03-02 16:37:17 +0000232\begin{methoddesc}[ControlsWindow]{do_controlhit}{window, control,
233 pcode, event}
234Part \var{pcode} of control \var{control} was hit by the
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000235user. Tracking and such has already been taken care of.
Fred Drakef6863c11999-03-02 16:37:17 +0000236\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000237
Fred Drakef6863c11999-03-02 16:37:17 +0000238
239\subsection{ScrolledWindow Object \label{scrolledwindow-object}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000240
241ScrolledWindow objects are ControlsWindow objects with the following
242extra methods:
243
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000244
Fred Drakef6863c11999-03-02 16:37:17 +0000245\begin{methoddesc}[ScrolledWindow]{scrollbars}{\optional{wantx\optional{,
246 wanty}}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000247Create (or destroy) horizontal and vertical scrollbars. The arguments
248specify which you want (default: both). The scrollbars always have
249minimum \code{0} and maximum \code{32767}.
Fred Drakef6863c11999-03-02 16:37:17 +0000250\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000251
Fred Drakef6863c11999-03-02 16:37:17 +0000252\begin{methoddesc}[ScrolledWindow]{getscrollbarvalues}{}
Fred Drake41788db1998-04-04 06:23:02 +0000253You must supply this method. It should return a tuple \code{(\var{x},
254\var{y})} giving the current position of the scrollbars (between
255\code{0} and \code{32767}). You can return \code{None} for either to
256indicate the whole document is visible in that direction.
Fred Drakef6863c11999-03-02 16:37:17 +0000257\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000258
Fred Drakef6863c11999-03-02 16:37:17 +0000259\begin{methoddesc}[ScrolledWindow]{updatescrollbars}{}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000260Call this method when the document has changed. It will call
Fred Drake41788db1998-04-04 06:23:02 +0000261\method{getscrollbarvalues()} and update the scrollbars.
Fred Drakef6863c11999-03-02 16:37:17 +0000262\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000263
Fred Drakef6863c11999-03-02 16:37:17 +0000264\begin{methoddesc}[ScrolledWindow]{scrollbar_callback}{which, what, value}
Fred Drake41788db1998-04-04 06:23:02 +0000265Supplied by you and called after user interaction. \var{which} will
266be \code{'x'} or \code{'y'}, \var{what} will be \code{'-'},
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000267\code{'--'}, \code{'set'}, \code{'++'} or \code{'+'}. For
Fred Drake41788db1998-04-04 06:23:02 +0000268\code{'set'}, \var{value} will contain the new scrollbar position.
Fred Drakef6863c11999-03-02 16:37:17 +0000269\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000270
Fred Drakef6863c11999-03-02 16:37:17 +0000271\begin{methoddesc}[ScrolledWindow]{scalebarvalues}{absmin, absmax,
272 curmin, curmax}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000273Auxiliary method to help you calculate values to return from
Fred Drake41788db1998-04-04 06:23:02 +0000274\method{getscrollbarvalues()}. You pass document minimum and maximum value
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000275and topmost (leftmost) and bottommost (rightmost) visible values and
276it returns the correct number or \code{None}.
Fred Drakef6863c11999-03-02 16:37:17 +0000277\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000278
Fred Drakef6863c11999-03-02 16:37:17 +0000279\begin{methoddesc}[ScrolledWindow]{do_activate}{onoff, event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000280Takes care of dimming/highlighting scrollbars when a window becomes
Fred Drake658865c2000-10-14 04:53:31 +0000281frontmost. If you override this method, call this one at the end of
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000282your method.
Fred Drakef6863c11999-03-02 16:37:17 +0000283\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000284
Fred Drakef6863c11999-03-02 16:37:17 +0000285\begin{methoddesc}[ScrolledWindow]{do_postresize}{width, height, window}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000286Moves scrollbars to the correct position. Call this method initially
287if you override it.
Fred Drakef6863c11999-03-02 16:37:17 +0000288\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000289
Fred Drakef6863c11999-03-02 16:37:17 +0000290\begin{methoddesc}[ScrolledWindow]{do_controlhit}{window, control,
291 pcode, event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000292Handles scrollbar interaction. If you override it call this method
293first, a nonzero return value indicates the hit was in the scrollbars
294and has been handled.
Fred Drakef6863c11999-03-02 16:37:17 +0000295\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000296
Fred Drakef6863c11999-03-02 16:37:17 +0000297
298\subsection{DialogWindow Objects \label{dialogwindow-objects}}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000299
300DialogWindow objects have the following methods besides those of
301\code{Window} objects:
302
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000303
Fred Drakef6863c11999-03-02 16:37:17 +0000304\begin{methoddesc}[DialogWindow]{open}{resid}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000305Create the dialog window, from the DLOG resource with id
Fred Drakef6863c11999-03-02 16:37:17 +0000306\var{resid}. The dialog object is stored in \member{self.wid}.
307\end{methoddesc}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000308
Fred Drakef6863c11999-03-02 16:37:17 +0000309\begin{methoddesc}[DialogWindow]{do_itemhit}{item, event}
Fred Drakeb4efb6c1998-02-18 17:19:11 +0000310Item number \var{item} was hit. You are responsible for redrawing
311toggle buttons, etc.
Fred Drakef6863c11999-03-02 16:37:17 +0000312\end{methoddesc}