]>
Commit | Line | Data |
---|---|---|
88310e2e VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/notebook.cpp | |
3 | // Purpose: implementation of wxNotebook | |
4 | // Author: Vadim Zeitlin | |
907f37b3 | 5 | // Modified by: |
88310e2e VZ |
6 | // Created: 11.06.98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
88310e2e | 12 | #ifdef __GNUG__ |
a3b46648 | 13 | #pragma implementation "notebook.h" |
88310e2e VZ |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
1e6feb95 | 20 | #pragma hdrstop |
88310e2e VZ |
21 | #endif |
22 | ||
1e6feb95 VZ |
23 | #if wxUSE_NOTEBOOK |
24 | ||
88310e2e VZ |
25 | // wxWindows |
26 | #ifndef WX_PRECOMP | |
3096bd2f | 27 | #include "wx/string.h" |
88310e2e VZ |
28 | #endif // WX_PRECOMP |
29 | ||
3096bd2f VZ |
30 | #include "wx/log.h" |
31 | #include "wx/imaglist.h" | |
32 | #include "wx/event.h" | |
33 | #include "wx/control.h" | |
34 | #include "wx/notebook.h" | |
88310e2e | 35 | |
3096bd2f | 36 | #include "wx/msw/private.h" |
88310e2e VZ |
37 | |
38 | // Windows standard headers | |
39 | #ifndef __WIN95__ | |
2432b92d | 40 | #error "wxNotebook is only supported Windows 95 and above" |
88310e2e VZ |
41 | #endif //Win95 |
42 | ||
aaab7c01 VZ |
43 | #include <windowsx.h> // for SetWindowFont |
44 | ||
57c208c5 | 45 | #ifndef __TWIN32__ |
c42404a5 VZ |
46 | #ifdef __GNUWIN32_OLD__ |
47 | #include "wx/msw/gnuwin32/extra.h" | |
48 | #endif | |
65fd5cb0 | 49 | #endif |
57c208c5 | 50 | |
ae090fdb | 51 | #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__)) |
c42404a5 | 52 | #include <commctrl.h> |
88310e2e VZ |
53 | #endif |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // macros | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // check that the page index is valid | |
60 | #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount())) | |
61 | ||
62 | // hide the ugly cast | |
63 | #define m_hwnd (HWND)GetHWND() | |
64 | ||
74b31181 VZ |
65 | // ---------------------------------------------------------------------------- |
66 | // constants | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // This is a work-around for missing defines in gcc-2.95 headers | |
70 | #ifndef TCS_RIGHT | |
71 | #define TCS_RIGHT 0x0002 | |
72 | #endif | |
73 | ||
74 | #ifndef TCS_VERTICAL | |
75 | #define TCS_VERTICAL 0x0080 | |
76 | #endif | |
77 | ||
78 | #ifndef TCS_BOTTOM | |
79 | #define TCS_BOTTOM TCS_RIGHT | |
80 | #endif | |
81 | ||
88310e2e VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // event table | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
2e4df4bf VZ |
86 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) |
87 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
88 | ||
d9317fd4 | 89 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) |
88310e2e VZ |
90 | EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) |
91 | ||
9026ad85 | 92 | EVT_SIZE(wxNotebook::OnSize) |
42e69d6b | 93 | |
88310e2e | 94 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) |
42e69d6b | 95 | |
88310e2e | 96 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) |
d9317fd4 | 97 | END_EVENT_TABLE() |
88310e2e | 98 | |
d9317fd4 VZ |
99 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) |
100 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) | |
88310e2e VZ |
101 | |
102 | // ============================================================================ | |
103 | // implementation | |
104 | // ============================================================================ | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // wxNotebook construction | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | // common part of all ctors | |
111 | void wxNotebook::Init() | |
112 | { | |
1e6feb95 | 113 | m_imageList = NULL; |
88310e2e VZ |
114 | m_nSelection = -1; |
115 | } | |
116 | ||
117 | // default for dynamic class | |
118 | wxNotebook::wxNotebook() | |
119 | { | |
120 | Init(); | |
121 | } | |
122 | ||
123 | // the same arguments as for wxControl | |
124 | wxNotebook::wxNotebook(wxWindow *parent, | |
8b9518ee | 125 | wxWindowID id, |
88310e2e VZ |
126 | const wxPoint& pos, |
127 | const wxSize& size, | |
8b9518ee | 128 | long style, |
88310e2e VZ |
129 | const wxString& name) |
130 | { | |
131 | Init(); | |
132 | ||
133 | Create(parent, id, pos, size, style, name); | |
134 | } | |
135 | ||
136 | // Create() function | |
137 | bool wxNotebook::Create(wxWindow *parent, | |
8b9518ee | 138 | wxWindowID id, |
88310e2e VZ |
139 | const wxPoint& pos, |
140 | const wxSize& size, | |
8b9518ee | 141 | long style, |
88310e2e VZ |
142 | const wxString& name) |
143 | { | |
144 | // base init | |
8d99be5f VZ |
145 | if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) ) |
146 | return FALSE; | |
88310e2e VZ |
147 | |
148 | // colors and font | |
149 | m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE)); | |
4b7f2165 | 150 | m_foregroundColour = *wxBLACK; |
88310e2e | 151 | |
88310e2e | 152 | // style |
fd2daa68 | 153 | m_windowStyle = style | wxTAB_TRAVERSAL; |
88310e2e | 154 | |
d13c32e9 JS |
155 | long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS; |
156 | ||
b0766406 JS |
157 | if ( m_windowStyle & wxCLIP_SIBLINGS ) |
158 | tabStyle |= WS_CLIPSIBLINGS; | |
d13c32e9 JS |
159 | if (m_windowStyle & wxCLIP_CHILDREN) |
160 | tabStyle |= WS_CLIPCHILDREN; | |
88310e2e VZ |
161 | if ( m_windowStyle & wxTC_MULTILINE ) |
162 | tabStyle |= TCS_MULTILINE; | |
163 | if ( m_windowStyle & wxBORDER ) | |
b54e41c5 | 164 | tabStyle |= WS_BORDER; |
58a8ab88 JS |
165 | if (m_windowStyle & wxNB_FIXEDWIDTH) |
166 | tabStyle |= TCS_FIXEDWIDTH ; | |
7a3ac804 RS |
167 | if (m_windowStyle & wxNB_BOTTOM) |
168 | tabStyle |= TCS_RIGHT; | |
169 | if (m_windowStyle & wxNB_LEFT) | |
170 | tabStyle |= TCS_VERTICAL; | |
171 | if (m_windowStyle & wxNB_RIGHT) | |
172 | tabStyle |= TCS_VERTICAL|TCS_RIGHT; | |
0398b1d6 | 173 | |
88310e2e | 174 | |
b225f659 | 175 | if ( !MSWCreateControl(WC_TABCONTROL, tabStyle, pos, size) ) |
789295bf | 176 | { |
88310e2e VZ |
177 | return FALSE; |
178 | } | |
179 | ||
27529614 | 180 | // Not all compilers recognise SetWindowFont |
789295bf VZ |
181 | ::SendMessage(GetHwnd(), WM_SETFONT, |
182 | (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE); | |
27529614 | 183 | |
aaab7c01 | 184 | |
907f37b3 | 185 | if ( parent != NULL ) |
88310e2e | 186 | parent->AddChild(this); |
907f37b3 | 187 | |
88310e2e VZ |
188 | return TRUE; |
189 | } | |
190 | ||
88310e2e VZ |
191 | // ---------------------------------------------------------------------------- |
192 | // wxNotebook accessors | |
193 | // ---------------------------------------------------------------------------- | |
07b8d7ec | 194 | |
88310e2e VZ |
195 | int wxNotebook::GetPageCount() const |
196 | { | |
197 | // consistency check | |
1e6feb95 | 198 | wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) ); |
88310e2e | 199 | |
1e6feb95 | 200 | return m_pages.Count(); |
88310e2e VZ |
201 | } |
202 | ||
203 | int wxNotebook::GetRowCount() const | |
204 | { | |
205 | return TabCtrl_GetRowCount(m_hwnd); | |
206 | } | |
207 | ||
208 | int wxNotebook::SetSelection(int nPage) | |
209 | { | |
223d09f6 | 210 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") ); |
88310e2e VZ |
211 | |
212 | ChangePage(m_nSelection, nPage); | |
213 | ||
214 | return TabCtrl_SetCurSel(m_hwnd, nPage); | |
215 | } | |
216 | ||
88310e2e VZ |
217 | bool wxNotebook::SetPageText(int nPage, const wxString& strText) |
218 | { | |
223d09f6 | 219 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") ); |
88310e2e VZ |
220 | |
221 | TC_ITEM tcItem; | |
222 | tcItem.mask = TCIF_TEXT; | |
837e5743 | 223 | tcItem.pszText = (wxChar *)strText.c_str(); |
88310e2e VZ |
224 | |
225 | return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0; | |
226 | } | |
227 | ||
228 | wxString wxNotebook::GetPageText(int nPage) const | |
229 | { | |
223d09f6 | 230 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxT(""), wxT("notebook page out of range") ); |
88310e2e | 231 | |
837e5743 | 232 | wxChar buf[256]; |
88310e2e VZ |
233 | TC_ITEM tcItem; |
234 | tcItem.mask = TCIF_TEXT; | |
235 | tcItem.pszText = buf; | |
236 | tcItem.cchTextMax = WXSIZEOF(buf); | |
237 | ||
238 | wxString str; | |
239 | if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ) | |
240 | str = tcItem.pszText; | |
241 | ||
242 | return str; | |
243 | } | |
244 | ||
245 | int wxNotebook::GetPageImage(int nPage) const | |
246 | { | |
223d09f6 | 247 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") ); |
88310e2e VZ |
248 | |
249 | TC_ITEM tcItem; | |
250 | tcItem.mask = TCIF_IMAGE; | |
251 | ||
252 | return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1; | |
253 | } | |
254 | ||
255 | bool wxNotebook::SetPageImage(int nPage, int nImage) | |
256 | { | |
223d09f6 | 257 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") ); |
88310e2e VZ |
258 | |
259 | TC_ITEM tcItem; | |
260 | tcItem.mask = TCIF_IMAGE; | |
261 | tcItem.iImage = nImage; | |
262 | ||
263 | return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0; | |
264 | } | |
265 | ||
266 | void wxNotebook::SetImageList(wxImageList* imageList) | |
907f37b3 | 267 | { |
07b8d7ec VZ |
268 | wxNotebookBase::SetImageList(imageList); |
269 | ||
270 | if ( imageList ) | |
1e6feb95 | 271 | { |
07b8d7ec | 272 | TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST()); |
1e6feb95 | 273 | } |
b656febd VS |
274 | } |
275 | ||
d9506e77 VZ |
276 | // ---------------------------------------------------------------------------- |
277 | // wxNotebook size settings | |
278 | // ---------------------------------------------------------------------------- | |
279 | ||
280 | void wxNotebook::SetPageSize(const wxSize& size) | |
281 | { | |
282 | // transform the page size into the notebook size | |
283 | RECT rc; | |
284 | rc.left = | |
285 | rc.top = 0; | |
286 | rc.right = size.x; | |
287 | rc.bottom = size.y; | |
288 | ||
289 | TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc); | |
290 | ||
291 | // and now set it | |
292 | SetSize(rc.right - rc.left, rc.bottom - rc.top); | |
293 | } | |
294 | ||
295 | void wxNotebook::SetPadding(const wxSize& padding) | |
296 | { | |
297 | TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y); | |
298 | } | |
42e69d6b VZ |
299 | |
300 | // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH | |
301 | // style. | |
302 | void wxNotebook::SetTabSize(const wxSize& sz) | |
303 | { | |
304 | ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y)); | |
305 | } | |
306 | ||
88310e2e VZ |
307 | // ---------------------------------------------------------------------------- |
308 | // wxNotebook operations | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
311 | // remove one page from the notebook | |
312 | bool wxNotebook::DeletePage(int nPage) | |
313 | { | |
223d09f6 | 314 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") ); |
88310e2e | 315 | |
96d37807 VZ |
316 | if ( m_nSelection == nPage ) { |
317 | // advance selection backwards - the page being deleted shouldn't be left | |
318 | // selected | |
319 | AdvanceSelection(FALSE); | |
320 | } | |
321 | ||
88310e2e VZ |
322 | TabCtrl_DeleteItem(m_hwnd, nPage); |
323 | ||
1e6feb95 | 324 | delete m_pages[nPage]; |
b54e41c5 | 325 | m_pages.RemoveAt(nPage); |
88310e2e | 326 | |
1e6feb95 | 327 | if ( m_pages.IsEmpty() ) { |
96d37807 VZ |
328 | // no selection if the notebook became empty |
329 | m_nSelection = -1; | |
330 | } | |
9b6b5750 JS |
331 | else |
332 | m_nSelection = TabCtrl_GetCurSel(m_hwnd); | |
333 | ||
96d37807 | 334 | |
88310e2e VZ |
335 | return TRUE; |
336 | } | |
337 | ||
621793f4 | 338 | // remove one page from the notebook, without deleting |
1e6feb95 | 339 | wxNotebookPage *wxNotebook::DoRemovePage(int nPage) |
621793f4 | 340 | { |
10199e27 VZ |
341 | wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage); |
342 | if ( !pageRemoved ) | |
343 | return NULL; | |
621793f4 JS |
344 | |
345 | TabCtrl_DeleteItem(m_hwnd, nPage); | |
346 | ||
1e6feb95 VZ |
347 | if ( m_pages.IsEmpty() ) |
348 | m_nSelection = -1; | |
349 | else | |
350 | m_nSelection = TabCtrl_GetCurSel(m_hwnd); | |
47f12f58 | 351 | |
1e6feb95 | 352 | return pageRemoved; |
621793f4 JS |
353 | } |
354 | ||
88310e2e VZ |
355 | // remove all pages |
356 | bool wxNotebook::DeleteAllPages() | |
357 | { | |
88310e2e VZ |
358 | int nPageCount = GetPageCount(); |
359 | int nPage; | |
360 | for ( nPage = 0; nPage < nPageCount; nPage++ ) | |
1e6feb95 | 361 | delete m_pages[nPage]; |
88310e2e | 362 | |
1e6feb95 | 363 | m_pages.Clear(); |
88310e2e | 364 | |
907f37b3 VZ |
365 | TabCtrl_DeleteAllItems(m_hwnd); |
366 | ||
47f12f58 JS |
367 | m_nSelection = -1; |
368 | ||
88310e2e VZ |
369 | return TRUE; |
370 | } | |
371 | ||
372 | // add a page to the notebook | |
373 | bool wxNotebook::AddPage(wxNotebookPage *pPage, | |
374 | const wxString& strText, | |
375 | bool bSelect, | |
376 | int imageId) | |
377 | { | |
378 | return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId); | |
379 | } | |
380 | ||
381 | // same as AddPage() but does it at given position | |
382 | bool wxNotebook::InsertPage(int nPage, | |
383 | wxNotebookPage *pPage, | |
384 | const wxString& strText, | |
385 | bool bSelect, | |
386 | int imageId) | |
387 | { | |
388 | wxASSERT( pPage != NULL ); | |
389 | wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE ); | |
390 | ||
43427087 VZ |
391 | // do add the tab to the control |
392 | ||
393 | // init all fields to 0 | |
88310e2e | 394 | TC_ITEM tcItem; |
43427087 | 395 | memset(&tcItem, 0, sizeof(tcItem)); |
58a8ab88 | 396 | |
43427087 | 397 | if ( imageId != -1 ) |
58a8ab88 JS |
398 | { |
399 | tcItem.mask |= TCIF_IMAGE; | |
400 | tcItem.iImage = imageId; | |
401 | } | |
58a8ab88 | 402 | |
43427087 | 403 | if ( !strText.IsEmpty() ) |
58a8ab88 | 404 | { |
43427087 VZ |
405 | tcItem.mask |= TCIF_TEXT; |
406 | tcItem.pszText = (wxChar *)strText.c_str(); // const_cast | |
58a8ab88 | 407 | } |
88310e2e VZ |
408 | |
409 | if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) { | |
223d09f6 | 410 | wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str()); |
43427087 | 411 | |
88310e2e VZ |
412 | return FALSE; |
413 | } | |
414 | ||
43427087 VZ |
415 | // if the inserted page is before the selected one, we must update the |
416 | // index of the selected page | |
417 | if ( nPage <= m_nSelection ) | |
418 | { | |
419 | // one extra page added | |
420 | m_nSelection++; | |
421 | } | |
422 | ||
88310e2e | 423 | // save the pointer to the page |
1e6feb95 | 424 | m_pages.Insert(pPage, nPage); |
88310e2e | 425 | |
88310e2e | 426 | // don't show pages by default (we'll need to adjust their size first) |
42e69d6b | 427 | HWND hwnd = GetWinHwnd(pPage); |
88310e2e VZ |
428 | SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE); |
429 | ||
42e69d6b VZ |
430 | // this updates internal flag too - otherwise it will get out of sync |
431 | pPage->Show(FALSE); | |
432 | ||
0398b1d6 RD |
433 | // fit the notebook page to the tab control's display area |
434 | RECT rc; | |
435 | rc.left = rc.top = 0; | |
436 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
437 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
438 | pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); | |
0398b1d6 | 439 | |
43427087 VZ |
440 | // some page should be selected: either this one or the first one if there is |
441 | // still no selection | |
442 | int selNew = -1; | |
443 | if ( bSelect ) | |
444 | selNew = nPage; | |
445 | else if ( m_nSelection == -1 ) | |
446 | selNew = 0; | |
447 | ||
448 | if ( selNew != -1 ) | |
449 | SetSelection(selNew); | |
96d37807 | 450 | |
88310e2e VZ |
451 | return TRUE; |
452 | } | |
453 | ||
454 | // ---------------------------------------------------------------------------- | |
455 | // wxNotebook callbacks | |
456 | // ---------------------------------------------------------------------------- | |
457 | ||
9026ad85 | 458 | void wxNotebook::OnSize(wxSizeEvent& event) |
88310e2e | 459 | { |
b5c3b538 VZ |
460 | // fit the notebook page to the tab control's display area |
461 | RECT rc; | |
462 | rc.left = rc.top = 0; | |
463 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
464 | ||
465 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
4b7f2165 VZ |
466 | |
467 | int width = rc.right - rc.left, | |
468 | height = rc.bottom - rc.top; | |
1e6feb95 | 469 | size_t nCount = m_pages.Count(); |
c86f1403 | 470 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) { |
1e6feb95 | 471 | wxNotebookPage *pPage = m_pages[nPage]; |
4b7f2165 | 472 | pPage->SetSize(rc.left, rc.top, width, height); |
b5c3b538 VZ |
473 | } |
474 | ||
88310e2e VZ |
475 | event.Skip(); |
476 | } | |
477 | ||
478 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
479 | { | |
480 | // is it our tab control? | |
481 | if ( event.GetEventObject() == this ) | |
5d1d2d46 | 482 | { |
5d1d2d46 VZ |
483 | int sel = event.GetOldSelection(); |
484 | if ( sel != -1 ) | |
1e6feb95 | 485 | m_pages[sel]->Show(FALSE); |
0398b1d6 | 486 | |
5d1d2d46 VZ |
487 | sel = event.GetSelection(); |
488 | if ( sel != -1 ) | |
489 | { | |
1e6feb95 | 490 | wxNotebookPage *pPage = m_pages[sel]; |
5d1d2d46 VZ |
491 | pPage->Show(TRUE); |
492 | pPage->SetFocus(); | |
493 | } | |
0398b1d6 | 494 | |
5d1d2d46 VZ |
495 | m_nSelection = sel; |
496 | } | |
88310e2e VZ |
497 | |
498 | // we want to give others a chance to process this message as well | |
499 | event.Skip(); | |
500 | } | |
501 | ||
502 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
503 | { | |
d9506e77 VZ |
504 | // this function is only called when the focus is explicitly set (i.e. from |
505 | // the program) to the notebook - in this case we don't need the | |
506 | // complicated OnNavigationKey() logic because the programmer knows better | |
507 | // what [s]he wants | |
88310e2e | 508 | |
d9506e77 VZ |
509 | // set focus to the currently selected page if any |
510 | if ( m_nSelection != -1 ) | |
1e6feb95 | 511 | m_pages[m_nSelection]->SetFocus(); |
d9506e77 VZ |
512 | |
513 | event.Skip(); | |
88310e2e VZ |
514 | } |
515 | ||
516 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
517 | { | |
d9506e77 VZ |
518 | if ( event.IsWindowChange() ) { |
519 | // change pages | |
520 | AdvanceSelection(event.GetDirection()); | |
521 | } | |
522 | else { | |
523 | // we get this event in 2 cases | |
524 | // | |
525 | // a) one of our pages might have generated it because the user TABbed | |
526 | // out from it in which case we should propagate the event upwards and | |
527 | // our parent will take care of setting the focus to prev/next sibling | |
528 | // | |
529 | // or | |
530 | // | |
531 | // b) the parent panel wants to give the focus to us so that we | |
532 | // forward it to our selected page. We can't deal with this in | |
533 | // OnSetFocus() because we don't know which direction the focus came | |
534 | // from in this case and so can't choose between setting the focus to | |
535 | // first or last panel child | |
536 | ||
537 | wxWindow *parent = GetParent(); | |
538 | if ( event.GetEventObject() == parent ) | |
539 | { | |
540 | // no, it doesn't come from child, case (b): forward to a page | |
541 | if ( m_nSelection != -1 ) | |
542 | { | |
543 | // so that the page knows that the event comes from it's parent | |
544 | // and is being propagated downwards | |
545 | event.SetEventObject(this); | |
546 | ||
1e6feb95 | 547 | wxWindow *page = m_pages[m_nSelection]; |
d9506e77 VZ |
548 | if ( !page->GetEventHandler()->ProcessEvent(event) ) |
549 | { | |
550 | page->SetFocus(); | |
551 | } | |
552 | //else: page manages focus inside it itself | |
553 | } | |
554 | else | |
555 | { | |
556 | // we have no pages - still have to give focus to _something_ | |
557 | SetFocus(); | |
558 | } | |
559 | } | |
560 | else | |
561 | { | |
562 | // it comes from our child, case (a), pass to the parent | |
563 | if ( parent ) { | |
564 | event.SetCurrentFocus(this); | |
565 | parent->GetEventHandler()->ProcessEvent(event); | |
566 | } | |
567 | } | |
88310e2e | 568 | } |
88310e2e VZ |
569 | } |
570 | ||
571 | // ---------------------------------------------------------------------------- | |
572 | // wxNotebook base class virtuals | |
573 | // ---------------------------------------------------------------------------- | |
b5c3b538 VZ |
574 | |
575 | // override these 2 functions to do nothing: everything is done in OnSize | |
576 | ||
4b7f2165 | 577 | void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse)) |
b5c3b538 VZ |
578 | { |
579 | // don't set the sizes of the pages - their correct size is not yet known | |
580 | wxControl::SetConstraintSizes(FALSE); | |
581 | } | |
582 | ||
4b7f2165 | 583 | bool wxNotebook::DoPhase(int WXUNUSED(nPhase)) |
b5c3b538 VZ |
584 | { |
585 | return TRUE; | |
586 | } | |
587 | ||
a23fd0e1 | 588 | bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result) |
88310e2e | 589 | { |
93a19f17 | 590 | wxNotebookEvent event(wxEVT_NULL, m_windowId); |
88310e2e VZ |
591 | |
592 | NMHDR* hdr = (NMHDR *)lParam; | |
593 | switch ( hdr->code ) { | |
594 | case TCN_SELCHANGE: | |
595 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
596 | break; | |
597 | ||
598 | case TCN_SELCHANGING: | |
599 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING); | |
600 | break; | |
601 | ||
fd3f686c | 602 | default: |
a23fd0e1 | 603 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
88310e2e VZ |
604 | } |
605 | ||
93a19f17 VZ |
606 | event.SetSelection(TabCtrl_GetCurSel(m_hwnd)); |
607 | event.SetOldSelection(m_nSelection); | |
88310e2e | 608 | event.SetEventObject(this); |
a23fd0e1 | 609 | event.SetInt(idCtrl); |
88310e2e | 610 | |
fd3f686c VZ |
611 | bool processed = GetEventHandler()->ProcessEvent(event); |
612 | *result = !event.IsAllowed(); | |
613 | return processed; | |
88310e2e VZ |
614 | } |
615 | ||
616 | // ---------------------------------------------------------------------------- | |
617 | // wxNotebook helper functions | |
618 | // ---------------------------------------------------------------------------- | |
619 | ||
43427087 VZ |
620 | // generate the page changing and changed events, hide the currently active |
621 | // panel and show the new one | |
88310e2e VZ |
622 | void wxNotebook::ChangePage(int nOldSel, int nSel) |
623 | { | |
fd3f686c VZ |
624 | // MT-FIXME should use a real semaphore |
625 | static bool s_bInsideChangePage = FALSE; | |
626 | ||
627 | // when we call ProcessEvent(), our own OnSelChange() is called which calls | |
628 | // this function - break the infinite loop | |
629 | if ( s_bInsideChangePage ) | |
630 | return; | |
631 | ||
aaab7c01 VZ |
632 | // it's not an error (the message may be generated by the tab control itself) |
633 | // and it may happen - just do nothing | |
634 | if ( nSel == nOldSel ) | |
635 | return; | |
88310e2e | 636 | |
fd3f686c VZ |
637 | s_bInsideChangePage = TRUE; |
638 | ||
639 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId); | |
640 | event.SetSelection(nSel); | |
641 | event.SetOldSelection(nOldSel); | |
642 | event.SetEventObject(this); | |
1e6feb95 | 643 | if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() ) |
fd3f686c VZ |
644 | { |
645 | // program doesn't allow the page change | |
646 | s_bInsideChangePage = FALSE; | |
647 | return; | |
648 | } | |
649 | ||
fd3f686c | 650 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); |
1e6feb95 | 651 | GetEventHandler()->ProcessEvent(event); |
fd3f686c | 652 | |
fd3f686c | 653 | s_bInsideChangePage = FALSE; |
88310e2e | 654 | } |
1e6feb95 VZ |
655 | |
656 | #endif // wxUSE_NOTEBOOK |