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