1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/notebook.cpp
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"
17 #include "wx/notebook.h"
22 #include "wx/dcclient.h"
23 #include "wx/string.h"
24 #include "wx/settings.h"
27 #include "wx/control.h"
30 #include "wx/imaglist.h"
32 #include "wx/os2/private.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // check that the page index is valid
39 #define IS_VALID_PAGE(nPage) ( \
40 /* size_t is _always_ >= 0 */ \
41 /* ((nPage) >= 0) && */ \
42 ((nPage) < GetPageCount()) \
46 #define m_hWnd (HWND)GetHWND()
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
59 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
60 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
61 EVT_SIZE(wxNotebook::OnSize
)
62 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
63 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
66 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxBookCtrlBase
)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wxNotebook construction
74 // ----------------------------------------------------------------------------
77 // Common part of all ctors
79 void wxNotebook::Init()
84 } // end of wxNotebook::Init
87 // Default for dynamic class
89 wxNotebook::wxNotebook()
92 } // end of wxNotebook::wxNotebook
95 // The same arguments as for wxControl
97 wxNotebook::wxNotebook(
100 , const wxPoint
& rPos
101 , const wxSize
& rSize
103 , const wxString
& rsName
114 } // end of wxNotebook::wxNotebook
119 bool wxNotebook::Create( wxWindow
* pParent
,
124 const wxString
& rsName
)
126 if ( (lStyle
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
131 if (!CreateControl( pParent
142 // Notebook, so explicitly specify 0 as last parameter
144 if (!OS2CreateControl( wxT("NOTEBOOK")
148 ,lStyle
| wxTAB_TRAVERSAL
152 SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
154 } // end of wxNotebook::Create
156 WXDWORD
wxNotebook::OS2GetStyle (
158 , WXDWORD
* pdwExstyle
161 WXDWORD dwTabStyle
= wxControl::OS2GetStyle( lStyle
165 dwTabStyle
|= WS_TABSTOP
| BKS_SOLIDBIND
| BKS_ROUNDEDTABS
| BKS_TABTEXTCENTER
| BKS_TABBEDDIALOG
;
167 if (lStyle
& wxBK_BOTTOM
)
168 dwTabStyle
|= BKS_MAJORTABBOTTOM
| BKS_BACKPAGESBL
;
169 else if (lStyle
& wxBK_RIGHT
)
170 dwTabStyle
|= BKS_MAJORTABRIGHT
| BKS_BACKPAGESBR
;
171 else if (lStyle
& wxBK_LEFT
)
172 dwTabStyle
|= BKS_MAJORTABLEFT
| BKS_BACKPAGESTL
;
173 else // default to top
174 dwTabStyle
|= BKS_MAJORTABTOP
| BKS_BACKPAGESTR
;
182 // Note that we never want to have the default WS_EX_CLIENTEDGE style
183 // as it looks too ugly for the notebooks
188 } // end of wxNotebook::OS2GetStyle
190 // ----------------------------------------------------------------------------
191 // wxNotebook accessors
192 // ----------------------------------------------------------------------------
194 size_t wxNotebook::GetPageCount() const
199 wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
));
200 return m_pages
.Count();
201 } // end of wxNotebook::GetPageCount
203 int wxNotebook::GetRowCount() const
205 return (int)::WinSendMsg( GetHWND()
210 } // end of wxNotebook::GetRowCount
212 int wxNotebook::SetSelection( size_t nPage
)
214 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
216 if (nPage
!= (size_t)m_nSelection
)
218 wxBookCtrlEvent
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
222 vEvent
.SetSelection(nPage
);
223 vEvent
.SetOldSelection(m_nSelection
);
224 vEvent
.SetEventObject(this);
225 if (!HandleWindowEvent(vEvent
) || vEvent
.IsAllowed())
229 // Program allows the page change
231 vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
232 HandleWindowEvent(vEvent
);
234 ::WinSendMsg( GetHWND()
236 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
241 m_nSelection
= nPage
;
243 } // end of wxNotebook::SetSelection
245 int wxNotebook::ChangeSelection( size_t nPage
)
247 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
249 if (nPage
!= (size_t)m_nSelection
)
251 ::WinSendMsg( GetHWND()
253 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
257 m_nSelection
= nPage
;
261 bool wxNotebook::SetPageText( size_t nPage
,
262 const wxString
& rsStrText
)
264 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
265 return (bool)::WinSendMsg( m_hWnd
267 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
268 ,MPFROMP((const char*)rsStrText
.c_str())
270 } // end of wxNotebook::SetPageText
272 wxString
wxNotebook::GetPageText ( size_t nPage
) const
279 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
281 memset(&vBookText
, '\0', sizeof(BOOKTEXT
));
282 vBookText
.textLen
= 0; // This will get the length
283 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
285 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
288 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
290 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
)
292 wxLogError(wxT("Invalid Page Id for page text querry."));
294 return wxEmptyString
;
296 vBookText
.textLen
= ulRc
+ 1; // To get the null terminator
297 vBookText
.pString
= (char*)zBuf
;
300 // Now get the actual text
302 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
304 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
307 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
309 return wxEmptyString
;
314 vBookText
.pString
[ulRc
] = '\0';
315 sStr
= (wxChar
*)vBookText
.pString
;
317 } // end of wxNotebook::GetPageText
319 int wxNotebook::GetPageImage ( size_t nPage
) const
321 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
324 // For OS/2 just return the page
327 } // end of wxNotebook::GetPageImage
329 bool wxNotebook::SetPageImage (
334 wxBitmap vBitmap
= (wxBitmap
)m_imageList
->GetBitmap(nImage
);
336 return (bool)::WinSendMsg( GetHWND()
338 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
339 ,(MPARAM
)wxCopyBmp(vBitmap
.GetHBITMAP(), true)
341 } // end of wxNotebook::SetPageImage
343 void wxNotebook::SetImageList (
344 wxImageList
* pImageList
348 // Does not really do anything yet, but at least we need to
349 // update the base class.
351 wxNotebookBase::SetImageList(pImageList
);
352 } // end of wxNotebook::SetImageList
354 // ----------------------------------------------------------------------------
355 // wxNotebook size settings
356 // ----------------------------------------------------------------------------
357 void wxNotebook::SetPageSize (
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 ( size_t nPage
)
395 wxNotebookPage
* pPageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
400 ::WinSendMsg( GetHWND()
402 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
405 if (m_pages
.IsEmpty())
408 // No selection any more, the notebook becamse empty
412 else // notebook still not empty
415 // Change the selected page if it was deleted or became invalid
419 if (m_nSelection
== (int)GetPageCount())
422 // Last page deleted, make the new last page the new selection
424 nSelNew
= m_nSelection
- 1;
426 else if (nPage
<= (size_t)m_nSelection
)
429 // We must show another page, even if it has the same index
431 nSelNew
= m_nSelection
;
433 else // nothing changes for the currently selected page
438 // We still must refresh the current page: this needs to be done
439 // for some unknown reason if the tab control shows the up-down
440 // control (i.e. when there are too many pages) -- otherwise after
441 // deleting a page nothing at all is shown
443 m_pages
[m_nSelection
]->Refresh();
449 // m_nSelection must be always valid so reset it before calling
453 SetSelection(nSelNew
);
457 } // end of wxNotebook::DoRemovePage
462 bool wxNotebook::DeleteAllPages()
464 int nPageCount
= GetPageCount();
467 for (nPage
= 0; nPage
< nPageCount
; nPage
++)
468 delete m_pages
[nPage
];
470 ::WinSendMsg( GetHWND()
478 } // end of wxNotebook::DeleteAllPages
481 // Add a page to the notebook
483 bool wxNotebook::AddPage (
484 wxNotebookPage
* pPage
485 , const wxString
& rStrText
490 return InsertPage( GetPageCount()
496 } // end of wxNotebook::AddPage
499 // Same as AddPage() but does it at given position
501 bool wxNotebook::InsertPage ( size_t nPage
,
502 wxNotebookPage
* pPage
,
503 const wxString
& rsStrText
,
509 wxASSERT( pPage
!= NULL
);
510 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
513 // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV. Requires
514 // two different calls to the API. Page 1 uses the BKA_FIRST. Subsequent
515 // pages use the previous page ID coupled with a BKA_NEXT call. Unlike
516 // Windows, OS/2 uses an internal Page ID to ID the pages.
518 // OS/2 also has a nice auto-size feature that automatically sizes the
519 // the attached window so we don't have to worry about the size of the
520 // window on the page.
524 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
527 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_FIRST
)
534 vError
= ::WinGetLastError(vHabmain
);
535 sError
= wxPMErrorToStr(vError
);
538 m_alPageId
.Insert((long)ulApiPage
, nPage
);
542 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
544 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
- 1])
545 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_NEXT
)
552 vError
= ::WinGetLastError(vHabmain
);
553 sError
= wxPMErrorToStr(vError
);
556 m_alPageId
.Insert((long)ulApiPage
, nPage
);
560 // Associate a window handle with the page
564 if (!::WinSendMsg( GetHWND()
565 ,BKM_SETPAGEWINDOWHWND
566 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
567 ,MPFROMHWND(pPage
->GetHWND())
572 // If the inserted page is before the selected one, we must update the
573 // index of the selected page
575 if (nPage
<= (size_t)m_nSelection
)
578 // One extra page added
586 // Save the pointer to the page
588 m_pages
.Insert( pPage
594 // Now set TAB dimenstions
597 wxWindowDC
vDC(this);
601 vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
);
603 nTextX
= (wxCoord
)(nTextX
* 1.3);
604 if (nTextX
> m_nTabSize
)
607 ::WinSendMsg( GetHWND()
609 ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
)
610 ,(MPARAM
)BKA_MAJORTAB
614 // Now set any TAB text
616 if (!rsStrText
.empty())
618 if (!SetPageText( nPage
625 // Now set any TAB bitmap image
629 if (!SetPageImage( nPage
638 // Don't show pages by default (we'll need to adjust their size first)
640 HWND hWnd
= GetWinHwnd(pPage
);
642 WinSetWindowULong( hWnd
644 ,WinQueryWindowULong( hWnd
650 // This updates internal flag too - otherwise it will get out of sync
656 // Some page should be selected: either this one or the first one if there is
657 // still no selection
663 else if ( m_nSelection
== -1 )
667 SetSelection(nSelNew
);
669 InvalidateBestSize();
672 } // end of wxNotebook::InsertPage
674 // ----------------------------------------------------------------------------
675 // wxNotebook callbacks
676 // ----------------------------------------------------------------------------
677 void wxNotebook::OnSize(
682 } // end of wxNotebook::OnSize
684 void wxNotebook::OnSelChange (
685 wxBookCtrlEvent
& rEvent
689 // Is it our tab control?
691 if (rEvent
.GetEventObject() == this)
693 int nPageCount
= GetPageCount();
695 ULONG ulOS2Sel
= (ULONG
)rEvent
.GetOldSelection();
698 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
700 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
710 m_pages
[nSel
]->Show(false);
712 ulOS2Sel
= (ULONG
)rEvent
.GetSelection();
716 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
718 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
728 wxNotebookPage
* pPage
= m_pages
[nSel
];
735 // We want to give others a chance to process this message as well
738 } // end of wxNotebook::OnSelChange
740 void wxNotebook::OnSetFocus (
745 // This function is only called when the focus is explicitly set (i.e. from
746 // the program) to the notebook - in this case we don't need the
747 // complicated OnNavigationKey() logic because the programmer knows better
750 // set focus to the currently selected page if any
752 if (m_nSelection
!= -1)
753 m_pages
[m_nSelection
]->SetFocus();
755 } // end of wxNotebook::OnSetFocus
757 void wxNotebook::OnNavigationKey (
758 wxNavigationKeyEvent
& rEvent
761 if (rEvent
.IsWindowChange())
766 AdvanceSelection(rEvent
.GetDirection());
771 // We get this event in 2 cases
773 // a) one of our pages might have generated it because the user TABbed
774 // out from it in which case we should propagate the event upwards and
775 // our parent will take care of setting the focus to prev/next sibling
779 // b) the parent panel wants to give the focus to us so that we
780 // forward it to our selected page. We can't deal with this in
781 // OnSetFocus() because we don't know which direction the focus came
782 // from in this case and so can't choose between setting the focus to
783 // first or last panel child
785 wxWindow
* pParent
= GetParent();
787 if (rEvent
.GetEventObject() == pParent
)
790 // No, it doesn't come from child, case (b): forward to a page
792 if (m_nSelection
!= -1)
795 // So that the page knows that the event comes from it's parent
796 // and is being propagated downwards
798 rEvent
.SetEventObject(this);
800 wxWindow
* pPage
= m_pages
[m_nSelection
];
802 if (!pPage
->HandleWindowEvent(rEvent
))
806 //else: page manages focus inside it itself
811 // We have no pages - still have to give focus to _something_
819 // It comes from our child, case (a), pass to the parent
823 rEvent
.SetCurrentFocus(this);
824 pParent
->HandleWindowEvent(rEvent
);
828 } // end of wxNotebook::OnNavigationKey
830 // ----------------------------------------------------------------------------
831 // wxNotebook base class virtuals
832 // ----------------------------------------------------------------------------
835 // Override these 2 functions to do nothing: everything is done in OnSize
837 void wxNotebook::SetConstraintSizes( bool WXUNUSED(bRecurse
) )
840 // Don't set the sizes of the pages - their correct size is not yet known
842 wxControl::SetConstraintSizes(false);
843 } // end of wxNotebook::SetConstraintSizes
845 bool wxNotebook::DoPhase ( int WXUNUSED(nPhase
) )
848 } // end of wxNotebook::DoPhase
850 // ----------------------------------------------------------------------------
851 // wxNotebook Windows message handlers
852 // ----------------------------------------------------------------------------
853 bool wxNotebook::OS2OnScroll ( int nOrientation
,
859 // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
864 return wxNotebookBase::OS2OnScroll( nOrientation
869 } // end of wxNotebook::OS2OnScroll
871 #endif // wxUSE_NOTEBOOK