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