]>
Commit | Line | Data |
---|---|---|
1e6d9499 | 1 | /////////////////////////////////////////////////////////////////////////////// |
7fc65a03 WS |
2 | // Name: src/generic/notebook.cpp |
3 | // Purpose: generic implementation of wxNotebook | |
1e6d9499 JS |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
1e6d9499 JS |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
ffecfa5a | 19 | |
1e6d9499 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
df91131c | 24 | #pragma hdrstop |
1e6d9499 JS |
25 | #endif |
26 | ||
bc5a5239 WS |
27 | #if wxUSE_NOTEBOOK |
28 | ||
df91131c WS |
29 | #include "wx/notebook.h" |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/string.h" | |
e4db172a | 33 | #include "wx/log.h" |
ed4b0fdc | 34 | #include "wx/dcclient.h" |
9eddec69 | 35 | #include "wx/settings.h" |
df91131c | 36 | #endif |
4055ed82 | 37 | |
1c36c09c | 38 | #include "wx/imaglist.h" |
00dd3b18 | 39 | #include "wx/generic/tabg.h" |
1e6d9499 JS |
40 | |
41 | // ---------------------------------------------------------------------------- | |
42 | // macros | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // check that the page index is valid | |
0ea23391 | 46 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) |
1e6d9499 JS |
47 | |
48 | // ---------------------------------------------------------------------------- | |
49 | // event table | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
2e4df4bf VZ |
52 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) |
53 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
54 | ||
1e6d9499 | 55 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) |
ca65c044 | 56 | EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange) |
1e6d9499 JS |
57 | EVT_SIZE(wxNotebook::OnSize) |
58 | EVT_PAINT(wxNotebook::OnPaint) | |
59 | EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent) | |
60 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) | |
61 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
1e6d9499 JS |
62 | END_EVENT_TABLE() |
63 | ||
64 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) | |
65 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) | |
1e6d9499 JS |
66 | |
67 | // ============================================================================ | |
68 | // implementation | |
69 | // ============================================================================ | |
70 | ||
00dd3b18 MB |
71 | // ============================================================================ |
72 | // Private class | |
73 | // ============================================================================ | |
74 | ||
dd5e8687 MW |
75 | WX_DECLARE_HASH_MAP(int, wxNotebookPage*, wxIntegerHash, wxIntegerEqual, |
76 | wxIntToNotebookPageHashMap); | |
77 | ||
78 | WX_DECLARE_HASH_MAP(wxNotebookPage*, int, wxPointerHash, wxPointerEqual, | |
79 | wxNotebookPageToIntHashMap); | |
80 | ||
00dd3b18 MB |
81 | // This reuses wxTabView to draw the tabs. |
82 | class WXDLLEXPORT wxNotebookTabView: public wxTabView | |
83 | { | |
84 | DECLARE_DYNAMIC_CLASS(wxNotebookTabView) | |
85 | public: | |
bc5a5239 | 86 | wxNotebookTabView(wxNotebook* notebook, long style = wxTAB_STYLE_DRAW_BOX | wxTAB_STYLE_COLOUR_INTERIOR); |
d3c7fc99 | 87 | virtual ~wxNotebookTabView(void); |
00dd3b18 | 88 | |
bc5a5239 WS |
89 | // Called when a tab is activated |
90 | virtual void OnTabActivate(int activateId, int deactivateId); | |
91 | // Allows vetoing | |
92 | virtual bool OnTabPreActivate(int activateId, int deactivateId); | |
00dd3b18 | 93 | |
dd5e8687 MW |
94 | // map integer ids used by wxTabView to wxNotebookPage pointers |
95 | int GetId(wxNotebookPage *page); | |
96 | wxNotebookPage *GetPage(int id) { return m_idToPage[id]; } | |
97 | ||
00dd3b18 | 98 | protected: |
dd5e8687 MW |
99 | wxNotebook* m_notebook; |
100 | ||
101 | private: | |
102 | wxIntToNotebookPageHashMap m_idToPage; | |
103 | wxNotebookPageToIntHashMap m_pageToId; | |
104 | int m_nextid; | |
00dd3b18 MB |
105 | }; |
106 | ||
dd5e8687 MW |
107 | static int GetPageId(wxTabView *tabview, wxNotebookPage *page) |
108 | { | |
98b9b5ea | 109 | return wx_static_cast(wxNotebookTabView*, tabview)->GetId(page); |
dd5e8687 MW |
110 | } |
111 | ||
1e6d9499 JS |
112 | // ---------------------------------------------------------------------------- |
113 | // wxNotebook construction | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | // common part of all ctors | |
117 | void wxNotebook::Init() | |
118 | { | |
119 | m_tabView = (wxNotebookTabView*) NULL; | |
1e6d9499 JS |
120 | m_nSelection = -1; |
121 | } | |
122 | ||
123 | // default for dynamic class | |
124 | wxNotebook::wxNotebook() | |
125 | { | |
126 | Init(); | |
127 | } | |
128 | ||
129 | // the same arguments as for wxControl | |
130 | wxNotebook::wxNotebook(wxWindow *parent, | |
131 | wxWindowID id, | |
132 | const wxPoint& pos, | |
133 | const wxSize& size, | |
134 | long style, | |
135 | const wxString& name) | |
136 | { | |
137 | Init(); | |
138 | ||
139 | Create(parent, id, pos, size, style, name); | |
140 | } | |
141 | ||
142 | // Create() function | |
143 | bool wxNotebook::Create(wxWindow *parent, | |
144 | wxWindowID id, | |
145 | const wxPoint& pos, | |
146 | const wxSize& size, | |
147 | long style, | |
148 | const wxString& name) | |
149 | { | |
150 | // base init | |
151 | SetName(name); | |
152 | ||
90f9b8ef JS |
153 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
154 | style |= wxBK_TOP; | |
155 | ||
ca65c044 | 156 | m_windowId = id == wxID_ANY ? NewControlId() : id; |
1e6d9499 | 157 | |
b4f8693c | 158 | if (!wxControl::Create(parent, id, pos, size, style|wxNO_BORDER, wxDefaultValidator, name)) |
ca65c044 | 159 | return false; |
1e6d9499 | 160 | |
a756f210 | 161 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
f60d0f94 | 162 | |
1e6d9499 JS |
163 | SetTabView(new wxNotebookTabView(this)); |
164 | ||
ca65c044 | 165 | return true; |
1e6d9499 JS |
166 | } |
167 | ||
168 | // dtor | |
169 | wxNotebook::~wxNotebook() | |
170 | { | |
171 | delete m_tabView; | |
172 | } | |
173 | ||
174 | // ---------------------------------------------------------------------------- | |
175 | // wxNotebook accessors | |
176 | // ---------------------------------------------------------------------------- | |
1e6d9499 JS |
177 | int wxNotebook::GetRowCount() const |
178 | { | |
179 | // TODO | |
180 | return 0; | |
181 | } | |
182 | ||
15aad3b9 | 183 | int wxNotebook::SetSelection(size_t nPage) |
1e6d9499 | 184 | { |
1e6d9499 JS |
185 | wxASSERT( IS_VALID_PAGE(nPage) ); |
186 | ||
187 | wxNotebookPage* pPage = GetPage(nPage); | |
188 | ||
dd5e8687 | 189 | m_tabView->SetTabSelection(GetPageId(m_tabView, pPage)); |
3a5bcc4d | 190 | |
1e6d9499 JS |
191 | // TODO |
192 | return 0; | |
193 | } | |
194 | ||
45f22d48 | 195 | #if 0 |
1e6d9499 JS |
196 | void wxNotebook::AdvanceSelection(bool bForward) |
197 | { | |
198 | int nSel = GetSelection(); | |
199 | int nMax = GetPageCount() - 1; | |
200 | if ( bForward ) | |
201 | SetSelection(nSel == nMax ? 0 : nSel + 1); | |
202 | else | |
203 | SetSelection(nSel == 0 ? nMax : nSel - 1); | |
204 | } | |
45f22d48 | 205 | #endif |
1e6d9499 | 206 | |
15aad3b9 | 207 | bool wxNotebook::SetPageText(size_t nPage, const wxString& strText) |
1e6d9499 JS |
208 | { |
209 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
3a5bcc4d | 210 | |
1e6d9499 JS |
211 | wxNotebookPage* page = GetPage(nPage); |
212 | if (page) | |
213 | { | |
dd5e8687 | 214 | m_tabView->SetTabText(GetPageId(m_tabView, page), strText); |
1e6d9499 | 215 | Refresh(); |
ca65c044 | 216 | return true; |
1e6d9499 | 217 | } |
3a5bcc4d | 218 | |
ca65c044 | 219 | return false; |
1e6d9499 JS |
220 | } |
221 | ||
15aad3b9 | 222 | wxString wxNotebook::GetPageText(size_t nPage) const |
1e6d9499 JS |
223 | { |
224 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
225 | ||
226 | wxNotebookPage* page = ((wxNotebook*)this)->GetPage(nPage); | |
227 | if (page) | |
dd5e8687 | 228 | return m_tabView->GetTabText(GetPageId(m_tabView, page)); |
1e6d9499 JS |
229 | else |
230 | return wxEmptyString; | |
231 | } | |
232 | ||
3f9ee1cd | 233 | int wxNotebook::GetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage)) const |
1e6d9499 JS |
234 | { |
235 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
236 | ||
237 | // TODO | |
238 | return 0; | |
239 | } | |
240 | ||
3f9ee1cd VZ |
241 | bool wxNotebook::SetPageImage(size_t WXUNUSED_UNLESS_DEBUG(nPage), |
242 | int WXUNUSED(nImage)) | |
1e6d9499 JS |
243 | { |
244 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
245 | ||
246 | // TODO | |
ca65c044 | 247 | return false; |
1e6d9499 JS |
248 | } |
249 | ||
9806a47c | 250 | // set the size (the same for all pages) |
7fc65a03 | 251 | void wxNotebook::SetPageSize(const wxSize& WXUNUSED(size)) |
9806a47c JS |
252 | { |
253 | // TODO | |
254 | } | |
255 | ||
256 | // set the padding between tabs (in pixels) | |
7fc65a03 | 257 | void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding)) |
9806a47c JS |
258 | { |
259 | // TODO | |
260 | } | |
261 | ||
262 | // set the size of the tabs for wxNB_FIXEDWIDTH controls | |
7fc65a03 | 263 | void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) |
9806a47c JS |
264 | { |
265 | // TODO | |
266 | } | |
267 | ||
1e6d9499 JS |
268 | // ---------------------------------------------------------------------------- |
269 | // wxNotebook operations | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | // remove one page from the notebook and delete it | |
15aad3b9 | 273 | bool wxNotebook::DeletePage(size_t nPage) |
1e6d9499 | 274 | { |
ca65c044 | 275 | wxCHECK( IS_VALID_PAGE(nPage), false ); |
1e6d9499 JS |
276 | |
277 | if (m_nSelection != -1) | |
278 | { | |
ca65c044 | 279 | m_pages[m_nSelection]->Show(false); |
45f22d48 | 280 | m_pages[m_nSelection]->Lower(); |
1e6d9499 JS |
281 | } |
282 | ||
283 | wxNotebookPage* pPage = GetPage(nPage); | |
3a5bcc4d | 284 | |
dd5e8687 | 285 | m_tabView->RemoveTab(GetPageId(m_tabView, pPage)); |
1e6d9499 | 286 | |
45f22d48 JS |
287 | m_pages.Remove(pPage); |
288 | delete pPage; | |
1e6d9499 | 289 | |
45f22d48 | 290 | if (m_pages.GetCount() == 0) |
1e6d9499 | 291 | { |
bc5a5239 WS |
292 | m_nSelection = -1; |
293 | m_tabView->SetTabSelection(-1, false); | |
1e6d9499 JS |
294 | } |
295 | else if (m_nSelection > -1) | |
296 | { | |
bc5a5239 | 297 | m_nSelection = -1; |
3a5bcc4d | 298 | |
dd5e8687 | 299 | m_tabView->SetTabSelection(GetPageId(m_tabView, GetPage(0)), false); |
3a5bcc4d | 300 | |
bc5a5239 WS |
301 | if (m_nSelection != 0) |
302 | ChangePage(-1, 0); | |
1e6d9499 JS |
303 | } |
304 | ||
ca65c044 | 305 | RefreshLayout(false); |
1e6d9499 | 306 | |
ca65c044 | 307 | return true; |
1e6d9499 JS |
308 | } |
309 | ||
310 | bool wxNotebook::DeletePage(wxNotebookPage* page) | |
311 | { | |
312 | int pagePos = FindPagePosition(page); | |
313 | if (pagePos > -1) | |
314 | return DeletePage(pagePos); | |
315 | else | |
ca65c044 | 316 | return false; |
1e6d9499 JS |
317 | } |
318 | ||
15aad3b9 | 319 | bool wxNotebook::RemovePage(size_t nPage) |
64b090c7 MB |
320 | { |
321 | return DoRemovePage(nPage) != NULL; | |
322 | } | |
323 | ||
324 | // remove one page from the notebook | |
325 | wxWindow* wxNotebook::DoRemovePage(size_t nPage) | |
1e6d9499 | 326 | { |
10f2222c | 327 | wxCHECK( IS_VALID_PAGE(nPage), NULL ); |
1e6d9499 | 328 | |
ca65c044 | 329 | m_pages[nPage]->Show(false); |
45f22d48 | 330 | // m_pages[nPage]->Lower(); |
1e6d9499 JS |
331 | |
332 | wxNotebookPage* pPage = GetPage(nPage); | |
3a5bcc4d | 333 | |
dd5e8687 | 334 | m_tabView->RemoveTab(GetPageId(m_tabView, pPage)); |
1e6d9499 | 335 | |
45f22d48 | 336 | m_pages.Remove(pPage); |
1e6d9499 | 337 | |
45f22d48 | 338 | if (m_pages.GetCount() == 0) |
1e6d9499 JS |
339 | { |
340 | m_nSelection = -1; | |
ca65c044 | 341 | m_tabView->SetTabSelection(-1, true); |
1e6d9499 JS |
342 | } |
343 | else if (m_nSelection > -1) | |
344 | { | |
345 | // Only change the selection if the page we | |
346 | // deleted was the selection. | |
30d2b71d | 347 | if (nPage == (size_t)m_nSelection) |
1e6d9499 JS |
348 | { |
349 | m_nSelection = -1; | |
350 | // Select the first tab. Generates a ChangePage. | |
ca65c044 | 351 | m_tabView->SetTabSelection(0, true); |
1e6d9499 JS |
352 | } |
353 | else | |
354 | { | |
ca65c044 | 355 | // We must adjust which tab we think is selected. |
1e6d9499 JS |
356 | // If greater than the page we deleted, it must be moved down |
357 | // a notch. | |
30d2b71d | 358 | if (size_t(m_nSelection) > nPage) |
1e6d9499 JS |
359 | m_nSelection -- ; |
360 | } | |
361 | } | |
362 | ||
ca65c044 | 363 | RefreshLayout(false); |
1e6d9499 | 364 | |
64b090c7 | 365 | return pPage; |
1e6d9499 JS |
366 | } |
367 | ||
368 | bool wxNotebook::RemovePage(wxNotebookPage* page) | |
369 | { | |
370 | int pagePos = FindPagePosition(page); | |
371 | if (pagePos > -1) | |
372 | return RemovePage(pagePos); | |
373 | else | |
ca65c044 | 374 | return false; |
1e6d9499 JS |
375 | } |
376 | ||
377 | // Find the position of the wxNotebookPage, -1 if not found. | |
378 | int wxNotebook::FindPagePosition(wxNotebookPage* page) const | |
379 | { | |
15aad3b9 VZ |
380 | size_t nPageCount = GetPageCount(); |
381 | size_t nPage; | |
1e6d9499 | 382 | for ( nPage = 0; nPage < nPageCount; nPage++ ) |
45f22d48 | 383 | if (m_pages[nPage] == page) |
1e6d9499 JS |
384 | return nPage; |
385 | return -1; | |
386 | } | |
387 | ||
388 | // remove all pages | |
389 | bool wxNotebook::DeleteAllPages() | |
390 | { | |
ca65c044 | 391 | m_tabView->ClearTabs(true); |
1e6d9499 | 392 | |
15aad3b9 VZ |
393 | size_t nPageCount = GetPageCount(); |
394 | size_t nPage; | |
1e6d9499 | 395 | for ( nPage = 0; nPage < nPageCount; nPage++ ) |
45f22d48 | 396 | delete m_pages[nPage]; |
1e6d9499 | 397 | |
45f22d48 | 398 | m_pages.Clear(); |
1e6d9499 | 399 | |
ca65c044 | 400 | return true; |
1e6d9499 JS |
401 | } |
402 | ||
1e6d9499 | 403 | // same as AddPage() but does it at given position |
15aad3b9 | 404 | bool wxNotebook::InsertPage(size_t nPage, |
1e6d9499 JS |
405 | wxNotebookPage *pPage, |
406 | const wxString& strText, | |
407 | bool bSelect, | |
7fc65a03 | 408 | int WXUNUSED(imageId)) |
1e6d9499 JS |
409 | { |
410 | wxASSERT( pPage != NULL ); | |
ca65c044 | 411 | wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false ); |
1e6d9499 | 412 | |
dd5e8687 | 413 | m_tabView->AddTab(GetPageId(m_tabView, pPage), strText); |
3a5bcc4d | 414 | |
1e6d9499 | 415 | if (!bSelect) |
ca65c044 | 416 | pPage->Show(false); |
1e6d9499 JS |
417 | |
418 | // save the pointer to the page | |
45f22d48 | 419 | m_pages.Insert(pPage, nPage); |
1e6d9499 JS |
420 | |
421 | if (bSelect) | |
422 | { | |
423 | // This will cause ChangePage to be called, via OnSelPage | |
3a5bcc4d | 424 | |
dd5e8687 | 425 | m_tabView->SetTabSelection(GetPageId(m_tabView, pPage), true); |
1e6d9499 JS |
426 | } |
427 | ||
428 | // some page must be selected: either this one or the first one if there is | |
429 | // still no selection | |
430 | if ( m_nSelection == -1 ) | |
431 | ChangePage(-1, 0); | |
432 | ||
ca65c044 | 433 | RefreshLayout(false); |
1e6d9499 | 434 | |
ca65c044 | 435 | return true; |
1e6d9499 JS |
436 | } |
437 | ||
438 | // ---------------------------------------------------------------------------- | |
439 | // wxNotebook callbacks | |
440 | // ---------------------------------------------------------------------------- | |
441 | ||
442 | // @@@ OnSize() is used for setting the font when it's called for the first | |
443 | // time because doing it in ::Create() doesn't work (for unknown reasons) | |
444 | void wxNotebook::OnSize(wxSizeEvent& event) | |
445 | { | |
ca65c044 | 446 | static bool s_bFirstTime = true; |
1e6d9499 JS |
447 | if ( s_bFirstTime ) { |
448 | // TODO: any first-time-size processing. | |
ca65c044 | 449 | s_bFirstTime = false; |
1e6d9499 JS |
450 | } |
451 | ||
452 | RefreshLayout(); | |
453 | ||
454 | // Processing continues to next OnSize | |
455 | event.Skip(); | |
456 | } | |
457 | ||
458 | // This was supposed to cure the non-display of the notebook | |
459 | // until the user resizes the window. | |
460 | // What's going on? | |
5180055b | 461 | void wxNotebook::OnInternalIdle() |
1e6d9499 | 462 | { |
5180055b JS |
463 | wxWindow::OnInternalIdle(); |
464 | ||
ca65c044 WS |
465 | #if 0 |
466 | static bool s_bFirstTime = true; | |
1e6d9499 JS |
467 | if ( s_bFirstTime ) { |
468 | /* | |
469 | wxSize sz(GetSize()); | |
470 | sz.x ++; | |
471 | SetSize(sz); | |
472 | sz.x --; | |
473 | SetSize(sz); | |
474 | */ | |
475 | ||
476 | /* | |
477 | wxSize sz(GetSize()); | |
478 | wxSizeEvent sizeEvent(sz, GetId()); | |
479 | sizeEvent.SetEventObject(this); | |
480 | GetEventHandler()->ProcessEvent(sizeEvent); | |
481 | Refresh(); | |
482 | */ | |
ca65c044 | 483 | s_bFirstTime = false; |
1e6d9499 | 484 | } |
5180055b | 485 | #endif |
1e6d9499 JS |
486 | } |
487 | ||
488 | // Implementation: calculate the layout of the view rect | |
489 | // and resize the children if required | |
490 | bool wxNotebook::RefreshLayout(bool force) | |
491 | { | |
492 | if (m_tabView) | |
493 | { | |
494 | wxRect oldRect = m_tabView->GetViewRect(); | |
495 | ||
496 | int cw, ch; | |
497 | GetClientSize(& cw, & ch); | |
498 | ||
499 | int tabHeight = m_tabView->GetTotalTabHeight(); | |
500 | wxRect rect; | |
501 | rect.x = 4; | |
502 | rect.y = tabHeight + 4; | |
503 | rect.width = cw - 8; | |
504 | rect.height = ch - 4 - rect.y ; | |
2e4df4bf | 505 | |
1e6d9499 JS |
506 | m_tabView->SetViewRect(rect); |
507 | ||
25889d3c | 508 | m_tabView->LayoutTabs(); |
1e6d9499 JS |
509 | |
510 | // Need to do it a 2nd time to get the tab height with | |
511 | // the new view width, since changing the view width changes the | |
512 | // tab layout. | |
513 | tabHeight = m_tabView->GetTotalTabHeight(); | |
514 | rect.x = 4; | |
515 | rect.y = tabHeight + 4; | |
516 | rect.width = cw - 8; | |
517 | rect.height = ch - 4 - rect.y ; | |
2e4df4bf | 518 | |
1e6d9499 JS |
519 | m_tabView->SetViewRect(rect); |
520 | ||
25889d3c | 521 | m_tabView->LayoutTabs(); |
1e6d9499 JS |
522 | |
523 | if (!force && (rect == oldRect)) | |
ca65c044 | 524 | return false; |
1e6d9499 JS |
525 | |
526 | // fit the notebook page to the tab control's display area | |
527 | ||
15aad3b9 VZ |
528 | size_t nCount = m_pages.Count(); |
529 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) { | |
45f22d48 | 530 | wxNotebookPage *pPage = m_pages[nPage]; |
10d1f413 | 531 | wxRect clientRect = GetAvailableClientSize(); |
1e6d9499 JS |
532 | if (pPage->IsShown()) |
533 | { | |
1e6d9499 JS |
534 | pPage->SetSize(clientRect.x, clientRect.y, clientRect.width, clientRect.height); |
535 | if ( pPage->GetAutoLayout() ) | |
536 | pPage->Layout(); | |
537 | } | |
538 | } | |
539 | Refresh(); | |
540 | } | |
ca65c044 | 541 | return true; |
1e6d9499 JS |
542 | } |
543 | ||
544 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
545 | { | |
546 | // is it our tab control? | |
547 | if ( event.GetEventObject() == this ) | |
548 | { | |
549 | if (event.GetSelection() != m_nSelection) | |
550 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
551 | } | |
552 | ||
553 | // we want to give others a chance to process this message as well | |
554 | event.Skip(); | |
555 | } | |
556 | ||
557 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
558 | { | |
559 | // set focus to the currently selected page if any | |
560 | if ( m_nSelection != -1 ) | |
45f22d48 | 561 | m_pages[m_nSelection]->SetFocus(); |
1e6d9499 JS |
562 | |
563 | event.Skip(); | |
564 | } | |
565 | ||
566 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
567 | { | |
568 | if ( event.IsWindowChange() ) { | |
569 | // change pages | |
570 | AdvanceSelection(event.GetDirection()); | |
571 | } | |
572 | else { | |
573 | // pass to the parent | |
574 | if ( GetParent() ) { | |
575 | event.SetCurrentFocus(this); | |
576 | GetParent()->ProcessEvent(event); | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
581 | // ---------------------------------------------------------------------------- | |
582 | // wxNotebook base class virtuals | |
583 | // ---------------------------------------------------------------------------- | |
584 | ||
585 | // override these 2 functions to do nothing: everything is done in OnSize | |
586 | ||
587 | void wxNotebook::SetConstraintSizes(bool /* recurse */) | |
588 | { | |
589 | // don't set the sizes of the pages - their correct size is not yet known | |
ca65c044 | 590 | wxControl::SetConstraintSizes(false); |
1e6d9499 JS |
591 | } |
592 | ||
593 | bool wxNotebook::DoPhase(int /* nPhase */) | |
594 | { | |
ca65c044 | 595 | return true; |
1e6d9499 JS |
596 | } |
597 | ||
af111fc3 | 598 | void wxNotebook::Command(wxCommandEvent& WXUNUSED(event)) |
1e6d9499 | 599 | { |
b0c0a393 | 600 | wxFAIL_MSG(wxT("wxNotebook::Command not implemented")); |
1e6d9499 JS |
601 | } |
602 | ||
603 | // ---------------------------------------------------------------------------- | |
604 | // wxNotebook helper functions | |
605 | // ---------------------------------------------------------------------------- | |
606 | ||
607 | // hide the currently active panel and show the new one | |
608 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
609 | { | |
610 | // cout << "ChangePage: " << nOldSel << ", " << nSel << "\n"; | |
611 | wxASSERT( nOldSel != nSel ); // impossible | |
612 | ||
613 | if ( nOldSel != -1 ) { | |
ca65c044 | 614 | m_pages[nOldSel]->Show(false); |
45f22d48 | 615 | m_pages[nOldSel]->Lower(); |
1e6d9499 JS |
616 | } |
617 | ||
45f22d48 | 618 | wxNotebookPage *pPage = m_pages[nSel]; |
1e6d9499 JS |
619 | |
620 | wxRect clientRect = GetAvailableClientSize(); | |
621 | pPage->SetSize(clientRect.x, clientRect.y, clientRect.width, clientRect.height); | |
622 | ||
f6bcfd97 BP |
623 | Refresh(); |
624 | ||
ca65c044 | 625 | pPage->Show(true); |
1e6d9499 JS |
626 | pPage->Raise(); |
627 | pPage->SetFocus(); | |
628 | ||
1e6d9499 JS |
629 | m_nSelection = nSel; |
630 | } | |
631 | ||
632 | void wxNotebook::OnMouseEvent(wxMouseEvent& event) | |
633 | { | |
634 | if (m_tabView) | |
635 | m_tabView->OnEvent(event); | |
636 | } | |
637 | ||
638 | void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event) ) | |
639 | { | |
640 | wxPaintDC dc(this); | |
641 | if (m_tabView) | |
642 | m_tabView->Draw(dc); | |
643 | } | |
644 | ||
6bae6726 MB |
645 | wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const |
646 | { | |
647 | // MBN: since the total tab height is really a function of the | |
648 | // width, this should really call | |
649 | // GetTotalTabHeightPretendingWidthIs(), but the current | |
650 | // implementation will suffice, provided the wxNotebook has been | |
651 | // created with a sensible initial width. | |
652 | return wxSize( sizePage.x + 12, | |
653 | sizePage.y + m_tabView->GetTotalTabHeight() + 6 + 4 ); | |
654 | } | |
655 | ||
1e6d9499 JS |
656 | wxRect wxNotebook::GetAvailableClientSize() |
657 | { | |
658 | int cw, ch; | |
659 | GetClientSize(& cw, & ch); | |
660 | ||
661 | int tabHeight = m_tabView->GetTotalTabHeight(); | |
662 | ||
663 | // TODO: these margins should be configurable. | |
664 | wxRect rect; | |
665 | rect.x = 6; | |
666 | rect.y = tabHeight + 6; | |
667 | rect.width = cw - 12; | |
668 | rect.height = ch - 4 - rect.y ; | |
669 | ||
670 | return rect; | |
671 | } | |
672 | ||
673 | /* | |
674 | * wxNotebookTabView | |
675 | */ | |
2e4df4bf | 676 | |
1e6d9499 JS |
677 | IMPLEMENT_CLASS(wxNotebookTabView, wxTabView) |
678 | ||
dd5e8687 MW |
679 | wxNotebookTabView::wxNotebookTabView(wxNotebook *notebook, long style) |
680 | : wxTabView(style), m_nextid(1) | |
1e6d9499 JS |
681 | { |
682 | m_notebook = notebook; | |
683 | ||
684 | m_notebook->SetTabView(this); | |
685 | ||
686 | SetWindow(m_notebook); | |
687 | } | |
688 | ||
689 | wxNotebookTabView::~wxNotebookTabView(void) | |
690 | { | |
691 | } | |
692 | ||
dd5e8687 MW |
693 | int wxNotebookTabView::GetId(wxNotebookPage *page) |
694 | { | |
695 | int& id = m_pageToId[page]; | |
696 | ||
697 | if (!id) | |
698 | { | |
699 | id = m_nextid++; | |
700 | m_idToPage[id] = page; | |
701 | } | |
702 | ||
703 | return id; | |
704 | } | |
705 | ||
1e6d9499 JS |
706 | // Called when a tab is activated |
707 | void wxNotebookTabView::OnTabActivate(int activateId, int deactivateId) | |
708 | { | |
709 | if (!m_notebook) | |
710 | return; | |
711 | ||
712 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_notebook->GetId()); | |
713 | ||
714 | // Translate from wxTabView's ids (which aren't position-dependent) | |
715 | // to wxNotebook's (which are). | |
dd5e8687 MW |
716 | wxNotebookPage* pActive = GetPage(activateId); |
717 | wxNotebookPage* pDeactive = GetPage(deactivateId); | |
1e6d9499 JS |
718 | |
719 | int activatePos = m_notebook->FindPagePosition(pActive); | |
720 | int deactivatePos = m_notebook->FindPagePosition(pDeactive); | |
721 | ||
722 | event.SetEventObject(m_notebook); | |
723 | event.SetSelection(activatePos); | |
724 | event.SetOldSelection(deactivatePos); | |
725 | m_notebook->GetEventHandler()->ProcessEvent(event); | |
726 | } | |
727 | ||
1c507b17 JS |
728 | // Allows Vetoing |
729 | bool wxNotebookTabView::OnTabPreActivate(int activateId, int deactivateId) | |
730 | { | |
ca65c044 WS |
731 | bool retval = true; |
732 | ||
1c507b17 JS |
733 | if (m_notebook) |
734 | { | |
735 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_notebook->GetId()); | |
736 | ||
1c507b17 JS |
737 | // Translate from wxTabView's ids (which aren't position-dependent) |
738 | // to wxNotebook's (which are). | |
dd5e8687 MW |
739 | wxNotebookPage* pActive = GetPage(activateId); |
740 | wxNotebookPage* pDeactive = GetPage(deactivateId); | |
1c507b17 JS |
741 | |
742 | int activatePos = m_notebook->FindPagePosition(pActive); | |
743 | int deactivatePos = m_notebook->FindPagePosition(pDeactive); | |
744 | ||
1c507b17 JS |
745 | event.SetEventObject(m_notebook); |
746 | event.SetSelection(activatePos); | |
747 | event.SetOldSelection(deactivatePos); | |
748 | if (m_notebook->GetEventHandler()->ProcessEvent(event)) | |
749 | { | |
750 | retval = event.IsAllowed(); | |
751 | } | |
752 | } | |
753 | return retval; | |
ca65c044 | 754 | } |
bc5a5239 WS |
755 | |
756 | #endif // wxUSE_NOTEBOOK |