Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 192, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name srcedtw.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -isystem /usr/include/libxml2 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D SW_DLLIMPLEMENTATION -D SWUI_DLL_NAME="libswuilo.so" -D SYSTEM_LIBXML -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/sw/source/core/inc -I /home/maarten/src/libreoffice/core/sw/source/filter/inc -I /home/maarten/src/libreoffice/core/sw/source/uibase/inc -I /home/maarten/src/libreoffice/core/sw/inc -I /home/maarten/src/libreoffice/core/workdir/SdiTarget/sw/sdi -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/sw/generated -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/oovbaapi/normal -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx

/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx

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#include <sal/config.h>
21
22#include <hintids.hxx>
23#include <cmdid.h>
24
25#include <com/sun/star/beans/XMultiPropertySet.hpp>
26#include <com/sun/star/beans/XPropertiesChangeListener.hpp>
27#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28#include <cppuhelper/implbase.hxx>
29#include <officecfg/Office/Common.hxx>
30#include <rtl/ustring.hxx>
31#include <sal/log.hxx>
32#include <vcl/commandevent.hxx>
33#include <vcl/event.hxx>
34#include <vcl/svapp.hxx>
35#include <vcl/textview.hxx>
36#include <vcl/scrbar.hxx>
37#include <vcl/ptrstyle.hxx>
38#include <sfx2/dispatch.hxx>
39#include <sfx2/viewfrm.hxx>
40#include <svtools/htmltokn.h>
41#include <vcl/txtattr.hxx>
42#include <vcl/settings.hxx>
43#include <svtools/colorcfg.hxx>
44#include <editeng/flstitem.hxx>
45#include <vcl/metric.hxx>
46#include <svtools/ctrltool.hxx>
47#include <tools/time.hxx>
48#include <swmodule.hxx>
49#include <docsh.hxx>
50#include <srcview.hxx>
51#include <helpids.h>
52#include <vector>
53
54namespace
55{
56
57struct TextPortion
58{
59 sal_uInt16 nStart, nEnd;
60 svtools::ColorConfigEntry eType;
61};
62
63}
64
65#define MAX_SYNTAX_HIGHLIGHT20 20
66#define MAX_HIGHLIGHTTIME200 200
67
68typedef std::vector<TextPortion> TextPortions;
69
70static void lcl_Highlight(const OUString& rSource, TextPortions& aPortionList)
71{
72 const sal_Unicode cOpenBracket = '<';
73 const sal_Unicode cCloseBracket= '>';
74 const sal_Unicode cSlash = '/';
75 const sal_Unicode cExclamation = '!';
76 const sal_Unicode cMinus = '-';
77 const sal_Unicode cSpace = ' ';
78 const sal_Unicode cTab = 0x09;
79 const sal_Unicode cLF = 0x0a;
80 const sal_Unicode cCR = 0x0d;
81
82 const sal_uInt16 nStrLen = rSource.getLength();
83 sal_uInt16 nInsert = 0; // number of inserted portions
84 sal_uInt16 nActPos = 0; // position, where '<' was found
85 sal_uInt16 nPortStart = USHRT_MAX(32767 *2 +1); // for the TextPortion
86 sal_uInt16 nPortEnd = 0;
87 TextPortion aText;
88 while(nActPos < nStrLen)
89 {
90 if((nActPos < nStrLen - 2) && (rSource[nActPos] == cOpenBracket))
91 {
92 svtools::ColorConfigEntry eFoundType = svtools::HTMLUNKNOWN;
93 // insert 'empty' portion
94 if(nPortEnd < nActPos - 1 )
95 {
96 // don't move at the beginning
97 aText.nStart = nPortEnd;
98 if(nInsert)
99 aText.nStart += 1;
100 aText.nEnd = nActPos - 1;
101 aText.eType = svtools::HTMLUNKNOWN;
102 aPortionList.push_back( aText );
103 nInsert++;
104 }
105 sal_Unicode cFollowFirst = rSource[nActPos + 1];
106 sal_Unicode cFollowNext = rSource[nActPos + 2];
107 if(cExclamation == cFollowFirst)
108 {
109 // "<!" SGML or comment
110 if(cMinus == cFollowNext &&
111 nActPos < nStrLen - 3 && cMinus == rSource[nActPos + 3])
112 {
113 eFoundType = svtools::HTMLCOMMENT;
114 }
115 else
116 eFoundType = svtools::HTMLSGML;
117 nPortStart = nActPos;
118 nPortEnd = nActPos + 1;
119 }
120 else if(cSlash == cFollowFirst)
121 {
122 // "</" ignore slash
123 nPortStart = nActPos;
124 nActPos++;
125 }
126 if(svtools::HTMLUNKNOWN == eFoundType)
127 {
128 // now here a keyword could follow
129 sal_uInt16 nSrchPos = nActPos;
130 while(++nSrchPos < nStrLen - 1)
131 {
132 sal_Unicode cNext = rSource[nSrchPos];
133 if( cNext == cSpace ||
134 cNext == cTab ||
135 cNext == cLF ||
136 cNext == cCR)
137 break;
138 else if(cNext == cCloseBracket)
139 {
140 break;
141 }
142 }
143 if(nSrchPos > nActPos + 1)
144 {
145 // some string was found
146 OUString sToken = rSource.copy(nActPos + 1, nSrchPos - nActPos - 1 );
147 sToken = sToken.toAsciiUpperCase();
148 HtmlTokenId nToken = ::GetHTMLToken(sToken);
149 if(nToken != HtmlTokenId::NONE)
150 {
151 // Token was found
152 eFoundType = svtools::HTMLKEYWORD;
153 nPortEnd = nSrchPos;
154 nPortStart = nActPos;
155 }
156 else
157 {
158 // what was that?
159 SAL_WARN(do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sw.level2")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Token " << sToken
<< " not recognised!") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.level2"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Token " << sToken << " not recognised!"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
160 "sw.level2",do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sw.level2")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Token " << sToken
<< " not recognised!") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.level2"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Token " << sToken << " not recognised!"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
161 "Token " << sTokendo { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sw.level2")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Token " << sToken
<< " not recognised!") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.level2"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Token " << sToken << " not recognised!"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
162 << " not recognised!")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sw.level2")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Token " << sToken
<< " not recognised!") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.level2"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Token " << sToken << " not recognised!"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Token " << sToken << " not recognised!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Token " << sToken << " not recognised!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.level2"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "162" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
163 }
164
165 }
166 }
167 // now we still have to look for '>'
168 if(svtools::HTMLUNKNOWN != eFoundType)
169 {
170 bool bFound = false;
171 for(sal_uInt16 i = nPortEnd; i < nStrLen; i++)
172 if(cCloseBracket == rSource[i])
173 {
174 bFound = true;
175 nPortEnd = i;
176 break;
177 }
178 if(!bFound && (eFoundType == svtools::HTMLCOMMENT))
179 {
180 // comment without ending in this line
181 bFound = true;
182 nPortEnd = nStrLen - 1;
183 }
184
185 if(bFound ||(eFoundType == svtools::HTMLCOMMENT))
186 {
187 TextPortion aTextPortion;
188 aTextPortion.nStart = nPortStart + 1;
189 aTextPortion.nEnd = nPortEnd;
190 aTextPortion.eType = eFoundType;
191 aPortionList.push_back( aTextPortion );
192 nInsert++;
193 }
194
195 }
196 }
197 nActPos++;
198 }
199 if(nInsert && nPortEnd < nActPos - 1)
200 {
201 aText.nStart = nPortEnd + 1;
202 aText.nEnd = nActPos - 1;
203 aText.eType = svtools::HTMLUNKNOWN;
204 aPortionList.push_back( aText );
205 nInsert++;
206 }
207}
208
209class SwSrcEditWindow::ChangesListener:
210 public cppu::WeakImplHelper< css::beans::XPropertiesChangeListener >
211{
212public:
213 explicit ChangesListener(SwSrcEditWindow & editor): editor_(editor) {}
214
215private:
216 virtual ~ChangesListener() override {}
217
218 virtual void SAL_CALL disposing(css::lang::EventObject const &) override
219 {
220 osl::MutexGuard g(editor_.mutex_);
221 editor_.m_xNotifier.clear();
222 }
223
224 virtual void SAL_CALL propertiesChange(
225 css::uno::Sequence< css::beans::PropertyChangeEvent > const &) override
226 {
227 SolarMutexGuard g;
228 editor_.SetFont();
229 }
230
231 SwSrcEditWindow & editor_;
232};
233
234SwSrcEditWindow::SwSrcEditWindow( vcl::Window* pParent, SwSrcView* pParentView ) :
235 Window( pParent, WB_BORDER|WB_CLIPCHILDREN ),
236
237 m_pOutWin(nullptr),
238 m_pHScrollbar(nullptr),
239 m_pVScrollbar(nullptr),
240
241 m_pSrcView(pParentView),
242
243 m_nCurTextWidth(0),
244 m_nStartLine(USHRT_MAX(32767 *2 +1)),
245 m_eSourceEncoding(osl_getThreadTextEncoding()),
246 m_bReadonly(false),
247 m_bHighlighting(false),
248 m_aSyntaxIdle("sw uibase SwSrcEditWindow Syntax")
249{
250 SetHelpId(HID_SOURCE_EDITWIN"SW_HID_SOURCE_EDITWIN");
251 CreateTextEngine();
252
253 // Using "this" in ctor is a little fishy, but should work here at least as
254 // long as there are no derivations:
255 m_xListener = new ChangesListener(*this);
256 css::uno::Reference< css::beans::XMultiPropertySet > n(
257 officecfg::Office::Common::Font::SourceViewFont::get(),
258 css::uno::UNO_QUERY_THROW);
259 {
260 osl::MutexGuard g(mutex_);
261 m_xNotifier = n;
262 }
263 css::uno::Sequence< OUString > s(2);
264 s[0] = "FontHeight";
265 s[1] = "FontName";
266 n->addPropertiesChangeListener(s, m_xListener.get());
267}
268
269SwSrcEditWindow::~SwSrcEditWindow()
270{
271 disposeOnce();
272}
273
274void SwSrcEditWindow::dispose()
275{
276 css::uno::Reference< css::beans::XMultiPropertySet > n;
277 {
278 osl::MutexGuard g(mutex_);
279 n = m_xNotifier;
280 }
281 if (n.is()) {
1
Taking false branch
282 n->removePropertiesChangeListener(m_xListener.get());
283 }
284 m_aSyntaxIdle.Stop();
285 if ( m_pOutWin )
2
Taking true branch
286 m_pOutWin->SetTextView( nullptr );
287
288 if ( m_pTextEngine )
3
Taking false branch
289 {
290 EndListening( *m_pTextEngine );
291 m_pTextEngine->RemoveView( m_pTextView.get() );
292
293 m_pTextView.reset();
294 m_pTextEngine.reset();
295 }
296 m_pHScrollbar.disposeAndClear();
297 m_pVScrollbar.disposeAndClear();
298 m_pOutWin.disposeAndClear();
4
Calling 'VclPtr::disposeAndClear'
299 vcl::Window::dispose();
300}
301
302void SwSrcEditWindow::DataChanged( const DataChangedEvent& rDCEvt )
303{
304 Window::DataChanged( rDCEvt );
305
306 switch ( rDCEvt.GetType() )
307 {
308 case DataChangedEventType::SETTINGS:
309 // newly rearrange ScrollBars or trigger Resize, because
310 // ScrollBar size could have changed. For this, in the
311 // Resize handler the size of ScrollBars has to be queried
312 // from the settings as well.
313 if( rDCEvt.GetFlags() & AllSettingsFlags::STYLE )
314 Resize();
315 break;
316 default: break;
317 }
318}
319
320void SwSrcEditWindow::Resize()
321{
322 // ScrollBars, etc. happens in Adjust...
323 if ( !m_pTextView )
324 return;
325
326 long nVisY = m_pTextView->GetStartDocPos().Y();
327 m_pTextView->ShowCursor();
328 Size aOutSz( GetOutputSizePixel() );
329 long nMaxVisAreaStart = m_pTextView->GetTextEngine()->GetTextHeight() - aOutSz.Height();
330 if ( nMaxVisAreaStart < 0 )
331 nMaxVisAreaStart = 0;
332 if ( m_pTextView->GetStartDocPos().Y() > nMaxVisAreaStart )
333 {
334 Point aStartDocPos( m_pTextView->GetStartDocPos() );
335 aStartDocPos.setY( nMaxVisAreaStart );
336 m_pTextView->SetStartDocPos( aStartDocPos );
337 m_pTextView->ShowCursor();
338 }
339 long nScrollStd = GetSettings().GetStyleSettings().GetScrollBarSize();
340 Size aScrollSz(aOutSz.Width() - nScrollStd, nScrollStd );
341 Point aScrollPos(0, aOutSz.Height() - nScrollStd);
342
343 m_pHScrollbar->SetPosSizePixel( aScrollPos, aScrollSz);
344
345 aScrollSz.setWidth( aScrollSz.Height() );
346 aScrollSz.setHeight( aOutSz.Height() );
347 aScrollPos = Point(aOutSz.Width() - nScrollStd, 0);
348
349 m_pVScrollbar->SetPosSizePixel( aScrollPos, aScrollSz);
350 aOutSz.AdjustWidth( -nScrollStd );
351 aOutSz.AdjustHeight( -nScrollStd );
352 m_pOutWin->SetOutputSizePixel(aOutSz);
353 InitScrollBars();
354
355 // set line in first Resize
356 if(USHRT_MAX(32767 *2 +1) != m_nStartLine)
357 {
358 if(m_nStartLine < m_pTextEngine->GetParagraphCount())
359 {
360 TextSelection aSel(TextPaM( m_nStartLine, 0 ), TextPaM( m_nStartLine, 0x0 ));
361 m_pTextView->SetSelection(aSel);
362 m_pTextView->ShowCursor();
363 }
364 m_nStartLine = USHRT_MAX(32767 *2 +1);
365 }
366
367 if ( nVisY != m_pTextView->GetStartDocPos().Y() )
368 Invalidate();
369
370
371}
372
373void TextViewOutWin::DataChanged( const DataChangedEvent& rDCEvt )
374{
375 Window::DataChanged( rDCEvt );
376
377 switch( rDCEvt.GetType() )
378 {
379 case DataChangedEventType::SETTINGS:
380 // query settings
381 if( rDCEvt.GetFlags() & AllSettingsFlags::STYLE )
382 {
383 const Color &rCol = GetSettings().GetStyleSettings().GetWindowColor();
384 SetBackground( rCol );
385 vcl::Font aFont( pTextView->GetTextEngine()->GetFont() );
386 aFont.SetFillColor( rCol );
387 pTextView->GetTextEngine()->SetFont( aFont );
388 }
389 break;
390 default: break;
391 }
392}
393
394void TextViewOutWin::MouseMove( const MouseEvent &rEvt )
395{
396 if ( pTextView )
397 pTextView->MouseMove( rEvt );
398}
399
400void TextViewOutWin::MouseButtonUp( const MouseEvent &rEvt )
401{
402 if ( pTextView )
403 {
404 pTextView->MouseButtonUp( rEvt );
405 SfxViewFrame *pFrame = static_cast<SwSrcEditWindow*>(GetParent())->GetSrcView()->GetViewFrame();
406 if ( pFrame )
407 {
408 SfxBindings& rBindings = pFrame->GetBindings();
409 rBindings.Invalidate( SID_TABLE_CELL( 10000 + 225 ) );
410 rBindings.Invalidate( SID_CUT(5000 + 710) );
411 rBindings.Invalidate( SID_COPY(5000 + 711) );
412 }
413 }
414}
415
416void TextViewOutWin::MouseButtonDown( const MouseEvent &rEvt )
417{
418 GrabFocus();
419 if ( pTextView )
420 pTextView->MouseButtonDown( rEvt );
421}
422
423void TextViewOutWin::Command( const CommandEvent& rCEvt )
424{
425 switch(rCEvt.GetCommand())
426 {
427 case CommandEventId::ContextMenu:
428 SfxDispatcher::ExecutePopup();
429 break;
430 case CommandEventId::Wheel:
431 case CommandEventId::StartAutoScroll:
432 case CommandEventId::AutoScroll:
433 {
434 const CommandWheelData* pWData = rCEvt.GetWheelData();
435 if( !pWData || CommandWheelMode::ZOOM != pWData->GetMode() )
436 {
437 static_cast<SwSrcEditWindow*>(GetParent())->HandleWheelCommand( rCEvt );
438 }
439 }
440 break;
441
442 default:
443 if ( pTextView )
444 pTextView->Command( rCEvt );
445 else
446 Window::Command(rCEvt);
447 }
448}
449
450void TextViewOutWin::KeyInput( const KeyEvent& rKEvt )
451{
452 bool bDone = false;
453 SwSrcEditWindow* pSrcEditWin = static_cast<SwSrcEditWindow*>(GetParent());
454 bool bChange = !pSrcEditWin->IsReadonly() || !TextEngine::DoesKeyChangeText( rKEvt );
455 if(bChange)
456 bDone = pTextView->KeyInput( rKEvt );
457
458 SfxBindings& rBindings = static_cast<SwSrcEditWindow*>(GetParent())->GetSrcView()->GetViewFrame()->GetBindings();
459 if ( !bDone )
460 {
461 if ( !SfxViewShell::Current()->KeyInput( rKEvt ) )
462 Window::KeyInput( rKEvt );
463 }
464 else
465 {
466 rBindings.Invalidate( SID_TABLE_CELL( 10000 + 225 ) );
467 if ( rKEvt.GetKeyCode().GetGroup() == KEYGROUP_CURSOR )
468 rBindings.Update( SID_BASICIDE_STAT_POS( (30000 + 768) + 38 ) );
469 if (pSrcEditWin->GetTextEngine()->IsModified() )
470 {
471 rBindings.Invalidate( SID_SAVEDOC(5000 + 505) );
472 rBindings.Invalidate( SID_DOC_MODIFIED(5000 + 584) );
473 }
474 if( rKEvt.GetKeyCode().GetCode() == KEY_INSERT )
475 rBindings.Invalidate( SID_ATTR_INSERTTypedWhichId<SfxBoolItem>( 10000 + 221 ) );
476 }
477
478 rBindings.Invalidate( SID_CUT(5000 + 710) );
479 rBindings.Invalidate( SID_COPY(5000 + 711) );
480
481 SwDocShell* pDocShell = pSrcEditWin->GetSrcView()->GetDocShell();
482 if(pSrcEditWin->GetTextEngine()->IsModified())
483 {
484 pDocShell->SetModified();
485 }
486}
487
488void TextViewOutWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
489{
490 pTextView->Paint(rRenderContext, rRect);
491}
492
493void SwSrcEditWindow::CreateTextEngine()
494{
495 // FIXME RenderContext
496
497 const Color &rCol = GetSettings().GetStyleSettings().GetWindowColor();
498 m_pOutWin = VclPtr<TextViewOutWin>::Create(this, 0);
499 m_pOutWin->SetBackground(Wallpaper(rCol));
500 m_pOutWin->SetPointer(PointerStyle::Text);
501 m_pOutWin->Show();
502
503 // create Scrollbars
504 m_pHScrollbar = VclPtr<ScrollBar>::Create(this, WB_3DLOOK |WB_HSCROLL|WB_DRAG);
505 m_pHScrollbar->EnableRTL( false );
506 m_pHScrollbar->SetScrollHdl(LINK(this, SwSrcEditWindow, ScrollHdl)::tools::detail::makeLink( ::tools::detail::castTo<SwSrcEditWindow
*>(this), &SwSrcEditWindow::LinkStubScrollHdl)
);
507 m_pHScrollbar->Show();
508
509 m_pVScrollbar = VclPtr<ScrollBar>::Create(this, WB_3DLOOK |WB_VSCROLL|WB_DRAG);
510 m_pVScrollbar->EnableRTL( false );
511 m_pVScrollbar->SetScrollHdl(LINK(this, SwSrcEditWindow, ScrollHdl)::tools::detail::makeLink( ::tools::detail::castTo<SwSrcEditWindow
*>(this), &SwSrcEditWindow::LinkStubScrollHdl)
);
512 m_pHScrollbar->EnableDrag();
513 m_pVScrollbar->Show();
514
515 m_pTextEngine.reset(new ExtTextEngine);
516 m_pTextView.reset(new TextView( m_pTextEngine.get(), m_pOutWin ));
517 m_pTextView->SetAutoIndentMode(true);
518 m_pOutWin->SetTextView(m_pTextView.get());
519
520 m_pTextEngine->SetUpdateMode( false );
521 m_pTextEngine->InsertView( m_pTextView.get() );
522
523 vcl::Font aFont;
524 aFont.SetTransparent( false );
525 aFont.SetFillColor( rCol );
526 SetPointFont(*this, aFont);
527 aFont = GetFont();
528 aFont.SetFillColor( rCol );
529 m_pOutWin->SetFont( aFont );
530 m_pTextEngine->SetFont( aFont );
531
532 m_aSyntaxIdle.SetInvokeHandler( LINK( this, SwSrcEditWindow, SyntaxTimerHdl )::tools::detail::makeLink( ::tools::detail::castTo<SwSrcEditWindow
*>(this), &SwSrcEditWindow::LinkStubSyntaxTimerHdl)
);
533
534 m_pTextEngine->EnableUndo( true );
535 m_pTextEngine->SetUpdateMode( true );
536
537 m_pTextView->ShowCursor();
538 InitScrollBars();
539 StartListening( *m_pTextEngine );
540
541 SfxBindings& rBind = GetSrcView()->GetViewFrame()->GetBindings();
542 rBind.Invalidate( SID_TABLE_CELL( 10000 + 225 ) );
543}
544
545void SwSrcEditWindow::SetScrollBarRanges()
546{
547 // Extra method, not InitScrollBars, because also for TextEngine events.
548
549 m_pHScrollbar->SetRange( Range( 0, m_nCurTextWidth-1 ) );
550 m_pVScrollbar->SetRange( Range(0, m_pTextEngine->GetTextHeight()-1) );
551}
552
553void SwSrcEditWindow::InitScrollBars()
554{
555 SetScrollBarRanges();
556
557 Size aOutSz( m_pOutWin->GetOutputSizePixel() );
558 m_pVScrollbar->SetVisibleSize( aOutSz.Height() );
559 m_pVScrollbar->SetPageSize( aOutSz.Height() * 8 / 10 );
560 m_pVScrollbar->SetLineSize( m_pOutWin->GetTextHeight() );
561 m_pVScrollbar->SetThumbPos( m_pTextView->GetStartDocPos().Y() );
562 m_pHScrollbar->SetVisibleSize( aOutSz.Width() );
563 m_pHScrollbar->SetPageSize( aOutSz.Width() * 8 / 10 );
564 m_pHScrollbar->SetLineSize( m_pOutWin->GetTextWidth(OUString('x')) );
565 m_pHScrollbar->SetThumbPos( m_pTextView->GetStartDocPos().X() );
566
567}
568
569IMPL_LINK(SwSrcEditWindow, ScrollHdl, ScrollBar*, pScroll, void)void SwSrcEditWindow::LinkStubScrollHdl(void * instance, ScrollBar
* data) { return static_cast<SwSrcEditWindow *>(instance
)->ScrollHdl(data); } void SwSrcEditWindow::ScrollHdl(ScrollBar
* pScroll)
570{
571 if(pScroll == m_pVScrollbar)
572 {
573 long nDiff = m_pTextView->GetStartDocPos().Y() - pScroll->GetThumbPos();
574 GetTextView()->Scroll( 0, nDiff );
575 m_pTextView->ShowCursor( false );
576 pScroll->SetThumbPos( m_pTextView->GetStartDocPos().Y() );
577 }
578 else
579 {
580 long nDiff = m_pTextView->GetStartDocPos().X() - pScroll->GetThumbPos();
581 GetTextView()->Scroll( nDiff, 0 );
582 m_pTextView->ShowCursor( false );
583 pScroll->SetThumbPos( m_pTextView->GetStartDocPos().X() );
584 }
585 GetSrcView()->GetViewFrame()->GetBindings().Invalidate( SID_TABLE_CELL( 10000 + 225 ) );
586}
587
588IMPL_LINK( SwSrcEditWindow, SyntaxTimerHdl, Timer*, pIdle, void )void SwSrcEditWindow::LinkStubSyntaxTimerHdl(void * instance,
Timer* data) { return static_cast<SwSrcEditWindow *>(instance
)->SyntaxTimerHdl(data); } void SwSrcEditWindow::SyntaxTimerHdl
(Timer* pIdle)
589{
590 tools::Time aSyntaxCheckStart( tools::Time::SYSTEM );
591 SAL_WARN_IF(m_pTextView == nullptr, "sw", "No View yet, but syntax highlighting?!")do { if (true && (m_pTextView == nullptr)) { switch (
sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN, "sw")) { case
SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "No View yet, but syntax highlighting?!") == 1) {
::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "591" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "No View yet, but syntax highlighting?!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "No View yet, but syntax highlighting?!"; ::sal::detail
::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "591" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "No View yet, but syntax highlighting?!") == 1) {
::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "591" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "No View yet, but syntax highlighting?!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "No View yet, but syntax highlighting?!"; ::sal::detail
::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/docvw/srcedtw.cxx"
":" "591" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
592
593 m_bHighlighting = true;
594 sal_uInt16 nCount = 0;
595 // at first the region around the cursor is processed
596 TextSelection aSel = m_pTextView->GetSelection();
597 sal_uInt16 nCur = static_cast<sal_uInt16>(aSel.GetStart().GetPara());
598 if(nCur > 40)
599 nCur -= 40;
600 else
601 nCur = 0;
602 if(!m_aSyntaxLineTable.empty())
603 for(sal_uInt16 i = 0; i < 80 && nCount < 40; i++, nCur++)
604 {
605 if(m_aSyntaxLineTable.find(nCur) != m_aSyntaxLineTable.end())
606 {
607 DoSyntaxHighlight( nCur );
608 m_aSyntaxLineTable.erase( nCur );
609 nCount++;
610 if(m_aSyntaxLineTable.empty())
611 break;
612 if((tools::Time( tools::Time::SYSTEM ).GetTime() - aSyntaxCheckStart.GetTime()) > MAX_HIGHLIGHTTIME200 )
613 {
614 break;
615 }
616 }
617 }
618
619 // when there is still anything left by then, go on from the beginning
620 while ( !m_aSyntaxLineTable.empty() && nCount < MAX_SYNTAX_HIGHLIGHT20)
621 {
622 sal_uInt16 nLine = *m_aSyntaxLineTable.begin();
623 DoSyntaxHighlight( nLine );
624 m_aSyntaxLineTable.erase(nLine);
625 nCount ++;
626 if(tools::Time( tools::Time::SYSTEM ).GetTime() - aSyntaxCheckStart.GetTime() > MAX_HIGHLIGHTTIME200)
627 {
628 break;
629 }
630 }
631
632 if(!m_aSyntaxLineTable.empty() && !pIdle->IsActive())
633 pIdle->Start();
634 // SyntaxTimerHdl is called when text changed
635 // => good opportunity to determine text width!
636 long nPrevTextWidth = m_nCurTextWidth;
637 m_nCurTextWidth = m_pTextEngine->CalcTextWidth() + 25; // small tolerance
638 if ( m_nCurTextWidth != nPrevTextWidth )
639 SetScrollBarRanges();
640 m_bHighlighting = false;
641}
642
643void SwSrcEditWindow::DoSyntaxHighlight( sal_uInt16 nPara )
644{
645 // Because of DelayedSyntaxHighlight it could happen,
646 // that the line doesn't exist anymore!
647 if ( nPara >= m_pTextEngine->GetParagraphCount() )
648 return;
649
650 bool bTempModified = IsModified();
651 m_pTextEngine->RemoveAttribs( nPara );
652 OUString aSource( m_pTextEngine->GetText( nPara ) );
653 m_pTextEngine->SetUpdateMode( false );
654 ImpDoHighlight( aSource, nPara );
655 TextView* pTmp = m_pTextEngine->GetActiveView();
656 pTmp->SetAutoScroll(false);
657 m_pTextEngine->SetActiveView(nullptr);
658 m_pTextEngine->SetUpdateMode( true );
659 m_pTextEngine->SetActiveView(pTmp);
660 pTmp->SetAutoScroll(true);
661 pTmp->ShowCursor( false/*pTmp->IsAutoScroll()*/ );
662
663 if(!bTempModified)
664 ClearModifyFlag();
665
666}
667
668void SwSrcEditWindow::ImpDoHighlight( const OUString& rSource, sal_uInt16 nLineOff )
669{
670 TextPortions aPortionList;
671 lcl_Highlight(rSource, aPortionList);
672
673 size_t nCount = aPortionList.size();
674 if ( !nCount )
675 return;
676
677 TextPortion& rLast = aPortionList[nCount-1];
678 if ( rLast.nStart > rLast.nEnd ) // Only until Bug from MD is resolved
679 {
680 nCount--;
681 aPortionList.pop_back();
682 if ( !nCount )
683 return;
684 }
685
686 {
687 // Only blanks and tabs have to be attributed along.
688 // When two identical attributes are placed consecutively,
689 // it optimises the TextEngine.
690 sal_uInt16 nLastEnd = 0;
691
692 for ( size_t i = 0; i < nCount; i++ )
693 {
694 TextPortion& r = aPortionList[i];
695 if ( r.nStart > r.nEnd ) // only until Bug from MD is resolved
696 continue;
697
698 if ( r.nStart > nLastEnd )
699 {
700 // Can I rely on the fact that all except blank and tab
701 // are being highlighted?!
702 r.nStart = nLastEnd;
703 }
704 nLastEnd = r.nEnd+1;
705 if ( ( i == (nCount-1) ) && ( r.nEnd < rSource.getLength() ) )
706 r.nEnd = rSource.getLength();
707 }
708 }
709
710 for (TextPortion & r : aPortionList)
711 {
712 if ( r.nStart > r.nEnd ) // only until Bug from MD is resolved
713 continue;
714 if(r.eType != svtools::HTMLSGML &&
715 r.eType != svtools::HTMLCOMMENT &&
716 r.eType != svtools::HTMLKEYWORD &&
717 r.eType != svtools::HTMLUNKNOWN)
718 r.eType = svtools::HTMLUNKNOWN;
719 Color aColor(SW_MOD()( static_cast<SwModule*>(SfxApplication::GetModule(SfxToolsModule
::Writer)))
->GetColorConfig().GetColorValue(r.eType).nColor);
720 m_pTextEngine->SetAttrib( TextAttribFontColor( aColor ), nLineOff, r.nStart, r.nEnd+1 );
721 }
722}
723
724void SwSrcEditWindow::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
725{
726 const TextHint* pTextHint = dynamic_cast<const TextHint*>(&rHint);
727 if (!pTextHint)
728 return;
729
730 switch (pTextHint->GetId())
731 {
732 case SfxHintId::TextViewScrolled:
733 m_pHScrollbar->SetThumbPos( m_pTextView->GetStartDocPos().X() );
734 m_pVScrollbar->SetThumbPos( m_pTextView->GetStartDocPos().Y() );
735 break;
736
737 case SfxHintId::TextHeightChanged:
738 if ( m_pTextEngine->GetTextHeight() < m_pOutWin->GetOutputSizePixel().Height() )
739 m_pTextView->Scroll( 0, m_pTextView->GetStartDocPos().Y() );
740 m_pVScrollbar->SetThumbPos( m_pTextView->GetStartDocPos().Y() );
741 SetScrollBarRanges();
742 break;
743
744 case SfxHintId::TextParaInserted:
745 case SfxHintId::TextParaContentChanged:
746 if ( !m_bHighlighting )
747 {
748 m_aSyntaxLineTable.insert( static_cast<sal_uInt16>(pTextHint->GetValue()) );
749 m_aSyntaxIdle.Start();
750 }
751 break;
752 default: break;
753 }
754}
755
756void SwSrcEditWindow::Invalidate(InvalidateFlags )
757{
758 m_pOutWin->Invalidate();
759 Window::Invalidate();
760}
761
762void SwSrcEditWindow::Command( const CommandEvent& rCEvt )
763{
764 switch(rCEvt.GetCommand())
765 {
766 case CommandEventId::Wheel:
767 case CommandEventId::StartAutoScroll:
768 case CommandEventId::AutoScroll:
769 {
770 const CommandWheelData* pWData = rCEvt.GetWheelData();
771 if( !pWData || CommandWheelMode::ZOOM != pWData->GetMode() )
772 HandleScrollCommand( rCEvt, m_pHScrollbar, m_pVScrollbar );
773 }
774 break;
775 default:
776 Window::Command(rCEvt);
777 }
778}
779
780void SwSrcEditWindow::HandleWheelCommand( const CommandEvent& rCEvt )
781{
782 m_pTextView->Command(rCEvt);
783 HandleScrollCommand( rCEvt, m_pHScrollbar, m_pVScrollbar );
784}
785
786void SwSrcEditWindow::GetFocus()
787{
788 if (m_pOutWin)
789 m_pOutWin->GrabFocus();
790}
791
792static bool lcl_GetLanguagesForEncoding(rtl_TextEncoding eEnc, LanguageType aLanguages[])
793{
794 switch(eEnc)
795 {
796 case RTL_TEXTENCODING_UTF7(((rtl_TextEncoding) 75)) :
797 case RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)) :
798 // don#t fill - all LANGUAGE_SYSTEM means unicode font has to be used
799 break;
800
801 case RTL_TEXTENCODING_ISO_8859_3(((rtl_TextEncoding) 14)):
802 case RTL_TEXTENCODING_ISO_8859_1(((rtl_TextEncoding) 12)) :
803 case RTL_TEXTENCODING_MS_1252(((rtl_TextEncoding) 1)) :
804 case RTL_TEXTENCODING_APPLE_ROMAN(((rtl_TextEncoding) 2)) :
805 case RTL_TEXTENCODING_IBM_850(((rtl_TextEncoding) 4)) :
806 case RTL_TEXTENCODING_ISO_8859_14(((rtl_TextEncoding) 21)) :
807 case RTL_TEXTENCODING_ISO_8859_15(((rtl_TextEncoding) 22)) :
808 //fill with western languages
809 aLanguages[0] = LANGUAGE_GERMANLanguageType(0x0407);
810 aLanguages[1] = LANGUAGE_FRENCHLanguageType(0x040C);
811 aLanguages[2] = LANGUAGE_ITALIANLanguageType(0x0410);
812 aLanguages[3] = LANGUAGE_SPANISHLanguageType(0x0C0A);
813 break;
814
815 case RTL_TEXTENCODING_IBM_865(((rtl_TextEncoding) 8)) :
816 //scandinavian
817 aLanguages[0] = LANGUAGE_FINNISHLanguageType(0x040B);
818 aLanguages[1] = LANGUAGE_NORWEGIANLanguageType(0x0014);
819 aLanguages[2] = LANGUAGE_SWEDISHLanguageType(0x041D);
820 aLanguages[3] = LANGUAGE_DANISHLanguageType(0x0406);
821 break;
822
823 case RTL_TEXTENCODING_ISO_8859_10(((rtl_TextEncoding) 77)) :
824 case RTL_TEXTENCODING_ISO_8859_13(((rtl_TextEncoding) 78)) :
825 case RTL_TEXTENCODING_ISO_8859_2(((rtl_TextEncoding) 13)) :
826 case RTL_TEXTENCODING_IBM_852(((rtl_TextEncoding) 25)) :
827 case RTL_TEXTENCODING_MS_1250(((rtl_TextEncoding) 33)) :
828 case RTL_TEXTENCODING_APPLE_CENTEURO(((rtl_TextEncoding) 42)) :
829 aLanguages[0] = LANGUAGE_POLISHLanguageType(0x0415);
830 aLanguages[1] = LANGUAGE_CZECHLanguageType(0x0405);
831 aLanguages[2] = LANGUAGE_HUNGARIANLanguageType(0x040E);
832 aLanguages[3] = LANGUAGE_SLOVAKLanguageType(0x041B);
833 break;
834
835 case RTL_TEXTENCODING_ISO_8859_4(((rtl_TextEncoding) 15)) :
836 case RTL_TEXTENCODING_IBM_775(((rtl_TextEncoding) 24)) :
837 case RTL_TEXTENCODING_MS_1257(((rtl_TextEncoding) 39)) :
838 aLanguages[0] = LANGUAGE_LATVIANLanguageType(0x0426) ;
839 aLanguages[1] = LANGUAGE_LITHUANIANLanguageType(0x0427);
840 aLanguages[2] = LANGUAGE_ESTONIANLanguageType(0x0425) ;
841 break;
842
843 case RTL_TEXTENCODING_IBM_863(((rtl_TextEncoding) 7)) : aLanguages[0] = LANGUAGE_FRENCH_CANADIANLanguageType(0x0C0C); break;
844 case RTL_TEXTENCODING_APPLE_FARSI(((rtl_TextEncoding) 46)) : aLanguages[0] = LANGUAGE_FARSILanguageType(0x0429); break;
845 case RTL_TEXTENCODING_APPLE_ROMANIAN(((rtl_TextEncoding) 52)):aLanguages[0] = LANGUAGE_ROMANIANLanguageType(0x0418); break;
846
847 case RTL_TEXTENCODING_IBM_861(((rtl_TextEncoding) 6)) :
848 case RTL_TEXTENCODING_APPLE_ICELAND(((rtl_TextEncoding) 51)) :
849 aLanguages[0] = LANGUAGE_ICELANDICLanguageType(0x040F);
850 break;
851
852 case RTL_TEXTENCODING_APPLE_CROATIAN(((rtl_TextEncoding) 43)):aLanguages[0] = LANGUAGE_CROATIANLanguageType(0x041A); break;
853
854 case RTL_TEXTENCODING_IBM_437(((rtl_TextEncoding) 3)) :
855 case RTL_TEXTENCODING_ASCII_US(((rtl_TextEncoding) 11)) : aLanguages[0] = LANGUAGE_ENGLISHLanguageType(0x0009); break;
856
857 case RTL_TEXTENCODING_IBM_862(((rtl_TextEncoding) 28)) :
858 case RTL_TEXTENCODING_MS_1255(((rtl_TextEncoding) 37)) :
859 case RTL_TEXTENCODING_APPLE_HEBREW(((rtl_TextEncoding) 50)) :
860 case RTL_TEXTENCODING_ISO_8859_8(((rtl_TextEncoding) 19)) :
861 aLanguages[0] = LANGUAGE_HEBREWLanguageType(0x040D);
862 break;
863
864 case RTL_TEXTENCODING_IBM_857(((rtl_TextEncoding) 27)) :
865 case RTL_TEXTENCODING_MS_1254(((rtl_TextEncoding) 36)) :
866 case RTL_TEXTENCODING_APPLE_TURKISH(((rtl_TextEncoding) 54)):
867 case RTL_TEXTENCODING_ISO_8859_9(((rtl_TextEncoding) 20)) :
868 aLanguages[0] = LANGUAGE_TURKISHLanguageType(0x041F);
869 break;
870
871 case RTL_TEXTENCODING_IBM_860(((rtl_TextEncoding) 5)) :
872 aLanguages[0] = LANGUAGE_PORTUGUESELanguageType(0x0816);
873 break;
874
875 case RTL_TEXTENCODING_IBM_869(((rtl_TextEncoding) 31)) :
876 case RTL_TEXTENCODING_MS_1253(((rtl_TextEncoding) 35)) :
877 case RTL_TEXTENCODING_APPLE_GREEK(((rtl_TextEncoding) 47)) :
878 case RTL_TEXTENCODING_ISO_8859_7(((rtl_TextEncoding) 18)) :
879 case RTL_TEXTENCODING_IBM_737(((rtl_TextEncoding) 23)) :
880 aLanguages[0] = LANGUAGE_GREEKLanguageType(0x0408);
881 break;
882
883 case RTL_TEXTENCODING_KOI8_R(((rtl_TextEncoding) 74)) :
884 case RTL_TEXTENCODING_ISO_8859_5(((rtl_TextEncoding) 16)) :
885 case RTL_TEXTENCODING_IBM_855(((rtl_TextEncoding) 26)) :
886 case RTL_TEXTENCODING_MS_1251(((rtl_TextEncoding) 34)) :
887 case RTL_TEXTENCODING_IBM_866(((rtl_TextEncoding) 30)) :
888 case RTL_TEXTENCODING_APPLE_CYRILLIC(((rtl_TextEncoding) 44)) :
889 aLanguages[0] = LANGUAGE_RUSSIANLanguageType(0x0419);
890 break;
891
892 case RTL_TEXTENCODING_APPLE_UKRAINIAN(((rtl_TextEncoding) 55)):
893 case RTL_TEXTENCODING_KOI8_U(((rtl_TextEncoding) 88)):
894 aLanguages[0] = LANGUAGE_UKRAINIANLanguageType(0x0422);
895 break;
896
897 case RTL_TEXTENCODING_IBM_864(((rtl_TextEncoding) 29)) :
898 case RTL_TEXTENCODING_MS_1256(((rtl_TextEncoding) 38)) :
899 case RTL_TEXTENCODING_ISO_8859_6(((rtl_TextEncoding) 17)) :
900 case RTL_TEXTENCODING_APPLE_ARABIC(((rtl_TextEncoding) 41)) :
901 aLanguages[0] = LANGUAGE_ARABIC_SAUDI_ARABIALanguageType(0x0401);
902 break;
903
904 case RTL_TEXTENCODING_APPLE_CHINTRAD(((rtl_TextEncoding) 57)) :
905 case RTL_TEXTENCODING_MS_950(((rtl_TextEncoding) 63)) :
906 case RTL_TEXTENCODING_GBT_12345(((rtl_TextEncoding) 66)) :
907 case RTL_TEXTENCODING_BIG5(((rtl_TextEncoding) 68)) :
908 case RTL_TEXTENCODING_EUC_TW(((rtl_TextEncoding) 71)) :
909 case RTL_TEXTENCODING_BIG5_HKSCS(((rtl_TextEncoding) 86)) :
910 aLanguages[0] = LANGUAGE_CHINESE_TRADITIONALLanguageType(0x0404);
911 break;
912
913 case RTL_TEXTENCODING_EUC_JP(((rtl_TextEncoding) 69)) :
914 case RTL_TEXTENCODING_ISO_2022_JP(((rtl_TextEncoding) 72)) :
915 case RTL_TEXTENCODING_JIS_X_0201(((rtl_TextEncoding) 81)) :
916 case RTL_TEXTENCODING_JIS_X_0208(((rtl_TextEncoding) 82)) :
917 case RTL_TEXTENCODING_JIS_X_0212(((rtl_TextEncoding) 83)) :
918 case RTL_TEXTENCODING_APPLE_JAPANESE(((rtl_TextEncoding) 58)) :
919 case RTL_TEXTENCODING_MS_932(((rtl_TextEncoding) 60)) :
920 case RTL_TEXTENCODING_SHIFT_JIS(((rtl_TextEncoding) 64)) :
921 aLanguages[0] = LANGUAGE_JAPANESELanguageType(0x0411);
922 break;
923
924 case RTL_TEXTENCODING_GB_2312(((rtl_TextEncoding) 65)) :
925 case RTL_TEXTENCODING_MS_936(((rtl_TextEncoding) 61)) :
926 case RTL_TEXTENCODING_GBK(((rtl_TextEncoding) 67)) :
927 case RTL_TEXTENCODING_GB_18030(((rtl_TextEncoding) 85)) :
928 case RTL_TEXTENCODING_APPLE_CHINSIMP(((rtl_TextEncoding) 56)) :
929 case RTL_TEXTENCODING_EUC_CN(((rtl_TextEncoding) 70)) :
930 case RTL_TEXTENCODING_ISO_2022_CN(((rtl_TextEncoding) 73)) :
931 aLanguages[0] = LANGUAGE_CHINESE_SIMPLIFIEDLanguageType(0x0804);
932 break;
933
934 case RTL_TEXTENCODING_APPLE_KOREAN(((rtl_TextEncoding) 59)) :
935 case RTL_TEXTENCODING_MS_949(((rtl_TextEncoding) 62)) :
936 case RTL_TEXTENCODING_EUC_KR(((rtl_TextEncoding) 79)) :
937 case RTL_TEXTENCODING_ISO_2022_KR(((rtl_TextEncoding) 80)) :
938 case RTL_TEXTENCODING_MS_1361(((rtl_TextEncoding) 84)) :
939 aLanguages[0] = LANGUAGE_KOREANLanguageType(0x0412);
940 break;
941
942 case RTL_TEXTENCODING_APPLE_THAI(((rtl_TextEncoding) 53)) :
943 case RTL_TEXTENCODING_MS_874(((rtl_TextEncoding) 32)) :
944 case RTL_TEXTENCODING_TIS_620(((rtl_TextEncoding) 87)) :
945 aLanguages[0] = LANGUAGE_THAILanguageType(0x041E);
946 break;
947 default: aLanguages[0] = Application::GetSettings().GetUILanguageTag().getLanguageType();
948 }
949 return aLanguages[0] != LANGUAGE_SYSTEMLanguageType(0x0000);
950}
951void SwSrcEditWindow::SetFont()
952{
953 OUString sFontName(
954 officecfg::Office::Common::Font::SourceViewFont::FontName::get().
955 value_or(OUString()));
956 if(sFontName.isEmpty())
957 {
958 LanguageType aLanguages[5] =
959 {
960 LANGUAGE_SYSTEMLanguageType(0x0000), LANGUAGE_SYSTEMLanguageType(0x0000), LANGUAGE_SYSTEMLanguageType(0x0000), LANGUAGE_SYSTEMLanguageType(0x0000), LANGUAGE_SYSTEMLanguageType(0x0000)
961 };
962 vcl::Font aFont;
963 if(lcl_GetLanguagesForEncoding(m_eSourceEncoding, aLanguages))
964 {
965 //TODO: check for multiple languages
966 aFont = OutputDevice::GetDefaultFont(DefaultFontType::FIXED, aLanguages[0], GetDefaultFontFlags::NONE, this);
967 }
968 else
969 aFont = OutputDevice::GetDefaultFont(DefaultFontType::SANS_UNICODE,
970 Application::GetSettings().GetLanguageTag().getLanguageType(), GetDefaultFontFlags::NONE, this);
971 sFontName = aFont.GetFamilyName();
972 }
973 const SvxFontListItem* pFontListItem =
974 static_cast<const SvxFontListItem* >(m_pSrcView->GetDocShell()->GetItem( SID_ATTR_CHAR_FONTLIST( 10000 + 22 ) ));
975 const FontList* pList = pFontListItem->GetFontList();
976 FontMetric aFontMetric = pList->Get(sFontName,WEIGHT_NORMAL, ITALIC_NONE);
977
978 const vcl::Font& rFont = GetTextEngine()->GetFont();
979 vcl::Font aFont(aFontMetric);
980 Size aSize(rFont.GetFontSize());
981 //font height is stored in point and set in twip
982 aSize.setHeight(
983 officecfg::Office::Common::Font::SourceViewFont::FontHeight::get() * 20 );
984 aFont.SetFontSize(m_pOutWin->LogicToPixel(aSize, MapMode(MapUnit::MapTwip)));
985 GetTextEngine()->SetFont( aFont );
986 m_pOutWin->SetFont(aFont);
987}
988
989void SwSrcEditWindow::SetTextEncoding(rtl_TextEncoding eEncoding)
990{
991 m_eSourceEncoding = eEncoding;
992 SetFont();
993}
994
995/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx

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#ifndef INCLUDED_VCL_PTR_HXX
21#define INCLUDED_VCL_PTR_HXX
22
23#include <sal/config.h>
24
25#include <rtl/ref.hxx>
26
27#include <utility>
28#include <type_traits>
29
30#ifdef DBG_UTIL
31#ifndef _WIN32
32#include <vcl/vclmain.hxx>
33#endif
34#endif
35
36class VclReferenceBase;
37
38namespace vcl::detail {
39
40template<typename>
41constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
42
43template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase(
44 int (*)[sizeof(T)])
45{ return std::is_base_of<VclReferenceBase, T>::value; }
46
47} // namespace vcl::detail
48
49/**
50 * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses.
51 *
52 * For more details on the design please see vcl/README.lifecycle
53 *
54 * @param reference_type must be a subclass of vcl::Window
55 */
56template <class reference_type>
57class VclPtr
58{
59 static_assert(
60 vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>(
61 nullptr),
62 "template argument type must be derived from VclReferenceBase");
63
64 ::rtl::Reference<reference_type> m_rInnerRef;
65
66public:
67 /** Constructor...
68 */
69 VclPtr()
70 : m_rInnerRef()
71 {}
72
73 /** Constructor...
74 */
75 VclPtr (reference_type * pBody)
76 : m_rInnerRef(pBody)
77 {}
78
79 /** Constructor... that doesn't take a ref.
80 */
81 VclPtr (reference_type * pBody, __sal_NoAcquire)
82 : m_rInnerRef(pBody, SAL_NO_ACQUIRE)
83 {}
84
85 /** Up-casting conversion constructor: Copies interface reference.
86
87 Does not work for up-casts to ambiguous bases. For the special case of
88 up-casting to Reference< XInterface >, see the corresponding conversion
89 operator.
90
91 @param rRef another reference
92 */
93 template< class derived_type >
94 VclPtr(
95 const VclPtr< derived_type > & rRef,
96 typename std::enable_if<
97 std::is_base_of<reference_type, derived_type>::value, int>::type
98 = 0 )
99 : m_rInnerRef( static_cast<reference_type*>(rRef) )
100 {
101 }
102
103#if defined(DBG_UTIL) && !defined(_WIN32)
104 virtual ~VclPtr()
105 {
106 assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain
::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 106, __extension__ __PRETTY_FUNCTION__))
;
107 // We can be one of the intermediate counts, but if we are the last
108 // VclPtr keeping this object alive, then something forgot to call dispose().
109 assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
110 && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
;
111 }
112 VclPtr(VclPtr const &) = default;
113 VclPtr(VclPtr &&) = default;
114 VclPtr & operator =(VclPtr const &) = default;
115 VclPtr & operator =(VclPtr &&) = default;
116#endif
117
118 /**
119 * A construction helper for VclPtr. Since VclPtr types are created
120 * with a reference-count of one - to help fit into the existing
121 * code-flow; this helps us to construct them easily.
122 *
123 * For more details on the design please see vcl/README.lifecycle
124 *
125 * @tparam reference_type must be a subclass of vcl::Window
126 */
127 template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg)
128 {
129 return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE );
130 }
131
132 /** Probably most common used: handle->someBodyOp().
133 */
134 reference_type * operator->() const
135 {
136 return m_rInnerRef.get();
137 }
138
139 /** Get the body. Can be used instead of operator->().
140 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
141 are the same.
142 */
143 reference_type * get() const
144 {
145 return m_rInnerRef.get();
146 }
147
148 void set(reference_type *pBody)
149 {
150 m_rInnerRef.set(pBody);
151 }
152
153 void reset(reference_type *pBody)
154 {
155 m_rInnerRef.set(pBody);
156 }
157
158 /** Up-casting copy assignment operator.
159
160 Does not work for up-casts to ambiguous bases.
161
162 @param rRef another reference
163 */
164 template<typename derived_type>
165 typename std::enable_if<
166 std::is_base_of<reference_type, derived_type>::value,
167 VclPtr &>::type
168 operator =(VclPtr<derived_type> const & rRef)
169 {
170 m_rInnerRef.set(rRef.get());
171 return *this;
172 }
173
174 VclPtr & operator =(reference_type * pBody)
175 {
176 m_rInnerRef.set(pBody);
177 return *this;
178 }
179
180 operator reference_type * () const
181 {
182 return m_rInnerRef.get();
183 }
184
185 explicit operator bool () const
186 {
187 return m_rInnerRef.get() != nullptr;
188 }
189
190 void clear()
191 {
192 m_rInnerRef.clear();
193 }
194
195 void reset()
196 {
197 m_rInnerRef.clear();
198 }
199
200 void disposeAndClear()
201 {
202 // hold it alive for the lifetime of this method
203 ::rtl::Reference<reference_type> aTmp(m_rInnerRef);
204 m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-)
5
Calling 'Reference::clear'
12
Returning; memory was released
205 if (aTmp.get()) {
13
Calling 'Reference::get'
206 aTmp->disposeOnce();
207 }
208 }
209
210 /** Needed to place VclPtr's into STL collection.
211 */
212 bool operator< (const VclPtr<reference_type> & handle) const
213 {
214 return (m_rInnerRef < handle.m_rInnerRef);
215 }
216}; // class VclPtr
217
218template<typename T1, typename T2>
219inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
220 return p1.get() == p2.get();
221}
222
223template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2)
224{
225 return p1.get() == p2;
226}
227
228template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) {
229 return p1.get() == p2;
230}
231
232template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2)
233{
234 return p1 == p2.get();
235}
236
237template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) {
238 return p1 == p2.get();
239}
240
241template<typename T1, typename T2>
242inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
243 return !(p1 == p2);
244}
245
246template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2)
247{
248 return !(p1 == p2);
249}
250
251template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) {
252 return !(p1 == p2);
253}
254
255template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2)
256{
257 return !(p1 == p2);
258}
259
260template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) {
261 return !(p1 == p2);
262}
263
264/**
265 * A construction helper for a temporary VclPtr. Since VclPtr types
266 * are created with a reference-count of one - to help fit into
267 * the existing code-flow; this helps us to construct them easily.
268 * see also VclPtr::Create and ScopedVclPtr
269 *
270 * For more details on the design please see vcl/README.lifecycle
271 *
272 * @param reference_type must be a subclass of vcl::Window
273 */
274template <class reference_type>
275class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type>
276{
277public:
278 template<typename... Arg> VclPtrInstance(Arg &&... arg)
279 : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
280 {
281 }
282
283 /**
284 * Override and disallow this, to prevent people accidentally calling it and actually
285 * getting VclPtr::Create and getting a naked VclPtr<> instance
286 */
287 template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete;
288};
289
290template <class reference_type>
291class ScopedVclPtr : public VclPtr<reference_type>
292{
293public:
294 /** Constructor...
295 */
296 ScopedVclPtr()
297 : VclPtr<reference_type>()
298 {}
299
300 /** Constructor
301 */
302 ScopedVclPtr (reference_type * pBody)
303 : VclPtr<reference_type>(pBody)
304 {}
305
306 /** Copy constructor...
307 */
308 ScopedVclPtr (const VclPtr<reference_type> & handle)
309 : VclPtr<reference_type>(handle)
310 {}
311
312 /**
313 Assignment that releases the last reference.
314 */
315 void disposeAndReset(reference_type *pBody)
316 {
317 if (pBody != this->get()) {
318 VclPtr<reference_type>::disposeAndClear();
319 VclPtr<reference_type>::set(pBody);
320 }
321 }
322
323 /**
324 Assignment that releases the last reference.
325 */
326 ScopedVclPtr<reference_type>& operator = (reference_type * pBody)
327 {
328 disposeAndReset(pBody);
329 return *this;
330 }
331
332 /** Up-casting conversion constructor: Copies interface reference.
333
334 Does not work for up-casts to ambiguous bases. For the special case of
335 up-casting to Reference< XInterface >, see the corresponding conversion
336 operator.
337
338 @param rRef another reference
339 */
340 template< class derived_type >
341 ScopedVclPtr(
342 const VclPtr< derived_type > & rRef,
343 typename std::enable_if<
344 std::is_base_of<reference_type, derived_type>::value, int>::type
345 = 0 )
346 : VclPtr<reference_type>( rRef )
347 {
348 }
349
350 /** Up-casting assignment operator.
351
352 Does not work for up-casts to ambiguous bases.
353
354 @param rRef another VclPtr
355 */
356 template<typename derived_type>
357 typename std::enable_if<
358 std::is_base_of<reference_type, derived_type>::value,
359 ScopedVclPtr &>::type
360 operator =(VclPtr<derived_type> const & rRef)
361 {
362 disposeAndReset(rRef.get());
363 return *this;
364 }
365
366 /**
367 * Override and disallow this, to prevent people accidentally calling it and actually
368 * getting VclPtr::Create and getting a naked VclPtr<> instance
369 */
370 template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete;
371
372 ~ScopedVclPtr()
373 {
374 VclPtr<reference_type>::disposeAndClear();
375 assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get(
) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 375, __extension__ __PRETTY_FUNCTION__))
; // make sure there are no lingering references
376 }
377
378private:
379 // Most likely we don't want this default copy-constructor.
380 ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete;
381 // And certainly we don't want a default assignment operator.
382 ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete;
383 // And disallow reset as that doesn't call disposeAndClear on the original reference
384 void reset() = delete;
385 void reset(reference_type *pBody) = delete;
386
387protected:
388 ScopedVclPtr (reference_type * pBody, __sal_NoAcquire)
389 : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE)
390 {}
391};
392
393/**
394 * A construction helper for ScopedVclPtr. Since VclPtr types are created
395 * with a reference-count of one - to help fit into the existing
396 * code-flow; this helps us to construct them easily.
397 *
398 * For more details on the design please see vcl/README.lifecycle
399 *
400 * @param reference_type must be a subclass of vcl::Window
401 */
402#if defined _MSC_VER
403#pragma warning(push)
404#pragma warning(disable: 4521) // " multiple copy constructors specified"
405#endif
406template <class reference_type>
407class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type>
408{
409public:
410 template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg)
411 : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
412 {
413 }
414
415 /**
416 * Override and disallow this, to prevent people accidentally calling it and actually
417 * getting VclPtr::Create and getting a naked VclPtr<> instance
418 */
419 template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete;
420
421private:
422 // Prevent the above perfect forwarding ctor from hijacking (accidental)
423 // attempts at ScopedVclPtrInstance copy construction (where the hijacking
424 // would typically lead to somewhat obscure error messages); both non-const
425 // and const variants are needed here, as the ScopedVclPtr base class has a
426 // const--variant copy ctor, so the implicitly declared copy ctor for
427 // ScopedVclPtrInstance would also be the const variant, so non-const copy
428 // construction attempts would be hijacked by the perfect forwarding ctor;
429 // but if we only declared a non-const variant here, the const variant would
430 // no longer be implicitly declared (as there would already be an explicitly
431 // declared copy ctor), so const copy construction attempts would then be
432 // hijacked by the perfect forwarding ctor:
433 ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete;
434 ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete;
435};
436#if defined _MSC_VER
437#pragma warning(pop)
438#endif
439
440#endif // INCLUDED_VCL_PTR_HXX
441
442/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY1
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
48 /** Constructor...
49 */
50 Reference()
51 : m_pBody (NULL__null)
52 {}
53
54
55 /** Constructor...
56 */
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
62 /** Constructor...
63 */
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
71 /** Copy constructor...
72 */
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY1
81 /** Move constructor...
82 */
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY1
91 /** Up-casting conversion constructor: Copies interface reference.
92
93 Does not work for up-casts to ambiguous bases.
94
95 @param rRef another reference
96 */
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
108 /** Destructor...
109 */
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody)
113 m_pBody->release();
114 }
115
116 /** Set...
117 Similar to assignment.
118 */
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld)
127 pOld->release();
128 return *this;
129 }
130
131 /** Assignment.
132 Unbinds this instance from its body (if bound) and
133 bind it to the body represented by the handle.
134 */
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
139 }
140
141#ifdef LIBO_INTERNAL_ONLY1
142 /** Assignment.
143 * Unbinds this instance from its body (if bound),
144 * bind it to the body represented by the handle, and
145 * set the body represented by the handle to nullptr.
146 */
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
159 /** Assignment...
160 */
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
167 /** Unbind the body from this handle.
168 Note that for a handle representing a large body,
169 "handle.clear().set(new body());" _might_
170 perform a little bit better than "handle.set(new body());",
171 since in the second case two large objects exist in memory
172 (the old body and the new body).
173 */
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody
5.1
Field 'm_pBody' is non-null
5.1
Field 'm_pBody' is non-null
5.1
Field 'm_pBody' is non-null
5.1
Field 'm_pBody' is non-null
)
6
Taking true branch
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
7
Calling 'VclReferenceBase::release'
11
Returning; memory was released
181 }
182 return *this;
183 }
184
185
186 /** Get the body. Can be used instead of operator->().
187 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
188 are the same.
189 */
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
14
Use of memory after it is freed
193 }
194
195
196 /** Probably most common used: handle->someBodyOp().
197 */
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 200, __extension__ __PRETTY_FUNCTION__))
;
201 return m_pBody;
202 }
203
204
205 /** Allows (*handle).someBodyOp().
206 */
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 209, __extension__ __PRETTY_FUNCTION__))
;
210 return *m_pBody;
211 }
212
213
214 /** Returns True if the handle does point to a valid body.
215 */
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL__null);
219 }
220
221#if defined LIBO_INTERNAL_ONLY1
222 /** Returns True if the handle does point to a valid body.
223 */
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
230 /** Returns True if this points to pBody.
231 */
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
238 /** Returns True if handle points to the same body.
239 */
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
247 /** Needed to place References into STL collection.
248 */
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
256 /** Needed to place References into STL collection.
257 */
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
265 /** Needed to place References into STL collection.
266 */
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY1
277namespace std
278{
279
280/// @cond INTERNAL
281/**
282 Make rtl::Reference hashable by default for use in STL containers.
283
284 @since LibreOffice 6.3
285*/
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
292/// @endcond
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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#ifndef INCLUDED_VCL_Reference_HXX
20#define INCLUDED_VCL_Reference_HXX
21
22#include <vcl/dllapi.h>
23#include <osl/interlck.h>
24
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
32 void acquire() const
33 {
34 osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1);
35 }
36
37 void release() const
38 {
39 if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0)
8
Assuming the condition is true
9
Taking true branch
40 delete this;
10
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif