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