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