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