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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
// Copyright (c) 2011 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/frame/browser_view.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chromeos/frame/panel_browser_view.h"
#include "chrome/browser/chromeos/status/input_method_menu_button.h"
#include "chrome/browser/chromeos/status/network_menu_button.h"
#include "chrome/browser/chromeos/status/status_area_button.h"
#include "chrome/browser/chromeos/status/status_area_view.h"
#include "chrome/browser/chromeos/view_ids.h"
#include "chrome/browser/chromeos/wm_ipc.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/browser/ui/views/frame/browser_frame_gtk.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/browser_view_layout.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/theme_background.h"
#include "chrome/browser/ui/views/toolbar_view.h"
#include "chrome/common/chrome_switches.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources_standard.h"
#include "third_party/cros/chromeos_wm_ipc_enums.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/canvas.h"
#include "views/controls/button/button.h"
#include "views/controls/button/image_button.h"
#include "views/controls/menu/menu_delegate.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/screen.h"
#include "views/widget/root_view.h"
#include "views/window/hit_test.h"
#include "views/window/window.h"
namespace {
// Amount to offset the toolbar by when vertical tabs are enabled.
const int kVerticalTabStripToolbarOffset = 2;
// Amount to tweak the position of the status area to get it to look right.
const int kStatusAreaVerticalAdjustment = -1;
// If a popup window is larger than this fraction of the screen, create a tab.
const float kPopupMaxWidthFactor = 0.5;
const float kPopupMaxHeightFactor = 0.6;
// This MenuItemView delegate class forwards to the
// SimpleMenuModel::Delegate() implementation.
class SimpleMenuModelDelegateAdapter : public views::MenuDelegate {
public:
explicit SimpleMenuModelDelegateAdapter(
ui::SimpleMenuModel::Delegate* simple_menu_model_delegate);
// views::MenuDelegate implementation.
virtual bool GetAccelerator(int id,
views::Accelerator* accelerator) OVERRIDE;
virtual std::wstring GetLabel(int id) const OVERRIDE;
virtual bool IsCommandEnabled(int id) const OVERRIDE;
virtual bool IsItemChecked(int id) const OVERRIDE;
virtual void ExecuteCommand(int id) OVERRIDE;
private:
ui::SimpleMenuModel::Delegate* simple_menu_model_delegate_;
DISALLOW_COPY_AND_ASSIGN(SimpleMenuModelDelegateAdapter);
};
// SimpleMenuModelDelegateAdapter:
SimpleMenuModelDelegateAdapter::SimpleMenuModelDelegateAdapter(
ui::SimpleMenuModel::Delegate* simple_menu_model_delegate)
: simple_menu_model_delegate_(simple_menu_model_delegate) {
}
// SimpleMenuModelDelegateAdapter, views::MenuDelegate implementation.
bool SimpleMenuModelDelegateAdapter::GetAccelerator(
int id,
views::Accelerator* accelerator) {
return simple_menu_model_delegate_->GetAcceleratorForCommandId(
id, accelerator);
}
std::wstring SimpleMenuModelDelegateAdapter::GetLabel(int id) const {
return UTF16ToWide(simple_menu_model_delegate_->GetLabelForCommandId(id));
}
bool SimpleMenuModelDelegateAdapter::IsCommandEnabled(int id) const {
return simple_menu_model_delegate_->IsCommandIdEnabled(id);
}
bool SimpleMenuModelDelegateAdapter::IsItemChecked(int id) const {
return simple_menu_model_delegate_->IsCommandIdChecked(id);
}
void SimpleMenuModelDelegateAdapter::ExecuteCommand(int id) {
simple_menu_model_delegate_->ExecuteCommand(id);
}
} // namespace
namespace chromeos {
// LayoutManager for BrowserView, which layouts extra components such as
// the status views as follows:
// ____ __ __
// / \ \ \ [StatusArea]
//
class BrowserViewLayout : public ::BrowserViewLayout {
public:
BrowserViewLayout() : ::BrowserViewLayout() {}
virtual ~BrowserViewLayout() {}
//////////////////////////////////////////////////////////////////////////////
// BrowserViewLayout overrides:
void Installed(views::View* host) {
status_area_ = NULL;
::BrowserViewLayout::Installed(host);
}
void ViewAdded(views::View* host,
views::View* view) {
::BrowserViewLayout::ViewAdded(host, view);
switch (view->id()) {
case VIEW_ID_STATUS_AREA:
status_area_ = static_cast<chromeos::StatusAreaView*>(view);
break;
}
}
// In the normal and the compact navigation bar mode, ChromeOS
// layouts compact navigation buttons and status views in the title
// area. See Layout
virtual int LayoutTabStripRegion() OVERRIDE {
if (browser_view_->IsFullscreen() || !browser_view_->IsTabStripVisible()) {
status_area_->SetVisible(false);
tabstrip_->SetVisible(false);
tabstrip_->SetBounds(0, 0, 0, 0);
return 0;
}
gfx::Rect tabstrip_bounds(
browser_view_->frame()->GetBoundsForTabStrip(tabstrip_));
gfx::Point tabstrip_origin = tabstrip_bounds.origin();
views::View::ConvertPointToView(browser_view_->parent(), browser_view_,
&tabstrip_origin);
tabstrip_bounds.set_origin(tabstrip_origin);
return browser_view_->UseVerticalTabs() ?
LayoutTitlebarComponentsWithVerticalTabs(tabstrip_bounds) :
LayoutTitlebarComponents(tabstrip_bounds);
}
virtual int LayoutToolbar(int top) OVERRIDE {
if (!browser_view_->IsFullscreen() && browser_view_->IsTabStripVisible() &&
browser_view_->UseVerticalTabs()) {
// For vertical tabs the toolbar is positioned in
// LayoutTitlebarComponentsWithVerticalTabs.
return top;
}
return ::BrowserViewLayout::LayoutToolbar(top);
}
virtual bool IsPositionInWindowCaption(const gfx::Point& point) OVERRIDE {
return ::BrowserViewLayout::IsPositionInWindowCaption(point)
&& !IsPointInViewsInTitleArea(point);
}
virtual int NonClientHitTest(const gfx::Point& point) OVERRIDE {
gfx::Point point_in_browser_view_coords(point);
views::View::ConvertPointToView(
browser_view_->parent(), browser_view_,
&point_in_browser_view_coords);
return IsPointInViewsInTitleArea(point_in_browser_view_coords) ?
HTCLIENT : ::BrowserViewLayout::NonClientHitTest(point);
}
private:
chromeos::BrowserView* chromeos_browser_view() {
return static_cast<chromeos::BrowserView*>(browser_view_);
}
// Tests if the point is on one of views that are within the
// considered title bar area of client view.
bool IsPointInViewsInTitleArea(const gfx::Point& point)
const {
gfx::Point point_in_status_area_coords(point);
views::View::ConvertPointToView(browser_view_, status_area_,
&point_in_status_area_coords);
if (status_area_->HitTest(point_in_status_area_coords))
return true;
return false;
}
// Positions the titlebar, toolbar and tabstrip. This is
// used when side tabs are enabled.
int LayoutTitlebarComponentsWithVerticalTabs(const gfx::Rect& bounds) {
if (bounds.IsEmpty())
return 0;
tabstrip_->SetVisible(true);
status_area_->SetVisible(true);
gfx::Size status_size = status_area_->GetPreferredSize();
int status_height = status_size.height();
int status_x = bounds.x();
// Layout the status area.
status_area_->SetBounds(status_x, bounds.bottom() - status_height,
status_size.width(), status_height);
// The tabstrip's width is the bigger of it's preferred width and the width
// the status area.
int tabstrip_w = std::max(status_x + status_size.width(),
tabstrip_->GetPreferredSize().width());
tabstrip_->SetBounds(bounds.x(), bounds.y(), tabstrip_w,
bounds.height() - status_height);
// The toolbar is promoted to the title for vertical tabs.
bool toolbar_visible = browser_view_->IsToolbarVisible();
int toolbar_height = 0;
if (toolbar_) {
toolbar_->SetVisible(toolbar_visible);
if (toolbar_visible)
toolbar_height = toolbar_->GetPreferredSize().height();
int tabstrip_max_x = tabstrip_->bounds().right();
toolbar_->SetBounds(tabstrip_max_x,
bounds.y() - kVerticalTabStripToolbarOffset,
browser_view_->width() - tabstrip_max_x,
toolbar_height);
}
// Adjust the available bounds for other components.
gfx::Rect available_bounds = vertical_layout_rect();
available_bounds.Inset(tabstrip_w, 0, 0, 0);
set_vertical_layout_rect(available_bounds);
return bounds.y() + toolbar_height;
}
// Lays out tabstrip and status area in the title bar area (given by
// |bounds|).
int LayoutTitlebarComponents(const gfx::Rect& bounds) {
if (bounds.IsEmpty())
return 0;
tabstrip_->SetVisible(true);
status_area_->SetVisible(true);
// Layout status area after tab strip.
gfx::Size status_size = status_area_->GetPreferredSize();
status_area_->SetBounds(
bounds.right() - status_size.width(),
bounds.y() + kStatusAreaVerticalAdjustment,
status_size.width(),
status_size.height());
tabstrip_->SetBounds(bounds.x(), bounds.y(),
std::max(0, status_area_->bounds().x() - bounds.x()),
bounds.height());
return bounds.bottom();
}
chromeos::StatusAreaView* status_area_;
DISALLOW_COPY_AND_ASSIGN(BrowserViewLayout);
};
// BrowserView
BrowserView::BrowserView(Browser* browser)
: ::BrowserView(browser),
status_area_(NULL),
saved_focused_widget_(NULL) {
system_menu_delegate_.reset(new SimpleMenuModelDelegateAdapter(this));
}
BrowserView::~BrowserView() {
if (toolbar())
toolbar()->RemoveMenuListener(this);
}
// BrowserView, ::BrowserView overrides:
void BrowserView::Init() {
::BrowserView::Init();
status_area_ = new StatusAreaView(this);
status_area_->set_id(VIEW_ID_STATUS_AREA);
AddChildView(status_area_);
status_area_->Init();
frame()->non_client_view()->SetContextMenuController(this);
// Listen to wrench menu opens.
if (toolbar())
toolbar()->AddMenuListener(this);
// Make sure the window is set to the right type.
std::vector<int> params;
params.push_back(browser()->tab_count());
params.push_back(browser()->active_index());
params.push_back(gtk_get_current_event_time());
WmIpc::instance()->SetWindowType(
GTK_WIDGET(frame()->GetNativeWindow()),
WM_IPC_WINDOW_CHROME_TOPLEVEL,
¶ms);
}
void BrowserView::Show() {
ShowInternal(true);
}
void BrowserView::ShowInactive() {
ShowInternal(false);
}
void BrowserView::ShowInternal(bool is_active) {
bool was_visible = frame()->IsVisible();
if (is_active)
::BrowserView::Show();
else
::BrowserView::ShowInactive();
if (!was_visible) {
// Have to update the tab count and selected index to reflect reality.
std::vector<int> params;
params.push_back(browser()->tab_count());
params.push_back(browser()->active_index());
WmIpc::instance()->SetWindowType(
GTK_WIDGET(frame()->GetNativeWindow()),
WM_IPC_WINDOW_CHROME_TOPLEVEL,
¶ms);
}
}
void BrowserView::FocusChromeOSStatus() {
SaveFocusedView();
status_area_->SetPaneFocus(last_focused_view_storage_id(), NULL);
}
views::LayoutManager* BrowserView::CreateLayoutManager() const {
return new BrowserViewLayout();
}
void BrowserView::ChildPreferredSizeChanged(View* child) {
Layout();
}
bool BrowserView::GetSavedWindowBounds(gfx::Rect* bounds) const {
if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeosFrame)) {
// Typically we don't request a full screen size. This means we'll request a
// non-full screen size, layout/paint at that size, then the window manager
// will snap us to full screen size. This results in an ugly
// resize/paint. To avoid this we always request a full screen size.
*bounds = views::Screen::GetMonitorWorkAreaNearestWindow(
GTK_WIDGET(GetWidget()->GetNativeWindow()));
return true;
}
return ::BrowserView::GetSavedWindowBounds(bounds);
}
void BrowserView::Cut() {
gtk_util::DoCut(this);
}
void BrowserView::Copy() {
gtk_util::DoCopy(this);
}
void BrowserView::Paste() {
gtk_util::DoPaste(this);
}
WindowOpenDisposition BrowserView::GetDispositionForPopupBounds(
const gfx::Rect& bounds) {
// If a popup is larger than a given fraction of the screen, turn it into
// a foreground tab. Also check for width or height == 0, which would
// indicate a tab sized popup window.
GdkScreen* screen = gdk_screen_get_default();
int max_width = gdk_screen_get_width(screen) * kPopupMaxWidthFactor;
int max_height = gdk_screen_get_height(screen) * kPopupMaxHeightFactor;
if (bounds.width() > max_width ||
bounds.width() == 0 ||
bounds.height() > max_height ||
bounds.height() == 0) {
return NEW_FOREGROUND_TAB;
}
return NEW_POPUP;
}
// views::ContextMenuController implementation.
void BrowserView::ShowContextMenuForView(views::View* source,
const gfx::Point& p,
bool is_mouse_gesture) {
// Only show context menu if point is in unobscured parts of browser, i.e.
// if NonClientHitTest returns :
// - HTCAPTION: in title bar or unobscured part of tabstrip
// - HTNOWHERE: as the name implies.
gfx::Point point_in_parent_coords(p);
views::View::ConvertPointToView(NULL, parent(), &point_in_parent_coords);
int hit_test = NonClientHitTest(point_in_parent_coords);
if (hit_test == HTCAPTION || hit_test == HTNOWHERE) {
// rebuild menu so it reflects current application state
InitSystemMenu();
system_menu_->RunMenuAt(source->GetWidget()->GetNativeWindow(), NULL,
gfx::Rect(p, gfx::Size(0,0)),
views::MenuItemView::TOPLEFT,
true);
}
}
// BrowserView, views::MenuListener implementation.
void BrowserView::OnMenuOpened() {
// Save the focused widget before wrench menu opens.
saved_focused_widget_ = gtk_window_get_focus(GetNativeHandle());
}
// BrowserView, StatusAreaHost implementation.
Profile* BrowserView::GetProfile() const {
return browser()->profile();
}
gfx::NativeWindow BrowserView::GetNativeWindow() const {
return GetWidget()->GetNativeWindow();
}
bool BrowserView::ShouldOpenButtonOptions(
const views::View* button_view) const {
return true;
}
void BrowserView::ExecuteBrowserCommand(int id) const {
browser()->ExecuteCommand(id);
}
void BrowserView::OpenButtonOptions(const views::View* button_view) {
if (button_view == status_area_->network_view()) {
browser()->OpenInternetOptionsDialog();
} else if (button_view == status_area_->input_method_view()) {
browser()->OpenLanguageOptionsDialog();
} else {
browser()->OpenSystemOptionsDialog();
}
}
StatusAreaHost::ScreenMode BrowserView::GetScreenMode() const {
return kBrowserMode;
}
StatusAreaHost::TextStyle BrowserView::GetTextStyle() const {
ui::ThemeProvider* tp = GetThemeProvider();
return tp->HasCustomImage(IDR_THEME_FRAME) ?
StatusAreaHost::kWhiteHaloed : (IsOffTheRecord() ?
StatusAreaHost::kWhitePlain : StatusAreaHost::kGrayEmbossed);
}
// BrowserView protected:
void BrowserView::GetAccessiblePanes(
std::vector<AccessiblePaneView*>* panes) {
::BrowserView::GetAccessiblePanes(panes);
panes->push_back(status_area_);
}
// BrowserView private.
void BrowserView::InitSystemMenu() {
system_menu_.reset(new views::MenuItemView(system_menu_delegate_.get()));
system_menu_->AppendDelegateMenuItem(IDC_RESTORE_TAB);
system_menu_->AppendMenuItemWithLabel(
IDC_NEW_TAB,
UTF16ToWide(l10n_util::GetStringUTF16(IDS_NEW_TAB)));
system_menu_->AppendSeparator();
system_menu_->AppendMenuItemWithLabel(
IDC_TASK_MANAGER,
UTF16ToWide(l10n_util::GetStringUTF16(IDS_TASK_MANAGER)));
}
} // namespace chromeos
// static
BrowserWindow* BrowserWindow::CreateBrowserWindow(Browser* browser) {
// Create a browser view for chromeos.
BrowserView* view;
if (browser->is_type_popup() || browser->is_type_panel())
view = new chromeos::PanelBrowserView(browser);
else
view = new chromeos::BrowserView(browser);
(new BrowserFrame(view))->InitBrowserFrame();
return view;
}
|