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 .
20 #include <controls/table/mousefunction.hxx>
21 #include <controls/table/tablecontrolinterface.hxx>
22 #include <controls/table/tablesort.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <vcl/ptrstyle.hxx>
34 FunctionResult
ColumnResize::handleMouseMove( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
36 Point
const aPoint
= i_event
.GetPosPixel();
38 if ( m_nResizingColumn
== COL_INVALID
)
40 // if we hit a column divider, change the mouse pointer accordingly
41 PointerStyle
aNewPointer( PointerStyle::Arrow
);
42 TableCell
const tableCell
= i_tableControl
.hitTest( aPoint
);
43 if ( ( tableCell
.nRow
== ROW_COL_HEADERS
) && ( tableCell
.eArea
== ColumnDivider
) )
45 aNewPointer
= PointerStyle::HSplit
;
47 i_tableControl
.setPointer( aNewPointer
);
49 return SkipFunction
; // TODO: is this correct?
52 ::Size
const tableSize
= i_tableControl
.getTableSizePixel();
55 PointerStyle
aNewPointer( PointerStyle::Arrow
);
56 ColumnMetrics
const columnMetrics( i_tableControl
.getColumnMetrics( m_nResizingColumn
) );
57 if ( ( aPoint
.X() > tableSize
.Width() )
58 || ( aPoint
.X() < columnMetrics
.nStartPixel
)
61 aNewPointer
= PointerStyle::NotAllowed
;
65 aNewPointer
= PointerStyle::HSplit
;
67 i_tableControl
.setPointer( aNewPointer
);
70 i_tableControl
.hideTracking();
71 i_tableControl
.showTracking(
73 Point( aPoint
.X(), 0 ),
74 Size( 1, tableSize
.Height() )
76 ShowTrackFlags::Split
| ShowTrackFlags::TrackWindow
79 return ContinueFunction
;
83 FunctionResult
ColumnResize::handleMouseDown( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
85 if ( m_nResizingColumn
!= COL_INVALID
)
87 OSL_ENSURE( false, "ColumnResize::handleMouseDown: suspicious: MouseButtonDown while still tracking?" );
88 return ContinueFunction
;
91 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
92 if ( tableCell
.nRow
== ROW_COL_HEADERS
)
94 if ( ( tableCell
.nColumn
!= COL_INVALID
)
95 && ( tableCell
.eArea
== ColumnDivider
)
98 m_nResizingColumn
= tableCell
.nColumn
;
99 i_tableControl
.captureMouse();
100 return ActivateFunction
;
108 FunctionResult
ColumnResize::handleMouseUp( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
110 if ( m_nResizingColumn
== COL_INVALID
)
113 Point
const aPoint
= i_event
.GetPosPixel();
115 i_tableControl
.hideTracking();
116 PColumnModel
const pColumn
= i_tableControl
.getModel()->getColumnModel( m_nResizingColumn
);
117 tools::Long
const maxWidthLogical
= pColumn
->getMaxWidth();
118 tools::Long
const minWidthLogical
= pColumn
->getMinWidth();
120 // new position of mouse
121 tools::Long
const requestedEnd
= aPoint
.X();
123 // old position of right border
124 tools::Long
const oldEnd
= i_tableControl
.getColumnMetrics( m_nResizingColumn
).nEndPixel
;
126 // position of left border if cursor in the to-be-resized column
127 tools::Long
const columnStart
= i_tableControl
.getColumnMetrics( m_nResizingColumn
).nStartPixel
;
128 tools::Long
const requestedWidth
= requestedEnd
- columnStart
;
129 // TODO: this is not correct, strictly: It assumes that the mouse was pressed exactly on the "end" pos,
130 // but for a while now, we have relaxed this, and allow clicking a few pixels aside, too
132 if ( requestedEnd
>= columnStart
)
134 tools::Long requestedWidthLogical
= i_tableControl
.pixelWidthToAppFont( requestedWidth
);
135 // respect column width limits
136 if ( oldEnd
> requestedEnd
)
138 // column has become smaller, check against minimum width
139 if ( ( minWidthLogical
!= 0 ) && ( requestedWidthLogical
< minWidthLogical
) )
140 requestedWidthLogical
= minWidthLogical
;
142 else if ( oldEnd
< requestedEnd
)
144 // column has become larger, check against max width
145 if ( ( maxWidthLogical
!= 0 ) && ( requestedWidthLogical
>= maxWidthLogical
) )
146 requestedWidthLogical
= maxWidthLogical
;
148 pColumn
->setWidth( requestedWidthLogical
);
149 i_tableControl
.invalidate( TableArea::All
);
152 i_tableControl
.setPointer( PointerStyle::Arrow
);
153 i_tableControl
.releaseMouse();
155 m_nResizingColumn
= COL_INVALID
;
156 return DeactivateFunction
;
163 FunctionResult
RowSelection::handleMouseMove( ITableControl
&, MouseEvent
const & )
169 FunctionResult
RowSelection::handleMouseDown( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
171 bool handled
= false;
173 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
174 if ( tableCell
.nRow
>= 0 )
176 if ( i_tableControl
.getSelEngine()->GetSelectionMode() == SelectionMode::NONE
)
178 i_tableControl
.activateCell( tableCell
.nColumn
, tableCell
.nRow
);
183 handled
= i_tableControl
.getSelEngine()->SelMouseButtonDown( i_event
);
189 return handled
? ActivateFunction
: SkipFunction
;
193 FunctionResult
RowSelection::handleMouseUp( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
195 TableCell
const tableCell
= i_tableControl
.hitTest( i_event
.GetPosPixel() );
196 if ( tableCell
.nRow
>= 0 )
198 if ( i_tableControl
.getSelEngine()->GetSelectionMode() != SelectionMode::NONE
)
200 i_tableControl
.getSelEngine()->SelMouseButtonUp( i_event
);
206 return DeactivateFunction
;
212 //= ColumnSortHandler
215 FunctionResult
ColumnSortHandler::handleMouseMove( ITableControl
&, MouseEvent
const & )
221 FunctionResult
ColumnSortHandler::handleMouseDown( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
223 if ( m_nActiveColumn
!= COL_INVALID
)
225 OSL_ENSURE( false, "ColumnSortHandler::handleMouseDown: called while already active - suspicious!" );
226 return ContinueFunction
;
229 if ( i_tableControl
.getModel()->getSortAdapter() == nullptr )
230 // no sorting support at the model
233 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
234 if ( ( tableCell
.nRow
!= ROW_COL_HEADERS
) || ( tableCell
.nColumn
< 0 ) )
237 // TODO: ensure the column header is rendered in some special way, indicating its current state
239 m_nActiveColumn
= tableCell
.nColumn
;
240 return ActivateFunction
;
244 FunctionResult
ColumnSortHandler::handleMouseUp( ITableControl
& i_tableControl
, MouseEvent
const & i_event
)
246 if ( m_nActiveColumn
== COL_INVALID
)
249 TableCell
const tableCell( i_tableControl
.hitTest( i_event
.GetPosPixel() ) );
250 if ( ( tableCell
.nRow
== ROW_COL_HEADERS
) && ( tableCell
.nColumn
== m_nActiveColumn
) )
252 ITableDataSort
* pSort
= i_tableControl
.getModel()->getSortAdapter();
253 ENSURE_OR_RETURN( pSort
!= nullptr, "ColumnSortHandler::handleMouseUp: somebody is mocking with us!", DeactivateFunction
);
254 // in handleMousButtonDown, the model claimed to have sort support ...
256 ColumnSortDirection eSortDirection
= ColumnSortAscending
;
257 ColumnSort
const aCurrentSort
= pSort
->getCurrentSortOrder();
258 if ( aCurrentSort
.nColumnPos
== m_nActiveColumn
)
259 // invert existing sort order
260 eSortDirection
= ( aCurrentSort
.eSortDirection
== ColumnSortAscending
) ? ColumnSortDescending
: ColumnSortAscending
;
262 pSort
->sortByColumn( m_nActiveColumn
, eSortDirection
);
265 m_nActiveColumn
= COL_INVALID
;
266 return DeactivateFunction
;
270 } // namespace svt::table
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */