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