(***********************************************)
(*                                             *)
(* The CONTROLS 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
  MAIN_ICON = 102;

var
  wc:  TWndClass;

function CreateEditControl(hwndDlg: HWND; WordWrap: Boolean): THandle;
var
  hEdit: THandle;
  Style: DWord;
  Rect: TRect;
  Buffer: Pointer;
  Size: DWord;
  SelStart, SelEnd: Longint;
begin
  if Editor = 0 then
    FillChar(Rect, SizeOf(Rect), 0)
  else begin
    Size := SendMessage(Editor, WM_GETTEXTLENGTH, 0, 0);
    GetMem(Buffer, Size + 1);
    SendMessage(Editor, WM_GETTEXT, Size + 1, DWORD(Buffer));
    SendMessage(Editor, EM_GETSEL, DWORD(@SelStart), DWORD(@SelEnd));
    GetWindowRect(Editor, Rect);
    Rect.Right -:= Rect.Left;
    Rect.Bottom -:= Rect.Top;
    Rect.Left := 0;
    Rect.Top := 27;
    DestroyWindow(Editor);
  end;
  if WordWrap then
    Style := WS_VSCROLL or ES_AUTOVSCROLL or  WS_CHILD or WS_VISIBLE
      and not ES_AUTOHSCROLL or ES_MULTILINE or ES_NOHIDESEL
  else
    Style := WS_VSCROLL or ES_AUTOVSCROLL or WS_CHILD or WS_VISIBLE
      or WS_HSCROLL or ES_AUTOHSCROLL or ES_MULTILINE or ES_NOHIDESEL;
  hEdit := CreateWindowEx(
    0,                                  // no extended styles
    'EDIT',                             // class name
    '',                                 // no title (caption)
    Style,                              // style
    Rect.Left, Rect.Top,                // position
    Rect.Right, Rect.Bottom,            // size
    hwndDlg,                            // parent window
    0,                                  // control identifier
    hInstance,                          // instance
    NIL                                 // no WM_CREATE parameter
   );
  SetFocus(hEdit);
  if Editor <> 0 then
  begin
    SendMessage(hEdit, WM_SETTEXT, 0, DWORD(Buffer));
    SendMessage(hEdit, EM_SETSEL, SelStart, SelEnd);
    SendMessage(hEdit, EM_SCROLLCARET, 0, 0);
    FreeMem(Buffer, Size + 1);
  end;
  Result := hEdit;
end;

function CreateMainWindow(WndProc: Pointer; hMenu: THandle): HWND;
begin
  FillChar(wc,SizeOf(wc),0);
  with wc do begin
    style := CS_HREDRAW + CS_VREDRAW;
    lpfnWndProc := WndProc;
    cbClsExtra := 0;
    cbWndExtra := 0;
    hInstance := System.hInstance;
    hIcon := LoadIcon(System.hInstance, MAKEINTRESOURCE(MAIN_ICON));
    hCursor := LoadCursor(THandle(nil), IDC_ARROW);
    hbrBackGround := COLOR_WINDOW + 1;
    lpszMenuName := NIL;
    lpszClassName := 'TMT Simple Text Editor';
  end;

  if RegisterClass(wc) = 0 then halt;

  Result := CreateWindow(wc.lpszClassName, 'Simple Text Editor',
    WS_SYSMENU or WS_CAPTION or WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
    CW_USEDEFAULT, 0, 0, hMenu, hInstance, nil);
end;

function CreateMainToolBar: HWND;
const
  CommandID: array [0..10] of DWORD =
    (
     ID_FILE_NEW, ID_FILE_OPEN, ID_FILE_SAVE_AS, 0, ID_EDIT_FIND,
     ID_EDIT_FIND_NEXT, 0, ID_EDIT_CUT, ID_EDIT_COPY, ID_EDIT_PASTE, ID_APP_ABOUT
    );
var
  lpBtn: array [0..10] of TTBButton;
  i: DWord;
begin
  for i := 0 to 10 do
  begin
    lpBtn[i].iBitmap := i;
    lpBtn[i].idCommand := CommandID[i];
    lpBtn[i].fsState := TBSTATE_ENABLED;
    if CommandID[i] = 0 then
      lpBtn[i].fsStyle := TBSTYLE_SEP
    else
      lpBtn[i].fsStyle := TBSTYLE_BUTTON;
    lpBtn[i].dwData := 0;
    lpBtn[i].iString := 0;
  end;
  Result := CreateToolbarEx(
    MainWindow,
    WS_CHILD,
    103,
    11,
    hInstance,
    103,
    @lpBtn,
    11,
    16,
    15,
    16,
    15,
    SizeOf(TTBButton)
   );
end;