home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 April
/
Chip_1997-04_cd.bin
/
prezent
/
cb
/
data.z
/
APP.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-01-16
|
40KB
|
1,340 lines
/* APP.CPP: Application Expert Implementation
*/
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "app.h"
#include "exconst.h"
#include "filters.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TAppExpert *AppExpert;
// page numbers
int pgMenus = 0;
int pgExtensions = 1;
int pgSpeedbar = 2;
int pgAppInfo = 3;
int FirstPage = pgMenus;
int LastPage = pgAppInfo;
TPoint *DefaultButtonSize; //(24, 24);
int DefaultButtonSpace = 6;
int MenuItemCount = 18;
int MenuItemCounts[] = { 7, 4, 3, 4 };
int MenuItemOffsets[] = { 0, 7, 11, 14 };
char *SampleBitmaps[] = { "MENUDSGN",
"EXTDSGN",
"SPEEDDSGN",
"INFODSGN" };
int SourceBufferSize = 1024;
char *cCodeSnipets[csLastObject-1];
THandle *hCodeResource;
char *cSourceBuffer;
char *cResourceBuffer;
//========================================= TAppExpert ==========
void __fastcall TAppExpert::MenuClicked(TObject *Sender)
{
// TMainItems MenuIndex;
bool MenuOn;
// a menu category has been turned on/off
for (int MenuIndex = mmFile; MenuIndex < mmHelp; ++MenuIndex)
{
switch(MenuIndex)
{
case mmFile:
MenuOn = cbFileMenu->Checked;
break;
case mmEdit:
MenuOn = cbEditMenu->Checked;
break;
case mmWindow:
MenuOn = cbWindowMenu->Checked;
break;
case mmHelp:
MenuOn = cbHelpMenu->Checked;
break;
}
if (!MenuOn)
{
RemoveItems(*SpeedList, (TMainItems)MenuIndex);
FSpeedIndex = 0;
}
if (MenuList->ItemIndex == MenuIndex)
MenuListClick(this);
}
} // end of TAppExpert::MenuClicked()
//========================================== TAppExpert =========
bool __fastcall TAppExpert::ValidateInfo(void)
{
AnsiString e;
if (AppName->Text == "")
{
e = LoadStr(sAppNameRequired);
Application->MessageBox(e.c_str(), "ERROR", mbOK);
return false;
}
if (!IsValidIdent(AppName->Text))
{
e = LoadStr(sInvalidAppName);
Application->MessageBox(e.c_str(), "ERROR", mbOK);
return false;
}
if (!DirectoryExists(AppPath->Text))
{
e = LoadStr(sInvalidPath);
Application->MessageBox(e.c_str(), "ERROR", mbOK);
return false;
}
return true;
} // end of TAppExpert::ValidateInfo()
//========================================== TAppExpert =========
// calculate which page is next based on current page and settings->
// -1 = last page
// -2 = cannot move in requested direction
int __fastcall TAppExpert::NextPage(TMoveDirection Direction)
{
int CurPage;
int Result = -2;
CurPage = PageControl->ActivePage->PageIndex;
switch(Direction)
{
case mdNoMove:
if (CurPage == LastPage)
Result = -1;
else
Result = 0;
break;
case mdPrevious:
switch(CurPage)
{
case 0: //pgMenus: // do nothing
break;
case 1: //pgExtensions:
Result = pgMenus;
break;
case 2: //pgSpeedbar:
if (cbFileMenu->Checked)
Result = pgExtensions;
else
Result = pgMenus;
break;
case 3: //pgAppInfo:
if (HasMenus())
Result = pgSpeedbar;
else
Result = pgMenus;
break;
}
case mdNext:
switch(CurPage)
{
case 0: //pgMenus:
if (cbFileMenu->Checked)
Result = pgExtensions;
else
if (HasMenus())
Result = pgSpeedbar;
else
Result = pgAppInfo;
break;
case 1: //pgExtensions:
Result = pgSpeedbar;
break;
case 2: //pgSpeedbar:
Result = pgAppInfo;
break;
case 3: //pgAppInfo:
Result = -1;
break;
}
}
return Result;
} // end of TAppExpert::NextPage()
//---------------------------------------------------------------
void __fastcall GenerateMainFormFile(TAppExpert *AppExpert)
{
int buttonWidth = 25;
int spaceWidth = 4;
AnsiString filter;
int buttonNumber;
int buttonID;
AnsiString buttonMethod;
AnsiString buttonHint;
int buttonX;
int i;
TFileName *textName = new TFileName(AppExpert->AppPath->Text);
if (textName->Length() > 0 && (textName[textName->Length()] != ':' ||
textName[textName->Length()] != '\\'))
textName += '\\';
TFileName *formName = new TFileName(*textName + LoadStr(sMainFormFile));
*textName += LoadStr(sMainFormText);
TFileStream *textStream = new TFileStream(*textName, fmCreate);
TFileStream *formStream = new TFileStream(*formName, fmCreate);
try
{
WriteSnipet(textStream, csForm);
if (AppExpert->cbMDIApp->Checked)
WriteSnipet(textStream, csFormMDI);
if (AppExpert->HasMenus())
WriteSnipet(textStream, csFormMenu);
if (AppExpert->cbHints->Checked)
{
WriteSnipet(textStream, csHints);
if (AppExpert->cbStatusLine->Checked)
WriteSnipet(textStream, csCreateMethod);
}
// write menus
if (AppExpert->HasMenus())
{
WriteSnipet(textStream, csMenuObject);
if (AppExpert->cbFileMenu->Checked)
WriteSnipet(textStream, csFileMenuObject);
if (AppExpert->cbEditMenu->Checked)
WriteSnipet(textStream, csEditMenuObject);
if (AppExpert->cbWindowMenu->Checked)
WriteSnipet(textStream, csWindowMenuObject);
if (AppExpert->cbHelpMenu->Checked)
WriteSnipet(textStream, csHelpMenuObject);
FmtWrite(textStream, " end\n", OPENARRAY(TVarRec, (NULL)));
if (AppExpert->cbFileMenu->Checked)
{
// create the dialog objects
filter = "";
for (i=0; i < AppExpert->ExtListBox->Items->Count; i++)
filter = AppExpert->ExtListBox->Items->Strings[i] + "|";
//TListBox
if (filter.SubString(1, filter.Length()) == "|")
filter.Delete(1, filter.Length());
FmtWrite(textStream, cCodeSnipets[csOpenDialogObject],
OPENARRAY(TVarRec, (filter)));
FmtWrite(textStream, cCodeSnipets[csSaveDialogObject],
OPENARRAY(TVarRec, (filter)));
WriteSnipet(textStream, csPrintDialogObject);
WriteSnipet(textStream, csPrintSetupDialogObject);
}
}
if (AppExpert->cbStatusLine->Checked)
WriteSnipet(textStream, csStatusLineObject);
// create speedbuttons
if (AppExpert->SpeedButtonCount > 0)
{
WriteSnipet(textStream, csSpeedbarObject);
buttonNumber = 0;
buttonX = 8;
for (i=0; i < AppExpert->SpeedButtonCount; i++)
{
if (AppExpert->SpeedButtonID[i] > -1)
{
buttonNumber++;
buttonID = AppExpert->SpeedButtonID[i]-sMenuItemTextBase;
buttonMethod = LoadStr(buttonID + sMenuProcNames);
buttonHint = LoadStr(buttonID + sHintBase);
FmtWrite(textStream, cCodeSnipets[csSpeedButtonObject],
OPENARRAY(TVarRec, (buttonNumber, buttonX,
buttonMethod, buttonHint)));
WriteGlyphData(textStream, buttonID + 100);
buttonX += buttonWidth-1;
}
else
{
buttonX += spaceWidth;
}
}
FmtWrite(textStream, " end\n", OPENARRAY(TVarRec, (NULL)));
}
FmtWrite(textStream, "end\n", OPENARRAY(TVarRec, (NULL)));
// reset the text stream for conversion
textStream->Position = 0;
try
{
ObjectTextToResource(textStream, formStream);
} catch(...)
{
delete formStream;
formStream = NULL;
}
} catch(...)
{
delete textStream;
textStream = NULL;
}
if (formStream) delete formStream;
if (textStream) delete textStream;
} // end of GenerateMainFormFile()
//---------------------------------------------------------------
void __fastcall InitCodeGeneration(void)
{
cSourceBuffer = new char[SourceBufferSize];
// Find out how much data is in the code snippets resource and...
int ResourceSize = SizeofResource((void*)HInstance,
FindResource((void*)HInstance,
"SNIPETS", RT_RCDATA));
// ...get them into global memory.
hCodeResource = (int*)LoadResource((void*)HInstance,
FindResource((void*)HInstance,
"SNIPETS",
RT_RCDATA));
// Keep 'um from moving around,...
char *cResourcePtr = (char*)LockResource(hCodeResource);
// ...allocate some local memory for them and...
cResourceBuffer = new char[ResourceSize];
// ...transfer the global data to the local allocation.
memcpy(cResourceBuffer, cResourcePtr, ResourceSize);
// Make ASCIIZ strings out of the snippets by replacing bars
// ('|') with zeros.
char *cText = cResourceBuffer;
for (int i=0; i < ResourceSize; i++)
{
if (*cText == '|')
*cText = 0;
}
} // end of InitCodeGeneration()
//---------------------------------------------------------------
void __fastcall DoneCodeGeneration(void)
{
delete cSourceBuffer;
UnlockResource(hCodeResource);
FreeResource(hCodeResource);
delete cResourceBuffer;
} // end of DoneCodeGeneration()
//---------------------------------------------------------------
void __fastcall BinToHex(unsigned char *Binary, unsigned char *Text,
int Count)
{
char *HexChars = "0123456789ABCDEF";
int i;
for (i=0; i < Count; i++)
{
*Text = HexChars[(Binary[i] && 0xF0) >> 4];
Text++;
*Text = HexChars[Binary[i] && 0x0F];
Text++;
}
} // end of BinToHex()
//---------------------------------------------------------------
void __fastcall WriteBinaryAsText(TStream *Input, TStream *Output)
{
const int BytesPerLine = 32;
char *NewLine = "\r\n";
bool MultiLine;
int i;
long Count;
char Buffer[BytesPerLine];
char Text[BytesPerLine * 2 - 1];
// Get the size of the input buffer and...
Count = Input->Size;
// ...if its longer than our max line length (32 bytes)
// flag it a multiline buffer.
MultiLine = Count > BytesPerLine;
// Convert buffer size to hex and display.
BinToHex((unsigned char*)&Count, (unsigned char*)Text, 4); //110796EAS
Output->Write(Text, 4 * 2);
while(Count > 0)
{
if (MultiLine)
Output->Write((void*)NewLine[0], 2);
if (Count >= BytesPerLine)
i = BytesPerLine;
else
i = Count;
Input->Read(Buffer, i);
BinToHex((unsigned char*)Buffer, (unsigned char*)Text, i);
Output->Write(Text, i * 2);
Count -= i;
}
} // end of WriteBinaryAsText()
//---------------------------------------------------------------
void _fastcall FmtWrite(TStream *Stream, char *Fmt,
const TVarRec *Args, const int Args_Size)
{
sprintf(cSourceBuffer, Fmt, Args);
Stream->Write((void*)cSourceBuffer, strlen(cSourceBuffer));
} // end of FmtWrite()
//---------------------------------------------------------------
void __fastcall WriteSnipet(TStream *Stream, TCodeSnipet Snipet)
{
Stream->Write((void*)cCodeSnipets[Snipet],
strlen(cCodeSnipets[Snipet]));
} // end of WriteSnipet()
//---------------------------------------------------------------
void __fastcall WriteIdent(TStream *Stream, long ResID,
const AnsiString VarType) {
sprintf(cSourceBuffer, "%s%s *%s;\n", INDENT, VarType.c_str(),
LoadStr(ResID).c_str());
Stream->Write((void*)cSourceBuffer, strlen(cSourceBuffer));
} // WriteIdent()
//---------------------------------------------------------------
void __fastcall WriteMenuItems(TStream *Stream, TMainItems MenuIndex)
{
int i;
for (i=0; i<MenuItemCounts[MenuIndex]-1; i++)
{
WriteIdent(Stream,
sMenuItemNameBase+MenuItemOffsets[MenuIndex]+i,
"TMenuItem");
}
} // end of WriteMenuItems()
//---------------------------------------------------------------
void __fastcall WriteMethodDecl(TStream *Stream, long ResID)
{
sprintf(cSourceBuffer, "%svoid __fastcall %s(TObject *Sender);\n",
INDENT, LoadStr(ResID).c_str());
Stream->Write((void*)cSourceBuffer, strlen(cSourceBuffer));
} // end of WriteMethodDecl()
//---------------------------------------------------------------
void __fastcall WriteMethodHeader(TStream *Stream, long ResID)
{
sprintf(cSourceBuffer, "%s%svoid __fastcall T%s::%s(TObject *Sender)",
SEPARATOR, CRLF, LoadStr(sMainForm).c_str(),
LoadStr(ResID).c_str());
Stream->Write((void*)cSourceBuffer, strlen(cSourceBuffer));
} // end of WriteMethodHeader()
//---------------------------------------------------------------
void __fastcall WriteMenuMethodDecls(TStream *Stream,
TMainItems MenuIndex)
{
int i;
for (i=0; i<MenuItemCounts[MenuIndex]; i++)
WriteMethodDecl(Stream, sMenuProcNames+MenuItemOffsets[MenuIndex]+i);
} // end of WriteMethodDecls()
//---------------------------------------------------------------
void __fastcall WriteMenuMethods(TStream *Stream, TMainItems MenuIndex,
TCodeSnipet BaseSnipet)
{
int id;
int i;
TCodeSnipet snipet;
id = sMenuProcNames + MenuItemOffsets[MenuIndex];
for (i=0; i<MenuItemCounts[MenuIndex]; i++)
{
WriteMethodHeader(Stream, id+i);
snipet = TCodeSnipet(i+BaseSnipet);
WriteSnipet(Stream, snipet);
}
} // end of WriteMenuMethods()
//---------------------------------------------------------------
void __fastcall WriteGlyphData(TStream *Stream, long BitmapID)
{
Graphics::TBitmap *bitmap = new Graphics::TBitmap;
TMemoryStream *memory = new TMemoryStream;
try
{
bitmap->Handle = LoadBitmap((void*)HInstance, PChar(BitmapID));
// stream the bitmap to a memory stream, and the write that
// stream as text.
try
{
bitmap->SaveToStream(memory);
memory->Position = 0;
WriteBinaryAsText(memory, Stream); // 1 invokation
} catch(...)
{
delete memory;
memory = NULL;
}
} catch(...)
{
delete bitmap;
bitmap = NULL;
}
FmtWrite(Stream, "}\nend\n", OPENARRAY(TVarRec, (NULL)));
if (bitmap) delete bitmap;
if (memory) delete memory;
} // end of WriteGlyphData()
//---------------------------------------------------------------
TFileName* __fastcall GenerateProjectSource(TAppExpert *AppExpert)
{
// Get the application expert's fully qualified specification
// and if it is not slash terminated, slash terminate it and...
TFileName *result = new TFileName;
*result = AppExpert->AppPath->Text;
if (result->Length() > 0 && (result[result->Length()] != ':' &&
result[result->Length()] != '\\'))
result += '\\';
// ...add a DPR extension.
*result += AppExpert->AppName->Text + ".CPP";
TFileStream *projectFile = new TFileStream(*result, fmCreate);
try
{
sprintf(cSourceBuffer, cCodeSnipets[csProgram],
AppExpert->AppName->Text.c_str());
projectFile->Write(cSourceBuffer, strlen(cSourceBuffer));
} catch(...)
{
delete projectFile;
projectFile = NULL;
}
return result;
} // end of GenerateProjectSource()
//---------------------------------------------------------------
void __fastcall GenerateHdrSourceFile(TAppExpert *AppExpert)
{
AnsiString buttonName;//[80];
AnsiString buttonText;//[30];
int buttonID;
int i;
TFileName *filename = new TFileName(AppExpert->AppPath->Text);
if (filename->Length() > 0 && (filename[filename->Length()] != ':' &&
filename[filename->Length()] != '\\'))
filename += '\\';
*filename += LoadStr(sHdrSourceFile);
TFileStream *stream = new TFileStream(*filename, fmCreate);
try
{
WriteSnipet(stream, csHdr1);
// sourcePos = cSourceBuffer;
*cSourceBuffer = 0;
// create the menu declarations
if (AppExpert->HasMenus())
{
WriteIdent(stream, sMainMenu, "TMainMenu");
if (AppExpert->cbFileMenu->Checked)
WriteMenuItems(stream, mmFile);
if (AppExpert->cbEditMenu->Checked)
WriteMenuItems(stream, mmEdit);
if (AppExpert->cbWindowMenu->Checked)
WriteMenuItems(stream, mmWindow);
if (AppExpert->cbHelpMenu->Checked)
WriteMenuItems(stream, mmHelp);
}
// create any variable declarations
if (AppExpert->cbStatusLine->Checked)
WriteIdent(stream, sStatusLine, "TStatusBar");
if (AppExpert->cbFileMenu->Checked)
{
WriteIdent(stream, sOpenDialog, "TOpenDialog");
WriteIdent(stream, sSaveDialog, "TSaveDialog");
WriteIdent(stream, sPrintDialog, "TPrintDialog");
WriteIdent(stream, sPrintSetupDialog, "TPrinterSetupDialog");
}
// create speedbuttons
if (AppExpert->SpeedButtonCount > 0)
{
WriteIdent(stream, sSpeedBar, "TPanel");
buttonName = (AnsiString)INDENT + "TSpeedButton *" + LoadStr(sSpeedButton) +
"; // %s" + CRLF;
buttonID = 1;
for (i=0; i < AppExpert->SpeedButtonCount; i++)
{
if (AppExpert->SpeedButtonID[i] > -1)
{
buttonText = LoadStr(AppExpert->SpeedButtonID[i]);
sprintf(cSourceBuffer, buttonName.c_str(), buttonID,
buttonText.c_str());
stream->Write(cSourceBuffer, strlen(cSourceBuffer));
buttonID++;
}
}
}
// generate method declarations
if (AppExpert->cbStatusLine->Checked &&
AppExpert->cbHints->Checked)
{
WriteMethodDecl(stream, sFormCreateProc);
WriteMethodDecl(stream, sShowHelpProc);
}
if (AppExpert->cbFileMenu->Checked)
WriteMenuMethodDecls(stream, mmFile);
if (AppExpert->cbEditMenu->Checked)
WriteMenuMethodDecls(stream, mmEdit);
if (AppExpert->cbWindowMenu->Checked)
WriteMenuMethodDecls(stream, mmWindow);
if (AppExpert->cbHelpMenu->Checked)
WriteMenuMethodDecls(stream, mmHelp);
WriteSnipet(stream, csHdr2);
} catch(...)
{
delete stream;
stream = NULL;
}
if (stream) delete stream;
} // end of GenerateHdrSourceFile()
//---------------------------------------------------------------
void __fastcall GenerateMainSourceFile(TAppExpert *AppExpert) {
AnsiString ButtonName;
AnsiString ButtonText;
TFileName *filename = new TFileName(AppExpert->AppPath->Text);
if (filename->Length() > 0 && (filename[filename->Length()] != ':' &&
filename[filename->Length()] != '\\'))
filename += '\\';
*filename += LoadStr(sMainSourceFile);
TFileStream *stream = new TFileStream(*filename, fmCreate);
try
{
WriteSnipet(stream, csMainImpl);
*cSourceBuffer = 0;
// write code implementations
if (AppExpert->cbStatusLine->Checked &&
AppExpert->cbHints->Checked)
{
WriteMethodHeader(stream, sFormCreateProc);
WriteSnipet(stream, csFormCreateProc);
WriteMethodHeader(stream, sShowHelpProc);
WriteSnipet(stream, csShowHelpProc);
}
// create any variable declarations
if (AppExpert->cbFileMenu->Checked)
WriteMenuMethods(stream, mmFile, csFileNewProc);
if (AppExpert->cbEditMenu->Checked)
WriteMenuMethods(stream, mmEdit, csEditUndoProc);
// create speedbuttons
if (AppExpert->cbWindowMenu->Checked)
WriteMenuMethods(stream, mmWindow, csWindowTileProc);
if (AppExpert->cbHelpMenu->Checked)
WriteMenuMethods(stream, mmHelp, csHelpContentsProc);
AnsiString s = (AnsiString)SEPARATOR + CRLF;
FmtWrite(stream, s.c_str(), OPENARRAY(TVarRec, (NULL)));
} catch(...)
{
delete stream;
stream = NULL;
}
if (stream) delete stream;
} // end of GenerateMainSourceFile()
//---------------------------------------------------------------
// interface void
void __fastcall ApplicationExpert(TIToolServices *ToolServices)
{
AnsiString usesClause;
TAppExpert *d = new TAppExpert(Application);
TFileName *projectName = new TFileName;
try
{
if (d->ShowModal() == 1 /*mrOK*/) //112696EAS
InitCodeGeneration();
try
{
projectName = &ExpandFileName(*GenerateProjectSource(d));
GenerateHdrSourceFile(d);
GenerateMainSourceFile(d);
GenerateMainFormFile(d);
} catch(...)
{
DoneCodeGeneration();
}
// open the new project
if (ToolServices != NULL && ToolServices->CloseProject())
ToolServices->OpenProject(*projectName);
} catch(...)
{
delete d;
d = NULL;
}
if (d) delete d;
} // end of ApplicationExpert()
//---------------------------------------------------------------
bool __fastcall EditFilterInfo(AnsiString &Filter)
{
TFilterDlg *d;
bool result;
d = new TFilterDlg(Application);
try
{
d->Filter = Filter;
result = d->ShowModal() == 1; // mrOK; //112696EAS
if (result)
Filter = d->Filter;
} catch(...)
{
delete d;
d = NULL;
}
if (d) delete d;
return result;
} // end of EditFilterInfo()
//---------------------------------------------------------------
void __fastcall ClearButtonImages(TList *List)
{
int i;
for (i=0; i < List->Count; i++)
(TButtonImage*)(List[i]).Free();
List->Clear();
} // end ClearButtonImages()
//=========================================[ TButtonImage ]======
// TButtonImage
__fastcall TButtonImage::TButtonImage(void)
{
bmFBitmap = new Graphics::TBitmap;
FNumGlyphs = 1;
}
//=========================================[ TButtonImage ]======
__fastcall TButtonImage::~TButtonImage(void)
{
delete bmFBitmap;
}
//========================================= TButtonImage ========
void __fastcall TButtonImage::SetBitmapID(long Value)
{
if (FBitmapID != Value)
{
FBitmapID = Value;
bmFBitmap->Handle = LoadBitmap((void*)HInstance, PChar(FBitmapID));
}
} // end of TButtonImage::SetBitmapID()
//===============================================================
void __fastcall TButtonImage::Draw(TCanvas *Canvas, int X, int Y)
{
int BX;
TRect Target;
TRect Source;
TColor SavePen;
TColor SaveBrush;
SavePen = Canvas->Pen->Color;
SaveBrush = Canvas->Brush->Color;
Target = DrawButtonFace(Canvas, Bounds(X, Y, DefaultButtonSize->x,
DefaultButtonSize->y), 1, bsWin31, False,
False, False);
// draw bitmap
BX = bmFBitmap->Width / FNumGlyphs;
if (BX > 0)
{
Target = Bounds(X, Y, BX, bmFBitmap->Height);
OffsetRect((RECT*)&Target, (DefaultButtonSize->x / 2) - (BX / 2),
(DefaultButtonSize->y / 2) - (bmFBitmap->Height / 2));
Source = Bounds(0, 0, BX, bmFBitmap->Height);
Canvas->BrushCopy(Target, bmFBitmap, Source,
bmFBitmap->Canvas->Pixels[0][bmFBitmap->Height - 1]);
}
Canvas->Pen->Color = SavePen;
Canvas->Brush->Color = SaveBrush;
} // end of TButtonImage::Draw()
//===============================================================
//===============================================================
//========================================== TAppExpert =========
void __fastcall TAppExpert::FormCreate(TObject *Sender)
{
long ID;
TButtonImage *ButtonImage;
SpeedList = new TList;
ButtonList = new TList;
Offscreen = new Graphics::TBitmap;
Offscreen->Width = Speedbar->Width;
Offscreen->Height = Speedbar->Height;
SpeedPointer = new Graphics::TBitmap;
SpeedPointer->Handle = LoadBitmap((void*)HInstance, "SPEEDPOINTER");
SampleBmp = new Graphics::TBitmap;
// fill the MenuItemList with the speedbuttons
for (ID=sMenuItemTextBase; ID < sMenuItemTextBase + MenuItemCount; ID++)
{
ButtonImage = new TButtonImage;
ButtonImage->NumGlyphs = 2;
ButtonImage->BitmapID = ID;
ButtonList->Add(ButtonImage);
}
// This is required to prevent the Speedbar from erasing its
// background each time it paints-> This dramatically reduces
// (eliminates) any flicker when painting-> (try { commenting
// out this line to see the difference)
Speedbar->ControlStyle << csOpaque;
PageControl->ActivePage = PageControl->Pages[FirstPage];
SampleBmp->Handle = LoadBitmap((void*)HInstance,
SampleBitmaps[FirstPage]);
RefreshButtons();
} // end of TAppExpert::FormCreate()
//========================================== TAppExpert =========
void __fastcall TAppExpert::FormDestroy(TObject *Sender)
{
ClearButtonImages(ButtonList);
delete ButtonList;
delete SpeedList;
delete SpeedPointer;
delete Offscreen;
delete SampleBmp;
} // end of TAppExpert::FormDestroy()
//========================================== TAppExpert =========
bool __fastcall TAppExpert::HasMenus(void)
{
return (cbFileMenu->Checked || cbEditMenu->Checked ||
cbWindowMenu->Checked || cbHelpMenu->Checked);
} // end of TAppExpert::HasMenus()
//========================================= TAppExpert ==========
void __fastcall TAppExpert::RefreshButtons(void)
{
switch(NextPage(mdNoMove))
{
case -1:
NextButton->Caption = LoadStr(sFinish);
break;
case 0:
NextButton->Caption = LoadStr(sNext);
break;
}
switch(NextPage(mdPrevious))
{
case -2:
PrevButton->Enabled = False;
break;
default:
PrevButton->Enabled = True;
break;
}
} // end of TAppExpert::RefreshButtons()
//---------------------------------------------------------------
void __fastcall RemoveItems(TList &List, TMainItems MenuIndex)
{
int StartID;
int EndID;
int i;
TButtonImage *ButtonImage;
StartID = sMenuItemTextBase + MenuItemOffsets[MenuIndex];
EndID = StartID + MenuItemCounts[MenuIndex];
i = 0;
while(i < List.Count)
{
ButtonImage = (TButtonImage*)List.Items[i];
if (ButtonImage != 0 && ButtonImage->BitmapID < EndID &&
ButtonImage->BitmapID >= StartID)
List.Delete(i);
else
i++;
}
} // end of TAppExpert::RemoveItems()
//========================================== TAppExpert =========
void __fastcall TAppExpert::NextPrevClick(TObject *Sender)
{
int NewPage;
if (Sender == PrevButton)
NewPage = NextPage(mdPrevious);
else
NewPage = NextPage(mdNext);
switch(NewPage)
{
case -1:
if (ValidateInfo())
ModalResult = 1; //mrOK; //112796
break;
case -2:
break; // do nothing
default:
if (SampleBitmaps[NewPage] != 0)
{
SampleBmp->Handle = LoadBitmap((void*)HInstance,
SampleBitmaps[NewPage]);
Sample->Invalidate();
}
PageControl->ActivePage = PageControl->Pages[NewPage];
}
RefreshButtons();
} // end of TAppExpert::NextPrevClick()
//=========================================== TAppExpert ========
// draw the file extension list box
void __fastcall TAppExpert::DrawExtension(TWinControl *Control,
int Index,
const TRect &Rect,
TOwnerDrawState State)
{
int P;
TRect R;
char C[255];
AnsiString S;
// find the separator in the string
P = ExtListBox->Items->Strings[Index].Pos("|");
// adjust the rectangle so we draw only the left "column"
R = Rect;
// draw the filter description
S = ExtListBox->Items->Strings[Index].SubString(1, P - 1);
R.Right = R.Left + ExtHeader->SectionWidth[0];
ExtTextOut((void*)ExtListBox->Canvas->Handle, R.Left, R.Top,
ETO_CLIPPED | ETO_OPAQUE, (RECT*)&R, StrPCopy(C, S),
S.Length(), NULL);
// move the rectangle to the next column
R.Left = R.Right;
R.Right = Rect.Right;
S = ExtListBox->Items->Strings[Index].SubString(P + 1, 255);
ExtTextOut((void*)ExtListBox->Canvas->Handle, R.Left, R.Top,
ETO_CLIPPED | ETO_OPAQUE, (RECT*)&R, StrPCopy(C, S),
S.Length(), NULL);
} // end of TAppExpert::DrawEstension()
//========================================== TAppExpert =========
void __fastcall TAppExpert::HeaderSized(TObject *Sender,
int ASection,
int AWidth)
{
ExtListBox->Invalidate();
} // end of TAppExpert::HeaderSized()
//=========================================== TAppExpert ========
void __fastcall TAppExpert::AddClick(TObject *Sender)
{
AnsiString Filter;
Filter = "";
if (EditFilterInfo(Filter))
ExtListBox->Items->Add(Filter);
} // end of TAppExpert::AddClick()
//========================================== TAppExpert =========
void __fastcall TAppExpert::EditClick(TObject *Sender)
{
AnsiString Filter;
if (ExtListBox->ItemIndex > -1)
{
Filter = ExtListBox->Items->Strings[ExtListBox->ItemIndex];
if (EditFilterInfo(Filter))
ExtListBox->Items->Strings[ExtListBox->ItemIndex] = Filter;
}
} // end of TAppExpert::EditClick()
//========================================== TappExpert =========
void __fastcall TAppExpert::DeleteClick(TObject *Sender)
{
if (ExtListBox->ItemIndex > -1)
ExtListBox->Items->Delete(ExtListBox->ItemIndex);
} // end of TAppExpert::DeleteClick()
//========================================= TAppExpert ==========
void __fastcall TAppExpert::MoveClick(TObject *Sender)
{
int Delta;
int NewPos;
if (ExtListBox->ItemIndex != -1)
{
if (Sender == UpButton)
Delta = -1;
else
if (Sender == DownButton)
Delta = 1;
else
Delta = 0;
if (Delta != 0)
{
NewPos = ExtListBox->ItemIndex + Delta;
if (NewPos >= 0 && NewPos < ExtListBox->Items->Count)
{
ExtListBox->Items->Move(ExtListBox->ItemIndex, NewPos);
ExtListBox->ItemIndex = NewPos;
}
}
}
} // end of TAppExpert::MoveClick()
//========================================== TAppExpert =========
// return the rectangle of the specified speedbutton or space
TRect __fastcall TAppExpert::SpeedButtonRect(int Index)
{
int i;
int x;
TRect Result;
x = 10; // first usable position
for (i=0; i < Index; i++)
if (SpeedList->Items[i] == NULL)
x += DefaultButtonSpace;
else
x += DefaultButtonSize->x-1;
Result = Bounds(x, 5, DefaultButtonSize->x, DefaultButtonSize->y);
if (Index < SpeedList->Count && SpeedList->Items[Index] == NULL)
Result.Right = Result.Left + DefaultButtonSpace;
return Result;
} // end of TAppExpert::SpeedButtonRect()
//========================================== TAppExpert =========
// return an index into SpeedList from the TPoint
int __fastcall TAppExpert::SpeedButtonAtPos(const tagPOINT &Pos)
{
TRect R;
int i;
for (i=0; i < SpeedList->Count; i++)
{
R = SpeedButtonRect(i);
if (PtInRect((RECT*)&R, Pos))
{
return i;
}
}
return -1;
} // end of TAppExpert::SpeedButtonAtPos()
//========================================== TAppExpert =========
int __fastcall TAppExpert::GetSpeedButtonCount(void)
{
return SpeedList->Count;
} // end of TAppExpert::GetSpeedButtonCount()
//========================================== TAppExpert =========
int __fastcall TAppExpert::GetSpeedButtonID(int Value)
{
TButtonImage *ButtonImage;
int Result;
ButtonImage = (TButtonImage*)SpeedList->Items[Value];
if (ButtonImage != NULL)
Result = ButtonImage->BitmapID;
else
Result = -1;
return Result;
} // end of TAppExpert::GetSpeedButtonID()
//========================================== TAppExpert =========
void __fastcall TAppExpert::SpeedbarPaint(TObject *Sender)
{
int i;
TButtonImage *ButtonImage;
TRect R;
Offscreen->Canvas->Pen->Color = (TColor)clWindowFrame;
Offscreen->Canvas->Brush->Style = bsClear;
Offscreen->Canvas->Brush->Color = Speedbar->Color;
Offscreen->Canvas->Rectangle(1, 1, Speedbar->Width - 1,
Speedbar->Height - 1);
Offscreen->Canvas->Pen->Color = (TColor)clBtnShadow;
TPoint p[3];
p[0].x = 0; p[0].y = Speedbar->Height-1;
p[1].x = 0; p[1].y = 0;
p[2].x = Speedbar->Width-1; p[2].y = 0;
Offscreen->Canvas->Polyline(p, 3);
Offscreen->Canvas->Pen->Color = (TColor)clBtnHighlight;
p[0].x = Speedbar->Width-1; p[0].y = 0;
p[1].x = Speedbar->Width-1; p[1].y = Speedbar->Height;
Offscreen->Canvas->Polyline(p, 2);
// Draw the buttons in the list }
int x = 10;
for (i=0; i < SpeedList->Count; i++)
{
ButtonImage = (TButtonImage*)SpeedList->Items[i];
if (ButtonImage == NULL)
{
Offscreen->Canvas->Brush->Style = bsSolid;
Offscreen->Canvas->Brush->Color = (TColor)clBtnShadow;
R = Bounds(x + 2, 5, DefaultButtonSpace - 3,
DefaultButtonSize->y - 2);
Offscreen->Canvas->FillRect((TRect&)R);
x += DefaultButtonSpace;
}
else
{
Offscreen->Canvas->Brush->Style = bsSolid;
ButtonImage->Draw(Offscreen->Canvas, x, 4);
x += DefaultButtonSize->x-1;
}
if (x + (DefaultButtonSize->x * 2) > Speedbar->Width)
break;
// draw the insertion point }
R = SpeedButtonRect(FSpeedIndex);
OffsetRect((RECT*)&R, -5, 0);
R.Top = R.Bottom + 2;
R.Bottom = R.Top + SpeedPointer->Height;
R.Right = R.Left + SpeedPointer->Width;
Offscreen->Canvas->Brush->Color = Speedbar->Color;
Offscreen->Canvas->BrushCopy((TRect&)R, SpeedPointer,
(TRect&)Rect(0, 0, SpeedPointer->Width,
SpeedPointer->Height),
clWhite);
}
Speedbar->Canvas->Draw(0, 0, Offscreen);
} // end of TAppExpert::SpeedbarPaint()
//========================================= TAppExpert ==========
// The list of menus was clicked
void __fastcall TAppExpert::MenuListClick(TObject *Sender)
{
long ID;
int i;
int ButtonIndex;
bool MenuOn;
if (MenuList->ItemIndex > -1)
{
ID = sMenuItemTextBase + MenuItemOffsets[ TMainItems(MenuList->ItemIndex) ];
MenuItemList->Items->BeginUpdate();
try
{
MenuItemList->Clear();
switch(MenuList->ItemIndex)
{
case 0:
MenuOn = cbFileMenu->Checked;
break;
case 1:
MenuOn = cbEditMenu->Checked;
break;
case 2:
MenuOn = cbWindowMenu->Checked;
break;
case 3:
MenuOn = cbHelpMenu->Checked;
break;
}
if (MenuOn)
{
// load the list box with the buttons and text }
for (i=0; i < MenuItemCounts[TMainItems(MenuList->ItemIndex)]; i++)
{
ButtonIndex = i + MenuItemOffsets[TMainItems(MenuList->ItemIndex)];
MenuItemList->Items->AddObject(LoadStr(ID+i),
(TObject*)ButtonList->Items[ButtonIndex]);
}
}
}
catch(...)
{
}
MenuItemList->Items->EndUpdate();
}
} // end of TAppExpert::MenuListClick()
//========================================== TAppExpert =========
void __fastcall TAppExpert::DrawMenuItem(TWinControl *Control,
int Index,
const TRect &Rect,
TOwnerDrawState State)
{
TButtonImage *ButtonImage;
TRect R;
char C[255];
ExtTextOut((void*)MenuItemList->Canvas->Handle, R.Left, R.Top,
ETO_OPAQUE, (RECT*)&Rect, NULL, 0, NULL);
ButtonImage = (TButtonImage*)MenuItemList->Items->Objects[Index];
ButtonImage->Draw(MenuItemList->Canvas, Rect.Left+2, Rect.Top+1);
R = Rect;
R.Left += DefaultButtonSize->x + 2 + 4;
DrawText((void*)MenuItemList->Canvas->Handle,
StrPCopy(C, MenuItemList->Items->Strings[Index]), -1,
(RECT*)&R, DT_VCENTER || DT_SINGLELINE);
} // end of TAppExpert::DrawMenuItem()
//========================================== TAppExpert =========
// Insert the current button into the Speedbar }
void __fastcall TAppExpert::InsertClick(TObject *Sender)
{
TButtonImage *ButtonImage;
if (MenuItemList->ItemIndex > -1)
{
ButtonImage = (TButtonImage*)MenuItemList->Items->Objects[MenuItemList->ItemIndex];
if (FSpeedIndex < SpeedList->Count)
SpeedList->Insert(FSpeedIndex, ButtonImage);
else
SpeedList->Add(ButtonImage);
FSpeedIndex++;
Speedbar->Invalidate();
}
} // end of TAppExpert::InsertClick()
//========================================== TAppExpert =========
void __fastcall TAppExpert::SpaceClick(TObject *Sender)
{
if (FSpeedIndex < SpeedList->Count)
SpeedList->Insert(FSpeedIndex, NULL);
else
SpeedList->Add(NULL);
FSpeedIndex++;
Speedbar->Invalidate();
} // end of TAppExpert::SpaceClick()
//========================================== TAppExpert =========
void __fastcall TAppExpert::RemoveClick(TObject *Sender)
{
if (FSpeedIndex < SpeedList->Count) {
SpeedList->Delete(FSpeedIndex);
if (FSpeedIndex > SpeedList->Count)
FSpeedIndex = SpeedList->Count;
Speedbar->Invalidate();
}
} // end of TAppExpert::RemoveClick()
//========================================== TAppExpert =========
// The mouse was clicked in the Speedbar area
void __fastcall TAppExpert::SpeedMouseDown(TObject *Sender,
TMouseButton Button,
TShiftState Shift,
int X, int Y)
{
int Index;
Index = SpeedButtonAtPos(Point(X, Y));
if (Index != -1)
FSpeedIndex = Index;
else
FSpeedIndex = SpeedList->Count;
Speedbar->Invalidate();
} // end of TAppExpert::SpeeMouseDown()
//========================================== TAppExpert =========
void __fastcall TAppExpert::BrowseClick(TObject *Sender)
{
AnsiString d;
d = AppPath->Text;
TSelectDirOpts tso;
tso << sdAllowCreate << sdPrompt << sdPerformCreate;
if (SelectDirectory(d, tso, NULL))
AppPath->Text = d;
} // end of TAppExpert::BrowseClick()
//========================================== TAppExpert =========
void __fastcall TAppExpert::SamplePaint(TObject *Sender)
{
if (SampleBmp != NULL)
Sample->Canvas->Draw(0, 0, SampleBmp);
} // end of TAppExpert::SamplePaint()
//========================================[ TAppExpert ]=========
__fastcall TAppExpert::TAppExpert(TComponent* Owner)
:TForm(Owner)
{
}