undoredobar.cpp

来自「A Model-View-Controller Framework that i」· C++ 代码 · 共 240 行

CPP
240
字号
#include "stdafx.h"
#include "UndoRedoBar.h"
#include "UndoRedoMenu.h"
#include "UndoRedoButton.h"
#include "UndoRedoListBox.h"
#include "WndController.h"
#include "WndMsgHandler.h"

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


namespace SbjCore
{
 	namespace Mvc
	{
		struct UndoRedoBarImpl : public WndController
		{
			UndoRedoListBox	theListBox;
			CRect			rectLabel;
			int				nLabelHeight;
			CString			sLabel;
			
			class UndoRedoBarCreateHandler : public SbjCore::Mvc::WndMsgHandler
			{
				CALL_DEFAULT_FIRST() // comment out to handle message before default

					virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
				{
					wParam;
					lParam;
					*pResult = 0;
					LRESULT lRslt = 1;

					SbjCore::Mvc::UndoRedoBarImpl* pCtrlr = dynamic_cast<SbjCore::Mvc::UndoRedoBarImpl*>(GetController());

					if (pCtrlr != NULL)
					{
						SbjCore::Mvc::UndoRedoBar* pBar = dynamic_cast<SbjCore::Mvc::UndoRedoBar*>(pCtrlr->GetWnd());	

						if (pBar != NULL)
						{
							CFont* pMenuFont = (CFont*)&CMFCMenuBar::GetMenuFont();

							CRect rectDummy (0, 0, 0, 0);

							pCtrlr->theListBox.Create (WS_VISIBLE | WS_CHILD | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY | WS_VSCROLL | LBS_MULTIPLESEL, 
								rectDummy, pBar, 1);

							pCtrlr->theListBox.ModifyStyle(WS_BORDER, 0);

							pCtrlr->theListBox.SetFont(pMenuFont);

							CStringList& list = UndoRedoButton::GetUndoRedoList();

							for (POSITION pos = list.GetHeadPosition(); pos != NULL;)
							{
								pCtrlr->theListBox.AddString(list.GetNext(pos));
							}
						}
					}

					return lRslt;
				}

			} theUndoRedoBarCreateHandler;
			friend UndoRedoBarCreateHandler;
			
			class UndoRedoBarSizeHandler : public SbjCore::Mvc::WndMsgHandler
			{
				CALL_DEFAULT_FIRST() // comment out to handle message before default

					virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
				{
					wParam;
					lParam;
					*pResult = 0;
					LRESULT lRslt = 1;

					SbjCore::Mvc::UndoRedoBarImpl* pCtrlr = dynamic_cast<SbjCore::Mvc::UndoRedoBarImpl*>(GetController());

					if (pCtrlr != NULL)
					{
						SbjCore::Mvc::UndoRedoBar* pBar = dynamic_cast<SbjCore::Mvc::UndoRedoBar*>(pCtrlr->GetWnd());	

						if (pBar != NULL)
						{
							CRect rectClient;
							pBar->GetClientRect(rectClient);

							CFont* pMenuFont = (CFont*) &CMFCMenuBar::GetMenuFont();

							CClientDC dc(pBar);
							CFont* pOldFont = dc.SelectObject(pMenuFont);

							TEXTMETRIC tm;
							dc.GetTextMetrics (&tm);

							pCtrlr->nLabelHeight = tm.tmHeight + 2;

							dc.SelectObject (pOldFont);


							pCtrlr->rectLabel = rectClient;
							pCtrlr->rectLabel.top = (pCtrlr->rectLabel.bottom - pCtrlr->nLabelHeight);
							pCtrlr->rectLabel.top -= (pCtrlr->rectLabel.top % tm.tmHeight);

							CRect rectList = rectClient;
							rectList.bottom = pCtrlr->rectLabel.top;

							pCtrlr->theListBox.MoveWindow(rectList);
						}
					}
					return lRslt;
				}

			} theUndoRedoBarSizeHandler;
			friend UndoRedoBarSizeHandler;
			
			UndoRedoBarImpl()
			{
				AddHandler(WM_CREATE, &theUndoRedoBarCreateHandler);
				AddHandler(WM_SIZE, &theUndoRedoBarSizeHandler);
			}
			
			virtual ~UndoRedoBarImpl()
			{
			}
		};
		
		/////////////////////////////////////////////////////////////////////////////

		IMPLEMENT_SERIAL(UndoRedoBar, CMFCPopupMenuBar, 1)

		UndoRedoBar::UndoRedoBar() :
			m_pImpl(new UndoRedoBarImpl)
		{
			SetController(m_pImpl);
		}

		UndoRedoBar::~UndoRedoBar()
		{
			try
			{
				delete m_pImpl;
			}
			catch(...)
			{
				ASSERT(FALSE);
			}
		}

		/////////////////////////////////////////////////////////////////////////////
		// UndoRedoBar message handlers

		void UndoRedoBar::OnFillBackground (CDC* pDC)
		{
			ASSERT_VALID (pDC);

			CRect rectClient;
			GetClientRect (rectClient);

			::FillRect (pDC->GetSafeHdc (), &rectClient, ::GetSysColorBrush (COLOR_WINDOW));

			CBrush br;
			br.CreateSolidBrush(RGB(0xF8, 0xF8, 0xF8));
			// Draw label:
			::FillRect(pDC->GetSafeHdc (), &m_pImpl->rectLabel, br);

			CFont* pOldFont = pDC->SelectObject ((CFont*) &CMFCMenuBar::GetMenuFont ());
			pDC->SetBkMode (TRANSPARENT);
			pDC->SetTextColor (::GetSysColor (COLOR_BTNTEXT));

			CRect rectText = m_pImpl->rectLabel;
			rectText.OffsetRect(CPoint(0,4));
			pDC->DrawText (m_pImpl->sLabel, rectText, DT_CENTER | DT_VCENTER);

			pDC->SelectObject (pOldFont);
		}

		CSize UndoRedoBar::CalcSize (BOOL /*bVertDock*/)
		{
			return CSize (160, 115);
		}

		UndoRedoButton* UndoRedoBar::GetUndoRedoButton () const
		{
			// Get parent button:
			UndoRedoButton* pUndoRedoButton = NULL;

			UndoRedoMenu* pParentMenu = DYNAMIC_DOWNCAST(UndoRedoMenu, GetParent());
			if (pParentMenu != NULL)
			{
				pUndoRedoButton = DYNAMIC_DOWNCAST(UndoRedoButton, pParentMenu->GetParentButton());
			}

			return pUndoRedoButton;
		}

		void UndoRedoBar::DoUndo ()
		{
			UndoRedoButton* pUndoRedoButton = GetUndoRedoButton();

			pUndoRedoButton->SetCount(m_pImpl->theListBox.GetSelCount());
			GetOwner()->PostMessage (WM_COMMAND, pUndoRedoButton->m_nID);

			CMFCPopupMenu* pMenu = (CMFCPopupMenu*)GetParent();

			pMenu->CloseMenu ();
		}

		void UndoRedoBar::SetLabelCount(const int nCount)
		{
			UndoRedoButton* pUndoRedoButton = GetUndoRedoButton();
			
			if (ID_EDIT_UNDO == pUndoRedoButton->m_nID)
			{
				m_pImpl->sLabel.Format(_T("Undo %d Actions"), nCount);
			}

			if (ID_EDIT_REDO == pUndoRedoButton->m_nID)
			{
				m_pImpl->sLabel.Format(_T("Redo %d Actions"), nCount);
			}

			if (GetSafeHwnd () != NULL)
			{
				InvalidateRect (m_pImpl->rectLabel);
				UpdateWindow ();
			}
		}

	}
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?