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