Spreadsheet
Volume Number: 5
Issue Number: 3
Column Tag: C Workshop
How to Write a Spreadsheet in LS C 
By Bryan Waters, Casselberry, FL
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
Bryan Waters is a Software Engineer for Maynard Electronics, and is currently
working as part of a team to develop a finder-like tape backup system.
Dear Cary Mariash
In the January ’88 issue of MacTutor, a letter sent by Cary Mariash requested
information on Macintosh tools to create spreadsheet type windows. MacTutorCalc is
an example of a simple (very simple!) spreadsheet type application, that uses the List
Manager ( IM - IV ) to create it’s windows. (Note: MacTutorCalc is written in Think C
3.0, see the project window in figure 1 )
Figure 1. Lightspeed C Project Window
List Manager in action!
The call to Lnew is used to create the list (figure 2). The dataBounds parameter
is set to have an extra row and column than our work area. This, of course, is to
support the row and column headers. After the list has been created the programmer
has several more options, involving selection, and installing a routine in the lClikLoop
field to be called repeatedly while LClick has control. The selFlags field of the list
handle allow complete customization of the selection algorithm used by the list
manager. MacTutorCalc is content to use the default selection, but when we call LClick
to handle our mouseDown events in the window, any scrolling causes the spreadsheet
to scroll, without the grid being updated. To fix this, MacTutorCalc installs a
lClickLoop routine to update the grid continuously during calls to LClick. This works
great, except the List Manager does not call this routine when the mouse button is
pressed in the scroll bars, so that the grid is updated then only after the mouse button
is released. Although it is not implemented here, this could be fixed by installing a
pointer to our grid routine in the system global DragHook ( $9F6 ) which is called by
DragGrayRgn. Since the control manager routine TrackControl calls DragGrayRgn to
drag the outline of the scroll bars thumb button, this would achieve the desired effect.
Figure 2. Spreadsheet & Dialog Entry
There are other points to take into account when using a window such as this. For
example, when the user resizes the window, it would not be desirable to allow the user
to size the window out of alignment with the cell boundries simply because it looks bad.
To handle this, after the call to GrowWindow, we make sure that the new size is a
multiple of the cell size. Updating is handled almost completely by the list manager
routine LUpdate, although we still have to draw the grid, and the grow icon.
Data entry is probably where MacTutorCalc could be improved the most.
Currently data entry is implemented by double-clicking on the desired cell ( LClick
returns TRUE when a cell was double-clicked and a call to LLastClick is used to get the
cell’s address). This brings up a dialog prompting for the data. In an application
intended for serious use, this would get extremely tiring. This could be fixed by adding
a data entry window at the top of the screen ( Excel Style ) and allowing the user to
enter data into the currently selected cell through this window. At this point we have
data that needs to processed, so we determine the type of data ( formula, or string ) and
then set our calculation flag. The routine DoCalc is called in response to this, and
processes the whole spreadsheet, calling the parser for formula, and ignoring strings.
After the data has been parsed, LSetCell is used to put the data into the list.
Alternatively, if the cell has been blanked out, then LClrCell is used to clear the data.
About MacTutorCalc
MacTutorCalc is a simple spreadsheet written for the purpose of demonstrating
use of the list manager. The work area for our application is set to 8 columns by 8
rows. It is capable of accepting both strings and formulae, and constants. Data is
entered into a cell by double-clicking on it. I know that this is awkward, but other
methods would have unnecessarily complicated this example. Any data entered into a
cell that starts with “=” is considered a formula and will be parsed, and any number
will be treated as a constant, everything else is considered a string. A formula can be
any simple algebraic expression, using floating or absolute cell addresses. There a few
simple functions in MacTutorCalc that use the Math library distributed with Think C.
In the future, we could expand our spreadsheet to include:
• ability to save and retrieve spreadsheets
• a full function library
• cell formatting and spreadsheet editing features
• memory paging system to support larger spreadsheet sizes
• support for SYLK format
[There is a known bug in the text to binary conversion routines. Apple's
conversion routines were used, and so large formatted numbers will not be converted
to text correctly. Note also that Apple frowns on trying to use the List Manager to write
the next Excel Product, so if you get serious about this project, re-write it without
using the List Manager. -Ed]
Listing: CalcData.h
/* Global data */
extern int quit_flag ;
extern Rect minmax_size ;
extern Rect curr_screen ;
extern int automatic_calculation ;
extern int calc_data ;
extern int do_calc_now ;
extern SHEET_WIN_PTR curr_sheet_ptr ;
extern SHEET_WIN_HDL calc_hdl ;
extern FUN_ENTRY fun_table[] ;
extern ARG arg_free_pool[ ] ;
Listing: MacCalc.h
#include
#include
#ifndef NULL
#define NULL 0L
#endif
#define MAX_ROWS 8
#define MAX_COLUMNS 8
#define CELL_WIDTH 96
#define CELL_HEIGTH 16
#define ENTER_DATA_DIALOG 128
#define APPLE_MENU 1
#define FILE_MENU 256
#define EDIT_MENU 257
/* Types for cell */
#define CLEARED -1
#define UNDEFINED 0
#define STRING 1
#define FORMULA 2
#define CONSTANT 3
struct cell_struct{
int type ;
double value ;
Str255 formula ;
} ;
typedef struct cell_struct CELL, *CELL_PTR, **CELL_HDL ;
struct sheet_win{
WindowPtr sheet_ window_ptr ;
ListHandle sheet_list_hdl ;
CELL sheet_data[MAX_ROWS][MAX_COLUMNS] ;
} ;
typedef struct sheet_win SHEET_WIN, *SHEET_WIN_PTR, **SHEET_WIN_HDL ;
/* Argument types */
#define VALUE_ARG 1
#define STRING_ARG 2
/* Arg usage defines */
#define IN_USE TRUE
#define FREE_ARG FALSE
struct fun_arg{
struct fun_arg *next_arg ;
int in_use ;
int type ;
double value ;
Str255 string ;
} ;
typedef struct fun_arg ARG, *ARG_PTR, **ARG_HDL ;
struct fun_entry{
char fun_name[32] ;
double (*fun_ptr)( ) ;
} ;
typedef struct fun_entry FUN_ENTRY, *FUN_ENTRY_PTR, **FUN_ENTRY_HDL ;
/* Prototypes */
int DoInit( void ) ;
void DoEventLoop( void ) ;
void DoActivate( EventRecord * ) ;
void DoUpdate( EventRecord * ) ;
void DoMouseDown( EventRecord * ) ;
int ClikLoop( void ) ;
void EnterData( Cell, SHEET_WIN_HDL ) ;
void DoCalc( SHEET_WIN_HDL ) ;
Listing: SheetHndlg.h
/* SpreadSheet Handling prototypes */
int OpenNewSpreadsheet( char * ) ;
int DrawGrid( WindowPtr ) ;
Listing: Parser.h
/* Parse errors */
#define MISMATCHED_PARENTHESIS 200
#define INVALID_NUMBER 201
#define INVALID_ADDRESS 202
#define ADDRESS_TOO_LARGE 203
#define INVALID_FUNCTION 204
/* Prototypes */
void SetType( CELL_PTR, int ) ;
double ParseFormula( unsigned char *, int * ) ;
double ParseExpression( void ) ;
double ParseFactor( void ) ;
double ParseValue( void ) ;
double ParseAddress( void ) ;
#define IsDigit(x) (((x)<=’9') && ((x)>=’0'))
#define IsAlpha(x) ( ( ( (x) >= ‘A’ ) && ( (x) <= ‘Z’ ) ) || ( ( (x)
>= ‘a’ ) && ( (x) <= ‘z’ ) ) )
int IsFunction( void ) ;
int GetRow( void ) ;
int GetColumn( void ) ;
double CallFunction( ) ;
int Lookup( unsigned char * ) ;
ARG_PTR BuildArg( void ) ;
double atof( void ) ;
void ftoa( double, unsigned char * ) ;
double GetFloat( unsigned char *, int * ) ;
ARG_PTR GetArg( void ) ;
void PutArg( ARG_PTR ) ;
void DestroyArgs( ARG_PTR ) ;
Listing: CalcData.c
#include
#include
#include
#include
#include “MacCalc.h”
#include “SheetHndlg.h”
/* Global data */
int quit_flag = FALSE ;
/* Grow window limits */
Rect minmax_size ;
/* Current screen size */
Rect curr_screen ;
/* do calculation switches */
int automatic_calculation = TRUE ;
int calc_data = FALSE ;
int do_calc_now = FALSE ;
/* Current spreadsheet globals */
SHEET_WIN_PTR curr_sheet_ptr ;
SHEET_WIN_HDL calc_hdl ;