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 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "notebook.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
32 #include <wx/string.h>
36 #include <wx/imaglist.h>
37 #include <wx/notebook.h>
39 #include <wx/msw/private.h>
41 // Windows standard headers
43 #error "wxNotebook is not supported under Windows 3.1"
46 #include <windowsx.h> // for SetWindowFont
49 #include "wx/msw/gnuwin32/extra.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // check that the page index is valid
59 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
62 #define m_hwnd (HWND)GetHWND()
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 #if !USE_SHARED_LIBRARIES
69 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
70 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
72 EVT_SIZE(wxNotebook::OnSize
)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
74 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
77 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
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
;
133 m_defaultForegroundColour
= *wxBLACK
;
134 m_defaultBackgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
137 m_windowStyle
= style
;
139 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
140 if ( m_windowStyle
& wxTC_MULTILINE
)
141 tabStyle
|= TCS_MULTILINE
;
142 if ( m_windowStyle
& wxBORDER
)
143 tabStyle
&= WS_BORDER
;
145 // create the tab control.
146 m_hWnd
= (WXHWND
)CreateWindowEx
149 WC_TABCONTROL
, // class name for the tab control
152 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
153 (HWND
)parent
->GetHWND(), // parent window
154 (HMENU
)m_windowId
, // child id
155 wxGetInstance(), // current instance
156 NULL
// no class data
160 wxLogSysError("Can't create the notebook control");
164 SetWindowFont((HWND
)m_hwnd
, ::GetStockObject(DEFAULT_GUI_FONT
), FALSE
);
166 if ( parent
!= NULL
)
167 parent
->AddChild(this);
175 wxNotebook::~wxNotebook()
179 // ----------------------------------------------------------------------------
180 // wxNotebook accessors
181 // ----------------------------------------------------------------------------
182 int wxNotebook::GetPageCount() const
185 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
187 return m_aPages
.Count();
190 int wxNotebook::GetRowCount() const
192 return TabCtrl_GetRowCount(m_hwnd
);
195 int wxNotebook::SetSelection(int nPage
)
197 wxASSERT( IS_VALID_PAGE(nPage
) );
199 ChangePage(m_nSelection
, nPage
);
201 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
204 void wxNotebook::AdvanceSelection(bool bForward
)
206 int nSel
= GetSelection();
207 int nMax
= GetPageCount() - 1;
209 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
211 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
214 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
216 wxASSERT( IS_VALID_PAGE(nPage
) );
219 tcItem
.mask
= TCIF_TEXT
;
220 tcItem
.pszText
= (char *)strText
.c_str();
222 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
225 wxString
wxNotebook::GetPageText(int nPage
) const
227 wxASSERT( IS_VALID_PAGE(nPage
) );
231 tcItem
.mask
= TCIF_TEXT
;
232 tcItem
.pszText
= buf
;
233 tcItem
.cchTextMax
= WXSIZEOF(buf
);
236 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
237 str
= tcItem
.pszText
;
242 int wxNotebook::GetPageImage(int nPage
) const
244 wxASSERT( IS_VALID_PAGE(nPage
) );
247 tcItem
.mask
= TCIF_IMAGE
;
249 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
252 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
254 wxASSERT( IS_VALID_PAGE(nPage
) );
257 tcItem
.mask
= TCIF_IMAGE
;
258 tcItem
.iImage
= nImage
;
260 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
263 void wxNotebook::SetImageList(wxImageList
* imageList
)
265 m_pImageList
= imageList
;
266 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
269 // ----------------------------------------------------------------------------
270 // wxNotebook operations
271 // ----------------------------------------------------------------------------
273 // remove one page from the notebook
274 bool wxNotebook::DeletePage(int nPage
)
276 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
278 TabCtrl_DeleteItem(m_hwnd
, nPage
);
280 delete m_aPages
[nPage
];
281 m_aPages
.Remove(nPage
);
287 bool wxNotebook::DeleteAllPages()
289 TabCtrl_DeleteAllItems(m_hwnd
);
291 int nPageCount
= GetPageCount();
293 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
294 delete m_aPages
[nPage
];
301 // add a page to the notebook
302 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
303 const wxString
& strText
,
307 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
310 // same as AddPage() but does it at given position
311 bool wxNotebook::InsertPage(int nPage
,
312 wxNotebookPage
*pPage
,
313 const wxString
& strText
,
317 wxASSERT( pPage
!= NULL
);
318 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
320 // add the tab to the control
322 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
323 tcItem
.pszText
= (char *)strText
.c_str();
324 tcItem
.iImage
= imageId
;
326 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
327 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
331 // save the pointer to the page
332 m_aPages
.Insert(pPage
, nPage
);
334 // some page must be selected: either this one or the first one if there is
335 // still no selection
337 m_nSelection
= nPage
;
338 else if ( m_nSelection
== -1 )
341 // don't show pages by default (we'll need to adjust their size first)
342 HWND hwnd
= (HWND
)pPage
->GetHWND();
343 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
348 // ----------------------------------------------------------------------------
349 // wxNotebook callbacks
350 // ----------------------------------------------------------------------------
352 void wxNotebook::OnSize(wxSizeEvent
& event
)
354 // emulate page change (it's esp. important to do it first time because
355 // otherwise our page would stay invisible)
356 int nSel
= m_nSelection
;
360 // fit the notebook page to the tab control's display area
362 rc
.left
= rc
.top
= 0;
363 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
365 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
366 uint nCount
= m_aPages
.Count();
367 for ( uint nPage
= 0; nPage
< nCount
; nPage
++ ) {
368 wxNotebookPage
*pPage
= m_aPages
[nPage
];
369 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
370 if ( pPage
->GetAutoLayout() )
377 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
379 // is it our tab control?
380 if ( event
.GetEventObject() == this )
381 ChangePage(event
.GetOldSelection(), event
.GetSelection());
383 // we want to give others a chance to process this message as well
387 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
389 // set focus to the currently selected page if any
390 if ( m_nSelection
!= -1 )
391 m_aPages
[m_nSelection
]->SetFocus();
396 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
398 if ( event
.IsWindowChange() ) {
400 AdvanceSelection(event
.GetDirection());
403 // pass to the parent
405 event
.SetCurrentFocus(this);
406 GetParent()->ProcessEvent(event
);
411 // ----------------------------------------------------------------------------
412 // wxNotebook base class virtuals
413 // ----------------------------------------------------------------------------
415 // override these 2 functions to do nothing: everything is done in OnSize
417 void wxNotebook::SetConstraintSizes(bool /* recurse */)
419 // don't set the sizes of the pages - their correct size is not yet known
420 wxControl::SetConstraintSizes(FALSE
);
423 bool wxNotebook::DoPhase(int /* nPhase */)
428 void wxNotebook::Command(wxCommandEvent
& event
)
430 wxFAIL_MSG("wxNotebook::Command not implemented");
433 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
435 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
437 NMHDR
* hdr
= (NMHDR
*)lParam
;
438 switch ( hdr
->code
) {
440 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
443 case TCN_SELCHANGING
:
444 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
447 // prevent this msg from being passed to wxControl::MSWNotify which would
448 // retrun FALSE disabling the change of page
453 return wxControl::MSWNotify(wParam
, lParam
);
456 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
457 event
.SetOldSelection(m_nSelection
);
458 event
.SetEventObject(this);
459 event
.SetInt(LOWORD(wParam
));
461 return ProcessEvent(event
);
464 // ----------------------------------------------------------------------------
465 // wxNotebook helper functions
466 // ----------------------------------------------------------------------------
468 // hide the currently active panel and show the new one
469 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
471 // it's not an error (the message may be generated by the tab control itself)
472 // and it may happen - just do nothing
473 if ( nSel
== nOldSel
)
477 m_aPages
[nOldSel
]->Show(FALSE
);
479 wxNotebookPage
*pPage
= m_aPages
[nSel
];