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