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