Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: ui/views/controls/tabbed_pane/native_tabbed_pane_win.cc

Issue 12211122: Reland Remove NativeTabbedPane[Win|Wrapper]; promote Views impl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix leak of tab content Views. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/views/controls/tabbed_pane/native_tabbed_pane_win.h"
6
7 #include <vssym32.h>
8
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "ui/base/l10n/l10n_util_win.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/base/win/hwnd_util.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/font.h"
16 #include "ui/native_theme/native_theme_win.h"
17 #include "ui/views/controls/tabbed_pane/tabbed_pane.h"
18 #include "ui/views/controls/tabbed_pane/tabbed_pane_listener.h"
19 #include "ui/views/layout/fill_layout.h"
20 #include "ui/views/widget/root_view.h"
21 #include "ui/views/widget/widget.h"
22
23 namespace views {
24
25 // A background object that paints the tab panel background which may be
26 // rendered by the system visual styles system.
27 class TabBackground : public Background {
28 public:
29 explicit TabBackground() {
30 // TMT_FILLCOLORHINT returns a color value that supposedly
31 // approximates the texture drawn by PaintTabPanelBackground.
32 SkColor tab_page_color =
33 ui::NativeThemeWin::instance()->GetThemeColorWithDefault(
34 ui::NativeThemeWin::TAB, TABP_BODY, 0, TMT_FILLCOLORHINT,
35 COLOR_3DFACE);
36 SetNativeControlColor(tab_page_color);
37 }
38 virtual ~TabBackground() {}
39
40 virtual void Paint(gfx::Canvas* canvas, View* view) const {
41 gfx::Rect r(0, 0, view->width(), view->height());
42 ui::NativeTheme::ExtraParams extra;
43 ui::NativeTheme::instance()->Paint(
44 canvas->sk_canvas(), ui::NativeTheme::kTabPanelBackground,
45 ui::NativeTheme::kNormal, r, extra);
46 }
47
48 private:
49 DISALLOW_COPY_AND_ASSIGN(TabBackground);
50 };
51
52 // Custom layout manager that takes care of sizing and displaying the tab pages.
53 class TabLayout : public LayoutManager {
54 public:
55 TabLayout() {}
56
57 // Switches to the tab page identified by the given index.
58 void SwitchToPage(View* host, View* page) {
59 for (int i = 0; i < host->child_count(); ++i) {
60 View* child = host->child_at(i);
61 // The child might not have been laid out yet.
62 if (child == page)
63 child->SetBoundsRect(host->GetContentsBounds());
64 child->SetVisible(child == page);
65 }
66
67 FocusManager* focus_manager = page->GetFocusManager();
68 DCHECK(focus_manager);
69 const View* focused_view = focus_manager->GetFocusedView();
70 if (focused_view && host->Contains(focused_view) &&
71 !page->Contains(focused_view))
72 focus_manager->SetFocusedView(page);
73 }
74
75 private:
76 // LayoutManager overrides:
77 virtual void Layout(View* host) {
78 gfx::Rect bounds(host->GetContentsBounds());
79 for (int i = 0; i < host->child_count(); ++i) {
80 View* child = host->child_at(i);
81 // We only layout visible children, since it may be expensive.
82 if (child->visible() && child->bounds() != bounds)
83 child->SetBoundsRect(bounds);
84 }
85 }
86
87 virtual gfx::Size GetPreferredSize(View* host) {
88 // First, query the preferred sizes to determine a good width.
89 int width = 0;
90 for (int i = 0; i < host->child_count(); ++i) {
91 View* page = host->child_at(i);
92 width = std::max(width, page->GetPreferredSize().width());
93 }
94 // After we know the width, decide on the height.
95 return gfx::Size(width, GetPreferredHeightForWidth(host, width));
96 }
97
98 virtual int GetPreferredHeightForWidth(View* host, int width) {
99 int height = 0;
100 for (int i = 0; i < host->child_count(); ++i) {
101 View* page = host->child_at(i);
102 height = std::max(height, page->GetHeightForWidth(width));
103 }
104 return height;
105 }
106
107 DISALLOW_COPY_AND_ASSIGN(TabLayout);
108 };
109
110 ////////////////////////////////////////////////////////////////////////////////
111 // NativeTabbedPaneWin, public:
112
113 NativeTabbedPaneWin::NativeTabbedPaneWin(TabbedPane* tabbed_pane)
114 : NativeControlWin(),
115 tabbed_pane_(tabbed_pane),
116 tab_layout_manager_(NULL),
117 content_window_(NULL),
118 selected_index_(-1) {
119 // Associates the actual HWND with the tabbed-pane so the tabbed-pane is
120 // the one considered as having the focus (not the wrapper) when the HWND is
121 // focused directly (with a click for example).
122 set_focus_view(tabbed_pane);
123 }
124
125 NativeTabbedPaneWin::~NativeTabbedPaneWin() {
126 // We own the tab views, let's delete them.
127 STLDeleteContainerPointers(tab_views_.begin(), tab_views_.end());
128 }
129
130 ////////////////////////////////////////////////////////////////////////////////
131 // NativeTabbedPaneWin, NativeTabbedPaneWrapper implementation:
132
133 void NativeTabbedPaneWin::AddTab(const string16& title, View* contents) {
134 AddTabAtIndex(static_cast<int>(tab_views_.size()), title, contents, true);
135 }
136
137 void NativeTabbedPaneWin::AddTabAtIndex(int index,
138 const string16& title,
139 View* contents,
140 bool select_if_first_tab) {
141 DCHECK(index <= static_cast<int>(tab_views_.size()));
142 contents->set_owned_by_client();
143 contents->SetVisible(false);
144 tab_views_.insert(tab_views_.begin() + index, contents);
145 tab_titles_.insert(tab_titles_.begin() + index, title);
146
147 if (!contents->background())
148 contents->set_background(new TabBackground);
149
150 if (tab_views_.size() == 1 && select_if_first_tab)
151 selected_index_ = 0;
152
153 // Add native tab only if the native control is alreay created.
154 if (content_window_) {
155 AddNativeTab(index, title);
156 // The newly added tab may have made the contents window smaller.
157 ResizeContents();
158
159 View* content_root = content_window_->GetRootView();
160 content_root->AddChildView(contents);
161 // Switch to the newly added tab if requested;
162 if (tab_views_.size() == 1 && select_if_first_tab)
163 tab_layout_manager_->SwitchToPage(content_root, contents);
164 }
165 }
166
167 void NativeTabbedPaneWin::AddNativeTab(int index, const string16& title) {
168 TCITEM tcitem;
169 tcitem.mask = TCIF_TEXT;
170
171 // If the locale is RTL, we set the TCIF_RTLREADING so that BiDi text is
172 // rendered properly on the tabs.
173 if (base::i18n::IsRTL()) {
174 tcitem.mask |= TCIF_RTLREADING;
175 }
176
177 tcitem.pszText = const_cast<wchar_t*>(title.c_str());
178 int result = TabCtrl_InsertItem(native_view(), index, &tcitem);
179 DCHECK(result != -1);
180 }
181
182 View* NativeTabbedPaneWin::RemoveTabAtIndex(int index) {
183 int tab_count = static_cast<int>(tab_views_.size());
184 DCHECK(index >= 0 && index < tab_count);
185
186 if (index < (tab_count - 1)) {
187 // Select the next tab.
188 SelectTabAt(index + 1);
189 } else {
190 // We are the last tab, select the previous one.
191 if (index > 0) {
192 SelectTabAt(index - 1);
193 } else if (content_window_) {
194 // That was the last tab. Remove the contents.
195 content_window_->GetRootView()->RemoveAllChildViews(false);
196 }
197 }
198 TabCtrl_DeleteItem(native_view(), index);
199
200 // The removed tab may have made the contents window bigger.
201 if (content_window_)
202 ResizeContents();
203
204 std::vector<View*>::iterator iter = tab_views_.begin() + index;
205 View* removed_tab = *iter;
206 if (content_window_)
207 content_window_->GetRootView()->RemoveChildView(removed_tab);
208 tab_views_.erase(iter);
209 tab_titles_.erase(tab_titles_.begin() + index);
210
211 return removed_tab;
212 }
213
214 void NativeTabbedPaneWin::SelectTabAt(int index) {
215 DCHECK((index >= 0) && (index < static_cast<int>(tab_views_.size())));
216 if (native_view())
217 TabCtrl_SetCurSel(native_view(), index);
218 DoSelectTabAt(index, true);
219 }
220
221 int NativeTabbedPaneWin::GetTabCount() {
222 return TabCtrl_GetItemCount(native_view());
223 }
224
225 int NativeTabbedPaneWin::GetSelectedTabIndex() {
226 return TabCtrl_GetCurSel(native_view());
227 }
228
229 View* NativeTabbedPaneWin::GetSelectedTab() {
230 if (selected_index_ < 0)
231 return NULL;
232 return tab_views_[selected_index_];
233 }
234
235 View* NativeTabbedPaneWin::GetView() {
236 return this;
237 }
238
239 void NativeTabbedPaneWin::SetFocus() {
240 // Focus the associated HWND.
241 OnFocus();
242 }
243
244 gfx::Size NativeTabbedPaneWin::GetPreferredSize() {
245 if (!native_view())
246 return gfx::Size();
247
248 gfx::Rect contentSize(content_window_->GetRootView()->GetPreferredSize());
249 RECT paneSize = { 0, 0, contentSize.width(), contentSize.height() };
250 TabCtrl_AdjustRect(native_view(), TRUE, &paneSize);
251 return gfx::Size(paneSize.right - paneSize.left,
252 paneSize.bottom - paneSize.top);
253 }
254
255 gfx::NativeView NativeTabbedPaneWin::GetTestingHandle() const {
256 return native_view();
257 }
258
259 ////////////////////////////////////////////////////////////////////////////////
260 // NativeTabbedPaneWin, NativeControlWin override:
261
262 void NativeTabbedPaneWin::CreateNativeControl() {
263 // Create the tab control.
264 //
265 // Note that we don't follow the common convention for NativeControl
266 // subclasses and we don't pass the value returned from
267 // NativeControl::GetAdditionalExStyle() as the dwExStyle parameter. Here is
268 // why: on RTL locales, if we pass NativeControl::GetAdditionalExStyle() when
269 // we basically tell Windows to create our HWND with the WS_EX_LAYOUTRTL. If
270 // we do that, then the HWND we create for |content_window_| below will
271 // inherit the WS_EX_LAYOUTRTL property and this will result in the contents
272 // being flipped, which is not what we want (because we handle mirroring in
273 // views without the use of Windows' support for mirroring). Therefore,
274 // we initially create our HWND without WS_EX_LAYOUTRTL and we explicitly set
275 // this property our child is created. This way, on RTL locales, our tabs
276 // will be nicely rendered from right to left (by virtue of Windows doing the
277 // right thing with the TabbedPane HWND) and each tab contents will use an
278 // RTL layout correctly (by virtue of the mirroring infrastructure in views
279 // doing the right thing with each View we put in the tab).
280 DWORD style = WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | WS_CLIPCHILDREN;
281 HWND tab_control = ::CreateWindowEx(0,
282 WC_TABCONTROL,
283 L"",
284 style,
285 0, 0, width(), height(),
286 GetWidget()->GetNativeView(), NULL, NULL,
287 NULL);
288 ui::CheckWindowCreated(tab_control);
289
290 HFONT font = ResourceBundle::GetSharedInstance().
291 GetFont(ResourceBundle::BaseFont).GetNativeFont();
292 SendMessage(tab_control, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE);
293
294 // Create the view container which is a child of the TabControl.
295 content_window_ = new Widget;
296 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
297 params.parent = tab_control;
298 content_window_->Init(params);
299
300 // Explicitly setting the WS_EX_LAYOUTRTL property for the HWND (see above
301 // for why we waited until |content_window_| is created before we set this
302 // property for the tabbed pane's HWND).
303 if (base::i18n::IsRTL())
304 l10n_util::HWNDSetRTLLayout(tab_control);
305
306 View* root_view = content_window_->GetRootView();
307 tab_layout_manager_ = new TabLayout();
308 root_view->SetLayoutManager(tab_layout_manager_);
309 DWORD sys_color = ::GetSysColor(COLOR_3DHILIGHT);
310 SkColor color = SkColorSetRGB(GetRValue(sys_color), GetGValue(sys_color),
311 GetBValue(sys_color));
312 root_view->set_background(Background::CreateSolidBackground(color));
313
314 content_window_->SetFocusTraversableParentView(this);
315
316 NativeControlCreated(tab_control);
317
318 // Add tabs that are already added if any.
319 if (!tab_views_.empty()) {
320 InitializeTabs();
321 if (selected_index_ >= 0)
322 DoSelectTabAt(selected_index_, false);
323 }
324
325 ResizeContents();
326 }
327
328 bool NativeTabbedPaneWin::ProcessMessage(UINT message,
329 WPARAM w_param,
330 LPARAM l_param,
331 LRESULT* result) {
332 if (message == WM_NOTIFY &&
333 reinterpret_cast<LPNMHDR>(l_param)->code == TCN_SELCHANGE) {
334 int selected_tab = TabCtrl_GetCurSel(native_view());
335 DCHECK(selected_tab != -1);
336 DoSelectTabAt(selected_tab, true);
337 return TRUE;
338 }
339 return NativeControlWin::ProcessMessage(message, w_param, l_param, result);
340 }
341
342 ////////////////////////////////////////////////////////////////////////////////
343 // View override:
344
345 void NativeTabbedPaneWin::Layout() {
346 NativeControlWin::Layout();
347 ResizeContents();
348 }
349
350 FocusTraversable* NativeTabbedPaneWin::GetFocusTraversable() {
351 return content_window_;
352 }
353
354 void NativeTabbedPaneWin::ViewHierarchyChanged(bool is_add,
355 View *parent,
356 View *child) {
357 NativeControlWin::ViewHierarchyChanged(is_add, parent, child);
358
359 if (is_add && (child == this) && content_window_) {
360 // We have been added to a view hierarchy, update the FocusTraversable
361 // parent.
362 content_window_->SetFocusTraversableParent(
363 GetWidget()->GetFocusTraversable());
364 }
365 }
366
367 ////////////////////////////////////////////////////////////////////////////////
368 // NativeTabbedPaneWin, private:
369
370 void NativeTabbedPaneWin::InitializeTabs() {
371 for (size_t i = 0; i < tab_titles_.size(); ++i)
372 AddNativeTab(i, tab_titles_[i]);
373
374 View* content_root = content_window_->GetRootView();
375 for (std::vector<View*>::iterator tab(tab_views_.begin());
376 tab != tab_views_.end(); ++tab)
377 content_root->AddChildView(*tab);
378 }
379
380 void NativeTabbedPaneWin::DoSelectTabAt(int index, boolean invoke_listener) {
381 selected_index_ = index;
382 if (content_window_) {
383 View* content_root = content_window_->GetRootView();
384 tab_layout_manager_->SwitchToPage(content_root, tab_views_[index]);
385 }
386 if (invoke_listener && tabbed_pane_->listener())
387 tabbed_pane_->listener()->TabSelectedAt(index);
388 }
389
390 void NativeTabbedPaneWin::ResizeContents() {
391 RECT content_bounds;
392 if (!GetClientRect(native_view(), &content_bounds))
393 return;
394 TabCtrl_AdjustRect(native_view(), FALSE, &content_bounds);
395 content_window_->SetBounds(gfx::Rect(content_bounds));
396 }
397
398 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/tabbed_pane/native_tabbed_pane_win.h ('k') | ui/views/controls/tabbed_pane/native_tabbed_pane_wrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698