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