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