Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / inc / ChartController.hxx
blob752e551828c22ac711fd26e63ede2b380c03dbba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #pragma once
21 #include <LifeTime.hxx>
22 #include "CommandDispatchContainer.hxx"
23 #include "SelectionHelper.hxx"
25 #include <svx/svdtypes.hxx>
26 #include <vcl/timer.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <o3tl/sorted_vector.hxx>
30 #include <salhelper/simplereferenceobject.hxx>
32 #include <com/sun/star/frame/XDispatchProvider.hpp>
33 #include <com/sun/star/ui/XContextMenuInterception.hpp>
34 #include <com/sun/star/util/XModeChangeListener.hpp>
35 #include <com/sun/star/util/XCloseListener.hpp>
36 #include <com/sun/star/util/XModifyListener.hpp>
37 #include <com/sun/star/frame/XController2.hpp>
38 #include <com/sun/star/frame/XLayoutManagerListener.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <memory>
42 #include <string_view>
44 namespace com::sun::star::accessibility { class XAccessible; }
45 namespace com::sun::star::accessibility { class XAccessibleContext; }
46 namespace com::sun::star::awt { class XFocusListener; }
47 namespace com::sun::star::awt { class XKeyListener; }
48 namespace com::sun::star::awt { class XMouseListener; }
49 namespace com::sun::star::awt { class XMouseMotionListener; }
50 namespace com::sun::star::awt { class XPaintListener; }
51 namespace com::sun::star::awt { class XWindow; }
52 namespace com::sun::star::awt { class XWindowListener; }
53 namespace com::sun::star::awt { struct Point; }
54 namespace com::sun::star::document { class XUndoManager; }
55 namespace com::sun::star::frame { class XDispatch; }
56 namespace com::sun::star::frame { class XLayoutManagerEventBroadcaster; }
57 namespace com::sun::star::graphic { class XGraphic; }
58 namespace com::sun::star::lang { class XInitialization; }
59 namespace com::sun::star::uno { class XComponentContext; }
60 namespace com::sun::star::util { class XCloseable; }
61 namespace com::sun::star::view { class XSelectionSupplier; }
63 class SdrModel;
65 namespace svt
67 class AcceleratorExecute;
70 namespace svx::sidebar {
71 class SelectionChangeHandler;
74 namespace weld {
75 class Window;
78 class DropTargetHelper;
80 namespace chart
83 class UndoGuard;
84 class ChartView;
85 class ChartWindow;
86 class DrawModelWrapper;
87 class DrawViewWrapper;
88 class ReferenceSizeProvider;
89 class ViewElementListProvider;
90 class Diagram;
91 class AccessibleChartView;
92 class AccessibleTextHelper;
94 enum ChartDrawMode { CHARTDRAW_INSERT, CHARTDRAW_SELECT };
97 class ChartController final : public ::cppu::WeakImplHelper <
98 css::frame::XController2 //comprehends XComponent (css::frame::XController is required interface)
99 ,css::frame::XDispatchProvider //(required interface)
100 ,css::view::XSelectionSupplier //(optional interface)
101 ,css::ui::XContextMenuInterception //(optional interface)
102 ,css::util::XCloseListener //(needed for communication with XModel)
103 ,css::frame::XDispatch
104 ,css::awt::XWindow //this is the Window Controller part of this Controller, that will be given to a Frame via setComponent
105 ,css::util::XModifyListener
106 ,css::util::XModeChangeListener
107 ,css::frame::XLayoutManagerListener
110 public:
111 ChartController() = delete;
112 explicit ChartController(css::uno::Reference< css::uno::XComponentContext > xContext);
113 virtual ~ChartController() override;
115 OUString GetContextName();
117 // css::frame::XController (required interface)
118 virtual void SAL_CALL
119 attachFrame( const css::uno::Reference< css::frame::XFrame > & xFrame ) override;
121 virtual sal_Bool SAL_CALL
122 attachModel( const css::uno::Reference< css::frame::XModel > & xModel ) override;
124 virtual css::uno::Reference< css::frame::XFrame > SAL_CALL
125 getFrame() override;
127 virtual css::uno::Reference< css::frame::XModel > SAL_CALL
128 getModel() override;
130 virtual css::uno::Any SAL_CALL
131 getViewData() override;
133 virtual void SAL_CALL
134 restoreViewData( const css::uno::Any& rValue ) override;
136 virtual sal_Bool SAL_CALL
137 suspend( sal_Bool bSuspend ) override;
139 // css::frame::XController2
140 virtual css::uno::Reference<css::awt::XWindow> SAL_CALL getComponentWindow() override;
141 virtual OUString SAL_CALL getViewControllerName() override;
142 virtual css::uno::Sequence<css::beans::PropertyValue> SAL_CALL getCreationArguments() override;
143 virtual css::uno::Reference<css::ui::XSidebarProvider> SAL_CALL getSidebar() override;
145 // css::lang::XComponent (base of XController)
146 virtual void SAL_CALL
147 dispose() override;
149 virtual void SAL_CALL
150 addEventListener( const css::uno::Reference< css::lang::XEventListener > & xListener ) override;
152 virtual void SAL_CALL
153 removeEventListener( const css::uno::Reference< css::lang::XEventListener > & xListener ) override;
155 // css::frame::XDispatchProvider (required interface)
156 virtual css::uno::Reference< css::frame::XDispatch> SAL_CALL
157 queryDispatch( const css::util::URL& rURL
158 , const OUString& rTargetFrameName
159 , sal_Int32 nSearchFlags) override;
161 virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL
162 queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor > & xDescripts) override;
164 // css::view::XSelectionSupplier (optional interface)
165 virtual sal_Bool SAL_CALL
166 select( const css::uno::Any& rSelection ) override;
168 virtual css::uno::Any SAL_CALL
169 getSelection() override;
171 virtual void SAL_CALL
172 addSelectionChangeListener( const css::uno::Reference< css::view::XSelectionChangeListener > & xListener ) override;
174 virtual void SAL_CALL
175 removeSelectionChangeListener( const css::uno::Reference< css::view::XSelectionChangeListener > & xListener ) override;
177 // css::ui::XContextMenuInterception (optional interface)
178 virtual void SAL_CALL
179 registerContextMenuInterceptor( const css::uno::Reference< css::ui::XContextMenuInterceptor > & xInterceptor) override;
181 virtual void SAL_CALL
182 releaseContextMenuInterceptor( const css::uno::Reference< css::ui::XContextMenuInterceptor > & xInterceptor) override;
184 //additional interfaces
186 // css::util::XCloseListener
187 virtual void SAL_CALL
188 queryClosing( const css::lang::EventObject& Source
189 , sal_Bool GetsOwnership ) override;
191 virtual void SAL_CALL
192 notifyClosing( const css::lang::EventObject& Source ) override;
194 // css::util::XEventListener (base of XCloseListener and XModifyListener)
195 virtual void SAL_CALL
196 disposing( const css::lang::EventObject& Source ) override;
198 // css::frame::XDispatch
200 virtual void SAL_CALL
201 dispatch( const css::util::URL& aURL
202 , const css::uno::Sequence< css::beans::PropertyValue >& aArgs ) override;
204 virtual void SAL_CALL
205 addStatusListener( const css::uno::Reference< css::frame::XStatusListener >& xControl
206 , const css::util::URL& aURL ) override;
208 virtual void SAL_CALL
209 removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& xControl
210 , const css::util::URL& aURL ) override;
212 // css::awt::XWindow
213 virtual void SAL_CALL
214 setPosSize( sal_Int32 X, sal_Int32 Y
215 , sal_Int32 Width, sal_Int32 Height, sal_Int16 Flags ) override;
217 virtual css::awt::Rectangle SAL_CALL
218 getPosSize() override;
220 virtual void SAL_CALL
221 setVisible( sal_Bool Visible ) override;
223 virtual void SAL_CALL
224 setEnable( sal_Bool Enable ) override;
226 virtual void SAL_CALL
227 setFocus() override;
229 virtual void SAL_CALL
230 addWindowListener( const css::uno::Reference< css::awt::XWindowListener >& xListener ) override;
232 virtual void SAL_CALL
233 removeWindowListener( const css::uno::Reference< css::awt::XWindowListener >& xListener ) override;
235 virtual void SAL_CALL
236 addFocusListener( const css::uno::Reference< css::awt::XFocusListener >& xListener ) override;
238 virtual void SAL_CALL
239 removeFocusListener( const css::uno::Reference< css::awt::XFocusListener >& xListener ) override;
241 virtual void SAL_CALL
242 addKeyListener( const css::uno::Reference< css::awt::XKeyListener >& xListener ) override;
244 virtual void SAL_CALL
245 removeKeyListener( const css::uno::Reference< css::awt::XKeyListener >& xListener ) override;
247 virtual void SAL_CALL
248 addMouseListener( const css::uno::Reference< css::awt::XMouseListener >& xListener ) override;
250 virtual void SAL_CALL
251 removeMouseListener( const css::uno::Reference< css::awt::XMouseListener >& xListener ) override;
253 virtual void SAL_CALL
254 addMouseMotionListener( const css::uno::Reference< css::awt::XMouseMotionListener >& xListener ) override;
256 virtual void SAL_CALL
257 removeMouseMotionListener( const css::uno::Reference< css::awt::XMouseMotionListener >& xListener ) override;
259 virtual void SAL_CALL
260 addPaintListener( const css::uno::Reference< css::awt::XPaintListener >& xListener ) override;
262 virtual void SAL_CALL
263 removePaintListener( const css::uno::Reference< css::awt::XPaintListener >& xListener ) override;
265 // css::util::XModifyListener
266 virtual void SAL_CALL modified(
267 const css::lang::EventObject& aEvent ) override;
269 // css::util::XModeChangeListener
270 virtual void SAL_CALL modeChanged(
271 const css::util::ModeChangeEvent& _rSource ) override;
273 // css::frame::XLayoutManagerListener
274 virtual void SAL_CALL layoutEvent(
275 const css::lang::EventObject& aSource,
276 ::sal_Int16 eLayoutEvent,
277 const css::uno::Any& aInfo ) override;
279 // WindowController stuff
280 void PrePaint();
281 void execute_Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect);
282 void execute_MouseButtonDown( const MouseEvent& rMEvt );
283 void execute_MouseMove( const MouseEvent& rMEvt );
284 void execute_MouseButtonUp( const MouseEvent& rMEvt );
285 void execute_Resize();
286 void execute_Command( const CommandEvent& rCEvt );
287 bool execute_KeyInput( const KeyEvent& rKEvt );
289 /** get help text to be shown in a quick help
291 @param aAtLogicPosition the position in logic coordinates (of the
292 window) of the mouse cursor to determine for
293 which object help is requested.
295 @param bIsBalloonHelp determines whether to return the long text version
296 (balloon help) or the shorter one (quick help).
298 @param rOutQuickHelpText is filled with the quick help text
300 @param rOutEqualRect is filled with a rectangle that denotes the region
301 in which the quick help does not change.
303 @return </sal_True>, if a quick help should be shown.
305 bool requestQuickHelp(
306 ::Point aAtLogicPosition, bool bIsBalloonHelp,
307 OUString & rOutQuickHelpText, css::awt::Rectangle & rOutEqualRect );
309 css::uno::Reference< css::accessibility::XAccessible > CreateAccessible();
311 /** Creates a helper accessibility class that must be initialized via initialize(). For
312 parameters see
314 The returned object should not be used directly. Instead a proxy object
315 should use this helper to retrieve its children and add them to its own
316 children.
318 rtl::Reference< ::chart::AccessibleTextHelper > createAccessibleTextContext();
320 static bool isObjectDeleteable( const css::uno::Any& rSelection );
322 void setDrawMode( ChartDrawMode eMode ) { m_eDrawMode = eMode; }
324 bool isShapeContext() const;
326 ViewElementListProvider getViewElementListProvider();
327 DrawModelWrapper* GetDrawModelWrapper();
328 DrawViewWrapper* GetDrawViewWrapper();
329 ChartWindow* GetChartWindow() const;
330 weld::Window* GetChartFrame();
331 bool isAdditionalShapeSelected() const;
332 void SetAndApplySelection(const css::uno::Reference<css::drawing::XShape>& rxShape);
333 void StartTextEdit( const Point* pMousePixel = nullptr );
335 void NotifyUndoActionHdl( std::unique_ptr<SdrUndoAction> );
337 rtl::Reference<::chart::ChartView> const & getChartView() const { return m_xChartView; }
339 rtl::Reference<::chart::ChartModel> getChartModel();
340 rtl::Reference<::chart::Diagram> getFirstDiagram();
342 private:
343 class TheModel : public salhelper::SimpleReferenceObject
345 public:
346 explicit TheModel( rtl::Reference<::chart::ChartModel> xModel );
348 virtual ~TheModel() override;
350 void addListener( ChartController* pController );
351 void removeListener( ChartController* pController );
352 void tryTermination();
353 const rtl::Reference<::chart::ChartModel>&
354 getModel() const { return m_xModel;}
356 private:
357 rtl::Reference<::chart::ChartModel> m_xModel;
359 //the ownership between model and controller is not clear at first
360 //each controller might consider himself as owner of the model first
361 bool m_bOwnership;
363 class TheModelRef final
365 public:
366 TheModelRef( TheModel* pTheModel, ::osl::Mutex& rMutex );
367 TheModelRef( const TheModelRef& rTheModel, ::osl::Mutex& rMutex );
368 TheModelRef& operator=(ChartController::TheModel* pTheModel);
369 TheModelRef& operator=(const TheModelRef& rTheModel);
370 ~TheModelRef();
371 bool is() const;
372 TheModel* operator->() const { return m_xTheModel.get(); }
373 private:
374 rtl::Reference<TheModel> m_xTheModel;
375 ::osl::Mutex& m_rModelMutex;
378 mutable ::apphelper::LifeTimeManager m_aLifeTimeManager;
380 bool m_bSuspended;
382 css::uno::Reference< css::uno::XComponentContext> m_xCC;
384 //model
385 css::uno::Reference< css::frame::XFrame > m_xFrame;
386 mutable ::osl::Mutex m_aModelMutex;
387 TheModelRef m_aModel;
389 //view
390 css::uno::Reference<css::awt::XWindow> m_xViewWindow;
391 rtl::Reference<::chart::ChartView> m_xChartView;
392 std::shared_ptr< DrawModelWrapper > m_pDrawModelWrapper;
393 std::unique_ptr<DrawViewWrapper> m_pDrawViewWrapper;
395 Selection m_aSelection;
396 SdrDragMode m_eDragMode;
398 Timer m_aDoubleClickTimer;
399 bool m_bWaitingForDoubleClick;
400 bool m_bWaitingForMouseUp;
401 bool m_bFieldButtonDown;
403 bool m_bConnectingToView;
404 bool m_bDisposed;
406 css::uno::Reference< css::document::XUndoManager > m_xUndoManager;
407 std::unique_ptr< UndoGuard > m_pTextActionUndoGuard;
409 std::unique_ptr< ::svt::AcceleratorExecute > m_apAccelExecute;
411 CommandDispatchContainer m_aDispatchContainer;
413 std::unique_ptr< DropTargetHelper > m_apDropTargetHelper;
414 css::uno::Reference<
415 css::frame::XLayoutManagerEventBroadcaster > m_xLayoutManagerEventBroadcaster;
417 ChartDrawMode m_eDrawMode;
419 rtl::Reference<svx::sidebar::SelectionChangeHandler> mpSelectionChangeHandler;
421 bool impl_isDisposedOrSuspended() const;
422 std::unique_ptr<ReferenceSizeProvider> impl_createReferenceSizeProvider();
423 void impl_adaptDataSeriesAutoResize();
425 void impl_createDrawViewController();
426 void impl_deleteDrawViewController();
428 //executeDispatch methods
429 void executeDispatch_ObjectProperties();
430 void executeDispatch_FormatObject( std::u16string_view rDispatchCommand );
431 void executeDlg_ObjectProperties( const OUString& rObjectCID );
432 void executeDlg_ObjectProperties_withUndoGuard( std::shared_ptr<UndoGuard> aUndoGuard, const OUString& rObjectCID, bool bSuccessOnUnchanged );
434 void executeDispatch_ChartType();
436 void executeDispatch_InsertTitles();
437 void executeDispatch_InsertLegend();
438 void executeDispatch_DeleteLegend();
439 void executeDispatch_OpenLegendDialog();
440 void executeDispatch_InsertAxes();
441 void executeDispatch_InsertGrid();
443 void executeDispatch_InsertDataTable();
444 void executeDispatch_DeleteDataTable();
445 void executeDispatch_OpenInsertDataTableDialog();
447 void executeDispatch_InsertMenu_DataLabels();
448 void executeDispatch_InsertMenu_Trendlines();
449 void executeDispatch_InsertMenu_MeanValues();
451 void executeDispatch_InsertMeanValue();
452 void executeDispatch_InsertTrendline();
453 void executeDispatch_InsertTrendlineEquation( bool bInsertR2=false );
454 void executeDispatch_InsertErrorBars( bool bYError );
456 void executeDispatch_InsertR2Value();
457 void executeDispatch_DeleteR2Value();
459 void executeDispatch_DeleteMeanValue();
460 void executeDispatch_DeleteTrendline();
461 void executeDispatch_DeleteTrendlineEquation();
462 void executeDispatch_DeleteErrorBars( bool bYError );
464 void executeDispatch_InsertDataLabels();
465 void executeDispatch_InsertDataLabel();
466 void executeDispatch_DeleteDataLabels();
467 void executeDispatch_DeleteDataLabel();
469 void executeDispatch_ResetAllDataPoints();
470 void executeDispatch_ResetDataPoint();
472 void executeDispatch_InsertAxis();
473 void executeDispatch_InsertAxisTitle();
474 void executeDispatch_InsertMajorGrid();
475 void executeDispatch_InsertMinorGrid();
476 void executeDispatch_DeleteAxis();
477 void executeDispatch_DeleteMajorGrid();
478 void executeDispatch_DeleteMinorGrid();
480 void executeDispatch_InsertSpecialCharacter();
481 void executeDispatch_EditText( const Point* pMousePixel = nullptr );
482 void executeDispatch_SourceData();
483 void executeDispatch_MoveSeries( bool bForward );
485 bool EndTextEdit();
487 void executeDispatch_View3D();
488 void executeDispatch_PositionAndSize( const ::css::uno::Sequence< ::css::beans::PropertyValue >* pArgs = nullptr );
490 void executeDispatch_EditData();
492 void executeDispatch_NewArrangement();
493 void executeDispatch_ScaleText();
495 void executeDispatch_Paste();
496 void executeDispatch_Copy();
497 void executeDispatch_Cut();
498 bool executeDispatch_Delete();
499 void executeDispatch_ToggleLegend();
500 void executeDispatch_ToggleGridHorizontal();
501 void executeDispatch_ToggleGridVertical();
503 void executeDispatch_LOKSetTextSelection(int nType, int nX, int nY);
504 void executeDispatch_LOKPieSegmentDragging(int nOffset);
505 void executeDispatch_FillColor(sal_uInt32 nColor);
506 void executeDispatch_FillGradient(std::u16string_view sJSONGradient);
507 void executeDispatch_LineColor(sal_uInt32 nColor);
508 void executeDispatch_LineWidth(sal_uInt32 nWidth);
510 void sendPopupRequest(std::u16string_view rCID, tools::Rectangle aRectangle);
512 void impl_ShapeControllerDispatch( const css::util::URL& rURL,
513 const css::uno::Sequence< css::beans::PropertyValue >& rArgs );
515 DECL_LINK( DoubleClickWaitingHdl, Timer*, void );
516 void execute_DoubleClick( const Point* pMousePixel );
517 void startDoubleClickWaiting();
518 void stopDoubleClickWaiting();
520 void impl_selectObjectAndNotiy();
521 void impl_notifySelectionChangeListeners();
522 void impl_invalidateAccessible();
523 void impl_initializeAccessible();
524 void impl_initializeAccessible( AccessibleChartView& xInit );
526 //sets the model member to null if it equals the parameter
527 //returns true if successful
528 bool impl_releaseThisModel( const css::uno::Reference< css::uno::XInterface > & xModel );
530 enum eMoveOrResizeType
532 MOVE_OBJECT,
533 CENTERED_RESIZE_OBJECT
535 /// @return </sal_True>, if resize/move was successful
536 bool impl_moveOrResizeObject(
537 const OUString & rCID, eMoveOrResizeType eType, double fAmountLogicX, double fAmountLogicY );
538 bool impl_DragDataPoint( std::u16string_view rCID, double fOffset );
540 static const o3tl::sorted_vector< OUString >& impl_getAvailableCommands();
542 void impl_PasteGraphic( css::uno::Reference< css::graphic::XGraphic > const & xGraphic,
543 const ::Point & aPosition );
544 void impl_PasteShapes( SdrModel* pModel );
545 void impl_PasteStringAsTextShape( const OUString& rString, const css::awt::Point& rPosition );
546 void impl_SetMousePointer( const MouseEvent & rEvent );
548 void impl_ClearSelection();
550 void impl_switchDiagramPositioningToExcludingPositioning();
553 } // namespace chart
555 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */