Click here to Skip to main content
15,897,518 members
Articles / Desktop Programming / MFC

Building a Dynamic UI using a CWnd Free Pool

Rate me:
Please Sign up or sign in to vote.
4.84/5 (38 votes)
6 Jan 2007CPOL12 min read 325K   3.2K   141  
Classes for building MFC-based user interfaces dynamically, with a focus on minimizing resource usage.
// Filename: ControlXml.h
// 29-Nov-2006 nschan Initial revision. 

#ifndef CONTROL_XML_INCLUDED
#define CONTROL_XML_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <list>
#include <map>

// Forward class declarations.
class CControlGroup;
class CWndControl;
class CWndRadioButton;
class CWndFactory;
class CMarkup;

// Function pointer type for parsing a control.
typedef CWndControl* (*ParseControlFn)(CMarkup& xml, CWndFactory* controlFactory);

// CControlXml class uses CMarkup to parse an XML file
// and build the control tree in the form of top-level
// control group instances.
class CControlXml
{
public:
    CControlXml();
    ~CControlXml();
    
    bool ParseXml(const CString& xmlFile,
                  std::list<CControlGroup*>& topLevelGroupsList,
                  std::list<CWndControl*>& allControlsList);

private:
    bool ReadFile(const CString& xmlFile, CString& xmlText);
    bool RegisterParseControlFunction(const CString& strType, ParseControlFn parseControlFn);

    void ParseGroups(CMarkup& xml, CControlGroup* parentGroup, std::list<CControlGroup*>& groupList, std::list<CWndControl*>& controlList);
    bool ParseGroup(CMarkup& xml, CControlGroup* parentGroup, std::list<CControlGroup*>& groupList, std::list<CWndControl*>& controlList);    

    void        ParseControls(CMarkup& xml, CControlGroup* parentGroup, std::list<CWndControl*>& controlList);
    static void ParseControl_Common(CMarkup& xml, CWndControl* pControl);    
    void        PerformLinking(CMarkup& xml, CWndControl* pControl, std::list<CWndControl*>& controlList);

    static CWndControl* ParseControl_Button(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_CheckBox(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_ComboBox(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_GroupBox(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_IpAddress(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_Label(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_ListBox(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_RadioButton(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_SpinButton(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_TextBox(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_TrackBar(CMarkup& xml, CWndFactory* controlFactory);
    static CWndControl* ParseControl_WebBrowser(CMarkup& xml, CWndFactory* controlFactory);

    // Factory class for creating CWndControl subclasses.
    CWndFactory* m_controlFactory;

    // Store function pointers for parsing controls.
    std::map<CString, ParseControlFn> m_parseControlFunctions;
};

#endif // CONTROL_XML_INCLUDED

// END

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions