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 #pragma once 21 22 #include <vcl/virdev.hxx> 23 #include <unotools/options.hxx> 24 25 #include <vector> 26 #include <memory> 27 #include <scdllapi.h> 28 #include <editutil.hxx> 29 #include "csvcontrol.hxx" 30 #include "csvsplits.hxx" 31 32 namespace svtools { class ColorConfig; } 33 class EditEngine; 34 class ScAsciiOptions; 35 class ScAccessibleCsvControl; 36 class ScCsvTableBox; 37 38 const sal_uInt32 CSV_COLUMN_INVALID = CSV_VEC_NOTFOUND; 39 40 /** This struct contains the state of one table column. */ 41 struct ScCsvColState 42 { 43 sal_Int32 mnType; /// Data type. 44 bool mbColumnSelected; 45 ScCsvColStateScCsvColState46 explicit ScCsvColState( sal_Int32 nType = CSV_TYPE_DEFAULT ) : 47 mnType( nType ), mbColumnSelected( false ) {} 48 IsSelectedScCsvColState49 bool IsSelected() const { return mbColumnSelected; } SelectScCsvColState50 void Select( bool bSel ) { mbColumnSelected = bSel; } 51 }; 52 53 typedef ::std::vector< ScCsvColState > ScCsvColStateVec; 54 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 SC_DLLPUBLIC ScCsvGrid : public ScCsvControl, public utl::ConfigurationListener 58 { 59 private: 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. 64 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. 75 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. 82 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. 87 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. 93 94 public: 95 explicit ScCsvGrid(const ScCsvLayoutData& rData, std::unique_ptr<weld::Menu> xPopup, ScCsvTableBox* pTableBox); 96 virtual void SetDrawingArea(weld::DrawingArea* pDrawingArea) override; GetTableBox()97 ScCsvTableBox* GetTableBox() { return mpTableBox; } 98 virtual ~ScCsvGrid() override; 99 100 /** Finishes initialization. Must be called after constructing a new object. */ 101 void Init(); 102 103 // common grid handling --------------------------------------------------- 104 public: 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 ); 113 114 /** Finds a column position nearest to nPos which does not cause scrolling the visible area. */ 115 sal_Int32 GetNoScrollCol( sal_Int32 nPos ) const; 116 117 private: 118 /** Reads colors from system settings. */ 119 SAL_DLLPRIVATE void InitColors(); 120 /** Initializes all font settings. */ 121 SAL_DLLPRIVATE void InitFonts(); 122 /** Initializes all data dependent from the control's size. */ 123 SAL_DLLPRIVATE void InitSizeData(); 124 125 // split handling --------------------------------------------------------- 126 public: 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 ); 137 138 private: 139 /** Inserts a split and adjusts column data. */ 140 SAL_DLLPRIVATE bool ImplInsertSplit( sal_Int32 nPos ); 141 /** Removes a split and adjusts column data. */ 142 SAL_DLLPRIVATE bool ImplRemoveSplit( sal_Int32 nPos ); 143 /** Clears the split array and re-inserts boundary splits. */ 144 SAL_DLLPRIVATE void ImplClearSplits(); 145 146 // columns/column types --------------------------------------------------- 147 public: 148 /** Returns the number of columns. */ GetColumnCount() const149 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; 154 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; 159 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; 164 165 /** Returns start position of the column with the specified index. */ GetColumnPos(sal_uInt32 nColIndex) const166 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; 171 172 /** Returns the vector with the states of all columns. */ GetColumnStates() const173 const ScCsvColStateVec& GetColumnStates() const { return maColStates; } 174 /** Sets all column states to the values in the passed vector. */ 175 void SetColumnStates( const ScCsvColStateVec& rColStates ); 176 /** Returns the data type of the selected columns. */ 177 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( const std::vector<OUString>& rTypeNames ); 182 /** Returns the UI type name of the specified column. */ 183 const OUString& GetColumnTypeName( sal_uInt32 nColIndex ) const; 184 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; 189 190 private: 191 /** Returns the data type of the specified column. */ 192 SAL_DLLPRIVATE sal_Int32 GetColumnType( sal_uInt32 nColIndex ) const; 193 /** Sets the data type of the specified column. */ 194 SAL_DLLPRIVATE void SetColumnType( sal_uInt32 nColIndex, sal_Int32 nColType ); 195 196 /** Scrolls data grid vertically. */ 197 SAL_DLLPRIVATE void ScrollVertRel( ScMoveMode eDir ); 198 /** Executes the data type popup menu. */ 199 SAL_DLLPRIVATE void ExecutePopup( const Point& rPos ); 200 201 // selection handling ----------------------------------------------------- 202 public: 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 ); 217 218 /** Returns index of the focused column. */ GetFocusColumn() const219 sal_uInt32 GetFocusColumn() const { return GetColumnFromPos( GetGridCursorPos() ); } 220 221 private: 222 /** Moves column cursor to a new position. */ 223 SAL_DLLPRIVATE void MoveCursor( sal_uInt32 nColIndex ); 224 /** Moves column cursor to the given direction. */ 225 SAL_DLLPRIVATE void MoveCursorRel( ScMoveMode eDir ); 226 227 /** Clears the entire selection without notify. */ 228 SAL_DLLPRIVATE void ImplClearSelection(); 229 230 /** Executes selection action for a specific column. */ 231 SAL_DLLPRIVATE void DoSelectAction( sal_uInt32 nColIndex, sal_uInt16 nModifier ); 232 233 // cell contents ---------------------------------------------------------- 234 public: 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, const OUString& rTextLine ); 241 242 /** Returns the text of the specified cell. */ 243 const OUString& GetCellText( sal_uInt32 nColIndex, sal_Int32 nLine ) const; 244 245 // event handling --------------------------------------------------------- 246 protected: 247 virtual void Resize() override; 248 virtual void GetFocus() override; 249 virtual void LoseFocus() override; 250 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; 256 257 virtual tools::Rectangle GetFocusRect() override; 258 259 virtual void StyleUpdated() override; 260 261 virtual void ConfigurationChanged( ::utl::ConfigurationBroadcaster*, ConfigurationHints ) override; 262 263 // painting --------------------------------------------------------------- 264 protected: 265 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& ) override; 266 267 public: 268 /** Redraws the entire data grid. */ 269 void ImplRedraw(vcl::RenderContext& rRenderContext); 270 /** Returns a pointer to the used edit engine. */ 271 EditEngine* GetEditEngine(); 272 273 private: 274 /** Returns the width of the control. */ GetWidth() const275 sal_Int32 GetWidth() const { return maWinSize.Width(); } 276 /** Returns the height of the control. */ GetHeight() const277 sal_Int32 GetHeight() const { return maWinSize.Height(); } 278 279 /** Sets a clip region in the specified output device for the specified column. */ 280 SAL_DLLPRIVATE void ImplSetColumnClipRegion( OutputDevice& rOutDev, sal_uInt32 nColIndex ); 281 /** Draws the header of the specified column to the specified output device. */ 282 SAL_DLLPRIVATE void ImplDrawColumnHeader( OutputDevice& rOutDev, sal_uInt32 nColIndex, Color aFillColor ); 283 284 /** Draws the text at the specified position to maBackgrDev. */ 285 SAL_DLLPRIVATE void ImplDrawCellText( const Point& rPos, const OUString& rText ); 286 /** Draws the "first imported line" separator to maBackgrDev (or erases, if bSet is false). */ 287 SAL_DLLPRIVATE void ImplDrawFirstLineSep( bool bSet ); 288 /** Draws the column with index nColIndex to maBackgrDev. */ 289 SAL_DLLPRIVATE void ImplDrawColumnBackgr( sal_uInt32 nColIndex ); 290 /** Draws the row headers column to maBackgrDev. */ 291 SAL_DLLPRIVATE void ImplDrawRowHeaders(); 292 /** Draws all columns and the row headers column to maBackgrDev. */ 293 SAL_DLLPRIVATE void ImplDrawBackgrDev(); 294 295 /** Draws the column with index nColIndex with its selection state to maGridDev. */ 296 SAL_DLLPRIVATE void ImplDrawColumnSelection( sal_uInt32 nColIndex ); 297 /** Draws all columns with selection and cursor to maGridDev. */ 298 SAL_DLLPRIVATE void ImplDrawGridDev(); 299 300 /** Redraws the entire column (background and selection). */ 301 SAL_DLLPRIVATE void ImplDrawColumn( sal_uInt32 nColIndex ); 302 303 /** Optimized drawing: Scrolls horizontally and redraws only missing parts. */ 304 SAL_DLLPRIVATE void ImplDrawHorzScrolled( sal_Int32 nOldPos ); 305 306 /** Inverts the cursor bar at the specified position in maGridDev. */ 307 SAL_DLLPRIVATE void ImplInvertCursor( sal_Int32 nPos ); 308 309 // accessibility ---------------------------------------------------------- 310 protected: 311 /** Creates a new accessible object. */ 312 virtual css::uno::Reference<css::accessibility::XAccessible> CreateAccessible() override; 313 }; 314 315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 316