]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
wxCaret MSW bug fixes
[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
5ad998f2
VZ
71 // doesn't work yet EVT_WINDOW_CREATE(wxNotebook::OnWindowCreate)
72 EVT_SIZE(wxNotebook::OnWindowCreate)
42e69d6b 73
88310e2e 74 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
42e69d6b 75
88310e2e
VZ
76 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
77 END_EVENT_TABLE()
78
79 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
92976ab6 80 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
81#endif
82
83// ============================================================================
84// implementation
85// ============================================================================
86
87// ----------------------------------------------------------------------------
88// wxNotebook construction
89// ----------------------------------------------------------------------------
90
91// common part of all ctors
92void wxNotebook::Init()
93{
94 m_pImageList = NULL;
95 m_nSelection = -1;
96}
97
98// default for dynamic class
99wxNotebook::wxNotebook()
100{
101 Init();
102}
103
104// the same arguments as for wxControl
105wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 106 wxWindowID id,
88310e2e
VZ
107 const wxPoint& pos,
108 const wxSize& size,
8b9518ee 109 long style,
88310e2e
VZ
110 const wxString& name)
111{
112 Init();
113
114 Create(parent, id, pos, size, style, name);
115}
116
117// Create() function
118bool wxNotebook::Create(wxWindow *parent,
8b9518ee 119 wxWindowID id,
88310e2e
VZ
120 const wxPoint& pos,
121 const wxSize& size,
8b9518ee 122 long style,
88310e2e
VZ
123 const wxString& name)
124{
125 // base init
d66a042c 126 CreateBase(parent, id, pos, size, style, name);
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{
1c4a764c 189 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "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{
1c4a764c 208 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
88310e2e
VZ
209
210 TC_ITEM tcItem;
211 tcItem.mask = TCIF_TEXT;
212 tcItem.pszText = (char *)strText.c_str();
213
214 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
215}
216
217wxString wxNotebook::GetPageText(int nPage) const
218{
1c4a764c 219 wxCHECK_MSG( IS_VALID_PAGE(nPage), "", "notebook page out of range" );
88310e2e
VZ
220
221 char buf[256];
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{
1c4a764c 236 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "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{
1c4a764c 246 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "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{
1c4a764c 276 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
88310e2e
VZ
277
278 TabCtrl_DeleteItem(m_hwnd, nPage);
279
280 delete m_aPages[nPage];
281 m_aPages.Remove(nPage);
282
283 return TRUE;
284}
285
621793f4
JS
286// remove one page from the notebook, without deleting
287bool wxNotebook::RemovePage(int nPage)
288{
289 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
290
291 TabCtrl_DeleteItem(m_hwnd, nPage);
292
293 m_aPages.Remove(nPage);
294
295 return TRUE;
296}
297
88310e2e
VZ
298// remove all pages
299bool wxNotebook::DeleteAllPages()
300{
88310e2e
VZ
301 int nPageCount = GetPageCount();
302 int nPage;
303 for ( nPage = 0; nPage < nPageCount; nPage++ )
304 delete m_aPages[nPage];
305
306 m_aPages.Clear();
307
907f37b3
VZ
308 TabCtrl_DeleteAllItems(m_hwnd);
309
88310e2e
VZ
310 return TRUE;
311}
312
313// add a page to the notebook
314bool wxNotebook::AddPage(wxNotebookPage *pPage,
315 const wxString& strText,
316 bool bSelect,
317 int imageId)
318{
319 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
320}
321
322// same as AddPage() but does it at given position
323bool wxNotebook::InsertPage(int nPage,
324 wxNotebookPage *pPage,
325 const wxString& strText,
326 bool bSelect,
327 int imageId)
328{
329 wxASSERT( pPage != NULL );
330 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
331
332 // add the tab to the control
333 TC_ITEM tcItem;
58a8ab88
JS
334 tcItem.mask = 0;
335
336 if (imageId != -1)
337 {
338 tcItem.mask |= TCIF_IMAGE;
339 tcItem.iImage = imageId;
340 }
341 else
342 tcItem.iImage = 0;
343
344 if (!strText.IsEmpty())
345 {
346 tcItem.mask |= TCIF_TEXT;
347 tcItem.pszText = (char *)strText.c_str();
348 }
349 else
350 tcItem.pszText = (char *) NULL;
88310e2e
VZ
351
352 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
353 wxLogError("Can't create the notebook page '%s'.", strText.c_str());
354 return FALSE;
355 }
356
357 // save the pointer to the page
358 m_aPages.Insert(pPage, nPage);
359
907f37b3 360 // some page must be selected: either this one or the first one if there is
88310e2e
VZ
361 // still no selection
362 if ( bSelect )
363 m_nSelection = nPage;
364 else if ( m_nSelection == -1 )
365 m_nSelection = 0;
366
367 // don't show pages by default (we'll need to adjust their size first)
42e69d6b 368 HWND hwnd = GetWinHwnd(pPage);
88310e2e
VZ
369 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
370
42e69d6b
VZ
371 // this updates internal flag too - otherwise it will get out of sync
372 pPage->Show(FALSE);
373
88310e2e
VZ
374 return TRUE;
375}
376
377// ----------------------------------------------------------------------------
378// wxNotebook callbacks
379// ----------------------------------------------------------------------------
380
42e69d6b 381void wxNotebook::OnWindowCreate(wxWindowCreateEvent& event)
88310e2e 382{
4fa688d8
VZ
383 // make sure the current page is shown and has focus (it's useful because all
384 // pages are created invisible initially)
385 if ( m_nSelection != -1 ) {
386 wxNotebookPage *pPage = m_aPages[m_nSelection];
387 pPage->Show(TRUE);
388 pPage->SetFocus();
389 }
88310e2e 390
b5c3b538
VZ
391 // fit the notebook page to the tab control's display area
392 RECT rc;
393 rc.left = rc.top = 0;
394 GetSize((int *)&rc.right, (int *)&rc.bottom);
395
396 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
c86f1403
VZ
397 size_t nCount = m_aPages.Count();
398 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538
VZ
399 wxNotebookPage *pPage = m_aPages[nPage];
400 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
401 if ( pPage->GetAutoLayout() )
402 pPage->Layout();
403 }
404
88310e2e
VZ
405 event.Skip();
406}
407
408void wxNotebook::OnSelChange(wxNotebookEvent& event)
409{
410 // is it our tab control?
411 if ( event.GetEventObject() == this )
412 ChangePage(event.GetOldSelection(), event.GetSelection());
413
414 // we want to give others a chance to process this message as well
415 event.Skip();
416}
417
418void wxNotebook::OnSetFocus(wxFocusEvent& event)
419{
420 // set focus to the currently selected page if any
421 if ( m_nSelection != -1 )
422 m_aPages[m_nSelection]->SetFocus();
423
424 event.Skip();
425}
426
427void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
428{
429 if ( event.IsWindowChange() ) {
430 // change pages
431 AdvanceSelection(event.GetDirection());
432 }
433 else {
434 // pass to the parent
435 if ( GetParent() ) {
436 event.SetCurrentFocus(this);
02800301 437 GetParent()->GetEventHandler()->ProcessEvent(event);
88310e2e
VZ
438 }
439 }
440}
441
442// ----------------------------------------------------------------------------
443// wxNotebook base class virtuals
444// ----------------------------------------------------------------------------
b5c3b538
VZ
445
446// override these 2 functions to do nothing: everything is done in OnSize
447
448void wxNotebook::SetConstraintSizes(bool /* recurse */)
449{
450 // don't set the sizes of the pages - their correct size is not yet known
451 wxControl::SetConstraintSizes(FALSE);
452}
453
454bool wxNotebook::DoPhase(int /* nPhase */)
455{
456 return TRUE;
457}
458
a23fd0e1 459bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 460{
93a19f17 461 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
462
463 NMHDR* hdr = (NMHDR *)lParam;
464 switch ( hdr->code ) {
465 case TCN_SELCHANGE:
466 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
467 break;
468
469 case TCN_SELCHANGING:
470 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
471 break;
472
fd3f686c 473 default:
a23fd0e1 474 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
475 }
476
93a19f17
VZ
477 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
478 event.SetOldSelection(m_nSelection);
88310e2e 479 event.SetEventObject(this);
a23fd0e1 480 event.SetInt(idCtrl);
88310e2e 481
fd3f686c
VZ
482 bool processed = GetEventHandler()->ProcessEvent(event);
483 *result = !event.IsAllowed();
484 return processed;
88310e2e
VZ
485}
486
487// ----------------------------------------------------------------------------
488// wxNotebook helper functions
489// ----------------------------------------------------------------------------
490
491// hide the currently active panel and show the new one
492void wxNotebook::ChangePage(int nOldSel, int nSel)
493{
fd3f686c
VZ
494 // MT-FIXME should use a real semaphore
495 static bool s_bInsideChangePage = FALSE;
496
497 // when we call ProcessEvent(), our own OnSelChange() is called which calls
498 // this function - break the infinite loop
499 if ( s_bInsideChangePage )
500 return;
501
aaab7c01
VZ
502 // it's not an error (the message may be generated by the tab control itself)
503 // and it may happen - just do nothing
504 if ( nSel == nOldSel )
505 return;
88310e2e 506
fd3f686c
VZ
507 s_bInsideChangePage = TRUE;
508
509 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
510 event.SetSelection(nSel);
511 event.SetOldSelection(nOldSel);
512 event.SetEventObject(this);
513 if ( ProcessEvent(event) && !event.IsAllowed() )
514 {
515 // program doesn't allow the page change
516 s_bInsideChangePage = FALSE;
517 return;
518 }
519
aaab7c01 520 if ( nOldSel != -1 )
88310e2e 521 m_aPages[nOldSel]->Show(FALSE);
88310e2e
VZ
522
523 wxNotebookPage *pPage = m_aPages[nSel];
88310e2e 524 pPage->Show(TRUE);
b5c3b538 525 pPage->SetFocus();
88310e2e 526
fd3f686c
VZ
527 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
528 ProcessEvent(event);
529
88310e2e 530 m_nSelection = nSel;
fd3f686c 531 s_bInsideChangePage = FALSE;
88310e2e 532}