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/views/controls/menu/menu_controller.h" | 5 #include "ui/views/controls/menu/menu_controller.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 1867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1878 int MenuController::MenuDepth(MenuItemView* item) { | 1878 int MenuController::MenuDepth(MenuItemView* item) { |
1879 return item ? (MenuDepth(item->GetParentMenuItem()) + 1) : 0; | 1879 return item ? (MenuDepth(item->GetParentMenuItem()) + 1) : 0; |
1880 } | 1880 } |
1881 | 1881 |
1882 void MenuController::IncrementSelection(int delta) { | 1882 void MenuController::IncrementSelection(int delta) { |
1883 MenuItemView* item = pending_state_.item; | 1883 MenuItemView* item = pending_state_.item; |
1884 DCHECK(item); | 1884 DCHECK(item); |
1885 if (pending_state_.submenu_open && item->HasSubmenu() && | 1885 if (pending_state_.submenu_open && item->HasSubmenu() && |
1886 item->GetSubmenu()->IsShowing()) { | 1886 item->GetSubmenu()->IsShowing()) { |
1887 // A menu is selected and open, but none of its children are selected, | 1887 // A menu is selected and open, but none of its children are selected, |
1888 // select the first menu item. | 1888 // select the first menu item that is visible and enabled. |
1889 if (item->GetSubmenu()->GetMenuItemCount()) { | 1889 if (item->GetSubmenu()->GetMenuItemCount()) { |
1890 SetSelection(item->GetSubmenu()->GetMenuItemAt(0), SELECTION_DEFAULT); | 1890 MenuItemView* to_select = FindFirstSelectableMenuItem(item); |
| 1891 if (to_select) |
| 1892 SetSelection(to_select, SELECTION_DEFAULT); |
1891 return; | 1893 return; |
1892 } | 1894 } |
1893 } | 1895 } |
1894 | 1896 |
1895 if (item->has_children()) { | 1897 if (item->has_children()) { |
1896 CustomButton* button = GetFirstHotTrackedView(item); | 1898 CustomButton* button = GetFirstHotTrackedView(item); |
1897 if (button) { | 1899 if (button) { |
1898 button->SetHotTracked(false); | 1900 button->SetHotTracked(false); |
1899 View* to_make_hot = GetNextFocusableView(item, button, delta == 1); | 1901 View* to_make_hot = GetNextFocusableView(item, button, delta == 1); |
1900 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); | 1902 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); |
(...skipping 26 matching lines...) Expand all Loading... |
1927 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); | 1929 CustomButton* button_hot = CustomButton::AsCustomButton(to_make_hot); |
1928 if (button_hot) | 1930 if (button_hot) |
1929 button_hot->SetHotTracked(true); | 1931 button_hot->SetHotTracked(true); |
1930 break; | 1932 break; |
1931 } | 1933 } |
1932 } | 1934 } |
1933 } | 1935 } |
1934 } | 1936 } |
1935 } | 1937 } |
1936 | 1938 |
| 1939 MenuItemView* MenuController::FindFirstSelectableMenuItem( |
| 1940 MenuItemView* parent) { |
| 1941 MenuItemView* child = parent->GetSubmenu()->GetMenuItemAt(0); |
| 1942 if (!child->visible() || !child->enabled()) |
| 1943 child = FindNextSelectableMenuItem(parent, 0, 1); |
| 1944 return child; |
| 1945 } |
| 1946 |
1937 MenuItemView* MenuController::FindNextSelectableMenuItem(MenuItemView* parent, | 1947 MenuItemView* MenuController::FindNextSelectableMenuItem(MenuItemView* parent, |
1938 int index, | 1948 int index, |
1939 int delta) { | 1949 int delta) { |
1940 int start_index = index; | 1950 int start_index = index; |
1941 int parent_count = parent->GetSubmenu()->GetMenuItemCount(); | 1951 int parent_count = parent->GetSubmenu()->GetMenuItemCount(); |
1942 // Loop through the menu items skipping any invisible menus. The loop stops | 1952 // Loop through the menu items skipping any invisible menus. The loop stops |
1943 // when we wrap or find a visible child. | 1953 // when we wrap or find a visible and enabled child. |
1944 do { | 1954 do { |
1945 index = (index + delta + parent_count) % parent_count; | 1955 index = (index + delta + parent_count) % parent_count; |
1946 if (index == start_index) | 1956 if (index == start_index) |
1947 return NULL; | 1957 return NULL; |
1948 MenuItemView* child = parent->GetSubmenu()->GetMenuItemAt(index); | 1958 MenuItemView* child = parent->GetSubmenu()->GetMenuItemAt(index); |
1949 if (child->visible()) | 1959 if (child->visible() && child->enabled()) |
1950 return child; | 1960 return child; |
1951 } while (index != start_index); | 1961 } while (index != start_index); |
1952 return NULL; | 1962 return NULL; |
1953 } | 1963 } |
1954 | 1964 |
1955 void MenuController::OpenSubmenuChangeSelectionIfCan() { | 1965 void MenuController::OpenSubmenuChangeSelectionIfCan() { |
1956 MenuItemView* item = pending_state_.item; | 1966 MenuItemView* item = pending_state_.item; |
1957 if (item->HasSubmenu() && item->enabled()) { | 1967 if (!item->HasSubmenu() || !item->enabled()) |
1958 if (item->GetSubmenu()->GetMenuItemCount() > 0) { | 1968 return; |
1959 SetSelection(item->GetSubmenu()->GetMenuItemAt(0), | 1969 MenuItemView* to_select = NULL; |
1960 SELECTION_UPDATE_IMMEDIATELY); | 1970 if (item->GetSubmenu()->GetMenuItemCount() > 0) |
1961 } else { | 1971 to_select = FindFirstSelectableMenuItem(item); |
1962 // No menu items, just show the sub-menu. | 1972 if (to_select) { |
1963 SetSelection(item, SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY); | 1973 SetSelection(to_select, SELECTION_UPDATE_IMMEDIATELY); |
1964 } | 1974 return; |
1965 } | 1975 } |
| 1976 // No menu items, just show the sub-menu. |
| 1977 SetSelection(item, SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY); |
1966 } | 1978 } |
1967 | 1979 |
1968 void MenuController::CloseSubmenu() { | 1980 void MenuController::CloseSubmenu() { |
1969 MenuItemView* item = state_.item; | 1981 MenuItemView* item = state_.item; |
1970 DCHECK(item); | 1982 DCHECK(item); |
1971 if (!item->GetParentMenuItem()) | 1983 if (!item->GetParentMenuItem()) |
1972 return; | 1984 return; |
1973 if (item->HasSubmenu() && item->GetSubmenu()->IsShowing()) | 1985 if (item->HasSubmenu() && item->GetSubmenu()->IsShowing()) |
1974 SetSelection(item, SELECTION_UPDATE_IMMEDIATELY); | 1986 SetSelection(item, SELECTION_UPDATE_IMMEDIATELY); |
1975 else if (item->GetParentMenuItem()->GetParentMenuItem()) | 1987 else if (item->GetParentMenuItem()->GetParentMenuItem()) |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2328 } | 2340 } |
2329 } | 2341 } |
2330 | 2342 |
2331 gfx::Screen* MenuController::GetScreen() { | 2343 gfx::Screen* MenuController::GetScreen() { |
2332 Widget* root = owner_ ? owner_->GetTopLevelWidget() : NULL; | 2344 Widget* root = owner_ ? owner_->GetTopLevelWidget() : NULL; |
2333 return root ? gfx::Screen::GetScreenFor(root->GetNativeView()) | 2345 return root ? gfx::Screen::GetScreenFor(root->GetNativeView()) |
2334 : gfx::Screen::GetNativeScreen(); | 2346 : gfx::Screen::GetNativeScreen(); |
2335 } | 2347 } |
2336 | 2348 |
2337 } // namespace views | 2349 } // namespace views |
OLD | NEW |