// ★コメント未記入
/*-----------------------------------------------------------------------------
[名称]	SettingViewDlg.h
[概要]	タイプ別設定ダイアログ（表示）ヘッダ
[備考]	なし
[履歴]	2003-12-22	b031228 (moralog) 等幅フォントの時はルーラを文字数に
		2003-12-28  b040111 (3632)キャレット位置の強調(moralog)
-----------------------------------------------------------------------------*/

#include "stdafx.h"
#include "shitarabian.h"
#include "SettingViewDlg.h"
#include "GeneralSetting.h"
//#include "EditDrawer.h"
//#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSettingViewDlg ダイアログ


CSettingViewDlg::CSettingViewDlg(CWnd* pParent /*=NULL*/)
	: COwnerTabPage(CSettingViewDlg::IDD, pParent)
	, m_fontRuler()
{
	//{{AFX_DATA_INIT(CSettingViewDlg)
	m_strTabString = _T("");
	m_bTabConvert = FALSE;
	m_uiRowGap = 0;
	m_uiBlankWidth = 0;
	m_uiNumberWidth = 0;
	m_bVisibleNumber = FALSE;
	m_uiRulerHeight = 0;
	m_bVisibleRuler = FALSE;
	m_bFreeCursor = FALSE;
	m_iRulerUnit = -1;
	m_bCaretMark = FALSE;
	m_bCaretLine = FALSE;
	//}}AFX_DATA_INIT
}


void CSettingViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSettingViewDlg)
	DDX_Control(pDX, IDC_SETTING_TABSTRING, m_EmphasisTabString);
	DDX_Text(pDX, IDC_SETTING_TABSTRING, m_strTabString);
	DDX_Check(pDX, IDC_SETTING_TAB, m_bTabConvert);
	DDX_Text(pDX, IDC_SETTING_ROWGAP, m_uiRowGap);
	DDX_Text(pDX, IDC_SETTING_BLANKWIDTH, m_uiBlankWidth);
	DDX_Text(pDX, IDC_SETTING_NUMBERWIDTH, m_uiNumberWidth);
	DDX_Check(pDX, IDC_SETTING_VISIBLENUMBER, m_bVisibleNumber);
	DDX_Text(pDX, IDC_SETTING_RULERHEIGHT, m_uiRulerHeight);
	DDX_Check(pDX, IDC_SETTING_VISIBLERULER, m_bVisibleRuler);
	DDX_Check(pDX, IDC_SETTING_VIEW_FREECURSOR, m_bFreeCursor);
	DDX_Radio(pDX, IDC_SETTING_RULERUNIT_PIXEL, m_iRulerUnit);
	DDX_Check(pDX, IDC_SETTING_VISIBLECARETMARK, m_bCaretMark);
	DDX_Check(pDX, IDC_SETTING_VISIBLECARETLINE, m_bCaretLine);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSettingViewDlg, CDialog)
	//{{AFX_MSG_MAP(CSettingViewDlg)
	ON_BN_CLICKED(IDC_SETTING_RULERFONT, OnSettingRulerfont)
	ON_BN_CLICKED(IDC_SETTING_FONT, OnSettingFont)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSettingViewDlg メッセージ ハンドラ

void CSettingViewDlg::Confirm() 
{
	WriteToGeneral();
}

void CSettingViewDlg::WriteToGeneral()
{
	UpdateData( TRUE );

	CGeneralSetting*	pGeneralSetting	= CGeneralSetting::GetInstance();

	pGeneralSetting->GetLogFont()->Copy( m_fontText );

	pGeneralSetting->SetTabString( m_strTabString );

	pGeneralSetting->SetTabConvert( !m_bTabConvert );

	if ( ((CButton*)GetDlgItem( IDC_SETTING_TAB2 ))->GetCheck() == 1 )
	{
		pGeneralSetting->SetTabCount( 2 );
	}
	if ( ((CButton*)GetDlgItem( IDC_SETTING_TAB4 ))->GetCheck() == 1 )
	{
		pGeneralSetting->SetTabCount( 4 );
	}
	if ( ((CButton*)GetDlgItem( IDC_SETTING_TAB8 ))->GetCheck() == 1 )
	{
		pGeneralSetting->SetTabCount( 8 );
	}

	pGeneralSetting->SetRowGap( m_uiRowGap );

	pGeneralSetting->SetLeftBlankMargin( m_uiBlankWidth );
	pGeneralSetting->SetLeftNumberMargin( m_uiNumberWidth );
	pGeneralSetting->SetVisibleNumber( m_bVisibleNumber );

	pGeneralSetting->SetRulerHeight( m_uiRulerHeight );
	pGeneralSetting->SetVisibleRuler( m_bVisibleRuler );
	pGeneralSetting->SetRulerFont( m_fontRuler );
// ▼ (3181) BGN 2003.12.22 (moralog) 等幅フォントの時はルーラを文字数に
	pGeneralSetting->SetRulerUnitPixel( m_iRulerUnit == 0 );
// ▲ (3181) END
// ▼ (3632) BGN 2003.12.28 (moralog) キャレット位置の表示
	pGeneralSetting->SetCaretMark( m_bCaretMark );
	pGeneralSetting->SetCaretLine( m_bCaretLine );
// ▲ (3632) END


	pGeneralSetting->SetFreeCursor( m_bFreeCursor );
}


void CSettingViewDlg::ReadFromGeneral()
{
	CGeneralSetting*	pGeneralSetting	= CGeneralSetting::GetInstance();
	if( pGeneralSetting != NULL )
	{
		m_fontText.Copy( *pGeneralSetting->GetLogFont() );

		m_strTabString = pGeneralSetting->GetTabString();
		m_bTabConvert = !pGeneralSetting->GetTabConvert();

		((CButton*)GetDlgItem( IDC_SETTING_TAB2 ))->SetCheck( FALSE );
		((CButton*)GetDlgItem( IDC_SETTING_TAB4 ))->SetCheck( FALSE );
		((CButton*)GetDlgItem( IDC_SETTING_TAB8 ))->SetCheck( FALSE );
		switch( pGeneralSetting->GetTabCount() )
		{
		case 2:
			((CButton*)GetDlgItem( IDC_SETTING_TAB2 ))->SetCheck( TRUE );
			break;
		case 4:
			((CButton*)GetDlgItem( IDC_SETTING_TAB4 ))->SetCheck( TRUE );
			break;
		case 8:
			((CButton*)GetDlgItem( IDC_SETTING_TAB8 ))->SetCheck( TRUE );
			break;
		default:
			break;
		}

		m_uiRowGap = pGeneralSetting->GetRowGap();

		m_uiBlankWidth = pGeneralSetting->GetLeftBlankMargin();
		m_uiNumberWidth = pGeneralSetting->GetLeftNumberMargin();
		m_bVisibleNumber = pGeneralSetting->GetVisibleNumber();

		m_uiRulerHeight = pGeneralSetting->GetRulerHeight();
		m_bVisibleRuler = pGeneralSetting->GetVisibleRuler();
		m_fontRuler.Copy( *(pGeneralSetting->GetRulerFont()) );
// ▼ (3181) BGN 2003.12.22 (moralog) 等幅フォントの時はルーラを文字数に
		m_iRulerUnit = pGeneralSetting->GetRulerUnitPixel()?0:1;
// ▲ (3181) END
// ▼ (3632) BGN 2003.12.28 (moralog) キャレット位置の表示
		m_bCaretMark = pGeneralSetting->GetCaretMark();
		m_bCaretLine = pGeneralSetting->GetCaretLine();
// ▲ (3632) END

		m_bFreeCursor = pGeneralSetting->GetFreeCursor();
	}
	else
	{
		m_strTabString = _T("　　");

		m_bTabConvert = FALSE;

		m_uiRowGap = 2;

		m_uiBlankWidth = 16;
		m_uiNumberWidth = 60;
		m_bVisibleNumber = TRUE;

		m_uiRulerHeight = 20;
		m_bVisibleRuler = TRUE;

// ▼ (3632) BGN 2003.12.28 (moralog) キャレット位置の表示
		m_bCaretMark = TRUE;
		m_bCaretLine = TRUE;
// ▲ (3632) END

		m_bFreeCursor = FALSE;
	}

	UpdateData( FALSE );
}

BOOL CSettingViewDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	ReadFromGeneral();
	
	return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
	              // 例外: OCX プロパティ ページの戻り値は FALSE となります
}


void CSettingViewDlg::OnSettingRulerfont() 
{
	HDC		hdc = ::GetDC( NULL );
	CDC		DC;
	DC.Attach( hdc );

	CFontDialog		dlg( m_fontRuler.GetLogFont( &DC ), CF_SCREENFONTS );
	DC.Detach();
	::ReleaseDC( NULL, hdc );

	if ( dlg.DoModal() == IDOK)
	{
		// Settingデータを設定しなおす
		m_fontRuler.m_iPointSize = (dlg.GetSize())/10;
		m_fontRuler.m_strFontName = dlg.GetFaceName();
		m_fontRuler.m_bBold = dlg.IsBold();
		m_fontRuler.m_bItalic = dlg.IsItalic();
		m_fontRuler.m_bUnderLine = dlg.IsUnderline();
		m_fontRuler.m_bStrikeOut = dlg.IsStrikeOut();

		LOGFONT		tLogFont;
		dlg.GetCurrentFont( &tLogFont );
		m_fontRuler.m_nCharSet = tLogFont.lfCharSet;
	}
}

void CSettingViewDlg::OnSettingFont() 
{
	// FontDialog でフォントを選択し、メンバに保持する。
	// 反映は Confirm() で行う。
	HDC		hdc = ::GetDC( NULL );
	CDC		DC;
	DC.Attach( hdc );
	CFontDialog		dlg( m_fontText.GetLogFont( &DC ), CF_SCREENFONTS );
	DC.Detach();
	::ReleaseDC( NULL, hdc );

	if ( dlg.DoModal() == IDOK)
	{
		// Settingデータを設定しなおす
		m_fontText.m_iPointSize = (dlg.GetSize())/10;
		m_fontText.m_strFontName = dlg.GetFaceName();
		m_fontText.m_bBold = dlg.IsBold();
		m_fontText.m_bItalic = dlg.IsItalic();
		m_fontText.m_bUnderLine = dlg.IsUnderline();
		m_fontText.m_bStrikeOut = dlg.IsStrikeOut();

		LOGFONT		tLogFont;
		dlg.GetCurrentFont( &tLogFont );
		m_fontText.m_nCharSet = tLogFont.lfCharSet;
	}
}
