⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabdockingbox.h

📁 These listed libraries are written in WTL. But it s really hard to mix both MFC & WTL together. Obvi
💻 H
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) 2002
// Sergey Klimov (kidd@ukr.net)
// WTL Docking windows
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in  this file is used in any commercial application
// then a simple email woulod be nice.

#ifndef __WTL_DW__TABDOCKINGBOX_H__
#define __WTL_DW__TABDOCKINGBOX_H__

#include "DockingBox.h"
#include "FlyingTabs.h"

namespace dockwins{


template<class TTraits=COutlookLikeDockingBoxTraits>
class CTabDockingBox :
			public  CDockingBoxBaseImpl<CTabDockingBox<TTraits>,CWindow,TTraits>
{
	typedef	CDockingBoxBaseImpl<CTabDockingBox,CWindow,TTraits> baseClass;
	typedef CTabDockingBox  thisClass;
	typedef CFlyingTabCtrl	CTabCtrl;
protected:
	static void SetIndex(DFDOCKRECT* pHdr,int index)
	{
		pHdr->rect.left=index;
		pHdr->rect.right=index;
		pHdr->rect.top=index;
		pHdr->rect.bottom=index;
	}
	static int GetIndex(DFDOCKRECT* pHdr)
	{
		return pHdr->rect.left;
	}
public:
	static HWND CreateInstance(HWND hWnd)
	{
		thisClass* ptr=new thisClass;
		HWND hNewWnd=ptr->Create(hWnd);
		assert(hNewWnd);
		if(hNewWnd==NULL)
			delete ptr;
		return hNewWnd;
	}
	virtual bool DockMe(DFDOCKRECT* pHdr)
	{
		if(CDockingBox::IsWindowBox(pHdr->hdr.hBar))
		{
			assert(m_wnd.m_hWnd);
			HWND hActiveWnd=m_wnd.m_hWnd;
			int n=m_tabs.GetItemCount();
			assert(n>=2);
			while(n>0)
			{
				pHdr->hdr.hWnd=GetItemHWND(--n);
				if(pHdr->hdr.hWnd!=NULL)
				{
					RemoveWindow(pHdr->hdr.hWnd);
					m_docker.Dock(pHdr);
				}
			}
			pHdr->hdr.hWnd=hActiveWnd;
//			pHdr->hdr.code=DC_ACTIVATE;
			m_docker.Activate(&pHdr->hdr);

			PostMessage(WM_CLOSE);
		}
		else
			m_docker.Dock(pHdr);
		return true;
	}
	bool IsPointInAcceptedArea(POINT *pPt) const
	{
		HWND hWnd=::WindowFromPoint(*pPt);
		while( (hWnd!=m_hWnd)
					&&(hWnd!=NULL))
			hWnd=::GetParent(hWnd);
		bool bRes=(hWnd!=NULL);
		if(bRes)
		{
			CRect rc;
			CPoint pt(*pPt);
			m_tabs.GetTabsRect(&rc);
			m_tabs.ScreenToClient(pPt);
			bRes=(rc.PtInRect(*pPt)!=FALSE);
			if(!bRes)
			{
				bRes=::SendMessage(m_hWnd,WM_NCHITTEST,NULL,MAKELPARAM(pt.x, pt.y))==HTCAPTION;
				if(bRes)
				{
					if( !IsDocking() || m_caption.IsHorizontal() )
						pPt->y=(rc.bottom+rc.top)/2;
					else
						*pPt=rc.CenterPoint();
					// now only horizontal tab control supported
///11					assert((m_tabs.GetWindowLong(GWL_STYLE)&TCS_VERTICAL)==0);
					assert((m_tabs.GetWindowLong(GWL_STYLE)&CTCS_VERTICAL)==0);
				}
			}
		}
		return bRes;
	}
	LRESULT OnAcceptDock(DFDOCKRECT* pHdr)
	{
		CPoint pt(pHdr->rect.left,pHdr->rect.top);
		BOOL bRes=IsPointInAcceptedArea(&pt);
		if(bRes)
		{
			MSG msg={0};
			int curSel=m_tabs.GetCurSel();
			assert(curSel!=-1);
			HWND hWnd=GetItemHWND(curSel);
///11		bool bHorizontal=!(m_tabs.GetWindowLong(GWL_STYLE)&TCS_VERTICAL);
			bool bHorizontal=!(m_tabs.GetWindowLong(GWL_STYLE)&CTCS_VERTICAL);
			int pos = bHorizontal ? pt.x : pt.y;

///11		TCHITTESTINFO tchti = { 0 };
			CTCHITTESTINFO tchti = { 0 };
			tchti.pt.x = pt.x;
			tchti.pt.y = pt.y;
			int index=m_tabs.HitTest(&tchti);
			if((hWnd!=NULL) && (hWnd!=pHdr->hdr.hWnd))
			{
				if(index==-1)
				{
					RECT rc;
					m_tabs.GetItemRect(0,&rc);
					if(bHorizontal)
						index=(rc.left>pos) ? 0 : m_tabs.GetItemCount();
					else
						index=(rc.top>pos) ? 0 : m_tabs.GetItemCount();
				}
				m_prevSelItem=curSel;
				curSel=InsertWndTab(index,pHdr->hdr.hWnd,0);
				// dispatch all notifications
				while(PeekMessage(&msg, NULL, WM_NOTIFY, WM_NOTIFY, PM_REMOVE))
					DispatchMessage(&msg);
				m_prevItem=curSel;
				m_prevPos=pos;
				assert(index==curSel);
			}
			if(
				(index!=-1)
					&&( index!=curSel)
						 &&
						 !( ( index==m_prevItem)
								&& ( (pos-m_prevPos)*(m_prevItem-curSel) <=0) ) )
			{
				m_tabs.SwapItemPositions(curSel,index, false, false);
//				m_tabs.MoveItem(curSel,index);
				curSel=index;
				m_tabs.SetCurSel(curSel);
				m_prevItem=m_tabs.HitTest(&tchti);
			}
			m_prevPos = pos;
			pHdr->hdr.hBar=m_hWnd;
			SetIndex(pHdr,curSel);

			//check next message
			while(WaitMessage())
			{
				//redraw first
				while(PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE))
					DispatchMessage(&msg);
				if(PeekMessage(&msg, 0, WM_SYSKEYDOWN, WM_SYSKEYDOWN, PM_NOREMOVE))
					break;
				if(PeekMessage(&msg,pHdr->hdr.hWnd,0, 0, PM_NOREMOVE))
				{
					if(pHdr->hdr.hWnd==msg.hwnd)
											break;
					else
					{
						if(PeekMessage(&msg, msg.hwnd, msg.message, msg.message, PM_REMOVE))
							DispatchMessage(&msg);
					}
				}
			}

			pt.x=GET_X_LPARAM( msg.lParam );
			pt.y=GET_Y_LPARAM( msg.lParam );
			::ClientToScreen(msg.hwnd,&pt);

			if( msg.message!=WM_MOUSEMOVE
				|| (::GetKeyState(VK_CONTROL)&0x8000)
					|| !IsPointInAcceptedArea(&pt) )
			{
				if(GetItemHWND(curSel)==NULL)
				{
					m_tabs.DeleteItem(curSel,false);
					assert(m_prevSelItem>=0);
					assert(m_prevSelItem<m_tabs.GetItemCount());
					m_tabs.SetCurSel(m_prevSelItem);
				}
				//let control update itself!!!
				while(PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE))
					DispatchMessage(&msg);
			}
		}
		return bRes;
	}
	LRESULT OnDock(DFDOCKRECT* pHdr)
	{
		assert(pHdr->hdr.hWnd);
		int index=GetIndex(pHdr);
		int n=m_tabs.GetItemCount();
		if( (index<0) || (index>n) )
								index=n;
		return (InsertWndTab(index,pHdr->hdr.hWnd)!=-1);
	}

    LRESULT OnUndock(DFMHDR* pHdr)
    {
		CWindow wnd(pHdr->hWnd);
		assert(::IsWindow(pHdr->hWnd));
		BOOL bRes=RemoveWindow(pHdr->hWnd);
		IsStillAlive();
		return bRes;
    }

	LRESULT OnActivate(DFMHDR* pHdr)
	{
		int index=FindItem(pHdr->hWnd);
		BOOL bRes=(index!=-1);
		if(bRes)
			m_tabs.SetCurSel(index);
		if(!IsWindowVisible())
							Show();
		return bRes;
	}

	void PrepareForDock(CWindow wnd)
	{
        wnd.ShowWindow(SW_HIDE);
        DWORD style = wnd.GetWindowLong(GWL_STYLE);
        DWORD newStyle = style&(~(WS_POPUP | WS_CAPTION))|WS_CHILD;
        wnd.SetWindowLong( GWL_STYLE, newStyle);
		wnd.SetParent(m_hWnd);
		wnd.SendMessage(WM_NCACTIVATE,TRUE);
        wnd.SendMessage(WMDF_NDOCKSTATECHANGED,
                                MAKEWPARAM(TRUE,FALSE),
                                reinterpret_cast<LPARAM>(m_hWnd));

	}
	void PrepareForUndock(CWindow wnd)
	{
        wnd.ShowWindow(SW_HIDE);
        DWORD style = wnd.GetWindowLong(GWL_STYLE);
        DWORD newStyle = style&(~WS_CHILD) | WS_POPUP | WS_CAPTION;
        wnd.SetWindowLong( GWL_STYLE, newStyle);
		wnd.SetParent(NULL);
        wnd.SendMessage(WMDF_NDOCKSTATECHANGED,
                FALSE,
                reinterpret_cast<LPARAM>(m_hWnd));
	}
	int InsertWndTab(int index,CWindow wnd)
	{
         assert(wnd.IsWindow());
		PrepareForDock(wnd);
		return InsertWndTab(index,wnd,reinterpret_cast<DWORD>(wnd.m_hWnd));
	}
	int InsertWndTab(int index,CWindow wnd,DWORD param)
	{
		assert(index>=0);
		assert(index<=m_tabs.GetItemCount());
		assert(wnd.IsWindow());
		int txtLen=wnd.GetWindowTextLength()+1;
		TCHAR* ptxt = new TCHAR[txtLen];
		wnd.GetWindowText(ptxt,txtLen);
		int image = -1;
		HICON hIcon=wnd.GetIcon(FALSE);
		if(hIcon == NULL)
			hIcon = (HICON) ::GetClassLong(wnd.m_hWnd, GCL_HICONSM);
		if(hIcon)
			image = m_images.AddIcon(hIcon);
		index=m_tabs.InsertItem(index,ptxt,image,param);
        delete[] ptxt;
        return index;
	}
	BOOL RemoveWindow(CWindow wnd)
	{
		int index=FindItem(wnd);
		BOOL bRes=(index!=-1);
		if(bRes)
		{
/*
			TCITEM tci;
			tci.mask=TCIF_IMAGE ;
			if(m_tabs.GetItem(index,&tci))
				m_images.Remove(tci.iImage);
*/
			if(m_wnd.m_hWnd==wnd.m_hWnd)
				m_wnd.m_hWnd=NULL;
			bRes=m_tabs.DeleteItem(index);
			if(bRes)
				PrepareForUndock(wnd);
		}
		return bRes;
	}
	int FindItem(HWND hWnd) const
	{
		int n=m_tabs.GetItemCount();
		for( int i=0;i<n;i++)
		{
			if(GetItemHWND(i)==hWnd)
				return i;
		}
		return -1;
	}
    HWND GetItemHWND(int index) const
    {
        return reinterpret_cast<HWND>(m_tabs.GetItemData(index));
    }

	void AdjustCurentItem()
	{
		if(m_wnd.m_hWnd!=0)
		{

⌨️ 快捷键说明

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