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>
30 #include <wx/notebook.h>
32 #include <wx/msw/private.h>
34 // Windows standard headers
36 #error "wxNotebook is not supported under Windows 3.1"
39 #include <windowsx.h> // for SetWindowFont
42 #include "wx/msw/gnuwin32/extra.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // check that the page index is valid
52 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
55 #define m_hwnd (HWND)GetHWND()
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 #if !USE_SHARED_LIBRARIES
62 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
63 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
65 EVT_SIZE(wxNotebook::OnSize
)
66 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
67 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
68 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
71 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
72 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // wxNotebook construction
81 // ----------------------------------------------------------------------------
83 // common part of all ctors
84 void wxNotebook::Init()
90 // default for dynamic class
91 wxNotebook::wxNotebook()
96 // the same arguments as for wxControl
97 wxNotebook::wxNotebook(wxWindow
*parent
,
102 const wxString
& name
)
106 Create(parent
, id
, pos
, size
, style
, name
);
110 bool wxNotebook::Create(wxWindow
*parent
,
115 const wxString
& name
)
121 m_windowId
= id
== -1 ? NewControlId() : id
;
124 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
125 m_foregroundColour
= *wxBLACK
;
128 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
130 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
131 if ( m_windowStyle
& wxTC_MULTILINE
)
132 tabStyle
|= TCS_MULTILINE
;
133 if ( m_windowStyle
& wxBORDER
)
134 tabStyle
&= WS_BORDER
;
136 // create the tab control.
137 m_hWnd
= (WXHWND
)CreateWindowEx
140 WC_TABCONTROL
, // class name for the tab control
143 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
144 (HWND
)parent
->GetHWND(), // parent window
145 (HMENU
)m_windowId
, // child id
146 wxGetInstance(), // current instance
147 NULL
// no class data
151 wxLogSysError("Can't create the notebook control");
155 // Not all compilers recognise SetWindowFont
156 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
157 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
158 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
161 if ( parent
!= NULL
)
162 parent
->AddChild(this);
170 wxNotebook::~wxNotebook()
174 // ----------------------------------------------------------------------------
175 // wxNotebook accessors
176 // ----------------------------------------------------------------------------
177 int wxNotebook::GetPageCount() const
180 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
182 return m_aPages
.Count();
185 int wxNotebook::GetRowCount() const
187 return TabCtrl_GetRowCount(m_hwnd
);
190 int wxNotebook::SetSelection(int nPage
)
192 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
194 ChangePage(m_nSelection
, nPage
);
196 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
199 void wxNotebook::AdvanceSelection(bool bForward
)
201 int nSel
= GetSelection();
202 int nMax
= GetPageCount() - 1;
204 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
206 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
209 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
211 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
214 tcItem
.mask
= TCIF_TEXT
;
215 tcItem
.pszText
= (char *)strText
.c_str();
217 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
220 wxString
wxNotebook::GetPageText(int nPage
) const
222 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
226 tcItem
.mask
= TCIF_TEXT
;
227 tcItem
.pszText
= buf
;
228 tcItem
.cchTextMax
= WXSIZEOF(buf
);
231 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
232 str
= tcItem
.pszText
;
237 int wxNotebook::GetPageImage(int nPage
) const
239 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
242 tcItem
.mask
= TCIF_IMAGE
;
244 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
247 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
249 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
252 tcItem
.mask
= TCIF_IMAGE
;
253 tcItem
.iImage
= nImage
;
255 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
258 void wxNotebook::SetImageList(wxImageList
* imageList
)
260 m_pImageList
= imageList
;
261 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
264 // ----------------------------------------------------------------------------
265 // wxNotebook operations
266 // ----------------------------------------------------------------------------
268 // remove one page from the notebook
269 bool wxNotebook::DeletePage(int nPage
)
271 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
273 TabCtrl_DeleteItem(m_hwnd
, nPage
);
275 delete m_aPages
[nPage
];
276 m_aPages
.Remove(nPage
);
281 // remove one page from the notebook, without deleting
282 bool wxNotebook::RemovePage(int nPage
)
284 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
286 TabCtrl_DeleteItem(m_hwnd
, nPage
);
288 m_aPages
.Remove(nPage
);
294 bool wxNotebook::DeleteAllPages()
296 TabCtrl_DeleteAllItems(m_hwnd
);
298 int nPageCount
= GetPageCount();
300 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
301 delete m_aPages
[nPage
];
308 // add a page to the notebook
309 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
310 const wxString
& strText
,
314 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
317 // same as AddPage() but does it at given position
318 bool wxNotebook::InsertPage(int nPage
,
319 wxNotebookPage
*pPage
,
320 const wxString
& strText
,
324 wxASSERT( pPage
!= NULL
);
325 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
327 // add the tab to the control
329 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
330 tcItem
.pszText
= (char *)strText
.c_str();
331 tcItem
.iImage
= imageId
;
333 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
334 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
338 // save the pointer to the page
339 m_aPages
.Insert(pPage
, nPage
);
341 // some page must be selected: either this one or the first one if there is
342 // still no selection
344 m_nSelection
= nPage
;
345 else if ( m_nSelection
== -1 )
348 // don't show pages by default (we'll need to adjust their size first)
349 HWND hwnd
= (HWND
)pPage
->GetHWND();
350 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
355 // ----------------------------------------------------------------------------
356 // wxNotebook callbacks
357 // ----------------------------------------------------------------------------
359 void wxNotebook::OnSize(wxSizeEvent
& event
)
361 // emulate page change (it's esp. important to do it first time because
362 // otherwise our page would stay invisible)
363 int nSel
= m_nSelection
;
367 // fit the notebook page to the tab control's display area
369 rc
.left
= rc
.top
= 0;
370 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
372 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
373 size_t nCount
= m_aPages
.Count();
374 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
375 wxNotebookPage
*pPage
= m_aPages
[nPage
];
376 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
377 if ( pPage
->GetAutoLayout() )
384 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
386 // is it our tab control?
387 if ( event
.GetEventObject() == this )
388 ChangePage(event
.GetOldSelection(), event
.GetSelection());
390 // we want to give others a chance to process this message as well
394 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
396 // set focus to the currently selected page if any
397 if ( m_nSelection
!= -1 )
398 m_aPages
[m_nSelection
]->SetFocus();
403 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
405 if ( event
.IsWindowChange() ) {
407 AdvanceSelection(event
.GetDirection());
410 // pass to the parent
412 event
.SetCurrentFocus(this);
413 GetParent()->GetEventHandler()->ProcessEvent(event
);
418 // ----------------------------------------------------------------------------
419 // wxNotebook base class virtuals
420 // ----------------------------------------------------------------------------
422 // override these 2 functions to do nothing: everything is done in OnSize
424 void wxNotebook::SetConstraintSizes(bool /* recurse */)
426 // don't set the sizes of the pages - their correct size is not yet known
427 wxControl::SetConstraintSizes(FALSE
);
430 bool wxNotebook::DoPhase(int /* nPhase */)
435 void wxNotebook::Command(wxCommandEvent
& event
)
437 wxFAIL_MSG("wxNotebook::Command not implemented");
440 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
* result
)
442 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
444 NMHDR
* hdr
= (NMHDR
*)lParam
;
445 switch ( hdr
->code
) {
447 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
450 case TCN_SELCHANGING
:
451 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
455 return wxControl::MSWNotify(wParam
, lParam
, result
);
458 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
459 event
.SetOldSelection(m_nSelection
);
460 event
.SetEventObject(this);
461 event
.SetInt(LOWORD(wParam
)); // ctrl id
463 bool processed
= GetEventHandler()->ProcessEvent(event
);
464 *result
= !event
.IsAllowed();
468 // ----------------------------------------------------------------------------
469 // wxNotebook helper functions
470 // ----------------------------------------------------------------------------
472 // hide the currently active panel and show the new one
473 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
475 // MT-FIXME should use a real semaphore
476 static bool s_bInsideChangePage
= FALSE
;
478 // when we call ProcessEvent(), our own OnSelChange() is called which calls
479 // this function - break the infinite loop
480 if ( s_bInsideChangePage
)
483 // it's not an error (the message may be generated by the tab control itself)
484 // and it may happen - just do nothing
485 if ( nSel
== nOldSel
)
488 s_bInsideChangePage
= TRUE
;
490 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
491 event
.SetSelection(nSel
);
492 event
.SetOldSelection(nOldSel
);
493 event
.SetEventObject(this);
494 if ( ProcessEvent(event
) && !event
.IsAllowed() )
496 // program doesn't allow the page change
497 s_bInsideChangePage
= FALSE
;
502 m_aPages
[nOldSel
]->Show(FALSE
);
504 wxNotebookPage
*pPage
= m_aPages
[nSel
];
508 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
512 s_bInsideChangePage
= FALSE
;
515 void wxNotebook::OnEraseBackground(wxEraseEvent
& event
)