| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "views/examples/native_theme_button_example.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/stringprintf.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "ui/base/animation/throb_animation.h" | |
| 13 #include "ui/base/models/combobox_model.h" | |
| 14 #include "ui/gfx/canvas.h" | |
| 15 #include "views/controls/label.h" | |
| 16 #include "views/examples/example_combobox_model.h" | |
| 17 #include "views/layout/grid_layout.h" | |
| 18 #include "views/native_theme_painter.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char* kParts[] = { | |
| 23 "PushButton", | |
| 24 "RadioButton", | |
| 25 "Checkbox", | |
| 26 }; | |
| 27 | |
| 28 const char* kStates[] = { | |
| 29 "Disabled", | |
| 30 "Normal", | |
| 31 "Hot", | |
| 32 "Pressed", | |
| 33 "<Dynamic>", | |
| 34 }; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 namespace examples { | |
| 39 | |
| 40 ExampleNativeThemeButton::ExampleNativeThemeButton( | |
| 41 views::ButtonListener* listener, | |
| 42 views::Combobox* cb_part, | |
| 43 views::Combobox* cb_state) | |
| 44 : CustomButton(listener), | |
| 45 cb_part_(cb_part), | |
| 46 cb_state_(cb_state), | |
| 47 count_(0), | |
| 48 is_checked_(false), | |
| 49 is_indeterminate_(false) { | |
| 50 cb_part_->set_listener(this); | |
| 51 cb_state_->set_listener(this); | |
| 52 | |
| 53 painter_.reset(new views::NativeThemePainter(this)); | |
| 54 set_background(views::Background::CreateBackgroundPainter( | |
| 55 false, painter_.get())); | |
| 56 } | |
| 57 | |
| 58 ExampleNativeThemeButton::~ExampleNativeThemeButton() { | |
| 59 } | |
| 60 | |
| 61 std::string ExampleNativeThemeButton::MessWithState() { | |
| 62 const char* message = NULL; | |
| 63 switch (GetThemePart()) { | |
| 64 case gfx::NativeTheme::kPushButton: | |
| 65 message = "Pressed! count:%d"; | |
| 66 break; | |
| 67 case gfx::NativeTheme::kRadio: | |
| 68 is_checked_ = !is_checked_; | |
| 69 message = is_checked_ ? "Checked! count:%d" : "Unchecked! count:%d"; | |
| 70 break; | |
| 71 case gfx::NativeTheme::kCheckbox: | |
| 72 if (is_indeterminate_) { | |
| 73 is_checked_ = false; | |
| 74 is_indeterminate_ = false; | |
| 75 } else if (!is_checked_) { | |
| 76 is_checked_ = true; | |
| 77 } else { | |
| 78 is_checked_ = false; | |
| 79 is_indeterminate_ = true; | |
| 80 } | |
| 81 | |
| 82 message = is_checked_ ? "Checked! count:%d" : | |
| 83 is_indeterminate_ ? "Indeterminate! count:%d" : "Unchecked! count:%d"; | |
| 84 break; | |
| 85 default: | |
| 86 DCHECK(false); | |
| 87 } | |
| 88 | |
| 89 return base::StringPrintf(message, ++count_); | |
| 90 } | |
| 91 | |
| 92 void ExampleNativeThemeButton::ItemChanged(views::Combobox* combo_box, | |
| 93 int prev_index, | |
| 94 int new_index) { | |
| 95 SchedulePaint(); | |
| 96 } | |
| 97 | |
| 98 gfx::NativeTheme::Part ExampleNativeThemeButton::GetThemePart() const { | |
| 99 int selected = cb_part_->selected_item(); | |
| 100 switch (selected) { | |
| 101 case 0: | |
| 102 return gfx::NativeTheme::kPushButton; | |
| 103 case 1: | |
| 104 return gfx::NativeTheme::kRadio; | |
| 105 case 2: | |
| 106 return gfx::NativeTheme::kCheckbox; | |
| 107 default: | |
| 108 DCHECK(false); | |
| 109 } | |
| 110 return gfx::NativeTheme::kPushButton; | |
| 111 } | |
| 112 | |
| 113 gfx::Rect ExampleNativeThemeButton::GetThemePaintRect() const { | |
| 114 gfx::NativeTheme::ExtraParams extra; | |
| 115 gfx::NativeTheme::State state = GetThemeState(&extra); | |
| 116 gfx::Size size(gfx::NativeTheme::instance()->GetPartSize(GetThemePart(), | |
| 117 state, | |
| 118 extra)); | |
| 119 gfx::Rect rect(size); | |
| 120 rect.set_x(GetMirroredXForRect(rect)); | |
| 121 return rect; | |
| 122 } | |
| 123 | |
| 124 gfx::NativeTheme::State ExampleNativeThemeButton::GetThemeState( | |
| 125 gfx::NativeTheme::ExtraParams* params) const { | |
| 126 GetExtraParams(params); | |
| 127 | |
| 128 int selected = cb_state_->selected_item(); | |
| 129 if (selected > 3) { | |
| 130 switch (state()) { | |
| 131 case BS_DISABLED: | |
| 132 return gfx::NativeTheme::kDisabled; | |
| 133 case BS_NORMAL: | |
| 134 return gfx::NativeTheme::kNormal; | |
| 135 case BS_HOT: | |
| 136 return gfx::NativeTheme::kHovered; | |
| 137 case BS_PUSHED: | |
| 138 return gfx::NativeTheme::kPressed; | |
| 139 default: | |
| 140 DCHECK(false); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 switch (selected) { | |
| 145 case 0: | |
| 146 return gfx::NativeTheme::kDisabled; | |
| 147 case 1: | |
| 148 return gfx::NativeTheme::kNormal; | |
| 149 case 2: | |
| 150 return gfx::NativeTheme::kHovered; | |
| 151 case 3: | |
| 152 return gfx::NativeTheme::kPressed; | |
| 153 default: | |
| 154 DCHECK(false); | |
| 155 } | |
| 156 return gfx::NativeTheme::kNormal; | |
| 157 } | |
| 158 | |
| 159 void ExampleNativeThemeButton::GetExtraParams( | |
| 160 gfx::NativeTheme::ExtraParams* params) const { | |
| 161 | |
| 162 params->button.checked = is_checked_; | |
| 163 params->button.indeterminate = is_indeterminate_; | |
| 164 params->button.is_default = false; | |
| 165 params->button.has_border = false; | |
| 166 params->button.classic_state = 0; | |
| 167 params->button.background_color = SkColorSetARGB(0, 0, 0, 0); | |
| 168 } | |
| 169 | |
| 170 const ui::Animation* ExampleNativeThemeButton::GetThemeAnimation() const { | |
| 171 int selected = cb_state_->selected_item(); | |
| 172 return selected <= 3 ? NULL : hover_animation_.get(); | |
| 173 } | |
| 174 | |
| 175 gfx::NativeTheme::State ExampleNativeThemeButton::GetBackgroundThemeState( | |
| 176 gfx::NativeTheme::ExtraParams* params) const { | |
| 177 GetExtraParams(params); | |
| 178 return gfx::NativeTheme::kNormal; | |
| 179 } | |
| 180 | |
| 181 gfx::NativeTheme::State ExampleNativeThemeButton::GetForegroundThemeState( | |
| 182 gfx::NativeTheme::ExtraParams* params) const { | |
| 183 GetExtraParams(params); | |
| 184 return gfx::NativeTheme::kHovered; | |
| 185 } | |
| 186 | |
| 187 gfx::Size ExampleNativeThemeButton::GetPreferredSize() { | |
| 188 return painter_.get() == NULL ? gfx::Size() : painter_->GetPreferredSize(); | |
| 189 } | |
| 190 | |
| 191 void ExampleNativeThemeButton::OnPaintBackground(gfx::Canvas* canvas) { | |
| 192 // Fill the background with a known colour so that we know where the bounds | |
| 193 // of the View are. | |
| 194 canvas->FillRect(SkColorSetRGB(255, 128, 128), GetLocalBounds()); | |
| 195 CustomButton::OnPaintBackground(canvas); | |
| 196 } | |
| 197 | |
| 198 //////////////////////////////////////////////////////////////////////////////// | |
| 199 | |
| 200 NativeThemeButtonExample::NativeThemeButtonExample(ExamplesMain* main) | |
| 201 : ExampleBase(main, "Native Theme Button") { | |
| 202 } | |
| 203 | |
| 204 NativeThemeButtonExample::~NativeThemeButtonExample() { | |
| 205 } | |
| 206 | |
| 207 void NativeThemeButtonExample::CreateExampleView(views::View* container) { | |
| 208 views::GridLayout* layout = new views::GridLayout(container); | |
| 209 container->SetLayoutManager(layout); | |
| 210 | |
| 211 layout->AddPaddingRow(0, 8); | |
| 212 | |
| 213 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
| 214 column_set->AddPaddingColumn(0, 8); | |
| 215 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, | |
| 216 0.1f, views::GridLayout::USE_PREF, 0, 0); | |
| 217 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
| 218 0.9f, views::GridLayout::USE_PREF, 0, 0); | |
| 219 column_set->AddPaddingColumn(0, 8); | |
| 220 | |
| 221 layout->StartRow(0, 0); | |
| 222 layout->AddView(new views::Label(ASCIIToUTF16("Part:"))); | |
| 223 views::Combobox* cb_part = new views::Combobox( | |
| 224 new ExampleComboboxModel(kParts, arraysize(kParts))); | |
| 225 cb_part->SetSelectedItem(0); | |
| 226 layout->AddView(cb_part); | |
| 227 | |
| 228 layout->StartRow(0, 0); | |
| 229 layout->AddView(new views::Label(ASCIIToUTF16("State:"))); | |
| 230 views::Combobox* cb_state = new views::Combobox( | |
| 231 new ExampleComboboxModel(kStates, arraysize(kStates))); | |
| 232 cb_state->SetSelectedItem(0); | |
| 233 layout->AddView(cb_state); | |
| 234 | |
| 235 layout->AddPaddingRow(0, 32); | |
| 236 | |
| 237 button_ = new ExampleNativeThemeButton(this, cb_part, cb_state); | |
| 238 | |
| 239 column_set = layout->AddColumnSet(1); | |
| 240 column_set->AddPaddingColumn(0, 16); | |
| 241 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
| 242 1, views::GridLayout::USE_PREF, 0, 0); | |
| 243 column_set->AddPaddingColumn(0, 16); | |
| 244 layout->StartRow(1, 1); | |
| 245 layout->AddView(button_); | |
| 246 | |
| 247 layout->AddPaddingRow(0, 8); | |
| 248 } | |
| 249 | |
| 250 void NativeThemeButtonExample::ButtonPressed(views::Button* sender, | |
| 251 const views::Event& event) { | |
| 252 PrintStatus(button_->MessWithState().c_str()); | |
| 253 } | |
| 254 | |
| 255 } // namespace examples | |
| OLD | NEW |