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 "csextern.h" 00032 00033 #include "csws/csscrbar.h" 00034 #include "csutil/array.h" 00035 #include "csutil/parray.h" 00036 #include "csutil/csstring.h" 00037 00050 typedef bool (*csRegionTreeFunc) (void* node, void* databag); 00051 00052 class csRegionTree2D; 00053 class csSparseGrid; 00054 class csGridCell; 00055 class csGridView; 00056 class csGrid; 00057 class csSplitter; 00058 00059 class CS_CSWS_EXPORT csRegionTree2D 00060 { 00061 public: 00062 csRect region; 00063 csRegionTree2D *children[5]; // max. 5 children possible 00064 void* data; 00065 00066 public: 00068 csRegionTree2D (); 00070 csRegionTree2D (csRect area, void* data); 00072 ~csRegionTree2D (); 00073 00077 void Insert (csRect &area, void* data); 00078 00082 void FindRegion (const csRect &area, csArray<csRegionTree2D*> &vLeafList); 00083 00087 void Traverse (csRegionTreeFunc userFunc, void* databag = 0); 00088 00089 }; 00090 00095 class csSparseGrid 00096 { 00097 friend class csGrid; 00098 /* 00099 * A single entry in the "grid row" array. 00100 */ 00101 struct csGridRowEntry 00102 { 00103 int col; 00104 void* data; 00105 // Initialize the object with given column and associated data 00106 csGridRowEntry (int theCol, void* theData) : col (theCol), data (theData) {} 00107 }; 00108 00109 /* 00110 * A "grid row" is a horizontal stripe of cells which makes up the 00111 * entire grid. Every data item in this array is a csGridRowEntry. 00112 * The grid row object does not contain all the cells as separate objects; 00113 * this would waste too much memory. Instead, we keep only those cell 00114 * objects which have associated data items. The cells are kept sorted 00115 * by column number for faster searching. 00116 */ 00117 class csGridRow : public csPDelArray<csGridRowEntry> 00118 { 00119 int col; 00120 public: 00121 // Initialize the object 00122 csGridRow (int theCol); 00123 // Set the data at given column 00124 void SetAt (int col, void* data); 00125 // Compare two row entries 00126 static int Compare (csGridRowEntry* const&, csGridRowEntry* const&); 00127 // Compare a row entry with a key 00128 static int CompareKey (csGridRowEntry* const&, int const& Key); 00129 // Functor wrapping CompareKey() for a given number. 00130 static csArrayCmp<csGridRowEntry*,int> KeyCmp(int n) 00131 { return csArrayCmp<csGridRowEntry*,int>(n, CompareKey); } 00132 }; 00133 friend class csSparseGrid::csGridRow; 00134 00135 /* 00136 * A "grid row set" is an array of "grid rows", 00137 * e.g. this is the grid itself. 00138 */ 00139 class csGridRowSet : public csGridRow 00140 { 00141 public: 00142 // Initialize the grid row set object 00143 csGridRowSet (int theRow) : csGridRow (theRow) {} 00144 }; 00145 00146 // The Grid (AKA The Matrix :) 00147 csGridRowSet rows; 00148 00149 public: 00151 csSparseGrid () : rows (8) {} 00152 00154 void* GetAt (int row, int col) 00155 { 00156 void* result = 0; 00157 int idx1 = rows.FindSortedKey (rows.KeyCmp(row)); 00158 if (idx1 != -1) 00159 { 00160 int idx2 = ((csGridRow *)rows.Get (idx1)->data)->FindSortedKey ( 00161 rows.KeyCmp(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, void* data) 00170 { 00171 int idx = rows.FindSortedKey (rows.KeyCmp(row)); 00172 if (idx == -1) 00173 idx = rows.InsertSorted (new csGridRowEntry (row, new csGridRow (row)), 00174 rows.Compare); 00175 ((csGridRow *)rows.Get (idx)->data)->SetAt (col, data); 00176 } 00177 }; 00178 00182 enum csGridCellBorderStyle 00183 { 00185 gcbsNone = 0, 00187 gcbsDash, 00189 gcbsDashPoint, 00191 gcbsDashPointPoint, 00193 gcbsDashDashPoint, 00195 gcbsLine 00196 }; 00197 00199 #define CSS_GRIDCELL_SELECTED 0x00010000 00200 00206 class CS_CSWS_EXPORT csGridCell : public csComponent 00207 { 00209 class csCellBorder 00210 { 00211 public: 00213 csGridCellBorderStyle style; 00215 int thick; 00217 csCellBorder () : style (gcbsLine), thick (1) {} 00218 }; 00219 00221 bool inUse; 00222 00223 public: 00225 csCellBorder upper, lower, left, right; 00227 int row, col; 00229 void* data; 00231 csString valuePattern; 00232 00234 csGridCell (); 00236 virtual void Draw (); 00238 bool IsUsed () { return inUse; } 00240 void SetUsed (bool iState = true) { inUse = iState; } 00241 00242 protected: 00244 void DrawLine (int x1, int y1, int x2, int y2, csCellBorder &border); 00245 }; 00246 00247 00251 00252 #define CSGVS_HSCROLL 0x00000001 00253 00254 #define CSGVS_VSCROLL 0x00000002 00255 00256 #define CSGVS_DEFAULTVALUE (CSGVS_HSCROLL | CSGVS_VSCROLL) 00257 00265 class CS_CSWS_EXPORT csGridView : public csComponent 00266 { 00267 protected: 00269 csRect area; 00271 csGrid *pGrid; 00273 int row, col; 00275 bool fPlaceItems; 00277 int Style; 00279 csScrollBar *hscroll, *vscroll; 00280 00282 void CooAt (int theX, int theY, int &theRow, int &theCol); 00283 00284 public: 00290 float areafactor; 00291 00293 csGridView (csGrid *pParent, const csRect ®ion, 00294 int iStyle = CSGVS_DEFAULTVALUE); 00296 csGridView (const csGridView &view, int iStyle = -1); 00297 00299 virtual void Draw (); 00301 virtual bool HandleEvent (iEvent& Event); 00303 virtual bool SetRect (int xmin, int ymin, int xmax, int ymax); 00305 const csRect& GetArea (){return area;} 00307 virtual void FixSize (int &newW, int &newH); 00309 virtual void SuggestSize (int &w, int &h); 00310 00315 csGridView *SplitX (int x, int iStyle = -1); 00320 csGridView *SplitY (int y, int iStyle = -1); 00321 00325 void SetViewArea (const csRect& rc) 00326 { 00327 area.Set (rc.xmin, rc.ymin, rc.xmax, rc.ymax); 00328 col = area.xmin; row = area.ymin; 00329 } 00330 00331 protected: 00335 virtual csGridView *CreateCopy (int iStyle); 00339 void PlaceItems (); 00340 }; 00341 00349 00350 #define CSGS_HSPLIT 0x00000004 00351 00352 #define CSGS_VSPLIT 0x00000008 00353 00354 #define CSGS_DEFAULTVALUE (CSGS_HSPLIT | CSGS_VSPLIT) 00355 00357 #define CSGCS_NONE 1 00358 00359 #define CSGCS_CELL 2 00360 00361 #define CSGCS_ROW 3 00362 00363 #define CSGCS_COLUMN 4 00364 00366 00367 enum 00368 { 00373 cscmdGridCursorChanged = 0x00000F00 00374 }; 00375 00383 class CS_CSWS_EXPORT csGrid : public csComponent 00384 { 00385 protected: 00386 friend class csGridView; 00388 csRegionTree2D *regions, *viewlayout; 00390 csSparseGrid *grid; 00392 csArray<csGridView*> vViews; 00394 csGridView *activeView; 00396 csArray<csGridCell*> vRegionStyles; 00398 csSplitter *splitterX, *splitterY; 00400 int cursorStyle; 00402 int xcur, ycur; 00403 00405 void CalcMinimalSize (csRegionTree2D *node, int &w, int &h); 00407 void PlaceGadgets (); 00408 00409 private: 00411 void init (csComponent *pParent, csRect &rc, int iStyle, csGridCell *gc); 00412 00413 public: 00415 csGrid (csComponent *pParent, int nRows, int nCols, 00416 int iStyle = CSGS_DEFAULTVALUE | CSGVS_DEFAULTVALUE); 00418 csGrid (csComponent *pParent, int nRows, int nCols, csGridCell *gridpattern, 00419 int iStyle = CSGS_DEFAULTVALUE | CSGVS_DEFAULTVALUE); 00421 virtual ~csGrid (); 00422 00424 virtual void SetCursorStyle (int iCursorStyle = CSGCS_NONE); 00426 virtual int GetCursorStyle (); 00428 virtual void GetCursorPos (int &row, int &col); 00430 virtual void SetCursorPos (int row, int col); 00431 00433 virtual void Draw (); 00435 virtual bool SetRect (int xmin, int ymin, int xmax, int ymax); 00437 virtual void FixSize (int &newW, int &newH); 00439 virtual void SuggestSize (int &w, int &h); 00441 virtual bool HandleEvent (iEvent &Event); 00442 00444 void CreateRegion (csRect& rc, csGridCell *cell); 00446 csGridView* GetRootView () 00447 { return (csGridView*)vViews.Get (0); } 00449 csGridView *GetActiveView () {return activeView;} 00451 void SetActiveView (csGridView *view); 00452 00456 virtual void SetStringAt (int row, int col, const char *data); 00457 csString *GetStringAt (int row, int col); 00458 }; 00459 00462 #endif // __CS_CSGRID_H__
Generated for Crystal Space by doxygen 1.2.18