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