1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "notebook.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include <wx/string.h>
29 #include <wx/imaglist.h>
31 #include <wx/control.h>
32 #include <wx/notebook.h>
34 #include <wx/msw/private.h>
36 // Windows standard headers
38 #error "wxNotebook is only supported Windows 95 and above"
41 #include <windowsx.h> // for SetWindowFont
45 #include "wx/msw/gnuwin32/extra.h"
49 #if !defined(__GNUWIN32__) || defined(__TWIN32__)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // check that the page index is valid
58 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
61 #define m_hwnd (HWND)GetHWND()
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #if !USE_SHARED_LIBRARIES
68 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
69 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
71 EVT_SIZE(wxNotebook::OnSize
)
72 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
74 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
77 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
86 // wxNotebook construction
87 // ----------------------------------------------------------------------------
89 // common part of all ctors
90 void wxNotebook::Init()
96 // default for dynamic class
97 wxNotebook::wxNotebook()
102 // the same arguments as for wxControl
103 wxNotebook::wxNotebook(wxWindow
*parent
,
108 const wxString
& name
)
112 Create(parent
, id
, pos
, size
, style
, name
);
116 bool wxNotebook::Create(wxWindow
*parent
,
121 const wxString
& name
)
127 m_windowId
= id
== -1 ? NewControlId() : id
;
130 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
131 m_foregroundColour
= *wxBLACK
;
134 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
136 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
137 if ( m_windowStyle
& wxTC_MULTILINE
)
138 tabStyle
|= TCS_MULTILINE
;
139 if ( m_windowStyle
& wxBORDER
)
140 tabStyle
&= WS_BORDER
;
142 // create the tab control.
143 m_hWnd
= (WXHWND
)CreateWindowEx
146 WC_TABCONTROL
, // class name for the tab control
149 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
150 (HWND
)parent
->GetHWND(), // parent window
151 (HMENU
)m_windowId
, // child id
152 wxGetInstance(), // current instance
153 NULL
// no class data
157 wxLogSysError("Can't create the notebook control");
161 // Not all compilers recognise SetWindowFont
162 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
163 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
164 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
167 if ( parent
!= NULL
)
168 parent
->AddChild(this);
176 wxNotebook::~wxNotebook()
180 // ----------------------------------------------------------------------------
181 // wxNotebook accessors
182 // ----------------------------------------------------------------------------
183 int wxNotebook::GetPageCount() const
186 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
188 return m_aPages
.Count();
191 int wxNotebook::GetRowCount() const
193 return TabCtrl_GetRowCount(m_hwnd
);
196 int wxNotebook::SetSelection(int nPage
)
198 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
200 ChangePage(m_nSelection
, nPage
);
202 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
205 void wxNotebook::AdvanceSelection(bool bForward
)
207 int nSel
= GetSelection();
208 int nMax
= GetPageCount() - 1;
210 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
212 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
215 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
217 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
220 tcItem
.mask
= TCIF_TEXT
;
221 tcItem
.pszText
= (char *)strText
.c_str();
223 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
226 wxString
wxNotebook::GetPageText(int nPage
) const
228 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
232 tcItem
.mask
= TCIF_TEXT
;
233 tcItem
.pszText
= buf
;
234 tcItem
.cchTextMax
= WXSIZEOF(buf
);
237 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
238 str
= tcItem
.pszText
;
243 int wxNotebook::GetPageImage(int nPage
) const
245 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
248 tcItem
.mask
= TCIF_IMAGE
;
250 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
253 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
255 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
258 tcItem
.mask
= TCIF_IMAGE
;
259 tcItem
.iImage
= nImage
;
261 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
264 void wxNotebook::SetImageList(wxImageList
* imageList
)
266 m_pImageList
= imageList
;
267 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
270 // ----------------------------------------------------------------------------
271 // wxNotebook operations
272 // ----------------------------------------------------------------------------
274 // remove one page from the notebook
275 bool wxNotebook::DeletePage(int nPage
)
277 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
279 TabCtrl_DeleteItem(m_hwnd
, nPage
);
281 delete m_aPages
[nPage
];
282 m_aPages
.Remove(nPage
);
287 // remove one page from the notebook, without deleting
288 bool wxNotebook::RemovePage(int nPage
)
290 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
292 TabCtrl_DeleteItem(m_hwnd
, nPage
);
294 m_aPages
.Remove(nPage
);
300 bool wxNotebook::DeleteAllPages()
302 TabCtrl_DeleteAllItems(m_hwnd
);
304 int nPageCount
= GetPageCount();
306 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
307 delete m_aPages
[nPage
];
314 // add a page to the notebook
315 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
316 const wxString
& strText
,
320 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
323 // same as AddPage() but does it at given position
324 bool wxNotebook::InsertPage(int nPage
,
325 wxNotebookPage
*pPage
,
326 const wxString
& strText
,
330 wxASSERT( pPage
!= NULL
);
331 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
333 // add the tab to the control
335 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
336 tcItem
.pszText
= (char *)strText
.c_str();
337 tcItem
.iImage
= imageId
;
339 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
340 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
344 // save the pointer to the page
345 m_aPages
.Insert(pPage
, nPage
);
347 // some page must be selected: either this one or the first one if there is
348 // still no selection
350 m_nSelection
= nPage
;
351 else if ( m_nSelection
== -1 )
354 // don't show pages by default (we'll need to adjust their size first)
355 HWND hwnd
= (HWND
)pPage
->GetHWND();
356 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
361 // ----------------------------------------------------------------------------
362 // wxNotebook callbacks
363 // ----------------------------------------------------------------------------
365 void wxNotebook::OnSize(wxSizeEvent
& event
)
367 // make sure the current page is shown and has focus (it's useful because all
368 // pages are created invisible initially)
369 if ( m_nSelection
!= -1 ) {
370 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
375 // fit the notebook page to the tab control's display area
377 rc
.left
= rc
.top
= 0;
378 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
380 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
381 size_t nCount
= m_aPages
.Count();
382 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
383 wxNotebookPage
*pPage
= m_aPages
[nPage
];
384 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
385 if ( pPage
->GetAutoLayout() )
392 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
394 // is it our tab control?
395 if ( event
.GetEventObject() == this )
396 ChangePage(event
.GetOldSelection(), event
.GetSelection());
398 // we want to give others a chance to process this message as well
402 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
404 // set focus to the currently selected page if any
405 if ( m_nSelection
!= -1 )
406 m_aPages
[m_nSelection
]->SetFocus();
411 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
413 if ( event
.IsWindowChange() ) {
415 AdvanceSelection(event
.GetDirection());
418 // pass to the parent
420 event
.SetCurrentFocus(this);
421 GetParent()->GetEventHandler()->ProcessEvent(event
);
426 // ----------------------------------------------------------------------------
427 // wxNotebook base class virtuals
428 // ----------------------------------------------------------------------------
430 // override these 2 functions to do nothing: everything is done in OnSize
432 void wxNotebook::SetConstraintSizes(bool /* recurse */)
434 // don't set the sizes of the pages - their correct size is not yet known
435 wxControl::SetConstraintSizes(FALSE
);
438 bool wxNotebook::DoPhase(int /* nPhase */)
443 void wxNotebook::Command(wxCommandEvent
& event
)
445 wxFAIL_MSG("wxNotebook::Command not implemented");
448 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
* result
)
450 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
452 NMHDR
* hdr
= (NMHDR
*)lParam
;
453 switch ( hdr
->code
) {
455 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
458 case TCN_SELCHANGING
:
459 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
463 return wxControl::MSWNotify(wParam
, lParam
, result
);
466 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
467 event
.SetOldSelection(m_nSelection
);
468 event
.SetEventObject(this);
469 event
.SetInt(LOWORD(wParam
)); // ctrl id
471 bool processed
= GetEventHandler()->ProcessEvent(event
);
472 *result
= !event
.IsAllowed();
476 // ----------------------------------------------------------------------------
477 // wxNotebook helper functions
478 // ----------------------------------------------------------------------------
480 // hide the currently active panel and show the new one
481 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
483 // MT-FIXME should use a real semaphore
484 static bool s_bInsideChangePage
= FALSE
;
486 // when we call ProcessEvent(), our own OnSelChange() is called which calls
487 // this function - break the infinite loop
488 if ( s_bInsideChangePage
)
491 // it's not an error (the message may be generated by the tab control itself)
492 // and it may happen - just do nothing
493 if ( nSel
== nOldSel
)
496 s_bInsideChangePage
= TRUE
;
498 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
499 event
.SetSelection(nSel
);
500 event
.SetOldSelection(nOldSel
);
501 event
.SetEventObject(this);
502 if ( ProcessEvent(event
) && !event
.IsAllowed() )
504 // program doesn't allow the page change
505 s_bInsideChangePage
= FALSE
;
510 m_aPages
[nOldSel
]->Show(FALSE
);
512 wxNotebookPage
*pPage
= m_aPages
[nSel
];
516 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
520 s_bInsideChangePage
= FALSE
;
523 void wxNotebook::OnEraseBackground(wxEraseEvent
& event
)