// ★コメント未記入
// FinderDlg.cpp : インプリメンテーション ファイル
/*
		2003-12-27	b031228	(3332)選択範囲が１行のときの置換範囲
*/

#include "stdafx.h"
#include "Shitarabian.h"
#include "FinderDlg.h"
#include "ShitarabianDoc.h"
#include "ShitarabianView.h"
#include "CommandCaret.h"
#include "Selection.h"
#include "Finder.h"
#include "BregPool.h"
#include "Common.h"
#include "ExeCommon.h"
#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFinderDlg ダイアログ


CFinderDlg::CFinderDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFinderDlg::IDD, pParent),
	m_pDoc( NULL )
{
	//{{AFX_DATA_INIT(CFinderDlg)
	m_strSubString = _T("");
	m_bCaseCare = FALSE;
	m_bLoop = FALSE;
	m_bToken = FALSE;
	m_bRegExp = FALSE;
	//}}AFX_DATA_INIT
}


void CFinderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFinderDlg)
	DDX_Control(pDX, IDC_FINDER_REGEXP, m_btnRegName);
	DDX_Control(pDX, IDC_FINDER_SUBSTRING, m_comboSubString);
	DDX_CBString(pDX, IDC_FINDER_SUBSTRING, m_strSubString);
	DDX_Check(pDX, IDC_FINDER_UPPERLOWER, m_bCaseCare);
	DDX_Check(pDX, IDC_FINDER_LOOP, m_bLoop);
	DDX_Check(pDX, IDC_FINDER_TOKEN, m_bToken);
	DDX_Check(pDX, IDC_FINDER_REGEXP, m_bRegExp);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFinderDlg, CDialog)
	//{{AFX_MSG_MAP(CFinderDlg)
	ON_BN_CLICKED(IDC_FINDER_NEXT, OnFinderNext)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_FINDER_PREV, OnFinderPrev)
	ON_BN_CLICKED(IDC_FINDER_LOOP, OnVisibleControl)
	ON_COMMAND(IDM_REGEXP_CHAR, OnRegexpChar)
	ON_COMMAND(IDM_REGEXP_RENGE, OnRegexpRenge)
	ON_COMMAND(IDM_REGEXP_RENGEOVER, OnRegexpRengeover)
	ON_COMMAND(IDM_REGEXP_HEAD, OnRegexpHead)
	ON_COMMAND(IDM_REGEXP_LAST, OnRegexpLast)
	ON_COMMAND(IDM_REGEXP_TOKEN, OnRegexpToken)
	ON_COMMAND(IDM_REGEXP_NOTTOKEN, OnRegexpNotToken)
	ON_COMMAND(IDM_REGEXP_COUNT, OnRegexpCount)
	ON_COMMAND(IDM_REGEXP_SPACE, OnRegexpSpace)
	ON_COMMAND(IDM_REGEXP_NOTSPACE, OnRegexpNotSpace)
	ON_COMMAND(IDM_REGEXP_TAG, OnRegexpTag)
	ON_COMMAND(IDM_REGEXP_NOT, OnRegexpNot)
	ON_COMMAND(IDM_REGEXP_OR, OnRegexpOr)
	ON_COMMAND(IDM_REGEXP_0MATCH, OnRegexp0match)
	ON_COMMAND(IDM_REGEXP_1MATCH, OnRegexp1match)
	ON_COMMAND(IDM_REGEXP_GROUP, OnRegexpGroup)
	ON_BN_CLICKED(IDC_FINDER_REGPROPOSE, OnFinderRegpropose)
	ON_BN_CLICKED(IDC_FINDER_MARK, OnFinderMark)
	ON_CBN_EDITCHANGE(IDC_FINDER_SUBSTRING, OnVisibleControl)
	ON_CBN_CLOSEUP(IDC_FINDER_SUBSTRING, OnVisibleControl)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
void CFinderDlg::OnVisibleControl()
{
	VisibleControl();
}

void CFinderDlg::VisibleControl() 
{
	UpdateData( TRUE );

	if( m_pDoc != NULL )
	{
		// 上下循環
		GetDlgItem( IDC_FINDER_LOOP )->EnableWindow( TRUE );

		// 検索ボタン類
		GetDlgItem( IDC_FINDER_PREV )->EnableWindow( !m_strSubString.IsEmpty() );
		GetDlgItem( IDC_FINDER_NEXT )->EnableWindow( !m_strSubString.IsEmpty() );
		GetDlgItem( IDC_FINDER_MARK )->EnableWindow( !m_strSubString.IsEmpty() );

		// 正規表現
		CFinder*	pFinder = ::GetFinder( TRUE );
		BOOL	bRegExp = pFinder->IsAlive();
		GetDlgItem( IDC_FINDER_REGEXP )->EnableWindow( bRegExp );
		bRegExp &= ( ((CButton*)GetDlgItem( IDC_FINDER_REGEXP ))->GetCheck () == 1 );
		GetDlgItem( IDC_FINDER_REGPROPOSE )->EnableWindow( bRegExp );
		GetDlgItem( IDC_FINDER_UPPERLOWER )->EnableWindow( pFinder->EnableCareCase() );
		GetDlgItem( IDC_FINDER_TOKEN )->EnableWindow( !bRegExp );
	}

//	UpdateData( FALSE );
}


/////////////////////////////////////////////////////////////////////////////




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

void CFinderDlg::OnFinderPrev()
{
	UpdateData( FALSE );
	::GetMainFrame()->AddSubString( m_strSubString, &m_comboSubString );
	UpdateData( TRUE );
	FindPrev();
}

// 後方検索
void CFinderDlg::OnFinderNext() 
{
	UpdateData( FALSE );
	::GetMainFrame()->AddSubString( m_strSubString, &m_comboSubString );
	UpdateData( TRUE );
	FindNext();
}

void CFinderDlg::FindPrev()
{
	CFinder*	pFinder = ::GetFinder( m_bRegExp );
	pFinder->FindPrevForSelect(
		m_pDoc,
		m_strSubString,
		m_bLoop,
		m_bCaseCare,
		m_bToken
	);
}

void CFinderDlg::FindNext()
{
	CFinder*	pFinder = ::GetFinder( m_bRegExp );
	pFinder->FindNextForSelect(
		m_pDoc,
		m_strSubString,
		m_bLoop,
		m_bCaseCare,
		m_bToken
	);
}


void CFinderDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);

// ▼ (3332) BGN 2003.12.27 (moralog) 「選択文字を検索文字に設定」処理を１行選択のみに変更
	BOOL	bMultiLine;
	bMultiLine = (m_pDoc->GetSelection()->GetSelectedStartRow() != m_pDoc->GetSelection()->GetSelectedEndRow() );
	if ( !bMultiLine )
	{
		// １行のときには選択範囲を検索文字列とする
		CString	strSelected = m_pDoc->GetSelection()->GetSelectedText();
		if ( !strSelected.IsEmpty() )
		{
			GetDlgItem( IDC_FINDER_SUBSTRING )->SetWindowText( strSelected );
		}
	}
// ▽ oridinal BGN
//	// 選択文字を検索文字に設定
//	CString	strSelected = m_pDoc->GetSelection()->GetSelectedText();
//	if ( !strSelected.IsEmpty() )
//	{
//		m_strSubString = strSelected;
//		UpdateData( FALSE );
//	}
// △ oridinal END
// ▲ (3332) END

	VisibleControl();

	// コンボボックスにフォーカスを移す
	m_comboSubString.SetEditSel( 0, -1 );
	m_comboSubString.SetFocus();

	SetWindowPos( &wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );

}

BOOL CFinderDlg::PreTranslateMessage(MSG* pMsg) 
{
	if( pMsg->hwnd == m_hWnd )
	{
//		VisibleControl();
	}
	return CDialog::PreTranslateMessage(pMsg);
}






void CFinderDlg::OnRegexpChar() 		{ CFinder::RegexpChar( &m_comboSubString ); }
void CFinderDlg::OnRegexpRenge()		{ CFinder::RegexpRenge( &m_comboSubString ); }
void CFinderDlg::OnRegexpRengeover()	{ CFinder::RegexpRengeover( &m_comboSubString ); }
void CFinderDlg::OnRegexpHead() 		{ CFinder::RegexpHead( &m_comboSubString ); }
void CFinderDlg::OnRegexpLast()			{ CFinder::RegexpLast( &m_comboSubString ); }
void CFinderDlg::OnRegexpToken()		{ CFinder::RegexpToken( &m_comboSubString ); }
void CFinderDlg::OnRegexpNotToken()		{ CFinder::RegexpNotToken( &m_comboSubString ); }
void CFinderDlg::OnRegexpSpace()		{ CFinder::RegexpSpace( &m_comboSubString ); }
void CFinderDlg::OnRegexpNotSpace()		{ CFinder::RegexpNotSpace( &m_comboSubString ); }
void CFinderDlg::OnRegexpTag()			{ CFinder::RegexpTag( &m_comboSubString ); }
void CFinderDlg::OnRegexpNot()		 	{ CFinder::RegexpNot( &m_comboSubString ); }
void CFinderDlg::OnRegexpOr() 			{ CFinder::RegexpOr( &m_comboSubString ); }
void CFinderDlg::OnRegexp0match()		{ CFinder::Regexp0match( &m_comboSubString ); }
void CFinderDlg::OnRegexp1match()		{ CFinder::Regexp1match( &m_comboSubString ); }
void CFinderDlg::OnRegexpCount()		{ CFinder::RegexpCount( &m_comboSubString ); }
void CFinderDlg::OnRegexpGroup()		{ CFinder::RegexpGroup( &m_comboSubString ); }


void CFinderDlg::OnFinderRegpropose() 
{
	CMenu menu;
	VERIFY(menu.LoadMenu( IDR_REGEXP ));
	CMenu* pPopup = menu.GetSubMenu(0);
	ASSERT(pPopup != NULL);

	CRect	rcButton;
	GetDlgItem( IDC_FINDER_REGPROPOSE )->GetWindowRect( &rcButton );

	pPopup->TrackPopupMenu(
		TPM_LEFTALIGN | TPM_RIGHTBUTTON,
		rcButton.right, rcButton.top,
		this
	);
}

void CFinderDlg::OnFinderMark() 
{
	int		idxStartRow = 0;
	int		idxEndRow = m_pDoc->GetLineCount()-1;

	BOOL	bFound = TRUE;

	while( bFound)
	{
		long		idxFoundCaretRow;
		long		idxFoundCaretColumn;
		long		nCaretColumn;
		CFinder*	pFinder = ::GetFinder( m_bRegExp );
		bFound = pFinder->FindNextAtLinez(
			m_pDoc,
			idxStartRow,
			0,
			idxEndRow,
			INT_MAX,
			m_strSubString,
			&idxFoundCaretRow,
			&idxFoundCaretColumn,
			&nCaretColumn,
			m_bCaseCare,
			m_bToken
		);
		if ( bFound )
		{
			m_pDoc->Mark( idxFoundCaretRow );
		}
		idxStartRow = idxFoundCaretRow + 1;
	}
	m_pDoc->SetAllUpdate();
	m_pDoc->ReDraw();
}

BOOL CFinderDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CFinder*	pFinder = ::GetFinder( TRUE );
	CString	strCaption;
	strCaption.Format( "正規表現（%s）", pFinder->Name() );
	m_btnRegName.SetWindowText( strCaption );
	
	return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
	              // 例外: OCX プロパティ ページの戻り値は FALSE となります
}
