]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
Some but not all compile fixes for typetest (VC++ 1.5); added datetime.cpp
[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 // wxNotebook size settings
287 // ----------------------------------------------------------------------------
288
289 void wxNotebook::SetPageSize(const wxSize& size)
290 {
291 // transform the page size into the notebook size
292 RECT rc;
293 rc.left =
294 rc.top = 0;
295 rc.right = size.x;
296 rc.bottom = size.y;
297
298 TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc);
299
300 // and now set it
301 SetSize(rc.right - rc.left, rc.bottom - rc.top);
302 }
303
304 void wxNotebook::SetPadding(const wxSize& padding)
305 {
306 TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
307 }
308
309 // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH
310 // style.
311 void wxNotebook::SetTabSize(const wxSize& sz)
312 {
313 ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y));
314 }
315
316 // ----------------------------------------------------------------------------
317 // wxNotebook operations
318 // ----------------------------------------------------------------------------
319
320 // remove one page from the notebook
321 bool wxNotebook::DeletePage(int nPage)
322 {
323 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
324
325 if ( m_nSelection == nPage ) {
326 // advance selection backwards - the page being deleted shouldn't be left
327 // selected
328 AdvanceSelection(FALSE);
329 }
330
331 TabCtrl_DeleteItem(m_hwnd, nPage);
332
333 delete m_aPages[nPage];
334 m_aPages.Remove(nPage);
335
336 if ( m_aPages.IsEmpty() ) {
337 // no selection if the notebook became empty
338 m_nSelection = -1;
339 }
340 else
341 m_nSelection = TabCtrl_GetCurSel(m_hwnd);
342
343
344 return TRUE;
345 }
346
347 // remove one page from the notebook, without deleting
348 bool wxNotebook::RemovePage(int nPage)
349 {
350 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
351
352 TabCtrl_DeleteItem(m_hwnd, nPage);
353
354 m_aPages.Remove(nPage);
355
356 if ( m_aPages.IsEmpty() )
357 m_nSelection = -1;
358 else
359 m_nSelection = TabCtrl_GetCurSel(m_hwnd);
360
361 return TRUE;
362 }
363
364 // remove all pages
365 bool wxNotebook::DeleteAllPages()
366 {
367 int nPageCount = GetPageCount();
368 int nPage;
369 for ( nPage = 0; nPage < nPageCount; nPage++ )
370 delete m_aPages[nPage];
371
372 m_aPages.Clear();
373
374 TabCtrl_DeleteAllItems(m_hwnd);
375
376 m_nSelection = -1;
377
378 return TRUE;
379 }
380
381 // add a page to the notebook
382 bool wxNotebook::AddPage(wxNotebookPage *pPage,
383 const wxString& strText,
384 bool bSelect,
385 int imageId)
386 {
387 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
388 }
389
390 // same as AddPage() but does it at given position
391 bool wxNotebook::InsertPage(int nPage,
392 wxNotebookPage *pPage,
393 const wxString& strText,
394 bool bSelect,
395 int imageId)
396 {
397 wxASSERT( pPage != NULL );
398 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
399
400 // do add the tab to the control
401
402 // init all fields to 0
403 TC_ITEM tcItem;
404 memset(&tcItem, 0, sizeof(tcItem));
405
406 if ( imageId != -1 )
407 {
408 tcItem.mask |= TCIF_IMAGE;
409 tcItem.iImage = imageId;
410 }
411
412 if ( !strText.IsEmpty() )
413 {
414 tcItem.mask |= TCIF_TEXT;
415 tcItem.pszText = (wxChar *)strText.c_str(); // const_cast
416 }
417
418 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
419 wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str());
420
421 return FALSE;
422 }
423
424 // if the inserted page is before the selected one, we must update the
425 // index of the selected page
426 if ( nPage <= m_nSelection )
427 {
428 // one extra page added
429 m_nSelection++;
430 }
431
432 // save the pointer to the page
433 m_aPages.Insert(pPage, nPage);
434
435 // don't show pages by default (we'll need to adjust their size first)
436 HWND hwnd = GetWinHwnd(pPage);
437 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
438
439 // this updates internal flag too - otherwise it will get out of sync
440 pPage->Show(FALSE);
441
442 // fit the notebook page to the tab control's display area
443 RECT rc;
444 rc.left = rc.top = 0;
445 GetSize((int *)&rc.right, (int *)&rc.bottom);
446 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
447 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
448
449 // some page should be selected: either this one or the first one if there is
450 // still no selection
451 int selNew = -1;
452 if ( bSelect )
453 selNew = nPage;
454 else if ( m_nSelection == -1 )
455 selNew = 0;
456
457 if ( selNew != -1 )
458 SetSelection(selNew);
459
460 return TRUE;
461 }
462
463 // ----------------------------------------------------------------------------
464 // wxNotebook callbacks
465 // ----------------------------------------------------------------------------
466
467 void wxNotebook::OnSize(wxSizeEvent& event)
468 {
469 // fit the notebook page to the tab control's display area
470 RECT rc;
471 rc.left = rc.top = 0;
472 GetSize((int *)&rc.right, (int *)&rc.bottom);
473
474 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
475
476 int width = rc.right - rc.left,
477 height = rc.bottom - rc.top;
478 size_t nCount = m_aPages.Count();
479 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
480 wxNotebookPage *pPage = m_aPages[nPage];
481 pPage->SetSize(rc.left, rc.top, width, height);
482 }
483
484 event.Skip();
485 }
486
487 void wxNotebook::OnSelChange(wxNotebookEvent& event)
488 {
489 // is it our tab control?
490 if ( event.GetEventObject() == this )
491 {
492 int sel = event.GetOldSelection();
493 if ( sel != -1 )
494 m_aPages[sel]->Show(FALSE);
495
496 sel = event.GetSelection();
497 if ( sel != -1 )
498 {
499 wxNotebookPage *pPage = m_aPages[sel];
500 pPage->Show(TRUE);
501 pPage->SetFocus();
502 }
503
504 m_nSelection = sel;
505 }
506
507 // we want to give others a chance to process this message as well
508 event.Skip();
509 }
510
511 void wxNotebook::OnSetFocus(wxFocusEvent& event)
512 {
513 // this function is only called when the focus is explicitly set (i.e. from
514 // the program) to the notebook - in this case we don't need the
515 // complicated OnNavigationKey() logic because the programmer knows better
516 // what [s]he wants
517
518 // set focus to the currently selected page if any
519 if ( m_nSelection != -1 )
520 m_aPages[m_nSelection]->SetFocus();
521
522 event.Skip();
523 }
524
525 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
526 {
527 if ( event.IsWindowChange() ) {
528 // change pages
529 AdvanceSelection(event.GetDirection());
530 }
531 else {
532 // we get this event in 2 cases
533 //
534 // a) one of our pages might have generated it because the user TABbed
535 // out from it in which case we should propagate the event upwards and
536 // our parent will take care of setting the focus to prev/next sibling
537 //
538 // or
539 //
540 // b) the parent panel wants to give the focus to us so that we
541 // forward it to our selected page. We can't deal with this in
542 // OnSetFocus() because we don't know which direction the focus came
543 // from in this case and so can't choose between setting the focus to
544 // first or last panel child
545
546 wxWindow *parent = GetParent();
547 if ( event.GetEventObject() == parent )
548 {
549 // no, it doesn't come from child, case (b): forward to a page
550 if ( m_nSelection != -1 )
551 {
552 // so that the page knows that the event comes from it's parent
553 // and is being propagated downwards
554 event.SetEventObject(this);
555
556 wxWindow *page = m_aPages[m_nSelection];
557 if ( !page->GetEventHandler()->ProcessEvent(event) )
558 {
559 page->SetFocus();
560 }
561 //else: page manages focus inside it itself
562 }
563 else
564 {
565 // we have no pages - still have to give focus to _something_
566 SetFocus();
567 }
568 }
569 else
570 {
571 // it comes from our child, case (a), pass to the parent
572 if ( parent ) {
573 event.SetCurrentFocus(this);
574 parent->GetEventHandler()->ProcessEvent(event);
575 }
576 }
577 }
578 }
579
580 // ----------------------------------------------------------------------------
581 // wxNotebook base class virtuals
582 // ----------------------------------------------------------------------------
583
584 // override these 2 functions to do nothing: everything is done in OnSize
585
586 void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
587 {
588 // don't set the sizes of the pages - their correct size is not yet known
589 wxControl::SetConstraintSizes(FALSE);
590 }
591
592 bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
593 {
594 return TRUE;
595 }
596
597 bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
598 {
599 wxNotebookEvent event(wxEVT_NULL, m_windowId);
600
601 NMHDR* hdr = (NMHDR *)lParam;
602 switch ( hdr->code ) {
603 case TCN_SELCHANGE:
604 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
605 break;
606
607 case TCN_SELCHANGING:
608 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
609 break;
610
611 default:
612 return wxControl::MSWOnNotify(idCtrl, lParam, result);
613 }
614
615 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
616 event.SetOldSelection(m_nSelection);
617 event.SetEventObject(this);
618 event.SetInt(idCtrl);
619
620 bool processed = GetEventHandler()->ProcessEvent(event);
621 *result = !event.IsAllowed();
622 return processed;
623 }
624
625 // ----------------------------------------------------------------------------
626 // wxNotebook helper functions
627 // ----------------------------------------------------------------------------
628
629 // generate the page changing and changed events, hide the currently active
630 // panel and show the new one
631 void wxNotebook::ChangePage(int nOldSel, int nSel)
632 {
633 // MT-FIXME should use a real semaphore
634 static bool s_bInsideChangePage = FALSE;
635
636 // when we call ProcessEvent(), our own OnSelChange() is called which calls
637 // this function - break the infinite loop
638 if ( s_bInsideChangePage )
639 return;
640
641 // it's not an error (the message may be generated by the tab control itself)
642 // and it may happen - just do nothing
643 if ( nSel == nOldSel )
644 return;
645
646 s_bInsideChangePage = TRUE;
647
648 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
649 event.SetSelection(nSel);
650 event.SetOldSelection(nOldSel);
651 event.SetEventObject(this);
652 if ( ProcessEvent(event) && !event.IsAllowed() )
653 {
654 // program doesn't allow the page change
655 s_bInsideChangePage = FALSE;
656 return;
657 }
658
659 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
660 ProcessEvent(event);
661
662 s_bInsideChangePage = FALSE;
663 }