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