home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / vista_1 / !Manual_manual_objects < prev    next >
Encoding:
Text File  |  1996-01-27  |  18.6 KB  |  554 lines

  1. <html>
  2. <title>Objects</title>
  3.  
  4. <h1>Objects</h1>
  5.  
  6. A Window in Vista can contain a number of 'Objects'.  These are things which reside
  7. inside the window are need to be redrawn.  Icons are effectively objects which are
  8. handled by the WIMP.  An object is created inside a window and every time the
  9. window needs redrawn (for a resize or scroll) the objects are asked to redraw
  10. themselves by the Window.  They are passed the <b>WORK AREA</b> coordinates within the
  11. window which needs updated.  They can be used to perform clipping of the
  12. redraw in order to make it more efficient.
  13. <p>
  14. Objects in a window has a priority assicoated with them.  This is to allow
  15. objects to overlap.  A higher priority object will receive events
  16. such as mouse clicks and key presses before lower priority ones.  
  17. <p>
  18. <h2>Synopsis</h2>
  19.  
  20. <pre>
  21.  
  22. class Object
  23.    {
  24.    public:
  25.       <a href="file:objects#ct">Object</a> (Window *w, char *name, int priority = 0, char *menu = 0) ;
  26.       virtual <a href="file:objects#dt">~Object</a>() ;
  27.       virtual void <a href="file:objects#redraw">redraw</a> (int x0, int y0, int x1, int y1) ;
  28.       virtual void <a href="file:objects#update">update</a> (int x0, int y0, int x1, int y1) ;
  29.       virtual int <a href="file:objects#compare">compare</a> (int x, int y) ;
  30.       virtual void <a href="file:objects#move1">move</a> (int x0, int y0, int x1, int y1) ;
  31.       virtual void <a href="file:objects#move2">move</a> (int dx, int dy) ;
  32.       virtual void <a href="file:objects#drag">drag</a> (int mx, int my, int buttons) ;
  33.       virtual void <a href="file:objects#end_drag">end_drag</a> (int x0, int y0, int x1, int y1, int id) ;
  34.       virtual void <a href="file:objects#click">click</a>(int mx, int my, int button) ;
  35.       virtual void <a href="file:objects#key">key</a> (int x, int y, int height, int index, int code) ;
  36.       virtual void <a href="file:objects#double_click">double_click</a> (int mx, int my, int buttons) ;
  37.       virtual void <a href="file:objects#select">select</a>() ;
  38.       virtual void <a href="file:objects#unselect">unselect</a>() ;
  39.       virtual int <a href="file:objects#width">width</a>() ;
  40.       virtual int <a href="file:objects#height">height</a>() ;
  41.       virtual void <a href="file:objects#pointer">pointer</a> (int entering) ;
  42.       virtual void <a href="file:objects#mode_change">mode_change</a>() ;
  43.       Menu *<a href="file:objects#display_menu">display_menu</a> (int x, int y, int button, int icon) ;
  44.       virtual void <a href="file:objects#pre_menu">pre_menu</a>(Menu *m, int x, int y, int button, int icon) ;
  45.       virtual char *<a href="file:objects#get_menu">get_menu</a> (int x, int y, int button, int icon) ;
  46.  
  47.       virtual void <a href="file:objects#menu">menu</a> (MenuItem items[]) ;
  48.       virtual char (<a href="file:objects#help">help</a> (int mx, int my, int buttons) ;
  49.       int <a href="file:objects#x0">x0</a> ;
  50.       int <a href="file:objects#x0">y0</a> ;
  51.       int <a href="file:objects#x0">x1</a> ;
  52.       int <a href="file:objects#x0">y1</a> ;
  53.       Window *<a href="file:objects#window">window</a> ;
  54.       Menu *<a href="file:objects#default_menu">default_menu</a> ;
  55.       Object *<a href="file:objects#next">next, *prev</a> ;
  56.       char <a href="file:objects#name">name</a>[32] ;
  57.       int <a href="file:objects#priority">priority</a> ;
  58.       int <a href="file:objects#selected">selected</a> ;
  59.    } ;
  60.  
  61. </pre>
  62. <p>
  63. <h2>Members</h2>
  64.  
  65. <p>
  66. <a name="ct"></a>
  67. <h3>Object::Object (Window *w, char *name, int priority = 0, char *menu = 0)</h3>
  68.  
  69. This is the Object constructor.  The parameters are:
  70. <p>
  71. <ul>
  72. <li><b>w</b>: The window in which the object exists
  73. <li><b>name</b>: The name of the object (a text string - may be "")
  74. <li><b>priority</b>: The priority of the object.  This determines the
  75. order in which the object is placed in the window.  Higher priority
  76. objects are handled as if they are "on top" of other objects and therefore
  77. are first to be given mouse clicks.  They are also the first to be redrawn.
  78. <li><b>menu</b>: A menu to use as default if the object responds to menu
  79. clicks.
  80. </ul>
  81.  
  82. <p>
  83. <a name="dt"></a>
  84. <h3>Object::~Object()</h3>
  85.  
  86. This is the object destructor.  It will remove the object from the
  87. window.
  88.  
  89. <p>
  90. <a name="redraw"></a>
  91. <h3>Object::redraw (int x0, int y0, int x1, int y1)</h3>
  92.  
  93. This function is called when the object may need to be redrawn.  The
  94. function should check if the object boundary box lies within the
  95. coordinates passed before redrawing itself.  The background
  96. has already been cleared by the WIMP.
  97. <p>
  98. The object can check the coordinates using the following code:
  99. <pre>
  100.  
  101. if (x0 <= this->x1 && x1 >= this->x0 && y0 <= this->y1 && y1 >= this->y0)
  102.    {
  103.    // redraw the object
  104.    }
  105.    
  106. </pre>
  107.  
  108. The coordinates passed are window coordinates (not screen coordinates),
  109. but can be converted to screen coordinates by code such as:
  110. <pre>
  111.  
  112. sx = window->x0 + this->x0 - window->scx ;        // screen x
  113. sy = window->y1 + this->y0 - window->scy ;        // screen y
  114.       
  115. </pre>
  116. or by calling the Window functions <a href="file:window#coords">xtoscreen(), ytoscreen()</a>.
  117.  
  118. <p>
  119. <a name="update"></a>
  120. <h3>Object::update (int x0, int y0, int x1, int y1)</h3>
  121.  
  122. This function is called when the user has requested an update of the
  123. window by use of the Wimp_UpdateWindow call.  The coords passed are
  124. window coordinates (as in redraw).  The window is not cleared to the
  125. background colour.
  126.  
  127. <p>
  128. <a name="compare"></a>
  129. <h3>Object::compare (int x, int y)</h3>
  130.  
  131. This function is called to check if the coordinates passed (in screen coordinates
  132. this time) are within the object's boundary.
  133. <p>
  134. In the simplest case, the following code is provided as part of the
  135. Object base class.  It simply compares the bounding box of the
  136. object.  If the object is not a simple box, more complex code is
  137. required:
  138. <pre>
  139.  
  140. int Object::compare (int x, int y)
  141.    {
  142.    x = x + (window->scx - window->x0) ;   // x in window coords
  143.    y = y + (window->scy - window->y1) ;   // y in window coords
  144.    return x <= this->x1 && x >= this->x0 && y <= this->y1 && y >= this->y0 ;
  145.    }
  146.    
  147. </pre>
  148.  
  149. <p>
  150. <a name="move1"></a>
  151. <h3>Object::move (int x0, int y0, int x1, int y1)</h3>
  152.  
  153. This function moves the object to a new position (in window coordinates).
  154. It also allows the object's bounding box to be resized.
  155.  
  156. <p>
  157. <a name="move2"></a>
  158. <h3>Object::move (int dx, int dy)</h3>
  159.  
  160. This function moves the object a certain distance from its current
  161. position.  The deltas are in the x and y direction and are in
  162. window coordinates.
  163.  
  164. <p>
  165. <a name="drag"></a>
  166. <h3>Object::drag (int mx, int my, int buttons)</h3>
  167.  
  168. This function begins an object drag.  The default function as provided
  169. by the Object base class starts a drag by using the do_drag function
  170. in the window.
  171.  
  172. <p>
  173. <a name="end_drag"></a>
  174. <h3>Object::end_drag (int x0, int y0, int x1, int y1, int id)</h3>
  175.  
  176. This function is called by the Task when the drag operation has completed.
  177. It is passed the drag bounding box coordinates (screen coordinates)
  178. and an integral identifier passed to the dragger.  The default
  179. function provided by the Object base class moves the object to the
  180. new position.
  181.  
  182. <p>
  183. <a name="click"></a>
  184. <h3>Object::click(int mx, int my, int button)</h3>
  185.  
  186. This function is called when the user clicks select over the object.
  187. The parameters passed are exactly as returned from Wimp_Poll for this
  188. event.
  189. <p>
  190. Note that the window must have an appropriate button type for this
  191. event to be received.
  192.  
  193.  
  194. <p>
  195. <a name="key"></a>
  196. <h3>Object::key (int x, int y, int height, int index, int code)</h3>
  197.  
  198. This function is called when the user presses a key over the object.
  199. The paramaters passed are as returned from Wimp_Poll.
  200.  
  201. <p>
  202. <a name="double_click"></a>
  203. <h3>Object::double_click (int mx, int my, int buttons)</h3>
  204.  
  205. This function is called when the user has double-clicked the mouse
  206. over the object.  Note that the window must have an appropriate 
  207. button type for this event to be received.
  208.  
  209. <p>
  210. <a name="select"></a>
  211. <h3>Object::select()</h3>
  212.  
  213. Mark the object as selected.  The selection state can be read by
  214. accessing the variable 'selected' within the object class.
  215.  
  216. <p>
  217. <a name="unselect"></a>
  218. <h3>Object::unselect()</h3>
  219.  
  220. Mark the object as not selected.  The selection state can be read by
  221. accessing the variable 'selected' within the object class.
  222.  
  223.  
  224. <p>
  225. <a name="width"></a>
  226. <h3>Object::width()</h3>
  227.  
  228. Return the width of the object in OS units.
  229.  
  230.  
  231. <p>
  232. <a name="height"></a>
  233. <h3>Object::height()</h3>
  234.  
  235. Return the height of the object in OS units.
  236.  
  237. <p>
  238. <a name="pointer"></a>
  239. <h3>Object::pointer (int entering)</h3>
  240.  
  241. This is called when the mouse has entered or exited the object.  This
  242. is equivalent to the function of the same name in the Window class.
  243. See <a href="file:window#pointer">Window::pointer</a>.
  244.  
  245. <p>
  246. <a name="mode_change"></a>
  247. <h3> Object::mode_change()</h3>
  248.  
  249. This is invoked when the screen mode has been changed.
  250.  
  251. <p>
  252. <a name="display_menu"></a>
  253. <h3> Object::display_menu (Menu *m, int x, int y, int button, int icon)</h3>
  254.  
  255. This function is used to display a menu for this object.  If the object
  256. has been created with a default menu, this will be displayed.  Otherwise
  257. the function get_menu will be called.  If the default menu is being
  258. displayed, the function pre_menu will be called in order to allow the user
  259. to change the default menu.
  260.  
  261. <p>
  262. <a name="pre_menu"></a>
  263. <h3> Object::pre_menu (Menu *m, int x, int y, int button, int icon)</h3>
  264.  
  265. This virtual function is called just before a menu is displayed on the
  266. screen.  The purpose is to allow the user to change the contents of the
  267. menu.
  268.  
  269.  
  270. <p>
  271. <a name="get_menu"></a>
  272. <h3> Object::get_menu (int x, int y, int button, int icon)</h3>
  273.  
  274. This is called when there was no default menu provided to the constructor.
  275. It is a virtual function which should be supplied by a derived class
  276. if it needs to decide on a number of menus.  It returns the name
  277. of the menu to display.
  278.  
  279. <p>
  280. <a name="menu"></a>
  281. <h3> Object::menu(MenuItem items[])</h3>
  282.  
  283. This is invoked when the user has made a menu selection from a menu belonging
  284. to the object.  If the program wishes to deal with menu hits, this function
  285. must be provided by a derived class.  The function is passed an array of
  286. MenuItem structures which detail the selection made.  It is up to the
  287. function to determine the action based on the selection.
  288.  
  289. <p>
  290. <a name="help"></a>
  291. <h3> Object::help (int mx, int my, int buttons)</h3>
  292.  
  293. This function is called when help is being requested.  The default
  294. action is to return NULL, meaning that the object is not prepared
  295. to give anything away.  Derived classes may provide their own
  296. help function.  The function return a string (char *) which
  297. is a pointer to the help text.
  298. <p>
  299. The function is called when the mouse is over an object in the
  300. window.  By returning NULL, you are asking the window to
  301. provide its own help.
  302.  
  303. <p>
  304. <a name="x0"></a>
  305. <h3> Object::x0, Object::y0, Object::x1, Object::y1</h3>
  306.  
  307. These are the bounding coordinates for the object.  They are in 
  308. window coordinates so (0,0) is at the top left and y is always
  309. negative.
  310.  
  311. <p>
  312. <a name="window"></a>
  313. <h3>Object::window</h3>
  314.  
  315. This is the window to which the object belongs.
  316.  
  317. <p>
  318. <a name="default_menu"></a>
  319. <h3>Object::default_menu</h3>
  320.  
  321. This is a pointer to the Menu which will be dislayed when the
  322. user clicks menu over the object.  if it is NULL, there is no
  323. default menu.
  324.  
  325. <p>
  326. <a name="next"></a>
  327. <h3>Object::next, Object::prev</h3>
  328.  
  329. Pointers to the next and previous objects.  The next object is guaranteed
  330. to be of the same or lower priority than this object.
  331.  
  332. <p>
  333. <a name="name"></a>
  334. <h3>Object::name</h3>
  335.  
  336. The name given to the object when it was created.
  337.  
  338. <p>
  339. <a name="priority"></a>
  340. <h3>Object::priority</h3>
  341.  
  342. The priority given to the object when it was created.  It may be modified
  343. by writing to this class member.
  344.  
  345. <p>
  346. <a name="selected"></a>
  347. <h3>Object::selected</h3>
  348.  
  349. This contains the value 1 if the object is selected, 0 otherwise.  An object
  350. is selected and unselected by use of the select() and unselect() functions.
  351.  
  352.  
  353. <h2>Predefined Objects</h2>
  354.  
  355. Vista contains a set of predefined Objects for use by the program.  These
  356. objects are meant to be used as example for defining you own objects, although
  357. they are fully functional.
  358. <p>
  359. The objects available are:
  360. <p>
  361. <a name="icon"></a>
  362. <h3>IconObject</h3>
  363. This object contains an <a href="file:icon">Icon</a> or one of its derived classes.
  364. <pre>
  365.  
  366. class IconObject : public Object
  367.    {
  368.    public:
  369.       IconObject (Window *w, char *name, Icon *icon, int priority = 0, char *menu = 0) ;
  370.       ~IconObject() ;
  371.       void redraw (int x0, int y0, int x1, int y1) ;
  372.    private:
  373.       Icon *icon ;
  374.    } ;
  375.    
  376. </pre>
  377. <p>
  378. <a name="rawsprite"></a>
  379. <h3>RawSpriteObject</h3> 
  380. This object is a sprite.
  381. <pre>
  382.  
  383. class RawSpriteObject : public Object
  384.    {
  385.    public:
  386.       RawSpriteObject (Window *w, char *name, char *sprite, void *area, int x, int y, int priority = 0, char *menu = 0) ;
  387.       RawSpriteObject (Window *w, char *name, char *sprite, int x, int y, int priority = 0, char *menu = 0) ;
  388.       ~RawSpriteObject() ;
  389.       void redraw (int x0, int y0, int x1, int y1) ;
  390.    protected:
  391.       char *sprite ;
  392.       void *area ;
  393.       int scale_factors[4] ;
  394.       char *pixel_trans ;
  395.       void init() ;
  396.       int mode ;
  397.    } ;
  398. </pre>
  399. <p>
  400. <a name="text"></a>
  401. <h3>TextObject</h3>
  402. This is an object which contains lines of text.  
  403. The text can either be set when
  404.    the object is created or can be added later.  It will redraw itself when requested
  405.    to by the Window and will only redraw the appropriate parts inside the clipping
  406.    window.  
  407. <pre>
  408.  
  409. const int TextObject_Colour = 1 ;
  410.  
  411.  
  412. class TextObject : public Object
  413.    {
  414.    struct line
  415.       {
  416.       char *text ;           // actual text
  417.       int length ;           // length in bytes
  418.       } ;
  419.    public:
  420.       TextObject (Window *w, char *name, char *text, int x, int y, int priority = 0, char *menu = 0) ;
  421.       TextObject (Window *w, char *name, int x, int y, int priority = 0, char *menu = 0) ;
  422.       virtual ~TextObject() ;
  423.       void redraw (int x0, int y0, int x1, int y1) ;
  424.       void insert_line (char *text, int length) ;
  425.       void insert_line (char *text) ;
  426.    protected:
  427.       char *text ;         // text stored in object
  428.       line *lines ;        // array of lines
  429.       int num_lines ;      // number of lines
  430.       int max_lines ;      // mac line space available
  431.       int max_length ;     // max line length in units
  432.  
  433.    } ;
  434.  
  435. </pre>
  436. <p>
  437. The text can contain line feeds and will be split into lines at these
  438. line feeds.  The colour of the text can be set by inserting 2 byte
  439. sequence into the text.  The first byte should be the value 1 and the
  440. second should be the colour to use.
  441. <p>
  442. The <a href="file:objects#font">FontObject</a> is a much superior object as it can display text
  443. in an outline font and has much more functionality.  The TextObject
  444. should be used only when simple text is needed.
  445. <p>
  446. <a name="line"></a>
  447. <h3>LineObject</h3>
  448.    This is an example of a graphics object.  
  449. It draws a straight line.  It does not do any clipping but relies on 
  450. the OS to do it.
  451. <pre>
  452.  
  453. class LineObject : public Object
  454.    {
  455.    public:
  456.       LineObject (Window *w, char *name, int x1, int y1, int x2, int y2, int thickness = 1, int priority = 0) ;
  457.       virtual ~LineObject() ;
  458.       void redraw (int x0, int y0, int x1, int y1) ;
  459.    protected:
  460.       int thickness ;
  461.       int dx ;            // delta to add to x for thickness
  462.       int dy ;            // delta to subtract from y for thickness
  463.    } ;
  464.  
  465. </pre>
  466. The line's thinkness can be set.  This is in OS units.
  467.  
  468. <p>
  469. <a name="font"></a>
  470. <h3>FontObject</h3>
  471.  This complex object displays text in an
  472. outline font.  Once the object is created, you may add text to it
  473. and modify the appearance of the text until you tell it that you
  474. have finished.
  475. <p>
  476. The object exists at certain coordinates and has a certain width.  The
  477. text in the object will be split at spaces inside the bounding
  478. box.
  479. <p>
  480. The object is created by passing a font handle to the constructor.  This
  481. handle must be obtained by Task::open_font().  The add_text()
  482. function is used to add text to the end of any text already in the
  483. object.
  484. <p>
  485. When building the object, you may modify the appearance of the
  486. text as follows:
  487. <p>
  488.    <ol>
  489.    <li>Set the text colour to any RGB foreground and background colours.  See the
  490.        PRM for information about setting the text colour in a font.
  491.    <li>Change the font.  This allows things such as bold and italics to be used.
  492.    <li>Underline a section of text.
  493.    </ol>
  494. <p>
  495. The first line of the object may be indented from the left of the object by use
  496. of the 'set_indent()' functon.  The indent is in OS units. 
  497.  
  498. <p>
  499. <a name="draw"></a>
  500. <h3>DrawObject</h3>
  501.  This object renders a draw file on the screen.  It
  502. relies on Acorn's DrawFile module which can be obtained from public
  503. domain libraries.  The drawfile supplied to the constructor may be
  504. either a file on disk or held in memory.
  505. <pre>
  506.  
  507. class DrawObject : public Object
  508.    {
  509.    public:
  510.       DrawObject(Window *w, char *name, char *data, int length, int x, int y, int priority = 0, char *menu = 0) ;
  511.       DrawObject(Window *w, char *name, char *filename, int x, int y, int priority = 0, char *menu = 0) ;
  512.       DrawObject(Window *w, char *name, int priority = 0, char *menu = 0) ;
  513.       ~DrawObject() ;
  514.       void redraw (int x0, int y0, int x1, int y1) ;
  515.    protected:
  516.       char *data ;                     // draw data (in drawfile format)
  517.       int data_length ;
  518.       int matrix[6] ;                  // translation matrix
  519.       int xoffset, yoffset ;           // draw unit offset to bounding box
  520.       bool dealloc_data ;              // OK to deallocate data in destructor
  521.       int max_length ;                 // max length for dynamic data array
  522.  
  523.       void init(int x, int y) ;                       // init for drawing on screen
  524.       void make_header (int width, int height) ;      // make a file header
  525.       void insert_word (int word) ;                   // insert a word
  526.       void insert_string (char *str) ;                // insert a string
  527.       void insert_byte (char c) ;                     // insert a byte
  528.       void insert_data (void *s, int length) ;        // insert a load of data
  529.       void align() ;                                  // align to next word
  530.       void check_space (int num_bytes) ;              // check space and extend if necessary
  531.    } ;
  532.  
  533. </pre>
  534. <p>
  535. <a name="sprite"></a>
  536. <h3>SpriteObject</h3>
  537.  This is an alternative sprite object which uses
  538. the DrawObject.  It copes with the various colour problems associated
  539. with sprites.
  540. <pre>
  541.  
  542. class SpriteObject : public DrawObject
  543.    {
  544.    public:
  545.       SpriteObject (Window *w, char *name, char *sprite, int x, int y, int priority = 0, char *menu = 0) ;
  546.       ~SpriteObject() ;
  547.    } ;
  548.    
  549. </pre>
  550.  
  551. You can derive other objects from the Object class at will.  If you find
  552. that the predefined objects do not suit your purposes, you should define
  553. your own or derive a new object from the predefined ones.
  554.