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 // create the tab control.
147 m_hWnd
= (WXHWND
)CreateWindowEx
150 WC_TABCONTROL
, // class name for the tab control
153 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
154 (HWND
)parent
->GetHWND(), // parent window
155 (HMENU
)m_windowId
, // child id
156 wxGetInstance(), // current instance
157 NULL
// no class data
161 wxLogSysError("Can't create the notebook control");
165 // Not all compilers recognise SetWindowFont
166 ::SendMessage((HWND
) m_hwnd
, WM_SETFONT
,
167 (WPARAM
)::GetStockObject(DEFAULT_GUI_FONT
),TRUE
);
170 if ( parent
!= NULL
)
171 parent
->AddChild(this);
179 wxNotebook::~wxNotebook()
183 // ----------------------------------------------------------------------------
184 // wxNotebook accessors
185 // ----------------------------------------------------------------------------
186 int wxNotebook::GetPageCount() const
189 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
191 return m_aPages
.Count();
194 int wxNotebook::GetRowCount() const
196 return TabCtrl_GetRowCount(m_hwnd
);
199 int wxNotebook::SetSelection(int nPage
)
201 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
203 ChangePage(m_nSelection
, nPage
);
205 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
208 void wxNotebook::AdvanceSelection(bool bForward
)
210 int nSel
= GetSelection();
211 int nMax
= GetPageCount() - 1;
213 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
215 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
218 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
220 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
223 tcItem
.mask
= TCIF_TEXT
;
224 tcItem
.pszText
= (char *)strText
.c_str();
226 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
229 wxString
wxNotebook::GetPageText(int nPage
) const
231 wxCHECK_MSG( IS_VALID_PAGE(nPage
), "", "notebook page out of range" );
235 tcItem
.mask
= TCIF_TEXT
;
236 tcItem
.pszText
= buf
;
237 tcItem
.cchTextMax
= WXSIZEOF(buf
);
240 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
241 str
= tcItem
.pszText
;
246 int wxNotebook::GetPageImage(int nPage
) const
248 wxCHECK_MSG( IS_VALID_PAGE(nPage
), -1, "notebook page out of range" );
251 tcItem
.mask
= TCIF_IMAGE
;
253 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
256 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
258 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
261 tcItem
.mask
= TCIF_IMAGE
;
262 tcItem
.iImage
= nImage
;
264 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
267 void wxNotebook::SetImageList(wxImageList
* imageList
)
269 m_pImageList
= imageList
;
270 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
274 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
276 void wxNotebook::SetTabSize(const wxSize
& sz
)
278 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE
, 0, MAKELPARAM(sz
.x
, sz
.y
));
281 // ----------------------------------------------------------------------------
282 // wxNotebook operations
283 // ----------------------------------------------------------------------------
285 // remove one page from the notebook
286 bool wxNotebook::DeletePage(int nPage
)
288 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
290 TabCtrl_DeleteItem(m_hwnd
, nPage
);
292 delete m_aPages
[nPage
];
293 m_aPages
.Remove(nPage
);
298 // remove one page from the notebook, without deleting
299 bool wxNotebook::RemovePage(int nPage
)
301 wxCHECK_MSG( IS_VALID_PAGE(nPage
), FALSE
, "notebook page out of range" );
303 TabCtrl_DeleteItem(m_hwnd
, nPage
);
305 m_aPages
.Remove(nPage
);
311 bool wxNotebook::DeleteAllPages()
313 int nPageCount
= GetPageCount();
315 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
316 delete m_aPages
[nPage
];
320 TabCtrl_DeleteAllItems(m_hwnd
);
325 // add a page to the notebook
326 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
327 const wxString
& strText
,
331 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
334 // same as AddPage() but does it at given position
335 bool wxNotebook::InsertPage(int nPage
,
336 wxNotebookPage
*pPage
,
337 const wxString
& strText
,
341 wxASSERT( pPage
!= NULL
);
342 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
344 // add the tab to the control
350 tcItem
.mask
|= TCIF_IMAGE
;
351 tcItem
.iImage
= imageId
;
356 if (!strText
.IsEmpty())
358 tcItem
.mask
|= TCIF_TEXT
;
359 tcItem
.pszText
= (char *)strText
.c_str();
362 tcItem
.pszText
= (char *) NULL
;
364 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
365 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
369 // save the pointer to the page
370 m_aPages
.Insert(pPage
, nPage
);
372 // some page must be selected: either this one or the first one if there is
373 // still no selection
375 m_nSelection
= nPage
;
376 else if ( m_nSelection
== -1 )
379 // don't show pages by default (we'll need to adjust their size first)
380 HWND hwnd
= GetWinHwnd(pPage
);
381 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
383 // this updates internal flag too - otherwise it will get out of sync
389 // ----------------------------------------------------------------------------
390 // wxNotebook callbacks
391 // ----------------------------------------------------------------------------
393 void wxNotebook::OnWindowCreate(wxWindowCreateEvent
& event
)
395 // make sure the current page is shown and has focus (it's useful because all
396 // pages are created invisible initially)
397 if ( m_nSelection
!= -1 ) {
398 wxNotebookPage
*pPage
= m_aPages
[m_nSelection
];
403 // fit the notebook page to the tab control's display area
405 rc
.left
= rc
.top
= 0;
406 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
408 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
409 size_t nCount
= m_aPages
.Count();
410 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
411 wxNotebookPage
*pPage
= m_aPages
[nPage
];
412 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
413 if ( pPage
->GetAutoLayout() )
420 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
422 // is it our tab control?
423 if ( event
.GetEventObject() == this )
424 ChangePage(event
.GetOldSelection(), event
.GetSelection());
426 // we want to give others a chance to process this message as well
430 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
432 // set focus to the currently selected page if any
433 if ( m_nSelection
!= -1 )
434 m_aPages
[m_nSelection
]->SetFocus();
439 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
441 if ( event
.IsWindowChange() ) {
443 AdvanceSelection(event
.GetDirection());
446 // pass to the parent
448 event
.SetCurrentFocus(this);
449 GetParent()->GetEventHandler()->ProcessEvent(event
);
454 // ----------------------------------------------------------------------------
455 // wxNotebook base class virtuals
456 // ----------------------------------------------------------------------------
458 // override these 2 functions to do nothing: everything is done in OnSize
460 void wxNotebook::SetConstraintSizes(bool /* recurse */)
462 // don't set the sizes of the pages - their correct size is not yet known
463 wxControl::SetConstraintSizes(FALSE
);
466 bool wxNotebook::DoPhase(int /* nPhase */)
471 bool wxNotebook::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
* result
)
473 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
475 NMHDR
* hdr
= (NMHDR
*)lParam
;
476 switch ( hdr
->code
) {
478 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
481 case TCN_SELCHANGING
:
482 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
486 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
489 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
490 event
.SetOldSelection(m_nSelection
);
491 event
.SetEventObject(this);
492 event
.SetInt(idCtrl
);
494 bool processed
= GetEventHandler()->ProcessEvent(event
);
495 *result
= !event
.IsAllowed();
499 // ----------------------------------------------------------------------------
500 // wxNotebook helper functions
501 // ----------------------------------------------------------------------------
503 // hide the currently active panel and show the new one
504 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
506 // MT-FIXME should use a real semaphore
507 static bool s_bInsideChangePage
= FALSE
;
509 // when we call ProcessEvent(), our own OnSelChange() is called which calls
510 // this function - break the infinite loop
511 if ( s_bInsideChangePage
)
514 // it's not an error (the message may be generated by the tab control itself)
515 // and it may happen - just do nothing
516 if ( nSel
== nOldSel
)
519 s_bInsideChangePage
= TRUE
;
521 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
, m_windowId
);
522 event
.SetSelection(nSel
);
523 event
.SetOldSelection(nOldSel
);
524 event
.SetEventObject(this);
525 if ( ProcessEvent(event
) && !event
.IsAllowed() )
527 // program doesn't allow the page change
528 s_bInsideChangePage
= FALSE
;
533 m_aPages
[nOldSel
]->Show(FALSE
);
535 wxNotebookPage
*pPage
= m_aPages
[nSel
];
539 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
543 s_bInsideChangePage
= FALSE
;