(***********************************************)
(*                                             *)
(* The ROUTINES include file                   *)
(* This is a part of the TMT Multimedia Player *)
(* Copyright (c) 1999 by TMT Development Corp. *)
(* Author: Vadim Bodrov, TMT Development Corp. *)
(*                                             *)
(***********************************************)

// Converts TMSF value into seconds
function mci_TMSFtoSeconds(TMSF: DWORD): DWORD;
begin
  Result := mci_TMSF_second(TMSF) + mci_TMSF_minute(TMSF) * 60;
end;

// Converts MSF value into seconds
function mci_MSFtoSeconds(TMSF: DWORD): DWORD;
begin
  Result := mci_MSF_second(TMSF) + mci_MSF_minute(TMSF) * 60;
end;

// Converts seconds into TMSF format
function mci_SecondsToTMSF(Seconds, Pos: DWORD): DWORD;
var
  Minutes: DWORD;
begin
  Minutes := (Seconds div 60) and $FF;
  Result := mci_TMSF_Track(Pos) + (Minutes shl 8) + (((Seconds - Minutes * 60) and $FF) shl 16) +
    (DWORD(mci_TMSF_Frame(Pos)) shl 24);
end;

// Converts seconds into MSF format
function mci_SecondsToMSF(Seconds, Pos: DWORD): DWORD;
var
  Minutes: DWORD;
begin
  Minutes := (Seconds div 60) and $FF;
  Result := Minutes + (((Seconds - Minutes * 60) and $FF) shl 8) + (DWORD(mci_MSF_Frame(Pos)) shl 16);
end;

// Leading zero
function LZ(Arg: Longint): String;
begin
  if Arg < 10 then
    Result := '0' + IntToStr(Arg)
  else
    Result := IntToStr(Arg);
end;

// Returns time
function GetTime(Time: DWORD; ShowMilliseconds: Boolean): String;
var
  H, M, MS: DWORD;
begin
  MS := Time mod 1000;
  Time := Time div 1000;
  H := Time div 3600;
  Time -:= H * 3600;
  M := Time div 60;
  Time -:= M * 60;
  if ShowMilliseconds then
    Result := LZ(Time) + ',' + LZ(MS div 10) + ' sec.'
  else
    Result := LZ(H) + ':' + LZ(M) + ':' + LZ(Time);
end;

// Shows an open dialog
function OpenFile: Boolean;
const
  FILTER = 'Multimedia files'+ #0 + '*.mid;*.rmi;*.avi;*.mpg;*.mp3;*.mpe;*.wav' + #0 +
           'All files' + #0 + '*.*' + #0 +
           'Video for Windows (*.avi)' + #0 + '*.avi' + #0 +
           'Sound (*.wav)'+ #0 + '*.wav' + #0 +
           'MIDI (*.mid, *.rmi)'+ #0 + '*.mid;*.rmi' + #0 +
           + #0#0;
var
  OFN: TOpenFileName;
  FileName: array [0..MAX_PATH] of char;
begin
  FillChar(OFN, SizeOf(OFN), 0);
  OFN.lStructSize := SizeOf(OFN);
  StrPCopy(FileName, MediaFile);
  OFN.lpstrFilter := FILTER;
  OFN.nFilterIndex := 1;
  OFN.lpstrFile := FileName;
  OFN.HInstance := hInstance;
  OFN.nMaxFile  := MAX_PATH;
  Result := GetOpenFileName(OFN);
  MediaFile := StrPas(FileName);
end;

// Extracts the file name
function ExtractFileName(FileName: String): String;
var
  i: Longint;
begin
  if FileName <> '' then
  begin
    i := Length(FileName);
    repeat
      i -:= 1;
    until (FileName[i] in [':', '\', '/']) or (i = 0);
    Result := Copy(fileName, i + 1, Length(FileName) - i);
  end else
    Result := '';
end;

// Updates the statusbar control with the appropiate text
procedure UpdateStatusBar(lpszStatusString: LPSTR; partNumber, displayFlags: WORD);
begin
  SendMessage(hWndStatusbar,
              SB_SETTEXT,
              partNumber or displayFlags,
              LPARAM(lpszStatusString));
end;

// Initialize two-parts status bar
procedure InitializeStatusBar(hwndParent: HWND);
const
  cSpaceInBetween = 8;
var
  ptArray: array [0..39] of Longint;
  rect: TRect;
  hDC: HDC;
begin
  hDC := GetDC(hwndParent);
  GetClientRect(hwndParent, rect);

  ptArray[0] := Round((rect.right - rect.left) / 2.6);
  ptArray[1] := -1;

  ReleaseDC(hwndParent, hDC);
  SendMessage(hWndStatusbar,
              SB_SETPARTS,
              2,
              LPARAM(@ptArray));
end;

// Calls CreateStatusWindow to create the status bar
function CreateSBar(hwndParent: HWND): Boolean;
begin
  hWndStatusbar := CreateStatusWindow(WS_CHILD or WS_VISIBLE or WS_BORDER or SBARS_SIZEGRIP,
                                      '',
                                      hwndParent,
                                      IDM_STATUSBAR);
  if hWndStatusbar <> 0 then
  begin
    InitializeStatusBar(hwndParent);
    Result := TRUE;
  end else
    Result := FALSE;
end;
