1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/string.h"
21 #include "wx/settings.h"
25 #include "wx/imaglist.h"
27 #include "wx/control.h"
28 #include "wx/notebook.h"
30 #include "wx/os2/private.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // check that the page index is valid
37 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
40 #define m_hWnd (HWND)GetHWND()
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
53 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
54 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
55 EVT_SIZE(wxNotebook::OnSize
)
56 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
57 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
60 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxNotebook construction
69 // ----------------------------------------------------------------------------
72 // Common part of all ctors
74 void wxNotebook::Init()
79 } // end of wxNotebook::Init
82 // Default for dynamic class
84 wxNotebook::wxNotebook()
87 } // end of wxNotebook::wxNotebook
90 // The same arguments as for wxControl
92 wxNotebook::wxNotebook(
98 , const wxString
& rsName
109 } // end of wxNotebook::wxNotebook
114 bool wxNotebook::Create(
117 , const wxPoint
& rPos
118 , const wxSize
& rSize
120 , const wxString
& rsName
126 if (!CreateControl( pParent
137 // Notebook, so explicitly specify 0 as last parameter
139 if (!OS2CreateControl( "NOTEBOOK"
143 ,lStyle
| wxTAB_TRAVERSAL
147 SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
149 } // end of wxNotebook::Create
151 WXDWORD
wxNotebook::OS2GetStyle (
153 , WXDWORD
* pdwExstyle
156 WXDWORD dwTabStyle
= wxControl::OS2GetStyle( lStyle
160 dwTabStyle
|= WS_TABSTOP
| BKS_SOLIDBIND
| BKS_ROUNDEDTABS
| BKS_TABTEXTCENTER
;
162 if (lStyle
& wxNB_BOTTOM
)
163 dwTabStyle
|= BKS_MAJORTABBOTTOM
| BKS_BACKPAGESBL
;
164 else if (lStyle
& wxNB_RIGHT
)
165 dwTabStyle
|= BKS_MAJORTABRIGHT
| BKS_BACKPAGESBR
;
166 else if (lStyle
& wxNB_LEFT
)
167 dwTabStyle
|= BKS_MAJORTABLEFT
| BKS_BACKPAGESTL
;
168 else // default to top
169 dwTabStyle
|= BKS_MAJORTABTOP
| BKS_BACKPAGESTR
;
177 // Note that we never want to have the default WS_EX_CLIENTEDGE style
178 // as it looks too ugly for the notebooks
183 } // end of wxNotebook::OS2GetStyle
185 // ----------------------------------------------------------------------------
186 // wxNotebook accessors
187 // ----------------------------------------------------------------------------
189 int wxNotebook::GetPageCount() const
191 int nPageInternal
= m_pages
.Count();
192 int nPageAPI
= (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
);
197 wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
));
198 return m_pages
.Count();
199 } // end of wxNotebook::GetPageCount
201 int wxNotebook::GetRowCount() const
203 return (int)::WinSendMsg( GetHWND()
208 } // end of wxNotebook::GetRowCount
210 int wxNotebook::SetSelection(
214 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
215 int nOldPage
= GetSelection();
217 ChangePage( m_nSelection
221 ULONG ulPageId
= (ULONG
)m_alPageId
[nPage
];
223 ::WinSendMsg( GetHWND()
225 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
228 m_nSelection
= nPage
;
230 } // end of wxNotebook::SetSelection
232 bool wxNotebook::SetPageText(
234 , const wxString
& rsStrText
237 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
240 ULONG ulPageId
= (ULONG
)m_alPageId
[nPage
];
242 return (bool)::WinSendMsg( m_hWnd
244 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
245 ,MPFROMP((PSZ
)rsStrText
.c_str())
247 } // end of wxNotebook::SetPageText
249 wxString
wxNotebook::GetPageText (
258 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
261 ULONG ulPageId
= (ULONG
)m_alPageId
[nPage
];
263 memset(&vBookText
, '\0', sizeof(BOOKTEXT
));
264 vBookText
.textLen
= 0; // This will get the length
265 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
267 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
270 if (ulRc
== BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
272 if (ulRc
== BOOKERR_INVALID_PARAMETERS
)
274 wxLogError(wxT("Invalid Page Id for page text querry."));
276 return wxEmptyString
;
278 vBookText
.textLen
= ulRc
+ 1; // To get the null terminator
279 vBookText
.pString
= zBuf
;
282 // Now get the actual text
284 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
286 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
289 if (ulRc
== BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
291 return wxEmptyString
;
296 vBookText
.pString
[ulRc
] = '\0';
297 sStr
= vBookText
.pString
;
299 } // end of wxNotebook::GetPageText
301 int wxNotebook::GetPageImage (
305 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
308 // For OS/2 just return the page
311 } // end of wxNotebook::GetPageImage
313 bool wxNotebook::SetPageImage (
318 wxBitmap
* pBitmap
= (wxBitmap
*)m_imageList
->GetBitmap(nImage
);
321 ULONG ulPageId
= (ULONG
)m_alPageId
[nPage
];
323 return (bool)::WinSendMsg( GetHWND()
325 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
326 ,(MPARAM
)pBitmap
->GetHBITMAP()
328 } // end of wxNotebook::SetPageImage
330 void wxNotebook::SetImageList (
331 wxImageList
* WXUNUSED(pImageList
)
335 // Does nothing under OS/2
337 } // end of wxNotebook::SetImageList
339 // ----------------------------------------------------------------------------
340 // wxNotebook size settings
341 // ----------------------------------------------------------------------------
342 void wxNotebook::SetPageSize (
349 // Transform the page size into the notebook size
351 vRect
.xLeft
= vRect
.yTop
= 0;
352 vRect
.xRight
= rSize
.x
;
353 vRect
.yBottom
= rSize
.y
;
359 SetSize( vRect
.xRight
- vRect
.xLeft
360 ,vRect
.yBottom
- vRect
.yTop
362 } // end of wxNotebook::SetPageSize
364 void wxNotebook::SetPadding (
365 const wxSize
& WXUNUSED(rPadding
)
369 // No padding in OS/2
371 } // end of wxNotebook::SetPadding
373 void wxNotebook::SetTabSize (
377 ::WinSendMsg( GetHWND()
379 ,MPFROM2SHORT( (USHORT
)rSize
.x
382 ,(MPARAM
)BKA_MAJORTAB
384 } // end of wxNotebook::SetTabSize
386 // ----------------------------------------------------------------------------
387 // wxNotebook operations
388 // ----------------------------------------------------------------------------
391 // Remove one page from the notebook, without deleting
393 wxNotebookPage
* wxNotebook::DoRemovePage (
397 wxNotebookPage
* pPageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
403 ULONG ulPageId
= (ULONG
)m_alPageId
[nPage
];
405 ::WinSendMsg( GetHWND()
407 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
410 if (m_pages
.IsEmpty())
413 // No selection any more, the notebook becamse empty
417 else // notebook still not empty
420 // Change the selected page if it was deleted or became invalid
424 if (m_nSelection
== GetPageCount())
427 // Last page deleted, make the new last page the new selection
429 nSelNew
= m_nSelection
- 1;
431 else if (nPage
<= m_nSelection
)
434 // We must show another page, even if it has the same index
436 nSelNew
= m_nSelection
;
438 else // nothing changes for the currently selected page
443 // We still must refresh the current page: this needs to be done
444 // for some unknown reason if the tab control shows the up-down
445 // control (i.e. when there are too many pages) -- otherwise after
446 // deleting a page nothing at all is shown
448 m_pages
[m_nSelection
]->Refresh();
454 // m_nSelection must be always valid so reset it before calling
458 SetSelection(nSelNew
);
462 } // end of wxNotebook::DoRemovePage
467 bool wxNotebook::DeleteAllPages()
469 int nPageCount
= GetPageCount();
472 for (nPage
= 0; nPage
< nPageCount
; nPage
++)
473 delete m_pages
[nPage
];
475 ::WinSendMsg( GetHWND()
482 } // end of wxNotebook::DeleteAllPages
485 // Add a page to the notebook
487 bool wxNotebook::AddPage (
488 wxNotebookPage
* pPage
489 , const wxString
& rStrText
494 return InsertPage( GetPageCount()
500 } // end of wxNotebook::AddPage
503 // Same as AddPage() but does it at given position
505 bool wxNotebook::InsertPage (
507 , wxNotebookPage
* pPage
508 , const wxString
& rsStrText
515 wxASSERT( pPage
!= NULL
);
516 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
519 // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV. Requires
520 // two different calls to the API. Page 1 uses the BKA_FIRST. Subsequent
521 // pages use the previous page ID coupled with a BKA_NEXT call. Unlike
522 // Windows, OS/2 uses an internal Page ID to ID the pages.
524 // OS/2 also has a nice auto-size feature that automatically sizes the
525 // the attached window so we don't have to worry about the size of the
526 // window on the page.
530 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
533 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_FIRST
)
540 vError
= ::WinGetLastError(vHabmain
);
541 sError
= wxPMErrorToStr(vError
);
544 m_alPageId
.Insert((long)ulApiPage
, nPage
);
548 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
550 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
- 1])
551 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_NEXT
)
558 vError
= ::WinGetLastError(vHabmain
);
559 sError
= wxPMErrorToStr(vError
);
562 m_alPageId
.Insert((long)ulApiPage
, nPage
);
566 // Associate a window handle with the page
570 if (!::WinSendMsg( GetHWND()
571 ,BKM_SETPAGEWINDOWHWND
572 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
573 ,MPFROMHWND(pPage
->GetHWND())
578 // If the inserted page is before the selected one, we must update the
579 // index of the selected page
581 if (nPage
<= m_nSelection
)
584 // One extra page added
592 // Save the pointer to the page
594 m_pages
.Insert( pPage
600 // Now set TAB dimenstions
603 wxWindowDC
vDC(this);
607 vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
);
610 if (nTextX
> m_nTabSize
)
613 ::WinSendMsg( GetHWND()
615 ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
)
616 ,(MPARAM
)BKA_MAJORTAB
620 // Now set any TAB text
622 if (!rsStrText
.IsEmpty())
624 if (!SetPageText( nPage
631 // Now set any TAB bitmap image
635 if (!SetPageImage( nPage
644 // Don't show pages by default (we'll need to adjust their size first)
646 HWND hWnd
= GetWinHwnd(pPage
);
648 WinSetWindowULong( hWnd
650 ,WinQueryWindowULong( hWnd
656 // This updates internal flag too - otherwise it will get out of sync
662 // Some page should be selected: either this one or the first one if there is
663 // still no selection
669 else if ( m_nSelection
== -1 )
673 SetSelection(nSelNew
);
675 } // end of wxNotebook::InsertPage
677 // ----------------------------------------------------------------------------
678 // wxNotebook callbacks
679 // ----------------------------------------------------------------------------
680 void wxNotebook::OnSize(
685 int nCount
= (int)m_pages
.Count();
687 for (nPage
= 0; nPage
< nCount
; nPage
++)
689 if (m_nSelection
== nPage
)
690 m_pages
[nPage
]->Refresh();
692 ::WinSetWindowPos(m_pages
[nPage
]->GetHWND()
699 } // end of wxNotebook::OnSize
701 void wxNotebook::OnSelChange (
702 wxNotebookEvent
& rEvent
706 // Is it our tab control?
708 if (rEvent
.GetEventObject() == this)
710 int nPageCount
= GetPageCount();
712 ULONG ulOS2Sel
= (ULONG
)rEvent
.GetOldSelection();
715 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
717 if (ulOS2Sel
== m_alPageId
[nSel
])
727 m_pages
[nSel
]->Show(FALSE
);
729 ulOS2Sel
= (ULONG
)rEvent
.GetSelection();
733 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
735 if (ulOS2Sel
== m_alPageId
[nSel
])
745 wxNotebookPage
* pPage
= m_pages
[nSel
];
752 // We want to give others a chance to process this message as well
755 } // end of wxNotebook::OnSelChange
757 void wxNotebook::OnSetFocus (
762 // This function is only called when the focus is explicitly set (i.e. from
763 // the program) to the notebook - in this case we don't need the
764 // complicated OnNavigationKey() logic because the programmer knows better
767 // set focus to the currently selected page if any
769 if (m_nSelection
!= -1)
770 m_pages
[m_nSelection
]->SetFocus();
772 } // end of wxNotebook::OnSetFocus
774 void wxNotebook::OnNavigationKey (
775 wxNavigationKeyEvent
& rEvent
778 if (rEvent
.IsWindowChange())
783 AdvanceSelection(rEvent
.GetDirection());
788 // We get this event in 2 cases
790 // a) one of our pages might have generated it because the user TABbed
791 // out from it in which case we should propagate the event upwards and
792 // our parent will take care of setting the focus to prev/next sibling
796 // b) the parent panel wants to give the focus to us so that we
797 // forward it to our selected page. We can't deal with this in
798 // OnSetFocus() because we don't know which direction the focus came
799 // from in this case and so can't choose between setting the focus to
800 // first or last panel child
802 wxWindow
* pParent
= GetParent();
804 if (rEvent
.GetEventObject() == pParent
)
807 // No, it doesn't come from child, case (b): forward to a page
809 if (m_nSelection
!= -1)
812 // So that the page knows that the event comes from it's parent
813 // and is being propagated downwards
815 rEvent
.SetEventObject(this);
817 wxWindow
* pPage
= m_pages
[m_nSelection
];
819 if (!pPage
->GetEventHandler()->ProcessEvent(rEvent
))
823 //else: page manages focus inside it itself
828 // We have no pages - still have to give focus to _something_
836 // It comes from our child, case (a), pass to the parent
840 rEvent
.SetCurrentFocus(this);
841 pParent
->GetEventHandler()->ProcessEvent(rEvent
);
845 } // end of wxNotebook::OnNavigationKey
847 // ----------------------------------------------------------------------------
848 // wxNotebook base class virtuals
849 // ----------------------------------------------------------------------------
852 // Override these 2 functions to do nothing: everything is done in OnSize
854 void wxNotebook::SetConstraintSizes(
855 bool WXUNUSED(bRecurse
)
859 // Don't set the sizes of the pages - their correct size is not yet known
861 wxControl::SetConstraintSizes(FALSE
);
862 } // end of wxNotebook::SetConstraintSizes
864 bool wxNotebook::DoPhase (
869 } // end of wxNotebook::DoPhase
871 // ----------------------------------------------------------------------------
872 // wxNotebook Windows message handlers
873 // ----------------------------------------------------------------------------
874 bool wxNotebook::OS2OnScroll (
882 // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
887 return wxNotebookBase::OS2OnScroll( nOrientation
892 } // end of wxNotebook::OS2OnScroll
894 // ----------------------------------------------------------------------------
895 // wxNotebook helper functions
896 // ----------------------------------------------------------------------------
899 // Generate the page changing and changed events, hide the currently active
900 // panel and show the new one
902 void wxNotebook::ChangePage (
907 static bool sbInsideChangePage
= FALSE
;
910 // When we call ProcessEvent(), our own OnSelChange() is called which calls
911 // this function - break the infinite loop
913 if (sbInsideChangePage
)
917 // It's not an error (the message may be generated by the tab control itself)
918 // and it may happen - just do nothing
923 sbInsideChangePage
= TRUE
;
925 wxNotebookEvent
rEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
929 rEvent
.SetSelection(nSel
);
930 rEvent
.SetOldSelection(nOldSel
);
931 rEvent
.SetEventObject(this);
932 if (GetEventHandler()->ProcessEvent(rEvent
) && !rEvent
.IsAllowed())
935 // Program doesn't allow the page change
937 sbInsideChangePage
= FALSE
;
940 rEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
941 GetEventHandler()->ProcessEvent(rEvent
);
942 sbInsideChangePage
= FALSE
;
943 } // end of wxNotebook::ChangePage
945 #endif // wxUSE_NOTEBOOK