Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / services / native_viewport / platform_viewport_x11.cc
blobdbf7fb80d8718faecbefad341111aa6ccff66fc1
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/services/native_viewport/platform_viewport.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "mojo/services/public/cpp/input_events/lib/mojo_extended_key_event_data.h"
10 #include "ui/events/event.h"
11 #include "ui/events/event_utils.h"
12 #include "ui/events/platform/platform_event_dispatcher.h"
13 #include "ui/events/platform/platform_event_source.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/platform_window/platform_window.h"
16 #include "ui/platform_window/platform_window_delegate.h"
17 #include "ui/platform_window/x11/x11_window.h"
19 namespace mojo {
21 class PlatformViewportX11 : public PlatformViewport,
22 public ui::PlatformWindowDelegate {
23 public:
24 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) {
27 virtual ~PlatformViewportX11() {
28 // Destroy the platform-window while |this| is still alive.
29 platform_window_.reset();
32 private:
33 // Overridden from PlatformViewport:
34 virtual void Init(const gfx::Rect& bounds) OVERRIDE {
35 CHECK(!event_source_);
36 CHECK(!platform_window_);
38 event_source_ = ui::PlatformEventSource::CreateDefault();
40 platform_window_.reset(new ui::X11Window(this));
41 platform_window_->SetBounds(bounds);
44 virtual void Show() OVERRIDE {
45 platform_window_->Show();
48 virtual void Hide() OVERRIDE {
49 platform_window_->Hide();
52 virtual void Close() OVERRIDE {
53 platform_window_->Close();
56 virtual gfx::Size GetSize() OVERRIDE {
57 return bounds_.size();
60 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
61 platform_window_->SetBounds(bounds);
64 virtual void SetCapture() OVERRIDE {
65 platform_window_->SetCapture();
68 virtual void ReleaseCapture() OVERRIDE {
69 platform_window_->ReleaseCapture();
72 // ui::PlatformWindowDelegate:
73 virtual void OnBoundsChanged(const gfx::Rect& new_bounds) OVERRIDE {
74 bounds_ = new_bounds;
75 delegate_->OnBoundsChanged(new_bounds);
78 virtual void OnDamageRect(const gfx::Rect& damaged_region) OVERRIDE {
81 virtual void DispatchEvent(ui::Event* event) OVERRIDE {
82 delegate_->OnEvent(event);
84 // We want to emulate the WM_CHAR generation behaviour of Windows.
86 // On Linux, we've previously inserted characters by having
87 // InputMethodAuraLinux take all key down events and send a character event
88 // to the TextInputClient. This causes a mismatch in code that has to be
89 // shared between Windows and Linux, including blink code. Now that we're
90 // trying to have one way of doing things, we need to standardize on and
91 // emulate Windows character events.
93 // This is equivalent to what we're doing in the current Linux port, but
94 // done once instead of done multiple times in different places.
95 if (event->type() == ui::ET_KEY_PRESSED) {
96 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event);
97 ui::KeyEvent char_event(key_press_event->GetCharacter(),
98 key_press_event->key_code(),
99 key_press_event->flags());
101 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter());
102 DCHECK_EQ(key_press_event->key_code(), char_event.key_code());
103 DCHECK_EQ(key_press_event->flags(), char_event.flags());
105 char_event.SetExtendedKeyEventData(scoped_ptr<ui::ExtendedKeyEventData>(
106 new MojoExtendedKeyEventData(
107 ui::WindowsKeycodeFromNative(key_press_event->native_event()),
108 ui::TextFromNative(key_press_event->native_event()),
109 ui::UnmodifiedTextFromNative(key_press_event->native_event()))));
110 char_event.set_platform_keycode(key_press_event->platform_keycode());
112 delegate_->OnEvent(&char_event);
116 virtual void OnCloseRequest() OVERRIDE {
117 platform_window_->Close();
120 virtual void OnClosed() OVERRIDE {
121 delegate_->OnDestroyed();
124 virtual void OnWindowStateChanged(ui::PlatformWindowState state) OVERRIDE {
127 virtual void OnLostCapture() OVERRIDE {
130 virtual void OnAcceleratedWidgetAvailable(
131 gfx::AcceleratedWidget widget) OVERRIDE {
132 delegate_->OnAcceleratedWidgetAvailable(widget);
135 virtual void OnActivationChanged(bool active) OVERRIDE {}
137 scoped_ptr<ui::PlatformEventSource> event_source_;
138 scoped_ptr<ui::PlatformWindow> platform_window_;
139 Delegate* delegate_;
140 gfx::Rect bounds_;
142 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11);
145 // static
146 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) {
147 return scoped_ptr<PlatformViewport>(new PlatformViewportX11(delegate)).Pass();
150 } // namespace mojo