]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
eVC3 warning fix.
[wxWidgets.git] / src / msw / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "notebook.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_NOTEBOOK
24
25 // wxWidgets
26 #ifndef WX_PRECOMP
27 #include "wx/string.h"
28 #include "wx/dc.h"
29 #endif // WX_PRECOMP
30
31 #include "wx/log.h"
32 #include "wx/imaglist.h"
33 #include "wx/event.h"
34 #include "wx/control.h"
35 #include "wx/notebook.h"
36 #include "wx/app.h"
37
38 #include "wx/msw/private.h"
39
40 // Windows standard headers
41 #ifndef __WIN95__
42 #error "wxNotebook is only supported Windows 95 and above"
43 #endif //Win95
44
45 #include <windowsx.h> // for SetWindowFont
46
47 #ifdef __GNUWIN32_OLD__
48 #include "wx/msw/gnuwin32/extra.h"
49 #endif
50
51 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
52 #include <commctrl.h>
53 #endif
54
55 #include "wx/msw/winundef.h"
56
57 #if wxUSE_UXTHEME
58 #include "wx/msw/uxtheme.h"
59 #endif
60
61 // ----------------------------------------------------------------------------
62 // macros
63 // ----------------------------------------------------------------------------
64
65 // check that the page index is valid
66 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
67
68 // hide the ugly cast
69 #define m_hwnd (HWND)GetHWND()
70
71 // ----------------------------------------------------------------------------
72 // constants
73 // ----------------------------------------------------------------------------
74
75 // This is a work-around for missing defines in gcc-2.95 headers
76 #ifndef TCS_RIGHT
77 #define TCS_RIGHT 0x0002
78 #endif
79
80 #ifndef TCS_VERTICAL
81 #define TCS_VERTICAL 0x0080
82 #endif
83
84 #ifndef TCS_BOTTOM
85 #define TCS_BOTTOM TCS_RIGHT
86 #endif
87
88 // ----------------------------------------------------------------------------
89 // event table
90 // ----------------------------------------------------------------------------
91
92 #include <wx/listimpl.cpp>
93
94 WX_DEFINE_LIST( wxNotebookPageInfoList ) ;
95
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
98
99 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
100 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
101
102 EVT_SIZE(wxNotebook::OnSize)
103
104 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
105 END_EVENT_TABLE()
106
107 #if wxUSE_EXTENDED_RTTI
108 WX_DEFINE_FLAGS( wxNotebookStyle )
109
110 wxBEGIN_FLAGS( wxNotebookStyle )
111 // new style border flags, we put them first to
112 // use them for streaming out
113 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
114 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
115 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
116 wxFLAGS_MEMBER(wxBORDER_RAISED)
117 wxFLAGS_MEMBER(wxBORDER_STATIC)
118 wxFLAGS_MEMBER(wxBORDER_NONE)
119
120 // old style border flags
121 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
122 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
123 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
124 wxFLAGS_MEMBER(wxRAISED_BORDER)
125 wxFLAGS_MEMBER(wxSTATIC_BORDER)
126 wxFLAGS_MEMBER(wxBORDER)
127
128 // standard window styles
129 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
130 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
131 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
132 wxFLAGS_MEMBER(wxWANTS_CHARS)
133 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
134 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
135 wxFLAGS_MEMBER(wxVSCROLL)
136 wxFLAGS_MEMBER(wxHSCROLL)
137
138 wxFLAGS_MEMBER(wxNB_FIXEDWIDTH)
139 wxFLAGS_MEMBER(wxNB_LEFT)
140 wxFLAGS_MEMBER(wxNB_RIGHT)
141 wxFLAGS_MEMBER(wxNB_BOTTOM)
142
143 wxEND_FLAGS( wxNotebookStyle )
144
145 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h")
146 IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" )
147
148 wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ;
149
150 template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
151 {
152 wxListCollectionToVariantArray<wxNotebookPageInfoList::compatibility_iterator>( theList , value ) ;
153 }
154
155 wxBEGIN_PROPERTIES_TABLE(wxNotebook)
156 wxEVENT_PROPERTY( PageChanging , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING , wxNotebookEvent )
157 wxEVENT_PROPERTY( PageChanged , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED , wxNotebookEvent )
158
159 wxPROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
160 wxPROPERTY_FLAGS( WindowStyle , wxNotebookStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
161 wxEND_PROPERTIES_TABLE()
162
163 wxBEGIN_HANDLERS_TABLE(wxNotebook)
164 wxEND_HANDLERS_TABLE()
165
166 wxCONSTRUCTOR_5( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle)
167
168
169 wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
170 wxREADONLY_PROPERTY( Page , wxNotebookPage* , GetPage , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
171 wxREADONLY_PROPERTY( Text , wxString , GetText , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
172 wxREADONLY_PROPERTY( Selected , bool , GetSelected , false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
173 wxREADONLY_PROPERTY( ImageId , int , GetImageId , -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
174 wxEND_PROPERTIES_TABLE()
175
176 wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
177 wxEND_HANDLERS_TABLE()
178
179 wxCONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
180
181 #else
182 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
183 IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
184 #endif
185 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
186
187 // ============================================================================
188 // implementation
189 // ============================================================================
190
191 // ----------------------------------------------------------------------------
192 // wxNotebook construction
193 // ----------------------------------------------------------------------------
194
195 const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const
196 {
197 wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ;
198 WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ;
199 for( size_t i = 0 ; i < GetPageCount() ; ++i )
200 {
201 wxNotebookPageInfo *info = new wxNotebookPageInfo() ;
202 info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ;
203 list->Append( info ) ;
204 }
205 return m_pageInfos ;
206 }
207
208 // common part of all ctors
209 void wxNotebook::Init()
210 {
211 m_imageList = NULL;
212 m_nSelection = -1;
213
214 #if wxUSE_UXTHEME
215 m_hbrBackground = NULL;
216 #endif // wxUSE_UXTHEME
217 }
218
219 // default for dynamic class
220 wxNotebook::wxNotebook()
221 {
222 Init();
223 }
224
225 // the same arguments as for wxControl
226 wxNotebook::wxNotebook(wxWindow *parent,
227 wxWindowID id,
228 const wxPoint& pos,
229 const wxSize& size,
230 long style,
231 const wxString& name)
232 {
233 Init();
234
235 Create(parent, id, pos, size, style, name);
236 }
237
238 // Create() function
239 bool wxNotebook::Create(wxWindow *parent,
240 wxWindowID id,
241 const wxPoint& pos,
242 const wxSize& size,
243 long style,
244 const wxString& name)
245 {
246 // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the
247 // control is simply not rendered correctly), so disable them in this case
248 const int verComCtl32 = wxApp::GetComCtl32Version();
249 if ( verComCtl32 == 600 )
250 {
251 // check if we use themes at all -- if we don't, we're still ok
252 #if wxUSE_UXTHEME
253 if ( wxUxThemeEngine::GetIfActive() )
254 #endif
255 {
256 style &= ~(wxNB_BOTTOM | wxNB_LEFT | wxNB_RIGHT);
257 }
258 }
259
260 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
261 wxDefaultValidator, name) )
262 return false;
263
264 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
265 return false;
266
267 return true;
268 }
269
270 WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
271 {
272 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
273
274 tabStyle |= WS_TABSTOP | TCS_TABS;
275
276 if ( style & wxNB_MULTILINE )
277 tabStyle |= TCS_MULTILINE;
278 if ( style & wxNB_FIXEDWIDTH )
279 tabStyle |= TCS_FIXEDWIDTH;
280
281 if ( style & wxNB_BOTTOM )
282 tabStyle |= TCS_RIGHT;
283 else if ( style & wxNB_LEFT )
284 tabStyle |= TCS_VERTICAL;
285 else if ( style & wxNB_RIGHT )
286 tabStyle |= TCS_VERTICAL | TCS_RIGHT;
287
288 // ex style
289 if ( exstyle )
290 {
291 // note that we never want to have the default WS_EX_CLIENTEDGE style
292 // as it looks too ugly for the notebooks
293 *exstyle = 0;
294 }
295
296 return tabStyle;
297 }
298
299 wxNotebook::~wxNotebook()
300 {
301 #if wxUSE_UXTHEME
302 if ( m_hbrBackground )
303 ::DeleteObject((HBRUSH)m_hbrBackground);
304 #endif // wxUSE_UXTHEME
305 }
306
307 // ----------------------------------------------------------------------------
308 // wxNotebook accessors
309 // ----------------------------------------------------------------------------
310
311 size_t wxNotebook::GetPageCount() const
312 {
313 // consistency check
314 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
315
316 return m_pages.Count();
317 }
318
319 int wxNotebook::GetRowCount() const
320 {
321 return TabCtrl_GetRowCount(m_hwnd);
322 }
323
324 int wxNotebook::SetSelection(size_t nPage)
325 {
326 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
327
328 if ( int(nPage) != m_nSelection )
329 {
330 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
331 event.SetSelection(nPage);
332 event.SetOldSelection(m_nSelection);
333 event.SetEventObject(this);
334 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
335 {
336 // program allows the page change
337 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
338 (void)GetEventHandler()->ProcessEvent(event);
339
340 TabCtrl_SetCurSel(m_hwnd, nPage);
341 }
342 }
343
344 return m_nSelection;
345 }
346
347 bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
348 {
349 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
350
351 TC_ITEM tcItem;
352 tcItem.mask = TCIF_TEXT;
353 tcItem.pszText = (wxChar *)strText.c_str();
354
355 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
356 }
357
358 wxString wxNotebook::GetPageText(size_t nPage) const
359 {
360 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
361
362 wxChar buf[256];
363 TC_ITEM tcItem;
364 tcItem.mask = TCIF_TEXT;
365 tcItem.pszText = buf;
366 tcItem.cchTextMax = WXSIZEOF(buf);
367
368 wxString str;
369 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
370 str = tcItem.pszText;
371
372 return str;
373 }
374
375 int wxNotebook::GetPageImage(size_t nPage) const
376 {
377 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
378
379 TC_ITEM tcItem;
380 tcItem.mask = TCIF_IMAGE;
381
382 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
383 }
384
385 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
386 {
387 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
388
389 TC_ITEM tcItem;
390 tcItem.mask = TCIF_IMAGE;
391 tcItem.iImage = nImage;
392
393 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
394 }
395
396 void wxNotebook::SetImageList(wxImageList* imageList)
397 {
398 wxNotebookBase::SetImageList(imageList);
399
400 if ( imageList )
401 {
402 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
403 }
404 }
405
406 // ----------------------------------------------------------------------------
407 // wxNotebook size settings
408 // ----------------------------------------------------------------------------
409
410 void wxNotebook::SetPageSize(const wxSize& size)
411 {
412 // transform the page size into the notebook size
413 RECT rc;
414 rc.left =
415 rc.top = 0;
416 rc.right = size.x;
417 rc.bottom = size.y;
418
419 TabCtrl_AdjustRect(GetHwnd(), true, &rc);
420
421 // and now set it
422 SetSize(rc.right - rc.left, rc.bottom - rc.top);
423 }
424
425 void wxNotebook::SetPadding(const wxSize& padding)
426 {
427 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
428 }
429
430 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
431 // style.
432 void wxNotebook::SetTabSize(const wxSize& sz)
433 {
434 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
435 }
436
437 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
438 {
439 wxSize sizeTotal = sizePage;
440
441 // We need to make getting tab size part of the wxWidgets API.
442 wxSize tabSize(0, 0);
443 if (GetPageCount() > 0)
444 {
445 RECT rect;
446 TabCtrl_GetItemRect((HWND) GetHWND(), 0, & rect);
447 tabSize.x = rect.right - rect.left;
448 tabSize.y = rect.bottom - rect.top;
449 }
450 if ( HasFlag(wxNB_LEFT) || HasFlag(wxNB_RIGHT) )
451 {
452 sizeTotal.x += tabSize.x + 7;
453 sizeTotal.y += 7;
454 }
455 else
456 {
457 sizeTotal.x += 7;
458 sizeTotal.y += tabSize.y + 7;
459 }
460
461 return sizeTotal;
462 }
463
464 void wxNotebook::AdjustPageSize(wxNotebookPage *page)
465 {
466 wxCHECK_RET( page, _T("NULL page in wxNotebook::AdjustPageSize") );
467
468 RECT rc;
469 rc.left =
470 rc.top = 0;
471
472 // get the page size from the notebook size
473 GetSize((int *)&rc.right, (int *)&rc.bottom);
474
475 // This check is to work around a bug in TabCtrl_AdjustRect which will
476 // cause a crash on win2k, or on XP with themes disabled, if the
477 // wxNB_MULTILINE style is used and the rectangle is very small, (such as
478 // when the notebook is first created.) The value of 20 is just
479 // arbitrarily chosen, if there is a better way to determine this value
480 // then please do so. --RD
481 if (rc.right > 20 && rc.bottom > 20)
482 {
483 TabCtrl_AdjustRect(m_hwnd, false, &rc);
484 page->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
485 }
486 }
487
488 // ----------------------------------------------------------------------------
489 // wxNotebook operations
490 // ----------------------------------------------------------------------------
491
492 // remove one page from the notebook, without deleting
493 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
494 {
495 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
496 if ( !pageRemoved )
497 return NULL;
498
499 TabCtrl_DeleteItem(m_hwnd, nPage);
500
501 if ( m_pages.IsEmpty() )
502 {
503 // no selection any more, the notebook becamse empty
504 m_nSelection = -1;
505 }
506 else // notebook still not empty
507 {
508 int selNew = TabCtrl_GetCurSel(m_hwnd);
509 if (selNew != -1)
510 {
511 // No selection change, just refresh the current selection.
512 // Because it could be that the slection index changed
513 // we need to update it.
514 // Note: this does not mean the selection it self changed.
515 m_nSelection = selNew;
516 m_pages[m_nSelection]->Refresh();
517 }
518 else if (int(nPage) == m_nSelection)
519 {
520 // The selection was deleted.
521
522 // Determine new selection.
523 if (m_nSelection == int(GetPageCount()))
524 selNew = m_nSelection - 1;
525 else
526 selNew = m_nSelection;
527
528 // m_nSelection must be always valid so reset it before calling
529 // SetSelection()
530 m_nSelection = -1;
531 SetSelection(selNew);
532 }
533 else
534 {
535 wxFAIL; // Windows did not behave ok.
536 }
537 }
538
539 return pageRemoved;
540 }
541
542 // remove all pages
543 bool wxNotebook::DeleteAllPages()
544 {
545 size_t nPageCount = GetPageCount();
546 size_t nPage;
547 for ( nPage = 0; nPage < nPageCount; nPage++ )
548 delete m_pages[nPage];
549
550 m_pages.Clear();
551
552 TabCtrl_DeleteAllItems(m_hwnd);
553
554 m_nSelection = -1;
555
556 InvalidateBestSize();
557 return true;
558 }
559
560 // same as AddPage() but does it at given position
561 bool wxNotebook::InsertPage(size_t nPage,
562 wxNotebookPage *pPage,
563 const wxString& strText,
564 bool bSelect,
565 int imageId)
566 {
567 wxCHECK_MSG( pPage != NULL, false, _T("NULL page in wxNotebook::InsertPage") );
568 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false,
569 _T("invalid index in wxNotebook::InsertPage") );
570
571 wxASSERT_MSG( pPage->GetParent() == this,
572 _T("notebook pages must have notebook as parent") );
573
574 // add a new tab to the control
575 // ----------------------------
576
577 // init all fields to 0
578 TC_ITEM tcItem;
579 wxZeroMemory(tcItem);
580
581 // set the image, if any
582 if ( imageId != -1 )
583 {
584 tcItem.mask |= TCIF_IMAGE;
585 tcItem.iImage = imageId;
586 }
587
588 // and the text
589 if ( !strText.IsEmpty() )
590 {
591 tcItem.mask |= TCIF_TEXT;
592 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
593 }
594
595 // hide the page: unless it is selected, it shouldn't be shown (and if it
596 // is selected it will be shown later)
597 HWND hwnd = GetWinHwnd(pPage);
598 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
599
600 // this updates internal flag too -- otherwise it would get out of sync
601 // with the real state
602 pPage->Show(false);
603
604
605 // fit the notebook page to the tab control's display area: this should be
606 // done before adding it to the notebook or TabCtrl_InsertItem() will
607 // change the notebooks size itself!
608 AdjustPageSize(pPage);
609
610 // finally do insert it
611 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
612 {
613 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
614
615 return false;
616 }
617
618 // succeeded: save the pointer to the page
619 m_pages.Insert(pPage, nPage);
620
621 // we may need to adjust the size again if the notebook size changed:
622 // normally this only happens for the first page we add (the tabs which
623 // hadn't been there before are now shown) but for a multiline notebook it
624 // can happen for any page at all as a new row could have been started
625 if ( m_pages.GetCount() == 1 || HasFlag(wxNB_MULTILINE) )
626 {
627 AdjustPageSize(pPage);
628 }
629
630 // now deal with the selection
631 // ---------------------------
632
633 // if the inserted page is before the selected one, we must update the
634 // index of the selected page
635 if ( int(nPage) <= m_nSelection )
636 {
637 // one extra page added
638 m_nSelection++;
639 }
640
641 // some page should be selected: either this one or the first one if there
642 // is still no selection
643 int selNew = -1;
644 if ( bSelect )
645 selNew = nPage;
646 else if ( m_nSelection == -1 )
647 selNew = 0;
648
649 if ( selNew != -1 )
650 SetSelection(selNew);
651
652 InvalidateBestSize();
653 return true;
654 }
655
656 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
657 {
658 TC_HITTESTINFO hitTestInfo;
659 hitTestInfo.pt.x = pt.x;
660 hitTestInfo.pt.y = pt.y;
661 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
662
663 if ( flags )
664 {
665 *flags = 0;
666
667 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
668 *flags |= wxNB_HITTEST_NOWHERE;
669 if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM)
670 *flags |= wxNB_HITTEST_ONITEM;
671 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
672 *flags |= wxNB_HITTEST_ONICON;
673 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
674 *flags |= wxNB_HITTEST_ONLABEL;
675 }
676
677 return item;
678 }
679
680
681 // ----------------------------------------------------------------------------
682 // wxNotebook callbacks
683 // ----------------------------------------------------------------------------
684
685 void wxNotebook::OnSize(wxSizeEvent& event)
686 {
687 // fit the notebook page to the tab control's display area
688 RECT rc;
689 rc.left = rc.top = 0;
690 GetSize((int *)&rc.right, (int *)&rc.bottom);
691
692 // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it
693 // returns completely false values for multiline tab controls after the tabs
694 // are added but before getting the first WM_SIZE (off by ~50 pixels, see
695 //
696 // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863
697 //
698 // and the only work around I could find was this ugly hack... without it
699 // simply toggling the "multiline" checkbox in the notebook sample resulted
700 // in a noticeable page displacement
701 if ( HasFlag(wxNB_MULTILINE) )
702 {
703 // avoid an infinite recursion: we get another notification too!
704 static bool s_isInOnSize = false;
705
706 if ( !s_isInOnSize )
707 {
708 s_isInOnSize = true;
709 SendMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED,
710 MAKELPARAM(rc.right, rc.bottom));
711 s_isInOnSize = false;
712 }
713 }
714
715 TabCtrl_AdjustRect(m_hwnd, false, &rc);
716
717 int width = rc.right - rc.left,
718 height = rc.bottom - rc.top;
719 size_t nCount = m_pages.Count();
720 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
721 wxNotebookPage *pPage = m_pages[nPage];
722 pPage->SetSize(rc.left, rc.top, width, height);
723 }
724
725 #if wxUSE_UXTHEME
726 UpdateBgBrush();
727 #endif // wxUSE_UXTHEME
728
729 event.Skip();
730 }
731
732 void wxNotebook::OnSelChange(wxNotebookEvent& event)
733 {
734 // is it our tab control?
735 if ( event.GetEventObject() == this )
736 {
737 int sel = event.GetOldSelection();
738 if ( sel != -1 )
739 m_pages[sel]->Show(false);
740
741 sel = event.GetSelection();
742 if ( sel != -1 )
743 {
744 wxNotebookPage *pPage = m_pages[sel];
745 pPage->Show(true);
746 pPage->SetFocus();
747
748 // If the newly focused window is not a child of the new page,
749 // SetFocus was not successful and the notebook itself should be
750 // focused
751 wxWindow *currentFocus = FindFocus();
752 wxWindow *startFocus = currentFocus;
753 while ( currentFocus && currentFocus != pPage && currentFocus != this )
754 currentFocus = currentFocus->GetParent();
755
756 if ( startFocus == pPage || currentFocus != pPage )
757 SetFocus();
758
759 }
760 else // no pages in the notebook, give the focus to itself
761 {
762 SetFocus();
763 }
764
765 m_nSelection = sel;
766 }
767
768 // we want to give others a chance to process this message as well
769 event.Skip();
770 }
771
772 bool wxNotebook::MSWTranslateMessage(WXMSG *wxmsg)
773 {
774 const MSG * const msg = (MSG *)wxmsg;
775
776 // intercept TAB, CTRL+TAB and CTRL+SHIFT+TAB for processing by wxNotebook.
777 // TAB will be passed to the currently selected page, CTRL+TAB and
778 // CTRL+SHIFT+TAB will be processed by the notebook itself. do not
779 // intercept SHIFT+TAB. This goes to the parent of the notebook which will
780 // process it.
781 if ( msg->message == WM_KEYDOWN && msg->wParam == VK_TAB &&
782 msg->hwnd == m_hwnd &&
783 (wxIsCtrlDown() || !wxIsShiftDown()) )
784 {
785 return MSWProcessMessage(wxmsg);
786 }
787
788 return false;
789 }
790
791 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
792 {
793 if ( event.IsWindowChange() ) {
794 // change pages
795 AdvanceSelection(event.GetDirection());
796 }
797 else {
798 // we get this event in 3 cases
799 //
800 // a) one of our pages might have generated it because the user TABbed
801 // out from it in which case we should propagate the event upwards and
802 // our parent will take care of setting the focus to prev/next sibling
803 //
804 // or
805 //
806 // b) the parent panel wants to give the focus to us so that we
807 // forward it to our selected page. We can't deal with this in
808 // OnSetFocus() because we don't know which direction the focus came
809 // from in this case and so can't choose between setting the focus to
810 // first or last panel child
811 //
812 // or
813 //
814 // c) we ourselves (see MSWTranslateMessage) generated the event
815 //
816 wxWindow * const parent = GetParent();
817
818 const bool isFromParent = event.GetEventObject() == parent;
819 const bool isFromSelf = event.GetEventObject() == this;
820
821 if ( isFromParent || isFromSelf )
822 {
823 // no, it doesn't come from child, case (b) or (c): forward to a
824 // page but only if direction is backwards (TAB) or from ourselves,
825 if ( m_nSelection != -1 &&
826 (!event.GetDirection() || isFromSelf) )
827 {
828 // so that the page knows that the event comes from it's parent
829 // and is being propagated downwards
830 event.SetEventObject(this);
831
832 wxWindow *page = m_pages[m_nSelection];
833 if ( !page->GetEventHandler()->ProcessEvent(event) )
834 {
835 page->SetFocus();
836 }
837 //else: page manages focus inside it itself
838 }
839 else // otherwise set the focus to the notebook itself
840 {
841 SetFocus();
842 }
843 }
844 else
845 {
846 // it comes from our child, case (a), pass to the parent, but only
847 // if the direction is forwards. Otherwise set the focus to the
848 // notebook itself. The notebook is always the 'first' control of a
849 // page.
850 if ( !event.GetDirection() )
851 {
852 SetFocus();
853 }
854 else if ( parent )
855 {
856 event.SetCurrentFocus(this);
857 parent->GetEventHandler()->ProcessEvent(event);
858 }
859 }
860 }
861 }
862
863 #if wxUSE_UXTHEME
864
865 WXHANDLE wxNotebook::QueryBgBitmap(wxWindow *win)
866 {
867 RECT rc;
868 GetWindowRect(GetHwnd(), &rc);
869
870 WindowHDC hDC(GetHwnd());
871 MemoryHDC hDCMem(hDC);
872 CompatibleBitmap hBmp(hDC, rc.right - rc.left, rc.bottom - rc.top);
873
874 SelectInHDC selectBmp(hDCMem, hBmp);
875
876 ::SendMessage(GetHwnd(), WM_PRINTCLIENT,
877 (WPARAM)(HDC)hDCMem,
878 PRF_ERASEBKGND | PRF_CLIENT | PRF_NONCLIENT);
879
880 if ( win )
881 {
882 RECT rc2;
883 ::GetWindowRect(GetHwndOf(win), &rc2);
884
885 COLORREF c = ::GetPixel(hDCMem, rc2.left - rc.left, rc2.top - rc.top);
886
887 return (WXHANDLE)c;
888 }
889 else // we are asked to create the brush
890 {
891 return (WXHANDLE)::CreatePatternBrush(hBmp);
892 }
893 }
894
895 void wxNotebook::UpdateBgBrush()
896 {
897 if ( m_hbrBackground )
898 ::DeleteObject((HBRUSH)m_hbrBackground);
899
900 if ( !m_hasBgCol && wxUxThemeEngine::GetIfActive() )
901 {
902 m_hbrBackground = (WXHBRUSH)QueryBgBitmap();
903 }
904 else // no themes
905 {
906 m_hbrBackground = NULL;
907 }
908 }
909
910 WXHBRUSH wxNotebook::MSWGetBgBrushForChild(WXHDC hDC, wxWindow *win)
911 {
912 if ( m_hbrBackground )
913 {
914 // before drawing with the background brush, we need to position it
915 // correctly
916 RECT rc;
917 ::GetWindowRect(GetHwndOf(win), &rc);
918
919 ::MapWindowPoints(NULL, GetHwnd(), (POINT *)&rc, 1);
920
921 if ( !::SetBrushOrgEx((HDC)hDC, -rc.left, -rc.top, NULL) )
922 {
923 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
924 }
925
926 return m_hbrBackground;
927 }
928
929 return wxNotebookBase::MSWGetBgBrushForChild(hDC, win);
930 }
931
932 wxColour wxNotebook::MSWGetBgColourForChild(wxWindow *win)
933 {
934 if ( m_hasBgCol )
935 return GetBackgroundColour();
936
937 if ( !wxUxThemeEngine::GetIfActive() )
938 return wxNullColour;
939
940 COLORREF c = (COLORREF)QueryBgBitmap(win);
941
942 return c == CLR_INVALID ? wxNullColour : wxRGBToColour(c);
943 }
944
945 #endif // wxUSE_UXTHEME
946
947 // ----------------------------------------------------------------------------
948 // wxNotebook base class virtuals
949 // ----------------------------------------------------------------------------
950
951 #if wxUSE_CONSTRAINTS
952
953 // override these 2 functions to do nothing: everything is done in OnSize
954
955 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
956 {
957 // don't set the sizes of the pages - their correct size is not yet known
958 wxControl::SetConstraintSizes(false);
959 }
960
961 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
962 {
963 return true;
964 }
965
966 #endif // wxUSE_CONSTRAINTS
967
968 // ----------------------------------------------------------------------------
969 // wxNotebook Windows message handlers
970 // ----------------------------------------------------------------------------
971
972 bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
973 WXWORD pos, WXHWND control)
974 {
975 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
976 // up-down control
977 if ( control )
978 return false;
979
980 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
981 }
982
983 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
984 {
985 wxNotebookEvent event(wxEVT_NULL, m_windowId);
986
987 NMHDR* hdr = (NMHDR *)lParam;
988 switch ( hdr->code ) {
989 case TCN_SELCHANGE:
990 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
991 break;
992
993 case TCN_SELCHANGING:
994 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
995 break;
996
997 default:
998 return wxControl::MSWOnNotify(idCtrl, lParam, result);
999 }
1000
1001 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
1002 event.SetOldSelection(m_nSelection);
1003 event.SetEventObject(this);
1004 event.SetInt(idCtrl);
1005
1006 bool processed = GetEventHandler()->ProcessEvent(event);
1007 *result = !event.IsAllowed();
1008 return processed;
1009 }
1010
1011 #endif // wxUSE_NOTEBOOK