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