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