#include        "winplus.h"
#pragma         hdrstop

/*
  1. 18.08.98. Добавлен код, центрующий модальный диалог относительно окна
  - владельца.
  2. 10.10.99. Добавлен конструктор, сохраняющий работоспособность класса при динамической компоновке.
*/

static CDialog  *CurrentModalDialog=NULL;

static BOOL CALLBACK DefaultDlgCallback(HWND hDlg, UINT Message,
  WPARAM wParam, LPARAM lParam )
{
  switch(Message)
  {
    case WM_INITDIALOG:
      if(CurrentModalDialog)
        return CurrentModalDialog->OnInitDialog(hDlg,(HWND)wParam);
      else
        return 1;
    case WM_COMMAND:
      if(CurrentModalDialog)
        return CurrentModalDialog->OnCommand(hDlg,wParam);
      else
        return 0;
  }
  return 0;
}

//  ======================================================================
CDialog::CDialog(LPSTR lpTemplate, HWND hWnd)
{
  ParentInstance=GetModuleHandle(NULL);
  if(hWnd==NULL)
    this->ParentWnd= ::GetFocus();
  else
    this->ParentWnd=hWnd;
  this->lpTemplate=lpTemplate;
}

CDialog::CDialog(int iTemplate, HWND hWnd)
{
  ParentInstance=GetModuleHandle(NULL);
  if(hWnd==NULL)
    this->ParentWnd=::GetActiveWindow();
  else
    this->ParentWnd=hWnd;
  this->lpTemplate=MAKEINTRESOURCE(iTemplate);
}

CDialog::CDialog(int iTemplate, HWND hWnd, HINSTANCE hInst)
{
  ParentInstance=hInst;
  ParentWnd=hWnd;
  lpTemplate=MAKEINTRESOURCE(iTemplate);
}

CDialog::CDialog(LPSTR lpTemplate, CWindowBase &Parent)
{
  ParentInstance=GetModuleHandle(NULL);
  this->ParentWnd=Parent.GetWnd();
  this->lpTemplate=lpTemplate;
}

CDialog::CDialog(int iTemplate, CWindowBase &Parent)
{
  ParentInstance=GetModuleHandle(NULL);
  this->ParentWnd=Parent.GetWnd();
  this->lpTemplate=MAKEINTRESOURCE(iTemplate);
}

BOOL CDialog::DoModal()
{
  if((ParentWnd==NULL)||(lpTemplate==NULL)) return FALSE;
  PrevModalDialog=CurrentModalDialog;
  CurrentModalDialog=this;
  DialogBox(ParentInstance,lpTemplate,ParentWnd,DefaultDlgCallback);
  CurrentModalDialog=PrevModalDialog;
  return TRUE;
}

LRESULT CDialog::OnInitDialog(HWND hDlg, HWND hFocus)
{
  int x,y;
  RECT rc,prc;
  ::GetWindowRect(hDlg,&rc);
  ::GetWindowRect(::GetParent(hDlg),&prc);
  x = ((prc.right-prc.left)-(rc.right-rc.left))/2 + prc.left;
  y = ((prc.bottom-prc.top)-(rc.bottom-rc.top))/2 + prc.top;
  ::SetWindowPos(hDlg,HWND_NOTOPMOST,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
  return 1;
}

LRESULT CDialog::OnCommand(HWND hDlg, WPARAM idControl)
{
  EndDialog(hDlg,0);
  return 0;
}

