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