summaryrefslogtreecommitdiffstats
path: root/views/controls/menu/menu_host_gtk.cc
blob: c3d5d1f8f3dd87b12cacd7d9b5518fb0d2887d39 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2010 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_gtk.h"

#include <gdk/gdk.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 {

// static
MenuHost* MenuHost::Create(SubmenuView* submenu_view) {
  return new MenuHostGtk(submenu_view);
}

MenuHostGtk::MenuHostGtk(SubmenuView* submenu)
    : WidgetGtk(WidgetGtk::TYPE_POPUP),
      destroying_(false),
      submenu_(submenu),
      did_pointer_grab_(false) {
  GdkEvent* event = gtk_get_current_event();
  if (event) {
    if (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS ||
        event->type == GDK_3BUTTON_PRESS) {
      set_mouse_down(true);
    }
    gdk_event_free(event);
  }
}

MenuHostGtk::~MenuHostGtk() {
}

void MenuHostGtk::Init(gfx::NativeWindow parent,
                    const gfx::Rect& bounds,
                    View* contents_view,
                    bool do_capture) {
  make_transient_to_parent();
  WidgetGtk::Init(GTK_WIDGET(parent), bounds);
  // Make sure we get destroyed when the parent is destroyed.
  gtk_window_set_destroy_with_parent(GTK_WINDOW(GetNativeView()), TRUE);
  gtk_window_set_type_hint(GTK_WINDOW(GetNativeView()),
                           GDK_WINDOW_TYPE_HINT_MENU);
  SetContentsView(contents_view);
  ShowMenuHost(do_capture);
}

bool MenuHostGtk::IsMenuHostVisible() {
  return IsVisible();
}

void MenuHostGtk::ShowMenuHost(bool do_capture) {
  WidgetGtk::Show();
  if (do_capture)
    DoCapture();
}

void MenuHostGtk::HideMenuHost() {
  // Make sure we release capture before hiding.
  ReleaseMenuHostCapture();

  WidgetGtk::Hide();
}

void MenuHostGtk::DestroyMenuHost() {
  HideMenuHost();
  destroying_ = true;
  // We use Close instead of CloseNow to delay the deletion. If this invoked
  // during a key press event, gtk still generates the release event and the
  // AcceleratorHandler will use the window in the event. If we destroy the
  // window now, it means AcceleratorHandler attempts to use a window that has
  // been destroyed.
  Close();
}

void MenuHostGtk::SetMenuHostBounds(const gfx::Rect& bounds) {
  SetBounds(bounds);
}

void MenuHostGtk::ReleaseMenuHostCapture() {
  ReleaseGrab();
}

gfx::NativeWindow MenuHostGtk::GetMenuHostWindow() {
  return GTK_WINDOW(GetNativeView());
}

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

bool MenuHostGtk::ReleaseCaptureOnMouseReleased() {
  return false;
}

void MenuHostGtk::ReleaseGrab() {
  WidgetGtk::ReleaseGrab();
  if (did_pointer_grab_) {
    did_pointer_grab_ = false;
    gdk_pointer_ungrab(GDK_CURRENT_TIME);
  }
}

void MenuHostGtk::OnDestroy(GtkWidget* object) {
  if (!destroying_) {
    // We weren't explicitly told to destroy ourselves, which means the menu was
    // deleted out from under us (the window we're parented to was closed). Tell
    // the SubmenuView to drop references to us.
    submenu_->MenuHostDestroyed();
  }
  WidgetGtk::OnDestroy(object);
}

gboolean MenuHostGtk::OnGrabBrokeEvent(GtkWidget* widget, GdkEvent* event) {
  did_pointer_grab_ = false;
  // Grab can be broken by drag & drop, other menu or screen locker.
  MenuController* menu_controller =
      submenu_->GetMenuItem()->GetMenuController();
  if (!menu_controller->drag_in_progress()) {
    menu_controller->CancelAll();
  }
  return WidgetGtk::OnGrabBrokeEvent(widget, event);
}

void MenuHostGtk::DoCapture() {
  // Release the current grab.
  GtkWidget* current_grab_window = gtk_grab_get_current();
  if (current_grab_window)
    gtk_grab_remove(current_grab_window);

  // Make sure all app mouse events are targetted at us only.
  DoGrab();

  // And do a grab.
  // NOTE: we do this to ensure we get mouse events from other apps, a grab
  // done with gtk_grab_add doesn't get events from other apps.
  GdkGrabStatus grab_status =
      gdk_pointer_grab(window_contents()->window, FALSE,
                       static_cast<GdkEventMask>(
                           GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
                           GDK_POINTER_MOTION_MASK),
                       NULL, NULL, GDK_CURRENT_TIME);
  did_pointer_grab_ = (grab_status == GDK_GRAB_SUCCESS);
  DCHECK(did_pointer_grab_);
  // need keyboard grab.
}

}  // namespace views