Notes Unit

The code of the notes unit

unit Notes;
{
    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, StdCtrls, ComCtrls, Menus, TabNotBk, main_menu, novel, strutils;

type
  TNotes_frm = class(TForm)
    MainMenu1: TMainMenu;
    Exit1: TMenuItem;
    NewTab1: TMenuItem;
    DeleteTab1: TMenuItem;
    DeleteFile1: TMenuItem;
    LoadFile1: TMenuItem;
    Find1: TMenuItem;
    Chapter_page: TPageControl;
    TabSheet1: TTabSheet;
    Memo1: TMemo;
    RenameTab1: TMenuItem;
    Memo_menu: TPopupMenu;
    Tab_menu: TPopupMenu;
    NewTab2: TMenuItem;
    RenameTab2: TMenuItem;
    DeleteTab2: TMenuItem;
    Save1: TMenuItem;
    OpenDialog1: TOpenDialog;
    procedure NewTab1Click(Sender: TObject);
    procedure RenameTab1Click(Sender: TObject);
    procedure DeleteTab1Click(Sender: TObject);
    procedure DeleteFile1Click(Sender: TObject);
    procedure LoadFile1Click(Sender: TObject);
    procedure Find1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RenameTab2Click(Sender: TObject);
    procedure DeleteTab2Click(Sender: TObject);
    procedure Chapter_pageMouseUp(Sender: TObject; Button: TMouseButton;
                                  Shift: TShiftState; X, Y: integer);
    procedure FormHide(Sender: TObject);
    procedure Save1Click(Sender: TObject);
  private
    { Private declarations }
  public
    general_notes: boolean;
    { Public declarations }
  end;

var
  Notes_frm: TNotes_frm;
  tempcomp: TComponent;
  Tabname: string;
  File_save_path: string;
  opening: boolean;

implementation

{$R *.dfm}

procedure save_notes;
var
  i: integer;
  savepath: string;
begin
  for I := 0 to notes_frm.chapter_page.pagecount - 1 do
  begin
    tempcomp := Notes_frm.Components[notes_frm.componentcount + i -
                                     notes_frm.chapter_page.pagecount];
    Savepath := format('%s%s.txt', [File_save_path,
                       notes_frm.chapter_page.pages[i].Caption]);
    (tempcomp as tmemo).Lines.SaveToFile(savepath);
  end;
end;

procedure Delete_tab;
begin
  (tempcomp as TTabSheet).Free;
end;

procedure Rename_tab;
var
  Name_of_part, delete_path: string;
begin
  Delete_path := format('%s%s.txt', [File_save_path,
                        notes_frm.chapter_page.pages[notes_frm.chapter_page.ActivePageindex].Caption]);
  if fileexists(delete_path) then
  begin
    if deletefile(delete_path) then
      repeat
        Name_of_part := InputBox('New Tab', 'Please choose the name of the Notes.', 'Untitled');
      until Name_of_part <> '';
  end
  else
  begin
    repeat
      Name_of_part := InputBox('New Tab', 'Please choose the name of the Notes.', 'Untitled');
    until Name_of_part <> '';
  end;
  (tempcomp as TTabSheet).Caption := name_of_part;
  save_notes;
end;

procedure New_Tab(Name_of_part: string);
var
  TabSheet: TTabSheet;
  tabcount: integer;
  pagename: string;
begin
  pagename := stringreplace(name_of_part, ' ', '', [rfreplaceall]);
  TabSheet := TTabSheet.Create(notes_frm.Chapter_page);
  TabSheet.Caption := Name_of_part;
  TabSheet.PageControl := notes_frm.Chapter_page;
  tabcount := notes_frm.Chapter_page.PageCount;
  tabsheet.Name := pagename;
  with Tmemo.Create(notes_frm) do
  begin
    parent := tabsheet;
    anchors := [akLeft, akTop, akRight, akBottom];
    alignwithmargins := True;
    Enabled := True;
    Visible := True;
    wanttabs := True;
    Width := notes_frm.chapter_page.Width - 3;
    Height := notes_frm.chapter_page.Height - 3;
    Name := 'Memo' + Pagename;
    scrollbars := ssvertical;
    if opening then
    begin
      Lines.LoadFromFile(format('%s%s.txt', [File_save_path, name_of_part]));
      Opening := False;
    end
    else
    begin
      Lines.Delete(0);
    end;
  end;
end;

procedure TNotes_frm.Chapter_pageMouseUp(Sender: TObject; Button: TMouseButton;
                                         Shift: TShiftState; X, Y: integer);
begin
  tempcomp := chapter_page.pages[chapter_page.IndexOfTabAt(X, Y)];
  if (Button = mbRight) then
  begin
    Tab_menu.Popup(X + notes_frm.left, Y + notes_frm.top);
    //without the note_frm meant that it was in the top right if not maximised
  end;
end;

procedure TNotes_frm.DeleteFile1Click(Sender: TObject);
var
  delete_path: string;
begin
  Delete_path := format('%s%s.txt', [File_save_path,
                        chapter_page.pages[chapter_page.ActivePageindex].Caption]);
  tempcomp := chapter_page.ActivePage;
  delete_tab;
  if fileexists(delete_path) then
    if deletefile(delete_path) then
      ShowMessage('File successfully deleted');
  //Find file with filepath then delete it
end;

procedure TNotes_frm.DeleteTab1Click(Sender: TObject);
begin
  chapter_page.ActivePage.Free;
  //Delete_tab;
end;

procedure TNotes_frm.DeleteTab2Click(Sender: TObject);
begin
  Delete_tab;
end;

procedure TNotes_frm.Exit1Click(Sender: TObject);
begin
  notes_frm.Hide;
end;

procedure TNotes_frm.Find1Click(Sender: TObject);
var
  ToBeFound, txt: string;
  StartPos, TxtLen, FoundPos: integer;
begin
  repeat
    inputquery('Find', 'What word do you want to find?', ToBeFound);
  until ToBeFound <> '';
  tempcomp := notes_frm.Components[chapter_page.activepageindex +
                                   notes_frm.componentcount - chapter_page.pagecount];
  (tempcomp as tmemo).SetFocus;
  StartPos := 1;
  TxtLen := Length((tempcomp as tmemo).Text) - StartPos + 1;
  Txt := Copy((tempcomp as tmemo).Text, StartPos, TxtLen);
  FoundPos := Pos(ToBeFound, Txt);
  if FoundPos = 0 then
    ShowMessage('Not found')
  else
  begin
    (tempcomp as tmemo).HideSelection := False; // allways show selection
    (tempcomp as tmemo).SelStart := StartPos + FoundPos - 2;
    (tempcomp as tmemo).SelLength := Length(ToBeFound);
  end;
  //call procdure for finding words in active page
end;

procedure TNotes_frm.FormHide(Sender: TObject);
var
  i: integer;
begin
  Save_notes;
  for i := chapter_page.pagecount - 1 downto 1 do
    chapter_page.pages[i].Destroy;
end;

procedure TNotes_frm.FormShow(Sender: TObject);
var
  File_name: tsearchrec;
  nameoffile, File_path: string;
  First: boolean;
  i: integer;
begin
  chapter_page.Pages[0].Destroy;
  for i := chapter_page.PageCount - 1 downto 1 do
    chapter_page.Pages[i].Destroy;
  First := True;
  new_tab('new_tab');
  if general_notes then
  begin
    File_save_path := format('%sGeneral\notes\', [main_menu_frm.path]);
  end
  else
  begin
    File_save_path := main_menu_frm.path + main_menu_frm.filename + '\notes\';
  end;
  forcedirectories(file_save_path);
  if FindFirst(File_save_path + '*.txt', faanyfile, file_name) = 0 then
  begin
    try
      repeat
        Nameoffile := leftstr(file_name.Name, (length(file_name.Name) - 4));
        if First then
        begin
          tempcomp := notes_frm.Components[notes_frm.componentcount - 1];
          (tempcomp as tmemo).Lines.loadfromfile(
            format('%s%s', [file_save_path, file_name.Name]));
          chapter_page.Pages[0].Caption := nameoffile;
          First := False;
        end
        else
        begin
          opening := True;
          New_tab(Nameoffile);
        end;
      until FindNext(File_name) <> 0;
    finally
      FindClose(File_name);
    end;
  end;
end;

procedure TNotes_frm.LoadFile1Click(Sender: TObject);
var
  nameoffile: string;
begin
  if opendialog1.Execute then
  begin
    nameoffile := extractfilename(opendialog1.FileName);
    nameoffile := leftstr(nameoffile, length(nameoffile) - 4);
    New_tab(Nameoffile);
    tempcomp := notes_frm.Components[notes_frm.componentcount - 1];
    (tempcomp as tmemo).Lines.loadfromfile(opendialog1.FileName);
  end;
end;

procedure TNotes_frm.NewTab1Click(Sender: TObject);
begin
  repeat
    tabname := InputBox('New Tab', 'Please choose the name of the Notes.', 'Untitled');
  until tabname <> '';
  new_tab(tabname);
end;

procedure TNotes_frm.RenameTab1Click(Sender: TObject);
begin
  tempcomp := chapter_page.Components[chapter_page.ActivePageIndex];
  rename_tab;
end;

procedure TNotes_frm.RenameTab2Click(Sender: TObject);
begin
  Rename_tab;
end;

procedure TNotes_frm.Save1Click(Sender: TObject);
begin
  Save_notes;
end;

end.
Programming - a skill for life!

by Jerzy Griffiths: L6 Age ~17