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