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 : VclPtr<VirtualDevice> mpBackgrDev; /// Grid background, headers, cell texts.
79 : VclPtr<VirtualDevice> mpGridDev; /// 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 : virtual void dispose() SAL_OVERRIDE;
113 :
114 : /** Finishes initialization. Must be called after constructing a new object. */
115 : void Init();
116 :
117 : // common grid handling ---------------------------------------------------
118 : public:
119 : /** Updates layout data dependent from the control's state. */
120 : void UpdateLayoutData();
121 : /** Updates X coordinate of first visible position dependent from line numbers. */
122 : void UpdateOffsetX();
123 : /** Apply current layout data to the grid control. */
124 : void ApplyLayout( const ScCsvLayoutData& rOldData );
125 : /** Sets the number of the first imported line (for visual feedback). nLine is 0-based! */
126 : void SetFirstImportedLine( sal_Int32 nLine );
127 :
128 : /** Finds a column position nearest to nPos which does not cause scrolling the visible area. */
129 : sal_Int32 GetNoScrollCol( sal_Int32 nPos ) const;
130 :
131 : private:
132 : /** Reads colors from system settings. */
133 : SAL_DLLPRIVATE void InitColors();
134 : /** Initializes all font settings. */
135 : SAL_DLLPRIVATE void InitFonts();
136 : /** Initializes all data dependent from the control's size. */
137 : SAL_DLLPRIVATE void InitSizeData();
138 :
139 : // split handling ---------------------------------------------------------
140 : public:
141 : /** Inserts a split. */
142 : void InsertSplit( sal_Int32 nPos );
143 : /** Removes a split. */
144 : void RemoveSplit( sal_Int32 nPos );
145 : /** Inserts a new or removes an existing split. */
146 : void MoveSplit( sal_Int32 nPos, sal_Int32 nNewPos );
147 : /** Removes all splits. */
148 : void RemoveAllSplits();
149 : /** Removes all splits and inserts the splits from rSplits. */
150 : void SetSplits( const ScCsvSplits& rSplits );
151 :
152 : private:
153 : /** Inserts a split and adjusts column data. */
154 : SAL_DLLPRIVATE bool ImplInsertSplit( sal_Int32 nPos );
155 : /** Removes a split and adjusts column data. */
156 : SAL_DLLPRIVATE bool ImplRemoveSplit( sal_Int32 nPos );
157 : /** Clears the split array and re-inserts boundary splits. */
158 : SAL_DLLPRIVATE void ImplClearSplits();
159 :
160 : // columns/column types ---------------------------------------------------
161 : public:
162 : /** Returns the number of columns. */
163 0 : inline sal_uInt32 GetColumnCount() const { return maColStates.size(); }
164 : /** Returns the index of the first visible column. */
165 : sal_uInt32 GetFirstVisColumn() const;
166 : /** Returns the index of the last visible column. */
167 : sal_uInt32 GetLastVisColumn() const;
168 :
169 : /** Returns true, if nColIndex points to an existing column. */
170 : bool IsValidColumn( sal_uInt32 nColIndex ) const;
171 : /** Returns true, if column with index nColIndex is (at least partly) visible. */
172 : bool IsVisibleColumn( sal_uInt32 nColIndex ) const;
173 :
174 : /** Returns X coordinate of the specified column. */
175 : sal_Int32 GetColumnX( sal_uInt32 nColIndex ) const;
176 : /** Returns column index from output coordinate. */
177 : sal_uInt32 GetColumnFromX( sal_Int32 nX ) const;
178 :
179 : /** Returns start position of the column with the specified index. */
180 0 : inline sal_Int32 GetColumnPos( sal_uInt32 nColIndex ) const { return maSplits[ nColIndex ]; }
181 : /** Returns column index from position. A split counts to its following column. */
182 : sal_uInt32 GetColumnFromPos( sal_Int32 nPos ) const;
183 : /** Returns the character width of the column with the specified index. */
184 : sal_Int32 GetColumnWidth( sal_uInt32 nColIndex ) const;
185 :
186 : /** Returns the vector with the states of all columns. */
187 0 : inline const ScCsvColStateVec& GetColumnStates() const { return maColStates; }
188 : /** Sets all column states to the values in the passed vector. */
189 : void SetColumnStates( const ScCsvColStateVec& rColStates );
190 : /** Returns the data type of the selected columns. */
191 : sal_Int32 GetSelColumnType() const;
192 : /** Changes the data type of all selected columns. */
193 : void SetSelColumnType( sal_Int32 nType );
194 : /** Sets new UI data type names. */
195 : void SetTypeNames( const StringVec& rTypeNames );
196 : /** Returns the UI type name of the specified column. */
197 : const OUString& GetColumnTypeName( sal_uInt32 nColIndex ) const;
198 :
199 : /** Fills the options object with column data for separators mode. */
200 : void FillColumnDataSep( ScAsciiOptions& rOptions ) const;
201 : /** Fills the options object with column data for fixed width mode. */
202 : void FillColumnDataFix( ScAsciiOptions& rOptions ) const;
203 :
204 : private:
205 : /** Returns the data type of the specified column. */
206 : SAL_DLLPRIVATE sal_Int32 GetColumnType( sal_uInt32 nColIndex ) const;
207 : /** Returns the data type of the specified column. */
208 : SAL_DLLPRIVATE void SetColumnType( sal_uInt32 nColIndex, sal_Int32 nColType );
209 :
210 : /** Scrolls data grid vertically. */
211 : SAL_DLLPRIVATE void ScrollVertRel( ScMoveMode eDir );
212 : /** Executes the data type popup menu. */
213 : SAL_DLLPRIVATE void ExecutePopup( const Point& rPos );
214 :
215 : // selection handling -----------------------------------------------------
216 : public:
217 : /** Returns true, if the specified column is selected. */
218 : bool IsSelected( sal_uInt32 nColIndex ) const;
219 : /** Returns index of the first selected column. */
220 : sal_uInt32 GetFirstSelected() const;
221 : /** Returns index of the first selected column really after nFromIndex. */
222 : sal_uInt32 GetNextSelected( sal_uInt32 nFromIndex ) const;
223 : /** Returns true, if at least one column is selected. */
224 : inline bool HasSelection() const { return GetFirstSelected() != CSV_COLUMN_INVALID; }
225 :
226 : /** Selects or deselects the specified column. */
227 : void Select( sal_uInt32 nColIndex, bool bSelect = true );
228 : /** Toggles selection of the specified column. */
229 : void ToggleSelect( sal_uInt32 nColIndex );
230 : /** Selects or deselects the specified column range. */
231 : void SelectRange( sal_uInt32 nColIndex1, sal_uInt32 nColIndex2, bool bSelect = true );
232 : /** Selects or deselects all columns. */
233 : void SelectAll( bool bSelect = true );
234 :
235 : /** Returns index of the focused column. */
236 0 : inline sal_uInt32 GetFocusColumn() const { return GetColumnFromPos( GetGridCursorPos() ); }
237 :
238 : private:
239 : /** Moves column cursor to a new position. */
240 : SAL_DLLPRIVATE void MoveCursor( sal_uInt32 nColIndex );
241 : /** Moves column cursor to the given direction. */
242 : SAL_DLLPRIVATE void MoveCursorRel( ScMoveMode eDir );
243 :
244 : /** Clears the entire selection without notify. */
245 : SAL_DLLPRIVATE void ImplClearSelection();
246 :
247 : /** Executes selection action for a specific column. */
248 : SAL_DLLPRIVATE void DoSelectAction( sal_uInt32 nColIndex, sal_uInt16 nModifier );
249 :
250 : // cell contents ----------------------------------------------------------
251 : public:
252 : /** Fills all cells of a line with the passed text (separators mode). */
253 : void ImplSetTextLineSep(
254 : sal_Int32 nLine, const OUString& rTextLine,
255 : const OUString& rSepChars, sal_Unicode cTextSep, bool bMergeSep );
256 : /** Fills all cells of a line with the passed text (fixed width mode). */
257 : void ImplSetTextLineFix( sal_Int32 nLine, const OUString& rTextLine );
258 :
259 : /** Returns the text of the specified cell. */
260 : const OUString& GetCellText( sal_uInt32 nColIndex, sal_Int32 nLine ) const;
261 :
262 : // event handling ---------------------------------------------------------
263 : protected:
264 : virtual void Resize() SAL_OVERRIDE;
265 : virtual void GetFocus() SAL_OVERRIDE;
266 : virtual void LoseFocus() SAL_OVERRIDE;
267 :
268 : virtual void MouseButtonDown( const MouseEvent& rMEvt ) SAL_OVERRIDE;
269 : virtual void Tracking( const TrackingEvent& rTEvt ) SAL_OVERRIDE;
270 : virtual void KeyInput( const KeyEvent& rKEvt ) SAL_OVERRIDE;
271 : virtual void Command( const CommandEvent& rCEvt ) SAL_OVERRIDE;
272 :
273 : virtual void DataChanged( const DataChangedEvent& rDCEvt ) SAL_OVERRIDE;
274 :
275 : virtual void ConfigurationChanged( ::utl::ConfigurationBroadcaster*, sal_uInt32 ) SAL_OVERRIDE;
276 :
277 : // painting ---------------------------------------------------------------
278 : protected:
279 : virtual void Paint( vcl::RenderContext& rRenderContext, const Rectangle& ) SAL_OVERRIDE;
280 :
281 : public:
282 : /** Redraws the entire data grid. */
283 : void ImplRedraw();
284 : /** Returns a pointer to the used edit engine. */
285 : EditEngine* GetEditEngine();
286 :
287 : private:
288 : /** Returns the width of the control. */
289 0 : inline sal_Int32 GetWidth() const { return maWinSize.Width(); }
290 : /** Returns the height of the control. */
291 0 : inline sal_Int32 GetHeight() const { return maWinSize.Height(); }
292 :
293 : /** Sets a clip region in the specified output device for the specified column. */
294 : SAL_DLLPRIVATE void ImplSetColumnClipRegion( OutputDevice& rOutDev, sal_uInt32 nColIndex );
295 : /** Draws the header of the specified column to the specified output device. */
296 : SAL_DLLPRIVATE void ImplDrawColumnHeader( OutputDevice& rOutDev, sal_uInt32 nColIndex, Color aFillColor );
297 :
298 : /** Draws the text at the specified position to maBackgrDev. */
299 : SAL_DLLPRIVATE void ImplDrawCellText( const Point& rPos, const OUString& rText );
300 : /** Draws the "first imported line" separator to maBackgrDev (or erases, if bSet is false). */
301 : SAL_DLLPRIVATE void ImplDrawFirstLineSep( bool bSet );
302 : /** Draws the column with index nColIndex to maBackgrDev. */
303 : SAL_DLLPRIVATE void ImplDrawColumnBackgr( sal_uInt32 nColIndex );
304 : /** Draws the row headers column to maBackgrDev. */
305 : SAL_DLLPRIVATE void ImplDrawRowHeaders();
306 : /** Draws all columns and the row headers column to maBackgrDev. */
307 : SAL_DLLPRIVATE void ImplDrawBackgrDev();
308 :
309 : /** Draws the column with index nColIndex with its selection state to maGridDev. */
310 : SAL_DLLPRIVATE void ImplDrawColumnSelection( sal_uInt32 nColIndex );
311 : /** Draws all columns with selection and cursor to maGridDev. */
312 : SAL_DLLPRIVATE void ImplDrawGridDev();
313 :
314 : /** Redraws the entire column (background and selection). */
315 : SAL_DLLPRIVATE void ImplDrawColumn( sal_uInt32 nColIndex );
316 :
317 : /** Optimized drawing: Scrolls horizontally and redraws only missing parts. */
318 : SAL_DLLPRIVATE void ImplDrawHorzScrolled( sal_Int32 nOldPos );
319 :
320 : /** Inverts the cursor bar at the specified position in maGridDev. */
321 : SAL_DLLPRIVATE void ImplInvertCursor( sal_Int32 nPos );
322 :
323 : /** Draws directly tracking rectangle to the column with the specified index. */
324 : SAL_DLLPRIVATE void ImplDrawTrackingRect( sal_uInt32 nColIndex );
325 :
326 : // accessibility ----------------------------------------------------------
327 : protected:
328 : /** Creates a new accessible object. */
329 : virtual ScAccessibleCsvControl* ImplCreateAccessible() SAL_OVERRIDE;
330 : };
331 :
332 : #endif
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|