15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event_target.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui { 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)EventTarget::EventTarget() 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : target_handler_(NULL) { 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)EventTarget::~EventTarget() { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void EventTarget::ConvertEventToTarget(EventTarget* target, 22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) LocatedEvent* event) { 23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)} 24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EventTarget::AddPreTargetHandler(EventHandler* handler) { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pre_target_list_.push_back(handler); 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void EventTarget::PrependPreTargetHandler(EventHandler* handler) { 30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) pre_target_list_.insert(pre_target_list_.begin(), handler); 31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EventTarget::RemovePreTargetHandler(EventHandler* handler) { 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EventHandlerList::iterator find = 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::find(pre_target_list_.begin(), 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pre_target_list_.end(), 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) handler); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (find != pre_target_list_.end()) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pre_target_list_.erase(find); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EventTarget::AddPostTargetHandler(EventHandler* handler) { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) post_target_list_.push_back(handler); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EventTarget::RemovePostTargetHandler(EventHandler* handler) { 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EventHandlerList::iterator find = 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::find(post_target_list_.begin(), 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) post_target_list_.end(), 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) handler); 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (find != post_target_list_.end()) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) post_target_list_.erase(find); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool EventTarget::IsPreTargetListEmpty() const { 564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return pre_target_list_.empty(); 574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void EventTarget::OnEvent(Event* event) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK_EQ(this, event->target()); 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (target_handler_) 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) target_handler_->OnEvent(event); 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) else 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EventHandler::OnEvent(event); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void EventTarget::OnKeyEvent(KeyEvent* event) { 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK_EQ(this, event->target()); 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (target_handler_) 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) target_handler_->OnKeyEvent(event); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void EventTarget::OnMouseEvent(MouseEvent* event) { 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK_EQ(this, event->target()); 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (target_handler_) 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) target_handler_->OnMouseEvent(event); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void EventTarget::OnScrollEvent(ScrollEvent* event) { 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK_EQ(this, event->target()); 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (target_handler_) 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) target_handler_->OnScrollEvent(event); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void EventTarget::OnTouchEvent(TouchEvent* event) { 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK_EQ(this, event->target()); 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (target_handler_) 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) target_handler_->OnTouchEvent(event); 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void EventTarget::OnGestureEvent(GestureEvent* event) { 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CHECK_EQ(this, event->target()); 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (target_handler_) 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) target_handler_->OnGestureEvent(event); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EventTarget::GetPreTargetHandlers(EventHandlerList* list) { 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EventTarget* target = this; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (target) { 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EventHandlerList::reverse_iterator it, rend; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (it = target->pre_target_list_.rbegin(), 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rend = target->pre_target_list_.rend(); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) it != rend; 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++it) { 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) list->insert(list->begin(), *it); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) target = target->GetParentTarget(); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EventTarget::GetPostTargetHandlers(EventHandlerList* list) { 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EventTarget* target = this; 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (target) { 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (EventHandlerList::iterator it = target->post_target_list_.begin(), 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) end = target->post_target_list_.end(); it != end; ++it) { 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) list->push_back(*it); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) target = target->GetParentTarget(); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace ui 123