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 // doesn't work yet EVT_WINDOW_CREATE(wxNotebook::OnWindowCreate)
72 EVT_SIZE(wxNotebook::OnWindowCreate
)
74 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
76 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
79 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
80 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
88 // wxNotebook construction
89 // ----------------------------------------------------------------------------
91 // common part of all ctors
92 void wxNotebook::Init()
98 // default for dynamic class
99 wxNotebook::wxNotebook()
104 // the same arguments as for wxControl
105 wxNotebook::wxNotebook(wxWindow
*parent
,
110 const wxString
& name
)
114 Create(parent
, id
, pos
, size
, style
, name
);
118 bool wxNotebook::Create(wxWindow
*parent
,
123 const wxString
& name
)
129 m_windowId
= id
== -1 ? NewControlId() : id
;
132 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
133 m_foregroundColour
= *wxBLACK
;
136 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
138 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
140 if (m_windowStyle
& wxCLIP_CHILDREN
)
141 tabStyle
|= WS_CLIPCHILDREN
;
142 if ( m_windowStyle
& wxTC_MULTILINE
)
143 tabStyle
|= TCS_MULTILINE
;
144 if ( m_windowStyle
& wxBORDER
)
145 tabStyle
&= WS_BORDER
;
146 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
147 tabStyle
|= TCS_FIXEDWIDTH
;
149 // create the tab control.
150 m_hWnd
= (WXHWND
)CreateWindowEx
153 WC_TABCONTROL
, // class name for the tab control
156 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
157 (HWND
)parent
->GetHWND(), // parent window
158 (HMENU
)m_windowId
, // child id
159 wxGetInstance(), // current instance
160 NULL
// no class data
164 wxLogSysError("Can't create the notebook control");
168 // Not all compilers recognise SetWindowFont
169 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
170 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
173 if ( parent
!= NULL
)
174 parent
->AddChild(this);
182 wxNotebook::~wxNotebook()
186 // ----------------------------------------------------------------------------
187 // wxNotebook accessors
188 // ----------------------------------------------------------------------------
189 int wxNotebook::GetPageCount() const
192 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
194 return m_aPages
.Count();
197 int wxNotebook::GetRowCount() const
199 return TabCtrl_GetRowCount(m_hwnd
);
202 int wxNotebook::SetSelection(int nPage
)
204 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
206 ChangePage(m_nSelection
, nPage
);
208 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
211 void wxNotebook::AdvanceSelection(bool bForward
)
213 int nSel
= GetSelection();
214 int nMax
= GetPageCount() - 1;
216 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
218 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
221 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
223 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
226 tcItem
.mask
= TCIF_TEXT
;
227 tcItem
.pszText
= (char *)strText
.c_str();
229 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
232 wxString
wxNotebook::GetPageText(int nPage
) const
234 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
238 tcItem
.mask
= TCIF_TEXT
;
239 tcItem
.pszText
= buf
;
240 tcItem
.cchTextMax
= WXSIZEOF(buf
);
243 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
244 str
= tcItem
.pszText
;
249 int wxNotebook::GetPageImage(int nPage
) const
251 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
254 tcItem
.mask
= TCIF_IMAGE
;
256 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
259 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
261 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
264 tcItem
.mask
= TCIF_IMAGE
;
265 tcItem
.iImage
= nImage
;
267 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
270 void wxNotebook::SetImageList(wxImageList
* imageList
)
272 m_pImageList
= imageList
;
273 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
277 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
279 void wxNotebook::SetTabSize(const wxSize
& sz
)
281 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
284 // ----------------------------------------------------------------------------
285 // wxNotebook operations
286 // ----------------------------------------------------------------------------
288 // remove one page from the notebook
289 bool wxNotebook::DeletePage(int nPage
)
291 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
293 TabCtrl_DeleteItem(m_hwnd
, nPage
);
295 delete m_aPages
[nPage
];
296 m_aPages
.Remove(nPage
);
301 // remove one page from the notebook, without deleting
302 bool wxNotebook::RemovePage(int nPage
)
304 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
306 TabCtrl_DeleteItem(m_hwnd
, nPage
);
308 m_aPages
.Remove(nPage
);
314 bool wxNotebook::DeleteAllPages()
316 int nPageCount
= GetPageCount();
318 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
319 delete m_aPages
[nPage
];
323 TabCtrl_DeleteAllItems(m_hwnd
);
328 // add a page to the notebook
329 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
330 const wxString
& strText
,
334 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
337 // same as AddPage() but does it at given position
338 bool wxNotebook::InsertPage(int nPage
,
339 wxNotebookPage
*pPage
,
340 const wxString
& strText
,
344 wxASSERT( pPage
!= NULL
);
345 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
347 // add the tab to the control
353 tcItem
.mask
|= TCIF_IMAGE
;
354 tcItem
.iImage
= imageId
;
359 if (!strText
.IsEmpty())
361 tcItem
.mask
|= TCIF_TEXT
;
362 tcItem
.pszText
= (char *)strText
.c_str();
365 tcItem
.pszText
= (char *) NULL
;
367 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
368 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
372 // save the pointer to the page
373 m_aPages
.Insert(pPage
, nPage
);
375 // some page must be selected: either this one or the first one if there is
376 // still no selection
378 m_nSelection
= nPage
;
379 else if ( m_nSelection
== -1 )
382 // don't show pages by default (we'll need to adjust their size first)
383 HWND hwnd
= GetWinHwnd(pPage
);
384 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
386 // this updates internal flag too - otherwise it will get out of sync
392 // ----------------------------------------------------------------------------
393 // wxNotebook callbacks
394 // ----------------------------------------------------------------------------
396 void wxNotebook::OnWindowCreate(wxWindowCreateEvent
& event
)
398 // make sure the current page is shown and has focus (it's useful because all
399 // pages are created invisible initially)
400 if ( m_nSelection
!= -1 ) {
401 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
406 // fit the notebook page to the tab control's display area
408 rc
.left
= rc
.top
= 0;
409 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
411 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
412 size_t nCount
= m_aPages
.Count();
413 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
414 wxNotebookPage
*pPage
= m_aPages
[nPage
];
415 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
416 if ( pPage
->GetAutoLayout() )
423 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
425 // is it our tab control?
426 if ( event
.GetEventObject() == this )
427 ChangePage(event
.GetOldSelection(), event
.GetSelection());
429 // we want to give others a chance to process this message as well
433 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
435 // set focus to the currently selected page if any
436 if ( m_nSelection
!= -1 )
437 m_aPages
[m_nSelection
]->SetFocus();
442 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
444 if ( event
.IsWindowChange() ) {
446 AdvanceSelection(event
.GetDirection());
449 // pass to the parent
451 event
.SetCurrentFocus(this);
452 GetParent()->GetEventHandler()->ProcessEvent(event
);
457 // ----------------------------------------------------------------------------
458 // wxNotebook base class virtuals
459 // ----------------------------------------------------------------------------
461 // override these 2 functions to do nothing: everything is done in OnSize
463 void wxNotebook::SetConstraintSizes(bool /* recurse */)
465 // don't set the sizes of the pages - their correct size is not yet known
466 wxControl::SetConstraintSizes(FALSE
);
469 bool wxNotebook::DoPhase(int /* nPhase */)
474 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
476 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
478 NMHDR
* hdr
= (NMHDR
*)lParam
;
479 switch ( hdr
->code
) {
481 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
484 case TCN_SELCHANGING
:
485 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
489 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
492 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
493 event
.SetOldSelection(m_nSelection
);
494 event
.SetEventObject(this);
495 event
.SetInt(idCtrl
);
497 bool processed
= GetEventHandler()->ProcessEvent(event
);
498 *result
= !event
.IsAllowed();
502 // ----------------------------------------------------------------------------
503 // wxNotebook helper functions
504 // ----------------------------------------------------------------------------
506 // hide the currently active panel and show the new one
507 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
509 // MT-FIXME should use a real semaphore
510 static bool s_bInsideChangePage
= FALSE
;
512 // when we call ProcessEvent(), our own OnSelChange() is called which calls
513 // this function - break the infinite loop
514 if ( s_bInsideChangePage
)
517 // it's not an error (the message may be generated by the tab control itself)
518 // and it may happen - just do nothing
519 if ( nSel
== nOldSel
)
522 s_bInsideChangePage
= TRUE
;
524 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
525 event
.SetSelection(nSel
);
526 event
.SetOldSelection(nOldSel
);
527 event
.SetEventObject(this);
528 if ( ProcessEvent(event
) && !event
.IsAllowed() )
530 // program doesn't allow the page change
531 s_bInsideChangePage
= FALSE
;
536 m_aPages
[nOldSel
]->Show(FALSE
);
538 wxNotebookPage
*pPage
= m_aPages
[nSel
];
542 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
546 s_bInsideChangePage
= FALSE
;