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/controls/focusable_border.h" 6 7 #include "ui/gfx/canvas.h" 8 #include "ui/gfx/insets.h" 9 #include "ui/gfx/skia_util.h" 10 #include "ui/native_theme/native_theme.h" 11 12 namespace { 13 14 // Define the size of the insets 15 const int kTopInsetSize = 4; 16 const int kLeftInsetSize = 4; 17 const int kBottomInsetSize = 4; 18 const int kRightInsetSize = 4; 19 20 } // namespace 21 22 namespace views { 23 24 FocusableBorder::FocusableBorder() 25 : insets_(kTopInsetSize, kLeftInsetSize, 26 kBottomInsetSize, kRightInsetSize), 27 override_color_(SK_ColorWHITE), 28 use_default_color_(true) { 29 } 30 31 void FocusableBorder::SetColor(SkColor color) { 32 override_color_ = color; 33 use_default_color_ = false; 34 } 35 36 void FocusableBorder::UseDefaultColor() { 37 use_default_color_ = true; 38 } 39 40 void FocusableBorder::Paint(const View& view, gfx::Canvas* canvas) { 41 SkPath path; 42 path.addRect(gfx::RectToSkRect(view.GetLocalBounds()), SkPath::kCW_Direction); 43 SkPaint paint; 44 paint.setStyle(SkPaint::kStroke_Style); 45 SkColor color = override_color_; 46 if (use_default_color_) { 47 color = view.GetNativeTheme()->GetSystemColor( 48 view.HasFocus() ? ui::NativeTheme::kColorId_FocusedBorderColor : 49 ui::NativeTheme::kColorId_UnfocusedBorderColor); 50 } 51 52 paint.setColor(color); 53 paint.setStrokeWidth(SkIntToScalar(2)); 54 55 canvas->DrawPath(path, paint); 56 } 57 58 gfx::Insets FocusableBorder::GetInsets() const { 59 return insets_; 60 } 61 62 gfx::Size FocusableBorder::GetMinimumSize() const { 63 return gfx::Size(); 64 } 65 66 void FocusableBorder::SetInsets(int top, int left, int bottom, int right) { 67 insets_.Set(top, left, bottom, right); 68 } 69 70 } // namespace views 71