Home | History | Annotate | Download | only in translate
      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.
      4 
      5 #ifndef CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_VIEW_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "chrome/browser/translate/chrome_translate_client.h"
     13 #include "chrome/browser/ui/translate/language_combobox_model.h"
     14 #include "chrome/browser/ui/translate/translate_bubble_model.h"
     15 #include "components/translate/core/common/translate_errors.h"
     16 #include "content/public/browser/web_contents_observer.h"
     17 #include "ui/views/bubble/bubble_delegate.h"
     18 #include "ui/views/controls/button/button.h"
     19 #include "ui/views/controls/combobox/combobox_listener.h"
     20 #include "ui/views/controls/link_listener.h"
     21 
     22 class PrefService;
     23 
     24 namespace views {
     25 class Checkbox;
     26 class GridLayout;
     27 class LabelButton;
     28 class Link;
     29 class View;
     30 }
     31 
     32 namespace ui {
     33 class SimpleComboboxModel;
     34 }
     35 
     36 class TranslateBubbleView : public views::BubbleDelegateView,
     37                             public views::ButtonListener,
     38                             public views::ComboboxListener,
     39                             public views::LinkListener,
     40                             public content::WebContentsObserver {
     41  public:
     42   virtual ~TranslateBubbleView();
     43 
     44   // Shows the Translate bubble.
     45   //
     46   // |is_user_gesture| is true when the bubble is shown on the user's delibarate
     47   // action.
     48   static void ShowBubble(views::View* anchor_view,
     49                          content::WebContents* web_contents,
     50                          translate::TranslateStep step,
     51                          translate::TranslateErrors::Type error_type,
     52                          bool is_user_gesture);
     53 
     54   // Closes the current bubble if existing.
     55   static void CloseBubble();
     56 
     57   // If true, the Translate bubble is being shown.
     58   static bool IsShowing();
     59 
     60   // Returns the bubble view currently shown. This may return NULL.
     61   static TranslateBubbleView* GetCurrentBubble();
     62 
     63   TranslateBubbleModel* model() { return model_.get(); }
     64 
     65   // views::BubbleDelegateView methods.
     66   virtual void Init() OVERRIDE;
     67   virtual void ButtonPressed(views::Button* sender,
     68                              const ui::Event& event) OVERRIDE;
     69 
     70   // views::WidgetDelegate method.
     71   virtual void WindowClosing() OVERRIDE;
     72 
     73   // views::View methods.
     74   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
     75   virtual gfx::Size GetPreferredSize() const OVERRIDE;
     76 
     77   // views::CombboxListener methods.
     78   virtual void OnPerformAction(views::Combobox* combobox) OVERRIDE;
     79 
     80   // views::LinkListener method.
     81   virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
     82 
     83   // content::WebContentsObserver method.
     84   virtual void WebContentsDestroyed() OVERRIDE;
     85 
     86   // Returns the current view state.
     87   TranslateBubbleModel::ViewState GetViewState() const;
     88 
     89  private:
     90   enum LinkID {
     91     LINK_ID_ADVANCED,
     92     LINK_ID_LANGUAGE_SETTINGS,
     93   };
     94 
     95   enum ButtonID {
     96     BUTTON_ID_TRANSLATE,
     97     BUTTON_ID_DONE,
     98     BUTTON_ID_CANCEL,
     99     BUTTON_ID_SHOW_ORIGINAL,
    100     BUTTON_ID_TRY_AGAIN,
    101     BUTTON_ID_ALWAYS_TRANSLATE,
    102   };
    103 
    104   enum ComboboxID {
    105     COMBOBOX_ID_DENIAL,
    106     COMBOBOX_ID_SOURCE_LANGUAGE,
    107     COMBOBOX_ID_TARGET_LANGUAGE,
    108   };
    109 
    110   friend class TranslateBubbleViewTest;
    111   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest, TranslateButton);
    112   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest, AdvancedLink);
    113   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest, ShowOriginalButton);
    114   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest, TryAgainButton);
    115   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest,
    116                            AlwaysTranslateCheckboxAndCancelButton);
    117   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest,
    118                            AlwaysTranslateCheckboxAndDoneButton);
    119   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest, DoneButton);
    120   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest,
    121                            DoneButtonWithoutTranslating);
    122   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest,
    123                            CancelButtonReturningBeforeTranslate);
    124   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest,
    125                            CancelButtonReturningAfterTranslate);
    126   FRIEND_TEST_ALL_PREFIXES(TranslateBubbleViewTest, CancelButtonReturningError);
    127 
    128   TranslateBubbleView(views::View* anchor_view,
    129                       scoped_ptr<TranslateBubbleModel> model,
    130                       translate::TranslateErrors::Type error_type,
    131                       content::WebContents* web_contents);
    132 
    133   // Returns the current child view.
    134   views::View* GetCurrentView() const;
    135 
    136   // Handles the event when the user presses a button.
    137   void HandleButtonPressed(ButtonID sender_id);
    138 
    139   // Handles the event when the user clicks a link.
    140   void HandleLinkClicked(LinkID sender_id);
    141 
    142   // Handles the event when the user changes an index of a combobox.
    143   void HandleComboboxPerformAction(ComboboxID sender_id);
    144 
    145   // Updates the visibilities of child views according to the current view type.
    146   void UpdateChildVisibilities();
    147 
    148   // Creates the 'before translate' view. Caller takes ownership of the returned
    149   // view.
    150   views::View* CreateViewBeforeTranslate();
    151 
    152   // Creates the 'translating' view. Caller takes ownership of the returned
    153   // view.
    154   views::View* CreateViewTranslating();
    155 
    156   // Creates the 'after translate' view. Caller takes ownership of the returned
    157   // view.
    158   views::View* CreateViewAfterTranslate();
    159 
    160   // Creates the 'error' view. Caller takes ownership of the returned view.
    161   views::View* CreateViewError();
    162 
    163   // Creates the 'advanced' view. Caller takes ownership of the returned view.
    164   views::View* CreateViewAdvanced();
    165 
    166   // Switches the view type.
    167   void SwitchView(TranslateBubbleModel::ViewState view_state);
    168 
    169   // Switches to the error view.
    170   void SwitchToErrorView(translate::TranslateErrors::Type error_type);
    171 
    172   // Updates the advanced view.
    173   void UpdateAdvancedView();
    174 
    175   static TranslateBubbleView* translate_bubble_view_;
    176 
    177   views::View* before_translate_view_;
    178   views::View* translating_view_;
    179   views::View* after_translate_view_;
    180   views::View* error_view_;
    181   views::View* advanced_view_;
    182 
    183   scoped_ptr<ui::SimpleComboboxModel> denial_combobox_model_;
    184   scoped_ptr<LanguageComboboxModel> source_language_combobox_model_;
    185   scoped_ptr<LanguageComboboxModel> target_language_combobox_model_;
    186 
    187   views::Combobox* denial_combobox_;
    188   views::Combobox* source_language_combobox_;
    189   views::Combobox* target_language_combobox_;
    190 
    191   views::Checkbox* always_translate_checkbox_;
    192 
    193   views::LabelButton* advanced_cancel_button_;
    194   views::LabelButton* advanced_done_button_;
    195 
    196   scoped_ptr<TranslateBubbleModel> model_;
    197 
    198   translate::TranslateErrors::Type error_type_;
    199 
    200   // Whether the window is an incognito window.
    201   const bool is_in_incognito_window_;
    202 
    203   // Whether the translation is acutually executed.
    204   bool translate_executed_;
    205 
    206   // Whether one of denial buttons is clicked.
    207   bool denial_button_clicked_;
    208 
    209   DISALLOW_COPY_AND_ASSIGN(TranslateBubbleView);
    210 };
    211 
    212 #endif  // CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_VIEW_H_
    213