home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / New Venus / src / Dialog.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-05  |  5.3 KB  |  139 lines  |  [TEXT/CWIE]

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *                            Generic Dialog Class
  5.  *
  6.  *            to handle a modeless dialog: a window with controls
  7.  *
  8.  * We call it a dialog because we want to take advantage of DialogManager
  9.  * functionality to figure out which items needs to be redrawn, etc. chores
  10.  *
  11.  * Note the dialog is derived from the ScreenWindow, and it considers
  12.  * ScreenWindow::this_window to be a DialogPtr
  13.  *
  14.  * Note, ModelessDialog::Item etc objects contain a dialog item handle, etc. information,
  15.  * which is only valid as long as the corresponding dialog is still there. Therefore,
  16.  * there is a danger of keeping and using an Item object after the dialog itself gets 
  17.  * disposed of. Declaring the Item class within the Modeless dialog _protected_ scope,
  18.  * although does not remove that danger, certainly decreases chances of that
  19.  * happening: this declaration makes sure that the Items are going to be used only
  20.  * within dialog member functions, the only place it makes sense.
  21.  *
  22.  ***********************************************************************
  23.  */
  24.  
  25. #pragma once
  26. #include "window.h"
  27. #include "mymenv.h"
  28.  
  29. class ModelessDialog : public ScreenWindow
  30. {
  31.   friend class BasicControl;
  32.   friend class UserItem;
  33.   
  34.   virtual void draw_user_item(const int item_no)         {}
  35.   virtual Boolean handle_item_hit(const int item_no) = 0;
  36.   virtual Boolean handle_activate(Boolean onoff); // Handle suspend/resume events
  37.   virtual void draw(void) {}                // DialogSelect does all the drawing for us
  38.  
  39.   virtual void destroy_it(void);            // Virtual destructor kludge
  40.  
  41.   Boolean generic_item_hit(const int item_no);     // Return FALSE if additional attention needed
  42. protected:
  43.                                             // Item control utilities
  44. //  void draw_default_item_border(const int item_no);
  45.  
  46.                                             // Generic dialog items and more specialized ones
  47.   class Item
  48.   {
  49.   protected:
  50.     Handle item_handle;
  51.     short  item_type;
  52.     Rect   item_rect;
  53.   public:
  54.     Item(const ModelessDialog& the_dialog, const int item_no);
  55.     operator const Rect * (void) const    { return &item_rect; }
  56.   };
  57.  
  58.   friend class ModelessDialog::Item;
  59.   
  60.   class ControlItem : public Item
  61.   {
  62.   public:
  63.     ControlItem(const ModelessDialog& the_dialog, const int item_no);
  64.     operator ControlHandle (void) const        { return (ControlHandle)item_handle; }
  65.     void set_title(const Str255 new_title)        { SetControlTitle((ControlHandle)item_handle,new_title); }
  66.   };
  67.  
  68.   class TextItem : public Item
  69.   {
  70.   public:
  71.     TextItem(const ModelessDialog& the_dialog, const int item_no);
  72.     void draw(const Str255 str);                // Draw a new text in an item
  73.   };
  74.   
  75. public:
  76.                             // Create a color dialog from a resource template
  77.   ModelessDialog(const short resource_id);
  78.  
  79.                                   // ModelessDialog event dispatch entry points
  80.                                   // Return FALSE if this object doesn't want any
  81.                                   // more events
  82.   virtual Boolean handle_null_event(const long event_time);
  83.   virtual Boolean handle_event(const EventRecord& the_event);
  84. };
  85.  
  86.  
  87.                                         // Basic control object
  88.                                         // It's assumed that a pointer to this object
  89.                                         // is placed into RefCon of a control
  90.                                         // This object handles some specific
  91.                                         // click and highlighting features
  92.                                         // Thus it's a way of implementing custom
  93.                                         // control by slightly modifying a standard
  94.                                         // control, and all without messing with
  95.                                         // custom CDEF
  96. class BasicControl        
  97. {
  98.   friend class ModelessDialog;
  99.   const int signature;                        // To make sure we are who we are
  100.   enum { valid_sig = 0x43785 };
  101.   ControlHandle this_control;
  102.   
  103.  
  104.   static ControlDefProcPtr * real_defproc_handle;        // original ScrollBar CDEF handle
  105.   static ControlDefProcPtr * patch_defproc_handle;         // handle to our patch, see below
  106.   static ControlDefUPP defproc_patch_upp;                // Universal pointer to our patch
  107.   static pascal SInt32 defproc_patch                    // our patch itself
  108.       (SInt16 var_code, ControlRef the_control, ControlDefProcMessage message, SInt32 param);
  109.   //static pascal void track_action_relay(ControlHandle the_control, unsigned short part_no);
  110.   //static ControlActionUPP track_action_relay_upp;    // Universal pointer to the action_relay
  111.   virtual void track_action(const int part_no)         {}
  112.   
  113.                                               // Custom handle a mouse-click within this
  114.                                               // control. Return TRUE if it's _completely_
  115.                                               // handled, return FALSE if additional attention
  116.                                               // needed (say, control value was changed)
  117.   virtual Boolean handle_click(void)             { return FALSE;}
  118.   Boolean is_our_control(void) const             { return signature == valid_sig; }
  119. protected:
  120.   BasicControl(void) : signature(valid_sig), this_control(0) {}
  121.   ControlHandle our_control(void) const
  122.                           { assert( this_control != nil ); return this_control; }
  123.   void bind(const ModelessDialog::ControlItem& item);        // Late constructor
  124.   void set_value(const int new_value);        // Set a new value of a control, clipped to
  125.                                               // its [min,max] range
  126. };
  127.  
  128. class UserItem
  129. {
  130.   static pascal void user_item_relay_handler(WindowPtr the_dialog, short the_item);
  131.   static UserItemUPP user_item_relay_handler_upp;    // Universal pointer to the user_item_univ_handler
  132. protected:
  133.   Rect rect;
  134. public:
  135.   UserItem(void) {}
  136.   void bind(const ModelessDialog& the_dialog, const int item_no);        // Late constructor
  137.   void force_redraw(void)        { InvalRect(&rect); }
  138. };
  139.