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