]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/notebook.cpp
added the missing (newly added) colours to the switch
[wxWidgets.git] / src / msw / notebook.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/notebook.cpp
3// Purpose: implementation of wxNotebook
4// Author: Vadim Zeitlin
5// Modified by:
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
12#ifdef __GNUG__
13#pragma implementation "notebook.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#if wxUSE_NOTEBOOK
24
25// wxWindows
26#ifndef WX_PRECOMP
27 #include "wx/string.h"
28#endif // WX_PRECOMP
29
30#include "wx/log.h"
31#include "wx/imaglist.h"
32#include "wx/event.h"
33#include "wx/control.h"
34#include "wx/notebook.h"
35
36#include "wx/msw/private.h"
37
38// Windows standard headers
39#ifndef __WIN95__
40 #error "wxNotebook is only supported Windows 95 and above"
41#endif //Win95
42
43#include <windowsx.h> // for SetWindowFont
44
45#ifndef __TWIN32__
46 #ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
48 #endif
49#endif
50
51#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
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
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
82// ----------------------------------------------------------------------------
83// event table
84// ----------------------------------------------------------------------------
85
86DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
87DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
88
89BEGIN_EVENT_TABLE(wxNotebook, wxControl)
90 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
91
92 EVT_SIZE(wxNotebook::OnSize)
93
94 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
95
96 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
97END_EVENT_TABLE()
98
99IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
100IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
101
102// ============================================================================
103// implementation
104// ============================================================================
105
106// ----------------------------------------------------------------------------
107// wxNotebook construction
108// ----------------------------------------------------------------------------
109
110// common part of all ctors
111void wxNotebook::Init()
112{
113 m_imageList = NULL;
114 m_nSelection = -1;
115}
116
117// default for dynamic class
118wxNotebook::wxNotebook()
119{
120 Init();
121}
122
123// the same arguments as for wxControl
124wxNotebook::wxNotebook(wxWindow *parent,
125 wxWindowID id,
126 const wxPoint& pos,
127 const wxSize& size,
128 long style,
129 const wxString& name)
130{
131 Init();
132
133 Create(parent, id, pos, size, style, name);
134}
135
136// Create() function
137bool wxNotebook::Create(wxWindow *parent,
138 wxWindowID id,
139 const wxPoint& pos,
140 const wxSize& size,
141 long style,
142 const wxString& name)
143{
144 // base init
145 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
146 return FALSE;
147
148 parent->AddChild(this);
149
150 // style
151 m_windowStyle = style | wxTAB_TRAVERSAL;
152
153 long tabStyle = WS_TABSTOP | TCS_TABS;
154
155 if ( m_windowStyle & wxBORDER )
156 tabStyle |= WS_BORDER;
157 if ( m_windowStyle & wxCLIP_SIBLINGS )
158 tabStyle |= WS_CLIPSIBLINGS;
159 if (m_windowStyle & wxCLIP_CHILDREN)
160 tabStyle |= WS_CLIPCHILDREN;
161
162 if ( m_windowStyle & wxTC_MULTILINE )
163 tabStyle |= TCS_MULTILINE;
164 if (m_windowStyle & wxNB_FIXEDWIDTH)
165 tabStyle |= TCS_FIXEDWIDTH ;
166 if (m_windowStyle & wxNB_BOTTOM)
167 tabStyle |= TCS_RIGHT;
168 if (m_windowStyle & wxNB_LEFT)
169 tabStyle |= TCS_VERTICAL;
170 if (m_windowStyle & wxNB_RIGHT)
171 tabStyle |= TCS_VERTICAL|TCS_RIGHT;
172
173 // note that we don't want to have the default WS_EX_CLIENTEDGE style for the
174 // notebook, so explicitly specify 0 as last parameter
175 if ( !MSWCreateControl(WC_TABCONTROL, tabStyle, pos, size, _T(""), 0) )
176 {
177 return FALSE;
178 }
179
180 SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE)));
181
182 return TRUE;
183}
184
185// ----------------------------------------------------------------------------
186// wxNotebook accessors
187// ----------------------------------------------------------------------------
188
189int wxNotebook::GetPageCount() const
190{
191 // consistency check
192 wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) );
193
194 return m_pages.Count();
195}
196
197int wxNotebook::GetRowCount() const
198{
199 return TabCtrl_GetRowCount(m_hwnd);
200}
201
202int wxNotebook::SetSelection(int nPage)
203{
204 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
205
206 ChangePage(m_nSelection, nPage);
207
208 return TabCtrl_SetCurSel(m_hwnd, nPage);
209}
210
211bool wxNotebook::SetPageText(int nPage, const wxString& strText)
212{
213 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
214
215 TC_ITEM tcItem;
216 tcItem.mask = TCIF_TEXT;
217 tcItem.pszText = (wxChar *)strText.c_str();
218
219 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
220}
221
222wxString wxNotebook::GetPageText(int nPage) const
223{
224 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxT(""), wxT("notebook page out of range") );
225
226 wxChar buf[256];
227 TC_ITEM tcItem;
228 tcItem.mask = TCIF_TEXT;
229 tcItem.pszText = buf;
230 tcItem.cchTextMax = WXSIZEOF(buf);
231
232 wxString str;
233 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
234 str = tcItem.pszText;
235
236 return str;
237}
238
239int wxNotebook::GetPageImage(int nPage) const
240{
241 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
242
243 TC_ITEM tcItem;
244 tcItem.mask = TCIF_IMAGE;
245
246 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
247}
248
249bool wxNotebook::SetPageImage(int nPage, int nImage)
250{
251 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
252
253 TC_ITEM tcItem;
254 tcItem.mask = TCIF_IMAGE;
255 tcItem.iImage = nImage;
256
257 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
258}
259
260void wxNotebook::SetImageList(wxImageList* imageList)
261{
262 wxNotebookBase::SetImageList(imageList);
263
264 if ( imageList )
265 {
266 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
267 }
268}
269
270// ----------------------------------------------------------------------------
271// wxNotebook size settings
272// ----------------------------------------------------------------------------
273
274void wxNotebook::SetPageSize(const wxSize& size)
275{
276 // transform the page size into the notebook size
277 RECT rc;
278 rc.left =
279 rc.top = 0;
280 rc.right = size.x;
281 rc.bottom = size.y;
282
283 TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc);
284
285 // and now set it
286 SetSize(rc.right - rc.left, rc.bottom - rc.top);
287}
288
289void wxNotebook::SetPadding(const wxSize& padding)
290{
291 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
292}
293
294// Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
295// style.
296void wxNotebook::SetTabSize(const wxSize& sz)
297{
298 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
299}
300
301// ----------------------------------------------------------------------------
302// wxNotebook operations
303// ----------------------------------------------------------------------------
304
305// remove one page from the notebook
306bool wxNotebook::DeletePage(int nPage)
307{
308 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
309
310 if ( m_nSelection == nPage ) {
311 // advance selection backwards - the page being deleted shouldn't be left
312 // selected
313 AdvanceSelection(FALSE);
314 }
315
316 TabCtrl_DeleteItem(m_hwnd, nPage);
317
318 delete m_pages[nPage];
319 m_pages.RemoveAt(nPage);
320
321 if ( m_pages.IsEmpty() ) {
322 // no selection if the notebook became empty
323 m_nSelection = -1;
324 }
325 else
326 m_nSelection = TabCtrl_GetCurSel(m_hwnd);
327
328
329 return TRUE;
330}
331
332// remove one page from the notebook, without deleting
333wxNotebookPage *wxNotebook::DoRemovePage(int nPage)
334{
335 wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage);
336 if ( !pageRemoved )
337 return NULL;
338
339 TabCtrl_DeleteItem(m_hwnd, nPage);
340
341 if ( m_pages.IsEmpty() )
342 m_nSelection = -1;
343 else
344 m_nSelection = TabCtrl_GetCurSel(m_hwnd);
345
346 return pageRemoved;
347}
348
349// remove all pages
350bool wxNotebook::DeleteAllPages()
351{
352 int nPageCount = GetPageCount();
353 int nPage;
354 for ( nPage = 0; nPage < nPageCount; nPage++ )
355 delete m_pages[nPage];
356
357 m_pages.Clear();
358
359 TabCtrl_DeleteAllItems(m_hwnd);
360
361 m_nSelection = -1;
362
363 return TRUE;
364}
365
366// add a page to the notebook
367bool wxNotebook::AddPage(wxNotebookPage *pPage,
368 const wxString& strText,
369 bool bSelect,
370 int imageId)
371{
372 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
373}
374
375// same as AddPage() but does it at given position
376bool wxNotebook::InsertPage(int nPage,
377 wxNotebookPage *pPage,
378 const wxString& strText,
379 bool bSelect,
380 int imageId)
381{
382 wxASSERT( pPage != NULL );
383 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
384
385 // do add the tab to the control
386
387 // init all fields to 0
388 TC_ITEM tcItem;
389 memset(&tcItem, 0, sizeof(tcItem));
390
391 if ( imageId != -1 )
392 {
393 tcItem.mask |= TCIF_IMAGE;
394 tcItem.iImage = imageId;
395 }
396
397 if ( !strText.IsEmpty() )
398 {
399 tcItem.mask |= TCIF_TEXT;
400 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
401 }
402
403 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
404 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
405
406 return FALSE;
407 }
408
409 // if the inserted page is before the selected one, we must update the
410 // index of the selected page
411 if ( nPage <= m_nSelection )
412 {
413 // one extra page added
414 m_nSelection++;
415 }
416
417 // save the pointer to the page
418 m_pages.Insert(pPage, nPage);
419
420 // don't show pages by default (we'll need to adjust their size first)
421 HWND hwnd = GetWinHwnd(pPage);
422 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
423
424 // this updates internal flag too - otherwise it will get out of sync
425 pPage->Show(FALSE);
426
427 // fit the notebook page to the tab control's display area
428 RECT rc;
429 rc.left = rc.top = 0;
430 GetSize((int *)&rc.right, (int *)&rc.bottom);
431 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
432 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
433
434 // some page should be selected: either this one or the first one if there is
435 // still no selection
436 int selNew = -1;
437 if ( bSelect )
438 selNew = nPage;
439 else if ( m_nSelection == -1 )
440 selNew = 0;
441
442 if ( selNew != -1 )
443 SetSelection(selNew);
444
445 return TRUE;
446}
447
448// ----------------------------------------------------------------------------
449// wxNotebook callbacks
450// ----------------------------------------------------------------------------
451
452void wxNotebook::OnSize(wxSizeEvent& event)
453{
454 // fit the notebook page to the tab control's display area
455 RECT rc;
456 rc.left = rc.top = 0;
457 GetSize((int *)&rc.right, (int *)&rc.bottom);
458
459 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
460
461 int width = rc.right - rc.left,
462 height = rc.bottom - rc.top;
463 size_t nCount = m_pages.Count();
464 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
465 wxNotebookPage *pPage = m_pages[nPage];
466 pPage->SetSize(rc.left, rc.top, width, height);
467 }
468
469 event.Skip();
470}
471
472void wxNotebook::OnSelChange(wxNotebookEvent& event)
473{
474 // is it our tab control?
475 if ( event.GetEventObject() == this )
476 {
477 int sel = event.GetOldSelection();
478 if ( sel != -1 )
479 m_pages[sel]->Show(FALSE);
480
481 sel = event.GetSelection();
482 if ( sel != -1 )
483 {
484 wxNotebookPage *pPage = m_pages[sel];
485 pPage->Show(TRUE);
486 pPage->SetFocus();
487 }
488
489 m_nSelection = sel;
490 }
491
492 // we want to give others a chance to process this message as well
493 event.Skip();
494}
495
496void wxNotebook::OnSetFocus(wxFocusEvent& event)
497{
498 // this function is only called when the focus is explicitly set (i.e. from
499 // the program) to the notebook - in this case we don't need the
500 // complicated OnNavigationKey() logic because the programmer knows better
501 // what [s]he wants
502
503 // set focus to the currently selected page if any
504 if ( m_nSelection != -1 )
505 m_pages[m_nSelection]->SetFocus();
506
507 event.Skip();
508}
509
510void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
511{
512 if ( event.IsWindowChange() ) {
513 // change pages
514 AdvanceSelection(event.GetDirection());
515 }
516 else {
517 // we get this event in 2 cases
518 //
519 // a) one of our pages might have generated it because the user TABbed
520 // out from it in which case we should propagate the event upwards and
521 // our parent will take care of setting the focus to prev/next sibling
522 //
523 // or
524 //
525 // b) the parent panel wants to give the focus to us so that we
526 // forward it to our selected page. We can't deal with this in
527 // OnSetFocus() because we don't know which direction the focus came
528 // from in this case and so can't choose between setting the focus to
529 // first or last panel child
530
531 wxWindow *parent = GetParent();
532 if ( event.GetEventObject() == parent )
533 {
534 // no, it doesn't come from child, case (b): forward to a page
535 if ( m_nSelection != -1 )
536 {
537 // so that the page knows that the event comes from it's parent
538 // and is being propagated downwards
539 event.SetEventObject(this);
540
541 wxWindow *page = m_pages[m_nSelection];
542 if ( !page->GetEventHandler()->ProcessEvent(event) )
543 {
544 page->SetFocus();
545 }
546 //else: page manages focus inside it itself
547 }
548 else
549 {
550 // we have no pages - still have to give focus to _something_
551 SetFocus();
552 }
553 }
554 else
555 {
556 // it comes from our child, case (a), pass to the parent
557 if ( parent ) {
558 event.SetCurrentFocus(this);
559 parent->GetEventHandler()->ProcessEvent(event);
560 }
561 }
562 }
563}
564
565// ----------------------------------------------------------------------------
566// wxNotebook base class virtuals
567// ----------------------------------------------------------------------------
568
569// override these 2 functions to do nothing: everything is done in OnSize
570
571void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
572{
573 // don't set the sizes of the pages - their correct size is not yet known
574 wxControl::SetConstraintSizes(FALSE);
575}
576
577bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
578{
579 return TRUE;
580}
581
582bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
583{
584 wxNotebookEvent event(wxEVT_NULL, m_windowId);
585
586 NMHDR* hdr = (NMHDR *)lParam;
587 switch ( hdr->code ) {
588 case TCN_SELCHANGE:
589 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
590 break;
591
592 case TCN_SELCHANGING:
593 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
594 break;
595
596 default:
597 return wxControl::MSWOnNotify(idCtrl, lParam, result);
598 }
599
600 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
601 event.SetOldSelection(m_nSelection);
602 event.SetEventObject(this);
603 event.SetInt(idCtrl);
604
605 bool processed = GetEventHandler()->ProcessEvent(event);
606 *result = !event.IsAllowed();
607 return processed;
608}
609
610// ----------------------------------------------------------------------------
611// wxNotebook helper functions
612// ----------------------------------------------------------------------------
613
614// generate the page changing and changed events, hide the currently active
615// panel and show the new one
616void wxNotebook::ChangePage(int nOldSel, int nSel)
617{
618 // MT-FIXME should use a real semaphore
619 static bool s_bInsideChangePage = FALSE;
620
621 // when we call ProcessEvent(), our own OnSelChange() is called which calls
622 // this function - break the infinite loop
623 if ( s_bInsideChangePage )
624 return;
625
626 // it's not an error (the message may be generated by the tab control itself)
627 // and it may happen - just do nothing
628 if ( nSel == nOldSel )
629 return;
630
631 s_bInsideChangePage = TRUE;
632
633 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
634 event.SetSelection(nSel);
635 event.SetOldSelection(nOldSel);
636 event.SetEventObject(this);
637 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
638 {
639 // program doesn't allow the page change
640 s_bInsideChangePage = FALSE;
641 return;
642 }
643
644 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
645 GetEventHandler()->ProcessEvent(event);
646
647 s_bInsideChangePage = FALSE;
648}
649
650#endif // wxUSE_NOTEBOOK