#include "stdafx.h"
#include "ui_replace.h"


LRESULT CDialogReplace::OnFindNext(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	int pos;

	if (m_text.is_empty())
		return 0;

	SendMessage(m_hedit, SCI_CHARRIGHT, 0, 0);
	SendMessage(m_hedit, SCI_SEARCHANCHOR, 0, 0);
	pos = SendMessage(m_hedit, SCI_SEARCHNEXT, m_flags, (LPARAM)m_text.get_ptr());
	FindResult(pos);
	return 0;
}

void CDialogReplace::FindResult(int pos)
{
	if (pos != -1)
	{
		m_havefound = true;
		SendMessage(m_hedit, SCI_SCROLLCARET, 0, 0);
	}
	else
	{
		m_havefound = false;
		pfc::string8 temp = "Cannot find \"";

		temp += m_text;
		temp += "\"";
		uMessageBox(m_hWnd, temp.get_ptr(), "WSH Panel Mod", MB_ICONINFORMATION);
	}
}

LRESULT CDialogReplace::OnEditFindWhatEnChange(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	uGetWindowText(GetDlgItem(IDC_EDIT_FINDWHAT), m_text);
	return 0;
}

LRESULT CDialogReplace::OnFlagCommand(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	bool check = uButton_GetCheck(m_hWnd, wID);
	int flag = 0;

	switch (wID)
	{
	case IDC_CHECK_MATCHCASE:
		flag = SCFIND_MATCHCASE;
		break;

	case IDC_CHECK_WHOLEWORD:
		flag = SCFIND_WHOLEWORD;
		break;

	case IDC_CHECK_WORDSTART:
		flag = SCFIND_WORDSTART;
		break;

	case IDC_CHECK_REGEXP:
		flag = SCFIND_REGEXP;
		break;
	}

	if (check)
		m_flags |= flag;
	else
		m_flags &= ~flag;

	return 0;
}

LRESULT CDialogReplace::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	//DestroyWindow();
	ShowWindow(SW_HIDE);
	return 0;
}

void CDialogReplace::OnFinalMessage(HWND hWnd)
{
	modeless_dialog_manager::g_remove(m_hWnd);
	delete this;
}

LRESULT CDialogReplace::OnInitDialog(HWND hwndFocus, LPARAM lParam)
{
	modeless_dialog_manager::g_add(m_hWnd);
	DlgResize_Init();

	return TRUE; // set focus to default control
}

LRESULT CDialogReplace::OnEditReplaceEnChange(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	uGetWindowText(GetDlgItem(IDC_EDIT_REPLACE), m_reptext);
	return 0;
}

LRESULT CDialogReplace::OnReplace(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	if (m_havefound)
	{
		CHARRANGE cr = GetSelection();

		SendMessage(m_hedit, SCI_SETTARGETSTART, cr.cpMin, 0);
		SendMessage(m_hedit, SCI_SETTARGETEND, cr.cpMax, 0);
		SendMessage(m_hedit, SCI_REPLACETARGET, m_reptext.get_length(), (LPARAM)m_reptext.get_ptr());
		SendMessage(m_hedit, SCI_SETSEL, cr.cpMin + m_reptext.get_length(), cr.cpMin);
		m_havefound = false;
	}

	// Find Next
	OnFindNext(0, 0, 0);

	return 0;
}

LRESULT CDialogReplace::OnReplaceall(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	// Reset Pos
	SendMessage(m_hedit, SCI_SETSEL, 0, 0);
	SendMessage(m_hedit, SCI_BEGINUNDOACTION, 0, 0);

	while (true)
	{
		{
			int start_pos = SendMessage(m_hedit, SCI_GETSELECTIONEND, 0, 0);
			int end_pos = SendMessage(m_hedit, SCI_GETLENGTH, 0, 0);

			SendMessage(m_hedit, SCI_SETTARGETSTART, start_pos, 0);
			SendMessage(m_hedit, SCI_SETTARGETEND, end_pos, 0);
			SendMessage(m_hedit, SCI_SETSEARCHFLAGS, m_flags, 0);

			int pos_find = SendMessage(m_hedit, SCI_SEARCHINTARGET, m_text.get_length(), (LPARAM)m_text.get_ptr());

			if (pos_find == -1)
			{
				MessageBeep(MB_ICONASTERISK);
				break;
			}
			else
			{
				int start = SendMessage(m_hedit, SCI_GETTARGETSTART, 0, 0);
				int end = SendMessage(m_hedit, SCI_GETTARGETEND, 0, 0);

				SendMessage(m_hedit, SCI_SETSEL, start, end);
			}
		}

		CHARRANGE cr = GetSelection();

		SendMessage(m_hedit, SCI_SETTARGETSTART, cr.cpMin, 0);
		SendMessage(m_hedit, SCI_SETTARGETEND, cr.cpMax, 0);
		SendMessage(m_hedit, SCI_REPLACETARGET, m_reptext.get_length(), (LPARAM)m_reptext.get_ptr());
		SendMessage(m_hedit, SCI_SETSEL, cr.cpMin + m_reptext.get_length(), cr.cpMin);
	}

	SendMessage(m_hedit, SCI_ENDUNDOACTION, 0, 0);
	return 0;
}

CHARRANGE CDialogReplace::GetSelection()
{
	CHARRANGE cr;

	cr.cpMin = SendMessage(m_hedit, SCI_GETSELECTIONSTART, 0, 0);
	cr.cpMax = SendMessage(m_hedit, SCI_GETSELECTIONEND, 0, 0);
	return cr;
}