OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/controls/tabbed_pane/native_tabbed_pane_win.h" | 5 #include "views/controls/tabbed_pane/native_tabbed_pane_win.h" |
6 | 6 |
7 #include <vssym32.h> | 7 #include <vssym32.h> |
8 | 8 |
9 #include "app/l10n_util_win.h" | 9 #include "app/l10n_util_win.h" |
10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 HDC dc = canvas->BeginPlatformPaint(); | 39 HDC dc = canvas->BeginPlatformPaint(); |
40 RECT r = {0, 0, view->width(), view->height()}; | 40 RECT r = {0, 0, view->width(), view->height()}; |
41 gfx::NativeTheme::instance()->PaintTabPanelBackground(dc, &r); | 41 gfx::NativeTheme::instance()->PaintTabPanelBackground(dc, &r); |
42 canvas->EndPlatformPaint(); | 42 canvas->EndPlatformPaint(); |
43 } | 43 } |
44 | 44 |
45 private: | 45 private: |
46 DISALLOW_COPY_AND_ASSIGN(TabBackground); | 46 DISALLOW_COPY_AND_ASSIGN(TabBackground); |
47 }; | 47 }; |
48 | 48 |
| 49 // Custom layout manager that takes care of sizing and displaying the tab pages. |
| 50 class TabLayout : public LayoutManager { |
| 51 public: |
| 52 TabLayout() {} |
| 53 |
| 54 // Switches to the tab page identified by the given index. |
| 55 void SwitchToPage(View* host, View* page) { |
| 56 for (int i = 0; i < host->GetChildViewCount(); ++i) { |
| 57 View* child = host->GetChildViewAt(i); |
| 58 // The child might not have been laid out yet. |
| 59 if (child == page) |
| 60 child->SetBounds(gfx::Rect(host->size())); |
| 61 child->SetVisible(child == page); |
| 62 } |
| 63 } |
| 64 |
| 65 private: |
| 66 // LayoutManager overrides: |
| 67 virtual void Layout(View* host) { |
| 68 gfx::Rect bounds(host->size()); |
| 69 for (int i = 0; i < host->GetChildViewCount(); ++i) { |
| 70 View* child = host->GetChildViewAt(i); |
| 71 // We only layout visible children, since it may be expensive. |
| 72 if (child->IsVisible() && child->bounds() != bounds) |
| 73 child->SetBounds(bounds); |
| 74 } |
| 75 } |
| 76 |
| 77 virtual gfx::Size GetPreferredSize(View* host) { |
| 78 // First, query the preferred sizes to determine a good width. |
| 79 int width = 0; |
| 80 for (int i = 0; i < host->GetChildViewCount(); ++i) { |
| 81 View* page = host->GetChildViewAt(i); |
| 82 width = std::max(width, page->GetPreferredSize().width()); |
| 83 } |
| 84 // After we know the width, decide on the height. |
| 85 return gfx::Size(width, GetPreferredHeightForWidth(host, width)); |
| 86 } |
| 87 |
| 88 virtual int GetPreferredHeightForWidth(View* host, int width) { |
| 89 int height = 0; |
| 90 for (int i = 0; i < host->GetChildViewCount(); ++i) { |
| 91 View* page = host->GetChildViewAt(i); |
| 92 height = std::max(height, page->GetHeightForWidth(width)); |
| 93 } |
| 94 return height; |
| 95 } |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(TabLayout); |
| 98 }; |
| 99 |
49 //////////////////////////////////////////////////////////////////////////////// | 100 //////////////////////////////////////////////////////////////////////////////// |
50 // NativeTabbedPaneWin, public: | 101 // NativeTabbedPaneWin, public: |
51 | 102 |
52 NativeTabbedPaneWin::NativeTabbedPaneWin(TabbedPane* tabbed_pane) | 103 NativeTabbedPaneWin::NativeTabbedPaneWin(TabbedPane* tabbed_pane) |
53 : NativeControlWin(), | 104 : NativeControlWin(), |
54 tabbed_pane_(tabbed_pane), | 105 tabbed_pane_(tabbed_pane), |
| 106 tab_layout_manager_(NULL), |
55 content_window_(NULL), | 107 content_window_(NULL), |
56 selected_index_(-1) { | 108 selected_index_(-1) { |
57 // Associates the actual HWND with the tabbed-pane so the tabbed-pane is | 109 // Associates the actual HWND with the tabbed-pane so the tabbed-pane is |
58 // the one considered as having the focus (not the wrapper) when the HWND is | 110 // the one considered as having the focus (not the wrapper) when the HWND is |
59 // focused directly (with a click for example). | 111 // focused directly (with a click for example). |
60 set_focus_view(tabbed_pane); | 112 set_focus_view(tabbed_pane); |
61 } | 113 } |
62 | 114 |
63 NativeTabbedPaneWin::~NativeTabbedPaneWin() { | 115 NativeTabbedPaneWin::~NativeTabbedPaneWin() { |
64 // We own the tab views, let's delete them. | 116 // We own the tab views, let's delete them. |
65 STLDeleteContainerPointers(tab_views_.begin(), tab_views_.end()); | 117 STLDeleteContainerPointers(tab_views_.begin(), tab_views_.end()); |
66 } | 118 } |
67 | 119 |
68 //////////////////////////////////////////////////////////////////////////////// | 120 //////////////////////////////////////////////////////////////////////////////// |
69 // NativeTabbedPaneWin, NativeTabbedPaneWrapper implementation: | 121 // NativeTabbedPaneWin, NativeTabbedPaneWrapper implementation: |
70 | 122 |
71 void NativeTabbedPaneWin::AddTab(const std::wstring& title, View* contents) { | 123 void NativeTabbedPaneWin::AddTab(const std::wstring& title, View* contents) { |
72 AddTabAtIndex(static_cast<int>(tab_views_.size()), title, contents, true); | 124 AddTabAtIndex(static_cast<int>(tab_views_.size()), title, contents, true); |
73 } | 125 } |
74 | 126 |
75 void NativeTabbedPaneWin::AddTabAtIndex(int index, const std::wstring& title, | 127 void NativeTabbedPaneWin::AddTabAtIndex(int index, const std::wstring& title, |
76 View* contents, | 128 View* contents, |
77 bool select_if_first_tab) { | 129 bool select_if_first_tab) { |
78 DCHECK(index <= static_cast<int>(tab_views_.size())); | 130 DCHECK(index <= static_cast<int>(tab_views_.size())); |
79 contents->set_parent_owned(false); | 131 contents->set_parent_owned(false); |
| 132 contents->SetVisible(false); |
80 tab_views_.insert(tab_views_.begin() + index, contents); | 133 tab_views_.insert(tab_views_.begin() + index, contents); |
81 tab_titles_.insert(tab_titles_.begin() + index, title); | 134 tab_titles_.insert(tab_titles_.begin() + index, title); |
82 | 135 |
83 if (!contents->background()) | 136 if (!contents->background()) |
84 contents->set_background(new TabBackground); | 137 contents->set_background(new TabBackground); |
85 | 138 |
86 if (tab_views_.size() == 1 && select_if_first_tab) { | 139 if (tab_views_.size() == 1 && select_if_first_tab) |
87 // If this is the only tab displayed, make sure the contents is set. | |
88 selected_index_ = 0; | 140 selected_index_ = 0; |
89 if (content_window_) | |
90 content_window_->GetRootView()->AddChildView(contents); | |
91 } | |
92 | 141 |
93 // Add native tab only if the native control is alreay created. | 142 // Add native tab only if the native control is alreay created. |
94 if (content_window_) { | 143 if (content_window_) { |
95 AddNativeTab(index, title, contents); | 144 AddNativeTab(index, title); |
96 | |
97 // The newly added tab may have made the contents window smaller. | 145 // The newly added tab may have made the contents window smaller. |
98 ResizeContents(); | 146 ResizeContents(); |
| 147 |
| 148 RootView* content_root = content_window_->GetRootView(); |
| 149 content_root->AddChildView(contents); |
| 150 // Switch to the newly added tab if requested; |
| 151 if (tab_views_.size() == 1 && select_if_first_tab) |
| 152 tab_layout_manager_->SwitchToPage(content_root, contents); |
99 } | 153 } |
100 } | 154 } |
101 | 155 |
102 void NativeTabbedPaneWin::AddNativeTab(int index, | 156 void NativeTabbedPaneWin::AddNativeTab(int index, |
103 const std::wstring &title, | 157 const std::wstring &title) { |
104 views::View* contents) { | |
105 TCITEM tcitem; | 158 TCITEM tcitem; |
106 tcitem.mask = TCIF_TEXT; | 159 tcitem.mask = TCIF_TEXT; |
107 | 160 |
108 // If the locale is RTL, we set the TCIF_RTLREADING so that BiDi text is | 161 // If the locale is RTL, we set the TCIF_RTLREADING so that BiDi text is |
109 // rendered properly on the tabs. | 162 // rendered properly on the tabs. |
110 if (base::i18n::IsRTL()) { | 163 if (base::i18n::IsRTL()) { |
111 tcitem.mask |= TCIF_RTLREADING; | 164 tcitem.mask |= TCIF_RTLREADING; |
112 } | 165 } |
113 | 166 |
114 tcitem.pszText = const_cast<wchar_t*>(title.c_str()); | 167 tcitem.pszText = const_cast<wchar_t*>(title.c_str()); |
(...skipping 18 matching lines...) Expand all Loading... |
133 } | 186 } |
134 } | 187 } |
135 TabCtrl_DeleteItem(native_view(), index); | 188 TabCtrl_DeleteItem(native_view(), index); |
136 | 189 |
137 // The removed tab may have made the contents window bigger. | 190 // The removed tab may have made the contents window bigger. |
138 if (content_window_) | 191 if (content_window_) |
139 ResizeContents(); | 192 ResizeContents(); |
140 | 193 |
141 std::vector<View*>::iterator iter = tab_views_.begin() + index; | 194 std::vector<View*>::iterator iter = tab_views_.begin() + index; |
142 View* removed_tab = *iter; | 195 View* removed_tab = *iter; |
| 196 if (content_window_) |
| 197 content_window_->GetRootView()->RemoveChildView(removed_tab); |
143 tab_views_.erase(iter); | 198 tab_views_.erase(iter); |
144 tab_titles_.erase(tab_titles_.begin() + index); | 199 tab_titles_.erase(tab_titles_.begin() + index); |
145 | 200 |
146 return removed_tab; | 201 return removed_tab; |
147 } | 202 } |
148 | 203 |
149 void NativeTabbedPaneWin::SelectTabAt(int index) { | 204 void NativeTabbedPaneWin::SelectTabAt(int index) { |
150 DCHECK((index >= 0) && (index < static_cast<int>(tab_views_.size()))); | 205 DCHECK((index >= 0) && (index < static_cast<int>(tab_views_.size()))); |
151 if (native_view()) | 206 if (native_view()) |
152 TabCtrl_SetCurSel(native_view(), index); | 207 TabCtrl_SetCurSel(native_view(), index); |
(...skipping 16 matching lines...) Expand all Loading... |
169 | 224 |
170 View* NativeTabbedPaneWin::GetView() { | 225 View* NativeTabbedPaneWin::GetView() { |
171 return this; | 226 return this; |
172 } | 227 } |
173 | 228 |
174 void NativeTabbedPaneWin::SetFocus() { | 229 void NativeTabbedPaneWin::SetFocus() { |
175 // Focus the associated HWND. | 230 // Focus the associated HWND. |
176 Focus(); | 231 Focus(); |
177 } | 232 } |
178 | 233 |
| 234 gfx::Size NativeTabbedPaneWin::GetPreferredSize() { |
| 235 if (!native_view()) |
| 236 return gfx::Size(); |
| 237 |
| 238 gfx::Rect contentSize(content_window_->GetRootView()->GetPreferredSize()); |
| 239 RECT paneSize = { 0, 0, contentSize.width(), contentSize.height() }; |
| 240 TabCtrl_AdjustRect(native_view(), TRUE, &paneSize); |
| 241 return gfx::Size(paneSize.right - paneSize.left, |
| 242 paneSize.bottom - paneSize.top); |
| 243 } |
| 244 |
179 gfx::NativeView NativeTabbedPaneWin::GetTestingHandle() const { | 245 gfx::NativeView NativeTabbedPaneWin::GetTestingHandle() const { |
180 return native_view(); | 246 return native_view(); |
181 } | 247 } |
182 | 248 |
183 //////////////////////////////////////////////////////////////////////////////// | 249 //////////////////////////////////////////////////////////////////////////////// |
184 // NativeTabbedPaneWin, NativeControlWin override: | 250 // NativeTabbedPaneWin, NativeControlWin override: |
185 | 251 |
186 void NativeTabbedPaneWin::CreateNativeControl() { | 252 void NativeTabbedPaneWin::CreateNativeControl() { |
187 // Create the tab control. | 253 // Create the tab control. |
188 // | 254 // |
(...skipping 29 matching lines...) Expand all Loading... |
218 content_window_ = new WidgetWin(); | 284 content_window_ = new WidgetWin(); |
219 content_window_->Init(tab_control, gfx::Rect()); | 285 content_window_->Init(tab_control, gfx::Rect()); |
220 | 286 |
221 // Explicitly setting the WS_EX_LAYOUTRTL property for the HWND (see above | 287 // Explicitly setting the WS_EX_LAYOUTRTL property for the HWND (see above |
222 // for why we waited until |content_window_| is created before we set this | 288 // for why we waited until |content_window_| is created before we set this |
223 // property for the tabbed pane's HWND). | 289 // property for the tabbed pane's HWND). |
224 if (base::i18n::IsRTL()) | 290 if (base::i18n::IsRTL()) |
225 l10n_util::HWNDSetRTLLayout(tab_control); | 291 l10n_util::HWNDSetRTLLayout(tab_control); |
226 | 292 |
227 RootView* root_view = content_window_->GetRootView(); | 293 RootView* root_view = content_window_->GetRootView(); |
228 root_view->SetLayoutManager(new FillLayout()); | 294 tab_layout_manager_ = new TabLayout(); |
| 295 root_view->SetLayoutManager(tab_layout_manager_); |
229 DWORD sys_color = ::GetSysColor(COLOR_3DHILIGHT); | 296 DWORD sys_color = ::GetSysColor(COLOR_3DHILIGHT); |
230 SkColor color = SkColorSetRGB(GetRValue(sys_color), GetGValue(sys_color), | 297 SkColor color = SkColorSetRGB(GetRValue(sys_color), GetGValue(sys_color), |
231 GetBValue(sys_color)); | 298 GetBValue(sys_color)); |
232 root_view->set_background(Background::CreateSolidBackground(color)); | 299 root_view->set_background(Background::CreateSolidBackground(color)); |
233 | 300 |
234 content_window_->SetFocusTraversableParentView(this); | 301 content_window_->SetFocusTraversableParentView(this); |
235 | 302 |
236 NativeControlCreated(tab_control); | 303 NativeControlCreated(tab_control); |
237 | 304 |
238 // Add tabs that are already added if any. | 305 // Add tabs that are already added if any. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 // We have been added to a view hierarchy, update the FocusTraversable | 347 // We have been added to a view hierarchy, update the FocusTraversable |
281 // parent. | 348 // parent. |
282 content_window_->SetFocusTraversableParent(GetRootView()); | 349 content_window_->SetFocusTraversableParent(GetRootView()); |
283 } | 350 } |
284 } | 351 } |
285 | 352 |
286 //////////////////////////////////////////////////////////////////////////////// | 353 //////////////////////////////////////////////////////////////////////////////// |
287 // NativeTabbedPaneWin, private: | 354 // NativeTabbedPaneWin, private: |
288 | 355 |
289 void NativeTabbedPaneWin::InitializeTabs() { | 356 void NativeTabbedPaneWin::InitializeTabs() { |
290 for (size_t i = 0; i < tab_views_.size(); ++i) { | 357 for (size_t i = 0; i < tab_titles_.size(); ++i) |
291 AddNativeTab(i, tab_titles_[i], tab_views_[i]); | 358 AddNativeTab(i, tab_titles_[i]); |
292 } | 359 |
| 360 RootView* content_root = content_window_->GetRootView(); |
| 361 for (std::vector<View*>::iterator tab(tab_views_.begin()); |
| 362 tab != tab_views_.end(); ++tab) |
| 363 content_root->AddChildView(*tab); |
293 } | 364 } |
294 | 365 |
295 void NativeTabbedPaneWin::DoSelectTabAt(int index, boolean invoke_listener) { | 366 void NativeTabbedPaneWin::DoSelectTabAt(int index, boolean invoke_listener) { |
296 selected_index_ = index; | 367 selected_index_ = index; |
297 if (content_window_) { | 368 if (content_window_) { |
298 RootView* content_root = content_window_->GetRootView(); | 369 RootView* content_root = content_window_->GetRootView(); |
299 | 370 |
300 // Clear the focus if the focused view was on the tab. | 371 // Clear the focus if the focused view was on the tab. |
301 FocusManager* focus_manager = GetFocusManager(); | 372 FocusManager* focus_manager = GetFocusManager(); |
302 DCHECK(focus_manager); | 373 DCHECK(focus_manager); |
303 View* focused_view = focus_manager->GetFocusedView(); | 374 View* focused_view = focus_manager->GetFocusedView(); |
304 if (focused_view && content_root->IsParentOf(focused_view)) | 375 if (focused_view && content_root->IsParentOf(focused_view)) |
305 focus_manager->ClearFocus(); | 376 focus_manager->ClearFocus(); |
306 | 377 tab_layout_manager_->SwitchToPage(content_root, tab_views_.at(index)); |
307 content_root->RemoveAllChildViews(false); | |
308 content_root->AddChildView(tab_views_[index]); | |
309 content_root->Layout(); | |
310 } | 378 } |
311 if (invoke_listener && tabbed_pane_->listener()) | 379 if (invoke_listener && tabbed_pane_->listener()) |
312 tabbed_pane_->listener()->TabSelectedAt(index); | 380 tabbed_pane_->listener()->TabSelectedAt(index); |
313 } | 381 } |
314 | 382 |
315 void NativeTabbedPaneWin::ResizeContents() { | 383 void NativeTabbedPaneWin::ResizeContents() { |
316 CRect content_bounds; | 384 CRect content_bounds; |
317 if (!GetClientRect(native_view(), &content_bounds)) | 385 if (!GetClientRect(native_view(), &content_bounds)) |
318 return; | 386 return; |
319 TabCtrl_AdjustRect(native_view(), FALSE, &content_bounds); | 387 TabCtrl_AdjustRect(native_view(), FALSE, &content_bounds); |
320 content_window_->MoveWindow(content_bounds.left, content_bounds.top, | 388 content_window_->MoveWindow(content_bounds.left, content_bounds.top, |
321 content_bounds.Width(), content_bounds.Height(), | 389 content_bounds.Width(), content_bounds.Height(), |
322 TRUE); | 390 TRUE); |
323 } | 391 } |
324 | 392 |
325 //////////////////////////////////////////////////////////////////////////////// | 393 //////////////////////////////////////////////////////////////////////////////// |
326 // NativeTabbedPaneWrapper, public: | 394 // NativeTabbedPaneWrapper, public: |
327 | 395 |
328 // static | 396 // static |
329 NativeTabbedPaneWrapper* NativeTabbedPaneWrapper::CreateNativeWrapper( | 397 NativeTabbedPaneWrapper* NativeTabbedPaneWrapper::CreateNativeWrapper( |
330 TabbedPane* tabbed_pane) { | 398 TabbedPane* tabbed_pane) { |
331 return new NativeTabbedPaneWin(tabbed_pane); | 399 return new NativeTabbedPaneWin(tabbed_pane); |
332 } | 400 } |
333 | 401 |
334 } // namespace views | 402 } // namespace views |
OLD | NEW |