Invalidate notebook best size when pages are added or removed
[wxWidgets.git] / src / common / bookctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/bookctrl.cpp
3 // Purpose: wxBookCtrl implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.08.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "bookctrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_BOOKCTRL
32
33 #include "wx/imaglist.h"
34
35 #include "wx/bookctrl.h"
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // ----------------------------------------------------------------------------
42 // constructors and destructors
43 // ----------------------------------------------------------------------------
44
45 void wxBookCtrl::Init()
46 {
47 m_imageList = NULL;
48 m_ownsImageList = false;
49 }
50
51 bool
52 wxBookCtrl::Create(wxWindow *parent,
53 wxWindowID id,
54 const wxPoint& pos,
55 const wxSize& size,
56 long style,
57 const wxString& name)
58 {
59 return wxControl::Create
60 (
61 parent,
62 id,
63 pos,
64 size,
65 style,
66 wxDefaultValidator,
67 name
68 );
69 }
70
71 wxBookCtrl::~wxBookCtrl()
72 {
73 if ( m_ownsImageList )
74 {
75 // may be NULL, ok
76 delete m_imageList;
77 }
78 }
79
80 // ----------------------------------------------------------------------------
81 // image list
82 // ----------------------------------------------------------------------------
83
84 void wxBookCtrl::SetImageList(wxImageList *imageList)
85 {
86 if ( m_ownsImageList )
87 {
88 // may be NULL, ok
89 delete m_imageList;
90
91 m_ownsImageList = false;
92 }
93
94 m_imageList = imageList;
95 }
96
97 void wxBookCtrl::AssignImageList(wxImageList* imageList)
98 {
99 SetImageList(imageList);
100
101 m_ownsImageList = true;
102 }
103
104 // ----------------------------------------------------------------------------
105 // geometry
106 // ----------------------------------------------------------------------------
107
108 void wxBookCtrl::SetPageSize(const wxSize& size)
109 {
110 SetClientSize(CalcSizeFromPage(size));
111 }
112
113 wxSize wxBookCtrl::DoGetBestSize() const
114 {
115 wxSize bestSize;
116
117 // iterate over all pages, get the largest width and height
118 const size_t nCount = m_pages.size();
119 for ( size_t nPage = 0; nPage < nCount; nPage++ )
120 {
121 wxWindow *pPage = m_pages[nPage];
122 wxSize childBestSize(pPage->GetBestSize());
123
124 if ( childBestSize.x > bestSize.x )
125 bestSize.x = childBestSize.x;
126
127 if ( childBestSize.y > bestSize.y )
128 bestSize.y = childBestSize.y;
129 }
130
131 // convert display area to window area, adding the size neccessary for the
132 // tabs
133 wxSize best = CalcSizeFromPage(bestSize);
134 CacheBestSize(best);
135 return best;
136 }
137
138 // ----------------------------------------------------------------------------
139 // pages management
140 // ----------------------------------------------------------------------------
141
142 bool
143 wxBookCtrl::InsertPage(size_t nPage,
144 wxWindow *page,
145 const wxString& WXUNUSED(text),
146 bool WXUNUSED(bSelect),
147 int WXUNUSED(imageId))
148 {
149 wxCHECK_MSG( page, false, _T("NULL page in wxBookCtrl::InsertPage()") );
150 wxCHECK_MSG( nPage <= m_pages.size(), false,
151 _T("invalid page index in wxBookCtrl::InsertPage()") );
152
153 m_pages.Insert(page, nPage);
154 InvalidateBestSize();
155
156 return true;
157 }
158
159 bool wxBookCtrl::DeletePage(size_t nPage)
160 {
161 wxWindow *page = DoRemovePage(nPage);
162 if ( !page )
163 return false;
164
165 delete page;
166
167 return true;
168 }
169
170 wxWindow *wxBookCtrl::DoRemovePage(size_t nPage)
171 {
172 wxCHECK_MSG( nPage < m_pages.size(), NULL,
173 _T("invalid page index in wxBookCtrl::DoRemovePage()") );
174
175 wxWindow *pageRemoved = m_pages[nPage];
176 m_pages.RemoveAt(nPage);
177 InvalidateBestSize();
178
179 return pageRemoved;
180 }
181
182 int wxBookCtrl::GetNextPage(bool forward) const
183 {
184 int nPage;
185
186 int nMax = GetPageCount();
187 if ( nMax-- ) // decrement it to get the last valid index
188 {
189 int nSel = GetSelection();
190
191 // change selection wrapping if it becomes invalid
192 nPage = forward ? nSel == nMax ? 0
193 : nSel + 1
194 : nSel == 0 ? nMax
195 : nSel - 1;
196 }
197 else // notebook is empty, no next page
198 {
199 nPage = -1;
200 }
201
202 return nPage;
203 }
204
205 #endif // wxUSE_BOOKCTRL
206