Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A WTL Grid

0.00/5 (No votes)
22 Jan 2002 1  
A WTL grid (really).

Sample Image - WtlGrid.gif

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.
Using it should be pretty straightforward:
#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 :

WtlGridValue

a cell value, stored as a CString.

WtlGridCell

a cell with a render mode.

WtlGridColumn

a column, with header, etc.

WtlGridColumns

a column collection, a vector of column.

WtlGridRow

a grid row, holding a vector of WtlGridCell.

WtlGridRows

a collection of rows.

WtlGrid

the control itself, containing columns and rows.

WtlGridWrapper

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...
I would be grateful to get comments and enhancement to this code, enjoy ...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here