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