summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/options/language_config_view.cc
blob: e8eb445c4ab07f1d96031d1bd63bd038159916e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/options/language_config_view.h"

#include <utility>
#include <vector>

#include "app/l10n_util.h"
#include "app/table_model.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/cros/language_library.h"
#include "chrome/browser/chromeos/options/language_hangul_config_view.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "views/controls/button/checkbox.h"
#include "views/controls/label.h"
#include "views/grid_layout.h"
#include "views/standard_layout.h"
#include "views/window/window.h"

namespace chromeos {

// This is a checkbox associated with input language information.
class LanguageCheckbox : public views::Checkbox {
 public:
  explicit LanguageCheckbox(const InputLanguage& language)
      : views::Checkbox(UTF8ToWide(language.display_name)),
        language_(language) {
  }

  const InputLanguage& language() const {
    return language_;
  }

 private:
  InputLanguage language_;
  DISALLOW_COPY_AND_ASSIGN(LanguageCheckbox);
};

// The table model for the list of preferred languages.
class PreferredLanguageTableModel : public TableModel {
 public:
  PreferredLanguageTableModel()
      : observer_(NULL) {
  }

  // TableModel overrides:
  virtual std::wstring GetText(int row, int column_id) {
    return L"Not yet implemented";
  }

  virtual void SetObserver(TableModelObserver* observer) {
    observer_ = observer;
  }

  virtual int RowCount() {
    return 1;
  }

 private:
  TableModelObserver* observer_;
  DISALLOW_COPY_AND_ASSIGN(PreferredLanguageTableModel);
};

LanguageConfigView::LanguageConfigView() :
    contents_(NULL),
    hangul_configure_button_(NULL),
    language_checkbox_(NULL),
    preferred_language_table_(NULL),
    preferred_language_table_model_(new PreferredLanguageTableModel) {
}

LanguageConfigView::~LanguageConfigView() {
  if (preferred_language_table_) {
    // Reset the model to avoid a destruction issue in TableView2.
    // See the comment at SetModel() in table_view2.h for details.
    preferred_language_table_->SetModel(NULL);
  }
}

void LanguageConfigView::ButtonPressed(
    views::Button* sender, const views::Event& event) {
  if (sender == static_cast<views::Button*>(hangul_configure_button_)) {
    views::Window* window = views::Window::CreateChromeWindow(
        NULL, gfx::Rect(), new LanguageHangulConfigView());
    window->SetIsAlwaysOnTop(true);
    window->Show();
  } else if (sender == static_cast<views::Button*>(language_checkbox_)) {
    LanguageCheckbox* checkbox = static_cast<LanguageCheckbox*>(sender);
    const InputLanguage& language = checkbox->language();
    // Check if the checkbox is now being checked.
    if (checkbox->checked()) {
      // TODO(yusukes): limit the number of active languages so the pop-up menu
      //   of the language_menu_button does not overflow.

      // Try to activate the language.
      if (!LanguageLibrary::Get()->ActivateLanguage(language.category,
                                                    language.id)) {
        // Activation should never fail (failure means something is broken),
        // but if it fails we just revert the checkbox and ignore the error.
        // TODO(satorux): Implement a better error handling if it becomes
        // necessary.
        checkbox->SetChecked(false);
        LOG(ERROR) << "Failed to activate language: "
                   << language.display_name;
      }
    } else {
      // Try to deactivate the language.
      if (!LanguageLibrary::Get()->DeactivateLanguage(language.category,
                                                      language.id)) {
        // See a comment above about the activation failure.
        checkbox->SetChecked(true);
        LOG(ERROR) << "Failed to deactivate language: "
                   << language.display_name;
      }
    }
  }
}

void LanguageConfigView::Layout() {
  // Not sure why but this is needed to show contents in the dialog.
  contents_->SetBounds(0, 0, width(), height());
}

std::wstring LanguageConfigView::GetWindowTitle() const {
  return l10n_util::GetString(
      IDS_OPTIONS_SETTINGS_LANGUAGES_DIALOG_TITLE);
}

gfx::Size LanguageConfigView::GetPreferredSize() {
  // TODO(satorux): Create our own localized content size once the UI is done.
  return gfx::Size(views::Window::GetLocalizedContentsSize(
      IDS_FONTSLANG_DIALOG_WIDTH_CHARS,
      IDS_FONTSLANG_DIALOG_HEIGHT_LINES));
}

void LanguageConfigView::ViewHierarchyChanged(
    bool is_add, views::View* parent, views::View* child) {
  // Can't init before we're inserted into a Container.
  if (is_add && child == this) {
    Init();
  }
}

void LanguageConfigView::OnSelectionChanged() {
  // TODO(satorux): Implement this.
}

// TODO(satorux): Refactor the function.
void LanguageConfigView::Init() {
  using views::ColumnSet;
  using views::GridLayout;

  if (contents_) return;  // Already initialized.
  contents_ = new views::View;
  AddChildView(contents_);

  // Set up the layout manager for the outer contents.  We'll place the
  // preferred language table on the left, and anything else on the right.
  GridLayout* outer_layout = new GridLayout(contents_);
  contents_->SetLayoutManager(outer_layout);
  outer_layout->SetInsets(kPanelVertMargin, kPanelHorizMargin,
                          kPanelVertMargin, kPanelHorizMargin);

  // Set up the column set for the outer contents.
  const int kOuterColumnSetId = 0;
  ColumnSet* column_set = outer_layout->AddColumnSet(kOuterColumnSetId);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
                        GridLayout::USE_PREF, 0, 0);

  // Set up the preferred language table.
  std::vector<TableColumn> columns;
  TableColumn column(0,
                     l10n_util::GetString(
                         IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES),
                     TableColumn::LEFT, -1 /* width */, 0 /* percent */);
  columns.push_back(column);
  preferred_language_table_ =
      new views::TableView2(preferred_language_table_model_.get(),
                            columns,
                            views::TEXT_ONLY,
                            true, true, true);
  preferred_language_table_->SetObserver(this);

  // Add the preferred language table.
  outer_layout->StartRow(1 /* expand */, kOuterColumnSetId);
  outer_layout->AddView(preferred_language_table_);

  // Set up the container for the contents on the right.
  views::View* right_contents = new views::View;
  GridLayout* layout = new GridLayout(right_contents);
  right_contents->SetLayoutManager(layout);
  outer_layout->AddView(right_contents);

  // Set up the single column set.
  const int kSingleColumnSetId = 1;
  column_set = layout->AddColumnSet(kSingleColumnSetId);
  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                        GridLayout::USE_PREF, 0, 0);

  // Set up the double column set.
  const int kDoubleColumnSetId = 2;
  column_set = layout->AddColumnSet(kDoubleColumnSetId);
  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                        GridLayout::USE_PREF, 0, 0);

  // GetActiveLanguages() and GetSupportedLanguages() never return NULL.
  scoped_ptr<InputLanguageList> active_language_list(
      LanguageLibrary::Get()->GetActiveLanguages());
  scoped_ptr<InputLanguageList> supported_language_list(
      LanguageLibrary::Get()->GetSupportedLanguages());

  for (size_t i = 0; i < supported_language_list->size(); ++i) {
    const InputLanguage& language = supported_language_list->at(i);
    // Check if |language| is active. Note that active_language_list is
    // small (usually a couple), so scanning here is fine.
    const bool language_is_active =
        (std::find(active_language_list->begin(),
                   active_language_list->end(),
                   language) != active_language_list->end());
    // Create a checkbox.
    language_checkbox_ = new LanguageCheckbox(language);
    language_checkbox_->SetChecked(language_is_active);
    language_checkbox_->set_listener(this);

    // We use two columns. Start a column if the counter is an even number.
    if (i % 2 == 0) {
      layout->StartRow(0, kDoubleColumnSetId);
    }
    // Add the checkbox to the layout manager.
    layout->AddView(language_checkbox_);
  }

  // Add the configure button for the Hangul IME.
  // TODO(satorux): We'll have better UI for this soon.
  layout->StartRow(0, kDoubleColumnSetId);
  hangul_configure_button_ = new views::NativeButton(
      this, l10n_util::GetString(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE));
  layout->AddView(new views::Label(
      l10n_util::GetString(IDS_OPTIONS_SETTINGS_HANGUL_IME_TEXT)));
  layout->AddView(hangul_configure_button_);
}

}  // namespace chromeos