summaryrefslogtreecommitdiffstats
path: root/views/controls/menu/menu_host_win.cc
blob: d6fd655f0be91cb99cdd13da3c930636beb851fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/controls/menu/menu_host_win.h"

#include "base/win_util.h"
#include "views/controls/menu/menu_controller.h"
#include "views/controls/menu/menu_host_root_view.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/controls/menu/submenu_view.h"

namespace views {

MenuHost::MenuHost(SubmenuView* submenu)
    : closed_(false),
      submenu_(submenu),
      owns_capture_(false) {
  set_window_style(WS_POPUP);
  set_initial_class_style(
      (win_util::GetWinVersion() < win_util::WINVERSION_XP) ?
      0 : CS_DROPSHADOW);
  is_mouse_down_ =
      ((GetKeyState(VK_LBUTTON) & 0x80) ||
       (GetKeyState(VK_RBUTTON) & 0x80) ||
       (GetKeyState(VK_MBUTTON) & 0x80) ||
       (GetKeyState(VK_XBUTTON1) & 0x80) ||
       (GetKeyState(VK_XBUTTON2) & 0x80));
  // Mouse clicks shouldn't give us focus.
  set_window_ex_style(WS_EX_TOPMOST | WS_EX_NOACTIVATE);
}

void MenuHost::Init(HWND parent,
                    const gfx::Rect& bounds,
                    View* contents_view,
                    bool do_capture) {
  WidgetWin::Init(parent, bounds);
  SetContentsView(contents_view);
  Show();
  owns_capture_ = do_capture;
  if (do_capture) {
    SetCapture();
    has_capture_ = true;
#ifdef DEBUG_MENU
    DLOG(INFO) << "Doing capture";
#endif
  }
}

void MenuHost::Show() {
  // We don't want to take focus away from the hosting window.
  ShowWindow(SW_SHOWNA);
}

void MenuHost::Hide() {
  if (closed_) {
    // We're already closed, nothing to do.
    // This is invoked twice if the first time just hid us, and the second
    // time deleted Closed (deleted) us.
    return;
  }
  // The menus are freed separately, and possibly before the window is closed,
  // remove them so that View doesn't try to access deleted objects.
  static_cast<MenuHostRootView*>(GetRootView())->suspend_events();
  GetRootView()->RemoveAllChildViews(false);
  closed_ = true;
  ReleaseCapture();
  WidgetWin::Hide();
}

void MenuHost::HideWindow() {
  // Make sure we release capture before hiding.
  ReleaseCapture();
  WidgetWin::Hide();
}

void MenuHost::OnCaptureChanged(HWND hwnd) {
  WidgetWin::OnCaptureChanged(hwnd);
  owns_capture_ = false;
#ifdef DEBUG_MENU
  DLOG(INFO) << "Capture changed";
#endif
}

void MenuHost::ReleaseCapture() {
  if (owns_capture_) {
#ifdef DEBUG_MENU
    DLOG(INFO) << "released capture";
#endif
    owns_capture_ = false;
    ::ReleaseCapture();
  }
}

RootView* MenuHost::CreateRootView() {
  return new MenuHostRootView(this, submenu_);
}

void MenuHost::OnCancelMode() {
  if (!closed_) {
#ifdef DEBUG_MENU
    DLOG(INFO) << "OnCanceMode, closing menu";
#endif
    submenu_->GetMenuItem()->GetMenuController()->Cancel(true);
  }
}

// Overriden to return false, we do NOT want to release capture on mouse
// release.
bool MenuHost::ReleaseCaptureOnMouseReleased() {
  return false;
}

}  // namespace views