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 .
22 #include <vcl/virdev.hxx>
23 #include <unotools/options.hxx>
28 #include <editutil.hxx>
29 #include "csvcontrol.hxx"
30 #include "csvsplits.hxx"
32 namespace svtools
{ class ColorConfig
; }
35 class ScAccessibleCsvControl
;
38 const sal_uInt32 CSV_COLUMN_INVALID
= CSV_VEC_NOTFOUND
;
40 /** This struct contains the state of one table column. */
43 sal_Int32 mnType
; /// Data type.
44 bool mbColumnSelected
;
46 explicit ScCsvColState( sal_Int32 nType
= CSV_TYPE_DEFAULT
) :
47 mnType( nType
), mbColumnSelected( false ) {}
49 bool IsSelected() const { return mbColumnSelected
; }
50 void Select( bool bSel
) { mbColumnSelected
= bSel
; }
53 typedef ::std::vector
< ScCsvColState
> ScCsvColStateVec
;
55 /** A data grid control for the CSV import dialog. The design of this control
56 simulates a Calc spreadsheet with row and column headers. */
57 class SAL_DLLPUBLIC_RTTI ScCsvGrid
: public ScCsvControl
, public utl::ConfigurationListener
60 ScCsvTableBox
* mpTableBox
; /// Grid Parent
61 VclPtr
<VirtualDevice
> mpBackgrDev
; /// Grid background, headers, cell texts.
62 VclPtr
<VirtualDevice
> mpGridDev
; /// Data grid with selection and cursor.
63 std::unique_ptr
<weld::Menu
> mxPopup
; /// Popup menu for column types.
65 ::svtools::ColorConfig
* mpColorConfig
; /// Application color configuration.
66 Color maBackColor
; /// Cell background color.
67 Color maGridColor
; /// Table grid color.
68 Color maGridPBColor
; /// Grid color for "first imported line" delimiter.
69 Color maAppBackColor
; /// Background color for unused area.
70 Color maTextColor
; /// Text color for data area.
71 Color maHeaderBackColor
; /// Background color for headers.
72 Color maHeaderGridColor
; /// Grid color for headers.
73 Color maHeaderTextColor
; /// Text color for headers.
74 Color maSelectColor
; /// Header color of selected columns.
76 std::unique_ptr
< ScEditEngineDefaulter
>
77 mpEditEngine
; /// For drawing cell texts.
78 vcl::Font maHeaderFont
; /// Font for column and row headers.
79 vcl::Font maMonoFont
; /// Monospace font for data cells.
80 Size maWinSize
; /// Size of the control.
81 Size maEdEngSize
; /// Paper size for edit engine.
83 ScCsvSplits maSplits
; /// Vector with split positions.
84 ScCsvColStateVec maColStates
; /// State of each column.
85 std::vector
<OUString
> maTypeNames
; /// UI names of data types.
86 std::vector
< std::vector
<OUString
> > maTexts
; /// 2D-vector for cell texts.
88 sal_Int32 mnFirstImpLine
; /// First imported line (0-based).
89 sal_uInt32 mnRecentSelCol
; /// Index of most recently selected column.
90 sal_uInt32 mnMTCurrCol
; /// Current column of mouse tracking.
91 bool mbTracking
; /// True if Mouse tracking
92 bool mbMTSelecting
; /// Mouse tracking mode: true = select, false = deselect.
95 explicit ScCsvGrid(const ScCsvLayoutData
& rData
, std::unique_ptr
<weld::Menu
> xPopup
, ScCsvTableBox
* pTableBox
);
96 virtual void SetDrawingArea(weld::DrawingArea
* pDrawingArea
) override
;
97 ScCsvTableBox
* GetTableBox() { return mpTableBox
; }
98 virtual ~ScCsvGrid() override
;
100 /** Finishes initialization. Must be called after constructing a new object. */
103 // common grid handling ---------------------------------------------------
105 /** Updates layout data dependent from the control's state. */
106 void UpdateLayoutData();
107 /** Updates X coordinate of first visible position dependent from line numbers. */
108 void UpdateOffsetX();
109 /** Apply current layout data to the grid control. */
110 void ApplyLayout( const ScCsvLayoutData
& rOldData
);
111 /** Sets the number of the first imported line (for visual feedback). nLine is 0-based! */
112 void SetFirstImportedLine( sal_Int32 nLine
);
114 /** Finds a column position nearest to nPos which does not cause scrolling the visible area. */
115 sal_Int32
GetNoScrollCol( sal_Int32 nPos
) const;
118 /** Reads colors from system settings. */
120 /** Initializes all font settings. */
122 /** Initializes all data dependent from the control's size. */
125 // split handling ---------------------------------------------------------
127 /** Inserts a split. */
128 void InsertSplit( sal_Int32 nPos
);
129 /** Removes a split. */
130 void RemoveSplit( sal_Int32 nPos
);
131 /** Inserts a new or removes an existing split. */
132 void MoveSplit( sal_Int32 nPos
, sal_Int32 nNewPos
);
133 /** Removes all splits. */
134 void RemoveAllSplits();
135 /** Removes all splits and inserts the splits from rSplits. */
136 void SetSplits( const ScCsvSplits
& rSplits
);
139 /** Inserts a split and adjusts column data. */
140 bool ImplInsertSplit( sal_Int32 nPos
);
141 /** Removes a split and adjusts column data. */
142 bool ImplRemoveSplit( sal_Int32 nPos
);
143 /** Clears the split array and re-inserts boundary splits. */
144 void ImplClearSplits();
146 // columns/column types ---------------------------------------------------
148 /** Returns the number of columns. */
149 sal_uInt32
GetColumnCount() const { return maColStates
.size(); }
150 /** Returns the index of the first visible column. */
151 sal_uInt32
GetFirstVisColumn() const;
152 /** Returns the index of the last visible column. */
153 sal_uInt32
GetLastVisColumn() const;
155 /** Returns true, if nColIndex points to an existing column. */
156 bool IsValidColumn( sal_uInt32 nColIndex
) const;
157 /** Returns true, if column with index nColIndex is (at least partly) visible. */
158 bool IsVisibleColumn( sal_uInt32 nColIndex
) const;
160 /** Returns X coordinate of the specified column. */
161 sal_Int32
GetColumnX( sal_uInt32 nColIndex
) const;
162 /** Returns column index from output coordinate. */
163 sal_uInt32
GetColumnFromX( sal_Int32 nX
) const;
165 /** Returns start position of the column with the specified index. */
166 sal_Int32
GetColumnPos( sal_uInt32 nColIndex
) const { return maSplits
[ nColIndex
]; }
167 /** Returns column index from position. A split counts to its following column. */
168 sal_uInt32
GetColumnFromPos( sal_Int32 nPos
) const;
169 /** Returns the character width of the column with the specified index. */
170 sal_Int32
GetColumnWidth( sal_uInt32 nColIndex
) const;
172 /** Returns the vector with the states of all columns. */
173 const ScCsvColStateVec
& GetColumnStates() const { return maColStates
; }
174 /** Sets all column states to the values in the passed vector. */
175 void SetColumnStates( ScCsvColStateVec
&& rColStates
);
176 /** Returns the data type of the selected columns. */
177 SC_DLLPUBLIC sal_Int32
GetSelColumnType() const;
178 /** Changes the data type of all selected columns. */
179 void SetSelColumnType( sal_Int32 nType
);
180 /** Sets new UI data type names. */
181 void SetTypeNames( std::vector
<OUString
>&& rTypeNames
);
182 /** Returns the UI type name of the specified column. */
183 OUString
GetColumnTypeName( sal_uInt32 nColIndex
) const;
185 /** Fills the options object with column data for separators mode. */
186 void FillColumnDataSep( ScAsciiOptions
& rOptions
) const;
187 /** Fills the options object with column data for fixed width mode. */
188 void FillColumnDataFix( ScAsciiOptions
& rOptions
) const;
191 /** Returns the data type of the specified column. */
192 sal_Int32
GetColumnType( sal_uInt32 nColIndex
) const;
193 /** Sets the data type of the specified column. */
194 void SetColumnType( sal_uInt32 nColIndex
, sal_Int32 nColType
);
196 /** Scrolls data grid vertically. */
197 void ScrollVertRel( ScMoveMode eDir
);
198 /** Executes the data type popup menu. */
199 void ExecutePopup( const Point
& rPos
);
201 // selection handling -----------------------------------------------------
203 /** Returns true, if the specified column is selected. */
204 bool IsSelected( sal_uInt32 nColIndex
) const;
205 /** Returns index of the first selected column. */
206 sal_uInt32
GetFirstSelected() const;
207 /** Returns index of the first selected column really after nFromIndex. */
208 sal_uInt32
GetNextSelected( sal_uInt32 nFromIndex
) const;
209 /** Selects or deselects the specified column. */
210 void Select( sal_uInt32 nColIndex
, bool bSelect
= true );
211 /** Toggles selection of the specified column. */
212 void ToggleSelect( sal_uInt32 nColIndex
);
213 /** Selects or deselects the specified column range. */
214 void SelectRange( sal_uInt32 nColIndex1
, sal_uInt32 nColIndex2
, bool bSelect
= true );
215 /** Selects or deselects all columns. */
216 void SelectAll( bool bSelect
= true );
218 /** Returns index of the focused column. */
219 sal_uInt32
GetFocusColumn() const { return GetColumnFromPos( GetGridCursorPos() ); }
222 /** Moves column cursor to a new position. */
223 void MoveCursor( sal_uInt32 nColIndex
);
224 /** Moves column cursor to the given direction. */
225 void MoveCursorRel( ScMoveMode eDir
);
227 /** Clears the entire selection without notify. */
228 void ImplClearSelection();
230 /** Executes selection action for a specific column. */
231 void DoSelectAction( sal_uInt32 nColIndex
, sal_uInt16 nModifier
);
233 // cell contents ----------------------------------------------------------
235 /** Fills all cells of a line with the passed text (separators mode). */
236 void ImplSetTextLineSep(
237 sal_Int32 nLine
, const OUString
& rTextLine
,
238 const OUString
& rSepChars
, sal_Unicode cTextSep
, bool bMergeSep
, bool bRemoveSpace
= false );
239 /** Fills all cells of a line with the passed text (fixed width mode). */
240 void ImplSetTextLineFix( sal_Int32 nLine
, std::u16string_view rTextLine
);
242 /** Returns the text of the specified cell. */
243 OUString
GetCellText( sal_uInt32 nColIndex
, sal_Int32 nLine
) const;
245 // event handling ---------------------------------------------------------
247 virtual void Resize() override
;
248 virtual void GetFocus() override
;
249 virtual void LoseFocus() override
;
251 virtual bool MouseButtonDown( const MouseEvent
& rMEvt
) override
;
252 virtual bool MouseMove( const MouseEvent
& rMEvt
) override
;
253 virtual bool MouseButtonUp( const MouseEvent
& rMEvt
) override
;
254 virtual bool KeyInput( const KeyEvent
& rKEvt
) override
;
255 virtual bool Command( const CommandEvent
& rCEvt
) override
;
257 virtual tools::Rectangle
GetFocusRect() override
;
259 virtual void StyleUpdated() override
;
261 virtual void ConfigurationChanged( ::utl::ConfigurationBroadcaster
*, ConfigurationHints
) override
;
263 // painting ---------------------------------------------------------------
265 virtual void Paint( vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& ) override
;
268 /** Redraws the entire data grid. */
269 void ImplRedraw(vcl::RenderContext
& rRenderContext
);
270 /** Returns a pointer to the used edit engine. */
271 EditEngine
* GetEditEngine();
274 /** Returns the width of the control. */
275 sal_Int32
GetWidth() const { return maWinSize
.Width(); }
276 /** Returns the height of the control. */
277 sal_Int32
GetHeight() const { return maWinSize
.Height(); }
279 /** Sets a clip region in the specified output device for the specified column. */
280 void ImplSetColumnClipRegion( OutputDevice
& rOutDev
, sal_uInt32 nColIndex
);
281 /** Draws the header of the specified column to the specified output device. */
282 void ImplDrawColumnHeader( OutputDevice
& rOutDev
, sal_uInt32 nColIndex
, Color aFillColor
);
284 /** Draws the text at the specified position to maBackgrDev. */
285 void ImplDrawCellText( const Point
& rPos
, const OUString
& rText
);
286 /** Draws the "first imported line" separator to maBackgrDev (or erases, if bSet is false). */
287 void ImplDrawFirstLineSep( bool bSet
);
288 /** Draws the column with index nColIndex to maBackgrDev. */
289 void ImplDrawColumnBackgr( sal_uInt32 nColIndex
);
290 /** Draws the row headers column to maBackgrDev. */
291 void ImplDrawRowHeaders();
292 /** Draws all columns and the row headers column to maBackgrDev. */
293 void ImplDrawBackgrDev();
295 /** Draws the column with index nColIndex with its selection state to maGridDev. */
296 void ImplDrawColumnSelection( sal_uInt32 nColIndex
);
297 /** Draws all columns with selection and cursor to maGridDev. */
298 void ImplDrawGridDev();
300 /** Redraws the entire column (background and selection). */
301 void ImplDrawColumn( sal_uInt32 nColIndex
);
303 /** Optimized drawing: Scrolls horizontally and redraws only missing parts. */
304 void ImplDrawHorzScrolled( sal_Int32 nOldPos
);
306 /** Inverts the cursor bar at the specified position in maGridDev. */
307 void ImplInvertCursor( sal_Int32 nPos
);
309 // accessibility ----------------------------------------------------------
311 /** Creates a new accessible object. */
312 virtual css::uno::Reference
<css::accessibility::XAccessible
> CreateAccessible() override
;
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */