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