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