]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
InsertPage/SetSelection bug fixed
[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__
45 #ifndef wxUSE_NORLANDER_HEADERS
46 #include "wx/msw/gnuwin32/extra.h"
47 #endif
48 #endif
49 #endif
50
51 #if !defined(__GNUWIN32__) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
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 // event table
67 // ----------------------------------------------------------------------------
68
69 #if !USE_SHARED_LIBRARIES
70 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
71 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
72
73 EVT_SIZE(wxNotebook::OnSize)
74
75 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
76
77 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
78 END_EVENT_TABLE()
79
80 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
81 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
82 #endif
83
84 // ============================================================================
85 // implementation
86 // ============================================================================
87
88 // ----------------------------------------------------------------------------
89 // wxNotebook construction
90 // ----------------------------------------------------------------------------
91
92 // common part of all ctors
93 void wxNotebook::Init()
94 {
95 m_pImageList = NULL;
96 m_nSelection = -1;
97 }
98
99 // default for dynamic class
100 wxNotebook::wxNotebook()
101 {
102 Init();
103 }
104
105 // the same arguments as for wxControl
106 wxNotebook::wxNotebook(wxWindow *parent,
107 wxWindowID id,
108 const wxPoint& pos,
109 const wxSize& size,
110 long style,
111 const wxString& name)
112 {
113 Init();
114
115 Create(parent, id, pos, size, style, name);
116 }
117
118 // Create() function
119 bool wxNotebook::Create(wxWindow *parent,
120 wxWindowID id,
121 const wxPoint& pos,
122 const wxSize& size,
123 long style,
124 const wxString& name)
125 {
126 // base init
127 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
128 return FALSE;
129
130 // colors and font
131 m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
132 m_foregroundColour = *wxBLACK ;
133
134 // style
135 m_windowStyle = style | wxTAB_TRAVERSAL;
136
137 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
138
139 if (m_windowStyle & wxCLIP_CHILDREN)
140 tabStyle |= WS_CLIPCHILDREN;
141 if ( m_windowStyle & wxTC_MULTILINE )
142 tabStyle |= TCS_MULTILINE;
143 if ( m_windowStyle & wxBORDER )
144 tabStyle &= WS_BORDER;
145 if (m_windowStyle & wxNB_FIXEDWIDTH)
146 tabStyle |= TCS_FIXEDWIDTH ;
147
148 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
149 this, NULL, pos.x, pos.y, size.x, size.y,
150 tabStyle, NULL, 0) )
151 {
152 return FALSE;
153 }
154
155 // Not all compilers recognise SetWindowFont
156 ::SendMessage(GetHwnd(), WM_SETFONT,
157 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
158
159
160 if ( parent != NULL )
161 parent->AddChild(this);
162
163 SubclassWin(m_hWnd);
164
165 return TRUE;
166 }
167
168 // dtor
169 wxNotebook::~wxNotebook()
170 {
171 }
172
173 // ----------------------------------------------------------------------------
174 // wxNotebook accessors
175 // ----------------------------------------------------------------------------
176 int wxNotebook::GetPageCount() const
177 {
178 // consistency check
179 wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) );
180
181 return m_aPages.Count();
182 }
183
184 int wxNotebook::GetRowCount() const
185 {
186 return TabCtrl_GetRowCount(m_hwnd);
187 }
188
189 int wxNotebook::SetSelection(int nPage)
190 {
191 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("notebook page out of range") );
192
193 ChangePage(m_nSelection, nPage);
194
195 return TabCtrl_SetCurSel(m_hwnd, nPage);
196 }
197
198 void wxNotebook::AdvanceSelection(bool bForward)
199 {
200 int nSel = GetSelection();
201 int nMax = GetPageCount() - 1;
202 if ( bForward )
203 SetSelection(nSel == nMax ? 0 : nSel + 1);
204 else
205 SetSelection(nSel == 0 ? nMax : nSel - 1);
206 }
207
208 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
209 {
210 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
211
212 TC_ITEM tcItem;
213 tcItem.mask = TCIF_TEXT;
214 tcItem.pszText = (wxChar *)strText.c_str();
215
216 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
217 }
218
219 wxString wxNotebook::GetPageText(int nPage) const
220 {
221 wxCHECK_MSG( IS_VALID_PAGE(nPage), _T(""), _T("notebook page out of range") );
222
223 wxChar buf[256];
224 TC_ITEM tcItem;
225 tcItem.mask = TCIF_TEXT;
226 tcItem.pszText = buf;
227 tcItem.cchTextMax = WXSIZEOF(buf);
228
229 wxString str;
230 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
231 str = tcItem.pszText;
232
233 return str;
234 }
235
236 int wxNotebook::GetPageImage(int nPage) const
237 {
238 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("notebook page out of range") );
239
240 TC_ITEM tcItem;
241 tcItem.mask = TCIF_IMAGE;
242
243 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
244 }
245
246 bool wxNotebook::SetPageImage(int nPage, int nImage)
247 {
248 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
249
250 TC_ITEM tcItem;
251 tcItem.mask = TCIF_IMAGE;
252 tcItem.iImage = nImage;
253
254 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
255 }
256
257 void wxNotebook::SetImageList(wxImageList* imageList)
258 {
259 m_pImageList = imageList;
260 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
261 }
262
263
264 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
265 // style.
266 void wxNotebook::SetTabSize(const wxSize& sz)
267 {
268 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
269 }
270
271 // ----------------------------------------------------------------------------
272 // wxNotebook operations
273 // ----------------------------------------------------------------------------
274
275 // remove one page from the notebook
276 bool wxNotebook::DeletePage(int nPage)
277 {
278 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
279
280 if ( m_nSelection == nPage ) {
281 // advance selection backwards - the page being deleted shouldn't be left
282 // selected
283 AdvanceSelection(FALSE);
284 }
285
286 TabCtrl_DeleteItem(m_hwnd, nPage);
287
288 delete m_aPages[nPage];
289 m_aPages.Remove(nPage);
290
291 if ( m_aPages.IsEmpty() ) {
292 // no selection if the notebook became empty
293 m_nSelection = -1;
294 }
295
296 return TRUE;
297 }
298
299 // remove one page from the notebook, without deleting
300 bool wxNotebook::RemovePage(int nPage)
301 {
302 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("notebook page out of range") );
303
304 TabCtrl_DeleteItem(m_hwnd, nPage);
305
306 m_aPages.Remove(nPage);
307
308 return TRUE;
309 }
310
311 // remove all pages
312 bool wxNotebook::DeleteAllPages()
313 {
314 int nPageCount = GetPageCount();
315 int nPage;
316 for ( nPage = 0; nPage < nPageCount; nPage++ )
317 delete m_aPages[nPage];
318
319 m_aPages.Clear();
320
321 TabCtrl_DeleteAllItems(m_hwnd);
322
323 return TRUE;
324 }
325
326 // add a page to the notebook
327 bool wxNotebook::AddPage(wxNotebookPage *pPage,
328 const wxString& strText,
329 bool bSelect,
330 int imageId)
331 {
332 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
333 }
334
335 // same as AddPage() but does it at given position
336 bool wxNotebook::InsertPage(int nPage,
337 wxNotebookPage *pPage,
338 const wxString& strText,
339 bool bSelect,
340 int imageId)
341 {
342 wxASSERT( pPage != NULL );
343 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
344
345 // do add the tab to the control
346
347 // init all fields to 0
348 TC_ITEM tcItem;
349 memset(&tcItem, 0, sizeof(tcItem));
350
351 if ( imageId != -1 )
352 {
353 tcItem.mask |= TCIF_IMAGE;
354 tcItem.iImage = imageId;
355 }
356
357 if ( !strText.IsEmpty() )
358 {
359 tcItem.mask |= TCIF_TEXT;
360 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
361 }
362
363 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
364 wxLogError(_T("Can't create the notebook page '%s'."), strText.c_str());
365
366 return FALSE;
367 }
368
369 // if the inserted page is before the selected one, we must update the
370 // index of the selected page
371 if ( nPage <= m_nSelection )
372 {
373 // one extra page added
374 m_nSelection++;
375 }
376
377 // save the pointer to the page
378 m_aPages.Insert(pPage, nPage);
379
380 // don't show pages by default (we'll need to adjust their size first)
381 HWND hwnd = GetWinHwnd(pPage);
382 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
383
384 // this updates internal flag too - otherwise it will get out of sync
385 pPage->Show(FALSE);
386
387 // some page should be selected: either this one or the first one if there is
388 // still no selection
389 int selNew = -1;
390 if ( bSelect )
391 selNew = nPage;
392 else if ( m_nSelection == -1 )
393 selNew = 0;
394
395 if ( selNew != -1 )
396 SetSelection(selNew);
397
398 return TRUE;
399 }
400
401 // ----------------------------------------------------------------------------
402 // wxNotebook callbacks
403 // ----------------------------------------------------------------------------
404
405 void wxNotebook::OnSize(wxSizeEvent& event)
406 {
407 // fit the notebook page to the tab control's display area
408 RECT rc;
409 rc.left = rc.top = 0;
410 GetSize((int *)&rc.right, (int *)&rc.bottom);
411
412 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
413 size_t nCount = m_aPages.Count();
414 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
415 wxNotebookPage *pPage = m_aPages[nPage];
416 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
417 if ( pPage->GetAutoLayout() )
418 pPage->Layout();
419 }
420
421 event.Skip();
422 }
423
424 void wxNotebook::OnSelChange(wxNotebookEvent& event)
425 {
426 // is it our tab control?
427 if ( event.GetEventObject() == this )
428 {
429 int sel = event.GetOldSelection();
430 if ( sel != -1 )
431 m_aPages[sel]->Show(FALSE);
432
433 sel = event.GetSelection();
434 if ( sel != -1 )
435 {
436 wxNotebookPage *pPage = m_aPages[sel];
437 pPage->Show(TRUE);
438 pPage->SetFocus();
439 }
440
441 m_nSelection = sel;
442 }
443
444 // we want to give others a chance to process this message as well
445 event.Skip();
446 }
447
448 void wxNotebook::OnSetFocus(wxFocusEvent& event)
449 {
450 // set focus to the currently selected page if any
451 if ( m_nSelection != -1 )
452 m_aPages[m_nSelection]->SetFocus();
453
454 event.Skip();
455 }
456
457 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
458 {
459 if ( event.IsWindowChange() ) {
460 // change pages
461 AdvanceSelection(event.GetDirection());
462 }
463 else {
464 // pass to the parent
465 if ( GetParent() ) {
466 event.SetCurrentFocus(this);
467 GetParent()->GetEventHandler()->ProcessEvent(event);
468 }
469 }
470 }
471
472 // ----------------------------------------------------------------------------
473 // wxNotebook base class virtuals
474 // ----------------------------------------------------------------------------
475
476 // override these 2 functions to do nothing: everything is done in OnSize
477
478 void wxNotebook::SetConstraintSizes(bool /* recurse */)
479 {
480 // don't set the sizes of the pages - their correct size is not yet known
481 wxControl::SetConstraintSizes(FALSE);
482 }
483
484 bool wxNotebook::DoPhase(int /* nPhase */)
485 {
486 return TRUE;
487 }
488
489 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
490 {
491 wxNotebookEvent event(wxEVT_NULL, m_windowId);
492
493 NMHDR* hdr = (NMHDR *)lParam;
494 switch ( hdr->code ) {
495 case TCN_SELCHANGE:
496 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
497 break;
498
499 case TCN_SELCHANGING:
500 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
501 break;
502
503 default:
504 return wxControl::MSWOnNotify(idCtrl, lParam, result);
505 }
506
507 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
508 event.SetOldSelection(m_nSelection);
509 event.SetEventObject(this);
510 event.SetInt(idCtrl);
511
512 bool processed = GetEventHandler()->ProcessEvent(event);
513 *result = !event.IsAllowed();
514 return processed;
515 }
516
517 // ----------------------------------------------------------------------------
518 // wxNotebook helper functions
519 // ----------------------------------------------------------------------------
520
521 // generate the page changing and changed events, hide the currently active
522 // panel and show the new one
523 void wxNotebook::ChangePage(int nOldSel, int nSel)
524 {
525 // MT-FIXME should use a real semaphore
526 static bool s_bInsideChangePage = FALSE;
527
528 // when we call ProcessEvent(), our own OnSelChange() is called which calls
529 // this function - break the infinite loop
530 if ( s_bInsideChangePage )
531 return;
532
533 // it's not an error (the message may be generated by the tab control itself)
534 // and it may happen - just do nothing
535 if ( nSel == nOldSel )
536 return;
537
538 s_bInsideChangePage = TRUE;
539
540 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
541 event.SetSelection(nSel);
542 event.SetOldSelection(nOldSel);
543 event.SetEventObject(this);
544 if ( ProcessEvent(event) && !event.IsAllowed() )
545 {
546 // program doesn't allow the page change
547 s_bInsideChangePage = FALSE;
548 return;
549 }
550
551 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
552 ProcessEvent(event);
553
554 s_bInsideChangePage = FALSE;
555 }