]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
wxButton::GetDefaultSize() fix
[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
345 // add the tab to the control
346 TC_ITEM tcItem;
58a8ab88
JS
347 tcItem.mask = 0;
348
349 if (imageId != -1)
350 {
351 tcItem.mask |= TCIF_IMAGE;
352 tcItem.iImage = imageId;
353 }
354 else
355 tcItem.iImage = 0;
356
357 if (!strText.IsEmpty())
358 {
359 tcItem.mask |= TCIF_TEXT;
837e5743 360 tcItem.pszText = (wxChar *)strText.c_str();
58a8ab88
JS
361 }
362 else
837e5743 363 tcItem.pszText = (wxChar *) NULL;
88310e2e
VZ
364
365 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
837e5743 366 wxLogError(_T("Can't create the notebook page '%s'."), strText.c_str());
88310e2e
VZ
367 return FALSE;
368 }
369
370 // save the pointer to the page
371 m_aPages.Insert(pPage, nPage);
372
907f37b3 373 // some page must be selected: either this one or the first one if there is
88310e2e
VZ
374 // still no selection
375 if ( bSelect )
376 m_nSelection = nPage;
377 else if ( m_nSelection == -1 )
378 m_nSelection = 0;
379
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
96d37807
VZ
387 // FIXME this is ugly, I'm breaking my own rules... but needed to get display
388 // right (why?)
389 wxSizeEvent event;
390 OnSize(event);
391
88310e2e
VZ
392 return TRUE;
393}
394
395// ----------------------------------------------------------------------------
396// wxNotebook callbacks
397// ----------------------------------------------------------------------------
398
9026ad85 399void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 400{
4fa688d8
VZ
401 // make sure the current page is shown and has focus (it's useful because all
402 // pages are created invisible initially)
403 if ( m_nSelection != -1 ) {
404 wxNotebookPage *pPage = m_aPages[m_nSelection];
405 pPage->Show(TRUE);
406 pPage->SetFocus();
407 }
88310e2e 408
b5c3b538
VZ
409 // fit the notebook page to the tab control's display area
410 RECT rc;
411 rc.left = rc.top = 0;
412 GetSize((int *)&rc.right, (int *)&rc.bottom);
413
414 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
c86f1403
VZ
415 size_t nCount = m_aPages.Count();
416 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538
VZ
417 wxNotebookPage *pPage = m_aPages[nPage];
418 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
419 if ( pPage->GetAutoLayout() )
420 pPage->Layout();
421 }
422
88310e2e
VZ
423 event.Skip();
424}
425
426void wxNotebook::OnSelChange(wxNotebookEvent& event)
427{
428 // is it our tab control?
429 if ( event.GetEventObject() == this )
5d1d2d46
VZ
430 {
431 // don't call ChangePage() here because it will generate redundant
432 // notification events
433 int sel = event.GetOldSelection();
434 if ( sel != -1 )
435 m_aPages[sel]->Show(FALSE);
436
437 sel = event.GetSelection();
438 if ( sel != -1 )
439 {
440 wxNotebookPage *pPage = m_aPages[sel];
441 pPage->Show(TRUE);
442 pPage->SetFocus();
443 }
444
445 m_nSelection = sel;
446 }
88310e2e
VZ
447
448 // we want to give others a chance to process this message as well
449 event.Skip();
450}
451
452void wxNotebook::OnSetFocus(wxFocusEvent& event)
453{
454 // set focus to the currently selected page if any
455 if ( m_nSelection != -1 )
456 m_aPages[m_nSelection]->SetFocus();
457
458 event.Skip();
459}
460
461void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
462{
463 if ( event.IsWindowChange() ) {
464 // change pages
465 AdvanceSelection(event.GetDirection());
466 }
467 else {
468 // pass to the parent
469 if ( GetParent() ) {
470 event.SetCurrentFocus(this);
02800301 471 GetParent()->GetEventHandler()->ProcessEvent(event);
88310e2e
VZ
472 }
473 }
474}
475
476// ----------------------------------------------------------------------------
477// wxNotebook base class virtuals
478// ----------------------------------------------------------------------------
b5c3b538
VZ
479
480// override these 2 functions to do nothing: everything is done in OnSize
481
482void wxNotebook::SetConstraintSizes(bool /* recurse */)
483{
484 // don't set the sizes of the pages - their correct size is not yet known
485 wxControl::SetConstraintSizes(FALSE);
486}
487
488bool wxNotebook::DoPhase(int /* nPhase */)
489{
490 return TRUE;
491}
492
a23fd0e1 493bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 494{
93a19f17 495 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
496
497 NMHDR* hdr = (NMHDR *)lParam;
498 switch ( hdr->code ) {
499 case TCN_SELCHANGE:
500 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
501 break;
502
503 case TCN_SELCHANGING:
504 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
505 break;
506
fd3f686c 507 default:
a23fd0e1 508 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
509 }
510
93a19f17
VZ
511 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
512 event.SetOldSelection(m_nSelection);
88310e2e 513 event.SetEventObject(this);
a23fd0e1 514 event.SetInt(idCtrl);
88310e2e 515
fd3f686c
VZ
516 bool processed = GetEventHandler()->ProcessEvent(event);
517 *result = !event.IsAllowed();
518 return processed;
88310e2e
VZ
519}
520
521// ----------------------------------------------------------------------------
522// wxNotebook helper functions
523// ----------------------------------------------------------------------------
524
525// hide the currently active panel and show the new one
526void wxNotebook::ChangePage(int nOldSel, int nSel)
527{
fd3f686c
VZ
528 // MT-FIXME should use a real semaphore
529 static bool s_bInsideChangePage = FALSE;
530
531 // when we call ProcessEvent(), our own OnSelChange() is called which calls
532 // this function - break the infinite loop
533 if ( s_bInsideChangePage )
534 return;
535
aaab7c01
VZ
536 // it's not an error (the message may be generated by the tab control itself)
537 // and it may happen - just do nothing
538 if ( nSel == nOldSel )
539 return;
88310e2e 540
fd3f686c
VZ
541 s_bInsideChangePage = TRUE;
542
543 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
544 event.SetSelection(nSel);
545 event.SetOldSelection(nOldSel);
546 event.SetEventObject(this);
547 if ( ProcessEvent(event) && !event.IsAllowed() )
548 {
549 // program doesn't allow the page change
550 s_bInsideChangePage = FALSE;
551 return;
552 }
553
aaab7c01 554 if ( nOldSel != -1 )
88310e2e 555 m_aPages[nOldSel]->Show(FALSE);
88310e2e
VZ
556
557 wxNotebookPage *pPage = m_aPages[nSel];
88310e2e 558 pPage->Show(TRUE);
b5c3b538 559 pPage->SetFocus();
88310e2e 560
fd3f686c
VZ
561 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
562 ProcessEvent(event);
563
88310e2e 564 m_nSelection = nSel;
fd3f686c 565 s_bInsideChangePage = FALSE;
88310e2e 566}