home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / Headers / Core / XView.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  13.0 KB  |  552 lines

  1. /*    XView.h
  2.  *
  3.  *        This is the declaration of the View object, and the junk that
  4.  *    surrounds the view object. 
  5.  *
  6.  *        The view object is the basic 'widget' or 'subview' that makes
  7.  *    up the user interface for my application. It is basically implemented
  8.  *    as a small subset of the drawing view
  9.  */
  10.  
  11. /*  YAAF - Yet another application framework
  12.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  13.  *  
  14.  *  This library is free software; you can redistribute it
  15.  *  and/or modify it under the terms of the GNU Library
  16.  *  General Public License as published by the Free Software
  17.  *  Foundation; either version 2 of the License, or any
  18.  *  later version.
  19.  *  
  20.  *  This library is distributed in the hope that it will be
  21.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  22.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  23.  *  PURPOSE. See the GNU Library General Public License for
  24.  *  more details.
  25.  *  
  26.  *  You should have received a copy of the GNU Library General
  27.  *  Public License along with this library; if not, write to the
  28.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  29.  *  Boston, MA 02111-1307, USA.
  30.  *  
  31.  *  To contact the author, either e-mail me at
  32.  *  woody@alumni.caltech.edu, or write to us at
  33.  *  
  34.  *          William Edward Woody
  35.  *          In Phase Consulting
  36.  *          1545 Ard Eevin Avenue
  37.  *          Glendale, CA 91202
  38.  */
  39.  
  40. #ifndef __XVIEW_H__
  41. #define __XVIEW_H__
  42.  
  43. #include <XConfig.h>
  44. #include <XEvent.h>
  45.  
  46. #if OPT_MACOS == 1
  47. #include <QDOffscreen.h>
  48. #endif
  49.  
  50. #if defined(__MWERKS__)
  51.     #if defined(macintosh)
  52.         #pragma options align=power
  53.     #endif
  54.     #if defined(__INTEL__)
  55.         #pragma pack(push,2)
  56.     #endif
  57. #endif
  58.  
  59. /************************************************************************/
  60. /*                                                                        */
  61. /*    Forwards                                                            */
  62. /*                                                                        */
  63. /************************************************************************/
  64.  
  65. class XGDraw;
  66. class XGWindow;
  67. class XGWinManager;
  68. class XGDocument;
  69. class XGView;
  70. class XGPrinter;
  71.  
  72. /************************************************************************/
  73. /*                                                                        */
  74. /*    Constructor Resource Records                                        */
  75. /*                                                                        */
  76. /************************************************************************/
  77.  
  78. /*    XGSViewResHeader
  79.  *
  80.  *        This is the view resource header object; this is the preamble
  81.  *    which goes in front of the custom resource data to indicate the
  82.  *    offsets to the imbedded views
  83.  */
  84.  
  85. struct XGSViewResHeader {
  86.     long                        mysize;            /* My size                */
  87.     long                        offchild;        /* Byte offset to first */
  88.     long                        offsibling;        /* Byte offset to sib    */
  89.     long                        fViewType;        /* Required first field    */
  90. };
  91.  
  92. /************************************************************************/
  93. /*                                                                        */
  94. /*    View Factory                                                        */
  95. /*                                                                        */
  96. /************************************************************************/
  97.  
  98. /*    XGArgStream 
  99.  *
  100.  *        This is the class which is used to stream the arguments for the
  101.  *    view factory. This class defines a simple object which is basically
  102.  *    a series of arguments--it is important that the arguments be pulled
  103.  *    in the correct order, from the first to the last.
  104.  */
  105.  
  106. class XGArgStream {
  107.     public:
  108.                                 XGArgStream(void *resData);
  109.         virtual                    ~XGArgStream();
  110.         
  111.         /*
  112.          *    Argument accessor functions
  113.          */
  114.         
  115.         bool                    GetBoolean();
  116.         long                    GetInteger();
  117.         void                    GetString(short, char *);
  118.         
  119.     protected:
  120.         void                    GetArg(short,void *);
  121.         
  122.     private:
  123.         void                    *fResData;
  124. };
  125.  
  126. /*    XGViewFactory
  127.  *
  128.  *        This is the factory abstract type. If you want to automatically
  129.  *    create views from a resource file, you need to specify the factory
  130.  *    class to construct the specified view as a subclass of this.
  131.  */
  132.  
  133. class XGViewFactory {
  134.     public:
  135.                                 XGViewFactory();
  136.         virtual                    ~XGViewFactory();
  137.         
  138.         /*
  139.          *    The function to override to create my thing
  140.          */
  141.         
  142.         virtual XGView            *CreateView(long viewID, XGView *parent, XGArgStream &) = 0;
  143.  
  144.         /*
  145.          *    Global (and internal) accessor
  146.          */
  147.         
  148.         static XGView            *_Create(long,XGView *,XGSViewResHeader *);
  149.         static XGView            *_Create(XGView *,short);
  150.     private:
  151.         XGViewFactory            *fNext;
  152.         static XGViewFactory    *gList;
  153. };
  154.  
  155. /************************************************************************/
  156. /*                                                                        */
  157. /*    Draw Management                                                        */
  158. /*                                                                        */
  159. /************************************************************************/
  160.  
  161. /*    XGDraw
  162.  *
  163.  *        This is the draw class object which forces the focus to the
  164.  *    specified view, sets things up, and sets a drawing global.
  165.  *
  166.  *        It is presumed that this drawing operation is an atomic
  167.  *    operation; that is, while drawing happens, no other objects can
  168.  *    draw in another task. That's done by using an internal mutex
  169.  *    object associated with the task ID; that is, serial operations
  170.  *    on the display is kept by preventing a second task to succeed
  171.  *    in calling the XGDraw constructor when a first task is operating.
  172.  *
  173.  *        As on some architectures the draw flush is associated with
  174.  *    the destructor of this object, it's probably not a good idea to
  175.  *    keep one of these around. That is, this should be created as
  176.  *    an automatic object.
  177.  */
  178.  
  179. class XGDraw {
  180.     public:
  181.                                 XGDraw(XGView *v, bool lc = true);
  182.                                 XGDraw(XGPrinter *v);
  183.         virtual                    ~XGDraw();
  184.         
  185.         /*
  186.          *    Additional state support
  187.          */
  188.         
  189.         static XGDraw            *GetDraw()
  190.                                     {
  191.                                         return gDraw;
  192.                                     }
  193.         
  194.         /*
  195.          *    Clipping Support
  196.          */
  197.         
  198.         void                    ClipRect(Rect *r = NULL);
  199.         
  200. #if OPT_WINOS == 1
  201.         /*
  202.          *    Windows support--This works as a global, as my serialized
  203.          *    access means that only one drawing context can be the current
  204.          *    drawing context.
  205.          */
  206.         
  207.         HDC                        GetDC() const
  208.                                     {
  209.                                         return fDC;
  210.                                     }
  211. #endif
  212.         
  213.     private:
  214.         void                    CreateContext(bool);
  215.         void                    SaveContext();
  216.         void                    RestoreContext();
  217.         void                    DestroyContext();
  218.     
  219.         /*
  220.          *    Internal globals
  221.          */
  222.         
  223.         static XGDraw            *gDraw;        // Draw stack
  224.         XGDraw                    *fLast;
  225.         XGView                    *fView;
  226.         XGPrinter                *fPrint;
  227.         
  228.         /*
  229.          *    Internal state information
  230.          */
  231.         
  232. #if OPT_MACOS == 1
  233.         PenState                fPenState;
  234.         short                    fFont;
  235.         short                    fFace;
  236.         short                    fSize;
  237.         short                    fMode;
  238.         RGBColor                fForeColor;
  239.         RGBColor                fBackColor;
  240.         Fixed                    fSpExtra;
  241.         
  242.         RgnHandle                fClip;
  243.         Point                    fOffset;
  244.         GrafPtr                    fPort;
  245. #endif
  246.  
  247. #if OPT_WINOS == 1
  248.         HDC                        fDC;
  249.         long                    fDCSave;
  250. #endif
  251. };
  252.  
  253.  
  254. /************************************************************************/
  255. /*                                                                        */
  256. /*    View Definition                                                        */
  257. /*                                                                        */
  258. /************************************************************************/
  259.  
  260. /*    XGSViewInitRecord
  261.  *
  262.  *        This is the initialization record for creating one of these
  263.  *    if it's programmatically built
  264.  */
  265.  
  266. struct XGSViewInitRecord {
  267.     /*
  268.      *    View specification
  269.      */
  270.     
  271.     long                    fViewType;
  272.     
  273.     short                    fViewID;
  274.     long                    fRefNum;
  275.     
  276.     bool                    fLockLeft;
  277.     bool                    fLockTop;
  278.     bool                    fLockRight;
  279.     bool                    fLockBottom;
  280.     
  281.     short                    fAutoLeft;
  282.     short                    fAutoTop;
  283.     short                    fAutoRight;
  284.     short                    fAutoBottom;
  285.  
  286.     bool                    fVisible;
  287.     bool                    fEnabled;
  288.     bool                    fTabStop;
  289. };
  290.  
  291. /*    XGView                                            [type 'VIEW']
  292.  *
  293.  *        This is the base class for defining a view object. This is the root
  294.  *    of all of my drawing handling and processing.
  295.  *
  296.  *        This is the workhorse class; this class handles encapsulating
  297.  *    the drawing subviews that I can draw into. How this is handled differs
  298.  *    between the Macintosh and Windows; the essence of this, however, is
  299.  *    that this class only specifies the boundary of the view--the
  300.  *    drawing is mediated by the XGDraw code.
  301.  *
  302.  *        On the Macintosh, the XGView only contains the geometry of the
  303.  *    area that this view covers. The XGDraw object then converts this
  304.  *    geometry into a drawing area that we can draw into.
  305.  *
  306.  *        On Windows, the XGView also contains the geometry of the HWND
  307.  *    child object this view contains--the XGDraw object simply opens the
  308.  *    HDC for this thing.
  309.  *
  310.  *        The essence of it, however, is that the drawing is all handled
  311.  *    by the XGDraw code; this manages the geometry.
  312.  */
  313.  
  314. class XGView : public XGDispatch {
  315.     public:
  316.         /*
  317.          *    Construction/Destruction
  318.          */
  319.         
  320.                                 XGView(long);            // Special-for windows
  321.                                 XGView(XGArgStream &);    // Special-for windows
  322.                                 XGView(XGView *parent, XGArgStream &init, bool nowin = false);
  323.                                 XGView(XGView *parent, XGSViewInitRecord &init, bool nowin = false);
  324.         virtual                    ~XGView();
  325.         
  326.         /*
  327.          *    Public event messages. Override these to handle this
  328.          *    view's behaviour.
  329.          */
  330.         
  331.         virtual void            DoDrawView(Rect);
  332.         virtual void            DoActivate(bool);
  333.         
  334.         virtual bool            DoMouseDown(Point,short);
  335.         virtual void            DoMouseMove(Point,short);
  336.         virtual void            DoMouseUp(Point,short);
  337.         
  338.         virtual void            DoSizeView(void);
  339.         virtual void            DoMoveView(void);
  340.         
  341.         /*
  342.          *    Focus support
  343.          */
  344.         
  345.         virtual XGDispatch        *SetFocus();
  346.         
  347.         /*
  348.          *    View display processing
  349.          */
  350.         
  351.         bool                    IsVisible() const        { return fVisible; }
  352.         virtual void            ShowView();
  353.         virtual void            HideView();
  354.         
  355.         bool                    IsEnabled() const        { return fEnabled; }
  356.         virtual void            EnableView();
  357.         virtual void            DisableView();
  358.         
  359.         bool                    IsActive() const;
  360.         bool                    IsCapture() const;
  361.         bool                    IsTabStop() const        { return fTabStop; }
  362.         
  363.         long                    GetRefNum() const        { return fRefNum; }
  364.         void                    SetRefNum(long x)        { fRefNum = x; }
  365.         
  366.         long                    GetViewType() const        { return fViewType; }
  367.         short                    GetViewID() const        { return fViewID; }
  368.         
  369.         /*
  370.          *    View hierarchy
  371.          */
  372.         
  373.         XGView                    *GetParent() const        { return fParent; }
  374.         XGView                    *GetChild() const        { return fChild; }
  375.         XGView                    *GetSibling() const        { return fSibling; }
  376.         XGWindow                *GetWindow() const        { return fRoot; }
  377.         XGDocument                *GetDocument() const;
  378.         
  379.         XGView                    *FindViewByID(short);
  380.         XGView                    *FindViewByPoint(Point);
  381.         
  382.         /*
  383.          *    View scrolling. This exists independant of the drawing
  384.          *    object XGDraw, in order to handle scrolling a scrollbar
  385.          */
  386.          
  387.         virtual void            Scroll(long,long);
  388.         void                    ScrollView(long,long,Rect *r = NULL);
  389.         
  390.         /*
  391.          *    View location and view manipulation
  392.          */
  393.         
  394.         Point                    GetPos() const            { return fPos; }
  395.         Point                    GetSize() const            { return fSize; }
  396.         virtual Rect            GetContentRect() const;
  397.         
  398.         void                    SetLocation(Rect);
  399.         void                    AutoLocate(Rect);
  400.         
  401.         void                    GlobalToView(Point *p) const
  402.                                     {
  403.                                         p->h -= fGlobalPos.h;
  404.                                         p->v -= fGlobalPos.v;
  405.                                     }
  406.         void                    GlobalToView(Rect *r) const
  407.                                     {
  408.                                         r->left   -= fGlobalPos.h;
  409.                                         r->top    -= fGlobalPos.v;
  410.                                         r->right  -= fGlobalPos.h;
  411.                                         r->bottom -= fGlobalPos.v;
  412.                                     }
  413.         void                    ViewToGlobal(Point *p) const
  414.                                     {
  415.                                         p->h += fGlobalPos.h;
  416.                                         p->v += fGlobalPos.v;
  417.                                     }
  418.         void                    ViewToGlobal(Rect *r) const
  419.                                     {
  420.                                         r->left   += fGlobalPos.h;
  421.                                         r->top    += fGlobalPos.v;
  422.                                         r->right  += fGlobalPos.h;
  423.                                         r->bottom += fGlobalPos.v;
  424.                                     }
  425.         
  426.         /*
  427.          *    Drawing Support routines
  428.          */
  429.         
  430.         void                    InvalView(Rect *r = NULL);
  431.         void                    ValidView(Rect *r = NULL);
  432.         void                    UpdateView();
  433.  
  434.         /*
  435.          *    Internal support routines
  436.          */
  437.         
  438.         Rect                    _GetClipRect() const    { return fGlobalRect; }
  439.         
  440. #if OPT_WINOS == 1
  441.         HWND                    _CreateHWND(Rect);
  442.         HWND                    _GetHWND() const        { return fViewHWND; }
  443.         void                    _SetHWND(HWND w)        { fViewHWND = w; }
  444.         static XGView            *_GetXGView(HWND w);
  445. #endif
  446.  
  447. #if OPT_MACOS == 1
  448.         void                    _DrawViewHierarchy(Rect ginval);
  449.         void                    _ActivateViewHierarchy(bool);
  450.  
  451.         /*
  452.          *    Magic support
  453.          */
  454.         
  455.         void                    _PushDC();
  456.         void                    _PopDC();
  457.         Rect                    _CalcInval();
  458. #endif
  459.  
  460.     protected:
  461.         void                    ClearTabStop()
  462.                                     {
  463.                                         fTabStop = false;
  464.                                     }
  465.  
  466.     private:
  467.         /*
  468.          *    Initialization support
  469.          */
  470.         
  471.         void                    IInitView(XGView *parent, XGSViewInitRecord &, bool);
  472.         
  473.         /*
  474.          *    Resize support
  475.          */
  476.         
  477.         void                    CalcPos(void);
  478.         Rect                    CalcAutoLocate(Rect);
  479.         
  480.         /*
  481.          *    Basic hierarchy relationship pointers
  482.          */
  483.         
  484.         XGWindow                *fRoot;
  485.         XGView                    *fChild;
  486.         XGView                    *fSibling;
  487.         XGView                    *fParent;
  488.         
  489.         /*
  490.          *    Personal status
  491.          */
  492.         
  493.         bool                    fVisible;
  494.         bool                    fEnabled;
  495.         bool                    fTabStop;
  496.         bool                    fReserved;
  497.         
  498.         /*
  499.          *    View specification
  500.          */
  501.         
  502.         long                    fViewType;
  503.         short                    fViewID;
  504.         long                    fRefNum;
  505. #if OPT_WINOS == 1
  506.         HWND                    fViewHWND;
  507. #endif
  508.  
  509.         /*
  510.          *    View location
  511.          */
  512.         
  513.         Point                    fSize;                // Current size of me
  514.         Point                    fPos;                // Current pos in parent
  515.         Point                    fGlobalPos;            // Global pos in window
  516.         Rect                    fGlobalRect;        // Clipped view rectangle
  517.         
  518.         /*
  519.          *    Auto resize structures
  520.          */
  521.         
  522.         bool                    fLockLeft;
  523.         bool                    fLockTop;
  524.         bool                    fLockRight;
  525.         bool                    fLockBottom;
  526.         
  527.         short                    fAutoLeft;
  528.         short                    fAutoTop;
  529.         short                    fAutoRight;
  530.         short                    fAutoBottom;
  531.     
  532.     friend class XGWindow;
  533.     friend class XGDraw;
  534.  
  535. #if OPT_WINOS == 1
  536.     friend LRESULT CALLBACK _GSingleWindowProc(HWND w, UINT msg, WPARAM wp, LPARAM lp);
  537.     friend LRESULT CALLBACK _GViewWindowProc(HWND w, UINT msg, WPARAM wp, LPARAM lp);
  538. #endif
  539. };
  540.  
  541. #if defined(__MWERKS__)
  542.     #if defined(macintosh)
  543.         #pragma options align=reset
  544.     #endif
  545.     #if defined(__INTEL__)
  546.         #pragma pack(pop)
  547.     #endif
  548. #endif
  549.  
  550.  
  551. #endif // __XVIEW_H__
  552.