Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / ui / inc / csvgrid.hxx
blob84a0b6cb8f73003f1e9ea76a629581092adb7284
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <unotools/options.hxx>
26 #include <vector>
27 #include <memory>
28 #include <scdllapi.h>
29 #include <editutil.hxx>
30 #include "csvcontrol.hxx"
31 #include "csvsplits.hxx"
33 namespace svtools { class ColorConfig; }
34 class EditEngine;
35 class ScAsciiOptions;
36 class ScAccessibleCsvControl;
37 class ScCsvTableBox;
39 const sal_uInt32 CSV_COLUMN_INVALID = CSV_VEC_NOTFOUND;
41 /** This struct contains the state of one table column. */
42 struct ScCsvColState
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
60 private:
61 typedef ::std::unique_ptr< ScEditEngineDefaulter > ScEditEnginePtr;
63 ScCsvTableBox* mpTableBox; /// Grid Parent
64 VclPtr<VirtualDevice> mpBackgrDev; /// Grid background, headers, cell texts.
65 VclPtr<VirtualDevice> mpGridDev; /// Data grid with selection and cursor.
66 std::unique_ptr<weld::Menu> mxPopup; /// Popup menu for column types.
68 ::svtools::ColorConfig* mpColorConfig; /// Application color configuration.
69 Color maBackColor; /// Cell background color.
70 Color maGridColor; /// Table grid color.
71 Color maGridPBColor; /// Grid color for "first imported line" delimiter.
72 Color maAppBackColor; /// Background color for unused area.
73 Color maTextColor; /// Text color for data area.
74 Color maHeaderBackColor; /// Background color for headers.
75 Color maHeaderGridColor; /// Grid color for headers.
76 Color maHeaderTextColor; /// Text color for headers.
77 Color maSelectColor; /// Header color of selected columns.
79 ScEditEnginePtr mpEditEngine; /// For drawing cell texts.
80 vcl::Font maHeaderFont; /// Font for column and row headers.
81 vcl::Font maMonoFont; /// Monospace font for data cells.
82 Size maWinSize; /// Size of the control.
83 Size maEdEngSize; /// Paper size for edit engine.
85 ScCsvSplits maSplits; /// Vector with split positions.
86 ScCsvColStateVec maColStates; /// State of each column.
87 std::vector<OUString> maTypeNames; /// UI names of data types.
88 std::vector< std::vector<OUString> > maTexts; /// 2D-vector for cell texts.
90 sal_Int32 mnFirstImpLine; /// First imported line (0-based).
91 sal_uInt32 mnRecentSelCol; /// Index of most recently selected column.
92 sal_uInt32 mnMTCurrCol; /// Current column of mouse tracking.
93 bool mbTracking; /// True if Mouse tracking
94 bool mbMTSelecting; /// Mouse tracking mode: true = select, false = deselect.
96 public:
97 explicit ScCsvGrid(const ScCsvLayoutData& rData, std::unique_ptr<weld::Menu> xPopup, ScCsvTableBox* pTableBox);
98 virtual void SetDrawingArea(weld::DrawingArea* pDrawingArea) override;
99 ScCsvTableBox* GetTableBox() { return mpTableBox; }
100 virtual ~ScCsvGrid() override;
102 /** Finishes initialization. Must be called after constructing a new object. */
103 void Init();
105 // common grid handling ---------------------------------------------------
106 public:
107 /** Updates layout data dependent from the control's state. */
108 void UpdateLayoutData();
109 /** Updates X coordinate of first visible position dependent from line numbers. */
110 void UpdateOffsetX();
111 /** Apply current layout data to the grid control. */
112 void ApplyLayout( const ScCsvLayoutData& rOldData );
113 /** Sets the number of the first imported line (for visual feedback). nLine is 0-based! */
114 void SetFirstImportedLine( sal_Int32 nLine );
116 /** Finds a column position nearest to nPos which does not cause scrolling the visible area. */
117 sal_Int32 GetNoScrollCol( sal_Int32 nPos ) const;
119 private:
120 /** Reads colors from system settings. */
121 SAL_DLLPRIVATE void InitColors();
122 /** Initializes all font settings. */
123 SAL_DLLPRIVATE void InitFonts();
124 /** Initializes all data dependent from the control's size. */
125 SAL_DLLPRIVATE void InitSizeData();
127 // split handling ---------------------------------------------------------
128 public:
129 /** Inserts a split. */
130 void InsertSplit( sal_Int32 nPos );
131 /** Removes a split. */
132 void RemoveSplit( sal_Int32 nPos );
133 /** Inserts a new or removes an existing split. */
134 void MoveSplit( sal_Int32 nPos, sal_Int32 nNewPos );
135 /** Removes all splits. */
136 void RemoveAllSplits();
137 /** Removes all splits and inserts the splits from rSplits. */
138 void SetSplits( const ScCsvSplits& rSplits );
140 private:
141 /** Inserts a split and adjusts column data. */
142 SAL_DLLPRIVATE bool ImplInsertSplit( sal_Int32 nPos );
143 /** Removes a split and adjusts column data. */
144 SAL_DLLPRIVATE bool ImplRemoveSplit( sal_Int32 nPos );
145 /** Clears the split array and re-inserts boundary splits. */
146 SAL_DLLPRIVATE void ImplClearSplits();
148 // columns/column types ---------------------------------------------------
149 public:
150 /** Returns the number of columns. */
151 sal_uInt32 GetColumnCount() const { return maColStates.size(); }
152 /** Returns the index of the first visible column. */
153 sal_uInt32 GetFirstVisColumn() const;
154 /** Returns the index of the last visible column. */
155 sal_uInt32 GetLastVisColumn() const;
157 /** Returns true, if nColIndex points to an existing column. */
158 bool IsValidColumn( sal_uInt32 nColIndex ) const;
159 /** Returns true, if column with index nColIndex is (at least partly) visible. */
160 bool IsVisibleColumn( sal_uInt32 nColIndex ) const;
162 /** Returns X coordinate of the specified column. */
163 sal_Int32 GetColumnX( sal_uInt32 nColIndex ) const;
164 /** Returns column index from output coordinate. */
165 sal_uInt32 GetColumnFromX( sal_Int32 nX ) const;
167 /** Returns start position of the column with the specified index. */
168 sal_Int32 GetColumnPos( sal_uInt32 nColIndex ) const { return maSplits[ nColIndex ]; }
169 /** Returns column index from position. A split counts to its following column. */
170 sal_uInt32 GetColumnFromPos( sal_Int32 nPos ) const;
171 /** Returns the character width of the column with the specified index. */
172 sal_Int32 GetColumnWidth( sal_uInt32 nColIndex ) const;
174 /** Returns the vector with the states of all columns. */
175 const ScCsvColStateVec& GetColumnStates() const { return maColStates; }
176 /** Sets all column states to the values in the passed vector. */
177 void SetColumnStates( const ScCsvColStateVec& rColStates );
178 /** Returns the data type of the selected columns. */
179 sal_Int32 GetSelColumnType() const;
180 /** Changes the data type of all selected columns. */
181 void SetSelColumnType( sal_Int32 nType );
182 /** Sets new UI data type names. */
183 void SetTypeNames( const std::vector<OUString>& rTypeNames );
184 /** Returns the UI type name of the specified column. */
185 const OUString& GetColumnTypeName( sal_uInt32 nColIndex ) const;
187 /** Fills the options object with column data for separators mode. */
188 void FillColumnDataSep( ScAsciiOptions& rOptions ) const;
189 /** Fills the options object with column data for fixed width mode. */
190 void FillColumnDataFix( ScAsciiOptions& rOptions ) const;
192 private:
193 /** Returns the data type of the specified column. */
194 SAL_DLLPRIVATE sal_Int32 GetColumnType( sal_uInt32 nColIndex ) const;
195 /** Sets the data type of the specified column. */
196 SAL_DLLPRIVATE void SetColumnType( sal_uInt32 nColIndex, sal_Int32 nColType );
198 /** Scrolls data grid vertically. */
199 SAL_DLLPRIVATE void ScrollVertRel( ScMoveMode eDir );
200 /** Executes the data type popup menu. */
201 SAL_DLLPRIVATE void ExecutePopup( const Point& rPos );
203 // selection handling -----------------------------------------------------
204 public:
205 /** Returns true, if the specified column is selected. */
206 bool IsSelected( sal_uInt32 nColIndex ) const;
207 /** Returns index of the first selected column. */
208 sal_uInt32 GetFirstSelected() const;
209 /** Returns index of the first selected column really after nFromIndex. */
210 sal_uInt32 GetNextSelected( sal_uInt32 nFromIndex ) const;
211 /** Selects or deselects the specified column. */
212 void Select( sal_uInt32 nColIndex, bool bSelect = true );
213 /** Toggles selection of the specified column. */
214 void ToggleSelect( sal_uInt32 nColIndex );
215 /** Selects or deselects the specified column range. */
216 void SelectRange( sal_uInt32 nColIndex1, sal_uInt32 nColIndex2, bool bSelect = true );
217 /** Selects or deselects all columns. */
218 void SelectAll( bool bSelect = true );
220 /** Returns index of the focused column. */
221 sal_uInt32 GetFocusColumn() const { return GetColumnFromPos( GetGridCursorPos() ); }
223 private:
224 /** Moves column cursor to a new position. */
225 SAL_DLLPRIVATE void MoveCursor( sal_uInt32 nColIndex );
226 /** Moves column cursor to the given direction. */
227 SAL_DLLPRIVATE void MoveCursorRel( ScMoveMode eDir );
229 /** Clears the entire selection without notify. */
230 SAL_DLLPRIVATE void ImplClearSelection();
232 /** Executes selection action for a specific column. */
233 SAL_DLLPRIVATE void DoSelectAction( sal_uInt32 nColIndex, sal_uInt16 nModifier );
235 // cell contents ----------------------------------------------------------
236 public:
237 /** Fills all cells of a line with the passed text (separators mode). */
238 void ImplSetTextLineSep(
239 sal_Int32 nLine, const OUString& rTextLine,
240 const OUString& rSepChars, sal_Unicode cTextSep, bool bMergeSep, bool bRemoveSpace = false );
241 /** Fills all cells of a line with the passed text (fixed width mode). */
242 void ImplSetTextLineFix( sal_Int32 nLine, const OUString& rTextLine );
244 /** Returns the text of the specified cell. */
245 const OUString& GetCellText( sal_uInt32 nColIndex, sal_Int32 nLine ) const;
247 // event handling ---------------------------------------------------------
248 protected:
249 virtual void Resize() override;
250 virtual void GetFocus() override;
251 virtual void LoseFocus() override;
253 virtual bool MouseButtonDown( const MouseEvent& rMEvt ) override;
254 virtual bool MouseMove( const MouseEvent& rMEvt ) override;
255 virtual bool MouseButtonUp( const MouseEvent& rMEvt ) override;
256 virtual bool KeyInput( const KeyEvent& rKEvt ) override;
257 virtual bool Command( const CommandEvent& rCEvt ) override;
259 virtual tools::Rectangle GetFocusRect() override;
261 virtual void StyleUpdated() override;
263 virtual void ConfigurationChanged( ::utl::ConfigurationBroadcaster*, ConfigurationHints ) override;
265 // painting ---------------------------------------------------------------
266 protected:
267 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& ) override;
269 public:
270 /** Redraws the entire data grid. */
271 void ImplRedraw(vcl::RenderContext& rRenderContext);
272 /** Returns a pointer to the used edit engine. */
273 EditEngine* GetEditEngine();
275 private:
276 /** Returns the width of the control. */
277 sal_Int32 GetWidth() const { return maWinSize.Width(); }
278 /** Returns the height of the control. */
279 sal_Int32 GetHeight() const { return maWinSize.Height(); }
281 /** Sets a clip region in the specified output device for the specified column. */
282 SAL_DLLPRIVATE void ImplSetColumnClipRegion( OutputDevice& rOutDev, sal_uInt32 nColIndex );
283 /** Draws the header of the specified column to the specified output device. */
284 SAL_DLLPRIVATE void ImplDrawColumnHeader( OutputDevice& rOutDev, sal_uInt32 nColIndex, Color aFillColor );
286 /** Draws the text at the specified position to maBackgrDev. */
287 SAL_DLLPRIVATE void ImplDrawCellText( const Point& rPos, const OUString& rText );
288 /** Draws the "first imported line" separator to maBackgrDev (or erases, if bSet is false). */
289 SAL_DLLPRIVATE void ImplDrawFirstLineSep( bool bSet );
290 /** Draws the column with index nColIndex to maBackgrDev. */
291 SAL_DLLPRIVATE void ImplDrawColumnBackgr( sal_uInt32 nColIndex );
292 /** Draws the row headers column to maBackgrDev. */
293 SAL_DLLPRIVATE void ImplDrawRowHeaders();
294 /** Draws all columns and the row headers column to maBackgrDev. */
295 SAL_DLLPRIVATE void ImplDrawBackgrDev();
297 /** Draws the column with index nColIndex with its selection state to maGridDev. */
298 SAL_DLLPRIVATE void ImplDrawColumnSelection( sal_uInt32 nColIndex );
299 /** Draws all columns with selection and cursor to maGridDev. */
300 SAL_DLLPRIVATE void ImplDrawGridDev();
302 /** Redraws the entire column (background and selection). */
303 SAL_DLLPRIVATE void ImplDrawColumn( sal_uInt32 nColIndex );
305 /** Optimized drawing: Scrolls horizontally and redraws only missing parts. */
306 SAL_DLLPRIVATE void ImplDrawHorzScrolled( sal_Int32 nOldPos );
308 /** Inverts the cursor bar at the specified position in maGridDev. */
309 SAL_DLLPRIVATE void ImplInvertCursor( sal_Int32 nPos );
311 // accessibility ----------------------------------------------------------
312 protected:
313 /** Creates a new accessible object. */
314 virtual css::uno::Reference<css::accessibility::XAccessible> CreateAccessible() override;
317 #endif
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */