Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / vcl / ctrl.hxx
blobc922bcfd18ff34c898ca54d338d91558d2cbdb1d
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 interval [start,end] of line nLine
60 // returns [-1,-1] for an invalid line
61 ::Pair GetLineStartEnd( tools::Long nLine ) const;
62 /** ToRelativeLineIndex changes a layout data index to a count relative to its line.
64 This is equivalent to getting the line start/end pairs with
65 GetLineStartEnd until the index lies within [start,end] of a line
67 @param nIndex
68 the absolute index inside the display text to be changed to a relative index
70 @returns
71 the relative index inside the displayed line or -1 if the absolute index does
72 not match any line
74 tools::Long ToRelativeLineIndex( tools::Long nIndex ) const;
77 } // namespace vcl
79 class VCL_DLLPUBLIC Control : public vcl::Window
81 protected:
82 mutable std::optional<vcl::ControlLayoutData> mxLayoutData;
83 VclPtr<OutputDevice> mpReferenceDevice;
85 private:
86 bool mbHasControlFocus;
87 bool mbShowAccelerator;
88 Link<Control&,void> maLoseFocusHdl;
90 SAL_DLLPRIVATE void ImplInitControlData();
92 Control (const Control &) = delete;
93 Control & operator= (const Control &) = delete;
95 protected:
96 Control( WindowType nType );
97 virtual void FillLayoutData() const;
99 // helper method for composite controls
100 void AppendLayoutData( const Control& rSubControl ) const;
102 /// creates the mpData->mpLayoutData structure
103 void CreateLayoutData() const;
104 /// determines whether we currently have layout data
105 bool HasLayoutData() const;
107 /** this calls both our event listeners, and a specified handler
109 If the Control instance is destroyed during any of those calls, the
110 method properly handles this (in particular, it doesn't crash :)
112 @param nEvent
113 the event to notify to our event listeners
114 @param callHandler
115 the lambda function that calls the handler
116 @return
117 if the Control instance has been destroyed in any of the call
119 bool ImplCallEventListenersAndHandler(
120 VclEventId nEvent, std::function<void()> const & callHandler
123 void CallEventListeners( VclEventId nEvent, void* pData = nullptr );
125 /** draws the given text onto the given device
127 If no reference device is set, the draw request will simply be forwarded to OutputDevice::DrawText. Otherwise,
128 the text will be rendered according to the metrics at the reference device.
130 return will contain the result of a GetTextRect call (either directly
131 at the target device, or taking the reference device into account) when
132 returning.
134 tools::Rectangle DrawControlText( OutputDevice& _rTargetDevice, const tools::Rectangle& _rRect,
135 const OUString& _rStr, DrawTextFlags _nStyle,
136 std::vector< tools::Rectangle >* _pVector, OUString* _pDisplayText,
137 const Size* i_pDeviceSize = nullptr ) const;
139 tools::Rectangle GetControlTextRect( OutputDevice& _rTargetDevice, const tools::Rectangle & rRect,
140 const OUString& _rStr, DrawTextFlags _nStyle,
141 Size* o_pDeviceSize = nullptr ) const;
143 virtual const vcl::Font&
144 GetCanonicalFont( const StyleSettings& _rStyle ) const;
145 virtual const Color&
146 GetCanonicalTextColor( const StyleSettings& _rStyle ) const;
148 void ImplInitSettings();
150 virtual void ApplySettings(vcl::RenderContext& rRenderContext) override;
152 virtual bool FocusWindowBelongsToControl(const vcl::Window* pFocusWin) const;
153 public:
154 SAL_DLLPRIVATE void ImplClearLayoutData() const;
155 /** draws a frame around the give rectangle, onto the given device
157 only to be used from within the Window::Draw method of your sub class.
159 The frame is always drawn with a single line (without 3D effects). In addition, any mono
160 color set at the control's settings is respected. Yet more additionally, if we're living
161 in a themed desktop, this theming is ignored.
163 Note that this makes sense, since the *only known* clients of Window::Draw
164 are form controls, when printed or print-previewed. For form controls embedded in office documents,
165 you don't want to have the theme look.
167 @param pDev
168 the device to draw onto
169 @param rRect
170 the rect for drawing the frame. Upon returning from the call, the rect will be inflated
171 by the space occupied by the drawn pixels.
173 SAL_DLLPRIVATE void ImplDrawFrame( OutputDevice* pDev, tools::Rectangle& rRect );
175 public:
176 explicit Control( vcl::Window* pParent, WinBits nWinStyle = 0 );
177 virtual ~Control() override;
178 virtual void dispose() override;
180 virtual void EnableRTL ( bool bEnable = true ) override;
182 virtual bool EventNotify( NotifyEvent& rNEvt ) override;
183 virtual void StateChanged( StateChangedType nStateChange ) override;
184 virtual void Resize() override;
186 // invalidates layout data
187 virtual void SetText( const OUString& rStr ) override;
188 // gets the displayed text
189 virtual OUString GetDisplayText() const override;
190 // returns the bounding box for the character at index nIndex (in control coordinates)
191 tools::Rectangle GetCharacterBounds( tools::Long nIndex ) const;
192 // returns the character index for corresponding to rPoint (in control coordinates)
193 // -1 is returned if no character is at that point
194 tools::Long GetIndexForPoint( const Point& rPoint ) const;
195 // returns the interval [start,end] of line nLine
196 // returns [-1,-1] for an invalid line
197 Pair GetLineStartEnd( tools::Long nLine ) const;
198 /** ToRelativeLineIndex changes a layout data index to a count relative to its line.
200 This is equivalent to getting the line start/end pairs with
201 GetLineStartEnd() until the index lies within [start,end] of a line
203 @param nIndex
204 the absolute index inside the display text to be changed to a relative index
206 @returns
207 the relative index inside the displayed line or -1 if the absolute index does
208 not match any line
210 tools::Long ToRelativeLineIndex( tools::Long nIndex ) const;
212 void SetLoseFocusHdl( const Link<Control&,void>& rLink ) { maLoseFocusHdl = rLink; }
214 /** determines whether the control currently has the focus
216 bool HasControlFocus() const { return mbHasControlFocus; }
218 void SetLayoutDataParent( const Control* pParent ) const;
220 virtual Size GetOptimalSize() const override;
222 /** sets a reference device used for rendering control text
223 @see DrawControlText
225 void SetReferenceDevice( OutputDevice* _referenceDevice );
226 OutputDevice* GetReferenceDevice() const;
228 vcl::Font GetUnzoomedControlPointFont() const;
229 void SetShowAccelerator (bool val);
231 /// Notify the LOK client about an invalidated area.
232 virtual void LogicInvalidate( const tools::Rectangle* pRectangle ) override;
235 #endif // INCLUDED_VCL_CTRL_HXX
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */