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
, wxControl
)
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
, wxControl
)
67 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxNotebook construction
75 // ----------------------------------------------------------------------------
78 // Common part of all ctors
80 void wxNotebook::Init()
85 } // end of wxNotebook::Init
88 // Default for dynamic class
90 wxNotebook::wxNotebook()
93 } // end of wxNotebook::wxNotebook
96 // The same arguments as for wxControl
98 wxNotebook::wxNotebook(
101 , const wxPoint
& rPos
102 , const wxSize
& rSize
104 , const wxString
& rsName
115 } // end of wxNotebook::wxNotebook
120 bool wxNotebook::Create( wxWindow
* pParent
,
125 const wxString
& rsName
)
130 if (!CreateControl( pParent
141 // Notebook, so explicitly specify 0 as last parameter
143 if (!OS2CreateControl( wxT("NOTEBOOK")
147 ,lStyle
| wxTAB_TRAVERSAL
151 SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
153 } // end of wxNotebook::Create
155 WXDWORD
wxNotebook::OS2GetStyle (
157 , WXDWORD
* pdwExstyle
160 WXDWORD dwTabStyle
= wxControl::OS2GetStyle( lStyle
164 dwTabStyle
|= WS_TABSTOP
| BKS_SOLIDBIND
| BKS_ROUNDEDTABS
| BKS_TABTEXTCENTER
| BKS_TABBEDDIALOG
;
166 if (lStyle
& wxBK_BOTTOM
)
167 dwTabStyle
|= BKS_MAJORTABBOTTOM
| BKS_BACKPAGESBL
;
168 else if (lStyle
& wxBK_RIGHT
)
169 dwTabStyle
|= BKS_MAJORTABRIGHT
| BKS_BACKPAGESBR
;
170 else if (lStyle
& wxBK_LEFT
)
171 dwTabStyle
|= BKS_MAJORTABLEFT
| BKS_BACKPAGESTL
;
172 else // default to top
173 dwTabStyle
|= BKS_MAJORTABTOP
| BKS_BACKPAGESTR
;
181 // Note that we never want to have the default WS_EX_CLIENTEDGE style
182 // as it looks too ugly for the notebooks
187 } // end of wxNotebook::OS2GetStyle
189 // ----------------------------------------------------------------------------
190 // wxNotebook accessors
191 // ----------------------------------------------------------------------------
193 size_t wxNotebook::GetPageCount() const
198 wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
));
199 return m_pages
.Count();
200 } // end of wxNotebook::GetPageCount
202 int wxNotebook::GetRowCount() const
204 return (int)::WinSendMsg( GetHWND()
209 } // end of wxNotebook::GetRowCount
211 int wxNotebook::SetSelection( size_t nPage
)
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
215 if (nPage
!= (size_t)m_nSelection
)
217 wxNotebookEvent
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
221 vEvent
.SetSelection(nPage
);
222 vEvent
.SetOldSelection(m_nSelection
);
223 vEvent
.SetEventObject(this);
224 if (!GetEventHandler()->ProcessEvent(vEvent
) || vEvent
.IsAllowed())
228 // Program allows the page change
230 vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
231 GetEventHandler()->ProcessEvent(vEvent
);
233 ::WinSendMsg( GetHWND()
235 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
240 m_nSelection
= nPage
;
242 } // end of wxNotebook::SetSelection
244 bool wxNotebook::SetPageText( size_t nPage
,
245 const wxString
& rsStrText
)
247 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
248 return (bool)::WinSendMsg( m_hWnd
250 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
251 ,MPFROMP((PSZ
)rsStrText
.c_str())
253 } // end of wxNotebook::SetPageText
255 wxString
wxNotebook::GetPageText ( size_t nPage
) const
262 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
264 memset(&vBookText
, '\0', sizeof(BOOKTEXT
));
265 vBookText
.textLen
= 0; // This will get the length
266 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
268 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
271 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
273 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
)
275 wxLogError(wxT("Invalid Page Id for page text querry."));
277 return wxEmptyString
;
279 vBookText
.textLen
= ulRc
+ 1; // To get the null terminator
280 vBookText
.pString
= (char*)zBuf
;
283 // Now get the actual text
285 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
287 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
290 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
292 return wxEmptyString
;
297 vBookText
.pString
[ulRc
] = '\0';
298 sStr
= (wxChar
*)vBookText
.pString
;
300 } // end of wxNotebook::GetPageText
302 int wxNotebook::GetPageImage ( size_t nPage
) const
304 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
307 // For OS/2 just return the page
310 } // end of wxNotebook::GetPageImage
312 bool wxNotebook::SetPageImage (
317 wxBitmap vBitmap
= (wxBitmap
)m_imageList
->GetBitmap(nImage
);
319 return (bool)::WinSendMsg( GetHWND()
321 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
322 ,(MPARAM
)wxFlipBmp(vBitmap
.GetHBITMAP())
324 } // end of wxNotebook::SetPageImage
326 void wxNotebook::SetImageList (
327 wxImageList
* pImageList
331 // Does not really do anything yet, but at least we need to
332 // update the base class.
334 wxNotebookBase::SetImageList(pImageList
);
335 } // end of wxNotebook::SetImageList
337 // ----------------------------------------------------------------------------
338 // wxNotebook size settings
339 // ----------------------------------------------------------------------------
340 void wxNotebook::SetPageSize (
345 } // end of wxNotebook::SetPageSize
347 void wxNotebook::SetPadding (
348 const wxSize
& WXUNUSED(rPadding
)
352 // No padding in OS/2
354 } // end of wxNotebook::SetPadding
356 void wxNotebook::SetTabSize (
360 ::WinSendMsg( GetHWND()
362 ,MPFROM2SHORT( (USHORT
)rSize
.x
365 ,(MPARAM
)BKA_MAJORTAB
367 } // end of wxNotebook::SetTabSize
369 // ----------------------------------------------------------------------------
370 // wxNotebook operations
371 // ----------------------------------------------------------------------------
374 // Remove one page from the notebook, without deleting
376 wxNotebookPage
* wxNotebook::DoRemovePage ( size_t nPage
)
378 wxNotebookPage
* pPageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
383 ::WinSendMsg( GetHWND()
385 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
388 if (m_pages
.IsEmpty())
391 // No selection any more, the notebook becamse empty
395 else // notebook still not empty
398 // Change the selected page if it was deleted or became invalid
402 if (m_nSelection
== (int)GetPageCount())
405 // Last page deleted, make the new last page the new selection
407 nSelNew
= m_nSelection
- 1;
409 else if (nPage
<= (size_t)m_nSelection
)
412 // We must show another page, even if it has the same index
414 nSelNew
= m_nSelection
;
416 else // nothing changes for the currently selected page
421 // We still must refresh the current page: this needs to be done
422 // for some unknown reason if the tab control shows the up-down
423 // control (i.e. when there are too many pages) -- otherwise after
424 // deleting a page nothing at all is shown
426 m_pages
[m_nSelection
]->Refresh();
432 // m_nSelection must be always valid so reset it before calling
436 SetSelection(nSelNew
);
440 } // end of wxNotebook::DoRemovePage
445 bool wxNotebook::DeleteAllPages()
447 int nPageCount
= GetPageCount();
450 for (nPage
= 0; nPage
< nPageCount
; nPage
++)
451 delete m_pages
[nPage
];
453 ::WinSendMsg( GetHWND()
461 } // end of wxNotebook::DeleteAllPages
464 // Add a page to the notebook
466 bool wxNotebook::AddPage (
467 wxNotebookPage
* pPage
468 , const wxString
& rStrText
473 return InsertPage( GetPageCount()
479 } // end of wxNotebook::AddPage
482 // Same as AddPage() but does it at given position
484 bool wxNotebook::InsertPage ( size_t nPage
,
485 wxNotebookPage
* pPage
,
486 const wxString
& rsStrText
,
492 wxASSERT( pPage
!= NULL
);
493 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
496 // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV. Requires
497 // two different calls to the API. Page 1 uses the BKA_FIRST. Subsequent
498 // pages use the previous page ID coupled with a BKA_NEXT call. Unlike
499 // Windows, OS/2 uses an internal Page ID to ID the pages.
501 // OS/2 also has a nice auto-size feature that automatically sizes the
502 // the attached window so we don't have to worry about the size of the
503 // window on the page.
507 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
510 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_FIRST
)
517 vError
= ::WinGetLastError(vHabmain
);
518 sError
= wxPMErrorToStr(vError
);
521 m_alPageId
.Insert((long)ulApiPage
, nPage
);
525 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
527 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
- 1])
528 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_NEXT
)
535 vError
= ::WinGetLastError(vHabmain
);
536 sError
= wxPMErrorToStr(vError
);
539 m_alPageId
.Insert((long)ulApiPage
, nPage
);
543 // Associate a window handle with the page
547 if (!::WinSendMsg( GetHWND()
548 ,BKM_SETPAGEWINDOWHWND
549 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
550 ,MPFROMHWND(pPage
->GetHWND())
555 // If the inserted page is before the selected one, we must update the
556 // index of the selected page
558 if (nPage
<= (size_t)m_nSelection
)
561 // One extra page added
569 // Save the pointer to the page
571 m_pages
.Insert( pPage
577 // Now set TAB dimenstions
580 wxWindowDC
vDC(this);
584 vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
);
586 nTextX
= (wxCoord
)(nTextX
* 1.3);
587 if (nTextX
> m_nTabSize
)
590 ::WinSendMsg( GetHWND()
592 ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
)
593 ,(MPARAM
)BKA_MAJORTAB
597 // Now set any TAB text
599 if (!rsStrText
.empty())
601 if (!SetPageText( nPage
608 // Now set any TAB bitmap image
612 if (!SetPageImage( nPage
621 // Don't show pages by default (we'll need to adjust their size first)
623 HWND hWnd
= GetWinHwnd(pPage
);
625 WinSetWindowULong( hWnd
627 ,WinQueryWindowULong( hWnd
633 // This updates internal flag too - otherwise it will get out of sync
639 // Some page should be selected: either this one or the first one if there is
640 // still no selection
646 else if ( m_nSelection
== -1 )
650 SetSelection(nSelNew
);
652 InvalidateBestSize();
655 } // end of wxNotebook::InsertPage
657 // ----------------------------------------------------------------------------
658 // wxNotebook callbacks
659 // ----------------------------------------------------------------------------
660 void wxNotebook::OnSize(
665 } // end of wxNotebook::OnSize
667 void wxNotebook::OnSelChange (
668 wxNotebookEvent
& rEvent
672 // Is it our tab control?
674 if (rEvent
.GetEventObject() == this)
676 int nPageCount
= GetPageCount();
678 ULONG ulOS2Sel
= (ULONG
)rEvent
.GetOldSelection();
681 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
683 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
693 m_pages
[nSel
]->Show(false);
695 ulOS2Sel
= (ULONG
)rEvent
.GetSelection();
699 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
701 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
711 wxNotebookPage
* pPage
= m_pages
[nSel
];
718 // We want to give others a chance to process this message as well
721 } // end of wxNotebook::OnSelChange
723 void wxNotebook::OnSetFocus (
728 // This function is only called when the focus is explicitly set (i.e. from
729 // the program) to the notebook - in this case we don't need the
730 // complicated OnNavigationKey() logic because the programmer knows better
733 // set focus to the currently selected page if any
735 if (m_nSelection
!= -1)
736 m_pages
[m_nSelection
]->SetFocus();
738 } // end of wxNotebook::OnSetFocus
740 void wxNotebook::OnNavigationKey (
741 wxNavigationKeyEvent
& rEvent
744 if (rEvent
.IsWindowChange())
749 AdvanceSelection(rEvent
.GetDirection());
754 // We get this event in 2 cases
756 // a) one of our pages might have generated it because the user TABbed
757 // out from it in which case we should propagate the event upwards and
758 // our parent will take care of setting the focus to prev/next sibling
762 // b) the parent panel wants to give the focus to us so that we
763 // forward it to our selected page. We can't deal with this in
764 // OnSetFocus() because we don't know which direction the focus came
765 // from in this case and so can't choose between setting the focus to
766 // first or last panel child
768 wxWindow
* pParent
= GetParent();
770 if (rEvent
.GetEventObject() == pParent
)
773 // No, it doesn't come from child, case (b): forward to a page
775 if (m_nSelection
!= -1)
778 // So that the page knows that the event comes from it's parent
779 // and is being propagated downwards
781 rEvent
.SetEventObject(this);
783 wxWindow
* pPage
= m_pages
[m_nSelection
];
785 if (!pPage
->GetEventHandler()->ProcessEvent(rEvent
))
789 //else: page manages focus inside it itself
794 // We have no pages - still have to give focus to _something_
802 // It comes from our child, case (a), pass to the parent
806 rEvent
.SetCurrentFocus(this);
807 pParent
->GetEventHandler()->ProcessEvent(rEvent
);
811 } // end of wxNotebook::OnNavigationKey
813 // ----------------------------------------------------------------------------
814 // wxNotebook base class virtuals
815 // ----------------------------------------------------------------------------
818 // Override these 2 functions to do nothing: everything is done in OnSize
820 void wxNotebook::SetConstraintSizes( bool WXUNUSED(bRecurse
) )
823 // Don't set the sizes of the pages - their correct size is not yet known
825 wxControl::SetConstraintSizes(false);
826 } // end of wxNotebook::SetConstraintSizes
828 bool wxNotebook::DoPhase ( int WXUNUSED(nPhase
) )
831 } // end of wxNotebook::DoPhase
833 // ----------------------------------------------------------------------------
834 // wxNotebook Windows message handlers
835 // ----------------------------------------------------------------------------
836 bool wxNotebook::OS2OnScroll ( int nOrientation
,
842 // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
847 return wxNotebookBase::OS2OnScroll( nOrientation
852 } // end of wxNotebook::OS2OnScroll
854 #endif // wxUSE_NOTEBOOK