"JD" <
XXXX@XXXXX.COM >wrote:
Quote
I'll toss something together to get you started and post it
here for everyone instead!
Untested:
//-------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//-------------------------------------------------------------
.....
//-------------------------------------------------------------
enum TActivityType
{
faUnspecified = -1,
faFirst,
faSecond,
faThird,
faCount
};
//-------------------------------------------------------------
class TForm1 : public TForm
{
__published:
TStringGrid *StringGrid1;
TImage *Image1;
TImage *Image2;
TImage *Image3;
void __fastcall Image1Click(TObject *Sender);
void __fastcall Image2Click(TObject *Sender);
void __fastcall Image3Click(TObject *Sender);
void __fastcall StringGrid1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State);
private:
struct TActivity
{
// fill-in the data fields
int something;
int SomethingElse;
};
TList *FActivityList;
TActivityType FActivity;
public:
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
};
//-------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//-------------------------------------------------------------
#endif
//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//-------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
FActivity = faUnspecified;
FActivityList = new TList();
StringGrid1->Options = StringGrid1->Options>>goVertLine>>goHorzLine;
StringGrid1->DefaultDrawing = false;
StringGrid1->OnDrawCell = StringGrid1DrawCell;
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
for( int x = 0; x < FActivityList->Count; ++x )
{
delete reinterpret_cast<TActivity*>( FActivityList->Items[x] );
}
}
//-------------------------------------------------------------
void __fastcall TForm1::Image1Click(TObject *Sender)
{
FActivity = faFirst;
}
//-------------------------------------------------------------
void __fastcall TForm1::Image2Click(TObject *Sender)
{
FActivity = faSecond;
}
//-------------------------------------------------------------
void __fastcall TForm1::Image3Click(TObject *Sender)
{
FActivity = faThird;
}
//-------------------------------------------------------------
void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
// trying to emulate the IDE and dropping components
if( Button == mbRight )
{
if( FActivity != faUnspecified )
{
int ACol, ARow;
StringGrid1->MouseToCell( X, Y, ACol, ARow );
if( ACol>= StringGrid1->FixedCols && ARow>= StringGrid1->FixedRows )
{
// was dropped on a valid cell
TActivity *Activity = new TActivity;
FActivityList->Add( Activity );
TActivityForm *pForm = new TActivityForm( this, Activity );
if( pForm->ShowModal() != mrCancel )
{
// OnClose needs to validate or validate here
if( Valid )
{
// assign pForm fields to TActivity. For example:
Activity->Something = 1; // pForm->SomeField
Activity->SomethingElse = 2; // pForm->SomeOtherField
if( StringGrid1->Objects[ACol][ARow] )
{
// an object has already been assigned to this
// cell so descide to insert above or below and
// then call insert on the appropriate row and
// insert it. For example:
TActivity *Activity = reinterpret_cast<TActivity*>( StringGrid1->Objects[ ACol ][ ARow ] );
if( pForm->StartTime>Activity->StartTime )
{
// insert below
}
else
{
// insert above
}
}
else
{
StringGrid1->Objects[ ACol ][ ARow ] = reinterpret_cast<TObject*>( Activity );
}
StringGrid1->Invalidate();
}
}
delete pForm;
}
}
FActivity = faUnspecified;
}
}
//-------------------------------------------------------------
/*
Besides adding logic like a popup menu, that's about all
there is for the GUI. The rest is painting the control so
that it's meaningful to the user (and saving and loading
that has been left to you).
*/
//-------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *pGrid = static_cast<TStringGrid*>( Sender );
TCanvas *pCanvas = pGrid->Canvas;
if( State.Contains(gdFixed) )
{
pCanvas->Brush->Color = pGrid->FixedColor;
pCanvas->Font->Color = pGrid->Font->Color;
pCanvas->FillRect( Rect );
}
else
{
pCanvas->Brush->Color = pGrid->Color;
switch( (reinterpret_cast<TAvtivity*>(pGrid->[ACol][ARow]))->ActivityType )
{
case faFirst: pCanvas->Font->Color = clRed; break;
case faSecond: pCanvas->Font->Color = clAqua; break;
case faThird: pCanvas->Font->Color = clYellow; break;
default : pCanvas->Font->Color = pGrid->Font->Color;
}
pCanvas->FillRect( Rect );
}
if( State.Contains(gdFixed) )
{
::DrawText( pCanvas->Handle, pGrid->Cells[ ACol ][ ARow ].c_str(), -1, &Rect, DT_LEFT | DT_VCENTER );
}
else
{
TActivity *Activity = reinterpret_cast<TActivity*>( StringGrid1->Objects[ ACol ][ ARow ] );
// use Activity to paint the cell
}
// optionally draw a focus rect
}
//-------------------------------------------------------------
~ JD