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