OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/chromeos/compact_location_bar_view.h" | 5 #include "chrome/browser/chromeos/compact_location_bar_view.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "app/drag_drop_types.h" |
11 #include "base/gfx/point.h" | 12 #include "base/gfx/point.h" |
12 #include "chrome/app/chrome_dll_resource.h" | 13 #include "chrome/app/chrome_dll_resource.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_drag_data.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model.h" |
13 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" | 16 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" |
14 #include "chrome/browser/browser_list.h" | 17 #include "chrome/browser/browser_list.h" |
15 #include "chrome/browser/browser_theme_provider.h" | 18 #include "chrome/browser/browser_theme_provider.h" |
16 #include "chrome/browser/chromeos/compact_location_bar_host.h" | 19 #include "chrome/browser/chromeos/compact_location_bar_host.h" |
| 20 #include "chrome/browser/metrics/user_metrics.h" |
17 #include "chrome/browser/profile.h" | 21 #include "chrome/browser/profile.h" |
| 22 #include "chrome/browser/tab_contents/tab_contents.h" |
18 #include "chrome/browser/view_ids.h" | 23 #include "chrome/browser/view_ids.h" |
19 #include "chrome/browser/views/event_utils.h" | 24 #include "chrome/browser/views/event_utils.h" |
20 #include "chrome/browser/views/frame/browser_view.h" | 25 #include "chrome/browser/views/frame/browser_view.h" |
| 26 #include "chrome/browser/views/toolbar_star_toggle.h" |
21 #include "grit/chromium_strings.h" | 27 #include "grit/chromium_strings.h" |
22 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
23 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
24 #include "views/background.h" | 30 #include "views/background.h" |
25 #include "views/controls/button/image_button.h" | 31 #include "views/controls/button/image_button.h" |
26 #include "views/controls/native/native_view_host.h" | 32 #include "views/controls/native/native_view_host.h" |
| 33 #include "views/drag_utils.h" |
27 #include "views/widget/widget.h" | 34 #include "views/widget/widget.h" |
28 #include "views/window/window.h" | 35 #include "views/window/window.h" |
29 | 36 |
30 namespace chromeos { | 37 namespace chromeos { |
| 38 |
31 const int kCompactLocationBarDefaultWidth = 700; | 39 const int kCompactLocationBarDefaultWidth = 700; |
| 40 const int kWidgetsSeparatorWidth = 2; |
32 | 41 |
33 CompactLocationBarView::CompactLocationBarView(CompactLocationBarHost* host) | 42 CompactLocationBarView::CompactLocationBarView(CompactLocationBarHost* host) |
34 : DropdownBarView(host), | 43 : DropdownBarView(host), |
35 reload_(NULL) { | 44 reload_(NULL), |
| 45 star_(NULL) { |
36 set_background(views::Background::CreateStandardPanelBackground()); | 46 set_background(views::Background::CreateStandardPanelBackground()); |
37 SetFocusable(true); | 47 SetFocusable(true); |
38 } | 48 } |
39 | 49 |
40 CompactLocationBarView::~CompactLocationBarView() { | 50 CompactLocationBarView::~CompactLocationBarView() { |
41 } | 51 } |
42 | 52 |
43 //////////////////////////////////////////////////////////////////////////////// | 53 //////////////////////////////////////////////////////////////////////////////// |
44 // CompactLocationBarView public: | 54 // CompactLocationBarView public: |
45 | 55 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 location_entry_->Init(); | 101 location_entry_->Init(); |
92 location_entry_->Update(browser()->GetSelectedTabContents()); | 102 location_entry_->Update(browser()->GetSelectedTabContents()); |
93 gtk_widget_show_all(location_entry_->widget()); | 103 gtk_widget_show_all(location_entry_->widget()); |
94 gtk_widget_hide(location_entry_->widget()); | 104 gtk_widget_hide(location_entry_->widget()); |
95 | 105 |
96 location_entry_view_ = new views::NativeViewHost; | 106 location_entry_view_ = new views::NativeViewHost; |
97 AddChildView(location_entry_view_); | 107 AddChildView(location_entry_view_); |
98 location_entry_view_->set_focus_view(this); | 108 location_entry_view_->set_focus_view(this); |
99 location_entry_view_->Attach(location_entry_->widget()); | 109 location_entry_view_->Attach(location_entry_->widget()); |
100 | 110 |
101 // TODO(oshima): Add Star Button | 111 star_ = new ToolbarStarToggle(this); |
| 112 star_->SetDragController(this); |
| 113 star_->set_profile(browser()->profile()); |
| 114 star_->set_host_view(this); |
| 115 star_->set_bubble_positioner(this); |
| 116 star_->Init(); |
| 117 AddChildView(star_); |
| 118 |
102 location_entry_->Update(browser()->GetSelectedTabContents()); | 119 location_entry_->Update(browser()->GetSelectedTabContents()); |
103 } | 120 } |
104 | 121 |
105 //////////////////////////////////////////////////////////////////////////////// | 122 //////////////////////////////////////////////////////////////////////////////// |
106 // views::View overrides: | 123 // views::View overrides: |
107 | 124 |
108 gfx::Size CompactLocationBarView::GetPreferredSize() { | 125 gfx::Size CompactLocationBarView::GetPreferredSize() { |
109 if (!reload_) | 126 if (!reload_) |
110 return gfx::Size(); // Not initialized yet, do nothing. | 127 return gfx::Size(); // Not initialized yet, do nothing. |
111 | 128 |
112 gfx::Size sz = reload_->GetPreferredSize(); | 129 gfx::Size reload_size = reload_->GetPreferredSize(); |
| 130 gfx::Size star_size = star_->GetPreferredSize(); |
| 131 gfx::Size location_size = location_entry_view_->GetPreferredSize(); |
113 | 132 |
114 return gfx::Size(500, sz.height()); | 133 int max_height = std::max(reload_size.height(), |
| 134 std::max(star_size.height(), |
| 135 location_size.height())); |
| 136 return gfx::Size(500, max_height); |
115 } | 137 } |
116 | 138 |
117 void CompactLocationBarView::Layout() { | 139 void CompactLocationBarView::Layout() { |
118 if (!reload_) | 140 if (!reload_) |
119 return; // Not initialized yet, do nothing. | 141 return; // Not initialized yet, do nothing. |
120 | 142 |
121 int cur_x = 0; | 143 int cur_x = 0; |
| 144 gfx::Size reload_size = reload_->GetPreferredSize(); |
| 145 int reload_height = std::min(reload_size.height(), height()); |
| 146 reload_->SetBounds(cur_x, (height() - reload_height) / 2, |
| 147 reload_size.width(), reload_height); |
| 148 cur_x += reload_size.width() + kWidgetsSeparatorWidth; |
122 | 149 |
123 gfx::Size sz = reload_->GetPreferredSize(); | 150 gfx::Size star_size = star_->GetPreferredSize(); |
124 reload_->SetBounds(cur_x, 0, sz.width(), sz.height()); | 151 int star_height = std::min(star_size.height(), height()); |
125 cur_x += sz.width(); | 152 star_->SetBounds(cur_x, (height() - star_height) / 2, |
| 153 star_size.width(), star_height); |
| 154 cur_x += star_size.width(); |
126 | 155 |
127 cur_x += 2; | 156 // The location bar is located exactly to the right of the star button and |
128 | 157 // takes all the space to the right except for small margin at the end. |
129 // The location bar gets the rest of the space in the middle. | 158 int location_width = width() - cur_x - kWidgetsSeparatorWidth; |
130 location_entry_view_->SetBounds(cur_x, 0, width() - cur_x * 2 - 2, height()); | 159 location_entry_view_->SetBounds(cur_x, 0, location_width, height()); |
131 | |
132 cur_x = width() - sz.width(); | |
133 } | 160 } |
134 | 161 |
135 void CompactLocationBarView::Paint(gfx::Canvas* canvas) { | 162 void CompactLocationBarView::Paint(gfx::Canvas* canvas) { |
136 View::Paint(canvas); | 163 View::Paint(canvas); |
137 } | 164 } |
138 | 165 |
139 void CompactLocationBarView::ViewHierarchyChanged(bool is_add, View* parent, | 166 void CompactLocationBarView::ViewHierarchyChanged(bool is_add, View* parent, |
140 View* child) { | 167 View* child) { |
141 if (is_add && child == this) | 168 if (is_add && child == this) |
142 Init(); | 169 Init(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 //////////////////////////////////////////////////////////////////////////////// | 218 //////////////////////////////////////////////////////////////////////////////// |
192 // BubblePositioner overrides: | 219 // BubblePositioner overrides: |
193 gfx::Rect CompactLocationBarView::GetLocationStackBounds() const { | 220 gfx::Rect CompactLocationBarView::GetLocationStackBounds() const { |
194 gfx::Point lower_left(0, height()); | 221 gfx::Point lower_left(0, height()); |
195 ConvertPointToScreen(this, &lower_left); | 222 ConvertPointToScreen(this, &lower_left); |
196 gfx::Rect popup = gfx::Rect(lower_left.x(), lower_left.y(), | 223 gfx::Rect popup = gfx::Rect(lower_left.x(), lower_left.y(), |
197 kCompactLocationBarDefaultWidth, 0); | 224 kCompactLocationBarDefaultWidth, 0); |
198 return popup.AdjustToFit(GetWidget()->GetWindow()->GetBounds()); | 225 return popup.AdjustToFit(GetWidget()->GetWindow()->GetBounds()); |
199 } | 226 } |
200 | 227 |
| 228 //////////////////////////////////////////////////////////////////////////////// |
| 229 // views::DragController overrides: |
| 230 void CompactLocationBarView::WriteDragData(views::View* sender, |
| 231 int press_x, |
| 232 int press_y, |
| 233 OSExchangeData* data) { |
| 234 DCHECK( |
| 235 GetDragOperations(sender, press_x, press_y) != DragDropTypes::DRAG_NONE); |
| 236 |
| 237 UserMetrics::RecordAction("CompactLocationBar_DragStar", |
| 238 browser()->profile()); |
| 239 |
| 240 // If there is a bookmark for the URL, add the bookmark drag data for it. We |
| 241 // do this to ensure the bookmark is moved, rather than creating an new |
| 242 // bookmark. |
| 243 TabContents* tab = browser()->GetSelectedTabContents(); |
| 244 if (tab) { |
| 245 Profile* profile = browser()->profile(); |
| 246 if (profile && profile->GetBookmarkModel()) { |
| 247 const BookmarkNode* node = profile->GetBookmarkModel()-> |
| 248 GetMostRecentlyAddedNodeForURL(tab->GetURL()); |
| 249 if (node) { |
| 250 BookmarkDragData bookmark_data(node); |
| 251 bookmark_data.Write(profile, data); |
| 252 } |
| 253 } |
| 254 |
| 255 drag_utils::SetURLAndDragImage(tab->GetURL(), |
| 256 UTF16ToWideHack(tab->GetTitle()), |
| 257 tab->GetFavIcon(), |
| 258 data); |
| 259 } |
| 260 } |
| 261 |
| 262 int CompactLocationBarView::GetDragOperations(views::View* sender, |
| 263 int x, |
| 264 int y) { |
| 265 DCHECK(sender == star_); |
| 266 TabContents* tab = browser()->GetSelectedTabContents(); |
| 267 if (!tab || !tab->ShouldDisplayURL() || !tab->GetURL().is_valid()) { |
| 268 return DragDropTypes::DRAG_NONE; |
| 269 } |
| 270 Profile* profile = browser()->profile(); |
| 271 if (profile && profile->GetBookmarkModel() && |
| 272 profile->GetBookmarkModel()->IsBookmarked(tab->GetURL())) { |
| 273 return DragDropTypes::DRAG_MOVE | DragDropTypes::DRAG_COPY | |
| 274 DragDropTypes::DRAG_LINK; |
| 275 } |
| 276 return DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK; |
| 277 } |
| 278 |
201 } // namespace chromeos | 279 } // namespace chromeos |
OLD | NEW |