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
, wxCommandEvent
)
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
);
282 bool wxNotebook::DeleteAllPages()
284 TabCtrl_DeleteAllItems(m_hwnd
);
286 int nPageCount
= GetPageCount();
288 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
289 delete m_aPages
[nPage
];
296 // add a page to the notebook
297 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
298 const wxString
& strText
,
302 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
305 // same as AddPage() but does it at given position
306 bool wxNotebook::InsertPage(int nPage
,
307 wxNotebookPage
*pPage
,
308 const wxString
& strText
,
312 wxASSERT( pPage
!= NULL
);
313 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
315 // add the tab to the control
317 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
318 tcItem
.pszText
= (char *)strText
.c_str();
319 tcItem
.iImage
= imageId
;
321 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
322 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
326 // save the pointer to the page
327 m_aPages
.Insert(pPage
, nPage
);
329 // some page must be selected: either this one or the first one if there is
330 // still no selection
332 m_nSelection
= nPage
;
333 else if ( m_nSelection
== -1 )
336 // don't show pages by default (we'll need to adjust their size first)
337 HWND hwnd
= (HWND
)pPage
->GetHWND();
338 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
343 // ----------------------------------------------------------------------------
344 // wxNotebook callbacks
345 // ----------------------------------------------------------------------------
347 void wxNotebook::OnSize(wxSizeEvent
& event
)
349 // emulate page change (it's esp. important to do it first time because
350 // otherwise our page would stay invisible)
351 int nSel
= m_nSelection
;
355 // fit the notebook page to the tab control's display area
357 rc
.left
= rc
.top
= 0;
358 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
360 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
361 size_t nCount
= m_aPages
.Count();
362 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
363 wxNotebookPage
*pPage
= m_aPages
[nPage
];
364 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
365 if ( pPage
->GetAutoLayout() )
372 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
374 // is it our tab control?
375 if ( event
.GetEventObject() == this )
376 ChangePage(event
.GetOldSelection(), event
.GetSelection());
378 // we want to give others a chance to process this message as well
382 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
384 // set focus to the currently selected page if any
385 if ( m_nSelection
!= -1 )
386 m_aPages
[m_nSelection
]->SetFocus();
391 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
393 if ( event
.IsWindowChange() ) {
395 AdvanceSelection(event
.GetDirection());
398 // pass to the parent
400 event
.SetCurrentFocus(this);
401 GetParent()->GetEventHandler()->ProcessEvent(event
);
406 // ----------------------------------------------------------------------------
407 // wxNotebook base class virtuals
408 // ----------------------------------------------------------------------------
410 // override these 2 functions to do nothing: everything is done in OnSize
412 void wxNotebook::SetConstraintSizes(bool /* recurse */)
414 // don't set the sizes of the pages - their correct size is not yet known
415 wxControl::SetConstraintSizes(FALSE
);
418 bool wxNotebook::DoPhase(int /* nPhase */)
423 void wxNotebook::Command(wxCommandEvent
& event
)
425 wxFAIL_MSG("wxNotebook::Command not implemented");
428 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
430 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
432 NMHDR
* hdr
= (NMHDR
*)lParam
;
433 switch ( hdr
->code
) {
435 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
438 case TCN_SELCHANGING
:
439 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
442 // prevent this msg from being passed to wxControl::MSWNotify which would
443 // retrun FALSE disabling the change of page
448 return wxControl::MSWNotify(wParam
, lParam
);
451 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
452 event
.SetOldSelection(m_nSelection
);
453 event
.SetEventObject(this);
454 event
.SetInt(LOWORD(wParam
));
456 return GetEventHandler()->ProcessEvent(event
);
459 // ----------------------------------------------------------------------------
460 // wxNotebook helper functions
461 // ----------------------------------------------------------------------------
463 // hide the currently active panel and show the new one
464 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
466 // it's not an error (the message may be generated by the tab control itself)
467 // and it may happen - just do nothing
468 if ( nSel
== nOldSel
)
472 m_aPages
[nOldSel
]->Show(FALSE
);
474 wxNotebookPage
*pPage
= m_aPages
[nSel
];
481 void wxNotebook::OnEraseBackground(wxEraseEvent
& event
)