xref: /aoo42x/main/sc/source/ui/dbgui/csvruler.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // ============================================================================
29cdf0e10cSrcweir #include "csvruler.hxx"
30cdf0e10cSrcweir #include "AccessibleCsvControl.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <optutil.hxx>
34cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
35cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
36cdf0e10cSrcweir #include "miscuno.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace rtl;
39cdf0e10cSrcweir using namespace com::sun::star::uno;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // ============================================================================
44cdf0e10cSrcweir #define SEP_PATH            "Office.Calc/Dialogs/CSVImport"
45cdf0e10cSrcweir #define FIXED_WIDTH_LIST    "FixedWidthList"
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // ============================================================================
49cdf0e10cSrcweir 
load_FixedWidthList(ScCsvSplits & aSplits)50cdf0e10cSrcweir static void load_FixedWidthList(ScCsvSplits &aSplits)
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     String sSplits;
53cdf0e10cSrcweir     OUString sFixedWidthLists;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     Sequence<Any>aValues;
56cdf0e10cSrcweir     const Any *pProperties;
57cdf0e10cSrcweir     Sequence<OUString> aNames(1);
58cdf0e10cSrcweir     OUString* pNames = aNames.getArray();
59cdf0e10cSrcweir     ScLinkConfigItem aItem( OUString::createFromAscii( SEP_PATH ) );
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     pNames[0] = OUString::createFromAscii( FIXED_WIDTH_LIST );
62cdf0e10cSrcweir     aValues = aItem.GetProperties( aNames );
63cdf0e10cSrcweir     pProperties = aValues.getConstArray();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     if( pProperties[0].hasValue() )
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         aSplits.Clear();
68cdf0e10cSrcweir         pProperties[0] >>= sFixedWidthLists;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         sSplits = String( sFixedWidthLists );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         // String ends with a semi-colon so there is no 'int' after the last one.
73cdf0e10cSrcweir         xub_StrLen n = sSplits.GetTokenCount() - 1;
74cdf0e10cSrcweir         for (xub_StrLen i = 0; i < n; ++i)
75cdf0e10cSrcweir             aSplits.Insert( sSplits.GetToken(i).ToInt32() );
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir }
save_FixedWidthList(ScCsvSplits aSplits)78cdf0e10cSrcweir static void save_FixedWidthList(ScCsvSplits aSplits)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     String sSplits;
81cdf0e10cSrcweir     // Create a semi-colon separated string to save the splits
82cdf0e10cSrcweir     sal_uInt32 n = aSplits.Count();
83cdf0e10cSrcweir     for (sal_uInt32 i = 0; i < n; ++i)
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         sSplits.Append( String::CreateFromInt32( aSplits[i] ) );
86cdf0e10cSrcweir         sSplits.Append((char)';');
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     OUString sFixedWidthLists = OUString( sSplits );
90cdf0e10cSrcweir     Sequence<Any> aValues;
91cdf0e10cSrcweir     Any *pProperties;
92cdf0e10cSrcweir     Sequence<OUString> aNames(1);
93cdf0e10cSrcweir     OUString* pNames = aNames.getArray();
94cdf0e10cSrcweir     ScLinkConfigItem aItem( OUString::createFromAscii( SEP_PATH ) );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     pNames[0] = OUString::createFromAscii( FIXED_WIDTH_LIST );
97cdf0e10cSrcweir     aValues = aItem.GetProperties( aNames );
98cdf0e10cSrcweir     pProperties = aValues.getArray();
99cdf0e10cSrcweir     pProperties[0] <<= sFixedWidthLists;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     aItem.PutProperties(aNames, aValues);
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
ScCsvRuler(ScCsvControl & rParent)104cdf0e10cSrcweir ScCsvRuler::ScCsvRuler( ScCsvControl& rParent ) :
105cdf0e10cSrcweir     ScCsvControl( rParent ),
106cdf0e10cSrcweir     mnPosCursorLast( 1 )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     EnableRTL( false ); // #107812# RTL
109cdf0e10cSrcweir     InitColors();
110cdf0e10cSrcweir     InitSizeData();
111cdf0e10cSrcweir     maBackgrDev.SetFont( GetFont() );
112cdf0e10cSrcweir     maRulerDev.SetFont( GetFont() );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     load_FixedWidthList( maSplits );
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
~ScCsvRuler()117cdf0e10cSrcweir ScCsvRuler::~ScCsvRuler()
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     save_FixedWidthList( maSplits );
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 
123cdf0e10cSrcweir // common ruler handling ------------------------------------------------------
124cdf0e10cSrcweir 
SetPosSizePixel(long nX,long nY,long nWidth,long nHeight,sal_uInt16 nFlags)125cdf0e10cSrcweir void ScCsvRuler::SetPosSizePixel(
126cdf0e10cSrcweir         long nX, long nY, long nWidth, long nHeight, sal_uInt16 nFlags )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     if( nFlags & WINDOW_POSSIZE_HEIGHT )
129cdf0e10cSrcweir         nHeight = GetTextHeight() + mnSplitSize + 2;
130cdf0e10cSrcweir     ScCsvControl::SetPosSizePixel( nX, nY, nWidth, nHeight, nFlags );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
ApplyLayout(const ScCsvLayoutData & rOldData)133cdf0e10cSrcweir void ScCsvRuler::ApplyLayout( const ScCsvLayoutData& rOldData )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     ScCsvDiff nDiff = GetLayoutData().GetDiff( rOldData ) & (CSV_DIFF_HORIZONTAL | CSV_DIFF_RULERCURSOR);
136cdf0e10cSrcweir     if( nDiff == CSV_DIFF_EQUAL ) return;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     DisableRepaint();
139cdf0e10cSrcweir     if( nDiff & CSV_DIFF_HORIZONTAL )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         InitSizeData();
142cdf0e10cSrcweir         if( GetRulerCursorPos() >= GetPosCount() )
143cdf0e10cSrcweir             MoveCursor( GetPosCount() - 1 );
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir     if( nDiff & CSV_DIFF_RULERCURSOR )
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         ImplInvertCursor( rOldData.mnPosCursor );
148cdf0e10cSrcweir         ImplInvertCursor( GetRulerCursorPos() );
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir     EnableRepaint();
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     if( nDiff & CSV_DIFF_POSOFFSET )
153cdf0e10cSrcweir         AccSendVisibleEvent();
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
InitColors()156cdf0e10cSrcweir void ScCsvRuler::InitColors()
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     const StyleSettings& rSett = GetSettings().GetStyleSettings();
159cdf0e10cSrcweir     maBackColor = rSett.GetFaceColor();
160cdf0e10cSrcweir     maActiveColor = rSett.GetWindowColor();
161cdf0e10cSrcweir     maTextColor = rSett.GetLabelTextColor();
162cdf0e10cSrcweir     maSplitColor = maBackColor.IsDark() ? maTextColor : Color( COL_LIGHTRED );
163cdf0e10cSrcweir     InvalidateGfx();
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
InitSizeData()166cdf0e10cSrcweir void ScCsvRuler::InitSizeData()
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     maWinSize = GetSizePixel();
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     mnSplitSize = (GetCharWidth() * 3 / 5) | 1; // make an odd number
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     sal_Int32 nActiveWidth = Min( GetWidth() - GetHdrWidth(), GetPosCount() * GetCharWidth() );
173cdf0e10cSrcweir     sal_Int32 nActiveHeight = GetTextHeight();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     maActiveRect.SetPos( Point( GetFirstX(), (GetHeight() - nActiveHeight - 1) / 2 ) );
176cdf0e10cSrcweir     maActiveRect.SetSize( Size( nActiveWidth, nActiveHeight ) );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     maBackgrDev.SetOutputSizePixel( maWinSize );
179cdf0e10cSrcweir     maRulerDev.SetOutputSizePixel( maWinSize );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     InvalidateGfx();
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
MoveCursor(sal_Int32 nPos,bool bScroll)184cdf0e10cSrcweir void ScCsvRuler::MoveCursor( sal_Int32 nPos, bool bScroll )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     DisableRepaint();
187cdf0e10cSrcweir     if( bScroll )
188cdf0e10cSrcweir         Execute( CSVCMD_MAKEPOSVISIBLE, nPos );
189cdf0e10cSrcweir     Execute( CSVCMD_MOVERULERCURSOR, IsVisibleSplitPos( nPos ) ? nPos : CSV_POS_INVALID );
190cdf0e10cSrcweir     EnableRepaint();
191cdf0e10cSrcweir     AccSendCaretEvent();
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
MoveCursorRel(ScMoveMode eDir)194cdf0e10cSrcweir void ScCsvRuler::MoveCursorRel( ScMoveMode eDir )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     if( GetRulerCursorPos() != CSV_POS_INVALID )
197cdf0e10cSrcweir     {
198cdf0e10cSrcweir         switch( eDir )
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             case MOVE_FIRST:
201cdf0e10cSrcweir                 MoveCursor( 1 );
202cdf0e10cSrcweir             break;
203cdf0e10cSrcweir             case MOVE_LAST:
204cdf0e10cSrcweir                 MoveCursor( GetPosCount() - 1 );
205cdf0e10cSrcweir             break;
206cdf0e10cSrcweir             case MOVE_PREV:
207cdf0e10cSrcweir                 if( GetRulerCursorPos() > 1 )
208cdf0e10cSrcweir                     MoveCursor( GetRulerCursorPos() - 1 );
209cdf0e10cSrcweir             break;
210cdf0e10cSrcweir             case MOVE_NEXT:
211cdf0e10cSrcweir                 if( GetRulerCursorPos() < GetPosCount() - 1 )
212cdf0e10cSrcweir                     MoveCursor( GetRulerCursorPos() + 1 );
213cdf0e10cSrcweir             break;
214cdf0e10cSrcweir             default:
215cdf0e10cSrcweir             {
216cdf0e10cSrcweir                 // added to avoid warnings
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
MoveCursorToSplit(ScMoveMode eDir)222cdf0e10cSrcweir void ScCsvRuler::MoveCursorToSplit( ScMoveMode eDir )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     if( GetRulerCursorPos() != CSV_POS_INVALID )
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         sal_uInt32 nIndex = CSV_VEC_NOTFOUND;
227cdf0e10cSrcweir         switch( eDir )
228cdf0e10cSrcweir         {
229cdf0e10cSrcweir             case MOVE_FIRST:    nIndex = maSplits.LowerBound( 0 );                          break;
230cdf0e10cSrcweir             case MOVE_LAST:     nIndex = maSplits.UpperBound( GetPosCount() );              break;
231cdf0e10cSrcweir             case MOVE_PREV:     nIndex = maSplits.UpperBound( GetRulerCursorPos() - 1 );    break;
232cdf0e10cSrcweir             case MOVE_NEXT:     nIndex = maSplits.LowerBound( GetRulerCursorPos() + 1 );    break;
233cdf0e10cSrcweir             default:
234cdf0e10cSrcweir             {
235cdf0e10cSrcweir                 // added to avoid warnings
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir         sal_Int32 nPos = maSplits[ nIndex ];
239cdf0e10cSrcweir         if( nPos != CSV_POS_INVALID )
240cdf0e10cSrcweir             MoveCursor( nPos );
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
ScrollVertRel(ScMoveMode eDir)244cdf0e10cSrcweir void ScCsvRuler::ScrollVertRel( ScMoveMode eDir )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     sal_Int32 nLine = GetFirstVisLine();
247cdf0e10cSrcweir     switch( eDir )
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         case MOVE_PREV:     --nLine;                        break;
250cdf0e10cSrcweir         case MOVE_NEXT:     ++nLine;                        break;
251cdf0e10cSrcweir         case MOVE_PREVPAGE: nLine -= GetVisLineCount() - 1; break;
252cdf0e10cSrcweir         case MOVE_NEXTPAGE: nLine += GetVisLineCount() - 1; break;
253cdf0e10cSrcweir         default:
254cdf0e10cSrcweir         {
255cdf0e10cSrcweir             // added to avoid warnings
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir     Execute( CSVCMD_SETLINEOFFSET, nLine );
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir // split handling -------------------------------------------------------------
263cdf0e10cSrcweir 
GetNoScrollPos(sal_Int32 nPos) const264cdf0e10cSrcweir sal_Int32 ScCsvRuler::GetNoScrollPos( sal_Int32 nPos ) const
265cdf0e10cSrcweir {
266cdf0e10cSrcweir     sal_Int32 nNewPos = nPos;
267cdf0e10cSrcweir     if( nNewPos != CSV_POS_INVALID )
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir         if( nNewPos < GetFirstVisPos() + CSV_SCROLL_DIST )
270cdf0e10cSrcweir         {
271cdf0e10cSrcweir             sal_Int32 nScroll = (GetFirstVisPos() > 0) ? CSV_SCROLL_DIST : 0;
272cdf0e10cSrcweir             nNewPos = Max( nPos, GetFirstVisPos() + nScroll );
273cdf0e10cSrcweir         }
274cdf0e10cSrcweir         else if( nNewPos > GetLastVisPos() - CSV_SCROLL_DIST - 1L )
275cdf0e10cSrcweir         {
276cdf0e10cSrcweir             sal_Int32 nScroll = (GetFirstVisPos() < GetMaxPosOffset()) ? CSV_SCROLL_DIST : 0;
277cdf0e10cSrcweir             nNewPos = Min( nNewPos, GetLastVisPos() - nScroll - sal_Int32( 1 ) );
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir     return nNewPos;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
InsertSplit(sal_Int32 nPos)283cdf0e10cSrcweir void ScCsvRuler::InsertSplit( sal_Int32 nPos )
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     if( maSplits.Insert( nPos ) )
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         ImplDrawSplit( nPos );
288cdf0e10cSrcweir         Repaint();
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
RemoveSplit(sal_Int32 nPos)292cdf0e10cSrcweir void ScCsvRuler::RemoveSplit( sal_Int32 nPos )
293cdf0e10cSrcweir {
294cdf0e10cSrcweir     if( maSplits.Remove( nPos ) )
295cdf0e10cSrcweir     {
296cdf0e10cSrcweir         ImplEraseSplit( nPos );
297cdf0e10cSrcweir         Repaint();
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
MoveSplit(sal_Int32 nPos,sal_Int32 nNewPos)301cdf0e10cSrcweir void ScCsvRuler::MoveSplit( sal_Int32 nPos, sal_Int32 nNewPos )
302cdf0e10cSrcweir {
303cdf0e10cSrcweir     bool bRemove = maSplits.Remove( nPos );
304cdf0e10cSrcweir     bool bInsert = maSplits.Insert( nNewPos );
305cdf0e10cSrcweir     if( bRemove || bInsert )
306cdf0e10cSrcweir     {
307cdf0e10cSrcweir         ImplEraseSplit( nPos );
308cdf0e10cSrcweir         ImplDrawSplit( nNewPos );
309cdf0e10cSrcweir         Repaint();
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
RemoveAllSplits()313cdf0e10cSrcweir void ScCsvRuler::RemoveAllSplits()
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     maSplits.Clear();
316cdf0e10cSrcweir     Repaint( true );
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
FindEmptyPos(sal_Int32 nPos,ScMoveMode eDir) const319cdf0e10cSrcweir sal_Int32 ScCsvRuler::FindEmptyPos( sal_Int32 nPos, ScMoveMode eDir ) const
320cdf0e10cSrcweir {
321cdf0e10cSrcweir     sal_Int32 nNewPos = nPos;
322cdf0e10cSrcweir     if( nNewPos != CSV_POS_INVALID )
323cdf0e10cSrcweir     {
324cdf0e10cSrcweir         switch( eDir )
325cdf0e10cSrcweir         {
326cdf0e10cSrcweir             case MOVE_FIRST:
327cdf0e10cSrcweir                 nNewPos = Min( nPos, FindEmptyPos( 0, MOVE_NEXT ) );
328cdf0e10cSrcweir             break;
329cdf0e10cSrcweir             case MOVE_LAST:
330cdf0e10cSrcweir                 nNewPos = Max( nPos, FindEmptyPos( GetPosCount(), MOVE_PREV ) );
331cdf0e10cSrcweir             break;
332cdf0e10cSrcweir             case MOVE_PREV:
333cdf0e10cSrcweir                 while( HasSplit( --nNewPos ) ) ;
334cdf0e10cSrcweir             break;
335cdf0e10cSrcweir             case MOVE_NEXT:
336cdf0e10cSrcweir                 while( HasSplit( ++nNewPos ) ) ;
337cdf0e10cSrcweir             break;
338cdf0e10cSrcweir             default:
339cdf0e10cSrcweir             {
340cdf0e10cSrcweir                 // added to avoid warnings
341cdf0e10cSrcweir             }
342cdf0e10cSrcweir         }
343cdf0e10cSrcweir     }
344cdf0e10cSrcweir     return IsValidSplitPos( nNewPos ) ? nNewPos : CSV_POS_INVALID;
345cdf0e10cSrcweir }
346cdf0e10cSrcweir 
MoveCurrSplit(sal_Int32 nNewPos)347cdf0e10cSrcweir void ScCsvRuler::MoveCurrSplit( sal_Int32 nNewPos )
348cdf0e10cSrcweir {
349cdf0e10cSrcweir     DisableRepaint();
350cdf0e10cSrcweir     Execute( CSVCMD_MOVESPLIT, GetRulerCursorPos(), nNewPos );
351cdf0e10cSrcweir     MoveCursor( nNewPos );
352cdf0e10cSrcweir     EnableRepaint();
353cdf0e10cSrcweir }
354cdf0e10cSrcweir 
MoveCurrSplitRel(ScMoveMode eDir)355cdf0e10cSrcweir void ScCsvRuler::MoveCurrSplitRel( ScMoveMode eDir )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir     if( HasSplit( GetRulerCursorPos() ) )
358cdf0e10cSrcweir     {
359cdf0e10cSrcweir         sal_Int32 nNewPos = FindEmptyPos( GetRulerCursorPos(), eDir );
360cdf0e10cSrcweir         if( nNewPos != CSV_POS_INVALID )
361cdf0e10cSrcweir             MoveCurrSplit( nNewPos );
362cdf0e10cSrcweir     }
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 
366cdf0e10cSrcweir // event handling -------------------------------------------------------------
367cdf0e10cSrcweir 
Resize()368cdf0e10cSrcweir void ScCsvRuler::Resize()
369cdf0e10cSrcweir {
370cdf0e10cSrcweir     ScCsvControl::Resize();
371cdf0e10cSrcweir     InitSizeData();
372cdf0e10cSrcweir     Repaint();
373cdf0e10cSrcweir }
374cdf0e10cSrcweir 
GetFocus()375cdf0e10cSrcweir void ScCsvRuler::GetFocus()
376cdf0e10cSrcweir {
377cdf0e10cSrcweir     ScCsvControl::GetFocus();
378cdf0e10cSrcweir     DisableRepaint();
379cdf0e10cSrcweir     if( GetRulerCursorPos() == CSV_POS_INVALID )
380cdf0e10cSrcweir         MoveCursor( GetNoScrollPos( mnPosCursorLast ) );
381cdf0e10cSrcweir     EnableRepaint();
382cdf0e10cSrcweir }
383cdf0e10cSrcweir 
LoseFocus()384cdf0e10cSrcweir void ScCsvRuler::LoseFocus()
385cdf0e10cSrcweir {
386cdf0e10cSrcweir     ScCsvControl::LoseFocus();
387cdf0e10cSrcweir     mnPosCursorLast = GetRulerCursorPos();
388cdf0e10cSrcweir     MoveCursor( CSV_POS_INVALID );
389cdf0e10cSrcweir }
390cdf0e10cSrcweir 
DataChanged(const DataChangedEvent & rDCEvt)391cdf0e10cSrcweir void ScCsvRuler::DataChanged( const DataChangedEvent& rDCEvt )
392cdf0e10cSrcweir {
393cdf0e10cSrcweir     if( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
394cdf0e10cSrcweir     {
395cdf0e10cSrcweir         InitColors();
396cdf0e10cSrcweir         Repaint();
397cdf0e10cSrcweir     }
398cdf0e10cSrcweir     ScCsvControl::DataChanged( rDCEvt );
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
MouseButtonDown(const MouseEvent & rMEvt)401cdf0e10cSrcweir void ScCsvRuler::MouseButtonDown( const MouseEvent& rMEvt )
402cdf0e10cSrcweir {
403cdf0e10cSrcweir     DisableRepaint();
404cdf0e10cSrcweir     if( !HasFocus() )
405cdf0e10cSrcweir         GrabFocus();
406cdf0e10cSrcweir     if( rMEvt.IsLeft() )
407cdf0e10cSrcweir     {
408cdf0e10cSrcweir         sal_Int32 nPos = GetPosFromX( rMEvt.GetPosPixel().X() );
409cdf0e10cSrcweir         if( IsVisibleSplitPos( nPos ) )
410cdf0e10cSrcweir             StartMouseTracking( nPos );
411cdf0e10cSrcweir         ImplSetMousePointer( nPos );
412cdf0e10cSrcweir     }
413cdf0e10cSrcweir     EnableRepaint();
414cdf0e10cSrcweir }
415cdf0e10cSrcweir 
MouseMove(const MouseEvent & rMEvt)416cdf0e10cSrcweir void ScCsvRuler::MouseMove( const MouseEvent& rMEvt )
417cdf0e10cSrcweir {
418cdf0e10cSrcweir     if( !rMEvt.IsModifierChanged() )
419cdf0e10cSrcweir     {
420cdf0e10cSrcweir         sal_Int32 nPos = GetPosFromX( rMEvt.GetPosPixel().X() );
421cdf0e10cSrcweir         if( IsTracking() )
422cdf0e10cSrcweir         {
423cdf0e10cSrcweir             // on mouse tracking: keep position valid
424cdf0e10cSrcweir             nPos = Max( Min( nPos, GetPosCount() - sal_Int32( 1 ) ), sal_Int32( 1 ) );
425cdf0e10cSrcweir             MoveMouseTracking( nPos );
426cdf0e10cSrcweir         }
427cdf0e10cSrcweir         else
428cdf0e10cSrcweir         {
429cdf0e10cSrcweir             Point aPoint;
430cdf0e10cSrcweir             Rectangle aRect( aPoint, maWinSize );
431cdf0e10cSrcweir             if( !IsVisibleSplitPos( nPos ) || !aRect.IsInside( rMEvt.GetPosPixel() ) )
432cdf0e10cSrcweir                 // if focused, keep old cursor position for key input
433cdf0e10cSrcweir                 nPos = HasFocus() ? GetRulerCursorPos() : CSV_POS_INVALID;
434cdf0e10cSrcweir             MoveCursor( nPos, false );
435cdf0e10cSrcweir         }
436cdf0e10cSrcweir         ImplSetMousePointer( nPos );
437cdf0e10cSrcweir     }
438cdf0e10cSrcweir }
439cdf0e10cSrcweir 
Tracking(const TrackingEvent & rTEvt)440cdf0e10cSrcweir void ScCsvRuler::Tracking( const TrackingEvent& rTEvt )
441cdf0e10cSrcweir {
442cdf0e10cSrcweir     if( rTEvt.IsTrackingEnded() || rTEvt.IsTrackingRepeat() )
443cdf0e10cSrcweir         MouseMove( rTEvt.GetMouseEvent() );
444cdf0e10cSrcweir     if( rTEvt.IsTrackingEnded() )
445cdf0e10cSrcweir         EndMouseTracking( !rTEvt.IsTrackingCanceled() );
446cdf0e10cSrcweir }
447cdf0e10cSrcweir 
KeyInput(const KeyEvent & rKEvt)448cdf0e10cSrcweir void ScCsvRuler::KeyInput( const KeyEvent& rKEvt )
449cdf0e10cSrcweir {
450cdf0e10cSrcweir     const KeyCode& rKCode = rKEvt.GetKeyCode();
451cdf0e10cSrcweir     sal_uInt16 nCode = rKCode.GetCode();
452cdf0e10cSrcweir     bool bNoMod = !rKCode.GetModifier();
453cdf0e10cSrcweir     bool bShift = (rKCode.GetModifier() == KEY_SHIFT);
454cdf0e10cSrcweir     bool bJump = (rKCode.GetModifier() == KEY_MOD1);
455cdf0e10cSrcweir     bool bMove = (rKCode.GetModifier() == (KEY_MOD1 | KEY_SHIFT));
456cdf0e10cSrcweir 
457cdf0e10cSrcweir     ScMoveMode eHDir = GetHorzDirection( nCode, true );
458cdf0e10cSrcweir     ScMoveMode eVDir = GetVertDirection( nCode, false );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     if( bNoMod )
461cdf0e10cSrcweir     {
462cdf0e10cSrcweir         if( eHDir != MOVE_NONE )
463cdf0e10cSrcweir             MoveCursorRel( eHDir );
464cdf0e10cSrcweir         else if( eVDir != MOVE_NONE )
465cdf0e10cSrcweir             ScrollVertRel( eVDir );
466cdf0e10cSrcweir         else switch( nCode )
467cdf0e10cSrcweir         {
468cdf0e10cSrcweir             case KEY_SPACE:     Execute( CSVCMD_TOGGLESPLIT, GetRulerCursorPos() ); break;
469cdf0e10cSrcweir             case KEY_INSERT:    Execute( CSVCMD_INSERTSPLIT, GetRulerCursorPos() ); break;
470cdf0e10cSrcweir             case KEY_DELETE:    Execute( CSVCMD_REMOVESPLIT, GetRulerCursorPos() ); break;
471cdf0e10cSrcweir         }
472cdf0e10cSrcweir     }
473cdf0e10cSrcweir     else if( bJump && (eHDir != MOVE_NONE) )
474cdf0e10cSrcweir         MoveCursorToSplit( eHDir );
475cdf0e10cSrcweir     else if( bMove && (eHDir != MOVE_NONE) )
476cdf0e10cSrcweir         MoveCurrSplitRel( eHDir );
477cdf0e10cSrcweir     else if( bShift && (nCode == KEY_DELETE) )
478cdf0e10cSrcweir         Execute( CSVCMD_REMOVEALLSPLITS );
479cdf0e10cSrcweir 
480cdf0e10cSrcweir     if( rKCode.GetGroup() != KEYGROUP_CURSOR )
481cdf0e10cSrcweir         ScCsvControl::KeyInput( rKEvt );
482cdf0e10cSrcweir }
483cdf0e10cSrcweir 
StartMouseTracking(sal_Int32 nPos)484cdf0e10cSrcweir void ScCsvRuler::StartMouseTracking( sal_Int32 nPos )
485cdf0e10cSrcweir {
486cdf0e10cSrcweir     mnPosMTStart = mnPosMTCurr = nPos;
487cdf0e10cSrcweir     mbPosMTMoved = false;
488cdf0e10cSrcweir     maOldSplits = maSplits;
489cdf0e10cSrcweir     Execute( CSVCMD_INSERTSPLIT, nPos );
490cdf0e10cSrcweir     if( HasSplit( nPos ) )
491cdf0e10cSrcweir         StartTracking( STARTTRACK_BUTTONREPEAT );
492cdf0e10cSrcweir }
493cdf0e10cSrcweir 
MoveMouseTracking(sal_Int32 nPos)494cdf0e10cSrcweir void ScCsvRuler::MoveMouseTracking( sal_Int32 nPos )
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     if( mnPosMTCurr != nPos )
497cdf0e10cSrcweir     {
498cdf0e10cSrcweir         DisableRepaint();
499cdf0e10cSrcweir         MoveCursor( nPos );
500cdf0e10cSrcweir         if( (mnPosMTCurr != mnPosMTStart) && maOldSplits.HasSplit( mnPosMTCurr ) )
501cdf0e10cSrcweir             Execute( CSVCMD_INSERTSPLIT, nPos );
502cdf0e10cSrcweir         else
503cdf0e10cSrcweir             Execute( CSVCMD_MOVESPLIT, mnPosMTCurr, nPos );
504cdf0e10cSrcweir         mnPosMTCurr = nPos;
505cdf0e10cSrcweir         mbPosMTMoved = true;
506cdf0e10cSrcweir         EnableRepaint();
507cdf0e10cSrcweir     }
508cdf0e10cSrcweir }
509cdf0e10cSrcweir 
EndMouseTracking(bool bApply)510cdf0e10cSrcweir void ScCsvRuler::EndMouseTracking( bool bApply )
511cdf0e10cSrcweir {
512cdf0e10cSrcweir     if( bApply )    // tracking finished successfully
513cdf0e10cSrcweir     {
514cdf0e10cSrcweir         // remove on simple click on an existing split
515cdf0e10cSrcweir         if( (mnPosMTCurr == mnPosMTStart) && maOldSplits.HasSplit( mnPosMTCurr ) && !mbPosMTMoved )
516cdf0e10cSrcweir             Execute( CSVCMD_REMOVESPLIT, mnPosMTCurr );
517cdf0e10cSrcweir     }
518cdf0e10cSrcweir     else            // tracking cancelled
519cdf0e10cSrcweir     {
520cdf0e10cSrcweir         MoveCursor( mnPosMTStart );
521cdf0e10cSrcweir         // move split to origin
522cdf0e10cSrcweir         if( maOldSplits.HasSplit( mnPosMTStart ) )
523cdf0e10cSrcweir             MoveMouseTracking( mnPosMTStart );
524cdf0e10cSrcweir         // remove temporarily inserted split
525cdf0e10cSrcweir         else if( !maOldSplits.HasSplit( mnPosMTCurr ) )
526cdf0e10cSrcweir             Execute( CSVCMD_REMOVESPLIT, mnPosMTCurr );
527cdf0e10cSrcweir     }
528cdf0e10cSrcweir     mnPosMTStart = CSV_POS_INVALID;
529cdf0e10cSrcweir }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 
532cdf0e10cSrcweir // painting -------------------------------------------------------------------
533cdf0e10cSrcweir 
Paint(const Rectangle &)534cdf0e10cSrcweir void ScCsvRuler::Paint( const Rectangle& )
535cdf0e10cSrcweir {
536cdf0e10cSrcweir     Repaint();
537cdf0e10cSrcweir }
538cdf0e10cSrcweir 
ImplRedraw()539cdf0e10cSrcweir void ScCsvRuler::ImplRedraw()
540cdf0e10cSrcweir {
541cdf0e10cSrcweir     if( IsVisible() )
542cdf0e10cSrcweir     {
543cdf0e10cSrcweir         if( !IsValidGfx() )
544cdf0e10cSrcweir         {
545cdf0e10cSrcweir             ValidateGfx();
546cdf0e10cSrcweir             ImplDrawBackgrDev();
547cdf0e10cSrcweir             ImplDrawRulerDev();
548cdf0e10cSrcweir         }
549cdf0e10cSrcweir         DrawOutDev( Point(), maWinSize, Point(), maWinSize, maRulerDev );
550cdf0e10cSrcweir         ImplDrawTrackingRect();
551cdf0e10cSrcweir     }
552cdf0e10cSrcweir }
553cdf0e10cSrcweir 
ImplDrawArea(sal_Int32 nPosX,sal_Int32 nWidth)554cdf0e10cSrcweir void ScCsvRuler::ImplDrawArea( sal_Int32 nPosX, sal_Int32 nWidth )
555cdf0e10cSrcweir {
556cdf0e10cSrcweir     maBackgrDev.SetLineColor();
557cdf0e10cSrcweir     Rectangle aRect( Point( nPosX, 0 ), Size( nWidth, GetHeight() ) );
558cdf0e10cSrcweir     maBackgrDev.SetFillColor( maBackColor );
559cdf0e10cSrcweir     maBackgrDev.DrawRect( aRect );
560cdf0e10cSrcweir 
561cdf0e10cSrcweir     aRect = maActiveRect;
562cdf0e10cSrcweir     aRect.Left() = Max( GetFirstX(), nPosX );
563cdf0e10cSrcweir     aRect.Right() = Min( Min( GetX( GetPosCount() ), GetLastX() ), nPosX + nWidth - sal_Int32( 1 ) );
564cdf0e10cSrcweir     if( aRect.Left() <= aRect.Right() )
565cdf0e10cSrcweir     {
566cdf0e10cSrcweir         maBackgrDev.SetFillColor( maActiveColor );
567cdf0e10cSrcweir         maBackgrDev.DrawRect( aRect );
568cdf0e10cSrcweir     }
569cdf0e10cSrcweir 
570cdf0e10cSrcweir     maBackgrDev.SetLineColor( maTextColor );
571cdf0e10cSrcweir     sal_Int32 nY = GetHeight() - 1;
572cdf0e10cSrcweir     maBackgrDev.DrawLine( Point( nPosX, nY ), Point( nPosX + nWidth - 1, nY ) );
573cdf0e10cSrcweir }
574cdf0e10cSrcweir 
ImplDrawBackgrDev()575cdf0e10cSrcweir void ScCsvRuler::ImplDrawBackgrDev()
576cdf0e10cSrcweir {
577cdf0e10cSrcweir     ImplDrawArea( 0, GetWidth() );
578cdf0e10cSrcweir 
579cdf0e10cSrcweir     // scale
580cdf0e10cSrcweir     maBackgrDev.SetLineColor( maTextColor );
581cdf0e10cSrcweir     maBackgrDev.SetFillColor();
582cdf0e10cSrcweir     sal_Int32 nPos;
583cdf0e10cSrcweir 
584cdf0e10cSrcweir     sal_Int32 nFirstPos = Max( GetPosFromX( 0 ) - (sal_Int32)(1L), (sal_Int32)(0L) );
585cdf0e10cSrcweir     sal_Int32 nLastPos = GetPosFromX( GetWidth() );
586cdf0e10cSrcweir     sal_Int32 nY = (maActiveRect.Top() + maActiveRect.Bottom()) / 2;
587cdf0e10cSrcweir     for( nPos = nFirstPos; nPos <= nLastPos; ++nPos )
588cdf0e10cSrcweir     {
589cdf0e10cSrcweir         sal_Int32 nX = GetX( nPos );
590cdf0e10cSrcweir         if( nPos % 5 )
591cdf0e10cSrcweir             maBackgrDev.DrawPixel( Point( nX, nY ) );
592cdf0e10cSrcweir         else
593cdf0e10cSrcweir             maBackgrDev.DrawLine( Point( nX, nY - 1 ), Point( nX, nY + 1 ) );
594cdf0e10cSrcweir     }
595cdf0e10cSrcweir 
596cdf0e10cSrcweir     // texts
597cdf0e10cSrcweir     maBackgrDev.SetTextColor( maTextColor );
598cdf0e10cSrcweir     maBackgrDev.SetTextFillColor();
599cdf0e10cSrcweir     for( nPos = ((nFirstPos + 9) / 10) * 10; nPos <= nLastPos; nPos += 10 )
600cdf0e10cSrcweir     {
601cdf0e10cSrcweir         String aText( String::CreateFromInt32( nPos ) );
602cdf0e10cSrcweir         sal_Int32 nTextWidth = maBackgrDev.GetTextWidth( aText );
603cdf0e10cSrcweir         sal_Int32 nTextX = GetX( nPos ) - nTextWidth / 2;
604cdf0e10cSrcweir         ImplDrawArea( nTextX - 1, nTextWidth + 2 );
605cdf0e10cSrcweir         maBackgrDev.DrawText( Point( nTextX, maActiveRect.Top() ), aText );
606cdf0e10cSrcweir     }
607cdf0e10cSrcweir }
608cdf0e10cSrcweir 
ImplDrawSplit(sal_Int32 nPos)609cdf0e10cSrcweir void ScCsvRuler::ImplDrawSplit( sal_Int32 nPos )
610cdf0e10cSrcweir {
611cdf0e10cSrcweir     if( IsVisibleSplitPos( nPos ) )
612cdf0e10cSrcweir     {
613cdf0e10cSrcweir         Point aPos( GetX( nPos ) - mnSplitSize / 2, GetHeight() - mnSplitSize - 2 );
614cdf0e10cSrcweir         Size aSize( mnSplitSize, mnSplitSize );
615cdf0e10cSrcweir         maRulerDev.SetLineColor( maTextColor );
616cdf0e10cSrcweir         maRulerDev.SetFillColor( maSplitColor );
617cdf0e10cSrcweir         maRulerDev.DrawEllipse( Rectangle( aPos, aSize ) );
618cdf0e10cSrcweir         maRulerDev.DrawPixel( Point( GetX( nPos ), GetHeight() - 2 ) );
619cdf0e10cSrcweir     }
620cdf0e10cSrcweir }
621cdf0e10cSrcweir 
ImplEraseSplit(sal_Int32 nPos)622cdf0e10cSrcweir void ScCsvRuler::ImplEraseSplit( sal_Int32 nPos )
623cdf0e10cSrcweir {
624cdf0e10cSrcweir     if( IsVisibleSplitPos( nPos ) )
625cdf0e10cSrcweir     {
626cdf0e10cSrcweir         ImplInvertCursor( GetRulerCursorPos() );
627cdf0e10cSrcweir         Point aPos( GetX( nPos ) - mnSplitSize / 2, 0 );
628cdf0e10cSrcweir         Size aSize( mnSplitSize, GetHeight() );
629cdf0e10cSrcweir         maRulerDev.DrawOutDev( aPos, aSize, aPos, aSize, maBackgrDev );
630cdf0e10cSrcweir         ImplInvertCursor( GetRulerCursorPos() );
631cdf0e10cSrcweir     }
632cdf0e10cSrcweir }
633cdf0e10cSrcweir 
ImplDrawRulerDev()634cdf0e10cSrcweir void ScCsvRuler::ImplDrawRulerDev()
635cdf0e10cSrcweir {
636cdf0e10cSrcweir     maRulerDev.DrawOutDev( Point(), maWinSize, Point(), maWinSize, maBackgrDev );
637cdf0e10cSrcweir     ImplInvertCursor( GetRulerCursorPos() );
638cdf0e10cSrcweir 
639cdf0e10cSrcweir     sal_uInt32 nFirst = maSplits.LowerBound( GetFirstVisPos() );
640cdf0e10cSrcweir     sal_uInt32 nLast = maSplits.UpperBound( GetLastVisPos() );
641cdf0e10cSrcweir     if( (nFirst != CSV_VEC_NOTFOUND) && (nLast != CSV_VEC_NOTFOUND) )
642cdf0e10cSrcweir         for( sal_uInt32 nIndex = nFirst; nIndex <= nLast; ++nIndex )
643cdf0e10cSrcweir             ImplDrawSplit( GetSplitPos( nIndex ) );
644cdf0e10cSrcweir }
645cdf0e10cSrcweir 
ImplInvertCursor(sal_Int32 nPos)646cdf0e10cSrcweir void ScCsvRuler::ImplInvertCursor( sal_Int32 nPos )
647cdf0e10cSrcweir {
648cdf0e10cSrcweir     if( IsVisibleSplitPos( nPos ) )
649cdf0e10cSrcweir     {
650cdf0e10cSrcweir         ImplInvertRect( maRulerDev, Rectangle( Point( GetX( nPos ) - 1, 0 ), Size( 3, GetHeight() - 1 ) ) );
651cdf0e10cSrcweir         if( HasSplit( nPos ) )
652cdf0e10cSrcweir             ImplDrawSplit( nPos );
653cdf0e10cSrcweir     }
654cdf0e10cSrcweir }
655cdf0e10cSrcweir 
ImplDrawTrackingRect()656cdf0e10cSrcweir void ScCsvRuler::ImplDrawTrackingRect()
657cdf0e10cSrcweir {
658cdf0e10cSrcweir     if( HasFocus() )
659cdf0e10cSrcweir         InvertTracking( Rectangle( 0, 0, GetWidth() - 1, GetHeight() - 2 ),
660cdf0e10cSrcweir             SHOWTRACK_SMALL | SHOWTRACK_WINDOW );
661cdf0e10cSrcweir }
662cdf0e10cSrcweir 
ImplSetMousePointer(sal_Int32 nPos)663cdf0e10cSrcweir void ScCsvRuler::ImplSetMousePointer( sal_Int32 nPos )
664cdf0e10cSrcweir {
665cdf0e10cSrcweir     SetPointer( Pointer( HasSplit( nPos ) ? POINTER_HSPLIT : POINTER_ARROW ) );
666cdf0e10cSrcweir }
667cdf0e10cSrcweir 
668cdf0e10cSrcweir 
669cdf0e10cSrcweir // accessibility ==============================================================
670cdf0e10cSrcweir 
ImplCreateAccessible()671cdf0e10cSrcweir ScAccessibleCsvControl* ScCsvRuler::ImplCreateAccessible()
672cdf0e10cSrcweir {
673cdf0e10cSrcweir     return new ScAccessibleCsvRuler( *this );
674cdf0e10cSrcweir }
675cdf0e10cSrcweir 
676cdf0e10cSrcweir 
677cdf0e10cSrcweir // ============================================================================
678cdf0e10cSrcweir 
679