Debian Codegraph @

toolkit/source/controls/table/tablecontrol.cxx

raw
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/tablecontrol.hxx>
21
22 #include "tablecontrol_impl.hxx"
23 #include "tabledatawindow.hxx"
24
25 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
27
28 #include <sal/log.hxx>
29 #include <comphelper/diagnose_ex.hxx>
30 #include <vcl/settings.hxx>
31 #include <vcl/vclevent.hxx>
32
33 using namespace ::com::sun::star::uno;
34 using ::com::sun::star::accessibility::XAccessible;
35 using namespace ::com::sun::star::accessibility;
36
37 namespace svt::table
38 {
39
40
41 namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
42
43
44 //= TableControl
45
46
47 TableControl::TableControl( vcl::Window* _pParent, WinBits _nStyle )
48 :Control( _pParent, _nStyle )
49 ,m_pImpl( std::make_shared<TableControl_Impl>( *this ) )
50 {
51 TableDataWindow& rDataWindow = m_pImpl->getDataWindow();
52 rDataWindow.SetSelectHdl( LINK( this, TableControl, ImplSelectHdl ) );
53
54 // by default, use the background as determined by the style settings
55 const Color aWindowColor( GetSettings().GetStyleSettings().GetFieldColor() );
56 SetBackground( Wallpaper( aWindowColor ) );
57 GetOutDev()->SetFillColor( aWindowColor );
58
59 SetCompoundControl( true );
60 }
61
62
63 TableControl::~TableControl()
64 {
65 disposeOnce();
66 }
67
68 void TableControl::dispose()
69 {
70 CallEventListeners( VclEventId::ObjectDying );
71
72 m_pImpl->setModel( PTableModel() );
73 m_pImpl->disposeAccessible();
74 m_pImpl.reset();
75 Control::dispose();
76 }
77
78
79 void TableControl::GetFocus()
80 {
81 if (m_pImpl)
82 m_pImpl->showCursor();
83
84 Control::GetFocus();
85 }
86
87
88 void TableControl::LoseFocus()
89 {
90 if (m_pImpl)
91 m_pImpl->hideCursor();
92
93 Control::LoseFocus();
94 }
95
96
97 void TableControl::KeyInput( const KeyEvent& rKEvt )
98 {
99 bool bHandled = false;
100 if (m_pImpl)
101 {
102 const vcl::KeyCode& rKeyCode = rKEvt.GetKeyCode();
103 sal_uInt16 nKeyCode = rKeyCode.GetCode();
104
105 struct ActionMapEntry
106 {
107 sal_uInt16 nKeyCode;
108 sal_uInt16 nKeyModifier;
109 TableControlAction eAction;
110 }
111 static const aKnownActions[] = {
112 { KEY_DOWN, 0, TableControlAction::cursorDown },
113 { KEY_UP, 0, TableControlAction::cursorUp },
114 { KEY_LEFT, 0, TableControlAction::cursorLeft },
115 { KEY_RIGHT, 0, TableControlAction::cursorRight },
116 { KEY_HOME, 0, TableControlAction::cursorToLineStart },
117 { KEY_END, 0, TableControlAction::cursorToLineEnd },
118 { KEY_PAGEUP, 0, TableControlAction::cursorPageUp },
119 { KEY_PAGEDOWN, 0, TableControlAction::cursorPageDown },
120 { KEY_PAGEUP, KEY_MOD1, TableControlAction::cursorToFirstLine },
121 { KEY_PAGEDOWN, KEY_MOD1, TableControlAction::cursorToLastLine },
122 { KEY_HOME, KEY_MOD1, TableControlAction::cursorTopLeft },
123 { KEY_END, KEY_MOD1, TableControlAction::cursorBottomRight },
124 { KEY_SPACE, KEY_MOD1, TableControlAction::cursorSelectRow },
125 { KEY_UP, KEY_SHIFT, TableControlAction::cursorSelectRowUp },
126 { KEY_DOWN, KEY_SHIFT, TableControlAction::cursorSelectRowDown },
127 { KEY_END, KEY_SHIFT, TableControlAction::cursorSelectRowAreaBottom },
128 { KEY_HOME, KEY_SHIFT, TableControlAction::cursorSelectRowAreaTop }
129 };
130 for (const ActionMapEntry& rAction : aKnownActions)
131 {
132 if ((rAction.nKeyCode == nKeyCode) && (rAction.nKeyModifier == rKeyCode.GetModifier()))
133 {
134 bHandled = m_pImpl->dispatchAction(rAction.eAction);
135 break;
136 }
137 }
138 }
139
140 if (!bHandled)
141 Control::KeyInput( rKEvt );
142 else
143 {
144 m_pImpl->commitCellEvent( AccessibleEventId::STATE_CHANGED,
145 Any( AccessibleStateType::FOCUSED ),
146 Any()
147 );
148 // Huh? What the heck? Why do we unconditionally notify a STATE_CHANGE/FOCUSED after each and every
149 // (handled) key stroke?
150
151 m_pImpl->commitTableEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
152 Any(),
153 Any()
154 );
155 // ditto: Why do we notify this unconditionally? We should find the right place to notify the
156 // ACTIVE_DESCENDANT_CHANGED event.
157 // Also, we should check if STATE_CHANGED/FOCUSED is really necessary: finally, the children are
158 // transient, aren't they?
159 }
160 }
161
162
163 void TableControl::StateChanged( StateChangedType i_nStateChange )
164 {
165 Control::StateChanged( i_nStateChange );
166
167 // forward certain settings to the data window
168 switch ( i_nStateChange )
169 {
170 case StateChangedType::ControlFocus:
171 m_pImpl->invalidateSelectedRows();
172 break;
173
174 case StateChangedType::ControlBackground:
175 if ( IsControlBackground() )
176 getDataWindow().SetControlBackground( GetControlBackground() );
177 else
178 getDataWindow().SetControlBackground();
179 break;
180
181 case StateChangedType::ControlForeground:
182 if ( IsControlForeground() )
183 getDataWindow().SetControlForeground( GetControlForeground() );
184 else
185 getDataWindow().SetControlForeground();
186 break;
187
188 case StateChangedType::ControlFont:
189 if ( IsControlFont() )
190 getDataWindow().SetControlFont( GetControlFont() );
191 else
192 getDataWindow().SetControlFont();
193 break;
194 default:;
195 }
196 }
197
198
199 void TableControl::Resize()
200 {
201 Control::Resize();
202 m_pImpl->onResize();
203 }
204
205
206 void TableControl::SetModel( const PTableModel& _pModel )
207 {
208 m_pImpl->setModel( _pModel );
209 }
210
211
212 PTableModel TableControl::GetModel() const
213 {
214 return m_pImpl->getModel();
215 }
216
217
218 sal_Int32 TableControl::GetCurrentRow() const
219 {
220 return m_pImpl->getCurrentRow();
221 }
222
223
224 sal_Int32 TableControl::GetCurrentColumn() const
225 {
226 return m_pImpl->getCurrentColumn();
227 }
228
229
230 void TableControl::GoTo( ColPos _nColumn, RowPos _nRow )
231 {
232 m_pImpl->goTo( _nColumn, _nRow );
233 }
234
235
236 void TableControl::GoToCell(sal_Int32 _nColPos, sal_Int32 _nRowPos)
237 {
238 m_pImpl->goTo( _nColPos, _nRowPos );
239 }
240
241
242 sal_Int32 TableControl::GetSelectedRowCount() const
243 {
244 return sal_Int32( m_pImpl->getSelectedRowCount() );
245 }
246
247
248 sal_Int32 TableControl::GetSelectedRowIndex( sal_Int32 const i_selectionIndex ) const
249 {
250 return m_pImpl->getSelectedRowIndex( i_selectionIndex );
251 }
252
253
254 bool TableControl::IsRowSelected( sal_Int32 const i_rowIndex ) const
255 {
256 return m_pImpl->isRowSelected( i_rowIndex );
257 }
258
259
260 void TableControl::SelectRow( sal_Int32 const i_rowIndex, bool const i_select )
261 {
262 ENSURE_OR_RETURN_VOID( ( i_rowIndex >= 0 ) && ( i_rowIndex < m_pImpl->getModel()->getRowCount() ),
263 "TableControl::SelectRow: invalid row index!" );
264
265 if ( i_select )
266 {
267 if ( !m_pImpl->markRowAsSelected( i_rowIndex ) )
268 // nothing to do
269 return;
270 }
271 else
272 {
273 m_pImpl->markRowAsDeselected( i_rowIndex );
274 }
275
276 m_pImpl->invalidateRowRange( i_rowIndex, i_rowIndex );
277 Select();
278 }
279
280
281 void TableControl::SelectAllRows( bool const i_select )
282 {
283 if ( i_select )
284 {
285 if ( !m_pImpl->markAllRowsAsSelected() )
286 // nothing to do
287 return;
288 }
289 else
290 {
291 if ( !m_pImpl->markAllRowsAsDeselected() )
292 // nothing to do
293 return;
294 }
295
296
297 Invalidate();
298 // TODO: can't we do better than this, and invalidate only the rows which changed?
299 Select();
300 }
301
302 SelectionEngine* TableControl::getSelEngine()
303 {
304 return m_pImpl->getSelEngine();
305 }
306
307
308 vcl::Window& TableControl::getDataWindow()
309 {
310 return m_pImpl->getDataWindow();
311 }
312
313
314 rtl::Reference<comphelper::OAccessible> TableControl::CreateAccessible()
315 {
316 css::uno::Reference<css::accessibility::XAccessible> xParent = GetAccessibleParent();
317 return m_pImpl->getAccessible(xParent);
318 }
319
320 OUString TableControl::GetAccessibleObjectName( AccessibleTableControlObjType eObjType, sal_Int32 _nRow, sal_Int32 _nCol) const
321 {
322 OUString aRetText;
323 //Window* pWin;
324 switch( eObjType )
325 {
326 case AccessibleTableControlObjType::GRIDCONTROL:
327 aRetText = "Grid control";
328 break;
329 case AccessibleTableControlObjType::TABLE:
330 aRetText = "Grid control";
331 break;
332 case AccessibleTableControlObjType::ROWHEADERBAR:
333 aRetText = "RowHeaderBar";
334 break;
335 case AccessibleTableControlObjType::COLUMNHEADERBAR:
336 aRetText = "ColumnHeaderBar";
337 break;
338 case AccessibleTableControlObjType::TABLECELL:
339 //the name of the cell consists of column name and row name if defined
340 //if the name is equal to cell content, it'll be read twice
341 if(GetModel()->hasColumnHeaders())
342 {
343 aRetText = GetColumnName(_nCol) + " , ";
344 }
345 if(GetModel()->hasRowHeaders())
346 {
347 aRetText += GetRowName(_nRow) + " , ";
348 }
349 //aRetText = GetAccessibleCellText(_nRow, _nCol);
350 break;
351 case AccessibleTableControlObjType::ROWHEADERCELL:
352 aRetText = GetRowName(_nRow);
353 break;
354 case AccessibleTableControlObjType::COLUMNHEADERCELL:
355 aRetText = GetColumnName(_nCol);
356 break;
357 default:
358 OSL_FAIL("GridControl::GetAccessibleName: invalid enum!");
359 }
360 return aRetText;
361 }
362
363
364 OUString TableControl::GetAccessibleObjectDescription( AccessibleTableControlObjType eObjType ) const
365 {
366 OUString aRetText;
367 switch( eObjType )
368 {
369 case AccessibleTableControlObjType::GRIDCONTROL:
370 aRetText = "Grid control description";
371 break;
372 case AccessibleTableControlObjType::TABLE:
373 aRetText = "TABLE description";
374 break;
375 case AccessibleTableControlObjType::ROWHEADERBAR:
376 aRetText = "ROWHEADERBAR description";
377 break;
378 case AccessibleTableControlObjType::COLUMNHEADERBAR:
379 aRetText = "COLUMNHEADERBAR description";
380 break;
381 case AccessibleTableControlObjType::TABLECELL:
382 // the description of the cell consists of column name and row name if defined
383 // if the name is equal to cell content, it'll be read twice
384 if ( GetModel()->hasColumnHeaders() )
385 {
386 aRetText = GetColumnName( GetCurrentColumn() ) + " , ";
387 }
388 if ( GetModel()->hasRowHeaders() )
389 {
390 aRetText += GetRowName( GetCurrentRow() );
391 }
392 break;
393 case AccessibleTableControlObjType::ROWHEADERCELL:
394 aRetText = "ROWHEADERCELL description";
395 break;
396 case AccessibleTableControlObjType::COLUMNHEADERCELL:
397 aRetText = "COLUMNHEADERCELL description";
398 break;
399 }
400 return aRetText;
401 }
402
403
404 OUString TableControl::GetRowName( sal_Int32 _nIndex) const
405 {
406 OUString sRowName;
407 GetModel()->getRowHeading( _nIndex ) >>= sRowName;
408 return sRowName;
409 }
410
411
412 OUString TableControl::GetColumnName( sal_Int32 _nIndex) const
413 {
414 return GetModel()->getColumnModel(_nIndex)->getName();
415 }
416
417
418 OUString TableControl::GetAccessibleCellText( sal_Int32 _nRowPos, sal_Int32 _nColPos) const
419 {
420 return m_pImpl->getCellContentAsString( _nRowPos, _nColPos );
421 }
422
423
424 void TableControl::FillAccessibleStateSet(
425 sal_Int64& rStateSet,
426 AccessibleTableControlObjType eObjType ) const
427 {
428 switch( eObjType )
429 {
430 case AccessibleTableControlObjType::GRIDCONTROL:
431 case AccessibleTableControlObjType::TABLE:
432
433 rStateSet |= AccessibleStateType::FOCUSABLE;
434
435 if ( m_pImpl->getSelEngine()->GetSelectionMode() == SelectionMode::Multiple )
436 rStateSet |= AccessibleStateType::MULTI_SELECTABLE;
437
438 if ( HasChildPathFocus() )
439 rStateSet |= AccessibleStateType::FOCUSED;
440
441 if ( IsActive() )
442 rStateSet |= AccessibleStateType::ACTIVE;
443
444 if ( m_pImpl->getDataWindow().IsEnabled() )
445 {
446 rStateSet |= AccessibleStateType::ENABLED;
447 rStateSet |= AccessibleStateType::SENSITIVE;
448 }
449
450 if ( IsReallyVisible() )
451 rStateSet |= AccessibleStateType::VISIBLE;
452
453 if ( eObjType == AccessibleTableControlObjType::TABLE )
454 rStateSet |= AccessibleStateType::MANAGES_DESCENDANTS;
455 break;
456
457 case AccessibleTableControlObjType::COLUMNHEADERBAR:
458 case AccessibleTableControlObjType::ROWHEADERBAR:
459 rStateSet |= AccessibleStateType::VISIBLE;
460 rStateSet |= AccessibleStateType::MANAGES_DESCENDANTS;
461 break;
462
463 case AccessibleTableControlObjType::TABLECELL:
464 {
465 rStateSet |= AccessibleStateType::FOCUSABLE;
466 if ( HasChildPathFocus() )
467 rStateSet |= AccessibleStateType::FOCUSED;
468 rStateSet |= AccessibleStateType::ACTIVE;
469 rStateSet |= AccessibleStateType::TRANSIENT;
470 rStateSet |= AccessibleStateType::SELECTABLE;
471 rStateSet |= AccessibleStateType::VISIBLE;
472 rStateSet |= AccessibleStateType::SHOWING;
473 if ( IsRowSelected( GetCurrentRow() ) )
474 // Hmm? Wouldn't we expect the affected row to be a parameter to this function?
475 rStateSet |= AccessibleStateType::SELECTED;
476 }
477 break;
478
479 case AccessibleTableControlObjType::ROWHEADERCELL:
480 rStateSet |= AccessibleStateType::VISIBLE;
481 rStateSet |= AccessibleStateType::TRANSIENT;
482 break;
483
484 case AccessibleTableControlObjType::COLUMNHEADERCELL:
485 rStateSet |= AccessibleStateType::VISIBLE;
486 break;
487 }
488 }
489
490 void TableControl::commitCellEvent(sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue)
491 {
492 m_pImpl->commitCellEvent( i_eventID, i_newValue, i_oldValue );
493 }
494
495 void TableControl::commitTableEvent(sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue)
496 {
497 m_pImpl->commitTableEvent( i_eventID, i_newValue, i_oldValue );
498 }
499
500 bool TableControl::HasRowHeader()
501 {
502 return GetModel()->hasRowHeaders();
503 }
504
505
506 bool TableControl::HasColHeader()
507 {
508 return GetModel()->hasColumnHeaders();
509 }
510
511
512 sal_Int32 TableControl::GetAccessibleControlCount() const
513 {
514 // TC_TABLE is always defined, no matter whether empty or not
515 sal_Int32 count = 1;
516 if ( GetModel()->hasRowHeaders() )
517 ++count;
518 if ( GetModel()->hasColumnHeaders() )
519 ++count;
520 return count;
521 }
522
523 sal_Int32 TableControl::GetRowCount() const
524 {
525 return GetModel()->getRowCount();
526 }
527
528
529 sal_Int32 TableControl::GetColumnCount() const
530 {
531 return GetModel()->getColumnCount();
532 }
533
534
535 bool TableControl::ConvertPointToCellAddress( sal_Int32& _rnRow, sal_Int32& _rnColPos, const Point& _rPoint )
536 {
537 _rnRow = m_pImpl->getRowAtPoint( _rPoint );
538 _rnColPos = m_pImpl->getColAtPoint( _rPoint );
539 return _rnRow >= 0;
540 }
541
542
543 void TableControl::FillAccessibleStateSetForCell( sal_Int64& _rStateSet, sal_Int32 _nRow, sal_uInt16 ) const
544 {
545 if ( IsRowSelected( _nRow ) )
546 _rStateSet |= AccessibleStateType::SELECTED;
547 if ( HasChildPathFocus() )
548 _rStateSet |= AccessibleStateType::FOCUSED;
549 else // only transient when column is not focused
550 _rStateSet |= AccessibleStateType::TRANSIENT;
551
552 _rStateSet |= AccessibleStateType::VISIBLE;
553 _rStateSet |= AccessibleStateType::SHOWING;
554 _rStateSet |= AccessibleStateType::ENABLED;
555 _rStateSet |= AccessibleStateType::SENSITIVE;
556 _rStateSet |= AccessibleStateType::ACTIVE;
557 }
558
559
560 tools::Rectangle TableControl::calcHeaderRect(bool _bIsColumnBar )
561 {
562 return m_pImpl->calcHeaderRect(_bIsColumnBar);
563 }
564
565
566 tools::Rectangle TableControl::calcHeaderCellRect( bool _bIsColumnBar, sal_Int32 nPos )
567 {
568 return m_pImpl->calcHeaderCellRect( _bIsColumnBar, nPos );
569 }
570
571
572 tools::Rectangle TableControl::calcTableRect()
573 {
574 return m_pImpl->calcTableRect();
575 }
576
577
578 tools::Rectangle TableControl::calcCellRect( sal_Int32 _nRowPos, sal_Int32 _nColPos )
579 {
580 return m_pImpl->calcCellRect( _nRowPos, _nColPos );
581 }
582
583
584 IMPL_LINK_NOARG(TableControl, ImplSelectHdl, LinkParamNone*, void)
585 {
586 Select();
587 }
588
589
590 void TableControl::Select()
591 {
592 ImplCallEventListenersAndHandler( VclEventId::TableRowSelect, nullptr );
593 m_pImpl->commitAccessibleEvent( AccessibleEventId::SELECTION_CHANGED );
594
595 m_pImpl->commitTableEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, Any(), Any() );
596 // TODO: why do we notify this when the *selection* changed? Shouldn't we find a better place for this,
597 // actually, when the active descendant, i.e. the current cell, *really* changed?
598 }
599
600 TableCell TableControl::hitTest(const Point& rPoint) const
601 {
602 return m_pImpl->hitTest(rPoint);
603 }
604
605 void TableControl::invalidate(const TableArea aArea)
606 {
607 return m_pImpl->invalidate(aArea);
608 }
609
610 } // namespace svt::table
611
612
613 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */