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
)
126 CreateBase(parent
, id
, pos
, size
, style
, name
);
129 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
130 m_foregroundColour
= *wxBLACK
;
133 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
135 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
137 if (m_windowStyle
& wxCLIP_CHILDREN
)
138 tabStyle
|= WS_CLIPCHILDREN
;
139 if ( m_windowStyle
& wxTC_MULTILINE
)
140 tabStyle
|= TCS_MULTILINE
;
141 if ( m_windowStyle
& wxBORDER
)
142 tabStyle
&= WS_BORDER
;
143 if (m_windowStyle
& wxNB_FIXEDWIDTH
)
144 tabStyle
|= TCS_FIXEDWIDTH
;
146 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL
,
147 this, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
,
153 // Not all compilers recognise SetWindowFont
154 ::SendMessage(GetHwnd(), WM_SETFONT
,
155 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
), TRUE
);
158 if ( parent
!= NULL
)
159 parent
->AddChild(this);
167 wxNotebook::~wxNotebook()
171 // ----------------------------------------------------------------------------
172 // wxNotebook accessors
173 // ----------------------------------------------------------------------------
174 int wxNotebook::GetPageCount() const
177 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
179 return m_aPages
.Count();
182 int wxNotebook::GetRowCount() const
184 return TabCtrl_GetRowCount(m_hwnd
);
187 int wxNotebook::SetSelection(int nPage
)
189 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
191 ChangePage(m_nSelection
, nPage
);
193 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
196 void wxNotebook::AdvanceSelection(bool bForward
)
198 int nSel
= GetSelection();
199 int nMax
= GetPageCount() - 1;
201 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
203 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
206 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
208 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
211 tcItem
.mask
= TCIF_TEXT
;
212 tcItem
.pszText
= (char *)strText
.c_str();
214 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
217 wxString
wxNotebook::GetPageText(int nPage
) const
219 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
223 tcItem
.mask
= TCIF_TEXT
;
224 tcItem
.pszText
= buf
;
225 tcItem
.cchTextMax
= WXSIZEOF(buf
);
228 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
229 str
= tcItem
.pszText
;
234 int wxNotebook::GetPageImage(int nPage
) const
236 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
239 tcItem
.mask
= TCIF_IMAGE
;
241 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
244 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
246 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
249 tcItem
.mask
= TCIF_IMAGE
;
250 tcItem
.iImage
= nImage
;
252 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
255 void wxNotebook::SetImageList(wxImageList
* imageList
)
257 m_pImageList
= imageList
;
258 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
262 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
264 void wxNotebook::SetTabSize(const wxSize
& sz
)
266 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
269 // ----------------------------------------------------------------------------
270 // wxNotebook operations
271 // ----------------------------------------------------------------------------
273 // remove one page from the notebook
274 bool wxNotebook::DeletePage(int nPage
)
276 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
278 TabCtrl_DeleteItem(m_hwnd
, nPage
);
280 delete m_aPages
[nPage
];
281 m_aPages
.Remove(nPage
);
286 // remove one page from the notebook, without deleting
287 bool wxNotebook::RemovePage(int nPage
)
289 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
291 TabCtrl_DeleteItem(m_hwnd
, nPage
);
293 m_aPages
.Remove(nPage
);
299 bool wxNotebook::DeleteAllPages()
301 int nPageCount
= GetPageCount();
303 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
304 delete m_aPages
[nPage
];
308 TabCtrl_DeleteAllItems(m_hwnd
);
313 // add a page to the notebook
314 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
315 const wxString
& strText
,
319 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
322 // same as AddPage() but does it at given position
323 bool wxNotebook::InsertPage(int nPage
,
324 wxNotebookPage
*pPage
,
325 const wxString
& strText
,
329 wxASSERT( pPage
!= NULL
);
330 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
332 // add the tab to the control
338 tcItem
.mask
|= TCIF_IMAGE
;
339 tcItem
.iImage
= imageId
;
344 if (!strText
.IsEmpty())
346 tcItem
.mask
|= TCIF_TEXT
;
347 tcItem
.pszText
= (char *)strText
.c_str();
350 tcItem
.pszText
= (char *) NULL
;
352 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
353 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
357 // save the pointer to the page
358 m_aPages
.Insert(pPage
, nPage
);
360 // some page must be selected: either this one or the first one if there is
361 // still no selection
363 m_nSelection
= nPage
;
364 else if ( m_nSelection
== -1 )
367 // don't show pages by default (we'll need to adjust their size first)
368 HWND hwnd
= GetWinHwnd(pPage
);
369 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
371 // this updates internal flag too - otherwise it will get out of sync
377 // ----------------------------------------------------------------------------
378 // wxNotebook callbacks
379 // ----------------------------------------------------------------------------
381 void wxNotebook::OnWindowCreate(wxWindowCreateEvent
& event
)
383 // make sure the current page is shown and has focus (it's useful because all
384 // pages are created invisible initially)
385 if ( m_nSelection
!= -1 ) {
386 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
391 // fit the notebook page to the tab control's display area
393 rc
.left
= rc
.top
= 0;
394 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
396 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
397 size_t nCount
= m_aPages
.Count();
398 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
399 wxNotebookPage
*pPage
= m_aPages
[nPage
];
400 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
401 if ( pPage
->GetAutoLayout() )
408 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
410 // is it our tab control?
411 if ( event
.GetEventObject() == this )
412 ChangePage(event
.GetOldSelection(), event
.GetSelection());
414 // we want to give others a chance to process this message as well
418 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
420 // set focus to the currently selected page if any
421 if ( m_nSelection
!= -1 )
422 m_aPages
[m_nSelection
]->SetFocus();
427 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
429 if ( event
.IsWindowChange() ) {
431 AdvanceSelection(event
.GetDirection());
434 // pass to the parent
436 event
.SetCurrentFocus(this);
437 GetParent()->GetEventHandler()->ProcessEvent(event
);
442 // ----------------------------------------------------------------------------
443 // wxNotebook base class virtuals
444 // ----------------------------------------------------------------------------
446 // override these 2 functions to do nothing: everything is done in OnSize
448 void wxNotebook::SetConstraintSizes(bool /* recurse */)
450 // don't set the sizes of the pages - their correct size is not yet known
451 wxControl::SetConstraintSizes(FALSE
);
454 bool wxNotebook::DoPhase(int /* nPhase */)
459 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
461 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
463 NMHDR
* hdr
= (NMHDR
*)lParam
;
464 switch ( hdr
->code
) {
466 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
469 case TCN_SELCHANGING
:
470 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
474 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
477 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
478 event
.SetOldSelection(m_nSelection
);
479 event
.SetEventObject(this);
480 event
.SetInt(idCtrl
);
482 bool processed
= GetEventHandler()->ProcessEvent(event
);
483 *result
= !event
.IsAllowed();
487 // ----------------------------------------------------------------------------
488 // wxNotebook helper functions
489 // ----------------------------------------------------------------------------
491 // hide the currently active panel and show the new one
492 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
494 // MT-FIXME should use a real semaphore
495 static bool s_bInsideChangePage
= FALSE
;
497 // when we call ProcessEvent(), our own OnSelChange() is called which calls
498 // this function - break the infinite loop
499 if ( s_bInsideChangePage
)
502 // it's not an error (the message may be generated by the tab control itself)
503 // and it may happen - just do nothing
504 if ( nSel
== nOldSel
)
507 s_bInsideChangePage
= TRUE
;
509 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
510 event
.SetSelection(nSel
);
511 event
.SetOldSelection(nOldSel
);
512 event
.SetEventObject(this);
513 if ( ProcessEvent(event
) && !event
.IsAllowed() )
515 // program doesn't allow the page change
516 s_bInsideChangePage
= FALSE
;
521 m_aPages
[nOldSel
]->Show(FALSE
);
523 wxNotebookPage
*pPage
= m_aPages
[nSel
];
527 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
531 s_bInsideChangePage
= FALSE
;