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