/////////////////////////////////////////////////////////////////////
// (C) Copyright 1993-1998 by Autodesk, Inc. 
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted, 
// provided that the above copyright notice appears in all copies and 
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting 
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to 
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
// eTestDetail.cpp : implementation file
//
/////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Detail.h"
#include "editExt.h"
#include "DetailDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern "C" HINSTANCE _hdllInstance;
#define APP_ARX_NAME "Detail.arx"

/////////////////////////////////////////////////////////////////////////////
// CTestDetail dialog


CTestDetail::CTestDetail(const AcDbObjectId& Id,CWnd* pParent /*=NULL*/)
	: CDialog(CTestDetail::IDD, pParent),mServerId(Id)
{
	//{{AFX_DATA_INIT(CTestDetail)
	//}}AFX_DATA_INIT
}

CTestDetail::~CTestDetail()
{
}

void CTestDetail::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDetail)
	DDX_Control(pDX, IDC_DRAW_LEADER, m_leader);
	DDX_Control(pDX, IDC_DOT, m_dot);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTestDetail, CDialog)   //CPreferencePage
	//{{AFX_MSG_MAP(CTestDetail)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, OnButtonApply)
	ON_BN_CLICKED(IDC_BUTTON_DEFAULT, OnButtonDefault)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDetail message handlers

BOOL CTestDetail::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
  ReadDialogData();
  
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//It reads all data for this dialog									
BOOL
CTestDetail::ReadDialogData()
{
  TestDetailStd* pDetStd;
  if (acdbOpenObject(pDetStd,mServerId,AcDb::kForRead) != Acad::eOk) return FALSE;
  
  // if draw leader, ....
  if (pDetStd->isLeaderToText())
    m_leader.SetCheck(1);
  else
    m_leader.SetCheck(0);

  // if draw dot at the end of leader, ....
  if (pDetStd->isDotDrawn())
    m_dot.SetCheck(1);
  else
    m_dot.SetCheck(0);

  pDetStd->close();
  return TRUE;
}


//save all variables into the standard
BOOL
CTestDetail::SaveDialogData()
{
  TestDetailStd* pDetStd;
  if (acdbOpenObject(pDetStd,mServerId,AcDb::kForWrite) != Acad::eOk) return FALSE;

  //draw leader ?
  pDetStd->setLeaderToText(m_leader.GetCheck());
  pDetStd->setDotDrawn(m_dot.GetCheck());

  pDetStd->close();
  return TRUE;
}

void CTestDetail::OnButtonApply() 
{
	// TODO: Add your control notification handler code here
  if (! SaveDialogData()) return;
  CWaitCursor wait;
  RegenSymbols();
  return;
}

void CTestDetail::OnButtonDefault() 
{
	// TODO: Add your control notification handler code here
  ASSERT (mServerId != AcDbObjectId::kNull);

  AcmSymbolStd *pRevision;
  if (Acad::eOk == acdbOpenObject(pRevision,mServerId,AcDb::kForWrite)) {
    pRevision->setFactoryDefaults();
    pRevision->close();
  }
  ReadDialogData();
	
}

void CTestDetail::OnOK() 
{
	// TODO: Add extra validation here
  //save data to the standard
  if (!SaveDialogData()) return;
  CWaitCursor wait;
  RegenSymbols();
  //close dialog
	
	CDialog::OnOK();
}

void
CTestDetail::RegenSymbols() const
{
  acmStdMgr->updateSymbols();
       
  actrTransactionManager->queueForGraphicsFlush();
  actrTransactionManager->enableGraphicsFlush(Adesk::kTrue);
  actrTransactionManager->flushGraphics();

  CWnd* pCWnd = CWnd::FromHandle(adsw_acadDocWnd());
  CRect rect;
  pCWnd->GetClientRect(&rect);

  // Small portion of Client Area
  rect.right = rect.left + 10;
  rect.bottom = rect.top + 10;

  // Invalidate this area
  pCWnd->InvalidateRect(&rect);
}

bool 
AcmCTestDetailEditExt::edit(const AcDbObject *pObj,CWnd* parentWindow /*= NULL*/) const
{
  if (!parentWindow)
    parentWindow = CWnd::FromHandle(adsw_acadMainWnd());

	CTemporaryResourceOverride tmpres;
  CTestDetail dlg(pObj->objectId(),parentWindow);
//  dlg.SetRevisionId();
  INT_PTR result = dlg.DoModal();				  

  if (IDCANCEL  == result)
    return false;
  else
    return true;
}
