csws/csgrid.h
Go to the documentation of this file.00001 /* 00002 Crystal Space Windowing System : grid class 00003 Copyright (C) 2000 by Norman Kraemer <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 __CS_CSGRID_H__ 00021 #define __CS_CSGRID_H__ 00022 00031 #include "csws/csscrbar.h" 00032 #include "csutil/array.h" 00033 #include "csutil/parray.h" 00034 #include "csutil/csstring.h" 00035 00048 typedef bool (*csRegionTreeFunc) (void* node, void* databag); 00049 00050 class csRegionTree2D; 00051 class csSparseGrid; 00052 class csGridCell; 00053 class csGridView; 00054 class csGrid; 00055 class csSplitter; 00056 00057 class csRegionTree2D 00058 { 00059 public: 00060 csRect region; 00061 csRegionTree2D *children[5]; // max. 5 children possible 00062 void* data; 00063 00064 public: 00066 csRegionTree2D (); 00068 csRegionTree2D (csRect area, void* data); 00070 ~csRegionTree2D (); 00071 00075 void Insert (csRect &area, void* data); 00076 00080 void FindRegion (const csRect &area, csArray<csRegionTree2D*> &vLeafList); 00081 00085 void Traverse (csRegionTreeFunc userFunc, void* databag = 0); 00086 00087 }; 00088 00093 class csSparseGrid 00094 { 00095 friend class csGrid; 00096 /* 00097 * A single entry in the "grid row" array. 00098 */ 00099 struct csGridRowEntry 00100 { 00101 int col; 00102 void* data; 00103 // Initialize the object with given column and associated data 00104 csGridRowEntry (int theCol, void* theData) : col (theCol), data (theData) {} 00105 }; 00106 00107 /* 00108 * A "grid row" is a horizontal stripe of cells which makes up the 00109 * entire grid. Every data item in this array is a csGridRowEntry. 00110 * The grid row object does not contain all the cells as separate objects; 00111 * this would waste too much memory. Instead, we keep only those cell 00112 * objects which have associated data items. The cells are kept sorted 00113 * by column number for faster searching. 00114 */ 00115 class csGridRow : public csPDelArray<csGridRowEntry> 00116 { 00117 int col; 00118 public: 00119 // Initialize the object 00120 csGridRow (int theCol); 00121 // Set the data at given column 00122 void SetAt (int col, void* data); 00123 // Compare two row entries 00124 static int Compare (csGridRowEntry* const& Item1, 00125 csGridRowEntry* const& Item2); 00126 // Compare a row entry with a key 00127 static int CompareKey (csGridRowEntry* const& Item1, void* Key); 00128 }; 00129 friend class csSparseGrid::csGridRow; 00130 00131 /* 00132 * A "grid row set" is an array of "grid rows", 00133 * e.g. this is the grid itself. 00134 */ 00135 class csGridRowSet : public csGridRow 00136 { 00137 public: 00138 // Initialize the grid row set object 00139 csGridRowSet (int theRow) : csGridRow (theRow) {} 00140 }; 00141 00142 // The Grid (AKA The Matrix :) 00143 csGridRowSet rows; 00144 00145 public: 00147 csSparseGrid () : rows (8) {} 00148 00150 void* GetAt (int row, int col) 00151 { 00152 void* result = 0; 00153 int idx1 = rows.FindSortedKey ((void*)row, rows.CompareKey); 00154 if (idx1 != -1) 00155 { 00156 int idx2 = ((csGridRow *)rows.Get (idx1)->data)->FindSortedKey ( 00157 (void*)col, rows.CompareKey); 00158 if (idx2 != -1) 00159 result = ((csGridRow *)rows.Get (idx1)->data)->Get (idx2)->data; 00160 } 00161 return result; 00162 } 00163 00164 // Set the data at given row/column 00165 void SetAt (int row, int col, void* data) 00166 { 00167 int idx = rows.FindSortedKey ((void*)row, rows.CompareKey); 00168 if (idx == -1) 00169 idx = rows.InsertSorted (new csGridRowEntry (row, new csGridRow (row)), 00170 rows.Compare); 00171 ((csGridRow *)rows.Get (idx)->data)->SetAt (col, data); 00172 } 00173 }; 00174 00178 enum csGridCellBorderStyle 00179 { 00181 gcbsNone = 0, 00183 gcbsDash, 00185 gcbsDashPoint, 00187 gcbsDashPointPoint, 00189 gcbsDashDashPoint, 00191 gcbsLine 00192 }; 00193 00195 #define CSS_GRIDCELL_SELECTED 0x00010000 00196 00202 class csGridCell : public csComponent 00203 { 00205 class csCellBorder 00206 { 00207 public: 00209 csGridCellBorderStyle style; 00211 int thick; 00213 csCellBorder () : style (gcbsLine), thick (1) {} 00214 }; 00215 00217 bool inUse; 00218 00219 public: 00221 csCellBorder upper, lower, left, right; 00223 int row, col; 00225 void* data; 00227 csString valuePattern; 00228 00230 csGridCell (); 00232 virtual void Draw (); 00234 bool IsUsed () { return inUse; } 00236 void SetUsed (bool iState = true) { inUse = iState; } 00237 00238 protected: 00240 void DrawLine (int x1, int y1, int x2, int y2, csCellBorder &border); 00241 }; 00242 00243 00247 00248 #define CSGVS_HSCROLL 0x00000001 00249 00250 #define CSGVS_VSCROLL 0x00000002 00251 00252 #define CSGVS_DEFAULTVALUE (CSGVS_HSCROLL | CSGVS_VSCROLL) 00253 00261 class csGridView : public csComponent 00262 { 00263 protected: 00265 csRect area; 00267 csGrid *pGrid; 00269 int row, col; 00271 bool fPlaceItems; 00273 int Style; 00275 csScrollBar *hscroll, *vscroll; 00276 00278 void CooAt (int theX, int theY, int &theRow, int &theCol); 00279 00280 public: 00286 float areafactor; 00287 00289 csGridView (csGrid *pParent, const csRect ®ion, 00290 int iStyle = CSGVS_DEFAULTVALUE); 00292 csGridView (const csGridView &view, int iStyle = -1); 00293 00295 virtual void Draw (); 00297 virtual bool HandleEvent (iEvent& Event); 00299 virtual bool SetRect (int xmin, int ymin, int xmax, int ymax); 00301 const csRect& GetArea (){return area;} 00303 virtual void FixSize (int &newW, int &newH); 00305 virtual void SuggestSize (int &w, int &h); 00306 00311 csGridView *SplitX (int x, int iStyle = -1); 00316 csGridView *SplitY (int y, int iStyle = -1); 00317 00321 void SetViewArea (const csRect& rc) 00322 { 00323 area.Set (rc.xmin, rc.ymin, rc.xmax, rc.ymax); 00324 col = area.xmin; row = area.ymin; 00325 } 00326 00327 protected: 00331 virtual csGridView *CreateCopy (int iStyle); 00335 void PlaceItems (); 00336 }; 00337 00345 00346 #define CSGS_HSPLIT 0x00000004 00347 00348 #define CSGS_VSPLIT 0x00000008 00349 00350 #define CSGS_DEFAULTVALUE (CSGS_HSPLIT | CSGS_VSPLIT) 00351 00353 #define CSGCS_NONE 1 00354 00355 #define CSGCS_CELL 2 00356 00357 #define CSGCS_ROW 3 00358 00359 #define CSGCS_COLUMN 4 00360 00362 00363 enum 00364 { 00369 cscmdGridCursorChanged = 0x00000F00 00370 }; 00371 00379 class csGrid : public csComponent 00380 { 00381 protected: 00382 friend class csGridView; 00384 csRegionTree2D *regions, *viewlayout; 00386 csSparseGrid *grid; 00388 csArray<csGridView*> vViews; 00390 csGridView *activeView; 00392 csArray<csGridCell*> vRegionStyles; 00394 csSplitter *splitterX, *splitterY; 00396 int cursorStyle; 00398 int xcur, ycur; 00399 00401 void CalcMinimalSize (csRegionTree2D *node, int &w, int &h); 00403 void PlaceGadgets (); 00404 00405 private: 00407 void init (csComponent *pParent, csRect &rc, int iStyle, csGridCell *gc); 00408 00409 public: 00411 csGrid (csComponent *pParent, int nRows, int nCols, 00412 int iStyle = CSGS_DEFAULTVALUE | CSGVS_DEFAULTVALUE); 00414 csGrid (csComponent *pParent, int nRows, int nCols, csGridCell *gridpattern, 00415 int iStyle = CSGS_DEFAULTVALUE | CSGVS_DEFAULTVALUE); 00417 virtual ~csGrid (); 00418 00420 virtual void SetCursorStyle (int iCursorStyle = CSGCS_NONE); 00422 virtual int GetCursorStyle (); 00424 virtual void GetCursorPos (int &row, int &col); 00426 virtual void SetCursorPos (int row, int col); 00427 00429 virtual void Draw (); 00431 virtual bool SetRect (int xmin, int ymin, int xmax, int ymax); 00433 virtual void FixSize (int &newW, int &newH); 00435 virtual void SuggestSize (int &w, int &h); 00437 virtual bool HandleEvent (iEvent &Event); 00438 00440 void CreateRegion (csRect& rc, csGridCell *cell); 00442 csGridView* GetRootView () 00443 { return (csGridView*)vViews.Get (0); } 00445 csGridView *GetActiveView () {return activeView;} 00447 void SetActiveView (csGridView *view); 00448 00452 virtual void SetStringAt (int row, int col, const char *data); 00453 csString *GetStringAt (int row, int col); 00454 }; 00455 00458 #endif // __CS_CSGRID_H__
Generated for Crystal Space by doxygen 1.2.18