tdf#149529 crash on deref deleted ScDocument*
[LibreOffice.git] / include / vcl / ctrl.hxx
bloba6797cc734bb6f535ff4a15a8a0c0843e3568e79
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 .
20 #ifndef INCLUDED_VCL_CTRL_HXX
21 #define INCLUDED_VCL_CTRL_HXX
23 #include <rtl/ustring.hxx>
24 #include <tools/link.hxx>
25 #include <tools/gen.hxx>
26 #include <vcl/dllapi.h>
27 #include <vcl/window.hxx>
28 #include <optional>
29 #include <vector>
31 // forward
32 class StyleSettings;
33 class Control;
35 namespace vcl
38 struct VCL_DLLPUBLIC ControlLayoutData
40 // contains the string really displayed
41 // there must be exactly one bounding rectangle in m_aUnicodeBoundRects
42 // for every character in m_aDisplayText
43 OUString m_aDisplayText;
44 // the bounding rectangle of every character
45 // where one character may consist of many glyphs
46 std::vector< tools::Rectangle > m_aUnicodeBoundRects;
47 // start indices of lines
48 std::vector< tools::Long > m_aLineIndices;
49 // notify parent control on destruction
50 VclPtr<const Control> m_pParent;
52 ControlLayoutData();
53 ~ControlLayoutData();
55 tools::Rectangle GetCharacterBounds( tools::Long nIndex ) const;
56 // returns the character index for corresponding to rPoint (in control coordinates)
57 // -1 is returned if no character is at that point
58 tools::Long GetIndexForPoint( const Point& rPoint ) const;
59 // returns the number of lines in the result of GetDisplayText()
60 tools::Long GetLineCount() const;
61 // returns the interval [start,end] of line nLine
62 // returns [-1,-1] for an invalid line
63 ::Pair GetLineStartEnd( tools::Long nLine ) const;
64 /** ToRelativeLineIndex changes a layout data index to a count relative to its line.
66 This is equivalent to getting the line start/end pairs with
67 GetLineStartEnd until the index lies within [start,end] of a line
69 @param nIndex
70 the absolute index inside the display text to be changed to a relative index
72 @returns
73 the relative index inside the displayed line or -1 if the absolute index does
74 not match any line
76 tools::Long ToRelativeLineIndex( tools::Long nIndex ) const;
79 } // namespace vcl
81 class VCL_DLLPUBLIC Control : public vcl::Window
83 protected:
84 mutable std::optional<vcl::ControlLayoutData> mxLayoutData;
85 VclPtr<OutputDevice> mpReferenceDevice;
87 private:
88 bool mbHasControlFocus;
89 bool mbShowAccelerator;
90 Link<Control&,void> maLoseFocusHdl;
92 SAL_DLLPRIVATE void ImplInitControlData();
94 Control (const Control &) = delete;
95 Control & operator= (const Control &) = delete;
97 protected:
98 Control( WindowType nType );
99 virtual void FillLayoutData() const;
101 // helper method for composite controls
102 void AppendLayoutData( const Control& rSubControl ) const;
104 /// creates the mpData->mpLayoutData structure
105 void CreateLayoutData() const;
106 /// determines whether we currently have layout data
107 bool HasLayoutData() const;
109 /** this calls both our event listeners, and a specified handler
111 If the Control instance is destroyed during any of those calls, the
112 method properly handles this (in particular, it doesn't crash :)
114 @param nEvent
115 the event to notify to our event listeners
116 @param callHandler
117 the lambda function that calls the handler
118 @return
119 if the Control instance has been destroyed in any of the call
121 bool ImplCallEventListenersAndHandler(
122 VclEventId nEvent, std::function<void()> const & callHandler
125 void CallEventListeners( VclEventId nEvent, void* pData = nullptr );
127 /** draws the given text onto the given device
129 If no reference device is set, the draw request will simply be forwarded to OutputDevice::DrawText. Otherwise,
130 the text will be rendered according to the metrics at the reference device.
132 return will contain the result of a GetTextRect call (either directly
133 at the target device, or taking the reference device into account) when
134 returning.
136 tools::Rectangle DrawControlText( OutputDevice& _rTargetDevice, const tools::Rectangle& _rRect,
137 const OUString& _rStr, DrawTextFlags _nStyle,
138 std::vector< tools::Rectangle >* _pVector, OUString* _pDisplayText,
139 const Size* i_pDeviceSize = nullptr ) const;
141 tools::Rectangle GetControlTextRect( OutputDevice& _rTargetDevice, const tools::Rectangle & rRect,
142 const OUString& _rStr, DrawTextFlags _nStyle,
143 Size* o_pDeviceSize = nullptr ) const;
145 virtual const vcl::Font&
146 GetCanonicalFont( const StyleSettings& _rStyle ) const;
147 virtual const Color&
148 GetCanonicalTextColor( const StyleSettings& _rStyle ) const;
150 void ImplInitSettings();
152 virtual void ApplySettings(vcl::RenderContext& rRenderContext) override;
154 public:
155 SAL_DLLPRIVATE void ImplClearLayoutData() const;
156 /** draws a frame around the give rectangle, onto the given device
158 only to be used from within the Window::Draw method of your sub class.
160 The frame is always drawn with a single line (without 3D effects). In addition, any mono
161 color set at the control's settings is respected. Yet more additionally, if we're living
162 in a themed desktop, this theming is ignored.
164 Note that this makes sense, since the *only known* clients of Window::Draw
165 are form controls, when printed or print-previewed. For form controls embedded in office documents,
166 you don't want to have the theme look.
168 @param pDev
169 the device to draw onto
170 @param rRect
171 the rect for drawing the frame. Upon returning from the call, the rect will be inflated
172 by the space occupied by the drawn pixels.
174 SAL_DLLPRIVATE void ImplDrawFrame( OutputDevice* pDev, tools::Rectangle& rRect );
176 public:
177 explicit Control( vcl::Window* pParent, WinBits nWinStyle = 0 );
178 virtual ~Control() override;
179 virtual void dispose() override;
181 virtual void EnableRTL ( bool bEnable = true ) override;
183 virtual bool EventNotify( NotifyEvent& rNEvt ) override;
184 virtual void StateChanged( StateChangedType nStateChange ) override;
185 virtual void Resize() override;
187 // invalidates layout data
188 virtual void SetText( const OUString& rStr ) override;
189 // gets the displayed text
190 virtual OUString GetDisplayText() const override;
191 // returns the bounding box for the character at index nIndex (in control coordinates)
192 tools::Rectangle GetCharacterBounds( tools::Long nIndex ) const;
193 // returns the character index for corresponding to rPoint (in control coordinates)
194 // -1 is returned if no character is at that point
195 tools::Long GetIndexForPoint( const Point& rPoint ) const;
196 // returns the interval [start,end] of line nLine
197 // returns [-1,-1] for an invalid line
198 Pair GetLineStartEnd( tools::Long nLine ) const;
199 /** ToRelativeLineIndex changes a layout data index to a count relative to its line.
201 This is equivalent to getting the line start/end pairs with
202 GetLineStartEnd() until the index lies within [start,end] of a line
204 @param nIndex
205 the absolute index inside the display text to be changed to a relative index
207 @returns
208 the relative index inside the displayed line or -1 if the absolute index does
209 not match any line
211 tools::Long ToRelativeLineIndex( tools::Long nIndex ) const;
213 void SetLoseFocusHdl( const Link<Control&,void>& rLink ) { maLoseFocusHdl = rLink; }
215 /** determines whether the control currently has the focus
217 bool HasControlFocus() const { return mbHasControlFocus; }
219 void SetLayoutDataParent( const Control* pParent ) const;
221 virtual Size GetOptimalSize() const override;
223 /** sets a reference device used for rendering control text
224 @see DrawControlText
226 void SetReferenceDevice( OutputDevice* _referenceDevice );
227 OutputDevice* GetReferenceDevice() const;
229 vcl::Font GetUnzoomedControlPointFont() const;
230 void SetShowAccelerator (bool val);
232 void LogicMouseButtonDown(const MouseEvent& rMouseEvent) override;
233 void LogicMouseButtonUp(const MouseEvent& rMouseEvent) override;
234 void LogicMouseMove(const MouseEvent& rMouseEvent) override;
237 #endif // INCLUDED_VCL_CTRL_HXX
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */