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