| 1 |
#include "wxc_project_metadata.h" |
| 2 |
|
| 3 |
#include "event_notifier.h" |
| 4 |
|
| 5 |
#include <wx/ffile.h> |
| 6 |
#include <wx/filename.h> |
| 7 |
|
| 8 |
const wxEventType wxEVT_CMD_WXCRAFTER_PROJECT_MODIFIED = wxNewEventType(); |
| 9 |
const wxEventType wxEVT_CMD_WXCRAFTER_PROJECT_SYNCHED = wxNewEventType(); |
| 10 |
|
| 11 |
wxcProjectMetadata::wxcProjectMetadata() |
| 12 |
: m_objCounter(0) |
| 13 |
, m_useHpp(true) |
| 14 |
, m_firstWindowId(10000) |
| 15 |
, m_useEnum(true) |
| 16 |
, m_useUnderscoreMacro(true) |
| 17 |
, m_addHandlers(true) |
| 18 |
{ |
| 19 |
SetGenerateCPPCode(true); |
| 20 |
SetGenerateXRC(false); |
| 21 |
} |
| 22 |
|
| 23 |
wxcProjectMetadata::~wxcProjectMetadata() {} |
| 24 |
|
| 25 |
void wxcProjectMetadata::FromJSON(const JSONElement& json) |
| 26 |
{ |
| 27 |
m_objCounter = json.namedObject("m_objCounter").toInt(); |
| 28 |
m_generatedFilesDir = json.namedObject("m_generatedFilesDir").toString(); |
| 29 |
m_includeFiles = json.namedObject("m_includeFiles").toArrayString(); |
| 30 |
m_bitmapFunction = json.namedObject("m_bitmapFunction").toString(); |
| 31 |
m_bitmapsFile = json.namedObject("m_bitmapsFile").toString(); |
| 32 |
m_GenerateCodeTypes = json.namedObject("m_GenerateCodeTypes").toInt(wxcGenerateCPPCode); |
| 33 |
m_outputFileName = json.namedObject("m_outputFileName").toString(); |
| 34 |
m_firstWindowId = json.namedObject("m_firstWindowId").toInt(m_firstWindowId); |
| 35 |
m_useEnum = json.namedObject("m_useEnum").toBool(true); |
| 36 |
m_useUnderscoreMacro = json.namedObject("m_useUnderscoreMacro").toBool(true); |
| 37 |
m_addHandlers = json.namedObject("m_addHandlers").toBool(m_addHandlers); |
| 38 |
|
| 39 |
wxcSettings::Get().MergeCustomControl(json.namedObject("m_templateClasses")); |
| 40 |
if(m_bitmapFunction.IsEmpty()) { |
| 41 |
DoGenerateBitmapFunctionName(); |
| 42 |
} |
| 43 |
|
| 44 |
// for backward-compatibility, we continue to use .h file extension if it's already there |
| 45 |
wxFileName headerFile = BaseCppFile(); |
| 46 |
headerFile.SetExt("h"); |
| 47 |
if(headerFile.IsRelative()) { |
| 48 |
headerFile.MakeAbsolute(GetProjectPath()); |
| 49 |
} |
| 50 |
wxString header_file = headerFile.GetFullPath(); |
| 51 |
m_useHpp = !wxFileName::FileExists(header_file); |
| 52 |
} |
| 53 |
|
| 54 |
JSONElement wxcProjectMetadata::ToJSON() |
| 55 |
{ |
| 56 |
JSONElement metadata = JSONElement::createObject("metadata"); |
| 57 |
UpdatePaths(); |
| 58 |
|
| 59 |
metadata.addProperty("m_generatedFilesDir", m_generatedFilesDir); |
| 60 |
metadata.addProperty("m_objCounter", (int)m_objCounter); |
| 61 |
metadata.addProperty("m_includeFiles", m_includeFiles); |
| 62 |
metadata.addProperty("m_bitmapFunction", m_bitmapFunction); |
| 63 |
metadata.addProperty("m_bitmapsFile", m_bitmapsFile); |
| 64 |
metadata.addProperty("m_GenerateCodeTypes", m_GenerateCodeTypes); |
| 65 |
metadata.addProperty("m_outputFileName", m_outputFileName); |
| 66 |
metadata.addProperty("m_firstWindowId", m_firstWindowId); |
| 67 |
metadata.addProperty("m_useEnum", m_useEnum); |
| 68 |
metadata.addProperty("m_useUnderscoreMacro", m_useUnderscoreMacro); |
| 69 |
metadata.addProperty("m_addHandlers", m_addHandlers); |
| 70 |
return metadata; |
| 71 |
} |
| 72 |
|
| 73 |
void wxcProjectMetadata::AppendCustomControlsJSON(const wxArrayString& controls, JSONElement& element) const |
| 74 |
{ |
| 75 |
JSONElement customControls = wxcSettings::Get().GetCustomControlsAsJSON(controls); |
| 76 |
element.append(customControls); |
| 77 |
} |
| 78 |
|
| 79 |
wxString wxcProjectMetadata::GetCppFileName() const |
| 80 |
{ |
| 81 |
wxFileName cpp(m_generatedFilesDir, m_projectFile); |
| 82 |
cpp.SetExt("cpp"); |
| 83 |
return cpp.GetFullPath(); |
| 84 |
} |
| 85 |
|
| 86 |
wxString wxcProjectMetadata::GetXrcFileName() const |
| 87 |
{ |
| 88 |
wxFileName xrc(m_projectFile); |
| 89 |
if(!xrc.IsAbsolute()) { |
| 90 |
xrc = wxFileName(m_generatedFilesDir, m_projectFile); |
| 91 |
} |
| 92 |
xrc.SetExt("xrc"); |
| 93 |
return xrc.GetFullPath(); |
| 94 |
} |
| 95 |
|
| 96 |
wxString wxcProjectMetadata::GetHeaderFileName() const |
| 97 |
{ |
| 98 |
wxFileName header(m_generatedFilesDir, m_projectFile); |
| 99 |
header.SetExt(GetHeaderFileExt()); |
| 100 |
return header.GetFullPath(); |
| 101 |
} |
| 102 |
|
| 103 |
wxcProjectMetadata& wxcProjectMetadata::Get() |
| 104 |
{ |
| 105 |
static wxcProjectMetadata p; |
| 106 |
return p; |
| 107 |
} |
| 108 |
|
| 109 |
wxString wxcProjectMetadata::GetProjectPath() const |
| 110 |
{ |
| 111 |
wxFileName fn(m_projectFile); |
| 112 |
return fn.GetPath(); |
| 113 |
} |
| 114 |
|
| 115 |
wxString wxcProjectMetadata::GetGeneratedFilesDir() const |
| 116 |
{ |
| 117 |
wxString value; |
| 118 |
m_generatedFilesDir.IsEmpty() ? value = "." : value = m_generatedFilesDir; |
| 119 |
return value; |
| 120 |
} |
| 121 |
|
| 122 |
wxFileName wxcProjectMetadata::BaseCppFile() const |
| 123 |
{ |
| 124 |
wxFileName baseFile(GetGeneratedFilesDir(), GetOutputFileName()); |
| 125 |
baseFile.SetExt("cpp"); |
| 126 |
return baseFile; |
| 127 |
} |
| 128 |
|
| 129 |
wxFileName wxcProjectMetadata::BaseHeaderFile() const |
| 130 |
{ |
| 131 |
wxFileName f = BaseCppFile(); |
| 132 |
f.SetExt(GetHeaderFileExt()); |
| 133 |
return f; |
| 134 |
} |
| 135 |
|
| 136 |
void wxcProjectMetadata::ClearAggregatedData() |
| 137 |
{ |
| 138 |
m_generatedHeader.Clear(); |
| 139 |
m_generatedSource.Clear(); |
| 140 |
m_generatedClassName.Clear(); |
| 141 |
m_additionalFiles.clear(); |
| 142 |
} |
| 143 |
|
| 144 |
void wxcProjectMetadata::Reset() |
| 145 |
{ |
| 146 |
m_generatedHeader.Clear(); |
| 147 |
m_generatedSource.Clear(); |
| 148 |
m_generatedClassName.Clear(); |
| 149 |
m_virtualFolder.Clear(); |
| 150 |
m_includeFiles.Clear(); |
| 151 |
m_projectFile.Clear(); |
| 152 |
m_generatedFilesDir.Clear(); |
| 153 |
m_bitmapsFile.Clear(); |
| 154 |
m_additionalFiles.clear(); |
| 155 |
m_outputFileName.Clear(); |
| 156 |
m_useHpp = true; |
| 157 |
m_useEnum = true; |
| 158 |
m_useUnderscoreMacro = true; |
| 159 |
m_firstWindowId = 10000; |
| 160 |
m_addHandlers = true; |
| 161 |
} |
| 162 |
|
| 163 |
void wxcProjectMetadata::DoGenerateBitmapFunctionName() |
| 164 |
{ |
| 165 |
m_bitmapFunction.Clear(); |
| 166 |
wxString name = wxFileName::CreateTempFileName("wxCrafter"); |
| 167 |
wxFileName fn(name); |
| 168 |
name.Clear(); |
| 169 |
name << fn.GetName() << "InitBitmapResources"; |
| 170 |
m_bitmapFunction = name; |
| 171 |
} |
| 172 |
|
| 173 |
wxString wxcProjectMetadata::DoGenerateBitmapsFile() const |
| 174 |
{ |
| 175 |
if(GetProjectFile().IsEmpty()) { |
| 176 |
return ""; |
| 177 |
} |
| 178 |
|
| 179 |
wxFileName projectFileName(GetProjectFile()); |
| 180 |
wxFileName fn = wxFileName(GetProjectPath(), projectFileName.GetFullName()); |
| 181 |
wxString projectName = projectFileName.GetName(); |
| 182 |
|
| 183 |
const wxArrayString& dirs = projectFileName.GetDirs(); |
| 184 |
wxString lastDirName = dirs.IsEmpty() ? "" : dirs.Last(); |
| 185 |
lastDirName.MakeLower(); |
| 186 |
|
| 187 |
fn.SetName(projectName + "_" + lastDirName + "_bitmaps"); |
| 188 |
fn.SetExt("cpp"); |
| 189 |
return fn.GetFullName(); |
| 190 |
} |
| 191 |
|
| 192 |
wxString wxcProjectMetadata::GetBitmapsFile() const |
| 193 |
{ |
| 194 |
if(m_bitmapsFile.IsEmpty()) { |
| 195 |
return DoGenerateBitmapsFile(); |
| 196 |
} |
| 197 |
|
| 198 |
return m_bitmapsFile; |
| 199 |
} |
| 200 |
void wxcProjectMetadata::Serialize(const wxcWidget::List_t& topLevelsList, const wxFileName& filename) |
| 201 |
{ |
| 202 |
wxcProjectMetadata p; |
| 203 |
p.GenerateBitmapFunctionName(); |
| 204 |
|
| 205 |
JSONRoot root(cJSON_Object); |
| 206 |
root.toElement().append(p.ToJSON()); |
| 207 |
|
| 208 |
// The windows |
| 209 |
JSONElement windows = JSONElement::createArray("windows"); |
| 210 |
root.toElement().append(windows); |
| 211 |
|
| 212 |
wxFFile fp(filename.GetFullPath(), "w+b"); |
| 213 |
if(fp.IsOpened()) { |
| 214 |
|
| 215 |
wxcWidget::List_t::const_iterator iter = topLevelsList.begin(); |
| 216 |
for(; iter != topLevelsList.end(); ++iter) { |
| 217 |
JSONElement obj = JSONElement::createObject(); |
| 218 |
(*iter)->FixPaths(filename.GetPath()); // Fix abs paths to fit the new project file |
| 219 |
(*iter)->Serialize(obj); |
| 220 |
windows.arrayAppend(obj); |
| 221 |
} |
| 222 |
|
| 223 |
fp.Write(root.toElement().format(), wxConvUTF8); |
| 224 |
fp.Close(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
wxString wxcProjectMetadata::GetOutputFileName() const |
| 229 |
{ |
| 230 |
if(m_outputFileName.IsEmpty()) { |
| 231 |
wxFileName fn(GetProjectFile()); |
| 232 |
return fn.GetName(); |
| 233 |
} |
| 234 |
return m_outputFileName; |
| 235 |
} |
| 236 |
|
| 237 |
wxString wxcProjectMetadata::GetHeaderFileExt() const { return m_useHpp ? "hpp" : "h"; } |
| 238 |
|
| 239 |
void wxcProjectMetadata::SetProjectFile(const wxString& filename) |
| 240 |
{ |
| 241 |
this->m_projectFile = filename; |
| 242 |
UpdatePaths(); |
| 243 |
} |
| 244 |
|
| 245 |
void wxcProjectMetadata::UpdatePaths() |
| 246 |
{ |
| 247 |
if(m_generatedFilesDir.IsEmpty()) { |
| 248 |
m_generatedFilesDir = "."; |
| 249 |
} |
| 250 |
if(m_bitmapsFile.IsEmpty()) { |
| 251 |
m_bitmapsFile = DoGenerateBitmapsFile(); |
| 252 |
} |
| 253 |
} |