//////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2025 Autodesk, Inc.  All rights reserved.
//
//  Use of this software is subject to the terms of the Autodesk license 
//  agreement provided at the time of installation or download, or which 
//  otherwise accompanies this software in either electronic or hard copy form.   
//
//////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Detail.h"
#include "editExt_crx.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;
}
