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