]>
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 licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
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 | #if wxUSE_NOTEBOOK | |
24 | ||
25 | // wxWindows | |
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/string.h" | |
28 | #endif // WX_PRECOMP | |
29 | ||
30 | #include "wx/log.h" | |
31 | #include "wx/imaglist.h" | |
32 | #include "wx/event.h" | |
33 | #include "wx/control.h" | |
34 | #include "wx/notebook.h" | |
35 | #include "wx/app.h" | |
36 | ||
37 | #include "wx/msw/private.h" | |
38 | ||
39 | // Windows standard headers | |
40 | #ifndef __WIN95__ | |
41 | #error "wxNotebook is only supported Windows 95 and above" | |
42 | #endif //Win95 | |
43 | ||
44 | #include <windowsx.h> // for SetWindowFont | |
45 | ||
46 | #ifdef __GNUWIN32_OLD__ | |
47 | #include "wx/msw/gnuwin32/extra.h" | |
48 | #endif | |
49 | ||
50 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) | |
51 | #include <commctrl.h> | |
52 | #endif | |
53 | ||
54 | #include "wx/msw/winundef.h" | |
55 | ||
56 | #if wxUSE_UXTHEME | |
57 | #include "wx/msw/uxtheme.h" | |
58 | ||
59 | #include "wx/radiobut.h" | |
60 | #include "wx/radiobox.h" | |
61 | #include "wx/checkbox.h" | |
62 | #include "wx/bmpbuttn.h" | |
63 | #include "wx/statline.h" | |
64 | #include "wx/statbox.h" | |
65 | #include "wx/stattext.h" | |
66 | #include "wx/slider.h" | |
67 | #include "wx/scrolwin.h" | |
68 | #include "wx/panel.h" | |
69 | #endif | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // macros | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | // check that the page index is valid | |
76 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
77 | ||
78 | // hide the ugly cast | |
79 | #define m_hwnd (HWND)GetHWND() | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // constants | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | // This is a work-around for missing defines in gcc-2.95 headers | |
86 | #ifndef TCS_RIGHT | |
87 | #define TCS_RIGHT 0x0002 | |
88 | #endif | |
89 | ||
90 | #ifndef TCS_VERTICAL | |
91 | #define TCS_VERTICAL 0x0080 | |
92 | #endif | |
93 | ||
94 | #ifndef TCS_BOTTOM | |
95 | #define TCS_BOTTOM TCS_RIGHT | |
96 | #endif | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // event table | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | #include <wx/listimpl.cpp> | |
103 | ||
104 | WX_DEFINE_LIST( wxNotebookPageInfoList ) ; | |
105 | ||
106 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) | |
107 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
108 | ||
109 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
110 | EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) | |
111 | ||
112 | EVT_SIZE(wxNotebook::OnSize) | |
113 | ||
114 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) | |
115 | ||
116 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
117 | END_EVENT_TABLE() | |
118 | ||
119 | #if wxUSE_EXTENDED_RTTI | |
120 | WX_DEFINE_FLAGS( wxNotebookStyle ) | |
121 | ||
122 | wxBEGIN_FLAGS( wxNotebookStyle ) | |
123 | // new style border flags, we put them first to | |
124 | // use them for streaming out | |
125 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
126 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
127 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
128 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
129 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
130 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
131 | ||
132 | // old style border flags | |
133 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
134 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
135 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
136 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
137 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
138 | wxFLAGS_MEMBER(wxNO_BORDER) | |
139 | ||
140 | // standard window styles | |
141 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
142 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
143 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
144 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
145 | wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE) | |
146 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
147 | wxFLAGS_MEMBER(wxVSCROLL) | |
148 | wxFLAGS_MEMBER(wxHSCROLL) | |
149 | ||
150 | wxFLAGS_MEMBER(wxNB_FIXEDWIDTH) | |
151 | wxFLAGS_MEMBER(wxNB_LEFT) | |
152 | wxFLAGS_MEMBER(wxNB_RIGHT) | |
153 | wxFLAGS_MEMBER(wxNB_BOTTOM) | |
154 | ||
155 | wxEND_FLAGS( wxNotebookStyle ) | |
156 | ||
157 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebook, wxControl,"wx/notebook.h") | |
158 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxNotebookPageInfo, wxObject , "wx/notebook.h" ) | |
159 | ||
160 | wxCOLLECTION_TYPE_INFO( wxNotebookPageInfo * , wxNotebookPageInfoList ) ; | |
161 | ||
162 | template<> void wxCollectionToVariantArray( wxNotebookPageInfoList const &theList, wxxVariantArray &value) | |
163 | { | |
164 | wxListCollectionToVariantArray<wxNotebookPageInfoList::compatibility_iterator>( theList , value ) ; | |
165 | } | |
166 | ||
167 | wxBEGIN_PROPERTIES_TABLE(wxNotebook) | |
168 | wxEVENT_PROPERTY( PageChanging , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING , wxNotebookEvent ) | |
169 | wxEVENT_PROPERTY( PageChanged , wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED , wxNotebookEvent ) | |
170 | ||
171 | wxPROPERTY_COLLECTION( PageInfos , wxNotebookPageInfoList , wxNotebookPageInfo* , AddPageInfo , GetPageInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
172 | wxPROPERTY_FLAGS( WindowStyle , wxNotebookStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
173 | wxEND_PROPERTIES_TABLE() | |
174 | ||
175 | wxBEGIN_HANDLERS_TABLE(wxNotebook) | |
176 | wxEND_HANDLERS_TABLE() | |
177 | ||
178 | wxCONSTRUCTOR_5( wxNotebook , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle) | |
179 | ||
180 | ||
181 | wxBEGIN_PROPERTIES_TABLE(wxNotebookPageInfo) | |
182 | wxREADONLY_PROPERTY( Page , wxNotebookPage* , GetPage , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
183 | wxREADONLY_PROPERTY( Text , wxString , GetText , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
184 | wxREADONLY_PROPERTY( Selected , bool , GetSelected , false, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
185 | wxREADONLY_PROPERTY( ImageId , int , GetImageId , -1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
186 | wxEND_PROPERTIES_TABLE() | |
187 | ||
188 | wxBEGIN_HANDLERS_TABLE(wxNotebookPageInfo) | |
189 | wxEND_HANDLERS_TABLE() | |
190 | ||
191 | wxCONSTRUCTOR_4( wxNotebookPageInfo , wxNotebookPage* , Page , wxString , Text , bool , Selected , int , ImageId ) | |
192 | ||
193 | #else | |
194 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) | |
195 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookPageInfo, wxObject ) | |
196 | #endif | |
197 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) | |
198 | ||
199 | // ============================================================================ | |
200 | // implementation | |
201 | // ============================================================================ | |
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // wxNotebook construction | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | const wxNotebookPageInfoList& wxNotebook::GetPageInfos() const | |
208 | { | |
209 | wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ) ; | |
210 | WX_CLEAR_LIST( wxNotebookPageInfoList , *list ) ; | |
211 | for( size_t i = 0 ; i < GetPageCount() ; ++i ) | |
212 | { | |
213 | wxNotebookPageInfo *info = new wxNotebookPageInfo() ; | |
214 | info->Create( const_cast<wxNotebook*>(this)->GetPage(i) , GetPageText(i) , GetSelection() == int(i) , GetPageImage(i) ) ; | |
215 | list->Append( info ) ; | |
216 | } | |
217 | return m_pageInfos ; | |
218 | } | |
219 | ||
220 | // common part of all ctors | |
221 | void wxNotebook::Init() | |
222 | { | |
223 | m_imageList = NULL; | |
224 | m_nSelection = -1; | |
225 | } | |
226 | ||
227 | // default for dynamic class | |
228 | wxNotebook::wxNotebook() | |
229 | { | |
230 | Init(); | |
231 | } | |
232 | ||
233 | // the same arguments as for wxControl | |
234 | wxNotebook::wxNotebook(wxWindow *parent, | |
235 | wxWindowID id, | |
236 | const wxPoint& pos, | |
237 | const wxSize& size, | |
238 | long style, | |
239 | const wxString& name) | |
240 | { | |
241 | Init(); | |
242 | ||
243 | Create(parent, id, pos, size, style, name); | |
244 | } | |
245 | ||
246 | // Create() function | |
247 | bool wxNotebook::Create(wxWindow *parent, | |
248 | wxWindowID id, | |
249 | const wxPoint& pos, | |
250 | const wxSize& size, | |
251 | long style, | |
252 | const wxString& name) | |
253 | { | |
254 | // Does ComCtl32 support non-top tabs? | |
255 | int verComCtl32 = wxApp::GetComCtl32Version(); | |
256 | if ( verComCtl32 < 470 || verComCtl32 >= 600 ) | |
257 | { | |
258 | if (style & wxNB_BOTTOM) | |
259 | style &= ~wxNB_BOTTOM; | |
260 | ||
261 | if (style & wxNB_LEFT) | |
262 | style &= ~wxNB_LEFT; | |
263 | ||
264 | if (style & wxNB_RIGHT) | |
265 | style &= ~wxNB_RIGHT; | |
266 | } | |
267 | ||
268 | if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL, | |
269 | wxDefaultValidator, name) ) | |
270 | return FALSE; | |
271 | ||
272 | if ( !MSWCreateControl(WC_TABCONTROL, wxEmptyString, pos, size) ) | |
273 | return FALSE; | |
274 | ||
275 | SetBackgroundColour(wxColour(::GetSysColor(COLOR_BTNFACE))); | |
276 | ||
277 | return TRUE; | |
278 | } | |
279 | ||
280 | WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const | |
281 | { | |
282 | WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle); | |
283 | ||
284 | tabStyle |= WS_TABSTOP | TCS_TABS; | |
285 | ||
286 | if ( style & wxNB_MULTILINE ) | |
287 | tabStyle |= TCS_MULTILINE; | |
288 | if ( style & wxNB_FIXEDWIDTH ) | |
289 | tabStyle |= TCS_FIXEDWIDTH; | |
290 | ||
291 | if ( style & wxNB_BOTTOM ) | |
292 | tabStyle |= TCS_RIGHT; | |
293 | else if ( style & wxNB_LEFT ) | |
294 | tabStyle |= TCS_VERTICAL; | |
295 | else if ( style & wxNB_RIGHT ) | |
296 | tabStyle |= TCS_VERTICAL | TCS_RIGHT; | |
297 | ||
298 | // ex style | |
299 | if ( exstyle ) | |
300 | { | |
301 | // note that we never want to have the default WS_EX_CLIENTEDGE style | |
302 | // as it looks too ugly for the notebooks | |
303 | *exstyle = 0; | |
304 | } | |
305 | ||
306 | return tabStyle; | |
307 | } | |
308 | ||
309 | // ---------------------------------------------------------------------------- | |
310 | // wxNotebook accessors | |
311 | // ---------------------------------------------------------------------------- | |
312 | ||
313 | size_t wxNotebook::GetPageCount() const | |
314 | { | |
315 | // consistency check | |
316 | wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(m_hwnd) ); | |
317 | ||
318 | return m_pages.Count(); | |
319 | } | |
320 | ||
321 | int wxNotebook::GetRowCount() const | |
322 | { | |
323 | return TabCtrl_GetRowCount(m_hwnd); | |
324 | } | |
325 | ||
326 | int wxNotebook::SetSelection(size_t nPage) | |
327 | { | |
328 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") ); | |
329 | ||
330 | if ( int(nPage) != m_nSelection ) | |
331 | { | |
332 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId); | |
333 | event.SetSelection(nPage); | |
334 | event.SetOldSelection(m_nSelection); | |
335 | event.SetEventObject(this); | |
336 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
337 | { | |
338 | // program allows the page change | |
339 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
340 | (void)GetEventHandler()->ProcessEvent(event); | |
341 | ||
342 | TabCtrl_SetCurSel(m_hwnd, nPage); | |
343 | } | |
344 | } | |
345 | ||
346 | return m_nSelection; | |
347 | } | |
348 | ||
349 | bool wxNotebook::SetPageText(size_t nPage, const wxString& strText) | |
350 | { | |
351 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") ); | |
352 | ||
353 | TC_ITEM tcItem; | |
354 | tcItem.mask = TCIF_TEXT; | |
355 | tcItem.pszText = (wxChar *)strText.c_str(); | |
356 | ||
357 | return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0; | |
358 | } | |
359 | ||
360 | wxString wxNotebook::GetPageText(size_t nPage) const | |
361 | { | |
362 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") ); | |
363 | ||
364 | wxChar buf[256]; | |
365 | TC_ITEM tcItem; | |
366 | tcItem.mask = TCIF_TEXT; | |
367 | tcItem.pszText = buf; | |
368 | tcItem.cchTextMax = WXSIZEOF(buf); | |
369 | ||
370 | wxString str; | |
371 | if ( TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ) | |
372 | str = tcItem.pszText; | |
373 | ||
374 | return str; | |
375 | } | |
376 | ||
377 | int wxNotebook::GetPageImage(size_t nPage) const | |
378 | { | |
379 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") ); | |
380 | ||
381 | TC_ITEM tcItem; | |
382 | tcItem.mask = TCIF_IMAGE; | |
383 | ||
384 | return TabCtrl_GetItem(m_hwnd, nPage, &tcItem) ? tcItem.iImage : -1; | |
385 | } | |
386 | ||
387 | bool wxNotebook::SetPageImage(size_t nPage, int nImage) | |
388 | { | |
389 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") ); | |
390 | ||
391 | TC_ITEM tcItem; | |
392 | tcItem.mask = TCIF_IMAGE; | |
393 | tcItem.iImage = nImage; | |
394 | ||
395 | return TabCtrl_SetItem(m_hwnd, nPage, &tcItem) != 0; | |
396 | } | |
397 | ||
398 | void wxNotebook::SetImageList(wxImageList* imageList) | |
399 | { | |
400 | wxNotebookBase::SetImageList(imageList); | |
401 | ||
402 | if ( imageList ) | |
403 | { | |
404 | TabCtrl_SetImageList(m_hwnd, (HIMAGELIST)imageList->GetHIMAGELIST()); | |
405 | } | |
406 | } | |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // wxNotebook size settings | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
412 | void wxNotebook::SetPageSize(const wxSize& size) | |
413 | { | |
414 | // transform the page size into the notebook size | |
415 | RECT rc; | |
416 | rc.left = | |
417 | rc.top = 0; | |
418 | rc.right = size.x; | |
419 | rc.bottom = size.y; | |
420 | ||
421 | TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc); | |
422 | ||
423 | // and now set it | |
424 | SetSize(rc.right - rc.left, rc.bottom - rc.top); | |
425 | } | |
426 | ||
427 | void wxNotebook::SetPadding(const wxSize& padding) | |
428 | { | |
429 | TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y); | |
430 | } | |
431 | ||
432 | // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH | |
433 | // style. | |
434 | void wxNotebook::SetTabSize(const wxSize& sz) | |
435 | { | |
436 | ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y)); | |
437 | } | |
438 | ||
439 | wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const | |
440 | { | |
441 | wxSize sizeTotal = sizePage; | |
442 | ||
443 | // We need to make getting tab size part of the wxWindows API. | |
444 | wxSize tabSize(0, 0); | |
445 | if (GetPageCount() > 0) | |
446 | { | |
447 | RECT rect; | |
448 | TabCtrl_GetItemRect((HWND) GetHWND(), 0, & rect); | |
449 | tabSize.x = rect.right - rect.left; | |
450 | tabSize.y = rect.bottom - rect.top; | |
451 | } | |
452 | if ( HasFlag(wxNB_LEFT) || HasFlag(wxNB_RIGHT) ) | |
453 | { | |
454 | sizeTotal.x += tabSize.x + 7; | |
455 | sizeTotal.y += 7; | |
456 | } | |
457 | else | |
458 | { | |
459 | sizeTotal.x += 7; | |
460 | sizeTotal.y += tabSize.y + 7; | |
461 | } | |
462 | ||
463 | return sizeTotal; | |
464 | } | |
465 | ||
466 | void wxNotebook::AdjustPageSize(wxNotebookPage *page) | |
467 | { | |
468 | wxCHECK_RET( page, _T("NULL page in wxNotebook::AdjustPageSize") ); | |
469 | ||
470 | RECT rc; | |
471 | rc.left = | |
472 | rc.top = 0; | |
473 | ||
474 | // get the page size from the notebook size | |
475 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
476 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
477 | ||
478 | page->SetSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); | |
479 | } | |
480 | ||
481 | // ---------------------------------------------------------------------------- | |
482 | // wxNotebook operations | |
483 | // ---------------------------------------------------------------------------- | |
484 | ||
485 | // remove one page from the notebook, without deleting | |
486 | wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage) | |
487 | { | |
488 | wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage); | |
489 | if ( !pageRemoved ) | |
490 | return NULL; | |
491 | ||
492 | TabCtrl_DeleteItem(m_hwnd, nPage); | |
493 | ||
494 | if ( m_pages.IsEmpty() ) | |
495 | { | |
496 | // no selection any more, the notebook becamse empty | |
497 | m_nSelection = -1; | |
498 | } | |
499 | else // notebook still not empty | |
500 | { | |
501 | // change the selected page if it was deleted or became invalid | |
502 | int selNew; | |
503 | if ( m_nSelection == int(GetPageCount()) ) | |
504 | { | |
505 | // last page deleted, make the new last page the new selection | |
506 | selNew = m_nSelection - 1; | |
507 | } | |
508 | else if ( int(nPage) <= m_nSelection ) | |
509 | { | |
510 | // we must show another page, even if it has the same index | |
511 | selNew = m_nSelection; | |
512 | } | |
513 | else // nothing changes for the currently selected page | |
514 | { | |
515 | selNew = -1; | |
516 | ||
517 | // we still must refresh the current page: this needs to be done | |
518 | // for some unknown reason if the tab control shows the up-down | |
519 | // control (i.e. when there are too many pages) -- otherwise after | |
520 | // deleting a page nothing at all is shown | |
521 | if (m_nSelection >= 0) | |
522 | m_pages[m_nSelection]->Refresh(); | |
523 | } | |
524 | ||
525 | if ( selNew != -1 ) | |
526 | { | |
527 | // m_nSelection must be always valid so reset it before calling | |
528 | // SetSelection() | |
529 | m_nSelection = -1; | |
530 | SetSelection(selNew); | |
531 | } | |
532 | } | |
533 | ||
534 | return pageRemoved; | |
535 | } | |
536 | ||
537 | // remove all pages | |
538 | bool wxNotebook::DeleteAllPages() | |
539 | { | |
540 | size_t nPageCount = GetPageCount(); | |
541 | size_t nPage; | |
542 | for ( nPage = 0; nPage < nPageCount; nPage++ ) | |
543 | delete m_pages[nPage]; | |
544 | ||
545 | m_pages.Clear(); | |
546 | ||
547 | TabCtrl_DeleteAllItems(m_hwnd); | |
548 | ||
549 | m_nSelection = -1; | |
550 | ||
551 | return TRUE; | |
552 | } | |
553 | ||
554 | // same as AddPage() but does it at given position | |
555 | bool wxNotebook::InsertPage(size_t nPage, | |
556 | wxNotebookPage *pPage, | |
557 | const wxString& strText, | |
558 | bool bSelect, | |
559 | int imageId) | |
560 | { | |
561 | wxCHECK_MSG( pPage != NULL, FALSE, _T("NULL page in wxNotebook::InsertPage") ); | |
562 | wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE, | |
563 | _T("invalid index in wxNotebook::InsertPage") ); | |
564 | ||
565 | wxASSERT_MSG( pPage->GetParent() == this, | |
566 | _T("notebook pages must have notebook as parent") ); | |
567 | ||
568 | #if wxUSE_UXTHEME && wxUSE_UXTHEME_AUTO | |
569 | static bool g_TestedForTheme = FALSE; | |
570 | static bool g_UseTheme = FALSE; | |
571 | if (!g_TestedForTheme) | |
572 | { | |
573 | int commCtrlVersion = wxTheApp->GetComCtl32Version() ; | |
574 | ||
575 | g_UseTheme = (commCtrlVersion >= 600); | |
576 | g_TestedForTheme = TRUE; | |
577 | } | |
578 | ||
579 | // Automatically apply the theme background, | |
580 | // changing the colour of the panel to match the | |
581 | // tab page colour. This won't work well with all | |
582 | // themes but it's a start. | |
583 | if (g_UseTheme && wxUxThemeEngine::Get() && pPage->IsKindOf(CLASSINFO(wxPanel))) | |
584 | { | |
585 | ApplyThemeBackground(pPage, GetThemeBackgroundColour()); | |
586 | } | |
587 | #endif | |
588 | ||
589 | // add a new tab to the control | |
590 | // ---------------------------- | |
591 | ||
592 | // init all fields to 0 | |
593 | TC_ITEM tcItem; | |
594 | wxZeroMemory(tcItem); | |
595 | ||
596 | // set the image, if any | |
597 | if ( imageId != -1 ) | |
598 | { | |
599 | tcItem.mask |= TCIF_IMAGE; | |
600 | tcItem.iImage = imageId; | |
601 | } | |
602 | ||
603 | // and the text | |
604 | if ( !strText.IsEmpty() ) | |
605 | { | |
606 | tcItem.mask |= TCIF_TEXT; | |
607 | tcItem.pszText = (wxChar *)strText.c_str(); // const_cast | |
608 | } | |
609 | ||
610 | // fit the notebook page to the tab control's display area: this should be | |
611 | // done before adding it to the notebook or TabCtrl_InsertItem() will | |
612 | // change the notebooks size itself! | |
613 | AdjustPageSize(pPage); | |
614 | ||
615 | // finally do insert it | |
616 | if ( TabCtrl_InsertItem(m_hwnd, nPage, &tcItem) == -1 ) | |
617 | { | |
618 | wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str()); | |
619 | ||
620 | return FALSE; | |
621 | } | |
622 | ||
623 | // succeeded: save the pointer to the page | |
624 | m_pages.Insert(pPage, nPage); | |
625 | ||
626 | // for the first page (only) we need to adjust the size again because the | |
627 | // notebook size changed: the tabs which hadn't been there before are now | |
628 | // shown | |
629 | if ( m_pages.GetCount() == 1 ) | |
630 | { | |
631 | AdjustPageSize(pPage); | |
632 | } | |
633 | ||
634 | // hide the page: unless it is selected, it shouldn't be shown (and if it | |
635 | // is selected it will be shown later) | |
636 | HWND hwnd = GetWinHwnd(pPage); | |
637 | SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE); | |
638 | ||
639 | // this updates internal flag too -- otherwise it would get out of sync | |
640 | // with the real state | |
641 | pPage->Show(FALSE); | |
642 | ||
643 | ||
644 | // now deal with the selection | |
645 | // --------------------------- | |
646 | ||
647 | // if the inserted page is before the selected one, we must update the | |
648 | // index of the selected page | |
649 | if ( int(nPage) <= m_nSelection ) | |
650 | { | |
651 | // one extra page added | |
652 | m_nSelection++; | |
653 | } | |
654 | ||
655 | // some page should be selected: either this one or the first one if there | |
656 | // is still no selection | |
657 | int selNew = -1; | |
658 | if ( bSelect ) | |
659 | selNew = nPage; | |
660 | else if ( m_nSelection == -1 ) | |
661 | selNew = 0; | |
662 | ||
663 | if ( selNew != -1 ) | |
664 | SetSelection(selNew); | |
665 | ||
666 | return TRUE; | |
667 | } | |
668 | ||
669 | int wxNotebook::HitTest(const wxPoint& pt, long *flags) const | |
670 | { | |
671 | TC_HITTESTINFO hitTestInfo; | |
672 | hitTestInfo.pt.x = pt.x; | |
673 | hitTestInfo.pt.y = pt.y; | |
674 | int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo); | |
675 | ||
676 | if ( flags ) | |
677 | { | |
678 | *flags = 0; | |
679 | ||
680 | if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE) | |
681 | *flags |= wxNB_HITTEST_NOWHERE; | |
682 | if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM) | |
683 | *flags |= wxNB_HITTEST_ONITEM; | |
684 | if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON) | |
685 | *flags |= wxNB_HITTEST_ONICON; | |
686 | if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL) | |
687 | *flags |= wxNB_HITTEST_ONLABEL; | |
688 | } | |
689 | ||
690 | return item; | |
691 | } | |
692 | ||
693 | ||
694 | // ---------------------------------------------------------------------------- | |
695 | // wxNotebook callbacks | |
696 | // ---------------------------------------------------------------------------- | |
697 | ||
698 | void wxNotebook::OnSize(wxSizeEvent& event) | |
699 | { | |
700 | // fit the notebook page to the tab control's display area | |
701 | RECT rc; | |
702 | rc.left = rc.top = 0; | |
703 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
704 | ||
705 | TabCtrl_AdjustRect(m_hwnd, FALSE, &rc); | |
706 | ||
707 | int width = rc.right - rc.left, | |
708 | height = rc.bottom - rc.top; | |
709 | size_t nCount = m_pages.Count(); | |
710 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) { | |
711 | wxNotebookPage *pPage = m_pages[nPage]; | |
712 | pPage->SetSize(rc.left, rc.top, width, height); | |
713 | } | |
714 | ||
715 | event.Skip(); | |
716 | } | |
717 | ||
718 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
719 | { | |
720 | // is it our tab control? | |
721 | if ( event.GetEventObject() == this ) | |
722 | { | |
723 | int sel = event.GetOldSelection(); | |
724 | if ( sel != -1 ) | |
725 | m_pages[sel]->Show(FALSE); | |
726 | ||
727 | sel = event.GetSelection(); | |
728 | if ( sel != -1 ) | |
729 | { | |
730 | wxNotebookPage *pPage = m_pages[sel]; | |
731 | pPage->Show(TRUE); | |
732 | pPage->SetFocus(); | |
733 | } | |
734 | ||
735 | m_nSelection = sel; | |
736 | } | |
737 | ||
738 | // we want to give others a chance to process this message as well | |
739 | event.Skip(); | |
740 | } | |
741 | ||
742 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
743 | { | |
744 | // this function is only called when the focus is explicitly set (i.e. from | |
745 | // the program) to the notebook - in this case we don't need the | |
746 | // complicated OnNavigationKey() logic because the programmer knows better | |
747 | // what [s]he wants | |
748 | ||
749 | // set focus to the currently selected page if any | |
750 | if ( m_nSelection != -1 ) | |
751 | m_pages[m_nSelection]->SetFocus(); | |
752 | ||
753 | event.Skip(); | |
754 | } | |
755 | ||
756 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
757 | { | |
758 | if ( event.IsWindowChange() ) { | |
759 | // change pages | |
760 | AdvanceSelection(event.GetDirection()); | |
761 | } | |
762 | else { | |
763 | // we get this event in 2 cases | |
764 | // | |
765 | // a) one of our pages might have generated it because the user TABbed | |
766 | // out from it in which case we should propagate the event upwards and | |
767 | // our parent will take care of setting the focus to prev/next sibling | |
768 | // | |
769 | // or | |
770 | // | |
771 | // b) the parent panel wants to give the focus to us so that we | |
772 | // forward it to our selected page. We can't deal with this in | |
773 | // OnSetFocus() because we don't know which direction the focus came | |
774 | // from in this case and so can't choose between setting the focus to | |
775 | // first or last panel child | |
776 | wxWindow *parent = GetParent(); | |
777 | // the cast is here to fic a GCC ICE | |
778 | if ( ((wxWindow*)event.GetEventObject()) == parent ) | |
779 | { | |
780 | // no, it doesn't come from child, case (b): forward to a page | |
781 | if ( m_nSelection != -1 ) | |
782 | { | |
783 | // so that the page knows that the event comes from it's parent | |
784 | // and is being propagated downwards | |
785 | event.SetEventObject(this); | |
786 | ||
787 | wxWindow *page = m_pages[m_nSelection]; | |
788 | if ( !page->GetEventHandler()->ProcessEvent(event) ) | |
789 | { | |
790 | page->SetFocus(); | |
791 | } | |
792 | //else: page manages focus inside it itself | |
793 | } | |
794 | else | |
795 | { | |
796 | // we have no pages - still have to give focus to _something_ | |
797 | SetFocus(); | |
798 | } | |
799 | } | |
800 | else | |
801 | { | |
802 | // it comes from our child, case (a), pass to the parent | |
803 | if ( parent ) { | |
804 | event.SetCurrentFocus(this); | |
805 | parent->GetEventHandler()->ProcessEvent(event); | |
806 | } | |
807 | } | |
808 | } | |
809 | } | |
810 | ||
811 | // ---------------------------------------------------------------------------- | |
812 | // wxNotebook base class virtuals | |
813 | // ---------------------------------------------------------------------------- | |
814 | ||
815 | #if wxUSE_CONSTRAINTS | |
816 | ||
817 | // override these 2 functions to do nothing: everything is done in OnSize | |
818 | ||
819 | void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse)) | |
820 | { | |
821 | // don't set the sizes of the pages - their correct size is not yet known | |
822 | wxControl::SetConstraintSizes(FALSE); | |
823 | } | |
824 | ||
825 | bool wxNotebook::DoPhase(int WXUNUSED(nPhase)) | |
826 | { | |
827 | return TRUE; | |
828 | } | |
829 | ||
830 | #endif // wxUSE_CONSTRAINTS | |
831 | ||
832 | // ---------------------------------------------------------------------------- | |
833 | // wxNotebook Windows message handlers | |
834 | // ---------------------------------------------------------------------------- | |
835 | ||
836 | bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode, | |
837 | WXWORD pos, WXHWND control) | |
838 | { | |
839 | // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the | |
840 | // up-down control | |
841 | if ( control ) | |
842 | return FALSE; | |
843 | ||
844 | return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control); | |
845 | } | |
846 | ||
847 | bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result) | |
848 | { | |
849 | wxNotebookEvent event(wxEVT_NULL, m_windowId); | |
850 | ||
851 | NMHDR* hdr = (NMHDR *)lParam; | |
852 | switch ( hdr->code ) { | |
853 | case TCN_SELCHANGE: | |
854 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
855 | break; | |
856 | ||
857 | case TCN_SELCHANGING: | |
858 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING); | |
859 | break; | |
860 | ||
861 | default: | |
862 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
863 | } | |
864 | ||
865 | event.SetSelection(TabCtrl_GetCurSel(m_hwnd)); | |
866 | event.SetOldSelection(m_nSelection); | |
867 | event.SetEventObject(this); | |
868 | event.SetInt(idCtrl); | |
869 | ||
870 | bool processed = GetEventHandler()->ProcessEvent(event); | |
871 | *result = !event.IsAllowed(); | |
872 | return processed; | |
873 | } | |
874 | ||
875 | // Windows only: attempts to get colour for UX theme page background | |
876 | wxColour wxNotebook::GetThemeBackgroundColour() | |
877 | { | |
878 | #if wxUSE_UXTHEME | |
879 | if (wxUxThemeEngine::Get()) | |
880 | { | |
881 | wxUxThemeHandle hTheme(this, L"TAB"); | |
882 | if (hTheme) | |
883 | { | |
884 | // This is total guesswork. | |
885 | // See PlatformSDK\Include\Tmschema.h for values | |
886 | COLORREF themeColor; | |
887 | wxUxThemeEngine::Get()->GetThemeColor | |
888 | ( | |
889 | hTheme, | |
890 | 10 /* TABP_BODY */, | |
891 | 1 /* NORMAL */, | |
892 | 3821, /* FILLCOLORHINT */ | |
893 | & themeColor | |
894 | ); | |
895 | ||
896 | wxColour colour(GetRValue(themeColor), GetGValue(themeColor), GetBValue(themeColor)); | |
897 | return colour; | |
898 | } | |
899 | } | |
900 | #endif // wxUSE_UXTHEME | |
901 | ||
902 | return GetBackgroundColour(); | |
903 | } | |
904 | ||
905 | // Windows only: attempts to apply the UX theme page background to this page | |
906 | #if wxUSE_UXTHEME | |
907 | void wxNotebook::ApplyThemeBackground(wxWindow* window, const wxColour& colour) | |
908 | #else | |
909 | void wxNotebook::ApplyThemeBackground(wxWindow*, const wxColour&) | |
910 | #endif | |
911 | { | |
912 | #if wxUSE_UXTHEME | |
913 | // Don't set the background for buttons since this will | |
914 | // switch it into ownerdraw mode | |
915 | if (window->IsKindOf(CLASSINFO(wxButton)) && !window->IsKindOf(CLASSINFO(wxBitmapButton))) | |
916 | // This is essential, otherwise you'll see dark grey | |
917 | // corners in the buttons. | |
918 | ((wxButton*)window)->wxControl::SetBackgroundColour(colour); | |
919 | else if (window->IsKindOf(CLASSINFO(wxStaticText)) || | |
920 | window->IsKindOf(CLASSINFO(wxStaticBox)) || | |
921 | window->IsKindOf(CLASSINFO(wxStaticLine)) || | |
922 | window->IsKindOf(CLASSINFO(wxRadioButton)) || | |
923 | window->IsKindOf(CLASSINFO(wxRadioBox)) || | |
924 | window->IsKindOf(CLASSINFO(wxCheckBox)) || | |
925 | window->IsKindOf(CLASSINFO(wxBitmapButton)) || | |
926 | window->IsKindOf(CLASSINFO(wxSlider)) || | |
927 | window->IsKindOf(CLASSINFO(wxPanel)) || | |
928 | (window->IsKindOf(CLASSINFO(wxNotebook)) && (window != this)) || | |
929 | window->IsKindOf(CLASSINFO(wxScrolledWindow)) | |
930 | ) | |
931 | { | |
932 | window->SetBackgroundColour(colour); | |
933 | } | |
934 | ||
935 | for ( wxWindowList::compatibility_iterator node = window->GetChildren().GetFirst(); node; node = node->GetNext() ) | |
936 | { | |
937 | wxWindow *child = node->GetData(); | |
938 | ApplyThemeBackground(child, colour); | |
939 | } | |
940 | #endif | |
941 | } | |
942 | ||
943 | long wxNotebook::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
944 | { | |
945 | static bool g_TestedForTheme = FALSE; | |
946 | static bool g_UseTheme = FALSE; | |
947 | switch ( nMsg ) | |
948 | { | |
949 | case WM_ERASEBKGND: | |
950 | { | |
951 | if (!g_TestedForTheme) | |
952 | { | |
953 | int commCtrlVersion = wxTheApp->GetComCtl32Version() ; | |
954 | ||
955 | g_UseTheme = (commCtrlVersion >= 600); | |
956 | g_TestedForTheme = TRUE; | |
957 | } | |
958 | ||
959 | // If using XP themes, it seems we can get away | |
960 | // with not drawing a background, which reduces flicker. | |
961 | if (g_UseTheme) | |
962 | return TRUE; | |
963 | } | |
964 | } | |
965 | ||
966 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
967 | } | |
968 | ||
969 | ||
970 | #endif // wxUSE_NOTEBOOK |