]>
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 | ||
12 | // TODO: | |
13 | // 1) keyboard interface for changing pages ([Shift]+Ctrl-Tab) | |
14 | // 2) using OnSize() for showing pages for the first time works, but it surely | |
15 | // us ugly | |
16 | // 3) I'm not sure that setting fonts works | |
17 | ||
18 | // ============================================================================ | |
19 | // declarations | |
20 | // ============================================================================ | |
21 | ||
22 | // ---------------------------------------------------------------------------- | |
23 | // headers | |
24 | // ---------------------------------------------------------------------------- | |
25 | #ifdef __GNUG__ | |
26 | #pragma implementation "notebook.h" | |
27 | #endif | |
28 | ||
29 | // For compilers that support precompilation, includes "wx.h". | |
30 | #include "wx/wxprec.h" | |
31 | ||
32 | #ifdef __BORLANDC__ | |
33 | #pragma hdrstop | |
34 | #endif | |
35 | ||
36 | // wxWindows | |
37 | #ifndef WX_PRECOMP | |
38 | #include <wx/string.h> | |
39 | #endif // WX_PRECOMP | |
40 | ||
41 | #include <wx/log.h> | |
42 | #include <wx/imaglist.h> | |
43 | #include <wx/notebook.h> | |
44 | ||
45 | #include <wx/msw/private.h> | |
46 | ||
47 | // Windows standard headers | |
48 | #ifndef __WIN95__ | |
49 | #error "wxNotebook is not supported under Windows 3.1" | |
50 | #endif //Win95 | |
51 | ||
52 | #ifdef __GNUWIN32__ | |
53 | #include "wx/msw/gnuwin32/extra.h" | |
54 | #else //!GnuWin32 | |
55 | #include <commctrl.h> | |
56 | #endif | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // macros | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // check that the page index is valid | |
63 | #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount())) | |
64 | ||
65 | // hide the ugly cast | |
66 | #define m_hwnd (HWND)GetHWND() | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // event table | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | #if !USE_SHARED_LIBRARIES | |
73 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
74 | EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) | |
75 | ||
76 | EVT_SIZE(wxNotebook::OnSize) | |
77 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) | |
78 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
79 | END_EVENT_TABLE() | |
80 | ||
81 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) | |
82 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) | |
83 | #endif | |
84 | ||
85 | // ============================================================================ | |
86 | // implementation | |
87 | // ============================================================================ | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // wxNotebook construction | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // common part of all ctors | |
94 | void wxNotebook::Init() | |
95 | { | |
96 | m_pImageList = NULL; | |
97 | m_nSelection = -1; | |
98 | } | |
99 | ||
100 | // default for dynamic class | |
101 | wxNotebook::wxNotebook() | |
102 | { | |
103 | Init(); | |
104 | } | |
105 | ||
106 | // the same arguments as for wxControl | |
107 | wxNotebook::wxNotebook(wxWindow *parent, | |
8b9518ee | 108 | wxWindowID id, |
88310e2e VZ |
109 | const wxPoint& pos, |
110 | const wxSize& size, | |
8b9518ee | 111 | long style, |
88310e2e VZ |
112 | const wxString& name) |
113 | { | |
114 | Init(); | |
115 | ||
116 | Create(parent, id, pos, size, style, name); | |
117 | } | |
118 | ||
119 | // Create() function | |
120 | bool wxNotebook::Create(wxWindow *parent, | |
8b9518ee | 121 | wxWindowID id, |
88310e2e VZ |
122 | const wxPoint& pos, |
123 | const wxSize& size, | |
8b9518ee | 124 | long style, |
88310e2e VZ |
125 | const wxString& name) |
126 | { | |
127 | // base init | |
128 | SetName(name); | |
129 | SetParent(parent); | |
130 | ||
131 | m_windowId = id == -1 ? NewControlId() : id; | |
132 | ||
133 | // colors and font | |
134 | m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE)); | |
135 | m_foregroundColour = *wxBLACK ; | |
136 | ||
137 | m_defaultForegroundColour = *wxBLACK ; | |
138 | m_defaultBackgroundColour = wxColour(GetSysColor(COLOR_BTNFACE)); | |
139 | ||
140 | // style | |
141 | m_windowStyle = style; | |
142 | ||
143 | long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS; | |
144 | if ( m_windowStyle & wxTC_MULTILINE ) | |
145 | tabStyle |= TCS_MULTILINE; | |
146 | if ( m_windowStyle & wxBORDER ) | |
147 | tabStyle &= WS_BORDER; | |
148 | ||
149 | // create the tab control. | |
150 | m_hWnd = (WXHWND)CreateWindowEx | |
151 | ( | |
152 | 0, // extended style | |
153 | WC_TABCONTROL, // class name for the tab control | |
154 | "", // no caption | |
155 | tabStyle, // style | |
156 | pos.x, pos.y, size.x, size.y, // size and position | |
157 | (HWND)parent->GetHWND(), // parent window | |
158 | (HMENU)m_windowId, // child id | |
159 | wxGetInstance(), // current instance | |
160 | NULL // no class data | |
161 | ); | |
162 | ||
163 | if ( m_hWnd == 0 ) { | |
164 | wxLogSysError("Can't create the notebook control"); | |
165 | return FALSE; | |
166 | } | |
167 | ||
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 | ||
354 | // @@@ OnSize() is used for setting the font when it's called for the first | |
355 | // time because doing it in ::Create() doesn't work (for unknown reasons) | |
356 | void wxNotebook::OnSize(wxSizeEvent& event) | |
357 | { | |
358 | static bool s_bFirstTime = TRUE; | |
359 | if ( s_bFirstTime ) { | |
360 | SendMessage((HWND)m_hwnd, WM_SETFONT, | |
361 | (WPARAM)GetStockObject(DEFAULT_GUI_FONT), | |
362 | MAKELPARAM(TRUE, 0)); | |
363 | s_bFirstTime = FALSE; | |
364 | } | |
365 | ||
366 | // emulate page change (it's esp. important to do it first time because | |
367 | // otherwise our page would stay invisible) | |
368 | int nSel = m_nSelection; | |
369 | m_nSelection = -1; | |
370 | SetSelection(nSel); | |
371 | ||
372 | event.Skip(); | |
373 | } | |
374 | ||
375 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
376 | { | |
377 | // is it our tab control? | |
378 | if ( event.GetEventObject() == this ) | |
379 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
380 | ||
381 | // we want to give others a chance to process this message as well | |
382 | event.Skip(); | |
383 | } | |
384 | ||
385 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
386 | { | |
387 | // set focus to the currently selected page if any | |
388 | if ( m_nSelection != -1 ) | |
389 | m_aPages[m_nSelection]->SetFocus(); | |
390 | ||
391 | event.Skip(); | |
392 | } | |
393 | ||
394 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
395 | { | |
396 | if ( event.IsWindowChange() ) { | |
397 | // change pages | |
398 | AdvanceSelection(event.GetDirection()); | |
399 | } | |
400 | else { | |
401 | // pass to the parent | |
402 | if ( GetParent() ) { | |
403 | event.SetCurrentFocus(this); | |
404 | GetParent()->ProcessEvent(event); | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | // ---------------------------------------------------------------------------- | |
410 | // wxNotebook base class virtuals | |
411 | // ---------------------------------------------------------------------------- | |
412 | void wxNotebook::Command(wxCommandEvent& event) | |
413 | { | |
414 | wxFAIL_MSG("wxNotebook::Command not implemented"); | |
415 | } | |
416 | ||
8b9518ee | 417 | bool wxNotebook::MSWNotify(WXWPARAM wParam, WXLPARAM lParam) |
88310e2e VZ |
418 | { |
419 | wxNotebookEvent event(wxEVT_NULL, m_windowId, | |
420 | TabCtrl_GetCurSel(m_hwnd), m_nSelection); | |
421 | ||
422 | NMHDR* hdr = (NMHDR *)lParam; | |
423 | switch ( hdr->code ) { | |
424 | case TCN_SELCHANGE: | |
425 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
426 | break; | |
427 | ||
428 | case TCN_SELCHANGING: | |
429 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING); | |
430 | break; | |
431 | ||
432 | default : | |
433 | return wxControl::MSWNotify(wParam, lParam); | |
434 | } | |
435 | ||
436 | event.SetEventObject(this); | |
437 | event.SetInt(LOWORD(wParam)); | |
438 | ||
439 | return ProcessEvent(event); | |
440 | } | |
441 | ||
442 | // ---------------------------------------------------------------------------- | |
443 | // wxNotebook helper functions | |
444 | // ---------------------------------------------------------------------------- | |
445 | ||
446 | // hide the currently active panel and show the new one | |
447 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
448 | { | |
449 | wxASSERT( nOldSel != nSel ); // impossible | |
450 | ||
451 | if ( nOldSel != -1 ) { | |
452 | m_aPages[nOldSel]->Show(FALSE); | |
453 | } | |
454 | ||
455 | wxNotebookPage *pPage = m_aPages[nSel]; | |
456 | FitPage(pPage); | |
457 | pPage->Show(TRUE); | |
458 | ||
459 | // set focus to the currently selected page | |
460 | wxWindow *win = m_aPages[nSel]; | |
461 | if ( win->IsKindOf(CLASSINFO(wxPanel)) ) { | |
462 | wxList *children = win->GetChildren(); | |
463 | if ( children->First() != NULL ); | |
464 | win = (wxWindow *)children->First()->Data(); | |
465 | } | |
466 | win->SetFocus(); | |
467 | ||
468 | m_nSelection = nSel; | |
469 | } | |
470 | ||
471 | // fit the notebook page to the tab control's display area | |
472 | void wxNotebook::FitPage(wxNotebookPage *pPage) | |
473 | { | |
474 | RECT rc; | |
475 | rc.left = rc.top = 0; | |
476 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
477 | ||
478 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
479 | pPage->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); | |
480 | } |