xref: /trunk/main/sc/source/ui/inc/csvcontrol.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // ============================================================================
29 
30 #ifndef _SC_CSVCONTROL_HXX
31 #define _SC_CSVCONTROL_HXX
32 
33 #include <vcl/ctrl.hxx>
34 #include "scdllapi.h"
35 #include "global.hxx"
36 #include "address.hxx"
37 #include "csvsplits.hxx"
38 #include <com/sun/star/uno/Reference.hxx>
39 
40 
41 class ScAccessibleCsvControl;
42 namespace com { namespace sun { namespace star { namespace accessibility {
43     class XAccessible;
44 } } } }
45 
46 
47 // ============================================================================
48 
49 /** Minimum character count for a column in separators mode. */
50 const sal_Int32 CSV_MINCOLWIDTH         = 8;
51 /** Maximum length of a cell string. */
52 const xub_StrLen CSV_MAXSTRLEN          = 0x7FFF;
53 /** Transparency for header color of selected columns. */
54 const sal_uInt16 CSV_HDR_TRANSPARENCY   = 85;
55 /** Minimum distance to border for auto scroll. */
56 const sal_Int32 CSV_SCROLL_DIST         = 3;
57 
58 //! TODO make string array dynamic
59 const sal_Int32 CSV_PREVIEW_LINES       = 32; // maximum count of preview lines
60 /** Maximum count of columns. */
61 const sal_Int32 CSV_MAXCOLCOUNT         = MAXCOLCOUNT;
62 
63 /** Default column data type. */
64 const sal_Int32 CSV_TYPE_DEFAULT        = 0;
65 /** Multi selection with different types. */
66 const sal_Int32 CSV_TYPE_MULTI          = -1;
67 /** No column selected. */
68 const sal_Int32 CSV_TYPE_NOSELECTION    = -2;
69 
70 // External used column types.
71 const sal_uInt8 SC_COL_STANDARD         = 1;
72 const sal_uInt8 SC_COL_TEXT             = 2;
73 const sal_uInt8 SC_COL_MDY              = 3;
74 const sal_uInt8 SC_COL_DMY              = 4;
75 const sal_uInt8 SC_COL_YMD              = 5;
76 const sal_uInt8 SC_COL_SKIP             = 9;
77 const sal_uInt8 SC_COL_ENGLISH          = 10;
78 
79 
80 // ============================================================================
81 
82 /** Exported data of a column (data used in the dialog). */
83 struct ScCsvExpData
84 {
85     xub_StrLen                  mnIndex;        /// Index of a column.
86     sal_uInt8                   mnType;         /// External type of the column.
87 
88     inline                      ScCsvExpData() : mnIndex( 0 ), mnType( SC_COL_STANDARD ) {}
89     inline                      ScCsvExpData( xub_StrLen nIndex, sal_uInt8 nType ) :
90                                     mnIndex( nIndex ), mnType( nType ) {}
91 };
92 
93 typedef ::std::vector< ScCsvExpData > ScCsvExpDataVec;
94 
95 
96 // ============================================================================
97 
98 /** Specifies which element should be used to perform an action. */
99 enum ScMoveMode
100 {
101     MOVE_NONE,                  /// No action.
102     MOVE_FIRST,                 /// First element in current context.
103     MOVE_LAST,                  /// Last element in current context.
104     MOVE_PREV,                  /// Predecessor of current element in current context.
105     MOVE_NEXT,                  /// Successor of current element in current context.
106     MOVE_PREVPAGE,              /// Previous page relative to current context.
107     MOVE_NEXTPAGE               /// Next page relative to current context.
108 };
109 
110 
111 // ============================================================================
112 
113 /** Flags for comparison of old and new control layout data. */
114 typedef sal_uInt32 ScCsvDiff;
115 
116 const ScCsvDiff CSV_DIFF_EQUAL          = 0x00000000;
117 const ScCsvDiff CSV_DIFF_POSCOUNT       = 0x00000001;
118 const ScCsvDiff CSV_DIFF_POSOFFSET      = 0x00000002;
119 const ScCsvDiff CSV_DIFF_HDRWIDTH       = 0x00000004;
120 const ScCsvDiff CSV_DIFF_CHARWIDTH      = 0x00000008;
121 const ScCsvDiff CSV_DIFF_LINECOUNT      = 0x00000010;
122 const ScCsvDiff CSV_DIFF_LINEOFFSET     = 0x00000020;
123 const ScCsvDiff CSV_DIFF_HDRHEIGHT      = 0x00000040;
124 const ScCsvDiff CSV_DIFF_LINEHEIGHT     = 0x00000080;
125 const ScCsvDiff CSV_DIFF_RULERCURSOR    = 0x00000100;
126 const ScCsvDiff CSV_DIFF_GRIDCURSOR     = 0x00000200;
127 
128 const ScCsvDiff CSV_DIFF_HORIZONTAL     = CSV_DIFF_POSCOUNT | CSV_DIFF_POSOFFSET | CSV_DIFF_HDRWIDTH | CSV_DIFF_CHARWIDTH;
129 const ScCsvDiff CSV_DIFF_VERTICAL       = CSV_DIFF_LINECOUNT | CSV_DIFF_LINEOFFSET | CSV_DIFF_HDRHEIGHT | CSV_DIFF_LINEHEIGHT;
130 const ScCsvDiff CSV_DIFF_CURSOR         = CSV_DIFF_RULERCURSOR | CSV_DIFF_GRIDCURSOR;
131 
132 
133 // ----------------------------------------------------------------------------
134 
135 /** A structure containing all layout data valid for both ruler and data grid
136     (i.e. scroll position or column width). */
137 struct ScCsvLayoutData
138 {
139     // horizontal settings
140     sal_Int32                   mnPosCount;         /// Number of positions.
141     sal_Int32                   mnPosOffset;        /// Horizontal scroll offset.
142 
143     sal_Int32                   mnWinWidth;         /// Width of ruler and data grid.
144     sal_Int32                   mnHdrWidth;         /// Width of the header column.
145     sal_Int32                   mnCharWidth;        /// Pixel width of one character.
146 
147     // vertical settings
148     sal_Int32                   mnLineCount;        /// Number of data lines.
149     sal_Int32                   mnLineOffset;       /// Index of first visible line (0-based).
150 
151     sal_Int32                   mnWinHeight;        /// Height of entire data grid (incl. header).
152     sal_Int32                   mnHdrHeight;        /// Height of the header line.
153     sal_Int32                   mnLineHeight;       /// Height of a data line.
154 
155     // cursor settings
156     sal_Int32                   mnPosCursor;        /// Position of ruler cursor.
157     sal_Int32                   mnColCursor;        /// Position of grid column cursor.
158 
159     mutable sal_Int32           mnNoRepaint;        /// >0 = no repaint.
160     bool                        mbAppRTL;           /// true = application in RTL mode.
161 
162     explicit                    ScCsvLayoutData();
163 
164     /** Returns differences to rData.
165         @descr  For each difference the appropriate bit is set in the returned value. */
166     ScCsvDiff                   GetDiff( const ScCsvLayoutData& rData ) const;
167 };
168 
169 inline bool operator==( const ScCsvLayoutData& rData1, const ScCsvLayoutData& rData2 )
170 {
171     return rData1.GetDiff( rData2 ) == CSV_DIFF_EQUAL;
172 }
173 
174 inline bool operator!=( const ScCsvLayoutData& rData1, const ScCsvLayoutData& rData2 )
175 {
176     return !(rData1 == rData2);
177 }
178 
179 
180 // ============================================================================
181 
182 /** Enumeration of possible commands to change any settings of the CSV controls.
183     @descr  Controls have to send commands instead of changing their settings directly.
184     This helps to keep the different controls consistent to each other.
185     A command can contain 0 to 2 sal_Int32 parameters. In the description of each
186     command the required parameters are swown in brackets. [-] means no parameter. */
187 enum ScCsvCmdType
188 {
189     // misc
190     CSVCMD_NONE,                /// No command. [-]
191     CSVCMD_REPAINT,             /// Repaint all controls. [-]
192 
193     // modify horizontal dimensions
194     CSVCMD_SETPOSCOUNT,         /// Change position/column count. [character count]
195     CSVCMD_SETPOSOFFSET,        /// Change position offset (scroll pos). [position]
196     CSVCMD_SETHDRWIDTH,         /// Change width of the header column. [width in pixel]
197     CSVCMD_SETCHARWIDTH,        /// Change character pixel width. [width in pixel]
198 
199     // modify vertical dimensions
200     CSVCMD_SETLINECOUNT,        /// Change number of data lines. [line count]
201     CSVCMD_SETLINEOFFSET,       /// Change first visible line. [line index]
202     CSVCMD_SETHDRHEIGHT,        /// Change height of top header line. [height in pixel]
203     CSVCMD_SETLINEHEIGHT,       /// Change data line pixel height. [height in pixel}
204 
205     // cursors/positions
206     CSVCMD_MOVERULERCURSOR,     /// Move ruler cursor to new position. [position]
207     CSVCMD_MOVEGRIDCURSOR,      /// Move data grid cursor to new column. [position]
208     CSVCMD_MAKEPOSVISIBLE,      /// Move to make passed position visible (for mouse tracking). [position]
209 
210     // table contents
211     CSVCMD_NEWCELLTEXTS,        /// Recalculate splits and cell texts. [-]
212     CSVCMD_UPDATECELLTEXTS,     /// Update cell texts with current split settings. [-]
213     CSVCMD_SETCOLUMNTYPE,       /// Change data type of selected columns. [column type]
214     CSVCMD_EXPORTCOLUMNTYPE,    /// Send selected column type to external controls. [-]
215     CSVCMD_SETFIRSTIMPORTLINE,  /// Set number of first imported line. [line index]
216 
217     // splits
218     CSVCMD_INSERTSPLIT,         /// Insert a split. [position]
219     CSVCMD_REMOVESPLIT,         /// Remove a split. [position]
220     CSVCMD_TOGGLESPLIT,         /// Inserts or removes a split. [position]
221     CSVCMD_MOVESPLIT,           /// Move a split. [old position, new position]
222     CSVCMD_REMOVEALLSPLITS      /// Remove all splits. [-]
223 };
224 
225 
226 // ----------------------------------------------------------------------------
227 
228 /** Data for a CSV control command. The stored position data is aways character based,
229     it's never a column index (required for internal consistency). */
230 class ScCsvCmd
231 {
232 private:
233     ScCsvCmdType                meType;         /// The command.
234     sal_Int32                   mnParam1;       /// First parameter.
235     sal_Int32                   mnParam2;       /// Second parameter.
236 
237 public:
238     inline explicit             ScCsvCmd() : meType( CSVCMD_NONE ),
239                                     mnParam1( CSV_POS_INVALID ), mnParam2( CSV_POS_INVALID ) {}
240 
241     inline void                 Set( ScCsvCmdType eType, sal_Int32 nParam1, sal_Int32 nParam2 );
242 
243     inline ScCsvCmdType         GetType() const     { return meType; }
244     inline sal_Int32            GetParam1() const   { return mnParam1; }
245     inline sal_Int32            GetParam2() const   { return mnParam2; }
246 };
247 
248 inline void ScCsvCmd::Set( ScCsvCmdType eType, sal_Int32 nParam1, sal_Int32 nParam2 )
249 {
250     meType = eType; mnParam1 = nParam1; mnParam2 = nParam2;
251 }
252 
253 
254 // ============================================================================
255 
256 /** Base class for the CSV ruler and the data grid control. Implements command handling. */
257 class SC_DLLPUBLIC ScCsvControl : public Control
258 {
259 protected:
260     typedef ::std::vector< String >     StringVec;
261     typedef ::std::vector< StringVec >  StringVecVec;
262 
263     typedef ::com::sun::star::uno::Reference<
264         ::com::sun::star::accessibility::XAccessible > XAccessibleRef;
265 
266 private:
267     Link                        maCmdHdl;           /// External command handler.
268     ScCsvCmd                    maCmd;              /// Data of last command.
269     const ScCsvLayoutData&      mrData;             /// Shared layout data.
270 
271     XAccessibleRef              mxAccessible;       /// The accessible object of the control.
272     ScAccessibleCsvControl*     mpAccessible;       /// Pointer to the accessible implementation object.
273     bool                        mbValidGfx;         /// Content of virtual devices valid?
274 
275     // ------------------------------------------------------------------------
276 public:
277     explicit                    ScCsvControl( ScCsvControl& rParent );
278     explicit                    ScCsvControl( Window* pParent, const ScCsvLayoutData& rData, WinBits nStyle = 0 );
279     explicit                    ScCsvControl( Window* pParent, const ScCsvLayoutData& rData, const ResId& rResId );
280     virtual                     ~ScCsvControl();
281 
282     // event handling ---------------------------------------------------------
283 
284     virtual void                GetFocus();
285     virtual void                LoseFocus();
286 
287     /** Sends a GetFocus or LoseFocus event to the accessibility object. */
288     void                        AccSendFocusEvent( bool bFocused );
289     /** Sends a caret changed event to the accessibility object. */
290     void                        AccSendCaretEvent();
291     /** Sends a visible area changed event to the accessibility object. */
292     void                        AccSendVisibleEvent();
293     /** Sends a selection changed event to the accessibility object. */
294     void                        AccSendSelectionEvent();
295     /** Sends a table model changed event for changed cell contents to the accessibility object. */
296     void                        AccSendTableUpdateEvent( sal_uInt32 nFirstColumn, sal_uInt32 nLastColumn, bool bAllRows = true );
297     /** Sends a table model changed event for an inserted column to the accessibility object. */
298     void                        AccSendInsertColumnEvent( sal_uInt32 nFirstColumn, sal_uInt32 nLastColumn );
299     /** Sends a table model changed event for a removed column to the accessibility object. */
300     void                        AccSendRemoveColumnEvent( sal_uInt32 nFirstColumn, sal_uInt32 nLastColumn );
301 
302     // repaint helpers --------------------------------------------------------
303 
304     /** Sets the graphic invalid (next Redraw() will not use cached graphic). */
305     inline void                 InvalidateGfx() { mbValidGfx = false; }
306     /** Sets the graphic valid (next Redraw() will use cached graphic). */
307     inline void                 ValidateGfx() { mbValidGfx = true; }
308     /** Returns true, if cached graphic is valid. */
309     inline bool                 IsValidGfx() const { return mbValidGfx; }
310 
311     /** Repaints all controls.
312         @param bInvalidate  true = invalidates graphics of this control (not all). */
313     void                        Repaint( bool bInvalidate = false );
314     /** Increases no-repaint counter (controls do not repaint until the last EnableRepaint()). */
315     void                        DisableRepaint();
316     /** Decreases no-repaint counter and repaints if counter reaches 0.
317         @param bInvalidate  true = invalidates graphics of this control (not all). */
318     void                        EnableRepaint( bool bInvalidate = false );
319     /** Returns true, if controls will not repaint. */
320     inline bool                 IsNoRepaint() const { return mrData.mnNoRepaint > 0; }
321 
322     // command handling -------------------------------------------------------
323 
324     /** Sets a new command handler. */
325     inline void                 SetCmdHdl( const Link& rHdl ) { maCmdHdl = rHdl; }
326     /** Returns the current command handler. */
327     inline const Link&          GetCmdHdl() const { return maCmdHdl; }
328     /** Returns data of the last command. */
329     inline const ScCsvCmd&      GetCmd() const { return maCmd; }
330 
331     /** Executes a command by calling command handler. */
332     void                        Execute(
333                                     ScCsvCmdType eType,
334                                     sal_Int32 nParam1 = CSV_POS_INVALID,
335                                     sal_Int32 nParam2 = CSV_POS_INVALID );
336 
337     // layout helpers ---------------------------------------------------------
338 
339     /** Returns a reference to the current layout data. */
340     inline const ScCsvLayoutData& GetLayoutData() const { return mrData; }
341     /** Returns true, if the Right-to-Left layout mode is active. */
342     inline bool                 IsRTL() const { return mrData.mbAppRTL; }
343 
344     /** Returns the number of available positions. */
345     inline sal_Int32            GetPosCount() const { return mrData.mnPosCount; }
346     /** Returns the number of visible positions. */
347     sal_Int32                   GetVisPosCount() const;
348     /** Returns the first visible position. */
349     inline sal_Int32            GetFirstVisPos() const { return mrData.mnPosOffset; }
350     /** Returns the last visible position. */
351     inline sal_Int32            GetLastVisPos() const { return GetFirstVisPos() + GetVisPosCount(); }
352     /** Returns highest possible position for first visible character. */
353     sal_Int32                   GetMaxPosOffset() const;
354 
355     /** Returns true, if it is allowed to set a split at nPos. */
356     bool                        IsValidSplitPos( sal_Int32 nPos ) const;
357     /** Returns true, if nPos is an allowed AND visible split position. */
358     bool                        IsVisibleSplitPos( sal_Int32 nPos ) const;
359 
360     /** Returns the width of the header column. */
361     inline sal_Int32            GetHdrWidth() const { return mrData.mnHdrWidth; }
362     /** Returns the width of one character column. */
363     inline sal_Int32            GetCharWidth() const { return mrData.mnCharWidth; }
364     /** Returns the start position of the header column. */
365     sal_Int32                   GetHdrX() const;
366     /** Returns the X position of the first pixel of the data area. */
367     sal_Int32                   GetFirstX() const;
368     /** Returns the X position of the last pixel of the data area. */
369     sal_Int32                   GetLastX() const;
370     /** Returns output X coordinate of the specified position. */
371     sal_Int32                   GetX( sal_Int32 nPos ) const;
372     /** Returns position from output coordinate. */
373     sal_Int32                   GetPosFromX( sal_Int32 nX ) const;
374 
375     /** Returns the number of data lines. */
376     inline sal_Int32            GetLineCount() const { return mrData.mnLineCount; }
377     /** Returns the number of visible lines (including partly visible bottom line). */
378     sal_Int32                   GetVisLineCount() const;
379     /** Returns index of first visible line. */
380     inline sal_Int32            GetFirstVisLine() const { return mrData.mnLineOffset; }
381     /** Returns index of last visible line. */
382     sal_Int32                   GetLastVisLine() const;
383     /** Returns highest possible index for first line. */
384     sal_Int32                   GetMaxLineOffset() const;
385 
386     /** Returns true, if nLine is a valid line index. */
387     bool                        IsValidLine( sal_Int32 nLine ) const;
388     /** Returns true, if nLine is a valid and visible line index. */
389     bool                        IsVisibleLine( sal_Int32 nLine ) const;
390 
391     /** Returns the height of the header line. */
392     inline sal_Int32            GetHdrHeight() const { return mrData.mnHdrHeight; }
393     /** Returns the height of one line. */
394     inline sal_Int32            GetLineHeight() const { return mrData.mnLineHeight; }
395     /** Returns output Y coordinate of the specified line. */
396     sal_Int32                   GetY( sal_Int32 nLine ) const;
397     /** Returns line index from output coordinate. */
398     sal_Int32                   GetLineFromY( sal_Int32 nY ) const;
399 
400     /** Returns the ruler cursor position. */
401     inline sal_Int32            GetRulerCursorPos() const { return mrData.mnPosCursor; }
402     /** Returns the data grid cursor position (not column index!). */
403     inline sal_Int32            GetGridCursorPos() const { return mrData.mnColCursor; }
404 
405     // static helpers ---------------------------------------------------------
406 
407     /** Inverts a rectangle in the specified output device. */
408     static void                 ImplInvertRect( OutputDevice& rOutDev, const Rectangle& rRect );
409 
410     /** Returns direction code for the keys LEFT, RIGHT, HOME, END.
411         @param bHomeEnd  false = ignore HOME and END key. */
412     static ScMoveMode           GetHorzDirection( sal_uInt16 nCode, bool bHomeEnd );
413     /** Returns direction code for the keys UP, DOWN, HOME, END, PAGE UP, PAGE DOWN.
414         @param bHomeEnd  false = ignore HOME and END key. */
415     static ScMoveMode           GetVertDirection( sal_uInt16 nCode, bool bHomeEnd );
416 
417     // accessibility ----------------------------------------------------------
418 public:
419     /** Creates and returns the accessible object of this control. Do not overwrite in
420         derived classes, use ImplCreateAccessible() instead. */
421     virtual XAccessibleRef     CreateAccessible();
422 
423 protected:
424     /** Derived classes create a new accessible object here. */
425     virtual ScAccessibleCsvControl* ImplCreateAccessible() = 0;
426 };
427 
428 
429 // ============================================================================
430 
431 #endif
432 
433