- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / reload_button.h
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 #ifndef CHROME_BROWSER_UI_VIEWS_RELOAD_BUTTON_H__
6 #define CHROME_BROWSER_UI_VIEWS_RELOAD_BUTTON_H__
7
8 #include "base/basictypes.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/timer/timer.h"
11 #include "ui/base/models/simple_menu_model.h"
12 #include "ui/views/controls/button/button_dropdown.h"
13
14 class CommandUpdater;
15 class LocationBarView;
16
17 ////////////////////////////////////////////////////////////////////////////////
18 //
19 // ReloadButton
20 //
21 // The reload button in the toolbar, which changes to a stop button when a page
22 // load is in progress. Trickiness comes from the desire to have the 'stop'
23 // button not change back to 'reload' if the user's mouse is hovering over it
24 // (to prevent mis-clicks).
25 //
26 ////////////////////////////////////////////////////////////////////////////////
27
28 class ReloadButton : public views::ButtonDropDown,
29                      public views::ButtonListener,
30                      public ui::SimpleMenuModel::Delegate {
31  public:
32   enum Mode { MODE_RELOAD = 0, MODE_STOP };
33
34   // The button's class name.
35   static const char kViewClassName[];
36
37   ReloadButton(LocationBarView* location_bar,
38                CommandUpdater* command_updater);
39   virtual ~ReloadButton();
40
41   // Ask for a specified button state.  If |force| is true this will be applied
42   // immediately.
43   void ChangeMode(Mode mode, bool force);
44
45   // Enable reload drop-down menu.
46   void set_menu_enabled(bool enable) { menu_enabled_ = enable; }
47
48   void LoadImages(ui::ThemeProvider* tp);
49
50   // Overridden from views::View:
51   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
52   virtual bool GetTooltipText(const gfx::Point& p,
53                               string16* tooltip) const OVERRIDE;
54   virtual const char* GetClassName() const OVERRIDE;
55   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
56
57   // Overridden from views::ButtonDropDown:
58   virtual bool ShouldShowMenu() OVERRIDE;
59   virtual void ShowDropDownMenu(ui::MenuSourceType source_type) OVERRIDE;
60
61   // Overridden from views::ButtonListener:
62   virtual void ButtonPressed(views::Button* /* button */,
63                              const ui::Event& event) OVERRIDE;
64
65   // Overridden from ui::SimpleMenuModel::Delegate:
66   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
67   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
68   virtual bool IsCommandIdVisible(int command_id) const OVERRIDE;
69   virtual bool GetAcceleratorForCommandId(
70       int command_id,
71       ui::Accelerator* accelerator) OVERRIDE;
72   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
73
74  private:
75   friend class ReloadButtonTest;
76
77   ui::SimpleMenuModel* CreateMenuModel();
78
79   void ExecuteBrowserCommand(int command, int event_flags);
80   void ChangeModeInternal(Mode mode);
81
82   void OnDoubleClickTimer();
83   void OnStopToReloadTimer();
84
85   base::OneShotTimer<ReloadButton> double_click_timer_;
86   base::OneShotTimer<ReloadButton> stop_to_reload_timer_;
87
88   // These may be NULL when testing.
89   LocationBarView* location_bar_;
90   CommandUpdater* command_updater_;
91
92   // The mode we should be in assuming no timers are running.
93   Mode intended_mode_;
94
95   // The currently-visible mode - this may differ from the intended mode.
96   Mode visible_mode_;
97
98   // The delay times for the timers.  These are members so that tests can modify
99   // them.
100   base::TimeDelta double_click_timer_delay_;
101   base::TimeDelta stop_to_reload_timer_delay_;
102
103   // Indicates if reload menu is enabled.
104   bool menu_enabled_;
105
106   // The parent class's images_ member is used for the current images,
107   // and this array is used to hold the alternative images.
108   // We swap between the two when changing mode.
109   gfx::ImageSkia alternate_images_[STATE_COUNT];
110
111   // TESTING ONLY
112   // True if we should pretend the button is hovered.
113   bool testing_mouse_hovered_;
114   // Increments when we would tell the browser to "reload", so
115   // test code can tell whether we did so (as there may be no |browser_|).
116   int testing_reload_count_;
117
118   DISALLOW_IMPLICIT_CONSTRUCTORS(ReloadButton);
119 };
120
121 #endif  // CHROME_BROWSER_UI_VIEWS_RELOAD_BUTTON_H__