DlgItemTemplate.cpp
上传用户:kj0090
上传日期:2007-03-02
资源大小:39k
文件大小:14k
源码类别:
xml/soap/webservice
开发平台:
C/C++
- /***********************************************************************
- *
- * This module is part of the XMLGUI system
- *
- * File name: DlgItemTemplate.cpp
- *
- * Creation date: [12 AUGUST 2002]
- *
- * Author(s): [Kolosenko Ruslan]
- *
- * Description: Implements the class CDlgItemTemplate
- *
- **********************************************************************/
- #include "stdafx.h"
- #include "DlgItemTemplate.h"
- #include "XMLTags.h"
- #include <afxpriv.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- /**************************************************************************
- * Function : CDlgItemTemplate::CDlgItemTemplate
- * Description : Constructor
- ***************************************************************************/
- CDlgItemTemplate::CDlgItemTemplate()
- {
- m_pWinTemplate = NULL;
- m_pInitData = NULL;
- m_bIsActiveX = FALSE;
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::~CDlgItemTemplate
- * Description : Destructor
- ***************************************************************************/
- CDlgItemTemplate::~CDlgItemTemplate()
- {
- Invalidate(); // if any memory was allocated - free it
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::operator LPDLGITEMTEMPLATE
- * Description : Converts CDlgItemTemplate object to Win32 DLGITEMTEMPLATE
- * : structure, allocates memory and initializes it with dialog
- * : item template fields values
- * Return value : pointer to the allocated memory holding DLGITEMTEMPLATE structure
- * CAUTION : zero may be returned, which indicates structure building failure
- ***************************************************************************/
- CDlgItemTemplate::operator LPDLGITEMTEMPLATE()
- {
- if ( m_pWinTemplate ) return m_pWinTemplate; // if the structure
- // has been built already and it is not invalidated yet - return it
- m_pWinTemplate = (LPDLGITEMTEMPLATE)malloc(Length()); // allocate memory
- if ( !FillBuffer(m_pWinTemplate) ) // fill the memory with the template
- {
- free(m_pWinTemplate); // free memory in case of filling failure
- m_pWinTemplate = NULL;
- }
- return m_pWinTemplate;
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::Length
- * Description : Calculates number of bytes necessary to store Win32 DLGITEMTEMPLATE
- * : structure holding all control's data
- * Return value : number of bytes calculated
- ***************************************************************************/
- UINT CDlgItemTemplate::Length()
- {
- UINT nLen = sizeof(DLGITEMTEMPLATE); // all static length fields
- nLen += m_nWndClassOrdinal ? 4 : 2*(1+m_strWndClass.length()); // 4 bytes for
- // window class ordinal if any, or length of the class name
- // ( 1+ for trailing 0 , 2* for UNICODE )
- nLen += m_nRCOrdinal ? 4 : 2*(1+m_strTitle.length()); // 4 bytes for RC ID
- // if any, or length of the window title ( in UNICODE , with trailing 0 )
- nLen += 2; // reserved for creation data, will be filled with 0, inasmuch as
- // we deal with creation data for ActiveX controls in a separate structure
- // pointed to with m_pInitData
- nLen += nLen%4 ? 4-nLen%4 : 0; // align on a DWORD boundary,
- // nLen must be aliqout of 4
- return nLen;
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::GetInitData
- * Description : Allocates memory and initializes it with control's init data
- * : section used for control's creation
- * Return value : pointer to the allocated memory holding init data section
- * : or zero if the control doesn't require any init data
- ***************************************************************************/
- LPVOID CDlgItemTemplate::GetInitData()
- {
- if ( m_pInitData )
- {
- // if the memory was already allocated and it is not invalidated yet
- // we just return pointer to the old memory
- return m_pInitData;
- }
- UINT nInitDataLen = InitDataLength(); // get the required number of bytes
- if ( !nInitDataLen )
- {
- return NULL; // if no init data required for the control - return NULL
- }
- m_pInitData = malloc(nInitDataLen); // allocate new memory
- if ( !FillInitDataBuffer(m_pInitData) ) // fill the memory with init data
- {
- free(m_pInitData); // free memory in case of filling failure
- m_pInitData = NULL;
- }
- return m_pInitData;
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::InitDataLength
- * Description : Calculates number of bytes necessary to store init data
- * : section for the control's initialization
- * Return value : number of bytes calculated
- ***************************************************************************/
- UINT CDlgItemTemplate::InitDataLength()
- {
- // now only creation data for ActiveX controls is supported as init data
- // for this creation data we have to reserve 8 bytes,
- // see function FillInitDataBuffer for the fillup for these 8 bytes
- return m_bIsActiveX ? 8 : 0;
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::FillBuffer
- * Description : Fills buffer with Win32 DLGITEMTEMPLATE structure
- * Return value : number of bytes actually written to the buffer, zero indicates
- * : some data inconsistency and structure building failure
- * Arguments : lpStructBuffer - pointer to the allocated memory which is
- * : to be filled with structure fields values
- ***************************************************************************/
- UINT CDlgItemTemplate::FillBuffer(LPVOID lpStructBuffer)
- {
- UINT nTemplateLength = Length();
- if ( !AfxIsValidAddress(lpStructBuffer,nTemplateLength,TRUE) )
- {
- ASSERT(FALSE); // a not valid pointer passed to the function
- return 0; // 0 bytes were written to the buffer
- }
- if ( m_strWndClass == _bstr_t(_T("")) && !m_nWndClassOrdinal )
- {
- TRACE(_T("nNot initialized control's window class. Building DLGITEMTEMPLATE failed."));
- return 0; // no bytes were written to the buffer
- }
- // fill all fields of static length
- LPDLGITEMTEMPLATE pStaticFields = (LPDLGITEMTEMPLATE)lpStructBuffer;
- pStaticFields->style = style;
- pStaticFields->dwExtendedStyle = dwExtendedStyle;
- pStaticFields->x = x;
- pStaticFields->y = y;
- pStaticFields->cx = cx;
- pStaticFields->cy = cy;
- pStaticFields->id = id;
- // and now copy all variable length fields to the buffer
- LPBYTE pToWrite = (LPBYTE)lpStructBuffer + sizeof(DLGITEMTEMPLATE); // pointer to
- // the current byte in the structure being filled,
- // start copying from the next byte after the structure's static fields
- // store control's window class name
- if ( m_nWndClassOrdinal )
- {
- // if window class specified by an ordinal - in the template it looks like
- // ... FF FF <WORD value> ...
- *((LPWORD)pToWrite) = 0xFFFF;
- *((LPWORD)pToWrite+1) = m_nWndClassOrdinal;
- pToWrite += 4; // two words were written to the template
- }
- else
- {
- int nSizeOfClassString = 2*m_strWndClass.length(); // length of the UNICODE
- // string that will be built from m_strWndClass , excluding trailing 0
- if ( !!m_strWndClass ) // copy UNICODE string only if it is not empty
- {
- memcpy(pToWrite,(const wchar_t*)m_strWndClass,nSizeOfClassString);
- }
- *((LPWORD)(pToWrite+nSizeOfClassString)) = 0; // add trailing zero
- // to the UNICODE string
- pToWrite += nSizeOfClassString+2; // nSizeOfClassString+2 bytes were written
- }
- // store control's window text
- if ( m_nRCOrdinal )
- {
- // if specified resource ID ordinal - in the template it looks like
- // ... FF FF <WORD value> ...
- *((LPWORD)pToWrite) = 0xFFFF;
- *((LPWORD)pToWrite+1) = m_nRCOrdinal;
- pToWrite += 4; // two words were written to the template
- }
- else
- {
- int nSizeOfTitleString = 2*m_strTitle.length(); // length of the UNICODE string
- // without trailing 0 that will be built from m_strTitle
- if ( !!m_strTitle )
- {
- memcpy(pToWrite,(const wchar_t*)m_strTitle,nSizeOfTitleString);
- }
- *((LPWORD)(pToWrite+nSizeOfTitleString)) = 0;
- pToWrite += nSizeOfTitleString+2; // nSizeOfTitleString+2 bytes were written
- }
- // write 0 as init data for the control
- *(LPWORD)pToWrite = 0;
- // DLGITEMTEMPLATE structure has been filled successfully
- return nTemplateLength; // nTemplateLength bytes were written to the buffer
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::FillInitDataBuffer
- * Description : Fills buffer with control's init data section
- * Return value : number of bytes actually written to the buffer, zero indicates
- * : that the control doesn't require any init data for its creation
- * Arguments : lpInitDataBuffer - pointer to the allocated memory which is
- * : to be filled with init data
- ***************************************************************************/
- UINT CDlgItemTemplate::FillInitDataBuffer(LPVOID lpInitDataBuffer)
- {
- UINT nInitDataLen = InitDataLength();
- if ( !nInitDataLen )
- {
- return 0; // the control doesn't require init data
- }
- if ( !AfxIsValidAddress(lpInitDataBuffer,nInitDataLen,TRUE) )
- {
- ASSERT(FALSE); // a not valid pointer passed to the function
- return 0; // 0 bytes were written to the buffer
- }
- LPBYTE pToWrite = (LPBYTE)lpInitDataBuffer;
- *((LPWORD&)pToWrite)++ = id; // store control's ID
- *((LPWORD&)pToWrite)++ = WM_OCC_INITNEW; // store OCC message for creation
- *((LPDWORD)pToWrite) = 0; // store 0 as the initialization data length
- // reading stored properties of OLE controls is not yet supported in XMLGUI
- return nInitDataLen;
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::SerializeFrom
- * Description : Initializes CDlgItemTemplate object from XML tag pointer
- * : describing dialog control
- * Return value : nonzero if XML parsing is successful, otherwise zero
- * Arguments : pXMLTag - pointer to the COM interface of the XML element
- * : which contains XML-style template for dialog's control
- ***************************************************************************/
- BOOL CDlgItemTemplate::SerializeFrom(IXMLElement* pXMLTag)
- {
- try // watch possible CXMLParsingException
- {
- // construct CXMLControlTag object from the COM interface pointer
- CXMLControlTag xmlControl(pXMLTag);
- // check validity of the tag, naturally compare the tag name with "CONTROL"
- if ( !CXMLControlTag::IsTypeValid(xmlControl) )
- {
- TRACE(_T("nXML file with dialog template is not valid. Serialization failed."));
- return FALSE;
- }
- // fill all control's properties from the XML
- style = xmlControl.GetStyles();
- dwExtendedStyle = xmlControl.GetExStyles();
- x = (short)xmlControl.GetAttributeInt(ATTRIBUTE_CONTROL_LEFT);
- y = (short)xmlControl.GetAttributeInt(ATTRIBUTE_CONTROL_TOP);
- cx = (short)xmlControl.GetAttributeInt(ATTRIBUTE_CONTROL_WIDTH);
- cy = (short)xmlControl.GetAttributeInt(ATTRIBUTE_CONTROL_HEIGHT);
- id = (WORD)xmlControl.GetAttributeInt(ATTRIBUTE_CONTROL_ID);
- SetWndClassOrdinal(xmlControl.GetWindowClassOrdinal());
- if ( !GetWndClassOrdinal() )
- {
- SetWndClass(xmlControl.GetWindowClass());
- }
- SetRCOrdinal((WORD)xmlControl.GetAttributeInt(ATTRIBUTE_CONTROL_RCID));
- if ( !GetRCOrdinal() )
- {
- SetTitle(xmlControl.GetText());
- }
- m_bIsActiveX = xmlControl.IsActiveX();
- // dialog item template object was successfully initialized from XML
- return TRUE;
- }
- catch ( const CXMLParsingException& excp )
- {
- TRACE(excp.GetDescription());
- return FALSE;
- }
- }
- /**************************************************************************
- * Function : CDlgItemTemplate::Invalidate
- * Description : Discards any memory that was allocated in previous calls to
- * : the operator LPDLGITEMTEMPLATE, the next call to this operator
- * : will allocate a new memory and build a new DLGITEMTEMPLATE structure
- * CAUTION : all LPDLGITEMTEMPLATE pointers that were previously obtained
- * : through calls to operator LPDLGITEMTEMPLATE become invalid
- * : and should not be used
- ***************************************************************************/
- void CDlgItemTemplate::Invalidate()
- {
- // if any memory was allocated for DLGITEMTEMPLATE structure - free it
- // and set m_pWinTemplate to NULL which allows next memory allocation
- if ( m_pWinTemplate )
- {
- free (m_pWinTemplate);
- m_pWinTemplate = NULL;
- }
- // free memory allocated for init data section as well and set m_pInitData
- // to NULL allowing next allocations
- if ( m_pInitData )
- {
- free ( m_pInitData );
- m_pInitData = NULL;
- }
- }