xref: /trunk/main/svx/source/sdr/event/eventhandler.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 #include <svx/sdr/event/eventhandler.hxx>
31 
32 // for SOLARIS compiler include of algorithm part of _STL is necesary to
33 // get access to basic algos like ::std::find
34 #include <algorithm>
35 #include <tools/debug.hxx>
36 
37 //////////////////////////////////////////////////////////////////////////////
38 
39 namespace sdr
40 {
41     namespace event
42     {
43         BaseEvent::BaseEvent(EventHandler& rEventHandler)
44         :   mrEventHandler(rEventHandler)
45         {
46             mrEventHandler.AddEvent(*this);
47         }
48 
49         BaseEvent::~BaseEvent()
50         {
51             mrEventHandler.RemoveEvent(*this);
52         }
53     } // end of namespace mixer
54 } // end of namespace sdr
55 
56 //////////////////////////////////////////////////////////////////////////////
57 
58 namespace sdr
59 {
60     namespace event
61     {
62         void EventHandler::AddEvent(BaseEvent& rBaseEvent)
63         {
64             maVector.push_back(&rBaseEvent);
65         }
66 
67         void EventHandler::RemoveEvent(BaseEvent& rBaseEvent)
68         {
69             if(maVector.back() == &rBaseEvent)
70             {
71                 // the one to remove is the last, pop
72                 maVector.pop_back();
73             }
74             else
75             {
76                 const BaseEventVector::iterator aFindResult = ::std::find(
77                     maVector.begin(), maVector.end(), &rBaseEvent);
78                 DBG_ASSERT(aFindResult != maVector.end(),
79                     "EventHandler::RemoveEvent: Event to be removed not found (!)");
80                 maVector.erase(aFindResult);
81             }
82         }
83 
84         BaseEvent* EventHandler::GetEvent()
85         {
86             if(!maVector.empty())
87             {
88                 // get the last event, that one is fastest to be removed
89                 return maVector.back();
90             }
91             else
92             {
93                 return 0L;
94             }
95         }
96 
97         EventHandler::EventHandler()
98         {
99         }
100 
101         EventHandler::~EventHandler()
102         {
103             while(!maVector.empty())
104             {
105                 delete GetEvent();
106             }
107         }
108 
109         // Trigger and consume the events
110         void EventHandler::ExecuteEvents()
111         {
112             for(;;)
113             {
114                 BaseEvent* pEvent = GetEvent();
115                 if(pEvent == NULL)
116                     break;
117                 pEvent->ExecuteEvent();
118                 delete pEvent;
119             }
120         }
121 
122         // for control
123         sal_Bool EventHandler::IsEmpty() const
124         {
125             return (0L == maVector.size());
126         }
127     } // end of namespace mixer
128 } // end of namespace sdr
129 
130 //////////////////////////////////////////////////////////////////////////////
131 
132 namespace sdr
133 {
134     namespace event
135     {
136         TimerEventHandler::TimerEventHandler(sal_uInt32 nTimeout)
137         {
138             SetTimeout(nTimeout);
139             Stop();
140         }
141 
142         TimerEventHandler::~TimerEventHandler()
143         {
144             Stop();
145         }
146 
147         // The timer when it is triggered; from class Timer
148         void TimerEventHandler::Timeout()
149         {
150             ExecuteEvents();
151         }
152 
153         // reset the timer
154         void TimerEventHandler::Restart()
155         {
156             Start();
157         }
158     } // end of namespace mixer
159 } // end of namespace sdr
160 
161 //////////////////////////////////////////////////////////////////////////////
162 // eof
163