DlgItemTemplate.cpp ( File view )

  • By 2010-07-16
  • View(s):10
  • Download(s):0
  • Point(s): 1
			/***********************************************************************
*
* 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 CDlgI
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.00 B
DlgItemTemplate.cpp14.14 kB01-12-03 17:47
DlgItemTemplate.h4.50 kB01-12-03 17:36
DlgTemplate.cpp16.08 kB01-12-03 17:47
DlgTemplate.h5.53 kB01-13-03 11:05
01.00 B
XMLGUI.rc2398.00 B03-26-01 17:35
StdAfx.cpp208.00 B03-26-01 17:35
StdAfx.h1.47 kB11-27-02 16:34
XMLCustomizable.cpp4.05 kB01-12-03 17:28
XMLCustomizableDialogs.h4.20 kB01-04-03 18:27
XMLCustomizableForms.h6.37 kB01-06-03 16:33
XMLCustomizablePages.h3.77 kB01-04-03 19:33
XMLGUI.cpp1.50 kB07-30-02 18:31
XMLGUI.def184.00 B11-27-02 16:22
XMLGUI.dsp7.89 kB01-13-03 16:42
XMLGUI.rc4.13 kB08-27-02 15:20
XMLGUIMacro.h567.00 B11-27-02 16:34
XMLGUISyntax.cpp25.71 kB01-13-03 14:28
XMLGUISyntax.h3.23 kB01-11-03 14:17
xmlgui_resource.h505.00 B08-27-02 15:19
XMLIterator.h5.23 kB11-27-02 16:20
XMLParsingException.cpp3.12 kB11-22-02 15:12
XMLParsingException.h1.33 kB11-22-02 15:12
XMLTags.cpp22.36 kB01-10-03 18:02
XMLTags.h5.50 kB01-12-03 16:47
...
Sponsored links

DlgItemTemplate.cpp (39.09 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D