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 // 1) keyboard interface for changing pages ([Shift]+Ctrl-Tab)
14 // 2) using OnSize() for showing pages for the first time works, but it surely
16 // 3) I'm not sure that setting fonts works
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #pragma implementation "notebook.h"
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
38 #include <wx/string.h>
42 #include <wx/imaglist.h>
43 #include <wx/notebook.h>
45 #include <wx/msw/private.h>
47 // Windows standard headers
49 #error "wxNotebook is not supported under Windows 3.1"
53 #include "wx/msw/gnuwin32/extra.h"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // check that the page index is valid
63 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
66 #define m_hwnd (HWND)GetHWND()
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 #if !USE_SHARED_LIBRARIES
73 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
74 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
76 EVT_SIZE(wxNotebook::OnSize
)
77 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
78 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
81 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
82 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
85 // ============================================================================
87 // ============================================================================
89 // ----------------------------------------------------------------------------
90 // wxNotebook construction
91 // ----------------------------------------------------------------------------
93 // common part of all ctors
94 void wxNotebook::Init()
100 // default for dynamic class
101 wxNotebook::wxNotebook()
106 // the same arguments as for wxControl
107 wxNotebook::wxNotebook(wxWindow
*parent
,
112 const wxString
& name
)
116 Create(parent
, id
, pos
, size
, style
, name
);
120 bool wxNotebook::Create(wxWindow
*parent
,
125 const wxString
& name
)
131 m_windowId
= id
== -1 ? NewControlId() : id
;
134 m_backgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
135 m_foregroundColour
= *wxBLACK
;
137 m_defaultForegroundColour
= *wxBLACK
;
138 m_defaultBackgroundColour
= wxColour(GetSysColor(COLOR_BTNFACE
));
141 m_windowStyle
= style
;
143 long tabStyle
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| TCS_TABS
;
144 if ( m_windowStyle
& wxTC_MULTILINE
)
145 tabStyle
|= TCS_MULTILINE
;
146 if ( m_windowStyle
& wxBORDER
)
147 tabStyle
&= WS_BORDER
;
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 if ( parent
!= NULL
)
169 parent
->AddChild(this);
177 wxNotebook::~wxNotebook()
181 // ----------------------------------------------------------------------------
182 // wxNotebook accessors
183 // ----------------------------------------------------------------------------
184 int wxNotebook::GetPageCount() const
187 wxASSERT( (int)m_aPages
.Count() == TabCtrl_GetItemCount(m_hwnd
) );
189 return m_aPages
.Count();
192 int wxNotebook::GetRowCount() const
194 return TabCtrl_GetRowCount(m_hwnd
);
197 int wxNotebook::SetSelection(int nPage
)
199 wxASSERT( IS_VALID_PAGE(nPage
) );
201 ChangePage(m_nSelection
, nPage
);
203 return TabCtrl_SetCurSel(m_hwnd
, nPage
);
206 void wxNotebook::AdvanceSelection(bool bForward
)
208 int nSel
= GetSelection();
209 int nMax
= GetPageCount() - 1;
211 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
213 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
216 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
218 wxASSERT( IS_VALID_PAGE(nPage
) );
221 tcItem
.mask
= TCIF_TEXT
;
222 tcItem
.pszText
= (char *)strText
.c_str();
224 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
227 wxString
wxNotebook::GetPageText(int nPage
) const
229 wxASSERT( IS_VALID_PAGE(nPage
) );
233 tcItem
.mask
= TCIF_TEXT
;
234 tcItem
.pszText
= buf
;
235 tcItem
.cchTextMax
= WXSIZEOF(buf
);
238 if ( TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) )
239 str
= tcItem
.pszText
;
244 int wxNotebook::GetPageImage(int nPage
) const
246 wxASSERT( IS_VALID_PAGE(nPage
) );
249 tcItem
.mask
= TCIF_IMAGE
;
251 return TabCtrl_GetItem(m_hwnd
, nPage
, &tcItem
) ? tcItem
.iImage
: -1;
254 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
256 wxASSERT( IS_VALID_PAGE(nPage
) );
259 tcItem
.mask
= TCIF_IMAGE
;
260 tcItem
.iImage
= nImage
;
262 return TabCtrl_SetItem(m_hwnd
, nPage
, &tcItem
) != 0;
265 void wxNotebook::SetImageList(wxImageList
* imageList
)
267 m_pImageList
= imageList
;
268 TabCtrl_SetImageList(m_hwnd
, (HIMAGELIST
)imageList
->GetHIMAGELIST());
271 // ----------------------------------------------------------------------------
272 // wxNotebook operations
273 // ----------------------------------------------------------------------------
275 // remove one page from the notebook
276 bool wxNotebook::DeletePage(int nPage
)
278 wxCHECK( IS_VALID_PAGE(nPage
), FALSE
);
280 TabCtrl_DeleteItem(m_hwnd
, nPage
);
282 delete m_aPages
[nPage
];
283 m_aPages
.Remove(nPage
);
289 bool wxNotebook::DeleteAllPages()
291 TabCtrl_DeleteAllItems(m_hwnd
);
293 int nPageCount
= GetPageCount();
295 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
296 delete m_aPages
[nPage
];
303 // add a page to the notebook
304 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
305 const wxString
& strText
,
309 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
312 // same as AddPage() but does it at given position
313 bool wxNotebook::InsertPage(int nPage
,
314 wxNotebookPage
*pPage
,
315 const wxString
& strText
,
319 wxASSERT( pPage
!= NULL
);
320 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
322 // add the tab to the control
324 tcItem
.mask
= TCIF_TEXT
| TCIF_IMAGE
;
325 tcItem
.pszText
= (char *)strText
.c_str();
326 tcItem
.iImage
= imageId
;
328 if ( TabCtrl_InsertItem(m_hwnd
, nPage
, &tcItem
) == -1 ) {
329 wxLogError("Can't create the notebook page '%s'.", strText
.c_str());
333 // save the pointer to the page
334 m_aPages
.Insert(pPage
, nPage
);
336 // some page must be selected: either this one or the first one if there is
337 // still no selection
339 m_nSelection
= nPage
;
340 else if ( m_nSelection
== -1 )
343 // don't show pages by default (we'll need to adjust their size first)
344 HWND hwnd
= (HWND
)pPage
->GetHWND();
345 SetWindowLong(hwnd
, GWL_STYLE
, GetWindowLong(hwnd
, GWL_STYLE
) & ~WS_VISIBLE
);
350 // ----------------------------------------------------------------------------
351 // wxNotebook callbacks
352 // ----------------------------------------------------------------------------
354 // @@@ OnSize() is used for setting the font when it's called for the first
355 // time because doing it in ::Create() doesn't work (for unknown reasons)
356 void wxNotebook::OnSize(wxSizeEvent
& event
)
358 static bool s_bFirstTime
= TRUE
;
359 if ( s_bFirstTime
) {
360 SendMessage((HWND
)m_hwnd
, WM_SETFONT
,
361 (WPARAM
)GetStockObject(DEFAULT_GUI_FONT
),
362 MAKELPARAM(TRUE
, 0));
363 s_bFirstTime
= FALSE
;
366 // emulate page change (it's esp. important to do it first time because
367 // otherwise our page would stay invisible)
368 int nSel
= m_nSelection
;
372 // fit the notebook page to the tab control's display area
374 rc
.left
= rc
.top
= 0;
375 GetSize((int *)&rc
.right
, (int *)&rc
.bottom
);
377 TabCtrl_AdjustRect(m_hwnd
, FALSE
, &rc
);
378 uint nCount
= m_aPages
.Count();
379 for ( uint nPage
= 0; nPage
< nCount
; nPage
++ ) {
380 wxNotebookPage
*pPage
= m_aPages
[nPage
];
381 pPage
->SetSize(rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
382 if ( pPage
->GetAutoLayout() )
389 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
391 // is it our tab control?
392 if ( event
.GetEventObject() == this )
393 ChangePage(event
.GetOldSelection(), event
.GetSelection());
395 // we want to give others a chance to process this message as well
399 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
401 // set focus to the currently selected page if any
402 if ( m_nSelection
!= -1 )
403 m_aPages
[m_nSelection
]->SetFocus();
408 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
410 if ( event
.IsWindowChange() ) {
412 AdvanceSelection(event
.GetDirection());
415 // pass to the parent
417 event
.SetCurrentFocus(this);
418 GetParent()->ProcessEvent(event
);
423 // ----------------------------------------------------------------------------
424 // wxNotebook base class virtuals
425 // ----------------------------------------------------------------------------
427 // override these 2 functions to do nothing: everything is done in OnSize
429 void wxNotebook::SetConstraintSizes(bool /* recurse */)
431 // don't set the sizes of the pages - their correct size is not yet known
432 wxControl::SetConstraintSizes(FALSE
);
435 bool wxNotebook::DoPhase(int /* nPhase */)
440 void wxNotebook::Command(wxCommandEvent
& event
)
442 wxFAIL_MSG("wxNotebook::Command not implemented");
445 bool wxNotebook::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
447 wxNotebookEvent
event(wxEVT_NULL
, m_windowId
,
448 TabCtrl_GetCurSel(m_hwnd
), m_nSelection
);
450 NMHDR
* hdr
= (NMHDR
*)lParam
;
451 switch ( hdr
->code
) {
453 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
);
456 case TCN_SELCHANGING
:
457 event
.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
);
461 return wxControl::MSWNotify(wParam
, lParam
);
464 event
.SetEventObject(this);
465 event
.SetInt(LOWORD(wParam
));
467 return ProcessEvent(event
);
470 // ----------------------------------------------------------------------------
471 // wxNotebook helper functions
472 // ----------------------------------------------------------------------------
474 // hide the currently active panel and show the new one
475 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
477 wxASSERT( nOldSel
!= nSel
); // impossible
479 if ( nOldSel
!= -1 ) {
480 m_aPages
[nOldSel
]->Show(FALSE
);
483 wxNotebookPage
*pPage
= m_aPages
[nSel
];