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