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

📄 bcamtopologyviewctl.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//  (c) 2002 by Basler Vision Technologies
//  Section:  Vision Components
//  Project:  BCAM
//  $Header: BcamTopologyViewCtl.cpp, 15, 21.01.2003 15:34:00, Happe, A.$
// Author: Margret Albrecht
// Date: 29.08.2002
//-----------------------------------------------------------------------------
/**
\file     BcamTopologyViewCtl.cpp
*
\brief   Impementation of the BcamTopologyViewCtl
*
* BcamTopologyViewCtl is a control showing the available IEEE1394 devices 
* in a tree structure, the control distinguishes BCAM devices,
* DCAM compliant devices and other devices
* In this file you find the OnDraw implementation, IBcamTopologyViewCtl methods implementation 
* and implementation of the message handlers
*/
//-----------------------------------------------------------------------------



#include "stdafx.h"
#include <Bcam.h>
#include <BcamAdapter.h>
#include "BcamTopologyView.h"
#include "BcamTopologyViewCtl.h"
#include <utilities.h>

/////////////////////////////////////////////////////////////////////////////
// CBcamTopologyViewCtl

//------------------------------------------------------------------------------
// HRESULT CBcamTopologyViewCtl::OnDraw(ATL_DRAWINFO& di)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* Writes "BcamTopologyViewCtl" into the client area, if the control is inactive
* that is, when it has no window, otherwise it does nothing.
*
* \param     di holds the client rect and the device context
* \return    
*
*	allways S_OK
* 
*/
//------------------------------------------------------------------------------
HRESULT CBcamTopologyViewCtl::OnDraw(ATL_DRAWINFO& di)
{
	if (m_hWnd == NULL)
	{
		RECT& rc = *(RECT*)di.prcBounds;
		Rectangle(di.hdcDraw, rc.left, rc.top, rc.right, rc.bottom);
		
		SetTextAlign(di.hdcDraw, TA_CENTER|TA_BASELINE);
		LPCTSTR pszText = _T("BcamTopologyViewCtl");
		TextOut(di.hdcDraw, 
			(rc.left + rc.right) / 2, 
			(rc.top + rc.bottom) / 2, 
			pszText, 
			lstrlen(pszText));
	}
	return S_OK;
} 



//------------------------------------------------------------------------------
// STDMETHODIMP CBcamTopologyViewCtl::Update()
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* method of the IBcamTopologyViewCtl interface, rescans the devices 
*
* \return    
*
*  error code, Update returns allways S_OK
* 
*/
//------------------------------------------------------------------------------
STDMETHODIMP CBcamTopologyViewCtl::Update()
{
  HCURSOR oldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
	ReleaseNodes();
	BuildTree();
	SetCursor(oldCursor);
	
	return S_OK;
}

//------------------------------------------------------------------------------
// STDMETHODIMP CBcamTopologyViewCtl::ResetBus()
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* method of the IBcamTopologyViewCtl interface, generates a Bus Reset for each 
* adapter
*
* \return    
*
* error code, ResetBus returns allways S_OK
* 
*/
//------------------------------------------------------------------------------
STDMETHODIMP CBcamTopologyViewCtl::ResetBus()
{

	for (AdapterList::iterator it = m_Adapters.begin(); it != m_Adapters.end(); it++)
	{
		(*it) -> Open( (*it)->GetDeviceName() );
		(*it) -> ResetBus();
		(*it) -> Close();
	}
	
	return S_OK;
}

//------------------------------------------------------------------------------
// STDMETHODIMP CBcamTopologyViewCtl::get_Details(VARIANT_BOOL *pVal)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* tells, whether the control is showing details
*
* \param     pVal Address where to put the result
* \return    
*
* error code returns always S_OK
* 
*/
//------------------------------------------------------------------------------
STDMETHODIMP CBcamTopologyViewCtl::get_Details(VARIANT_BOOL *pVal)
{
	*pVal = m_Details;
	
	return S_OK;
}

//------------------------------------------------------------------------------
// STDMETHODIMP CBcamTopologyViewCtl::put_Details(VARIANT_BOOL newVal)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* Show or Hide Details
*
* \param     newVal true show details, false hide details
* \return    
*
* error code returns allways S_OK;
* 
*/
//------------------------------------------------------------------------------
STDMETHODIMP CBcamTopologyViewCtl::put_Details(VARIANT_BOOL newVal)
{
	if (newVal != m_Details)
	{
		m_Details = newVal;
		DeleteAllItems();
		return BuildItemTree();
	}
	
	return S_OK;
}
//------------------------------------------------------------------------------
// LRESULT CBcamTopologyViewCtl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* message handler for WM_CREATE generates the tree of connected devices
*
* \param     uMsg 
* \param     wParam
* \param     lParam
* \param     bHandled
* \return    
*
* returns the result of the DefWindowProc of the control
* 
*/
//------------------------------------------------------------------------------
LRESULT CBcamTopologyViewCtl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	
	// Let the control first create itself...
	LRESULT res = DefWindowProc(uMsg, wParam, lParam);
	// the bitmaps of the lines may be constructed with the background color of the dialog
	// so set backbround color color again, to force the background color of the control 
	SendMessage(TVM_SETBKCOLOR, 0, (LPARAM)GetSysColor(COLOR_WINDOW));
	// Create Image List
	m_ImageList.Create(16, 16, ILC_MASK, 4, 4);
	CBitmap img;
	CBitmap mask;
	// insert image for "This PC"
	img.LoadBitmap(IDB_BITMAP1);
	mask.LoadBitmap(IDB_BITMAP2);
	m_ImageList.Add(img, mask);
	// insert image for "BCAM device"
	img.Detach();
	mask.Detach();
	img.LoadBitmap(IDB_BITMAP3);
	mask.LoadBitmap(IDB_BITMAP4);
	m_ImageList.Add(img, mask);
	// insert image for connection with no device
	img.Detach();
	mask.Detach();
	img.LoadBitmap(IDB_BITMAP5);
	mask.LoadBitmap(IDB_BITMAP6);
	m_ImageList.Add(img, mask);
	// insert image for "camera device"
	img.Detach();
	mask.Detach();
	img.LoadBitmap(IDB_BITMAP7);
	mask.LoadBitmap(IDB_BITMAP8);
	m_ImageList.Add(img, mask);

	// insert image for "SBP2 device"
	img.Detach();
	mask.Detach();
	img.LoadBitmap(IDB_BITMAP9);
	mask.LoadBitmap(IDB_BITMAP10);
	m_ImageList.Add(img, mask);
	
	SetImageList(m_ImageList, TVSIL_NORMAL);

  // register the window to receive device notifications
  {
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = GUID_BACC_DEVICE;

    m_hNotify = RegisterDeviceNotification( m_hWnd, 
        &NotificationFilter,
        DEVICE_NOTIFY_WINDOW_HANDLE
    );
  }

  // build the adapter list and the tree of connected devices
  BuildAdapters();
	res = BuildTree();
	if (m_FirstItem != NULL) SelectItem(m_FirstItem);

  // without the following trick there will be a vertical scroll bar, making the window very small 
	// and resizing it to it's initial size, lets an unneccessary scroll bar disappear
	CWindow w(m_hWnd);
	RECT window;  
	w.GetWindowRect(&window);
	w.MoveWindow(window.left,window.top,20,20);
	w.MoveWindow(window.left,window.top,window.right-window.left,window.bottom-window.top);
	Invalidate(FALSE);
	UpdateWindow();

	return res;
}

//------------------------------------------------------------------------------
// LRESULT CBcamTopologyViewCtl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
 * Create a list with available adapters. For each adapter create a resource dialog
 *
 */
void CBcamTopologyViewCtl::BuildAdapters()
{
  if ( m_Adapters.size() != 0 )
    ReleaseAdapters();

	list<CString>&devs = CBcamAdapter::DeviceNames();
	for (list<CString>::iterator sit = devs.begin(); sit != devs.end(); sit++)
	{
		CBcamAdapter * adapter = new CBcamAdapter();
		adapter->Open(*sit);
		m_Adapters.insert( m_Adapters.end(), adapter );
		adapter->SetOnBusResetCallback( CBcamTopologyViewCtl::BusResetCallback, this );
		adapter->Close();
		CResourcesDlg *adapterResources = new CResourcesDlg(adapter);
		m_ResourceDialogs.insert(m_ResourceDialogs.end(),adapterResources); 
	}
}


//------------------------------------------------------------------------------
// LRESULT CBcamTopologyViewCtl::BuildTree()
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* helper method builds the tree of connected devices
*
* \return    
*
* 0 on success, 1 if an error occureed
* 
*/
//------------------------------------------------------------------------------
LRESULT CBcamTopologyViewCtl::BuildTree()
{
	LRESULT res = 1;
	
    try
    {
		m_Roots.clear();
		for (AdapterList::iterator it = m_Adapters.begin(); it != m_Adapters.end(); it++)
		{
			(*it)->Open((*it)->GetDeviceName());


			// On BusReset our cameras may issue another BusReset, which causes
			// an exception when getting the tree, so let's try to get the topology
			// up to ten times 
			for (int count = 0; count < 10; count++)
			{
				try
				{
					CNodePtr ptrRoot;
					CTopology top( sizeof (BaccResGetTopology) );
					top.Attach( *it );

					do 
					{
						top.GetTreeRequest();
						DWORD ret = top.Wait( 10000 );
						assert( WAIT_OBJECT_0 == ret );
						ptrRoot.Attach( top.GetTreeResponse(), true );
						if (NULL == ptrRoot)
						top.SetSize( top.RequiredSize() );
					} while (NULL == ptrRoot);
					m_Roots.insert( m_Roots.end(), ptrRoot);
					break;
				}
				catch( BcamException &e )
				{
					if (e.Error() != STATUS_TOPOLOGY_INVALIDATED) throw e;
				}
			}
			(*it)->Close();

		}
		res = 0;
		
    }
	CATCH_MSGBOX( "CBcamTopologyViewCtl::BuildTree"  );	

	if (res == 0) return BuildItemTree();
	else return res;
	
}
			
//------------------------------------------------------------------------------
// LRESULT CBcamTopologyViewCtl::BuildItemTree()
// Author: 
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
 * Builds the item tree showing the nodes tree
 *
 * \return    
 *
 * error code 0 = no error 1 = failed
 * 
 */
//------------------------------------------------------------------------------
LRESULT CBcamTopologyViewCtl::BuildItemTree()
{
	LRESULT res = 1;
	
    try
    {
		int bus = 0;
		for (RootList::iterator it = m_Roots.begin(); it != m_Roots.end(); it++)
		{
			// Insert an Item in the TreeControl 
			HTREEITEM hCurrentRoot = InsertNode((*it),TVI_ROOT, "Root bus", bus);
			Visit( (*it), hCurrentRoot );
			
			// Make sure the user sees a fully expanded tree
			Expand(hCurrentRoot, TVE_EXPAND);
			
			bus++;
		}
		res = 0;
		
    }
	CATCH_MSGBOX( "CBcamTopologyViewCtl::BuildItemTree"  );	
	return res;
	
}

//------------------------------------------------------------------------------
// void CBcamTopologyViewCtl::Visit( CNodePtr p, HTREEITEM hCurrentItem)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* recursive helper method sets the item data and adds the childs
*
* \param     p current node holding device information
* \param     hCurrentItem current tree item
*/
//------------------------------------------------------------------------------
void CBcamTopologyViewCtl::Visit( CNodePtr ptr, HTREEITEM hCurrentItem)
{
	if (ptr == NULL)
		return;
	CNode *p = (CNode*)ptr;
	SetItemData(hCurrentItem, (DWORD)p);
	ULONG parentConnection = p->Parent();
	
	
	for (unsigned int i= 0; i<p->NumPorts(); i++) 
	{
		if (parentConnection != i)
		{
			if (p->Child( i ) != NULL)
			{
				CNodePtr ptr;
				ptr.Attach( p->Child( i ), true );
				HTREEITEM hCurrentChild = InsertNode(ptr, hCurrentItem, "Port", i);
				
				Visit( ptr, hCurrentChild );
				// Make sure the user sees a fully expanded tree
				Expand(hCurrentChild, TVE_EXPAND);
			}
			else
			{
				if (m_Details) 
					HTREEITEM hCurrentChild = InsertNode(NULL, hCurrentItem, "Port", i);
			}
		}
	}
	
}

//------------------------------------------------------------------------------
// HTREEITEM CBcamTopologyViewCtl::InsertNode(CNodePtr ptr, HTREEITEM hCurrentItem)
// Author: 
// Date: 29.08.2002
//------------------------------------------------------------------------------
/**
* helper method inserts one item with the type dependent image and text 
*
* \param     ptr holds the device information
* \param     hCurrentItem the parent item of the new item to be inserted
* \param     text text to be inserted before the port number
* \port      port index of the parent node the new node is connected to 
* \return    
*
* the new item inserted
* 
*/
//------------------------------------------------------------------------------
HTREEITEM CBcamTopologyViewCtl::InsertNode(CNodePtr ptr, HTREEITEM hCurrentItem, char *text, int port)
{
	port = port+1;  // start with 1 
	
	CString s;
	HTREEITEM hNewItem=NULL;
	if (ptr != NULL)
	{
		enum NodeType nodeType =  ptr->Type(); 
		
		switch (nodeType)
		{
		case NT_Bcam:
			{
				if (m_Details) s.Format(_T( "%s %d : %s"), text, port, (LPCSTR)ptr->ModelName() ) ;
				else s.Format(_T( "%s"), (LPCSTR)ptr->ModelName() ) ;
				hNewItem = InsertItem(s,1,1, hCurrentItem, TVI_LAST);
				if (m_FirstItem == NULL)
					m_FirstItem = hNewItem;
				break;
			}
		case NT_Dcam:
			{
				if (m_Details) s.Format(_T( "%s %d : %s"), text, port, (LPCSTR)ptr->ModelName() ) ;
				else s.Format(_T( "%s"), (LPCSTR)ptr->ModelName() ) ;
				hNewItem = InsertItem(s,3,3, hCurrentItem, TVI_LAST);
				if (m_FirstItem == NULL)
					m_FirstItem = hNewItem;
				break;
			}
        case NT_Sbp2:
			{
				if (m_Details) s.Format(_T( "%s %d : PhysicalId: %d"), text, port, (LPCSTR)ptr->PhysicalId() ) ;
				else s = "Node";
				hNewItem = InsertItem(s,4,4, hCurrentItem, TVI_LAST);
                                break;
			}
		case NT_Unknown:

⌨️ 快捷键说明

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