| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <controls/table/mousefunction.hxx> |
| 21 |
#include <controls/table/tablecontrolinterface.hxx> |
| 22 |
#include <controls/table/tablesort.hxx> |
| 23 |
|
| 24 |
#include <comphelper/diagnose_ex.hxx> |
| 25 |
#include <vcl/ptrstyle.hxx> |
| 26 |
|
| 27 |
namespace svt::table |
| 28 |
{ |
| 29 |
|
| 30 |
|
| 31 |
//= ColumnResize |
| 32 |
|
| 33 |
|
| 34 |
FunctionResult ColumnResize::handleMouseMove( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 35 |
{ |
| 36 |
Point const aPoint = i_event.GetPosPixel(); |
| 37 |
|
| 38 |
if ( m_nResizingColumn == COL_INVALID ) |
| 39 |
{ |
| 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 ) ) |
| 44 |
{ |
| 45 |
aNewPointer = PointerStyle::HSplit; |
| 46 |
} |
| 47 |
i_tableControl.setPointer( aNewPointer ); |
| 48 |
|
| 49 |
return SkipFunction; // TODO: is this correct? |
| 50 |
} |
| 51 |
|
| 52 |
::Size const tableSize = i_tableControl.getTableSizePixel(); |
| 53 |
|
| 54 |
// set proper pointer |
| 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 ) |
| 59 |
) |
| 60 |
{ |
| 61 |
aNewPointer = PointerStyle::NotAllowed; |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
aNewPointer = PointerStyle::HSplit; |
| 66 |
} |
| 67 |
i_tableControl.setPointer( aNewPointer ); |
| 68 |
|
| 69 |
// show tracking line |
| 70 |
i_tableControl.hideTracking(); |
| 71 |
i_tableControl.showTracking( |
| 72 |
tools::Rectangle( |
| 73 |
Point( aPoint.X(), 0 ), |
| 74 |
Size( 1, tableSize.Height() ) |
| 75 |
), |
| 76 |
ShowTrackFlags::Split | ShowTrackFlags::TrackWindow |
| 77 |
); |
| 78 |
|
| 79 |
return ContinueFunction; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
FunctionResult ColumnResize::handleMouseDown( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 84 |
{ |
| 85 |
if ( m_nResizingColumn != COL_INVALID ) |
| 86 |
{ |
| 87 |
OSL_ENSURE( false, "ColumnResize::handleMouseDown: suspicious: MouseButtonDown while still tracking?" ); |
| 88 |
return ContinueFunction; |
| 89 |
} |
| 90 |
|
| 91 |
TableCell const tableCell( i_tableControl.hitTest( i_event.GetPosPixel() ) ); |
| 92 |
if ( tableCell.nRow == ROW_COL_HEADERS ) |
| 93 |
{ |
| 94 |
if ( ( tableCell.nColumn != COL_INVALID ) |
| 95 |
&& ( tableCell.eArea == ColumnDivider ) |
| 96 |
) |
| 97 |
{ |
| 98 |
m_nResizingColumn = tableCell.nColumn; |
| 99 |
i_tableControl.captureMouse(); |
| 100 |
return ActivateFunction; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return SkipFunction; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
FunctionResult ColumnResize::handleMouseUp( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 109 |
{ |
| 110 |
if ( m_nResizingColumn == COL_INVALID ) |
| 111 |
return SkipFunction; |
| 112 |
|
| 113 |
Point const aPoint = i_event.GetPosPixel(); |
| 114 |
|
| 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(); |
| 119 |
|
| 120 |
// new position of mouse |
| 121 |
tools::Long const requestedEnd = aPoint.X(); |
| 122 |
|
| 123 |
// old position of right border |
| 124 |
tools::Long const oldEnd = i_tableControl.getColumnMetrics( m_nResizingColumn ).nEndPixel; |
| 125 |
|
| 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 |
| 131 |
|
| 132 |
if ( requestedEnd >= columnStart ) |
| 133 |
{ |
| 134 |
tools::Long requestedWidthLogical = i_tableControl.pixelWidthToAppFont( requestedWidth ); |
| 135 |
// respect column width limits |
| 136 |
if ( oldEnd > requestedEnd ) |
| 137 |
{ |
| 138 |
// column has become smaller, check against minimum width |
| 139 |
if ( ( minWidthLogical != 0 ) && ( requestedWidthLogical < minWidthLogical ) ) |
| 140 |
requestedWidthLogical = minWidthLogical; |
| 141 |
} |
| 142 |
else if ( oldEnd < requestedEnd ) |
| 143 |
{ |
| 144 |
// column has become larger, check against max width |
| 145 |
if ( ( maxWidthLogical != 0 ) && ( requestedWidthLogical >= maxWidthLogical ) ) |
| 146 |
requestedWidthLogical = maxWidthLogical; |
| 147 |
} |
| 148 |
pColumn->setWidth( requestedWidthLogical ); |
| 149 |
i_tableControl.invalidate( TableArea::All ); |
| 150 |
} |
| 151 |
|
| 152 |
i_tableControl.setPointer( PointerStyle::Arrow ); |
| 153 |
i_tableControl.releaseMouse(); |
| 154 |
|
| 155 |
m_nResizingColumn = COL_INVALID; |
| 156 |
return DeactivateFunction; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
//= RowSelection |
| 161 |
|
| 162 |
|
| 163 |
FunctionResult RowSelection::handleMouseMove( ITableControl&, MouseEvent const & ) |
| 164 |
{ |
| 165 |
return SkipFunction; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
FunctionResult RowSelection::handleMouseDown( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 170 |
{ |
| 171 |
bool handled = false; |
| 172 |
|
| 173 |
TableCell const tableCell( i_tableControl.hitTest( i_event.GetPosPixel() ) ); |
| 174 |
if ( tableCell.nRow >= 0 ) |
| 175 |
{ |
| 176 |
if ( i_tableControl.getSelEngine()->GetSelectionMode() == SelectionMode::NONE ) |
| 177 |
{ |
| 178 |
i_tableControl.activateCell( tableCell.nColumn, tableCell.nRow ); |
| 179 |
handled = true; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
handled = i_tableControl.getSelEngine()->SelMouseButtonDown( i_event ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if ( handled ) |
| 188 |
m_bActive = true; |
| 189 |
return handled ? ActivateFunction : SkipFunction; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
FunctionResult RowSelection::handleMouseUp( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 194 |
{ |
| 195 |
TableCell const tableCell = i_tableControl.hitTest( i_event.GetPosPixel() ); |
| 196 |
if ( tableCell.nRow >= 0 ) |
| 197 |
{ |
| 198 |
if ( i_tableControl.getSelEngine()->GetSelectionMode() != SelectionMode::NONE ) |
| 199 |
{ |
| 200 |
i_tableControl.getSelEngine()->SelMouseButtonUp( i_event ); |
| 201 |
} |
| 202 |
} |
| 203 |
if ( m_bActive ) |
| 204 |
{ |
| 205 |
m_bActive = false; |
| 206 |
return DeactivateFunction; |
| 207 |
} |
| 208 |
return SkipFunction; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
//= ColumnSortHandler |
| 213 |
|
| 214 |
|
| 215 |
FunctionResult ColumnSortHandler::handleMouseMove( ITableControl&, MouseEvent const & ) |
| 216 |
{ |
| 217 |
return SkipFunction; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
FunctionResult ColumnSortHandler::handleMouseDown( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 222 |
{ |
| 223 |
if ( m_nActiveColumn != COL_INVALID ) |
| 224 |
{ |
| 225 |
OSL_ENSURE( false, "ColumnSortHandler::handleMouseDown: called while already active - suspicious!" ); |
| 226 |
return ContinueFunction; |
| 227 |
} |
| 228 |
|
| 229 |
if ( i_tableControl.getModel()->getSortAdapter() == nullptr ) |
| 230 |
// no sorting support at the model |
| 231 |
return SkipFunction; |
| 232 |
|
| 233 |
TableCell const tableCell( i_tableControl.hitTest( i_event.GetPosPixel() ) ); |
| 234 |
if ( ( tableCell.nRow != ROW_COL_HEADERS ) || ( tableCell.nColumn < 0 ) ) |
| 235 |
return SkipFunction; |
| 236 |
|
| 237 |
// TODO: ensure the column header is rendered in some special way, indicating its current state |
| 238 |
|
| 239 |
m_nActiveColumn = tableCell.nColumn; |
| 240 |
return ActivateFunction; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
FunctionResult ColumnSortHandler::handleMouseUp( ITableControl& i_tableControl, MouseEvent const & i_event ) |
| 245 |
{ |
| 246 |
if ( m_nActiveColumn == COL_INVALID ) |
| 247 |
return SkipFunction; |
| 248 |
|
| 249 |
TableCell const tableCell( i_tableControl.hitTest( i_event.GetPosPixel() ) ); |
| 250 |
if ( ( tableCell.nRow == ROW_COL_HEADERS ) && ( tableCell.nColumn == m_nActiveColumn ) ) |
| 251 |
{ |
| 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 ... |
| 255 |
|
| 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; |
| 261 |
|
| 262 |
pSort->sortByColumn( m_nActiveColumn, eSortDirection ); |
| 263 |
} |
| 264 |
|
| 265 |
m_nActiveColumn = COL_INVALID; |
| 266 |
return DeactivateFunction; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
} // namespace svt::table |
| 271 |
|
| 272 |
|
| 273 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |