home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / r / risclib < prev    next >
Text File  |  1996-11-14  |  6KB  |  122 lines

  1. <TITLE>RiscLib -- Python library reference</TITLE>
  2. Prev: <A HREF="../d/drawf" TYPE="Prev">drawf</A>  
  3. Up: <A HREF="../r/riscos_only" TYPE="Up">RISCOS ONLY</A>  
  4. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  5. <H1>13.5. Built-in Module <CODE>RiscLib</CODE></H1>
  6. @modindex{risclib}experimental
  7. <P>
  8. These modules provide an experimental interface to the RiscOS toolbox.
  9. <P>
  10. They provide a class <CODE>TBObject</CODE> that implements the common toolbox
  11. methods, and derived classes <CODE>ColourDbox</CODE>, <CODE>IconBar</CODE>,
  12. <CODE>Menu</CODE>, <CODE>Quit</CODE>, <CODE>SaveAs</CODE>, <CODE>Window</CODE>. They provide a
  13. simple event polling system.
  14. <P>
  15. Neither the modules or the documentation are complete. I have added methods
  16. and objects when I needed to use them. The methods provided just call the
  17. toolbox SWIs, and can be inspected by examining the code.
  18. <P>
  19. The event polling system works as follows (using !Ibrowse as an example) :
  20. <P>
  21. The controlling program imports <CODE>tbevent</CODE>.
  22. <CODE>tbevent</CODE> imports three dictionaries from a module <CODE>events</CODE> that
  23. include details of the toolbox events, wimp events and wimp messages that the
  24. application is interested in. The dictionaries are called <CODE>edict</CODE>, 
  25. <CODE>wdict</CODE> and <CODE>mdict</CODE>. They contain event or message numbers as
  26. keys and names as values. See @verb{!ibrowse.py.events} for an example.
  27. <P>
  28. The program must supply classes with methods to handle these events. The
  29. methods will have the same names as the dictionary values. 
  30. <P>
  31. The program calls
  32. @verb{tbevent.initialise("<ibrowse$Dir>")} supplying an application
  33. directory incuding a Resource file ``res'' and a ``Messages'' file.
  34. <P>
  35. The program registers instances of the handler classes with tbevent (see below).
  36. <P>
  37. The program then calls <CODE>tbevent.poll()</CODE> repeatedly.
  38. <P>
  39. Toolbox event handling works as follows:
  40. <P>
  41. The ancestor object number of the event is used to find a class instance to
  42. handle the event. It is looked up in a dictionary <CODE>tbevent.tbclasses</CODE> to
  43. find the handler, so each class that handles toolbox methods should have a
  44. <CODE>__init__</CODE> method that registers itself in this dictionary.
  45. <P>
  46. Here is an example from ``!Graph''
  47. <P>
  48. <UL COMPACT><CODE>class Display(window.Window):<P>
  49.       actions={}                                 #1<P>
  50.       def __init__(self):<P>
  51.           window.Window.__init__(self,"Display") #2<P>
  52.           tbevent.tbclasses[self.id]=self        #3<P>
  53.           self.handle=self.getwimphandle()       #4<P>
  54.           tbevent.winclasses[self.handle]=self   #5<P>
  55. </CODE></UL>
  56. In line @verb{#2} the underlying window class is initialised. This creates
  57. a toolbox object from a template named ''Display''
  58. (This must be a window object. It should be an ancestor object, but not
  59. autocreated).
  60. <CODE>self.id</CODE> is set to the object identifier.
  61. Line @verb{#3} now registers the class instance as a toolbox event handler.
  62. <P>
  63. If an event now arrives for an object with this ancestor its event number is
  64. looked up in <CODE>edict</CODE>. The corresponding name should be the name of a
  65. method in class <CODE>Display</CODE>. It will be called for this class instance.
  66. <P>
  67. The class method names are cached in a dictionary called <CODE>actions</CODE>, so
  68. each handler class should contain an empty dictionary as in line @verb{#1}.
  69. <P>
  70. Some toolbox events will have no ancestor. These will be sent to a class
  71. instance registered with a zero identifier. Most programs will have code
  72. such as this, which will handle an autocreated bar icon object.
  73. <P>
  74. <UL COMPACT><CODE>class Orphans: #class to handle events with zero ancestor<P>
  75.       actions={}<P>
  76.       def __init__(self):<P>
  77.           tbevent.tbclasses[0]=self<P>
  78.       def BarClick(self):<P>
  79.           a=Display("dir","Top")<P>
  80.       def E_Q_Quit(self):<P>
  81.           exit()<P>
  82. <P>
  83. oi=Orphans()  #create an instance of the class (the only one!)<P>
  84. <P>
  85. </CODE></UL>
  86. Wimp event handling is similar. Wimp codes other than 7, 9, 13, 17 and 18
  87. are sent to class instances registered by their wimp window handles.
  88. Codes 7, 9 and 13 are given handles of zero. The classes are registered in
  89. the dictionary <CODE>tbevent.winclasses</CODE>. Lines @verb{#4} and @verb{#5} in
  90. the example above give an example of registration. The names of the actions
  91. are obtained from dictionary <CODE>wdict</CODE>. Codes not in this dictionary are
  92. ignored. Note that wimp and toolbox codes use the same <CODE>actions</CODE>
  93. dictionary. This means they must have <I>different event numbers</I>. In
  94. practice this means not giving low numbers to user defined toolbox events.
  95. Starting user defined nubers at <CODE>0x1000</CODE> is a safe rule.
  96. <P>
  97. Messages are handled by a single class instance <CODE>tbevent.msghandler</CODE>
  98. method names are looked up from the message number in dictionary <CODE>mdict</CODE>.
  99. <P>
  100. For example here is a trivial message handling class that just handles the
  101. quit message.
  102. <P>
  103. <UL COMPACT><CODE>class Messenger:                #This is the message handler class<P>
  104.       def Wimp_MQuit(self):<P>
  105.           exit()<P>
  106. <P>
  107. tbevent.msghandler=Messenger()  #Register the instance <P>
  108. </CODE></UL>
  109. Toolbox event methods can examine the toolbox id block as <VAR>toolbox.idblock</VAR>.
  110. All methods can use the wimp poll block as <VAR>tbevent.wblock</VAR>.
  111. <P>
  112. There is a simple system to assist with debugging built in to <CODE>tbevent</CODE>.
  113. The function <CODE>tbevent.report(string,[priority])</CODE> broadcasts a wimp
  114. message containing the string (truncated to 200 characters). The application
  115. ``!Reporter'' can display these messages. Messages are only sent if
  116. <CODE>priority<=tbevent.usereport</CODE>, so debugging messages can be suppressed
  117. by setting this variable. <CODE>priority</CODE> defaults to 16.
  118. <CODE>tbevent.usereport</CODE> is initially 8. <CODE>tbevent</CODE> generates some
  119. reports itself, error reports with priority 2, and reports of all events
  120. with priority 32.
  121. <P>
  122.