| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/button/button_dropdown.h" | 5 #include "views/controls/button/button_dropdown.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "grit/app_strings.h" | 10 #include "grit/app_strings.h" |
| 11 #include "views/controls/menu/view_menu_delegate.h" | 11 #include "views/controls/menu/view_menu_delegate.h" |
| 12 #include "views/widget/widget.h" | 12 #include "views/widget/widget.h" |
| 13 | 13 |
| 14 namespace views { | 14 namespace views { |
| 15 | 15 |
| 16 // How long to wait before showing the menu | 16 // How long to wait before showing the menu |
| 17 static const int kMenuTimerDelay = 500; | 17 static const int kMenuTimerDelay = 500; |
| 18 | 18 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
| 20 // | 20 // |
| 21 // ButtonDropDown - constructors, destructors, initialization, cleanup | 21 // ButtonDropDown - constructors, destructors, initialization, cleanup |
| 22 // | 22 // |
| 23 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
| 24 | 24 |
| 25 ButtonDropDown::ButtonDropDown(ButtonListener* listener, | 25 ButtonDropDown::ButtonDropDown(ButtonListener* listener, |
| 26 Menu::Delegate* menu_delegate) | 26 Menu2Model* model) |
| 27 : ImageButton(listener), | 27 : ImageButton(listener), |
| 28 menu_delegate_(menu_delegate), | 28 model_(model), |
| 29 y_position_on_lbuttondown_(0), | 29 y_position_on_lbuttondown_(0), |
| 30 ALLOW_THIS_IN_INITIALIZER_LIST(show_menu_factory_(this)) { | 30 ALLOW_THIS_IN_INITIALIZER_LIST(show_menu_factory_(this)) { |
| 31 } | 31 } |
| 32 | 32 |
| 33 ButtonDropDown::~ButtonDropDown() { | 33 ButtonDropDown::~ButtonDropDown() { |
| 34 } | 34 } |
| 35 | 35 |
| 36 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
| 37 // | 37 // |
| 38 // ButtonDropDown - Events | 38 // ButtonDropDown - Events |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // NOTE: SetState() schedules a paint, but it won't occur until after the | 105 // NOTE: SetState() schedules a paint, but it won't occur until after the |
| 106 // context menu message loop has terminated, so we PaintNow() to | 106 // context menu message loop has terminated, so we PaintNow() to |
| 107 // update the appearance synchronously. | 107 // update the appearance synchronously. |
| 108 SetState(BS_PUSHED); | 108 SetState(BS_PUSHED); |
| 109 PaintNow(); | 109 PaintNow(); |
| 110 ShowDropDownMenu(GetWidget()->GetNativeView()); | 110 ShowDropDownMenu(GetWidget()->GetNativeView()); |
| 111 SetState(BS_HOT); | 111 SetState(BS_HOT); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) { | 114 void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) { |
| 115 if (menu_delegate_) { | 115 if (model_) { |
| 116 gfx::Rect lb = GetLocalBounds(true); | 116 gfx::Rect lb = GetLocalBounds(true); |
| 117 | 117 |
| 118 // Both the menu position and the menu anchor type change if the UI layout | 118 // Both the menu position and the menu anchor type change if the UI layout |
| 119 // is right-to-left. | 119 // is right-to-left. |
| 120 gfx::Point menu_position(lb.origin()); | 120 gfx::Point menu_position(lb.origin()); |
| 121 menu_position.Offset(0, lb.height() - 1); | 121 menu_position.Offset(0, lb.height() - 1); |
| 122 if (UILayoutIsRightToLeft()) | 122 if (UILayoutIsRightToLeft()) |
| 123 menu_position.Offset(lb.width() - 1, 0); | 123 menu_position.Offset(lb.width() - 1, 0); |
| 124 | 124 |
| 125 Menu::AnchorPoint anchor = Menu::TOPLEFT; | |
| 126 if (UILayoutIsRightToLeft()) | |
| 127 anchor = Menu::TOPRIGHT; | |
| 128 | |
| 129 View::ConvertPointToScreen(this, &menu_position); | 125 View::ConvertPointToScreen(this, &menu_position); |
| 130 | 126 |
| 131 #if defined(OS_WIN) | 127 #if defined(OS_WIN) |
| 132 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN); | 128 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN); |
| 133 #else | 129 #else |
| 134 int left_bound = 0; | 130 int left_bound = 0; |
| 135 NOTIMPLEMENTED(); | 131 NOTIMPLEMENTED(); |
| 136 #endif | 132 #endif |
| 137 if (menu_position.x() < left_bound) | 133 if (menu_position.x() < left_bound) |
| 138 menu_position.set_x(left_bound); | 134 menu_position.set_x(left_bound); |
| 139 | 135 |
| 140 scoped_ptr<Menu> menu(Menu::Create(menu_delegate_, anchor, window)); | 136 menu_.reset(new Menu2(model_)); |
| 141 | 137 Menu2::Alignment align = Menu2::ALIGN_TOPLEFT; |
| 142 // ID's for AppendMenu is 1-based because RunMenu will ignore the user | 138 if (UILayoutIsRightToLeft()) |
| 143 // selection if id=0 is selected (0 = NO-OP) so we add 1 here and subtract 1 | 139 align = Menu2::ALIGN_TOPLEFT; |
| 144 // in the handlers above to get the actual index | 140 menu_->RunMenuAt(menu_position, align); |
| 145 int item_count = menu_delegate_->GetItemCount(); | |
| 146 for (int i = 0; i < item_count; i++) { | |
| 147 if (menu_delegate_->IsItemSeparator(i + 1)) { | |
| 148 menu->AppendSeparator(); | |
| 149 } else { | |
| 150 if (menu_delegate_->HasIcon(i + 1)) | |
| 151 menu->AppendMenuItemWithIcon(i + 1, L"", SkBitmap()); | |
| 152 else | |
| 153 menu->AppendMenuItem(i+1, L"", Menu::NORMAL); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 menu->RunMenuAt(menu_position.x(), menu_position.y()); | |
| 158 | 141 |
| 159 // Need to explicitly clear mouse handler so that events get sent | 142 // Need to explicitly clear mouse handler so that events get sent |
| 160 // properly after the menu finishes running. If we don't do this, then | 143 // properly after the menu finishes running. If we don't do this, then |
| 161 // the first click to other parts of the UI is eaten. | 144 // the first click to other parts of the UI is eaten. |
| 162 SetMouseHandler(NULL); | 145 SetMouseHandler(NULL); |
| 163 } | 146 } |
| 164 } | 147 } |
| 165 | 148 |
| 166 //////////////////////////////////////////////////////////////////////////////// | 149 //////////////////////////////////////////////////////////////////////////////// |
| 167 // | 150 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 184 } | 167 } |
| 185 | 168 |
| 186 bool ButtonDropDown::GetAccessibleState(AccessibilityTypes::State* state) { | 169 bool ButtonDropDown::GetAccessibleState(AccessibilityTypes::State* state) { |
| 187 DCHECK(state); | 170 DCHECK(state); |
| 188 | 171 |
| 189 *state = AccessibilityTypes::STATE_HASPOPUP; | 172 *state = AccessibilityTypes::STATE_HASPOPUP; |
| 190 return true; | 173 return true; |
| 191 } | 174 } |
| 192 | 175 |
| 193 } // namespace views | 176 } // namespace views |
| OLD | NEW |