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