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/caption_buttons/frame_caption_button_container_view.h"
      8 #include "ash/wm/frame_border_hit_test_controller.h"
      9 #include "ash/wm/header_painter.h"
     10 #include "grit/ash_resources.h"
     11 #include "ui/base/hit_test.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 #include "ui/gfx/canvas.h"
     14 #include "ui/gfx/font.h"
     15 #include "ui/views/controls/image_view.h"
     16 #include "ui/views/widget/native_widget_aura.h"
     17 #include "ui/views/widget/widget.h"
     18 #include "ui/views/widget/widget_delegate.h"
     19 
     20 namespace ash {
     21 
     22 // static
     23 const char PanelFrameView::kViewClassName[] = "PanelFrameView";
     24 
     25 PanelFrameView::PanelFrameView(views::Widget* frame, FrameType frame_type)
     26     : frame_(frame),
     27       caption_button_container_(NULL),
     28       window_icon_(NULL),
     29       title_font_(gfx::Font(views::NativeWidgetAura::GetWindowTitleFont())),
     30       frame_border_hit_test_controller_(
     31           new FrameBorderHitTestController(frame_)) {
     32   DCHECK(!frame_->widget_delegate()->CanMaximize());
     33   if (frame_type != FRAME_NONE)
     34     InitHeaderPainter();
     35 }
     36 
     37 PanelFrameView::~PanelFrameView() {
     38 }
     39 
     40 const char* PanelFrameView::GetClassName() const {
     41   return kViewClassName;
     42 }
     43 
     44 void PanelFrameView::InitHeaderPainter() {
     45   header_painter_.reset(new HeaderPainter);
     46 
     47   caption_button_container_ = new FrameCaptionButtonContainerView(frame_,
     48       FrameCaptionButtonContainerView::MINIMIZE_ALLOWED);
     49   AddChildView(caption_button_container_);
     50 
     51   if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
     52     window_icon_ = new views::ImageView();
     53     AddChildView(window_icon_);
     54   }
     55 
     56   header_painter_->Init(frame_, this, window_icon_, caption_button_container_);
     57 }
     58 
     59 int PanelFrameView::NonClientTopBorderHeight() const {
     60   if (!header_painter_)
     61     return 0;
     62   // Reserve enough space to see the buttons and the separator line.
     63   return caption_button_container_->bounds().bottom() +
     64       header_painter_->HeaderContentSeparatorSize();
     65 }
     66 
     67 gfx::Size PanelFrameView::GetMinimumSize() {
     68   if (!header_painter_)
     69     return gfx::Size();
     70   gfx::Size min_client_view_size(frame_->client_view()->GetMinimumSize());
     71   return gfx::Size(
     72       std::max(header_painter_->GetMinimumHeaderWidth(),
     73                min_client_view_size.width()),
     74       NonClientTopBorderHeight() + min_client_view_size.height());
     75 }
     76 
     77 void PanelFrameView::Layout() {
     78   if (!header_painter_)
     79     return;
     80   header_painter_->LayoutHeader(true);
     81   header_painter_->set_header_height(NonClientTopBorderHeight());
     82 }
     83 
     84 void PanelFrameView::GetWindowMask(const gfx::Size&, gfx::Path*) {
     85   // Nothing.
     86 }
     87 
     88 void PanelFrameView::ResetWindowControls() {
     89   NOTIMPLEMENTED();
     90 }
     91 
     92 void PanelFrameView::UpdateWindowIcon() {
     93   if (!window_icon_)
     94     return;
     95   views::WidgetDelegate* delegate = frame_->widget_delegate();
     96   if (delegate)
     97     window_icon_->SetImage(delegate->GetWindowIcon());
     98   window_icon_->SchedulePaint();
     99 }
    100 
    101 void PanelFrameView::UpdateWindowTitle() {
    102   if (!header_painter_)
    103     return;
    104   header_painter_->SchedulePaintForTitle(title_font_);
    105 }
    106 
    107 int PanelFrameView::NonClientHitTest(const gfx::Point& point) {
    108   if (!header_painter_)
    109     return HTNOWHERE;
    110   return FrameBorderHitTestController::NonClientHitTest(this,
    111       header_painter_.get(), point);
    112 }
    113 
    114 void PanelFrameView::OnPaint(gfx::Canvas* canvas) {
    115   if (!header_painter_)
    116     return;
    117   bool paint_as_active = ShouldPaintAsActive();
    118   int theme_frame_id = 0;
    119   if (paint_as_active)
    120     theme_frame_id = IDR_AURA_WINDOW_HEADER_BASE_ACTIVE;
    121   else
    122     theme_frame_id = IDR_AURA_WINDOW_HEADER_BASE_INACTIVE;
    123 
    124   header_painter_->PaintHeader(
    125       canvas,
    126       paint_as_active ? HeaderPainter::ACTIVE : HeaderPainter::INACTIVE,
    127       theme_frame_id,
    128       0);
    129   header_painter_->PaintTitleBar(canvas, title_font_);
    130   header_painter_->PaintHeaderContentSeparator(canvas);
    131 }
    132 
    133 gfx::Rect PanelFrameView::GetBoundsForClientView() const {
    134   if (!header_painter_)
    135     return bounds();
    136   return HeaderPainter::GetBoundsForClientView(
    137       NonClientTopBorderHeight(), bounds());
    138 }
    139 
    140 gfx::Rect PanelFrameView::GetWindowBoundsForClientBounds(
    141     const gfx::Rect& client_bounds) const {
    142   if (!header_painter_)
    143     return client_bounds;
    144   return HeaderPainter::GetWindowBoundsForClientBounds(
    145       NonClientTopBorderHeight(), client_bounds);
    146 }
    147 
    148 }  // namespace ash
    149