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