Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

csgrid.h

00001 /*
00002     Crystal Space Windowing System : grid class
00003     Copyright (C) 2000 by Norman Krämer <normank@lycosmail.com>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CSWSGRID_H__
00021 #define __CSWSGRID_H__
00022 
00023 #include "csws/csscrbar.h"
00024 #include "csutil/csvector.h"
00025 #include "csutil/csstring.h"
00026 
00039 typedef bool (*csRegionTreeFunc) (csSome node, csSome databag);
00040 
00041 class csRegionTree2D;
00042 class csSparseGrid;
00043 class csGridCell;
00044 class csGridView;
00045 class csGrid;
00046 class csSplitter;
00047 
00048 class csRegionTree2D
00049 {
00050 public:
00051   csRect region;
00052   csRegionTree2D *children[5]; // max. 5 children possible
00053   csSome data;
00054 
00055 public:
00057   csRegionTree2D ();
00059   csRegionTree2D (csRect area, csSome data);
00061   ~csRegionTree2D ();
00062 
00066   void Insert (csRect &area, csSome data);
00067 
00071   void FindRegion (const csRect &area, csVector &vLeafList);
00072 
00076   void Traverse (csRegionTreeFunc userFunc, csSome databag = NULL);
00077 
00078 };
00079 
00084 class csSparseGrid
00085 {
00086   friend class csGrid;
00087   /*
00088    * A single entry in the "grid row" array.
00089    */
00090   struct csGridRowEntry
00091   {
00092     int col;
00093     csSome data;
00094     // Initialize the object with given column and associated data
00095     csGridRowEntry (int theCol, csSome theData) : col (theCol), data (theData) {}
00096   };
00097 
00098   /*
00099    * A "grid row" is a horizontal stripe of cells which makes up the
00100    * entire grid. Every data item in this csVector is a csGridRowEntry.
00101    * The grid row object does not contain all the cells as separate objects;
00102    * this would waste too much memory. Instead, we keep only those cell
00103    * objects which have associated data items. The cells are kept sorted
00104    * by column number for faster searching.
00105    */
00106   class csGridRow : public csVector
00107   {
00108     int col;
00109   public:
00110     // Initialize the object
00111     csGridRow (int theCol);
00112     // Destroy the object
00113     virtual ~csGridRow ();
00114     // Set the data at given column
00115     void SetAt (int col, csSome data);
00116     // Get the row entry at given column
00117     csGridRowEntry *Get (int index);
00118     // Compare two row entries
00119     virtual int Compare (csSome Item1, csSome Item2, int Mode) const;
00120     // Compare a row entry with a key
00121     virtual int CompareKey (csSome Item1, csConstSome Key, int Mode) const;
00122     // Free a row entry item
00123     virtual bool FreeItem (csSome Item);
00124   };
00125   friend class csSparseGrid::csGridRow;
00126 
00127   /*
00128    * A "grid row set" is an array of "grid rows",
00129    * e.g. this is the grid itself.
00130    */
00131   class csGridRowSet : public csGridRow
00132   {
00133   public:
00134     // Initialize the grid row set object
00135     csGridRowSet (int theRow) : csGridRow (theRow) {}
00136     // destructor
00137     virtual ~csGridRowSet () {DeleteAll ();}
00138     // Free a particular grid row object
00139     virtual bool FreeItem (csSome Item)
00140     {
00141       delete (csGridRow *)((csGridRowEntry *)Item)->data;
00142       delete (csGridRowEntry *)Item;
00143       return true;
00144     }
00145   };
00146 
00147   // The Grid (AKA The Matrix :)
00148   csGridRowSet rows;
00149 
00150 public:
00152   csSparseGrid () : rows (8) {}
00153 
00155   csSome GetAt (int row, int col)
00156   {
00157     csSome result = NULL;
00158     int idx1 = rows.FindSortedKey ((csConstSome)row);
00159     if (idx1 != -1)
00160     {
00161       int idx2 = ((csGridRow *)rows.Get (idx1)->data)->FindSortedKey ((csConstSome)col);
00162       if (idx2 != -1)
00163         result = ((csGridRow *)rows.Get (idx1)->data)->Get (idx2)->data;
00164     }
00165     return result;
00166   }
00167 
00168   // Set the data at given row/column
00169   void SetAt (int row, int col, csSome data)
00170   {
00171     int idx = rows.FindSortedKey ((csConstSome)row);
00172     if (idx == -1)
00173       idx = rows.InsertSorted (new csGridRowEntry (row, new csGridRow (row)));
00174     ((csGridRow *)rows.Get (idx)->data)->SetAt (col, data);
00175   }
00176 };
00177 
00181 enum csGridCellBorderStyle
00182 {
00184   gcbsNone = 0,
00186   gcbsDash,
00188   gcbsDashPoint,
00190   gcbsDashPointPoint,
00192   gcbsDashDashPoint,
00194   gcbsLine
00195 };
00196 
00198 #define CSS_GRIDCELL_SELECTED        0x00010000
00199 
00205 class csGridCell : public csComponent
00206 {
00208   class csCellBorder
00209   {
00210   public:
00212     csGridCellBorderStyle style;
00214     int thick;
00216     csCellBorder () : style (gcbsLine), thick (1) {}
00217   };
00218 
00220   bool inUse;
00221 
00222 public:
00224   csCellBorder upper, lower, left, right;
00226   int row, col;
00228   csSome data;
00230   csString valuePattern;
00231 
00233   csGridCell ();
00235   virtual void Draw ();
00237   bool IsUsed () { return inUse; }
00239   void SetUsed (bool iState = true) { inUse = iState; }
00240 
00241 protected:
00243   void DrawLine (int x1, int y1, int x2, int y2, csCellBorder &border);
00244 };
00245 
00246 
00251 
00252 #define CSGVS_HSCROLL  0x00000001
00253 
00254 #define CSGVS_VSCROLL  0x00000002
00255 
00256 #define CSGVS_DEFAULTVALUE (CSGVS_HSCROLL | CSGVS_VSCROLL)
00257 
00264 class csGridView : public csComponent
00265 {
00266 protected:
00268   csRect area;
00270   csGrid *pGrid;
00272   int row, col;
00274   bool fPlaceItems;
00276   int Style;
00278   csScrollBar *hscroll, *vscroll;
00279 
00281   void CooAt (int theX, int theY, int &theRow, int &theCol);
00282 
00283 public:
00289   float areafactor;
00290 
00292   csGridView (csGrid *pParent, const csRect &region,
00293     int iStyle = CSGVS_DEFAULTVALUE);
00295   csGridView (const csGridView &view, int iStyle = -1);
00296 
00298   virtual void Draw ();
00300   virtual bool HandleEvent (iEvent& Event);
00302   virtual bool SetRect (int xmin, int ymin, int xmax, int ymax);
00304   const csRect& GetArea (){return area;}
00306   virtual void FixSize (int &newW, int &newH);
00308   virtual void SuggestSize (int &w, int &h);
00309 
00314   csGridView *SplitX (int x, int iStyle = -1);
00319   csGridView *SplitY (int y, int iStyle = -1);
00320 
00324   void SetViewArea (const csRect& rc)
00325   {
00326     area.Set (rc.xmin, rc.ymin, rc.xmax, rc.ymax);
00327     col = area.xmin; row = area.ymin;
00328   }
00329 
00330 protected:
00334   virtual csGridView *CreateCopy (int iStyle);
00338   void PlaceItems ();
00339 };
00340 
00348 
00349 #define CSGS_HSPLIT             0x00000004
00350 
00351 #define CSGS_VSPLIT             0x00000008
00352 
00353 #define CSGS_DEFAULTVALUE       (CSGS_HSPLIT | CSGS_VSPLIT)
00354 
00356 #define CSGCS_NONE   1
00357 #define CSGCS_CELL   2
00358 #define CSGCS_ROW    3
00359 #define CSGCS_COLUMN 4
00360 
00361 enum
00362 {
00367   cscmdGridCursorChanged = 0x00000F00
00368 };
00369 
00377 class csGrid : public csComponent
00378 {
00379 protected:
00380   friend class csGridView;
00382   csRegionTree2D *regions, *viewlayout;
00384   csSparseGrid *grid;
00386   csVector vViews;
00388   csGridView *activeView;
00390   csVector vRegionStyles;
00392   csSplitter *splitterX, *splitterY;
00394   int cursorStyle;
00396   int xcur, ycur;
00397 
00399   void CalcMinimalSize (csRegionTree2D *node, int &w, int &h);
00401   void PlaceGadgets ();
00402 
00403 private:
00404   // Common method for constructors
00405   void init (csComponent *pParent, csRect &rc, int iStyle, csGridCell *gc);
00406 
00407 public:
00409   csGrid (csComponent *pParent, int nRows, int nCols,
00410     int iStyle = CSGS_DEFAULTVALUE | CSGVS_DEFAULTVALUE);
00412   csGrid (csComponent *pParent, int nRows, int nCols, csGridCell *gridpattern,
00413    int iStyle = CSGS_DEFAULTVALUE | CSGVS_DEFAULTVALUE);
00415   virtual ~csGrid ();
00416 
00418   virtual void SetCursorStyle (int iCursorStyle = CSGCS_NONE);
00420   virtual int GetCursorStyle ();
00422   virtual void GetCursorPos (int &row, int &col);
00424   virtual void SetCursorPos (int row, int col);
00425 
00427   virtual void Draw ();
00429   virtual bool SetRect (int xmin, int ymin, int xmax, int ymax);
00431   virtual void FixSize (int &newW, int &newH);
00433   virtual void SuggestSize (int &w, int &h);
00435   virtual bool HandleEvent (iEvent &Event);
00436 
00438   void CreateRegion (csRect& rc, csGridCell *cell);
00440   csGridView* GetRootView ()
00441   { return (csGridView*)vViews.Get (0); }
00443   csGridView *GetActiveView () {return activeView;}
00445   void SetActiveView (csGridView *view);
00446 
00450   virtual void SetStringAt (int row, int col, const char *data);
00451   csString *GetStringAt (int row, int col);
00452 };
00453 
00454 #endif

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000