A WTL Grid






3.79/5 (10 votes)
Oct 8, 2001
1 min read

186684

4838
A WTL grid (really).
Introduction
After using WTL for some time, I've developed the control that I miss the most for the kind of project that I do : A grid.
This one is written from scratch, derived from a CScrollWindowImpl
, it has the following features :
- Grid is a vector of vector of
CString
. - It has a optional Toolbar where to add buttons with tooltip for edition operation (see screenshot).
- Resizable column title
- In place edit using a edit box, a combo box or a check-box.
- Collapsible tree like behavior.
- Overwritable behavior (
BeforeEdit
,AfterEdit
,CellRendering
, etc) via a derivable listener class.
#include "WtlGrid.h" WtlGridWrapper grid; grid.Create(*this,rcDefault,NULL,WS_CHILD|WS_VISIBLE); grid.construct(); //setup grid.listener(new LevelListener); //add a listener to overwrite some behavior grid.vertical_delimiter(true); //show vertical lines grid.horizontal_delimiter(true); grid.column_header(true); //show header grid.toolbar(); //show toolbar grid.m_toolbar.AddButton(IDB_NEW,true,"New"); //resource ID and tooltip grid.m_toolbar.AddButton(IDB_DELETE,false,"Delete"); grid.m_toolbar.AddButton(IDB_UP,true,"Up"); grid.m_toolbar.AddButton(IDB_DOWN,true,"Down"); grid.m_toolbar.AddButton(IDB_TOP,true,"Top"); grid.m_toolbar.AddButton(IDB_BOTTOM,true,"Bottom"); grid.m_toolbar.enable_button(1,false); grid.header_height(25); grid.selection_mode(); //now add the columns (header,width,alignment,Resource // ID,render mode,edit mode,can grow, can be selected) grid.add_column("Name",-1,WtlDC::left,-1, WtlGridCell::rstring,WtlGridColumn::edit,true,true); grid.add_column("Label",-1,WtlDC::left,-1, WtlGridCell::rstring, WtlGridColumn::edit,true,true); grid.add_column("",25,WtlDC::left,IDB_VISIBLE, WtlGridCell::rcheck, WtlGridColumn::check,false, true,IDB_SELECTED); //to add rows, add a vector of cstring vector<CString> buf; buf.push_back("whatever"); ... grid->add_row(buf,lev);
The inner code is also designed to be easy to understand, you'll find the following classes :
|
a cell value, stored as a |
|
a cell with a render mode. |
|
a column, with header, etc. |
|
a column collection, a vector of column. |
|
a grid row, holding a vector of |
|
a collection of rows. |
|
the control itself, containing columns and rows. |
|
a wrapper control for the toolbar and the column header, this is the control that you include in your app. |
Some behavior, especially the way cell are rendered and edited can be changed by overwriting a listener class, eg:
class LevelListener : public WtlGridListener { public : LevelListener(); virtual bool after_edit(WtlGrid *grid, CString &old_value, int row_nb, int column_nb,void *user,bool add); virtual bool after_select(WtlGrid *grid,int row_nb,int column_nb,void *user); virtual bool toolbar_button(WtlGrid *grid,int button_nb,void *user); virtual bool header_select(WtlGrid *grid,int header_nb,void *user); virtual bool render(WtlGrid *grid,CDCHandle dc, CRect &rc, WtlGridRow *row,WtlGridCell *cell,void *user); virtual WtlGridColumn::e_edit_mode before_edit(WtlGrid *grid, int row_nb, int column_nb, void *user); };
What is missing:
- Obviously, the grid should be bindable to a database, and do it virtually (not loading all the records in memory), this will be my next move.
- A lot of features...