summaryrefslogtreecommitdiffstats
path: root/views
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-15 23:49:40 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-15 23:49:40 +0000
commit9f1591d628b6b90e5bad4ad00264300188b29194 (patch)
treeb6798fafe7abf95cfc118b1406a88fd4de0d9eb4 /views
parent3f701737c41986b7d1cf7f39aa5f1aa7033e3fc7 (diff)
downloadchromium_src-9f1591d628b6b90e5bad4ad00264300188b29194.zip
chromium_src-9f1591d628b6b90e5bad4ad00264300188b29194.tar.gz
chromium_src-9f1591d628b6b90e5bad4ad00264300188b29194.tar.bz2
Upgrade BackForwardMenuModelViews to use new menu API. Also adds accelerator to the "Show Full History" item.
This requires bringing the owner-draw system for native menus over from the old code. I haven't really changed anything in it other than the format of dwItemData. This code could be improved/simplified by using gfx::Canvas more, but don't want to do it here. BUG=none TEST=make sure BackForwardMenuModel tests still pass, test the menu functionality in the toolbar. Review URL: http://codereview.chromium.org/126092 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18454 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r--views/controls/button/button_dropdown.cc33
-rw-r--r--views/controls/button/button_dropdown.h9
-rw-r--r--views/controls/menu/menu_2.h3
-rw-r--r--views/controls/menu/native_menu_win.cc166
-rw-r--r--views/controls/menu/native_menu_win.h4
5 files changed, 162 insertions, 53 deletions
diff --git a/views/controls/button/button_dropdown.cc b/views/controls/button/button_dropdown.cc
index f12e276..e59b01c 100644
--- a/views/controls/button/button_dropdown.cc
+++ b/views/controls/button/button_dropdown.cc
@@ -23,9 +23,9 @@ static const int kMenuTimerDelay = 500;
////////////////////////////////////////////////////////////////////////////////
ButtonDropDown::ButtonDropDown(ButtonListener* listener,
- Menu::Delegate* menu_delegate)
+ Menu2Model* model)
: ImageButton(listener),
- menu_delegate_(menu_delegate),
+ model_(model),
y_position_on_lbuttondown_(0),
ALLOW_THIS_IN_INITIALIZER_LIST(show_menu_factory_(this)) {
}
@@ -112,7 +112,7 @@ void ButtonDropDown::ShowContextMenu(int x, int y, bool is_mouse_gesture) {
}
void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) {
- if (menu_delegate_) {
+ if (model_) {
gfx::Rect lb = GetLocalBounds(true);
// Both the menu position and the menu anchor type change if the UI layout
@@ -122,10 +122,6 @@ void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) {
if (UILayoutIsRightToLeft())
menu_position.Offset(lb.width() - 1, 0);
- Menu::AnchorPoint anchor = Menu::TOPLEFT;
- if (UILayoutIsRightToLeft())
- anchor = Menu::TOPRIGHT;
-
View::ConvertPointToScreen(this, &menu_position);
#if defined(OS_WIN)
@@ -137,24 +133,11 @@ void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) {
if (menu_position.x() < left_bound)
menu_position.set_x(left_bound);
- scoped_ptr<Menu> menu(Menu::Create(menu_delegate_, anchor, window));
-
- // ID's for AppendMenu is 1-based because RunMenu will ignore the user
- // selection if id=0 is selected (0 = NO-OP) so we add 1 here and subtract 1
- // in the handlers above to get the actual index
- int item_count = menu_delegate_->GetItemCount();
- for (int i = 0; i < item_count; i++) {
- if (menu_delegate_->IsItemSeparator(i + 1)) {
- menu->AppendSeparator();
- } else {
- if (menu_delegate_->HasIcon(i + 1))
- menu->AppendMenuItemWithIcon(i + 1, L"", SkBitmap());
- else
- menu->AppendMenuItem(i+1, L"", Menu::NORMAL);
- }
- }
-
- menu->RunMenuAt(menu_position.x(), menu_position.y());
+ menu_.reset(new Menu2(model_));
+ Menu2::Alignment align = Menu2::ALIGN_TOPLEFT;
+ if (UILayoutIsRightToLeft())
+ align = Menu2::ALIGN_TOPLEFT;
+ menu_->RunMenuAt(menu_position, align);
// Need to explicitly clear mouse handler so that events get sent
// properly after the menu finishes running. If we don't do this, then
diff --git a/views/controls/button/button_dropdown.h b/views/controls/button/button_dropdown.h
index 4ff4b24..e0ba3e3 100644
--- a/views/controls/button/button_dropdown.h
+++ b/views/controls/button/button_dropdown.h
@@ -7,7 +7,7 @@
#include "base/task.h"
#include "views/controls/button/image_button.h"
-#include "views/controls/menu/menu.h"
+#include "views/controls/menu/menu_2.h"
namespace views {
@@ -21,7 +21,7 @@ namespace views {
////////////////////////////////////////////////////////////////////////////////
class ButtonDropDown : public ImageButton {
public:
- ButtonDropDown(ButtonListener* listener, Menu::Delegate* menu_delegate);
+ ButtonDropDown(ButtonListener* listener, Menu2Model* model);
virtual ~ButtonDropDown();
// Accessibility accessors, overridden from View.
@@ -45,8 +45,9 @@ class ButtonDropDown : public ImageButton {
// Internal function to show the dropdown menu
void ShowDropDownMenu(gfx::NativeView window);
- // Specifies who to delegate populating the menu
- Menu::Delegate* menu_delegate_;
+ // The model that populates the attached menu.
+ Menu2Model* model_;
+ scoped_ptr<Menu2> menu_;
// Y position of mouse when left mouse button is pressed
int y_position_on_lbuttondown_;
diff --git a/views/controls/menu/menu_2.h b/views/controls/menu/menu_2.h
index 68c4833..665b807 100644
--- a/views/controls/menu/menu_2.h
+++ b/views/controls/menu/menu_2.h
@@ -94,6 +94,9 @@ class Menu2Model {
// Called when the item at the specified index has been activated.
virtual void ActivatedAt(int index) = 0;
+ // Called when the menu is about to be shown.
+ virtual void MenuWillShow() {}
+
// Retrieves the model and index that contains a specific command id. Returns
// true if an item with the specified command id is found. |model| is inout,
// and specifies the model to start searching from.
diff --git a/views/controls/menu/native_menu_win.cc b/views/controls/menu/native_menu_win.cc
index c5f8331..dc07dad 100644
--- a/views/controls/menu/native_menu_win.cc
+++ b/views/controls/menu/native_menu_win.cc
@@ -4,15 +4,32 @@
#include "views/controls/menu/native_menu_win.h"
+#include "app/gfx/canvas.h"
+#include "app/gfx/font.h"
#include "app/l10n_util.h"
#include "app/l10n_util_win.h"
#include "base/logging.h"
#include "base/stl_util-inl.h"
+#include "third_party/skia/include/core/SkBitmap.h"
#include "views/accelerator.h"
#include "views/controls/menu/menu_2.h"
namespace views {
+// The width of an icon, including the pixels between the icon and
+// the item label.
+static const int kIconWidth = 23;
+// Margins between the top of the item and the label.
+static const int kItemTopMargin = 3;
+// Margins between the bottom of the item and the label.
+static const int kItemBottomMargin = 4;
+// Margins between the left of the item and the icon.
+static const int kItemLeftMargin = 4;
+// Margins between the right of the item and the label.
+static const int kItemRightMargin = 10;
+// The width for displaying the sub-menu arrow.
+static const int kArrowWidth = 10;
+
struct NativeMenuWin::ItemData {
// The Windows API requires that whoever creates the menus must own the
// strings used for labels, and keep them around for the lifetime of the
@@ -21,6 +38,12 @@ struct NativeMenuWin::ItemData {
// Someone needs to own submenus, it may as well be us.
scoped_ptr<Menu2> submenu;
+
+ // We need a pointer back to the containing menu in various circumstances.
+ NativeMenuWin* native_menu_win;
+
+ // The index of the item within the menu's model.
+ int model_index;
};
// A window that receives messages from Windows relevant to the native menu
@@ -90,6 +113,10 @@ class NativeMenuWin::MenuHostWindow {
return w_param;
}
+ NativeMenuWin::ItemData* GetItemData(ULONG_PTR item_data) {
+ return reinterpret_cast<NativeMenuWin::ItemData*>(item_data);
+ }
+
// Called when the user selects a specific item.
void OnMenuCommand(int position, HMENU menu) {
NativeMenuWin* intergoat = GetNativeMenuWinFromHMENU(menu);
@@ -108,6 +135,112 @@ class NativeMenuWin::MenuHostWindow {
GetNativeMenuWinFromHMENU(menu)->model_->HighlightChangedTo(position);
}
+ // Called by Windows to measure the size of an owner-drawn menu item.
+ void OnMeasureItem(WPARAM w_param, MEASUREITEMSTRUCT* measure_item_struct) {
+ NativeMenuWin::ItemData* data = GetItemData(measure_item_struct->itemData);
+ if (data) {
+ gfx::Font font;
+ measure_item_struct->itemWidth = font.GetStringWidth(data->label) +
+ kIconWidth + kItemLeftMargin + kItemRightMargin -
+ GetSystemMetrics(SM_CXMENUCHECK);
+ if (data->submenu.get())
+ measure_item_struct->itemWidth += kArrowWidth;
+ // If the label contains an accelerator, make room for tab.
+ if (data->label.find(L'\t') != std::wstring::npos)
+ measure_item_struct->itemWidth += font.GetStringWidth(L" ");
+ measure_item_struct->itemHeight =
+ font.height() + kItemBottomMargin + kItemTopMargin;
+ } else {
+ // Measure separator size.
+ measure_item_struct->itemHeight = GetSystemMetrics(SM_CYMENU) / 2;
+ measure_item_struct->itemWidth = 0;
+ }
+ }
+
+ // Called by Windows to paint an owner-drawn menu item.
+ void OnDrawItem(UINT w_param, DRAWITEMSTRUCT* draw_item_struct) {
+ HDC dc = draw_item_struct->hDC;
+ COLORREF prev_bg_color, prev_text_color;
+
+ // Set background color and text color
+ if (draw_item_struct->itemState & ODS_SELECTED) {
+ prev_bg_color = SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
+ prev_text_color = SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
+ } else {
+ prev_bg_color = SetBkColor(dc, GetSysColor(COLOR_MENU));
+ if (draw_item_struct->itemState & ODS_DISABLED)
+ prev_text_color = SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
+ else
+ prev_text_color = SetTextColor(dc, GetSysColor(COLOR_MENUTEXT));
+ }
+
+ if (draw_item_struct->itemData) {
+ NativeMenuWin::ItemData* data = GetItemData(draw_item_struct->itemData);
+ // Draw the background.
+ HBRUSH hbr = CreateSolidBrush(GetBkColor(dc));
+ FillRect(dc, &draw_item_struct->rcItem, hbr);
+ DeleteObject(hbr);
+
+ // Draw the label.
+ RECT rect = draw_item_struct->rcItem;
+ rect.top += kItemTopMargin;
+ // Should we add kIconWidth only when icon.width() != 0 ?
+ rect.left += kItemLeftMargin + kIconWidth;
+ rect.right -= kItemRightMargin;
+ UINT format = DT_TOP | DT_SINGLELINE;
+ // Check whether the mnemonics should be underlined.
+ BOOL underline_mnemonics;
+ SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &underline_mnemonics, 0);
+ if (!underline_mnemonics)
+ format |= DT_HIDEPREFIX;
+ gfx::Font font;
+ HGDIOBJ old_font = static_cast<HFONT>(SelectObject(dc, font.hfont()));
+ int fontsize = font.FontSize();
+
+ // If an accelerator is specified (with a tab delimiting the rest of the
+ // label from the accelerator), we have to justify the fist part on the
+ // left and the accelerator on the right.
+ // TODO(jungshik): This will break in RTL UI. Currently, he/ar use the
+ // window system UI font and will not hit here.
+ std::wstring label = data->label;
+ std::wstring accel;
+ std::wstring::size_type tab_pos = label.find(L'\t');
+ if (tab_pos != std::wstring::npos) {
+ accel = label.substr(tab_pos);
+ label = label.substr(0, tab_pos);
+ }
+ DrawTextEx(dc, const_cast<wchar_t*>(label.data()),
+ static_cast<int>(label.size()), &rect, format | DT_LEFT, NULL);
+ if (!accel.empty())
+ DrawTextEx(dc, const_cast<wchar_t*>(accel.data()),
+ static_cast<int>(accel.size()), &rect,
+ format | DT_RIGHT, NULL);
+ SelectObject(dc, old_font);
+
+ // Draw the icon after the label, otherwise it would be covered
+ // by the label.
+ SkBitmap icon;
+ if (data->native_menu_win->model_->GetIconAt(data->model_index, &icon)) {
+ gfx::Canvas canvas(icon.width(), icon.height(), false);
+ canvas.drawColor(SK_ColorBLACK, SkPorterDuff::kClear_Mode);
+ canvas.DrawBitmapInt(icon, 0, 0);
+ canvas.getTopPlatformDevice().drawToHDC(dc,
+ draw_item_struct->rcItem.left + kItemLeftMargin,
+ draw_item_struct->rcItem.top + (draw_item_struct->rcItem.bottom -
+ draw_item_struct->rcItem.top - icon.height()) / 2, NULL);
+ }
+
+ } else {
+ // Draw the separator
+ draw_item_struct->rcItem.top +=
+ (draw_item_struct->rcItem.bottom - draw_item_struct->rcItem.top) / 3;
+ DrawEdge(dc, &draw_item_struct->rcItem, EDGE_ETCHED, BF_TOP);
+ }
+
+ SetBkColor(dc, prev_bg_color);
+ SetTextColor(dc, prev_text_color);
+ }
+
bool ProcessWindowMessage(HWND window,
UINT message,
WPARAM w_param,
@@ -122,6 +255,14 @@ class NativeMenuWin::MenuHostWindow {
OnMenuSelect(LOWORD(w_param), reinterpret_cast<HMENU>(l_param));
*l_result = 0;
return true;
+ case WM_MEASUREITEM:
+ OnMeasureItem(w_param, reinterpret_cast<MEASUREITEMSTRUCT*>(l_param));
+ *l_result = 0;
+ return true;
+ case WM_DRAWITEM:
+ OnDrawItem(w_param, reinterpret_cast<DRAWITEMSTRUCT*>(l_param));
+ *l_result = 0;
+ return true;
// TODO(beng): bring over owner draw from old menu system.
}
return false;
@@ -213,7 +354,7 @@ void NativeMenuWin::UpdateStates() {
SetMenuItemLabel(menu_index, model_index,
model_->GetLabelAt(model_index));
}
- Menu2* submenu = items_.at(model_index)->submenu.get();
+ Menu2* submenu = items_[model_index]->submenu.get();
if (submenu)
submenu->UpdateStates();
}
@@ -242,9 +383,9 @@ void NativeMenuWin::AddMenuItemAt(int menu_index, int model_index) {
mii.fType = MFT_STRING;
else
mii.fType = MFT_OWNERDRAW;
- mii.dwItemData = reinterpret_cast<ULONG_PTR>(this);
ItemData* item_data = new ItemData;
+ item_data->label = std::wstring();
Menu2Model::ItemType type = model_->GetTypeAt(model_index);
if (type == Menu2Model::TYPE_SUBMENU) {
item_data->submenu.reset(new Menu2(model_->GetSubmenuModelAt(model_index)));
@@ -255,7 +396,10 @@ void NativeMenuWin::AddMenuItemAt(int menu_index, int model_index) {
mii.fType |= MFT_RADIOCHECK;
mii.wID = model_->GetCommandIdAt(model_index);
}
+ item_data->native_menu_win = this;
+ item_data->model_index = model_index;
items_.insert(items_.begin() + model_index, item_data);
+ mii.dwItemData = reinterpret_cast<ULONG_PTR>(item_data);
UpdateMenuItemInfoForString(&mii, model_index,
model_->GetLabelAt(model_index));
InsertMenuItem(menu_, menu_index, TRUE, &mii);
@@ -331,24 +475,6 @@ void NativeMenuWin::UpdateMenuItemInfoForString(
}
}
-NativeMenuWin* NativeMenuWin::GetMenuForCommandId(UINT command_id) const {
- // Menus can have nested submenus. In the views Menu system, each submenu is
- // wrapped in a NativeMenu instance, which may have a different model and
- // delegate from the parent menu. The trouble is, RunMenuAt is called on the
- // parent NativeMenuWin, and so it's not possible to assume that we can just
- // dispatch the command id returned by TrackPopupMenuEx to the parent's
- // delegate. For this reason, we stow a pointer on every menu item we create
- // to the NativeMenuWin that most closely contains it. Fortunately, Windows
- // provides GetMenuItemInfo, which can walk down the menu item tree from
- // the root |menu_| to find the data for a given item even if it's in a
- // submenu.
- MENUITEMINFO mii = {0};
- mii.cbSize = sizeof(mii);
- mii.fMask = MIIM_DATA;
- GetMenuItemInfo(menu_, command_id, FALSE, &mii);
- return reinterpret_cast<NativeMenuWin*>(mii.dwItemData);
-}
-
UINT NativeMenuWin::GetAlignmentFlags(int alignment) const {
bool rtl = l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT;
UINT alignment_flags = TPM_TOPALIGN;
diff --git a/views/controls/menu/native_menu_win.h b/views/controls/menu/native_menu_win.h
index c22313a..428f624 100644
--- a/views/controls/menu/native_menu_win.h
+++ b/views/controls/menu/native_menu_win.h
@@ -71,10 +71,6 @@ class NativeMenuWin : public MenuWrapper {
int model_index,
const std::wstring& label);
- // Returns the NativeMenuWin object that contains an item with the specified
- // command id. This function must only be called from RunMenuAt!
- NativeMenuWin* GetMenuForCommandId(UINT command_id) const;
-
// Returns the alignment flags to be passed to TrackPopupMenuEx, based on the
// supplied alignment and the UI text direction.
UINT GetAlignmentFlags(int alignment) const;