Click here to Skip to main content
15,881,424 members
Articles / Web Development / HTML

CSSSandbox

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
12 Feb 2013CPOL6 min read 26.9K   1.3K   29  
A tool for simultaneous editing and viewing of CSS and HTML.
// ControlSplitter.cpp : �C���v�������e�[�V���� �t�@�C��
//

#include "stdafx.h"

#include "ControlSplitter.h"

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

/////////////////////////////////////////////////////////////////////////////
// CControlSplitter

CControlSplitter::CControlSplitter()
{
	m_pOldDragCapture = NULL;
    m_bDragging = false;
	m_nType = CS_NONE;	
	m_hCursor = NULL;
	m_rectMax.SetRectEmpty();
	minSplitterPos = -1;
	maxSplitterPos = -1;
	
}

CControlSplitter::~CControlSplitter()
{
	DestroyCursor(m_hCursor);
}


BEGIN_MESSAGE_MAP(CControlSplitter, CButton)
	//{{AFX_MSG_MAP(CControlSplitter)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CControlSplitter ���b�Z�[�W �n���h��

void CControlSplitter::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: ���̈ʒu�Ƀ��b�Z�[�W �n���h���p�̃R�[�h��lj����邩�܂��̓f�t�H���g�̏�����Ăяo���Ă�������
	
	//if(DragDetect(m_hWnd,point))
	{        
        m_bDragging = true;
        if(m_pOldDragCapture != NULL)
		{
			ReleaseCapture();
			m_pOldDragCapture = NULL;
		}
		//TRACE0("Drag");
		m_pOldDragCapture = SetCapture();        
		GetCursorPos(&m_ptStartDrag);
		CRect rect;
		GetWindowRect(&rect);
		m_ptStartPos = rect.TopLeft();
	}
	CButton::OnLButtonDown(nFlags, point);
}

void CControlSplitter::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: ���̈ʒu�Ƀ��b�Z�[�W �n���h���p�̃R�[�h��lj����邩�܂��̓f�t�H���g�̏�����Ăяo���Ă�������
	//TRACE0("OnLButtonUp");
	if(m_pOldDragCapture != NULL)
    {		
		ReleaseCapture();
        m_pOldDragCapture = NULL;
    }
	m_bDragging = false;
	GetParent()->Invalidate();
	GetParent()->SendMessage(UWM_SPLIT_MOVED,(WPARAM)m_hWnd,(LPARAM)MAKELONG(point.x,point.y));
	CButton::OnLButtonUp(nFlags, point);
}

void CControlSplitter::OnMouseMove(UINT nFlags, CPoint point) 
{		
	if(m_bDragging)
	{
		CRect rect;
		::GetWindowRect(m_hWnd,&rect);		
		CPoint ptMouse;
		GetCursorPos(&ptMouse);
		CSize sizeDiff = ptMouse - m_ptStartDrag;
		CSize sizeMove = m_ptStartPos-rect.TopLeft();
		CRect rectBefore = rect;
		rect.OffsetRect(sizeMove);
		if(m_nType == CS_HORZ)
		{							
			rect.OffsetRect(0, sizeDiff.cy);
		}else{
			rect.OffsetRect(sizeDiff.cx,0);
		}
		CRect rectAfter = rect;
		CRect cr = rect;
		GetParent()->ScreenToClient(cr);
		if (m_nType == CS_HORZ)
		{
			if ((minSplitterPos >= 0) && (rectAfter.top < rectBefore.top))
			{
				if (cr.top <= minSplitterPos)
					return;
			}
			else if ((maxSplitterPos >= 0) && (rectAfter.bottom > rectBefore.bottom))
			{
				if (cr.bottom >= maxSplitterPos)
					return;
			}
		}
		else if (m_nType == CS_VERT)
		{
			if ((minSplitterPos >= 0) && (rectAfter.left < rectBefore.left))
			{
				if (cr.left <= minSplitterPos)
					return;
			}
			else if ((maxSplitterPos >= 0) && (rectAfter.right > rectBefore.right))
			{
				if (cr.right >= maxSplitterPos)
					return;
			}
		}
		std::vector<DWORD>::iterator it;
		GetParent()->ScreenToClient(&rect);	
		/*
		if((m_nType == CS_HORZ) && ((m_rectMax.top >= rect.top) || (m_rectMax.bottom <= rect.bottom)))
		{
			CString s;
			s.Format(_T("--- 0, %d, %d, %d, %d\n"), m_rectMax.top, rect.top, m_rectMax.bottom, rect.bottom);
			TRACE1("%s",s);
			return;
		}
		else if((m_nType == CS_VERT) && ((m_rectMax.left >= rect.left) || (m_rectMax.right <= rect.right)))
		{
			CString s;
			s.Format(_T("--- 1, %d, %d, %d, %d\n"), m_rectMax.left, rect.left, m_rectMax.right, rect.right);
			TRACE1("%s",s);
			return;
		}
		*/
		/*
		TRACE2("M(%d,%d,",m_rectMax.top,m_rectMax.left);
		TRACE2("%d,%d)\n",m_rectMax.bottom,m_rectMax.right);
		TRACE2("(%d,%d,",rect.top,rect.left);
		TRACE2("%d,%d)\n",rect.bottom,rect.right);
		*/
		for ( it=m_vtTopLeftControls.begin() ; it < m_vtTopLeftControls.end(); it++ )
		{
			CWnd* pCtrl = GetParent()->GetDlgItem(LOWORD(*it));
			UINT nFlag = HIWORD(*it);
			if(pCtrl == NULL) continue;
			CRect rectCtrl;
			pCtrl->GetWindowRect(&rectCtrl);
			if(m_nType == CS_HORZ)
			{
				if(nFlag&SPF_BOTTOM)
				{
					rectCtrl.bottom +=  sizeMove.cy;
					rectCtrl.bottom += sizeDiff.cy;
				}
				if(!(nFlag&SPF_TOP))
				{
					rectCtrl.top +=  sizeMove.cy;
					rectCtrl.top += sizeDiff.cy;
				}

			}else{
				if((nFlag&SPF_RIGHT))
				{
					rectCtrl.right +=  sizeMove.cx;
					rectCtrl.right += sizeDiff.cx;
				}
				if(0 ==(nFlag&SPF_LEFT))
				{
					rectCtrl.left +=  sizeMove.cx;
					rectCtrl.left += sizeDiff.cx;
				}
			}			
			GetParent()->ScreenToClient(&rectCtrl);		
			pCtrl->MoveWindow(rectCtrl);
		}
		for ( it=m_vtBottomRightControls.begin() ; it < m_vtBottomRightControls.end(); it++ )
		{
			CWnd* pCtrl = GetParent()->GetDlgItem(LOWORD(*it));
			UINT nFlag = HIWORD(*it);
			if(pCtrl == NULL) continue;
			CRect rectCtrl;
			pCtrl->GetWindowRect(&rectCtrl);
			if(m_nType == CS_HORZ)
			{
				if(nFlag&SPF_TOP)
				{
					rectCtrl.top +=  sizeMove.cy;
					rectCtrl.top += sizeDiff.cy;
				}
				if(!(nFlag&SPF_BOTTOM))
				{
					rectCtrl.bottom +=  sizeMove.cy;
					rectCtrl.bottom += sizeDiff.cy;
				}
			}else{
				if((nFlag&SPF_LEFT))
				{
					rectCtrl.left +=  sizeMove.cx;
					rectCtrl.left += sizeDiff.cx;
				}
				if(!(nFlag&SPF_RIGHT))
				{
					rectCtrl.right +=  sizeMove.cx;
					rectCtrl.right += sizeDiff.cx;
				}
			}
			GetParent()->ScreenToClient(&rectCtrl);		
			pCtrl->MoveWindow(rectCtrl);
			
		}
		
		MoveWindow(rect);		
	}
	CButton::OnMouseMove(nFlags, point);
	
}

BOOL CControlSplitter::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{	
	if(m_hCursor != NULL)
	{
		DestroyCursor(m_hCursor);
	}
	if(m_nType == CS_HORZ)
	{
		m_hCursor = ::LoadCursor(NULL,IDC_SIZENS);
	}else
	{
		m_hCursor = ::LoadCursor(NULL,IDC_SIZEWE);
	}
	SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)m_hCursor);		
		
	return CButton::OnSetCursor(pWnd, nHitTest, message);
}


void CControlSplitter::SetType(UINT nType)
{
	m_nType = nType;
}

void CControlSplitter::AddToTopOrLeftCtrls(UINT nCtrlId, WORD nFlags)
{
	ASSERT(m_nType);
	m_vtTopLeftControls.push_back(MAKELONG(nCtrlId,nFlags));
	if(m_rectMax.IsRectEmpty())
	{
		GetParent()->GetWindowRect(m_rectMax);
		/*
		TRACE3("I-%d(%d,%d,",nCtrlId,m_rectMax.top,m_rectMax.left);
		TRACE2("%d,%d)\n",m_rectMax.bottom,m_rectMax.right);
		*/
	}
	CWnd * pCtrl = GetParent()->GetDlgItem(nCtrlId);
	
	if(pCtrl != NULL)
	{
		CRect rect;
		pCtrl->GetWindowRect(&rect);
		//GetParent()->ClientToScreen(rect);
		if((m_nType == CS_HORZ) &&(m_rectMax.top < rect.top))
		{
			m_rectMax.top = rect.top;
		}
		if((m_nType == CS_VERT) && (m_rectMax.left < rect.left))
		{
			m_rectMax.left = rect.left;
		}
	}
	/*
	TRACE3("M-%d(%d,%d,",nCtrlId,m_rectMax.top,m_rectMax.left);
	TRACE2("%d,%d)\n",m_rectMax.bottom,m_rectMax.right);
	*/
}

void CControlSplitter::AddToBottomOrRightCtrls(UINT nCtrlId, WORD nFlags)
{
	ASSERT(m_nType);
	m_vtBottomRightControls.push_back(MAKELONG(nCtrlId,nFlags));
	if(m_rectMax.IsRectEmpty())
	{
		GetParent()->GetWindowRect(m_rectMax);
		GetParent()->ClientToScreen(m_rectMax);
		/*
		TRACE3("I-%d(%d,%d,",nCtrlId,m_rectMax.top,m_rectMax.left);
		TRACE2("%d,%d)\n",m_rectMax.bottom,m_rectMax.right);
		*/
	}
	CWnd * pCtrl = GetParent()->GetDlgItem(nCtrlId);
	if(pCtrl != NULL)
	{
		CRect rect;
		pCtrl->GetWindowRect(&rect);
		GetParent()->ClientToScreen(rect);
		if((m_nType == CS_HORZ) &&(m_rectMax.bottom > rect.bottom))
		{
			m_rectMax.bottom = rect.bottom;
		}
		if((m_nType == CS_VERT) &&(m_rectMax.right > rect.right))
		{
			m_rectMax.right = rect.right;
		}
	}
	/*
	TRACE3("M-%d(%d,%d,",nCtrlId,m_rectMax.top,m_rectMax.left);
	TRACE2("%d,%d)\n",m_rectMax.bottom,m_rectMax.right);
	*/
}

void CControlSplitter::SetMinMaxSplitterPos(int minSplitterPos, int maxSplitterPos)
{
	this->minSplitterPos = minSplitterPos;
	this->maxSplitterPos = maxSplitterPos;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions