xref: /aoo41x/main/sc/source/ui/inc/csvgrid.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_CSVGRID_HXX
31 #define _SC_CSVGRID_HXX
32 
33 #include <vcl/virdev.hxx>
34 #include <vcl/menu.hxx>
35 #include <unotools/options.hxx>
36 
37 #include <vector>
38 #include <memory>
39 #include "scdllapi.h"
40 #include "csvcontrol.hxx"
41 #include "csvsplits.hxx"
42 
43 
44 // ----------------------------------------------------------------------------
45 
46 namespace svtools { class ColorConfig; }
47 class EditEngine;
48 class ScEditEngineDefaulter;
49 class ScAsciiOptions;
50 class ScAccessibleCsvControl;
51 
52 
53 // ============================================================================
54 
55 const sal_uInt8 CSV_COLFLAG_NONE    = 0x00;         /// Nothing set.
56 const sal_uInt8 CSV_COLFLAG_SELECT  = 0x01;         /// Column is selected.
57 
58 const sal_uInt32 CSV_COLUMN_INVALID = CSV_VEC_NOTFOUND;
59 
60 
61 // ----------------------------------------------------------------------------
62 
63 /** This struct contains the state of one table column. */
64 struct ScCsvColState
65 {
66     sal_Int32                   mnType;             /// Data type.
67     sal_uInt8                   mnFlags;            /// Flags (i.e. selection state).
68 
69     inline explicit             ScCsvColState(
70                                         sal_Int32 nType = CSV_TYPE_DEFAULT,
71                                         sal_uInt8 nFlags = CSV_COLFLAG_NONE ) :
72                                     mnType( nType ), mnFlags( nFlags ) {}
73 
74     inline bool                 IsSelected() const;
75     inline void                 Select( bool bSel );
76 };
77 
78 inline bool ScCsvColState::IsSelected() const
79 {
80     return (mnFlags & CSV_COLFLAG_SELECT) != 0;
81 }
82 
83 inline void ScCsvColState::Select( bool bSel )
84 {
85     if( bSel ) mnFlags |= CSV_COLFLAG_SELECT; else mnFlags &= ~CSV_COLFLAG_SELECT;
86 }
87 
88 
89 // ----------------------------------------------------------------------------
90 
91 typedef ::std::vector< ScCsvColState > ScCsvColStateVec;
92 
93 
94 // ============================================================================
95 
96 /** A data grid control for the CSV import dialog. The design of this control
97     simulates a Calc spreadsheet with row and column headers. */
98 class SC_DLLPUBLIC ScCsvGrid : public ScCsvControl, public utl::ConfigurationListener
99 {
100 private:
101     typedef ::std::auto_ptr< ScEditEngineDefaulter > ScEditEnginePtr;
102 
103     VirtualDevice               maBackgrDev;        /// Grid background, headers, cell texts.
104     VirtualDevice               maGridDev;          /// Data grid with selection and cursor.
105     PopupMenu                   maPopup;            /// Popup menu for column types.
106 
107     ::svtools::ColorConfig&     mrColorConfig;      /// Application color configuration.
108     Color                       maBackColor;        /// Cell background color.
109     Color                       maGridColor;        /// Table grid color.
110     Color                       maGridPBColor;      /// Grid color for "first imported line" delimiter.
111     Color                       maAppBackColor;     /// Background color for unused area.
112     Color                       maTextColor;        /// Text color for data area.
113     Color                       maHeaderBackColor;  /// Background color for headers.
114     Color                       maHeaderGridColor;  /// Grid color for headers.
115     Color                       maHeaderTextColor;  /// Text color for headers.
116     Color                       maSelectColor;      /// Header color of selected columns.
117 
118     ScEditEnginePtr             mpEditEngine;       /// For drawing cell texts.
119     Font                        maHeaderFont;       /// Font for column and row headers.
120     Font                        maMonoFont;         /// Monospace font for data cells.
121     Size                        maWinSize;          /// Size of the control.
122     Size                        maEdEngSize;        /// Paper size for edit engine.
123 
124     ScCsvSplits                 maSplits;           /// Vector with split positions.
125     ScCsvColStateVec            maColStates;        /// State of each column.
126     StringVec                   maTypeNames;        /// UI names of data types.
127     StringVecVec                maTexts;            /// 2D-vector for cell texts.
128 
129     sal_Int32                   mnFirstImpLine;     /// First imported line (0-based).
130     sal_uInt32                  mnRecentSelCol;     /// Index of most recently selected column.
131     sal_uInt32                  mnMTCurrCol;        /// Current column of mouse tracking.
132     bool                        mbMTSelecting;      /// Mouse tracking: true = select, false = deselect.
133 
134     // ------------------------------------------------------------------------
135 public:
136     explicit                    ScCsvGrid( ScCsvControl& rParent );
137     virtual                     ~ScCsvGrid();
138 
139     // common grid handling ---------------------------------------------------
140 public:
141     /** Updates layout data dependent from the control's state. */
142     void                        UpdateLayoutData();
143     /** Updates X coordinate of first visible position dependent from line numbers. */
144     void                        UpdateOffsetX();
145     /** Apply current layout data to the grid control. */
146     void                        ApplyLayout( const ScCsvLayoutData& rOldData );
147     /** Sets the number of the first imported line (for visual feedback). nLine is 0-based! */
148     void                        SetFirstImportedLine( sal_Int32 nLine );
149 
150     /** Finds a column position nearest to nPos which does not cause scrolling the visible area. */
151     sal_Int32                   GetNoScrollCol( sal_Int32 nPos ) const;
152 
153 private:
154     /** Reads colors from system settings. */
155     SC_DLLPRIVATE void                        InitColors();
156     /** Initializes all font settings. */
157     SC_DLLPRIVATE void                        InitFonts();
158     /** Initializes all data dependent from the control's size. */
159     SC_DLLPRIVATE void                        InitSizeData();
160 
161     // split handling ---------------------------------------------------------
162 public:
163     /** Inserts a split. */
164     void                        InsertSplit( sal_Int32 nPos );
165     /** Removes a split. */
166     void                        RemoveSplit( sal_Int32 nPos );
167     /** Inserts a new or removes an existing split. */
168     void                        MoveSplit( sal_Int32 nPos, sal_Int32 nNewPos );
169     /** Removes all splits. */
170     void                        RemoveAllSplits();
171     /** Removes all splits and inserts the splits from rSplits. */
172     void                        SetSplits( const ScCsvSplits& rSplits );
173 
174 private:
175     /** Inserts a split and adjusts column data. */
176     SC_DLLPRIVATE bool                        ImplInsertSplit( sal_Int32 nPos );
177     /** Removes a split and adjusts column data. */
178     SC_DLLPRIVATE bool                        ImplRemoveSplit( sal_Int32 nPos );
179     /** Clears the split array and re-inserts boundary splits. */
180     SC_DLLPRIVATE void                        ImplClearSplits();
181 
182     // columns/column types ---------------------------------------------------
183 public:
184     /** Returns the number of columns. */
185     inline sal_uInt32           GetColumnCount() const { return maColStates.size(); }
186     /** Returns the index of the first visible column. */
187     sal_uInt32                  GetFirstVisColumn() const;
188     /** Returns the index of the last visible column. */
189     sal_uInt32                  GetLastVisColumn() const;
190 
191     /** Returns true, if nColIndex points to an existing column. */
192     bool                        IsValidColumn( sal_uInt32 nColIndex ) const;
193     /** Returns true, if column with index nColIndex is (at least partly) visible. */
194     bool                        IsVisibleColumn( sal_uInt32 nColIndex ) const;
195 
196     /** Returns X coordinate of the specified column. */
197     sal_Int32                   GetColumnX( sal_uInt32 nColIndex ) const;
198     /** Returns column index from output coordinate. */
199     sal_uInt32                  GetColumnFromX( sal_Int32 nX ) const;
200 
201     /** Returns start position of the column with the specified index. */
202     inline sal_Int32            GetColumnPos( sal_uInt32 nColIndex ) const { return maSplits[ nColIndex ]; }
203     /** Returns column index from position. A split counts to its following column. */
204     sal_uInt32                  GetColumnFromPos( sal_Int32 nPos ) const;
205     /** Returns the character width of the column with the specified index. */
206     sal_Int32                   GetColumnWidth( sal_uInt32 nColIndex ) const;
207 
208     /** Returns the vector with the states of all columns. */
209     inline const ScCsvColStateVec& GetColumnStates() const { return maColStates; }
210     /** Sets all column states to the values in the passed vector. */
211     void                        SetColumnStates( const ScCsvColStateVec& rColStates );
212     /** Returns the data type of the selected columns. */
213     sal_Int32                   GetSelColumnType() const;
214     /** Changes the data type of all selected columns. */
215     void                        SetSelColumnType( sal_Int32 nType );
216     /** Sets new UI data type names. */
217     void                        SetTypeNames( const StringVec& rTypeNames );
218     /** Returns the UI type name of the specified column. */
219     const String&               GetColumnTypeName( sal_uInt32 nColIndex ) const;
220 
221     /** Fills the options object with column data for separators mode. */
222     void                        FillColumnDataSep( ScAsciiOptions& rOptions ) const;
223     /** Fills the options object with column data for fixed width mode. */
224     void                        FillColumnDataFix( ScAsciiOptions& rOptions ) const;
225 
226 private:
227     /** Returns the data type of the specified column. */
228     SC_DLLPRIVATE sal_Int32                   GetColumnType( sal_uInt32 nColIndex ) const;
229     /** Returns the data type of the specified column. */
230     SC_DLLPRIVATE void                        SetColumnType( sal_uInt32 nColIndex, sal_Int32 nColType );
231 
232     /** Scrolls data grid vertically. */
233     SC_DLLPRIVATE void                        ScrollVertRel( ScMoveMode eDir );
234     /** Executes the data type popup menu. */
235     SC_DLLPRIVATE void                        ExecutePopup( const Point& rPos );
236 
237     // selection handling -----------------------------------------------------
238 public:
239     /** Returns true, if the specified column is selected. */
240     bool                        IsSelected( sal_uInt32 nColIndex ) const;
241     /** Returns index of the first selected column. */
242     sal_uInt32                  GetFirstSelected() const;
243     /** Returns index of the first selected column really after nFromIndex. */
244     sal_uInt32                  GetNextSelected( sal_uInt32 nFromIndex ) const;
245     /** Returns true, if at least one column is selected. */
246     inline bool                 HasSelection() const { return GetFirstSelected() != CSV_COLUMN_INVALID; }
247 
248     /** Selects or deselects the specified column. */
249     void                        Select( sal_uInt32 nColIndex, bool bSelect = true );
250     /** Toggles selection of the specified column. */
251     void                        ToggleSelect( sal_uInt32 nColIndex );
252     /** Selects or deselects the specified column range. */
253     void                        SelectRange( sal_uInt32 nColIndex1, sal_uInt32 nColIndex2, bool bSelect = true );
254     /** Selects or deselects all columns. */
255     void                        SelectAll( bool bSelect = true );
256 
257     /** Returns index of the focused column. */
258     inline sal_uInt32           GetFocusColumn() const { return GetColumnFromPos( GetGridCursorPos() ); }
259 
260 private:
261     /** Moves column cursor to a new position. */
262     SC_DLLPRIVATE void                        MoveCursor( sal_uInt32 nColIndex );
263     /** Moves column cursor to the given direction. */
264     SC_DLLPRIVATE void                        MoveCursorRel( ScMoveMode eDir );
265 
266     /** Clears the entire selection without notify. */
267     SC_DLLPRIVATE void                        ImplClearSelection();
268 
269     /** Executes selection action for a specific column. */
270     SC_DLLPRIVATE void                        DoSelectAction( sal_uInt32 nColIndex, sal_uInt16 nModifier );
271 
272     // cell contents ----------------------------------------------------------
273 public:
274     /** Fills all cells of a line with the passed text (separators mode). */
275     void                        ImplSetTextLineSep(
276                                     sal_Int32 nLine, const String& rTextLine,
277                                     const String& rSepChars, sal_Unicode cTextSep, bool bMergeSep );
278     /** Fills all cells of a line with the passed text (fixed width mode). */
279     void                        ImplSetTextLineFix( sal_Int32 nLine, const String& rTextLine );
280 
281     /** Returns the text of the specified cell. */
282     const String&               GetCellText( sal_uInt32 nColIndex, sal_Int32 nLine ) const;
283 
284     // event handling ---------------------------------------------------------
285 protected:
286     virtual void                Resize();
287     virtual void                GetFocus();
288     virtual void                LoseFocus();
289 
290     virtual void                MouseButtonDown( const MouseEvent& rMEvt );
291     virtual void                Tracking( const TrackingEvent& rTEvt );
292     virtual void                KeyInput( const KeyEvent& rKEvt );
293     virtual void                Command( const CommandEvent& rCEvt );
294 
295     virtual void                DataChanged( const DataChangedEvent& rDCEvt );
296 
297     virtual void                ConfigurationChanged( ::utl::ConfigurationBroadcaster*, sal_uInt32 );
298 
299     // painting ---------------------------------------------------------------
300 protected:
301     virtual void                Paint( const Rectangle& );
302 
303 public:
304     /** Redraws the entire data grid. */
305     void                        ImplRedraw();
306     /** Returns a pointer to the used edit engine. */
307     EditEngine*                 GetEditEngine();
308 
309 private:
310     /** Returns the width of the control. */
311     inline sal_Int32            GetWidth() const { return maWinSize.Width(); }
312     /** Returns the height of the control. */
313     inline sal_Int32            GetHeight() const { return maWinSize.Height(); }
314 
315     /** Sets a clip region in the specified output device for the specified column. */
316     SC_DLLPRIVATE void                        ImplSetColumnClipRegion( OutputDevice& rOutDev, sal_uInt32 nColIndex );
317     /** Draws the header of the specified column to the specified output device. */
318     SC_DLLPRIVATE void                        ImplDrawColumnHeader( OutputDevice& rOutDev, sal_uInt32 nColIndex, Color aFillColor );
319 
320     /** Draws the text at the specified position to maBackgrDev. */
321     SC_DLLPRIVATE void                        ImplDrawCellText( const Point& rPos, const String& rText );
322     /** Draws the "first imported line" separator to maBackgrDev (or erases, if bSet is false). */
323     SC_DLLPRIVATE void                        ImplDrawFirstLineSep( bool bSet );
324     /** Draws the column with index nColIndex to maBackgrDev. */
325     SC_DLLPRIVATE void                        ImplDrawColumnBackgr( sal_uInt32 nColIndex );
326     /** Draws the row headers column to maBackgrDev. */
327     SC_DLLPRIVATE void                        ImplDrawRowHeaders();
328     /** Draws all columns and the row headers column to maBackgrDev. */
329     SC_DLLPRIVATE void                        ImplDrawBackgrDev();
330 
331     /** Draws the column with index nColIndex with its selection state to maGridDev. */
332     SC_DLLPRIVATE void                        ImplDrawColumnSelection( sal_uInt32 nColIndex );
333     /** Draws all columns with selection and cursor to maGridDev. */
334     SC_DLLPRIVATE void                        ImplDrawGridDev();
335 
336     /** Redraws the entire column (background and selection). */
337     SC_DLLPRIVATE void                        ImplDrawColumn( sal_uInt32 nColIndex );
338 
339     /** Optimized drawing: Scrolls horizontally and redraws only missing parts. */
340     SC_DLLPRIVATE void                        ImplDrawHorzScrolled( sal_Int32 nOldPos );
341 
342     /** Inverts the cursor bar at the specified position in maGridDev. */
343     SC_DLLPRIVATE void                        ImplInvertCursor( sal_Int32 nPos );
344 
345     /** Draws directly tracking rectangle to the column with the specified index. */
346     SC_DLLPRIVATE void                        ImplDrawTrackingRect( sal_uInt32 nColIndex );
347 
348     // accessibility ----------------------------------------------------------
349 protected:
350     /** Creates a new accessible object. */
351     virtual ScAccessibleCsvControl* ImplCreateAccessible();
352 };
353 
354 
355 // ============================================================================
356 
357 #endif
358 
359