]>
Commit | Line | Data |
---|---|---|
88310e2e | 1 | /////////////////////////////////////////////////////////////////////////////// |
2ddb4d13 | 2 | // Name: src/msw/notebook.cpp |
88310e2e VZ |
3 | // Purpose: implementation of wxNotebook |
4 | // Author: Vadim Zeitlin | |
907f37b3 | 5 | // Modified by: |
88310e2e | 6 | // Created: 11.06.98 |
88310e2e | 7 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
65571936 | 8 | // Licence: wxWindows licence |
88310e2e VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
88310e2e VZ |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
1e6feb95 | 15 | #pragma hdrstop |
88310e2e VZ |
16 | #endif |
17 | ||
1e6feb95 VZ |
18 | #if wxUSE_NOTEBOOK |
19 | ||
57bd4c60 | 20 | #include "wx/notebook.h" |
e4db172a | 21 | |
88310e2e | 22 | #ifndef WX_PRECOMP |
57bd4c60 WS |
23 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
24 | #include "wx/string.h" | |
25 | #include "wx/dc.h" | |
26 | #include "wx/log.h" | |
27 | #include "wx/event.h" | |
28 | #include "wx/app.h" | |
29 | #include "wx/dcclient.h" | |
30 | #include "wx/dcmemory.h" | |
31 | #include "wx/control.h" | |
bb93b4a8 | 32 | #include "wx/panel.h" |
88310e2e VZ |
33 | #endif // WX_PRECOMP |
34 | ||
57bd4c60 WS |
35 | #include "wx/imaglist.h" |
36 | #include "wx/sysopt.h" | |
88310e2e | 37 | |
57bd4c60 | 38 | #include "wx/msw/private.h" |
74052fe8 | 39 | #include "wx/msw/dc.h" |
88310e2e | 40 | |
57bd4c60 | 41 | #include <windowsx.h> |
85b43fbf JS |
42 | #include "wx/msw/winundef.h" |
43 | ||
44 | #if wxUSE_UXTHEME | |
caf95d2a | 45 | #include "wx/msw/uxtheme.h" |
85b43fbf JS |
46 | #endif |
47 | ||
88310e2e VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // macros | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | // check that the page index is valid | |
8d34bf5c | 53 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) |
88310e2e | 54 | |
014ee083 VZ |
55 | // you can set USE_NOTEBOOK_ANTIFLICKER to 0 for desktop Windows versions too |
56 | // to disable code whih results in flicker-less notebook redrawing at the | |
57 | // expense of some extra GDI resource consumption | |
254fbd14 | 58 | #ifdef __WXWINCE__ |
014ee083 VZ |
59 | // notebooks are never resized under CE anyhow |
60 | #define USE_NOTEBOOK_ANTIFLICKER 0 | |
254fbd14 | 61 | #else |
014ee083 | 62 | #define USE_NOTEBOOK_ANTIFLICKER 1 |
254fbd14 JS |
63 | #endif |
64 | ||
74b31181 VZ |
65 | // ---------------------------------------------------------------------------- |
66 | // constants | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // This is a work-around for missing defines in gcc-2.95 headers | |
70 | #ifndef TCS_RIGHT | |
71 | #define TCS_RIGHT 0x0002 | |
72 | #endif | |
73 | ||
74 | #ifndef TCS_VERTICAL | |
75 | #define TCS_VERTICAL 0x0080 | |
76 | #endif | |
77 | ||
78 | #ifndef TCS_BOTTOM | |
79 | #define TCS_BOTTOM TCS_RIGHT | |
80 | #endif | |
81 | ||
014ee083 VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // global variables | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | #if USE_NOTEBOOK_ANTIFLICKER | |
87 | ||
88 | // the pointer to standard spin button wnd proc | |
89 | static WXFARPROC gs_wndprocNotebookSpinBtn = (WXFARPROC)NULL; | |
90 | ||
6143d3b6 | 91 | // the pointer to standard tab control wnd proc |
e1cffcd6 | 92 | static WXFARPROC gs_wndprocNotebook = (WXFARPROC)NULL; |
6143d3b6 VZ |
93 | |
94 | LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd, | |
95 | UINT message, | |
96 | WPARAM wParam, | |
97 | LPARAM lParam); | |
98 | ||
014ee083 VZ |
99 | #endif // USE_NOTEBOOK_ANTIFLICKER |
100 | ||
b013c971 VZ |
101 | // ---------------------------------------------------------------------------- |
102 | // global functions | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
105 | static bool HasTroubleWithNonTopTabs() | |
106 | { | |
107 | const int verComCtl32 = wxApp::GetComCtl32Version(); | |
108 | ||
109 | // 600 is XP, 616 is Vista -- and both have a problem with tabs not on top | |
110 | // (but don't just test for >= 600 as Microsoft might decide to fix it in | |
111 | // later versions, who knows...) | |
112 | return verComCtl32 >= 600 && verComCtl32 <= 616; | |
113 | } | |
114 | ||
88310e2e VZ |
115 | // ---------------------------------------------------------------------------- |
116 | // event table | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
8dba4c72 | 119 | BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase) |
9026ad85 | 120 | EVT_SIZE(wxNotebook::OnSize) |
88310e2e | 121 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) |
014ee083 VZ |
122 | |
123 | #if USE_NOTEBOOK_ANTIFLICKER | |
124 | EVT_ERASE_BACKGROUND(wxNotebook::OnEraseBackground) | |
125 | EVT_PAINT(wxNotebook::OnPaint) | |
126 | #endif // USE_NOTEBOOK_ANTIFLICKER | |
d9317fd4 | 127 | END_EVENT_TABLE() |
88310e2e | 128 | |
88310e2e VZ |
129 | // ============================================================================ |
130 | // implementation | |
131 | // ============================================================================ | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // wxNotebook construction | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | // common part of all ctors | |
138 | void wxNotebook::Init() | |
139 | { | |
caf95d2a | 140 | #if wxUSE_UXTHEME |
7ec69821 | 141 | m_hbrBackground = NULL; |
caf95d2a | 142 | #endif // wxUSE_UXTHEME |
dfb47d83 VZ |
143 | |
144 | #if USE_NOTEBOOK_ANTIFLICKER | |
7ec69821 | 145 | m_hasSubclassedUpdown = false; |
feb32e31 | 146 | m_doneUpdateHack = false; |
dfb47d83 | 147 | #endif // USE_NOTEBOOK_ANTIFLICKER |
88310e2e VZ |
148 | } |
149 | ||
150 | // default for dynamic class | |
151 | wxNotebook::wxNotebook() | |
152 | { | |
153 | Init(); | |
154 | } | |
155 | ||
156 | // the same arguments as for wxControl | |
157 | wxNotebook::wxNotebook(wxWindow *parent, | |
8b9518ee | 158 | wxWindowID id, |
88310e2e VZ |
159 | const wxPoint& pos, |
160 | const wxSize& size, | |
8b9518ee | 161 | long style, |
88310e2e VZ |
162 | const wxString& name) |
163 | { | |
164 | Init(); | |
165 | ||
166 | Create(parent, id, pos, size, style, name); | |
167 | } | |
168 | ||
169 | // Create() function | |
170 | bool wxNotebook::Create(wxWindow *parent, | |
8b9518ee | 171 | wxWindowID id, |
88310e2e VZ |
172 | const wxPoint& pos, |
173 | const wxSize& size, | |
8b9518ee | 174 | long style, |
88310e2e VZ |
175 | const wxString& name) |
176 | { | |
895cc205 WS |
177 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
178 | { | |
179 | #if defined(__POCKETPC__) | |
180 | style |= wxBK_BOTTOM | wxNB_FLAT; | |
181 | #else | |
182 | style |= wxBK_TOP; | |
183 | #endif | |
184 | } | |
185 | ||
45418059 JS |
186 | #ifdef __WXWINCE__ |
187 | // Not sure why, but without this style, there is no border | |
188 | // around the notebook tabs. | |
189 | if (style & wxNB_FLAT) | |
190 | style |= wxBORDER_SUNKEN; | |
191 | #endif | |
c3732409 | 192 | |
e4db172a | 193 | #if !wxUSE_UXTHEME |
b013c971 VZ |
194 | // ComCtl32 notebook tabs simply don't work unless they're on top if we |
195 | // have uxtheme, we can work around it later (after control creation), but | |
196 | // if we have been compiled without uxtheme support, we have to clear those | |
197 | // styles | |
198 | if ( HasTroubleWithNonTopTabs() ) | |
04eb05b0 | 199 | { |
5483756f | 200 | style &= ~(wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT); |
04eb05b0 | 201 | } |
7cc1ad29 | 202 | #endif //wxUSE_UXTHEME |
078cf5cb | 203 | |
82c51a8e JS |
204 | #if defined(__WINE__) && wxUSE_UNICODE |
205 | LPCTSTR className = L"SysTabControl32"; | |
206 | #else | |
c1637c89 | 207 | LPCTSTR className = WC_TABCONTROL; |
82c51a8e | 208 | #endif |
c1637c89 | 209 | |
6143d3b6 | 210 | #if USE_NOTEBOOK_ANTIFLICKER |
c1637c89 VZ |
211 | // SysTabCtl32 class has natively CS_HREDRAW and CS_VREDRAW enabled and it |
212 | // causes horrible flicker when resizing notebook, so get rid of it by | |
213 | // using a class without these styles (but otherwise identical to it) | |
214 | if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) ) | |
215 | { | |
216 | static ClassRegistrar s_clsNotebook; | |
217 | if ( !s_clsNotebook.IsInitialized() ) | |
218 | { | |
219 | // get a copy of standard class and modify it | |
220 | WNDCLASS wc; | |
221 | ||
6143d3b6 | 222 | if ( ::GetClassInfo(NULL, WC_TABCONTROL, &wc) ) |
c1637c89 | 223 | { |
6143d3b6 | 224 | gs_wndprocNotebook = |
5c33522f | 225 | reinterpret_cast<WXFARPROC>(wc.lpfnWndProc); |
c1637c89 VZ |
226 | wc.lpszClassName = wxT("_wx_SysTabCtl32"); |
227 | wc.style &= ~(CS_HREDRAW | CS_VREDRAW); | |
6143d3b6 VZ |
228 | wc.hInstance = wxGetInstance(); |
229 | wc.lpfnWndProc = wxNotebookWndProc; | |
c1637c89 VZ |
230 | s_clsNotebook.Register(wc); |
231 | } | |
232 | else | |
233 | { | |
9a83f860 | 234 | wxLogLastError(wxT("GetClassInfoEx(SysTabCtl32)")); |
c1637c89 VZ |
235 | } |
236 | } | |
237 | ||
238 | // use our custom class if available but fall back to the standard | |
239 | // notebook if we failed to register it | |
240 | if ( s_clsNotebook.IsRegistered() ) | |
241 | { | |
242 | // it's ok to use c_str() here as the static s_clsNotebook object | |
243 | // has sufficiently long lifetime | |
244 | className = s_clsNotebook.GetName().c_str(); | |
245 | } | |
246 | } | |
6143d3b6 | 247 | #endif // USE_NOTEBOOK_ANTIFLICKER |
c1637c89 | 248 | |
6dd16e4f VZ |
249 | if ( !CreateControl(parent, id, pos, size, style | wxTAB_TRAVERSAL, |
250 | wxDefaultValidator, name) ) | |
b8bdaa7c | 251 | return false; |
88310e2e | 252 | |
c1637c89 | 253 | if ( !MSWCreateControl(className, wxEmptyString, pos, size) ) |
b8bdaa7c | 254 | return false; |
907f37b3 | 255 | |
b8d8362c VZ |
256 | // Inherit parent attributes and, unlike the default, also inherit the |
257 | // parent background colour in order to blend in with its background if | |
258 | // it's set to a non-default value. | |
259 | InheritAttributes(); | |
260 | if ( parent->InheritsBackgroundColour() && !UseBgCol() ) | |
261 | SetBackgroundColour(parent->GetBackgroundColour()); | |
262 | ||
7dccdf81 VZ |
263 | #if wxUSE_UXTHEME |
264 | if ( HasFlag(wxNB_NOPAGETHEME) || | |
265 | wxSystemOptions::IsFalse(wxT("msw.notebook.themed-background")) ) | |
f2b7be7a | 266 | { |
7dccdf81 | 267 | SetBackgroundColour(GetThemeBackgroundColour()); |
f2b7be7a | 268 | } |
30556182 VZ |
269 | else // use themed background by default |
270 | { | |
271 | // create backing store | |
272 | UpdateBgBrush(); | |
273 | } | |
e4db172a | 274 | |
7cc1ad29 JS |
275 | // comctl32.dll 6.0 doesn't support non-top tabs with visual styles (the |
276 | // control is simply not rendered correctly), so we disable themes | |
277 | // if possible, otherwise we simply clear the styles. | |
b013c971 VZ |
278 | if ( HasTroubleWithNonTopTabs() && |
279 | (style & (wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT)) ) | |
7cc1ad29 JS |
280 | { |
281 | // check if we use themes at all -- if we don't, we're still okay | |
b013c971 | 282 | if ( wxUxThemeEngine::GetIfActive() ) |
7cc1ad29 | 283 | { |
b013c971 VZ |
284 | wxUxThemeEngine::GetIfActive()->SetWindowTheme(GetHwnd(), L"", L""); |
285 | ||
286 | // correct the background color for the new non-themed control | |
8dba4c72 | 287 | SetBackgroundColour(GetThemeBackgroundColour()); |
7cc1ad29 JS |
288 | } |
289 | } | |
7dccdf81 | 290 | #endif // wxUSE_UXTHEME |
b554cf63 JS |
291 | |
292 | // Undocumented hack to get flat notebook style | |
293 | // In fact, we should probably only do this in some | |
294 | // curcumstances, i.e. if we know we will have a border | |
295 | // at the bottom (the tab control doesn't draw it itself) | |
296 | #if defined(__POCKETPC__) || defined(__SMARTPHONE__) | |
297 | if (HasFlag(wxNB_FLAT)) | |
298 | { | |
01c8cbf5 | 299 | SendMessage(GetHwnd(), CCM_SETVERSION, COMCTL32_VERSION, 0); |
b554cf63 JS |
300 | if (!m_hasBgCol) |
301 | SetBackgroundColour(*wxWHITE); | |
302 | } | |
303 | #endif | |
b8bdaa7c | 304 | return true; |
0df3fbd7 VZ |
305 | } |
306 | ||
307 | WXDWORD wxNotebook::MSWGetStyle(long style, WXDWORD *exstyle) const | |
308 | { | |
309 | WXDWORD tabStyle = wxControl::MSWGetStyle(style, exstyle); | |
310 | ||
311 | tabStyle |= WS_TABSTOP | TCS_TABS; | |
312 | ||
2b5f62a0 | 313 | if ( style & wxNB_MULTILINE ) |
0df3fbd7 VZ |
314 | tabStyle |= TCS_MULTILINE; |
315 | if ( style & wxNB_FIXEDWIDTH ) | |
316 | tabStyle |= TCS_FIXEDWIDTH; | |
317 | ||
2ddb4d13 | 318 | if ( style & wxBK_BOTTOM ) |
0df3fbd7 | 319 | tabStyle |= TCS_RIGHT; |
2ddb4d13 | 320 | else if ( style & wxBK_LEFT ) |
0df3fbd7 | 321 | tabStyle |= TCS_VERTICAL; |
2ddb4d13 | 322 | else if ( style & wxBK_RIGHT ) |
c3732409 | 323 | tabStyle |= TCS_VERTICAL | TCS_RIGHT; |
0df3fbd7 | 324 | |
0df3fbd7 | 325 | return tabStyle; |
88310e2e VZ |
326 | } |
327 | ||
caf95d2a VZ |
328 | wxNotebook::~wxNotebook() |
329 | { | |
330 | #if wxUSE_UXTHEME | |
331 | if ( m_hbrBackground ) | |
332 | ::DeleteObject((HBRUSH)m_hbrBackground); | |
333 | #endif // wxUSE_UXTHEME | |
334 | } | |
335 | ||
88310e2e VZ |
336 | // ---------------------------------------------------------------------------- |
337 | // wxNotebook accessors | |
338 | // ---------------------------------------------------------------------------- | |
07b8d7ec | 339 | |
8d34bf5c | 340 | size_t wxNotebook::GetPageCount() const |
88310e2e | 341 | { |
8ecd5de2 WS |
342 | // consistency check |
343 | wxASSERT( (int)m_pages.Count() == TabCtrl_GetItemCount(GetHwnd()) ); | |
88310e2e | 344 | |
8ecd5de2 | 345 | return m_pages.Count(); |
88310e2e VZ |
346 | } |
347 | ||
348 | int wxNotebook::GetRowCount() const | |
349 | { | |
8ecd5de2 | 350 | return TabCtrl_GetRowCount(GetHwnd()); |
88310e2e VZ |
351 | } |
352 | ||
8d34bf5c | 353 | int wxNotebook::SetSelection(size_t nPage) |
88310e2e | 354 | { |
8ecd5de2 WS |
355 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") ); |
356 | ||
681be2ef | 357 | if ( m_selection == wxNOT_FOUND || nPage != (size_t)m_selection ) |
2b5f62a0 | 358 | { |
a570e8f8 | 359 | if ( SendPageChangingEvent(nPage) ) |
8ecd5de2 WS |
360 | { |
361 | // program allows the page change | |
426ebc99 | 362 | const int selectionOld = m_selection; |
2b5f62a0 | 363 | |
409f747e VZ |
364 | UpdateSelection(nPage); |
365 | ||
a570e8f8 | 366 | TabCtrl_SetCurSel(GetHwnd(), nPage); |
426ebc99 VZ |
367 | |
368 | SendPageChangedEvent(selectionOld, nPage); | |
8ecd5de2 | 369 | } |
2b5f62a0 | 370 | } |
88310e2e | 371 | |
681be2ef | 372 | return m_selection; |
88310e2e VZ |
373 | } |
374 | ||
5482ee07 | 375 | void wxNotebook::UpdateSelection(int selNew) |
1d6fcbcc | 376 | { |
681be2ef VZ |
377 | if ( m_selection != wxNOT_FOUND ) |
378 | m_pages[m_selection]->Show(false); | |
1d6fcbcc | 379 | |
5482ee07 | 380 | if ( selNew != wxNOT_FOUND ) |
1d6fcbcc | 381 | { |
5482ee07 | 382 | wxNotebookPage *pPage = m_pages[selNew]; |
1d6fcbcc | 383 | pPage->Show(true); |
1d6fcbcc | 384 | |
256edfbf VZ |
385 | // In addition to showing the page, we also want to give focus to it to |
386 | // make it possible to work with it from keyboard easily. However there | |
387 | // are two exceptions: first, don't touch the focus at all if the | |
388 | // notebook itself is not currently shown. | |
389 | if ( ::IsWindowVisible(GetHwnd()) ) | |
390 | { | |
391 | // And second, don't give focus away if the tab control itself has | |
392 | // it, as this is how the native property sheets behave: if you | |
393 | // explicitly click on the tab label giving it focus, it will | |
394 | // remain after switching to another page. But if the focus was | |
395 | // inside the notebook page, it switches to the new page. | |
396 | if ( !HasFocus() ) | |
397 | pPage->SetFocus(); | |
398 | } | |
399 | } | |
1d6fcbcc | 400 | |
681be2ef | 401 | m_selection = selNew; |
1d6fcbcc VZ |
402 | } |
403 | ||
404 | int wxNotebook::ChangeSelection(size_t nPage) | |
405 | { | |
406 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") ); | |
407 | ||
681be2ef | 408 | const int selOld = m_selection; |
937d5b60 | 409 | |
681be2ef | 410 | if ( m_selection == wxNOT_FOUND || nPage != (size_t)m_selection ) |
1d6fcbcc VZ |
411 | { |
412 | TabCtrl_SetCurSel(GetHwnd(), nPage); | |
413 | ||
414 | UpdateSelection(nPage); | |
415 | } | |
416 | ||
937d5b60 | 417 | return selOld; |
1d6fcbcc VZ |
418 | } |
419 | ||
8d34bf5c | 420 | bool wxNotebook::SetPageText(size_t nPage, const wxString& strText) |
88310e2e | 421 | { |
8ecd5de2 | 422 | wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") ); |
88310e2e | 423 | |
8ecd5de2 WS |
424 | TC_ITEM tcItem; |
425 | tcItem.mask = TCIF_TEXT; | |
017dc06b | 426 | tcItem.pszText = wxMSW_CONV_LPTSTR(strText); |
88310e2e | 427 | |
88a01064 JS |
428 | if ( !HasFlag(wxNB_MULTILINE) ) |
429 | return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0; | |
430 | ||
431 | // multiline - we need to set new page size if a line is added or removed | |
432 | int rows = GetRowCount(); | |
433 | bool ret = TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0; | |
434 | ||
435 | if ( ret && rows != GetRowCount() ) | |
436 | { | |
437 | const wxRect r = GetPageSize(); | |
438 | const size_t count = m_pages.Count(); | |
439 | for ( size_t page = 0; page < count; page++ ) | |
440 | m_pages[page]->SetSize(r); | |
441 | } | |
442 | ||
443 | return ret; | |
88310e2e VZ |
444 | } |
445 | ||
8d34bf5c | 446 | wxString wxNotebook::GetPageText(size_t nPage) const |
88310e2e | 447 | { |
8ecd5de2 | 448 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("notebook page out of range") ); |
88310e2e | 449 | |
8ecd5de2 WS |
450 | wxChar buf[256]; |
451 | TC_ITEM tcItem; | |
452 | tcItem.mask = TCIF_TEXT; | |
453 | tcItem.pszText = buf; | |
454 | tcItem.cchTextMax = WXSIZEOF(buf); | |
88310e2e | 455 | |
8ecd5de2 WS |
456 | wxString str; |
457 | if ( TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) ) | |
458 | str = tcItem.pszText; | |
88310e2e | 459 | |
8ecd5de2 | 460 | return str; |
88310e2e VZ |
461 | } |
462 | ||
8d34bf5c | 463 | int wxNotebook::GetPageImage(size_t nPage) const |
88310e2e | 464 | { |
8ecd5de2 | 465 | wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("notebook page out of range") ); |
88310e2e | 466 | |
8ecd5de2 WS |
467 | TC_ITEM tcItem; |
468 | tcItem.mask = TCIF_IMAGE; | |
88310e2e | 469 | |
5482ee07 VZ |
470 | return TabCtrl_GetItem(GetHwnd(), nPage, &tcItem) ? tcItem.iImage |
471 | : wxNOT_FOUND; | |
88310e2e VZ |
472 | } |
473 | ||
8d34bf5c | 474 | bool wxNotebook::SetPageImage(size_t nPage, int nImage) |
88310e2e | 475 | { |
8ecd5de2 | 476 | wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("notebook page out of range") ); |
88310e2e | 477 | |
8ecd5de2 WS |
478 | TC_ITEM tcItem; |
479 | tcItem.mask = TCIF_IMAGE; | |
480 | tcItem.iImage = nImage; | |
88310e2e | 481 | |
8ecd5de2 | 482 | return TabCtrl_SetItem(GetHwnd(), nPage, &tcItem) != 0; |
88310e2e VZ |
483 | } |
484 | ||
485 | void wxNotebook::SetImageList(wxImageList* imageList) | |
907f37b3 | 486 | { |
8ecd5de2 | 487 | wxNotebookBase::SetImageList(imageList); |
07b8d7ec | 488 | |
8ecd5de2 WS |
489 | if ( imageList ) |
490 | { | |
5482ee07 | 491 | (void) TabCtrl_SetImageList(GetHwnd(), GetHimagelistOf(imageList)); |
8ecd5de2 | 492 | } |
b656febd VS |
493 | } |
494 | ||
d9506e77 VZ |
495 | // ---------------------------------------------------------------------------- |
496 | // wxNotebook size settings | |
497 | // ---------------------------------------------------------------------------- | |
498 | ||
c3732409 VZ |
499 | wxRect wxNotebook::GetPageSize() const |
500 | { | |
501 | wxRect r; | |
502 | ||
503 | RECT rc; | |
504 | ::GetClientRect(GetHwnd(), &rc); | |
505 | ||
506 | // This check is to work around a bug in TabCtrl_AdjustRect which will | |
628eae0b VZ |
507 | // cause a crash on win2k or on XP with themes disabled if either |
508 | // wxNB_MULTILINE is used or tabs are placed on a side, if the rectangle | |
509 | // is too small. | |
510 | // | |
511 | // The value of 20 is chosen arbitrarily but seems to work | |
512 | if ( rc.right > 20 && rc.bottom > 20 ) | |
c3732409 | 513 | { |
01c8cbf5 | 514 | TabCtrl_AdjustRect(GetHwnd(), false, &rc); |
c3732409 VZ |
515 | |
516 | wxCopyRECTToRect(rc, r); | |
517 | } | |
518 | ||
519 | return r; | |
520 | } | |
521 | ||
d9506e77 VZ |
522 | void wxNotebook::SetPageSize(const wxSize& size) |
523 | { | |
524 | // transform the page size into the notebook size | |
525 | RECT rc; | |
526 | rc.left = | |
527 | rc.top = 0; | |
528 | rc.right = size.x; | |
529 | rc.bottom = size.y; | |
530 | ||
b8bdaa7c | 531 | TabCtrl_AdjustRect(GetHwnd(), true, &rc); |
d9506e77 VZ |
532 | |
533 | // and now set it | |
534 | SetSize(rc.right - rc.left, rc.bottom - rc.top); | |
535 | } | |
536 | ||
537 | void wxNotebook::SetPadding(const wxSize& padding) | |
538 | { | |
539 | TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y); | |
540 | } | |
42e69d6b VZ |
541 | |
542 | // Windows-only at present. Also, you must use the wxNB_FIXEDWIDTH | |
543 | // style. | |
544 | void wxNotebook::SetTabSize(const wxSize& sz) | |
545 | { | |
546 | ::SendMessage(GetHwnd(), TCM_SETITEMSIZE, 0, MAKELPARAM(sz.x, sz.y)); | |
547 | } | |
548 | ||
2ce7af35 JS |
549 | wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const |
550 | { | |
669c595d | 551 | // we can't use TabCtrl_AdjustRect here because it only works for wxNB_TOP |
2ce7af35 | 552 | wxSize sizeTotal = sizePage; |
078cf5cb | 553 | |
8b5d5223 | 554 | wxSize tabSize; |
669c595d | 555 | if ( GetPageCount() > 0 ) |
2ce7af35 JS |
556 | { |
557 | RECT rect; | |
669c595d | 558 | TabCtrl_GetItemRect(GetHwnd(), 0, &rect); |
2ce7af35 JS |
559 | tabSize.x = rect.right - rect.left; |
560 | tabSize.y = rect.bottom - rect.top; | |
561 | } | |
669c595d | 562 | |
49ac39e3 VZ |
563 | const int rows = GetRowCount(); |
564 | ||
669c595d VZ |
565 | // add an extra margin in both directions |
566 | const int MARGIN = 8; | |
567 | if ( IsVertical() ) | |
2ce7af35 | 568 | { |
669c595d | 569 | sizeTotal.x += MARGIN; |
49ac39e3 | 570 | sizeTotal.y += tabSize.y * rows + MARGIN; |
2ce7af35 | 571 | } |
669c595d | 572 | else // horizontal layout |
2ce7af35 | 573 | { |
49ac39e3 | 574 | sizeTotal.x += tabSize.x * rows + MARGIN; |
669c595d | 575 | sizeTotal.y += MARGIN; |
2ce7af35 JS |
576 | } |
577 | ||
578 | return sizeTotal; | |
579 | } | |
580 | ||
2015f2b3 VZ |
581 | void wxNotebook::AdjustPageSize(wxNotebookPage *page) |
582 | { | |
9a83f860 | 583 | wxCHECK_RET( page, wxT("NULL page in wxNotebook::AdjustPageSize") ); |
2015f2b3 | 584 | |
c3732409 VZ |
585 | const wxRect r = GetPageSize(); |
586 | if ( !r.IsEmpty() ) | |
14a6b6e5 | 587 | { |
c3732409 | 588 | page->SetSize(r); |
14a6b6e5 | 589 | } |
2015f2b3 VZ |
590 | } |
591 | ||
88310e2e VZ |
592 | // ---------------------------------------------------------------------------- |
593 | // wxNotebook operations | |
594 | // ---------------------------------------------------------------------------- | |
595 | ||
621793f4 | 596 | // remove one page from the notebook, without deleting |
8d34bf5c | 597 | wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage) |
621793f4 | 598 | { |
df7145da VZ |
599 | wxNotebookPage *pageRemoved = wxNotebookBase::DoRemovePage(nPage); |
600 | if ( !pageRemoved ) | |
601 | return NULL; | |
621793f4 | 602 | |
8138f406 VS |
603 | // hide the removed page to maintain the invariant that only the |
604 | // selected page is visible and others are hidden: | |
605 | pageRemoved->Show(false); | |
606 | ||
01c8cbf5 | 607 | TabCtrl_DeleteItem(GetHwnd(), nPage); |
621793f4 | 608 | |
df7145da VZ |
609 | if ( m_pages.IsEmpty() ) |
610 | { | |
611 | // no selection any more, the notebook becamse empty | |
681be2ef | 612 | m_selection = wxNOT_FOUND; |
df7145da VZ |
613 | } |
614 | else // notebook still not empty | |
615 | { | |
01c8cbf5 | 616 | int selNew = TabCtrl_GetCurSel(GetHwnd()); |
5482ee07 | 617 | if ( selNew != wxNOT_FOUND ) |
df7145da | 618 | { |
623f5f70 | 619 | // No selection change, just refresh the current selection. |
078cf5cb WS |
620 | // Because it could be that the slection index changed |
621 | // we need to update it. | |
623f5f70 | 622 | // Note: this does not mean the selection it self changed. |
681be2ef VZ |
623 | m_selection = selNew; |
624 | m_pages[m_selection]->Refresh(); | |
df7145da | 625 | } |
681be2ef | 626 | else if (int(nPage) == m_selection) |
43a997b6 | 627 | { |
623f5f70 | 628 | // The selection was deleted. |
078cf5cb | 629 | |
623f5f70 | 630 | // Determine new selection. |
681be2ef VZ |
631 | if (m_selection == int(GetPageCount())) |
632 | selNew = m_selection - 1; | |
623f5f70 | 633 | else |
681be2ef | 634 | selNew = m_selection; |
078cf5cb | 635 | |
681be2ef | 636 | // m_selection must be always valid so reset it before calling |
43a997b6 | 637 | // SetSelection() |
681be2ef | 638 | m_selection = wxNOT_FOUND; |
43a997b6 VZ |
639 | SetSelection(selNew); |
640 | } | |
623f5f70 JS |
641 | else |
642 | { | |
643 | wxFAIL; // Windows did not behave ok. | |
644 | } | |
df7145da | 645 | } |
47f12f58 | 646 | |
df7145da | 647 | return pageRemoved; |
621793f4 JS |
648 | } |
649 | ||
88310e2e VZ |
650 | // remove all pages |
651 | bool wxNotebook::DeleteAllPages() | |
652 | { | |
7ec69821 WS |
653 | size_t nPageCount = GetPageCount(); |
654 | size_t nPage; | |
655 | for ( nPage = 0; nPage < nPageCount; nPage++ ) | |
656 | delete m_pages[nPage]; | |
88310e2e | 657 | |
7ec69821 | 658 | m_pages.Clear(); |
88310e2e | 659 | |
7ec69821 | 660 | TabCtrl_DeleteAllItems(GetHwnd()); |
907f37b3 | 661 | |
681be2ef | 662 | m_selection = wxNOT_FOUND; |
47f12f58 | 663 | |
7ec69821 WS |
664 | InvalidateBestSize(); |
665 | return true; | |
88310e2e VZ |
666 | } |
667 | ||
88310e2e | 668 | // same as AddPage() but does it at given position |
8d34bf5c | 669 | bool wxNotebook::InsertPage(size_t nPage, |
88310e2e VZ |
670 | wxNotebookPage *pPage, |
671 | const wxString& strText, | |
672 | bool bSelect, | |
673 | int imageId) | |
674 | { | |
9a83f860 | 675 | wxCHECK_MSG( pPage != NULL, false, wxT("NULL page in wxNotebook::InsertPage") ); |
b8bdaa7c | 676 | wxCHECK_MSG( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), false, |
9a83f860 | 677 | wxT("invalid index in wxNotebook::InsertPage") ); |
88310e2e | 678 | |
efa14cf2 | 679 | wxASSERT_MSG( pPage->GetParent() == this, |
9a83f860 | 680 | wxT("notebook pages must have notebook as parent") ); |
43427087 | 681 | |
22f3361e VZ |
682 | // add a new tab to the control |
683 | // ---------------------------- | |
58a8ab88 | 684 | |
22f3361e VZ |
685 | // init all fields to 0 |
686 | TC_ITEM tcItem; | |
687 | wxZeroMemory(tcItem); | |
58a8ab88 | 688 | |
22f3361e VZ |
689 | // set the image, if any |
690 | if ( imageId != -1 ) | |
691 | { | |
692 | tcItem.mask |= TCIF_IMAGE; | |
693 | tcItem.iImage = imageId; | |
694 | } | |
88310e2e | 695 | |
22f3361e | 696 | // and the text |
8b5d5223 | 697 | if ( !strText.empty() ) |
22f3361e VZ |
698 | { |
699 | tcItem.mask |= TCIF_TEXT; | |
017dc06b | 700 | tcItem.pszText = wxMSW_CONV_LPTSTR(strText); |
22f3361e | 701 | } |
43427087 | 702 | |
e830a6a6 RD |
703 | // hide the page: unless it is selected, it shouldn't be shown (and if it |
704 | // is selected it will be shown later) | |
705 | HWND hwnd = GetWinHwnd(pPage); | |
706 | SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_VISIBLE); | |
707 | ||
708 | // this updates internal flag too -- otherwise it would get out of sync | |
709 | // with the real state | |
710 | pPage->Show(false); | |
711 | ||
712 | ||
22f3361e VZ |
713 | // fit the notebook page to the tab control's display area: this should be |
714 | // done before adding it to the notebook or TabCtrl_InsertItem() will | |
715 | // change the notebooks size itself! | |
2015f2b3 | 716 | AdjustPageSize(pPage); |
43427087 | 717 | |
22f3361e | 718 | // finally do insert it |
01c8cbf5 | 719 | if ( TabCtrl_InsertItem(GetHwnd(), nPage, &tcItem) == -1 ) |
2015f2b3 | 720 | { |
22f3361e | 721 | wxLogError(wxT("Can't create the notebook page '%s'."), strText.c_str()); |
88310e2e | 722 | |
b8bdaa7c | 723 | return false; |
22f3361e | 724 | } |
88310e2e | 725 | |
ebd9ec29 JG |
726 | // need to update the bg brush when the first page is added |
727 | // so the first panel gets the correct themed background | |
728 | if ( m_pages.empty() ) | |
729 | { | |
11e70ae6 | 730 | #if wxUSE_UXTHEME |
ebd9ec29 | 731 | UpdateBgBrush(); |
11e70ae6 | 732 | #endif // wxUSE_UXTHEME |
ebd9ec29 JG |
733 | } |
734 | ||
22f3361e VZ |
735 | // succeeded: save the pointer to the page |
736 | m_pages.Insert(pPage, nPage); | |
42e69d6b | 737 | |
56b9925b VZ |
738 | // we may need to adjust the size again if the notebook size changed: |
739 | // normally this only happens for the first page we add (the tabs which | |
740 | // hadn't been there before are now shown) but for a multiline notebook it | |
741 | // can happen for any page at all as a new row could have been started | |
742 | if ( m_pages.GetCount() == 1 || HasFlag(wxNB_MULTILINE) ) | |
2015f2b3 VZ |
743 | { |
744 | AdjustPageSize(pPage); | |
80ceadc1 VZ |
745 | |
746 | // Additionally, force the layout of the notebook itself by posting a | |
747 | // size event to it. If we don't do it, notebooks with pages on the | |
748 | // left or the right side may fail to account for the fact that they | |
749 | // are now big enough to fit all all of their pages on one row and | |
750 | // still reserve space for the second row of tabs, see #1792. | |
751 | const wxSize s = GetSize(); | |
752 | ::PostMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED, MAKELPARAM(s.x, s.y)); | |
2015f2b3 VZ |
753 | } |
754 | ||
22f3361e VZ |
755 | // now deal with the selection |
756 | // --------------------------- | |
757 | ||
758 | // if the inserted page is before the selected one, we must update the | |
759 | // index of the selected page | |
681be2ef | 760 | if ( int(nPage) <= m_selection ) |
22f3361e VZ |
761 | { |
762 | // one extra page added | |
681be2ef | 763 | m_selection++; |
22f3361e VZ |
764 | } |
765 | ||
60d5c563 | 766 | DoSetSelectionAfterInsertion(nPage, bSelect); |
22f3361e | 767 | |
37144cf0 | 768 | InvalidateBestSize(); |
25057aba | 769 | |
b8bdaa7c | 770 | return true; |
88310e2e VZ |
771 | } |
772 | ||
e450aa69 | 773 | int wxNotebook::HitTest(const wxPoint& pt, long *flags) const |
ef094fa0 JS |
774 | { |
775 | TC_HITTESTINFO hitTestInfo; | |
776 | hitTestInfo.pt.x = pt.x; | |
777 | hitTestInfo.pt.y = pt.y; | |
e450aa69 | 778 | int item = TabCtrl_HitTest(GetHwnd(), &hitTestInfo); |
ef094fa0 | 779 | |
e450aa69 VZ |
780 | if ( flags ) |
781 | { | |
782 | *flags = 0; | |
783 | ||
784 | if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE) | |
9804d540 | 785 | *flags |= wxBK_HITTEST_NOWHERE; |
e450aa69 | 786 | if ((hitTestInfo.flags & TCHT_ONITEM) == TCHT_ONITEM) |
9804d540 | 787 | *flags |= wxBK_HITTEST_ONITEM; |
e450aa69 | 788 | if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON) |
9804d540 | 789 | *flags |= wxBK_HITTEST_ONICON; |
e450aa69 | 790 | if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL) |
9804d540 | 791 | *flags |= wxBK_HITTEST_ONLABEL; |
22a35096 | 792 | if ( item == wxNOT_FOUND && GetPageSize().Contains(pt) ) |
9804d540 | 793 | *flags |= wxBK_HITTEST_ONPAGE; |
e450aa69 | 794 | } |
ef094fa0 JS |
795 | |
796 | return item; | |
797 | } | |
798 | ||
014ee083 VZ |
799 | // ---------------------------------------------------------------------------- |
800 | // flicker-less notebook redraw | |
801 | // ---------------------------------------------------------------------------- | |
802 | ||
803 | #if USE_NOTEBOOK_ANTIFLICKER | |
804 | ||
805 | // wnd proc for the spin button | |
806 | LRESULT APIENTRY _EXPORT wxNotebookSpinBtnWndProc(HWND hwnd, | |
807 | UINT message, | |
808 | WPARAM wParam, | |
809 | LPARAM lParam) | |
810 | { | |
811 | if ( message == WM_ERASEBKGND ) | |
812 | return 0; | |
813 | ||
814 | return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebookSpinBtn, | |
815 | hwnd, message, wParam, lParam); | |
816 | } | |
817 | ||
6143d3b6 VZ |
818 | LRESULT APIENTRY _EXPORT wxNotebookWndProc(HWND hwnd, |
819 | UINT message, | |
820 | WPARAM wParam, | |
821 | LPARAM lParam) | |
822 | { | |
823 | return ::CallWindowProc(CASTWNDPROC gs_wndprocNotebook, | |
824 | hwnd, message, wParam, lParam); | |
825 | } | |
826 | ||
014ee083 VZ |
827 | void wxNotebook::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
828 | { | |
829 | // do nothing here | |
830 | } | |
831 | ||
832 | void wxNotebook::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
833 | { | |
834 | wxPaintDC dc(this); | |
835 | wxMemoryDC memdc; | |
836 | RECT rc; | |
837 | ::GetClientRect(GetHwnd(), &rc); | |
838 | wxBitmap bmp(rc.right, rc.bottom); | |
839 | memdc.SelectObject(bmp); | |
840 | ||
6614aa49 VZ |
841 | const wxLayoutDirection dir = dc.GetLayoutDirection(); |
842 | memdc.SetLayoutDirection(dir); | |
843 | ||
b8d8362c VZ |
844 | const HDC hdc = GetHdcOf(memdc); |
845 | ||
846 | // The drawing logic of the native tab control is absolutely impenetrable | |
847 | // but observation shows that in the current Windows versions (XP and 7), | |
848 | // the tab control always erases its entire background in its window proc | |
849 | // when the tabs are top-aligned but does not do it when the tabs are in | |
850 | // any other position. | |
851 | // | |
852 | // This means that we can't rely on our background colour being used for | |
853 | // the blank area in the tab row because this doesn't work in the default | |
854 | // top-aligned case, hence the hack with ExtFloodFill() below. But it also | |
855 | // means that we still do need to erase the DC to account for the other | |
856 | // cases. | |
857 | // | |
858 | // Moreover, just in case some very old or very new (or even future, | |
859 | // although it seems unlikely that this is ever going to change by now) | |
860 | // version of Windows didn't do it like this, do both things in all cases | |
861 | // instead of optimizing away the one of them which doesn't do anything for | |
862 | // the effectively used tab orientation -- better safe than fast. | |
863 | ||
864 | // Notice that we use our own background here, not the background used for | |
865 | // the pages, because the tab row background must blend with the parent and | |
866 | // so the background colour inherited from it (if any) must be used. | |
867 | AutoHBRUSH hbr(wxColourToRGB(GetBackgroundColour())); | |
868 | ||
869 | ::FillRect(hdc, &rc, hbr); | |
870 | ||
871 | MSWDefWindowProc(WM_PAINT, (WPARAM)hdc, 0); | |
872 | ||
873 | // At least for the top-aligned tabs, our background colour was overwritten | |
874 | // and so we now replace the default background with our colour. This is | |
875 | // horribly inefficient, of course, but seems to be the only way to do it. | |
876 | if ( UseBgCol() ) | |
e32703a9 | 877 | { |
b8d8362c | 878 | SelectInHDC selectBrush(hdc, hbr); |
014ee083 | 879 | |
b8d8362c VZ |
880 | // Find the point which must contain the default background colour: |
881 | // this is a hack, of course, but using this point "close" to the | |
882 | // corner seems to work fine in practice. | |
883 | int x = 0, | |
884 | y = 0; | |
014ee083 | 885 | |
b8d8362c VZ |
886 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) |
887 | { | |
888 | case wxBK_TOP: | |
889 | x = rc.right - 2; | |
890 | y = 2; | |
891 | break; | |
892 | ||
893 | case wxBK_BOTTOM: | |
894 | x = rc.right - 2; | |
895 | y = rc.bottom - 2; | |
896 | break; | |
897 | ||
898 | case wxBK_LEFT: | |
899 | x = 2; | |
900 | y = rc.bottom - 2; | |
901 | break; | |
902 | ||
903 | case wxBK_RIGHT: | |
904 | x = 2; | |
905 | y = rc.bottom - 2; | |
906 | break; | |
907 | } | |
888dde65 | 908 | |
b8d8362c VZ |
909 | ::ExtFloodFill(hdc, x, y, ::GetSysColor(COLOR_BTNFACE), FLOODFILLSURFACE); |
910 | } | |
014ee083 | 911 | |
f4322df6 | 912 | // For some reason in RTL mode, source offset has to be -1, otherwise the |
6614aa49 VZ |
913 | // right border (physical) remains unpainted. |
914 | const wxCoord ofs = dir == wxLayout_RightToLeft ? -1 : 0; | |
915 | dc.Blit(ofs, 0, rc.right, rc.bottom, &memdc, ofs, 0); | |
014ee083 VZ |
916 | } |
917 | ||
918 | #endif // USE_NOTEBOOK_ANTIFLICKER | |
e450aa69 | 919 | |
88310e2e VZ |
920 | // ---------------------------------------------------------------------------- |
921 | // wxNotebook callbacks | |
922 | // ---------------------------------------------------------------------------- | |
923 | ||
9026ad85 | 924 | void wxNotebook::OnSize(wxSizeEvent& event) |
88310e2e | 925 | { |
7dccdf81 | 926 | if ( GetPageCount() == 0 ) |
f7eaa62f JS |
927 | { |
928 | // Prevents droppings on resize, but does cause some flicker | |
929 | // when there are no pages. | |
2aa24b60 | 930 | Refresh(); |
f7eaa62f JS |
931 | event.Skip(); |
932 | return; | |
933 | } | |
254fbd14 JS |
934 | #ifndef __WXWINCE__ |
935 | else | |
936 | { | |
937 | // Without this, we can sometimes get droppings at the edges | |
938 | // of a notebook, for example a notebook in a splitter window. | |
939 | // This needs to be reconciled with the RefreshRect calls | |
940 | // at the end of this function, which weren't enough to prevent | |
941 | // the droppings. | |
01c8cbf5 | 942 | |
254fbd14 JS |
943 | wxSize sz = GetClientSize(); |
944 | ||
945 | // Refresh right side | |
946 | wxRect rect(sz.x-4, 0, 4, sz.y); | |
947 | RefreshRect(rect); | |
01c8cbf5 | 948 | |
254fbd14 JS |
949 | // Refresh bottom side |
950 | rect = wxRect(0, sz.y-4, sz.x, 4); | |
951 | RefreshRect(rect); | |
01c8cbf5 | 952 | |
254fbd14 JS |
953 | // Refresh left side |
954 | rect = wxRect(0, 0, 4, sz.y); | |
955 | RefreshRect(rect); | |
956 | } | |
014ee083 | 957 | #endif // !__WXWINCE__ |
f7eaa62f | 958 | |
c1637c89 | 959 | // fit all the notebook pages to the tab control's display area |
56b9925b | 960 | |
c1637c89 VZ |
961 | RECT rc; |
962 | rc.left = rc.top = 0; | |
963 | GetSize((int *)&rc.right, (int *)&rc.bottom); | |
56b9925b | 964 | |
c1637c89 VZ |
965 | // save the total size, we'll use it below |
966 | int widthNbook = rc.right - rc.left, | |
967 | heightNbook = rc.bottom - rc.top; | |
968 | ||
969 | // there seems to be a bug in the implementation of TabCtrl_AdjustRect(): it | |
970 | // returns completely false values for multiline tab controls after the tabs | |
971 | // are added but before getting the first WM_SIZE (off by ~50 pixels, see | |
972 | // | |
973 | // http://sf.net/tracker/index.php?func=detail&aid=645323&group_id=9863&atid=109863 | |
974 | // | |
975 | // and the only work around I could find was this ugly hack... without it | |
976 | // simply toggling the "multiline" checkbox in the notebook sample resulted | |
977 | // in a noticeable page displacement | |
978 | if ( HasFlag(wxNB_MULTILINE) ) | |
979 | { | |
980 | // avoid an infinite recursion: we get another notification too! | |
981 | static bool s_isInOnSize = false; | |
4b7f2165 | 982 | |
c1637c89 VZ |
983 | if ( !s_isInOnSize ) |
984 | { | |
985 | s_isInOnSize = true; | |
986 | SendMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED, | |
987 | MAKELPARAM(rc.right, rc.bottom)); | |
988 | s_isInOnSize = false; | |
989 | } | |
49ac39e3 VZ |
990 | |
991 | // The best size depends on the number of rows of tabs, which can | |
992 | // change when the notepad is resized. | |
993 | InvalidateBestSize(); | |
c1637c89 | 994 | } |
b5c3b538 | 995 | |
e2f6f8da RD |
996 | #if wxUSE_UXTHEME |
997 | // background bitmap size has changed, update the brush using it too | |
998 | UpdateBgBrush(); | |
999 | #endif // wxUSE_UXTHEME | |
1000 | ||
01c8cbf5 | 1001 | TabCtrl_AdjustRect(GetHwnd(), false, &rc); |
c1637c89 VZ |
1002 | |
1003 | int width = rc.right - rc.left, | |
1004 | height = rc.bottom - rc.top; | |
1005 | size_t nCount = m_pages.Count(); | |
1006 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) { | |
1007 | wxNotebookPage *pPage = m_pages[nPage]; | |
1008 | pPage->SetSize(rc.left, rc.top, width, height); | |
1009 | } | |
1010 | ||
1011 | ||
1012 | // unless we had already repainted everything, we now need to refresh | |
1013 | if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) ) | |
1014 | { | |
1015 | // invalidate areas not covered by pages | |
1016 | RefreshRect(wxRect(0, 0, widthNbook, rc.top), false); | |
1017 | RefreshRect(wxRect(0, rc.top, rc.left, height), false); | |
1018 | RefreshRect(wxRect(0, rc.bottom, widthNbook, heightNbook - rc.bottom), | |
1019 | false); | |
3550706d | 1020 | RefreshRect(wxRect(rc.right, rc.top, widthNbook - rc.right, height), |
c1637c89 VZ |
1021 | false); |
1022 | } | |
1023 | ||
014ee083 VZ |
1024 | #if USE_NOTEBOOK_ANTIFLICKER |
1025 | // subclass the spin control used by the notebook to scroll pages to | |
1026 | // prevent it from flickering on resize | |
dfb47d83 | 1027 | if ( !m_hasSubclassedUpdown ) |
014ee083 VZ |
1028 | { |
1029 | // iterate over all child windows to find spin button | |
1030 | for ( HWND child = ::GetWindow(GetHwnd(), GW_CHILD); | |
1031 | child; | |
1032 | child = ::GetWindow(child, GW_HWNDNEXT) ) | |
1033 | { | |
1034 | wxWindow *childWindow = wxFindWinFromHandle((WXHWND)child); | |
1035 | ||
1036 | // see if it exists, if no wxWindow found then assume it's the spin | |
1037 | // btn | |
1038 | if ( !childWindow ) | |
1039 | { | |
1040 | // subclass the spin button to override WM_ERASEBKGND | |
dfb47d83 VZ |
1041 | if ( !gs_wndprocNotebookSpinBtn ) |
1042 | gs_wndprocNotebookSpinBtn = (WXFARPROC)wxGetWindowProc(child); | |
014ee083 VZ |
1043 | |
1044 | wxSetWindowProc(child, wxNotebookSpinBtnWndProc); | |
dfb47d83 | 1045 | m_hasSubclassedUpdown = true; |
014ee083 VZ |
1046 | break; |
1047 | } | |
1048 | } | |
1049 | } | |
feb32e31 VZ |
1050 | |
1051 | // Probably because of the games we play above to avoid flicker sometimes | |
1052 | // the text controls inside notebook pages are not shown correctly (they | |
1053 | // don't have their borders) when the notebook is shown for the first time. | |
1054 | // It's not really clear why does this happen and maybe the bug is in | |
1055 | // wxTextCtrl itself and not here but updating the page when it's about to | |
1056 | // be shown doesn't cost much and works around the problem so do it here | |
1057 | // for now. | |
1058 | if ( !m_doneUpdateHack && IsShownOnScreen() ) | |
1059 | { | |
1060 | m_doneUpdateHack = true; | |
1061 | wxWindow* const page = GetCurrentPage(); | |
1062 | if ( page ) | |
1063 | page->Update(); | |
1064 | } | |
014ee083 VZ |
1065 | #endif // USE_NOTEBOOK_ANTIFLICKER |
1066 | ||
c1637c89 | 1067 | event.Skip(); |
88310e2e VZ |
1068 | } |
1069 | ||
88310e2e VZ |
1070 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) |
1071 | { | |
d9506e77 VZ |
1072 | if ( event.IsWindowChange() ) { |
1073 | // change pages | |
1074 | AdvanceSelection(event.GetDirection()); | |
1075 | } | |
1076 | else { | |
b8bdaa7c | 1077 | // we get this event in 3 cases |
d9506e77 VZ |
1078 | // |
1079 | // a) one of our pages might have generated it because the user TABbed | |
1080 | // out from it in which case we should propagate the event upwards and | |
1081 | // our parent will take care of setting the focus to prev/next sibling | |
1082 | // | |
1083 | // or | |
1084 | // | |
1085 | // b) the parent panel wants to give the focus to us so that we | |
1086 | // forward it to our selected page. We can't deal with this in | |
1087 | // OnSetFocus() because we don't know which direction the focus came | |
1088 | // from in this case and so can't choose between setting the focus to | |
1089 | // first or last panel child | |
b8bdaa7c VZ |
1090 | // |
1091 | // or | |
1092 | // | |
1093 | // c) we ourselves (see MSWTranslateMessage) generated the event | |
1094 | // | |
1095 | wxWindow * const parent = GetParent(); | |
1096 | ||
3c99602b MB |
1097 | // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE |
1098 | const bool isFromParent = event.GetEventObject() == (wxObject*) parent; | |
1099 | const bool isFromSelf = event.GetEventObject() == (wxObject*) this; | |
4a0dcc87 | 1100 | const bool isForward = event.GetDirection(); |
b8bdaa7c | 1101 | |
4a0dcc87 VS |
1102 | if ( isFromSelf && !isForward ) |
1103 | { | |
1104 | // focus is currently on notebook tab and should leave | |
1105 | // it backwards (Shift-TAB) | |
1106 | event.SetCurrentFocus(this); | |
1107 | parent->HandleWindowEvent(event); | |
1108 | } | |
1109 | else if ( isFromParent || isFromSelf ) | |
d9506e77 | 1110 | { |
b8bdaa7c | 1111 | // no, it doesn't come from child, case (b) or (c): forward to a |
4a0dcc87 VS |
1112 | // page but only if entering notebook page (i.e. direction is |
1113 | // backwards (Shift-TAB) comething from out-of-notebook, or | |
1114 | // direction is forward (TAB) from ourselves), | |
681be2ef | 1115 | if ( m_selection != wxNOT_FOUND && |
b8bdaa7c | 1116 | (!event.GetDirection() || isFromSelf) ) |
d9506e77 VZ |
1117 | { |
1118 | // so that the page knows that the event comes from it's parent | |
1119 | // and is being propagated downwards | |
1120 | event.SetEventObject(this); | |
1121 | ||
681be2ef | 1122 | wxWindow *page = m_pages[m_selection]; |
937013e0 | 1123 | if ( !page->HandleWindowEvent(event) ) |
d9506e77 VZ |
1124 | { |
1125 | page->SetFocus(); | |
1126 | } | |
1127 | //else: page manages focus inside it itself | |
1128 | } | |
b8bdaa7c | 1129 | else // otherwise set the focus to the notebook itself |
d9506e77 | 1130 | { |
d9506e77 VZ |
1131 | SetFocus(); |
1132 | } | |
1133 | } | |
1134 | else | |
1135 | { | |
b8bdaa7c VZ |
1136 | // it comes from our child, case (a), pass to the parent, but only |
1137 | // if the direction is forwards. Otherwise set the focus to the | |
1138 | // notebook itself. The notebook is always the 'first' control of a | |
1139 | // page. | |
4a0dcc87 | 1140 | if ( !isForward ) |
b8bdaa7c VZ |
1141 | { |
1142 | SetFocus(); | |
1143 | } | |
1144 | else if ( parent ) | |
1145 | { | |
d9506e77 | 1146 | event.SetCurrentFocus(this); |
937013e0 | 1147 | parent->HandleWindowEvent(event); |
d9506e77 VZ |
1148 | } |
1149 | } | |
88310e2e | 1150 | } |
88310e2e VZ |
1151 | } |
1152 | ||
caf95d2a VZ |
1153 | #if wxUSE_UXTHEME |
1154 | ||
c3732409 | 1155 | bool wxNotebook::DoDrawBackground(WXHDC hDC, wxWindow *child) |
caf95d2a | 1156 | { |
c3732409 VZ |
1157 | wxUxThemeHandle theme(child ? child : this, L"TAB"); |
1158 | if ( !theme ) | |
1159 | return false; | |
1160 | ||
1161 | // get the notebook client rect (we're not interested in drawing tabs | |
1162 | // themselves) | |
1163 | wxRect r = GetPageSize(); | |
1164 | if ( r.IsEmpty() ) | |
1165 | return false; | |
1166 | ||
c4a95f6f | 1167 | RECT rc; |
c3732409 VZ |
1168 | wxCopyRectToRECT(r, rc); |
1169 | ||
1170 | // map rect to the coords of the window we're drawing in | |
1171 | if ( child ) | |
1172 | ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2); | |
1173 | ||
2ff53fd3 JS |
1174 | // we have the content area (page size), but we need to draw all of the |
1175 | // background for it to be aligned correctly | |
1176 | wxUxThemeEngine::Get()->GetThemeBackgroundExtent | |
1177 | ( | |
1178 | theme, | |
1179 | (HDC) hDC, | |
1180 | 9 /* TABP_PANE */, | |
1181 | 0, | |
1182 | &rc, | |
1183 | &rc | |
1184 | ); | |
c3732409 VZ |
1185 | wxUxThemeEngine::Get()->DrawThemeBackground |
1186 | ( | |
1187 | theme, | |
92199f4c | 1188 | (HDC) hDC, |
c3732409 VZ |
1189 | 9 /* TABP_PANE */, |
1190 | 0, | |
1191 | &rc, | |
1192 | NULL | |
1193 | ); | |
1194 | ||
1195 | return true; | |
1196 | } | |
de359565 | 1197 | |
c3732409 VZ |
1198 | WXHBRUSH wxNotebook::QueryBgBitmap() |
1199 | { | |
1200 | wxRect r = GetPageSize(); | |
1201 | if ( r.IsEmpty() ) | |
1202 | return 0; | |
c4a95f6f VZ |
1203 | |
1204 | WindowHDC hDC(GetHwnd()); | |
1205 | MemoryHDC hDCMem(hDC); | |
c3732409 | 1206 | CompatibleBitmap hBmp(hDC, r.x + r.width, r.y + r.height); |
c4a95f6f VZ |
1207 | |
1208 | SelectInHDC selectBmp(hDCMem, hBmp); | |
caf95d2a | 1209 | |
c3732409 VZ |
1210 | if ( !DoDrawBackground((WXHDC)(HDC)hDCMem) ) |
1211 | return 0; | |
0f770734 | 1212 | |
de359565 | 1213 | return (WXHBRUSH)::CreatePatternBrush(hBmp); |
c4a95f6f | 1214 | } |
caf95d2a | 1215 | |
c4a95f6f VZ |
1216 | void wxNotebook::UpdateBgBrush() |
1217 | { | |
1218 | if ( m_hbrBackground ) | |
1219 | ::DeleteObject((HBRUSH)m_hbrBackground); | |
caf95d2a | 1220 | |
c4a95f6f VZ |
1221 | if ( !m_hasBgCol && wxUxThemeEngine::GetIfActive() ) |
1222 | { | |
de359565 | 1223 | m_hbrBackground = QueryBgBitmap(); |
caf95d2a | 1224 | } |
c3732409 | 1225 | else // no themes or we've got user-defined solid colour |
caf95d2a VZ |
1226 | { |
1227 | m_hbrBackground = NULL; | |
1228 | } | |
1229 | } | |
1230 | ||
c3732409 | 1231 | bool wxNotebook::MSWPrintChild(WXHDC hDC, wxWindow *child) |
07c19327 | 1232 | { |
c3732409 VZ |
1233 | // solid background colour overrides themed background drawing |
1234 | if ( !UseBgCol() && DoDrawBackground(hDC, child) ) | |
1235 | return true; | |
de359565 | 1236 | |
4dab5279 JS |
1237 | // If we're using a solid colour (for example if we've switched off |
1238 | // theming for this notebook), paint it | |
1239 | if (UseBgCol()) | |
1240 | { | |
1241 | wxRect r = GetPageSize(); | |
1242 | if ( r.IsEmpty() ) | |
1243 | return false; | |
1244 | ||
1245 | RECT rc; | |
1246 | wxCopyRectToRECT(r, rc); | |
1247 | ||
1248 | // map rect to the coords of the window we're drawing in | |
1249 | if ( child ) | |
1250 | ::MapWindowPoints(GetHwnd(), GetHwndOf(child), (POINT *)&rc, 2); | |
1251 | ||
1252 | wxBrush brush(GetBackgroundColour()); | |
1253 | HBRUSH hbr = GetHbrushOf(brush); | |
01c8cbf5 | 1254 | |
4dab5279 JS |
1255 | ::FillRect((HDC) hDC, &rc, hbr); |
1256 | ||
1257 | return true; | |
1258 | } | |
1259 | ||
c3732409 | 1260 | return wxNotebookBase::MSWPrintChild(hDC, child); |
07c19327 VZ |
1261 | } |
1262 | ||
caf95d2a VZ |
1263 | #endif // wxUSE_UXTHEME |
1264 | ||
25057aba JS |
1265 | // Windows only: attempts to get colour for UX theme page background |
1266 | wxColour wxNotebook::GetThemeBackgroundColour() const | |
1267 | { | |
1268 | #if wxUSE_UXTHEME | |
1269 | if (wxUxThemeEngine::Get()) | |
1270 | { | |
1271 | wxUxThemeHandle hTheme((wxNotebook*) this, L"TAB"); | |
1272 | if (hTheme) | |
1273 | { | |
1274 | // This is total guesswork. | |
b380533c JS |
1275 | // See PlatformSDK\Include\Tmschema.h for values. |
1276 | // JACS: can also use 9 (TABP_PANE) | |
25057aba | 1277 | COLORREF themeColor; |
b380533c | 1278 | bool success = (S_OK == wxUxThemeEngine::Get()->GetThemeColor( |
25057aba JS |
1279 | hTheme, |
1280 | 10 /* TABP_BODY */, | |
1281 | 1 /* NORMAL */, | |
1282 | 3821 /* FILLCOLORHINT */, | |
b380533c JS |
1283 | &themeColor)); |
1284 | if (!success) | |
1285 | return GetBackgroundColour(); | |
25057aba JS |
1286 | |
1287 | /* | |
1288 | [DS] Workaround for WindowBlinds: | |
1289 | Some themes return a near black theme color using FILLCOLORHINT, | |
1290 | this makes notebook pages have an ugly black background and makes | |
1291 | text (usually black) unreadable. Retry again with FILLCOLOR. | |
1292 | ||
1293 | This workaround potentially breaks appearance of some themes, | |
1294 | but in practice it already fixes some themes. | |
1295 | */ | |
1296 | if (themeColor == 1) | |
1297 | { | |
1298 | wxUxThemeEngine::Get()->GetThemeColor( | |
1299 | hTheme, | |
1300 | 10 /* TABP_BODY */, | |
1301 | 1 /* NORMAL */, | |
1302 | 3802 /* FILLCOLOR */, | |
1303 | &themeColor); | |
1304 | } | |
1305 | ||
b380533c JS |
1306 | wxColour colour = wxRGBToColour(themeColor); |
1307 | ||
1308 | // Under Vista, the tab background colour is reported incorrectly. | |
1309 | // So for the default theme at least, hard-code the colour to something | |
1310 | // that will blend in. | |
1311 | ||
1312 | static int s_AeroStatus = -1; | |
1313 | if (s_AeroStatus == -1) | |
1314 | { | |
1315 | WCHAR szwThemeFile[1024]; | |
1316 | WCHAR szwThemeColor[256]; | |
1317 | if (S_OK == wxUxThemeEngine::Get()->GetCurrentThemeName(szwThemeFile, 1024, szwThemeColor, 256, NULL, 0)) | |
1318 | { | |
1319 | wxString themeFile(szwThemeFile), themeColor(szwThemeColor); | |
1320 | if (themeFile.Find(wxT("Aero")) != -1 && themeColor == wxT("NormalColor")) | |
1321 | s_AeroStatus = 1; | |
1322 | else | |
1323 | s_AeroStatus = 0; | |
1324 | } | |
1325 | else | |
1326 | s_AeroStatus = 0; | |
1327 | } | |
1328 | ||
1329 | if (s_AeroStatus == 1) | |
1330 | colour = wxColour(255, 255, 255); | |
1331 | ||
1332 | return colour; | |
25057aba JS |
1333 | } |
1334 | } | |
1335 | #endif // wxUSE_UXTHEME | |
1336 | ||
1337 | return GetBackgroundColour(); | |
1338 | } | |
1339 | ||
88310e2e VZ |
1340 | // ---------------------------------------------------------------------------- |
1341 | // wxNotebook base class virtuals | |
1342 | // ---------------------------------------------------------------------------- | |
b5c3b538 | 1343 | |
0b481c72 VZ |
1344 | #if wxUSE_CONSTRAINTS |
1345 | ||
b5c3b538 VZ |
1346 | // override these 2 functions to do nothing: everything is done in OnSize |
1347 | ||
4b7f2165 | 1348 | void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse)) |
b5c3b538 VZ |
1349 | { |
1350 | // don't set the sizes of the pages - their correct size is not yet known | |
b8bdaa7c | 1351 | wxControl::SetConstraintSizes(false); |
b5c3b538 VZ |
1352 | } |
1353 | ||
4b7f2165 | 1354 | bool wxNotebook::DoPhase(int WXUNUSED(nPhase)) |
b5c3b538 | 1355 | { |
b8bdaa7c | 1356 | return true; |
b5c3b538 VZ |
1357 | } |
1358 | ||
0b481c72 VZ |
1359 | #endif // wxUSE_CONSTRAINTS |
1360 | ||
0df3fbd7 VZ |
1361 | // ---------------------------------------------------------------------------- |
1362 | // wxNotebook Windows message handlers | |
1363 | // ---------------------------------------------------------------------------- | |
1364 | ||
1365 | bool wxNotebook::MSWOnScroll(int orientation, WXWORD nSBCode, | |
1366 | WXWORD pos, WXHWND control) | |
1367 | { | |
1368 | // don't generate EVT_SCROLLWIN events for the WM_SCROLLs coming from the | |
1369 | // up-down control | |
1370 | if ( control ) | |
b8bdaa7c | 1371 | return false; |
0df3fbd7 VZ |
1372 | |
1373 | return wxNotebookBase::MSWOnScroll(orientation, nSBCode, pos, control); | |
1374 | } | |
1375 | ||
a23fd0e1 | 1376 | bool wxNotebook::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result) |
88310e2e | 1377 | { |
3e97a905 | 1378 | wxBookCtrlEvent event(wxEVT_NULL, m_windowId); |
88310e2e VZ |
1379 | |
1380 | NMHDR* hdr = (NMHDR *)lParam; | |
1381 | switch ( hdr->code ) { | |
1382 | case TCN_SELCHANGE: | |
ce7fe42e | 1383 | event.SetEventType(wxEVT_NOTEBOOK_PAGE_CHANGED); |
88310e2e VZ |
1384 | break; |
1385 | ||
1386 | case TCN_SELCHANGING: | |
ce7fe42e | 1387 | event.SetEventType(wxEVT_NOTEBOOK_PAGE_CHANGING); |
88310e2e VZ |
1388 | break; |
1389 | ||
fd3f686c | 1390 | default: |
a23fd0e1 | 1391 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
88310e2e VZ |
1392 | } |
1393 | ||
01c8cbf5 | 1394 | event.SetSelection(TabCtrl_GetCurSel(GetHwnd())); |
681be2ef | 1395 | event.SetOldSelection(m_selection); |
88310e2e | 1396 | event.SetEventObject(this); |
a23fd0e1 | 1397 | event.SetInt(idCtrl); |
88310e2e | 1398 | |
c7e94140 VZ |
1399 | // Change the selection before generating the event as its handler should |
1400 | // already see the new page selected. | |
409f747e VZ |
1401 | if ( hdr->code == TCN_SELCHANGE ) |
1402 | UpdateSelection(event.GetSelection()); | |
1403 | ||
c7e94140 | 1404 | bool processed = HandleWindowEvent(event); |
fd3f686c VZ |
1405 | *result = !event.IsAllowed(); |
1406 | return processed; | |
88310e2e VZ |
1407 | } |
1408 | ||
1e6feb95 | 1409 | #endif // wxUSE_NOTEBOOK |