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