]>
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) | |
72 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) | |
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 | ||
281 | // remove all pages | |
282 | bool wxNotebook::DeleteAllPages() | |
283 | { | |
284 | TabCtrl_DeleteAllItems(m_hwnd); | |
285 | ||
286 | int nPageCount = GetPageCount(); | |
287 | int nPage; | |
288 | for ( nPage = 0; nPage < nPageCount; nPage++ ) | |
289 | delete m_aPages[nPage]; | |
290 | ||
291 | m_aPages.Clear(); | |
292 | ||
293 | return TRUE; | |
294 | } | |
295 | ||
296 | // add a page to the notebook | |
297 | bool wxNotebook::AddPage(wxNotebookPage *pPage, | |
298 | const wxString& strText, | |
299 | bool bSelect, | |
300 | int imageId) | |
301 | { | |
302 | return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId); | |
303 | } | |
304 | ||
305 | // same as AddPage() but does it at given position | |
306 | bool wxNotebook::InsertPage(int nPage, | |
307 | wxNotebookPage *pPage, | |
308 | const wxString& strText, | |
309 | bool bSelect, | |
310 | int imageId) | |
311 | { | |
312 | wxASSERT( pPage != NULL ); | |
313 | wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE ); | |
314 | ||
315 | // add the tab to the control | |
316 | TC_ITEM tcItem; | |
317 | tcItem.mask = TCIF_TEXT | TCIF_IMAGE; | |
318 | tcItem.pszText = (char *)strText.c_str(); | |
319 | tcItem.iImage = imageId; | |
320 | ||
321 | if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) { | |
322 | wxLogError("Can't create the notebook page '%s'.", strText.c_str()); | |
323 | return FALSE; | |
324 | } | |
325 | ||
326 | // save the pointer to the page | |
327 | m_aPages.Insert(pPage, nPage); | |
328 | ||
329 | // some page must be selected: either this one or the first one if there is | |
330 | // still no selection | |
331 | if ( bSelect ) | |
332 | m_nSelection = nPage; | |
333 | else if ( m_nSelection == -1 ) | |
334 | m_nSelection = 0; | |
335 | ||
336 | // don't show pages by default (we'll need to adjust their size first) | |
337 | HWND hwnd = (HWND)pPage->GetHWND(); | |
338 | SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE); | |
339 | ||
340 | return TRUE; | |
341 | } | |
342 | ||
343 | // ---------------------------------------------------------------------------- | |
344 | // wxNotebook callbacks | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
88310e2e VZ |
347 | void wxNotebook::OnSize(wxSizeEvent& event) |
348 | { | |
88310e2e VZ |
349 | // emulate page change (it's esp. important to do it first time because |
350 | // otherwise our page would stay invisible) | |
351 | int nSel = m_nSelection; | |
352 | m_nSelection = -1; | |
353 | SetSelection(nSel); | |
354 | ||
b5c3b538 VZ |
355 | // fit the notebook page to the tab control's display area |
356 | RECT rc; | |
357 | rc.left = rc.top = 0; | |
358 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
359 | ||
360 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
c86f1403 VZ |
361 | size_t nCount = m_aPages.Count(); |
362 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) { | |
b5c3b538 VZ |
363 | wxNotebookPage *pPage = m_aPages[nPage]; |
364 | pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); | |
365 | if ( pPage->GetAutoLayout() ) | |
366 | pPage->Layout(); | |
367 | } | |
368 | ||
88310e2e VZ |
369 | event.Skip(); |
370 | } | |
371 | ||
372 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
373 | { | |
374 | // is it our tab control? | |
375 | if ( event.GetEventObject() == this ) | |
376 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
377 | ||
378 | // we want to give others a chance to process this message as well | |
379 | event.Skip(); | |
380 | } | |
381 | ||
382 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
383 | { | |
384 | // set focus to the currently selected page if any | |
385 | if ( m_nSelection != -1 ) | |
386 | m_aPages[m_nSelection]->SetFocus(); | |
387 | ||
388 | event.Skip(); | |
389 | } | |
390 | ||
391 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
392 | { | |
393 | if ( event.IsWindowChange() ) { | |
394 | // change pages | |
395 | AdvanceSelection(event.GetDirection()); | |
396 | } | |
397 | else { | |
398 | // pass to the parent | |
399 | if ( GetParent() ) { | |
400 | event.SetCurrentFocus(this); | |
02800301 | 401 | GetParent()->GetEventHandler()->ProcessEvent(event); |
88310e2e VZ |
402 | } |
403 | } | |
404 | } | |
405 | ||
406 | // ---------------------------------------------------------------------------- | |
407 | // wxNotebook base class virtuals | |
408 | // ---------------------------------------------------------------------------- | |
b5c3b538 VZ |
409 | |
410 | // override these 2 functions to do nothing: everything is done in OnSize | |
411 | ||
412 | void wxNotebook::SetConstraintSizes(bool /* recurse */) | |
413 | { | |
414 | // don't set the sizes of the pages - their correct size is not yet known | |
415 | wxControl::SetConstraintSizes(FALSE); | |
416 | } | |
417 | ||
418 | bool wxNotebook::DoPhase(int /* nPhase */) | |
419 | { | |
420 | return TRUE; | |
421 | } | |
422 | ||
88310e2e VZ |
423 | void wxNotebook::Command(wxCommandEvent& event) |
424 | { | |
425 | wxFAIL_MSG("wxNotebook::Command not implemented"); | |
426 | } | |
427 | ||
8b9518ee | 428 | bool wxNotebook::MSWNotify(WXWPARAM wParam, WXLPARAM lParam) |
88310e2e | 429 | { |
93a19f17 | 430 | wxNotebookEvent event(wxEVT_NULL, m_windowId); |
88310e2e VZ |
431 | |
432 | NMHDR* hdr = (NMHDR *)lParam; | |
433 | switch ( hdr->code ) { | |
434 | case TCN_SELCHANGE: | |
435 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
436 | break; | |
437 | ||
438 | case TCN_SELCHANGING: | |
439 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING); | |
440 | break; | |
441 | ||
93a19f17 VZ |
442 | // prevent this msg from being passed to wxControl::MSWNotify which would |
443 | // retrun FALSE disabling the change of page | |
aaab7c01 | 444 | case UDN_DELTAPOS: |
aaab7c01 VZ |
445 | return TRUE; |
446 | ||
88310e2e VZ |
447 | default : |
448 | return wxControl::MSWNotify(wParam, lParam); | |
449 | } | |
450 | ||
93a19f17 VZ |
451 | event.SetSelection(TabCtrl_GetCurSel(m_hwnd)); |
452 | event.SetOldSelection(m_nSelection); | |
88310e2e VZ |
453 | event.SetEventObject(this); |
454 | event.SetInt(LOWORD(wParam)); | |
455 | ||
02800301 | 456 | return GetEventHandler()->ProcessEvent(event); |
88310e2e VZ |
457 | } |
458 | ||
459 | // ---------------------------------------------------------------------------- | |
460 | // wxNotebook helper functions | |
461 | // ---------------------------------------------------------------------------- | |
462 | ||
463 | // hide the currently active panel and show the new one | |
464 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
465 | { | |
aaab7c01 VZ |
466 | // it's not an error (the message may be generated by the tab control itself) |
467 | // and it may happen - just do nothing | |
468 | if ( nSel == nOldSel ) | |
469 | return; | |
88310e2e | 470 | |
aaab7c01 | 471 | if ( nOldSel != -1 ) |
88310e2e | 472 | m_aPages[nOldSel]->Show(FALSE); |
88310e2e VZ |
473 | |
474 | wxNotebookPage *pPage = m_aPages[nSel]; | |
88310e2e | 475 | pPage->Show(TRUE); |
b5c3b538 | 476 | pPage->SetFocus(); |
88310e2e VZ |
477 | |
478 | m_nSelection = nSel; | |
479 | } | |
a7e594b2 JS |
480 | |
481 | void wxNotebook::OnEraseBackground(wxEraseEvent& event) | |
482 | { | |
483 | Default(); | |
484 | } | |
485 |