OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "chrome/browser/views/star_toggle.h" | |
6 | |
7 #include "app/gfx/canvas.h" | |
8 #include "app/resource_bundle.h" | |
9 #include "base/keyboard_codes.h" | |
10 #include "chrome/app/chrome_dll_resource.h" | |
11 #include "grit/theme_resources.h" | |
12 | |
13 StarToggle::StarToggle(Delegate* delegate) | |
14 : delegate_(delegate), | |
15 state_(false), | |
16 change_state_immediately_(false) { | |
17 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
18 state_off_ = rb.GetBitmapNamed(IDR_CONTENT_STAR_OFF); | |
19 state_on_ = rb.GetBitmapNamed(IDR_CONTENT_STAR_ON); | |
20 SetFocusable(true); | |
21 } | |
22 | |
23 StarToggle::~StarToggle() { | |
24 } | |
25 | |
26 void StarToggle::SetState(bool s) { | |
27 if (s != state_) { | |
28 state_ = s; | |
29 SchedulePaint(); | |
30 } | |
31 } | |
32 | |
33 bool StarToggle::GetState() const { | |
34 return state_; | |
35 } | |
36 | |
37 void StarToggle::Paint(gfx::Canvas* canvas) { | |
38 PaintFocusBorder(canvas); | |
39 canvas->DrawBitmapInt(state_ ? *state_on_ : *state_off_, | |
40 (width() - state_off_->width()) / 2, | |
41 (height() - state_off_->height()) / 2); | |
42 } | |
43 | |
44 gfx::Size StarToggle::GetPreferredSize() { | |
45 return gfx::Size(state_off_->width(), state_off_->height()); | |
46 } | |
47 | |
48 bool StarToggle::OnMouseDragged(const views::MouseEvent& e) { | |
49 return e.IsLeftMouseButton(); | |
50 } | |
51 | |
52 bool StarToggle::OnMousePressed(const views::MouseEvent& e) { | |
53 if (e.IsLeftMouseButton() && HitTest(e.location())) { | |
54 RequestFocus(); | |
55 return true; | |
56 } | |
57 return false; | |
58 } | |
59 | |
60 void StarToggle::OnMouseReleased(const views::MouseEvent& e, | |
61 bool canceled) { | |
62 if (e.IsLeftMouseButton() && HitTest(e.location())) | |
63 SwitchState(); | |
64 } | |
65 | |
66 bool StarToggle::OnKeyPressed(const views::KeyEvent& e) { | |
67 if ((e.GetKeyCode() == base::VKEY_SPACE) || | |
68 (e.GetKeyCode() == base::VKEY_RETURN)) { | |
69 SwitchState(); | |
70 return true; | |
71 } | |
72 return false; | |
73 } | |
74 | |
75 void StarToggle::SwitchState() { | |
76 const bool new_state = !state_; | |
77 if (change_state_immediately_) | |
78 state_ = new_state; | |
79 SchedulePaint(); | |
80 delegate_->StarStateChanged(new_state); | |
81 } | |
OLD | NEW |