| 1 |
#include "notebook_base_wrapper.h" |
| 2 |
|
| 3 |
#include "allocator_mgr.h" |
| 4 |
#include "notebook_page_wrapper.h" |
| 5 |
#include "wxc_notebook_code_helper.h" |
| 6 |
#include "wxgui_defs.h" |
| 7 |
#include "wxgui_helpers.h" |
| 8 |
|
| 9 |
#include <wx/bookctrl.h> |
| 10 |
#include <wx/imaglist.h> |
| 11 |
|
| 12 |
NotebookBaseWrapper::NotebookBaseWrapper(int id) |
| 13 |
: wxcWidget(id) |
| 14 |
{ |
| 15 |
PREPEND_STYLE_TRUE(wxBK_DEFAULT); |
| 16 |
PREPEND_STYLE_FALSE(wxBK_LEFT); |
| 17 |
PREPEND_STYLE_FALSE(wxBK_RIGHT); |
| 18 |
PREPEND_STYLE_FALSE(wxBK_TOP); |
| 19 |
PREPEND_STYLE_FALSE(wxBK_BOTTOM); |
| 20 |
} |
| 21 |
|
| 22 |
NotebookBaseWrapper::~NotebookBaseWrapper() {} |
| 23 |
|
| 24 |
wxString NotebookBaseWrapper::CppCtorCode() const |
| 25 |
{ |
| 26 |
wxString code; |
| 27 |
code << CPPStandardWxCtor("0"); |
| 28 |
|
| 29 |
// Set the window name |
| 30 |
code << GetName() << "->SetName(" << wxCrafter::WXT(GetName()) << ");\n"; |
| 31 |
|
| 32 |
int xx, yy; |
| 33 |
|
| 34 |
xx = -1; |
| 35 |
yy = -1; |
| 36 |
List_t::const_iterator iter = m_children.begin(); |
| 37 |
for(; iter != m_children.end(); iter++) { |
| 38 |
NotebookPageWrapper* page = dynamic_cast<NotebookPageWrapper*>((*iter)); |
| 39 |
if(page) { |
| 40 |
wxSize sz = page->GetImageSize(); |
| 41 |
xx = wxMax(sz.x, xx); |
| 42 |
yy = wxMax(sz.y, yy); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Add image list |
| 47 |
bool isAuiBook = (GetType() == ID_WXAUINOTEBOOK); |
| 48 |
if((xx != -1 && yy != -1) && !isAuiBook) { |
| 49 |
wxString ilName; |
| 50 |
ilName << GetName() << "_il"; |
| 51 |
code << "wxImageList* " << ilName << " = new wxImageList(" << xx << ", " << yy << ");\n"; |
| 52 |
code << GetName() << "->AssignImageList(" << ilName << ");\n"; |
| 53 |
} |
| 54 |
|
| 55 |
wxString& helperCode = wxcNotebookCodeHelper::Get().Code(); |
| 56 |
wxcWidget* tlw = GetTopLevel(); |
| 57 |
if(tlw && tlw->IsPropertyChecked(PROP_PERSISTENT)) { |
| 58 |
helperCode << "\n"; |
| 59 |
helperCode << wxCrafter::WX29_BLOCK_START(); |
| 60 |
helperCode << "if(!wxPersistenceManager::Get().Find(" << GetName() << ")){\n"; |
| 61 |
helperCode << " wxPersistenceManager::Get().RegisterAndRestore(" << GetName() << ");\n"; |
| 62 |
helperCode << "} else {\n"; |
| 63 |
helperCode << " wxPersistenceManager::Get().Restore(" << GetName() << ");\n"; |
| 64 |
helperCode << "}\n"; |
| 65 |
helperCode << "#endif\n"; |
| 66 |
} |
| 67 |
return code; |
| 68 |
} |
| 69 |
|
| 70 |
void NotebookBaseWrapper::ToXRC(wxString& text, XRC_TYPE type) const |
| 71 |
{ |
| 72 |
text << XRCPrefix() << XRCSize() << XRCStyle() << XRCCommonAttributes(); |
| 73 |
|
| 74 |
ChildrenXRC(text, type); |
| 75 |
text << XRCSuffix(); |
| 76 |
} |
| 77 |
|
| 78 |
NotebookPageWrapper* NotebookBaseWrapper::GetSelection() const |
| 79 |
{ |
| 80 |
List_t::const_iterator iter = m_children.begin(); |
| 81 |
for(; iter != m_children.end(); iter++) { |
| 82 |
NotebookPageWrapper* page = (NotebookPageWrapper*)*iter; |
| 83 |
|
| 84 |
// Check the children of this page and the page itself |
| 85 |
NotebookPageWrapper* sel = DoGetSelection(page); |
| 86 |
if(sel) { |
| 87 |
return sel; |
| 88 |
} |
| 89 |
} |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
void NotebookBaseWrapper::SetSelection(wxcWidget* page) |
| 94 |
{ |
| 95 |
List_t::iterator iter = m_children.begin(); |
| 96 |
for(; iter != m_children.end(); iter++) { |
| 97 |
NotebookPageWrapper* child = (NotebookPageWrapper*)*iter; |
| 98 |
DoSetSelection(child, page); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void NotebookBaseWrapper::DoSetSelection(NotebookPageWrapper* page, wxcWidget* pageToSelect) |
| 103 |
{ |
| 104 |
page->SetSelected(pageToSelect == page); |
| 105 |
wxcWidget::List_t& list = page->GetChildren(); |
| 106 |
wxcWidget::List_t::iterator iter = list.begin(); |
| 107 |
for(; iter != list.end(); iter++) { |
| 108 |
NotebookPageWrapper* child = dynamic_cast<NotebookPageWrapper*>(*iter); |
| 109 |
if(child) { |
| 110 |
// another notebook page |
| 111 |
DoSetSelection(child, pageToSelect); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
NotebookPageWrapper* NotebookBaseWrapper::DoGetSelection(NotebookPageWrapper* page) const |
| 117 |
{ |
| 118 |
if(page->IsSelected()) { |
| 119 |
return page; |
| 120 |
} |
| 121 |
|
| 122 |
wxcWidget::List_t& list = page->GetChildren(); |
| 123 |
wxcWidget::List_t::const_iterator iter = list.begin(); |
| 124 |
for(; iter != list.end(); iter++) { |
| 125 |
NotebookPageWrapper* child = dynamic_cast<NotebookPageWrapper*>(*iter); |
| 126 |
if(child) { |
| 127 |
NotebookPageWrapper* sel = DoGetSelection(child); |
| 128 |
if(sel) { |
| 129 |
return sel; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
int NotebookBaseWrapper::GetPageIndex(const NotebookPageWrapper* page) const |
| 137 |
{ |
| 138 |
int count(0); |
| 139 |
const wxcWidget::List_t& list = GetChildren(); |
| 140 |
wxcWidget::List_t::const_iterator iter = list.begin(); |
| 141 |
for(; iter != list.end(); iter++) { |
| 142 |
if(DoGetPageIndex((const NotebookPageWrapper*)(*iter), page, count)) { |
| 143 |
return count; |
| 144 |
} |
| 145 |
} |
| 146 |
return wxNOT_FOUND; |
| 147 |
} |
| 148 |
|
| 149 |
bool NotebookBaseWrapper::DoGetPageIndex(const NotebookPageWrapper* page, const NotebookPageWrapper* pageToFind, |
| 150 |
int& count) const |
| 151 |
{ |
| 152 |
if(page == pageToFind) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
count++; |
| 157 |
if(page->GetChildren().empty()) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
List_t::const_iterator iter = page->GetChildren().begin(); |
| 162 |
for(; iter != page->GetChildren().end(); iter++) { |
| 163 |
NotebookPageWrapper* child = dynamic_cast<NotebookPageWrapper*>(*iter); |
| 164 |
if(child) { |
| 165 |
if(DoGetPageIndex(child, pageToFind, count)) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
NotebookPageWrapper* NotebookBaseWrapper::GetLastPage() const |
| 174 |
{ |
| 175 |
return dynamic_cast<NotebookPageWrapper*>(*GetChildren().rbegin()); |
| 176 |
} |
| 177 |
|
| 178 |
wxcWidget* NotebookBaseWrapper::GetChildPageAtDepth(size_t targetlevel) |
| 179 |
{ |
| 180 |
wxcWidget* target = NULL; |
| 181 |
|
| 182 |
NotebookPageWrapper* page = GetLastPage(); // Find the last page of the book |
| 183 |
if(page) { |
| 184 |
target = DoGetChildPageAtDepth(page, targetlevel, 0); |
| 185 |
} |
| 186 |
|
| 187 |
return target; |
| 188 |
} |
| 189 |
|
| 190 |
NotebookPageWrapper* NotebookBaseWrapper::DoGetChildPageAtDepth(NotebookPageWrapper* page, size_t targetlevel, |
| 191 |
size_t currentlevel) const |
| 192 |
{ |
| 193 |
if(currentlevel == targetlevel) { |
| 194 |
return page; |
| 195 |
} |
| 196 |
|
| 197 |
// The _last_ child notebookpage is the one to check |
| 198 |
const wxcWidget::List_t& children = page->GetChildren(); |
| 199 |
wxcWidget::List_t::const_reverse_iterator riter = children.rbegin(); |
| 200 |
for(; riter != children.rend(); ++riter) { |
| 201 |
NotebookPageWrapper* child = dynamic_cast<NotebookPageWrapper*>(*riter); |
| 202 |
if(child) { |
| 203 |
return DoGetChildPageAtDepth(child, targetlevel, ++currentlevel); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return NULL; |
| 208 |
} |