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
44 #include "wx/msw/gnuwin32/extra.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // check that the page index is valid
54 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
57 #define m_hwnd (HWND)GetHWND()
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 #if !USE_SHARED_LIBRARIES
64 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
65 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
67 EVT_SIZE(wxNotebook::OnSize
)
68 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
69 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
70 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
73 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
74 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
82 // wxNotebook construction
83 // ----------------------------------------------------------------------------
85 // common part of all ctors
86 void wxNotebook::Init()
92 // default for dynamic class
93 wxNotebook::wxNotebook()
98 // the same arguments as for wxControl
99 wxNotebook::wxNotebook(wxWindow
*parent
,
104 const wxString
& name
)
108 Create(parent
, id
, pos
, size
, style
, name
);
112 bool wxNotebook::Create(wxWindow
*parent
,
117 const wxString
& name
)
123 m_windowId
= id
== -1 ? NewControlId() : id
;
126 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
127 m_foregroundColour
= *wxBLACK
;
130 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
132 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
133 if ( m_windowStyle
& wxTC_MULTILINE
)
134 tabStyle
|= TCS_MULTILINE
;
135 if ( m_windowStyle
& wxBORDER
)
136 tabStyle
&= WS_BORDER
;
138 // create the tab control.
139 m_hWnd
= (WXHWND
)CreateWindowEx
142 WC_TABCONTROL
, // class name for the tab control
145 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
146 (HWND
)parent
->GetHWND(), // parent window
147 (HMENU
)m_windowId
, // child id
148 wxGetInstance(), // current instance
149 NULL
// no class data
153 wxLogSysError("Can't create the notebook control");
157 // Not all compilers recognise SetWindowFont
158 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
159 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
160 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
163 if ( parent
!= NULL
)
164 parent
->AddChild(this);
172 wxNotebook::~wxNotebook()
176 // ----------------------------------------------------------------------------
177 // wxNotebook accessors
178 // ----------------------------------------------------------------------------
179 int wxNotebook::GetPageCount() const
182 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
184 return m_aPages
.Count();
187 int wxNotebook::GetRowCount() const
189 return TabCtrl_GetRowCount(m_hwnd
);
192 int wxNotebook::SetSelection(int nPage
)
194 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
196 ChangePage(m_nSelection
, nPage
);
198 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
201 void wxNotebook::AdvanceSelection(bool bForward
)
203 int nSel
= GetSelection();
204 int nMax
= GetPageCount() - 1;
206 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
208 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
211 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
213 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
216 tcItem
.mask
= TCIF_TEXT
;
217 tcItem
.pszText
= (char *)strText
.c_str();
219 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
222 wxString
wxNotebook::GetPageText(int nPage
) const
224 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
228 tcItem
.mask
= TCIF_TEXT
;
229 tcItem
.pszText
= buf
;
230 tcItem
.cchTextMax
= WXSIZEOF(buf
);
233 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
234 str
= tcItem
.pszText
;
239 int wxNotebook::GetPageImage(int nPage
) const
241 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
244 tcItem
.mask
= TCIF_IMAGE
;
246 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
249 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
251 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
254 tcItem
.mask
= TCIF_IMAGE
;
255 tcItem
.iImage
= nImage
;
257 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
260 void wxNotebook::SetImageList(wxImageList
* imageList
)
262 m_pImageList
= imageList
;
263 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
266 // ----------------------------------------------------------------------------
267 // wxNotebook operations
268 // ----------------------------------------------------------------------------
270 // remove one page from the notebook
271 bool wxNotebook::DeletePage(int nPage
)
273 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
275 TabCtrl_DeleteItem(m_hwnd
, nPage
);
277 delete m_aPages
[nPage
];
278 m_aPages
.Remove(nPage
);
283 // remove one page from the notebook, without deleting
284 bool wxNotebook::RemovePage(int nPage
)
286 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
288 TabCtrl_DeleteItem(m_hwnd
, nPage
);
290 m_aPages
.Remove(nPage
);
296 bool wxNotebook::DeleteAllPages()
298 TabCtrl_DeleteAllItems(m_hwnd
);
300 int nPageCount
= GetPageCount();
302 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
303 delete m_aPages
[nPage
];
310 // add a page to the notebook
311 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
312 const wxString
& strText
,
316 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
319 // same as AddPage() but does it at given position
320 bool wxNotebook::InsertPage(int nPage
,
321 wxNotebookPage
*pPage
,
322 const wxString
& strText
,
326 wxASSERT( pPage
!= NULL
);
327 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
329 // add the tab to the control
331 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
332 tcItem
.pszText
= (char *)strText
.c_str();
333 tcItem
.iImage
= imageId
;
335 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
336 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
340 // save the pointer to the page
341 m_aPages
.Insert(pPage
, nPage
);
343 // some page must be selected: either this one or the first one if there is
344 // still no selection
346 m_nSelection
= nPage
;
347 else if ( m_nSelection
== -1 )
350 // don't show pages by default (we'll need to adjust their size first)
351 HWND hwnd
= (HWND
)pPage
->GetHWND();
352 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
357 // ----------------------------------------------------------------------------
358 // wxNotebook callbacks
359 // ----------------------------------------------------------------------------
361 void wxNotebook::OnSize(wxSizeEvent
& event
)
363 // emulate page change (it's esp. important to do it first time because
364 // otherwise our page would stay invisible)
365 int nSel
= m_nSelection
;
369 // fit the notebook page to the tab control's display area
371 rc
.left
= rc
.top
= 0;
372 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
374 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
375 size_t nCount
= m_aPages
.Count();
376 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
377 wxNotebookPage
*pPage
= m_aPages
[nPage
];
378 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
379 if ( pPage
->GetAutoLayout() )
386 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
388 // is it our tab control?
389 if ( event
.GetEventObject() == this )
390 ChangePage(event
.GetOldSelection(), event
.GetSelection());
392 // we want to give others a chance to process this message as well
396 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
398 // set focus to the currently selected page if any
399 if ( m_nSelection
!= -1 )
400 m_aPages
[m_nSelection
]->SetFocus();
405 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
407 if ( event
.IsWindowChange() ) {
409 AdvanceSelection(event
.GetDirection());
412 // pass to the parent
414 event
.SetCurrentFocus(this);
415 GetParent()->GetEventHandler()->ProcessEvent(event
);
420 // ----------------------------------------------------------------------------
421 // wxNotebook base class virtuals
422 // ----------------------------------------------------------------------------
424 // override these 2 functions to do nothing: everything is done in OnSize
426 void wxNotebook::SetConstraintSizes(bool /* recurse */)
428 // don't set the sizes of the pages - their correct size is not yet known
429 wxControl::SetConstraintSizes(FALSE
);
432 bool wxNotebook::DoPhase(int /* nPhase */)
437 void wxNotebook::Command(wxCommandEvent
& event
)
439 wxFAIL_MSG("wxNotebook::Command not implemented");
442 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
* result
)
444 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
446 NMHDR
* hdr
= (NMHDR
*)lParam
;
447 switch ( hdr
->code
) {
449 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
452 case TCN_SELCHANGING
:
453 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
457 return wxControl::MSWNotify(wParam
, lParam
, result
);
460 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
461 event
.SetOldSelection(m_nSelection
);
462 event
.SetEventObject(this);
463 event
.SetInt(LOWORD(wParam
)); // ctrl id
465 bool processed
= GetEventHandler()->ProcessEvent(event
);
466 *result
= !event
.IsAllowed();
470 // ----------------------------------------------------------------------------
471 // wxNotebook helper functions
472 // ----------------------------------------------------------------------------
474 // hide the currently active panel and show the new one
475 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
477 // MT-FIXME should use a real semaphore
478 static bool s_bInsideChangePage
= FALSE
;
480 // when we call ProcessEvent(), our own OnSelChange() is called which calls
481 // this function - break the infinite loop
482 if ( s_bInsideChangePage
)
485 // it's not an error (the message may be generated by the tab control itself)
486 // and it may happen - just do nothing
487 if ( nSel
== nOldSel
)
490 s_bInsideChangePage
= TRUE
;
492 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
493 event
.SetSelection(nSel
);
494 event
.SetOldSelection(nOldSel
);
495 event
.SetEventObject(this);
496 if ( ProcessEvent(event
) && !event
.IsAllowed() )
498 // program doesn't allow the page change
499 s_bInsideChangePage
= FALSE
;
504 m_aPages
[nOldSel
]->Show(FALSE
);
506 wxNotebookPage
*pPage
= m_aPages
[nSel
];
510 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
514 s_bInsideChangePage
= FALSE
;
517 void wxNotebook::OnEraseBackground(wxEraseEvent
& event
)