]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
Do archive name case conversion for wxMac and wxMotif
[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>
6c9a19aa 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
88310e2e
VZ
25// wxWindows
26#ifndef WX_PRECOMP
3096bd2f 27 #include "wx/string.h"
88310e2e
VZ
28#endif // WX_PRECOMP
29
3096bd2f
VZ
30#include "wx/log.h"
31#include "wx/imaglist.h"
32#include "wx/event.h"
33#include "wx/control.h"
34#include "wx/notebook.h"
04eb05b0 35#include "wx/app.h"
88310e2e 36
3096bd2f 37#include "wx/msw/private.h"
88310e2e
VZ
38
39// Windows standard headers
40#ifndef __WIN95__
2432b92d 41 #error "wxNotebook is only supported Windows 95 and above"
88310e2e
VZ
42#endif //Win95
43
aaab7c01
VZ
44#include <windowsx.h> // for SetWindowFont
45
b39dbf34
JS
46#ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 48#endif
57c208c5 49
b39dbf34 50#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
c42404a5 51 #include <commctrl.h>
88310e2e
VZ
52#endif
53
85b43fbf
JS
54#include "wx/msw/winundef.h"
55
56#if wxUSE_UXTHEME
57#include "wx/msw/uxtheme.h"
58
59#include "wx/radiobut.h"
60#include "wx/radiobox.h"
61#include "wx/checkbox.h"
62#include "wx/bmpbuttn.h"
63#include "wx/statline.h"
64#include "wx/statbox.h"
65#include "wx/stattext.h"
66#include "wx/slider.h"
67#include "wx/scrolwin.h"
68#include "wx/panel.h"
69#endif
70
88310e2e
VZ
71// ----------------------------------------------------------------------------
72// macros
73// ----------------------------------------------------------------------------
74
75// check that the page index is valid
8d34bf5c 76#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
88310e2e
VZ
77
78// hide the ugly cast
79#define m_hwnd (HWND)GetHWND()
80
74b31181
VZ
81// ----------------------------------------------------------------------------
82// constants
83// ----------------------------------------------------------------------------
84
85// This is a work-around for missing defines in gcc-2.95 headers
86#ifndef TCS_RIGHT
87 #define TCS_RIGHT 0x0002
88#endif
89
90#ifndef TCS_VERTICAL
91 #define TCS_VERTICAL 0x0080
92#endif
93
94#ifndef TCS_BOTTOM
95 #define TCS_BOTTOM TCS_RIGHT
96#endif
97
88310e2e
VZ
98// ----------------------------------------------------------------------------
99// event table
100// ----------------------------------------------------------------------------
101
51741307
SC
102#include <wx/listimpl.cpp>
103
104WX_DEFINE_LIST( wxNotebookPageInfoList ) ;
105
2e4df4bf
VZ
106DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
107DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
108
d9317fd4 109BEGIN_EVENT_TABLE(wxNotebook, wxControl)
88310e2e
VZ
110 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
111
9026ad85 112 EVT_SIZE(wxNotebook::OnSize)
42e69d6b 113
88310e2e 114 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
42e69d6b 115
88310e2e 116 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
d9317fd4 117END_EVENT_TABLE()
88310e2e 118
51596bcb 119#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
120WX_DEFINE_FLAGS( wxNotebookStyle )
121
122WX_BEGIN_FLAGS( wxNotebookStyle )
123 // new style border flags, we put them first to
124 // use them for streaming out
125 WX_FLAGS_MEMBER(wxBORDER_SIMPLE)
126 WX_FLAGS_MEMBER(wxBORDER_SUNKEN)
127 WX_FLAGS_MEMBER(wxBORDER_DOUBLE)
128 WX_FLAGS_MEMBER(wxBORDER_RAISED)
129 WX_FLAGS_MEMBER(wxBORDER_STATIC)
130 WX_FLAGS_MEMBER(wxBORDER_NONE)
131
132 // old style border flags
133 WX_FLAGS_MEMBER(wxSIMPLE_BORDER)
134 WX_FLAGS_MEMBER(wxSUNKEN_BORDER)
135 WX_FLAGS_MEMBER(wxDOUBLE_BORDER)
136 WX_FLAGS_MEMBER(wxRAISED_BORDER)
137 WX_FLAGS_MEMBER(wxSTATIC_BORDER)
138 WX_FLAGS_MEMBER(wxNO_BORDER)
139
140 // standard window styles
141 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL)
142 WX_FLAGS_MEMBER(wxCLIP_CHILDREN)
143 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW)
144 WX_FLAGS_MEMBER(wxWANTS_CHARS)
145 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE)
146 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB )
147 WX_FLAGS_MEMBER(wxVSCROLL)
148 WX_FLAGS_MEMBER(wxHSCROLL)
149
150 WX_FLAGS_MEMBER(wxNB_FIXEDWIDTH)
151 WX_FLAGS_MEMBER(wxNB_LEFT)
152 WX_FLAGS_MEMBER(wxNB_RIGHT)
153 WX_FLAGS_MEMBER(wxNB_BOTTOM)
154
155WX_END_FLAGS( wxNotebookStyle )
51741307 156
51596bcb 157IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h")
51741307
SC
158IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" )
159
067e9be6 160WX_COLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ;
51596bcb 161
f0a126fe
SC
162template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value)
163{
0c6b0084 164 wxListCollectionToVariantArray<wxNotebookPageInfoList::compatibility_iterator>( theList , value ) ;
f0a126fe
SC
165}
166
51596bcb 167WX_BEGIN_PROPERTIES_TABLE(wxNotebook)
ffca575c
SC
168 WX_EVENT_PROPERTY( PageChanging , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING , wxNotebookEvent )
169 WX_EVENT_PROPERTY( PageChanged , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED , wxNotebookEvent )
c5ca409b 170
067e9be6 171 WX_PROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
bc9fb572 172 WX_PROPERTY_FLAGS( WindowStyle , wxNotebookStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
51596bcb
SC
173WX_END_PROPERTIES_TABLE()
174
175WX_BEGIN_HANDLERS_TABLE(wxNotebook)
176WX_END_HANDLERS_TABLE()
177
0c6b0084 178WX_CONSTRUCTOR_5( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle)
51596bcb 179
51741307
SC
180
181WX_BEGIN_PROPERTIES_TABLE(wxNotebookPageInfo)
067e9be6 182 WX_READONLY_PROPERTY( Page , wxNotebookPage* , GetPage , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
e70b4f10 183 WX_READONLY_PROPERTY( Text , wxString , GetText , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
067e9be6
SC
184 WX_READONLY_PROPERTY( Selected , bool , GetSelected , false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
185 WX_READONLY_PROPERTY( ImageId , int , GetImageId , -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
51741307
SC
186WX_END_PROPERTIES_TABLE()
187
188WX_BEGIN_HANDLERS_TABLE(wxNotebookPageInfo)
189WX_END_HANDLERS_TABLE()
190
191WX_CONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId )
192
51596bcb 193#else
d9317fd4 194IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
51741307 195IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject )
51596bcb 196#endif
d9317fd4 197IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
198
199// ============================================================================
200// implementation
201// ============================================================================
202
203// ----------------------------------------------------------------------------
204// wxNotebook construction
205// ----------------------------------------------------------------------------
206
51741307
SC
207const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const
208{
209 wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ;
210 WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ;
34a0c9f4 211 for( size_t i = 0 ; i < GetPageCount() ; ++i )
51741307
SC
212 {
213 wxNotebookPageInfo *info = new wxNotebookPageInfo() ;
34a0c9f4 214 info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ;
51741307
SC
215 list->Append( info ) ;
216 }
217 return m_pageInfos ;
218}
219
88310e2e
VZ
220// common part of all ctors
221void wxNotebook::Init()
222{
1e6feb95 223 m_imageList = NULL;
88310e2e
VZ
224 m_nSelection = -1;
225}
226
227// default for dynamic class
228wxNotebook::wxNotebook()
229{
230 Init();
231}
232
233// the same arguments as for wxControl
234wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 235 wxWindowID id,
88310e2e
VZ
236 const wxPoint& pos,
237 const wxSize& size,
8b9518ee 238 long style,
88310e2e
VZ
239 const wxString& name)
240{
241 Init();
242
243 Create(parent, id, pos, size, style, name);
244}
245
246// Create() function
247bool wxNotebook::Create(wxWindow *parent,
8b9518ee 248 wxWindowID id,
88310e2e
VZ
249 const wxPoint& pos,
250 const wxSize& size,
8b9518ee 251 long style,
88310e2e
VZ
252 const wxString& name)
253{
04eb05b0
JS
254 // Does ComCtl32 support non-top tabs?
255 int verComCtl32 = wxApp::GetComCtl32Version();
256 if ( verComCtl32 < 470 || verComCtl32 >= 600 )
257 {
258 if (style & wxNB_BOTTOM)
259 style &= ~wxNB_BOTTOM;
260
261 if (style & wxNB_LEFT)
262 style &= ~wxNB_LEFT;
263
264 if (style & wxNB_RIGHT)
265 style &= ~wxNB_RIGHT;
266 }
267
6dd16e4f
VZ
268 if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL,
269 wxDefaultValidator, name) )
0df3fbd7 270 return FALSE;
88310e2e 271
fda7962d 272 if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) )
0df3fbd7 273 return FALSE;
907f37b3 274
0df3fbd7
VZ
275 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE)));
276
277 return TRUE;
278}
279
280WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const
281{
282 WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle);
283
284 tabStyle |= WS_TABSTOP | TCS_TABS;
285
2b5f62a0 286 if ( style & wxNB_MULTILINE )
0df3fbd7
VZ
287 tabStyle |= TCS_MULTILINE;
288 if ( style & wxNB_FIXEDWIDTH )
289 tabStyle |= TCS_FIXEDWIDTH;
290
291 if ( style & wxNB_BOTTOM )
292 tabStyle |= TCS_RIGHT;
293 else if ( style & wxNB_LEFT )
294 tabStyle |= TCS_VERTICAL;
295 else if ( style & wxNB_RIGHT )
296 tabStyle |= TCS_VERTICAL | TCS_RIGHT;
297
298 // ex style
299 if ( exstyle )
300 {
301 // note that we never want to have the default WS_EX_CLIENTEDGE style
302 // as it looks too ugly for the notebooks
303 *exstyle = 0;
304 }
305
306 return tabStyle;
88310e2e
VZ
307}
308
88310e2e
VZ
309// ----------------------------------------------------------------------------
310// wxNotebook accessors
311// ----------------------------------------------------------------------------
07b8d7ec 312
8d34bf5c 313size_t wxNotebook::GetPageCount() const
88310e2e
VZ
314{
315 // consistency check
1e6feb95 316 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
88310e2e 317
1e6feb95 318 return m_pages.Count();
88310e2e
VZ
319}
320
321int wxNotebook::GetRowCount() const
322{
323 return TabCtrl_GetRowCount(m_hwnd);
324}
325
8d34bf5c 326int wxNotebook::SetSelection(size_t nPage)
88310e2e 327{
223d09f6 328 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e 329
34a0c9f4 330 if ( int(nPage) != m_nSelection )
2b5f62a0
VZ
331 {
332 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
333 event.SetSelection(nPage);
334 event.SetOldSelection(m_nSelection);
335 event.SetEventObject(this);
336 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
337 {
338 // program allows the page change
339 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
340 (void)GetEventHandler()->ProcessEvent(event);
341
342 TabCtrl_SetCurSel(m_hwnd, nPage);
343 }
344 }
88310e2e 345
2b5f62a0 346 return m_nSelection;
88310e2e
VZ
347}
348
8d34bf5c 349bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
88310e2e 350{
223d09f6 351 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e
VZ
352
353 TC_ITEM tcItem;
354 tcItem.mask = TCIF_TEXT;
837e5743 355 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e
VZ
356
357 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
358}
359
8d34bf5c 360wxString wxNotebook::GetPageText(size_t nPage) const
88310e2e 361{
fda7962d 362 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") );
88310e2e 363
837e5743 364 wxChar buf[256];
88310e2e
VZ
365 TC_ITEM tcItem;
366 tcItem.mask = TCIF_TEXT;
367 tcItem.pszText = buf;
368 tcItem.cchTextMax = WXSIZEOF(buf);
369
370 wxString str;
371 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
372 str = tcItem.pszText;
373
374 return str;
375}
376
8d34bf5c 377int wxNotebook::GetPageImage(size_t nPage) const
88310e2e 378{
223d09f6 379 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e
VZ
380
381 TC_ITEM tcItem;
382 tcItem.mask = TCIF_IMAGE;
383
384 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
385}
386
8d34bf5c 387bool wxNotebook::SetPageImage(size_t nPage, int nImage)
88310e2e 388{
223d09f6 389 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e
VZ
390
391 TC_ITEM tcItem;
392 tcItem.mask = TCIF_IMAGE;
393 tcItem.iImage = nImage;
394
395 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
396}
397
398void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 399{
07b8d7ec
VZ
400 wxNotebookBase::SetImageList(imageList);
401
402 if ( imageList )
1e6feb95 403 {
07b8d7ec 404 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
1e6feb95 405 }
b656febd
VS
406}
407
d9506e77
VZ
408// ----------------------------------------------------------------------------
409// wxNotebook size settings
410// ----------------------------------------------------------------------------
411
412void wxNotebook::SetPageSize(const wxSize& size)
413{
414 // transform the page size into the notebook size
415 RECT rc;
416 rc.left =
417 rc.top = 0;
418 rc.right = size.x;
419 rc.bottom = size.y;
420
421 TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc);
422
423 // and now set it
424 SetSize(rc.right - rc.left, rc.bottom - rc.top);
425}
426
427void wxNotebook::SetPadding(const wxSize& padding)
428{
429 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
430}
42e69d6b
VZ
431
432// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
433// style.
434void wxNotebook::SetTabSize(const wxSize& sz)
435{
436 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
437}
438
2ce7af35
JS
439wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
440{
441 wxSize sizeTotal = sizePage;
442
443 // We need to make getting tab size part of the wxWindows API.
444 wxSize tabSize(0, 0);
445 if (GetPageCount() > 0)
446 {
447 RECT rect;
448 TabCtrl_GetItemRect((HWND) GetHWND(), 0, & rect);
449 tabSize.x = rect.right - rect.left;
450 tabSize.y = rect.bottom - rect.top;
451 }
452 if ( HasFlag(wxNB_LEFT) || HasFlag(wxNB_RIGHT) )
453 {
454 sizeTotal.x += tabSize.x + 7;
455 sizeTotal.y += 7;
456 }
457 else
458 {
459 sizeTotal.x += 7;
460 sizeTotal.y += tabSize.y + 7;
461 }
462
463 return sizeTotal;
464}
465
2015f2b3
VZ
466void wxNotebook::AdjustPageSize(wxNotebookPage *page)
467{
468 wxCHECK_RET( page, _T("NULL page in wxNotebook::AdjustPageSize") );
469
470 RECT rc;
471 rc.left =
472 rc.top = 0;
473
474 // get the page size from the notebook size
475 GetSize((int *)&rc.right, (int *)&rc.bottom);
476 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
477
478 page->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
479}
480
88310e2e
VZ
481// ----------------------------------------------------------------------------
482// wxNotebook operations
483// ----------------------------------------------------------------------------
484
621793f4 485// remove one page from the notebook, without deleting
8d34bf5c 486wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
621793f4 487{
df7145da
VZ
488 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
489 if ( !pageRemoved )
490 return NULL;
621793f4 491
df7145da 492 TabCtrl_DeleteItem(m_hwnd, nPage);
621793f4 493
df7145da
VZ
494 if ( m_pages.IsEmpty() )
495 {
496 // no selection any more, the notebook becamse empty
497 m_nSelection = -1;
498 }
499 else // notebook still not empty
500 {
43a997b6
VZ
501 // change the selected page if it was deleted or became invalid
502 int selNew;
34a0c9f4 503 if ( m_nSelection == int(GetPageCount()) )
df7145da 504 {
43a997b6
VZ
505 // last page deleted, make the new last page the new selection
506 selNew = m_nSelection - 1;
df7145da 507 }
34a0c9f4 508 else if ( int(nPage) <= m_nSelection )
43a997b6
VZ
509 {
510 // we must show another page, even if it has the same index
511 selNew = m_nSelection;
512 }
513 else // nothing changes for the currently selected page
514 {
515 selNew = -1;
df7145da 516
43a997b6
VZ
517 // we still must refresh the current page: this needs to be done
518 // for some unknown reason if the tab control shows the up-down
519 // control (i.e. when there are too many pages) -- otherwise after
520 // deleting a page nothing at all is shown
68d31398
JS
521 if (m_nSelection >= 0)
522 m_pages[m_nSelection]->Refresh();
43a997b6
VZ
523 }
524
525 if ( selNew != -1 )
526 {
527 // m_nSelection must be always valid so reset it before calling
528 // SetSelection()
529 m_nSelection = -1;
530 SetSelection(selNew);
531 }
df7145da 532 }
47f12f58 533
df7145da 534 return pageRemoved;
621793f4
JS
535}
536
88310e2e
VZ
537// remove all pages
538bool wxNotebook::DeleteAllPages()
539{
8d34bf5c
VZ
540 size_t nPageCount = GetPageCount();
541 size_t nPage;
88310e2e 542 for ( nPage = 0; nPage < nPageCount; nPage++ )
1e6feb95 543 delete m_pages[nPage];
88310e2e 544
1e6feb95 545 m_pages.Clear();
88310e2e 546
907f37b3
VZ
547 TabCtrl_DeleteAllItems(m_hwnd);
548
47f12f58
JS
549 m_nSelection = -1;
550
88310e2e
VZ
551 return TRUE;
552}
553
88310e2e 554// same as AddPage() but does it at given position
8d34bf5c 555bool wxNotebook::InsertPage(size_t nPage,
88310e2e
VZ
556 wxNotebookPage *pPage,
557 const wxString& strText,
558 bool bSelect,
559 int imageId)
560{
22f3361e
VZ
561 wxCHECK_MSG( pPage != NULL, FALSE, _T("NULL page in wxNotebook::InsertPage") );
562 wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE,
563 _T("invalid index in wxNotebook::InsertPage") );
88310e2e 564
efa14cf2
VZ
565 wxASSERT_MSG( pPage->GetParent() == this,
566 _T("notebook pages must have notebook as parent") );
43427087 567
85b43fbf 568#if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO
c02e5a31
JS
569 static bool g_TestedForTheme = FALSE;
570 static bool g_UseTheme = FALSE;
571 if (!g_TestedForTheme)
572 {
573 int commCtrlVersion = wxTheApp->GetComCtl32Version() ;
574
575 g_UseTheme = (commCtrlVersion >= 600);
576 g_TestedForTheme = TRUE;
577 }
578
85b43fbf
JS
579 // Automatically apply the theme background,
580 // changing the colour of the panel to match the
581 // tab page colour. This won't work well with all
582 // themes but it's a start.
c02e5a31 583 if (g_UseTheme && wxUxThemeEngine::Get() && pPage->IsKindOf(CLASSINFO(wxPanel)))
85b43fbf
JS
584 {
585 ApplyThemeBackground(pPage, GetThemeBackgroundColour());
586 }
587#endif
588
22f3361e
VZ
589 // add a new tab to the control
590 // ----------------------------
58a8ab88 591
22f3361e
VZ
592 // init all fields to 0
593 TC_ITEM tcItem;
594 wxZeroMemory(tcItem);
58a8ab88 595
22f3361e
VZ
596 // set the image, if any
597 if ( imageId != -1 )
598 {
599 tcItem.mask |= TCIF_IMAGE;
600 tcItem.iImage = imageId;
601 }
88310e2e 602
22f3361e
VZ
603 // and the text
604 if ( !strText.IsEmpty() )
605 {
606 tcItem.mask |= TCIF_TEXT;
607 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
608 }
43427087 609
22f3361e
VZ
610 // fit the notebook page to the tab control's display area: this should be
611 // done before adding it to the notebook or TabCtrl_InsertItem() will
612 // change the notebooks size itself!
2015f2b3 613 AdjustPageSize(pPage);
43427087 614
22f3361e 615 // finally do insert it
2015f2b3
VZ
616 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 )
617 {
22f3361e 618 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
88310e2e 619
22f3361e
VZ
620 return FALSE;
621 }
88310e2e 622
22f3361e
VZ
623 // succeeded: save the pointer to the page
624 m_pages.Insert(pPage, nPage);
42e69d6b 625
2015f2b3
VZ
626 // for the first page (only) we need to adjust the size again because the
627 // notebook size changed: the tabs which hadn't been there before are now
628 // shown
629 if ( m_pages.GetCount() == 1 )
630 {
631 AdjustPageSize(pPage);
632 }
633
22f3361e
VZ
634 // hide the page: unless it is selected, it shouldn't be shown (and if it
635 // is selected it will be shown later)
636 HWND hwnd = GetWinHwnd(pPage);
637 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
0398b1d6 638
22f3361e
VZ
639 // this updates internal flag too -- otherwise it would get out of sync
640 // with the real state
641 pPage->Show(FALSE);
43427087 642
96d37807 643
22f3361e
VZ
644 // now deal with the selection
645 // ---------------------------
646
647 // if the inserted page is before the selected one, we must update the
648 // index of the selected page
34a0c9f4 649 if ( int(nPage) <= m_nSelection )
22f3361e
VZ
650 {
651 // one extra page added
652 m_nSelection++;
653 }
654
655 // some page should be selected: either this one or the first one if there
656 // is still no selection
657 int selNew = -1;
658 if ( bSelect )
659 selNew = nPage;
660 else if ( m_nSelection == -1 )
661 selNew = 0;
662
663 if ( selNew != -1 )
664 SetSelection(selNew);
665
666 return TRUE;
88310e2e
VZ
667}
668
e450aa69 669int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
ef094fa0
JS
670{
671 TC_HITTESTINFO hitTestInfo;
672 hitTestInfo.pt.x = pt.x;
673 hitTestInfo.pt.y = pt.y;
e450aa69 674 int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo);
ef094fa0 675
e450aa69
VZ
676 if ( flags )
677 {
678 *flags = 0;
679
680 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
681 *flags |= wxNB_HITTEST_NOWHERE;
682 if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM)
683 *flags |= wxNB_HITTEST_ONITEM;
684 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
685 *flags |= wxNB_HITTEST_ONICON;
686 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
687 *flags |= wxNB_HITTEST_ONLABEL;
688 }
ef094fa0
JS
689
690 return item;
691}
692
e450aa69 693
88310e2e
VZ
694// ----------------------------------------------------------------------------
695// wxNotebook callbacks
696// ----------------------------------------------------------------------------
697
9026ad85 698void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 699{
b5c3b538
VZ
700 // fit the notebook page to the tab control's display area
701 RECT rc;
702 rc.left = rc.top = 0;
703 GetSize((int *)&rc.right, (int *)&rc.bottom);
704
705 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
4b7f2165
VZ
706
707 int width = rc.right - rc.left,
708 height = rc.bottom - rc.top;
1e6feb95 709 size_t nCount = m_pages.Count();
c86f1403 710 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
1e6feb95 711 wxNotebookPage *pPage = m_pages[nPage];
4b7f2165 712 pPage->SetSize(rc.left, rc.top, width, height);
b5c3b538
VZ
713 }
714
88310e2e
VZ
715 event.Skip();
716}
717
718void wxNotebook::OnSelChange(wxNotebookEvent& event)
719{
720 // is it our tab control?
721 if ( event.GetEventObject() == this )
5d1d2d46 722 {
5d1d2d46
VZ
723 int sel = event.GetOldSelection();
724 if ( sel != -1 )
1e6feb95 725 m_pages[sel]->Show(FALSE);
0398b1d6 726
5d1d2d46
VZ
727 sel = event.GetSelection();
728 if ( sel != -1 )
729 {
1e6feb95 730 wxNotebookPage *pPage = m_pages[sel];
5d1d2d46
VZ
731 pPage->Show(TRUE);
732 pPage->SetFocus();
733 }
0398b1d6 734
5d1d2d46
VZ
735 m_nSelection = sel;
736 }
88310e2e
VZ
737
738 // we want to give others a chance to process this message as well
739 event.Skip();
740}
741
742void wxNotebook::OnSetFocus(wxFocusEvent& event)
743{
d9506e77
VZ
744 // this function is only called when the focus is explicitly set (i.e. from
745 // the program) to the notebook - in this case we don't need the
746 // complicated OnNavigationKey() logic because the programmer knows better
747 // what [s]he wants
88310e2e 748
d9506e77
VZ
749 // set focus to the currently selected page if any
750 if ( m_nSelection != -1 )
1e6feb95 751 m_pages[m_nSelection]->SetFocus();
d9506e77
VZ
752
753 event.Skip();
88310e2e
VZ
754}
755
756void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
757{
d9506e77
VZ
758 if ( event.IsWindowChange() ) {
759 // change pages
760 AdvanceSelection(event.GetDirection());
761 }
762 else {
763 // we get this event in 2 cases
764 //
765 // a) one of our pages might have generated it because the user TABbed
766 // out from it in which case we should propagate the event upwards and
767 // our parent will take care of setting the focus to prev/next sibling
768 //
769 // or
770 //
771 // b) the parent panel wants to give the focus to us so that we
772 // forward it to our selected page. We can't deal with this in
773 // OnSetFocus() because we don't know which direction the focus came
774 // from in this case and so can't choose between setting the focus to
775 // first or last panel child
d9506e77 776 wxWindow *parent = GetParent();
fc10daf3
MB
777 // the cast is here to fic a GCC ICE
778 if ( ((wxWindow*)event.GetEventObject()) == parent )
d9506e77
VZ
779 {
780 // no, it doesn't come from child, case (b): forward to a page
781 if ( m_nSelection != -1 )
782 {
783 // so that the page knows that the event comes from it's parent
784 // and is being propagated downwards
785 event.SetEventObject(this);
786
1e6feb95 787 wxWindow *page = m_pages[m_nSelection];
d9506e77
VZ
788 if ( !page->GetEventHandler()->ProcessEvent(event) )
789 {
790 page->SetFocus();
791 }
792 //else: page manages focus inside it itself
793 }
794 else
795 {
796 // we have no pages - still have to give focus to _something_
797 SetFocus();
798 }
799 }
800 else
801 {
802 // it comes from our child, case (a), pass to the parent
803 if ( parent ) {
804 event.SetCurrentFocus(this);
805 parent->GetEventHandler()->ProcessEvent(event);
806 }
807 }
88310e2e 808 }
88310e2e
VZ
809}
810
811// ----------------------------------------------------------------------------
812// wxNotebook base class virtuals
813// ----------------------------------------------------------------------------
b5c3b538 814
0b481c72
VZ
815#if wxUSE_CONSTRAINTS
816
b5c3b538
VZ
817// override these 2 functions to do nothing: everything is done in OnSize
818
4b7f2165 819void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
b5c3b538
VZ
820{
821 // don't set the sizes of the pages - their correct size is not yet known
822 wxControl::SetConstraintSizes(FALSE);
823}
824
4b7f2165 825bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
b5c3b538
VZ
826{
827 return TRUE;
828}
829
0b481c72
VZ
830#endif // wxUSE_CONSTRAINTS
831
0df3fbd7
VZ
832// ----------------------------------------------------------------------------
833// wxNotebook Windows message handlers
834// ----------------------------------------------------------------------------
835
836bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode,
837 WXWORD pos, WXHWND control)
838{
839 // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
840 // up-down control
841 if ( control )
842 return FALSE;
843
844 return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control);
845}
846
a23fd0e1 847bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 848{
93a19f17 849 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
850
851 NMHDR* hdr = (NMHDR *)lParam;
852 switch ( hdr->code ) {
853 case TCN_SELCHANGE:
854 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
855 break;
856
857 case TCN_SELCHANGING:
858 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
859 break;
860
fd3f686c 861 default:
a23fd0e1 862 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
863 }
864
93a19f17
VZ
865 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
866 event.SetOldSelection(m_nSelection);
88310e2e 867 event.SetEventObject(this);
a23fd0e1 868 event.SetInt(idCtrl);
88310e2e 869
fd3f686c
VZ
870 bool processed = GetEventHandler()->ProcessEvent(event);
871 *result = !event.IsAllowed();
872 return processed;
88310e2e
VZ
873}
874
85b43fbf
JS
875// Windows only: attempts to get colour for UX theme page background
876wxColour wxNotebook::GetThemeBackgroundColour()
877{
878#if wxUSE_UXTHEME
879 if (wxUxThemeEngine::Get())
880 {
2142d7ba 881 wxUxThemeHandle hTheme(this, L"TAB");
85b43fbf
JS
882 if (hTheme)
883 {
884 // This is total guesswork.
885 // See PlatformSDK\Include\Tmschema.h for values
886 COLORREF themeColor;
2142d7ba
VZ
887 wxUxThemeEngine::Get()->GetThemeColor
888 (
889 hTheme,
890 10 /* TABP_BODY */,
891 1 /* NORMAL */,
892 3821, /* FILLCOLORHINT */
893 & themeColor
894 );
85b43fbf
JS
895
896 wxColour colour(GetRValue(themeColor), GetGValue(themeColor), GetBValue(themeColor));
897 return colour;
898 }
899 }
2142d7ba
VZ
900#endif // wxUSE_UXTHEME
901
85b43fbf
JS
902 return GetBackgroundColour();
903}
904
905// Windows only: attempts to apply the UX theme page background to this page
840015f0 906#if wxUSE_UXTHEME
85b43fbf 907void wxNotebook::ApplyThemeBackground(wxWindow* window, const wxColour& colour)
840015f0
MB
908#else
909void wxNotebook::ApplyThemeBackground(wxWindow*, const wxColour&)
910#endif
85b43fbf
JS
911{
912#if wxUSE_UXTHEME
913 // Don't set the background for buttons since this will
914 // switch it into ownerdraw mode
915 if (window->IsKindOf(CLASSINFO(wxButton)) && !window->IsKindOf(CLASSINFO(wxBitmapButton)))
916 // This is essential, otherwise you'll see dark grey
917 // corners in the buttons.
918 ((wxButton*)window)->wxControl::SetBackgroundColour(colour);
919 else if (window->IsKindOf(CLASSINFO(wxStaticText)) ||
920 window->IsKindOf(CLASSINFO(wxStaticBox)) ||
921 window->IsKindOf(CLASSINFO(wxStaticLine)) ||
922 window->IsKindOf(CLASSINFO(wxRadioButton)) ||
923 window->IsKindOf(CLASSINFO(wxRadioBox)) ||
924 window->IsKindOf(CLASSINFO(wxCheckBox)) ||
925 window->IsKindOf(CLASSINFO(wxBitmapButton)) ||
926 window->IsKindOf(CLASSINFO(wxSlider)) ||
927 window->IsKindOf(CLASSINFO(wxPanel)) ||
928 (window->IsKindOf(CLASSINFO(wxNotebook)) && (window != this)) ||
929 window->IsKindOf(CLASSINFO(wxScrolledWindow))
930 )
931 {
932 window->SetBackgroundColour(colour);
933 }
934
fc10daf3 935 for ( wxWindowList::compatibility_iterator node = window->GetChildren().GetFirst(); node; node = node->GetNext() )
85b43fbf
JS
936 {
937 wxWindow *child = node->GetData();
938 ApplyThemeBackground(child, colour);
939 }
940#endif
941}
942
c02e5a31
JS
943long wxNotebook::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
944{
945 static bool g_TestedForTheme = FALSE;
946 static bool g_UseTheme = FALSE;
947 switch ( nMsg )
948 {
949 case WM_ERASEBKGND:
950 {
951 if (!g_TestedForTheme)
952 {
953 int commCtrlVersion = wxTheApp->GetComCtl32Version() ;
954
955 g_UseTheme = (commCtrlVersion >= 600);
956 g_TestedForTheme = TRUE;
957 }
958
959 // If using XP themes, it seems we can get away
960 // with not drawing a background, which reduces flicker.
961 if (g_UseTheme)
962 return TRUE;
963 }
964 }
965
966 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
967}
968
969
1e6feb95 970#endif // wxUSE_NOTEBOOK