(***********************************************)
(*                                             *)
(* The CDLG include file                       *)
(* This is a part of the Simple Text Editor    *)
(* Copyright (c) 1999 by TMT Development Corp. *)
(* Author: Vadim Bodrov, TMT Development Corp. *)
(*                                             *)
(***********************************************)

const
  FILTER = 'Text files (*.txt)'+ #0 + '*.txt' + #0 +
           'All Files (*.*)' + #0+'*.*' + #0 + #0#0;

function AboutDlgProc conv arg_stdcall (Dialog: HWND; Mess: UINT; WParam: WParam; LParam: LParam): LongInt;
const
  MEM_AVAIL_CAPTION = 1000;
  MEM_INUSE_CAPTION = 1001;
var
  Txt: String;
  MS: TMemoryStatus;
begin
  Result := 0;
  case Mess of
    WM_INITDIALOG:
      begin
        GlobalMemoryStatus(MS);
        Txt := IntToStr(MS.dwTotalPhys div 1024) + ' KB' + #0;
        SendDlgItemMessage(Dialog, MEM_AVAIL_CAPTION, WM_SETTEXT, 0, DWORD(@Txt[1]));
        Txt := IntToStr(MS.dwMemoryLoad) + ' %' + #0;
        SendDlgItemMessage(Dialog, MEM_INUSE_CAPTION, WM_SETTEXT, 0, DWORD(@Txt[1]));
      end;

    WM_CLOSE:
      EndDialog(Dialog, 0);

    WM_COMMAND:
      if LoWord(WParam) = IDOK then SendMessage(Dialog, WM_CLOSE, 0, 0);
  end;
end;

function FindDlgProc conv arg_stdcall (Dialog: HWND; Mess: UINT; WParam: WParam; LParam: LParam): Longint;
const
  FIND_BUTTON   = 0001;
  CANCEL_BUTTON = 0002;
  EDIT_BOX      = 1000;
  MATCH_CASE    = 1001;
  FIND_UP       = 1002;
  FIND_DOWN     = 1003;
  WHOLE_WORDS   = 1004;
var
  TextBuf: array [0..255] of char;
begin
  Result := 0;
  case Mess of
    WM_INITDIALOG:
    begin
      SendDlgItemMessage(Dialog, FIND_BUTTON, WM_ENABLE, 0, 0);
      SendDlgItemMessage(Dialog, EDIT_BOX, WM_SETTEXT, 0, DWORD(StrPCopy(TextBuf, SrchString)));
      SetFocus(GetDlgItem(Dialog, EDIT_BOX));
      with FindOptions do
      begin
        SendDlgItemMessage(Dialog, FIND_DOWN, BM_SETCHECK, BST_CHECKED * DWORD(frDown), 0);
        SendDlgItemMessage(Dialog, FIND_UP, BM_SETCHECK, BST_CHECKED * DWORD(not frDown), 0);
        SendDlgItemMessage(Dialog, MATCH_CASE, BM_SETCHECK, BST_CHECKED * DWORD(frMatchcase), 0);
        SendDlgItemMessage(Dialog, WHOLE_WORDS, BM_SETCHECK, BST_CHECKED * DWORD(frWholeWord), 0);
      end;
    end;

    WM_CLOSE:
    begin
      EndDialog(Dialog, 0);
      Result := 1;
    end;

    WM_COMMAND:
      begin
        if LoWord(WParam) = FIND_BUTTON then
        begin
          FillChar(TextBuf, 255, 0);
          SendDlgItemMessage(Dialog, EDIT_BOX, WM_GETTEXT, 255, DWORD(@TextBuf));
          SrchString := StrPas(TextBuf);
          with FindOptions do
          begin
            frDown := SendDlgItemMessage(Dialog, FIND_DOWN, BM_GETCHECK, 0, 0) = BST_CHECKED;
            frMatchCase := SendDlgItemMessage(Dialog, MATCH_CASE, BM_GETCHECK, 0, 0) = BST_CHECKED;
            frWholeWord := SendDlgItemMessage(Dialog, WHOLE_WORDS, BM_GETCHECK, 0, 0) = BST_CHECKED;
          end;
          SetFocus(Editor);
          SearchText(SrchString, FindOptions);
        end;
        if LoWord(WParam) = CANCEL_BUTTON then
          SendMessage(Dialog, WM_CLOSE, 0, 0);
      end;
  end;
end;


procedure eOpenFile(Wnd: HWND; var sFileName: String; AskForOpen: Boolean);
var
  OFN: TOpenFileName;
  FileName: array [0..MAX_PATH] of char;
  FSize: DWord;
  f: File;
  buffer: Pointer;
  h: THandle;
begin
  FillChar(OFN, SizeOf(OFN), 0);
  OFN.lStructSize := SizeOf(OFN);
  StrPCopy(FileName, sFileName);
  OFN.lpstrFilter := Filter;
  OFN.lpstrFile := FileName;
  OFN.HInstance := hInstance;
  OFN.nMaxFile  := MAX_PATH;
  if AskForOpen then
    if not GetOpenFileName(OFN) then exit;
  TextChanged := FALSE;
  sFileName := StrPas(FileName);
  assign(f, sFileName);
{$i-}
  reset(f, 1);
{$i+}
  if IOResult <> NO_ERROR then exit;
  FSize := FileSize(f);
{$i-}
  GetMem(Buffer, FSize + 1);
  FillChar(Buffer^, FSize + 1, 0);
  BlockRead(f, Buffer^, FSize);

  SendMessage(Wnd, WM_SETTEXT, 0, DWORD(Buffer));
  Close(f);
  FreeMem(Buffer, FSize + 1);
end;

procedure eSaveFile(Wnd: HWND; var sFileName: String; NewName: Boolean);
var
  OFN: TOpenFileName;
  FileName: array [0..MAX_PATH] of char;
  FSize: DWord;
  f: File;
  buffer: Pointer;
  h: THandle;
begin
  FillChar(OFN, SizeOf(OFN), 0);
  OFN.lStructSize := SizeOf(OFN);
  StrPCopy(FileName, sFileName);
  OFN.lpstrFilter := Filter;
  OFN.lpstrFile := FileName;
  OFN.HInstance := hInstance;
  OFN.nMaxFile  := MAX_PATH;
  OFN.lpstrDefExt := '.txt';
  if NewName then
  begin
    if not GetSaveFileName(OFN) then exit;
    sFileName := StrPas(FileName);
  end;
  TextChanged := FALSE;
  assign(f, sFileName);
{$i-}
  rewrite(f, 1);
{$i+}
  if IOResult <> NO_ERROR then exit;
  FSize := SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0) + 1;
{$i-}
  GetMem(Buffer, FSize);
  SendMessage(Wnd, WM_GETTEXT, FSize, DWORD(Buffer));
  BlockWrite(f, Buffer^, FSize - 1);
  Close(f);
  FreeMem(Buffer, FSize);
end;

procedure ePageSetupDlg;
var
  ps: TPageSetupDlg;
begin
  FillChar(ps, SizeOf(ps), 0);
  with ps do
  begin
    lStructSize := SizeOf(ps);
    hwndOwner := Editor;
  end;
  PageSetupDlg(ps);
  StartPage(GetWindowDC(Editor));
  EndPage(GetWindowDC(Editor));
end;

