unit FormMain;

interface

uses
  Classes, SysUtils, LazFileUtils, Forms, Controls, Graphics, Dialogs, Menus,
  ComCtrls, FormContacts, FormEx;

type

  { TFormMain }

  TFormMain = class(TFormEx)
    CoolBar1: TCoolBar;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItemColumns: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    MenuItemToolbar: TMenuItem;
    MenuItemView: TMenuItem;
    MenuItemExit: TMenuItem;
    MenuItemHomePage: TMenuItem;
    MenuItemAbout: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItemFileNew: TMenuItem;
    MenuItemFileOpen: TMenuItem;
    MenuItemSettings: TMenuItem;
    MenuItemTools: TMenuItem;
    MenuItemFileOpenRecent: TMenuItem;
    MenuItemFileSave: TMenuItem;
    MenuItemFileSaveAs: TMenuItem;
    MenuItemFileClose: TMenuItem;
    MenuItemHelp: TMenuItem;
    MenuItemFile: TMenuItem;
    PopupMenuOpenRecent: TPopupMenu;
    Separator1: TMenuItem;
    Separator2: TMenuItem;
    ToolBarOther: TToolBar;
    ToolBarFile: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormDropFiles(Sender: TObject; const FileNames: array of string);
    procedure FormResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MenuItemColumnsClick(Sender: TObject);
    procedure MenuItemToolbarClick(Sender: TObject);
  private
    procedure SetToolbarHints;
    procedure UpdateFormTitle;
    procedure DataFileChangeExecute(Sender: TObject);
    procedure LastOpenedFileChangeExecute(Sender: TObject);
    procedure LoadConfig;
    procedure SaveConfig;
  public
    FormContacts: TFormContacts;
    procedure UpdateInterface;
    procedure SettingsChanged;
  end;


implementation

{$R *.lfm}

uses
  Core, VCard, VCardFile, RegistryEx;

resourcestring
  SModified = 'Modified';
  SOnlyOneFileCanBeDropped = 'Only one file can be dropped at once.';

{ TFormMain }

procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  SaveConfig;
  FormContacts.Close;
  Application.Terminate;
end;

procedure TFormMain.FormActivate(Sender: TObject);
begin
  Core.Core.Initialize;
end;

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  Core.Core.AFileClose.Execute;
  CanClose := Core.Core.FileClosed;
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  FormContacts := TFormContacts.Create(nil);
  Core.Core.OnDataFileChange := DataFileChangeExecute;
  Core.Core.OnLastOpenedListChange := LastOpenedFileChangeExecute;
  LastOpenedFileChangeExecute(Self);
  LoadConfig;
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FormContacts);
end;

procedure TFormMain.FormDropFiles(Sender: TObject;
  const FileNames: array of string);
begin
  if Length(FileNames) <> 1 then ShowMessage(SOnlyOneFileCanBeDropped)
    else begin
      Core.Core.FileOpen(FileNames[0]);
      Core.Core.UpdateFile;
    end;
end;

procedure TFormMain.FormResize(Sender: TObject);
begin
  CoolBar1.AutosizeBands;
end;

procedure TFormMain.FormShow(Sender: TObject);
begin
  FormContacts.BeginUpdate;
  try
    SetToolbarHints;
    ScaleDPI.ScaleControl(CoolBar1, Core.Core.ScaleDPI1.DesignDPI);
    CoolBar1.AutosizeBands;

    FormContacts.Context := TRegistryContext.Create(Core.Core.ApplicationInfo1.RegistryRoot,
      Core.Core.ApplicationInfo1.RegistryKey + '\ContactsColumns');
    FormContacts.ManualDock(Self, nil, alClient);
    FormContacts.Align := alClient;
    FormContacts.Show;
  finally
    FormContacts.EndUpdate;
  end;
end;

procedure TFormMain.MenuItemColumnsClick(Sender: TObject);
begin
  FormContacts.AColumns.Execute;
end;

procedure TFormMain.MenuItemToolbarClick(Sender: TObject);
begin
  UpdateInterface;
end;

procedure TFormMain.SetToolbarHints;
var
  I: Integer;
  J: Integer;
  Control: TControl;
begin
  for J := 0 to CoolBar1.ControlCount - 1 do begin
    Control := CoolBar1.Controls[J];
    if Control is TToolBar then begin
      for I := 0 to TToolBar(Control).ButtonCount - 1 do begin
        TToolBar(Control).Buttons[I].ShowHint := True;
        TToolBar(Control).Buttons[I].Hint := TToolBar(Control).Buttons[I].Caption;
      end;
    end;
  end;
end;

procedure TFormMain.UpdateFormTitle;
var
  Title: string;
begin
  Title := '';
  if Assigned(Core.Core.DataFile) and
  (ExtractFileNameWithoutExt(ExtractFileName(Core.Core.DataFile.FileName)) <> '') then
    Title := Title + ExtractFileNameWithoutExt(ExtractFileName(Core.Core.DataFile.FileName));
  if Assigned(Core.Core.DataFile) and Core.Core.DataFile.Modified then
    Title := Title + ' (' + SModified + ')';
  if Title <> '' then Title := Title + ' - ';
  Title := Title + Core.Core.ApplicationInfo1.AppName;
  //Application.Title := Title;
  Caption := Title;
end;

procedure TFormMain.DataFileChangeExecute(Sender: TObject);
begin
  if Assigned(FormContacts) then begin
    if Assigned(Core.Core.DataFile) then
      FormContacts.Contacts := TVCardFile(Core.Core.DataFile).VCard.Contacts
      else FormContacts.Contacts := nil;
  end;

  FormContacts.ReloadList;
  FormContacts.UpdateInterface;
  UpdateInterface;
end;

procedure TFormMain.LastOpenedFileChangeExecute(Sender: TObject);
begin
  Core.Core.LastOpenedList1.LoadToMenuItem(MenuItemFileOpenRecent, Core.Core.AFileOpenRecentExecute);
  Core.Core.LastOpenedList1.LoadToMenuItem(PopupMenuOpenRecent.Items, Core.Core.AFileOpenRecentExecute);
end;

procedure TFormMain.LoadConfig;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    MenuItemToolbar.Checked := ReadBoolWithDefault('ToolBarVisible', True);
  finally
    Free;
  end;
end;

procedure TFormMain.SaveConfig;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    WriteBool('ToolBarVisible', MenuItemToolbar.Checked);
  finally
    Free;
  end;
end;

procedure TFormMain.UpdateInterface;
begin
  UpdateFormTitle;
  CoolBar1.Visible := MenuItemToolbar.Checked;
end;

procedure TFormMain.SettingsChanged;
begin
  ThemeManager.UseTheme(Self);
  Translator.TranslateComponentRecursive(Self);
  FormContacts.ThemeManager.UseTheme(FormContacts);
  FormContacts.Translator.TranslateComponentRecursive(FormContacts);
end;

end.

