(Ebook - Pdf) Kick Ass Delphi Programming
.pdfRNum : Integer; begin
RHeight := Calendar.ClientHeight div 7; RNum := Y div RHeight + 1;
Accept := RNum > 1; end;
Dropping the Payload
I proceeded to Step 3. The strategy was to figure out the row and column locations of the first day of the month and the cursor position. From that, a little arithmetic would give me the number of days difference between the two dates, which I could then add to the first day’s date to obtain the absolute date.
That first day will always appear in the second row (under the day titles), no matter which day of the week it falls on. So much for the row. Since I hadn’t changed the calendar from the standard Sunday-Saturday format, getting the day of the week for the first day gave me a column reference position. From there, I just had to advance the number of days into the calendar. Fortunately, the Orpheus package comes with some powerful date conversion and arithmetic routines that helped greatly. My calculation routine for the date pointed at is shown in Listing 12.3.
Listing 12.3 Calculating the date from the mouse position
function DatePointedTo : TOvcDate; var
Idx : Longint;
DOW : Integer; Day1 : TOvcDate;
begin
{Compute first day as Row = 2, Col = DOW, then calculate an offset to the date pointed to. Then
add |
the |
two. } |
Day1 := DMYToDate(1, Calendar.Month, Calendar.Year); |
||
DOW := |
Ord(DayOfWeek(Day1)) + 1; |
|
Idx := |
(RNum - 2) * 7; |
|
if CNum < |
DOW |
|
then |
Idx |
:= Idx - (DOW - CNum) |
else |
if CNum > DOW |
|
|
then Idx := Idx + (CNum - DOW); |
|
Result |
:= |
IncDate(Day1, Idx, 0, 0); |
end; { |
DatePointedTo } |
All that was left was some simple housekeeping—converting the date and edit box text to a string, and adding that string to the string grid. I also found it handy to clear out the edit box once the drop was complete; with the editing capabilities of the edit box somewhat hampered by the dragging support, it became cumbersome to clear it manually.
Note to myself: In this case, it was appropriate to clear the edit box as part of the dragdrop event handler, because I wanted it performed only if the drop was performed. Had I wanted to clear the edit box whether or not the drop succeeded, I would have done so in an OnEndDrag handler for the edit box.
Reminder to myself: Make sure the OvcCalendar’s Initialize property is set to True.
Otherwise, it comes up in an undefined state!
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Label4: TLabel;
Label5: TLabel;
procedure QuitBtnClick(Sender: TObject);
procedure EditBoxMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure CalendarDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
procedure CalendarDragDrop(Sender, Source: TObject; X, Y: Integer); private
{Private declarations } public
{Public declarations }
end;
var
DDDemoForm: TDDDemoForm;
implementation
{$R *.DFM}
procedure TDDDemoForm.QuitBtnClick(Sender: TObject); begin
Close;
end;
procedure TDDDemoForm.EditBoxMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if (Button = mbLeft)
and (EditBox.Text <> '') and not (ssDouble in Shift)
then TEdit(Sender).BeginDrag(False); end;
procedure TDDDemoForm.CalendarDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
var
RHeight : Integer; RNum : Integer;
begin
RHeight := Calendar.ClientHeight div 7; RNum := Y div RHeight + 1;
Accept := RNum > 1; end;
procedure TDDDemoForm.CalendarDragDrop(Sender, Source: TObject; X, Y: Integer);
var |
|
RHeight |
: Integer; |
CWidth |
: Integer; |
RNum |
: Integer; |
CNum |
: Integer; |
s |
: String; |
function DatePointedTo : TOvcDate; var
Idx : Longint;
DOW : Integer; Day1 : TOvcDate;
begin
{ Compute first day as Row = 2, Col = DOW, then calculate an offset to the date pointed to. Then add the two. }
Day1 := DMYToDate(1, Calendar.Month, Calendar.Year); DOW := Ord(DayOfWeek(Day1)) + 1;
Idx := (RNum - 2) * 7; if CNum < DOW
then Idx := Idx - (DOW - CNum) else if CNum > DOW
then Idx := Idx + (CNum - DOW); Result := IncDate(Day1, Idx, 0, 0);
end; { DatePointedTo }
begin
RHeight := Calendar.ClientHeight div 7; RNum := Y div RHeight + 1;
CWidth := Calendar.ClientWidth div 7; CNum := X div CWidth + 1;
{ Put the date and task in the string list }
s := DateTimeToStr(OvcDateToDateTime(DatePointedTo)) + ' - ' + EditBox.Text;
StringGrid.Cells[0, StringGrid.RowCount - 1] := s;
{ Add a blank row to the string list } StringGrid.RowCount := StringGrid.RowCount + 1;
EditBox.Text := '';
end;
end.
End of entry, March 19.
The sinister figure leaned forward, intently reading the purloined Casebook. The dark eyes swept across to the next page.
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
{ Last Updated 3/20/96 } {———————————————————————————————————————————————————}
unit PakTable;
interface
uses
SysUtils, Dialogs, DBTables, DBiTypes, DBiProcs, DBiErrs;
function PackTable(var ATable : TTable) : Boolean;
implementation
type
EDBPackMisc = class(Exception);
var
ActiveStatus : Boolean; ExclusiveStatus : Boolean; Error : DBiResult; ErrorMsg : DBiMsg; pTableDesc : pCRTblDesc; AHandle : hDBiDB;
{PackTable packs the records (and in the case of dBASE tables, actually removes records previously marked for deletion) in Paradox and dBASE tables. The TableType property of the table to be packed must
be either ttParadox or ttDBase; ttDefault will not work. Also, the table must not be in use by anyone else, because it has to be put in Exclusive mode. }
function PackTable(var ATable : TTable) : Boolean; begin
Result := False; try
with ATable do begin
{Save the current status of the table } ActiveStatus := Active;
ExclusiveStatus := Exclusive;
{Disconnect the table from controls and make it exclusive } DisableControls;
Active := False; Exclusive := True;
end; { with }
try
{ Pack the table, depending on the table's type } case ATable.TableType of
ttParadox : begin
{ Create a description table and prepare it for use } GetMem(pTableDesc, SizeOf(CRTblDesc)); FillChar(pTableDesc^, SizeOf(CRTblDesc), 0);
with pTableDesc^ do begin
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.