1// Copyright (c) 2011 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 "chrome/browser/chromeos/frame/panel_browser_view.h"
6
7#include "chrome/browser/chromeos/frame/panel_controller.h"
8#include "third_party/cros/chromeos_wm_ipc_enums.h"
9#include "views/widget/widget.h"
10#include "views/window/window.h"
11
12namespace {
13
14const int kPanelMinWidthPixels = 100;
15const int kPanelMinHeightPixels = 100;
16const int kPanelDefaultWidthPixels = 250;
17const int kPanelDefaultHeightPixels = 300;
18const float kPanelMaxWidthFactor = 0.80;
19const float kPanelMaxHeightFactor = 0.80;
20
21}
22
23namespace chromeos {
24
25PanelBrowserView::PanelBrowserView(Browser* browser)
26    : BrowserView(browser),
27      creator_xid_(0) {
28}
29
30////////////////////////////////////////////////////////////////////////////////
31// PanelBrowserView functions
32
33void PanelBrowserView::LimitBounds(gfx::Rect* bounds) const {
34  GdkScreen* screen = gtk_widget_get_screen(GetWidget()->GetNativeView());
35  int max_width = gdk_screen_get_width(screen) * kPanelMaxWidthFactor;
36  int max_height = gdk_screen_get_height(screen) * kPanelMaxHeightFactor;
37
38  if (bounds->width() == 0 && bounds->height() == 0) {
39    bounds->set_width(kPanelDefaultWidthPixels);
40    bounds->set_height(kPanelDefaultHeightPixels);
41  }
42
43  if (bounds->width() < kPanelMinWidthPixels)
44    bounds->set_width(kPanelMinWidthPixels);
45  else if (bounds->width() > max_width)
46    bounds->set_width(max_width);
47
48  if (bounds->height() < kPanelMinHeightPixels)
49    bounds->set_height(kPanelMinHeightPixels);
50  else if (bounds->height() > max_height)
51    bounds->set_height(max_height);
52}
53
54
55////////////////////////////////////////////////////////////////////////////////
56// BrowserView overrides.
57
58void PanelBrowserView::Show() {
59  InitPanelController(true);  // focus when opened
60  ::BrowserView::Show();
61}
62
63void PanelBrowserView::ShowInactive() {
64  InitPanelController(false);
65  ::BrowserView::ShowInactive();
66}
67
68void PanelBrowserView::InitPanelController(bool is_active) {
69  if (panel_controller_.get() == NULL) {
70    panel_controller_.reset(new PanelController(this, GetNativeHandle()));
71    panel_controller_->Init(
72        is_active, bounds(), creator_xid_,
73        WM_IPC_PANEL_USER_RESIZE_HORIZONTALLY_AND_VERTICALLY);
74  }
75}
76
77void PanelBrowserView::SetBounds(const gfx::Rect& bounds) {
78  gfx::Rect limit_bounds = bounds;
79  LimitBounds(&limit_bounds);
80  ::BrowserView::SetBounds(limit_bounds);
81}
82
83void PanelBrowserView::Close() {
84  ::BrowserView::Close();
85  if (panel_controller_.get())
86    panel_controller_->Close();
87}
88
89void PanelBrowserView::UpdateTitleBar() {
90  ::BrowserView::UpdateTitleBar();
91  if (panel_controller_.get())
92    panel_controller_->UpdateTitleBar();
93}
94
95void PanelBrowserView::SetCreatorView(PanelBrowserView* creator) {
96  DCHECK(creator);
97  GtkWindow* window = creator->GetNativeHandle();
98  creator_xid_ = ui::GetX11WindowFromGtkWidget(GTK_WIDGET(window));
99}
100
101bool PanelBrowserView::GetSavedWindowBounds(gfx::Rect* bounds) const {
102  bool res = ::BrowserView::GetSavedWindowBounds(bounds);
103  if (res)
104    LimitBounds(bounds);
105  return res;
106}
107
108void PanelBrowserView::OnWindowActivationChanged(bool active) {
109  ::BrowserView::OnWindowActivationChanged(active);
110  if (panel_controller_.get()) {
111    if (active)
112      panel_controller_->OnFocusIn();
113    else
114      panel_controller_->OnFocusOut();
115  }
116}
117
118////////////////////////////////////////////////////////////////////////////////
119// TabStripModelObserver overrides.
120
121void PanelBrowserView::TabChangedAt(TabContentsWrapper* contents,
122                                    int index,
123                                    TabChangeType change_type) {
124  if (change_type == TabStripModelObserver::TITLE_NOT_LOADING)
125    panel_controller_->SetUrgent(true);
126}
127
128////////////////////////////////////////////////////////////////////////////////
129// PanelController::Delegate overrides.
130
131string16 PanelBrowserView::GetPanelTitle() {
132  return browser()->GetWindowTitleForCurrentTab();
133}
134
135SkBitmap PanelBrowserView::GetPanelIcon() {
136  return browser()->GetCurrentPageIcon();
137}
138
139bool PanelBrowserView::CanClosePanel() {
140  return ::BrowserView::CanClose();
141}
142
143void PanelBrowserView::ClosePanel() {
144  Close();
145}
146
147void PanelBrowserView::ActivatePanel() {
148  Activate();
149}
150
151}  // namespace chromeos
152