system_menu_model_builder.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 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#include "chrome/browser/ui/views/frame/system_menu_model_builder.h" 6 7#include "base/command_line.h" 8#include "base/utf_string_conversions.h" 9#include "chrome/app/chrome_command_ids.h" 10#include "chrome/browser/ui/browser_commands.h" 11#include "chrome/browser/ui/host_desktop.h" 12#include "chrome/browser/ui/toolbar/wrench_menu_model.h" 13#include "chrome/common/chrome_switches.h" 14#include "grit/generated_resources.h" 15#include "ui/base/accelerators/accelerator.h" 16#include "ui/base/models/simple_menu_model.h" 17 18SystemMenuModelBuilder::SystemMenuModelBuilder( 19 ui::AcceleratorProvider* provider, 20 Browser* browser) 21 : menu_delegate_(provider, browser) { 22} 23 24SystemMenuModelBuilder::~SystemMenuModelBuilder() { 25} 26 27void SystemMenuModelBuilder::Init() { 28 ui::SimpleMenuModel* model = new ui::SimpleMenuModel(&menu_delegate_); 29 menu_model_.reset(model); 30 BuildMenu(model); 31#if defined(OS_WIN) 32 // On Windows with HOST_DESKTOP_TYPE_NATIVE we put the menu items in the 33 // system menu (not at the end). Doing this necessitates adding a trailing 34 // separator. 35 if (browser()->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE) 36 model->AddSeparator(ui::NORMAL_SEPARATOR); 37#endif 38} 39 40void SystemMenuModelBuilder::BuildMenu(ui::SimpleMenuModel* model) { 41 // We add the menu items in reverse order so that insertion_index never needs 42 // to change. 43 if (browser()->is_type_tabbed()) 44 BuildSystemMenuForBrowserWindow(model); 45 else 46 BuildSystemMenuForAppOrPopupWindow(model); 47 AddFrameToggleItems(model); 48} 49 50void SystemMenuModelBuilder::BuildSystemMenuForBrowserWindow( 51 ui::SimpleMenuModel* model) { 52 model->AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB); 53 model->AddItemWithStringId(IDC_RESTORE_TAB, IDS_RESTORE_TAB); 54 if (chrome::CanOpenTaskManager()) { 55 model->AddSeparator(ui::NORMAL_SEPARATOR); 56 model->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER); 57 } 58 // If it's a regular browser window with tabs, we don't add any more items, 59 // since it already has menus (Page, Chrome). 60} 61 62void SystemMenuModelBuilder::BuildSystemMenuForAppOrPopupWindow( 63 ui::SimpleMenuModel* model) { 64 model->AddItemWithStringId(IDC_BACK, IDS_CONTENT_CONTEXT_BACK); 65 model->AddItemWithStringId(IDC_FORWARD, IDS_CONTENT_CONTEXT_FORWARD); 66 model->AddItemWithStringId(IDC_RELOAD, IDS_APP_MENU_RELOAD); 67 model->AddSeparator(ui::NORMAL_SEPARATOR); 68 if (browser()->is_app()) 69 model->AddItemWithStringId(IDC_NEW_TAB, IDS_APP_MENU_NEW_WEB_PAGE); 70 else 71 model->AddItemWithStringId(IDC_SHOW_AS_TAB, IDS_SHOW_AS_TAB); 72 model->AddSeparator(ui::NORMAL_SEPARATOR); 73 model->AddItemWithStringId(IDC_CUT, IDS_CUT); 74 model->AddItemWithStringId(IDC_COPY, IDS_COPY); 75 model->AddItemWithStringId(IDC_PASTE, IDS_PASTE); 76 model->AddSeparator(ui::NORMAL_SEPARATOR); 77 model->AddItemWithStringId(IDC_FIND, IDS_FIND); 78 model->AddItemWithStringId(IDC_PRINT, IDS_PRINT); 79 zoom_menu_contents_.reset(new ZoomMenuModel(&menu_delegate_)); 80 model->AddSubMenuWithStringId(IDC_ZOOM_MENU, IDS_ZOOM_MENU, 81 zoom_menu_contents_.get()); 82 encoding_menu_contents_.reset(new EncodingMenuModel(browser())); 83 model->AddSubMenuWithStringId(IDC_ENCODING_MENU, 84 IDS_ENCODING_MENU, 85 encoding_menu_contents_.get()); 86 if (browser()->is_app() && chrome::CanOpenTaskManager()) { 87 model->AddSeparator(ui::NORMAL_SEPARATOR); 88 model->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER); 89 } 90} 91 92void SystemMenuModelBuilder::AddFrameToggleItems(ui::SimpleMenuModel* model) { 93 if (CommandLine::ForCurrentProcess()->HasSwitch( 94 switches::kDebugEnableFrameToggle)) { 95 model->AddSeparator(ui::NORMAL_SEPARATOR); 96 model->AddItem(IDC_DEBUG_FRAME_TOGGLE, ASCIIToUTF16("Toggle Frame Type")); 97 } 98} 99 100