Home | History | Annotate | Download | only in views
      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.
      4 
      5 #include "ui/views/event_utils.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "ui/aura/client/screen_position_client.h"
     10 #include "ui/aura/root_window.h"
     11 #include "ui/events/event.h"
     12 #include "ui/gfx/point.h"
     13 #include "ui/views/views_delegate.h"
     14 
     15 using aura::client::ScreenPositionClient;
     16 
     17 namespace views {
     18 
     19 bool RepostLocatedEvent(gfx::NativeWindow window,
     20                         const ui::LocatedEvent& event) {
     21   if (!window)
     22     return false;
     23 
     24 #if defined(OS_WIN)
     25   if (ViewsDelegate::views_delegate &&
     26       !ViewsDelegate::views_delegate->IsWindowInMetro(window)) {
     27     return RepostLocatedEventWin(
     28         window->GetDispatcher()->host()->GetAcceleratedWidget(), event);
     29   }
     30 #endif
     31   aura::Window* root_window = window->GetRootWindow();
     32 
     33   gfx::Point root_loc(event.location());
     34   ScreenPositionClient* spc =
     35       aura::client::GetScreenPositionClient(root_window);
     36   if (!spc)
     37     return false;
     38 
     39   spc->ConvertPointFromScreen(root_window, &root_loc);
     40 
     41   scoped_ptr<ui::LocatedEvent> relocated;
     42   if (event.IsMouseEvent()) {
     43     const ui::MouseEvent& orig = static_cast<const ui::MouseEvent&>(event);
     44     relocated.reset(new ui::MouseEvent(orig));
     45   } else if (event.IsGestureEvent()) {
     46     // TODO(rbyers): Gesture event repost is tricky to get right
     47     // crbug.com/170987.
     48     return false;
     49   } else {
     50     NOTREACHED();
     51     return false;
     52   }
     53   relocated->set_location(root_loc);
     54   relocated->set_root_location(root_loc);
     55 
     56   root_window->GetDispatcher()->RepostEvent(*relocated);
     57   return true;
     58 }
     59 
     60 }  // namespace views
     61