]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
rewrote and virtualized the background erasing logic: each window may now
[wxWidgets.git] / src / msw / notebook.cpp
CommitLineData
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>
65571936 9// Licence: wxWindows licence
88310e2e
VZ
10///////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
77ffb593 25// wxWidgets
88310e2e 26#ifndef WX_PRECOMP
3096bd2f 27 #include "wx/string.h"
bb180f90 28 #include "wx/dc.h"
88310e2e
VZ
29#endif // WX_PRECOMP
30
3096bd2f
VZ
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"
04eb05b0 36#include "wx/app.h"
88310e2e 37
3096bd2f 38#include "wx/msw/private.h"
88310e2e
VZ
39
40// Windows standard headers
41#ifndef __WIN95__
2432b92d 42 #error "wxNotebook is only supported Windows 95 and above"
88310e2e
VZ
43#endif //Win95
44
aaab7c01
VZ
45#include <windowsx.h> // for SetWindowFont
46
b39dbf34
JS
47#ifdef __GNUWIN32_OLD__
48 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 49#endif
57c208c5 50
b39dbf34 51#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
c42404a5 52 #include <commctrl.h>
88310e2e
VZ
53#endif
54
85b43fbf
JS
55#include "wx/msw/winundef.h"
56
57#if wxUSE_UXTHEME
caf95d2a 58 #include "wx/msw/uxtheme.h"
85b43fbf
JS
59#endif
60
88310e2e
VZ
61// ----------------------------------------------------------------------------
62// macros
63// ----------------------------------------------------------------------------
64
65// check that the page index is valid
8d34bf5c 66#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
88310e2e
VZ
67
68// hide the ugly cast
69#define m_hwnd (HWND)GetHWND()
70
74b31181
VZ
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
88310e2e
VZ
88// ----------------------------------------------------------------------------
89// event table
90// ----------------------------------------------------------------------------
91
51741307
SC
92#include <wx/listimpl.cpp>
93
94WX_DEFINE_LIST( wxNotebookPageInfoList ) ;
95
2e4df4bf
VZ
96DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
97DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
98
d9317fd4 99BEGIN_EVENT_TABLE(wxNotebook, wxControl)
88310e2e
VZ
100 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
101
9026ad85 102 EVT_SIZE(wxNotebook::OnSize)
42e69d6b 103
88310e2e 104 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
d9317fd4 105END_EVENT_TABLE()
88310e2e 106
51596bcb 107#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
108WX_DEFINE_FLAGS( wxNotebookStyle )
109
3ff066a4 110wxBEGIN_FLAGS( wxNotebookStyle )
bc9fb572
JS
111 // new style border flags, we put them first to
112 // use them for streaming out
3ff066a4
SC
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)
078cf5cb 119
bc9fb572 120 // old style border flags
3ff066a4
SC
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)
cb0afb26 126 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
127
128 // standard window styles
3ff066a4
SC
129 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
130 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
131 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
132 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 133 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
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
143wxEND_FLAGS( wxNotebookStyle )
51741307 144
51596bcb 145IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h")
51741307
SC
146IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" )
147
3ff066a4 148wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ;
51596bcb 149
f0a126fe
SC
150template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
151{
0c6b0084 152 wxListCollectionToVariantArray<wxNotebookPageInfoList::compatibility_iterator>( theList , value ) ;
f0a126fe
SC
153}
154
3ff066a4
SC
155wxBEGIN_PROPERTIES_TABLE(wxNotebook)
156 wxEVENT_PROPERTY( PageChanging , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING , wxNotebookEvent )
157 wxEVENT_PROPERTY( PageChanged , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED , wxNotebookEvent )
c5ca409b 158
3ff066a4 159 wxPROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 160 wxPROPERTY_FLAGS( WindowStyle , wxNotebookStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 161wxEND_PROPERTIES_TABLE()
51596bcb 162
3ff066a4
SC
163wxBEGIN_HANDLERS_TABLE(wxNotebook)
164wxEND_HANDLERS_TABLE()
51596bcb 165
078cf5cb 166wxCONSTRUCTOR_5( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle)
51596bcb 167
51741307 168
3ff066a4 169wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
af498247 170 wxREADONLY_PROPERTY( Page , wxNotebookPage* , GetPage , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4
SC
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"))
174wxEND_PROPERTIES_TABLE()
51741307 175
3ff066a4
SC
176wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
177wxEND_HANDLERS_TABLE()
51741307 178
078cf5cb 179wxCONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
51741307 180
51596bcb 181#else
d9317fd4 182IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
51741307 183IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
51596bcb 184#endif
d9317fd4 185IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
186
187// ============================================================================
188// implementation
189// ============================================================================
190
191// ----------------------------------------------------------------------------
192// wxNotebook construction
193// ----------------------------------------------------------------------------
194
51741307
SC
195const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const
196{
197 wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ;
198 WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ;
34a0c9f4 199 for( size_t i = 0 ; i < GetPageCount() ; ++i )
51741307
SC
200 {
201 wxNotebookPageInfo *info = new wxNotebookPageInfo() ;
34a0c9f4 202 info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ;
51741307
SC
203 list->Append( info ) ;
204 }
205 return m_pageInfos ;
206}
207
88310e2e
VZ
208// common part of all ctors
209void wxNotebook::Init()
210{
1e6feb95 211 m_imageList = NULL;
88310e2e 212 m_nSelection = -1;
caf95d2a
VZ
213
214#if wxUSE_UXTHEME
215 m_hbrBackground = NULL;
216#endif // wxUSE_UXTHEME
88310e2e
VZ
217}
218
219// default for dynamic class
220wxNotebook::wxNotebook()
221{
222 Init();
223}
224
225// the same arguments as for wxControl
226wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 227 wxWindowID id,
88310e2e
VZ
228 const wxPoint& pos,
229 const wxSize& size,
8b9518ee 230 long style,
88310e2e
VZ
231 const wxString& name)
232{
233 Init();
234
235 Create(parent, id, pos, size, style, name);
236}
237
238// Create() function
239bool wxNotebook::Create(wxWindow *parent,
8b9518ee 240 wxWindowID id,
88310e2e
VZ
241 const wxPoint& pos,
242 const wxSize& size,
8b9518ee 243 long style,
88310e2e
VZ
244 const wxString& name)
245{
df10208f
VZ
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 )
04eb05b0 250 {
df10208f
VZ
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 }
04eb05b0 258 }
078cf5cb 259
6dd16e4f
VZ
260 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
261 wxDefaultValidator, name) )
b8bdaa7c 262 return false;
88310e2e 263
fda7962d 264 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
b8bdaa7c 265 return false;
907f37b3 266
b8bdaa7c 267 return true;
0df3fbd7
VZ
268}
269
270WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
271{
272 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
273
274 tabStyle |= WS_TABSTOP | TCS_TABS;
275
2b5f62a0 276 if ( style & wxNB_MULTILINE )
0df3fbd7
VZ
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;
88310e2e
VZ
297}
298
caf95d2a
VZ
299wxNotebook::~wxNotebook()
300{
301#if wxUSE_UXTHEME
302 if ( m_hbrBackground )
303 ::DeleteObject((HBRUSH)m_hbrBackground);
304#endif // wxUSE_UXTHEME
305}
306
88310e2e
VZ
307// ----------------------------------------------------------------------------
308// wxNotebook accessors
309// ----------------------------------------------------------------------------
07b8d7ec 310
8d34bf5c 311size_t wxNotebook::GetPageCount() const
88310e2e
VZ
312{
313 // consistency check
1e6feb95 314 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
88310e2e 315
1e6feb95 316 return m_pages.Count();
88310e2e
VZ
317}
318
319int wxNotebook::GetRowCount() const
320{
321 return TabCtrl_GetRowCount(m_hwnd);
322}
323
8d34bf5c 324int wxNotebook::SetSelection(size_t nPage)
88310e2e 325{
078cf5cb 326 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") );
88310e2e 327
34a0c9f4 328 if ( int(nPage) != m_nSelection )
2b5f62a0
VZ
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 }
88310e2e 343
2b5f62a0 344 return m_nSelection;
88310e2e
VZ
345}
346
8d34bf5c 347bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
88310e2e 348{
b8bdaa7c 349 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
88310e2e
VZ
350
351 TC_ITEM tcItem;
352 tcItem.mask = TCIF_TEXT;
837e5743 353 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e
VZ
354
355 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
356}
357
8d34bf5c 358wxString wxNotebook::GetPageText(size_t nPage) const
88310e2e 359{
fda7962d 360 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
88310e2e 361
837e5743 362 wxChar buf[256];
88310e2e
VZ
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
8d34bf5c 375int wxNotebook::GetPageImage(size_t nPage) const
88310e2e 376{
223d09f6 377 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e
VZ
378
379 TC_ITEM tcItem;
380 tcItem.mask = TCIF_IMAGE;
381
382 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
383}
384
8d34bf5c 385bool wxNotebook::SetPageImage(size_t nPage, int nImage)
88310e2e 386{
b8bdaa7c 387 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") );
88310e2e
VZ
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
396void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 397{
07b8d7ec
VZ
398 wxNotebookBase::SetImageList(imageList);
399
400 if ( imageList )
1e6feb95 401 {
07b8d7ec 402 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
1e6feb95 403 }
b656febd
VS
404}
405
d9506e77
VZ
406// ----------------------------------------------------------------------------
407// wxNotebook size settings
408// ----------------------------------------------------------------------------
409
410void 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
b8bdaa7c 419 TabCtrl_AdjustRect(GetHwnd(), true, &rc);
d9506e77
VZ
420
421 // and now set it
422 SetSize(rc.right - rc.left, rc.bottom - rc.top);
423}
424
425void wxNotebook::SetPadding(const wxSize& padding)
426{
427 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
428}
42e69d6b
VZ
429
430// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
431// style.
432void wxNotebook::SetTabSize(const wxSize& sz)
433{
434 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
435}
436
2ce7af35
JS
437wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
438{
439 wxSize sizeTotal = sizePage;
078cf5cb 440
77ffb593 441 // We need to make getting tab size part of the wxWidgets API.
2ce7af35
JS
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
2015f2b3
VZ
464void 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);
2015f2b3 474
14a6b6e5
RD
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 }
2015f2b3
VZ
486}
487
88310e2e
VZ
488// ----------------------------------------------------------------------------
489// wxNotebook operations
490// ----------------------------------------------------------------------------
491
621793f4 492// remove one page from the notebook, without deleting
8d34bf5c 493wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
621793f4 494{
df7145da
VZ
495 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
496 if ( !pageRemoved )
497 return NULL;
621793f4 498
df7145da 499 TabCtrl_DeleteItem(m_hwnd, nPage);
621793f4 500
df7145da
VZ
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 {
623f5f70
JS
508 int selNew = TabCtrl_GetCurSel(m_hwnd);
509 if (selNew != -1)
df7145da 510 {
623f5f70 511 // No selection change, just refresh the current selection.
078cf5cb
WS
512 // Because it could be that the slection index changed
513 // we need to update it.
623f5f70
JS
514 // Note: this does not mean the selection it self changed.
515 m_nSelection = selNew;
516 m_pages[m_nSelection]->Refresh();
df7145da 517 }
623f5f70 518 else if (int(nPage) == m_nSelection)
43a997b6 519 {
623f5f70 520 // The selection was deleted.
078cf5cb 521
623f5f70
JS
522 // Determine new selection.
523 if (m_nSelection == int(GetPageCount()))
524 selNew = m_nSelection - 1;
525 else
526 selNew = m_nSelection;
078cf5cb 527
43a997b6
VZ
528 // m_nSelection must be always valid so reset it before calling
529 // SetSelection()
530 m_nSelection = -1;
531 SetSelection(selNew);
532 }
623f5f70
JS
533 else
534 {
535 wxFAIL; // Windows did not behave ok.
536 }
df7145da 537 }
47f12f58 538
df7145da 539 return pageRemoved;
621793f4
JS
540}
541
88310e2e
VZ
542// remove all pages
543bool wxNotebook::DeleteAllPages()
544{
8d34bf5c
VZ
545 size_t nPageCount = GetPageCount();
546 size_t nPage;
88310e2e 547 for ( nPage = 0; nPage < nPageCount; nPage++ )
1e6feb95 548 delete m_pages[nPage];
88310e2e 549
1e6feb95 550 m_pages.Clear();
88310e2e 551
907f37b3
VZ
552 TabCtrl_DeleteAllItems(m_hwnd);
553
47f12f58
JS
554 m_nSelection = -1;
555
37144cf0 556 InvalidateBestSize();
b8bdaa7c 557 return true;
88310e2e
VZ
558}
559
88310e2e 560// same as AddPage() but does it at given position
8d34bf5c 561bool wxNotebook::InsertPage(size_t nPage,
88310e2e
VZ
562 wxNotebookPage *pPage,
563 const wxString& strText,
564 bool bSelect,
565 int imageId)
566{
b8bdaa7c
VZ
567 wxCHECK_MSG( pPage != NULL, false, _T("NULL page in wxNotebook::InsertPage") );
568 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false,
22f3361e 569 _T("invalid index in wxNotebook::InsertPage") );
88310e2e 570
efa14cf2
VZ
571 wxASSERT_MSG( pPage->GetParent() == this,
572 _T("notebook pages must have notebook as parent") );
43427087 573
22f3361e
VZ
574 // add a new tab to the control
575 // ----------------------------
58a8ab88 576
22f3361e
VZ
577 // init all fields to 0
578 TC_ITEM tcItem;
579 wxZeroMemory(tcItem);
58a8ab88 580
22f3361e
VZ
581 // set the image, if any
582 if ( imageId != -1 )
583 {
584 tcItem.mask |= TCIF_IMAGE;
585 tcItem.iImage = imageId;
586 }
88310e2e 587
22f3361e
VZ
588 // and the text
589 if ( !strText.IsEmpty() )
590 {
591 tcItem.mask |= TCIF_TEXT;
592 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
593 }
43427087 594
e830a6a6
RD
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
22f3361e
VZ
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!
2015f2b3 608 AdjustPageSize(pPage);
43427087 609
22f3361e 610 // finally do insert it
2015f2b3
VZ
611 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
612 {
22f3361e 613 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
88310e2e 614
b8bdaa7c 615 return false;
22f3361e 616 }
88310e2e 617
22f3361e
VZ
618 // succeeded: save the pointer to the page
619 m_pages.Insert(pPage, nPage);
42e69d6b 620
56b9925b
VZ
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) )
2015f2b3
VZ
626 {
627 AdjustPageSize(pPage);
628 }
629
22f3361e
VZ
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
34a0c9f4 635 if ( int(nPage) <= m_nSelection )
22f3361e
VZ
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
37144cf0 652 InvalidateBestSize();
b8bdaa7c 653 return true;
88310e2e
VZ
654}
655
e450aa69 656int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
ef094fa0
JS
657{
658 TC_HITTESTINFO hitTestInfo;
659 hitTestInfo.pt.x = pt.x;
660 hitTestInfo.pt.y = pt.y;
e450aa69 661 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
ef094fa0 662
e450aa69
VZ
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 }
ef094fa0
JS
676
677 return item;
678}
679
e450aa69 680
88310e2e
VZ
681// ----------------------------------------------------------------------------
682// wxNotebook callbacks
683// ----------------------------------------------------------------------------
684
9026ad85 685void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 686{
b5c3b538
VZ
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
56b9925b
VZ
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
b8bdaa7c 715 TabCtrl_AdjustRect(m_hwnd, false, &rc);
4b7f2165
VZ
716
717 int width = rc.right - rc.left,
718 height = rc.bottom - rc.top;
1e6feb95 719 size_t nCount = m_pages.Count();
c86f1403 720 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
1e6feb95 721 wxNotebookPage *pPage = m_pages[nPage];
4b7f2165 722 pPage->SetSize(rc.left, rc.top, width, height);
b5c3b538
VZ
723 }
724
caf95d2a
VZ
725#if wxUSE_UXTHEME
726 UpdateBgBrush();
727#endif // wxUSE_UXTHEME
728
88310e2e
VZ
729 event.Skip();
730}
731
732void wxNotebook::OnSelChange(wxNotebookEvent& event)
733{
734 // is it our tab control?
735 if ( event.GetEventObject() == this )
5d1d2d46 736 {
5d1d2d46
VZ
737 int sel = event.GetOldSelection();
738 if ( sel != -1 )
b8bdaa7c 739 m_pages[sel]->Show(false);
0398b1d6 740
5d1d2d46
VZ
741 sel = event.GetSelection();
742 if ( sel != -1 )
743 {
1e6feb95 744 wxNotebookPage *pPage = m_pages[sel];
b8bdaa7c 745 pPage->Show(true);
1d5b3bf0
VZ
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();
5d1d2d46 763 }
0398b1d6 764
5d1d2d46
VZ
765 m_nSelection = sel;
766 }
88310e2e
VZ
767
768 // we want to give others a chance to process this message as well
769 event.Skip();
770}
771
b8bdaa7c 772bool wxNotebook::MSWTranslateMessage(WXMSG *wxmsg)
88310e2e 773{
b8bdaa7c 774 const MSG * const msg = (MSG *)wxmsg;
88310e2e 775
1d5b3bf0
VZ
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.
b8bdaa7c
VZ
781 if ( msg->message == WM_KEYDOWN && msg->wParam == VK_TAB &&
782 msg->hwnd == m_hwnd &&
1d5b3bf0 783 (wxIsCtrlDown() || !wxIsShiftDown()) )
b8bdaa7c
VZ
784 {
785 return MSWProcessMessage(wxmsg);
786 }
d9506e77 787
b8bdaa7c 788 return false;
88310e2e
VZ
789}
790
791void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
792{
d9506e77
VZ
793 if ( event.IsWindowChange() ) {
794 // change pages
795 AdvanceSelection(event.GetDirection());
796 }
797 else {
b8bdaa7c 798 // we get this event in 3 cases
d9506e77
VZ
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
b8bdaa7c
VZ
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 )
d9506e77 822 {
b8bdaa7c
VZ
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) )
d9506e77
VZ
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
1e6feb95 832 wxWindow *page = m_pages[m_nSelection];
d9506e77
VZ
833 if ( !page->GetEventHandler()->ProcessEvent(event) )
834 {
835 page->SetFocus();
836 }
837 //else: page manages focus inside it itself
838 }
b8bdaa7c 839 else // otherwise set the focus to the notebook itself
d9506e77 840 {
d9506e77
VZ
841 SetFocus();
842 }
843 }
844 else
845 {
b8bdaa7c
VZ
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 {
d9506e77
VZ
856 event.SetCurrentFocus(this);
857 parent->GetEventHandler()->ProcessEvent(event);
858 }
859 }
88310e2e 860 }
88310e2e
VZ
861}
862
caf95d2a
VZ
863#if wxUSE_UXTHEME
864
865void wxNotebook::UpdateBgBrush()
866{
867 if ( m_hbrBackground )
868 ::DeleteObject((HBRUSH)m_hbrBackground);
869
870 if ( wxUxThemeEngine::GetIfActive() )
871 {
872 RECT rc;
873 GetWindowRect(GetHwnd(), &rc);
874
875 WindowHDC hDC(GetHwnd());
876 MemoryHDC hDCMem(hDC);
877 CompatibleBitmap hBmp(hDC, rc.right - rc.left, rc.bottom - rc.top);
878
879 SelectInHDC selectBmp(hDCMem, hBmp);
880
881 SendMessage(GetHwnd(), WM_PRINTCLIENT, (WPARAM)(HDC)hDCMem,
882 PRF_ERASEBKGND | PRF_CLIENT | PRF_NONCLIENT);
883
884 m_hbrBackground = (WXHBRUSH)::CreatePatternBrush(hBmp);
885 }
886 else // no themes
887 {
888 m_hbrBackground = NULL;
889 }
890}
891
5c836c46 892WXHBRUSH wxNotebook::GetThemeBackgroundBrush(WXHDC hDC, wxWindow *win) const
caf95d2a 893{
caf95d2a
VZ
894 if ( m_hbrBackground )
895 {
896 // before drawing with the background brush, we need to position it
897 // correctly
caf95d2a
VZ
898 RECT rc;
899 ::GetWindowRect(GetHwndOf(win), &rc);
900
901 ::MapWindowPoints(NULL, GetHwnd(), (POINT *)&rc, 1);
902
5c836c46 903 if ( !::SetBrushOrgEx((HDC)hDC, -rc.left, -rc.top, NULL) )
caf95d2a
VZ
904 {
905 wxLogLastError(_T("SetBrushOrgEx(notebook bg brush)"));
906 }
5c836c46
VZ
907 }
908
909 return m_hbrBackground;
910}
caf95d2a 911
5c836c46 912void wxNotebook::DoEraseBackground(wxEraseEvent& event)
9b7d2b81
VZ
913{
914 DoEraseBackground((wxWindow *)event.GetEventObject(),
915 (WXHDC)GetHdcOf(*event.GetDC()));
916}
917
918void wxNotebook::DoEraseBackground(wxWindow *win, WXHDC hDC)
5c836c46
VZ
919{
920 // we can either draw the background ourselves or let DrawThemeBackground()
921 // do it, but as we already have the correct brush, let's do it ourselves
922 // (note that we use the same code in wxControl::MSWControlColor(), so if
923 // it breaks, it should at least break in consistent way)
9b7d2b81 924 WXHBRUSH hbr = GetThemeBackgroundBrush(hDC, win);
5c836c46
VZ
925 if ( hbr )
926 {
caf95d2a
VZ
927 RECT rectClient;
928 ::GetClientRect(GetHwndOf(win), &rectClient);
9b7d2b81 929 ::FillRect((HDC)hDC, &rectClient, (HBRUSH)hbr);
caf95d2a
VZ
930 }
931}
932
933#endif // wxUSE_UXTHEME
934
88310e2e
VZ
935// ----------------------------------------------------------------------------
936// wxNotebook base class virtuals
937// ----------------------------------------------------------------------------
b5c3b538 938
0b481c72
VZ
939#if wxUSE_CONSTRAINTS
940
b5c3b538
VZ
941// override these 2 functions to do nothing: everything is done in OnSize
942
4b7f2165 943void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
b5c3b538
VZ
944{
945 // don't set the sizes of the pages - their correct size is not yet known
b8bdaa7c 946 wxControl::SetConstraintSizes(false);
b5c3b538
VZ
947}
948
4b7f2165 949bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
b5c3b538 950{
b8bdaa7c 951 return true;
b5c3b538
VZ
952}
953
0b481c72
VZ
954#endif // wxUSE_CONSTRAINTS
955
0df3fbd7
VZ
956// ----------------------------------------------------------------------------
957// wxNotebook Windows message handlers
958// ----------------------------------------------------------------------------
959
960bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
961 WXWORD pos, WXHWND control)
962{
963 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
964 // up-down control
965 if ( control )
b8bdaa7c 966 return false;
0df3fbd7
VZ
967
968 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
969}
970
a23fd0e1 971bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 972{
93a19f17 973 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
974
975 NMHDR* hdr = (NMHDR *)lParam;
976 switch ( hdr->code ) {
977 case TCN_SELCHANGE:
978 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
979 break;
980
981 case TCN_SELCHANGING:
982 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
983 break;
984
fd3f686c 985 default:
a23fd0e1 986 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
987 }
988
93a19f17
VZ
989 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
990 event.SetOldSelection(m_nSelection);
88310e2e 991 event.SetEventObject(this);
a23fd0e1 992 event.SetInt(idCtrl);
88310e2e 993
fd3f686c
VZ
994 bool processed = GetEventHandler()->ProcessEvent(event);
995 *result = !event.IsAllowed();
996 return processed;
88310e2e
VZ
997}
998
1e6feb95 999#endif // wxUSE_NOTEBOOK