]> git.saurik.com Git - wxWidgets.git/blame - src/msw/notebook.cpp
added intelligent scaling of icons -- cutting empty borders so that the icon is not...
[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));
4b7f2165 147 m_foregroundColour = *wxBLACK;
88310e2e 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);
4b7f2165
VZ
453
454 int width = rc.right - rc.left,
455 height = rc.bottom - rc.top;
c86f1403
VZ
456 size_t nCount = m_aPages.Count();
457 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
b5c3b538 458 wxNotebookPage *pPage = m_aPages[nPage];
4b7f2165 459 pPage->SetSize(rc.left, rc.top, width, height);
b5c3b538
VZ
460 }
461
88310e2e
VZ
462 event.Skip();
463}
464
465void wxNotebook::OnSelChange(wxNotebookEvent& event)
466{
467 // is it our tab control?
468 if ( event.GetEventObject() == this )
5d1d2d46 469 {
5d1d2d46
VZ
470 int sel = event.GetOldSelection();
471 if ( sel != -1 )
472 m_aPages[sel]->Show(FALSE);
0398b1d6 473
5d1d2d46
VZ
474 sel = event.GetSelection();
475 if ( sel != -1 )
476 {
477 wxNotebookPage *pPage = m_aPages[sel];
478 pPage->Show(TRUE);
479 pPage->SetFocus();
480 }
0398b1d6 481
5d1d2d46
VZ
482 m_nSelection = sel;
483 }
88310e2e
VZ
484
485 // we want to give others a chance to process this message as well
486 event.Skip();
487}
488
489void wxNotebook::OnSetFocus(wxFocusEvent& event)
490{
491 // set focus to the currently selected page if any
492 if ( m_nSelection != -1 )
493 m_aPages[m_nSelection]->SetFocus();
494
495 event.Skip();
496}
497
498void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
499{
500 if ( event.IsWindowChange() ) {
501 // change pages
502 AdvanceSelection(event.GetDirection());
503 }
504 else {
505 // pass to the parent
506 if ( GetParent() ) {
507 event.SetCurrentFocus(this);
02800301 508 GetParent()->GetEventHandler()->ProcessEvent(event);
88310e2e
VZ
509 }
510 }
511}
512
513// ----------------------------------------------------------------------------
514// wxNotebook base class virtuals
515// ----------------------------------------------------------------------------
b5c3b538
VZ
516
517// override these 2 functions to do nothing: everything is done in OnSize
518
4b7f2165 519void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
b5c3b538
VZ
520{
521 // don't set the sizes of the pages - their correct size is not yet known
522 wxControl::SetConstraintSizes(FALSE);
523}
524
4b7f2165 525bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
b5c3b538
VZ
526{
527 return TRUE;
528}
529
a23fd0e1 530bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
88310e2e 531{
93a19f17 532 wxNotebookEvent event(wxEVT_NULL, m_windowId);
88310e2e
VZ
533
534 NMHDR* hdr = (NMHDR *)lParam;
535 switch ( hdr->code ) {
536 case TCN_SELCHANGE:
537 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
538 break;
539
540 case TCN_SELCHANGING:
541 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
542 break;
543
fd3f686c 544 default:
a23fd0e1 545 return wxControl::MSWOnNotify(idCtrl, lParam, result);
88310e2e
VZ
546 }
547
93a19f17
VZ
548 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
549 event.SetOldSelection(m_nSelection);
88310e2e 550 event.SetEventObject(this);
a23fd0e1 551 event.SetInt(idCtrl);
88310e2e 552
fd3f686c
VZ
553 bool processed = GetEventHandler()->ProcessEvent(event);
554 *result = !event.IsAllowed();
555 return processed;
88310e2e
VZ
556}
557
558// ----------------------------------------------------------------------------
559// wxNotebook helper functions
560// ----------------------------------------------------------------------------
561
43427087
VZ
562// generate the page changing and changed events, hide the currently active
563// panel and show the new one
88310e2e
VZ
564void wxNotebook::ChangePage(int nOldSel, int nSel)
565{
fd3f686c
VZ
566 // MT-FIXME should use a real semaphore
567 static bool s_bInsideChangePage = FALSE;
568
569 // when we call ProcessEvent(), our own OnSelChange() is called which calls
570 // this function - break the infinite loop
571 if ( s_bInsideChangePage )
572 return;
573
aaab7c01
VZ
574 // it's not an error (the message may be generated by the tab control itself)
575 // and it may happen - just do nothing
576 if ( nSel == nOldSel )
577 return;
88310e2e 578
fd3f686c
VZ
579 s_bInsideChangePage = TRUE;
580
581 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
582 event.SetSelection(nSel);
583 event.SetOldSelection(nOldSel);
584 event.SetEventObject(this);
585 if ( ProcessEvent(event) && !event.IsAllowed() )
586 {
587 // program doesn't allow the page change
588 s_bInsideChangePage = FALSE;
589 return;
590 }
591
fd3f686c
VZ
592 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
593 ProcessEvent(event);
594
fd3f686c 595 s_bInsideChangePage = FALSE;
88310e2e 596}