1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include "custom_control_wrapper.h"
#include "allocator_mgr.h"
#include "file_logger.h"
#include "wxc_settings.h"
#include "xmlutils.h"
#include <macromanager.h>
#include <wx/regex.h>
CustomControlWrapper::CustomControlWrapper()
: wxcWidget(ID_WXCUSTOMCONTROL)
{
DelProperty(_("Control Specific Settings"));
AddProperty(new CategoryProperty(m_templInfoName, "Custom Control"));
m_namePattern = wxT("m_custom");
// Generate a name if there isn't one; but don't overwrite e.g. an imported one
if(!GetName().empty()) { SetName(GenerateName()); }
}
CustomControlWrapper::~CustomControlWrapper() {}
wxcWidget* CustomControlWrapper::Clone() const { return new CustomControlWrapper(); }
wxString CustomControlWrapper::CppCtorCode() const
{
CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
if(cct.IsValid() == false) return wxT("");
// support for Subclass
wxString subclass = PropertyString(PROP_SUBCLASS_NAME);
subclass.Trim().Trim(false);
wxString cppCode = cct.GetAllocationLine();
cppCode.Trim().Trim(false);
// cppCode contains the allocation line
// something like:
// $name = new MyCustomClassName(...);
static wxRegEx reNewClass("new[ \\t]+([A-Za-z_]{1}[A-Za-z0-9_]*[ \\t]*\\()");
if(!subclass.IsEmpty() && reNewClass.IsValid() && reNewClass.Matches(cppCode)) {
reNewClass.Replace(&cppCode, "new " + subclass + "(");
}
if(!cppCode.EndsWith(wxT(";"))) { cppCode << wxT(";"); }
// Replace $name with the actual control c++ name
cppCode = MacroManager::Instance()->Replace(cppCode, wxT("name"), GetName(), true);
cppCode = MacroManager::Instance()->Replace(cppCode, wxT("parent"), GetWindowParent(), true);
cppCode = MacroManager::Instance()->Replace(cppCode, wxT("id"), GetId(), true);
return cppCode;
}
void CustomControlWrapper::GetIncludeFile(wxArrayString& headers) const
{
CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
if(cct.IsValid() == false) return;
wxString inclLine = cct.GetIncludeFile();
if(inclLine.IsEmpty()) return;
inclLine.Trim().Trim(false);
if(inclLine.EndsWith(wxT(";"))) { inclLine.RemoveLast(); }
headers.Add(inclLine);
}
wxString CustomControlWrapper::GetWxClassName() const
{
CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
if(cct.IsValid() == false) return wxT("");
return cct.GetClassName();
}
void CustomControlWrapper::ToXRC(wxString& text, XRC_TYPE type) const
{
CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
if(cct.IsValid() && !cct.GetXrcPreviewClass().IsEmpty())
text << wxT("<object class=\"") << cct.GetXrcPreviewClass() << wxT("\" name=\"") << GetName() << wxT("\">");
else
text << wxT("<object class=\"unknown\" name=\"") << GetName() << wxT("\">");
text << XRCSize() << XRCCommonAttributes() << XRCSuffix();
}
void CustomControlWrapper::Serialize(JSONElement& json) const
{
wxcWidget::Serialize(json);
json.addProperty(wxT("m_templInfoName"), m_templInfoName);
ms_customControlsUsed.insert(
std::make_pair(m_templInfoName, wxcSettings::Get().FindByControlName(m_templInfoName)));
}
void CustomControlWrapper::UnSerialize(const JSONElement& json)
{
m_templInfoName = json.namedObject(wxT("m_templInfoName")).toString();
DoUpdateEvents();
wxcWidget::UnSerialize(json);
m_properties.Item("Custom Control")->SetValue(m_templInfoName);
}
/*
void CustomControlWrapper::LoadPropertiesFromwxSmith(const wxXmlNode* node)
{
// First call the base-class for the standard things
wxcWidget::LoadPropertiesFromwxSmith(node);
wxString wxclassname, include, construction, declaration, name, xrcclassname, settings;
xrcclassname = XmlUtils::ReadString(node, wxT("name")); // I suppose that's right: "ID_CUSTOM1"
name = XmlUtils::ReadString(node, wxT("subclass")); // I suppose that's right: "MyCustomClass"
propertynode = XmlUtils::FindFirstByTagName(node, wxT("creating_code"));
if (propertynode) {
construction = propertynode->GetNodeContent());
}
propertynode = XmlUtils::FindFirstByTagName(node, wxT("include_file"));
if (propertynode) {
include = propertynode->GetNodeContent());
}
// Hmm. I can't find any way to find the superclass's wx name e.g. wxPanel, so I'll abort this atm
}*/
void CustomControlWrapper::LoadPropertiesFromwxFB(const wxXmlNode* node)
{
// First call the base-class for the standard things
wxcWidget::LoadPropertiesFromwxFB(node);
wxString include, construction, declaration, name, xrcclassname, settings;
wxXmlNode* propertynode = XmlUtils::FindNodeByName(node, "property", "name");
if(propertynode) { name = propertynode->GetNodeContent(); }
propertynode = XmlUtils::FindNodeByName(node, "property", "declaration");
if(propertynode) { declaration = propertynode->GetNodeContent(); }
propertynode = XmlUtils::FindNodeByName(node, "property", "include");
if(propertynode) { include = propertynode->GetNodeContent(); }
propertynode = XmlUtils::FindNodeByName(node, "property", "construction");
if(propertynode) { construction = propertynode->GetNodeContent(); }
propertynode = XmlUtils::FindNodeByName(node, "property", "settings");
if(propertynode) { settings = propertynode->GetNodeContent(); }
propertynode = XmlUtils::FindNodeByName(node, "property", "class");
if(propertynode) { xrcclassname = propertynode->GetNodeContent(); }
// The wxFB fields aren't the same as the wxC ones, so we need to do some guesswork here:
wxString wxclassname = declaration.BeforeFirst('*').Trim(); // Get the real classname from 'MyFoo * m_foo;'
if(wxclassname.empty() || construction.empty() ||
declaration.empty()) { // settings and xrcclassname are optional; 'include' probably is too
CL_WARNING("Failed to load a Custom Control from wxFB: not all necessary data was available");
return;
}
CustomControlTemplate controlData;
controlData.SetAllocationLine(construction);
controlData.SetClassName(wxclassname);
controlData.SetIncludeFile(include);
// controlData.Set/**/Class(settings); wxC doesn't do this atm
controlData.SetXrcPreviewClass(xrcclassname);
SetTemplInfoName(wxclassname);
wxcSettings::Get().RegisterCustomControl(controlData);
wxcSettings::Get().Save();
}
void CustomControlWrapper::SetTemplInfoName(const wxString& templInfoName)
{
this->m_templInfoName = templInfoName;
DoUpdateEvents();
}
void CustomControlWrapper::DoUpdateEvents()
{
m_controlEvents.Clear();
m_connectedEvents.Clear();
CustomControlTemplate cct = wxcSettings::Get().FindByControlName(m_templInfoName);
if(cct.IsValid() == false) return;
const wxStringMap_t& events = cct.GetEvents();
wxStringMap_t::const_iterator iter = events.begin();
for(; iter != events.end(); ++iter) {
RegisterEvent(iter->first, iter->second, "");
// m_controlEvents.Add(ConnectDetails(iter->first, iter->second, "", wxString() << iter->second << "Handler"));
}
}
void CustomControlWrapper::DoDeepCopy(const wxcWidget& rhs, enum DuplicatingOptions nametypesToChange,
const std::set<wxString>& existingNames, const wxString& chosenName,
const wxString& chosenInheritedName, const wxString& chosenFilename)
{
wxcWidget::DoDeepCopy(rhs, nametypesToChange, existingNames, chosenName, chosenInheritedName, chosenFilename);
/// copy some custom control specific content
const CustomControlWrapper& origin = static_cast<const CustomControlWrapper&>(rhs);
m_templInfoName = origin.m_templInfoName;
}
|