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_SET_FOCUS(wxNotebook::OnSetFocus
)
67 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
70 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
71 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // wxNotebook construction
80 // ----------------------------------------------------------------------------
82 // common part of all ctors
83 void wxNotebook::Init()
89 // default for dynamic class
90 wxNotebook::wxNotebook()
95 // the same arguments as for wxControl
96 wxNotebook::wxNotebook(wxWindow
*parent
,
101 const wxString
& name
)
105 Create(parent
, id
, pos
, size
, style
, name
);
109 bool wxNotebook::Create(wxWindow
*parent
,
114 const wxString
& name
)
120 m_windowId
= id
== -1 ? NewControlId() : id
;
123 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
124 m_foregroundColour
= *wxBLACK
;
127 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
129 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
130 if ( m_windowStyle
& wxTC_MULTILINE
)
131 tabStyle
|= TCS_MULTILINE
;
132 if ( m_windowStyle
& wxBORDER
)
133 tabStyle
&= WS_BORDER
;
135 // create the tab control.
136 m_hWnd
= (WXHWND
)CreateWindowEx
139 WC_TABCONTROL
, // class name for the tab control
142 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
143 (HWND
)parent
->GetHWND(), // parent window
144 (HMENU
)m_windowId
, // child id
145 wxGetInstance(), // current instance
146 NULL
// no class data
150 wxLogSysError("Can't create the notebook control");
154 // Not all compilers recognise SetWindowFont
155 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
156 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
157 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
160 if ( parent
!= NULL
)
161 parent
->AddChild(this);
169 wxNotebook::~wxNotebook()
173 // ----------------------------------------------------------------------------
174 // wxNotebook accessors
175 // ----------------------------------------------------------------------------
176 int wxNotebook::GetPageCount() const
179 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
181 return m_aPages
.Count();
184 int wxNotebook::GetRowCount() const
186 return TabCtrl_GetRowCount(m_hwnd
);
189 int wxNotebook::SetSelection(int nPage
)
191 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
193 ChangePage(m_nSelection
, nPage
);
195 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
198 void wxNotebook::AdvanceSelection(bool bForward
)
200 int nSel
= GetSelection();
201 int nMax
= GetPageCount() - 1;
203 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
205 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
208 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
210 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
213 tcItem
.mask
= TCIF_TEXT
;
214 tcItem
.pszText
= (char *)strText
.c_str();
216 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
219 wxString
wxNotebook::GetPageText(int nPage
) const
221 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
225 tcItem
.mask
= TCIF_TEXT
;
226 tcItem
.pszText
= buf
;
227 tcItem
.cchTextMax
= WXSIZEOF(buf
);
230 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
231 str
= tcItem
.pszText
;
236 int wxNotebook::GetPageImage(int nPage
) const
238 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
241 tcItem
.mask
= TCIF_IMAGE
;
243 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
246 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
248 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
251 tcItem
.mask
= TCIF_IMAGE
;
252 tcItem
.iImage
= nImage
;
254 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
257 void wxNotebook::SetImageList(wxImageList
* imageList
)
259 m_pImageList
= imageList
;
260 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
263 // ----------------------------------------------------------------------------
264 // wxNotebook operations
265 // ----------------------------------------------------------------------------
267 // remove one page from the notebook
268 bool wxNotebook::DeletePage(int nPage
)
270 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
272 TabCtrl_DeleteItem(m_hwnd
, nPage
);
274 delete m_aPages
[nPage
];
275 m_aPages
.Remove(nPage
);
281 bool wxNotebook::DeleteAllPages()
283 TabCtrl_DeleteAllItems(m_hwnd
);
285 int nPageCount
= GetPageCount();
287 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
288 delete m_aPages
[nPage
];
295 // add a page to the notebook
296 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
297 const wxString
& strText
,
301 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
304 // same as AddPage() but does it at given position
305 bool wxNotebook::InsertPage(int nPage
,
306 wxNotebookPage
*pPage
,
307 const wxString
& strText
,
311 wxASSERT( pPage
!= NULL
);
312 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
314 // add the tab to the control
316 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
317 tcItem
.pszText
= (char *)strText
.c_str();
318 tcItem
.iImage
= imageId
;
320 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
321 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
325 // save the pointer to the page
326 m_aPages
.Insert(pPage
, nPage
);
328 // some page must be selected: either this one or the first one if there is
329 // still no selection
331 m_nSelection
= nPage
;
332 else if ( m_nSelection
== -1 )
335 // don't show pages by default (we'll need to adjust their size first)
336 HWND hwnd
= (HWND
)pPage
->GetHWND();
337 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
342 // ----------------------------------------------------------------------------
343 // wxNotebook callbacks
344 // ----------------------------------------------------------------------------
346 void wxNotebook::OnSize(wxSizeEvent
& event
)
348 // emulate page change (it's esp. important to do it first time because
349 // otherwise our page would stay invisible)
350 int nSel
= m_nSelection
;
354 // fit the notebook page to the tab control's display area
356 rc
.left
= rc
.top
= 0;
357 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
359 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
360 size_t nCount
= m_aPages
.Count();
361 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
362 wxNotebookPage
*pPage
= m_aPages
[nPage
];
363 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
364 if ( pPage
->GetAutoLayout() )
371 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
373 // is it our tab control?
374 if ( event
.GetEventObject() == this )
375 ChangePage(event
.GetOldSelection(), event
.GetSelection());
377 // we want to give others a chance to process this message as well
381 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
383 // set focus to the currently selected page if any
384 if ( m_nSelection
!= -1 )
385 m_aPages
[m_nSelection
]->SetFocus();
390 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
392 if ( event
.IsWindowChange() ) {
394 AdvanceSelection(event
.GetDirection());
397 // pass to the parent
399 event
.SetCurrentFocus(this);
400 GetParent()->ProcessEvent(event
);
405 // ----------------------------------------------------------------------------
406 // wxNotebook base class virtuals
407 // ----------------------------------------------------------------------------
409 // override these 2 functions to do nothing: everything is done in OnSize
411 void wxNotebook::SetConstraintSizes(bool /* recurse */)
413 // don't set the sizes of the pages - their correct size is not yet known
414 wxControl::SetConstraintSizes(FALSE
);
417 bool wxNotebook::DoPhase(int /* nPhase */)
422 void wxNotebook::Command(wxCommandEvent
& event
)
424 wxFAIL_MSG("wxNotebook::Command not implemented");
427 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
429 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
431 NMHDR
* hdr
= (NMHDR
*)lParam
;
432 switch ( hdr
->code
) {
434 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
437 case TCN_SELCHANGING
:
438 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
441 // prevent this msg from being passed to wxControl::MSWNotify which would
442 // retrun FALSE disabling the change of page
447 return wxControl::MSWNotify(wParam
, lParam
);
450 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
451 event
.SetOldSelection(m_nSelection
);
452 event
.SetEventObject(this);
453 event
.SetInt(LOWORD(wParam
));
455 return ProcessEvent(event
);
458 // ----------------------------------------------------------------------------
459 // wxNotebook helper functions
460 // ----------------------------------------------------------------------------
462 // hide the currently active panel and show the new one
463 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
465 // it's not an error (the message may be generated by the tab control itself)
466 // and it may happen - just do nothing
467 if ( nSel
== nOldSel
)
471 m_aPages
[nOldSel
]->Show(FALSE
);
473 wxNotebookPage
*pPage
= m_aPages
[nSel
];