| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "views/controls/menu/menu_item_view.h" | 5 #include "views/controls/menu/menu_item_view.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "gfx/canvas.h" | 8 #include "gfx/canvas.h" |
| 9 #include "grit/app_strings.h" | 9 #include "grit/app_strings.h" |
| 10 #include "views/controls/menu/menu_config.h" | 10 #include "views/controls/menu/menu_config.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 SetID(kEmptyMenuItemViewID); | 31 SetID(kEmptyMenuItemViewID); |
| 32 SetEnabled(false); | 32 SetEnabled(false); |
| 33 } | 33 } |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(EmptyMenuMenuItem); | 36 DISALLOW_COPY_AND_ASSIGN(EmptyMenuMenuItem); |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 // Padding between child views. |
| 42 static const int kChildXPadding = 8; |
| 43 |
| 41 // MenuItemView --------------------------------------------------------------- | 44 // MenuItemView --------------------------------------------------------------- |
| 42 | 45 |
| 43 // static | 46 // static |
| 44 const int MenuItemView::kMenuItemViewID = 1001; | 47 const int MenuItemView::kMenuItemViewID = 1001; |
| 45 | 48 |
| 46 // static | 49 // static |
| 47 const int MenuItemView::kEmptyMenuItemViewID = | 50 const int MenuItemView::kEmptyMenuItemViewID = |
| 48 MenuItemView::kMenuItemViewID + 1; | 51 MenuItemView::kMenuItemViewID + 1; |
| 49 | 52 |
| 50 // static | 53 // static |
| 51 bool MenuItemView::allow_task_nesting_during_run_ = false; | 54 bool MenuItemView::allow_task_nesting_during_run_ = false; |
| 52 | 55 |
| 53 // static | 56 // static |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 controller_->Run(parent, NULL, this, bounds, anchor, NULL); | 160 controller_->Run(parent, NULL, this, bounds, anchor, NULL); |
| 158 } | 161 } |
| 159 | 162 |
| 160 void MenuItemView::Cancel() { | 163 void MenuItemView::Cancel() { |
| 161 if (controller_ && !canceled_) { | 164 if (controller_ && !canceled_) { |
| 162 canceled_ = true; | 165 canceled_ = true; |
| 163 controller_->Cancel(MenuController::EXIT_ALL); | 166 controller_->Cancel(MenuController::EXIT_ALL); |
| 164 } | 167 } |
| 165 } | 168 } |
| 166 | 169 |
| 170 MenuItemView* MenuItemView::AppendMenuItemImpl(int item_id, |
| 171 const std::wstring& label, |
| 172 const SkBitmap& icon, |
| 173 Type type) { |
| 174 if (!submenu_) |
| 175 CreateSubmenu(); |
| 176 if (type == SEPARATOR) { |
| 177 submenu_->AddChildView(new MenuSeparator()); |
| 178 return NULL; |
| 179 } |
| 180 MenuItemView* item = new MenuItemView(this, item_id, type); |
| 181 if (label.empty() && GetDelegate()) |
| 182 item->SetTitle(GetDelegate()->GetLabel(item_id)); |
| 183 else |
| 184 item->SetTitle(label); |
| 185 item->SetIcon(icon); |
| 186 if (type == SUBMENU) |
| 187 item->CreateSubmenu(); |
| 188 submenu_->AddChildView(item); |
| 189 return item; |
| 190 } |
| 191 |
| 167 SubmenuView* MenuItemView::CreateSubmenu() { | 192 SubmenuView* MenuItemView::CreateSubmenu() { |
| 168 if (!submenu_) | 193 if (!submenu_) |
| 169 submenu_ = new SubmenuView(this); | 194 submenu_ = new SubmenuView(this); |
| 170 return submenu_; | 195 return submenu_; |
| 171 } | 196 } |
| 172 | 197 |
| 173 void MenuItemView::SetSelected(bool selected) { | 198 void MenuItemView::SetSelected(bool selected) { |
| 174 selected_ = selected; | 199 selected_ = selected; |
| 175 SchedulePaint(); | 200 SchedulePaint(); |
| 176 } | 201 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 290 |
| 266 if (submenu_) { | 291 if (submenu_) { |
| 267 // Force a paint and layout. This handles the case of the top level window's | 292 // Force a paint and layout. This handles the case of the top level window's |
| 268 // size remaining the same, resulting in no change to the submenu's size and | 293 // size remaining the same, resulting in no change to the submenu's size and |
| 269 // no layout. | 294 // no layout. |
| 270 submenu_->Layout(); | 295 submenu_->Layout(); |
| 271 submenu_->SchedulePaint(); | 296 submenu_->SchedulePaint(); |
| 272 } | 297 } |
| 273 } | 298 } |
| 274 | 299 |
| 300 void MenuItemView::Layout() { |
| 301 int child_count = GetChildViewCount(); |
| 302 if (child_count == 0) |
| 303 return; |
| 304 |
| 305 // Child views are layed out right aligned and given the full height. To right |
| 306 // align start with the last view and progress to the first. |
| 307 for (int i = child_count - 1, x = width() - item_right_margin_; i >= 0; --i) { |
| 308 View* child = GetChildViewAt(i); |
| 309 int width = child->GetPreferredSize().width(); |
| 310 child->SetBounds(x - width, 0, width, height()); |
| 311 x -= width - kChildXPadding; |
| 312 } |
| 313 } |
| 314 |
| 275 MenuItemView::MenuItemView(MenuItemView* parent, | 315 MenuItemView::MenuItemView(MenuItemView* parent, |
| 276 int command, | 316 int command, |
| 277 MenuItemView::Type type) { | 317 MenuItemView::Type type) { |
| 278 Init(parent, command, type, NULL); | 318 Init(parent, command, type, NULL); |
| 279 } | 319 } |
| 280 | 320 |
| 281 // Calculates all sizes that we can from the OS. | 321 // Calculates all sizes that we can from the OS. |
| 282 // | 322 // |
| 283 // This is invoked prior to Running a menu. | 323 // This is invoked prior to Running a menu. |
| 284 void MenuItemView::UpdateMenuPartSizes(bool has_icons) { | 324 void MenuItemView::UpdateMenuPartSizes(bool has_icons) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 submenu_ = NULL; | 358 submenu_ = NULL; |
| 319 // Assign our ID, this allows SubmenuItemView to find MenuItemViews. | 359 // Assign our ID, this allows SubmenuItemView to find MenuItemViews. |
| 320 SetID(kMenuItemViewID); | 360 SetID(kMenuItemViewID); |
| 321 has_icons_ = false; | 361 has_icons_ = false; |
| 322 | 362 |
| 323 MenuDelegate* root_delegate = GetDelegate(); | 363 MenuDelegate* root_delegate = GetDelegate(); |
| 324 if (root_delegate) | 364 if (root_delegate) |
| 325 SetEnabled(root_delegate->IsCommandEnabled(command)); | 365 SetEnabled(root_delegate->IsCommandEnabled(command)); |
| 326 } | 366 } |
| 327 | 367 |
| 328 MenuItemView* MenuItemView::AppendMenuItemInternal(int item_id, | |
| 329 const std::wstring& label, | |
| 330 const SkBitmap& icon, | |
| 331 Type type) { | |
| 332 if (!submenu_) | |
| 333 CreateSubmenu(); | |
| 334 if (type == SEPARATOR) { | |
| 335 submenu_->AddChildView(new MenuSeparator()); | |
| 336 return NULL; | |
| 337 } | |
| 338 MenuItemView* item = new MenuItemView(this, item_id, type); | |
| 339 if (label.empty() && GetDelegate()) | |
| 340 item->SetTitle(GetDelegate()->GetLabel(item_id)); | |
| 341 else | |
| 342 item->SetTitle(label); | |
| 343 item->SetIcon(icon); | |
| 344 if (type == SUBMENU) | |
| 345 item->CreateSubmenu(); | |
| 346 submenu_->AddChildView(item); | |
| 347 return item; | |
| 348 } | |
| 349 | |
| 350 void MenuItemView::DropMenuClosed(bool notify_delegate) { | 368 void MenuItemView::DropMenuClosed(bool notify_delegate) { |
| 351 DCHECK(controller_); | 369 DCHECK(controller_); |
| 352 DCHECK(!controller_->IsBlockingRun()); | 370 DCHECK(!controller_->IsBlockingRun()); |
| 353 if (MenuController::GetActiveInstance() == controller_) | 371 if (MenuController::GetActiveInstance() == controller_) |
| 354 MenuController::SetActiveInstance(NULL); | 372 MenuController::SetActiveInstance(NULL); |
| 355 delete controller_; | 373 delete controller_; |
| 356 controller_ = NULL; | 374 controller_ = NULL; |
| 357 | 375 |
| 358 RemoveEmptyMenus(); | 376 RemoveEmptyMenus(); |
| 359 | 377 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 } else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { | 445 } else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { |
| 428 submenu_->RemoveChildView(child); | 446 submenu_->RemoveChildView(child); |
| 429 } | 447 } |
| 430 } | 448 } |
| 431 } | 449 } |
| 432 | 450 |
| 433 void MenuItemView::AdjustBoundsForRTLUI(gfx::Rect* rect) const { | 451 void MenuItemView::AdjustBoundsForRTLUI(gfx::Rect* rect) const { |
| 434 rect->set_x(MirroredLeftPointForRect(*rect)); | 452 rect->set_x(MirroredLeftPointForRect(*rect)); |
| 435 } | 453 } |
| 436 | 454 |
| 437 | |
| 438 void MenuItemView::DestroyAllMenuHosts() { | 455 void MenuItemView::DestroyAllMenuHosts() { |
| 439 if (!HasSubmenu()) | 456 if (!HasSubmenu()) |
| 440 return; | 457 return; |
| 441 | 458 |
| 442 submenu_->Close(); | 459 submenu_->Close(); |
| 443 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; | 460 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; |
| 444 ++i) { | 461 ++i) { |
| 445 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); | 462 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); |
| 446 } | 463 } |
| 447 } | 464 } |
| 448 | 465 |
| 449 int MenuItemView::GetTopMargin() { | 466 int MenuItemView::GetTopMargin() { |
| 450 MenuItemView* root = GetRootMenuItem(); | 467 MenuItemView* root = GetRootMenuItem(); |
| 451 return root && root->has_icons_ | 468 return root && root->has_icons_ |
| 452 ? MenuConfig::instance().item_top_margin : | 469 ? MenuConfig::instance().item_top_margin : |
| 453 MenuConfig::instance().item_no_icon_top_margin; | 470 MenuConfig::instance().item_no_icon_top_margin; |
| 454 } | 471 } |
| 455 | 472 |
| 456 int MenuItemView::GetBottomMargin() { | 473 int MenuItemView::GetBottomMargin() { |
| 457 MenuItemView* root = GetRootMenuItem(); | 474 MenuItemView* root = GetRootMenuItem(); |
| 458 return root && root->has_icons_ | 475 return root && root->has_icons_ |
| 459 ? MenuConfig::instance().item_bottom_margin : | 476 ? MenuConfig::instance().item_bottom_margin : |
| 460 MenuConfig::instance().item_no_icon_bottom_margin; | 477 MenuConfig::instance().item_no_icon_bottom_margin; |
| 461 } | 478 } |
| 462 | 479 |
| 480 int MenuItemView::GetChildPreferredWidth() { |
| 481 int child_count = GetChildViewCount(); |
| 482 if (child_count == 0) |
| 483 return 0; |
| 484 |
| 485 int width = 0; |
| 486 for (int i = 0; i < child_count; ++i) { |
| 487 if (i) |
| 488 width += kChildXPadding; |
| 489 width += GetChildViewAt(i)->GetPreferredSize().width(); |
| 490 } |
| 491 return width; |
| 492 } |
| 493 |
| 463 } // namespace views | 494 } // namespace views |
| OLD | NEW |