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