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