]> git.saurik.com Git - wxWidgets.git/blob - src/msw/notebook.cpp
Revised Motif todo list
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "notebook.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 // wxWindows
31 #ifndef WX_PRECOMP
32 #include <wx/string.h>
33 #endif // WX_PRECOMP
34
35 #include <wx/log.h>
36 #include <wx/imaglist.h>
37 #include <wx/notebook.h>
38
39 #include <wx/msw/private.h>
40
41 // Windows standard headers
42 #ifndef __WIN95__
43 #error "wxNotebook is not supported under Windows 3.1"
44 #endif //Win95
45
46 #include <windowsx.h> // for SetWindowFont
47
48 #ifdef __GNUWIN32__
49 #include "wx/msw/gnuwin32/extra.h"
50 #else //!GnuWin32
51 #include <commctrl.h>
52 #endif
53
54 // ----------------------------------------------------------------------------
55 // macros
56 // ----------------------------------------------------------------------------
57
58 // check that the page index is valid
59 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
60
61 // hide the ugly cast
62 #define m_hwnd (HWND)GetHWND()
63
64 // ----------------------------------------------------------------------------
65 // event table
66 // ----------------------------------------------------------------------------
67
68 #if !USE_SHARED_LIBRARIES
69 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
70 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
71
72 EVT_SIZE(wxNotebook::OnSize)
73 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
74 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
75 END_EVENT_TABLE()
76
77 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
79 #endif
80
81 // ============================================================================
82 // implementation
83 // ============================================================================
84
85 // ----------------------------------------------------------------------------
86 // wxNotebook construction
87 // ----------------------------------------------------------------------------
88
89 // common part of all ctors
90 void wxNotebook::Init()
91 {
92 m_pImageList = NULL;
93 m_nSelection = -1;
94 }
95
96 // default for dynamic class
97 wxNotebook::wxNotebook()
98 {
99 Init();
100 }
101
102 // the same arguments as for wxControl
103 wxNotebook::wxNotebook(wxWindow *parent,
104 wxWindowID id,
105 const wxPoint& pos,
106 const wxSize& size,
107 long style,
108 const wxString& name)
109 {
110 Init();
111
112 Create(parent, id, pos, size, style, name);
113 }
114
115 // Create() function
116 bool wxNotebook::Create(wxWindow *parent,
117 wxWindowID id,
118 const wxPoint& pos,
119 const wxSize& size,
120 long style,
121 const wxString& name)
122 {
123 // base init
124 SetName(name);
125 SetParent(parent);
126
127 m_windowId = id == -1 ? NewControlId() : id;
128
129 // colors and font
130 m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
131 m_foregroundColour = *wxBLACK ;
132
133 // style
134 m_windowStyle = style | wxTAB_TRAVERSAL;
135
136 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
137 if ( m_windowStyle & wxTC_MULTILINE )
138 tabStyle |= TCS_MULTILINE;
139 if ( m_windowStyle & wxBORDER )
140 tabStyle &= WS_BORDER;
141
142 // create the tab control.
143 m_hWnd = (WXHWND)CreateWindowEx
144 (
145 0, // extended style
146 WC_TABCONTROL, // class name for the tab control
147 "", // no caption
148 tabStyle, // style
149 pos.x, pos.y, size.x, size.y, // size and position
150 (HWND)parent->GetHWND(), // parent window
151 (HMENU)m_windowId, // child id
152 wxGetInstance(), // current instance
153 NULL // no class data
154 );
155
156 if ( m_hWnd == 0 ) {
157 wxLogSysError("Can't create the notebook control");
158 return FALSE;
159 }
160
161 // Not all compilers recognise SetWindowFont
162 // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE);
163 ::SendMessage((HWND) m_hwnd, WM_SETFONT,
164 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT),TRUE);
165
166
167 if ( parent != NULL )
168 parent->AddChild(this);
169
170 SubclassWin(m_hWnd);
171
172 return TRUE;
173 }
174
175 // dtor
176 wxNotebook::~wxNotebook()
177 {
178 }
179
180 // ----------------------------------------------------------------------------
181 // wxNotebook accessors
182 // ----------------------------------------------------------------------------
183 int wxNotebook::GetPageCount() const
184 {
185 // consistency check
186 wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) );
187
188 return m_aPages.Count();
189 }
190
191 int wxNotebook::GetRowCount() const
192 {
193 return TabCtrl_GetRowCount(m_hwnd);
194 }
195
196 int wxNotebook::SetSelection(int nPage)
197 {
198 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "notebook page out of range" );
199
200 ChangePage(m_nSelection, nPage);
201
202 return TabCtrl_SetCurSel(m_hwnd, nPage);
203 }
204
205 void wxNotebook::AdvanceSelection(bool bForward)
206 {
207 int nSel = GetSelection();
208 int nMax = GetPageCount() - 1;
209 if ( bForward )
210 SetSelection(nSel == nMax ? 0 : nSel + 1);
211 else
212 SetSelection(nSel == 0 ? nMax : nSel - 1);
213 }
214
215 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
216 {
217 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
218
219 TC_ITEM tcItem;
220 tcItem.mask = TCIF_TEXT;
221 tcItem.pszText = (char *)strText.c_str();
222
223 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
224 }
225
226 wxString wxNotebook::GetPageText(int nPage) const
227 {
228 wxCHECK_MSG( IS_VALID_PAGE(nPage), "", "notebook page out of range" );
229
230 char buf[256];
231 TC_ITEM tcItem;
232 tcItem.mask = TCIF_TEXT;
233 tcItem.pszText = buf;
234 tcItem.cchTextMax = WXSIZEOF(buf);
235
236 wxString str;
237 if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) )
238 str = tcItem.pszText;
239
240 return str;
241 }
242
243 int wxNotebook::GetPageImage(int nPage) const
244 {
245 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "notebook page out of range" );
246
247 TC_ITEM tcItem;
248 tcItem.mask = TCIF_IMAGE;
249
250 return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1;
251 }
252
253 bool wxNotebook::SetPageImage(int nPage, int nImage)
254 {
255 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
256
257 TC_ITEM tcItem;
258 tcItem.mask = TCIF_IMAGE;
259 tcItem.iImage = nImage;
260
261 return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0;
262 }
263
264 void wxNotebook::SetImageList(wxImageList* imageList)
265 {
266 m_pImageList = imageList;
267 TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST());
268 }
269
270 // ----------------------------------------------------------------------------
271 // wxNotebook operations
272 // ----------------------------------------------------------------------------
273
274 // remove one page from the notebook
275 bool wxNotebook::DeletePage(int nPage)
276 {
277 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" );
278
279 TabCtrl_DeleteItem(m_hwnd, nPage);
280
281 delete m_aPages[nPage];
282 m_aPages.Remove(nPage);
283
284 return TRUE;
285 }
286
287 // remove all pages
288 bool wxNotebook::DeleteAllPages()
289 {
290 TabCtrl_DeleteAllItems(m_hwnd);
291
292 int nPageCount = GetPageCount();
293 int nPage;
294 for ( nPage = 0; nPage < nPageCount; nPage++ )
295 delete m_aPages[nPage];
296
297 m_aPages.Clear();
298
299 return TRUE;
300 }
301
302 // add a page to the notebook
303 bool wxNotebook::AddPage(wxNotebookPage *pPage,
304 const wxString& strText,
305 bool bSelect,
306 int imageId)
307 {
308 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
309 }
310
311 // same as AddPage() but does it at given position
312 bool wxNotebook::InsertPage(int nPage,
313 wxNotebookPage *pPage,
314 const wxString& strText,
315 bool bSelect,
316 int imageId)
317 {
318 wxASSERT( pPage != NULL );
319 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
320
321 // add the tab to the control
322 TC_ITEM tcItem;
323 tcItem.mask = TCIF_TEXT | TCIF_IMAGE;
324 tcItem.pszText = (char *)strText.c_str();
325 tcItem.iImage = imageId;
326
327 if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) {
328 wxLogError("Can't create the notebook page '%s'.", strText.c_str());
329 return FALSE;
330 }
331
332 // save the pointer to the page
333 m_aPages.Insert(pPage, nPage);
334
335 // some page must be selected: either this one or the first one if there is
336 // still no selection
337 if ( bSelect )
338 m_nSelection = nPage;
339 else if ( m_nSelection == -1 )
340 m_nSelection = 0;
341
342 // don't show pages by default (we'll need to adjust their size first)
343 HWND hwnd = (HWND)pPage->GetHWND();
344 SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE);
345
346 return TRUE;
347 }
348
349 // ----------------------------------------------------------------------------
350 // wxNotebook callbacks
351 // ----------------------------------------------------------------------------
352
353 void wxNotebook::OnSize(wxSizeEvent& event)
354 {
355 // emulate page change (it's esp. important to do it first time because
356 // otherwise our page would stay invisible)
357 int nSel = m_nSelection;
358 m_nSelection = -1;
359 SetSelection(nSel);
360
361 // fit the notebook page to the tab control's display area
362 RECT rc;
363 rc.left = rc.top = 0;
364 GetSize((int *)&rc.right, (int *)&rc.bottom);
365
366 TabCtrl_AdjustRect(m_hwnd, FALSE, &rc);
367 size_t nCount = m_aPages.Count();
368 for ( size_t nPage = 0; nPage < nCount; nPage++ ) {
369 wxNotebookPage *pPage = m_aPages[nPage];
370 pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
371 if ( pPage->GetAutoLayout() )
372 pPage->Layout();
373 }
374
375 event.Skip();
376 }
377
378 void wxNotebook::OnSelChange(wxNotebookEvent& event)
379 {
380 // is it our tab control?
381 if ( event.GetEventObject() == this )
382 ChangePage(event.GetOldSelection(), event.GetSelection());
383
384 // we want to give others a chance to process this message as well
385 event.Skip();
386 }
387
388 void wxNotebook::OnSetFocus(wxFocusEvent& event)
389 {
390 // set focus to the currently selected page if any
391 if ( m_nSelection != -1 )
392 m_aPages[m_nSelection]->SetFocus();
393
394 event.Skip();
395 }
396
397 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
398 {
399 if ( event.IsWindowChange() ) {
400 // change pages
401 AdvanceSelection(event.GetDirection());
402 }
403 else {
404 // pass to the parent
405 if ( GetParent() ) {
406 event.SetCurrentFocus(this);
407 GetParent()->ProcessEvent(event);
408 }
409 }
410 }
411
412 // ----------------------------------------------------------------------------
413 // wxNotebook base class virtuals
414 // ----------------------------------------------------------------------------
415
416 // override these 2 functions to do nothing: everything is done in OnSize
417
418 void wxNotebook::SetConstraintSizes(bool /* recurse */)
419 {
420 // don't set the sizes of the pages - their correct size is not yet known
421 wxControl::SetConstraintSizes(FALSE);
422 }
423
424 bool wxNotebook::DoPhase(int /* nPhase */)
425 {
426 return TRUE;
427 }
428
429 void wxNotebook::Command(wxCommandEvent& event)
430 {
431 wxFAIL_MSG("wxNotebook::Command not implemented");
432 }
433
434 bool wxNotebook::MSWNotify(WXWPARAM wParam, WXLPARAM lParam)
435 {
436 wxNotebookEvent event(wxEVT_NULL, m_windowId);
437
438 NMHDR* hdr = (NMHDR *)lParam;
439 switch ( hdr->code ) {
440 case TCN_SELCHANGE:
441 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
442 break;
443
444 case TCN_SELCHANGING:
445 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
446 break;
447
448 // prevent this msg from being passed to wxControl::MSWNotify which would
449 // retrun FALSE disabling the change of page
450 case UDN_DELTAPOS:
451 return TRUE;
452
453 default :
454 return wxControl::MSWNotify(wParam, lParam);
455 }
456
457 event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
458 event.SetOldSelection(m_nSelection);
459 event.SetEventObject(this);
460 event.SetInt(LOWORD(wParam));
461
462 return ProcessEvent(event);
463 }
464
465 // ----------------------------------------------------------------------------
466 // wxNotebook helper functions
467 // ----------------------------------------------------------------------------
468
469 // hide the currently active panel and show the new one
470 void wxNotebook::ChangePage(int nOldSel, int nSel)
471 {
472 // it's not an error (the message may be generated by the tab control itself)
473 // and it may happen - just do nothing
474 if ( nSel == nOldSel )
475 return;
476
477 if ( nOldSel != -1 )
478 m_aPages[nOldSel]->Show(FALSE);
479
480 wxNotebookPage *pPage = m_aPages[nSel];
481 pPage->Show(TRUE);
482 pPage->SetFocus();
483
484 m_nSelection = nSel;
485 }