OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/models/simple_menu_model.h" | 5 #include "ui/base/models/simple_menu_model.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/gfx/image/image_skia.h" | 10 #include "ui/gfx/image/image_skia.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 InsertItemAtIndex(item, index); | 178 InsertItemAtIndex(item, index); |
179 } | 179 } |
180 | 180 |
181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( | 181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( |
182 int index, int command_id, int string_id, MenuModel* model) { | 182 int index, int command_id, int string_id, MenuModel* model) { |
183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), | 183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), |
184 model); | 184 model); |
185 } | 185 } |
186 | 186 |
187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { | 187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { |
188 items_[index].icon = icon; | 188 items_[ValidateItemIndex(index)].icon = icon; |
189 } | 189 } |
190 | 190 |
191 void SimpleMenuModel::Clear() { | 191 void SimpleMenuModel::Clear() { |
192 items_.clear(); | 192 items_.clear(); |
193 } | 193 } |
194 | 194 |
195 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { | 195 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { |
196 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { | 196 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { |
197 if ((*i).command_id == command_id) { | 197 if (i->command_id == command_id) { |
198 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i))); | 198 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i))); |
199 } | 199 } |
200 } | 200 } |
201 return -1; | 201 return -1; |
202 } | 202 } |
203 | 203 |
204 //////////////////////////////////////////////////////////////////////////////// | 204 //////////////////////////////////////////////////////////////////////////////// |
205 // SimpleMenuModel, MenuModel implementation: | 205 // SimpleMenuModel, MenuModel implementation: |
206 | 206 |
207 bool SimpleMenuModel::HasIcons() const { | 207 bool SimpleMenuModel::HasIcons() const { |
208 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { | 208 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { |
209 if (!i->icon.isNull()) | 209 if (!i->icon.isNull()) |
210 return true; | 210 return true; |
211 } | 211 } |
212 | 212 |
213 return false; | 213 return false; |
214 } | 214 } |
215 | 215 |
216 int SimpleMenuModel::GetItemCount() const { | 216 int SimpleMenuModel::GetItemCount() const { |
217 return static_cast<int>(items_.size()); | 217 return static_cast<int>(items_.size()); |
218 } | 218 } |
219 | 219 |
220 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { | 220 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { |
221 return items_.at(FlipIndex(index)).type; | 221 return items_[ValidateItemIndex(FlipIndex(index))].type; |
222 } | 222 } |
223 | 223 |
224 int SimpleMenuModel::GetCommandIdAt(int index) const { | 224 int SimpleMenuModel::GetCommandIdAt(int index) const { |
225 return items_.at(FlipIndex(index)).command_id; | 225 return items_[ValidateItemIndex(FlipIndex(index))].command_id; |
226 } | 226 } |
227 | 227 |
228 string16 SimpleMenuModel::GetLabelAt(int index) const { | 228 string16 SimpleMenuModel::GetLabelAt(int index) const { |
229 if (IsItemDynamicAt(index)) | 229 if (IsItemDynamicAt(index)) |
230 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); | 230 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); |
231 return items_.at(FlipIndex(index)).label; | 231 return items_[ValidateItemIndex(FlipIndex(index))].label; |
232 } | 232 } |
233 | 233 |
234 bool SimpleMenuModel::IsItemDynamicAt(int index) const { | 234 bool SimpleMenuModel::IsItemDynamicAt(int index) const { |
235 if (delegate_) | 235 if (delegate_) |
236 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); | 236 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); |
237 return false; | 237 return false; |
238 } | 238 } |
239 | 239 |
240 bool SimpleMenuModel::GetAcceleratorAt(int index, | 240 bool SimpleMenuModel::GetAcceleratorAt(int index, |
241 ui::Accelerator* accelerator) const { | 241 ui::Accelerator* accelerator) const { |
242 if (delegate_) { | 242 if (delegate_) { |
243 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), | 243 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), |
244 accelerator); | 244 accelerator); |
245 } | 245 } |
246 return false; | 246 return false; |
247 } | 247 } |
248 | 248 |
249 bool SimpleMenuModel::IsItemCheckedAt(int index) const { | 249 bool SimpleMenuModel::IsItemCheckedAt(int index) const { |
250 if (!delegate_) | 250 if (!delegate_) |
251 return false; | 251 return false; |
252 int item_index = FlipIndex(index); | 252 MenuModel::ItemType item_type = GetTypeAt(index); |
253 MenuModel::ItemType item_type = items_[item_index].type; | |
254 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? | 253 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? |
255 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; | 254 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; |
256 } | 255 } |
257 | 256 |
258 int SimpleMenuModel::GetGroupIdAt(int index) const { | 257 int SimpleMenuModel::GetGroupIdAt(int index) const { |
259 return items_.at(FlipIndex(index)).group_id; | 258 return items_[ValidateItemIndex(FlipIndex(index))].group_id; |
260 } | 259 } |
261 | 260 |
262 bool SimpleMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) { | 261 bool SimpleMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) { |
263 if (IsItemDynamicAt(index)) | 262 if (IsItemDynamicAt(index)) |
264 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); | 263 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); |
265 | 264 |
| 265 ValidateItemIndex(index); |
266 if (items_[index].icon.isNull()) | 266 if (items_[index].icon.isNull()) |
267 return false; | 267 return false; |
268 | 268 |
269 *icon = items_[index].icon; | 269 *icon = items_[index].icon; |
270 return true; | 270 return true; |
271 } | 271 } |
272 | 272 |
273 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { | 273 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { |
274 return items_.at(FlipIndex(index)).button_model; | 274 return items_[ValidateItemIndex(FlipIndex(index))].button_model; |
275 } | 275 } |
276 | 276 |
277 bool SimpleMenuModel::IsEnabledAt(int index) const { | 277 bool SimpleMenuModel::IsEnabledAt(int index) const { |
278 int command_id = GetCommandIdAt(index); | 278 int command_id = GetCommandIdAt(index); |
279 if (!delegate_ || command_id == kSeparatorId || | 279 if (!delegate_ || command_id == kSeparatorId || GetButtonMenuItemAt(index)) |
280 items_.at(FlipIndex(index)).button_model) | |
281 return true; | 280 return true; |
282 return delegate_->IsCommandIdEnabled(command_id); | 281 return delegate_->IsCommandIdEnabled(command_id); |
283 } | 282 } |
284 | 283 |
285 bool SimpleMenuModel::IsVisibleAt(int index) const { | 284 bool SimpleMenuModel::IsVisibleAt(int index) const { |
286 int command_id = GetCommandIdAt(index); | 285 int command_id = GetCommandIdAt(index); |
287 if (!delegate_ || command_id == kSeparatorId || | 286 if (!delegate_ || command_id == kSeparatorId || GetButtonMenuItemAt(index)) |
288 items_.at(FlipIndex(index)).button_model) | |
289 return true; | 287 return true; |
290 return delegate_->IsCommandIdVisible(command_id); | 288 return delegate_->IsCommandIdVisible(command_id); |
291 } | 289 } |
292 | 290 |
293 void SimpleMenuModel::HighlightChangedTo(int index) { | 291 void SimpleMenuModel::HighlightChangedTo(int index) { |
294 if (delegate_) | 292 if (delegate_) |
295 delegate_->CommandIdHighlighted(GetCommandIdAt(index)); | 293 delegate_->CommandIdHighlighted(GetCommandIdAt(index)); |
296 } | 294 } |
297 | 295 |
298 void SimpleMenuModel::ActivatedAt(int index) { | 296 void SimpleMenuModel::ActivatedAt(int index) { |
299 if (delegate_) | 297 if (delegate_) |
300 delegate_->ExecuteCommand(GetCommandIdAt(index)); | 298 delegate_->ExecuteCommand(GetCommandIdAt(index)); |
301 } | 299 } |
302 | 300 |
303 void SimpleMenuModel::ActivatedAt(int index, int event_flags) { | 301 void SimpleMenuModel::ActivatedAt(int index, int event_flags) { |
304 if (delegate_) | 302 if (delegate_) |
305 delegate_->ExecuteCommand(GetCommandIdAt(index), event_flags); | 303 delegate_->ExecuteCommand(GetCommandIdAt(index), event_flags); |
306 } | 304 } |
307 | 305 |
308 MenuModel* SimpleMenuModel::GetSubmenuModelAt(int index) const { | 306 MenuModel* SimpleMenuModel::GetSubmenuModelAt(int index) const { |
309 return items_.at(FlipIndex(index)).submenu; | 307 return items_[ValidateItemIndex(FlipIndex(index))].submenu; |
310 } | 308 } |
311 | 309 |
312 void SimpleMenuModel::MenuWillShow() { | 310 void SimpleMenuModel::MenuWillShow() { |
313 if (delegate_) | 311 if (delegate_) |
314 delegate_->MenuWillShow(this); | 312 delegate_->MenuWillShow(this); |
315 } | 313 } |
316 | 314 |
317 void SimpleMenuModel::MenuClosed() { | 315 void SimpleMenuModel::MenuClosed() { |
318 // Due to how menus work on the different platforms, ActivatedAt will be | 316 // Due to how menus work on the different platforms, ActivatedAt will be |
319 // called after this. It's more convenient for the delegate to be called | 317 // called after this. It's more convenient for the delegate to be called |
(...skipping 13 matching lines...) Expand all Loading... |
333 delegate_->MenuClosed(this); | 331 delegate_->MenuClosed(this); |
334 } | 332 } |
335 | 333 |
336 int SimpleMenuModel::FlipIndex(int index) const { | 334 int SimpleMenuModel::FlipIndex(int index) const { |
337 return index; | 335 return index; |
338 } | 336 } |
339 | 337 |
340 //////////////////////////////////////////////////////////////////////////////// | 338 //////////////////////////////////////////////////////////////////////////////// |
341 // SimpleMenuModel, Private: | 339 // SimpleMenuModel, Private: |
342 | 340 |
| 341 int SimpleMenuModel::ValidateItemIndex(int index) const { |
| 342 CHECK_GE(index, 0); |
| 343 CHECK_LT(static_cast<size_t>(index), items_.size()); |
| 344 return index; |
| 345 } |
| 346 |
343 void SimpleMenuModel::AppendItem(const Item& item) { | 347 void SimpleMenuModel::AppendItem(const Item& item) { |
344 ValidateItem(item); | 348 ValidateItem(item); |
345 items_.push_back(item); | 349 items_.push_back(item); |
346 } | 350 } |
347 | 351 |
348 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { | 352 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { |
349 ValidateItem(item); | 353 ValidateItem(item); |
350 items_.insert(items_.begin() + FlipIndex(index), item); | 354 items_.insert(items_.begin() + FlipIndex(index), item); |
351 } | 355 } |
352 | 356 |
353 void SimpleMenuModel::ValidateItem(const Item& item) { | 357 void SimpleMenuModel::ValidateItem(const Item& item) { |
354 #ifndef NDEBUG | 358 #ifndef NDEBUG |
355 if (item.type == TYPE_SEPARATOR) { | 359 if (item.type == TYPE_SEPARATOR) { |
356 DCHECK_EQ(item.command_id, kSeparatorId); | 360 DCHECK_EQ(item.command_id, kSeparatorId); |
357 } else { | 361 } else { |
358 DCHECK_GE(item.command_id, 0); | 362 DCHECK_GE(item.command_id, 0); |
359 } | 363 } |
360 #endif // NDEBUG | 364 #endif // NDEBUG |
361 } | 365 } |
362 | 366 |
363 } // namespace ui | 367 } // namespace ui |
OLD | NEW |