]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
More Motif stuff, minor stubs correction
[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
VZ
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
aaab7c01
VZ
46#include <windowsx.h> // for SetWindowFont
47
88310e2e
VZ
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,
8b9518ee 104 wxWindowID id,
88310e2e
VZ
105 const wxPoint& pos,
106 const wxSize& size,
8b9518ee 107 long style,
88310e2e
VZ
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,
8b9518ee 117 wxWindowID id,
88310e2e
VZ
118 const wxPoint& pos,
119 const wxSize& size,
8b9518ee 120 long style,
88310e2e
VZ
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
fd2daa68 137 m_windowStyle = style | wxTAB_TRAVERSAL;
88310e2e
VZ
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
27529614
JS
164 // Not all compilers recognise SetWindowFont
165// SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
166 ::SendMessage((HWND) m_hwnd, WM_SETFONT,
167 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT),TRUE);
168
aaab7c01 169
88310e2e
VZ
170 if ( parent != NULL )
171 parent->AddChild(this);
172
173 SubclassWin(m_hWnd);
174
175 return TRUE;
176}
177
178// dtor
179wxNotebook::~wxNotebook()
180{
181}
182
183// ----------------------------------------------------------------------------
184// wxNotebook accessors
185// ----------------------------------------------------------------------------
186int wxNotebook::GetPageCount() const
187{
188 // consistency check
189 wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) );
190
191 return m_aPages.Count();
192}
193
194int wxNotebook::GetRowCount() const
195{
196 return TabCtrl_GetRowCount(m_hwnd);
197}
198
199int wxNotebook::SetSelection(int nPage)
200{
201 wxASSERT( IS_VALID_PAGE(nPage) );
202
203 ChangePage(m_nSelection, nPage);
204
205 return TabCtrl_SetCurSel(m_hwnd, nPage);
206}
207
208void wxNotebook::AdvanceSelection(bool bForward)
209{
210 int nSel = GetSelection();
211 int nMax = GetPageCount() - 1;
212 if ( bForward )
213 SetSelection(nSel == nMax ? 0 : nSel + 1);
214 else
215 SetSelection(nSel == 0 ? nMax : nSel - 1);
216}
217
218bool wxNotebook::SetPageText(int nPage, const wxString& strText)
219{
220 wxASSERT( IS_VALID_PAGE(nPage) );
221
222 TC_ITEM tcItem;
223 tcItem.mask = TCIF_TEXT;
224 tcItem.pszText = (char *)strText.c_str();
225
226 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
227}
228
229wxString wxNotebook::GetPageText(int nPage) const
230{
231 wxASSERT( IS_VALID_PAGE(nPage) );
232
233 char buf[256];
234 TC_ITEM tcItem;
235 tcItem.mask = TCIF_TEXT;
236 tcItem.pszText = buf;
237 tcItem.cchTextMax = WXSIZEOF(buf);
238
239 wxString str;
240 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
241 str = tcItem.pszText;
242
243 return str;
244}
245
246int wxNotebook::GetPageImage(int nPage) const
247{
248 wxASSERT( IS_VALID_PAGE(nPage) );
249
250 TC_ITEM tcItem;
251 tcItem.mask = TCIF_IMAGE;
252
253 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
254}
255
256bool wxNotebook::SetPageImage(int nPage, int nImage)
257{
258 wxASSERT( IS_VALID_PAGE(nPage) );
259
260 TC_ITEM tcItem;
261 tcItem.mask = TCIF_IMAGE;
262 tcItem.iImage = nImage;
263
264 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
265}
266
267void wxNotebook::SetImageList(wxImageList* imageList)
268{
269 m_pImageList = imageList;
270 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
271}
272
273// ----------------------------------------------------------------------------
274// wxNotebook operations
275// ----------------------------------------------------------------------------
276
277// remove one page from the notebook
278bool wxNotebook::DeletePage(int nPage)
279{
280 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
281
282 TabCtrl_DeleteItem(m_hwnd, nPage);
283
284 delete m_aPages[nPage];
285 m_aPages.Remove(nPage);
286
287 return TRUE;
288}
289
290// remove all pages
291bool wxNotebook::DeleteAllPages()
292{
293 TabCtrl_DeleteAllItems(m_hwnd);
294
295 int nPageCount = GetPageCount();
296 int nPage;
297 for ( nPage = 0; nPage < nPageCount; nPage++ )
298 delete m_aPages[nPage];
299
300 m_aPages.Clear();
301
302 return TRUE;
303}
304
305// add a page to the notebook
306bool wxNotebook::AddPage(wxNotebookPage *pPage,
307 const wxString& strText,
308 bool bSelect,
309 int imageId)
310{
311 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
312}
313
314// same as AddPage() but does it at given position
315bool wxNotebook::InsertPage(int nPage,
316 wxNotebookPage *pPage,
317 const wxString& strText,
318 bool bSelect,
319 int imageId)
320{
321 wxASSERT( pPage != NULL );
322 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
323
324 // add the tab to the control
325 TC_ITEM tcItem;
326 tcItem.mask = TCIF_TEXT | TCIF_IMAGE;
327 tcItem.pszText = (char *)strText.c_str();
328 tcItem.iImage = imageId;
329
330 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
331 wxLogError("Can't create the notebook page '%s'.", strText.c_str());
332 return FALSE;
333 }
334
335 // save the pointer to the page
336 m_aPages.Insert(pPage, nPage);
337
338 // some page must be selected: either this one or the first one if there is
339 // still no selection
340 if ( bSelect )
341 m_nSelection = nPage;
342 else if ( m_nSelection == -1 )
343 m_nSelection = 0;
344
345 // don't show pages by default (we'll need to adjust their size first)
346 HWND hwnd = (HWND)pPage->GetHWND();
347 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
348
349 return TRUE;
350}
351
352// ----------------------------------------------------------------------------
353// wxNotebook callbacks
354// ----------------------------------------------------------------------------
355
88310e2e
VZ
356void wxNotebook::OnSize(wxSizeEvent& event)
357{
88310e2e
VZ
358 // emulate page change (it's esp. important to do it first time because
359 // otherwise our page would stay invisible)
360 int nSel = m_nSelection;
361 m_nSelection = -1;
362 SetSelection(nSel);
363
b5c3b538
VZ
364 // fit the notebook page to the tab control's display area
365 RECT rc;
366 rc.left = rc.top = 0;
367 GetSize((int *)&rc.right, (int *)&rc.bottom);
368
369 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
c86f1403
VZ
370 size_t nCount = m_aPages.Count();
371 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538
VZ
372 wxNotebookPage *pPage = m_aPages[nPage];
373 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
374 if ( pPage->GetAutoLayout() )
375 pPage->Layout();
376 }
377
88310e2e
VZ
378 event.Skip();
379}
380
381void wxNotebook::OnSelChange(wxNotebookEvent& event)
382{
383 // is it our tab control?
384 if ( event.GetEventObject() == this )
385 ChangePage(event.GetOldSelection(), event.GetSelection());
386
387 // we want to give others a chance to process this message as well
388 event.Skip();
389}
390
391void wxNotebook::OnSetFocus(wxFocusEvent& event)
392{
393 // set focus to the currently selected page if any
394 if ( m_nSelection != -1 )
395 m_aPages[m_nSelection]->SetFocus();
396
397 event.Skip();
398}
399
400void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
401{
402 if ( event.IsWindowChange() ) {
403 // change pages
404 AdvanceSelection(event.GetDirection());
405 }
406 else {
407 // pass to the parent
408 if ( GetParent() ) {
409 event.SetCurrentFocus(this);
410 GetParent()->ProcessEvent(event);
411 }
412 }
413}
414
415// ----------------------------------------------------------------------------
416// wxNotebook base class virtuals
417// ----------------------------------------------------------------------------
b5c3b538
VZ
418
419// override these 2 functions to do nothing: everything is done in OnSize
420
421void wxNotebook::SetConstraintSizes(bool /* recurse */)
422{
423 // don't set the sizes of the pages - their correct size is not yet known
424 wxControl::SetConstraintSizes(FALSE);
425}
426
427bool wxNotebook::DoPhase(int /* nPhase */)
428{
429 return TRUE;
430}
431
88310e2e
VZ
432void wxNotebook::Command(wxCommandEvent& event)
433{
434 wxFAIL_MSG("wxNotebook::Command not implemented");
435}
436
8b9518ee 437bool wxNotebook::MSWNotify(WXWPARAM wParam, WXLPARAM lParam)
88310e2e 438{
93a19f17 439 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
440
441 NMHDR* hdr = (NMHDR *)lParam;
442 switch ( hdr->code ) {
443 case TCN_SELCHANGE:
444 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
445 break;
446
447 case TCN_SELCHANGING:
448 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
449 break;
450
93a19f17
VZ
451 // prevent this msg from being passed to wxControl::MSWNotify which would
452 // retrun FALSE disabling the change of page
aaab7c01 453 case UDN_DELTAPOS:
aaab7c01
VZ
454 return TRUE;
455
88310e2e
VZ
456 default :
457 return wxControl::MSWNotify(wParam, lParam);
458 }
459
93a19f17
VZ
460 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
461 event.SetOldSelection(m_nSelection);
88310e2e
VZ
462 event.SetEventObject(this);
463 event.SetInt(LOWORD(wParam));
464
465 return ProcessEvent(event);
466}
467
468// ----------------------------------------------------------------------------
469// wxNotebook helper functions
470// ----------------------------------------------------------------------------
471
472// hide the currently active panel and show the new one
473void wxNotebook::ChangePage(int nOldSel, int nSel)
474{
aaab7c01
VZ
475 // it's not an error (the message may be generated by the tab control itself)
476 // and it may happen - just do nothing
477 if ( nSel == nOldSel )
478 return;
88310e2e 479
aaab7c01 480 if ( nOldSel != -1 )
88310e2e 481 m_aPages[nOldSel]->Show(FALSE);
88310e2e
VZ
482
483 wxNotebookPage *pPage = m_aPages[nSel];
88310e2e 484 pPage->Show(TRUE);
b5c3b538 485 pPage->SetFocus();
88310e2e
VZ
486
487 m_nSelection = nSel;
488}