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