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
45 #include "wx/msw/gnuwin32/extra.h"
49 #if !defined(__GNUWIN32__) || defined(__TWIN32__)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // check that the page index is valid
58 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
61 #define m_hwnd (HWND)GetHWND()
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #if !USE_SHARED_LIBRARIES
68 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
69 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
71 EVT_SIZE(wxNotebook::OnSize
)
72 EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground
)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
74 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
77 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
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
;
134 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
136 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
138 if (m_windowStyle
& wxCLIP_CHILDREN
)
139 tabStyle
|= WS_CLIPCHILDREN
;
140 if ( m_windowStyle
& wxTC_MULTILINE
)
141 tabStyle
|= TCS_MULTILINE
;
142 if ( m_windowStyle
& wxBORDER
)
143 tabStyle
&= WS_BORDER
;
144 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
145 tabStyle
|= TCS_FIXEDWIDTH
;
147 // create the tab control.
148 m_hWnd
= (WXHWND
)CreateWindowEx
151 WC_TABCONTROL
, // class name for the tab control
154 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
155 (HWND
)parent
->GetHWND(), // parent window
156 (HMENU
)m_windowId
, // child id
157 wxGetInstance(), // current instance
158 NULL
// no class data
162 wxLogSysError("Can't create the notebook control");
166 // Not all compilers recognise SetWindowFont
167 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
168 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
169 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
172 if ( parent
!= NULL
)
173 parent
->AddChild(this);
181 wxNotebook::~wxNotebook()
185 // ----------------------------------------------------------------------------
186 // wxNotebook accessors
187 // ----------------------------------------------------------------------------
188 int wxNotebook::GetPageCount() const
191 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
193 return m_aPages
.Count();
196 int wxNotebook::GetRowCount() const
198 return TabCtrl_GetRowCount(m_hwnd
);
201 int wxNotebook::SetSelection(int nPage
)
203 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
205 ChangePage(m_nSelection
, nPage
);
207 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
210 void wxNotebook::AdvanceSelection(bool bForward
)
212 int nSel
= GetSelection();
213 int nMax
= GetPageCount() - 1;
215 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
217 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
220 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
222 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
225 tcItem
.mask
= TCIF_TEXT
;
226 tcItem
.pszText
= (char *)strText
.c_str();
228 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
231 wxString
wxNotebook::GetPageText(int nPage
) const
233 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
237 tcItem
.mask
= TCIF_TEXT
;
238 tcItem
.pszText
= buf
;
239 tcItem
.cchTextMax
= WXSIZEOF(buf
);
242 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
243 str
= tcItem
.pszText
;
248 int wxNotebook::GetPageImage(int nPage
) const
250 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
253 tcItem
.mask
= TCIF_IMAGE
;
255 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
258 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
260 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
263 tcItem
.mask
= TCIF_IMAGE
;
264 tcItem
.iImage
= nImage
;
266 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
269 void wxNotebook::SetImageList(wxImageList
* imageList
)
271 m_pImageList
= imageList
;
272 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
275 // ----------------------------------------------------------------------------
276 // wxNotebook operations
277 // ----------------------------------------------------------------------------
279 // remove one page from the notebook
280 bool wxNotebook::DeletePage(int nPage
)
282 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
284 TabCtrl_DeleteItem(m_hwnd
, nPage
);
286 delete m_aPages
[nPage
];
287 m_aPages
.Remove(nPage
);
292 // remove one page from the notebook, without deleting
293 bool wxNotebook::RemovePage(int nPage
)
295 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
297 TabCtrl_DeleteItem(m_hwnd
, nPage
);
299 m_aPages
.Remove(nPage
);
305 bool wxNotebook::DeleteAllPages()
307 TabCtrl_DeleteAllItems(m_hwnd
);
309 int nPageCount
= GetPageCount();
311 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
312 delete m_aPages
[nPage
];
319 // add a page to the notebook
320 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
321 const wxString
& strText
,
325 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
328 // same as AddPage() but does it at given position
329 bool wxNotebook::InsertPage(int nPage
,
330 wxNotebookPage
*pPage
,
331 const wxString
& strText
,
335 wxASSERT( pPage
!= NULL
);
336 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
338 // add the tab to the control
344 tcItem
.mask
|= TCIF_IMAGE
;
345 tcItem
.iImage
= imageId
;
350 if (!strText
.IsEmpty())
352 tcItem
.mask
|= TCIF_TEXT
;
353 tcItem
.pszText
= (char *)strText
.c_str();
356 tcItem
.pszText
= (char *) NULL
;
358 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
359 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
363 // save the pointer to the page
364 m_aPages
.Insert(pPage
, nPage
);
366 // some page must be selected: either this one or the first one if there is
367 // still no selection
369 m_nSelection
= nPage
;
370 else if ( m_nSelection
== -1 )
373 // don't show pages by default (we'll need to adjust their size first)
374 HWND hwnd
= (HWND
)pPage
->GetHWND();
375 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
380 // ----------------------------------------------------------------------------
381 // wxNotebook callbacks
382 // ----------------------------------------------------------------------------
384 void wxNotebook::OnSize(wxSizeEvent
& event
)
386 // make sure the current page is shown and has focus (it's useful because all
387 // pages are created invisible initially)
388 if ( m_nSelection
!= -1 ) {
389 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
394 // fit the notebook page to the tab control's display area
396 rc
.left
= rc
.top
= 0;
397 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
399 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
400 size_t nCount
= m_aPages
.Count();
401 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
402 wxNotebookPage
*pPage
= m_aPages
[nPage
];
403 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
404 if ( pPage
->GetAutoLayout() )
411 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
413 // is it our tab control?
414 if ( event
.GetEventObject() == this )
415 ChangePage(event
.GetOldSelection(), event
.GetSelection());
417 // we want to give others a chance to process this message as well
421 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
423 // set focus to the currently selected page if any
424 if ( m_nSelection
!= -1 )
425 m_aPages
[m_nSelection
]->SetFocus();
430 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
432 if ( event
.IsWindowChange() ) {
434 AdvanceSelection(event
.GetDirection());
437 // pass to the parent
439 event
.SetCurrentFocus(this);
440 GetParent()->GetEventHandler()->ProcessEvent(event
);
445 // ----------------------------------------------------------------------------
446 // wxNotebook base class virtuals
447 // ----------------------------------------------------------------------------
449 // override these 2 functions to do nothing: everything is done in OnSize
451 void wxNotebook::SetConstraintSizes(bool /* recurse */)
453 // don't set the sizes of the pages - their correct size is not yet known
454 wxControl::SetConstraintSizes(FALSE
);
457 bool wxNotebook::DoPhase(int /* nPhase */)
462 void wxNotebook::Command(wxCommandEvent
& event
)
464 wxFAIL_MSG("wxNotebook::Command not implemented");
467 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
* result
)
469 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
471 NMHDR
* hdr
= (NMHDR
*)lParam
;
472 switch ( hdr
->code
) {
474 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
477 case TCN_SELCHANGING
:
478 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
482 return wxControl::MSWNotify(wParam
, lParam
, result
);
485 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
486 event
.SetOldSelection(m_nSelection
);
487 event
.SetEventObject(this);
488 event
.SetInt(LOWORD(wParam
)); // ctrl id
490 bool processed
= GetEventHandler()->ProcessEvent(event
);
491 *result
= !event
.IsAllowed();
495 // ----------------------------------------------------------------------------
496 // wxNotebook helper functions
497 // ----------------------------------------------------------------------------
499 // hide the currently active panel and show the new one
500 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
502 // MT-FIXME should use a real semaphore
503 static bool s_bInsideChangePage
= FALSE
;
505 // when we call ProcessEvent(), our own OnSelChange() is called which calls
506 // this function - break the infinite loop
507 if ( s_bInsideChangePage
)
510 // it's not an error (the message may be generated by the tab control itself)
511 // and it may happen - just do nothing
512 if ( nSel
== nOldSel
)
515 s_bInsideChangePage
= TRUE
;
517 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
518 event
.SetSelection(nSel
);
519 event
.SetOldSelection(nOldSel
);
520 event
.SetEventObject(this);
521 if ( ProcessEvent(event
) && !event
.IsAllowed() )
523 // program doesn't allow the page change
524 s_bInsideChangePage
= FALSE
;
529 m_aPages
[nOldSel
]->Show(FALSE
);
531 wxNotebookPage
*pPage
= m_aPages
[nSel
];
535 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
539 s_bInsideChangePage
= FALSE
;
542 void wxNotebook::OnEraseBackground(wxEraseEvent
& event
)
547 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
549 void wxNotebook::SetTabSize(const wxSize
& sz
)
551 ::SendMessage((HWND
) GetHWND(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));