]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
Misc small mods
[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
3096bd2f 25 #include "wx/string.h"
88310e2e
VZ
26#endif // WX_PRECOMP
27
3096bd2f
VZ
28#include "wx/log.h"
29#include "wx/imaglist.h"
30#include "wx/event.h"
31#include "wx/control.h"
32#include "wx/notebook.h"
88310e2e 33
3096bd2f 34#include "wx/msw/private.h"
88310e2e
VZ
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
74b31181
VZ
65// ----------------------------------------------------------------------------
66// constants
67// ----------------------------------------------------------------------------
68
69// This is a work-around for missing defines in gcc-2.95 headers
70#ifndef TCS_RIGHT
71 #define TCS_RIGHT 0x0002
72#endif
73
74#ifndef TCS_VERTICAL
75 #define TCS_VERTICAL 0x0080
76#endif
77
78#ifndef TCS_BOTTOM
79 #define TCS_BOTTOM TCS_RIGHT
80#endif
81
88310e2e
VZ
82// ----------------------------------------------------------------------------
83// event table
84// ----------------------------------------------------------------------------
85
88310e2e
VZ
86 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
87 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
88
9026ad85 89 EVT_SIZE(wxNotebook::OnSize)
42e69d6b 90
88310e2e 91 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
42e69d6b 92
88310e2e
VZ
93 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
94 END_EVENT_TABLE()
95
96 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
92976ab6 97 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
88310e2e
VZ
98
99// ============================================================================
100// implementation
101// ============================================================================
102
103// ----------------------------------------------------------------------------
104// wxNotebook construction
105// ----------------------------------------------------------------------------
106
107// common part of all ctors
108void wxNotebook::Init()
109{
110 m_pImageList = NULL;
111 m_nSelection = -1;
112}
113
114// default for dynamic class
115wxNotebook::wxNotebook()
116{
117 Init();
118}
119
120// the same arguments as for wxControl
121wxNotebook::wxNotebook(wxWindow *parent,
8b9518ee 122 wxWindowID id,
88310e2e
VZ
123 const wxPoint& pos,
124 const wxSize& size,
8b9518ee 125 long style,
88310e2e
VZ
126 const wxString& name)
127{
128 Init();
129
130 Create(parent, id, pos, size, style, name);
131}
132
133// Create() function
134bool wxNotebook::Create(wxWindow *parent,
8b9518ee 135 wxWindowID id,
88310e2e
VZ
136 const wxPoint& pos,
137 const wxSize& size,
8b9518ee 138 long style,
88310e2e
VZ
139 const wxString& name)
140{
141 // base init
8d99be5f
VZ
142 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
143 return FALSE;
88310e2e
VZ
144
145 // colors and font
146 m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
147 m_foregroundColour = *wxBLACK ;
148
88310e2e 149 // style
fd2daa68 150 m_windowStyle = style | wxTAB_TRAVERSAL;
88310e2e 151
d13c32e9
JS
152 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
153
154 if (m_windowStyle & wxCLIP_CHILDREN)
155 tabStyle |= WS_CLIPCHILDREN;
88310e2e
VZ
156 if ( m_windowStyle & wxTC_MULTILINE )
157 tabStyle |= TCS_MULTILINE;
158 if ( m_windowStyle & wxBORDER )
159 tabStyle &= WS_BORDER;
58a8ab88
JS
160 if (m_windowStyle & wxNB_FIXEDWIDTH)
161 tabStyle |= TCS_FIXEDWIDTH ;
7a3ac804
RS
162 if (m_windowStyle & wxNB_BOTTOM)
163 tabStyle |= TCS_RIGHT;
164 if (m_windowStyle & wxNB_LEFT)
165 tabStyle |= TCS_VERTICAL;
166 if (m_windowStyle & wxNB_RIGHT)
167 tabStyle |= TCS_VERTICAL|TCS_RIGHT;
0398b1d6 168
88310e2e 169
789295bf
VZ
170 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
171 this, NULL, pos.x, pos.y, size.x, size.y,
172 tabStyle, NULL, 0) )
173 {
88310e2e
VZ
174 return FALSE;
175 }
176
27529614 177 // Not all compilers recognise SetWindowFont
789295bf
VZ
178 ::SendMessage(GetHwnd(), WM_SETFONT,
179 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
27529614 180
aaab7c01 181
907f37b3 182 if ( parent != NULL )
88310e2e 183 parent->AddChild(this);
907f37b3 184
88310e2e
VZ
185 SubclassWin(m_hWnd);
186
187 return TRUE;
188}
189
190// dtor
191wxNotebook::~wxNotebook()
192{
193}
194
195// ----------------------------------------------------------------------------
196// wxNotebook accessors
197// ----------------------------------------------------------------------------
198int wxNotebook::GetPageCount() const
199{
200 // consistency check
201 wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) );
202
203 return m_aPages.Count();
204}
205
206int wxNotebook::GetRowCount() const
207{
208 return TabCtrl_GetRowCount(m_hwnd);
209}
210
211int wxNotebook::SetSelection(int nPage)
212{
223d09f6 213 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e
VZ
214
215 ChangePage(m_nSelection, nPage);
216
217 return TabCtrl_SetCurSel(m_hwnd, nPage);
218}
219
220void wxNotebook::AdvanceSelection(bool bForward)
221{
222 int nSel = GetSelection();
223 int nMax = GetPageCount() - 1;
224 if ( bForward )
225 SetSelection(nSel == nMax ? 0 : nSel + 1);
226 else
227 SetSelection(nSel == 0 ? nMax : nSel - 1);
228}
229
230bool wxNotebook::SetPageText(int nPage, const wxString& strText)
231{
223d09f6 232 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e
VZ
233
234 TC_ITEM tcItem;
235 tcItem.mask = TCIF_TEXT;
837e5743 236 tcItem.pszText = (wxChar *)strText.c_str();
88310e2e
VZ
237
238 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
239}
240
241wxString wxNotebook::GetPageText(int nPage) const
242{
223d09f6 243 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxT(""), wxT("notebook page out of range") );
88310e2e 244
837e5743 245 wxChar buf[256];
88310e2e
VZ
246 TC_ITEM tcItem;
247 tcItem.mask = TCIF_TEXT;
248 tcItem.pszText = buf;
249 tcItem.cchTextMax = WXSIZEOF(buf);
250
251 wxString str;
252 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
253 str = tcItem.pszText;
254
255 return str;
256}
257
258int wxNotebook::GetPageImage(int nPage) const
259{
223d09f6 260 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
88310e2e
VZ
261
262 TC_ITEM tcItem;
263 tcItem.mask = TCIF_IMAGE;
264
265 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
266}
267
268bool wxNotebook::SetPageImage(int nPage, int nImage)
269{
223d09f6 270 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e
VZ
271
272 TC_ITEM tcItem;
273 tcItem.mask = TCIF_IMAGE;
274 tcItem.iImage = nImage;
275
276 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
277}
278
279void wxNotebook::SetImageList(wxImageList* imageList)
907f37b3 280{
88310e2e
VZ
281 m_pImageList = imageList;
282 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
283}
284
42e69d6b
VZ
285
286// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
287// style.
288void wxNotebook::SetTabSize(const wxSize& sz)
289{
290 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
291}
292
88310e2e
VZ
293// ----------------------------------------------------------------------------
294// wxNotebook operations
295// ----------------------------------------------------------------------------
296
297// remove one page from the notebook
298bool wxNotebook::DeletePage(int nPage)
299{
223d09f6 300 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
88310e2e 301
96d37807
VZ
302 if ( m_nSelection == nPage ) {
303 // advance selection backwards - the page being deleted shouldn't be left
304 // selected
305 AdvanceSelection(FALSE);
306 }
307
88310e2e
VZ
308 TabCtrl_DeleteItem(m_hwnd, nPage);
309
310 delete m_aPages[nPage];
311 m_aPages.Remove(nPage);
312
96d37807
VZ
313 if ( m_aPages.IsEmpty() ) {
314 // no selection if the notebook became empty
315 m_nSelection = -1;
316 }
317
88310e2e
VZ
318 return TRUE;
319}
320
621793f4
JS
321// remove one page from the notebook, without deleting
322bool wxNotebook::RemovePage(int nPage)
323{
223d09f6 324 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
621793f4
JS
325
326 TabCtrl_DeleteItem(m_hwnd, nPage);
327
328 m_aPages.Remove(nPage);
329
47f12f58
JS
330 if ( m_aPages.IsEmpty() )
331 m_nSelection = -1;
332
621793f4
JS
333 return TRUE;
334}
335
88310e2e
VZ
336// remove all pages
337bool wxNotebook::DeleteAllPages()
338{
88310e2e
VZ
339 int nPageCount = GetPageCount();
340 int nPage;
341 for ( nPage = 0; nPage < nPageCount; nPage++ )
342 delete m_aPages[nPage];
343
344 m_aPages.Clear();
345
907f37b3
VZ
346 TabCtrl_DeleteAllItems(m_hwnd);
347
47f12f58
JS
348 m_nSelection = -1;
349
88310e2e
VZ
350 return TRUE;
351}
352
353// add a page to the notebook
354bool wxNotebook::AddPage(wxNotebookPage *pPage,
355 const wxString& strText,
356 bool bSelect,
357 int imageId)
358{
359 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
360}
361
362// same as AddPage() but does it at given position
363bool wxNotebook::InsertPage(int nPage,
364 wxNotebookPage *pPage,
365 const wxString& strText,
366 bool bSelect,
367 int imageId)
368{
369 wxASSERT( pPage != NULL );
370 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
371
43427087
VZ
372 // do add the tab to the control
373
374 // init all fields to 0
88310e2e 375 TC_ITEM tcItem;
43427087 376 memset(&tcItem, 0, sizeof(tcItem));
58a8ab88 377
43427087 378 if ( imageId != -1 )
58a8ab88
JS
379 {
380 tcItem.mask |= TCIF_IMAGE;
381 tcItem.iImage = imageId;
382 }
58a8ab88 383
43427087 384 if ( !strText.IsEmpty() )
58a8ab88 385 {
43427087
VZ
386 tcItem.mask |= TCIF_TEXT;
387 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
58a8ab88 388 }
88310e2e
VZ
389
390 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
223d09f6 391 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
43427087 392
88310e2e
VZ
393 return FALSE;
394 }
395
43427087
VZ
396 // if the inserted page is before the selected one, we must update the
397 // index of the selected page
398 if ( nPage <= m_nSelection )
399 {
400 // one extra page added
401 m_nSelection++;
402 }
403
88310e2e
VZ
404 // save the pointer to the page
405 m_aPages.Insert(pPage, nPage);
406
88310e2e 407 // don't show pages by default (we'll need to adjust their size first)
42e69d6b 408 HWND hwnd = GetWinHwnd(pPage);
88310e2e
VZ
409 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
410
42e69d6b
VZ
411 // this updates internal flag too - otherwise it will get out of sync
412 pPage->Show(FALSE);
413
0398b1d6
RD
414 // fit the notebook page to the tab control's display area
415 RECT rc;
416 rc.left = rc.top = 0;
417 GetSize((int *)&rc.right, (int *)&rc.bottom);
418 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
419 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
0398b1d6
RD
420
421
43427087
VZ
422 // some page should be selected: either this one or the first one if there is
423 // still no selection
424 int selNew = -1;
425 if ( bSelect )
426 selNew = nPage;
427 else if ( m_nSelection == -1 )
428 selNew = 0;
429
430 if ( selNew != -1 )
431 SetSelection(selNew);
96d37807 432
88310e2e
VZ
433 return TRUE;
434}
435
436// ----------------------------------------------------------------------------
437// wxNotebook callbacks
438// ----------------------------------------------------------------------------
439
9026ad85 440void wxNotebook::OnSize(wxSizeEvent& event)
88310e2e 441{
b5c3b538
VZ
442 // fit the notebook page to the tab control's display area
443 RECT rc;
444 rc.left = rc.top = 0;
445 GetSize((int *)&rc.right, (int *)&rc.bottom);
446
447 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
c86f1403
VZ
448 size_t nCount = m_aPages.Count();
449 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538
VZ
450 wxNotebookPage *pPage = m_aPages[nPage];
451 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
b5c3b538
VZ
452 }
453
88310e2e
VZ
454 event.Skip();
455}
456
457void wxNotebook::OnSelChange(wxNotebookEvent& event)
458{
459 // is it our tab control?
460 if ( event.GetEventObject() == this )
5d1d2d46 461 {
5d1d2d46
VZ
462 int sel = event.GetOldSelection();
463 if ( sel != -1 )
464 m_aPages[sel]->Show(FALSE);
0398b1d6 465
5d1d2d46
VZ
466 sel = event.GetSelection();
467 if ( sel != -1 )
468 {
469 wxNotebookPage *pPage = m_aPages[sel];
470 pPage->Show(TRUE);
471 pPage->SetFocus();
472 }
0398b1d6 473
5d1d2d46
VZ
474 m_nSelection = sel;
475 }
88310e2e
VZ
476
477 // we want to give others a chance to process this message as well
478 event.Skip();
479}
480
481void wxNotebook::OnSetFocus(wxFocusEvent& event)
482{
483 // set focus to the currently selected page if any
484 if ( m_nSelection != -1 )
485 m_aPages[m_nSelection]->SetFocus();
486
487 event.Skip();
488}
489
490void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
491{
492 if ( event.IsWindowChange() ) {
493 // change pages
494 AdvanceSelection(event.GetDirection());
495 }
496 else {
497 // pass to the parent
498 if ( GetParent() ) {
499 event.SetCurrentFocus(this);
02800301 500 GetParent()->GetEventHandler()->ProcessEvent(event);
88310e2e
VZ
501 }
502 }
503}
504
505// ----------------------------------------------------------------------------
506// wxNotebook base class virtuals
507// ----------------------------------------------------------------------------
b5c3b538
VZ
508
509// override these 2 functions to do nothing: everything is done in OnSize
510
511void wxNotebook::SetConstraintSizes(bool /* recurse */)
512{
513 // don't set the sizes of the pages - their correct size is not yet known
514 wxControl::SetConstraintSizes(FALSE);
515}
516
517bool wxNotebook::DoPhase(int /* nPhase */)
518{
519 return TRUE;
520}
521
a23fd0e1 522bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 523{
93a19f17 524 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
525
526 NMHDR* hdr = (NMHDR *)lParam;
527 switch ( hdr->code ) {
528 case TCN_SELCHANGE:
529 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
530 break;
531
532 case TCN_SELCHANGING:
533 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
534 break;
535
fd3f686c 536 default:
a23fd0e1 537 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
538 }
539
93a19f17
VZ
540 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
541 event.SetOldSelection(m_nSelection);
88310e2e 542 event.SetEventObject(this);
a23fd0e1 543 event.SetInt(idCtrl);
88310e2e 544
fd3f686c
VZ
545 bool processed = GetEventHandler()->ProcessEvent(event);
546 *result = !event.IsAllowed();
547 return processed;
88310e2e
VZ
548}
549
550// ----------------------------------------------------------------------------
551// wxNotebook helper functions
552// ----------------------------------------------------------------------------
553
43427087
VZ
554// generate the page changing and changed events, hide the currently active
555// panel and show the new one
88310e2e
VZ
556void wxNotebook::ChangePage(int nOldSel, int nSel)
557{
fd3f686c
VZ
558 // MT-FIXME should use a real semaphore
559 static bool s_bInsideChangePage = FALSE;
560
561 // when we call ProcessEvent(), our own OnSelChange() is called which calls
562 // this function - break the infinite loop
563 if ( s_bInsideChangePage )
564 return;
565
aaab7c01
VZ
566 // it's not an error (the message may be generated by the tab control itself)
567 // and it may happen - just do nothing
568 if ( nSel == nOldSel )
569 return;
88310e2e 570
fd3f686c
VZ
571 s_bInsideChangePage = TRUE;
572
573 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
574 event.SetSelection(nSel);
575 event.SetOldSelection(nOldSel);
576 event.SetEventObject(this);
577 if ( ProcessEvent(event) && !event.IsAllowed() )
578 {
579 // program doesn't allow the page change
580 s_bInsideChangePage = FALSE;
581 return;
582 }
583
fd3f686c
VZ
584 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
585 ProcessEvent(event);
586
fd3f686c 587 s_bInsideChangePage = FALSE;
88310e2e 588}