]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/msw/gnuwin32/extra.h" | |
46 | #endif | |
47 | #endif | |
48 | ||
49 | #if !defined(__GNUWIN32__) || defined(__TWIN32__) | |
50 | #include <commctrl.h> | |
51 | #endif | |
52 | ||
53 | // ---------------------------------------------------------------------------- | |
54 | // macros | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | // check that the page index is valid | |
58 | #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount())) | |
59 | ||
60 | // hide the ugly cast | |
61 | #define m_hwnd (HWND)GetHWND() | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // event table | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | #if !USE_SHARED_LIBRARIES | |
68 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
69 | EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) | |
70 | ||
71 | EVT_SIZE(wxNotebook::OnSize) | |
72 | EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground) | |
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, wxNotifyEvent) | |
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 | ||
138 | if (m_windowStyle & wxCLIP_CHILDREN) | |
139 | tabStyle |= WS_CLIPCHILDREN; | |
140 | if ( m_windowStyle & wxTC_MULTILINE ) | |
141 | tabStyle |= TCS_MULTILINE; | |
142 | if ( m_windowStyle & wxBORDER ) | |
143 | tabStyle &= WS_BORDER; | |
144 | if (m_windowStyle & wxNB_FIXEDWIDTH) | |
145 | tabStyle |= TCS_FIXEDWIDTH ; | |
146 | ||
147 | // create the tab control. | |
148 | m_hWnd = (WXHWND)CreateWindowEx | |
149 | ( | |
150 | 0, // extended style | |
151 | WC_TABCONTROL, // class name for the tab control | |
152 | "", // no caption | |
153 | tabStyle, // style | |
154 | pos.x, pos.y, size.x, size.y, // size and position | |
155 | (HWND)parent->GetHWND(), // parent window | |
156 | (HMENU)m_windowId, // child id | |
157 | wxGetInstance(), // current instance | |
158 | NULL // no class data | |
159 | ); | |
160 | ||
161 | if ( m_hWnd == 0 ) { | |
162 | wxLogSysError("Can't create the notebook control"); | |
163 | return FALSE; | |
164 | } | |
165 | ||
166 | // Not all compilers recognise SetWindowFont | |
167 | // SetWindowFont((HWND)m_hwnd, ::GetStockObject(DEFAULT_GUI_FONT), FALSE); | |
168 | ::SendMessage((HWND) m_hwnd, WM_SETFONT, | |
169 | (WPARAM)::GetStockObject(DEFAULT_GUI_FONT),TRUE); | |
170 | ||
171 | ||
172 | if ( parent != NULL ) | |
173 | parent->AddChild(this); | |
174 | ||
175 | SubclassWin(m_hWnd); | |
176 | ||
177 | return TRUE; | |
178 | } | |
179 | ||
180 | // dtor | |
181 | wxNotebook::~wxNotebook() | |
182 | { | |
183 | } | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
186 | // wxNotebook accessors | |
187 | // ---------------------------------------------------------------------------- | |
188 | int wxNotebook::GetPageCount() const | |
189 | { | |
190 | // consistency check | |
191 | wxASSERT( (int)m_aPages.Count() == TabCtrl_GetItemCount(m_hwnd) ); | |
192 | ||
193 | return m_aPages.Count(); | |
194 | } | |
195 | ||
196 | int wxNotebook::GetRowCount() const | |
197 | { | |
198 | return TabCtrl_GetRowCount(m_hwnd); | |
199 | } | |
200 | ||
201 | int wxNotebook::SetSelection(int nPage) | |
202 | { | |
203 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "notebook page out of range" ); | |
204 | ||
205 | ChangePage(m_nSelection, nPage); | |
206 | ||
207 | return TabCtrl_SetCurSel(m_hwnd, nPage); | |
208 | } | |
209 | ||
210 | void wxNotebook::AdvanceSelection(bool bForward) | |
211 | { | |
212 | int nSel = GetSelection(); | |
213 | int nMax = GetPageCount() - 1; | |
214 | if ( bForward ) | |
215 | SetSelection(nSel == nMax ? 0 : nSel + 1); | |
216 | else | |
217 | SetSelection(nSel == 0 ? nMax : nSel - 1); | |
218 | } | |
219 | ||
220 | bool wxNotebook::SetPageText(int nPage, const wxString& strText) | |
221 | { | |
222 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" ); | |
223 | ||
224 | TC_ITEM tcItem; | |
225 | tcItem.mask = TCIF_TEXT; | |
226 | tcItem.pszText = (char *)strText.c_str(); | |
227 | ||
228 | return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0; | |
229 | } | |
230 | ||
231 | wxString wxNotebook::GetPageText(int nPage) const | |
232 | { | |
233 | wxCHECK_MSG( IS_VALID_PAGE(nPage), "", "notebook page out of range" ); | |
234 | ||
235 | char buf[256]; | |
236 | TC_ITEM tcItem; | |
237 | tcItem.mask = TCIF_TEXT; | |
238 | tcItem.pszText = buf; | |
239 | tcItem.cchTextMax = WXSIZEOF(buf); | |
240 | ||
241 | wxString str; | |
242 | if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ) | |
243 | str = tcItem.pszText; | |
244 | ||
245 | return str; | |
246 | } | |
247 | ||
248 | int wxNotebook::GetPageImage(int nPage) const | |
249 | { | |
250 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, "notebook page out of range" ); | |
251 | ||
252 | TC_ITEM tcItem; | |
253 | tcItem.mask = TCIF_IMAGE; | |
254 | ||
255 | return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1; | |
256 | } | |
257 | ||
258 | bool wxNotebook::SetPageImage(int nPage, int nImage) | |
259 | { | |
260 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" ); | |
261 | ||
262 | TC_ITEM tcItem; | |
263 | tcItem.mask = TCIF_IMAGE; | |
264 | tcItem.iImage = nImage; | |
265 | ||
266 | return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0; | |
267 | } | |
268 | ||
269 | void wxNotebook::SetImageList(wxImageList* imageList) | |
270 | { | |
271 | m_pImageList = imageList; | |
272 | TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST()); | |
273 | } | |
274 | ||
275 | // ---------------------------------------------------------------------------- | |
276 | // wxNotebook operations | |
277 | // ---------------------------------------------------------------------------- | |
278 | ||
279 | // remove one page from the notebook | |
280 | bool wxNotebook::DeletePage(int nPage) | |
281 | { | |
282 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" ); | |
283 | ||
284 | TabCtrl_DeleteItem(m_hwnd, nPage); | |
285 | ||
286 | delete m_aPages[nPage]; | |
287 | m_aPages.Remove(nPage); | |
288 | ||
289 | return TRUE; | |
290 | } | |
291 | ||
292 | // remove one page from the notebook, without deleting | |
293 | bool wxNotebook::RemovePage(int nPage) | |
294 | { | |
295 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, "notebook page out of range" ); | |
296 | ||
297 | TabCtrl_DeleteItem(m_hwnd, nPage); | |
298 | ||
299 | m_aPages.Remove(nPage); | |
300 | ||
301 | return TRUE; | |
302 | } | |
303 | ||
304 | // remove all pages | |
305 | bool wxNotebook::DeleteAllPages() | |
306 | { | |
307 | TabCtrl_DeleteAllItems(m_hwnd); | |
308 | ||
309 | int nPageCount = GetPageCount(); | |
310 | int nPage; | |
311 | for ( nPage = 0; nPage < nPageCount; nPage++ ) | |
312 | delete m_aPages[nPage]; | |
313 | ||
314 | m_aPages.Clear(); | |
315 | ||
316 | return TRUE; | |
317 | } | |
318 | ||
319 | // add a page to the notebook | |
320 | bool wxNotebook::AddPage(wxNotebookPage *pPage, | |
321 | const wxString& strText, | |
322 | bool bSelect, | |
323 | int imageId) | |
324 | { | |
325 | return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId); | |
326 | } | |
327 | ||
328 | // same as AddPage() but does it at given position | |
329 | bool wxNotebook::InsertPage(int nPage, | |
330 | wxNotebookPage *pPage, | |
331 | const wxString& strText, | |
332 | bool bSelect, | |
333 | int imageId) | |
334 | { | |
335 | wxASSERT( pPage != NULL ); | |
336 | wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE ); | |
337 | ||
338 | // add the tab to the control | |
339 | TC_ITEM tcItem; | |
340 | tcItem.mask = 0; | |
341 | ||
342 | if (imageId != -1) | |
343 | { | |
344 | tcItem.mask |= TCIF_IMAGE; | |
345 | tcItem.iImage = imageId; | |
346 | } | |
347 | else | |
348 | tcItem.iImage = 0; | |
349 | ||
350 | if (!strText.IsEmpty()) | |
351 | { | |
352 | tcItem.mask |= TCIF_TEXT; | |
353 | tcItem.pszText = (char *)strText.c_str(); | |
354 | } | |
355 | else | |
356 | tcItem.pszText = (char *) NULL; | |
357 | ||
358 | if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) { | |
359 | wxLogError("Can't create the notebook page '%s'.", strText.c_str()); | |
360 | return FALSE; | |
361 | } | |
362 | ||
363 | // save the pointer to the page | |
364 | m_aPages.Insert(pPage, nPage); | |
365 | ||
366 | // some page must be selected: either this one or the first one if there is | |
367 | // still no selection | |
368 | if ( bSelect ) | |
369 | m_nSelection = nPage; | |
370 | else if ( m_nSelection == -1 ) | |
371 | m_nSelection = 0; | |
372 | ||
373 | // don't show pages by default (we'll need to adjust their size first) | |
374 | HWND hwnd = (HWND)pPage->GetHWND(); | |
375 | SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE); | |
376 | ||
377 | return TRUE; | |
378 | } | |
379 | ||
380 | // ---------------------------------------------------------------------------- | |
381 | // wxNotebook callbacks | |
382 | // ---------------------------------------------------------------------------- | |
383 | ||
384 | void wxNotebook::OnSize(wxSizeEvent& event) | |
385 | { | |
386 | // make sure the current page is shown and has focus (it's useful because all | |
387 | // pages are created invisible initially) | |
388 | if ( m_nSelection != -1 ) { | |
389 | wxNotebookPage *pPage = m_aPages[m_nSelection]; | |
390 | pPage->Show(TRUE); | |
391 | pPage->SetFocus(); | |
392 | } | |
393 | ||
394 | // fit the notebook page to the tab control's display area | |
395 | RECT rc; | |
396 | rc.left = rc.top = 0; | |
397 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
398 | ||
399 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
400 | size_t nCount = m_aPages.Count(); | |
401 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) { | |
402 | wxNotebookPage *pPage = m_aPages[nPage]; | |
403 | pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); | |
404 | if ( pPage->GetAutoLayout() ) | |
405 | pPage->Layout(); | |
406 | } | |
407 | ||
408 | event.Skip(); | |
409 | } | |
410 | ||
411 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
412 | { | |
413 | // is it our tab control? | |
414 | if ( event.GetEventObject() == this ) | |
415 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
416 | ||
417 | // we want to give others a chance to process this message as well | |
418 | event.Skip(); | |
419 | } | |
420 | ||
421 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
422 | { | |
423 | // set focus to the currently selected page if any | |
424 | if ( m_nSelection != -1 ) | |
425 | m_aPages[m_nSelection]->SetFocus(); | |
426 | ||
427 | event.Skip(); | |
428 | } | |
429 | ||
430 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
431 | { | |
432 | if ( event.IsWindowChange() ) { | |
433 | // change pages | |
434 | AdvanceSelection(event.GetDirection()); | |
435 | } | |
436 | else { | |
437 | // pass to the parent | |
438 | if ( GetParent() ) { | |
439 | event.SetCurrentFocus(this); | |
440 | GetParent()->GetEventHandler()->ProcessEvent(event); | |
441 | } | |
442 | } | |
443 | } | |
444 | ||
445 | // ---------------------------------------------------------------------------- | |
446 | // wxNotebook base class virtuals | |
447 | // ---------------------------------------------------------------------------- | |
448 | ||
449 | // override these 2 functions to do nothing: everything is done in OnSize | |
450 | ||
451 | void wxNotebook::SetConstraintSizes(bool /* recurse */) | |
452 | { | |
453 | // don't set the sizes of the pages - their correct size is not yet known | |
454 | wxControl::SetConstraintSizes(FALSE); | |
455 | } | |
456 | ||
457 | bool wxNotebook::DoPhase(int /* nPhase */) | |
458 | { | |
459 | return TRUE; | |
460 | } | |
461 | ||
462 | void wxNotebook::Command(wxCommandEvent& event) | |
463 | { | |
464 | wxFAIL_MSG("wxNotebook::Command not implemented"); | |
465 | } | |
466 | ||
467 | bool wxNotebook::MSWNotify(WXWPARAM wParam, WXLPARAM lParam, WXLPARAM* result) | |
468 | { | |
469 | wxNotebookEvent event(wxEVT_NULL, m_windowId); | |
470 | ||
471 | NMHDR* hdr = (NMHDR *)lParam; | |
472 | switch ( hdr->code ) { | |
473 | case TCN_SELCHANGE: | |
474 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
475 | break; | |
476 | ||
477 | case TCN_SELCHANGING: | |
478 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING); | |
479 | break; | |
480 | ||
481 | default: | |
482 | return wxControl::MSWNotify(wParam, lParam, result); | |
483 | } | |
484 | ||
485 | event.SetSelection(TabCtrl_GetCurSel(m_hwnd)); | |
486 | event.SetOldSelection(m_nSelection); | |
487 | event.SetEventObject(this); | |
488 | event.SetInt(LOWORD(wParam)); // ctrl id | |
489 | ||
490 | bool processed = GetEventHandler()->ProcessEvent(event); | |
491 | *result = !event.IsAllowed(); | |
492 | return processed; | |
493 | } | |
494 | ||
495 | // ---------------------------------------------------------------------------- | |
496 | // wxNotebook helper functions | |
497 | // ---------------------------------------------------------------------------- | |
498 | ||
499 | // hide the currently active panel and show the new one | |
500 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
501 | { | |
502 | // MT-FIXME should use a real semaphore | |
503 | static bool s_bInsideChangePage = FALSE; | |
504 | ||
505 | // when we call ProcessEvent(), our own OnSelChange() is called which calls | |
506 | // this function - break the infinite loop | |
507 | if ( s_bInsideChangePage ) | |
508 | return; | |
509 | ||
510 | // it's not an error (the message may be generated by the tab control itself) | |
511 | // and it may happen - just do nothing | |
512 | if ( nSel == nOldSel ) | |
513 | return; | |
514 | ||
515 | s_bInsideChangePage = TRUE; | |
516 | ||
517 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId); | |
518 | event.SetSelection(nSel); | |
519 | event.SetOldSelection(nOldSel); | |
520 | event.SetEventObject(this); | |
521 | if ( ProcessEvent(event) && !event.IsAllowed() ) | |
522 | { | |
523 | // program doesn't allow the page change | |
524 | s_bInsideChangePage = FALSE; | |
525 | return; | |
526 | } | |
527 | ||
528 | if ( nOldSel != -1 ) | |
529 | m_aPages[nOldSel]->Show(FALSE); | |
530 | ||
531 | wxNotebookPage *pPage = m_aPages[nSel]; | |
532 | pPage->Show(TRUE); | |
533 | pPage->SetFocus(); | |
534 | ||
535 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
536 | ProcessEvent(event); | |
537 | ||
538 | m_nSelection = nSel; | |
539 | s_bInsideChangePage = FALSE; | |
540 | } | |
541 | ||
542 | void wxNotebook::OnEraseBackground(wxEraseEvent& event) | |
543 | { | |
544 | Default(); | |
545 | } | |
546 | ||
547 | // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH | |
548 | // style. | |
549 | void wxNotebook::SetTabSize(const wxSize& sz) | |
550 | { | |
551 | ::SendMessage((HWND) GetHWND(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y)); | |
552 | } | |
553 |