1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25 ************************************************************************/
26 
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_svtools.hxx"
29 
30 #include "svtools/table/defaultinputhandler.hxx"
31 #include "svtools/table/tablecontrolinterface.hxx"
32 
33 #include "tabledatawindow.hxx"
34 #include "mousefunction.hxx"
35 
36 #include <tools/debug.hxx>
37 #include <vcl/event.hxx>
38 #include <vcl/cursor.hxx>
39 
40 //......................................................................................................................
41 namespace svt { namespace table
42 {
43 //......................................................................................................................
44 
45     typedef ::rtl::Reference< IMouseFunction >  PMouseFunction;
46     typedef ::std::vector< PMouseFunction >     MouseFunctions;
47     struct DefaultInputHandler_Impl
48     {
49         PMouseFunction  pActiveFunction;
50         MouseFunctions  aMouseFunctions;
51     };
52 
53 	//==================================================================================================================
54 	//= DefaultInputHandler
55 	//==================================================================================================================
56 	//------------------------------------------------------------------------------------------------------------------
57     DefaultInputHandler::DefaultInputHandler()
58         :m_pImpl( new DefaultInputHandler_Impl )
59     {
60         m_pImpl->aMouseFunctions.push_back( new ColumnResize );
61         m_pImpl->aMouseFunctions.push_back( new RowSelection );
62         m_pImpl->aMouseFunctions.push_back( new ColumnSortHandler );
63     }
64 
65 	//------------------------------------------------------------------------------------------------------------------
66     DefaultInputHandler::~DefaultInputHandler()
67     {
68     }
69 
70     //------------------------------------------------------------------------------------------------------------------
71     namespace
72     {
73         bool lcl_delegateMouseEvent( DefaultInputHandler_Impl& i_impl, ITableControl& i_control, const MouseEvent& i_event,
74             FunctionResult ( IMouseFunction::*i_handlerMethod )( ITableControl&, const MouseEvent& ) )
75         {
76             if ( i_impl.pActiveFunction.is() )
77             {
78                 bool furtherHandler = false;
79                 switch ( (i_impl.pActiveFunction.get()->*i_handlerMethod)( i_control, i_event ) )
80                 {
81                 case ActivateFunction:
82                     OSL_ENSURE( false, "lcl_delegateMouseEvent: unexpected - function already *is* active!" );
83                     break;
84                 case ContinueFunction:
85                     break;
86                 case DeactivateFunction:
87                     i_impl.pActiveFunction.clear();
88                     break;
89                 case SkipFunction:
90                     furtherHandler = true;
91                     break;
92                 }
93                 if ( !furtherHandler )
94                     // handled the event
95                     return true;
96             }
97 
98             // ask all other handlers
99             bool handled = false;
100             for (   MouseFunctions::iterator handler = i_impl.aMouseFunctions.begin();
101                     ( handler != i_impl.aMouseFunctions.end() ) && !handled;
102                     ++handler
103                 )
104             {
105                 if ( *handler == i_impl.pActiveFunction )
106                     // we already invoked this function
107                     continue;
108 
109                 switch ( (handler->get()->*i_handlerMethod)( i_control, i_event ) )
110                 {
111                 case ActivateFunction:
112                     i_impl.pActiveFunction = *handler;
113                     handled = true;
114                     break;
115                 case ContinueFunction:
116                 case DeactivateFunction:
117                     OSL_ENSURE( false, "lcl_delegateMouseEvent: unexpected: inactivate handler cannot be continued or deactivated!" );
118                     break;
119                 case SkipFunction:
120                     handled = false;
121                     break;
122                 }
123             }
124             return handled;
125         }
126     }
127 
128     //------------------------------------------------------------------------------------------------------------------
129     bool DefaultInputHandler::MouseMove( ITableControl& i_tableControl, const MouseEvent& i_event )
130     {
131         return lcl_delegateMouseEvent( *m_pImpl, i_tableControl, i_event, &IMouseFunction::handleMouseMove );
132     }
133 
134 	//------------------------------------------------------------------------------------------------------------------
135     bool DefaultInputHandler::MouseButtonDown( ITableControl& i_tableControl, const MouseEvent& i_event )
136     {
137         return lcl_delegateMouseEvent( *m_pImpl, i_tableControl, i_event, &IMouseFunction::handleMouseDown );
138     }
139 
140     //------------------------------------------------------------------------------------------------------------------
141     bool DefaultInputHandler::MouseButtonUp( ITableControl& i_tableControl, const MouseEvent& i_event )
142     {
143         return lcl_delegateMouseEvent( *m_pImpl, i_tableControl, i_event, &IMouseFunction::handleMouseUp );
144     }
145 
146     //------------------------------------------------------------------------------------------------------------------
147     bool DefaultInputHandler::KeyInput( ITableControl& _rControl, const KeyEvent& rKEvt )
148     {
149         bool bHandled = false;
150 
151         const KeyCode& rKeyCode = rKEvt.GetKeyCode();
152         sal_uInt16 nKeyCode = rKeyCode.GetCode();
153 
154         struct _ActionMapEntry
155         {
156             sal_uInt16              nKeyCode;
157             sal_uInt16              nKeyModifier;
158             TableControlAction  eAction;
159         }
160         static aKnownActions[] = {
161             { KEY_DOWN,     0,          cursorDown },
162             { KEY_UP,       0,          cursorUp },
163             { KEY_LEFT,     0,          cursorLeft },
164             { KEY_RIGHT,    0,          cursorRight },
165             { KEY_HOME,     0,          cursorToLineStart },
166             { KEY_END,      0,          cursorToLineEnd },
167             { KEY_PAGEUP,   0,          cursorPageUp },
168             { KEY_PAGEDOWN, 0,          cursorPageDown },
169             { KEY_PAGEUP,   KEY_MOD1,   cursorToFirstLine },
170             { KEY_PAGEDOWN, KEY_MOD1,   cursorToLastLine },
171             { KEY_HOME,     KEY_MOD1,   cursorTopLeft },
172             { KEY_END,      KEY_MOD1,   cursorBottomRight },
173             { KEY_SPACE,    KEY_MOD1,   cursorSelectRow },
174             { KEY_UP,       KEY_SHIFT,  cursorSelectRowUp },
175             { KEY_DOWN,     KEY_SHIFT,  cursorSelectRowDown },
176             { KEY_END,      KEY_SHIFT,  cursorSelectRowAreaBottom },
177             { KEY_HOME,     KEY_SHIFT,  cursorSelectRowAreaTop },
178 
179             { 0, 0, invalidTableControlAction }
180         };
181 
182         const _ActionMapEntry* pActions = aKnownActions;
183         for ( ; pActions->eAction != invalidTableControlAction; ++pActions )
184         {
185             if ( ( pActions->nKeyCode == nKeyCode ) && ( pActions->nKeyModifier == rKeyCode.GetAllModifier() ) )
186             {
187                 bHandled = _rControl.dispatchAction( pActions->eAction );
188                 break;
189             }
190         }
191 
192         return bHandled;
193     }
194 
195 	//------------------------------------------------------------------------------------------------------------------
196     bool DefaultInputHandler::GetFocus( ITableControl& _rControl )
197     {
198         _rControl.showCursor();
199         return false;   // continue processing
200     }
201 
202 	//------------------------------------------------------------------------------------------------------------------
203     bool DefaultInputHandler::LoseFocus( ITableControl& _rControl )
204     {
205         _rControl.hideCursor();
206         return false;   // continue processing
207     }
208 
209 	//------------------------------------------------------------------------------------------------------------------
210     bool DefaultInputHandler::RequestHelp( ITableControl& _rControl, const HelpEvent& _rHEvt )
211     {
212         (void)_rControl;
213         (void)_rHEvt;
214         // TODO
215         return false;
216     }
217 
218 	//------------------------------------------------------------------------------------------------------------------
219     bool DefaultInputHandler::Command( ITableControl& _rControl, const CommandEvent& _rCEvt )
220     {
221         (void)_rControl;
222         (void)_rCEvt;
223         // TODO
224         return false;
225     }
226 
227 	//------------------------------------------------------------------------------------------------------------------
228     bool DefaultInputHandler::PreNotify( ITableControl& _rControl, NotifyEvent& _rNEvt )
229     {
230         (void)_rControl;
231         (void)_rNEvt;
232         // TODO
233         return false;
234     }
235 
236 	//------------------------------------------------------------------------------------------------------------------
237     bool DefaultInputHandler::Notify( ITableControl& _rControl, NotifyEvent& _rNEvt )
238     {
239         (void)_rControl;
240         (void)_rNEvt;
241         // TODO
242         return false;
243     }
244 //......................................................................................................................
245 } } // namespace svt::table
246 //......................................................................................................................
247