Deadline Planner Unit

The code of the deadline planner unit

unit Deadline_planner;
{
    Copyright (c) 2013 Jerzy Griffiths

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ }
 
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, StdCtrls, novel, inifiles;

type
  TDeadline_Planner_frm = class(TForm)
    To_be_Completed_cal: TMonthCalendar;
    Label1: TLabel;
    Name_edit: TEdit;
    Label2: TLabel;
    Current_tree: TTreeView;
    Save_btn: TButton;
    Delete_btn: TButton;
    Label3: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Words_drop: TComboBox;
    Type_drop: TComboBox;
    Label7: TLabel;
    Change_btn: TButton;
    Notes_edit: TMemo;
    procedure Save_btnClick(Sender: TObject);
    procedure Change_btnClick(Sender: TObject);
    procedure Delete_btnClick(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    deadline_path: string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Deadline_Planner_frm: TDeadline_Planner_frm;

implementation

uses Main_Menu;

{$R *.dfm}

procedure TDeadline_Planner_frm.Change_btnClick(Sender: TObject);
var
  testnode, childnode, parentnode: ttreenode;
  i: integer;
begin
  //Make the selected node the top node
  testnode := current_tree.Selected;
  parentnode := current_tree.items.AddFirst(nil, testnode.Text);
  childnode := Current_tree.items.AddChild(parentnode, 'Type');
  current_tree.items.addchild(childnode, testnode.Item[0].item[0].Text);
  childnode := Current_tree.items.AddChild(parentnode, 'Words');
  current_tree.items.addchild(childnode, testnode.Item[1].item[0].Text);
  childnode := Current_tree.items.AddChild(parentnode, 'Date');
  current_tree.items.addchild(childnode, testnode.Item[2].item[0].Text);
  childnode := Current_tree.items.AddChild(parentnode, 'Description');
  current_tree.items.addchild(childnode, testnode.Item[3].item[0].Text);
  testnode.Delete;
end;

procedure TDeadline_Planner_frm.Delete_btnClick(Sender: TObject);
begin
  //Delete current deadline selected
  current_tree.selected.Delete;
end;

procedure TDeadline_Planner_frm.FormHide(Sender: TObject);
begin
  forcedirectories(deadline_path);
  if current_tree.Items.Count > 0 then
    current_tree.SaveToFile(deadline_path + 'deadlines.txt');
end;

procedure TDeadline_Planner_frm.FormShow(Sender: TObject);
begin
  Deadline_path := format('%swriting program folder\%s\General\Deadlines\',
                          [extractfilepath(application.ExeName), main_menu_frm.filename]);
  if directoryexists(Deadline_path) then
  begin
    current_tree.LoadFromFile(deadline_path + 'deadlines.txt');
  end;
end;

procedure TDeadline_Planner_frm.Save_btnClick(Sender: TObject);
var
  Parentnode: ttreenode;
  Childnode: ttreenode;
  ini: tinifile;
  Catchint, errorpos: integer;
begin
  val(words_drop.Text, catchint, errorpos);
  if not (errorpos = 0) then
  begin
    ShowMessage('Please enter an integer for words');
    exit;
  end;
  if StrToInt(words_drop.Text) < 0 then
  begin
    ShowMessage('Please ensure that the words is positive');
    Exit;
  end;
  if (name_edit.Text <> '') and (type_drop.ItemIndex >= 0) then
  begin
    Parentnode := Current_tree.items.add(nil, name_edit.Text);
    childnode := Current_tree.items.AddChild(parentnode, 'Type');
    current_tree.items.addchild(childnode, type_drop.Items.Strings[type_drop.ItemIndex]);
    childnode := Current_tree.items.AddChild(parentnode, 'Words');
    current_tree.items.addchild(childnode, words_drop.Text);
    childnode := Current_tree.items.AddChild(parentnode, 'Date');
    current_tree.items.addchild(childnode, datetostr(to_be_completed_cal.Date));
    childnode := Current_tree.items.AddChild(parentnode, 'Description');
    current_tree.items.addchild(childnode, Notes_edit.Text);
    Ini := tinifile.Create(main_menu_frm.path + main_menu_frm.filename +
                           '\Options\Options.ini');
    ini.writeinteger('Main Section', 'Words', StrToInt(words_drop.Text));
    Novel_frm.target := StrToInt(words_drop.Text);
  end;
end;

end.
Programming - a skill for life!

by Jerzy Griffiths: L6 Age ~17