1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_SOURCE_UI_INC_CSVGRID_HXX
21 #define INCLUDED_SC_SOURCE_UI_INC_CSVGRID_HXX
23 #include <vcl/virdev.hxx>
24 #include <vcl/menu.hxx>
25 #include <unotools/options.hxx>
30 #include "csvcontrol.hxx"
31 #include "csvsplits.hxx"
33 namespace svtools
{ class ColorConfig
; }
35 class ScEditEngineDefaulter
;
37 class ScAccessibleCsvControl
;
39 const sal_uInt32 CSV_COLUMN_INVALID
= CSV_VEC_NOTFOUND
;
41 /** This struct contains the state of one table column. */
44 sal_Int32 mnType
; /// Data type.
45 bool mbColumnSelected
;
47 explicit ScCsvColState( sal_Int32 nType
= CSV_TYPE_DEFAULT
) :
48 mnType( nType
), mbColumnSelected( false ) {}
50 bool IsSelected() const { return mbColumnSelected
; }
51 void Select( bool bSel
) { mbColumnSelected
= bSel
; }
54 typedef ::std::vector
< ScCsvColState
> ScCsvColStateVec
;
56 /** A data grid control for the CSV import dialog. The design of this control
57 simulates a Calc spreadsheet with row and column headers. */
58 class SC_DLLPUBLIC ScCsvGrid
: public ScCsvControl
, public utl::ConfigurationListener
61 typedef ::std::unique_ptr
< ScEditEngineDefaulter
> ScEditEnginePtr
;
63 VclPtr
<VirtualDevice
> mpBackgrDev
; /// Grid background, headers, cell texts.
64 VclPtr
<VirtualDevice
> mpGridDev
; /// Data grid with selection and cursor.
65 PopupMenu maPopup
; /// Popup menu for column types.
67 ::svtools::ColorConfig
* mpColorConfig
; /// Application color configuration.
68 Color maBackColor
; /// Cell background color.
69 Color maGridColor
; /// Table grid color.
70 Color maGridPBColor
; /// Grid color for "first imported line" delimiter.
71 Color maAppBackColor
; /// Background color for unused area.
72 Color maTextColor
; /// Text color for data area.
73 Color maHeaderBackColor
; /// Background color for headers.
74 Color maHeaderGridColor
; /// Grid color for headers.
75 Color maHeaderTextColor
; /// Text color for headers.
76 Color maSelectColor
; /// Header color of selected columns.
78 ScEditEnginePtr mpEditEngine
; /// For drawing cell texts.
79 vcl::Font maHeaderFont
; /// Font for column and row headers.
80 vcl::Font maMonoFont
; /// Monospace font for data cells.
81 Size maWinSize
; /// Size of the control.
82 Size maEdEngSize
; /// Paper size for edit engine.
84 ScCsvSplits maSplits
; /// Vector with split positions.
85 ScCsvColStateVec maColStates
; /// State of each column.
86 std::vector
<OUString
> maTypeNames
; /// UI names of data types.
87 std::vector
< std::vector
<OUString
> > maTexts
; /// 2D-vector for cell texts.
89 sal_Int32 mnFirstImpLine
; /// First imported line (0-based).
90 sal_uInt32 mnRecentSelCol
; /// Index of most recently selected column.
91 sal_uInt32 mnMTCurrCol
; /// Current column of mouse tracking.
92 bool mbMTSelecting
; /// Mouse tracking: true = select, false = deselect.
95 explicit ScCsvGrid( ScCsvControl
& rParent
);
97 virtual void dispose() override
;
99 /** Finishes initialization. Must be called after constructing a new object. */
102 // common grid handling ---------------------------------------------------
104 /** Updates layout data dependent from the control's state. */
105 void UpdateLayoutData();
106 /** Updates X coordinate of first visible position dependent from line numbers. */
107 void UpdateOffsetX();
108 /** Apply current layout data to the grid control. */
109 void ApplyLayout( const ScCsvLayoutData
& rOldData
);
110 /** Sets the number of the first imported line (for visual feedback). nLine is 0-based! */
111 void SetFirstImportedLine( sal_Int32 nLine
);
113 /** Finds a column position nearest to nPos which does not cause scrolling the visible area. */
114 sal_Int32
GetNoScrollCol( sal_Int32 nPos
) const;
117 /** Reads colors from system settings. */
118 SAL_DLLPRIVATE
void InitColors();
119 /** Initializes all font settings. */
120 SAL_DLLPRIVATE
void InitFonts();
121 /** Initializes all data dependent from the control's size. */
122 SAL_DLLPRIVATE
void InitSizeData();
124 // split handling ---------------------------------------------------------
126 /** Inserts a split. */
127 void InsertSplit( sal_Int32 nPos
);
128 /** Removes a split. */
129 void RemoveSplit( sal_Int32 nPos
);
130 /** Inserts a new or removes an existing split. */
131 void MoveSplit( sal_Int32 nPos
, sal_Int32 nNewPos
);
132 /** Removes all splits. */
133 void RemoveAllSplits();
134 /** Removes all splits and inserts the splits from rSplits. */
135 void SetSplits( const ScCsvSplits
& rSplits
);
138 /** Inserts a split and adjusts column data. */
139 SAL_DLLPRIVATE
bool ImplInsertSplit( sal_Int32 nPos
);
140 /** Removes a split and adjusts column data. */
141 SAL_DLLPRIVATE
bool ImplRemoveSplit( sal_Int32 nPos
);
142 /** Clears the split array and re-inserts boundary splits. */
143 SAL_DLLPRIVATE
void ImplClearSplits();
145 // columns/column types ---------------------------------------------------
147 /** Returns the number of columns. */
148 inline sal_uInt32
GetColumnCount() const { return maColStates
.size(); }
149 /** Returns the index of the first visible column. */
150 sal_uInt32
GetFirstVisColumn() const;
151 /** Returns the index of the last visible column. */
152 sal_uInt32
GetLastVisColumn() const;
154 /** Returns true, if nColIndex points to an existing column. */
155 bool IsValidColumn( sal_uInt32 nColIndex
) const;
156 /** Returns true, if column with index nColIndex is (at least partly) visible. */
157 bool IsVisibleColumn( sal_uInt32 nColIndex
) const;
159 /** Returns X coordinate of the specified column. */
160 sal_Int32
GetColumnX( sal_uInt32 nColIndex
) const;
161 /** Returns column index from output coordinate. */
162 sal_uInt32
GetColumnFromX( sal_Int32 nX
) const;
164 /** Returns start position of the column with the specified index. */
165 inline sal_Int32
GetColumnPos( sal_uInt32 nColIndex
) const { return maSplits
[ nColIndex
]; }
166 /** Returns column index from position. A split counts to its following column. */
167 sal_uInt32
GetColumnFromPos( sal_Int32 nPos
) const;
168 /** Returns the character width of the column with the specified index. */
169 sal_Int32
GetColumnWidth( sal_uInt32 nColIndex
) const;
171 /** Returns the vector with the states of all columns. */
172 inline const ScCsvColStateVec
& GetColumnStates() const { return maColStates
; }
173 /** Sets all column states to the values in the passed vector. */
174 void SetColumnStates( const ScCsvColStateVec
& rColStates
);
175 /** Returns the data type of the selected columns. */
176 sal_Int32
GetSelColumnType() const;
177 /** Changes the data type of all selected columns. */
178 void SetSelColumnType( sal_Int32 nType
);
179 /** Sets new UI data type names. */
180 void SetTypeNames( const std::vector
<OUString
>& rTypeNames
);
181 /** Returns the UI type name of the specified column. */
182 const OUString
& GetColumnTypeName( sal_uInt32 nColIndex
) const;
184 /** Fills the options object with column data for separators mode. */
185 void FillColumnDataSep( ScAsciiOptions
& rOptions
) const;
186 /** Fills the options object with column data for fixed width mode. */
187 void FillColumnDataFix( ScAsciiOptions
& rOptions
) const;
190 /** Returns the data type of the specified column. */
191 SAL_DLLPRIVATE sal_Int32
GetColumnType( sal_uInt32 nColIndex
) const;
192 /** Returns the data type of the specified column. */
193 SAL_DLLPRIVATE
void SetColumnType( sal_uInt32 nColIndex
, sal_Int32 nColType
);
195 /** Scrolls data grid vertically. */
196 SAL_DLLPRIVATE
void ScrollVertRel( ScMoveMode eDir
);
197 /** Executes the data type popup menu. */
198 SAL_DLLPRIVATE
void ExecutePopup( const Point
& rPos
);
200 // selection handling -----------------------------------------------------
202 /** Returns true, if the specified column is selected. */
203 bool IsSelected( sal_uInt32 nColIndex
) const;
204 /** Returns index of the first selected column. */
205 sal_uInt32
GetFirstSelected() const;
206 /** Returns index of the first selected column really after nFromIndex. */
207 sal_uInt32
GetNextSelected( sal_uInt32 nFromIndex
) const;
208 /** Selects or deselects the specified column. */
209 void Select( sal_uInt32 nColIndex
, bool bSelect
= true );
210 /** Toggles selection of the specified column. */
211 void ToggleSelect( sal_uInt32 nColIndex
);
212 /** Selects or deselects the specified column range. */
213 void SelectRange( sal_uInt32 nColIndex1
, sal_uInt32 nColIndex2
, bool bSelect
= true );
214 /** Selects or deselects all columns. */
215 void SelectAll( bool bSelect
= true );
217 /** Returns index of the focused column. */
218 inline sal_uInt32
GetFocusColumn() const { return GetColumnFromPos( GetGridCursorPos() ); }
221 /** Moves column cursor to a new position. */
222 SAL_DLLPRIVATE
void MoveCursor( sal_uInt32 nColIndex
);
223 /** Moves column cursor to the given direction. */
224 SAL_DLLPRIVATE
void MoveCursorRel( ScMoveMode eDir
);
226 /** Clears the entire selection without notify. */
227 SAL_DLLPRIVATE
void ImplClearSelection();
229 /** Executes selection action for a specific column. */
230 SAL_DLLPRIVATE
void DoSelectAction( sal_uInt32 nColIndex
, sal_uInt16 nModifier
);
232 // cell contents ----------------------------------------------------------
234 /** Fills all cells of a line with the passed text (separators mode). */
235 void ImplSetTextLineSep(
236 sal_Int32 nLine
, const OUString
& rTextLine
,
237 const OUString
& rSepChars
, sal_Unicode cTextSep
, bool bMergeSep
);
238 /** Fills all cells of a line with the passed text (fixed width mode). */
239 void ImplSetTextLineFix( sal_Int32 nLine
, const OUString
& rTextLine
);
241 /** Returns the text of the specified cell. */
242 const OUString
& GetCellText( sal_uInt32 nColIndex
, sal_Int32 nLine
) const;
244 // event handling ---------------------------------------------------------
246 virtual void Resize() override
;
247 virtual void GetFocus() override
;
248 virtual void LoseFocus() override
;
250 virtual void MouseButtonDown( const MouseEvent
& rMEvt
) override
;
251 virtual void Tracking( const TrackingEvent
& rTEvt
) override
;
252 virtual void KeyInput( const KeyEvent
& rKEvt
) override
;
253 virtual void Command( const CommandEvent
& rCEvt
) override
;
255 virtual void DataChanged( const DataChangedEvent
& rDCEvt
) override
;
257 virtual void ConfigurationChanged( ::utl::ConfigurationBroadcaster
*, sal_uInt32
) override
;
259 // painting ---------------------------------------------------------------
261 virtual void Paint( vcl::RenderContext
& rRenderContext
, const Rectangle
& ) override
;
264 /** Redraws the entire data grid. */
266 /** Returns a pointer to the used edit engine. */
267 EditEngine
* GetEditEngine();
270 /** Returns the width of the control. */
271 inline sal_Int32
GetWidth() const { return maWinSize
.Width(); }
272 /** Returns the height of the control. */
273 inline sal_Int32
GetHeight() const { return maWinSize
.Height(); }
275 /** Sets a clip region in the specified output device for the specified column. */
276 SAL_DLLPRIVATE
void ImplSetColumnClipRegion( OutputDevice
& rOutDev
, sal_uInt32 nColIndex
);
277 /** Draws the header of the specified column to the specified output device. */
278 SAL_DLLPRIVATE
void ImplDrawColumnHeader( OutputDevice
& rOutDev
, sal_uInt32 nColIndex
, Color aFillColor
);
280 /** Draws the text at the specified position to maBackgrDev. */
281 SAL_DLLPRIVATE
void ImplDrawCellText( const Point
& rPos
, const OUString
& rText
);
282 /** Draws the "first imported line" separator to maBackgrDev (or erases, if bSet is false). */
283 SAL_DLLPRIVATE
void ImplDrawFirstLineSep( bool bSet
);
284 /** Draws the column with index nColIndex to maBackgrDev. */
285 SAL_DLLPRIVATE
void ImplDrawColumnBackgr( sal_uInt32 nColIndex
);
286 /** Draws the row headers column to maBackgrDev. */
287 SAL_DLLPRIVATE
void ImplDrawRowHeaders();
288 /** Draws all columns and the row headers column to maBackgrDev. */
289 SAL_DLLPRIVATE
void ImplDrawBackgrDev();
291 /** Draws the column with index nColIndex with its selection state to maGridDev. */
292 SAL_DLLPRIVATE
void ImplDrawColumnSelection( sal_uInt32 nColIndex
);
293 /** Draws all columns with selection and cursor to maGridDev. */
294 SAL_DLLPRIVATE
void ImplDrawGridDev();
296 /** Redraws the entire column (background and selection). */
297 SAL_DLLPRIVATE
void ImplDrawColumn( sal_uInt32 nColIndex
);
299 /** Optimized drawing: Scrolls horizontally and redraws only missing parts. */
300 SAL_DLLPRIVATE
void ImplDrawHorzScrolled( sal_Int32 nOldPos
);
302 /** Inverts the cursor bar at the specified position in maGridDev. */
303 SAL_DLLPRIVATE
void ImplInvertCursor( sal_Int32 nPos
);
305 /** Draws directly tracking rectangle to the column with the specified index. */
306 SAL_DLLPRIVATE
void ImplDrawTrackingRect( sal_uInt32 nColIndex
);
308 // accessibility ----------------------------------------------------------
310 /** Creates a new accessible object. */
311 virtual rtl::Reference
<ScAccessibleCsvControl
> ImplCreateAccessible() override
;
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */