Tutorial 8 - Simple Table

This tutorial introduces tables with the "simpletable" program. The program consists of a window with a table containing a single column of numbers. This program is made up of five files - the source and header for the object that contains the window, the source and header for the simpletable widget and the source that contains the function main.

The SimpleTable object - SimpleTable.cc and SimpleTable.h

The object that implements printing is called SimpleTable and is derived from JXScrollableWidget

SimpleTable source file:

/******************************************************************************
 SimpleTable.cc

    This is a basic table. All of the data is hard coded, so it can't be
    changed in any way. The purpose of this table is demonstrate how tables
    are created and drawn.

    BASE CLASS = JXTable

    Written by Glenn Bach - 1998.

 ******************************************************************************/

#include "SimpleTable.h"
#include <JXWindowPainter.h>
#include <JXColormap.h>
#include <JString.h>
#include <jXPainterUtil.h>
#include <jXConstants.h>
#include <jAssert.h>

// The default row height and column width.
const JCoordinate kDefRowHeight    = 20;
const JCoordinate kDefColWidth    = 80;

/******************************************************************************
 Constructor

 ******************************************************************************/

SimpleTable::SimpleTable
    (
    JXScrollbarSet*     scrollbarSet,
    JXContainer*         enclosure,
    const HSizingOption hSizing,
    const VSizingOption vSizing,
    const JCoordinate     x,
    const JCoordinate     y,
    const JCoordinate     w,
    const JCoordinate     h
    )
    :
    JXTable(kDefRowHeight, kDefColWidth, scrollbarSet, enclosure, hSizing, vSizing, x, y, w, h)
{
    // We arbitrarily choose to have one column and 10 rows.
    AppendCol(kDefColWidth);
    for (JSize i = 1; i <= 10; i++)
        {
        AppendRow(kDefRowHeight);
        }
}

/******************************************************************************
 Destructor

 ******************************************************************************/

SimpleTable::~SimpleTable()
{
}

/******************************************************************************
 TableDrawCell

    This gets called by JTable every time the cell passed to
    the function needs to be redrawn.

    p is the Painter that you use to draw to the screen.  The main reason
    for Painter is to hide the system dependent details of drawing,
    and to provide a uniform interface for drawing to the screen, to an
    offscreen image, and to a printer.

    rect is the boundary of the cell.  The clipping rectangle has been set
    set to this so anything that you draw outside this rectangle will not
    be displayed.

    cell gives the row and column of the cell that needs to be redrawn.

    cell.x = column
    cell.y = row

 ******************************************************************************/

void
SimpleTable::TableDrawCell
    (
    JPainter&         p,
    const JPoint&     cell,
    const JRect&     rect
    )
{
    // Convert the row number into a JString.
    JString cellNumber(cell.y);

    // Draw the JString that holds the value.
    p.String(rect, cellNumber, JPainter::kHAlignLeft, JPainter::kVAlignTop);
}

SimpleTable header file:

/******************************************************************************
 SimpleTable.h

    Interface for the SimpleTable class

    Written by Glenn Bach - 1998.

 ******************************************************************************/

#ifndef _H_SimpleTable
#define _H_SimpleTable

#include <JXTable.h>

class SimpleTable : public JXTable
{
public:

    SimpleTable(JXScrollbarSet* scrollbarSet, JXContainer* enclosure,
           const HSizingOption hSizing, const VSizingOption vSizing,
           const JCoordinate x, const JCoordinate y,
           const JCoordinate w, const JCoordinate h);

    virtual ~SimpleTable();

protected:

    virtual void    TableDrawCell(JPainter& p, const JPoint& cell, const JRect& rect);

private:

    // not allowed

    SimpleTable(const SimpleTable& source);
    const SimpleTable& operator=(const SimpleTable& source);
};

#endif

The SimpleTableDir object - SimpleTableDir .cc and

SimpleTableDir .h

The object that holds the window is a SimpleTableDir object which is derived from JXWindowDirector.

SimpleTableDir source file:

/******************************************************************************
 SimpleTableDir.cc

    BASE CLASS = JXWindowDirector

    Written by Glenn Bach - 1998.

 ******************************************************************************/

#include "SimpleTableDir.h"
#include "SimpleTable.h"
#include <JXWindow.h>
#include <JXScrollbarSet.h>
#include <jAssert.h>

/******************************************************************************
 Constructor

 ******************************************************************************/

SimpleTableDir::SimpleTableDir
    (
    JXDirector* supervisor
    )
    :
    JXWindowDirector(supervisor)
{
    // Set up the window and all of its contents.
    BuildWindow();
}

/******************************************************************************
 Destructor

 ******************************************************************************/

SimpleTableDir::~SimpleTableDir()
{
    // Nothing to delete
    // Window is deleted automatically by its director
    // SimpleTable is deleted automatically by its enclosure
}

/******************************************************************************
 BuildWindow

     This is a convenient and organized way of putting all of the initial
     elements into a window. This will keep the constructor less cluttered.

 ******************************************************************************/

void
SimpleTableDir::BuildWindow()
{
    // Create the window
    JXWindow* window = new JXWindow(this, 300,200, "Test SimpleTable Program");
    assert( window != NULL );

    // Give the window to the director
    SetWindow(window);

    // Set sizing
    window->SetMinSize(300,200);
    window->SetMaxSize(800,600);

    // Create the scrollbar set to hold the table
    JXScrollbarSet* scrollbarSet =
        new JXScrollbarSet(window,
            JXWidget::kHElastic, JXWidget::kVElastic, 0,0, 300,200);
    assert( scrollbarSet != NULL );

    // Create our SimpleTable. It must be placed inside the
    // special widget that JXScrollbarSet creates.  We get a
    // pointer to this special widget by calling GetScrollEnclosure().
    SimpleTable* table =
        new SimpleTable(scrollbarSet, scrollbarSet->GetScrollEnclosure(),
            JXWidget::kHElastic, JXWidget::kVElastic,
            0, 0, 10, 10);
    assert( table != NULL );
    table->FitToEnclosure();
}
 

SimpleTableDir header file:

/******************************************************************************
 SimpleTableDir.h

    Interface for the SimpleTableDir class

    Written by Glenn Bach - 1998.

 ******************************************************************************/

#ifndef _H_SimpleTableDir
#define _H_SimpleTableDir

#include <JXWindowDirector.h>

class SimpleTableDir : public JXWindowDirector
{
public:

    SimpleTableDir(JXDirector* supervisor);

    virtual ~SimpleTableDir();

private:

    void BuildWindow();

    // not allowed

    SimpleTableDir(const SimpleTableDir& source);
    const SimpleTableDir& operator=(const SimpleTableDir& source);
};
 

#endif

The function main - printing.cc

/******************************************************************************
 simpletable_main.cc

 Written by Glenn Bach - 1998.

 ******************************************************************************/

#include "SimpleTableDir.h"
#include <JXApplication.h>
#include <jAssert.h>

/******************************************************************************
 main

 ******************************************************************************/

int
main
    (
    int      argc,
    char*    argv[]
    )
{
    // Create the application object - one per program
    JXApplication* app = new JXApplication(&argc, argv);
    assert( app != NULL );

    // Create our window director
    SimpleTableDir* mainDir = new SimpleTableDir(app);
    assert( mainDir != NULL );

    // Activate the director
    mainDir->Activate();

    // Start the event loop
    app->Run();
    return 0;
}