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