1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_VIEW_H_
11 #include "base/basictypes.h"
12 #include "chrome/browser/ui/translate/language_combobox_model.h"
13 #include "chrome/browser/ui/translate/translate_bubble_model.h"
14 #include "components/translate/core/common/translate_errors.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "ui/views/bubble/bubble_delegate.h"
17 #include "ui/views/controls/button/button.h"
18 #include "ui/views/controls/combobox/combobox_listener.h"
19 #include "ui/views/controls/link_listener.h"
22 class TranslateBubbleModel
;
32 class TranslateBubbleView
: public views::BubbleDelegateView
,
33 public views::ButtonListener
,
34 public views::ComboboxListener
,
35 public views::LinkListener
,
36 public content::WebContentsObserver
{
38 virtual ~TranslateBubbleView();
40 // Shows the Translate bubble.
41 static void ShowBubble(views::View
* anchor_view
,
42 content::WebContents
* web_contents
,
43 TranslateBubbleModel::ViewState type
,
44 TranslateErrors::Type error_type
);
46 // If true, the Translate bubble is being shown.
47 static bool IsShowing();
49 // Returns the bubble view currently shown. This may return NULL.
50 static TranslateBubbleView
* GetCurrentBubble();
52 TranslateBubbleModel
* model() { return model_
.get(); }
54 // views::BubbleDelegateView methods.
55 virtual void Init() OVERRIDE
;
56 virtual void ButtonPressed(views::Button
* sender
,
57 const ui::Event
& event
) OVERRIDE
;
59 // views::WidgetDelegate method.
60 virtual void WindowClosing() OVERRIDE
;
62 // views::View methods.
63 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
64 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
66 // views::CombboxListener methods.
67 virtual void OnSelectedIndexChanged(views::Combobox
* combobox
) OVERRIDE
;
68 virtual void OnComboboxTextButtonClicked(views::Combobox
* combobox
) OVERRIDE
;
70 // views::LinkListener method.
71 virtual void LinkClicked(views::Link
* source
, int event_flags
) OVERRIDE
;
73 // content::WebContentsObserver method.
74 virtual void WebContentsDestroyed(content::WebContents
* web_contents
)
77 // Returns the current view state.
78 TranslateBubbleModel::ViewState
GetViewState() const;
83 LINK_ID_LANGUAGE_SETTINGS
,
90 BUTTON_ID_SHOW_ORIGINAL
,
92 BUTTON_ID_ALWAYS_TRANSLATE
,
97 COMBOBOX_ID_SOURCE_LANGUAGE
,
98 COMBOBOX_ID_TARGET_LANGUAGE
,
101 friend class TranslateBubbleViewTest
;
102 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
, TranslateButton
);
103 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
, AdvancedLink
);
104 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
, ShowOriginalButton
);
105 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
, TryAgainButton
);
106 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
,
107 AlwaysTranslateCheckboxAndCancelButton
);
108 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
,
109 AlwaysTranslateCheckboxAndDoneButton
);
110 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
, DoneButton
);
111 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
,
112 DoneButtonWithoutTranslating
);
113 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
,
114 CancelButtonReturningBeforeTranslate
);
115 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
,
116 CancelButtonReturningAfterTranslate
);
117 FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest
, CancelButtonReturningError
);
119 TranslateBubbleView(views::View
* anchor_view
,
120 scoped_ptr
<TranslateBubbleModel
> model
,
121 TranslateErrors::Type error_type
,
122 content::WebContents
* web_contents
);
124 // Returns the current child view.
125 views::View
* GetCurrentView();
127 // Handles the event when the user presses a button.
128 void HandleButtonPressed(ButtonID sender_id
);
130 // Handles the event when the user clicks a link.
131 void HandleLinkClicked(LinkID sender_id
);
133 // Handles the event when the user changes an index of a combobox.
134 void HandleComboboxSelectedIndexChanged(ComboboxID sender_id
);
136 // Updates the visibilities of child views according to the current view type.
137 void UpdateChildVisibilities();
139 // Creates the 'before translate' view. Caller takes ownership of the returned
141 views::View
* CreateViewBeforeTranslate();
143 // Creates the 'translating' view. Caller takes ownership of the returned
145 views::View
* CreateViewTranslating();
147 // Creates the 'after translate' view. Caller takes ownership of the returned
149 views::View
* CreateViewAfterTranslate();
151 // Creates the 'error' view. Caller takes ownership of the returned view.
152 views::View
* CreateViewError();
154 // Creates the 'advanced' view. Caller takes ownership of the returned view.
155 views::View
* CreateViewAdvanced();
157 // Switches the view type.
158 void SwitchView(TranslateBubbleModel::ViewState view_state
);
160 // Switches to the error view.
161 void SwitchToErrorView(TranslateErrors::Type error_type
);
163 // Updates the advanced view.
164 void UpdateAdvancedView();
166 static TranslateBubbleView
* translate_bubble_view_
;
168 views::View
* before_translate_view_
;
169 views::View
* translating_view_
;
170 views::View
* after_translate_view_
;
171 views::View
* error_view_
;
172 views::View
* advanced_view_
;
174 scoped_ptr
<LanguageComboboxModel
> source_language_combobox_model_
;
175 scoped_ptr
<LanguageComboboxModel
> target_language_combobox_model_
;
177 views::Combobox
* denial_combobox_
;
178 views::Combobox
* source_language_combobox_
;
179 views::Combobox
* target_language_combobox_
;
181 views::Checkbox
* always_translate_checkbox_
;
183 views::LabelButton
* advanced_cancel_button_
;
184 views::LabelButton
* advanced_done_button_
;
186 scoped_ptr
<TranslateBubbleModel
> model_
;
188 TranslateErrors::Type error_type_
;
190 // Whether the window is an incognito window.
191 const bool is_in_incognito_window_
;
193 // Whether the translation is acutually executed.
194 bool translate_executed_
;
196 // Whether one of denial buttons is clicked.
197 bool denial_button_clicked_
;
199 DISALLOW_COPY_AND_ASSIGN(TranslateBubbleView
);
202 #endif // CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_VIEW_H_