OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/native_menu_x.h" | 5 #include "views/controls/menu/native_menu_linux.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "ui/gfx/canvas_skia.h" | 9 #include "ui/gfx/canvas_skia.h" |
10 #include "ui/gfx/skia_util.h" | 10 #include "ui/gfx/skia_util.h" |
11 #include "views/controls/menu/menu_2.h" | 11 #include "views/controls/menu/menu_2.h" |
12 #include "views/controls/menu/menu_runner.h" | 12 #include "views/controls/menu/menu_runner.h" |
13 #include "views/controls/menu/submenu_view.h" | 13 #include "views/controls/menu/submenu_view.h" |
14 | 14 |
15 namespace views { | 15 namespace views { |
16 | 16 |
17 NativeMenuX::NativeMenuX(Menu2* menu) | 17 NativeMenuLinux::NativeMenuLinux(Menu2* menu) |
18 : model_(menu->model()), | 18 : model_(menu->model()), |
19 ALLOW_THIS_IN_INITIALIZER_LIST(root_(new MenuItemView(this))), | 19 ALLOW_THIS_IN_INITIALIZER_LIST(root_(new MenuItemView(this))), |
20 menu_runner_(new MenuRunner(root_)) { | 20 menu_runner_(new MenuRunner(root_)) { |
21 } | 21 } |
22 | 22 |
23 NativeMenuX::~NativeMenuX() { | 23 NativeMenuLinux::~NativeMenuLinux() { |
24 } | 24 } |
25 | 25 |
26 // MenuWrapper implementation: | 26 // MenuWrapper implementation: |
27 void NativeMenuX::RunMenuAt(const gfx::Point& point, int alignment) { | 27 void NativeMenuLinux::RunMenuAt(const gfx::Point& point, int alignment) { |
28 // TODO: this should really return the value from MenuRunner. | 28 // TODO: this should really return the value from MenuRunner. |
29 UpdateStates(); | 29 UpdateStates(); |
30 if (menu_runner_->RunMenuAt(NULL, NULL, gfx::Rect(point, gfx::Size()), | 30 if (menu_runner_->RunMenuAt(NULL, NULL, gfx::Rect(point, gfx::Size()), |
31 alignment == Menu2::ALIGN_TOPLEFT ? MenuItemView::TOPLEFT : | 31 alignment == Menu2::ALIGN_TOPLEFT ? MenuItemView::TOPLEFT : |
32 MenuItemView::TOPRIGHT, MenuRunner::HAS_MNEMONICS) == | 32 MenuItemView::TOPRIGHT, MenuRunner::HAS_MNEMONICS) == |
33 MenuRunner::MENU_DELETED) | 33 MenuRunner::MENU_DELETED) |
34 return; | 34 return; |
35 } | 35 } |
36 | 36 |
37 void NativeMenuX::CancelMenu() { | 37 void NativeMenuLinux::CancelMenu() { |
38 NOTIMPLEMENTED(); | 38 NOTIMPLEMENTED(); |
39 } | 39 } |
40 | 40 |
41 void NativeMenuX::Rebuild() { | 41 void NativeMenuLinux::Rebuild() { |
42 if (SubmenuView* submenu = root_->GetSubmenu()) | 42 if (SubmenuView* submenu = root_->GetSubmenu()) |
43 submenu->RemoveAllChildViews(true); | 43 submenu->RemoveAllChildViews(true); |
44 AddMenuItemsFromModel(root_, model_); | 44 AddMenuItemsFromModel(root_, model_); |
45 } | 45 } |
46 | 46 |
47 void NativeMenuX::UpdateStates() { | 47 void NativeMenuLinux::UpdateStates() { |
48 SubmenuView* submenu = root_->CreateSubmenu(); | 48 SubmenuView* submenu = root_->CreateSubmenu(); |
49 UpdateMenuFromModel(submenu, model_); | 49 UpdateMenuFromModel(submenu, model_); |
50 } | 50 } |
51 | 51 |
52 gfx::NativeMenu NativeMenuX::GetNativeMenu() const { | 52 gfx::NativeMenu NativeMenuLinux::GetNativeMenu() const { |
53 NOTIMPLEMENTED(); | 53 NOTIMPLEMENTED(); |
54 return NULL; | 54 return NULL; |
55 } | 55 } |
56 | 56 |
57 MenuWrapper::MenuAction NativeMenuX::GetMenuAction() const { | 57 MenuWrapper::MenuAction NativeMenuLinux::GetMenuAction() const { |
58 NOTIMPLEMENTED(); | 58 NOTIMPLEMENTED(); |
59 return MENU_ACTION_NONE; | 59 return MENU_ACTION_NONE; |
60 } | 60 } |
61 | 61 |
62 void NativeMenuX::AddMenuListener(MenuListener* listener) { | 62 void NativeMenuLinux::AddMenuListener(MenuListener* listener) { |
63 NOTIMPLEMENTED(); | 63 NOTIMPLEMENTED(); |
64 } | 64 } |
65 | 65 |
66 void NativeMenuX::RemoveMenuListener(MenuListener* listener) { | 66 void NativeMenuLinux::RemoveMenuListener(MenuListener* listener) { |
67 NOTIMPLEMENTED(); | 67 NOTIMPLEMENTED(); |
68 } | 68 } |
69 | 69 |
70 void NativeMenuX::SetMinimumWidth(int width) { | 70 void NativeMenuLinux::SetMinimumWidth(int width) { |
71 NOTIMPLEMENTED(); | 71 NOTIMPLEMENTED(); |
72 } | 72 } |
73 | 73 |
74 // MenuDelegate implementation | 74 // MenuDelegate implementation |
75 | 75 |
76 bool NativeMenuX::IsItemChecked(int cmd) const { | 76 bool NativeMenuLinux::IsItemChecked(int cmd) const { |
77 int index; | 77 int index; |
78 ui::MenuModel* model = model_; | 78 ui::MenuModel* model = model_; |
79 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) | 79 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) |
80 return false; | 80 return false; |
81 return model->IsItemCheckedAt(index); | 81 return model->IsItemCheckedAt(index); |
82 } | 82 } |
83 | 83 |
84 bool NativeMenuX::IsCommandEnabled(int cmd) const { | 84 bool NativeMenuLinux::IsCommandEnabled(int cmd) const { |
85 int index; | 85 int index; |
86 ui::MenuModel* model = model_; | 86 ui::MenuModel* model = model_; |
87 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) | 87 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) |
88 return false; | 88 return false; |
89 return model->IsEnabledAt(index); | 89 return model->IsEnabledAt(index); |
90 } | 90 } |
91 | 91 |
92 void NativeMenuX::ExecuteCommand(int cmd) { | 92 void NativeMenuLinux::ExecuteCommand(int cmd) { |
93 int index; | 93 int index; |
94 ui::MenuModel* model = model_; | 94 ui::MenuModel* model = model_; |
95 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) | 95 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) |
96 return; | 96 return; |
97 model->ActivatedAt(index); | 97 model->ActivatedAt(index); |
98 } | 98 } |
99 | 99 |
100 bool NativeMenuX::GetAccelerator(int id, views::Accelerator* accelerator) { | 100 bool NativeMenuLinux::GetAccelerator(int id, views::Accelerator* accelerator) { |
101 int index; | 101 int index; |
102 ui::MenuModel* model = model_; | 102 ui::MenuModel* model = model_; |
103 if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) | 103 if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) |
104 return false; | 104 return false; |
105 | 105 |
106 ui::Accelerator menu_accelerator; | 106 ui::Accelerator menu_accelerator; |
107 if (!model->GetAcceleratorAt(index, &menu_accelerator)) | 107 if (!model->GetAcceleratorAt(index, &menu_accelerator)) |
108 return false; | 108 return false; |
109 | 109 |
110 *accelerator = views::Accelerator(menu_accelerator.key_code(), | 110 *accelerator = views::Accelerator(menu_accelerator.key_code(), |
111 menu_accelerator.modifiers()); | 111 menu_accelerator.modifiers()); |
112 return true; | 112 return true; |
113 } | 113 } |
114 | 114 |
115 // private | 115 // private |
116 void NativeMenuX::AddMenuItemsFromModel(MenuItemView* parent, | 116 void NativeMenuLinux::AddMenuItemsFromModel(MenuItemView* parent, |
117 ui::MenuModel* model) { | 117 ui::MenuModel* model) { |
118 for (int i = 0; i < model->GetItemCount(); ++i) { | 118 for (int i = 0; i < model->GetItemCount(); ++i) { |
119 int index = i + model->GetFirstItemIndex(NULL); | 119 int index = i + model->GetFirstItemIndex(NULL); |
120 MenuItemView* child = parent->AppendMenuItemFromModel(model, index, | 120 MenuItemView* child = parent->AppendMenuItemFromModel(model, index, |
121 model->GetCommandIdAt(index)); | 121 model->GetCommandIdAt(index)); |
122 | 122 |
123 if (child && child->GetType() == MenuItemView::SUBMENU) { | 123 if (child && child->GetType() == MenuItemView::SUBMENU) { |
124 AddMenuItemsFromModel(child, model->GetSubmenuModelAt(index)); | 124 AddMenuItemsFromModel(child, model->GetSubmenuModelAt(index)); |
125 } | 125 } |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 void NativeMenuX::UpdateMenuFromModel(SubmenuView* menu, | 129 void NativeMenuLinux::UpdateMenuFromModel(SubmenuView* menu, |
130 ui::MenuModel* model) { | 130 ui::MenuModel* model) { |
131 for (int i = 0, sep = 0; i < model->GetItemCount(); ++i) { | 131 for (int i = 0, sep = 0; i < model->GetItemCount(); ++i) { |
132 int index = i + model->GetFirstItemIndex(NULL); | 132 int index = i + model->GetFirstItemIndex(NULL); |
133 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR) { | 133 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR) { |
134 ++sep; | 134 ++sep; |
135 continue; | 135 continue; |
136 } | 136 } |
137 | 137 |
138 // The submenu excludes the separators when counting the menu-items | 138 // The submenu excludes the separators when counting the menu-items |
139 // in it. So exclude the number of separators to get the correct index. | 139 // in it. So exclude the number of separators to get the correct index. |
140 MenuItemView* mitem = menu->GetMenuItemAt(index - sep); | 140 MenuItemView* mitem = menu->GetMenuItemAt(index - sep); |
(...skipping 14 matching lines...) Expand all Loading... |
155 DCHECK(mitem->HasSubmenu()); | 155 DCHECK(mitem->HasSubmenu()); |
156 UpdateMenuFromModel(mitem->GetSubmenu(), model->GetSubmenuModelAt(index)); | 156 UpdateMenuFromModel(mitem->GetSubmenu(), model->GetSubmenuModelAt(index)); |
157 } | 157 } |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 // MenuWrapper, public: | 161 // MenuWrapper, public: |
162 | 162 |
163 // static | 163 // static |
164 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { | 164 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { |
165 return new NativeMenuX(menu); | 165 return new NativeMenuLinux(menu); |
166 } | 166 } |
167 | 167 |
168 } // namespace views | 168 } // namespace views |
OLD | NEW |