1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include <svx/sdr/event/eventhandler.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // for SOLARIS compiler include of algorithm part of _STL is necesary to
29cdf0e10cSrcweir // get access to basic algos like ::std::find
30cdf0e10cSrcweir #include <algorithm>
31cdf0e10cSrcweir #include <tools/debug.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace sdr
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 	namespace event
38cdf0e10cSrcweir 	{
39cdf0e10cSrcweir 		BaseEvent::BaseEvent(EventHandler& rEventHandler)
40cdf0e10cSrcweir 		:	mrEventHandler(rEventHandler)
41cdf0e10cSrcweir 		{
42cdf0e10cSrcweir 			mrEventHandler.AddEvent(*this);
43cdf0e10cSrcweir 		}
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 		BaseEvent::~BaseEvent()
46cdf0e10cSrcweir 		{
47cdf0e10cSrcweir 			mrEventHandler.RemoveEvent(*this);
48cdf0e10cSrcweir 		}
49cdf0e10cSrcweir 	} // end of namespace mixer
50cdf0e10cSrcweir } // end of namespace sdr
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
53cdf0e10cSrcweir 
54cdf0e10cSrcweir namespace sdr
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	namespace event
57cdf0e10cSrcweir 	{
58cdf0e10cSrcweir 		void EventHandler::AddEvent(BaseEvent& rBaseEvent)
59cdf0e10cSrcweir 		{
60cdf0e10cSrcweir 			maVector.push_back(&rBaseEvent);
61cdf0e10cSrcweir 		}
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 		void EventHandler::RemoveEvent(BaseEvent& rBaseEvent)
64cdf0e10cSrcweir 		{
65cdf0e10cSrcweir 			if(maVector.back() == &rBaseEvent)
66cdf0e10cSrcweir 			{
67cdf0e10cSrcweir 				// the one to remove is the last, pop
68cdf0e10cSrcweir 				maVector.pop_back();
69cdf0e10cSrcweir 			}
70cdf0e10cSrcweir 			else
71cdf0e10cSrcweir 			{
72cdf0e10cSrcweir 				const BaseEventVector::iterator aFindResult = ::std::find(
73cdf0e10cSrcweir 					maVector.begin(), maVector.end(), &rBaseEvent);
74cdf0e10cSrcweir 				DBG_ASSERT(aFindResult != maVector.end(),
75cdf0e10cSrcweir 					"EventHandler::RemoveEvent: Event to be removed not found (!)");
76cdf0e10cSrcweir 				maVector.erase(aFindResult);
77cdf0e10cSrcweir 			}
78cdf0e10cSrcweir 		}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		BaseEvent* EventHandler::GetEvent()
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			if(!maVector.empty())
83cdf0e10cSrcweir 			{
84cdf0e10cSrcweir 				// get the last event, that one is fastest to be removed
85cdf0e10cSrcweir 				return maVector.back();
86cdf0e10cSrcweir 			}
87cdf0e10cSrcweir 			else
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 				return 0L;
90cdf0e10cSrcweir 			}
91cdf0e10cSrcweir 		}
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 		EventHandler::EventHandler()
94cdf0e10cSrcweir 		{
95cdf0e10cSrcweir 		}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 		EventHandler::~EventHandler()
98cdf0e10cSrcweir 		{
99cdf0e10cSrcweir 			while(!maVector.empty())
100cdf0e10cSrcweir 			{
101cdf0e10cSrcweir 				delete GetEvent();
102cdf0e10cSrcweir 			}
103cdf0e10cSrcweir 		}
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 		// Trigger and consume the events
106cdf0e10cSrcweir 		void EventHandler::ExecuteEvents()
107cdf0e10cSrcweir 		{
108cdf0e10cSrcweir             for(;;)
109cdf0e10cSrcweir             {
110cdf0e10cSrcweir                 BaseEvent* pEvent = GetEvent();
111cdf0e10cSrcweir                 if(pEvent == NULL)
112cdf0e10cSrcweir                     break;
113cdf0e10cSrcweir 				pEvent->ExecuteEvent();
114cdf0e10cSrcweir 				delete pEvent;
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 		// for control
119cdf0e10cSrcweir 		sal_Bool EventHandler::IsEmpty() const
120cdf0e10cSrcweir 		{
121cdf0e10cSrcweir 			return (0L == maVector.size());
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 	} // end of namespace mixer
124cdf0e10cSrcweir } // end of namespace sdr
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
127cdf0e10cSrcweir 
128cdf0e10cSrcweir namespace sdr
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	namespace event
131cdf0e10cSrcweir 	{
132cdf0e10cSrcweir 		TimerEventHandler::TimerEventHandler(sal_uInt32 nTimeout)
133cdf0e10cSrcweir 		{
134cdf0e10cSrcweir 			SetTimeout(nTimeout);
135cdf0e10cSrcweir 			Stop();
136cdf0e10cSrcweir 		}
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 		TimerEventHandler::~TimerEventHandler()
139cdf0e10cSrcweir 		{
140cdf0e10cSrcweir 			Stop();
141cdf0e10cSrcweir 		}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 		// The timer when it is triggered; from class Timer
144cdf0e10cSrcweir 		void TimerEventHandler::Timeout()
145cdf0e10cSrcweir 		{
146cdf0e10cSrcweir 			ExecuteEvents();
147cdf0e10cSrcweir 		}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		// reset the timer
150cdf0e10cSrcweir 		void TimerEventHandler::Restart()
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			Start();
153cdf0e10cSrcweir 		}
154cdf0e10cSrcweir 	} // end of namespace mixer
155cdf0e10cSrcweir } // end of namespace sdr
156cdf0e10cSrcweir 
157cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
158cdf0e10cSrcweir // eof
159