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_WINDOW_CREATE(wxNotebook::OnWindowCreate
)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
75 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
79 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
87 // wxNotebook construction
88 // ----------------------------------------------------------------------------
90 // common part of all ctors
91 void wxNotebook::Init()
97 // default for dynamic class
98 wxNotebook::wxNotebook()
103 // the same arguments as for wxControl
104 wxNotebook::wxNotebook(wxWindow
*parent
,
109 const wxString
& name
)
113 Create(parent
, id
, pos
, size
, style
, name
);
117 bool wxNotebook::Create(wxWindow
*parent
,
122 const wxString
& name
)
128 m_windowId
= id
== -1 ? NewControlId() : id
;
131 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
132 m_foregroundColour
= *wxBLACK
;
135 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
137 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
139 if (m_windowStyle
& wxCLIP_CHILDREN
)
140 tabStyle
|= WS_CLIPCHILDREN
;
141 if ( m_windowStyle
& wxTC_MULTILINE
)
142 tabStyle
|= TCS_MULTILINE
;
143 if ( m_windowStyle
& wxBORDER
)
144 tabStyle
&= WS_BORDER
;
145 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
146 tabStyle
|= TCS_FIXEDWIDTH
;
148 // create the tab control.
149 m_hWnd
= (WXHWND
)CreateWindowEx
152 WC_TABCONTROL
, // class name for the tab control
155 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
156 (HWND
)parent
->GetHWND(), // parent window
157 (HMENU
)m_windowId
, // child id
158 wxGetInstance(), // current instance
159 NULL
// no class data
163 wxLogSysError("Can't create the notebook control");
167 // Not all compilers recognise SetWindowFont
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());
276 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
278 void wxNotebook::SetTabSize(const wxSize
& sz
)
280 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
283 // ----------------------------------------------------------------------------
284 // wxNotebook operations
285 // ----------------------------------------------------------------------------
287 // remove one page from the notebook
288 bool wxNotebook::DeletePage(int nPage
)
290 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
292 TabCtrl_DeleteItem(m_hwnd
, nPage
);
294 delete m_aPages
[nPage
];
295 m_aPages
.Remove(nPage
);
300 // remove one page from the notebook, without deleting
301 bool wxNotebook::RemovePage(int nPage
)
303 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
305 TabCtrl_DeleteItem(m_hwnd
, nPage
);
307 m_aPages
.Remove(nPage
);
313 bool wxNotebook::DeleteAllPages()
315 int nPageCount
= GetPageCount();
317 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
318 delete m_aPages
[nPage
];
322 TabCtrl_DeleteAllItems(m_hwnd
);
327 // add a page to the notebook
328 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
329 const wxString
& strText
,
333 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
336 // same as AddPage() but does it at given position
337 bool wxNotebook::InsertPage(int nPage
,
338 wxNotebookPage
*pPage
,
339 const wxString
& strText
,
343 wxASSERT( pPage
!= NULL
);
344 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
346 // add the tab to the control
352 tcItem
.mask
|= TCIF_IMAGE
;
353 tcItem
.iImage
= imageId
;
358 if (!strText
.IsEmpty())
360 tcItem
.mask
|= TCIF_TEXT
;
361 tcItem
.pszText
= (char *)strText
.c_str();
364 tcItem
.pszText
= (char *) NULL
;
366 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
367 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
371 // save the pointer to the page
372 m_aPages
.Insert(pPage
, nPage
);
374 // some page must be selected: either this one or the first one if there is
375 // still no selection
377 m_nSelection
= nPage
;
378 else if ( m_nSelection
== -1 )
381 // don't show pages by default (we'll need to adjust their size first)
382 HWND hwnd
= GetWinHwnd(pPage
);
383 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
385 // this updates internal flag too - otherwise it will get out of sync
391 // ----------------------------------------------------------------------------
392 // wxNotebook callbacks
393 // ----------------------------------------------------------------------------
395 void wxNotebook::OnWindowCreate(wxWindowCreateEvent
& event
)
397 // make sure the current page is shown and has focus (it's useful because all
398 // pages are created invisible initially)
399 if ( m_nSelection
!= -1 ) {
400 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
405 // fit the notebook page to the tab control's display area
407 rc
.left
= rc
.top
= 0;
408 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
410 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
411 size_t nCount
= m_aPages
.Count();
412 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
413 wxNotebookPage
*pPage
= m_aPages
[nPage
];
414 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
415 if ( pPage
->GetAutoLayout() )
422 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
424 // is it our tab control?
425 if ( event
.GetEventObject() == this )
426 ChangePage(event
.GetOldSelection(), event
.GetSelection());
428 // we want to give others a chance to process this message as well
432 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
434 // set focus to the currently selected page if any
435 if ( m_nSelection
!= -1 )
436 m_aPages
[m_nSelection
]->SetFocus();
441 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
443 if ( event
.IsWindowChange() ) {
445 AdvanceSelection(event
.GetDirection());
448 // pass to the parent
450 event
.SetCurrentFocus(this);
451 GetParent()->GetEventHandler()->ProcessEvent(event
);
456 // ----------------------------------------------------------------------------
457 // wxNotebook base class virtuals
458 // ----------------------------------------------------------------------------
460 // override these 2 functions to do nothing: everything is done in OnSize
462 void wxNotebook::SetConstraintSizes(bool /* recurse */)
464 // don't set the sizes of the pages - their correct size is not yet known
465 wxControl::SetConstraintSizes(FALSE
);
468 bool wxNotebook::DoPhase(int /* nPhase */)
473 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
475 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
477 NMHDR
* hdr
= (NMHDR
*)lParam
;
478 switch ( hdr
->code
) {
480 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
483 case TCN_SELCHANGING
:
484 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
488 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
491 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
492 event
.SetOldSelection(m_nSelection
);
493 event
.SetEventObject(this);
494 event
.SetInt(idCtrl
);
496 bool processed
= GetEventHandler()->ProcessEvent(event
);
497 *result
= !event
.IsAllowed();
501 // ----------------------------------------------------------------------------
502 // wxNotebook helper functions
503 // ----------------------------------------------------------------------------
505 // hide the currently active panel and show the new one
506 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
508 // MT-FIXME should use a real semaphore
509 static bool s_bInsideChangePage
= FALSE
;
511 // when we call ProcessEvent(), our own OnSelChange() is called which calls
512 // this function - break the infinite loop
513 if ( s_bInsideChangePage
)
516 // it's not an error (the message may be generated by the tab control itself)
517 // and it may happen - just do nothing
518 if ( nSel
== nOldSel
)
521 s_bInsideChangePage
= TRUE
;
523 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
524 event
.SetSelection(nSel
);
525 event
.SetOldSelection(nOldSel
);
526 event
.SetEventObject(this);
527 if ( ProcessEvent(event
) && !event
.IsAllowed() )
529 // program doesn't allow the page change
530 s_bInsideChangePage
= FALSE
;
535 m_aPages
[nOldSel
]->Show(FALSE
);
537 wxNotebookPage
*pPage
= m_aPages
[nSel
];
541 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
545 s_bInsideChangePage
= FALSE
;