1 // Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be found
3 // in the LICENSE file.
4
5 #ifndef CEF_LIBCEF_BROWSER_VIEWS_LABEL_BUTTON_IMPL_H_
6 #define CEF_LIBCEF_BROWSER_VIEWS_LABEL_BUTTON_IMPL_H_
7 #pragma once
8
9 #include "include/views/cef_button.h"
10 #include "include/views/cef_label_button.h"
11 #include "include/views/cef_menu_button.h"
12
13 #include "libcef/browser/image_impl.h"
14 #include "libcef/browser/views/button_impl.h"
15
16 #include "base/logging.h"
17 #include "ui/views/controls/button/label_button.h"
18
19 // Helpers for template boiler-plate.
20 #define CEF_LABEL_BUTTON_IMPL_T CEF_BUTTON_IMPL_T
21 #define CEF_LABEL_BUTTON_IMPL_A CEF_BUTTON_IMPL_A
22 #define CEF_LABEL_BUTTON_IMPL_D CefLabelButtonImpl<CEF_LABEL_BUTTON_IMPL_A>
23
24 // Template for implementing CefLabelButton-derived classes. See comments in
25 // view_impl.h for a usage overview.
26 CEF_LABEL_BUTTON_IMPL_T class CefLabelButtonImpl : public CEF_BUTTON_IMPL_D {
27 public:
28 using ParentClass = CEF_BUTTON_IMPL_D;
29
30 // CefLabelButton methods. When adding new As*() methods make sure to update
31 // CefViewAdapter::GetFor() in view_adapter.cc.
32 void SetText(const CefString& text) override;
33 CefString GetText() override;
34 void SetImage(cef_button_state_t button_state,
35 CefRefPtr<CefImage> image) override;
36 CefRefPtr<CefImage> GetImage(cef_button_state_t button_state) override;
37 void SetTextColor(cef_button_state_t for_state, cef_color_t color) override;
38 void SetEnabledTextColors(cef_color_t color) override;
39 void SetFontList(const CefString& font_list) override;
40 void SetHorizontalAlignment(cef_horizontal_alignment_t alignment) override;
41 void SetMinimumSize(const CefSize& size) override;
42 void SetMaximumSize(const CefSize& size) override;
43
44 // CefLabelButton methods:
AsMenuButton()45 CefRefPtr<CefMenuButton> AsMenuButton() override { return nullptr; }
46
47 // CefButton methods:
AsLabelButton()48 CefRefPtr<CefLabelButton> AsLabelButton() override { return this; }
49
50 // CefViewAdapter methods:
GetDebugInfo(base::DictionaryValue * info,bool include_children)51 void GetDebugInfo(base::DictionaryValue* info,
52 bool include_children) override {
53 ParentClass::GetDebugInfo(info, include_children);
54 info->SetString("text", ParentClass::root_view()->GetText());
55 }
56
57 protected:
58 // Create a new implementation object.
59 // Always call Initialize() after creation.
60 // |delegate| may be nullptr.
CefLabelButtonImpl(CefRefPtr<CefViewDelegateClass> delegate)61 explicit CefLabelButtonImpl(CefRefPtr<CefViewDelegateClass> delegate)
62 : ParentClass(delegate) {}
63 };
64
SetText(const CefString & text)65 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetText(
66 const CefString& text) {
67 CEF_REQUIRE_VALID_RETURN_VOID();
68 ParentClass::root_view()->SetText(text);
69 }
70
GetText()71 CEF_LABEL_BUTTON_IMPL_T CefString CEF_LABEL_BUTTON_IMPL_D::GetText() {
72 CEF_REQUIRE_VALID_RETURN(CefString());
73 return ParentClass::root_view()->GetText();
74 }
75
SetImage(cef_button_state_t button_state,CefRefPtr<CefImage> image)76 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetImage(
77 cef_button_state_t button_state,
78 CefRefPtr<CefImage> image) {
79 CEF_REQUIRE_VALID_RETURN_VOID();
80 gfx::ImageSkia image_skia;
81 if (image)
82 image_skia = static_cast<CefImageImpl*>(image.get())->AsImageSkia();
83 ParentClass::root_view()->SetImage(
84 static_cast<views::Button::ButtonState>(button_state), image_skia);
85 }
86
GetImage(cef_button_state_t button_state)87 CEF_LABEL_BUTTON_IMPL_T CefRefPtr<CefImage> CEF_LABEL_BUTTON_IMPL_D::GetImage(
88 cef_button_state_t button_state) {
89 CEF_REQUIRE_VALID_RETURN(nullptr);
90 const gfx::ImageSkia& image_skia = ParentClass::root_view()->GetImage(
91 static_cast<views::Button::ButtonState>(button_state));
92 if (image_skia.isNull())
93 return nullptr;
94 return new CefImageImpl(image_skia);
95 }
96
SetTextColor(cef_button_state_t for_state,cef_color_t color)97 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetTextColor(
98 cef_button_state_t for_state,
99 cef_color_t color) {
100 CEF_REQUIRE_VALID_RETURN_VOID();
101 ParentClass::root_view()->SetTextColor(
102 static_cast<views::Button::ButtonState>(for_state), color);
103 }
104
SetEnabledTextColors(cef_color_t color)105 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetEnabledTextColors(
106 cef_color_t color) {
107 CEF_REQUIRE_VALID_RETURN_VOID();
108 ParentClass::root_view()->SetEnabledTextColors(color);
109 }
110
SetFontList(const CefString & font_list)111 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetFontList(
112 const CefString& font_list) {
113 CEF_REQUIRE_VALID_RETURN_VOID();
114 ParentClass::root_view()->SetFontList(gfx::FontList(font_list));
115 }
116
SetHorizontalAlignment(cef_horizontal_alignment_t alignment)117 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetHorizontalAlignment(
118 cef_horizontal_alignment_t alignment) {
119 CEF_REQUIRE_VALID_RETURN_VOID();
120 ParentClass::root_view()->SetHorizontalAlignment(
121 static_cast<gfx::HorizontalAlignment>(alignment));
122 }
123
SetMinimumSize(const CefSize & size)124 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetMinimumSize(
125 const CefSize& size) {
126 CEF_REQUIRE_VALID_RETURN_VOID();
127 ParentClass::root_view()->SetMinSize(gfx::Size(size.width, size.height));
128 }
129
SetMaximumSize(const CefSize & size)130 CEF_LABEL_BUTTON_IMPL_T void CEF_LABEL_BUTTON_IMPL_D::SetMaximumSize(
131 const CefSize& size) {
132 CEF_REQUIRE_VALID_RETURN_VOID();
133 ParentClass::root_view()->SetMaxSize(gfx::Size(size.width, size.height));
134 }
135
136 #endif // CEF_LIBCEF_BROWSER_VIEWS_LABEL_BUTTON_IMPL_H_
137