//------------------------------------------------------------------------------

#include "stdafx.h"
#include "megatune.h"
#include "DtableUse.h"
#include "msDatabase.h"
#include "veconst.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern msDatabase mdb;

//------------------------------------------------------------------------------

DtableUse::DtableUse(CWnd* pParent /*=NULL*/)
   : CDialog(DtableUse::IDD, pParent)
{
   //{{AFX_DATA_INIT(DtableUse)
      // NOTE: the ClassWizard will add member initialization here
   //}}AFX_DATA_INIT
}

//------------------------------------------------------------------------------

void DtableUse::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
   //{{AFX_DATA_MAP(DtableUse)
      DDX_Control(pDX, IDC_TABLE_MODE, m_tableMode);
      DDX_Control(pDX, IDC_INJ2_GAMMA, m_inj2Gamma);
      DDX_Control(pDX, IDC_INJ2_OFF,   m_inj2Table);
      DDX_Control(pDX, IDC_INJ1_OFF,   m_inj1Table);
      DDX_Control(pDX, IDC_INJ1_GAMMA, m_inj1Gamma);
   //}}AFX_DATA_MAP
}

//------------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DtableUse, CDialog)
   //{{AFX_MSG_MAP(DtableUse)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//------------------------------------------------------------------------------
// DtableUse message handlers

typedef union {
   struct {
      int alt:1;
      int i11:1;
      int i12:1;
      int i21:1;
      int i22:1;
      int i1g:1;
      int i2g:1;
      int xxx:1;
   } bt;
   unsigned char bb;
} bitMap;

//------------------------------------------------------------------------------

BOOL DtableUse::OnInitDialog()
{
   CDialog::OnInitDialog();

   bitMap b;
   int savePage = mdb.pageNo();
   mdb.setPageNo(0);
   b.bb = mdb.Const(Dalternate);
   mdb.setPageNo(savePage);

   m_tableMode.SetCheck(b.bt.alt);

        if (b.bt.i11) static_cast<CButton *>(GetDlgItem(IDC_INJ1_1  ))->SetCheck(true);
   else if (b.bt.i12) static_cast<CButton *>(GetDlgItem(IDC_INJ1_2  ))->SetCheck(true);
   else               static_cast<CButton *>(GetDlgItem(IDC_INJ1_OFF))->SetCheck(true);
   m_inj1Gamma.SetCheck(b.bt.i1g);

        if (b.bt.i21) static_cast<CButton *>(GetDlgItem(IDC_INJ2_1  ))->SetCheck(true);
   else if (b.bt.i22) static_cast<CButton *>(GetDlgItem(IDC_INJ2_2  ))->SetCheck(true);
   else               static_cast<CButton *>(GetDlgItem(IDC_INJ2_OFF))->SetCheck(true);
   m_inj2Gamma.SetCheck(b.bt.i2g);

   return TRUE;
}

//------------------------------------------------------------------------------

void DtableUse::OnOK()
{
   bitMap b;
   b.bb = 0;

   b.bt.alt = m_tableMode.GetCheck();

   if (static_cast<CButton *>(GetDlgItem(IDC_INJ1_1  ))->GetCheck()) b.bt.i11 = 1;
   if (static_cast<CButton *>(GetDlgItem(IDC_INJ1_2  ))->GetCheck()) b.bt.i12 = 1;
   b.bt.i1g = m_inj1Gamma.GetCheck();

   if (static_cast<CButton *>(GetDlgItem(IDC_INJ2_1  ))->GetCheck()) b.bt.i21 = 1;
   if (static_cast<CButton *>(GetDlgItem(IDC_INJ2_2  ))->GetCheck()) b.bt.i22 = 1;
   b.bt.i2g = m_inj2Gamma.GetCheck();

   int savePage = mdb.pageNo();
   mdb.setPageNo(0);
   mdb.putConstByte(Dalternate, b.bb, true);
   mdb.setPageNo(savePage);

   CDialog::OnOK();
}

//------------------------------------------------------------------------------

