1// Copyright (c) 2012 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/ui/views/panels/panel_frame_view.h"
6
7#include "chrome/browser/ui/panels/panel.h"
8#include "chrome/browser/ui/panels/panel_constants.h"
9#include "chrome/browser/ui/views/panels/panel_view.h"
10#include "chrome/browser/ui/views/tab_icon_view.h"
11#include "content/public/browser/web_contents.h"
12#include "grit/generated_resources.h"
13#include "grit/theme_resources.h"
14#include "grit/ui_resources.h"
15#include "ui/base/hit_test.h"
16#include "ui/base/l10n/l10n_util.h"
17#include "ui/base/resource/resource_bundle.h"
18#include "ui/gfx/canvas.h"
19#include "ui/gfx/path.h"
20#include "ui/views/controls/button/image_button.h"
21#include "ui/views/controls/label.h"
22#include "ui/views/widget/widget.h"
23#include "ui/views/widget/widget_delegate.h"
24
25#if defined(OS_WIN)
26#include "base/win/scoped_gdi_object.h"
27#include "ui/base/win/shell.h"
28#include "ui/gfx/path_win.h"
29#include "ui/views/win/hwnd_util.h"
30#endif
31
32#if defined(USE_AURA)
33#include "ui/aura/window.h"
34#endif
35
36namespace {
37
38// The thickness of the border when Aero is not enabled. In this case, the
39// shadow around the window will not be painted by the system and we need to
40// paint a frame in order to differentiate the client area from the background.
41const int kNonAeroBorderThickness = 1;
42
43// The height and width in pixels of the icon.
44const int kIconSize = 16;
45
46// The font to use to draw the title.
47const char* kTitleFontName = "Arial Bold";
48const int kTitleFontSize = 14;
49
50// The extra padding between the button and the top edge.
51const int kExtraPaddingBetweenButtonAndTop = 1;
52
53// Colors used to draw titlebar background under default theme.
54const SkColor kActiveBackgroundDefaultColor = SkColorSetRGB(0x3a, 0x3d, 0x3d);
55const SkColor kInactiveBackgroundDefaultColor = SkColorSetRGB(0x7a, 0x7c, 0x7c);
56const SkColor kAttentionBackgroundDefaultColor =
57    SkColorSetRGB(0x53, 0xa9, 0x3f);
58
59// Color used to draw the minimized panel.
60const SkColor kMinimizeBackgroundDefaultColor = SkColorSetRGB(0xf5, 0xf4, 0xf0);
61
62// Color used to draw the title text under default theme.
63const SkColor kTitleTextDefaultColor = SkColorSetRGB(0xf9, 0xf9, 0xf9);
64
65gfx::ImageSkia* CreateImageForColor(SkColor color) {
66  gfx::Canvas canvas(gfx::Size(1, 1), 1.0f, true);
67  canvas.DrawColor(color);
68  return new gfx::ImageSkia(canvas.ExtractImageRep());
69}
70
71#if defined(OS_WIN)
72const gfx::ImageSkia& GetTopLeftCornerImage(panel::CornerStyle corner_style) {
73  static gfx::ImageSkia* rounded_image = NULL;
74  static gfx::ImageSkia* non_rounded_image = NULL;
75  if (!rounded_image) {
76    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
77    rounded_image = rb.GetImageSkiaNamed(IDR_WINDOW_TOP_LEFT_CORNER);
78    non_rounded_image = rb.GetImageSkiaNamed(IDR_PANEL_TOP_LEFT_CORNER);
79  }
80  return (corner_style & panel::TOP_ROUNDED) ? *rounded_image
81                                             : *non_rounded_image;
82}
83
84const gfx::ImageSkia& GetTopRightCornerImage(panel::CornerStyle corner_style) {
85  static gfx::ImageSkia* rounded_image = NULL;
86  static gfx::ImageSkia* non_rounded_image = NULL;
87  if (!rounded_image) {
88    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
89    rounded_image = rb.GetImageSkiaNamed(IDR_WINDOW_TOP_RIGHT_CORNER);
90    non_rounded_image = rb.GetImageSkiaNamed(IDR_PANEL_TOP_RIGHT_CORNER);
91  }
92  return (corner_style & panel::TOP_ROUNDED) ? *rounded_image
93                                             : *non_rounded_image;
94}
95
96const gfx::ImageSkia& GetBottomLeftCornerImage(
97    panel::CornerStyle corner_style) {
98  static gfx::ImageSkia* rounded_image = NULL;
99  static gfx::ImageSkia* non_rounded_image = NULL;
100  if (!rounded_image) {
101    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
102    rounded_image = rb.GetImageSkiaNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER);
103    non_rounded_image = rb.GetImageSkiaNamed(IDR_PANEL_BOTTOM_LEFT_CORNER);
104  }
105  return (corner_style & panel::BOTTOM_ROUNDED) ? *rounded_image
106                                                : *non_rounded_image;
107}
108
109const gfx::ImageSkia& GetBottomRightCornerImage(
110    panel::CornerStyle corner_style) {
111  static gfx::ImageSkia* rounded_image = NULL;
112  static gfx::ImageSkia* non_rounded_image = NULL;
113  if (!rounded_image) {
114    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
115    rounded_image = rb.GetImageSkiaNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER);
116    non_rounded_image = rb.GetImageSkiaNamed(IDR_PANEL_BOTTOM_RIGHT_CORNER);
117  }
118  return (corner_style & panel::BOTTOM_ROUNDED) ? *rounded_image
119                                                : *non_rounded_image;
120}
121
122const gfx::ImageSkia& GetTopEdgeImage() {
123  static gfx::ImageSkia* image = NULL;
124  if (!image) {
125    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
126    image = rb.GetImageSkiaNamed(IDR_WINDOW_TOP_CENTER);
127  }
128  return *image;
129}
130
131const gfx::ImageSkia& GetBottomEdgeImage() {
132  static gfx::ImageSkia* image = NULL;
133  if (!image) {
134    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
135    image = rb.GetImageSkiaNamed(IDR_WINDOW_BOTTOM_CENTER);
136  }
137  return *image;
138}
139
140const gfx::ImageSkia& GetLeftEdgeImage() {
141  static gfx::ImageSkia* image = NULL;
142  if (!image) {
143    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
144    image = rb.GetImageSkiaNamed(IDR_WINDOW_LEFT_SIDE);
145  }
146  return *image;
147}
148
149const gfx::ImageSkia& GetRightEdgeImage() {
150  static gfx::ImageSkia* image = NULL;
151  if (!image) {
152    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
153    image = rb.GetImageSkiaNamed(IDR_WINDOW_RIGHT_SIDE);
154  }
155  return *image;
156}
157#endif  // defined(OS_WIN)
158
159const gfx::Font& GetTitleFont() {
160  static gfx::Font* font = NULL;
161  if (!font)
162    font = new gfx::Font(kTitleFontName, kTitleFontSize);
163  return *font;
164}
165
166const gfx::ImageSkia* GetActiveBackgroundDefaultImage() {
167  static gfx::ImageSkia* image = NULL;
168  if (!image)
169    image = CreateImageForColor(kActiveBackgroundDefaultColor);
170  return image;
171}
172
173const gfx::ImageSkia* GetInactiveBackgroundDefaultImage() {
174  static gfx::ImageSkia* image = NULL;
175  if (!image)
176    image = CreateImageForColor(kInactiveBackgroundDefaultColor);
177  return image;
178}
179
180const gfx::ImageSkia* GetAttentionBackgroundDefaultImage() {
181  static gfx::ImageSkia* image = NULL;
182  if (!image)
183    image = CreateImageForColor(kAttentionBackgroundDefaultColor);
184  return image;
185}
186
187const gfx::ImageSkia* GetMinimizeBackgroundDefaultImage() {
188  static gfx::ImageSkia* image = NULL;
189  if (!image)
190    image = CreateImageForColor(kMinimizeBackgroundDefaultColor);
191  return image;
192}
193
194int GetFrameEdgeHitTest(const gfx::Point& point,
195                        const gfx::Size& frame_size,
196                        int resize_area_size,
197                        panel::Resizability resizability) {
198  int x = point.x();
199  int y = point.y();
200  int width = frame_size.width();
201  int height = frame_size.height();
202  if (x < resize_area_size) {
203    if (y < resize_area_size && (resizability & panel::RESIZABLE_TOP_LEFT)) {
204      return HTTOPLEFT;
205    } else if (y >= height - resize_area_size &&
206              (resizability & panel::RESIZABLE_BOTTOM_LEFT)) {
207      return HTBOTTOMLEFT;
208    } else if (resizability & panel::RESIZABLE_LEFT) {
209      return HTLEFT;
210    }
211  } else if (x >= width - resize_area_size) {
212    if (y < resize_area_size && (resizability & panel::RESIZABLE_TOP_RIGHT)) {
213      return HTTOPRIGHT;
214    } else if (y >= height - resize_area_size &&
215              (resizability & panel::RESIZABLE_BOTTOM_RIGHT)) {
216      return HTBOTTOMRIGHT;
217    } else if (resizability & panel::RESIZABLE_RIGHT) {
218      return HTRIGHT;
219    }
220  }
221
222  if (y < resize_area_size && (resizability & panel::RESIZABLE_TOP)) {
223    return HTTOP;
224  } else if (y >= height - resize_area_size &&
225            (resizability & panel::RESIZABLE_BOTTOM)) {
226    return HTBOTTOM;
227  }
228
229  return HTNOWHERE;
230}
231
232// Frameless is only supported when Aero is enabled and shadow effect is
233// present.
234bool ShouldRenderAsFrameless() {
235#if defined(OS_WIN)
236  bool is_frameless = ui::win::IsAeroGlassEnabled();
237  if (is_frameless) {
238    BOOL shadow_enabled = FALSE;
239    if (::SystemParametersInfo(SPI_GETDROPSHADOW, 0, &shadow_enabled, 0) &&
240        !shadow_enabled)
241      is_frameless = false;
242  }
243  return is_frameless;
244#else
245  return false;
246#endif
247}
248
249}  // namespace
250
251// static
252const char PanelFrameView::kViewClassName[] = "PanelFrameView";
253
254PanelFrameView::PanelFrameView(PanelView* panel_view)
255    : is_frameless_(ShouldRenderAsFrameless()),
256      panel_view_(panel_view),
257      close_button_(NULL),
258      minimize_button_(NULL),
259      restore_button_(NULL),
260      title_icon_(NULL),
261      title_label_(NULL),
262      corner_style_(panel::ALL_ROUNDED) {
263}
264
265PanelFrameView::~PanelFrameView() {
266}
267
268void PanelFrameView::Init() {
269  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
270
271  close_button_ = new views::ImageButton(this);
272  close_button_->SetImage(views::CustomButton::STATE_NORMAL,
273                          rb.GetImageSkiaNamed(IDR_PANEL_CLOSE));
274  close_button_->SetImage(views::CustomButton::STATE_HOVERED,
275                          rb.GetImageSkiaNamed(IDR_PANEL_CLOSE_H));
276  close_button_->SetImage(views::CustomButton::STATE_PRESSED,
277                          rb.GetImageSkiaNamed(IDR_PANEL_CLOSE_C));
278  close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
279                                   views::ImageButton::ALIGN_MIDDLE);
280  base::string16 tooltip_text =
281      l10n_util::GetStringUTF16(IDS_PANEL_CLOSE_TOOLTIP);
282  close_button_->SetTooltipText(tooltip_text);
283  AddChildView(close_button_);
284
285  minimize_button_ = new views::ImageButton(this);
286  minimize_button_->SetImage(views::CustomButton::STATE_NORMAL,
287                             rb.GetImageSkiaNamed(IDR_PANEL_MINIMIZE));
288  minimize_button_->SetImage(views::CustomButton::STATE_HOVERED,
289                             rb.GetImageSkiaNamed(IDR_PANEL_MINIMIZE_H));
290  minimize_button_->SetImage(views::CustomButton::STATE_PRESSED,
291                             rb.GetImageSkiaNamed(IDR_PANEL_MINIMIZE_C));
292  tooltip_text = l10n_util::GetStringUTF16(IDS_PANEL_MINIMIZE_TOOLTIP);
293  minimize_button_->SetTooltipText(tooltip_text);
294  minimize_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
295                                      views::ImageButton::ALIGN_MIDDLE);
296  AddChildView(minimize_button_);
297
298  restore_button_ = new views::ImageButton(this);
299  restore_button_->SetImage(views::CustomButton::STATE_NORMAL,
300                            rb.GetImageSkiaNamed(IDR_PANEL_RESTORE));
301  restore_button_->SetImage(views::CustomButton::STATE_HOVERED,
302                            rb.GetImageSkiaNamed(IDR_PANEL_RESTORE_H));
303  restore_button_->SetImage(views::CustomButton::STATE_PRESSED,
304                            rb.GetImageSkiaNamed(IDR_PANEL_RESTORE_C));
305  restore_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
306                                     views::ImageButton::ALIGN_MIDDLE);
307  tooltip_text = l10n_util::GetStringUTF16(IDS_PANEL_RESTORE_TOOLTIP);
308  restore_button_->SetTooltipText(tooltip_text);
309  restore_button_->SetVisible(false);  // only visible when panel is minimized
310  AddChildView(restore_button_);
311
312  title_icon_ = new TabIconView(this);
313  title_icon_->set_is_light(true);
314  AddChildView(title_icon_);
315  title_icon_->Update();
316
317  title_label_ = new views::Label(panel_view_->panel()->GetWindowTitle());
318  title_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
319  title_label_->SetAutoColorReadabilityEnabled(false);
320  title_label_->SetFont(GetTitleFont());
321  AddChildView(title_label_);
322
323#if defined(USE_AURA)
324  // Compute the thickness of the client area that needs to be counted towards
325  // mouse resizing.
326  int thickness_for_mouse_resizing =
327      PanelView::kResizeInsideBoundsSize - BorderThickness();
328  aura::Window* window = panel_view_->GetNativePanelWindow();
329  window->set_hit_test_bounds_override_inner(
330      gfx::Insets(thickness_for_mouse_resizing, thickness_for_mouse_resizing,
331                  thickness_for_mouse_resizing, thickness_for_mouse_resizing));
332#endif
333}
334
335void PanelFrameView::UpdateTitle() {
336  UpdateWindowTitle();
337}
338
339void PanelFrameView::UpdateIcon() {
340  UpdateWindowIcon();
341}
342
343void PanelFrameView::UpdateThrobber() {
344  title_icon_->Update();
345}
346
347void PanelFrameView::UpdateTitlebarMinimizeRestoreButtonVisibility() {
348  Panel* panel = panel_view_->panel();
349  minimize_button_->SetVisible(panel->CanShowMinimizeButton());
350  restore_button_->SetVisible(panel->CanShowRestoreButton());
351
352  // Reset the button states in case that the hover states are not cleared when
353  // mouse is clicked but not moved.
354  minimize_button_->SetState(views::CustomButton::STATE_NORMAL);
355  restore_button_->SetState(views::CustomButton::STATE_NORMAL);
356}
357
358void PanelFrameView::SetWindowCornerStyle(panel::CornerStyle corner_style) {
359  corner_style_ = corner_style;
360
361#if defined(OS_WIN)
362  // Changing the window region is going to force a paint. Only change the
363  // window region if the region really differs.
364  HWND native_window = views::HWNDForWidget(panel_view_->window());
365  base::win::ScopedRegion current_region(::CreateRectRgn(0, 0, 0, 0));
366  int current_region_result = ::GetWindowRgn(native_window, current_region);
367
368  gfx::Path window_mask;
369  GetWindowMask(size(), &window_mask);
370  base::win::ScopedRegion new_region(gfx::CreateHRGNFromSkPath(window_mask));
371
372  if (current_region_result == ERROR ||
373      !::EqualRgn(current_region, new_region)) {
374    // SetWindowRgn takes ownership of the new_region.
375    ::SetWindowRgn(native_window, new_region.release(), TRUE);
376  }
377#endif
378}
379
380gfx::Rect PanelFrameView::GetBoundsForClientView() const {
381  // The origin of client-area bounds starts after left border and titlebar and
382  // spans until hitting the right and bottom borders.
383  //    +------------------------------+
384  //    |         Top Titlebar         |
385  //    |-+--------------------------+-|
386  //    |L|                          |R|
387  //    |e|                          |i|
388  //    |f|                          |g|
389  //    |t|                          |h|
390  //    | |         Client           |t|
391  //    | |                          | |
392  //    |B|          Area            |B|
393  //    |o|                          |o|
394  //    |r|                          |r|
395  //    |d|                          |d|
396  //    |e|                          |e|
397  //    |r|                          |r|
398  //    | +--------------------------+ |
399  //    |        Bottom Border         |
400  //    +------------------------------+
401  int titlebar_height = TitlebarHeight();
402  int border_thickness = BorderThickness();
403  return gfx::Rect(border_thickness,
404                   titlebar_height,
405                   std::max(0, width() - border_thickness * 2),
406                   std::max(0, height() - titlebar_height - border_thickness));
407}
408
409gfx::Rect PanelFrameView::GetWindowBoundsForClientBounds(
410      const gfx::Rect& client_bounds) const {
411  int titlebar_height = TitlebarHeight();
412  int border_thickness = BorderThickness();
413  // The window bounds include both client area and non-client area (titlebar
414  // and left, right and bottom borders).
415  return gfx::Rect(client_bounds.x() - border_thickness,
416                   client_bounds.y() - titlebar_height,
417                   client_bounds.width() + border_thickness * 2,
418                   client_bounds.height() + titlebar_height + border_thickness);
419}
420
421int PanelFrameView::NonClientHitTest(const gfx::Point& point) {
422  panel::Resizability resizability = panel_view_->panel()->CanResizeByMouse();
423
424  // Check the frame first, as we allow a small area overlapping the contents
425  // to be used for resize handles.
426  int frame_component = GetFrameEdgeHitTest(
427      point, size(), PanelView::kResizeInsideBoundsSize, resizability);
428
429  if (frame_component != HTNOWHERE)
430    return frame_component;
431
432  int client_component =
433      panel_view_->window()->client_view()->NonClientHitTest(point);
434  if (client_component != HTNOWHERE)
435    return client_component;
436
437  if (close_button_ && close_button_->visible() &&
438      close_button_->GetMirroredBounds().Contains(point))
439    return HTCLOSE;
440
441  if (minimize_button_ && minimize_button_->visible() &&
442      minimize_button_->GetMirroredBounds().Contains(point))
443    return HTMINBUTTON;
444
445  if (restore_button_ && restore_button_->visible() &&
446      restore_button_->GetMirroredBounds().Contains(point))
447    return HTMAXBUTTON;
448
449  return HTNOWHERE;
450}
451
452void PanelFrameView::GetWindowMask(const gfx::Size& size,
453                                   gfx::Path* window_mask) {
454  int width = size.width();
455  int height = size.height();
456
457  if (corner_style_ & panel::TOP_ROUNDED) {
458    window_mask->moveTo(0, 3);
459    window_mask->lineTo(1, 2);
460    window_mask->lineTo(1, 1);
461    window_mask->lineTo(2, 1);
462    window_mask->lineTo(3, 0);
463    window_mask->lineTo(SkIntToScalar(width - 3), 0);
464    window_mask->lineTo(SkIntToScalar(width - 2), 1);
465    window_mask->lineTo(SkIntToScalar(width - 1), 1);
466    window_mask->lineTo(SkIntToScalar(width - 1), 2);
467    window_mask->lineTo(SkIntToScalar(width - 1), 3);
468  } else {
469    window_mask->moveTo(0, 0);
470    window_mask->lineTo(width, 0);
471  }
472
473  if (corner_style_ & panel::BOTTOM_ROUNDED) {
474    window_mask->lineTo(SkIntToScalar(width - 1), SkIntToScalar(height - 4));
475    window_mask->lineTo(SkIntToScalar(width - 2), SkIntToScalar(height - 3));
476    window_mask->lineTo(SkIntToScalar(width - 2), SkIntToScalar(height - 2));
477    window_mask->lineTo(SkIntToScalar(width - 3), SkIntToScalar(height - 2));
478    window_mask->lineTo(SkIntToScalar(width - 4), SkIntToScalar(height - 1));
479    window_mask->lineTo(3, SkIntToScalar(height - 1));
480    window_mask->lineTo(2, SkIntToScalar(height - 2));
481    window_mask->lineTo(1, SkIntToScalar(height - 2));
482    window_mask->lineTo(1, SkIntToScalar(height - 3));
483    window_mask->lineTo(0, SkIntToScalar(height - 4));
484  } else {
485    window_mask->lineTo(SkIntToScalar(width), SkIntToScalar(height));
486    window_mask->lineTo(0, SkIntToScalar(height));
487  }
488
489  window_mask->close();
490}
491
492void PanelFrameView::ResetWindowControls() {
493  // The controls aren't affected by this constraint.
494}
495
496void PanelFrameView::UpdateWindowIcon() {
497  title_icon_->SchedulePaint();
498}
499
500void PanelFrameView::UpdateWindowTitle() {
501  title_label_->SetText(panel_view_->panel()->GetWindowTitle());
502}
503
504gfx::Size PanelFrameView::GetPreferredSize() {
505  gfx::Size pref_size =
506      panel_view_->window()->client_view()->GetPreferredSize();
507  gfx::Rect bounds(0, 0, pref_size.width(), pref_size.height());
508  return panel_view_->window()->non_client_view()->
509      GetWindowBoundsForClientBounds(bounds).size();
510}
511
512const char* PanelFrameView::GetClassName() const {
513  return kViewClassName;
514}
515
516gfx::Size PanelFrameView::GetMinimumSize() {
517  return panel_view_->GetMinimumSize();
518}
519
520gfx::Size PanelFrameView::GetMaximumSize() {
521  return panel_view_->GetMaximumSize();
522}
523
524void PanelFrameView::Layout() {
525  is_frameless_ = ShouldRenderAsFrameless();
526
527  // Layout the close button.
528  int right = width();
529  close_button_->SetBounds(
530      width() - panel::kTitlebarRightPadding - panel::kPanelButtonSize,
531      (TitlebarHeight() - panel::kPanelButtonSize) / 2 +
532          kExtraPaddingBetweenButtonAndTop,
533      panel::kPanelButtonSize,
534      panel::kPanelButtonSize);
535  right = close_button_->x();
536
537  // Layout the minimize and restore button. Both occupy the same space,
538  // but at most one is visible at any time.
539  minimize_button_->SetBounds(
540      right - panel::kButtonPadding - panel::kPanelButtonSize,
541      (TitlebarHeight() - panel::kPanelButtonSize) / 2 +
542          kExtraPaddingBetweenButtonAndTop,
543      panel::kPanelButtonSize,
544      panel::kPanelButtonSize);
545  restore_button_->SetBoundsRect(minimize_button_->bounds());
546  right = minimize_button_->x();
547
548  // Layout the icon.
549  int icon_y = (TitlebarHeight() - kIconSize) / 2;
550  title_icon_->SetBounds(
551      panel::kTitlebarLeftPadding,
552      icon_y,
553      kIconSize,
554      kIconSize);
555
556  // Layout the title.
557  int title_x = title_icon_->bounds().right() + panel::kIconAndTitlePadding;
558  int title_height = GetTitleFont().GetHeight();
559  title_label_->SetBounds(
560      title_x,
561      icon_y + ((kIconSize - title_height - 1) / 2),
562      std::max(0, right - panel::kTitleAndButtonPadding - title_x),
563      title_height);
564}
565
566void PanelFrameView::OnPaint(gfx::Canvas* canvas) {
567  UpdateControlStyles(GetPaintState());
568  PaintFrameBackground(canvas);
569  PaintFrameEdge(canvas);
570}
571
572bool PanelFrameView::OnMousePressed(const ui::MouseEvent& event) {
573  if (event.IsOnlyLeftMouseButton()) {
574    // |event.location| is in the view's coordinate system. Convert it to the
575    // screen coordinate system.
576    gfx::Point mouse_location = event.location();
577    views::View::ConvertPointToScreen(this, &mouse_location);
578
579    // If the mouse location falls within the resizing area of the titlebar,
580    // do not handle the event so that the system resizing logic could kick in.
581    if (!panel_view_->IsWithinResizingArea(mouse_location) &&
582        panel_view_->OnTitlebarMousePressed(mouse_location))
583      return true;
584  }
585  return NonClientFrameView::OnMousePressed(event);
586}
587
588bool PanelFrameView::OnMouseDragged(const ui::MouseEvent& event) {
589  // |event.location| is in the view's coordinate system. Convert it to the
590  // screen coordinate system.
591  gfx::Point mouse_location = event.location();
592  views::View::ConvertPointToScreen(this, &mouse_location);
593
594  if (panel_view_->OnTitlebarMouseDragged(mouse_location))
595    return true;
596  return NonClientFrameView::OnMouseDragged(event);
597}
598
599void PanelFrameView::OnMouseReleased(const ui::MouseEvent& event) {
600  if (panel_view_->OnTitlebarMouseReleased(
601          event.IsControlDown() ? panel::APPLY_TO_ALL : panel::NO_MODIFIER))
602    return;
603  NonClientFrameView::OnMouseReleased(event);
604}
605
606void PanelFrameView::OnMouseCaptureLost() {
607  if (panel_view_->OnTitlebarMouseCaptureLost())
608    return;
609  NonClientFrameView::OnMouseCaptureLost();
610}
611
612void PanelFrameView::ButtonPressed(views::Button* sender,
613                                   const ui::Event& event) {
614  if (sender == close_button_) {
615    panel_view_->ClosePanel();
616  } else {
617    panel::ClickModifier modifier =
618        event.IsControlDown() ? panel::APPLY_TO_ALL : panel::NO_MODIFIER;
619    if (sender == minimize_button_)
620      panel_view_->panel()->OnMinimizeButtonClicked(modifier);
621    else if (sender == restore_button_)
622      panel_view_->panel()->OnRestoreButtonClicked(modifier);
623  }
624}
625
626bool PanelFrameView::ShouldTabIconViewAnimate() const {
627  // This function is queried during the creation of the window as the
628  // TabIconView we host is initialized, so we need to NULL check the selected
629  // WebContents because in this condition there is not yet a selected tab.
630  content::WebContents* contents = panel_view_->panel()->GetWebContents();
631  return contents ? contents->IsLoading() : false;
632}
633
634gfx::ImageSkia PanelFrameView::GetFaviconForTabIconView() {
635  return panel_view_->window()->widget_delegate()->GetWindowIcon();
636}
637
638gfx::Size PanelFrameView::NonClientAreaSize() const {
639  if (is_frameless_)
640    return gfx::Size(0, TitlebarHeight());
641  // When the frame is present, the width of non-client area consists of
642  // left and right borders, while the height consists of the top area
643  // (titlebar) and the bottom border.
644  return gfx::Size(2 * kNonAeroBorderThickness,
645                   TitlebarHeight() + kNonAeroBorderThickness);
646}
647
648int PanelFrameView::TitlebarHeight() const {
649  return panel::kTitlebarHeight;
650}
651
652int PanelFrameView::BorderThickness() const {
653  return is_frameless_ ? 0 : kNonAeroBorderThickness;
654}
655
656PanelFrameView::PaintState PanelFrameView::GetPaintState() const {
657  if (panel_view_->panel()->IsDrawingAttention())
658    return PAINT_FOR_ATTENTION;
659  if (bounds().height() <= panel::kMinimizedPanelHeight)
660    return PAINT_AS_MINIMIZED;
661  if (panel_view_->IsPanelActive() &&
662           !panel_view_->force_to_paint_as_inactive())
663    return PAINT_AS_ACTIVE;
664  return PAINT_AS_INACTIVE;
665}
666
667SkColor PanelFrameView::GetTitleColor(PaintState paint_state) const {
668  return kTitleTextDefaultColor;
669}
670
671const gfx::ImageSkia* PanelFrameView::GetFrameBackground(
672    PaintState paint_state) const {
673  switch (paint_state) {
674    case PAINT_AS_INACTIVE:
675      return GetInactiveBackgroundDefaultImage();
676    case PAINT_AS_ACTIVE:
677      return GetActiveBackgroundDefaultImage();
678    case PAINT_AS_MINIMIZED:
679      return GetMinimizeBackgroundDefaultImage();
680    case PAINT_FOR_ATTENTION:
681      return GetAttentionBackgroundDefaultImage();
682    default:
683      NOTREACHED();
684      return GetInactiveBackgroundDefaultImage();
685  }
686}
687
688void PanelFrameView::UpdateControlStyles(PaintState paint_state) {
689  title_label_->SetEnabledColor(GetTitleColor(paint_state));
690}
691
692void PanelFrameView::PaintFrameBackground(gfx::Canvas* canvas) {
693  // We only need to paint the title-bar since no resizing border is shown.
694  // Instead, we allow part of the inner content area be used to trigger the
695  // mouse resizing.
696  int titlebar_height = TitlebarHeight();
697  const gfx::ImageSkia* image = GetFrameBackground(GetPaintState());
698  canvas->TileImageInt(*image, 0, 0, width(), titlebar_height);
699
700  if (is_frameless_)
701    return;
702
703  // Left border, below title-bar.
704  canvas->TileImageInt(*image, 0, titlebar_height, kNonAeroBorderThickness,
705      height() - titlebar_height);
706
707  // Right border, below title-bar.
708  canvas->TileImageInt(*image, width() - kNonAeroBorderThickness,
709      titlebar_height, kNonAeroBorderThickness, height() - titlebar_height);
710
711  // Bottom border.
712  canvas->TileImageInt(*image, 0, height() - kNonAeroBorderThickness, width(),
713      kNonAeroBorderThickness);
714}
715
716void PanelFrameView::PaintFrameEdge(gfx::Canvas* canvas) {
717#if defined(OS_WIN)
718  // Border is not needed when panel is not shown as minimized.
719  if (GetPaintState() != PAINT_AS_MINIMIZED)
720    return;
721
722  const gfx::ImageSkia& top_left_image = GetTopLeftCornerImage(corner_style_);
723  const gfx::ImageSkia& top_right_image = GetTopRightCornerImage(corner_style_);
724  const gfx::ImageSkia& bottom_left_image =
725      GetBottomLeftCornerImage(corner_style_);
726  const gfx::ImageSkia& bottom_right_image =
727      GetBottomRightCornerImage(corner_style_);
728  const gfx::ImageSkia& top_image = GetTopEdgeImage();
729  const gfx::ImageSkia& bottom_image = GetBottomEdgeImage();
730  const gfx::ImageSkia& left_image = GetLeftEdgeImage();
731  const gfx::ImageSkia& right_image = GetRightEdgeImage();
732
733  // Draw the top border.
734  canvas->DrawImageInt(top_left_image, 0, 0);
735  canvas->TileImageInt(top_image,
736                       top_left_image.width(),
737                       0,
738                       width() - top_right_image.width(),
739                       top_image.height());
740  canvas->DrawImageInt(top_right_image, width() - top_right_image.width(), 0);
741
742  // Draw the right border.
743  canvas->TileImageInt(right_image,
744                       width() - right_image.width(),
745                       top_right_image.height(),
746                       right_image.width(),
747                       height() - top_right_image.height() -
748                           bottom_right_image.height());
749
750  // Draw the bottom border.
751  canvas->DrawImageInt(bottom_right_image,
752                       width() - bottom_right_image.width(),
753                       height() - bottom_right_image.height());
754  canvas->TileImageInt(bottom_image,
755                       bottom_left_image.width(),
756                       height() - bottom_image.height(),
757                       width() - bottom_left_image.width() -
758                           bottom_right_image.width(),
759                       bottom_image.height());
760  canvas->DrawImageInt(bottom_left_image,
761                       0,
762                       height() - bottom_left_image.height());
763
764  // Draw the left border.
765  canvas->TileImageInt(left_image,
766                       0,
767                       top_left_image.height(),
768                       left_image.width(),
769                       height() - top_left_image.height() -
770                           bottom_left_image.height());
771#endif
772}
773