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/dcclient.h"
21 #include "wx/string.h"
22 #include "wx/settings.h"
26 #include "wx/imaglist.h"
28 #include "wx/control.h"
29 #include "wx/notebook.h"
31 #include "wx/os2/private.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // check that the page index is valid
38 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
41 #define m_hWnd (HWND)GetHWND()
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
54 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
55 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
56 EVT_SIZE(wxNotebook::OnSize
)
57 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
58 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
61 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
62 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxNotebook construction
70 // ----------------------------------------------------------------------------
73 // Common part of all ctors
75 void wxNotebook::Init()
80 } // end of wxNotebook::Init
83 // Default for dynamic class
85 wxNotebook::wxNotebook()
88 } // end of wxNotebook::wxNotebook
91 // The same arguments as for wxControl
93 wxNotebook::wxNotebook(
99 , const wxString
& rsName
110 } // end of wxNotebook::wxNotebook
115 bool wxNotebook::Create(
118 , const wxPoint
& rPos
119 , const wxSize
& rSize
121 , const wxString
& rsName
127 if (!CreateControl( pParent
138 // Notebook, so explicitly specify 0 as last parameter
140 if (!OS2CreateControl( "NOTEBOOK"
144 ,lStyle
| wxTAB_TRAVERSAL
148 SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
150 } // end of wxNotebook::Create
152 WXDWORD
wxNotebook::OS2GetStyle (
154 , WXDWORD
* pdwExstyle
157 WXDWORD dwTabStyle
= wxControl::OS2GetStyle( lStyle
161 dwTabStyle
|= WS_TABSTOP
| BKS_SOLIDBIND
| BKS_ROUNDEDTABS
| BKS_TABTEXTCENTER
;
163 if (lStyle
& wxNB_BOTTOM
)
164 dwTabStyle
|= BKS_MAJORTABBOTTOM
| BKS_BACKPAGESBL
;
165 else if (lStyle
& wxNB_RIGHT
)
166 dwTabStyle
|= BKS_MAJORTABRIGHT
| BKS_BACKPAGESBR
;
167 else if (lStyle
& wxNB_LEFT
)
168 dwTabStyle
|= BKS_MAJORTABLEFT
| BKS_BACKPAGESTL
;
169 else // default to top
170 dwTabStyle
|= BKS_MAJORTABTOP
| BKS_BACKPAGESTR
;
178 // Note that we never want to have the default WS_EX_CLIENTEDGE style
179 // as it looks too ugly for the notebooks
184 } // end of wxNotebook::OS2GetStyle
186 // ----------------------------------------------------------------------------
187 // wxNotebook accessors
188 // ----------------------------------------------------------------------------
190 size_t wxNotebook::GetPageCount() const
195 wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
));
196 return m_pages
.Count();
197 } // end of wxNotebook::GetPageCount
199 int wxNotebook::GetRowCount() const
201 return (int)::WinSendMsg( GetHWND()
206 } // end of wxNotebook::GetRowCount
208 int wxNotebook::SetSelection(
212 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
214 if (nPage
!= (size_t)m_nSelection
)
216 wxNotebookEvent
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
220 vEvent
.SetSelection(nPage
);
221 vEvent
.SetOldSelection(m_nSelection
);
222 vEvent
.SetEventObject(this);
223 if (!GetEventHandler()->ProcessEvent(vEvent
) || vEvent
.IsAllowed())
227 // Program allows the page change
229 vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
230 GetEventHandler()->ProcessEvent(vEvent
);
232 ::WinSendMsg( GetHWND()
234 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
239 m_nSelection
= nPage
;
241 } // end of wxNotebook::SetSelection
243 bool wxNotebook::SetPageText(
245 , const wxString
& rsStrText
248 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, wxT("notebook page out of range") );
249 return (bool)::WinSendMsg( m_hWnd
251 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
252 ,MPFROMP((PSZ
)rsStrText
.c_str())
254 } // end of wxNotebook::SetPageText
256 wxString
wxNotebook::GetPageText (
265 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxT(""), wxT("notebook page out of range") );
267 memset(&vBookText
, '\0', sizeof(BOOKTEXT
));
268 vBookText
.textLen
= 0; // This will get the length
269 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
271 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
274 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
276 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
)
278 wxLogError(wxT("Invalid Page Id for page text querry."));
280 return wxEmptyString
;
282 vBookText
.textLen
= ulRc
+ 1; // To get the null terminator
283 vBookText
.pString
= zBuf
;
286 // Now get the actual text
288 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
290 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
293 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
295 return wxEmptyString
;
300 vBookText
.pString
[ulRc
] = '\0';
301 sStr
= vBookText
.pString
;
303 } // end of wxNotebook::GetPageText
305 int wxNotebook::GetPageImage (
309 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, wxT("notebook page out of range") );
312 // For OS/2 just return the page
315 } // end of wxNotebook::GetPageImage
317 bool wxNotebook::SetPageImage (
322 wxBitmap
* pBitmap
= (wxBitmap
*)m_imageList
->GetBitmap(nImage
);
324 return (bool)::WinSendMsg( GetHWND()
326 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
327 ,(MPARAM
)pBitmap
->GetHBITMAP()
329 } // end of wxNotebook::SetPageImage
331 void wxNotebook::SetImageList (
332 wxImageList
* WXUNUSED(pImageList
)
336 // Does nothing under OS/2
338 } // end of wxNotebook::SetImageList
340 // ----------------------------------------------------------------------------
341 // wxNotebook size settings
342 // ----------------------------------------------------------------------------
343 void wxNotebook::SetPageSize (
350 // Transform the page size into the notebook size
352 vRect
.xLeft
= vRect
.yTop
= 0;
353 vRect
.xRight
= rSize
.x
;
354 vRect
.yBottom
= rSize
.y
;
360 SetSize( vRect
.xRight
- vRect
.xLeft
361 ,vRect
.yBottom
- vRect
.yTop
363 } // end of wxNotebook::SetPageSize
365 void wxNotebook::SetPadding (
366 const wxSize
& WXUNUSED(rPadding
)
370 // No padding in OS/2
372 } // end of wxNotebook::SetPadding
374 void wxNotebook::SetTabSize (
378 ::WinSendMsg( GetHWND()
380 ,MPFROM2SHORT( (USHORT
)rSize
.x
383 ,(MPARAM
)BKA_MAJORTAB
385 } // end of wxNotebook::SetTabSize
387 // ----------------------------------------------------------------------------
388 // wxNotebook operations
389 // ----------------------------------------------------------------------------
392 // Remove one page from the notebook, without deleting
394 wxNotebookPage
* wxNotebook::DoRemovePage (
398 wxNotebookPage
* pPageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
403 ::WinSendMsg( GetHWND()
405 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
408 if (m_pages
.IsEmpty())
411 // No selection any more, the notebook becamse empty
415 else // notebook still not empty
418 // Change the selected page if it was deleted or became invalid
422 if (m_nSelection
== (int)GetPageCount())
425 // Last page deleted, make the new last page the new selection
427 nSelNew
= m_nSelection
- 1;
429 else if (nPage
<= (size_t)m_nSelection
)
432 // We must show another page, even if it has the same index
434 nSelNew
= m_nSelection
;
436 else // nothing changes for the currently selected page
441 // We still must refresh the current page: this needs to be done
442 // for some unknown reason if the tab control shows the up-down
443 // control (i.e. when there are too many pages) -- otherwise after
444 // deleting a page nothing at all is shown
446 m_pages
[m_nSelection
]->Refresh();
452 // m_nSelection must be always valid so reset it before calling
456 SetSelection(nSelNew
);
460 } // end of wxNotebook::DoRemovePage
465 bool wxNotebook::DeleteAllPages()
467 int nPageCount
= GetPageCount();
470 for (nPage
= 0; nPage
< nPageCount
; nPage
++)
471 delete m_pages
[nPage
];
473 ::WinSendMsg( GetHWND()
480 } // end of wxNotebook::DeleteAllPages
483 // Add a page to the notebook
485 bool wxNotebook::AddPage (
486 wxNotebookPage
* pPage
487 , const wxString
& rStrText
492 return InsertPage( GetPageCount()
498 } // end of wxNotebook::AddPage
501 // Same as AddPage() but does it at given position
503 bool wxNotebook::InsertPage (
505 , wxNotebookPage
* pPage
506 , const wxString
& rsStrText
513 wxASSERT( pPage
!= NULL
);
514 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
517 // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV. Requires
518 // two different calls to the API. Page 1 uses the BKA_FIRST. Subsequent
519 // pages use the previous page ID coupled with a BKA_NEXT call. Unlike
520 // Windows, OS/2 uses an internal Page ID to ID the pages.
522 // OS/2 also has a nice auto-size feature that automatically sizes the
523 // the attached window so we don't have to worry about the size of the
524 // window on the page.
528 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
531 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_FIRST
)
538 vError
= ::WinGetLastError(vHabmain
);
539 sError
= wxPMErrorToStr(vError
);
542 m_alPageId
.Insert((long)ulApiPage
, nPage
);
546 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
548 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
- 1])
549 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_NEXT
)
556 vError
= ::WinGetLastError(vHabmain
);
557 sError
= wxPMErrorToStr(vError
);
560 m_alPageId
.Insert((long)ulApiPage
, nPage
);
564 // Associate a window handle with the page
568 if (!::WinSendMsg( GetHWND()
569 ,BKM_SETPAGEWINDOWHWND
570 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
571 ,MPFROMHWND(pPage
->GetHWND())
576 // If the inserted page is before the selected one, we must update the
577 // index of the selected page
579 if (nPage
<= (size_t)m_nSelection
)
582 // One extra page added
590 // Save the pointer to the page
592 m_pages
.Insert( pPage
598 // Now set TAB dimenstions
601 wxWindowDC
vDC(this);
605 vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
);
607 nTextX
= (wxCoord
)(nTextX
* 1.3);
608 if (nTextX
> m_nTabSize
)
611 ::WinSendMsg( GetHWND()
613 ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
)
614 ,(MPARAM
)BKA_MAJORTAB
618 // Now set any TAB text
620 if (!rsStrText
.IsEmpty())
622 if (!SetPageText( nPage
629 // Now set any TAB bitmap image
633 if (!SetPageImage( nPage
642 // Don't show pages by default (we'll need to adjust their size first)
644 HWND hWnd
= GetWinHwnd(pPage
);
646 WinSetWindowULong( hWnd
648 ,WinQueryWindowULong( hWnd
654 // This updates internal flag too - otherwise it will get out of sync
660 // Some page should be selected: either this one or the first one if there is
661 // still no selection
667 else if ( m_nSelection
== -1 )
671 SetSelection(nSelNew
);
673 } // end of wxNotebook::InsertPage
675 // ----------------------------------------------------------------------------
676 // wxNotebook callbacks
677 // ----------------------------------------------------------------------------
678 void wxNotebook::OnSize(
683 int nCount
= (int)m_pages
.Count();
685 for (nPage
= 0; nPage
< nCount
; nPage
++)
687 if (m_nSelection
== nPage
)
688 m_pages
[nPage
]->Refresh();
690 ::WinSetWindowPos(m_pages
[nPage
]->GetHWND()
697 } // end of wxNotebook::OnSize
699 void wxNotebook::OnSelChange (
700 wxNotebookEvent
& rEvent
704 // Is it our tab control?
706 if (rEvent
.GetEventObject() == this)
708 int nPageCount
= GetPageCount();
710 ULONG ulOS2Sel
= (ULONG
)rEvent
.GetOldSelection();
713 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
715 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
725 m_pages
[nSel
]->Show(FALSE
);
727 ulOS2Sel
= (ULONG
)rEvent
.GetSelection();
731 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
733 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
743 wxNotebookPage
* pPage
= m_pages
[nSel
];
750 // We want to give others a chance to process this message as well
753 } // end of wxNotebook::OnSelChange
755 void wxNotebook::OnSetFocus (
760 // This function is only called when the focus is explicitly set (i.e. from
761 // the program) to the notebook - in this case we don't need the
762 // complicated OnNavigationKey() logic because the programmer knows better
765 // set focus to the currently selected page if any
767 if (m_nSelection
!= -1)
768 m_pages
[m_nSelection
]->SetFocus();
770 } // end of wxNotebook::OnSetFocus
772 void wxNotebook::OnNavigationKey (
773 wxNavigationKeyEvent
& rEvent
776 if (rEvent
.IsWindowChange())
781 AdvanceSelection(rEvent
.GetDirection());
786 // We get this event in 2 cases
788 // a) one of our pages might have generated it because the user TABbed
789 // out from it in which case we should propagate the event upwards and
790 // our parent will take care of setting the focus to prev/next sibling
794 // b) the parent panel wants to give the focus to us so that we
795 // forward it to our selected page. We can't deal with this in
796 // OnSetFocus() because we don't know which direction the focus came
797 // from in this case and so can't choose between setting the focus to
798 // first or last panel child
800 wxWindow
* pParent
= GetParent();
802 if (rEvent
.GetEventObject() == pParent
)
805 // No, it doesn't come from child, case (b): forward to a page
807 if (m_nSelection
!= -1)
810 // So that the page knows that the event comes from it's parent
811 // and is being propagated downwards
813 rEvent
.SetEventObject(this);
815 wxWindow
* pPage
= m_pages
[m_nSelection
];
817 if (!pPage
->GetEventHandler()->ProcessEvent(rEvent
))
821 //else: page manages focus inside it itself
826 // We have no pages - still have to give focus to _something_
834 // It comes from our child, case (a), pass to the parent
838 rEvent
.SetCurrentFocus(this);
839 pParent
->GetEventHandler()->ProcessEvent(rEvent
);
843 } // end of wxNotebook::OnNavigationKey
845 // ----------------------------------------------------------------------------
846 // wxNotebook base class virtuals
847 // ----------------------------------------------------------------------------
850 // Override these 2 functions to do nothing: everything is done in OnSize
852 void wxNotebook::SetConstraintSizes(
853 bool WXUNUSED(bRecurse
)
857 // Don't set the sizes of the pages - their correct size is not yet known
859 wxControl::SetConstraintSizes(FALSE
);
860 } // end of wxNotebook::SetConstraintSizes
862 bool wxNotebook::DoPhase (
867 } // end of wxNotebook::DoPhase
869 // ----------------------------------------------------------------------------
870 // wxNotebook Windows message handlers
871 // ----------------------------------------------------------------------------
872 bool wxNotebook::OS2OnScroll (
880 // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
885 return wxNotebookBase::OS2OnScroll( nOrientation
890 } // end of wxNotebook::OS2OnScroll
892 #endif // wxUSE_NOTEBOOK