home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Core / source / widget.cp < prev    next >
Encoding:
Text File  |  1995-02-25  |  10.8 KB  |  181 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    widget.cp
  3. //    Date:                    7/17/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains the methods widget class. a widget is a 
  7. //                                user interface item that handles clicks and keys and other 
  8. //                                forms of interraction.
  9. //
  10. //------------------------------------------------------------------------------
  11.  
  12. #include "event.h"
  13. #include "widget.h"
  14.  
  15. //------------------------------------------------------------------------------
  16. //    constructor
  17. //------------------------------------------------------------------------------
  18. widget::widget (void)                                                                                                                        //    constructor
  19. {                                                                                                                                                                //    begin
  20.     region = NewRgn ();                                                                                                                        //    create a new region (empty)
  21.     next = 0;                                                                                                                                            //    no pointer to next widget in the list
  22.     children = 0;                                                                                                                                    //    no pointer to child widgets
  23.     parent = 0;                                                                                                                                        //    no pointer to a parent widget
  24.     enabled = TRUE;                                                                                                                                //    all widgets start enabled
  25. }                                                                                                                                                                //    end
  26.  
  27. //------------------------------------------------------------------------------
  28. //    destructor
  29. //------------------------------------------------------------------------------
  30. widget::~widget (void)                                                                                                                    //    destructor
  31. {                                                                                                                                                                //    begin
  32.     if (region)    DisposeRgn (region);                                                                                            //    release the region, if any
  33.     while (children)                                                                                                                            //    for all the child widgets
  34.     {                                                                                                                                                            //    begin
  35.         widget    *current = children;                                                                                                //    get a copy of the current child pointer
  36.         children = children->next;                                                                                                    //    advance to the next child widget
  37.         delete current;                                                                                                                            //    kill the current child
  38.     }                                                                                                                                                            //    end
  39. }                                                                                                                                                                //    end
  40.  
  41. //------------------------------------------------------------------------------
  42. //    Adjust the cursor if it is inside the region
  43. //------------------------------------------------------------------------------
  44. widget    *widget::AdjustCursor (EventRecord &event)                                                            //    method to adjust the cursor appropriately
  45. {                                                                                                                                                                //    begin
  46.     widget    *current = FindWidget (event);                                                                                //    find the widget under the mouse
  47.     if (current)                                                                                                                                    //    if it is a valid widget
  48.     {                                                                                                                                                            //    begin
  49.         MouseRegion (current->region);                                                                                            //    copy its region to the mouse region
  50.         current->SetCursor ();                                                                                                            //    set the cursor to what it wants
  51.     }                                                                                                                                                            //    end
  52.     return current;                                                                                                                                //    return the widget
  53. }                                                                                                                                                                //    end
  54.  
  55. //------------------------------------------------------------------------------
  56. //    Set the cursor to what is appropriate for the widget
  57. //------------------------------------------------------------------------------
  58. void    widget::SetCursor (void)                                                                                                    //    method to set the cursor to the correct shape
  59. {                                                                                                                                                                //    begin
  60.     InitCursor ();                                                                                                                                //    default is to set the cursor to an arrow
  61. }                                                                                                                                                                //    end
  62.  
  63. //------------------------------------------------------------------------------
  64. //    Handle a mouse down event in the widget
  65. //------------------------------------------------------------------------------
  66. void        widget::HandleClick (EventRecord &event)                                                                //    handle mouse down/up
  67. {                                                                                                                                                                //    begin
  68.     widget    *current = FindWidget (event);                                                                                //    find the owner of the click
  69.     if (current && (current != this))                                                                                            //    if the current widget is valid
  70.         current->HandleClick (event);                                                                                                //    let it handle the click
  71. }                                                                                                                                                                //    end
  72.  
  73. //------------------------------------------------------------------------------
  74. //    Handle a key event in the widget
  75. //------------------------------------------------------------------------------
  76. void        widget::HandleKey (EventRecord&)                                                                                //    handle key press events
  77. {                                                                                                                                                                //    begin
  78. }                                                                                                                                                                //    end
  79.  
  80. //------------------------------------------------------------------------------
  81. //    update the widget
  82. //------------------------------------------------------------------------------
  83. void    widget::Update (EventRecord &event)                                                                                //    method to update the widget and all of its children
  84. {                                                                                                                                                                //    begin
  85.     if (children)                                                                                                                                    //    if this widget has a child
  86.         children->Update (event);                                                                                                        //    tell it to update
  87.     if (next)                                                                                                                                            //    if this widget has a sibling
  88.         next->Update (event);                                                                                                                //    tell it to update
  89.     Draw ();                                                                                                                                            //    draw the widget
  90. }                                                                                                                                                                //    end
  91.  
  92. //------------------------------------------------------------------------------
  93. //    activate the widget and all the children accordingly
  94. //------------------------------------------------------------------------------
  95. void        widget::Activate (EventRecord &event)                                                                        //    activate/deactivate the widget
  96. {                                                                                                                                                                //    begin
  97.     if (children)                                                                                                                                    //    if this widget has a child
  98.         children->Activate (event);                                                                                                    //    tell it to activate
  99.     if (next)                                                                                                                                            //    if this widget has a sibling
  100.         next->Activate (event);                                                                                                            //    tell it to activate
  101. }                                                                                                                                                                //    end
  102.  
  103. //------------------------------------------------------------------------------
  104. //    resize the widget and all the children accordingly
  105. //------------------------------------------------------------------------------
  106. void    widget::Resize (EventRecord &event)                                                                                //    method to recompute sizing information from parent
  107. {                                                                                                                                                                //    begin
  108.     if (parent)                                                                                                                                        //    if the widget has a parent
  109.         DiffRgn (parent->region, region, parent->region);                                                        //    subtract this region from the parent region
  110.     if (children)                                                                                                                                    //    if this widget has a child
  111.         children->Resize (event);                                                                                                        //    tell it to resize
  112.     if (next)                                                                                                                                            //    if this widget has a sibling
  113.         next->Resize (event);                                                                                                                //    tell it to resize
  114. }                                                                                                                                                                //    end
  115.  
  116. //------------------------------------------------------------------------------
  117. //    add a child widget to this widget
  118. //------------------------------------------------------------------------------
  119. void    widget::AddChild (widget *w)                                                                                            //    add a child widget to this one
  120. {                                                                                                                                                                //    begin
  121.     w->next = children;                                                                                                                        //    put the current child list into the new child
  122.     children = w;                                                                                                                                    //    put the new child at the front of the list
  123.     w->parent = this;                                                                                                                            //    assign the parent to the new widget
  124. }                                                                                                                                                                //    end
  125.  
  126. //------------------------------------------------------------------------------
  127. //    Find the widget that owns the current interface event
  128. //------------------------------------------------------------------------------
  129. widget    *widget::FindWidget (EventRecord &event)                                                                //    return a pointer to the widget that owns the location of the mouse
  130. {                                                                                                                                                                //    begin
  131.     if (PtInRgn (event.where, region))                                                                                        //    if the point is actually in the widget
  132.         return this;                                                                                                                                //    return, we've found it
  133.     widget    *current = children;                                                                                                    //    get the child list
  134.     while (current)                                                                                                                                //    loop over all the children
  135.     {                                                                                                                                                            //    begin
  136.         widget    *w = current->FindWidget (event);                                                                        //    try to find the owner in this child
  137.         if (w)                                                                                                                                            //    if successful
  138.             return w;                                                                                                                                    //    return the results
  139.         current = current->next;                                                                                                        //    skip to the next child
  140.     }                                                                                                                                                            //    end
  141.     return 0;                                                                                                                                            //    return the failure
  142. }                                                                                                                                                                //    end
  143.  
  144. //------------------------------------------------------------------------------
  145. //    Set the enabled state of the widget
  146. //------------------------------------------------------------------------------
  147. void        widget::Enable (bool enable)                                                                                        //    set the enable state of the widget
  148. {                                                                                                                                                                //    begin
  149.     enabled = enable;                                                                                                                            //    set the currently enabled state
  150.     widget    *current = children;                                                                                                    //    get the first child
  151.     while (current)                                                                                                                                //    as long as there are children
  152.     {                                                                                                                                                            //    begin
  153.         current->Enable (enable);                                                                                                        //    tell it to enable
  154.         current = current->next;                                                                                                        //    skip to the next child
  155.     }                                                                                                                                                            //    end
  156.     Draw ();                                                                                                                                            //    redraw the widget in its new state
  157. }                                                                                                                                                                //    end
  158.  
  159. //------------------------------------------------------------------------------
  160. //    an action command
  161. //------------------------------------------------------------------------------
  162. void        widget::Action (int code)                                                                                                //    an action command
  163. {                                                                                                                                                                //    begin
  164.     widget    *current = children;                                                                                                    //    get the first child
  165.     while (current)                                                                                                                                //    as long as there are children
  166.     {                                                                                                                                                            //    begin
  167.         current->Action (code);                                                                                                            //    tell it to perform the action
  168.         current = current->next;                                                                                                        //    skip to the next child
  169.     }                                                                                                                                                            //    end
  170. }                                                                                                                                                                //    end
  171.  
  172. //------------------------------------------------------------------------------
  173. //    draw the widget
  174. //------------------------------------------------------------------------------
  175. void        widget::Draw (void)                                                                                                            //    draw the widget
  176. {                                                                                                                                                                //    begin
  177.     //    set the clipping region to the widget region??
  178. }                                                                                                                                                                //    end
  179.  
  180. //------------------------------------------------------------------------------
  181.