Home | History | Annotate | Download | only in events
      1 // Copyright (c) 2012 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/events/event_target.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/logging.h"
     10 #include "ui/events/event.h"
     11 
     12 namespace ui {
     13 
     14 EventTarget::EventTarget()
     15     : target_handler_(NULL) {
     16 }
     17 
     18 EventTarget::~EventTarget() {
     19 }
     20 
     21 void EventTarget::ConvertEventToTarget(EventTarget* target,
     22                                        LocatedEvent* event) {
     23 }
     24 
     25 void EventTarget::AddPreTargetHandler(EventHandler* handler) {
     26   pre_target_list_.push_back(handler);
     27 }
     28 
     29 void EventTarget::PrependPreTargetHandler(EventHandler* handler) {
     30   pre_target_list_.insert(pre_target_list_.begin(), handler);
     31 }
     32 
     33 void EventTarget::RemovePreTargetHandler(EventHandler* handler) {
     34   EventHandlerList::iterator find =
     35       std::find(pre_target_list_.begin(),
     36                 pre_target_list_.end(),
     37                 handler);
     38   if (find != pre_target_list_.end())
     39     pre_target_list_.erase(find);
     40 }
     41 
     42 void EventTarget::AddPostTargetHandler(EventHandler* handler) {
     43   post_target_list_.push_back(handler);
     44 }
     45 
     46 void EventTarget::RemovePostTargetHandler(EventHandler* handler) {
     47   EventHandlerList::iterator find =
     48       std::find(post_target_list_.begin(),
     49                 post_target_list_.end(),
     50                 handler);
     51   if (find != post_target_list_.end())
     52     post_target_list_.erase(find);
     53 }
     54 
     55 bool EventTarget::IsPreTargetListEmpty() const {
     56   return pre_target_list_.empty();
     57 }
     58 
     59 void EventTarget::OnEvent(Event* event) {
     60   CHECK_EQ(this, event->target());
     61   if (target_handler_)
     62     target_handler_->OnEvent(event);
     63   else
     64     EventHandler::OnEvent(event);
     65 }
     66 
     67 void EventTarget::OnKeyEvent(KeyEvent* event) {
     68   CHECK_EQ(this, event->target());
     69   if (target_handler_)
     70     target_handler_->OnKeyEvent(event);
     71 }
     72 
     73 void EventTarget::OnMouseEvent(MouseEvent* event) {
     74   CHECK_EQ(this, event->target());
     75   if (target_handler_)
     76     target_handler_->OnMouseEvent(event);
     77 }
     78 
     79 void EventTarget::OnScrollEvent(ScrollEvent* event) {
     80   CHECK_EQ(this, event->target());
     81   if (target_handler_)
     82     target_handler_->OnScrollEvent(event);
     83 }
     84 
     85 void EventTarget::OnTouchEvent(TouchEvent* event) {
     86   CHECK_EQ(this, event->target());
     87   if (target_handler_)
     88     target_handler_->OnTouchEvent(event);
     89 }
     90 
     91 void EventTarget::OnGestureEvent(GestureEvent* event) {
     92   CHECK_EQ(this, event->target());
     93   if (target_handler_)
     94     target_handler_->OnGestureEvent(event);
     95 }
     96 
     97 void EventTarget::GetPreTargetHandlers(EventHandlerList* list) {
     98   EventTarget* target = this;
     99   while (target) {
    100     EventHandlerList::reverse_iterator it, rend;
    101     for (it = target->pre_target_list_.rbegin(),
    102             rend = target->pre_target_list_.rend();
    103         it != rend;
    104         ++it) {
    105       list->insert(list->begin(), *it);
    106     }
    107     target = target->GetParentTarget();
    108   }
    109 }
    110 
    111 void EventTarget::GetPostTargetHandlers(EventHandlerList* list) {
    112   EventTarget* target = this;
    113   while (target) {
    114     for (EventHandlerList::iterator it = target->post_target_list_.begin(),
    115         end = target->post_target_list_.end(); it != end; ++it) {
    116       list->push_back(*it);
    117     }
    118     target = target->GetParentTarget();
    119   }
    120 }
    121 
    122 }  // namespace ui
    123