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