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