FrameWork — Interactive application framework¶
Platforms:Mac
TheFrameWork module contains classes that together provide a frameworkfor an interactive Macintosh application. The programmer builds an applicationby creating subclasses that override various methods of the bases classes,thereby implementing the functionality wanted. Overriding functionality canoften be done on various different levels, i.e. to handle clicks in a singledialog window in a non-standard way it is not necessary to override the completeevent handling.
Warning
This module is removed in 3.0.
Work on theFrameWork has pretty much stopped, now thatPyObjC isavailable for full Cocoa access from Python, and the documentation describesonly the most important functionality, and not in the most logical manner atthat. Examine the source or the examples for more details. The following aresome comments posted on the MacPython newsgroup about the strengths andlimitations ofFrameWork:
The strong point ofFrameWork is that it allows you to break into thecontrol-flow at many different places.W, for instance, uses a differentway to enable/disable menus and that plugs right in leaving the rest intact.The weak points ofFrameWork are that it has no abstract commandinterface (but that shouldn’t be difficult), that its dialog support is minimaland that its control/toolbar support is non-existent.
TheFrameWork module defines the following functions:
- FrameWork.Application()¶
- An object representing the complete application. See below for a description ofthe methods. The default__init__() routine creates an empty windowdictionary and a menu bar with an apple menu.
- FrameWork.MenuBar()¶
- An object representing the menubar. This object is usually not created by theuser.
- FrameWork.Menu(bar,title[,after])¶
- An object representing a menu. Upon creation you pass theMenuBar the menuappears in, thetitle string and a position (1-based)after where the menushould appear (default: at the end).
- FrameWork.MenuItem(menu,title[,shortcut,callback])¶
Create a menu item object. The arguments are the menu to create, the item titlestring and optionally the keyboard shortcut and a callback routine. The callbackis called with the arguments menu-id, item number within menu (1-based), currentfront window and the event record.
Instead of a callable object the callback can also be a string. In this casemenu selection causes the lookup of a method in the topmost window and theapplication. The method name is the callback string with'domenu_'prepended.
Calling theMenuBarfixmenudimstate() method sets the correct dimmingfor all menu items based on the current front window.
- FrameWork.Separator(menu)¶
- Add a separator to the end of a menu.
- FrameWork.SubMenu(menu,label)¶
- Create a submenu namedlabel under menumenu. The menu object is returned.
- FrameWork.Window(parent)¶
- Creates a (modeless) window.Parent is the application object to which thewindow belongs. The window is not displayed until later.
- FrameWork.DialogWindow(parent)¶
- Creates a modeless dialog window.
- FrameWork.windowbounds(width,height)¶
- Return a(left,top,right,bottom) tuple suitable for creation of a windowof given width and height. The window will be staggered with respect to previouswindows, and an attempt is made to keep the whole window on-screen. However, thewindow will however always be the exact size given, so parts may be offscreen.
- FrameWork.setwatchcursor()¶
- Set the mouse cursor to a watch.
- FrameWork.setarrowcursor()¶
- Set the mouse cursor to an arrow.
Application Objects¶
Application objects have the following methods, among others:
- Application.makeusermenus()¶
- Override this method if you need menus in your application. Append the menus tothe attributemenubar.
- Application.getabouttext()¶
- Override this method to return a text string describing your application.Alternatively, override thedo_about() method for more elaborate “about”messages.
- Application.mainloop([mask[,wait]])¶
This routine is the main event loop, call it to set your application rolling.Mask is the mask of events you want to handle,wait is the number of ticksyou want to leave to other concurrent application (default 0, which is probablynot a good idea). While raisingself to exit the mainloop is still supportedit is not recommended: callself._quit() instead.
The event loop is split into many small parts, each of which can be overridden.The default methods take care of dispatching events to windows and dialogs,handling drags and resizes, Apple Events, events for non-FrameWork windows, etc.
In general, all event handlers should return1 if the event is fully handledand0 otherwise (because the front window was not a FrameWork window, forinstance). This is needed so that update events and such can be passed on toother windows like the Sioux console window. CallingMacOS.HandleEvent()is not allowed withinour_dispatch or its callees, since this may result in aninfinite loop if the code is called through the Python inner-loop event handler.
- Application.asyncevents(onoff)¶
Call this method with a nonzero parameter to enable asynchronous event handling.This will tell the inner interpreter loop to call the application event handlerasync_dispatch whenever events are available. This will cause FrameWork windowupdates and the user interface to remain working during long computations, butwill slow the interpreter down and may cause surprising results in non-reentrantcode (such as FrameWork itself). By defaultasync_dispatch will immediatelycallour_dispatch but you may override this to handle only certain eventsasynchronously. Events you do not handle will be passed to Sioux and such.
The old on/off value is returned.
- Application._quit()¶
- Terminate the runningmainloop() call at the next convenient moment.
- Application.do_char(c,event)¶
- The user typed characterc. The complete details of the event can be found intheevent structure. This method can also be provided in aWindow object,which overrides the application-wide handler if the window is frontmost.
- Application.do_dialogevent(event)¶
- Called early in the event loop to handle modeless dialog events. The defaultmethod simply dispatches the event to the relevant dialog (not through theDialogWindow object involved). Override if you need special handling ofdialog events (keyboard shortcuts, etc).
- Application.idle(event)¶
- Called by the main event loop when no events are available. The null-event ispassed (so you can look at mouse position, etc).
Window Objects¶
Window objects have the following methods, among others:
- Window.open()¶
- Override this method to open a window. Store the Mac OS window-id inself.wid and call thedo_postopen() method to register the windowwith the parent application.
- Window.close()¶
- Override this method to do any special processing on window close. Call thedo_postclose() method to cleanup the parent state.
- Window.do_postresize(width,height,macoswindowid)¶
- Called after the window is resized. Override if more needs to be done thancallingInvalRect.
- Window.do_contentclick(local,modifiers,event)¶
- The user clicked in the content part of a window. The arguments are thecoordinates (window-relative), the key modifiers and the raw event.
- Window.do_update(macoswindowid,event)¶
- An update event for the window was received. Redraw the window.
- Window.do_activate(activate,event)¶
- The window was activated (activate==1) or deactivated (activate==0).Handle things like focus highlighting, etc.
ControlsWindow Object¶
ControlsWindow objects have the following methods besides those ofWindowobjects:
- ControlsWindow.do_controlhit(window,control,pcode,event)¶
- Partpcode of controlcontrol was hit by the user. Tracking and such hasalready been taken care of.
ScrolledWindow Object¶
ScrolledWindow objects are ControlsWindow objects with the following extramethods:
- ScrolledWindow.scrollbars([wantx[,wanty]])¶
- Create (or destroy) horizontal and vertical scrollbars. The arguments specifywhich you want (default: both). The scrollbars always have minimum0 andmaximum32767.
- ScrolledWindow.getscrollbarvalues()¶
- You must supply this method. It should return a tuple(x,y) giving thecurrent position of the scrollbars (between0 and32767). You can returnNone for either to indicate the whole document is visible in that direction.
- ScrolledWindow.updatescrollbars()¶
- Call this method when the document has changed. It will callgetscrollbarvalues() and update the scrollbars.
- ScrolledWindow.scrollbar_callback(which,what,value)¶
- Supplied by you and called after user interaction.which will be'x' or'y',what will be'-','--','set','++' or'+'. For'set',value will contain the new scrollbar position.
- ScrolledWindow.scalebarvalues(absmin,absmax,curmin,curmax)¶
- Auxiliary method to help you calculate values to return fromgetscrollbarvalues(). You pass document minimum and maximum value andtopmost (leftmost) and bottommost (rightmost) visible values and it returns thecorrect number orNone.
- ScrolledWindow.do_activate(onoff,event)¶
- Takes care of dimming/highlighting scrollbars when a window becomes frontmost.If you override this method, call this one at the end of your method.
- ScrolledWindow.do_postresize(width,height,window)¶
- Moves scrollbars to the correct position. Call this method initially if youoverride it.
- ScrolledWindow.do_controlhit(window,control,pcode,event)¶
- Handles scrollbar interaction. If you override it call this method first, anonzero return value indicates the hit was in the scrollbars and has beenhandled.
DialogWindow Objects¶
DialogWindow objects have the following methods besides those ofWindowobjects:
- DialogWindow.open(resid)¶
- Create the dialog window, from the DLOG resource with idresid. The dialogobject is stored inself.wid.
- DialogWindow.do_itemhit(item,event)¶
- Item numberitem was hit. You are responsible for redrawing toggle buttons,etc.