1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: csvruler.cxx,v $
10 * $Revision: 1.16.146.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
35 // ============================================================================
36 #include "csvruler.hxx"
37 #include "AccessibleCsvControl.hxx"
40 #include <optutil.hxx>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include "miscuno.hxx"
46 using namespace com::sun::star::uno
;
50 // ============================================================================
51 #define SEP_PATH "Office.Calc/Dialogs/CSVImport"
52 #define FIXED_WIDTH_LIST "FixedWidthList"
55 // ============================================================================
57 static void load_FixedWidthList(ScCsvSplits
&aSplits
)
60 OUString sFixedWidthLists
;
63 const Any
*pProperties
;
64 Sequence
<OUString
> aNames(1);
65 OUString
* pNames
= aNames
.getArray();
66 ScLinkConfigItem
aItem( OUString::createFromAscii( SEP_PATH
) );
68 pNames
[0] = OUString::createFromAscii( FIXED_WIDTH_LIST
);
69 aValues
= aItem
.GetProperties( aNames
);
70 pProperties
= aValues
.getConstArray();
72 if( pProperties
[0].hasValue() )
75 pProperties
[0] >>= sFixedWidthLists
;
77 sSplits
= String( sFixedWidthLists
);
79 // String ends with a semi-colon so there is no 'int' after the last one.
80 for(int i
=0;i
<sSplits
.GetTokenCount()-1;i
++ )
81 aSplits
.Insert( sSplits
.GetToken(i
).ToInt32() );
84 static void save_FixedWidthList(ScCsvSplits aSplits
)
87 // Create a semi-colon separated string to save the splits
88 sal_uInt32 n
= aSplits
.Count();
89 for (sal_uInt32 i
= 0; i
< n
; ++i
)
91 sSplits
.Append( String::CreateFromInt32( aSplits
[i
] ) );
92 sSplits
.Append((char)';');
95 OUString sFixedWidthLists
= OUString( sSplits
);
96 Sequence
<Any
> aValues
;
98 Sequence
<OUString
> aNames(1);
99 OUString
* pNames
= aNames
.getArray();
100 ScLinkConfigItem
aItem( OUString::createFromAscii( SEP_PATH
) );
102 pNames
[0] = OUString::createFromAscii( FIXED_WIDTH_LIST
);
103 aValues
= aItem
.GetProperties( aNames
);
104 pProperties
= aValues
.getArray();
105 pProperties
[0] <<= sFixedWidthLists
;
107 aItem
.PutProperties(aNames
, aValues
);
110 ScCsvRuler::ScCsvRuler( ScCsvControl
& rParent
) :
111 ScCsvControl( rParent
),
114 EnableRTL( false ); // #107812# RTL
117 maBackgrDev
.SetFont( GetFont() );
118 maRulerDev
.SetFont( GetFont() );
120 load_FixedWidthList( maSplits
);
123 ScCsvRuler::~ScCsvRuler()
125 save_FixedWidthList( maSplits
);
129 // common ruler handling ------------------------------------------------------
131 void ScCsvRuler::SetPosSizePixel(
132 long nX
, long nY
, long nWidth
, long nHeight
, USHORT nFlags
)
134 if( nFlags
& WINDOW_POSSIZE_HEIGHT
)
135 nHeight
= GetTextHeight() + mnSplitSize
+ 2;
136 ScCsvControl::SetPosSizePixel( nX
, nY
, nWidth
, nHeight
, nFlags
);
139 void ScCsvRuler::ApplyLayout( const ScCsvLayoutData
& rOldData
)
141 ScCsvDiff nDiff
= GetLayoutData().GetDiff( rOldData
) & (CSV_DIFF_HORIZONTAL
| CSV_DIFF_RULERCURSOR
);
142 if( nDiff
== CSV_DIFF_EQUAL
) return;
145 if( nDiff
& CSV_DIFF_HORIZONTAL
)
148 if( GetRulerCursorPos() >= GetPosCount() )
149 MoveCursor( GetPosCount() - 1 );
151 if( nDiff
& CSV_DIFF_RULERCURSOR
)
153 ImplInvertCursor( rOldData
.mnPosCursor
);
154 ImplInvertCursor( GetRulerCursorPos() );
158 if( nDiff
& CSV_DIFF_POSOFFSET
)
159 AccSendVisibleEvent();
162 void ScCsvRuler::InitColors()
164 const StyleSettings
& rSett
= GetSettings().GetStyleSettings();
165 maBackColor
= rSett
.GetFaceColor();
166 maActiveColor
= rSett
.GetWindowColor();
167 maTextColor
= rSett
.GetLabelTextColor();
168 maSplitColor
= maBackColor
.IsDark() ? maTextColor
: Color( COL_LIGHTRED
);
172 void ScCsvRuler::InitSizeData()
174 maWinSize
= GetSizePixel();
176 mnSplitSize
= (GetCharWidth() * 3 / 5) | 1; // make an odd number
178 sal_Int32 nActiveWidth
= Min( GetWidth() - GetHdrWidth(), GetPosCount() * GetCharWidth() );
179 sal_Int32 nActiveHeight
= GetTextHeight();
181 maActiveRect
.SetPos( Point( GetFirstX(), (GetHeight() - nActiveHeight
- 1) / 2 ) );
182 maActiveRect
.SetSize( Size( nActiveWidth
, nActiveHeight
) );
184 maBackgrDev
.SetOutputSizePixel( maWinSize
);
185 maRulerDev
.SetOutputSizePixel( maWinSize
);
190 void ScCsvRuler::MoveCursor( sal_Int32 nPos
, bool bScroll
)
194 Execute( CSVCMD_MAKEPOSVISIBLE
, nPos
);
195 Execute( CSVCMD_MOVERULERCURSOR
, IsVisibleSplitPos( nPos
) ? nPos
: CSV_POS_INVALID
);
200 void ScCsvRuler::MoveCursorRel( ScMoveMode eDir
)
202 if( GetRulerCursorPos() != CSV_POS_INVALID
)
210 MoveCursor( GetPosCount() - 1 );
213 if( GetRulerCursorPos() > 1 )
214 MoveCursor( GetRulerCursorPos() - 1 );
217 if( GetRulerCursorPos() < GetPosCount() - 1 )
218 MoveCursor( GetRulerCursorPos() + 1 );
222 // added to avoid warnings
228 void ScCsvRuler::MoveCursorToSplit( ScMoveMode eDir
)
230 if( GetRulerCursorPos() != CSV_POS_INVALID
)
232 sal_uInt32 nIndex
= CSV_VEC_NOTFOUND
;
235 case MOVE_FIRST
: nIndex
= maSplits
.LowerBound( 0 ); break;
236 case MOVE_LAST
: nIndex
= maSplits
.UpperBound( GetPosCount() ); break;
237 case MOVE_PREV
: nIndex
= maSplits
.UpperBound( GetRulerCursorPos() - 1 ); break;
238 case MOVE_NEXT
: nIndex
= maSplits
.LowerBound( GetRulerCursorPos() + 1 ); break;
241 // added to avoid warnings
244 sal_Int32 nPos
= maSplits
[ nIndex
];
245 if( nPos
!= CSV_POS_INVALID
)
250 void ScCsvRuler::ScrollVertRel( ScMoveMode eDir
)
252 sal_Int32 nLine
= GetFirstVisLine();
255 case MOVE_PREV
: --nLine
; break;
256 case MOVE_NEXT
: ++nLine
; break;
257 case MOVE_PREVPAGE
: nLine
-= GetVisLineCount() - 1; break;
258 case MOVE_NEXTPAGE
: nLine
+= GetVisLineCount() - 1; break;
261 // added to avoid warnings
264 Execute( CSVCMD_SETLINEOFFSET
, nLine
);
268 // split handling -------------------------------------------------------------
270 sal_Int32
ScCsvRuler::GetNoScrollPos( sal_Int32 nPos
) const
272 sal_Int32 nNewPos
= nPos
;
273 if( nNewPos
!= CSV_POS_INVALID
)
275 if( nNewPos
< GetFirstVisPos() + CSV_SCROLL_DIST
)
277 sal_Int32 nScroll
= (GetFirstVisPos() > 0) ? CSV_SCROLL_DIST
: 0;
278 nNewPos
= Max( nPos
, GetFirstVisPos() + nScroll
);
280 else if( nNewPos
> GetLastVisPos() - CSV_SCROLL_DIST
- 1L )
282 sal_Int32 nScroll
= (GetFirstVisPos() < GetMaxPosOffset()) ? CSV_SCROLL_DIST
: 0;
283 nNewPos
= Min( nNewPos
, GetLastVisPos() - nScroll
- sal_Int32( 1 ) );
289 void ScCsvRuler::InsertSplit( sal_Int32 nPos
)
291 if( maSplits
.Insert( nPos
) )
293 ImplDrawSplit( nPos
);
298 void ScCsvRuler::RemoveSplit( sal_Int32 nPos
)
300 if( maSplits
.Remove( nPos
) )
302 ImplEraseSplit( nPos
);
307 void ScCsvRuler::MoveSplit( sal_Int32 nPos
, sal_Int32 nNewPos
)
309 bool bRemove
= maSplits
.Remove( nPos
);
310 bool bInsert
= maSplits
.Insert( nNewPos
);
311 if( bRemove
|| bInsert
)
313 ImplEraseSplit( nPos
);
314 ImplDrawSplit( nNewPos
);
319 void ScCsvRuler::RemoveAllSplits()
325 sal_Int32
ScCsvRuler::FindEmptyPos( sal_Int32 nPos
, ScMoveMode eDir
) const
327 sal_Int32 nNewPos
= nPos
;
328 if( nNewPos
!= CSV_POS_INVALID
)
333 nNewPos
= Min( nPos
, FindEmptyPos( 0, MOVE_NEXT
) );
336 nNewPos
= Max( nPos
, FindEmptyPos( GetPosCount(), MOVE_PREV
) );
339 while( HasSplit( --nNewPos
) ) ;
342 while( HasSplit( ++nNewPos
) ) ;
346 // added to avoid warnings
350 return IsValidSplitPos( nNewPos
) ? nNewPos
: CSV_POS_INVALID
;
353 void ScCsvRuler::MoveCurrSplit( sal_Int32 nNewPos
)
356 Execute( CSVCMD_MOVESPLIT
, GetRulerCursorPos(), nNewPos
);
357 MoveCursor( nNewPos
);
361 void ScCsvRuler::MoveCurrSplitRel( ScMoveMode eDir
)
363 if( HasSplit( GetRulerCursorPos() ) )
365 sal_Int32 nNewPos
= FindEmptyPos( GetRulerCursorPos(), eDir
);
366 if( nNewPos
!= CSV_POS_INVALID
)
367 MoveCurrSplit( nNewPos
);
372 // event handling -------------------------------------------------------------
374 void ScCsvRuler::Resize()
376 ScCsvControl::Resize();
381 void ScCsvRuler::GetFocus()
383 ScCsvControl::GetFocus();
385 if( GetRulerCursorPos() == CSV_POS_INVALID
)
386 MoveCursor( GetNoScrollPos( mnPosCursorLast
) );
390 void ScCsvRuler::LoseFocus()
392 ScCsvControl::LoseFocus();
393 mnPosCursorLast
= GetRulerCursorPos();
394 MoveCursor( CSV_POS_INVALID
);
397 void ScCsvRuler::DataChanged( const DataChangedEvent
& rDCEvt
)
399 if( (rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && (rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
404 ScCsvControl::DataChanged( rDCEvt
);
407 void ScCsvRuler::MouseButtonDown( const MouseEvent
& rMEvt
)
414 sal_Int32 nPos
= GetPosFromX( rMEvt
.GetPosPixel().X() );
415 if( IsVisibleSplitPos( nPos
) )
416 StartMouseTracking( nPos
);
417 ImplSetMousePointer( nPos
);
422 void ScCsvRuler::MouseMove( const MouseEvent
& rMEvt
)
424 if( !rMEvt
.IsModifierChanged() )
426 sal_Int32 nPos
= GetPosFromX( rMEvt
.GetPosPixel().X() );
429 // on mouse tracking: keep position valid
430 nPos
= Max( Min( nPos
, GetPosCount() - sal_Int32( 1 ) ), sal_Int32( 1 ) );
431 MoveMouseTracking( nPos
);
436 Rectangle
aRect( aPoint
, maWinSize
);
437 if( !IsVisibleSplitPos( nPos
) || !aRect
.IsInside( rMEvt
.GetPosPixel() ) )
438 // if focused, keep old cursor position for key input
439 nPos
= HasFocus() ? GetRulerCursorPos() : CSV_POS_INVALID
;
440 MoveCursor( nPos
, false );
442 ImplSetMousePointer( nPos
);
446 void ScCsvRuler::Tracking( const TrackingEvent
& rTEvt
)
448 if( rTEvt
.IsTrackingEnded() || rTEvt
.IsTrackingRepeat() )
449 MouseMove( rTEvt
.GetMouseEvent() );
450 if( rTEvt
.IsTrackingEnded() )
451 EndMouseTracking( !rTEvt
.IsTrackingCanceled() );
454 void ScCsvRuler::KeyInput( const KeyEvent
& rKEvt
)
456 const KeyCode
& rKCode
= rKEvt
.GetKeyCode();
457 sal_uInt16 nCode
= rKCode
.GetCode();
458 bool bNoMod
= !rKCode
.GetModifier();
459 bool bShift
= (rKCode
.GetModifier() == KEY_SHIFT
);
460 bool bJump
= (rKCode
.GetModifier() == KEY_MOD1
);
461 bool bMove
= (rKCode
.GetModifier() == (KEY_MOD1
| KEY_SHIFT
));
463 ScMoveMode eHDir
= GetHorzDirection( nCode
, true );
464 ScMoveMode eVDir
= GetVertDirection( nCode
, false );
468 if( eHDir
!= MOVE_NONE
)
469 MoveCursorRel( eHDir
);
470 else if( eVDir
!= MOVE_NONE
)
471 ScrollVertRel( eVDir
);
474 case KEY_SPACE
: Execute( CSVCMD_TOGGLESPLIT
, GetRulerCursorPos() ); break;
475 case KEY_INSERT
: Execute( CSVCMD_INSERTSPLIT
, GetRulerCursorPos() ); break;
476 case KEY_DELETE
: Execute( CSVCMD_REMOVESPLIT
, GetRulerCursorPos() ); break;
479 else if( bJump
&& (eHDir
!= MOVE_NONE
) )
480 MoveCursorToSplit( eHDir
);
481 else if( bMove
&& (eHDir
!= MOVE_NONE
) )
482 MoveCurrSplitRel( eHDir
);
483 else if( bShift
&& (nCode
== KEY_DELETE
) )
484 Execute( CSVCMD_REMOVEALLSPLITS
);
486 if( rKCode
.GetGroup() != KEYGROUP_CURSOR
)
487 ScCsvControl::KeyInput( rKEvt
);
490 void ScCsvRuler::StartMouseTracking( sal_Int32 nPos
)
492 mnPosMTStart
= mnPosMTCurr
= nPos
;
493 mbPosMTMoved
= false;
494 maOldSplits
= maSplits
;
495 Execute( CSVCMD_INSERTSPLIT
, nPos
);
496 if( HasSplit( nPos
) )
497 StartTracking( STARTTRACK_BUTTONREPEAT
);
500 void ScCsvRuler::MoveMouseTracking( sal_Int32 nPos
)
502 if( mnPosMTCurr
!= nPos
)
506 if( (mnPosMTCurr
!= mnPosMTStart
) && maOldSplits
.HasSplit( mnPosMTCurr
) )
507 Execute( CSVCMD_INSERTSPLIT
, nPos
);
509 Execute( CSVCMD_MOVESPLIT
, mnPosMTCurr
, nPos
);
516 void ScCsvRuler::EndMouseTracking( bool bApply
)
518 if( bApply
) // tracking finished successfully
520 // remove on simple click on an existing split
521 if( (mnPosMTCurr
== mnPosMTStart
) && maOldSplits
.HasSplit( mnPosMTCurr
) && !mbPosMTMoved
)
522 Execute( CSVCMD_REMOVESPLIT
, mnPosMTCurr
);
524 else // tracking cancelled
526 MoveCursor( mnPosMTStart
);
527 // move split to origin
528 if( maOldSplits
.HasSplit( mnPosMTStart
) )
529 MoveMouseTracking( mnPosMTStart
);
530 // remove temporarily inserted split
531 else if( !maOldSplits
.HasSplit( mnPosMTCurr
) )
532 Execute( CSVCMD_REMOVESPLIT
, mnPosMTCurr
);
534 mnPosMTStart
= CSV_POS_INVALID
;
538 // painting -------------------------------------------------------------------
540 void ScCsvRuler::Paint( const Rectangle
& )
545 void ScCsvRuler::ImplRedraw()
555 DrawOutDev( Point(), maWinSize
, Point(), maWinSize
, maRulerDev
);
556 ImplDrawTrackingRect();
560 void ScCsvRuler::ImplDrawArea( sal_Int32 nPosX
, sal_Int32 nWidth
)
562 maBackgrDev
.SetLineColor();
563 Rectangle
aRect( Point( nPosX
, 0 ), Size( nWidth
, GetHeight() ) );
564 maBackgrDev
.SetFillColor( maBackColor
);
565 maBackgrDev
.DrawRect( aRect
);
567 aRect
= maActiveRect
;
568 aRect
.Left() = Max( GetFirstX(), nPosX
);
569 aRect
.Right() = Min( Min( GetX( GetPosCount() ), GetLastX() ), nPosX
+ nWidth
- sal_Int32( 1 ) );
570 if( aRect
.Left() <= aRect
.Right() )
572 maBackgrDev
.SetFillColor( maActiveColor
);
573 maBackgrDev
.DrawRect( aRect
);
576 maBackgrDev
.SetLineColor( maTextColor
);
577 sal_Int32 nY
= GetHeight() - 1;
578 maBackgrDev
.DrawLine( Point( nPosX
, nY
), Point( nPosX
+ nWidth
- 1, nY
) );
581 void ScCsvRuler::ImplDrawBackgrDev()
583 ImplDrawArea( 0, GetWidth() );
586 maBackgrDev
.SetLineColor( maTextColor
);
587 maBackgrDev
.SetFillColor();
590 sal_Int32 nFirstPos
= Max( GetPosFromX( 0 ) - (sal_Int32
)(1L), (sal_Int32
)(0L) );
591 sal_Int32 nLastPos
= GetPosFromX( GetWidth() );
592 sal_Int32 nY
= (maActiveRect
.Top() + maActiveRect
.Bottom()) / 2;
593 for( nPos
= nFirstPos
; nPos
<= nLastPos
; ++nPos
)
595 sal_Int32 nX
= GetX( nPos
);
597 maBackgrDev
.DrawPixel( Point( nX
, nY
) );
599 maBackgrDev
.DrawLine( Point( nX
, nY
- 1 ), Point( nX
, nY
+ 1 ) );
603 maBackgrDev
.SetTextColor( maTextColor
);
604 maBackgrDev
.SetTextFillColor();
605 for( nPos
= ((nFirstPos
+ 9) / 10) * 10; nPos
<= nLastPos
; nPos
+= 10 )
607 String
aText( String::CreateFromInt32( nPos
) );
608 sal_Int32 nTextWidth
= maBackgrDev
.GetTextWidth( aText
);
609 sal_Int32 nTextX
= GetX( nPos
) - nTextWidth
/ 2;
610 ImplDrawArea( nTextX
- 1, nTextWidth
+ 2 );
611 maBackgrDev
.DrawText( Point( nTextX
, maActiveRect
.Top() ), aText
);
615 void ScCsvRuler::ImplDrawSplit( sal_Int32 nPos
)
617 if( IsVisibleSplitPos( nPos
) )
619 Point
aPos( GetX( nPos
) - mnSplitSize
/ 2, GetHeight() - mnSplitSize
- 2 );
620 Size
aSize( mnSplitSize
, mnSplitSize
);
621 maRulerDev
.SetLineColor( maTextColor
);
622 maRulerDev
.SetFillColor( maSplitColor
);
623 maRulerDev
.DrawEllipse( Rectangle( aPos
, aSize
) );
624 maRulerDev
.DrawPixel( Point( GetX( nPos
), GetHeight() - 2 ) );
628 void ScCsvRuler::ImplEraseSplit( sal_Int32 nPos
)
630 if( IsVisibleSplitPos( nPos
) )
632 ImplInvertCursor( GetRulerCursorPos() );
633 Point
aPos( GetX( nPos
) - mnSplitSize
/ 2, 0 );
634 Size
aSize( mnSplitSize
, GetHeight() );
635 maRulerDev
.DrawOutDev( aPos
, aSize
, aPos
, aSize
, maBackgrDev
);
636 ImplInvertCursor( GetRulerCursorPos() );
640 void ScCsvRuler::ImplDrawRulerDev()
642 maRulerDev
.DrawOutDev( Point(), maWinSize
, Point(), maWinSize
, maBackgrDev
);
643 ImplInvertCursor( GetRulerCursorPos() );
645 sal_uInt32 nFirst
= maSplits
.LowerBound( GetFirstVisPos() );
646 sal_uInt32 nLast
= maSplits
.UpperBound( GetLastVisPos() );
647 if( (nFirst
!= CSV_VEC_NOTFOUND
) && (nLast
!= CSV_VEC_NOTFOUND
) )
648 for( sal_uInt32 nIndex
= nFirst
; nIndex
<= nLast
; ++nIndex
)
649 ImplDrawSplit( GetSplitPos( nIndex
) );
652 void ScCsvRuler::ImplInvertCursor( sal_Int32 nPos
)
654 if( IsVisibleSplitPos( nPos
) )
656 ImplInvertRect( maRulerDev
, Rectangle( Point( GetX( nPos
) - 1, 0 ), Size( 3, GetHeight() - 1 ) ) );
657 if( HasSplit( nPos
) )
658 ImplDrawSplit( nPos
);
662 void ScCsvRuler::ImplDrawTrackingRect()
665 InvertTracking( Rectangle( 0, 0, GetWidth() - 1, GetHeight() - 2 ),
666 SHOWTRACK_SMALL
| SHOWTRACK_WINDOW
);
669 void ScCsvRuler::ImplSetMousePointer( sal_Int32 nPos
)
671 SetPointer( Pointer( HasSplit( nPos
) ? POINTER_HSPLIT
: POINTER_ARROW
) );
675 // accessibility ==============================================================
677 ScAccessibleCsvControl
* ScCsvRuler::ImplCreateAccessible()
679 return new ScAccessibleCsvRuler( *this );
683 // ============================================================================