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.
5 #include "chrome/browser/ui/views/infobars/translate_language_menu_model.h"
7 #include "chrome/browser/translate/translate_infobar_delegate.h"
8 #include "chrome/browser/ui/views/infobars/translate_infobar_base.h"
10 TranslateLanguageMenuModel::TranslateLanguageMenuModel(
11 LanguageType language_type
,
12 TranslateInfoBarDelegate
* infobar_delegate
,
13 TranslateInfoBarBase
* infobar
,
14 views::MenuButton
* button
,
15 bool translate_on_change
)
16 : ui::SimpleMenuModel(this),
17 language_type_(language_type
),
18 infobar_delegate_(infobar_delegate
),
21 translate_on_change_(translate_on_change
) {
22 for (size_t i
= 0; i
< infobar_delegate_
->num_languages(); ++i
)
23 AddCheckItem(static_cast<int>(i
), infobar_delegate_
->language_name_at(i
));
26 TranslateLanguageMenuModel::~TranslateLanguageMenuModel() {
29 bool TranslateLanguageMenuModel::IsCommandIdChecked(int command_id
) const {
30 return static_cast<size_t>(command_id
) == GetLanguageIndex();
33 bool TranslateLanguageMenuModel::IsCommandIdEnabled(int command_id
) const {
34 // Prevent the same language from being selectable in original and target.
35 return static_cast<size_t>(command_id
) != ((language_type_
== ORIGINAL
) ?
36 infobar_delegate_
->target_language_index() :
37 infobar_delegate_
->original_language_index());
40 bool TranslateLanguageMenuModel::GetAcceleratorForCommandId(
42 ui::Accelerator
* accelerator
) {
46 void TranslateLanguageMenuModel::ExecuteCommand(int command_id
,
48 size_t command_id_size_t
= static_cast<size_t>(command_id
);
49 if (language_type_
== ORIGINAL
)
50 infobar_delegate_
->UpdateOriginalLanguageIndex(command_id_size_t
);
52 infobar_delegate_
->UpdateTargetLanguageIndex(command_id_size_t
);
54 infobar_
->UpdateLanguageButtonText(button_
,
55 infobar_delegate_
->language_name_at(GetLanguageIndex()));
56 if (translate_on_change_
)
57 infobar_delegate_
->Translate();
60 size_t TranslateLanguageMenuModel::GetLanguageIndex() const {
61 return (language_type_
== ORIGINAL
) ?
62 infobar_delegate_
->original_language_index() :
63 infobar_delegate_
->target_language_index();