Play Script Unit

The code of the play script unit

unit Playscript;
{
    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, strutils,
  Dialogs, ComCtrls, ExtCtrls, ToolWin, StdCtrls, Menus, AppEvnts, ImgList, inifiles,
  shlobj, Gauges, Grids, Calendar, richedit;

type
  TPlayscript_frm = class(TForm)
    ToolBar1: TToolBar;
    tbtn_New: TToolButton;
    tbtn_Save: TToolButton;
    Tbtn_open: TToolButton;
    tbtn_Print: TToolButton;
    ToolButton5: TToolButton;
    tbtn_Cut: TToolButton;
    tbtn_Copy: TToolButton;
    tbtn_Paste: TToolButton;
    tbtn_Undo: TToolButton;
    Tbtn_redo: TToolButton;
    ToolButton21: TToolButton;
    tbtn_Bold: TToolButton;
    tbtn_Italic: TToolButton;
    Tbtn_Underlined: TToolButton;
    ToolButton13: TToolButton;
    ComboFont: TComboBox;
    text_size_combo: TComboBox;
    ToolButton1: TToolButton;
    ColorBox1: TColorBox;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton6: TToolButton;
    Button1: TButton;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    New1: TMenuItem;
    Save1: TMenuItem;
    Save2: TMenuItem;
    SaveAs1: TMenuItem;
    Print1: TMenuItem;
    ExitProgramsavesonexit1: TMenuItem;
    Edit1: TMenuItem;
    Copy1: TMenuItem;
    Cut1: TMenuItem;
    Copy2: TMenuItem;
    Paste1: TMenuItem;
    Wordcount: TMenuItem;
    Resources1: TMenuItem;
    Generalresources1: TMenuItem;
    ProjectResources1: TMenuItem;
    Notes1: TMenuItem;
    ProjectNotes1: TMenuItem;
    ProjectNotes2: TMenuItem;
    DeadlinePlanner1: TMenuItem;
    Options1: TMenuItem;
    WritingMode1: TMenuItem;
    ImageList1: TImageList;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    PrintDialog1: TPrintDialog;
    Panel1: TPanel;
    Chapter_page: TPageControl;
    TabSheet1: TTabSheet;
    RichEdit1: TRichEdit;
    Panel4: TPanel;
    Panel3: TPanel;
    TreeView1: TTreeView;
    Characters_tree: TTreeView;
    Panel2: TPanel;
    Splitter1: TSplitter;
    Label1: TLabel;
    Label2: TLabel;
    Splitter2: TSplitter;
    Character: TMenuItem;
    Scene1: TMenuItem;
    Richedit_menu: TPopupMenu;
    Popupundo: TMenuItem;
    N8: TMenuItem;
    PopUpCut: TMenuItem;
    PopUpCopy: TMenuItem;
    PopUpPaste: TMenuItem;
    PopUpDelete: TMenuItem;
    N1: TMenuItem;
    PopUpSelectAll: TMenuItem;
    Part_menu_btn: TMenuItem;
    Act_menu_btn: TMenuItem;
    Scene_menu_btn: TMenuItem;
    StatusBar1: TStatusBar;
    Help1: TMenuItem;
    PlayscriptFile1: TMenuItem;
    N2: TMenuItem;
    procedure Splitter1Moved(Sender: TObject);
    procedure Splitter2Moved(Sender: TObject);
    procedure Part_menu_btnClick(Sender: TObject);
    procedure Act_menu_btnClick(Sender: TObject);
    procedure Scene_menu_btnClick(Sender: TObject);
    procedure CharacterClick(Sender: TObject);
    procedure Open1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure SaveAs1Click(Sender: TObject);
    procedure Print1Click(Sender: TObject);
    procedure Undo1click(Sender: TObject);
    procedure Cut1Click(Sender: TObject);
    procedure Copy2Click(Sender: TObject);
    procedure Paste1Click(Sender: TObject);
    procedure WordcountClick(Sender: TObject);
    procedure Generalresources1Click(Sender: TObject);
    procedure ProjectResources1Click(Sender: TObject);
    procedure ProjectNotes2Click(Sender: TObject);
    procedure DeadlinePlanner1Click(Sender: TObject);
    procedure Options1Click(Sender: TObject);
    procedure WritingMode1Click(Sender: TObject);
    procedure tbtn_BoldClick(Sender: TObject);
    procedure tbtn_ItalicClick(Sender: TObject);
    procedure Tbtn_UnderlinedClick(Sender: TObject);
    procedure ComboFontChange(Sender: TObject);
    procedure ColorBox1Change(Sender: TObject);
    procedure Tbtn_redoClick(Sender: TObject);
    procedure tbtn_NewClick(Sender: TObject);
    procedure TreeView1DblClick(Sender: TObject);
    procedure text_size_comboChange(Sender: TObject);
    procedure PopUpDeleteClick(Sender: TObject);
    procedure PopUpSelectAllClick(Sender: TObject);
    procedure RichEdit1KeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
    procedure RichEdit1MouseUp(Sender: TObject; Button: TMouseButton;
                               Shift: TShiftState; X, Y: integer);
    procedure Richedit_menuPopup(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure RichEdit1SelectionChange(Sender: TObject);
    procedure RichEdit1KeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure Help1Click(Sender: TObject);
    procedure ExitProgramsavesonexit1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    open_file, creating: boolean;
    File_save_path: string;
    target: integer;
  end;

var
  Playscript_frm: TPlayscript_frm;
  tabs, wordscount, Startwordcount, writingmodecount, writingmodestart,
  writingmodetarget: integer;
  tempcomp: TComponent;
  ClassName: TComponent;
  tabname: string;
  Writing_mode, creating: boolean;

implementation

uses deadline_planner, options, notes, resources, main_menu;

{$R *.dfm}

procedure Get_Active_Component;
var
  i: integer;
begin
  // inspiration from http://www.festra.com/wwwboard/messages/2249.html
  if playscript_frm.activecontrol <> nil then
  begin
    for i := 0 to playscript_frm.ComponentCount - 1 do
    begin
      if (i > playscript_frm.componentcount - playscript_frm.chapter_page.pagecount - 1)
      then  {as long as the active component is a richedit
              As the only components created are richedits, and the amount created will be the tab count anything
              greater than componentcount - tab count will be a richedit (need to minus 1 as the array starts at 0) }
      begin
        if LowerCase(playscript_frm.Components[i].Name) = LowerCase(playscript_frm.ActiveControl.Name) then
        begin    //checks if the name of the component is equal to the active control
          TempComp := playscript_frm.Components[i];
          //tempcomp is now equal to the active richedit
        end;
      end;
    end;
  end;
end;

procedure countwords;
var
  countword, character_count, pointer: integer;
  strings: string;
  i: integer;
begin
  countword := 0;
  wordscount := 0;
  for I := 0 to Playscript_frm.chapter_page.pagecount - 1 do
  begin
    tempcomp := Playscript_frm.Components[Playscript_frm.componentcount +
                                          i - Playscript_frm.chapter_page.pagecount];
  end;
  // Removes linebreaks, replacing them with spaces, and then trims all the excess spaces
  strings := trim(StringReplace((tempcomp as trichedit).Text, sLineBreak, ' ', [rfReplaceAll]));
  //Finds the length of the whole string, then minuses the length of the string minus spaces
  //then adds one to include the last word.
  pointer := 1;
  if strings <> '' then
  begin
    repeat
      if (strings[pointer] = ' ') and (strings[pointer + 1] = ' ') then
      begin
        Delete(strings, pointer, 1);
      end
      else
      begin
        Inc(pointer);
      end;
      character_count := length(strings);
    until pointer = character_count + 1;
    countword := length(strings) - length(StringReplace(strings, ' ', '', [rfReplaceAll])) + 1;
  end
  else
    countword := 0;
  wordscount := wordscount + countword;
  if writing_mode = True then
  begin
    writingmodecount := wordscount - writingmodestart;
    Playscript_frm.statusbar1.panels[1].Text := 'Writing Mode Count: ' + IntToStr(writingmodecount);
    if writingmodecount = writingmodetarget then
    begin
      Playscript_frm.BorderStyle := bssizeable;
      Playscript_frm.WindowState := wsnormal;
      Playscript_frm.formstyle := fsnormal;
      Playscript_frm.toolbar1.Show;
      Playscript_frm.menu := Playscript_frm.mainmenu1;
      writing_mode := not writing_mode;
    end;
  end;
  if options_frm.Progress_deadline = 2 then
    wordscount := wordscount - startwordcount;
  Playscript_frm.statusbar1.panels[0].Text := format('Wordcount\Target: %s/%s', [IntToStr(wordscount),
                                                     IntToStr(playscript_frm.target)]);
end;

function BrowseDialog(const Title: string; const Flag: integer): string;
var
  lpItemID: PItemIDList;
  BrowseInfo: TBrowseInfo;
  DisplayName: array[0..MAX_PATH] of char;
  TempPath: array[0..MAX_PATH] of char;
begin
  Result := '';
  FillChar(BrowseInfo, sizeof(TBrowseInfo), #0);
  with BrowseInfo do
  begin
    hwndOwner := Application.Handle;
    pszDisplayName := @DisplayName;
    lpszTitle := PChar(Title);
    ulFlags := Flag;
  end;
  lpItemID := SHBrowseForFolder(BrowseInfo);
  if lpItemId <> nil then
  begin
    SHGetPathFromIDList(lpItemID, TempPath);
    Result := IncludeTrailingBackslash(TempPath);
    GlobalFreePtr(lpItemID);
  end;
end;

procedure writingmode;
var
  input: string;
  Errorpos, checkint: integer;
begin
{toggles writing mode on and off, if on then the form cannot be resized, nor exited
as well as the fact that the form will stay on top}
  if not writing_mode then
  begin
    repeat
      input := inputbox('Writing Mode.', 'How many words do you want to write in writingmode?', '1');
      val(input, checkint, errorpos);
      if not errorpos = 0 then
        ShowMessage('Please enter a valid integer number');
    until (input <> '') and (errorpos = 0);
    Playscript_frm.BorderStyle := bsnone;
    Playscript_frm.WindowState := wsmaximized;
    Playscript_frm.formstyle := fsstayontop;
    countwords;
    Writingmodestart := wordscount;
    Writingmodecount := 0;
    writing_mode := not writing_mode;
    Playscript_frm.toolbar1.hide;
    Playscript_frm.menu := nil;
  end;
end;

procedure Save_program();
var
  i: integer;
  node_count: integer;
  tabname: string;
  Ini: tinifile;
  myfile: textfile;
  jrgfile_path: string;
begin
  try
    if not main_menu_frm.saved_before then
    begin
      repeat
        if not inputquery('Saving...', 'Please choose the name of the project.',
                          main_menu_frm.filename) then
          exit;
      until main_menu_frm.filename <> '';
      jrgfile_path := BrowseDialog('Select a folder to save the shortcut file.',
                                   BIF_RETURNONLYFSDIRS);
      if jrgfile_path = '' then
        Exit;
      Playscript_frm.File_save_path := format('%s%s\', [main_menu_frm.path, main_menu_frm.filename]);
      main_menu_frm.saved_before := True;
      // use File_save_path to get the place where ini file has to be saved to
      forcedirectories(format('%soptions\', [Playscript_frm.file_save_path]));
      forcedirectories(format('%sDeadlines\', [Playscript_frm.file_save_path]));
      Ini := tinifile.Create(Playscript_frm.file_save_path + 'Options\Options.ini');
      ini.writeinteger('Main Section', 'Type of file', 2);
      ini.writeinteger('Main Section', 'Progress bar', 1);
    end;
    countwords;
    ini.WriteInteger('Main Section', 'Word Count', wordscount);
    // Use File_save_path to save the deadline planner and resources (notes will be on seperate form)
    // Save each tab into a new file at File_save_path
    Playscript_frm.treeview1.SaveToFile(format('%sTreeview.txt', [Playscript_frm.file_save_path]));
    Playscript_frm.Characters_tree.SaveToFile(format('%sCharacters.txt',
                                                     [Playscript_frm.file_save_path]));
    for i := 0 to Playscript_frm.chapter_page.pagecount - 1 do
    begin
      tempcomp := Playscript_frm.Components[Playscript_frm.componentcount + i -
                                            Playscript_frm.chapter_page.pagecount];
      tabname := format('%s%s.rtf', [Playscript_frm.file_save_path,
                                     Playscript_frm.Chapter_page.Pages[i].Caption]);
      if fileexists(tabname) then
      begin
        if deletefile(tabname) then
        else
          ShowMessage('Sorry, there has been an error overwriting the file so the program cannot save right now.');
        (tempcomp as TRichEdit).Lines.SaveToFile(tabname);
      end;
    end;
    ShowMessage('Saved program');
    Assign(myfile, format('%s\%s.jrg', [jrgfile_path, Main_menu_frm.filename]));
    rewrite(myfile);
    writeln(myfile, 'JRG file');
    closefile(myfile);
  except

  end;
end;

procedure Load_Program();
begin
  if Playscript_frm.opendialog1.Execute() then
  begin
    main_menu_frm.Opened_with := playscript_frm.opendialog1.filename;
    playscript_frm.hide;
    main_menu_frm.open_file;
  end;
end;

procedure Create_new_tab(Name_of_Tab: string);
var
  TabSheet: TTabSheet;
  tabcount: integer;
  pagecount: string;
  i: integer;
begin
  if Playscript_frm.treeview1.selected <> nil then
  begin
    for I := 0 to tabcount - 1 do
    begin
      if lowercase(Playscript_frm.chapter_page.pages[i].Caption) =
        lowercase(Playscript_frm.treeview1.selected.Text) then
      begin
        ShowMessage('That file is currently open');
        exit;
      end;
    end;
  end;
  creating := False;
  // code from http://stackoverflow.com/questions/12233205/dynamically-add-tab-sheets-to-page-control-and-embed-a-form
  // tab control - items belonged to contorl not tabs - page belongs to page, not control.
  TabSheet := TTabSheet.Create(Playscript_frm.Chapter_page);
  TabSheet.Caption := Name_of_tab;
  TabSheet.PageControl := Playscript_frm.Chapter_page;
  tabcount := Playscript_frm.Chapter_page.PageCount;
  // end;
  // code from stackoverflow.com ends
  with Trichedit.Create(Playscript_frm) do
  begin
    parent := tabsheet;
    anchors := [akLeft, akTop, akRight, akBottom];
    alignwithmargins := True;
    Enabled := True;
    Visible := True;
    wanttabs := True;
    Width := Playscript_frm.chapter_page.Width - 3;
    Height := Playscript_frm.chapter_page.Height - 3;
    Name := Name_of_tab + 'File';
    scrollbars := ssvertical;
    hidescrollbars := False;
    onkeydown := Playscript_frm.RichEdit1keydown;
    onmouseup := Playscript_frm.RichEdit1mouseup;
    OnSelectionChange := Playscript_frm.RichEdit1SelectionChange;
    onkeyup := Playscript_frm.richedit1keyup;
    //Had left it as onkeypress so nothing was happening, resulting in no increase in word count
    if Playscript_frm.open_file then
    begin
      Lines.LoadFromFile(format('%s%s\%s.rtf', [Main_menu_frm.path, Main_menu_frm.filename, name_of_tab]));
      Playscript_frm.open_file := False;
    end;
  end;
end;

procedure TPlayscript_frm.Act_menu_btnClick(Sender: TObject);
var
  name_of_part: string;
  i: integer;
begin
  if treeview1.selected <> nil then
  begin
    begin
      repeat
        Name_of_part := InputBox('New Tab', 'Please choose the name of the Act.', 'Untitled');
      until Name_of_part <> '';
      for i := 0 to playscript_frm.TreeView1.Items.Count - 1 do
      begin
        if lowercase(playscript_frm.treeview1.items[i].Text) = lowercase(Name_of_part) then
        begin
          ShowMessage('That file already exists.');
          exit;
        end;
      end;
      Create_new_tab(Name_of_part);
      treeview1.items.addchild(treeview1.Selected, name_of_part);
      treeview1.Selected.expand(False);
    end;
  end
  else
    ShowMessage('Please select a Part to put the Act in');
end;

procedure TPlayscript_frm.CharacterClick(Sender: TObject);
var
  Name_of_Character: string;
begin
  repeat
    Name_of_Character := InputBox('New Tab', 'Please choose the name of the Scene.', 'Untitled');
  until Name_of_Character <> '';
  Characters_tree.items.addchild(nil, name_of_character);//Add a character
end;

procedure TPlayscript_frm.ColorBox1Change(Sender: TObject);
begin
  get_active_component;
  (tempcomp as trichedit).SelAttributes.Color := colorbox1.selected;
  //changes the colour of the selected text to the colourbox's selcted item
end;

procedure TPlayscript_frm.ComboFontChange(Sender: TObject);
begin
  get_active_component;
  (tempcomp as trichedit).SelAttributes.Name := ComboFont.Text;
  //changes the font of the selected text to the combofont's selected item
end;

procedure TPlayscript_frm.Undo1click(Sender: TObject);
begin
  get_active_component;
  (tempcomp as trichedit).undo;//Undoes last action
end;

procedure TPlayscript_frm.Copy2Click(Sender: TObject);
begin
  get_Active_component;
  (tempcomp as trichedit).copytoClipboard; //copies current text from active richedit
end;

procedure TPlayscript_frm.Cut1Click(Sender: TObject);
begin
  get_Active_component;
  (tempcomp as trichedit).cuttoClipboard; //cuts the current text from the active richedit
end;

procedure TPlayscript_frm.DeadlinePlanner1Click(Sender: TObject);
begin
  if main_menu_frm.Saved_Before = True then
    deadline_planner_frm.Show
  else
    ShowMessage('You have not saved this before.');
  //shows the deadline planner so you can plan out your playscript
end;

procedure TPlayscript_frm.ExitProgramsavesonexit1Click(Sender: TObject);
begin
  Halt;
end;

procedure TPlayscript_frm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
var
  Result: integer;
begin
  if writing_mode = True then
    CanClose := False
  else
    CanClose := True;
  //allows the user to close the form as long as writing mode isn't active
  Result := messagedlg('Are you sure you want to close this project? (Will not Save!)',
                       mtConfirmation, mbyesno, 0);
  if Result = mrYes then
  else
    canclose := False;
end;

procedure TPlayscript_frm.FormCreate(Sender: TObject);
begin
  Text_size_combo.Text := '9';
  tempcomp := richedit1;//enables combofont to be filled without an error
     {The next two lines fill the combofont listbox (the one in the quickbar)
     with all the fonts that the user has installed on the computer}
  ComboFont.Items := Screen.Fonts;
  ComboFont.ItemIndex := ComboFont.Items.IndexOf((tempcomp as trichedit).Font.Name);
  playscript_frm.Caption := main_menu_frm.filename;
end;

procedure TPlayscript_frm.FormHide(Sender: TObject);
var
  i: integer;
begin
  creating := True;
  Chapter_page.Pages[0].Destroy;
  Create_new_tab('Closing_tab');
  if chapter_page.PageCount > 1 then
  begin
    for i := Chapter_page.pagecount - 1 to 1 do
    begin
      Chapter_page.Pages[i].Destroy;
    end;
    TreeView1.items.Clear;
  end;
end;

procedure TPlayscript_frm.FormShow(Sender: TObject);
var
  s: string;
  ini: tinifile;
  i: integer;
begin
  for i := chapter_page.PageCount - 1 downto 1 do
    chapter_page.Pages[i].Destroy;
  creating := True;
  Create_new_tab('new_tab');
  Chapter_page.Pages[0].Destroy;  {Had these two statements the other way round, however
  if you destroy the last page in runtime it seems to destroy the tabpage, so couldn't
  create another one, so was throwing an error}
  if main_menu_frm.saved_before then
  begin
    treeview1.LoadFromFile(format('%s%s\treeview.txt', [main_menu_frm.path, Main_menu_frm.filename]));
    tempcomp := playscript_frm.Components[playscript_frm.ComponentCount - chapter_page.pagecount];
    (tempcomp as trichedit).Lines.LoadFromFile(format('%s%s\%s.rtf',
                                               [Main_menu_frm.path, Main_menu_frm.filename, (treeview1.items[0].Text)]));
    Chapter_page.Pages[0].Caption := TreeView1.items[0].Text;
    Ini := tinifile.Create(format('%s%s\Options\Options.ini', [main_menu_frm.path, main_menu_frm.filename]));
    target := ini.readinteger('Main Section', 'Words', 0);
    startwordcount := ini.readinteger('Main Section', 'Word count', 0);
    file_save_path := main_menu_frm.path + Main_menu_frm.filename + '\';
  end
  else
  begin
    repeat
      s := InputBox('New Playscript File', 'Please choose the name of the first Part.', 'Untitled');
    until s <> '';
    Chapter_page.pages[0].Caption := s;
    Target := 0;
    startwordcount := 0;
    treeview1.Items.Add(nil, s);
  end;
end;

procedure TPlayscript_frm.Generalresources1Click(Sender: TObject);
begin
  resources_frm.General_resources := True;
  Resources_frm.Show;
  //opens the resources form with the general resources
end;

procedure TPlayscript_frm.Help1Click(Sender: TObject);
begin
  main_menu_frm.Openhelpfile;
end;

procedure TPlayscript_frm.Options1Click(Sender: TObject);
begin
  options_frm.Show;
end;

procedure TPlayscript_frm.Paste1Click(Sender: TObject);
begin
  get_active_component;
  (tempcomp as trichedit).pastefromclipboard;
end;

procedure TPlayscript_frm.PopUpDeleteClick(Sender: TObject);
begin
  Get_active_component;
  (tempcomp as trichedit).seltext := '';
end;

procedure TPlayscript_frm.PopUpSelectAllClick(Sender: TObject);
begin
  Get_active_component;
  (tempcomp as trichedit).selectall;
end;

procedure TPlayscript_frm.Print1Click(Sender: TObject);
begin
  get_active_component;
  if ((tempcomp as trichedit).SelLength <> 0) then
  begin
    PrintDialog1.PrintRange := prSelection;
  end
  else
    PrintDialog1.PrintRange := prAllPages;
  if printdialog1.Execute then
  begin
    (tempcomp as trichedit).Print(main_menu_frm.filename);
  end;
end;

procedure TPlayscript_frm.ProjectNotes2Click(Sender: TObject);
begin
  notes_frm.general_notes := True;
  notes_frm.Show;  //opens generic notes page
end;

procedure TPlayscript_frm.ProjectResources1Click(Sender: TObject);
begin
  resources_frm.general_resources := False;
  Resources_frm.Show;
  //opens project specific resources
end;

procedure TPlayscript_frm.RichEdit1KeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
begin
  Get_active_component;
  if (key = 8) and ((tempcomp as trichedit).Text = '') then
  begin
    ShowMessage('Nothing to delete!');
    exit;
  end;
  richedit1selectionchange(Sender);
end;

procedure TPlayscript_frm.RichEdit1KeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
begin
  Countwords;
end;

procedure TPlayscript_frm.RichEdit1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
begin
  if button = mbright then
  begin
    richedit_menu.popup(X + playscript_frm.Left, Y + playscript_frm.top);
  end;
end;

procedure TPlayscript_frm.RichEdit1SelectionChange(Sender: TObject);
begin
  if playscript_frm.Visible then
  begin
    get_active_component;
    tbtn_bold.down := fsbold in (tempcomp as trichedit).selattributes.style;
    tbtn_italic.down := fsitalic in (tempcomp as trichedit).selattributes.style;
    tbtn_underlined.down := fsunderline in (tempcomp as trichedit).selattributes.style;
    //checks what format the current text is, and toggles the bold, italic and
    //underline button accordingly
    colorbox1.selected := (tempcomp as trichedit).selattributes.color;
    Text_size_combo.Text := IntToStr((tempcomp as trichedit).SelAttributes.size);
    ComboFont.ItemIndex := ComboFont.Items.IndexOf((tempcomp as TRichEdit).SelAttributes.Name);
  end;
end;

procedure TPlayscript_frm.Richedit_menuPopup(Sender: TObject);
begin
  if (tempcomp as trichedit).seltext = '' then
  begin
    PopUpCut.Enabled := False;
    PopUpCopy.Enabled := False;
    PopUpDelete.Enabled := False;
  end
  else
  begin
    PopUpCut.Enabled := True;
    PopUpCopy.Enabled := True;
    PopUpDelete.Enabled := True;
  end;
end;

procedure TPlayscript_frm.Open1Click(Sender: TObject);
begin
  Load_program;
end;

procedure TPlayscript_frm.Save1Click(Sender: TObject);
begin
  Save_program;
end;

procedure TPlayscript_frm.SaveAs1Click(Sender: TObject);
begin
  Main_menu_frm.Saved_Before := False;
  save_program();// Choose place to save
end;

procedure TPlayscript_frm.Part_menu_btnClick(Sender: TObject);
var
  name_of_part: string;
  i: integer;
begin
  begin
    repeat
      Name_of_part := InputBox('New Tab', 'Please choose the name of the Scene.', 'Untitled');
    until Name_of_part <> '';
    for i := 0 to playscript_frm.TreeView1.Items.Count - 1 do
    begin
      if lowercase(playscript_frm.treeview1.items[i].Text) = lowercase(Name_of_part) then
      begin
        ShowMessage('That file already exists.');
        exit;
      end;
    end;
    Create_new_tab(Name_of_part);
    treeview1.items.addchild(nil, name_of_part);
    treeview1.Selected.Expand(False);
  end;
end;

procedure TPlayscript_frm.Scene_menu_btnClick(Sender: TObject);
var
  name_of_part: string;
  i: integer;
begin
  if treeview1.selected <> nil then
  begin
    if treeview1.Selected.Parent <> nil then
    begin
      repeat
        Name_of_part := InputBox('New Tab', 'Please choose the name of the Scene.', 'Untitled');
      until Name_of_part <> '';
      for i := 0 to playscript_frm.TreeView1.Items.Count - 1 do
      begin
        if lowercase(playscript_frm.treeview1.items[i].Text) = lowercase(Name_of_part) then
        begin
          ShowMessage('That file already exists.');
          exit;
        end;
      end;
      Create_new_tab(Name_of_part);
      treeview1.items.addchild(treeview1.selected, name_of_part);
      treeview1.Selected.Expand(False);
    end
    else
      ShowMessage('Please select a chapter for the scene to be added to.');
  end;
end;

procedure TPlayscript_frm.text_size_comboChange(Sender: TObject);
var
  Catchint, errorpos: integer;
begin
  get_active_component;
  try
    val(text_size_combo.Text, catchint, errorpos);
    if (text_size_combo.Text <> '') and (errorpos = 0) then
    begin
      (tempcomp as trichedit).SelAttributes.Size := StrToInt(text_size_combo.Text);
    end
    else
    begin
      ShowMessage('Invalid input');
    end;
  finally
  end;
end;

procedure TPlayscript_frm.Splitter1Moved(Sender: TObject);
begin
  panel4.Width := playscript_frm.Width - splitter1.Width - panel1.Width - 8;
end;

procedure TPlayscript_frm.Splitter2Moved(Sender: TObject);
begin
  panel3.Height := panel4.Height - splitter2.Height - panel2.Height - 8;
end;

procedure TPlayscript_frm.tbtn_BoldClick(Sender: TObject);
begin
  get_active_component;
  if fsbold in (tempcomp as trichedit).selattributes.Style then
    //toggles bold on/off for selected text; removes it if on, and adds if off
    (tempcomp as trichedit).selattributes.Style := (tempcomp as trichedit).selattributes.Style - [fsbold]
  else
    (tempcomp as trichedit).selattributes.Style := (tempcomp as trichedit).selattributes.Style + [fsbold];
end;

procedure TPlayscript_frm.tbtn_ItalicClick(Sender: TObject);
begin
  get_active_component;
  if fsbold in (tempcomp as trichedit).selattributes.Style then
    //toggles bold on/off for selected text; removes it if on, and adds if off
    (tempcomp as trichedit).selattributes.Style := (tempcomp as trichedit).selattributes.Style - [fsbold]
  else
    (tempcomp as trichedit).selattributes.Style := (tempcomp as trichedit).selattributes.Style + [fsbold];
end;

procedure TPlayscript_frm.tbtn_NewClick(Sender: TObject);
var
  Result, i: integer;
  Input: string;
begin
  Result := messagedlg('Are you sure you want to start a new project?', mtConfirmation, mbyesno, 0);
  if Result = mrYes then
  begin
    repeat
      input := Inputbox('Name of new project.', 'What would you like to name it?', 'Untitled');
    until input <> '';
    create_new_tab(input);
    for I := chapter_page.pagecount - 2 downto 0 do
      chapter_page.Pages[i].Destroy;
    treeview1.Items.Clear;
    treeview1.Items.Add(nil, input);
    main_menu_frm.saved_before := False;
  end
  else
  begin
    exit;
  end;
end;

procedure TPlayscript_frm.Tbtn_redoClick(Sender: TObject);
begin
  get_active_component;
  sendmessage((tempcomp as TRichEdit).handle, EM_redo, 0, 0);
end;

procedure TPlayscript_frm.Tbtn_UnderlinedClick(Sender: TObject);
begin
  get_active_component;
  if fsunderline in (tempcomp as TRichEdit).SelAttributes.style then
    // checks if the current text is underlined
    (tempcomp as TRichEdit).SelAttributes.style := (tempcomp as TRichEdit).SelAttributes.style - [fsunderline]
  // if yes, it makes the text not underlines
  else // and if not, it sets it to underlined
    (tempcomp as TRichEdit).SelAttributes.style := (tempcomp as TRichEdit).SelAttributes.style + [fsunderline];
end;

procedure TPlayscript_frm.TreeView1DblClick(Sender: TObject);
var
  To_open: string;
begin
  to_open := treeview1.selected.Text;
  Open_file := True;
  Create_new_tab(to_open);
end;

procedure TPlayscript_frm.WordcountClick(Sender: TObject);
begin
  countwords;
end;

procedure TPlayscript_frm.WritingMode1Click(Sender: TObject);
begin
  writingmode;//toggles writing mode
end;

end.
Programming - a skill for life!

by Jerzy Griffiths: L6 Age ~17