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 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
57 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY
, wxNotebook::OnSelChange
)
58 EVT_SIZE(wxNotebook::OnSize
)
59 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
60 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxNotebook construction
69 // ----------------------------------------------------------------------------
72 // Common part of all ctors
74 void wxNotebook::Init()
77 } // end of wxNotebook::Init
80 // Default for dynamic class
82 wxNotebook::wxNotebook()
85 } // end of wxNotebook::wxNotebook
88 // The same arguments as for wxControl
90 wxNotebook::wxNotebook(
96 , const wxString
& rsName
107 } // end of wxNotebook::wxNotebook
112 bool wxNotebook::Create( wxWindow
* pParent
,
117 const wxString
& rsName
)
119 if ( (lStyle
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
124 if (!CreateControl( pParent
135 // Notebook, so explicitly specify 0 as last parameter
137 if (!OS2CreateControl( wxT("NOTEBOOK")
141 ,lStyle
| wxTAB_TRAVERSAL
145 SetBackgroundColour(wxColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
147 } // end of wxNotebook::Create
149 WXDWORD
wxNotebook::OS2GetStyle (
151 , WXDWORD
* pdwExstyle
154 WXDWORD dwTabStyle
= wxControl::OS2GetStyle( lStyle
158 dwTabStyle
|= WS_TABSTOP
| BKS_SOLIDBIND
| BKS_ROUNDEDTABS
| BKS_TABTEXTCENTER
| BKS_TABBEDDIALOG
;
160 if (lStyle
& wxBK_BOTTOM
)
161 dwTabStyle
|= BKS_MAJORTABBOTTOM
| BKS_BACKPAGESBL
;
162 else if (lStyle
& wxBK_RIGHT
)
163 dwTabStyle
|= BKS_MAJORTABRIGHT
| BKS_BACKPAGESBR
;
164 else if (lStyle
& wxBK_LEFT
)
165 dwTabStyle
|= BKS_MAJORTABLEFT
| BKS_BACKPAGESTL
;
166 else // default to top
167 dwTabStyle
|= BKS_MAJORTABTOP
| BKS_BACKPAGESTR
;
175 // Note that we never want to have the default WS_EX_CLIENTEDGE style
176 // as it looks too ugly for the notebooks
181 } // end of wxNotebook::OS2GetStyle
183 // ----------------------------------------------------------------------------
184 // wxNotebook accessors
185 // ----------------------------------------------------------------------------
187 size_t wxNotebook::GetPageCount() const
192 wxASSERT((int)m_pages
.Count() == (int)::WinSendMsg(GetHWND(), BKM_QUERYPAGECOUNT
, (MPARAM
)0, (MPARAM
)BKA_END
));
193 return m_pages
.Count();
194 } // end of wxNotebook::GetPageCount
196 int wxNotebook::GetRowCount() const
198 return (int)::WinSendMsg( GetHWND()
203 } // end of wxNotebook::GetRowCount
205 int wxNotebook::SetSelection( size_t nPage
)
207 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
209 if (nPage
!= (size_t)m_selection
)
211 wxBookCtrlEvent
vEvent( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
215 vEvent
.SetSelection(nPage
);
216 vEvent
.SetOldSelection(m_selection
);
217 vEvent
.SetEventObject(this);
218 if (!HandleWindowEvent(vEvent
) || vEvent
.IsAllowed())
222 // Program allows the page change
224 vEvent
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
225 HandleWindowEvent(vEvent
);
227 ::WinSendMsg( GetHWND()
229 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
236 } // end of wxNotebook::SetSelection
238 int wxNotebook::ChangeSelection( size_t nPage
)
240 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
242 if (nPage
!= (size_t)m_selection
)
244 ::WinSendMsg( GetHWND()
246 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
254 bool wxNotebook::SetPageText( size_t nPage
,
255 const wxString
& rsStrText
)
257 wxCHECK_MSG( IS_VALID_PAGE(nPage
), false, wxT("notebook page out of range") );
258 return (bool)::WinSendMsg( m_hWnd
260 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
261 ,MPFROMP((const char*)rsStrText
.c_str())
263 } // end of wxNotebook::SetPageText
265 wxString
wxNotebook::GetPageText ( size_t nPage
) const
272 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxEmptyString
, wxT("notebook page out of range") );
274 memset(&vBookText
, '\0', sizeof(BOOKTEXT
));
275 vBookText
.textLen
= 0; // This will get the length
276 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
278 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
281 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
283 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
)
285 wxLogError(wxT("Invalid Page Id for page text querry."));
287 return wxEmptyString
;
289 vBookText
.textLen
= ulRc
+ 1; // To get the null terminator
290 vBookText
.pString
= (char*)zBuf
;
293 // Now get the actual text
295 ulRc
= LONGFROMMR(::WinSendMsg( m_hWnd
297 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
300 if (ulRc
== (ULONG
)BOOKERR_INVALID_PARAMETERS
|| ulRc
== 0L)
302 return wxEmptyString
;
307 vBookText
.pString
[ulRc
] = '\0';
308 sStr
= (wxChar
*)vBookText
.pString
;
310 } // end of wxNotebook::GetPageText
312 int wxNotebook::GetPageImage ( size_t nPage
) const
314 wxCHECK_MSG( IS_VALID_PAGE(nPage
), wxNOT_FOUND
, wxT("notebook page out of range") );
317 // For OS/2 just return the page
320 } // end of wxNotebook::GetPageImage
322 bool wxNotebook::SetPageImage (
327 wxBitmap vBitmap
= (wxBitmap
)GetImageList()->GetBitmap(nImage
);
329 return (bool)::WinSendMsg( GetHWND()
331 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
332 ,(MPARAM
)wxCopyBmp(vBitmap
.GetHBITMAP(), true)
334 } // end of wxNotebook::SetPageImage
336 void wxNotebook::SetImageList (
337 wxImageList
* pImageList
341 // Does not really do anything yet, but at least we need to
342 // update the base class.
344 wxNotebookBase::SetImageList(pImageList
);
345 } // end of wxNotebook::SetImageList
347 // ----------------------------------------------------------------------------
348 // wxNotebook size settings
349 // ----------------------------------------------------------------------------
350 void wxNotebook::SetPageSize (
355 } // end of wxNotebook::SetPageSize
357 void wxNotebook::SetPadding (
358 const wxSize
& WXUNUSED(rPadding
)
362 // No padding in OS/2
364 } // end of wxNotebook::SetPadding
366 void wxNotebook::SetTabSize (
370 ::WinSendMsg( GetHWND()
372 ,MPFROM2SHORT( (USHORT
)rSize
.x
375 ,(MPARAM
)BKA_MAJORTAB
377 } // end of wxNotebook::SetTabSize
379 // ----------------------------------------------------------------------------
380 // wxNotebook operations
381 // ----------------------------------------------------------------------------
384 // Remove one page from the notebook, without deleting
386 wxNotebookPage
* wxNotebook::DoRemovePage ( size_t nPage
)
388 wxNotebookPage
* pPageRemoved
= wxNotebookBase::DoRemovePage(nPage
);
393 ::WinSendMsg( GetHWND()
395 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
398 if (m_pages
.IsEmpty())
401 // No selection any more, the notebook becamse empty
403 m_selection
= wxNOT_FOUND
;
405 else // notebook still not empty
408 // Change the selected page if it was deleted or became invalid
412 if (m_selection
== (int)GetPageCount())
415 // Last page deleted, make the new last page the new selection
417 nSelNew
= m_selection
- 1;
419 else if (nPage
<= (size_t)m_selection
)
422 // We must show another page, even if it has the same index
424 nSelNew
= m_selection
;
426 else // nothing changes for the currently selected page
428 nSelNew
= wxNOT_FOUND
;
431 // We still must refresh the current page: this needs to be done
432 // for some unknown reason if the tab control shows the up-down
433 // control (i.e. when there are too many pages) -- otherwise after
434 // deleting a page nothing at all is shown
436 m_pages
[m_selection
]->Refresh();
439 if (nSelNew
!= wxNOT_FOUND
)
442 // m_selection must be always valid so reset it before calling
445 m_selection
= wxNOT_FOUND
;
446 SetSelection(nSelNew
);
450 } // end of wxNotebook::DoRemovePage
455 bool wxNotebook::DeleteAllPages()
457 int nPageCount
= GetPageCount();
460 for (nPage
= 0; nPage
< nPageCount
; nPage
++)
461 delete m_pages
[nPage
];
463 ::WinSendMsg( GetHWND()
468 m_selection
= wxNOT_FOUND
;
471 } // end of wxNotebook::DeleteAllPages
474 // Add a page to the notebook
476 bool wxNotebook::AddPage (
477 wxNotebookPage
* pPage
478 , const wxString
& rStrText
483 return InsertPage( GetPageCount()
489 } // end of wxNotebook::AddPage
492 // Same as AddPage() but does it at given position
494 bool wxNotebook::InsertPage ( size_t nPage
,
495 wxNotebookPage
* pPage
,
496 const wxString
& rsStrText
,
502 wxASSERT( pPage
!= NULL
);
503 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), false );
506 // Under OS/2 we can only insert FIRST, LAST, NEXT or PREV. Requires
507 // two different calls to the API. Page 1 uses the BKA_FIRST. Subsequent
508 // pages use the previous page ID coupled with a BKA_NEXT call. Unlike
509 // Windows, OS/2 uses an internal Page ID to ID the pages.
511 // OS/2 also has a nice auto-size feature that automatically sizes the
512 // the attached window so we don't have to worry about the size of the
513 // window on the page.
517 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
520 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_FIRST
)
527 vError
= ::WinGetLastError(vHabmain
);
528 sError
= wxPMErrorToStr(vError
);
531 m_alPageId
.Insert((long)ulApiPage
, nPage
);
535 ulApiPage
= LONGFROMMR(::WinSendMsg( GetHWND()
537 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
- 1])
538 ,MPFROM2SHORT(BKA_AUTOPAGESIZE
| BKA_MAJOR
, BKA_NEXT
)
545 vError
= ::WinGetLastError(vHabmain
);
546 sError
= wxPMErrorToStr(vError
);
549 m_alPageId
.Insert((long)ulApiPage
, nPage
);
553 // Associate a window handle with the page
557 if (!::WinSendMsg( GetHWND()
558 ,BKM_SETPAGEWINDOWHWND
559 ,MPFROMLONG((ULONG
)m_alPageId
[nPage
])
560 ,MPFROMHWND(pPage
->GetHWND())
565 // If the inserted page is before the selected one, we must update the
566 // index of the selected page
568 if (nPage
<= (size_t)m_selection
)
571 // One extra page added
579 // Save the pointer to the page
581 m_pages
.Insert( pPage
587 // Now set TAB dimenstions
590 wxWindowDC
vDC(this);
594 vDC
.GetTextExtent(rsStrText
, &nTextX
, &nTextY
);
596 nTextX
= (wxCoord
)(nTextX
* 1.3);
597 if (nTextX
> m_nTabSize
)
600 ::WinSendMsg( GetHWND()
602 ,MPFROM2SHORT((USHORT
)m_nTabSize
, (USHORT
)nTextY
)
603 ,(MPARAM
)BKA_MAJORTAB
607 // Now set any TAB text
609 if (!rsStrText
.empty())
611 if (!SetPageText( nPage
618 // Now set any TAB bitmap image
622 if (!SetPageImage( nPage
631 // Don't show pages by default (we'll need to adjust their size first)
633 HWND hWnd
= GetWinHwnd(pPage
);
635 WinSetWindowULong( hWnd
637 ,WinQueryWindowULong( hWnd
643 // This updates internal flag too - otherwise it will get out of sync
648 DoSetSelectionAfterInsertion(nPage
, bSelect
);
650 InvalidateBestSize();
653 } // end of wxNotebook::InsertPage
655 // ----------------------------------------------------------------------------
656 // wxNotebook callbacks
657 // ----------------------------------------------------------------------------
658 void wxNotebook::OnSize(
663 } // end of wxNotebook::OnSize
665 void wxNotebook::OnSelChange (
666 wxBookCtrlEvent
& rEvent
670 // Is it our tab control?
672 if (rEvent
.GetEventObject() == this)
674 int nPageCount
= GetPageCount();
676 ULONG ulOS2Sel
= (ULONG
)rEvent
.GetOldSelection();
679 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
681 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
691 m_pages
[nSel
]->Show(false);
693 ulOS2Sel
= (ULONG
)rEvent
.GetSelection();
697 for (nSel
= 0; nSel
< nPageCount
; nSel
++)
699 if (ulOS2Sel
== (ULONG
)m_alPageId
[nSel
])
709 wxNotebookPage
* pPage
= m_pages
[nSel
];
716 // We want to give others a chance to process this message as well
719 } // end of wxNotebook::OnSelChange
721 void wxNotebook::OnSetFocus (
726 // This function is only called when the focus is explicitly set (i.e. from
727 // the program) to the notebook - in this case we don't need the
728 // complicated OnNavigationKey() logic because the programmer knows better
731 // set focus to the currently selected page if any
733 if (m_selection
!= wxNOT_FOUND
)
734 m_pages
[m_selection
]->SetFocus();
736 } // end of wxNotebook::OnSetFocus
738 void wxNotebook::OnNavigationKey (
739 wxNavigationKeyEvent
& rEvent
742 if (rEvent
.IsWindowChange())
747 AdvanceSelection(rEvent
.GetDirection());
752 // We get this event in 2 cases
754 // a) one of our pages might have generated it because the user TABbed
755 // out from it in which case we should propagate the event upwards and
756 // our parent will take care of setting the focus to prev/next sibling
760 // b) the parent panel wants to give the focus to us so that we
761 // forward it to our selected page. We can't deal with this in
762 // OnSetFocus() because we don't know which direction the focus came
763 // from in this case and so can't choose between setting the focus to
764 // first or last panel child
766 wxWindow
* pParent
= GetParent();
768 if (rEvent
.GetEventObject() == pParent
)
771 // No, it doesn't come from child, case (b): forward to a page
773 if (m_selection
!= wxNOT_FOUND
)
776 // So that the page knows that the event comes from it's parent
777 // and is being propagated downwards
779 rEvent
.SetEventObject(this);
781 wxWindow
* pPage
= m_pages
[m_selection
];
783 if (!pPage
->HandleWindowEvent(rEvent
))
787 //else: page manages focus inside it itself
792 // We have no pages - still have to give focus to _something_
800 // It comes from our child, case (a), pass to the parent
804 rEvent
.SetCurrentFocus(this);
805 pParent
->HandleWindowEvent(rEvent
);
809 } // end of wxNotebook::OnNavigationKey
811 // ----------------------------------------------------------------------------
812 // wxNotebook base class virtuals
813 // ----------------------------------------------------------------------------
816 // Override these 2 functions to do nothing: everything is done in OnSize
818 void wxNotebook::SetConstraintSizes( bool WXUNUSED(bRecurse
) )
821 // Don't set the sizes of the pages - their correct size is not yet known
823 wxControl::SetConstraintSizes(false);
824 } // end of wxNotebook::SetConstraintSizes
826 bool wxNotebook::DoPhase ( int WXUNUSED(nPhase
) )
829 } // end of wxNotebook::DoPhase
831 // ----------------------------------------------------------------------------
832 // wxNotebook Windows message handlers
833 // ----------------------------------------------------------------------------
834 bool wxNotebook::OS2OnScroll ( int nOrientation
,
840 // Don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the
845 return wxNotebookBase::OS2OnScroll( nOrientation
850 } // end of wxNotebook::OS2OnScroll
852 #endif // wxUSE_NOTEBOOK