Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WTL
Article

A WTL Grid

Rate me:
Please Sign up or sign in to vote.
3.79/5 (11 votes)
22 Jan 20021 min read 183.4K   4.8K   52   28
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


Written By
Architect strategis
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalbug in header Pin
RiderV30-Dec-10 6:34
RiderV30-Dec-10 6:34 
GeneralDisable context menu Pin
amarbabu27-Sep-06 7:30
amarbabu27-Sep-06 7:30 
GeneralDO NOT WRITE YOUR CODE THIS WAY!!! Pin
mloskot14-Jun-05 12:14
mloskot14-Jun-05 12:14 
GeneralRe: DO NOT WRITE YOUR CODE THIS WAY!!! Pin
Noel Frankinet14-Jun-05 21:29
Noel Frankinet14-Jun-05 21:29 
GeneralRe: DO NOT WRITE YOUR CODE THIS WAY!!! Pin
mloskot15-Jun-05 12:51
mloskot15-Jun-05 12:51 
GeneralI wanna to put together cells Pin
i4uman14-Apr-05 20:43
i4uman14-Apr-05 20:43 
GeneralPretty code... Pin
volmax24-Feb-04 1:57
volmax24-Feb-04 1:57 
General&amp;#65311;&amp;#65311;&amp;#65311; Collapsible tree like behavior. Pin
newkey6-Feb-04 20:56
newkey6-Feb-04 20:56 
Generalthis needs work Pin
Eugene Polonsky26-Oct-02 18:41
Eugene Polonsky26-Oct-02 18:41 
Ok, first off, thanks for submitting this article -- I think the intent is good, and I do appreciate it.

However, there are several problems here that should be addressed:

1. Lack of comments. If you're submitting code to a public website, please please please comment it, so you don't force developers to pour over your (excuse the criticism) somewhat poor design.

2. Bugs. There are some really obvious ones -- even in your demo app, the toolbar has no buttons, and leaves draw traces when resized. You must have noticed this when testing... why not fix?
The problem another guy here mentioned with inserting your class into a dialog is also a bug: in the construct() function, you take rcDefault, and then expect an OnSize call to actually size your inner window. In a dialog, you won't get an OnSize call if the window is created with the correct size in the first place -- once again, easily fixed, but annoying as hell.

3. Examples. You provide no examples of how to implement the tree functionality in your class, and it's much less than obvious how to do that -- after messing around with your class for awhile I finally gave up, exactly due to this point. If you submit a demo for an article, at least make sure the demo covers all aspects of your class.

4. Design. It would've been nice (and user-friendly) to nest all your internal classes within WtlGridWrapper class, so they don't clutter up the ClassView window of the IDE. One tiny change, but really helpful.
If you work with WTL, you should follow WTL class naming conventions, such as putting a C in front of your class names, and using standard function and variable names. (Get/Set, etc)

Anyway, once again, I do appreciate the work that went into making this, but I think attention to small details (and COMMENTS!) is really important when submitting code to a public web site.
Big Grin | :-D

----------------------------------------
----I said my name wasn't important
---------------------------SlartiBartFast
GeneralRe: this needs work Pin
roel_6-Mar-03 0:41
roel_6-Mar-03 0:41 
GeneralRe: this needs work Pin
mloskot14-Jun-05 7:41
mloskot14-Jun-05 7:41 
Generalhi ,test Pin
DeXian2-Jun-02 17:34
DeXian2-Jun-02 17:34 
GeneralRe: hi ,test Pin
kamal_upreti17-Feb-03 20:09
kamal_upreti17-Feb-03 20:09 
GeneralExample source Pin
Keith Holme29-May-02 2:39
Keith Holme29-May-02 2:39 
QuestionWM_MOUSEWHEEL ?? Pin
25-Feb-02 18:40
suss25-Feb-02 18:40 
QuestionAnd how do I get it to show up in a dialog? Pin
NMTop4011-Feb-02 1:23
NMTop4011-Feb-02 1:23 
GeneralAnd some major issues Pin
NMTop407-Feb-02 3:21
NMTop407-Feb-02 3:21 
GeneralRe: And some major issues Pin
7-Feb-02 3:37
suss7-Feb-02 3:37 
GeneralRe: And some major issues Pin
NMTop407-Feb-02 6:03
NMTop407-Feb-02 6:03 
GeneralRe: some major issues in thinking Pin
sixth_voice1-Oct-03 3:42
susssixth_voice1-Oct-03 3:42 
GeneralRe: some major issues in thinking Pin
MoonlightM27-May-04 3:52
MoonlightM27-May-04 3:52 
GeneralA few suggested modifications Pin
6-Feb-02 1:36
suss6-Feb-02 1:36 
GeneralRe: A few suggested modifications Pin
Justicemonger31-Oct-03 3:38
Justicemonger31-Oct-03 3:38 
GeneralRedraw problem with example Pin
Parampreet Sidhu1-Feb-02 4:16
Parampreet Sidhu1-Feb-02 4:16 
GeneralRe: Redraw problem with example Pin
NMTop4011-Feb-02 3:25
NMTop4011-Feb-02 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.