]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
Minor corrections to be able to compile with VC++ 1.5C and VC++ 4.1
[wxWidgets.git] / src / msw / notebook.cpp
CommitLineData
88310e2e
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/notebook.cpp
3// Purpose: implementation of wxNotebook
4// Author: Vadim Zeitlin
907f37b3 5// Modified by:
88310e2e
VZ
6// Created: 11.06.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
88310e2e 12#ifdef __GNUG__
a3b46648 13#pragma implementation "notebook.h"
88310e2e
VZ
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
a3b46648 20#pragma hdrstop
88310e2e
VZ
21#endif
22
23// wxWindows
24#ifndef WX_PRECOMP
25 #include <wx/string.h>
26#endif // WX_PRECOMP
27
28#include <wx/log.h>
29#include <wx/imaglist.h>
2432b92d
JS
30#include <wx/event.h>
31#include <wx/control.h>
88310e2e
VZ
32#include <wx/notebook.h>
33
34#include <wx/msw/private.h>
35
36// Windows standard headers
37#ifndef __WIN95__
2432b92d 38 #error "wxNotebook is only supported Windows 95 and above"
88310e2e
VZ
39#endif //Win95
40
aaab7c01
VZ
41#include <windowsx.h> // for SetWindowFont
42
57c208c5 43#ifndef __TWIN32__
88310e2e 44#ifdef __GNUWIN32__
65fd5cb0 45#ifndef wxUSE_NORLANDER_HEADERS
88310e2e 46 #include "wx/msw/gnuwin32/extra.h"
57c208c5
JS
47#endif
48#endif
65fd5cb0 49#endif
57c208c5 50
65fd5cb0 51#if !defined(__GNUWIN32__) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
88310e2e
VZ
52 #include <commctrl.h>
53#endif
54
55// ----------------------------------------------------------------------------
56// macros
57// ----------------------------------------------------------------------------
58
59// check that the page index is valid
60#define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
61
62// hide the ugly cast
63#define m_hwnd (HWND)GetHWND()
64
65// ----------------------------------------------------------------------------
66// event table
67// ----------------------------------------------------------------------------
68
69#if !USE_SHARED_LIBRARIES
70 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
71 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
72
9026ad85 73 EVT_SIZE(wxNotebook::OnSize)
42e69d6b 74
88310e2e 75 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
42e69d6b 76
88310e2e
VZ
77 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
78 END_EVENT_TABLE()
79
80 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
92976ab6 81 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
82#endif
83
84// ============================================================================
85// implementation
86// ============================================================================
87
88// ----------------------------------------------------------------------------
89// wxNotebook construction
90// ----------------------------------------------------------------------------
91
92// common part of all ctors
93void wxNotebook::Init()
94{
95 m_pImageList = NULL;
96 m_nSelection = -1;
97}
98
99// default for dynamic class
100wxNotebook::wxNotebook()
101{
102 Init();
103}
104
105// the same arguments as for wxControl
106wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 107 wxWindowID id,
88310e2e
VZ
108 const wxPoint& pos,
109 const wxSize& size,
8b9518ee 110 long style,
88310e2e
VZ
111 const wxString& name)
112{
113 Init();
114
115 Create(parent, id, pos, size, style, name);
116}
117
118// Create() function
119bool wxNotebook::Create(wxWindow *parent,
8b9518ee 120 wxWindowID id,
88310e2e
VZ
121 const wxPoint& pos,
122 const wxSize& size,
8b9518ee 123 long style,
88310e2e
VZ
124 const wxString& name)
125{
126 // base init
8d99be5f
VZ
127 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
128 return FALSE;
88310e2e
VZ
129
130 // colors and font
131 m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
132 m_foregroundColour = *wxBLACK ;
133
88310e2e 134 // style
fd2daa68 135 m_windowStyle = style | wxTAB_TRAVERSAL;
88310e2e 136
d13c32e9
JS
137 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
138
139 if (m_windowStyle & wxCLIP_CHILDREN)
140 tabStyle |= WS_CLIPCHILDREN;
88310e2e
VZ
141 if ( m_windowStyle & wxTC_MULTILINE )
142 tabStyle |= TCS_MULTILINE;
143 if ( m_windowStyle & wxBORDER )
144 tabStyle &= WS_BORDER;
58a8ab88
JS
145 if (m_windowStyle & wxNB_FIXEDWIDTH)
146 tabStyle |= TCS_FIXEDWIDTH ;
88310e2e 147
789295bf
VZ
148 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
149 this, NULL, pos.x, pos.y, size.x, size.y,
150 tabStyle, NULL, 0) )
151 {
88310e2e
VZ
152 return FALSE;
153 }
154
27529614 155 // Not all compilers recognise SetWindowFont
789295bf
VZ
156 ::SendMessage(GetHwnd(), WM_SETFONT,
157 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
27529614 158
aaab7c01 159
907f37b3 160 if ( parent != NULL )
88310e2e 161 parent->AddChild(this);
907f37b3 162
88310e2e
VZ
163 SubclassWin(m_hWnd);
164
165 return TRUE;
166}
167
168// dtor
169wxNotebook::~wxNotebook()
170{
171}
172
173// ----------------------------------------------------------------------------
174// wxNotebook accessors
175// ----------------------------------------------------------------------------
176int wxNotebook::GetPageCount() const
177{
178 // consistency check
179 wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) );
180
181 return m_aPages.Count();
182}
183
184int wxNotebook::GetRowCount() const
185{
186 return TabCtrl_GetRowCount(m_hwnd);
187}
188
189int wxNotebook::SetSelection(int nPage)
190{
837e5743 191 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("notebook page out of range") );
88310e2e
VZ
192
193 ChangePage(m_nSelection, nPage);
194
195 return TabCtrl_SetCurSel(m_hwnd, nPage);
196}
197
198void wxNotebook::AdvanceSelection(bool bForward)
199{
200 int nSel = GetSelection();
201 int nMax = GetPageCount() - 1;
202 if ( bForward )
203 SetSelection(nSel == nMax ? 0 : nSel + 1);
204 else
205 SetSelection(nSel == 0 ? nMax : nSel - 1);
206}
207
208bool wxNotebook::SetPageText(int nPage, const wxString& strText)
209{
837e5743 210 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
88310e2e
VZ
211
212 TC_ITEM tcItem;
213 tcItem.mask = TCIF_TEXT;
837e5743 214 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e
VZ
215
216 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
217}
218
219wxString wxNotebook::GetPageText(int nPage) const
220{
837e5743 221 wxCHECK_MSG( IS_VALID_PAGE(nPage), _T(""), _T("notebook page out of range") );
88310e2e 222
837e5743 223 wxChar buf[256];
88310e2e
VZ
224 TC_ITEM tcItem;
225 tcItem.mask = TCIF_TEXT;
226 tcItem.pszText = buf;
227 tcItem.cchTextMax = WXSIZEOF(buf);
228
229 wxString str;
230 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
231 str = tcItem.pszText;
232
233 return str;
234}
235
236int wxNotebook::GetPageImage(int nPage) const
237{
837e5743 238 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("notebook page out of range") );
88310e2e
VZ
239
240 TC_ITEM tcItem;
241 tcItem.mask = TCIF_IMAGE;
242
243 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
244}
245
246bool wxNotebook::SetPageImage(int nPage, int nImage)
247{
837e5743 248 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
88310e2e
VZ
249
250 TC_ITEM tcItem;
251 tcItem.mask = TCIF_IMAGE;
252 tcItem.iImage = nImage;
253
254 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
255}
256
257void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 258{
88310e2e
VZ
259 m_pImageList = imageList;
260 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
261}
262
42e69d6b
VZ
263
264// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
265// style.
266void wxNotebook::SetTabSize(const wxSize& sz)
267{
268 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
269}
270
88310e2e
VZ
271// ----------------------------------------------------------------------------
272// wxNotebook operations
273// ----------------------------------------------------------------------------
274
275// remove one page from the notebook
276bool wxNotebook::DeletePage(int nPage)
277{
837e5743 278 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
88310e2e 279
96d37807
VZ
280 if ( m_nSelection == nPage ) {
281 // advance selection backwards - the page being deleted shouldn't be left
282 // selected
283 AdvanceSelection(FALSE);
284 }
285
88310e2e
VZ
286 TabCtrl_DeleteItem(m_hwnd, nPage);
287
288 delete m_aPages[nPage];
289 m_aPages.Remove(nPage);
290
96d37807
VZ
291 if ( m_aPages.IsEmpty() ) {
292 // no selection if the notebook became empty
293 m_nSelection = -1;
294 }
295
88310e2e
VZ
296 return TRUE;
297}
298
621793f4
JS
299// remove one page from the notebook, without deleting
300bool wxNotebook::RemovePage(int nPage)
301{
837e5743 302 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
621793f4
JS
303
304 TabCtrl_DeleteItem(m_hwnd, nPage);
305
306 m_aPages.Remove(nPage);
307
308 return TRUE;
309}
310
88310e2e
VZ
311// remove all pages
312bool wxNotebook::DeleteAllPages()
313{
88310e2e
VZ
314 int nPageCount = GetPageCount();
315 int nPage;
316 for ( nPage = 0; nPage < nPageCount; nPage++ )
317 delete m_aPages[nPage];
318
319 m_aPages.Clear();
320
907f37b3
VZ
321 TabCtrl_DeleteAllItems(m_hwnd);
322
88310e2e
VZ
323 return TRUE;
324}
325
326// add a page to the notebook
327bool wxNotebook::AddPage(wxNotebookPage *pPage,
328 const wxString& strText,
329 bool bSelect,
330 int imageId)
331{
332 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
333}
334
335// same as AddPage() but does it at given position
336bool wxNotebook::InsertPage(int nPage,
337 wxNotebookPage *pPage,
338 const wxString& strText,
339 bool bSelect,
340 int imageId)
341{
342 wxASSERT( pPage != NULL );
343 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
344
43427087
VZ
345 // do add the tab to the control
346
347 // init all fields to 0
88310e2e 348 TC_ITEM tcItem;
43427087 349 memset(&tcItem, 0, sizeof(tcItem));
58a8ab88 350
43427087 351 if ( imageId != -1 )
58a8ab88
JS
352 {
353 tcItem.mask |= TCIF_IMAGE;
354 tcItem.iImage = imageId;
355 }
58a8ab88 356
43427087 357 if ( !strText.IsEmpty() )
58a8ab88 358 {
43427087
VZ
359 tcItem.mask |= TCIF_TEXT;
360 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
58a8ab88 361 }
88310e2e
VZ
362
363 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
837e5743 364 wxLogError(_T("Can't create the notebook page '%s'."), strText.c_str());
43427087 365
88310e2e
VZ
366 return FALSE;
367 }
368
43427087
VZ
369 // if the inserted page is before the selected one, we must update the
370 // index of the selected page
371 if ( nPage <= m_nSelection )
372 {
373 // one extra page added
374 m_nSelection++;
375 }
376
88310e2e
VZ
377 // save the pointer to the page
378 m_aPages.Insert(pPage, nPage);
379
88310e2e 380 // don't show pages by default (we'll need to adjust their size first)
42e69d6b 381 HWND hwnd = GetWinHwnd(pPage);
88310e2e
VZ
382 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
383
42e69d6b
VZ
384 // this updates internal flag too - otherwise it will get out of sync
385 pPage->Show(FALSE);
386
43427087
VZ
387 // some page should be selected: either this one or the first one if there is
388 // still no selection
389 int selNew = -1;
390 if ( bSelect )
391 selNew = nPage;
392 else if ( m_nSelection == -1 )
393 selNew = 0;
394
395 if ( selNew != -1 )
396 SetSelection(selNew);
96d37807 397
88310e2e
VZ
398 return TRUE;
399}
400
401// ----------------------------------------------------------------------------
402// wxNotebook callbacks
403// ----------------------------------------------------------------------------
404
9026ad85 405void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 406{
b5c3b538
VZ
407 // fit the notebook page to the tab control's display area
408 RECT rc;
409 rc.left = rc.top = 0;
410 GetSize((int *)&rc.right, (int *)&rc.bottom);
411
412 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
c86f1403
VZ
413 size_t nCount = m_aPages.Count();
414 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538
VZ
415 wxNotebookPage *pPage = m_aPages[nPage];
416 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
417 if ( pPage->GetAutoLayout() )
418 pPage->Layout();
419 }
420
88310e2e
VZ
421 event.Skip();
422}
423
424void wxNotebook::OnSelChange(wxNotebookEvent& event)
425{
426 // is it our tab control?
427 if ( event.GetEventObject() == this )
5d1d2d46 428 {
5d1d2d46
VZ
429 int sel = event.GetOldSelection();
430 if ( sel != -1 )
431 m_aPages[sel]->Show(FALSE);
432
433 sel = event.GetSelection();
434 if ( sel != -1 )
435 {
436 wxNotebookPage *pPage = m_aPages[sel];
437 pPage->Show(TRUE);
438 pPage->SetFocus();
439 }
440
441 m_nSelection = sel;
442 }
88310e2e
VZ
443
444 // we want to give others a chance to process this message as well
445 event.Skip();
446}
447
448void wxNotebook::OnSetFocus(wxFocusEvent& event)
449{
450 // set focus to the currently selected page if any
451 if ( m_nSelection != -1 )
452 m_aPages[m_nSelection]->SetFocus();
453
454 event.Skip();
455}
456
457void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
458{
459 if ( event.IsWindowChange() ) {
460 // change pages
461 AdvanceSelection(event.GetDirection());
462 }
463 else {
464 // pass to the parent
465 if ( GetParent() ) {
466 event.SetCurrentFocus(this);
02800301 467 GetParent()->GetEventHandler()->ProcessEvent(event);
88310e2e
VZ
468 }
469 }
470}
471
472// ----------------------------------------------------------------------------
473// wxNotebook base class virtuals
474// ----------------------------------------------------------------------------
b5c3b538
VZ
475
476// override these 2 functions to do nothing: everything is done in OnSize
477
478void wxNotebook::SetConstraintSizes(bool /* recurse */)
479{
480 // don't set the sizes of the pages - their correct size is not yet known
481 wxControl::SetConstraintSizes(FALSE);
482}
483
484bool wxNotebook::DoPhase(int /* nPhase */)
485{
486 return TRUE;
487}
488
a23fd0e1 489bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 490{
93a19f17 491 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
492
493 NMHDR* hdr = (NMHDR *)lParam;
494 switch ( hdr->code ) {
495 case TCN_SELCHANGE:
496 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
497 break;
498
499 case TCN_SELCHANGING:
500 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
501 break;
502
fd3f686c 503 default:
a23fd0e1 504 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
505 }
506
93a19f17
VZ
507 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
508 event.SetOldSelection(m_nSelection);
88310e2e 509 event.SetEventObject(this);
a23fd0e1 510 event.SetInt(idCtrl);
88310e2e 511
fd3f686c
VZ
512 bool processed = GetEventHandler()->ProcessEvent(event);
513 *result = !event.IsAllowed();
514 return processed;
88310e2e
VZ
515}
516
517// ----------------------------------------------------------------------------
518// wxNotebook helper functions
519// ----------------------------------------------------------------------------
520
43427087
VZ
521// generate the page changing and changed events, hide the currently active
522// panel and show the new one
88310e2e
VZ
523void wxNotebook::ChangePage(int nOldSel, int nSel)
524{
fd3f686c
VZ
525 // MT-FIXME should use a real semaphore
526 static bool s_bInsideChangePage = FALSE;
527
528 // when we call ProcessEvent(), our own OnSelChange() is called which calls
529 // this function - break the infinite loop
530 if ( s_bInsideChangePage )
531 return;
532
aaab7c01
VZ
533 // it's not an error (the message may be generated by the tab control itself)
534 // and it may happen - just do nothing
535 if ( nSel == nOldSel )
536 return;
88310e2e 537
fd3f686c
VZ
538 s_bInsideChangePage = TRUE;
539
540 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
541 event.SetSelection(nSel);
542 event.SetOldSelection(nOldSel);
543 event.SetEventObject(this);
544 if ( ProcessEvent(event) && !event.IsAllowed() )
545 {
546 // program doesn't allow the page change
547 s_bInsideChangePage = FALSE;
548 return;
549 }
550
fd3f686c
VZ
551 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
552 ProcessEvent(event);
553
fd3f686c 554 s_bInsideChangePage = FALSE;
88310e2e 555}