1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "mousefunction.hxx"
22 #include "table/tablecontrolinterface.hxx"
24 #include <tools/diagnose_ex.h>
25 #include <vcl/window.hxx>
28 namespace svt
{ namespace table
36 FunctionResult
ColumnResize::handleMouseMove( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
38 Point
const aPoint
= i_event
.GetPosPixel();
40 if ( m_nResizingColumn
== COL_INVALID
)
42 // if we hit a column divider, change the mosue pointer accordingly
43 Pointer
aNewPointer( PointerStyle::Arrow
);
44 TableCell
const tableCell
= i_tableControl
.hitTest( aPoint
);
45 if ( ( tableCell
.nRow
== ROW_COL_HEADERS
) && ( tableCell
.eArea
== ColumnDivider
) )
47 aNewPointer
= Pointer( PointerStyle::HSplit
);
49 i_tableControl
.setPointer( aNewPointer
);
51 return SkipFunction
; // TODO: is this correct?
54 ::Size
const tableSize
= i_tableControl
.getTableSizePixel();
57 Pointer
aNewPointer( PointerStyle::Arrow
);
58 ColumnMetrics
const & columnMetrics( i_tableControl
.getColumnMetrics( m_nResizingColumn
) );
59 if ( ( aPoint
.X() > tableSize
.Width() )
60 || ( aPoint
.X() < columnMetrics
.nStartPixel
)
63 aNewPointer
= Pointer( PointerStyle::NotAllowed
);
67 aNewPointer
= Pointer( PointerStyle::HSplit
);
69 i_tableControl
.setPointer( aNewPointer
);
72 i_tableControl
.hideTracking();
73 i_tableControl
.showTracking(
75 Point( aPoint
.X(), 0 ),
76 Size( 1, tableSize
.Height() )
78 SHOWTRACK_SPLIT
| SHOWTRACK_WINDOW
82 return ContinueFunction
;
86 FunctionResult
ColumnResize::handleMouseDown( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
88 if ( m_nResizingColumn
!= COL_INVALID
)
90 OSL_ENSURE( false, "ColumnResize::handleMouseDown: suspicious: MouseButtonDown while still tracking?" );
91 return ContinueFunction
;
94 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
95 if ( tableCell
.nRow
== ROW_COL_HEADERS
)
97 if ( ( tableCell
.nColumn
!= COL_INVALID
)
98 && ( tableCell
.eArea
== ColumnDivider
)
101 m_nResizingColumn
= tableCell
.nColumn
;
102 i_tableControl
.captureMouse();
103 return ActivateFunction
;
111 FunctionResult
ColumnResize::handleMouseUp( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
113 if ( m_nResizingColumn
== COL_INVALID
)
116 Point
const aPoint
= i_event
.GetPosPixel();
118 i_tableControl
.hideTracking();
119 PColumnModel
const pColumn
= i_tableControl
.getModel()->getColumnModel( m_nResizingColumn
);
120 long const maxWidthLogical
= pColumn
->getMaxWidth();
121 long const minWidthLogical
= pColumn
->getMinWidth();
123 // new position of mouse
124 long const requestedEnd
= aPoint
.X();
126 // old position of right border
127 long const oldEnd
= i_tableControl
.getColumnMetrics( m_nResizingColumn
).nEndPixel
;
129 // position of left border if cursor in the to-be-resized column
130 long const columnStart
= i_tableControl
.getColumnMetrics( m_nResizingColumn
).nStartPixel
;
131 long const requestedWidth
= requestedEnd
- columnStart
;
132 // TODO: this is not correct, strictly: It assumes that the mouse was pressed exactly on the "end" pos,
133 // but for a while now, we have relaxed this, and allow clicking a few pixels aside, too
135 if ( requestedEnd
>= columnStart
)
137 long requestedWidthLogical
= i_tableControl
.pixelWidthToAppFont( requestedWidth
);
138 // respect column width limits
139 if ( oldEnd
> requestedEnd
)
141 // column has become smaller, check against minimum width
142 if ( ( minWidthLogical
!= 0 ) && ( requestedWidthLogical
< minWidthLogical
) )
143 requestedWidthLogical
= minWidthLogical
;
145 else if ( oldEnd
< requestedEnd
)
147 // column has become larger, check against max width
148 if ( ( maxWidthLogical
!= 0 ) && ( requestedWidthLogical
>= maxWidthLogical
) )
149 requestedWidthLogical
= maxWidthLogical
;
151 pColumn
->setWidth( requestedWidthLogical
);
152 i_tableControl
.invalidate( TableAreaAll
);
155 i_tableControl
.setPointer( Pointer() );
156 i_tableControl
.releaseMouse();
158 m_nResizingColumn
= COL_INVALID
;
159 return DeactivateFunction
;
166 FunctionResult
RowSelection::handleMouseMove( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
168 OSL_UNUSED( i_tableControl
);
169 OSL_UNUSED( i_event
);
174 FunctionResult
RowSelection::handleMouseDown( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
176 bool handled
= false;
178 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
179 if ( tableCell
.nRow
>= 0 )
181 if ( i_tableControl
.getSelEngine()->GetSelectionMode() == NO_SELECTION
)
183 i_tableControl
.activateCell( tableCell
.nColumn
, tableCell
.nRow
);
188 handled
= i_tableControl
.getSelEngine()->SelMouseButtonDown( i_event
);
194 return handled
? ActivateFunction
: SkipFunction
;
198 FunctionResult
RowSelection::handleMouseUp( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
200 TableCell
const tableCell
= i_tableControl
.hitTest( i_event
.GetPosPixel() );
201 if ( tableCell
.nRow
>= 0 )
203 if ( i_tableControl
.getSelEngine()->GetSelectionMode() != NO_SELECTION
)
205 i_tableControl
.getSelEngine()->SelMouseButtonUp( i_event
);
211 return DeactivateFunction
;
217 //= ColumnSortHandler
220 FunctionResult
ColumnSortHandler::handleMouseMove( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
222 OSL_UNUSED( i_tableControl
);
223 OSL_UNUSED( i_event
);
228 FunctionResult
ColumnSortHandler::handleMouseDown( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
230 if ( m_nActiveColumn
!= COL_INVALID
)
232 OSL_ENSURE( false, "ColumnSortHandler::handleMouseDown: called while already active - suspicious!" );
233 return ContinueFunction
;
236 if ( i_tableControl
.getModel()->getSortAdapter() == NULL
)
237 // no sorting support at the model
240 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
241 if ( ( tableCell
.nRow
!= ROW_COL_HEADERS
) || ( tableCell
.nColumn
< 0 ) )
244 // TODO: ensure the column header is rendered in some special way, indicating its current state
246 m_nActiveColumn
= tableCell
.nColumn
;
247 return ActivateFunction
;
251 FunctionResult
ColumnSortHandler::handleMouseUp( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
253 if ( m_nActiveColumn
== COL_INVALID
)
256 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
257 if ( ( tableCell
.nRow
== ROW_COL_HEADERS
) && ( tableCell
.nColumn
== m_nActiveColumn
) )
259 ITableDataSort
* pSort
= i_tableControl
.getModel()->getSortAdapter();
260 ENSURE_OR_RETURN( pSort
!= NULL
, "ColumnSortHandler::handleMouseUp: somebody is mocking with us!", DeactivateFunction
);
261 // in handleMousButtonDown, the model claimed to have sort support ...
263 ColumnSortDirection eSortDirection
= ColumnSortAscending
;
264 ColumnSort
const aCurrentSort
= pSort
->getCurrentSortOrder();
265 if ( aCurrentSort
.nColumnPos
== m_nActiveColumn
)
266 // invert existing sort order
267 eSortDirection
= ( aCurrentSort
.eSortDirection
== ColumnSortAscending
) ? ColumnSortDescending
: ColumnSortAscending
;
269 pSort
->sortByColumn( m_nActiveColumn
, eSortDirection
);
272 m_nActiveColumn
= COL_INVALID
;
273 return DeactivateFunction
;
277 } } // namespace svt::table
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */