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 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "notebook.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
32 #include <wx/string.h>
36 #include <wx/imaglist.h>
37 #include <wx/notebook.h>
39 #include <wx/msw/private.h>
41 // Windows standard headers
43 #error "wxNotebook is not supported under Windows 3.1"
46 #include <windowsx.h> // for SetWindowFont
49 #include "wx/msw/gnuwin32/extra.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // check that the page index is valid
59 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
62 #define m_hwnd (HWND)GetHWND()
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 #if !USE_SHARED_LIBRARIES
69 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
70 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
72 EVT_SIZE(wxNotebook::OnSize
)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
74 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
77 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
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
;
133 m_defaultForegroundColour
= *wxBLACK
;
134 m_defaultBackgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
137 m_windowStyle
= style
| wxTAB_TRAVERSAL
;
139 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
140 if ( m_windowStyle
& wxTC_MULTILINE
)
141 tabStyle
|= TCS_MULTILINE
;
142 if ( m_windowStyle
& wxBORDER
)
143 tabStyle
&= WS_BORDER
;
145 // create the tab control.
146 m_hWnd
= (WXHWND
)CreateWindowEx
149 WC_TABCONTROL
, // class name for the tab control
152 pos
.x
, pos
.y
, size
.x
, size
.y
, // size and position
153 (HWND
)parent
->GetHWND(), // parent window
154 (HMENU
)m_windowId
, // child id
155 wxGetInstance(), // current instance
156 NULL
// no class data
160 wxLogSysError("Can't create the notebook control");
164 // Not all compilers recognise SetWindowFont
165 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
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 wxASSERT( IS_VALID_PAGE(nPage
) );
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 wxASSERT( IS_VALID_PAGE(nPage
) );
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 wxASSERT( IS_VALID_PAGE(nPage
) );
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 wxASSERT( IS_VALID_PAGE(nPage
) );
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 wxASSERT( IS_VALID_PAGE(nPage
) );
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());
273 // ----------------------------------------------------------------------------
274 // wxNotebook operations
275 // ----------------------------------------------------------------------------
277 // remove one page from the notebook
278 bool wxNotebook::DeletePage(int nPage
)
280 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
282 TabCtrl_DeleteItem(m_hwnd
, nPage
);
284 delete m_aPages
[nPage
];
285 m_aPages
.Remove(nPage
);
291 bool wxNotebook::DeleteAllPages()
293 TabCtrl_DeleteAllItems(m_hwnd
);
295 int nPageCount
= GetPageCount();
297 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
298 delete m_aPages
[nPage
];
305 // add a page to the notebook
306 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
307 const wxString
& strText
,
311 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
314 // same as AddPage() but does it at given position
315 bool wxNotebook::InsertPage(int nPage
,
316 wxNotebookPage
*pPage
,
317 const wxString
& strText
,
321 wxASSERT( pPage
!= NULL
);
322 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
324 // add the tab to the control
326 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
327 tcItem
.pszText
= (char *)strText
.c_str();
328 tcItem
.iImage
= imageId
;
330 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
331 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
335 // save the pointer to the page
336 m_aPages
.Insert(pPage
, nPage
);
338 // some page must be selected: either this one or the first one if there is
339 // still no selection
341 m_nSelection
= nPage
;
342 else if ( m_nSelection
== -1 )
345 // don't show pages by default (we'll need to adjust their size first)
346 HWND hwnd
= (HWND
)pPage
->GetHWND();
347 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
352 // ----------------------------------------------------------------------------
353 // wxNotebook callbacks
354 // ----------------------------------------------------------------------------
356 void wxNotebook::OnSize(wxSizeEvent
& event
)
358 // emulate page change (it's esp. important to do it first time because
359 // otherwise our page would stay invisible)
360 int nSel
= m_nSelection
;
364 // fit the notebook page to the tab control's display area
366 rc
.left
= rc
.top
= 0;
367 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
369 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
370 size_t nCount
= m_aPages
.Count();
371 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ ) {
372 wxNotebookPage
*pPage
= m_aPages
[nPage
];
373 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
374 if ( pPage
->GetAutoLayout() )
381 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
383 // is it our tab control?
384 if ( event
.GetEventObject() == this )
385 ChangePage(event
.GetOldSelection(), event
.GetSelection());
387 // we want to give others a chance to process this message as well
391 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
393 // set focus to the currently selected page if any
394 if ( m_nSelection
!= -1 )
395 m_aPages
[m_nSelection
]->SetFocus();
400 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
402 if ( event
.IsWindowChange() ) {
404 AdvanceSelection(event
.GetDirection());
407 // pass to the parent
409 event
.SetCurrentFocus(this);
410 GetParent()->ProcessEvent(event
);
415 // ----------------------------------------------------------------------------
416 // wxNotebook base class virtuals
417 // ----------------------------------------------------------------------------
419 // override these 2 functions to do nothing: everything is done in OnSize
421 void wxNotebook::SetConstraintSizes(bool /* recurse */)
423 // don't set the sizes of the pages - their correct size is not yet known
424 wxControl::SetConstraintSizes(FALSE
);
427 bool wxNotebook::DoPhase(int /* nPhase */)
432 void wxNotebook::Command(wxCommandEvent
& event
)
434 wxFAIL_MSG("wxNotebook::Command not implemented");
437 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
439 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
);
441 NMHDR
* hdr
= (NMHDR
*)lParam
;
442 switch ( hdr
->code
) {
444 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
447 case TCN_SELCHANGING
:
448 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
451 // prevent this msg from being passed to wxControl::MSWNotify which would
452 // retrun FALSE disabling the change of page
457 return wxControl::MSWNotify(wParam
, lParam
);
460 event
.SetSelection(TabCtrl_GetCurSel(m_hwnd
));
461 event
.SetOldSelection(m_nSelection
);
462 event
.SetEventObject(this);
463 event
.SetInt(LOWORD(wParam
));
465 return ProcessEvent(event
);
468 // ----------------------------------------------------------------------------
469 // wxNotebook helper functions
470 // ----------------------------------------------------------------------------
472 // hide the currently active panel and show the new one
473 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
475 // it's not an error (the message may be generated by the tab control itself)
476 // and it may happen - just do nothing
477 if ( nSel
== nOldSel
)
481 m_aPages
[nOldSel
]->Show(FALSE
);
483 wxNotebookPage
*pPage
= m_aPages
[nSel
];