home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Venus 3.5 / window.h < prev   
Encoding:
C/C++ Source or Header  |  1997-04-10  |  10.5 KB  |  272 lines  |  [TEXT/CWIE]

  1. /*
  2.  *************************************************************************
  3.  *
  4.  *                        A Basic Window class
  5.  *
  6.  *         A generic simple window that can either show up by itself
  7.  *    (and be dragged around on the screen and closed by clicking a
  8.  *                            "go-away" button)
  9.  *    or appear as a special item in a dialog
  10.  *
  11.  * Class ScreenWindow provides a very generic event and control processing
  12.  * that is common to regular windows with controls, modal and modeless
  13.  * dialogs. The basic functionality is to route events to appropriate
  14.  * handlers, process close/destroy/etc events, and handle controls.
  15.  * The class ScreenWindow provides only basic event handling, and it's
  16.  * abstract: you have to create your own class on the base of ScreenWindow
  17.  * and define the draw() function that is called on Update event to
  18.  * paint/repaint the window. You may want to redefine couple of other
  19.  * vanilla handlers (say, for control items (buttons, etc)).
  20.  *
  21.  * When you create the window using explicit specification of the rectangle, title, etc,
  22.  * the window would be invisible right after the creation. It's better to set up
  23.  * invisible (or hidden) attribute in the window resource if you create
  24.  * a window from the resource template. That way you still can do some
  25.  * rearrangements (say, create child windows) before the event processing
  26.  * starts.
  27.  *
  28.  * To show the window (with all the controls, children etc) and start
  29.  * processing of events, call show(). If you need to wait until the window
  30.  * is closed (after the user clicked some button or clicked at the go-away
  31.  * region, or selected "Close" from the menu, or something) call the
  32.  * serve_to_death() function. It returns only when all the opened windows
  33.  * are closed.
  34.  *
  35.  * $Id: Window.h,v 2.2 1994/11/07 20:33:30 oleg Exp oleg $
  36.  *
  37.  ***********************************************************************
  38.  */
  39.  
  40. #ifndef __GNUC__
  41. #pragma once
  42. #else
  43. #pragma interface
  44. #endif
  45.  
  46. #ifndef _Window_h
  47. #define _Window_h 1
  48. #include <QDOffscreen.h>
  49. #include "mymenv.h"
  50.  
  51. /*
  52.  *----------------------------------------------------------------------
  53.  *                Service functions
  54.  */
  55.  
  56. class IMAGE;                // Opaque class
  57. class Palette;
  58. typedef Palette ** PaletteHandle;
  59.  
  60. class ScreenRect : public Rect
  61. {
  62. public:
  63.   ScreenRect(const Rect& rect) : Rect(rect) {}
  64.   ScreenRect(const IMAGE& image);
  65.   ScreenRect(const int height, const int width) { left=top=0; right=width; bottom=height; }
  66. //  ScreenRect(const rowcol& heightwidth);
  67.                     // Create a rectangle of given height/width
  68.                     // positioned at a given point
  69. //  ScreenRect(const rowcol& origin, const rowcol& heightwidth);
  70.   ScreenRect& operator += (const int offset);
  71.   operator Rect * (void)                 { return this; }
  72.   int q_width(void)  const              { return right >= left ? right - left : left - right; }
  73.   int q_height(void) const              { return bottom >= top ? bottom - top : top - bottom; }
  74.                                 // Give a STATIC string representation of
  75.                                 // the rectangle
  76. //  operator const char * (void) const;
  77.   void print(const char * title = "") const;
  78. };
  79.  
  80.  
  81.                 // Make it easy setting and resetting
  82.                 // the current GrafPtr
  83. class SetNewGrafPtr
  84. {
  85.   GrafPtr old_port;
  86. public:
  87.                     // Switch to an off-screen grafworld
  88.   SetNewGrafPtr(const GrafPtr new_grafport) { GetPort(&old_port); SetPort(new_grafport); }
  89.                     // Restore the original grafworld
  90.  ~SetNewGrafPtr(void) { SetPort(old_port); }
  91. };
  92.  
  93. /*
  94.  *----------------------------------------------------------------------
  95.  *    A generic simple window that can be dragged around the screen
  96.  *        and closed by clicking a "go-away" button
  97.  * No other events are handled, redefine the event handler if necessary.
  98.  */
  99.  
  100. class ScreenWindow
  101. {
  102.   friend class ModelessDialog;
  103.   
  104.   WindowPtr this_window;
  105.   PaletteHandle private_palette;    // Private palette set up by this window, if any
  106.   
  107. //  Boolean Not_canceled;         // set to FALSE if Cancel button was pressed
  108.                                 // or the window was forcibly closed
  109.  
  110. //  static long universal_handler(WINDOW win, EVENT *ep);
  111. //  static int no_opened_windows;
  112.     
  113.   void update(void);
  114.   virtual void draw(void) = 0;            // It is this function that really draws smth
  115.     
  116.                                           // A constructor that does nothing, and initializes
  117.                                           // nothing. Trick to emulate a virtual constructor
  118.   ScreenWindow(void) : this_window(nil), private_palette(nil) {}
  119.  
  120. protected:
  121.   WindowPtr our_window(void) const     { return this_window; }
  122.  
  123.                                 // Draw w/o waiting for an UPDATE event:
  124.                                 // Use for animation
  125.   void draw_immediately(void)   { draw(); /*?update()?? */}
  126.  
  127.   virtual void destroy_it(void);            // I wouldn't've needed this if a virtual destructor actually
  128.                                               // worked. Right now, CW 6.0 accepts attribute virtual
  129.                                               // for a destructor, but doesn't override
  130.                                               // destructors as it does for virtual functions.
  131.                                               // Oh, well, another kludge
  132.  
  133.   virtual Boolean handle_mouse_down(const EventRecord& the_event, WindowPtr where_window, short window_part);
  134.   virtual Boolean handle_key_down(const EventRecord& the_event);    // Handles key_down & auto_key events
  135.  
  136.   virtual Boolean handle_my_event(const EventRecord& the_event);    // Handles my private events...
  137.  
  138.   void set_private_palette(const CTabHandle clut);
  139.   
  140. public:
  141.   ScreenWindow(ScreenRect rect, const char * title);
  142.   ScreenWindow(const short resource_id);        // Create a window from a resource template
  143.   ~ScreenWindow(void)                    { destroy_it(); }
  144.   
  145.                                       // This is a post-constructor: unlike a real
  146.                                       // constructor, it can be virtual, and it _can_
  147.                                       // call virtual functions. Always call init()
  148.                                       // after the construction, but _not_ in the
  149.                                       // constructor itself
  150.   void init(void)                        { show(); }            
  151.   void refresh(void);
  152.                                 // Serve opened windows until all get closed
  153. //  static void serve_to_death(void);
  154.  
  155.   void show(const Boolean onoff=TRUE) const  { ShowHide(this_window, onoff); }
  156.   ScreenRect q_bounds(void) const        { return this_window->portRect; }
  157.   
  158.   // virtual void cancel(void);            // Close the window
  159.   // operator Boolean (void) const         { return Not_canceled; }
  160.   // Boolean is_this_window(const WindowPtr some_window) const { return some_window == this_window; }
  161.   // Boolean operator == (const WindowPtr some_window) const { return is_this_window(some_window); }
  162.  
  163.                                   // ScreenWindow event dispatch entry points
  164.                                   // Return FALSE if this object doesn't want any
  165.                                   // more events
  166.   virtual Boolean handle_null_event(const long event_time);
  167.   virtual Boolean handle_event(const EventRecord& the_event);
  168. };
  169.  
  170. /*
  171.  *----------------------------------------------------------------------
  172.  *                            Color Lookup Table
  173.  */
  174.  
  175. class CLUTable
  176. {
  177.   CTabHandle handle;
  178.   bool my_own_handle;
  179.   
  180.   CLUTable(const CLUTable&);            // unimplemented: no cloning of
  181.   CLUTable& operator = (const CLUTable&); // CLUTable is allowed
  182. public:
  183.   CLUTable(void) : handle(nil), my_own_handle(false) {}
  184.   CLUTable(const short clut_id);            // Load from a 'clut' resource
  185.                               // Wrap around (borrow from) a foreign handle
  186.   CLUTable(const CTabHandle other_handle) : handle(other_handle), my_own_handle(false) {}
  187.   
  188.   ~CLUTable(void)  { if( my_own_handle ) DisposeCTable(handle); my_own_handle = false; }
  189.   
  190.   void deep_copy_from(const CLUTable& another_clut); // another_clut may be null!
  191.   bool q_null(void) const            { return handle == nil; }
  192.   int q_size(void)  const            { assert(handle != 0); return (**handle).ctSize; }
  193.   operator CTabHandle (void) const    { assert(handle != 0); return handle; }
  194.   void dump(const char title []) const;        // Dump the whole contents of the CLUT
  195.   void info(const char title [] = "") const;        // Tell briefly about this CLUT
  196. };
  197.  
  198. /*
  199.  *----------------------------------------------------------------------
  200.  *                An off-screen pixel buffer for faster drawing
  201.  */
  202.  
  203.  
  204. class OffScreenBuffer
  205. {
  206.   GWorldPtr graf_world;                // (Offscreen) graphical world that contains the picture
  207.  
  208.   OffScreenBuffer(const OffScreenBuffer&);            // unimplemented: no copying/cloning of
  209.   OffScreenBuffer& operator = (const OffScreenBuffer&); // OffScreenBuffer is allowed
  210. protected:
  211.   PixMapHandle pixmap;                // Pixmap for the offscreen world
  212.   int _height;                        // Dimensions of the pixmap (placed here for
  213.   int _width;                        // easy reference)
  214.   int _bytes_per_row;                // Note, bytes_per_row >= width (and generally, not equal)
  215.  
  216.     
  217.   GrafPtr set_this_grafptr(void) const                // Set this grafptr and return the old one
  218.           { GrafPtr old_port; GetPort(&old_port); SetPort((GrafPtr)graf_world); return old_port; }
  219.  
  220. public:
  221.   OffScreenBuffer(ScreenRect rect, const CLUTable& clut=CLUTable());
  222.   ~OffScreenBuffer(void);
  223.  
  224.                                     // OffScreen world queries...
  225.   PixMapHandle get_pixmap(void) const    { return pixmap; }
  226.         
  227.                                           // Bounding rect of the grafworld
  228.   ScreenRect q_bounds(void) const        { return (**pixmap).bounds; }
  229.   int height(void) const                { return _height; }         
  230.   int width(void) const                    { return _width; }         
  231.   int bytes_per_row(void) const            { return _bytes_per_row; }         
  232.  
  233.   const CTabHandle q_clut(void) const    { return (**pixmap).pmTable; }
  234.  
  235.                                       // Blit the offscreen buffer on screen
  236.   void draw(const Rect& where_rect, const Rect& from_rect);                // only part of it
  237.   void draw(const Rect& where_rect)        { draw(where_rect,(**pixmap).bounds); } // or the whole
  238.   void clear(void);                    // Clear the buffer
  239.   
  240.                                       // Optimize the OffScreen buffer for a faster
  241.                                       // drawing on a given color device
  242.                                       // Return TRUE if the optimization succeeded
  243.                                       // (bitblitting shall be faster)
  244.   Boolean optimize_color_worlds(const GDHandle g_device);
  245.   
  246.                                       // Brute imposing of our color world on the
  247.                                       // g_device: setting up its colortables by force
  248.                                       // Chances are it would screw colors of all other
  249.                                       // windows; but if we're the only window on the screen
  250.                                       // or we don't care...
  251.                                       // If this imposition succeeds (the function returns
  252.                                       // TRUE), our grafworld when blitted on screen would
  253.                                       // certainly look at its (color) best
  254.   Boolean impose_on_gdevice(GDHandle g_device);
  255. };
  256.  
  257.                 // Make it easy setting and resetting
  258.                 // the current window
  259. class Set_NewGrafWorld
  260. {
  261.   CGrafPtr old_port;
  262.   GDHandle old_gdevice;
  263. public:
  264.                     // Switch to an off-screen grafworld
  265.   Set_NewGrafWorld(GWorldPtr graf_world)     
  266.     { GetGWorld(&old_port,&old_gdevice); SetGWorld((CGrafPtr)graf_world,nil); }
  267.                     // Restore the original grafworld
  268.  ~Set_NewGrafWorld(void) { SetGWorld(old_port,old_gdevice); }
  269. };
  270.  
  271. #endif
  272.