xref: /aoo41x/main/sc/source/ui/inc/fieldwnd.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SC_FIELDWND_HXX
29 #define SC_FIELDWND_HXX
30 
31 #include <utility>
32 #include <vector>
33 #include <boost/shared_ptr.hpp>
34 #include <cppuhelper/weakref.hxx>
35 #include <rtl/ref.hxx>
36 #include <vcl/ctrl.hxx>
37 #include <vcl/fixed.hxx>
38 
39 #include "address.hxx"
40 #include "pivot.hxx"
41 
42 // ============================================================================
43 
44 class ScPivotLayoutDlg;
45 class ScAccessibleDataPilotControl;
46 
47 const size_t PIVOTFIELD_INVALID = static_cast< size_t >( -1 );
48 
49 // ============================================================================
50 
51 /** Type of the pivot table field window. */
52 enum ScPivotFieldType
53 {
54     PIVOTFIELDTYPE_PAGE,            /// Window for all page fields.
55     PIVOTFIELDTYPE_COL,             /// Window for all column fields.
56     PIVOTFIELDTYPE_ROW,             /// Window for all row fields.
57     PIVOTFIELDTYPE_DATA,            /// Window for all data fields.
58     PIVOTFIELDTYPE_SELECT           /// Selection window with all fields.
59 };
60 
61 /** Type of an end tracking event. */
62 enum ScPivotFieldEndTracking
63 {
64     ENDTRACKING_SUSPEND,            /// Stop tracking in this window, but tracking still active (in another window).
65     ENDTRACKING_CANCEL,             /// Tracking has been cancelled.
66     ENDTRACKING_DROP                /// Tracking has ended, a field has been dropped.
67 };
68 
69 // ============================================================================
70 
71 typedef ::std::pair< const ScPivotFuncData*, size_t > ScPivotFuncDataEntry;
72 
73 // ============================================================================
74 
75 /** Represents a field area in the pivot table layout dialog. */
76 class ScPivotFieldWindow : public Control
77 {
78 public:
79                         ScPivotFieldWindow(
80                             ScPivotLayoutDlg* pDialog,
81                             const ResId& rResId,
82                             ScrollBar& rScrollBar,
83                             FixedText* pFtCaption,
84                             const ::rtl::OUString& rName,
85                             ScPivotFieldType eFieldType,
86                             const sal_Char* pcHelpId,
87                             PointerStyle eDropPointer,
88                             size_t nColCount,
89                             size_t nRowCount,
90                             long nFieldWidthFactor,
91                             long nSpaceSize );
92 
93     virtual             ~ScPivotFieldWindow();
94 
95     /** Initializes this field window from the passed label data (used for selection window). */
96     void                ReadDataLabels( const ScDPLabelDataVector& rLabels );
97     /** Initializes this field window from the passed field data (used for row/col/page/data window). */
98     void                ReadPivotFields( const ScPivotFieldVector& rPivotFields );
99 
100     /** Fills the passed vector with the plain names of all fields from this field window. */
101     void                WriteFieldNames( ScDPNameVec& rFieldNames ) const;
102     /** Fills the passed pivot field vector with the fields of this field window. */
103     void                WritePivotFields( ScPivotFieldVector& rPivotFields ) const;
104 
105     /** Returns the type of this field window. */
106     inline ScPivotFieldType GetType() const { return meFieldType; }
107     /** Returns the mouse pointer style for tracking over this window. */
108     inline PointerStyle GetDropPointerStyle() const { return meDropPointer; }
109     /** Returns the name of the control without shortcut. */
110     inline ::rtl::OUString GetName() const { return maName; }
111     /** Returns the description of the control which is used for the accessibility objects. */
112     ::rtl::OUString     GetDescription() const;
113 
114     /** Returns true, if the window is empty. */
115     inline bool         IsEmpty() const { return maFields.empty(); }
116     /** Returns the number of existing fields. */
117     inline size_t       GetFieldCount() const { return maFields.size(); }
118     /** Returns the text of an existing field. */
119     ::rtl::OUString     GetFieldText( size_t nFieldIndex ) const;
120 
121     /** Returns the index of a field with the specified column identifier. */
122     ScPivotFuncDataEntry FindFuncDataByCol( SCCOL nCol ) const;
123 
124     /** Returns the pixel size of a field. */
125     inline const Size&  GetFieldSize() const { return maFieldSize; }
126     /** Returns the pixel position of a field (without bound check). */
127     Point               GetFieldPosition( size_t nFieldIndex ) const;
128     /** Calculates the field index at a specific pixel position. */
129     size_t              GetFieldIndex( const Point& rWindowPos ) const;
130     /** Calculates the field insertion index for mouse drop at a specific pixel position. */
131     size_t              GetDropIndex( const Point& rWindowPos ) const;
132 
133     /** Returns the index of the selected field. */
134     inline size_t       GetSelectedIndex() const { return mnSelectIndex; }
135     /** Grabs focus and sets the passed selection. */
136     void                GrabFocusAndSelect( size_t nIndex );
137     /** Selects the next field. */
138     void                SelectNextField();
139 
140     /** Inserts a new field in front of the specified field. */
141     void                InsertField( size_t nInsertIndex, const ScPivotFuncData& rFuncData );
142     /** Removes the specified field. */
143     bool                RemoveField( size_t nRemoveIndex );
144     /** Moves the specified field to a new position. */
145     bool                MoveField( size_t nFieldIndex, size_t nInsertIndex );
146 
147     /** Returns the selected field (pointer is valid as long as field vector is not changed). */
148     const ScPivotFuncData* GetSelectedFuncData() const;
149     /** Removes the selected field. */
150     void                ModifySelectedField( const ScPivotFuncData& rFuncData );
151     /** Removes the selected field. */
152     bool                RemoveSelectedField();
153     /** Moves the selected field in front of the specified field. */
154     bool                MoveSelectedField( size_t nInsertIndex );
155 
156     /** Called from dialog when tracking starts in this field window. */
157     void                NotifyStartTracking();
158     /** Called from dialog while tracking in this field window. */
159     void                NotifyTracking( const Point& rWindowPos );
160     /** Called from dialog when tracking ends in this field window. */
161     void                NotifyEndTracking( ScPivotFieldEndTracking eEndType );
162 
163 protected:
164     virtual void        Paint( const Rectangle& rRect );
165     virtual void        StateChanged( StateChangedType nStateChange );
166     virtual void        DataChanged( const DataChangedEvent& rDCEvt );
167     virtual void        KeyInput( const KeyEvent& rKEvt );
168     virtual void        MouseButtonDown( const MouseEvent& rMEvt );
169     virtual void        RequestHelp( const HelpEvent& rHEvt );
170     virtual void        GetFocus();
171     virtual void        LoseFocus();
172     virtual ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible >
173                         CreateAccessible();
174 
175 private:
176     /** A structure containing all data needed for a field in this window. */
177     struct ScPivotWindowField
178     {
179         ScPivotFuncData     maFuncData;         /// Field data from source pivot table.
180         ::rtl::OUString     maFieldName;        /// Name displayed on the field button.
181         bool                mbClipped;          /// True = field text does not fit into button.
182         explicit            ScPivotWindowField( const ScDPLabelData& rLabelData );
183         explicit            ScPivotWindowField( ScPivotLayoutDlg& rDialog, const ScPivotField& rField, bool bDataWindow );
184         explicit            ScPivotWindowField( ScPivotLayoutDlg& rDialog, const ScPivotFuncData& rFuncData, bool bDataWindow );
185         void                InitFieldName( ScPivotLayoutDlg& rDialog, bool bDataWindow );
186     };
187 
188     /** Specifies how the selection cursor can move in the window. */
189     enum MoveType { PREV_FIELD, NEXT_FIELD, PREV_LINE, NEXT_LINE, PREV_PAGE, NEXT_PAGE, FIRST_FIELD, LAST_FIELD };
190 
191     /** Calculates a scroll position to make the passed field visible. Tries to
192         stick to current scroll position if possible. */
193     size_t              RecalcVisibleIndex( size_t nSelectIndex ) const;
194 
195     /** Sets selection to the specified field and changes scrolling position. */
196     void                SetSelectionUnchecked( size_t nSelectIndex, size_t nFirstVisIndex );
197     /** Selects a field and adjusts scrolling position to make the field visible. */
198     void                MoveSelection( size_t nSelectIndex );
199     /** Sets selection to a new position relative to current. */
200     void                MoveSelection( MoveType eMoveType );
201     /** Moves the selected field to a new position relative to current. */
202     void                MoveSelectedField( MoveType eMoveType );
203 
204     /** Inserts the passed field into the vector and notifies accessibility object. */
205     void                InsertFieldUnchecked( size_t nInsertIndex, const ScPivotWindowField& rField );
206     /** Removes the specified field from the vector and notifies accessibility object. */
207     void                RemoveFieldUnchecked( size_t nRemoveIndex );
208 
209     /** Draws the background. */
210     void                DrawBackground( OutputDevice& rDev );
211     /** Draws the specified field. */
212     void                DrawField( OutputDevice& rDev, size_t nFieldIndex );
213     /** Draws the insertion cursor. */
214     void                DrawInsertionCursor( OutputDevice& rDev );
215 
216     /** Returns a reference to the accessiblity object, if still alive. */
217     ::rtl::Reference< ScAccessibleDataPilotControl > GetAccessibleControl();
218 
219 	DECL_LINK( ScrollHdl, ScrollBar* );
220 
221 private:
222     typedef ::std::vector< ScPivotWindowField > ScPivotWindowFieldVector;
223 
224     ScPivotLayoutDlg*   mpDialog;           /// Parent pivot table dialog.
225     ::com::sun::star::uno::WeakReference< ::com::sun::star::accessibility::XAccessible >
226                         mxAccessible;       /// Weak reference to the accessible object.
227     ScAccessibleDataPilotControl*
228                         mpAccessible;       /// Pointer to the accessible implementation.
229     ScrollBar&          mrScrollBar;        /// Scrollbar of the select window.
230     FixedText*          mpFtCaption;        /// Associated fixedtext control with caption text and mnemonic.
231     ::rtl::OUString     maName;             /// Name of the control, used for accessibility.
232     ScPivotWindowFieldVector maFields;      /// Vector with all fields contained in this window.
233     Size                maFieldSize;        /// Size of a single field in pixels.
234     Size                maSpaceSize;        /// Size between fields in pixels.
235     ScPivotFieldType    meFieldType;        /// Type of this field window.
236     PointerStyle        meDropPointer;      /// Mouse pointer style for tracking over this window.
237     size_t              mnColCount;         /// Number of field columns.
238     size_t              mnRowCount;         /// Number of field rows.
239     size_t              mnLineSize;         /// Number of fields per line (depending on scrolling orientation).
240     size_t              mnPageSize;         /// Number of visible fields.
241     size_t              mnFirstVisIndex;    /// Index of first visible field (scrolling offset).
242     size_t              mnSelectIndex;      /// Currently selected field.
243     size_t              mnInsCursorIndex;   /// Position of the insertion cursor.
244     size_t              mnOldFirstVisIndex; /// Stores original scroll position during auto scrolling.
245     size_t              mnAutoScrollDelay;  /// Initial counter before auto scrolling starts on tracking.
246     bool                mbVertical;         /// True = sort fields vertically.
247     bool                mbIsTrackingSource; /// True = this field window is the source while tracking.
248 };
249 
250 // ============================================================================
251 
252 #endif
253