Home | History | Annotate | Download | only in panels
      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 "ash/wm/panels/panel_frame_view.h"
      6 
      7 #include "ash/wm/frame_painter.h"
      8 #include "grit/ash_resources.h"
      9 #include "grit/ui_strings.h"  // Accessibility names
     10 #include "third_party/skia/include/core/SkPaint.h"
     11 #include "ui/base/animation/throb_animation.h"
     12 #include "ui/base/cursor/cursor.h"
     13 #include "ui/base/hit_test.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 #include "ui/base/resource/resource_bundle.h"
     16 #include "ui/gfx/canvas.h"
     17 #include "ui/gfx/font.h"
     18 #include "ui/gfx/image/image.h"
     19 #include "ui/views/controls/button/image_button.h"
     20 #include "ui/views/widget/native_widget_aura.h"
     21 #include "ui/views/widget/widget.h"
     22 #include "ui/views/widget/widget_delegate.h"
     23 
     24 namespace ash {
     25 
     26 // static
     27 const char PanelFrameView::kViewClassName[] = "ash/wm/panels/PanelFrameView";
     28 
     29 PanelFrameView::PanelFrameView(views::Widget* frame, FrameType frame_type)
     30     : frame_(frame),
     31       close_button_(NULL),
     32       minimize_button_(NULL),
     33       window_icon_(NULL),
     34       title_font_(gfx::Font(views::NativeWidgetAura::GetWindowTitleFont())) {
     35   if (frame_type != FRAME_NONE)
     36     InitFramePainter();
     37 }
     38 
     39 PanelFrameView::~PanelFrameView() {
     40 }
     41 
     42 const char* PanelFrameView::GetClassName() const {
     43   return kViewClassName;
     44 }
     45 
     46 void PanelFrameView::InitFramePainter() {
     47   frame_painter_.reset(new FramePainter);
     48 
     49   close_button_ = new views::ImageButton(this);
     50   close_button_->SetAccessibleName(
     51       l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE));
     52   AddChildView(close_button_);
     53 
     54   minimize_button_ = new views::ImageButton(this);
     55   minimize_button_->SetAccessibleName(
     56       l10n_util::GetStringUTF16(IDS_APP_ACCNAME_MINIMIZE));
     57   AddChildView(minimize_button_);
     58 
     59   if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
     60     window_icon_ = new views::ImageButton(this);
     61     AddChildView(window_icon_);
     62   }
     63 
     64   frame_painter_->Init(frame_, window_icon_, minimize_button_, close_button_,
     65                        FramePainter::SIZE_BUTTON_MINIMIZES);
     66 }
     67 
     68 gfx::Size PanelFrameView::GetMinimumSize() {
     69   if (!frame_painter_)
     70     return gfx::Size();
     71   return frame_painter_->GetMinimumSize(this);
     72 }
     73 
     74 void PanelFrameView::Layout() {
     75   if (!frame_painter_)
     76     return;
     77   frame_painter_->LayoutHeader(this, true);
     78 }
     79 
     80 void PanelFrameView::ResetWindowControls() {
     81   NOTIMPLEMENTED();
     82 }
     83 
     84 void PanelFrameView::UpdateWindowIcon() {
     85   if (!window_icon_)
     86     return;
     87   views::WidgetDelegate* delegate = frame_->widget_delegate();
     88   if (delegate) {
     89     gfx::ImageSkia image = delegate->GetWindowIcon();
     90     window_icon_->SetImage(views::CustomButton::STATE_NORMAL, &image);
     91   }
     92   window_icon_->SchedulePaint();
     93 }
     94 
     95 void PanelFrameView::UpdateWindowTitle() {
     96   if (!frame_painter_)
     97     return;
     98   frame_painter_->SchedulePaintForTitle(title_font_);
     99 }
    100 
    101 void PanelFrameView::GetWindowMask(const gfx::Size&, gfx::Path*) {
    102   // Nothing.
    103 }
    104 
    105 int PanelFrameView::NonClientHitTest(const gfx::Point& point) {
    106   if (!frame_painter_)
    107     return HTNOWHERE;
    108   return frame_painter_->NonClientHitTest(this, point);
    109 }
    110 
    111 void PanelFrameView::OnPaint(gfx::Canvas* canvas) {
    112   if (!frame_painter_)
    113     return;
    114   bool paint_as_active = ShouldPaintAsActive();
    115   int theme_frame_id = 0;
    116   if (frame_painter_->ShouldUseMinimalHeaderStyle(FramePainter::THEMED_NO))
    117     theme_frame_id = IDR_AURA_WINDOW_HEADER_BASE_MINIMAL;
    118   else if (paint_as_active)
    119     theme_frame_id = IDR_AURA_WINDOW_HEADER_BASE_ACTIVE;
    120   else
    121     theme_frame_id = IDR_AURA_WINDOW_HEADER_BASE_INACTIVE;
    122 
    123   frame_painter_->PaintHeader(
    124       this,
    125       canvas,
    126       paint_as_active ? FramePainter::ACTIVE : FramePainter::INACTIVE,
    127       theme_frame_id,
    128       0);
    129   frame_painter_->PaintTitleBar(this, canvas, title_font_);
    130   frame_painter_->PaintHeaderContentSeparator(this, canvas);
    131 }
    132 
    133 gfx::Rect PanelFrameView::GetBoundsForClientView() const {
    134   if (!frame_painter_)
    135     return bounds();
    136   return frame_painter_->GetBoundsForClientView(
    137       close_button_->bounds().bottom(),
    138       bounds());
    139 }
    140 
    141 gfx::Rect PanelFrameView::GetWindowBoundsForClientBounds(
    142     const gfx::Rect& client_bounds) const {
    143   if (!frame_painter_)
    144     return client_bounds;
    145   return frame_painter_->GetWindowBoundsForClientBounds(
    146       close_button_->bounds().bottom(), client_bounds);
    147 }
    148 
    149 void PanelFrameView::ButtonPressed(views::Button* sender,
    150                                    const ui::Event& event) {
    151   if (sender == close_button_)
    152     GetWidget()->Close();
    153   if (sender == minimize_button_)
    154     GetWidget()->Minimize();
    155 }
    156 
    157 }  // namespace ash
    158