| 1 |
#include "notebook_page_wrapper.h" |
| 2 |
#include "DirectoryChanger.h" |
| 3 |
#include "allocator_mgr.h" |
| 4 |
#include "bool_property.h" |
| 5 |
#include "choice_book_wrapper.h" |
| 6 |
#include "file_ficker_property.h" |
| 7 |
#include "notebook_base_wrapper.h" |
| 8 |
#include "string_property.h" |
| 9 |
#include "tree_book_wrapper.h" |
| 10 |
#include "wxc_bitmap_code_generator.h" |
| 11 |
#include "wxc_notebook_code_helper.h" |
| 12 |
#include "wxc_project_metadata.h" |
| 13 |
#include "wxgui_bitmaploader.h" |
| 14 |
#include "wxgui_defs.h" |
| 15 |
#include "wxgui_helpers.h" |
| 16 |
#include "xmlutils.h" |
| 17 |
#include <wx/panel.h> |
| 18 |
|
| 19 |
NotebookPageWrapper::NotebookPageWrapper() |
| 20 |
: wxcWidget(ID_WXPANEL_NOTEBOOK_PAGE) |
| 21 |
, m_selected(false) |
| 22 |
{ |
| 23 |
|
| 24 |
SetPropertyString(_("Common Settings"), "wxNotebookPage"); |
| 25 |
AddProperty(new StringProperty(PROP_LABEL, _("Page"), _("The tab's label"))); |
| 26 |
AddProperty(new BitmapPickerProperty(PROP_BITMAP_PATH, wxT(""), _("Tab Image"))); |
| 27 |
AddProperty(new BoolProperty(PROP_SELECTED, false, _("Select this page"))); |
| 28 |
AddProperty( |
| 29 |
new BoolProperty(PROP_NULL_BOOK_PAGE, false, |
| 30 |
_("Add a NULL page. This is only makes sense when the notebook is of type wxTreebook"))); |
| 31 |
|
| 32 |
// Enable the 'wxTAB_TRAVERSAL' style |
| 33 |
EnableStyle(wxT("wxTAB_TRAVERSAL"), true); |
| 34 |
m_namePattern = wxT("m_panel"); |
| 35 |
SetName(GenerateName()); |
| 36 |
} |
| 37 |
|
| 38 |
NotebookPageWrapper::~NotebookPageWrapper() {} |
| 39 |
|
| 40 |
wxcWidget* NotebookPageWrapper::Clone() const { return new NotebookPageWrapper(); } |
| 41 |
|
| 42 |
wxString NotebookPageWrapper::CppCtorCode() const |
| 43 |
{ |
| 44 |
if(IsTreebookPage()) { return DoTreebookCppCtorCode(); } |
| 45 |
|
| 46 |
wxString code; |
| 47 |
code << CPPStandardWxCtor(wxT("wxTAB_TRAVERSAL")); |
| 48 |
|
| 49 |
// If our parent is not a notebook, return the code |
| 50 |
NotebookBaseWrapper* book = GetNotebook(); |
| 51 |
if(!book) { return code; } |
| 52 |
|
| 53 |
bool isAuiPage = (book->GetType() == ID_WXAUINOTEBOOK); |
| 54 |
bool isChoicePage = (book->GetType() == ID_WXCHOICEBOOK); |
| 55 |
|
| 56 |
if(!isChoicePage) { |
| 57 |
// wxChoicebook does not support images (yet) |
| 58 |
wxcCodeGeneratorHelper::Get().AddBitmap(PropertyFile(PROP_BITMAP_PATH)); |
| 59 |
} |
| 60 |
|
| 61 |
bool bHasBitmap = (GetImageSize() != wxSize(-1, -1)); |
| 62 |
wxString imgListName, imgIndex; |
| 63 |
if(!isChoicePage && !isAuiPage && bHasBitmap) { |
| 64 |
imgListName << GetParent()->GetName() << wxT("_il"); |
| 65 |
imgIndex << GetName() << wxT("ImgIndex"); |
| 66 |
code << wxT("int ") << imgIndex << wxT(";\n"); |
| 67 |
code << imgIndex << wxT(" = ") << imgListName << wxT("->Add(") |
| 68 |
<< wxcCodeGeneratorHelper::Get().BitmapCode(PropertyFile(PROP_BITMAP_PATH)) << wxT(");\n"); |
| 69 |
} |
| 70 |
|
| 71 |
if(isAuiPage && bHasBitmap) { |
| 72 |
// Aui passes the bitmap in the AddPage(...) call |
| 73 |
// Add this page to the notebook |
| 74 |
code << GetParent()->GetName() << wxT("->AddPage(") << GetName() << wxT(", ") << Label() << wxT(", ") |
| 75 |
<< PropertyBool(PROP_SELECTED) << wxT(", ") |
| 76 |
<< wxcCodeGeneratorHelper::Get().BitmapCode(PropertyFile(PROP_BITMAP_PATH)) << wxT(");\n"); |
| 77 |
|
| 78 |
} else if(bHasBitmap && !imgIndex.IsEmpty()) { |
| 79 |
// Add this page to the notebook |
| 80 |
code << GetParent()->GetName() << wxT("->AddPage(") << GetName() << wxT(", ") << Label() << wxT(", ") |
| 81 |
<< PropertyBool(PROP_SELECTED) << wxT(", ") << imgIndex << wxT(");\n"); |
| 82 |
} else { |
| 83 |
|
| 84 |
// Add this page to the notebook |
| 85 |
code << GetParent()->GetName() << wxT("->AddPage(") << GetName() << wxT(", ") << Label() << wxT(", ") |
| 86 |
<< PropertyBool(PROP_SELECTED) << wxT(");\n"); |
| 87 |
} |
| 88 |
return code; |
| 89 |
} |
| 90 |
|
| 91 |
void NotebookPageWrapper::GetIncludeFile(wxArrayString& headers) const |
| 92 |
{ |
| 93 |
headers.Add(wxT("#include <wx/panel.h>")); |
| 94 |
headers.Add(wxT("#include <wx/imaglist.h>")); |
| 95 |
} |
| 96 |
|
| 97 |
wxString NotebookPageWrapper::GetWxClassName() const { return wxT("wxPanel"); } |
| 98 |
|
| 99 |
void NotebookPageWrapper::ToXRC(wxString& text, XRC_TYPE type) const |
| 100 |
{ |
| 101 |
NotebookBaseWrapper* book = dynamic_cast<NotebookBaseWrapper*>(GetParent()); |
| 102 |
if(!book) { return; } |
| 103 |
|
| 104 |
if(IsTreebookPage()) { |
| 105 |
int depth(0); |
| 106 |
DoTreebookXRC(text, type, depth); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
text << wxT("<object class=\"") << book->GetXRCPageClass() << wxT("\">") << XRCLabel(); |
| 111 |
|
| 112 |
if(!IsChoicebookPage()) { |
| 113 |
wxString bmp = PropertyFile(PROP_BITMAP_PATH); |
| 114 |
if(bmp.IsEmpty() == false) { text << XRCBitmap(); } |
| 115 |
} |
| 116 |
|
| 117 |
if(type == XRC_LIVE) { |
| 118 |
text << wxT("<selected>") << wxCrafter::XMLEncode(PropertyString(PROP_SELECTED)) << wxT("</selected>"); |
| 119 |
|
| 120 |
} else { |
| 121 |
// For the preview we use the fake selection |
| 122 |
text << wxT("<selected>") << (IsSelected() ? wxT("1") : wxT("0")) << wxT("</selected>"); |
| 123 |
} |
| 124 |
|
| 125 |
text << XRCPrefix() << XRCSize() << XRCStyle() << XRCCommonAttributes(); |
| 126 |
|
| 127 |
ChildrenXRC(text, type); |
| 128 |
|
| 129 |
text << XRCSuffix() << wxT("</object>"); |
| 130 |
} |
| 131 |
|
| 132 |
void NotebookPageWrapper::LoadPropertiesFromXRC(const wxXmlNode* node) |
| 133 |
{ |
| 134 |
// First call the base-class for the standard things |
| 135 |
wxcWidget::LoadPropertiesFromXRC(node); |
| 136 |
|
| 137 |
// There are 3 possible properties. Not all will apply to all bookpage types, but the XRC should only supply |
| 138 |
// relevant ones... |
| 139 |
wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("bitmap")); |
| 140 |
if(propertynode) { |
| 141 |
wxString value = propertynode->GetNodeContent(); |
| 142 |
PropertyBase* prop = GetProperty(PROP_BITMAP_PATH); |
| 143 |
if(prop) { prop->SetValue(value); } |
| 144 |
} |
| 145 |
|
| 146 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("selection")); |
| 147 |
if(propertynode) { |
| 148 |
wxString value = propertynode->GetNodeContent(); |
| 149 |
PropertyBase* prop = GetProperty(PROP_SELECTION); |
| 150 |
if(prop) { prop->SetValue(value); } |
| 151 |
} |
| 152 |
/* |
| 153 |
propertynode = XmlUtils::FindFirstByTagName(node, wxT("depth")); |
| 154 |
if (propertynode) { |
| 155 |
wxString value = propertynode->GetNodeContent(); |
| 156 |
PropertyBase* prop = GetProperty(.....); // No corresponding property-id (yet)? |
| 157 |
if (prop) { |
| 158 |
prop->SetValue(value); |
| 159 |
} |
| 160 |
}*/ |
| 161 |
} |
| 162 |
|
| 163 |
void NotebookPageWrapper::LoadPropertiesFromwxSmith(const wxXmlNode* node) |
| 164 |
{ |
| 165 |
// First call the base-class for the standard things |
| 166 |
wxcWidget::LoadPropertiesFromwxSmith(node); |
| 167 |
|
| 168 |
// There are 3 possible properties. Not all will apply to all bookpage types, but the XRC should only supply |
| 169 |
// relevant ones... |
| 170 |
wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("selection")); |
| 171 |
if(propertynode) { |
| 172 |
wxString value = propertynode->GetNodeContent(); |
| 173 |
PropertyBase* prop = GetProperty(PROP_SELECTION); |
| 174 |
if(prop) { prop->SetValue(value); } |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
wxSize NotebookPageWrapper::GetImageSize() const |
| 179 |
{ |
| 180 |
DirectoryChanger dc(wxcProjectMetadata::Get().GetProjectPath()); |
| 181 |
wxString bmp = PropertyFile(PROP_BITMAP_PATH); |
| 182 |
wxFileName fn(bmp); |
| 183 |
if(fn.FileExists()) { |
| 184 |
wxBitmap bmpFile(fn.GetFullPath(), wxBITMAP_TYPE_ANY); |
| 185 |
if(bmpFile.IsOk()) { |
| 186 |
wxSize bmpSize; |
| 187 |
bmpSize.x = bmpFile.GetWidth(); |
| 188 |
bmpSize.y = bmpFile.GetHeight(); |
| 189 |
return bmpSize; |
| 190 |
} |
| 191 |
} |
| 192 |
return wxSize(-1, -1); |
| 193 |
} |
| 194 |
|
| 195 |
void NotebookPageWrapper::SetParent(wxcWidget* parent) |
| 196 |
{ |
| 197 |
wxcWidget::SetParent(parent); |
| 198 |
if(IsChoicebookPage()) { |
| 199 |
DelProperty(PROP_BITMAP_PATH); |
| 200 |
|
| 201 |
} else if(IsTreebookPage()) { |
| 202 |
AddProperty(new BoolProperty(PROP_EXPANDED, true, _("Expand this node"))); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
bool NotebookPageWrapper::IsChoicebookPage() const { return (dynamic_cast<ChoiceBookWrapper*>(GetParent()) != NULL); } |
| 207 |
|
| 208 |
bool NotebookPageWrapper::IsTreebookPage() const |
| 209 |
{ |
| 210 |
bool isTreebookPage = false; |
| 211 |
// Find the first parent of type Notebook |
| 212 |
NotebookBaseWrapper* book = GetNotebook(); |
| 213 |
if(book) { isTreebookPage = (dynamic_cast<TreeBookWrapper*>(book) != NULL); } |
| 214 |
|
| 215 |
if(!isTreebookPage) { |
| 216 |
NotebookPageWrapper* parent = dynamic_cast<NotebookPageWrapper*>(GetParent()); |
| 217 |
isTreebookPage = (parent != NULL); |
| 218 |
} |
| 219 |
return isTreebookPage; |
| 220 |
} |
| 221 |
|
| 222 |
void NotebookPageWrapper::DoTreebookXRC(wxString& text, XRC_TYPE type, int depth) const |
| 223 |
{ |
| 224 |
text << wxT("<object class=\"treebookpage\">") << XRCLabel(); |
| 225 |
|
| 226 |
if(!IsChoicebookPage()) { |
| 227 |
wxString bmp = PropertyFile(PROP_BITMAP_PATH); |
| 228 |
if(bmp.IsEmpty() == false) { text << XRCBitmap(); } |
| 229 |
} |
| 230 |
|
| 231 |
if(type == XRC_LIVE) { |
| 232 |
text << wxT("<selected>") << wxCrafter::XMLEncode(PropertyString(PROP_SELECTED)) << wxT("</selected>"); |
| 233 |
|
| 234 |
} else { |
| 235 |
// For the preview we use the fake selection |
| 236 |
text << wxT("<selected>") << (IsSelected() ? wxT("1") : wxT("0")) << wxT("</selected>"); |
| 237 |
} |
| 238 |
|
| 239 |
text << wxT("<depth>") << depth << wxT("</depth>"); |
| 240 |
text << wxT("<expanded>") << 1 << wxT("</expanded>"); |
| 241 |
text << XRCPrefix() << XRCSize() << XRCStyle() << XRCCommonAttributes(); |
| 242 |
|
| 243 |
// Loop over the children |
| 244 |
wxString childPageXRC; |
| 245 |
wxcWidget::List_t::const_iterator iter = m_children.begin(); |
| 246 |
for(; iter != m_children.end(); iter++) { |
| 247 |
wxString xrc; |
| 248 |
// If it is another page, recurse |
| 249 |
NotebookPageWrapper* page = dynamic_cast<NotebookPageWrapper*>(*iter); |
| 250 |
if(page) { |
| 251 |
page->DoTreebookXRC(xrc, type, depth + 1); |
| 252 |
childPageXRC << xrc; |
| 253 |
xrc.Clear(); |
| 254 |
|
| 255 |
} else { |
| 256 |
(*iter)->ToXRC(xrc, type); |
| 257 |
if((*iter)->IsSizerItem()) { xrc = (*iter)->WrapInSizerXRC(xrc); } |
| 258 |
} |
| 259 |
text << xrc; |
| 260 |
} |
| 261 |
|
| 262 |
text << XRCSuffix() << wxT("</object>"); |
| 263 |
|
| 264 |
text << childPageXRC; |
| 265 |
} |
| 266 |
|
| 267 |
NotebookBaseWrapper* NotebookPageWrapper::GetNotebook() const |
| 268 |
{ |
| 269 |
// Find the first parent of type Notebook |
| 270 |
wxcWidget* parent = GetParent(); |
| 271 |
while(parent) { |
| 272 |
if(dynamic_cast<NotebookBaseWrapper*>(parent)) { break; } |
| 273 |
parent = parent->GetParent(); |
| 274 |
} |
| 275 |
|
| 276 |
if(!parent) return NULL; |
| 277 |
|
| 278 |
return dynamic_cast<NotebookBaseWrapper*>(parent); |
| 279 |
} |
| 280 |
|
| 281 |
int NotebookPageWrapper::GetPageIndex() const |
| 282 |
{ |
| 283 |
NotebookBaseWrapper* book = GetNotebook(); |
| 284 |
if(!book) { return wxNOT_FOUND; } |
| 285 |
|
| 286 |
return book->GetPageIndex((const NotebookPageWrapper*)this); |
| 287 |
} |
| 288 |
|
| 289 |
wxString NotebookPageWrapper::DoTreebookCppCtorCode() const |
| 290 |
{ |
| 291 |
if(!GetNotebook()) return wxT(""); |
| 292 |
|
| 293 |
wxString code; |
| 294 |
bool isNullPage = (PropertyBool(PROP_NULL_BOOK_PAGE) == "true"); |
| 295 |
|
| 296 |
if(!isNullPage) { code << CPPStandardWxCtor(wxT("wxTAB_TRAVERSAL")); } |
| 297 |
|
| 298 |
bool bHasBitmap = false; |
| 299 |
wxString imgListName, imgIndex; |
| 300 |
|
| 301 |
NotebookPageWrapper* parentPage = dynamic_cast<NotebookPageWrapper*>(GetParent()); |
| 302 |
NotebookBaseWrapper* book = GetNotebook(); |
| 303 |
|
| 304 |
wxcCodeGeneratorHelper::Get().AddBitmap(PropertyFile(PROP_BITMAP_PATH)); |
| 305 |
|
| 306 |
wxSize sz = GetImageSize(); |
| 307 |
if(sz != wxSize(-1, -1)) { |
| 308 |
|
| 309 |
imgListName << book->GetName() << wxT("_il"); |
| 310 |
imgIndex << GetName() << wxT("ImgIndex"); |
| 311 |
code << wxT("int ") << imgIndex << wxT(";\n"); |
| 312 |
code << imgIndex << wxT(" = ") << imgListName << wxT("->Add(") |
| 313 |
<< wxcCodeGeneratorHelper::Get().BitmapCode(PropertyFile(PROP_BITMAP_PATH)) << wxT(");\n"); |
| 314 |
bHasBitmap = true; |
| 315 |
} |
| 316 |
|
| 317 |
if(parentPage) { |
| 318 |
// This is a sub page |
| 319 |
int parentIndex = parentPage->GetPageIndex(); |
| 320 |
if(parentIndex != wxNOT_FOUND) { |
| 321 |
code << book->GetName() << wxT("->InsertSubPage(") << parentIndex << wxT(", ") |
| 322 |
<< (isNullPage ? wxString("NULL") : GetName()) << wxT(", ") << Label() << wxT(", ") |
| 323 |
<< PropertyBool(PROP_SELECTED) << wxT(", ") |
| 324 |
<< (bHasBitmap && !imgIndex.IsEmpty() ? imgIndex : wxT("wxNOT_FOUND")) << wxT(");\n"); |
| 325 |
|
| 326 |
if(PropertyBool(PROP_EXPANDED) == wxT("true")) { |
| 327 |
wxcNotebookCodeHelper::Get().Code() |
| 328 |
<< book->GetName() << wxT("->ExpandNode( ") << GetPageIndex() << wxT(", true );\n"); |
| 329 |
} |
| 330 |
} |
| 331 |
} else { |
| 332 |
// top level page |
| 333 |
code << book->GetName() << wxT("->AddPage(") << (isNullPage ? wxString("NULL") : GetName()) << wxT(", ") |
| 334 |
<< Label() << wxT(", ") << PropertyBool(PROP_SELECTED) << wxT(", ") |
| 335 |
<< (bHasBitmap && !imgIndex.IsEmpty() ? imgIndex : wxT("wxNOT_FOUND")) << wxT(");\n"); |
| 336 |
|
| 337 |
if(PropertyBool(PROP_EXPANDED) == wxT("true")) { |
| 338 |
wxcNotebookCodeHelper::Get().Code() |
| 339 |
<< book->GetName() << wxT("->ExpandNode( ") << GetPageIndex() << wxT(", true );\n"); |
| 340 |
} |
| 341 |
} |
| 342 |
return code; |
| 343 |
} |