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