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