]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
Bumped the reference manual date.
[wxWidgets.git] / src / msw / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.06.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_NOTEBOOK
20
21 #include "wx/notebook.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
25 #include "wx/string.h"
26 #include "wx/dc.h"
27 #include "wx/log.h"
28 #include "wx/event.h"
29 #include "wx/app.h"
30 #include "wx/dcclient.h"
31 #include "wx/dcmemory.h"
32 #include "wx/control.h"
33 #include "wx/panel.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/imaglist.h"
37 #include "wx/sysopt.h"
38
39 #include "wx/msw/private.h"
40 #include "wx/msw/dc.h"
41
42 #include <windowsx.h>
43 #include "wx/msw/winundef.h"
44
45 #if wxUSE_UXTHEME
46 #include "wx/msw/uxtheme.h"
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 // check that the page index is valid
54 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
55
56 // you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too
57 // to disable code whih results in flicker-less notebook redrawing at the
58 // expense of some extra GDI resource consumption
59 #ifdef __WXWINCE__
60 // notebooks are never resized under CE anyhow
61 #define USE_NOTEBOOK_ANTIFLICKER 0
62 #else
63 #define USE_NOTEBOOK_ANTIFLICKER 1
64 #endif
65
66 // ----------------------------------------------------------------------------
67 // constants
68 // ----------------------------------------------------------------------------
69
70 // This is a work-around for missing defines in gcc-2.95 headers
71 #ifndef TCS_RIGHT
72 #define TCS_RIGHT 0x0002
73 #endif
74
75 #ifndef TCS_VERTICAL
76 #define TCS_VERTICAL 0x0080
77 #endif
78
79 #ifndef TCS_BOTTOM
80 #define TCS_BOTTOM TCS_RIGHT
81 #endif
82
83 // ----------------------------------------------------------------------------
84 // global variables
85 // ----------------------------------------------------------------------------
86
87 #if USE_NOTEBOOK_ANTIFLICKER
88
89 // the pointer to standard spin button wnd proc
90 static WXFARPROC gs_wndprocNotebookSpinBtn = (WXFARPROC)NULL;
91
92 // the pointer to standard tab control wnd proc
93 static WXFARPROC gs_wndprocNotebook = (WXFARPROC)NULL;
94
95 LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd,
96 UINT message,
97 WPARAM wParam,
98 LPARAM lParam);
99
100 #endif // USE_NOTEBOOK_ANTIFLICKER
101
102 // ----------------------------------------------------------------------------
103 // global functions
104 // ----------------------------------------------------------------------------
105
106 static bool HasTroubleWithNonTopTabs()
107 {
108 const int verComCtl32 = wxApp::GetComCtl32Version();
109
110 // 600 is XP, 616 is Vista -- and both have a problem with tabs not on top
111 // (but don't just test for >= 600 as Microsoft might decide to fix it in
112 // later versions, who knows...)
113 return verComCtl32 >= 600 && verComCtl32 <= 616;
114 }
115
116 // ----------------------------------------------------------------------------
117 // event table
118 // ----------------------------------------------------------------------------
119
120 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
121 EVT_SIZE(wxNotebook::OnSize)
122 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
123
124 #if USE_NOTEBOOK_ANTIFLICKER
125 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground)
126 EVT_PAINT(wxNotebook::OnPaint)
127 #endif // USE_NOTEBOOK_ANTIFLICKER
128 END_EVENT_TABLE()
129
130 // ============================================================================
131 // implementation
132 // ============================================================================
133
134 // ----------------------------------------------------------------------------
135 // wxNotebook construction
136 // ----------------------------------------------------------------------------
137
138 // common part of all ctors
139 void wxNotebook::Init()
140 {
141 #if wxUSE_UXTHEME
142 m_hbrBackground = NULL;
143 #endif // wxUSE_UXTHEME
144
145 #if USE_NOTEBOOK_ANTIFLICKER
146 m_hasSubclassedUpdown = false;
147 m_doneUpdateHack = false;
148 #endif // USE_NOTEBOOK_ANTIFLICKER
149 }
150
151 // default for dynamic class
152 wxNotebook::wxNotebook()
153 {
154 Init();
155 }
156
157 // the same arguments as for wxControl
158 wxNotebook::wxNotebook(wxWindow *parent,
159 wxWindowID id,
160 const wxPoint& pos,
161 const wxSize& size,
162 long style,
163 const wxString& name)
164 {
165 Init();
166
167 Create(parent, id, pos, size, style, name);
168 }
169
170 // Create() function
171 bool wxNotebook::Create(wxWindow *parent,
172 wxWindowID id,
173 const wxPoint& pos,
174 const wxSize& size,
175 long style,
176 const wxString& name)
177 {
178 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
179 {
180 #if defined(__POCKETPC__)
181 style |= wxBK_BOTTOM | wxNB_FLAT;
182 #else
183 style |= wxBK_TOP;
184 #endif
185 }
186
187 #ifdef __WXWINCE__
188 // Not sure why, but without this style, there is no border
189 // around the notebook tabs.
190 if (style & wxNB_FLAT)
191 style |= wxBORDER_SUNKEN;
192 #endif
193
194 #if !wxUSE_UXTHEME
195 // ComCtl32 notebook tabs simply don't work unless they're on top if we
196 // have uxtheme, we can work around it later (after control creation), but
197 // if we have been compiled without uxtheme support, we have to clear those
198 // styles
199 if ( HasTroubleWithNonTopTabs() )
200 {
201 style &= ~(wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT);
202 }
203 #endif //wxUSE_UXTHEME
204
205 #if defined(__WINE__) && wxUSE_UNICODE
206 LPCTSTR className = L"SysTabControl32";
207 #else
208 LPCTSTR className = WC_TABCONTROL;
209 #endif
210
211 #if USE_NOTEBOOK_ANTIFLICKER
212 // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it
213 // causes horrible flicker when resizing notebook, so get rid of it by
214 // using a class without these styles (but otherwise identical to it)
215 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
216 {
217 static ClassRegistrar s_clsNotebook;
218 if ( !s_clsNotebook.IsInitialized() )
219 {
220 // get a copy of standard class and modify it
221 WNDCLASS wc;
222
223 if ( ::GetClassInfo(NULL, WC_TABCONTROL, &wc) )
224 {
225 gs_wndprocNotebook =
226 reinterpret_cast<WXFARPROC>(wc.lpfnWndProc);
227 wc.lpszClassName = wxT("_wx_SysTabCtl32");
228 wc.style &= ~(CS_HREDRAW | CS_VREDRAW);
229 wc.hInstance = wxGetInstance();
230 wc.lpfnWndProc = wxNotebookWndProc;
231 s_clsNotebook.Register(wc);
232 }
233 else
234 {
235 wxLogLastError(wxT("GetClassInfoEx(SysTabCtl32)"));
236 }
237 }
238
239 // use our custom class if available but fall back to the standard
240 // notebook if we failed to register it
241 if ( s_clsNotebook.IsRegistered() )
242 {
243 // it's ok to use c_str() here as the static s_clsNotebook object
244 // has sufficiently long lifetime
245 className = s_clsNotebook.GetName().c_str();
246 }
247 }
248 #endif // USE_NOTEBOOK_ANTIFLICKER
249
250 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
251 wxDefaultValidator, name) )
252 return false;
253
254 if ( !MSWCreateControl(className, wxEmptyString, pos, size) )
255 return false;
256
257 #if wxUSE_UXTHEME
258 if ( HasFlag(wxNB_NOPAGETHEME) ||
259 wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) )
260 {
261 SetBackgroundColour(GetThemeBackgroundColour());
262 }
263 else // use themed background by default
264 {
265 // create backing store
266 UpdateBgBrush();
267 }
268
269 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
270 // control is simply not rendered correctly), so we disable themes
271 // if possible, otherwise we simply clear the styles.
272 if ( HasTroubleWithNonTopTabs() &&
273 (style & (wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT)) )
274 {
275 // check if we use themes at all -- if we don't, we're still okay
276 if ( wxUxThemeEngine::GetIfActive() )
277 {
278 wxUxThemeEngine::GetIfActive()->SetWindowTheme(GetHwnd(), L"", L"");
279
280 // correct the background color for the new non-themed control
281 SetBackgroundColour(GetThemeBackgroundColour());
282 }
283 }
284 #endif // wxUSE_UXTHEME
285
286 // Undocumented hack to get flat notebook style
287 // In fact, we should probably only do this in some
288 // curcumstances, i.e. if we know we will have a border
289 // at the bottom (the tab control doesn't draw it itself)
290 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
291 if (HasFlag(wxNB_FLAT))
292 {
293 SendMessage(GetHwnd(), CCM_SETVERSION, COMCTL32_VERSION, 0);
294 if (!m_hasBgCol)
295 SetBackgroundColour(*wxWHITE);
296 }
297 #endif
298 return true;
299 }
300
301 WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
302 {
303 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
304
305 tabStyle |= WS_TABSTOP | TCS_TABS;
306
307 if ( style & wxNB_MULTILINE )
308 tabStyle |= TCS_MULTILINE;
309 if ( style & wxNB_FIXEDWIDTH )
310 tabStyle |= TCS_FIXEDWIDTH;
311
312 if ( style & wxBK_BOTTOM )
313 tabStyle |= TCS_RIGHT;
314 else if ( style & wxBK_LEFT )
315 tabStyle |= TCS_VERTICAL;
316 else if ( style & wxBK_RIGHT )
317 tabStyle |= TCS_VERTICAL | TCS_RIGHT;
318
319 return tabStyle;
320 }
321
322 wxNotebook::~wxNotebook()
323 {
324 #if wxUSE_UXTHEME
325 if ( m_hbrBackground )
326 ::DeleteObject((HBRUSH)m_hbrBackground);
327 #endif // wxUSE_UXTHEME
328 }
329
330 // ----------------------------------------------------------------------------
331 // wxNotebook accessors
332 // ----------------------------------------------------------------------------
333
334 size_t wxNotebook::GetPageCount() const
335 {
336 // consistency check
337 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(GetHwnd()) );
338
339 return m_pages.Count();
340 }
341
342 int wxNotebook::GetRowCount() const
343 {
344 return TabCtrl_GetRowCount(GetHwnd());
345 }
346
347 int wxNotebook::SetSelection(size_t nPage)
348 {
349 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
350
351 if ( m_selection == wxNOT_FOUND || nPage != (size_t)m_selection )
352 {
353 if ( SendPageChangingEvent(nPage) )
354 {
355 // program allows the page change
356 const int selectionOld = m_selection;
357
358 UpdateSelection(nPage);
359
360 TabCtrl_SetCurSel(GetHwnd(), nPage);
361
362 SendPageChangedEvent(selectionOld, nPage);
363 }
364 }
365
366 return m_selection;
367 }
368
369 void wxNotebook::UpdateSelection(int selNew)
370 {
371 if ( m_selection != wxNOT_FOUND )
372 m_pages[m_selection]->Show(false);
373
374 if ( selNew != wxNOT_FOUND )
375 {
376 wxNotebookPage *pPage = m_pages[selNew];
377 pPage->Show(true);
378
379 // In addition to showing the page, we also want to give focus to it to
380 // make it possible to work with it from keyboard easily. However there
381 // are two exceptions: first, don't touch the focus at all if the
382 // notebook itself is not currently shown.
383 if ( ::IsWindowVisible(GetHwnd()) )
384 {
385 // And second, don't give focus away if the tab control itself has
386 // it, as this is how the native property sheets behave: if you
387 // explicitly click on the tab label giving it focus, it will
388 // remain after switching to another page. But if the focus was
389 // inside the notebook page, it switches to the new page.
390 if ( !HasFocus() )
391 pPage->SetFocus();
392 }
393 }
394
395 m_selection = selNew;
396 }
397
398 int wxNotebook::ChangeSelection(size_t nPage)
399 {
400 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
401
402 const int selOld = m_selection;
403
404 if ( m_selection == wxNOT_FOUND || nPage != (size_t)m_selection )
405 {
406 TabCtrl_SetCurSel(GetHwnd(), nPage);
407
408 UpdateSelection(nPage);
409 }
410
411 return selOld;
412 }
413
414 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
415 {
416 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
417
418 TC_ITEM tcItem;
419 tcItem.mask = TCIF_TEXT;
420 tcItem.pszText = wxMSW_CONV_LPTSTR(strText);
421
422 if ( !HasFlag(wxNB_MULTILINE) )
423 return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
424
425 // multiline - we need to set new page size if a line is added or removed
426 int rows = GetRowCount();
427 bool ret = TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
428
429 if ( ret && rows != GetRowCount() )
430 {
431 const wxRect r = GetPageSize();
432 const size_t count = m_pages.Count();
433 for ( size_t page = 0; page < count; page++ )
434 m_pages[page]->SetSize(r);
435 }
436
437 return ret;
438 }
439
440 wxString wxNotebook::GetPageText(size_t nPage) const
441 {
442 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
443
444 wxChar buf[256];
445 TC_ITEM tcItem;
446 tcItem.mask = TCIF_TEXT;
447 tcItem.pszText = buf;
448 tcItem.cchTextMax = WXSIZEOF(buf);
449
450 wxString str;
451 if ( TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) )
452 str = tcItem.pszText;
453
454 return str;
455 }
456
457 int wxNotebook::GetPageImage(size_t nPage) const
458 {
459 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
460
461 TC_ITEM tcItem;
462 tcItem.mask = TCIF_IMAGE;
463
464 return TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) ? tcItem.iImage
465 : wxNOT_FOUND;
466 }
467
468 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
469 {
470 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
471
472 TC_ITEM tcItem;
473 tcItem.mask = TCIF_IMAGE;
474 tcItem.iImage = nImage;
475
476 return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0;
477 }
478
479 void wxNotebook::SetImageList(wxImageList* imageList)
480 {
481 wxNotebookBase::SetImageList(imageList);
482
483 if ( imageList )
484 {
485 (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList));
486 }
487 }
488
489 // ----------------------------------------------------------------------------
490 // wxNotebook size settings
491 // ----------------------------------------------------------------------------
492
493 wxRect wxNotebook::GetPageSize() const
494 {
495 wxRect r;
496
497 RECT rc;
498 ::GetClientRect(GetHwnd(), &rc);
499
500 // This check is to work around a bug in TabCtrl_AdjustRect which will
501 // cause a crash on win2k or on XP with themes disabled if either
502 // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle
503 // is too small.
504 //
505 // The value of 20 is chosen arbitrarily but seems to work
506 if ( rc.right > 20 && rc.bottom > 20 )
507 {
508 TabCtrl_AdjustRect(GetHwnd(), false, &rc);
509
510 wxCopyRECTToRect(rc, r);
511 }
512
513 return r;
514 }
515
516 void wxNotebook::SetPageSize(const wxSize& size)
517 {
518 // transform the page size into the notebook size
519 RECT rc;
520 rc.left =
521 rc.top = 0;
522 rc.right = size.x;
523 rc.bottom = size.y;
524
525 TabCtrl_AdjustRect(GetHwnd(), true, &rc);
526
527 // and now set it
528 SetSize(rc.right - rc.left, rc.bottom - rc.top);
529 }
530
531 void wxNotebook::SetPadding(const wxSize& padding)
532 {
533 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
534 }
535
536 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
537 // style.
538 void wxNotebook::SetTabSize(const wxSize& sz)
539 {
540 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
541 }
542
543 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
544 {
545 // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP
546 wxSize sizeTotal = sizePage;
547
548 wxSize tabSize;
549 if ( GetPageCount() > 0 )
550 {
551 RECT rect;
552 TabCtrl_GetItemRect(GetHwnd(), 0, &rect);
553 tabSize.x = rect.right - rect.left;
554 tabSize.y = rect.bottom - rect.top;
555 }
556
557 const int rows = GetRowCount();
558
559 // add an extra margin in both directions
560 const int MARGIN = 8;
561 if ( IsVertical() )
562 {
563 sizeTotal.x += MARGIN;
564 sizeTotal.y += tabSize.y * rows + MARGIN;
565 }
566 else // horizontal layout
567 {
568 sizeTotal.x += tabSize.x * rows + MARGIN;
569 sizeTotal.y += MARGIN;
570 }
571
572 return sizeTotal;
573 }
574
575 void wxNotebook::AdjustPageSize(wxNotebookPage *page)
576 {
577 wxCHECK_RET( page, wxT("NULL page in wxNotebook::AdjustPageSize") );
578
579 const wxRect r = GetPageSize();
580 if ( !r.IsEmpty() )
581 {
582 page->SetSize(r);
583 }
584 }
585
586 // ----------------------------------------------------------------------------
587 // wxNotebook operations
588 // ----------------------------------------------------------------------------
589
590 // remove one page from the notebook, without deleting
591 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
592 {
593 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
594 if ( !pageRemoved )
595 return NULL;
596
597 // hide the removed page to maintain the invariant that only the
598 // selected page is visible and others are hidden:
599 pageRemoved->Show(false);
600
601 TabCtrl_DeleteItem(GetHwnd(), nPage);
602
603 if ( m_pages.IsEmpty() )
604 {
605 // no selection any more, the notebook becamse empty
606 m_selection = wxNOT_FOUND;
607 }
608 else // notebook still not empty
609 {
610 int selNew = TabCtrl_GetCurSel(GetHwnd());
611 if ( selNew != wxNOT_FOUND )
612 {
613 // No selection change, just refresh the current selection.
614 // Because it could be that the slection index changed
615 // we need to update it.
616 // Note: this does not mean the selection it self changed.
617 m_selection = selNew;
618 m_pages[m_selection]->Refresh();
619 }
620 else if (int(nPage) == m_selection)
621 {
622 // The selection was deleted.
623
624 // Determine new selection.
625 if (m_selection == int(GetPageCount()))
626 selNew = m_selection - 1;
627 else
628 selNew = m_selection;
629
630 // m_selection must be always valid so reset it before calling
631 // SetSelection()
632 m_selection = wxNOT_FOUND;
633 SetSelection(selNew);
634 }
635 else
636 {
637 wxFAIL; // Windows did not behave ok.
638 }
639 }
640
641 return pageRemoved;
642 }
643
644 // remove all pages
645 bool wxNotebook::DeleteAllPages()
646 {
647 size_t nPageCount = GetPageCount();
648 size_t nPage;
649 for ( nPage = 0; nPage < nPageCount; nPage++ )
650 delete m_pages[nPage];
651
652 m_pages.Clear();
653
654 TabCtrl_DeleteAllItems(GetHwnd());
655
656 m_selection = wxNOT_FOUND;
657
658 InvalidateBestSize();
659 return true;
660 }
661
662 // same as AddPage() but does it at given position
663 bool wxNotebook::InsertPage(size_t nPage,
664 wxNotebookPage *pPage,
665 const wxString& strText,
666 bool bSelect,
667 int imageId)
668 {
669 wxCHECK_MSG( pPage != NULL, false, wxT("NULL page in wxNotebook::InsertPage") );
670 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false,
671 wxT("invalid index in wxNotebook::InsertPage") );
672
673 wxASSERT_MSG( pPage->GetParent() == this,
674 wxT("notebook pages must have notebook as parent") );
675
676 // add a new tab to the control
677 // ----------------------------
678
679 // init all fields to 0
680 TC_ITEM tcItem;
681 wxZeroMemory(tcItem);
682
683 // set the image, if any
684 if ( imageId != -1 )
685 {
686 tcItem.mask |= TCIF_IMAGE;
687 tcItem.iImage = imageId;
688 }
689
690 // and the text
691 if ( !strText.empty() )
692 {
693 tcItem.mask |= TCIF_TEXT;
694 tcItem.pszText = wxMSW_CONV_LPTSTR(strText);
695 }
696
697 // hide the page: unless it is selected, it shouldn't be shown (and if it
698 // is selected it will be shown later)
699 HWND hwnd = GetWinHwnd(pPage);
700 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
701
702 // this updates internal flag too -- otherwise it would get out of sync
703 // with the real state
704 pPage->Show(false);
705
706
707 // fit the notebook page to the tab control's display area: this should be
708 // done before adding it to the notebook or TabCtrl_InsertItem() will
709 // change the notebooks size itself!
710 AdjustPageSize(pPage);
711
712 // finally do insert it
713 if ( TabCtrl_InsertItem(GetHwnd(), nPage, &tcItem) == -1 )
714 {
715 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
716
717 return false;
718 }
719
720 // need to update the bg brush when the first page is added
721 // so the first panel gets the correct themed background
722 if ( m_pages.empty() )
723 {
724 #if wxUSE_UXTHEME
725 UpdateBgBrush();
726 #endif // wxUSE_UXTHEME
727 }
728
729 // succeeded: save the pointer to the page
730 m_pages.Insert(pPage, nPage);
731
732 // we may need to adjust the size again if the notebook size changed:
733 // normally this only happens for the first page we add (the tabs which
734 // hadn't been there before are now shown) but for a multiline notebook it
735 // can happen for any page at all as a new row could have been started
736 if ( m_pages.GetCount() == 1 || HasFlag(wxNB_MULTILINE) )
737 {
738 AdjustPageSize(pPage);
739 }
740
741 // now deal with the selection
742 // ---------------------------
743
744 // if the inserted page is before the selected one, we must update the
745 // index of the selected page
746 if ( int(nPage) <= m_selection )
747 {
748 // one extra page added
749 m_selection++;
750 }
751
752 DoSetSelectionAfterInsertion(nPage, bSelect);
753
754 InvalidateBestSize();
755
756 return true;
757 }
758
759 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
760 {
761 TC_HITTESTINFO hitTestInfo;
762 hitTestInfo.pt.x = pt.x;
763 hitTestInfo.pt.y = pt.y;
764 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
765
766 if ( flags )
767 {
768 *flags = 0;
769
770 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
771 *flags |= wxBK_HITTEST_NOWHERE;
772 if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM)
773 *flags |= wxBK_HITTEST_ONITEM;
774 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
775 *flags |= wxBK_HITTEST_ONICON;
776 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
777 *flags |= wxBK_HITTEST_ONLABEL;
778 if ( item == wxNOT_FOUND && GetPageSize().Contains(pt) )
779 *flags |= wxBK_HITTEST_ONPAGE;
780 }
781
782 return item;
783 }
784
785 // ----------------------------------------------------------------------------
786 // flicker-less notebook redraw
787 // ----------------------------------------------------------------------------
788
789 #if USE_NOTEBOOK_ANTIFLICKER
790
791 // wnd proc for the spin button
792 LRESULT APIENTRY _EXPORT wxNotebookSpinBtnWndProc(HWND hwnd,
793 UINT message,
794 WPARAM wParam,
795 LPARAM lParam)
796 {
797 if ( message == WM_ERASEBKGND )
798 return 0;
799
800 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn,
801 hwnd, message, wParam, lParam);
802 }
803
804 LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd,
805 UINT message,
806 WPARAM wParam,
807 LPARAM lParam)
808 {
809 return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook,
810 hwnd, message, wParam, lParam);
811 }
812
813 void wxNotebook::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
814 {
815 // do nothing here
816 }
817
818 void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event))
819 {
820 wxPaintDC dc(this);
821 wxMemoryDC memdc;
822 RECT rc;
823 ::GetClientRect(GetHwnd(), &rc);
824 wxBitmap bmp(rc.right, rc.bottom);
825 memdc.SelectObject(bmp);
826
827 const wxLayoutDirection dir = dc.GetLayoutDirection();
828 memdc.SetLayoutDirection(dir);
829
830 // if there is no special brush just use the solid background colour
831 #if wxUSE_UXTHEME
832 HBRUSH hbr = (HBRUSH)m_hbrBackground;
833 #else
834 HBRUSH hbr = 0;
835 #endif
836 wxBrush brush;
837 if ( !hbr )
838 {
839 brush = wxBrush(GetBackgroundColour());
840 hbr = GetHbrushOf(brush);
841 }
842
843 wxMSWDCImpl *impl = (wxMSWDCImpl*) memdc.GetImpl();
844
845 ::FillRect(GetHdcOf(*impl), &rc, hbr);
846
847 MSWDefWindowProc(WM_PAINT, (WPARAM)(impl->GetHDC()), 0);
848
849 // For some reason in RTL mode, source offset has to be -1, otherwise the
850 // right border (physical) remains unpainted.
851 const wxCoord ofs = dir == wxLayout_RightToLeft ? -1 : 0;
852 dc.Blit(ofs, 0, rc.right, rc.bottom, &memdc, ofs, 0);
853 }
854
855 #endif // USE_NOTEBOOK_ANTIFLICKER
856
857 // ----------------------------------------------------------------------------
858 // wxNotebook callbacks
859 // ----------------------------------------------------------------------------
860
861 void wxNotebook::OnSize(wxSizeEvent& event)
862 {
863 if ( GetPageCount() == 0 )
864 {
865 // Prevents droppings on resize, but does cause some flicker
866 // when there are no pages.
867 Refresh();
868 event.Skip();
869 return;
870 }
871 #ifndef __WXWINCE__
872 else
873 {
874 // Without this, we can sometimes get droppings at the edges
875 // of a notebook, for example a notebook in a splitter window.
876 // This needs to be reconciled with the RefreshRect calls
877 // at the end of this function, which weren't enough to prevent
878 // the droppings.
879
880 wxSize sz = GetClientSize();
881
882 // Refresh right side
883 wxRect rect(sz.x-4, 0, 4, sz.y);
884 RefreshRect(rect);
885
886 // Refresh bottom side
887 rect = wxRect(0, sz.y-4, sz.x, 4);
888 RefreshRect(rect);
889
890 // Refresh left side
891 rect = wxRect(0, 0, 4, sz.y);
892 RefreshRect(rect);
893 }
894 #endif // !__WXWINCE__
895
896 // fit all the notebook pages to the tab control's display area
897
898 RECT rc;
899 rc.left = rc.top = 0;
900 GetSize((int *)&rc.right, (int *)&rc.bottom);
901
902 // save the total size, we'll use it below
903 int widthNbook = rc.right - rc.left,
904 heightNbook = rc.bottom - rc.top;
905
906 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
907 // returns completely false values for multiline tab controls after the tabs
908 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
909 //
910 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
911 //
912 // and the only work around I could find was this ugly hack... without it
913 // simply toggling the "multiline" checkbox in the notebook sample resulted
914 // in a noticeable page displacement
915 if ( HasFlag(wxNB_MULTILINE) )
916 {
917 // avoid an infinite recursion: we get another notification too!
918 static bool s_isInOnSize = false;
919
920 if ( !s_isInOnSize )
921 {
922 s_isInOnSize = true;
923 SendMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED,
924 MAKELPARAM(rc.right, rc.bottom));
925 s_isInOnSize = false;
926 }
927
928 // The best size depends on the number of rows of tabs, which can
929 // change when the notepad is resized.
930 InvalidateBestSize();
931 }
932
933 #if wxUSE_UXTHEME
934 // background bitmap size has changed, update the brush using it too
935 UpdateBgBrush();
936 #endif // wxUSE_UXTHEME
937
938 TabCtrl_AdjustRect(GetHwnd(), false, &rc);
939
940 int width = rc.right - rc.left,
941 height = rc.bottom - rc.top;
942 size_t nCount = m_pages.Count();
943 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
944 wxNotebookPage *pPage = m_pages[nPage];
945 pPage->SetSize(rc.left, rc.top, width, height);
946 }
947
948
949 // unless we had already repainted everything, we now need to refresh
950 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
951 {
952 // invalidate areas not covered by pages
953 RefreshRect(wxRect(0, 0, widthNbook, rc.top), false);
954 RefreshRect(wxRect(0, rc.top, rc.left, height), false);
955 RefreshRect(wxRect(0, rc.bottom, widthNbook, heightNbook - rc.bottom),
956 false);
957 RefreshRect(wxRect(rc.right, rc.top, widthNbook - rc.right, height),
958 false);
959 }
960
961 #if USE_NOTEBOOK_ANTIFLICKER
962 // subclass the spin control used by the notebook to scroll pages to
963 // prevent it from flickering on resize
964 if ( !m_hasSubclassedUpdown )
965 {
966 // iterate over all child windows to find spin button
967 for ( HWND child = ::GetWindow(GetHwnd(), GW_CHILD);
968 child;
969 child = ::GetWindow(child, GW_HWNDNEXT) )
970 {
971 wxWindow *childWindow = wxFindWinFromHandle((WXHWND)child);
972
973 // see if it exists, if no wxWindow found then assume it's the spin
974 // btn
975 if ( !childWindow )
976 {
977 // subclass the spin button to override WM_ERASEBKGND
978 if ( !gs_wndprocNotebookSpinBtn )
979 gs_wndprocNotebookSpinBtn = (WXFARPROC)wxGetWindowProc(child);
980
981 wxSetWindowProc(child, wxNotebookSpinBtnWndProc);
982 m_hasSubclassedUpdown = true;
983 break;
984 }
985 }
986 }
987
988 // Probably because of the games we play above to avoid flicker sometimes
989 // the text controls inside notebook pages are not shown correctly (they
990 // don't have their borders) when the notebook is shown for the first time.
991 // It's not really clear why does this happen and maybe the bug is in
992 // wxTextCtrl itself and not here but updating the page when it's about to
993 // be shown doesn't cost much and works around the problem so do it here
994 // for now.
995 if ( !m_doneUpdateHack && IsShownOnScreen() )
996 {
997 m_doneUpdateHack = true;
998 wxWindow* const page = GetCurrentPage();
999 if ( page )
1000 page->Update();
1001 }
1002 #endif // USE_NOTEBOOK_ANTIFLICKER
1003
1004 event.Skip();
1005 }
1006
1007 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
1008 {
1009 if ( event.IsWindowChange() ) {
1010 // change pages
1011 AdvanceSelection(event.GetDirection());
1012 }
1013 else {
1014 // we get this event in 3 cases
1015 //
1016 // a) one of our pages might have generated it because the user TABbed
1017 // out from it in which case we should propagate the event upwards and
1018 // our parent will take care of setting the focus to prev/next sibling
1019 //
1020 // or
1021 //
1022 // b) the parent panel wants to give the focus to us so that we
1023 // forward it to our selected page. We can't deal with this in
1024 // OnSetFocus() because we don't know which direction the focus came
1025 // from in this case and so can't choose between setting the focus to
1026 // first or last panel child
1027 //
1028 // or
1029 //
1030 // c) we ourselves (see MSWTranslateMessage) generated the event
1031 //
1032 wxWindow * const parent = GetParent();
1033
1034 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
1035 const bool isFromParent = event.GetEventObject() == (wxObject*) parent;
1036 const bool isFromSelf = event.GetEventObject() == (wxObject*) this;
1037 const bool isForward = event.GetDirection();
1038
1039 if ( isFromSelf && !isForward )
1040 {
1041 // focus is currently on notebook tab and should leave
1042 // it backwards (Shift-TAB)
1043 event.SetCurrentFocus(this);
1044 parent->HandleWindowEvent(event);
1045 }
1046 else if ( isFromParent || isFromSelf )
1047 {
1048 // no, it doesn't come from child, case (b) or (c): forward to a
1049 // page but only if entering notebook page (i.e. direction is
1050 // backwards (Shift-TAB) comething from out-of-notebook, or
1051 // direction is forward (TAB) from ourselves),
1052 if ( m_selection != wxNOT_FOUND &&
1053 (!event.GetDirection() || isFromSelf) )
1054 {
1055 // so that the page knows that the event comes from it's parent
1056 // and is being propagated downwards
1057 event.SetEventObject(this);
1058
1059 wxWindow *page = m_pages[m_selection];
1060 if ( !page->HandleWindowEvent(event) )
1061 {
1062 page->SetFocus();
1063 }
1064 //else: page manages focus inside it itself
1065 }
1066 else // otherwise set the focus to the notebook itself
1067 {
1068 SetFocus();
1069 }
1070 }
1071 else
1072 {
1073 // it comes from our child, case (a), pass to the parent, but only
1074 // if the direction is forwards. Otherwise set the focus to the
1075 // notebook itself. The notebook is always the 'first' control of a
1076 // page.
1077 if ( !isForward )
1078 {
1079 SetFocus();
1080 }
1081 else if ( parent )
1082 {
1083 event.SetCurrentFocus(this);
1084 parent->HandleWindowEvent(event);
1085 }
1086 }
1087 }
1088 }
1089
1090 #if wxUSE_UXTHEME
1091
1092 bool wxNotebook::DoDrawBackground(WXHDC hDC, wxWindow *child)
1093 {
1094 wxUxThemeHandle theme(child ? child : this, L"TAB");
1095 if ( !theme )
1096 return false;
1097
1098 // get the notebook client rect (we're not interested in drawing tabs
1099 // themselves)
1100 wxRect r = GetPageSize();
1101 if ( r.IsEmpty() )
1102 return false;
1103
1104 RECT rc;
1105 wxCopyRectToRECT(r, rc);
1106
1107 // map rect to the coords of the window we're drawing in
1108 if ( child )
1109 ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2);
1110
1111 // we have the content area (page size), but we need to draw all of the
1112 // background for it to be aligned correctly
1113 wxUxThemeEngine::Get()->GetThemeBackgroundExtent
1114 (
1115 theme,
1116 (HDC) hDC,
1117 9 /* TABP_PANE */,
1118 0,
1119 &rc,
1120 &rc
1121 );
1122 wxUxThemeEngine::Get()->DrawThemeBackground
1123 (
1124 theme,
1125 (HDC) hDC,
1126 9 /* TABP_PANE */,
1127 0,
1128 &rc,
1129 NULL
1130 );
1131
1132 return true;
1133 }
1134
1135 WXHBRUSH wxNotebook::QueryBgBitmap()
1136 {
1137 wxRect r = GetPageSize();
1138 if ( r.IsEmpty() )
1139 return 0;
1140
1141 WindowHDC hDC(GetHwnd());
1142 MemoryHDC hDCMem(hDC);
1143 CompatibleBitmap hBmp(hDC, r.x + r.width, r.y + r.height);
1144
1145 SelectInHDC selectBmp(hDCMem, hBmp);
1146
1147 if ( !DoDrawBackground((WXHDC)(HDC)hDCMem) )
1148 return 0;
1149
1150 return (WXHBRUSH)::CreatePatternBrush(hBmp);
1151 }
1152
1153 void wxNotebook::UpdateBgBrush()
1154 {
1155 if ( m_hbrBackground )
1156 ::DeleteObject((HBRUSH)m_hbrBackground);
1157
1158 if ( !m_hasBgCol && wxUxThemeEngine::GetIfActive() )
1159 {
1160 m_hbrBackground = QueryBgBitmap();
1161 }
1162 else // no themes or we've got user-defined solid colour
1163 {
1164 m_hbrBackground = NULL;
1165 }
1166 }
1167
1168 bool wxNotebook::MSWPrintChild(WXHDC hDC, wxWindow *child)
1169 {
1170 // solid background colour overrides themed background drawing
1171 if ( !UseBgCol() && DoDrawBackground(hDC, child) )
1172 return true;
1173
1174 // If we're using a solid colour (for example if we've switched off
1175 // theming for this notebook), paint it
1176 if (UseBgCol())
1177 {
1178 wxRect r = GetPageSize();
1179 if ( r.IsEmpty() )
1180 return false;
1181
1182 RECT rc;
1183 wxCopyRectToRECT(r, rc);
1184
1185 // map rect to the coords of the window we're drawing in
1186 if ( child )
1187 ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2);
1188
1189 wxBrush brush(GetBackgroundColour());
1190 HBRUSH hbr = GetHbrushOf(brush);
1191
1192 ::FillRect((HDC) hDC, &rc, hbr);
1193
1194 return true;
1195 }
1196
1197 return wxNotebookBase::MSWPrintChild(hDC, child);
1198 }
1199
1200 #endif // wxUSE_UXTHEME
1201
1202 // Windows only: attempts to get colour for UX theme page background
1203 wxColour wxNotebook::GetThemeBackgroundColour() const
1204 {
1205 #if wxUSE_UXTHEME
1206 if (wxUxThemeEngine::Get())
1207 {
1208 wxUxThemeHandle hTheme((wxNotebook*) this, L"TAB");
1209 if (hTheme)
1210 {
1211 // This is total guesswork.
1212 // See PlatformSDK\Include\Tmschema.h for values.
1213 // JACS: can also use 9 (TABP_PANE)
1214 COLORREF themeColor;
1215 bool success = (S_OK == wxUxThemeEngine::Get()->GetThemeColor(
1216 hTheme,
1217 10 /* TABP_BODY */,
1218 1 /* NORMAL */,
1219 3821 /* FILLCOLORHINT */,
1220 &themeColor));
1221 if (!success)
1222 return GetBackgroundColour();
1223
1224 /*
1225 [DS] Workaround for WindowBlinds:
1226 Some themes return a near black theme color using FILLCOLORHINT,
1227 this makes notebook pages have an ugly black background and makes
1228 text (usually black) unreadable. Retry again with FILLCOLOR.
1229
1230 This workaround potentially breaks appearance of some themes,
1231 but in practice it already fixes some themes.
1232 */
1233 if (themeColor == 1)
1234 {
1235 wxUxThemeEngine::Get()->GetThemeColor(
1236 hTheme,
1237 10 /* TABP_BODY */,
1238 1 /* NORMAL */,
1239 3802 /* FILLCOLOR */,
1240 &themeColor);
1241 }
1242
1243 wxColour colour = wxRGBToColour(themeColor);
1244
1245 // Under Vista, the tab background colour is reported incorrectly.
1246 // So for the default theme at least, hard-code the colour to something
1247 // that will blend in.
1248
1249 static int s_AeroStatus = -1;
1250 if (s_AeroStatus == -1)
1251 {
1252 WCHAR szwThemeFile[1024];
1253 WCHAR szwThemeColor[256];
1254 if (S_OK == wxUxThemeEngine::Get()->GetCurrentThemeName(szwThemeFile, 1024, szwThemeColor, 256, NULL, 0))
1255 {
1256 wxString themeFile(szwThemeFile), themeColor(szwThemeColor);
1257 if (themeFile.Find(wxT("Aero")) != -1 && themeColor == wxT("NormalColor"))
1258 s_AeroStatus = 1;
1259 else
1260 s_AeroStatus = 0;
1261 }
1262 else
1263 s_AeroStatus = 0;
1264 }
1265
1266 if (s_AeroStatus == 1)
1267 colour = wxColour(255, 255, 255);
1268
1269 return colour;
1270 }
1271 }
1272 #endif // wxUSE_UXTHEME
1273
1274 return GetBackgroundColour();
1275 }
1276
1277 // ----------------------------------------------------------------------------
1278 // wxNotebook base class virtuals
1279 // ----------------------------------------------------------------------------
1280
1281 #if wxUSE_CONSTRAINTS
1282
1283 // override these 2 functions to do nothing: everything is done in OnSize
1284
1285 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
1286 {
1287 // don't set the sizes of the pages - their correct size is not yet known
1288 wxControl::SetConstraintSizes(false);
1289 }
1290
1291 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
1292 {
1293 return true;
1294 }
1295
1296 #endif // wxUSE_CONSTRAINTS
1297
1298 // ----------------------------------------------------------------------------
1299 // wxNotebook Windows message handlers
1300 // ----------------------------------------------------------------------------
1301
1302 bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
1303 WXWORD pos, WXHWND control)
1304 {
1305 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
1306 // up-down control
1307 if ( control )
1308 return false;
1309
1310 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
1311 }
1312
1313 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
1314 {
1315 wxBookCtrlEvent event(wxEVT_NULL, m_windowId);
1316
1317 NMHDR* hdr = (NMHDR *)lParam;
1318 switch ( hdr->code ) {
1319 case TCN_SELCHANGE:
1320 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
1321 break;
1322
1323 case TCN_SELCHANGING:
1324 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
1325 break;
1326
1327 default:
1328 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1329 }
1330
1331 event.SetSelection(TabCtrl_GetCurSel(GetHwnd()));
1332 event.SetOldSelection(m_selection);
1333 event.SetEventObject(this);
1334 event.SetInt(idCtrl);
1335
1336 // Change the selection before generating the event as its handler should
1337 // already see the new page selected.
1338 if ( hdr->code == TCN_SELCHANGE )
1339 UpdateSelection(event.GetSelection());
1340
1341 bool processed = HandleWindowEvent(event);
1342 *result = !event.IsAllowed();
1343 return processed;
1344 }
1345
1346 #endif // wxUSE_NOTEBOOK