{*****************************************************************************
 *
 *  FMToDoProp
 *
 *      ToDo property window
 *
 *  Copyright (c) 2002 Arthur Hoogervorst
 *
 *  Author:     Arthur Hoogervorst
 *  E-mail:     ahooger@istar.ca
 *  Homepage:   http://roundabout.sourceforge.net
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation;
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *----------------------------------------------------------------------------
 *
 *  Revision history:
 *
 *     DATE     REV                 DESCRIPTION
 *  ----------- --- ----------------------------------------------------------
 *  2002        1.0    Initial Release of ToDo property window
 *****************************************************************************}

unit FMToDoProp;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  GradForm, StdCtrls, ExtCtrls, ComCtrls, Buttons, pmToDo, TB97Ctls;

type
  TToDoProperties = class(TdfsGradientForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Label1: TLabel;
    Panel1: TPanel;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    DateTimePicker3: TDateTimePicker;
    Bevel1: TBevel;
    DateTimePicker4: TDateTimePicker;
    DateTimePicker5: TDateTimePicker;
    DateTimePicker6: TDateTimePicker;
    Bevel2: TBevel;
    ComboBox1: TComboBox;
    Label5: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    SpeedButton1: TToolbarButton97;
    SpeedButton2: TToolbarButton97;
    SpeedButton3: TToolbarButton97;
    SpeedButton4: TToolbarButton97;
    SpeedButton5: TToolbarButton97;
    Label2: TLabel;
    Edit1: TEdit;
    procedure dfsGradientFormDestroy(Sender: TObject);
    procedure dfsGradientFormCreate(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    CurrentRecord : TToDoItem;
    CurrentStream : TToDoFile;

    procedure ReadFields(arecord : LongInt; Category : string);
    procedure WriteFields(ARecord : LongInt);  published
    { Published declarations }
  end;

var
  ToDoProperties : TToDoProperties;

implementation

uses PXStuff;

{$R *.DFM}


procedure TToDoProperties.WriteFields(ARecord : LongInt);
begin
  if ComboBox1.ItemIndex = -1 then exit;

  CurrentRecord.Deleted := False;

  CurrentRecord.Priority := 0;
  if SpeedButton1.Down then CurrentRecord.Priority := 1
  else if
    SpeedButton2.Down then CurrentRecord.Priority := 2
  else if
    SpeedButton3.Down then CurrentRecord.Priority := 3
  else if
    SpeedButton4.down then CurrentRecord.Priority := 4
  else if
    SpeedButton5.Down then CurrentRecord.Priority := 5;

  CurrentRecord.Description := Memo1.Text;
  CurrentRecord.KeyWord := Edit1.Text;

  CurrentRecord.StartDate := 0;
  if CheckBox1.Checked then
    CurrentRecord.StartDate := Int(DateTimePicker1.Date) +
      Frac(DateTimePicker4.Time);

  CurrentRecord.DueDate := 0;
  if CheckBox2.Checked then
    CurrentRecord.DueDate := Int(DateTimePicker2.Date) +
      Frac(DateTimePicker5.Time);

  CurrentRecord.EndDate := 0;
  if CheckBox3.Checked then
    CurrentRecord.EndDate := Int(DateTimePicker3.Date)
      + Frac(DateTimePicker6.Time);
  StrPCopy(CurrentRecord.Notes, Memo2.Text);

  CurrentStream := TToDoFile.Create(sToDoFolder +
      ComboBox1.Items.Strings[ComboBox1.ItemIndex]  + '.ptd', fmOpenReadWrite);
  if ARecord > 0 then
  begin
    CurrentStream.ToDoItem[ARecord] := CurrentRecord;
  end
  else
  begin
    CurrentStream.Add(CurrentRecord);
  end;
  CurrentStream.Free;
end;

procedure TToDoProperties.ReadFields(arecord : LongInt; Category : string);
begin
  ComboBox1.Enabled := (arecord <= 0);
  ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(Category);
  if (arecord <= 0) then
  begin
    CurrentRecord.Deleted := False;
    CurrentRecord.Priority := 1;
    CurrentRecord.Description := '';
    CurrentRecord.Keyword := '';
    CurrentRecord.StartDate := 0;
    CurrentRecord.DueDate := 0;
    CurrentRecord.EndDate := 0;
  end
  else
  begin
    CurrentStream := TToDoFile.Create(sToDoFolder +
      Category + '.ptd', fmOpenReadWrite);
    CurrentRecord := CurrentStream.ToDoItem[ARecord];
    CurrentStream.Free;
  end;

  Memo1.Text := CurrentRecord.Description;
  case CurrentRecord.Priority of
    1 : SpeedButton1.Down := True;
    2 : SpeedButton2.Down := True;
    3 : SpeedButton3.Down := True;
    4 : SpeedButton4.Down := True;
    5 : SpeedButton5.Down := True;
  end;

  DateTimePicker1.Date := Now;
  DateTimePicker4.Time := Now;
  if CurrentRecord.StartDate > 0 then
  begin
    CheckBox1.Checked := True;
    DateTimePicker1.DateTime := Int(CurrentRecord.StartDate);
    DateTimePicker4.DateTime := Frac(CurrentRecord.StartDate);
  end;

  DateTimePicker2.Date := Now;
  DateTimePicker5.Time := Now;
  if CurrentRecord.DueDate > 0 then
  begin
    CheckBox2.Checked := True;
    DateTimePicker2.DateTime := Int(CurrentRecord.DueDate);
    DateTimePicker5.DateTime := Frac(CurrentRecord.DueDate);
  end;

  DateTimePicker3.Date := Now;
  DateTimePicker6.Time := Now;
  if CurrentRecord.EndDate > 0 then
  begin
    CheckBox3.Checked := True;
    DateTimePicker3.DateTime := Int(CurrentRecord.EndDate);
    DateTimePicker6.DateTime := Frac(CurrentRecord.EndDate);
  end;

  Memo2.Text := TrimRight(CurrentRecord.Notes);
end;

procedure TToDoProperties.dfsGradientFormDestroy(Sender: TObject);
begin
  CurrentStream := nil;
  ToDoProperties := nil;
end;

procedure TToDoProperties.dfsGradientFormCreate(Sender: TObject);
begin
  ComboBox1.Items.Text := GetCategories(sToDoFolder);
  // Just set it...
  ComboBox1.ItemIndex := Ord(ComboBox1.Items.Count > 0) - 1;
end;

procedure TToDoProperties.CheckBox1Click(Sender: TObject);
begin
  if Sender = CheckBox1 then
  begin
    DateTimePicker1.Enabled := CheckBox1.Checked;
    DateTimePicker4.Enabled := CheckBox1.Checked;
  end;

  if Sender = CheckBox2 then
  begin
    DateTimePicker2.Enabled := CheckBox2.Checked;
    DateTimePicker5.Enabled := CheckBox2.Checked;
  end;

  if Sender = CheckBox3 then
  begin
    DateTimePicker3.Enabled := CheckBox3.Checked;
    DateTimePicker6.Enabled := CheckBox3.Checked;
  end;

end;

procedure TToDoProperties.Memo1Change(Sender: TObject);
var l : integer;
begin
  l := 32;
  if Length(Memo1.Text) < 33 then
    l := Length(Memo1.Text);
  Edit1.Text := StringReplace((Copy(Memo1.Text, 1, l)), ' ', '_',
   [rfReplaceAll]);
end;

initialization
   PMRegistry.AddCustomClass(TToDoProperties, 'TToDoProperties');

end.
