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