1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/compact_navigation_bar.h"
#include "app/gfx/canvas.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "app/theme_provider.h"
#include "base/logging.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h"
#include "chrome/browser/back_forward_menu_model.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_theme_provider.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/chromeos/status_area_view.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/view_ids.h"
#include "chrome/browser/views/event_utils.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "chrome/browser/views/theme_background.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "views/controls/button/button_dropdown.h"
#include "views/controls/button/image_button.h"
#include "views/controls/image_view.h"
#include "views/controls/native/native_view_host.h"
namespace chromeos {
// Padding inside each button around the image.
static const int kInnerPadding = 1;
// Spacing between buttons.
static const int kHorizPadding = 3;
static const int kURLWidth = 180;
// Preferred height.
static const int kPreferredHeight = 25;
// Draw this much white around the URL bar to make it look larger than it
// actually is.
static const int kURLPadding = 2;
////////////////////////////////////////////////////////////////////////////////
// CompactNavigationBar public:
CompactNavigationBar::CompactNavigationBar(::BrowserView* browser_view)
: browser_view_(browser_view),
initialized_(false) {
SetFocusable(true);
}
CompactNavigationBar::~CompactNavigationBar() {
if (location_entry_view_->native_view())
location_entry_view_->Detach();
}
void CompactNavigationBar::Init() {
DCHECK(!initialized_);
initialized_ = true;
Browser* browser = browser_view_->browser();
browser->command_updater()->AddCommandObserver(IDC_BACK, this);
browser->command_updater()->AddCommandObserver(IDC_FORWARD, this);
back_menu_model_.reset(new BackForwardMenuModel(
browser, BackForwardMenuModel::BACKWARD_MENU));
forward_menu_model_.reset(new BackForwardMenuModel(
browser, BackForwardMenuModel::FORWARD_MENU));
ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
back_ = new views::ButtonDropDown(this, back_menu_model_.get());
back_->set_triggerable_event_flags(views::Event::EF_LEFT_BUTTON_DOWN |
views::Event::EF_MIDDLE_BUTTON_DOWN);
back_->set_tag(IDC_BACK);
back_->SetTooltipText(l10n_util::GetString(IDS_TOOLTIP_BACK));
back_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_BACK));
back_->SetImage(views::CustomButton::BS_NORMAL,
resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_BACK));
back_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
views::ImageButton::ALIGN_MIDDLE);
AddChildView(back_);
bf_separator_ = new views::ImageView;
bf_separator_->SetImage(
resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_SEPARATOR));
AddChildView(bf_separator_);
forward_ = new views::ButtonDropDown(this, forward_menu_model_.get());
forward_->set_triggerable_event_flags(views::Event::EF_LEFT_BUTTON_DOWN |
views::Event::EF_MIDDLE_BUTTON_DOWN);
forward_->set_tag(IDC_FORWARD);
forward_->SetTooltipText(l10n_util::GetString(IDS_TOOLTIP_FORWARD));
forward_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_FORWARD));
forward_->SetImage(views::CustomButton::BS_NORMAL,
resource_bundle.GetBitmapNamed(IDR_COMPACTNAV_FORWARD));
forward_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
views::ImageButton::ALIGN_MIDDLE);
AddChildView(forward_);
// URL bar construction.
location_entry_.reset(new AutocompleteEditViewGtk(
this, browser->toolbar_model(), browser->profile(),
browser->command_updater(), false, this));
location_entry_->Init();
gtk_widget_show_all(location_entry_->widget());
gtk_widget_hide(location_entry_->widget());
location_entry_view_ = new views::NativeViewHost;
AddChildView(location_entry_view_);
location_entry_view_->set_focus_view(this);
location_entry_view_->Attach(location_entry_->widget());
set_background(new ThemeBackground(browser_view_));
}
void CompactNavigationBar::Focus() {
location_entry_->SetFocus();
}
void CompactNavigationBar::FocusLocation() {
location_entry_->SetFocus();
location_entry_->SelectAll(true);
}
gfx::Size CompactNavigationBar::GetPreferredSize() {
int width = 0;
width += kURLWidth + kHorizPadding + kURLPadding * 2; // URL bar.
width += back_->GetPreferredSize().width() + kHorizPadding +
kInnerPadding * 2;
width += bf_separator_->GetPreferredSize().width() + kHorizPadding;
width += forward_->GetPreferredSize().width() + kHorizPadding +
kInnerPadding * 2;
width++;
return gfx::Size(width, kPreferredHeight);
}
void CompactNavigationBar::Layout() {
if (!initialized_)
return;
// We hide navigation buttons when the entry has focus. Navigation
// buttons' visibility is controlled in OnKillFocus/OnSetFocus methods.
if (!back_->IsVisible()) {
// Fill the view with the entry view while it has focus.
location_entry_view_->SetBounds(kURLPadding, 0,
width() - kHorizPadding, height());
} else {
// Layout forward/back buttons after entry views as follows:
// [Entry View] [Back]|[Forward]
int curx = 0;
// URL bar.
location_entry_view_->SetBounds(curx + kURLPadding, 0,
kURLWidth + kURLPadding * 2, height());
curx += kURLWidth + kHorizPadding + kURLPadding * 2;
// "Back | Forward" section.
gfx::Size button_size = back_->GetPreferredSize();
button_size.set_width(button_size.width() + kInnerPadding * 2);
back_->SetBounds(curx, 0, button_size.width(), height());
curx += button_size.width() + kHorizPadding;
button_size = bf_separator_->GetPreferredSize();
bf_separator_->SetBounds(curx, 0, button_size.width(), height());
curx += button_size.width() + kHorizPadding;
button_size = forward_->GetPreferredSize();
button_size.set_width(button_size.width() + kInnerPadding * 2);
forward_->SetBounds(curx, 0, button_size.width(), height());
curx += button_size.width() + kHorizPadding;
}
}
void CompactNavigationBar::Paint(gfx::Canvas* canvas) {
PaintBackground(canvas);
// Draw a white box around the edit field so that it looks larger. This is
// kind of what the default GTK location bar does, although they have a
// fancier border.
canvas->FillRectInt(0xFFFFFFFF,
location_entry_view_->x() - kURLPadding,
2,
location_entry_view_->width() + kURLPadding * 2,
height() - 5);
}
////////////////////////////////////////////////////////////////////////////////
// views::ButtonListener implementation.
void CompactNavigationBar::ButtonPressed(
views::Button* sender, const views::Event& event) {
int id = sender->tag();
switch (id) {
case IDC_BACK:
case IDC_FORWARD:
case IDC_RELOAD:
// Forcibly reset the location bar, since otherwise it won't discard any
// ongoing user edits, since it doesn't realize this is a user-initiated
// action.
location_entry_->RevertAll();
break;
}
browser_view_->browser()->ExecuteCommandWithDisposition(
id, event_utils::DispositionFromEventFlags(sender->mouse_event_flags()));
}
////////////////////////////////////////////////////////////////////////////////
// AutocompleteController implementation.
void CompactNavigationBar::OnAutocompleteAccept(
const GURL& url,
WindowOpenDisposition disposition,
PageTransition::Type transition,
const GURL& alternate_nav_url) {
AddTabWithURL(url, transition);
}
void CompactNavigationBar::OnChanged() {
// Other one does "DoLayout" here.
}
void CompactNavigationBar::OnInputInProgress(bool in_progress) {
}
void CompactNavigationBar::OnKillFocus() {
back_->SetVisible(true);
bf_separator_->SetVisible(true);
forward_->SetVisible(true);
Layout();
SchedulePaint();
}
void CompactNavigationBar::OnSetFocus() {
back_->SetVisible(false);
bf_separator_->SetVisible(false);
forward_->SetVisible(false);
Layout();
SchedulePaint();
}
SkBitmap CompactNavigationBar::GetFavIcon() const {
return SkBitmap();
}
std::wstring CompactNavigationBar::GetTitle() const {
return std::wstring();
}
////////////////////////////////////////////////////////////////////////////////
// BubblePositioner implementation.
gfx::Rect CompactNavigationBar::GetLocationStackBounds() const {
gfx::Point origin;
ConvertPointToScreen(this, &origin);
return gfx::Rect(origin, size());
}
////////////////////////////////////////////////////////////////////////////////
// CommandUpdater::CommandObserver implementation.
void CompactNavigationBar::EnabledStateChangedForCommand(int id, bool enabled) {
switch (id) {
case IDC_BACK:
back_->SetEnabled(enabled);
break;
case IDC_FORWARD:
forward_->SetEnabled(enabled);
break;
}
}
////////////////////////////////////////////////////////////////////////////////
// CompactNavigationBar private:
void CompactNavigationBar::AddTabWithURL(const GURL& url,
PageTransition::Type transition) {
Browser* browser = browser_view_->browser();
switch (StatusAreaView::GetOpenTabsMode()) {
case StatusAreaView::OPEN_TABS_ON_LEFT: {
// Add the new tab at the first non-pinned location.
int index = browser->tabstrip_model()->IndexOfFirstNonAppTab();
browser->AddTabWithURL(url, GURL(), transition,
true, index, true, NULL);
break;
}
case StatusAreaView::OPEN_TABS_CLOBBER: {
browser->GetSelectedTabContents()->controller().LoadURL(
url, GURL(), transition);
break;
}
case StatusAreaView::OPEN_TABS_ON_RIGHT: {
browser->AddTabWithURL(url, GURL(), transition, true, -1, true, NULL);
break;
}
}
}
} // namespace chromeos
|