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 : has_focus_(false), 26 insets_(kTopInsetSize, kLeftInsetSize, 27 kBottomInsetSize, kRightInsetSize), 28 override_color_(SK_ColorWHITE), 29 use_default_color_(true) { 30 } 31 32 void FocusableBorder::SetColor(SkColor color) { 33 override_color_ = color; 34 use_default_color_ = false; 35 } 36 37 void FocusableBorder::UseDefaultColor() { 38 use_default_color_ = true; 39 } 40 41 void FocusableBorder::Paint(const View& view, gfx::Canvas* canvas) { 42 SkPath path; 43 path.addRect(gfx::RectToSkRect(view.GetLocalBounds()), SkPath::kCW_Direction); 44 SkPaint paint; 45 paint.setStyle(SkPaint::kStroke_Style); 46 SkColor color = override_color_; 47 if (use_default_color_) { 48 color = view.GetNativeTheme()->GetSystemColor( 49 has_focus_ ? ui::NativeTheme::kColorId_FocusedBorderColor : 50 ui::NativeTheme::kColorId_UnfocusedBorderColor); 51 } 52 53 paint.setColor(color); 54 paint.setStrokeWidth(SkIntToScalar(2)); 55 56 canvas->DrawPath(path, paint); 57 } 58 59 gfx::Insets FocusableBorder::GetInsets() const { 60 return insets_; 61 } 62 63 void FocusableBorder::SetInsets(int top, int left, int bottom, int right) { 64 insets_.Set(top, left, bottom, right); 65 } 66 67 } // namespace views 68