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