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 "ui/views/window/custom_frame_view.h"
6
7#include <algorithm>
8#include <vector>
9
10#include "base/strings/utf_string_conversions.h"
11#include "ui/base/hit_test.h"
12#include "ui/base/l10n/l10n_util.h"
13#include "ui/base/resource/resource_bundle.h"
14#include "ui/gfx/canvas.h"
15#include "ui/gfx/font.h"
16#include "ui/gfx/image/image.h"
17#include "ui/gfx/path.h"
18#include "ui/gfx/rect.h"
19#include "ui/resources/grit/ui_resources.h"
20#include "ui/strings/grit/ui_strings.h"
21#include "ui/views/color_constants.h"
22#include "ui/views/controls/button/image_button.h"
23#include "ui/views/views_delegate.h"
24#include "ui/views/widget/native_widget_private.h"
25#include "ui/views/widget/widget.h"
26#include "ui/views/widget/widget_delegate.h"
27#include "ui/views/window/client_view.h"
28#include "ui/views/window/frame_background.h"
29#include "ui/views/window/window_button_order_provider.h"
30#include "ui/views/window/window_resources.h"
31#include "ui/views/window/window_shape.h"
32
33namespace views {
34
35namespace {
36
37// The frame border is only visible in restored mode and is hardcoded to 4 px on
38// each side regardless of the system window border size.
39const int kFrameBorderThickness = 4;
40// In the window corners, the resize areas don't actually expand bigger, but the
41// 16 px at the end of each edge triggers diagonal resizing.
42const int kResizeAreaCornerSize = 16;
43// The titlebar never shrinks too short to show the caption button plus some
44// padding below it.
45const int kCaptionButtonHeightWithPadding = 19;
46// The titlebar has a 2 px 3D edge along the top and bottom.
47const int kTitlebarTopAndBottomEdgeThickness = 2;
48// The icon is inset 2 px from the left frame border.
49const int kIconLeftSpacing = 2;
50// The icon never shrinks below 16 px on a side.
51const int kIconMinimumSize = 16;
52// The space between the window icon and the title text.
53const int kTitleIconOffsetX = 4;
54// The space between the title text and the caption buttons.
55const int kTitleCaptionSpacing = 5;
56
57#if defined(OS_CHROMEOS)
58// Chrome OS uses a dark gray.
59const SkColor kDefaultColorFrame = SkColorSetRGB(109, 109, 109);
60const SkColor kDefaultColorFrameInactive = SkColorSetRGB(176, 176, 176);
61#else
62// Windows and Linux use a blue.
63const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
64const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
65#endif
66
67const gfx::FontList& GetTitleFontList() {
68  static const gfx::FontList title_font_list =
69      internal::NativeWidgetPrivate::GetWindowTitleFontList();
70  return title_font_list;
71}
72
73void LayoutButton(ImageButton* button, const gfx::Rect& bounds) {
74  button->SetVisible(true);
75  button->SetImageAlignment(ImageButton::ALIGN_LEFT,
76                            ImageButton::ALIGN_BOTTOM);
77  button->SetBoundsRect(bounds);
78}
79
80}  // namespace
81
82///////////////////////////////////////////////////////////////////////////////
83// CustomFrameView, public:
84
85CustomFrameView::CustomFrameView()
86    : frame_(NULL),
87      window_icon_(NULL),
88      minimize_button_(NULL),
89      maximize_button_(NULL),
90      restore_button_(NULL),
91      close_button_(NULL),
92      frame_background_(new FrameBackground()),
93      minimum_title_bar_x_(0),
94      maximum_title_bar_x_(-1) {
95}
96
97CustomFrameView::~CustomFrameView() {
98}
99
100void CustomFrameView::Init(Widget* frame) {
101  frame_ = frame;
102
103  close_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_CLOSE,
104      IDR_CLOSE, IDR_CLOSE_H, IDR_CLOSE_P);
105  minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE,
106      IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
107  maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE,
108      IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
109  restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE,
110      IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
111
112  if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
113    window_icon_ = new ImageButton(this);
114    AddChildView(window_icon_);
115  }
116}
117
118///////////////////////////////////////////////////////////////////////////////
119// CustomFrameView, NonClientFrameView implementation:
120
121gfx::Rect CustomFrameView::GetBoundsForClientView() const {
122  return client_view_bounds_;
123}
124
125gfx::Rect CustomFrameView::GetWindowBoundsForClientBounds(
126    const gfx::Rect& client_bounds) const {
127  int top_height = NonClientTopBorderHeight();
128  int border_thickness = NonClientBorderThickness();
129  return gfx::Rect(client_bounds.x() - border_thickness,
130                   client_bounds.y() - top_height,
131                   client_bounds.width() + (2 * border_thickness),
132                   client_bounds.height() + top_height + border_thickness);
133}
134
135int CustomFrameView::NonClientHitTest(const gfx::Point& point) {
136  // Sanity check.
137  if (!bounds().Contains(point))
138    return HTNOWHERE;
139
140  int frame_component = frame_->client_view()->NonClientHitTest(point);
141
142  // See if we're in the sysmenu region.  (We check the ClientView first to be
143  // consistent with OpaqueBrowserFrameView; it's not really necessary here.)
144  gfx::Rect sysmenu_rect(IconBounds());
145  // In maximized mode we extend the rect to the screen corner to take advantage
146  // of Fitts' Law.
147  if (frame_->IsMaximized())
148    sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
149  sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
150  if (sysmenu_rect.Contains(point))
151    return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
152
153  if (frame_component != HTNOWHERE)
154    return frame_component;
155
156  // Then see if the point is within any of the window controls.
157  if (close_button_->GetMirroredBounds().Contains(point))
158    return HTCLOSE;
159  if (restore_button_->GetMirroredBounds().Contains(point))
160    return HTMAXBUTTON;
161  if (maximize_button_->GetMirroredBounds().Contains(point))
162    return HTMAXBUTTON;
163  if (minimize_button_->GetMirroredBounds().Contains(point))
164    return HTMINBUTTON;
165  if (window_icon_ && window_icon_->GetMirroredBounds().Contains(point))
166    return HTSYSMENU;
167
168  int window_component = GetHTComponentForFrame(point, FrameBorderThickness(),
169      NonClientBorderThickness(), kResizeAreaCornerSize, kResizeAreaCornerSize,
170      frame_->widget_delegate()->CanResize());
171  // Fall back to the caption if no other component matches.
172  return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
173}
174
175void CustomFrameView::GetWindowMask(const gfx::Size& size,
176                                    gfx::Path* window_mask) {
177  DCHECK(window_mask);
178  if (frame_->IsMaximized() || !ShouldShowTitleBarAndBorder())
179    return;
180
181  GetDefaultWindowMask(size, window_mask);
182}
183
184void CustomFrameView::ResetWindowControls() {
185  restore_button_->SetState(CustomButton::STATE_NORMAL);
186  minimize_button_->SetState(CustomButton::STATE_NORMAL);
187  maximize_button_->SetState(CustomButton::STATE_NORMAL);
188  // The close button isn't affected by this constraint.
189}
190
191void CustomFrameView::UpdateWindowIcon() {
192  if (window_icon_)
193    window_icon_->SchedulePaint();
194}
195
196void CustomFrameView::UpdateWindowTitle() {
197  if (frame_->widget_delegate()->ShouldShowWindowTitle())
198    SchedulePaintInRect(title_bounds_);
199}
200
201void CustomFrameView::SizeConstraintsChanged() {
202  ResetWindowControls();
203  LayoutWindowControls();
204}
205
206///////////////////////////////////////////////////////////////////////////////
207// CustomFrameView, View overrides:
208
209void CustomFrameView::OnPaint(gfx::Canvas* canvas) {
210  if (!ShouldShowTitleBarAndBorder())
211    return;
212
213  if (frame_->IsMaximized())
214    PaintMaximizedFrameBorder(canvas);
215  else
216    PaintRestoredFrameBorder(canvas);
217  PaintTitleBar(canvas);
218  if (ShouldShowClientEdge())
219    PaintRestoredClientEdge(canvas);
220}
221
222void CustomFrameView::Layout() {
223  if (ShouldShowTitleBarAndBorder()) {
224    LayoutWindowControls();
225    LayoutTitleBar();
226  }
227
228  LayoutClientView();
229}
230
231gfx::Size CustomFrameView::GetPreferredSize() const {
232  return frame_->non_client_view()->GetWindowBoundsForClientBounds(
233      gfx::Rect(frame_->client_view()->GetPreferredSize())).size();
234}
235
236gfx::Size CustomFrameView::GetMinimumSize() const {
237  return frame_->non_client_view()->GetWindowBoundsForClientBounds(
238      gfx::Rect(frame_->client_view()->GetMinimumSize())).size();
239}
240
241gfx::Size CustomFrameView::GetMaximumSize() const {
242  gfx::Size max_size = frame_->client_view()->GetMaximumSize();
243  gfx::Size converted_size =
244      frame_->non_client_view()->GetWindowBoundsForClientBounds(
245          gfx::Rect(max_size)).size();
246  return gfx::Size(max_size.width() == 0 ? 0 : converted_size.width(),
247                   max_size.height() == 0 ? 0 : converted_size.height());
248}
249
250///////////////////////////////////////////////////////////////////////////////
251// CustomFrameView, ButtonListener implementation:
252
253void CustomFrameView::ButtonPressed(Button* sender, const ui::Event& event) {
254  if (sender == close_button_)
255    frame_->Close();
256  else if (sender == minimize_button_)
257    frame_->Minimize();
258  else if (sender == maximize_button_)
259    frame_->Maximize();
260  else if (sender == restore_button_)
261    frame_->Restore();
262}
263
264///////////////////////////////////////////////////////////////////////////////
265// CustomFrameView, private:
266
267int CustomFrameView::FrameBorderThickness() const {
268  return frame_->IsMaximized() ? 0 : kFrameBorderThickness;
269}
270
271int CustomFrameView::NonClientBorderThickness() const {
272  // In maximized mode, we don't show a client edge.
273  return FrameBorderThickness() +
274      (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
275}
276
277int CustomFrameView::NonClientTopBorderHeight() const {
278  return std::max(FrameBorderThickness() + IconSize(),
279                  CaptionButtonY() + kCaptionButtonHeightWithPadding) +
280      TitlebarBottomThickness();
281}
282
283int CustomFrameView::CaptionButtonY() const {
284  // Maximized buttons start at window top so that even if their images aren't
285  // drawn flush with the screen edge, they still obey Fitts' Law.
286#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
287  return FrameBorderThickness();
288#else
289  return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness;
290#endif
291}
292
293int CustomFrameView::TitlebarBottomThickness() const {
294  return kTitlebarTopAndBottomEdgeThickness +
295      (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
296}
297
298int CustomFrameView::IconSize() const {
299#if defined(OS_WIN)
300  // This metric scales up if either the titlebar height or the titlebar font
301  // size are increased.
302  return GetSystemMetrics(SM_CYSMICON);
303#else
304  return std::max(GetTitleFontList().GetHeight(), kIconMinimumSize);
305#endif
306}
307
308gfx::Rect CustomFrameView::IconBounds() const {
309  int size = IconSize();
310  int frame_thickness = FrameBorderThickness();
311  // Our frame border has a different "3D look" than Windows'.  Theirs has a
312  // more complex gradient on the top that they push their icon/title below;
313  // then the maximized window cuts this off and the icon/title are centered
314  // in the remaining space.  Because the apparent shape of our border is
315  // simpler, using the same positioning makes things look slightly uncentered
316  // with restored windows, so when the window is restored, instead of
317  // calculating the remaining space from below the frame border, we calculate
318  // from below the 3D edge.
319  int unavailable_px_at_top = frame_->IsMaximized() ?
320      frame_thickness : kTitlebarTopAndBottomEdgeThickness;
321  // When the icon is shorter than the minimum space we reserve for the caption
322  // button, we vertically center it.  We want to bias rounding to put extra
323  // space above the icon, since the 3D edge (+ client edge, for restored
324  // windows) below looks (to the eye) more like additional space than does the
325  // 3D edge (or nothing at all, for maximized windows) above; hence the +1.
326  int y = unavailable_px_at_top + (NonClientTopBorderHeight() -
327      unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2;
328  return gfx::Rect(frame_thickness + kIconLeftSpacing + minimum_title_bar_x_,
329                   y, size, size);
330}
331
332bool CustomFrameView::ShouldShowTitleBarAndBorder() const {
333  if (frame_->IsFullscreen())
334    return false;
335
336  if (ViewsDelegate::views_delegate) {
337    return !ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar(
338                frame_->IsMaximized());
339  }
340
341  return true;
342}
343
344bool CustomFrameView::ShouldShowClientEdge() const {
345  return !frame_->IsMaximized() && ShouldShowTitleBarAndBorder();
346}
347
348void CustomFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
349  frame_background_->set_frame_color(GetFrameColor());
350  const gfx::ImageSkia* frame_image = GetFrameImage();
351  frame_background_->set_theme_image(frame_image);
352  frame_background_->set_top_area_height(frame_image->height());
353
354  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
355
356  frame_background_->SetCornerImages(
357      rb.GetImageNamed(IDR_WINDOW_TOP_LEFT_CORNER).ToImageSkia(),
358      rb.GetImageNamed(IDR_WINDOW_TOP_RIGHT_CORNER).ToImageSkia(),
359      rb.GetImageNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER).ToImageSkia(),
360      rb.GetImageNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER).ToImageSkia());
361  frame_background_->SetSideImages(
362      rb.GetImageNamed(IDR_WINDOW_LEFT_SIDE).ToImageSkia(),
363      rb.GetImageNamed(IDR_WINDOW_TOP_CENTER).ToImageSkia(),
364      rb.GetImageNamed(IDR_WINDOW_RIGHT_SIDE).ToImageSkia(),
365      rb.GetImageNamed(IDR_WINDOW_BOTTOM_CENTER).ToImageSkia());
366
367  frame_background_->PaintRestored(canvas, this);
368}
369
370void CustomFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
371  const gfx::ImageSkia* frame_image = GetFrameImage();
372  frame_background_->set_theme_image(frame_image);
373  frame_background_->set_top_area_height(frame_image->height());
374  frame_background_->PaintMaximized(canvas, this);
375
376  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
377
378  // TODO(jamescook): Migrate this into FrameBackground.
379  // The bottom of the titlebar actually comes from the top of the Client Edge
380  // graphic, with the actual client edge clipped off the bottom.
381  const gfx::ImageSkia* titlebar_bottom = rb.GetImageNamed(
382      IDR_APP_TOP_CENTER).ToImageSkia();
383  int edge_height = titlebar_bottom->height() -
384      (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
385  canvas->TileImageInt(*titlebar_bottom, 0,
386      frame_->client_view()->y() - edge_height, width(), edge_height);
387}
388
389void CustomFrameView::PaintTitleBar(gfx::Canvas* canvas) {
390  WidgetDelegate* delegate = frame_->widget_delegate();
391
392  // It seems like in some conditions we can be asked to paint after the window
393  // that contains us is WM_DESTROYed. At this point, our delegate is NULL. The
394  // correct long term fix may be to shut down the RootView in WM_DESTROY.
395  if (!delegate || !delegate->ShouldShowWindowTitle())
396    return;
397
398  gfx::Rect rect = title_bounds_;
399  rect.set_x(GetMirroredXForRect(title_bounds_));
400  canvas->DrawStringRect(delegate->GetWindowTitle(), GetTitleFontList(),
401                         SK_ColorWHITE, rect);
402}
403
404void CustomFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
405  gfx::Rect client_area_bounds = frame_->client_view()->bounds();
406  // The shadows have a 1 pixel gap on the inside, so draw them 1 pixel inwards.
407  gfx::Rect shadowed_area_bounds = client_area_bounds;
408  shadowed_area_bounds.Inset(gfx::Insets(1, 1, 1, 1));
409  int shadowed_area_top = shadowed_area_bounds.y();
410
411  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
412
413  // Top: left, center, right sides.
414  const gfx::ImageSkia* top_left = rb.GetImageSkiaNamed(IDR_APP_TOP_LEFT);
415  const gfx::ImageSkia* top_center = rb.GetImageSkiaNamed(IDR_APP_TOP_CENTER);
416  const gfx::ImageSkia* top_right = rb.GetImageSkiaNamed(IDR_APP_TOP_RIGHT);
417  int top_edge_y = shadowed_area_top - top_center->height();
418  canvas->DrawImageInt(*top_left,
419                       shadowed_area_bounds.x() - top_left->width(),
420                       top_edge_y);
421  canvas->TileImageInt(*top_center,
422                       shadowed_area_bounds.x(),
423                       top_edge_y,
424                       shadowed_area_bounds.width(),
425                       top_center->height());
426  canvas->DrawImageInt(*top_right, shadowed_area_bounds.right(), top_edge_y);
427
428  // Right side.
429  const gfx::ImageSkia* right = rb.GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
430  int shadowed_area_bottom =
431      std::max(shadowed_area_top, shadowed_area_bounds.bottom());
432  int shadowed_area_height = shadowed_area_bottom - shadowed_area_top;
433  canvas->TileImageInt(*right,
434                       shadowed_area_bounds.right(),
435                       shadowed_area_top,
436                       right->width(),
437                       shadowed_area_height);
438
439  // Bottom: left, center, right sides.
440  const gfx::ImageSkia* bottom_left =
441      rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
442  const gfx::ImageSkia* bottom_center =
443      rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
444  const gfx::ImageSkia* bottom_right =
445      rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER);
446
447  canvas->DrawImageInt(*bottom_left,
448                       shadowed_area_bounds.x() - bottom_left->width(),
449                       shadowed_area_bottom);
450
451  canvas->TileImageInt(*bottom_center,
452                       shadowed_area_bounds.x(),
453                       shadowed_area_bottom,
454                       shadowed_area_bounds.width(),
455                       bottom_right->height());
456
457  canvas->DrawImageInt(*bottom_right,
458                       shadowed_area_bounds.right(),
459                       shadowed_area_bottom);
460  // Left side.
461  const gfx::ImageSkia* left = rb.GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE);
462  canvas->TileImageInt(*left,
463                       shadowed_area_bounds.x() - left->width(),
464                       shadowed_area_top,
465                       left->width(),
466                       shadowed_area_height);
467}
468
469SkColor CustomFrameView::GetFrameColor() const {
470  return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive;
471}
472
473const gfx::ImageSkia* CustomFrameView::GetFrameImage() const {
474  return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
475      frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia();
476}
477
478void CustomFrameView::LayoutWindowControls() {
479  minimum_title_bar_x_ = 0;
480  maximum_title_bar_x_ = width();
481
482  if (bounds().IsEmpty())
483    return;
484
485  int caption_y = CaptionButtonY();
486  bool is_maximized = frame_->IsMaximized();
487  // There should always be the same number of non-shadow pixels visible to the
488  // side of the caption buttons.  In maximized mode we extend the edge button
489  // to the screen corner to obey Fitts' Law.
490  int extra_width = is_maximized ?
491      (kFrameBorderThickness - kFrameShadowThickness) : 0;
492  int next_button_x = FrameBorderThickness();
493
494  bool is_restored = !is_maximized && !frame_->IsMinimized();
495  ImageButton* invisible_button = is_restored ? restore_button_
496                                              : maximize_button_;
497  invisible_button->SetVisible(false);
498
499  WindowButtonOrderProvider* button_order =
500      WindowButtonOrderProvider::GetInstance();
501  const std::vector<views::FrameButton>& leading_buttons =
502      button_order->leading_buttons();
503  const std::vector<views::FrameButton>& trailing_buttons =
504      button_order->trailing_buttons();
505
506  ImageButton* button = NULL;
507  for (std::vector<views::FrameButton>::const_iterator it =
508           leading_buttons.begin(); it != leading_buttons.end(); ++it) {
509    button = GetImageButton(*it);
510    if (!button)
511      continue;
512    gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
513                            button->GetPreferredSize());
514    if (it == leading_buttons.begin())
515      target_bounds.set_width(target_bounds.width() + extra_width);
516    LayoutButton(button, target_bounds);
517    next_button_x += button->width();
518    minimum_title_bar_x_ = std::min(width(), next_button_x);
519  }
520
521  // Trailing buttions are laid out in a RTL fashion
522  next_button_x = width() - FrameBorderThickness();
523  for (std::vector<views::FrameButton>::const_reverse_iterator it =
524           trailing_buttons.rbegin(); it != trailing_buttons.rend(); ++it) {
525    button = GetImageButton(*it);
526    if (!button)
527      continue;
528    gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
529                            button->GetPreferredSize());
530    if (it == trailing_buttons.rbegin())
531      target_bounds.set_width(target_bounds.width() + extra_width);
532    target_bounds.Offset(-target_bounds.width(), 0);
533    LayoutButton(button, target_bounds);
534    next_button_x = button->x();
535    maximum_title_bar_x_ = std::max(minimum_title_bar_x_, next_button_x);
536  }
537}
538
539void CustomFrameView::LayoutTitleBar() {
540  DCHECK_GE(maximum_title_bar_x_, 0);
541  // The window title position is calculated based on the icon position, even
542  // when there is no icon.
543  gfx::Rect icon_bounds(IconBounds());
544  bool show_window_icon = window_icon_ != NULL;
545  if (show_window_icon)
546    window_icon_->SetBoundsRect(icon_bounds);
547
548  if (!frame_->widget_delegate()->ShouldShowWindowTitle())
549    return;
550
551  // The offset between the window left edge and the title text.
552  int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
553                                 : icon_bounds.x();
554  int title_height = GetTitleFontList().GetHeight();
555  // We bias the title position so that when the difference between the icon and
556  // title heights is odd, the extra pixel of the title is above the vertical
557  // midline rather than below.  This compensates for how the icon is already
558  // biased downwards (see IconBounds()) and helps prevent descenders on the
559  // title from overlapping the 3D edge at the bottom of the titlebar.
560  title_bounds_.SetRect(title_x,
561      icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
562      std::max(0, maximum_title_bar_x_ - kTitleCaptionSpacing -
563      title_x), title_height);
564}
565
566void CustomFrameView::LayoutClientView() {
567  if (!ShouldShowTitleBarAndBorder()) {
568    client_view_bounds_ = bounds();
569    return;
570  }
571
572  int top_height = NonClientTopBorderHeight();
573  int border_thickness = NonClientBorderThickness();
574  client_view_bounds_.SetRect(border_thickness, top_height,
575      std::max(0, width() - (2 * border_thickness)),
576      std::max(0, height() - top_height - border_thickness));
577}
578
579ImageButton* CustomFrameView::InitWindowCaptionButton(
580    int accessibility_string_id,
581    int normal_image_id,
582    int hot_image_id,
583    int pushed_image_id) {
584  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
585  ImageButton* button = new ImageButton(this);
586  button->SetAccessibleName(l10n_util::GetStringUTF16(accessibility_string_id));
587  button->SetImage(CustomButton::STATE_NORMAL,
588                   rb.GetImageNamed(normal_image_id).ToImageSkia());
589  button->SetImage(CustomButton::STATE_HOVERED,
590                   rb.GetImageNamed(hot_image_id).ToImageSkia());
591  button->SetImage(CustomButton::STATE_PRESSED,
592                   rb.GetImageNamed(pushed_image_id).ToImageSkia());
593  AddChildView(button);
594  return button;
595}
596
597ImageButton* CustomFrameView::GetImageButton(views::FrameButton frame_button) {
598  ImageButton* button = NULL;
599  switch (frame_button) {
600    case views::FRAME_BUTTON_MINIMIZE: {
601      button = minimize_button_;
602      // If we should not show the minimize button, then we return NULL as we
603      // don't want this button to become visible and to be laid out.
604      bool should_show = frame_->widget_delegate()->CanMinimize();
605      button->SetVisible(should_show);
606      if (!should_show)
607        return NULL;
608
609      break;
610    }
611    case views::FRAME_BUTTON_MAXIMIZE: {
612      bool is_restored = !frame_->IsMaximized() && !frame_->IsMinimized();
613      button = is_restored ? maximize_button_ : restore_button_;
614      // If we should not show the maximize/restore button, then we return
615      // NULL as we don't want this button to become visible and to be laid
616      // out.
617      bool should_show = frame_->widget_delegate()->CanMaximize();
618      button->SetVisible(should_show);
619      if (!should_show)
620        return NULL;
621
622      break;
623    }
624    case views::FRAME_BUTTON_CLOSE: {
625      button = close_button_;
626      break;
627    }
628  }
629  return button;
630}
631
632}  // namespace views
633