tdf#148470 if macOS glyph fallback provided a partial result flag what failed
[LibreOffice.git] / include / svx / fmsrcimp.hxx
blobb9ff77a1c813c65ca45dc88f16153612f9c38126
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_SVX_FMSRCIMP_HXX
21 #define INCLUDED_SVX_FMSRCIMP_HXX
23 #include <svx/fmtools.hxx>
24 #include <svx/svxdllapi.h>
26 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
28 #include <cppuhelper/implbase.hxx>
29 #include <unotools/charclass.hxx>
30 #include <unotools/collatorwrapper.hxx>
31 #include <tools/link.hxx>
33 #include <atomic>
34 #include <deque>
35 #include <memory>
36 #include <string_view>
37 #include <vector>
39 namespace com::sun::star::awt { class XCheckBox; }
40 namespace com::sun::star::awt { class XListBox; }
41 namespace com::sun::star::awt { class XTextComponent; }
42 namespace com::sun::star::sdb { class XColumn; }
44 enum class TransliterationFlags;
46 /**
47 * struct FmSearchProgress - the owner of SearchEngine receives this structure for status updates
48 * (at the end of the search)
50 struct FmSearchProgress
52 enum class State { Progress, ProgressCounting, Canceled, Successful, NothingFound, Error };
53 // (move to new record; progress during counting of records; cancelled; record found; nothing found;
54 // any non-processable error)
55 State aSearchState;
57 // current record - always valid (e.g. of interest for continuing search in case of cancellation)
58 sal_uInt32 nCurrentRecord;
59 // Overflow - only valid in case of STATE_PROGRESS
60 bool bOverflow;
62 // the position of the search cursor - valid in case of STATE_SUCCESSFUL, STATE_CANCELED and STATE_NOTHING_FOUND
63 css::uno::Any aBookmark;
64 // the field, in which the text was found - valid in case of STATE_SUCCESSFUL
65 sal_Int32 nFieldIndex;
68 /**
69 * class FmRecordCountListener - utility class for FmSearchEngine, listens at a certain cursor and provides
70 * the differences in RecordCount
72 // workaround for incremental linking bugs in MSVC2019
73 class SAL_DLLPUBLIC_TEMPLATE FmRecordCountListener_Base : public cppu::WeakImplHelper< css::beans::XPropertyChangeListener > {};
74 class SAL_WARN_UNUSED FmRecordCountListener final : public FmRecordCountListener_Base
76 // attribute
77 Link<sal_Int32,void> m_lnkWhoWantsToKnow;
78 css::uno::Reference< css::beans::XPropertySet > m_xListening;
80 // attribute access
81 public:
82 void SetPropChangeHandler(const Link<sal_Int32,void>& lnk);
84 // methods
85 public:
86 FmRecordCountListener(const css::uno::Reference< css::sdbc::XResultSet >& dbcCursor);
87 // the set has to support the sdb::ResultSet service
88 virtual ~FmRecordCountListener() override;
90 // DECLARE_UNO3_AGG_DEFAULTS(FmPropertyListener, UsrObject)
91 // virtual sal_Bool queryInterface(css::uno::Uik aUik, css::uno::Reference< css::uno::XInterface >& rOut);
93 // css::lang::XEventListener
94 virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
96 // css::beans::XPropertyChangeListener
97 virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent& evt) override;
99 void DisConnect();
101 private:
102 void NotifyCurrentCount();
107 * class FmSearchEngine - Impl class for FmSearchDialog
109 namespace svxform {
110 // We have three possible control types we may search in, determined by the supported interfaces : css::awt::XTextComponent,
111 // css::awt::XListBox, css::awt::XCheckBox.
112 // While searching we don't want to do this distinction for every control in every round. So we need some helpers.
113 class SAL_WARN_UNUSED ControlTextWrapper
115 // attributes
116 css::uno::Reference< css::uno::XInterface > m_xControl;
117 // attribute access
118 public:
119 const css::uno::Reference< css::uno::XInterface >& getControl() const{ return m_xControl; }
120 public:
121 ControlTextWrapper(const css::uno::Reference< css::uno::XInterface >& _xControl) { m_xControl = _xControl; }
122 virtual ~ControlTextWrapper() { }
124 virtual OUString getCurrentText() const = 0;
127 class SAL_WARN_UNUSED SimpleTextWrapper final : public ControlTextWrapper
129 css::uno::Reference< css::awt::XTextComponent > m_xText;
130 public:
131 SimpleTextWrapper(const css::uno::Reference< css::awt::XTextComponent >& _xText);
132 virtual OUString getCurrentText() const override;
135 class SAL_WARN_UNUSED ListBoxWrapper final : public ControlTextWrapper
137 css::uno::Reference< css::awt::XListBox > m_xBox;
138 public:
139 ListBoxWrapper(const css::uno::Reference< css::awt::XListBox >& _xBox);
140 virtual OUString getCurrentText() const override;
143 class SAL_WARN_UNUSED CheckBoxWrapper final : public ControlTextWrapper
145 css::uno::Reference< css::awt::XCheckBox > m_xBox;
146 public:
147 CheckBoxWrapper(const css::uno::Reference< css::awt::XCheckBox >& _xBox);
148 virtual OUString getCurrentText() const override;
152 typedef std::vector< css::uno::Reference< css::uno::XInterface> > InterfaceArray;
154 class SAL_WARN_UNUSED SVX_DLLPUBLIC FmSearchEngine final
156 friend class FmSearchThread;
158 enum class SearchResult { Found, NotFound, Error, Cancelled };
159 enum class SearchFor { String, Null, NotNull };
161 CursorWrapper m_xSearchCursor;
162 std::deque<sal_Int32> m_arrFieldMapping;
163 // Since the iterator could have more columns, as managed here (in this field listbox),
164 // a mapping of this css::form keys on the indices of the respective columns is kept in the iterator
166 CharClass m_aCharacterClassficator;
167 CollatorWrapper m_aStringCompare;
169 // the collection of all interesting fields (or their css::data::XDatabaseVariant interfaces and FormatKeys)
170 struct FieldInfo
172 css::uno::Reference< css::sdb::XColumn > xContents;
175 typedef std::vector<FieldInfo> FieldCollection;
176 FieldCollection m_arrUsedFields;
177 sal_Int32 m_nCurrentFieldIndex; // the last parameter of RebuildUsedFields, it allows checks in FormatField
179 std::vector<std::unique_ptr<svxform::ControlTextWrapper>>
180 m_aControlTexts;
182 CursorWrapper m_xOriginalIterator;
183 CursorWrapper m_xClonedIterator;
185 // Data for the decision in which field a "Found" is accepted
186 css::uno::Any m_aPreviousLocBookmark; // position of the last finding
187 FieldCollection::iterator m_iterPreviousLocField; // field of the last finding
189 // Communication with the thread that does the actual searching
190 OUString m_strSearchExpression; // forward direction
191 SearchFor m_eSearchForType; // ditto
192 SearchResult m_srResult; // backward direction
194 // The link we broadcast the progress and the result to
195 Link<const FmSearchProgress*,void> m_aProgressHandler;
196 std::atomic<bool> m_bCancelAsynchRequest; // should be cancelled?
198 // parameters for the search
199 bool m_bSearchingCurrently : 1; // is an (asynchronous) search running?
200 bool m_bFormatter : 1; // use field formatting
201 bool m_bForward : 1; // direction
202 bool m_bWildcard : 1; // wildcard search
203 bool m_bRegular : 1; // regular expression
204 bool m_bLevenshtein : 1; // Levenshtein search
205 bool m_bTransliteration : 1; // Levenshtein search
207 bool m_bLevRelaxed : 1; // parameters for Levenshtein search
208 sal_uInt16 m_nLevOther;
209 sal_uInt16 m_nLevShorter;
210 sal_uInt16 m_nLevLonger;
212 sal_uInt16 m_nPosition; // if not regular or levenshtein, then one of the MATCHING_... values
214 TransliterationFlags m_nTransliterationFlags;
217 // member access
218 private:
219 SVX_DLLPRIVATE bool CancelRequested(); // provides a through m_aCancelAsynchAccess backed interpretation of m_bCancelAsynchRequest
221 public:
222 void SetCaseSensitive(bool bSet);
223 bool GetCaseSensitive() const;
225 void SetFormatterUsing(bool bSet); // this is somewhat more extensive, so no inline ... here
226 bool GetFormatterUsing() const { return m_bFormatter; }
228 void SetDirection(bool bForward) { m_bForward = bForward; }
229 bool GetDirection() const { return m_bForward; }
231 void SetWildcard(bool bSet) { m_bWildcard = bSet; }
232 bool GetWildcard() const { return m_bWildcard; }
234 void SetRegular(bool bSet) { m_bRegular = bSet; }
235 bool GetRegular() const { return m_bRegular; }
237 void SetLevenshtein(bool bSet) { m_bLevenshtein = bSet; }
238 bool GetLevenshtein() const { return m_bLevenshtein; }
240 void SetIgnoreWidthCJK(bool bSet);
241 bool GetIgnoreWidthCJK() const;
243 void SetTransliteration(bool bSet) { m_bTransliteration = bSet; }
244 bool GetTransliteration() const { return m_bTransliteration; }
246 void SetLevRelaxed(bool bSet) { m_bLevRelaxed = bSet; }
247 bool GetLevRelaxed() const { return m_bLevRelaxed; }
248 void SetLevOther(sal_uInt16 nHowMuch) { m_nLevOther = nHowMuch; }
249 sal_uInt16 GetLevOther() const { return m_nLevOther; }
250 void SetLevShorter(sal_uInt16 nHowMuch) { m_nLevShorter = nHowMuch; }
251 sal_uInt16 GetLevShorter() const { return m_nLevShorter; }
252 void SetLevLonger(sal_uInt16 nHowMuch) { m_nLevLonger = nHowMuch; }
253 sal_uInt16 GetLevLonger() const { return m_nLevLonger; }
254 // all Lev. values will only be considered in case of m_bLevenshtein==sal_True
256 void SetTransliterationFlags(TransliterationFlags _nFlags) { m_nTransliterationFlags = _nFlags; }
257 TransliterationFlags
258 GetTransliterationFlags() const { return m_nTransliterationFlags; }
260 void SetPosition(sal_uInt16 nValue) { m_nPosition = nValue; }
261 sal_uInt16 GetPosition() const { return m_nPosition; }
262 // position will be ignored in case of m_bWildCard==sal_True
264 public:
265 /** two constructs, both analogical to FmSearchDialog, therefore look this up for explanations...
266 xCursor has to implement css::data::DatabaseCursor service each time.
267 If eMode == SM_USETHREAD, a ProgressHandler should be set, because in this case the result forwarding will be done
268 by this handler.
269 If eMode != SM_USETHREAD, SearchNext and StarOver won't return, until the search has finished (independently of its
270 success), only then the result can be requested. If additionally the ProgressHandler is set, it will be called for
271 every record as well as at the end of the search.
273 FmSearchEngine(
274 const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
275 const css::uno::Reference< css::sdbc::XResultSet >& xCursor,
276 const OUString& strVisibleFields,
277 const InterfaceArray& arrFields);
279 /** the link will be called on every record and after the completion of the search, the parameter is a pointer to
280 a FmSearchProgress structure
281 the handler should be in any case thread-safe
283 void SetProgressHandler(Link<const FmSearchProgress*,void> aHdl) { m_aProgressHandler = aHdl; }
285 /// search for the next appearance (for nDirection values check DIRECTION_*-defines)
286 void SearchNext(const OUString& strExpression);
287 /// analogous, search for "NULL" (_bSearchForNull==sal_True) or "not NULL"
288 void SearchNextSpecial(bool _bSearchForNull);
289 /// search for the next appearance, dependent on nDirection from the start or end
290 void StartOver(const OUString& strExpression);
291 /// analogous, search for "NULL" (_bSearchForNull==sal_True) or "not NULL"
292 void StartOverSpecial(bool _bSearchForNull);
293 /// invalidate previous search reference
294 void InvalidatePreviousLoc();
296 /** rebuilds m_arrUsedFields (nFieldIndex==-1 means all fields, otherwise it specifies the field index)
297 if bForce is not set, nothing will happen in case of nFieldIndex == m_nCurrentFieldIndex
298 (calls InvalidatePreviousLoc)
300 void RebuildUsedFields(sal_Int32 nFieldIndex, bool bForce = false);
301 OUString FormatField(sal_Int32 nWhich);
303 /// returns directly; once it was really aborted, ProgressHandler is called with STATE_CANCELED
304 void CancelSearch();
306 /** only valid, if not an (asynchronous) search is running, the next search will then be executed
307 on top of the new iterator with the new parameter
309 void SwitchToContext(const css::uno::Reference< css::sdbc::XResultSet >& xCursor, const OUString& strVisibleFields, const InterfaceArray& arrFields,
310 sal_Int32 nFieldIndex);
312 private:
313 void Init(const OUString& strVisibleFields);
315 void SearchNextImpl();
316 // this Impl method is running in SearchThread
318 // start a thread-search (or call SearchNextImpl directly, depending on the search mode)
319 void ImplStartNextSearch();
321 SVX_DLLPRIVATE void fillControlTexts(const InterfaceArray& arrFields);
323 // three methods implementing a complete search loop (null/not null, wildcard, SearchText)
324 // (they all have some code in common, but with this solution we have to do a distinction only once per search (before
325 // starting the loop), not in every loop step
326 SVX_DLLPRIVATE SearchResult SearchSpecial(bool _bSearchForNull, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
327 const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
328 SVX_DLLPRIVATE SearchResult SearchWildcard(std::u16string_view strExpression, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
329 const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
330 SVX_DLLPRIVATE SearchResult SearchRegularApprox(const OUString& strExpression, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
331 const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
333 SVX_DLLPRIVATE void PropagateProgress(bool _bDontPropagateOverflow);
334 // call the ProgressHandler with STATE_PROGRESS and the current position of the search iterator
336 // helpers, that are needed several times
337 SVX_DLLPRIVATE bool MoveCursor();
338 // moves m_xSearchIterator with respect to direction/overflow cursor
339 SVX_DLLPRIVATE bool MoveField(sal_Int32& nPos, FieldCollection::iterator& iter, const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
340 // moves the iterator with respect to the direction/overflow iterator/overflow cursor
341 SVX_DLLPRIVATE void BuildAndInsertFieldInfo(const css::uno::Reference< css::container::XIndexAccess >& xAllFields, sal_Int32 nField);
343 void OnSearchTerminated();
344 // is used by SearchThread, after the return from this handler the thread removes itself
345 DECL_DLLPRIVATE_LINK(OnNewRecordCount, sal_Int32, void);
348 #endif // INCLUDED_SVX_FMSRCIMP_HXX
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */