summaryrefslogtreecommitdiffstats
path: root/views/controls/native_control_gtk.cc
blob: 5a2bed16f014dd5f9d86c102b98aa98551672853 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2011 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/native_control_gtk.h"

#include <gtk/gtk.h>

#include "base/logging.h"
#include "ui/base/accessibility/accessibility_types.h"
#include "views/focus/focus_manager.h"
#include "views/widget/widget.h"

#if defined(TOUCH_UI)
namespace {

GdkEvent* MouseButtonEvent(const views::MouseEvent& mouseev,
                           const views::NativeViewHost* source) {
  GdkEvent* event = NULL;
  switch (mouseev.type()) {
    case ui::ET_MOUSE_PRESSED:
      event = gdk_event_new(GDK_BUTTON_PRESS);
      break;
    case ui::ET_MOUSE_RELEASED:
      event = gdk_event_new(GDK_BUTTON_RELEASE);
      break;
    default:
      NOTREACHED();
      return NULL;
  }
  event->button.send_event = false;
  event->button.time = GDK_CURRENT_TIME;

  // Ideally using native_view()->window should work, but it doesn't.
  event->button.window = gdk_window_at_pointer(NULL, NULL);
  g_object_ref(event->button.window);
  event->button.x = mouseev.location().x();
  event->button.y = mouseev.location().y();

  gfx::Point point = mouseev.location();
  views::View::ConvertPointToScreen(source, &point);
  event->button.x_root = point.x();
  event->button.y_root = point.y();

  event->button.axes = NULL;

  // Ideally, this should reconstruct the state from mouseev.flags().
  GdkModifierType modifier;
  gdk_window_get_pointer(event->button.window, NULL, NULL, &modifier);
  event->button.state = modifier;

  event->button.button = (mouseev.flags() & ui::EF_LEFT_BUTTON_DOWN) ? 1 :
                         (mouseev.flags() & ui::EF_RIGHT_BUTTON_DOWN) ? 3 :
                         2;
  event->button.device = gdk_device_get_core_pointer();
  return event;
}

GdkEvent* MouseMoveEvent(const views::MouseEvent& mouseev,
                         const views::NativeViewHost* source) {
  GdkEvent* event = gdk_event_new(GDK_MOTION_NOTIFY);
  event->motion.send_event = false;
  event->motion.time = GDK_CURRENT_TIME;

  event->motion.window = source->native_view()->window;
  g_object_ref(event->motion.window);
  event->motion.x = mouseev.location().x();
  event->motion.y = mouseev.location().y();

  gfx::Point point = mouseev.location();
  views::View::ConvertPointToScreen(source, &point);
  event->motion.x_root = point.x();
  event->motion.y_root = point.y();

  event->motion.device = gdk_device_get_core_pointer();
  return event;
}

GdkEvent* MouseCrossEvent(const views::MouseEvent& mouseev,
                          const views::NativeViewHost* source) {
  GdkEvent* event = NULL;
  switch (mouseev.type()) {
    case ui::ET_MOUSE_ENTERED:
      event = gdk_event_new(GDK_ENTER_NOTIFY);
      break;
    case ui::ET_MOUSE_EXITED:
      event = gdk_event_new(GDK_LEAVE_NOTIFY);
      break;
    default:
      NOTREACHED();
      return NULL;
  }
  event->crossing.send_event = false;
  event->crossing.time = GDK_CURRENT_TIME;

  event->crossing.window = source->native_view()->window;
  g_object_ref(event->crossing.window);

  event->crossing.x = event->crossing.y = 0;
  event->crossing.x_root = event->crossing.y_root = 0;
  event->crossing.mode = GDK_CROSSING_NORMAL;
  event->crossing.detail = GDK_NOTIFY_VIRTUAL;
  event->crossing.focus = false;
  event->crossing.state = 0;

  return event;
}

}  // namespace
#endif  // defined(TOUCH_UI)

namespace views {

#if defined(TOUCH_UI)
void NativeControlGtk::FakeNativeMouseEvent(const MouseEvent& mouseev) {
  GdkEvent* event = NULL;
  switch (mouseev.type()) {
    case ui::ET_MOUSE_PRESSED:
    case ui::ET_MOUSE_RELEASED:
      event = MouseButtonEvent(mouseev, this);
      break;
    case ui::ET_MOUSE_MOVED:
      event = MouseMoveEvent(mouseev, this);
      break;
    case ui::ET_MOUSE_ENTERED:
    case ui::ET_MOUSE_EXITED:
      event = MouseCrossEvent(mouseev, this);
      break;
    default:
      NOTREACHED();
      return;
  }

  if (event) {
    // Do not gdk_event_put, since that will end up going through the
    // message-loop and turn into an infinite loop.
    gtk_widget_event(native_view(), event);
    gdk_event_free(event);
  }
}
#endif  // defined(TOUCH_UI)

NativeControlGtk::NativeControlGtk() {
}

NativeControlGtk::~NativeControlGtk() {
  if (native_view())
    gtk_widget_destroy(native_view());
}

////////////////////////////////////////////////////////////////////////////////
// NativeControlGtk, View overrides:

void NativeControlGtk::SetEnabled(bool enabled) {
  if (IsEnabled() != enabled) {
    View::SetEnabled(enabled);
    if (native_view())
      gtk_widget_set_sensitive(native_view(), IsEnabled());
  }
}

void NativeControlGtk::ViewHierarchyChanged(bool is_add, View* parent,
                                            View* child) {
  // Call the base class to hide the view if we're being removed.
  NativeViewHost::ViewHierarchyChanged(is_add, parent, child);

  if (!is_add && child == this && native_view()) {
    Detach();
  } else if (is_add && GetWidget() && !native_view()) {
    // Create the widget when we're added to a valid Widget. Many
    // controls need a parent widget to function properly.
    CreateNativeControl();
  }
}

void NativeControlGtk::VisibilityChanged(View* starting_from, bool is_visible) {
  if (!is_visible) {
    if (native_view()) {
      // We destroy the child widget when we become invisible because of the
      // performance cost of maintaining widgets that aren't currently needed.
      Detach();
      // Make sure that Detach destroyed the widget.
      DCHECK(!native_view());
    }
  } else if (!native_view()) {
    if (GetWidget())
      CreateNativeControl();
  } else {
    // The view becomes visible after native control is created.
    // Layout now.
    Layout();
  }
}

void NativeControlGtk::OnFocus() {
  DCHECK(native_view());
  gtk_widget_grab_focus(native_view());
  GetWidget()->NotifyAccessibilityEvent(
      parent(), ui::AccessibilityTypes::EVENT_FOCUS, true);
}

#if defined(TOUCH_UI)
bool NativeControlGtk::OnMousePressed(const MouseEvent& mouseev) {
  FakeNativeMouseEvent(mouseev);
  return true;
}

void NativeControlGtk::OnMouseReleased(const MouseEvent& mouseev) {
  FakeNativeMouseEvent(mouseev);
}

void NativeControlGtk::OnMouseMoved(const MouseEvent& mouseev) {
  FakeNativeMouseEvent(mouseev);
}

void NativeControlGtk::OnMouseEntered(const MouseEvent& mouseev) {
  FakeNativeMouseEvent(mouseev);
}

void NativeControlGtk::OnMouseExited(const MouseEvent& mouseev) {
  FakeNativeMouseEvent(mouseev);
}
#endif  // defined(TOUCH_UI)

void NativeControlGtk::NativeControlCreated(GtkWidget* native_control) {
  Attach(native_control);

  // Update the newly created GtkWidget with any resident enabled state.
  gtk_widget_set_sensitive(native_view(), IsEnabled());

  // Listen for focus change event to update the FocusManager focused view.
  g_signal_connect(native_control, "focus-in-event",
                   G_CALLBACK(CallFocusIn), this);
}

// static
gboolean NativeControlGtk::CallFocusIn(GtkWidget* widget,
                                       GdkEventFocus* event,
                                       NativeControlGtk* control) {
  FocusManager* focus_manager =
      FocusManager::GetFocusManagerForNativeView(widget);
  if (!focus_manager) {
    // TODO(jcampan): http://crbug.com/21378 Reenable this NOTREACHED() when the
    // options page is only based on views.
    // NOTREACHED();
    NOTIMPLEMENTED();
    return false;
  }
  focus_manager->SetFocusedView(control->focus_view());
  return false;
}

}  // namespace views