panel_browser_view.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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  if (panel_controller_.get() == NULL) {
60    panel_controller_.reset(new PanelController(this, GetNativeHandle()));
61    panel_controller_->Init(
62        true /* focus when opened */, bounds(), creator_xid_,
63        WM_IPC_PANEL_USER_RESIZE_HORIZONTALLY_AND_VERTICALLY);
64  }
65  ::BrowserView::Show();
66}
67
68void PanelBrowserView::SetBounds(const gfx::Rect& bounds) {
69  gfx::Rect limit_bounds = bounds;
70  LimitBounds(&limit_bounds);
71  ::BrowserView::SetBounds(limit_bounds);
72}
73
74void PanelBrowserView::Close() {
75  ::BrowserView::Close();
76  if (panel_controller_.get())
77    panel_controller_->Close();
78}
79
80void PanelBrowserView::UpdateTitleBar() {
81  ::BrowserView::UpdateTitleBar();
82  if (panel_controller_.get())
83    panel_controller_->UpdateTitleBar();
84}
85
86void PanelBrowserView::ActivationChanged(bool activated) {
87  ::BrowserView::ActivationChanged(activated);
88  if (panel_controller_.get()) {
89    if (activated)
90      panel_controller_->OnFocusIn();
91    else
92      panel_controller_->OnFocusOut();
93  }
94}
95
96void PanelBrowserView::SetCreatorView(PanelBrowserView* creator) {
97  DCHECK(creator);
98  GtkWindow* window = creator->GetNativeHandle();
99  creator_xid_ = x11_util::GetX11WindowFromGtkWidget(GTK_WIDGET(window));
100}
101
102bool PanelBrowserView::GetSavedWindowBounds(gfx::Rect* bounds) const {
103  bool res = ::BrowserView::GetSavedWindowBounds(bounds);
104  if (res)
105    LimitBounds(bounds);
106  return res;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110// PanelController::Delegate overrides.
111
112string16 PanelBrowserView::GetPanelTitle() {
113  return browser()->GetWindowTitleForCurrentTab();
114}
115
116SkBitmap PanelBrowserView::GetPanelIcon() {
117  return browser()->GetCurrentPageIcon();
118}
119
120void PanelBrowserView::ClosePanel() {
121  Close();
122}
123
124}  // namespace chromeos
125