]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
Augmented version to b5
[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
126 SetName(name);
127 SetParent(parent);
128
129 m_windowId = id == -1 ? NewControlId() : id;
130
131 // colors and font
132 m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
133 m_foregroundColour = *wxBLACK ;
134
88310e2e 135 // style
fd2daa68 136 m_windowStyle = style | wxTAB_TRAVERSAL;
88310e2e 137
d13c32e9
JS
138 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
139
140 if (m_windowStyle & wxCLIP_CHILDREN)
141 tabStyle |= WS_CLIPCHILDREN;
88310e2e
VZ
142 if ( m_windowStyle & wxTC_MULTILINE )
143 tabStyle |= TCS_MULTILINE;
144 if ( m_windowStyle & wxBORDER )
145 tabStyle &= WS_BORDER;
58a8ab88
JS
146 if (m_windowStyle & wxNB_FIXEDWIDTH)
147 tabStyle |= TCS_FIXEDWIDTH ;
88310e2e
VZ
148
149 // create the tab control.
150 m_hWnd = (WXHWND)CreateWindowEx
151 (
152 0, // extended style
153 WC_TABCONTROL, // class name for the tab control
154 "", // no caption
155 tabStyle, // style
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
161 );
162
163 if ( m_hWnd == 0 ) {
164 wxLogSysError("Can't create the notebook control");
165 return FALSE;
166 }
167
27529614 168 // Not all compilers recognise SetWindowFont
27529614
JS
169 ::SendMessage((HWND) m_hwnd, WM_SETFONT,
170 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT),TRUE);
171
aaab7c01 172
907f37b3 173 if ( parent != NULL )
88310e2e 174 parent->AddChild(this);
907f37b3 175
88310e2e
VZ
176 SubclassWin(m_hWnd);
177
178 return TRUE;
179}
180
181// dtor
182wxNotebook::~wxNotebook()
183{
184}
185
186// ----------------------------------------------------------------------------
187// wxNotebook accessors
188// ----------------------------------------------------------------------------
189int wxNotebook::GetPageCount() const
190{
191 // consistency check
192 wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) );
193
194 return m_aPages.Count();
195}
196
197int wxNotebook::GetRowCount() const
198{
199 return TabCtrl_GetRowCount(m_hwnd);
200}
201
202int wxNotebook::SetSelection(int nPage)
203{
1c4a764c 204 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "notebook page out of range" );
88310e2e
VZ
205
206 ChangePage(m_nSelection, nPage);
207
208 return TabCtrl_SetCurSel(m_hwnd, nPage);
209}
210
211void wxNotebook::AdvanceSelection(bool bForward)
212{
213 int nSel = GetSelection();
214 int nMax = GetPageCount() - 1;
215 if ( bForward )
216 SetSelection(nSel == nMax ? 0 : nSel + 1);
217 else
218 SetSelection(nSel == 0 ? nMax : nSel - 1);
219}
220
221bool wxNotebook::SetPageText(int nPage, const wxString& strText)
222{
1c4a764c 223 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
88310e2e
VZ
224
225 TC_ITEM tcItem;
226 tcItem.mask = TCIF_TEXT;
227 tcItem.pszText = (char *)strText.c_str();
228
229 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
230}
231
232wxString wxNotebook::GetPageText(int nPage) const
233{
1c4a764c 234 wxCHECK_MSG( IS_VALID_PAGE(nPage), "", "notebook page out of range" );
88310e2e
VZ
235
236 char buf[256];
237 TC_ITEM tcItem;
238 tcItem.mask = TCIF_TEXT;
239 tcItem.pszText = buf;
240 tcItem.cchTextMax = WXSIZEOF(buf);
241
242 wxString str;
243 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
244 str = tcItem.pszText;
245
246 return str;
247}
248
249int wxNotebook::GetPageImage(int nPage) const
250{
1c4a764c 251 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "notebook page out of range" );
88310e2e
VZ
252
253 TC_ITEM tcItem;
254 tcItem.mask = TCIF_IMAGE;
255
256 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
257}
258
259bool wxNotebook::SetPageImage(int nPage, int nImage)
260{
1c4a764c 261 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
88310e2e
VZ
262
263 TC_ITEM tcItem;
264 tcItem.mask = TCIF_IMAGE;
265 tcItem.iImage = nImage;
266
267 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
268}
269
270void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 271{
88310e2e
VZ
272 m_pImageList = imageList;
273 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
274}
275
42e69d6b
VZ
276
277// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
278// style.
279void wxNotebook::SetTabSize(const wxSize& sz)
280{
281 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
282}
283
88310e2e
VZ
284// ----------------------------------------------------------------------------
285// wxNotebook operations
286// ----------------------------------------------------------------------------
287
288// remove one page from the notebook
289bool wxNotebook::DeletePage(int nPage)
290{
1c4a764c 291 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
88310e2e
VZ
292
293 TabCtrl_DeleteItem(m_hwnd, nPage);
294
295 delete m_aPages[nPage];
296 m_aPages.Remove(nPage);
297
298 return TRUE;
299}
300
621793f4
JS
301// remove one page from the notebook, without deleting
302bool wxNotebook::RemovePage(int nPage)
303{
304 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
305
306 TabCtrl_DeleteItem(m_hwnd, nPage);
307
308 m_aPages.Remove(nPage);
309
310 return TRUE;
311}
312
88310e2e
VZ
313// remove all pages
314bool wxNotebook::DeleteAllPages()
315{
88310e2e
VZ
316 int nPageCount = GetPageCount();
317 int nPage;
318 for ( nPage = 0; nPage < nPageCount; nPage++ )
319 delete m_aPages[nPage];
320
321 m_aPages.Clear();
322
907f37b3
VZ
323 TabCtrl_DeleteAllItems(m_hwnd);
324
88310e2e
VZ
325 return TRUE;
326}
327
328// add a page to the notebook
329bool wxNotebook::AddPage(wxNotebookPage *pPage,
330 const wxString& strText,
331 bool bSelect,
332 int imageId)
333{
334 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
335}
336
337// same as AddPage() but does it at given position
338bool wxNotebook::InsertPage(int nPage,
339 wxNotebookPage *pPage,
340 const wxString& strText,
341 bool bSelect,
342 int imageId)
343{
344 wxASSERT( pPage != NULL );
345 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
346
347 // add the tab to the control
348 TC_ITEM tcItem;
58a8ab88
JS
349 tcItem.mask = 0;
350
351 if (imageId != -1)
352 {
353 tcItem.mask |= TCIF_IMAGE;
354 tcItem.iImage = imageId;
355 }
356 else
357 tcItem.iImage = 0;
358
359 if (!strText.IsEmpty())
360 {
361 tcItem.mask |= TCIF_TEXT;
362 tcItem.pszText = (char *)strText.c_str();
363 }
364 else
365 tcItem.pszText = (char *) NULL;
88310e2e
VZ
366
367 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
368 wxLogError("Can't create the notebook page '%s'.", strText.c_str());
369 return FALSE;
370 }
371
372 // save the pointer to the page
373 m_aPages.Insert(pPage, nPage);
374
907f37b3 375 // some page must be selected: either this one or the first one if there is
88310e2e
VZ
376 // still no selection
377 if ( bSelect )
378 m_nSelection = nPage;
379 else if ( m_nSelection == -1 )
380 m_nSelection = 0;
381
382 // don't show pages by default (we'll need to adjust their size first)
42e69d6b 383 HWND hwnd = GetWinHwnd(pPage);
88310e2e
VZ
384 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
385
42e69d6b
VZ
386 // this updates internal flag too - otherwise it will get out of sync
387 pPage->Show(FALSE);
388
88310e2e
VZ
389 return TRUE;
390}
391
392// ----------------------------------------------------------------------------
393// wxNotebook callbacks
394// ----------------------------------------------------------------------------
395
42e69d6b 396void wxNotebook::OnWindowCreate(wxWindowCreateEvent& event)
88310e2e 397{
4fa688d8
VZ
398 // make sure the current page is shown and has focus (it's useful because all
399 // pages are created invisible initially)
400 if ( m_nSelection != -1 ) {
401 wxNotebookPage *pPage = m_aPages[m_nSelection];
402 pPage->Show(TRUE);
403 pPage->SetFocus();
404 }
88310e2e 405
b5c3b538
VZ
406 // fit the notebook page to the tab control's display area
407 RECT rc;
408 rc.left = rc.top = 0;
409 GetSize((int *)&rc.right, (int *)&rc.bottom);
410
411 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
c86f1403
VZ
412 size_t nCount = m_aPages.Count();
413 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538
VZ
414 wxNotebookPage *pPage = m_aPages[nPage];
415 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
416 if ( pPage->GetAutoLayout() )
417 pPage->Layout();
418 }
419
88310e2e
VZ
420 event.Skip();
421}
422
423void wxNotebook::OnSelChange(wxNotebookEvent& event)
424{
425 // is it our tab control?
426 if ( event.GetEventObject() == this )
427 ChangePage(event.GetOldSelection(), event.GetSelection());
428
429 // we want to give others a chance to process this message as well
430 event.Skip();
431}
432
433void wxNotebook::OnSetFocus(wxFocusEvent& event)
434{
435 // set focus to the currently selected page if any
436 if ( m_nSelection != -1 )
437 m_aPages[m_nSelection]->SetFocus();
438
439 event.Skip();
440}
441
442void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
443{
444 if ( event.IsWindowChange() ) {
445 // change pages
446 AdvanceSelection(event.GetDirection());
447 }
448 else {
449 // pass to the parent
450 if ( GetParent() ) {
451 event.SetCurrentFocus(this);
02800301 452 GetParent()->GetEventHandler()->ProcessEvent(event);
88310e2e
VZ
453 }
454 }
455}
456
457// ----------------------------------------------------------------------------
458// wxNotebook base class virtuals
459// ----------------------------------------------------------------------------
b5c3b538
VZ
460
461// override these 2 functions to do nothing: everything is done in OnSize
462
463void wxNotebook::SetConstraintSizes(bool /* recurse */)
464{
465 // don't set the sizes of the pages - their correct size is not yet known
466 wxControl::SetConstraintSizes(FALSE);
467}
468
469bool wxNotebook::DoPhase(int /* nPhase */)
470{
471 return TRUE;
472}
473
a23fd0e1 474bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 475{
93a19f17 476 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
477
478 NMHDR* hdr = (NMHDR *)lParam;
479 switch ( hdr->code ) {
480 case TCN_SELCHANGE:
481 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
482 break;
483
484 case TCN_SELCHANGING:
485 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
486 break;
487
fd3f686c 488 default:
a23fd0e1 489 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
490 }
491
93a19f17
VZ
492 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
493 event.SetOldSelection(m_nSelection);
88310e2e 494 event.SetEventObject(this);
a23fd0e1 495 event.SetInt(idCtrl);
88310e2e 496
fd3f686c
VZ
497 bool processed = GetEventHandler()->ProcessEvent(event);
498 *result = !event.IsAllowed();
499 return processed;
88310e2e
VZ
500}
501
502// ----------------------------------------------------------------------------
503// wxNotebook helper functions
504// ----------------------------------------------------------------------------
505
506// hide the currently active panel and show the new one
507void wxNotebook::ChangePage(int nOldSel, int nSel)
508{
fd3f686c
VZ
509 // MT-FIXME should use a real semaphore
510 static bool s_bInsideChangePage = FALSE;
511
512 // when we call ProcessEvent(), our own OnSelChange() is called which calls
513 // this function - break the infinite loop
514 if ( s_bInsideChangePage )
515 return;
516
aaab7c01
VZ
517 // it's not an error (the message may be generated by the tab control itself)
518 // and it may happen - just do nothing
519 if ( nSel == nOldSel )
520 return;
88310e2e 521
fd3f686c
VZ
522 s_bInsideChangePage = TRUE;
523
524 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
525 event.SetSelection(nSel);
526 event.SetOldSelection(nOldSel);
527 event.SetEventObject(this);
528 if ( ProcessEvent(event) && !event.IsAllowed() )
529 {
530 // program doesn't allow the page change
531 s_bInsideChangePage = FALSE;
532 return;
533 }
534
aaab7c01 535 if ( nOldSel != -1 )
88310e2e 536 m_aPages[nOldSel]->Show(FALSE);
88310e2e
VZ
537
538 wxNotebookPage *pPage = m_aPages[nSel];
88310e2e 539 pPage->Show(TRUE);
b5c3b538 540 pPage->SetFocus();
88310e2e 541
fd3f686c
VZ
542 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
543 ProcessEvent(event);
544
88310e2e 545 m_nSelection = nSel;
fd3f686c 546 s_bInsideChangePage = FALSE;
88310e2e 547}