| 1 |
#include "wxc_bitmap_code_generator.h" |
| 2 |
#include "DirectoryChanger.h" |
| 3 |
#include "top_level_win_wrapper.h" |
| 4 |
#include "wxc_project_metadata.h" |
| 5 |
#include "wxgui_bitmaploader.h" |
| 6 |
#include "wxgui_helpers.h" |
| 7 |
#include "wxguicraft_main_view.h" |
| 8 |
#include "wxrc.h" |
| 9 |
#include <environmentconfig.h> |
| 10 |
#include <globals.h> |
| 11 |
#include <processreaderthread.h> |
| 12 |
#include <wx/frame.h> |
| 13 |
#include <wx/log.h> |
| 14 |
#include <wx/sstream.h> |
| 15 |
#include <wx/stdpaths.h> |
| 16 |
#include <wx/utils.h> |
| 17 |
#include <wx/xml/xml.h> |
| 18 |
|
| 19 |
const wxEventType wxEVT_BITMAP_CODE_GENERATION_DONE = ::wxNewEventType(); |
| 20 |
|
| 21 |
wxcCodeGeneratorHelper::wxcCodeGeneratorHelper() |
| 22 |
{ |
| 23 |
// m_bmpGenThread.Start(); |
| 24 |
} |
| 25 |
|
| 26 |
wxcCodeGeneratorHelper::~wxcCodeGeneratorHelper() {} |
| 27 |
|
| 28 |
wxcCodeGeneratorHelper& wxcCodeGeneratorHelper::Get() |
| 29 |
{ |
| 30 |
static wxcCodeGeneratorHelper mgr; |
| 31 |
return mgr; |
| 32 |
} |
| 33 |
|
| 34 |
void wxcCodeGeneratorHelper::Clear() |
| 35 |
{ |
| 36 |
m_bitmapMap.clear(); |
| 37 |
m_wxrcOutput.Clear(); |
| 38 |
m_winIds.clear(); |
| 39 |
m_icons.Clear(); |
| 40 |
} |
| 41 |
|
| 42 |
bool wxcCodeGeneratorHelper::CreateXRC() |
| 43 |
{ |
| 44 |
wxLogNull noLog; |
| 45 |
|
| 46 |
wxString text; |
| 47 |
text << wxT("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); |
| 48 |
text << wxT("<resource xmlns=\"http://www.wxwidgets.org/wxxrc\">"); |
| 49 |
if(wxcProjectMetadata::Get().IsAddHandlers()) { |
| 50 |
text << "<!-- Handler Generation is ON -->\n"; |
| 51 |
} else { |
| 52 |
text << "<!-- Handler Generation is OFF -->\n"; |
| 53 |
} |
| 54 |
MapString_t::const_iterator iter = m_bitmapMap.begin(); |
| 55 |
|
| 56 |
// Supported hi-res images extension |
| 57 |
wxArrayString exts; |
| 58 |
exts.Add("@2x"); |
| 59 |
exts.Add("@1.25x"); |
| 60 |
exts.Add("@1.5x"); |
| 61 |
|
| 62 |
for(; iter != m_bitmapMap.end(); ++iter) { |
| 63 |
wxFileName fn(iter->second); |
| 64 |
text << wxT("<object class=\"wxBitmap\" name=\"") << iter->first << wxT("\">") << fn.GetFullPath() |
| 65 |
<< wxT("</object>\n"); |
| 66 |
// Support for hi-res images |
| 67 |
// The logic: |
| 68 |
// If we find files with the following perfixes: @2x, @1.5x, @1.25x |
| 69 |
// add them to the resource files as well |
| 70 |
for(size_t i = 0; i < exts.size(); ++i) { |
| 71 |
wxFileName hiResImage = fn; |
| 72 |
hiResImage.MakeAbsolute(wxcProjectMetadata::Get().GetProjectPath()); |
| 73 |
hiResImage.SetName(hiResImage.GetName() + exts.Item(i)); |
| 74 |
if(hiResImage.FileExists()) { |
| 75 |
fn.SetName(hiResImage.GetName()); |
| 76 |
text << "<object class=\"wxBitmap\" name=\"" << iter->first << exts.Item(i) << "\">" << fn.GetFullPath() |
| 77 |
<< "</object>\n"; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
text << wxT("</resource>"); |
| 82 |
|
| 83 |
wxStringInputStream str(text); |
| 84 |
wxXmlDocument doc(str); |
| 85 |
|
| 86 |
wxFileName projectFileName(wxcProjectMetadata::Get().GetProjectFile()); |
| 87 |
m_cppFile = wxFileName(wxcProjectMetadata::Get().GetProjectPath(), wxcProjectMetadata::Get().GetOutputFileName()); |
| 88 |
m_cppFile.SetFullName(wxcProjectMetadata::Get().GetBitmapsFile()); |
| 89 |
m_cppFile.SetExt("cpp"); |
| 90 |
|
| 91 |
m_xrcFile = m_cppFile; |
| 92 |
m_xrcFile.SetExt(wxT("xrc")); |
| 93 |
|
| 94 |
wxString outputString; |
| 95 |
wxStringOutputStream outStream(&outputString); |
| 96 |
|
| 97 |
if(!doc.Save(outStream)) { return false; } |
| 98 |
|
| 99 |
// Check to see if we already got |
| 100 |
m_destCPP = |
| 101 |
wxFileName(wxcProjectMetadata::Get().GetGeneratedFilesDir(), wxcProjectMetadata::Get().GetBitmapsFile()); |
| 102 |
m_destCPP.SetExt("cpp"); |
| 103 |
wxCrafter::MakeAbsToProject(m_destCPP); |
| 104 |
|
| 105 |
bool isBitmapModifiedOutside = IsGenerateNeeded(); |
| 106 |
|
| 107 |
if(isBitmapModifiedOutside) { |
| 108 |
// Request for update |
| 109 |
wxCommandEvent evtUpdateDesigner(wxEVT_REFRESH_DESIGNER); |
| 110 |
EventNotifier::Get()->AddPendingEvent(evtUpdateDesigner); |
| 111 |
} |
| 112 |
|
| 113 |
#if !defined(__WXMAC__) |
| 114 |
if(wxCrafter::IsTheSame(outputString, m_xrcFile) && m_destCPP.FileExists() && !isBitmapModifiedOutside) { |
| 115 |
// the XRC used to generate the file is the same as the new one |
| 116 |
// and we got a CPP file - skip the code generation |
| 117 |
wxCommandEvent eventEnd(wxEVT_BITMAP_CODE_GENERATION_DONE); |
| 118 |
eventEnd.SetString(m_destCPP.GetFullPath()); |
| 119 |
EventNotifier::Get()->AddPendingEvent(eventEnd); |
| 120 |
return true; |
| 121 |
} |
| 122 |
#endif |
| 123 |
if(!doc.Save(m_xrcFile.GetFullPath())) { return false; } |
| 124 |
|
| 125 |
{ |
| 126 |
wxFrame* topFrame = EventNotifier::Get()->TopFrame(); |
| 127 |
|
| 128 |
topFrame->SetStatusText("Generating bitmap code..."); |
| 129 |
wxBusyCursor bc; |
| 130 |
wxString cppFile = m_destCPP.GetFullPath(); |
| 131 |
wxLogNull nl; |
| 132 |
wxcXmlResourceCmp cmp; |
| 133 |
cmp.Run(m_xrcFile.GetFullPath(), // Input XRC file |
| 134 |
cppFile, // Output file (our CPP file) |
| 135 |
wxcProjectMetadata::Get().GetBitmapFunction()); // The function name to generate |
| 136 |
|
| 137 |
wxCrafter::NotifyFileSaved(cppFile); |
| 138 |
wxCommandEvent eventEnd(wxEVT_BITMAP_CODE_GENERATION_DONE); |
| 139 |
eventEnd.SetString(cppFile); |
| 140 |
EventNotifier::Get()->AddPendingEvent(eventEnd); |
| 141 |
topFrame->SetStatusText("Ready"); |
| 142 |
} |
| 143 |
|
| 144 |
// m_bmpGenThread.AddMessage( req ); |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
wxString wxcCodeGeneratorHelper::GenerateInitCode(TopLevelWinWrapper* tw) const |
| 149 |
{ |
| 150 |
wxString code; |
| 151 |
code << " if ( !bBitmapLoaded ) {\n" |
| 152 |
<< " // We need to initialise the default bitmap handler\n"; |
| 153 |
if(wxcProjectMetadata::Get().IsAddHandlers()) { |
| 154 |
code << " wxXmlResource::Get()->AddHandler(new wxBitmapXmlHandler);\n"; |
| 155 |
} |
| 156 |
code << " " << wxcProjectMetadata::Get().GetBitmapFunction() << "();\n" |
| 157 |
<< " bBitmapLoaded = true;\n" |
| 158 |
<< " }\n"; |
| 159 |
|
| 160 |
if(tw->HasIcon()) { |
| 161 |
wxString appIconCode = GenerateTopLevelWindowIconCode(); |
| 162 |
if(!appIconCode.IsEmpty()) { |
| 163 |
// add the icon code here |
| 164 |
code << appIconCode << "\n"; |
| 165 |
} |
| 166 |
} |
| 167 |
return code; |
| 168 |
} |
| 169 |
|
| 170 |
wxString wxcCodeGeneratorHelper::GenerateExternCode() const |
| 171 |
{ |
| 172 |
wxString code; |
| 173 |
code << wxT("extern void ") << wxcProjectMetadata::Get().GetBitmapFunction() << wxT("();\n"); |
| 174 |
return code; |
| 175 |
} |
| 176 |
|
| 177 |
wxString wxcCodeGeneratorHelper::BitmapCode(const wxString& bmp, const wxString& bmpname) const |
| 178 |
{ |
| 179 |
wxString tmpbmp = bmp; |
| 180 |
tmpbmp.Trim().Trim(false); |
| 181 |
|
| 182 |
if(tmpbmp.IsEmpty()) { return wxT("wxNullBitmap"); } |
| 183 |
|
| 184 |
wxString artId, clientId, sizeHint; |
| 185 |
wxString code; |
| 186 |
if(wxCrafter::IsArtProviderBitmap(bmp, artId, clientId, sizeHint)) { |
| 187 |
code << "wxArtProvider::GetBitmap(" << artId << ", " << clientId << ", " << wxCrafter::MakeWxSizeStr(sizeHint) |
| 188 |
<< ")"; |
| 189 |
|
| 190 |
} else { |
| 191 |
wxFileName fn(tmpbmp); |
| 192 |
|
| 193 |
wxString name; |
| 194 |
bmpname.IsEmpty() ? name = fn.GetName() : name = bmpname; |
| 195 |
code << wxT("wxXmlResource::Get()->LoadBitmap(") << wxCrafter::WXT(name) << wxT(")"); |
| 196 |
} |
| 197 |
return code; |
| 198 |
} |
| 199 |
|
| 200 |
wxString wxcCodeGeneratorHelper::AddBitmap(const wxString& bitmapFile, const wxString& name) |
| 201 |
{ |
| 202 |
wxString bmppath = bitmapFile; |
| 203 |
bmppath.Trim().Trim(false); |
| 204 |
if(bmppath.IsEmpty()) return ""; |
| 205 |
|
| 206 |
wxString artId, clientId, sizeHint; |
| 207 |
if(wxCrafter::IsArtProviderBitmap(bmppath, artId, clientId, sizeHint)) { return ""; } |
| 208 |
|
| 209 |
wxFileName fn(bmppath); |
| 210 |
wxString bmpname; |
| 211 |
|
| 212 |
// set the name + remove old entry |
| 213 |
name.IsEmpty() ? bmpname = fn.GetName() : bmpname = name; |
| 214 |
if(m_bitmapMap.count(bmpname)) { m_bitmapMap.erase(bmpname); } |
| 215 |
|
| 216 |
m_bitmapMap.insert(std::make_pair(bmpname, bmppath)); |
| 217 |
return bmpname; |
| 218 |
} |
| 219 |
|
| 220 |
bool wxcCodeGeneratorHelper::IsGenerateNeeded() const |
| 221 |
{ |
| 222 |
if(!m_xrcFile.FileExists()) { return true; } |
| 223 |
|
| 224 |
wxString basepath = wxcProjectMetadata::Get().GetProjectPath(); |
| 225 |
time_t xrcModTime = m_xrcFile.GetModificationTime().GetTicks(); |
| 226 |
|
| 227 |
MapString_t::const_iterator iter = m_bitmapMap.begin(); |
| 228 |
for(; iter != m_bitmapMap.end(); ++iter) { |
| 229 |
wxFileName bmpFile(iter->second); |
| 230 |
if(bmpFile.MakeAbsolute(basepath)) { |
| 231 |
if(bmpFile.FileExists()) { |
| 232 |
time_t bmpMod = bmpFile.GetModificationTime().GetTicks(); |
| 233 |
if(bmpMod > xrcModTime) { |
| 234 |
// the bmp file is newer than the xrc file - regenerate |
| 235 |
return true; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
void wxcCodeGeneratorHelper::AddWindowId(const wxString& winid) { m_winIds.insert(winid); } |
| 244 |
|
| 245 |
void wxcCodeGeneratorHelper::ClearWindowIds() { m_winIds.clear(); } |
| 246 |
|
| 247 |
wxString wxcCodeGeneratorHelper::GenerateWinIdEnum() const |
| 248 |
{ |
| 249 |
if(m_winIds.empty() || !wxcProjectMetadata::Get().IsUseEnum()) { return ""; } |
| 250 |
|
| 251 |
int firstId = wxcProjectMetadata::Get().GetFirstWindowId(); |
| 252 |
wxString enumCode; |
| 253 |
enumCode << "public:\n" |
| 254 |
<< " enum {\n"; |
| 255 |
wxStringSet_t::const_iterator iter = m_winIds.begin(); |
| 256 |
for(; iter != m_winIds.end(); ++iter) { |
| 257 |
enumCode << " " << *iter << " = " << ++firstId << ",\n"; |
| 258 |
} |
| 259 |
enumCode << " };\n"; |
| 260 |
return enumCode; |
| 261 |
} |
| 262 |
|
| 263 |
void wxcCodeGeneratorHelper::AddIcon(const wxString& bitmapFile) |
| 264 |
{ |
| 265 |
if(bitmapFile.IsEmpty()) { return; } |
| 266 |
wxString bmpadded = AddBitmap(bitmapFile); |
| 267 |
if(bmpadded.IsEmpty()) return; |
| 268 |
m_icons.Add(bmpadded); |
| 269 |
} |
| 270 |
|
| 271 |
wxString wxcCodeGeneratorHelper::GenerateTopLevelWindowIconCode() const |
| 272 |
{ |
| 273 |
wxString code; |
| 274 |
|
| 275 |
if(!m_icons.IsEmpty()) { |
| 276 |
code << " // Set icon(s) to the application/dialog\n"; |
| 277 |
code << " wxIconBundle app_icons;\n"; |
| 278 |
for(size_t i = 0; i < m_icons.GetCount(); ++i) { |
| 279 |
code << " {\n" |
| 280 |
<< " wxBitmap iconBmp = " << BitmapCode(m_icons.Item(i)) << ";\n" |
| 281 |
<< " wxIcon icn;\n" |
| 282 |
<< " icn.CopyFromBitmap(iconBmp);\n" |
| 283 |
<< " app_icons.AddIcon( icn );\n" |
| 284 |
<< " }\n"; |
| 285 |
} |
| 286 |
code << " SetIcons( app_icons );\n"; |
| 287 |
} |
| 288 |
return code; |
| 289 |
} |
| 290 |
|
| 291 |
void wxcCodeGeneratorHelper::ClearIcons() |
| 292 |
{ |
| 293 |
// for(size_t i=0; i<m_icons.GetCount(); ++i) { |
| 294 |
// if ( m_bitmapMap.count(m_icons.Item(i)) ) { |
| 295 |
// m_bitmapMap.erase(m_icons.Item(i)); |
| 296 |
// } |
| 297 |
//} |
| 298 |
m_icons.Clear(); |
| 299 |
} |
| 300 |
|
| 301 |
void wxcCodeGeneratorHelper::UnInitialize() |
| 302 |
{ |
| 303 |
// m_bmpGenThread.Stop(); |
| 304 |
} |