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_EDIT_HXX
21 #define INCLUDED_VCL_EDIT_HXX
22 
23 #include <vcl/ctrl.hxx>
24 #include <vcl/textfilter.hxx>
25 
26 #include <memory>
27 
28 #include <o3tl/deleter.hxx>
29 #include <tools/solar.h>
30 #include <vcl/dllapi.h>
31 #include <vcl/menu.hxx>
32 #include <vcl/dndhelp.hxx>
33 #include <vcl/vclptr.hxx>
34 #include <com/sun/star/uno/Reference.h>
35 
36 namespace com {
37 namespace sun {
38 namespace star {
39 namespace i18n {
40     class XBreakIterator;
41     class XExtendedInputSequenceChecker;
42 }}}}
43 namespace weld {
44     class Widget;
45 }
46 
47 class VclBuilder;
48 struct DDInfo;
49 struct Impl_IMEInfos;
50 
51 #define EDIT_NOLIMIT                SAL_MAX_INT32
52 
53 typedef OUString (*FncGetSpecialChars)( weld::Widget* pWin, const vcl::Font& rFont );
54 
55 class Timer;
56 
57 class VCL_DLLPUBLIC Edit : public Control, public vcl::unohelper::DragAndDropClient
58 {
59 private:
60     VclPtr<Edit>        mpSubEdit;
61     TextFilter*         mpFilterText;
62     std::unique_ptr<DDInfo, o3tl::default_delete<DDInfo>> mpDDInfo;
63     std::unique_ptr<Impl_IMEInfos> mpIMEInfos;
64     OUStringBuffer      maText;
65     OUString            maPlaceholderText;
66     OUString            maSaveValue;
67     OUString            maUndoText;
68     long                mnXOffset;
69     Selection           maSelection;
70     sal_uInt16          mnAlign;
71     sal_Int32           mnMaxTextLen;
72     sal_Int32           mnWidthInChars;
73     sal_Int32           mnMaxWidthChars;
74     sal_Unicode         mcEchoChar;
75     bool                mbModified:1,
76                         mbSelectAllSingleClick:1,
77                         mbInternModified:1,
78                         mbReadOnly:1,
79                         mbInsertMode:1,
80                         mbClickedInSelection:1,
81                         mbIsSubEdit:1,
82                         mbActivePopup:1,
83                         mbForceControlBackground:1,
84                         mbPassword;
85     Link<Edit&,void>    maModifyHdl;
86     Link<Edit&,void>    maAutocompleteHdl;
87     Link<Edit&,bool>    maActivateHdl;
88     std::unique_ptr<VclBuilder> mpUIBuilder;
89 
90     css::uno::Reference<css::i18n::XExtendedInputSequenceChecker> mxISC;
91 
92     SAL_DLLPRIVATE bool        ImplTruncateToMaxLen( OUString&, sal_Int32 nSelectionLen ) const;
93     SAL_DLLPRIVATE void        ImplInitEditData();
94     SAL_DLLPRIVATE void        ImplModified();
95     SAL_DLLPRIVATE OUString    ImplGetText() const;
96     SAL_DLLPRIVATE void        ImplRepaint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRectangle);
97     SAL_DLLPRIVATE void        ImplInvalidateOrRepaint();
98     SAL_DLLPRIVATE void        ImplDelete( const Selection& rSelection, sal_uInt8 nDirection, sal_uInt8 nMode );
99     SAL_DLLPRIVATE void        ImplSetText( const OUString& rStr, const Selection* pNewSelection );
100     SAL_DLLPRIVATE void        ImplInsertText( const OUString& rStr, const Selection* pNewSelection = nullptr, bool bIsUserInput = false );
101     SAL_DLLPRIVATE static OUString ImplGetValidString( const OUString& rString );
102     SAL_DLLPRIVATE void        ImplClearBackground(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRectangle, long nXStart, long nXEnd);
103     SAL_DLLPRIVATE void        ImplPaintBorder(vcl::RenderContext const & rRenderContext);
104     SAL_DLLPRIVATE void        ImplShowCursor( bool bOnlyIfVisible = true );
105     SAL_DLLPRIVATE void        ImplAlign();
106     SAL_DLLPRIVATE void        ImplAlignAndPaint();
107     SAL_DLLPRIVATE sal_Int32   ImplGetCharPos( const Point& rWindowPos ) const;
108     SAL_DLLPRIVATE void        ImplSetCursorPos( sal_Int32 nChar, bool bSelect );
109     SAL_DLLPRIVATE void        ImplShowDDCursor();
110     SAL_DLLPRIVATE void        ImplHideDDCursor();
111     SAL_DLLPRIVATE bool        ImplHandleKeyEvent( const KeyEvent& rKEvt );
112     SAL_DLLPRIVATE void        ImplCopyToSelectionClipboard();
113     SAL_DLLPRIVATE void        ImplCopy(css::uno::Reference<css::datatransfer::clipboard::XClipboard> const & rxClipboard);
114     SAL_DLLPRIVATE void        ImplPaste(css::uno::Reference<css::datatransfer::clipboard::XClipboard> const & rxClipboard);
115     SAL_DLLPRIVATE long        ImplGetTextYPosition() const;
116     SAL_DLLPRIVATE css::uno::Reference<css::i18n::XExtendedInputSequenceChecker > const & ImplGetInputSequenceChecker();
117     SAL_DLLPRIVATE static css::uno::Reference<css::i18n::XBreakIterator > ImplGetBreakIterator();
118     SAL_DLLPRIVATE void        filterText();
119 
120 protected:
121     using Control::ImplInitSettings;
122     using Window::ImplInit;
123     SAL_DLLPRIVATE void        ImplInit( vcl::Window* pParent, WinBits nStyle );
124     SAL_DLLPRIVATE static WinBits ImplInitStyle( WinBits nStyle );
125     SAL_DLLPRIVATE void        ImplSetSelection( const Selection& rSelection, bool bPaint = true );
126     SAL_DLLPRIVATE ControlType ImplGetNativeControlType() const;
127     SAL_DLLPRIVATE long        ImplGetExtraXOffset() const;
128     SAL_DLLPRIVATE long        ImplGetExtraYOffset() const;
129     static SAL_DLLPRIVATE void ImplInvalidateOutermostBorder( vcl::Window* pWin );
130 
131     css::uno::Reference<css::datatransfer::dnd::XDragSourceListener > mxDnDListener;
132 
133     // DragAndDropClient
134     using vcl::unohelper::DragAndDropClient::dragEnter;
135     using vcl::unohelper::DragAndDropClient::dragExit;
136     using vcl::unohelper::DragAndDropClient::dragOver;
137     virtual void dragGestureRecognized(const css::datatransfer::dnd::DragGestureEvent& dge) override;
138     virtual void dragDropEnd(const css::datatransfer::dnd::DragSourceDropEvent& dsde) override;
139     virtual void drop(const css::datatransfer::dnd::DropTargetDropEvent& dtde) override;
140     virtual void dragEnter(const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee) override;
141     virtual void dragExit(const css::datatransfer::dnd::DropTargetEvent& dte) override;
142     virtual void dragOver(const css::datatransfer::dnd::DropTargetDragEvent& dtde) override;
143 
144 protected:
145     Edit(WindowType nType);
146     virtual void FillLayoutData() const override;
147     virtual void ApplySettings(vcl::RenderContext& rRenderContext) override;
148 public:
149     // public because needed in button.cxx
150     SAL_DLLPRIVATE bool ImplUseNativeBorder(vcl::RenderContext const & rRenderContext, WinBits nStyle);
151 
152     Edit( vcl::Window* pParent, WinBits nStyle = WB_BORDER );
153     virtual ~Edit() override;
154     virtual void dispose() override;
155 
156     virtual void        MouseButtonDown( const MouseEvent& rMEvt ) override;
157     virtual void        MouseButtonUp( const MouseEvent& rMEvt ) override;
158     virtual void        KeyInput( const KeyEvent& rKEvt ) override;
159     virtual void        Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
160     virtual void        Resize() override;
161     virtual void        Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
162     virtual void        GetFocus() override;
163     virtual void        LoseFocus() override;
164     virtual void        Tracking( const TrackingEvent& rTEvt ) override;
165     virtual void        Command( const CommandEvent& rCEvt ) override;
166     virtual void        StateChanged( StateChangedType nType ) override;
167     virtual void        DataChanged( const DataChangedEvent& rDCEvt ) override;
168 
169     virtual void        Modify();
170 
171     static bool         IsCharInput( const KeyEvent& rKEvt );
172 
173     virtual void        SetModifyFlag();
174     virtual void        ClearModifyFlag();
IsModified() const175     virtual bool        IsModified() const { return mpSubEdit ? mpSubEdit->mbModified : mbModified; }
176 
177     void                SetEchoChar( sal_Unicode c );
GetEchoChar() const178     sal_Unicode         GetEchoChar() const { return mcEchoChar; }
179 
180     virtual void        SetReadOnly( bool bReadOnly = true );
IsReadOnly() const181     virtual bool        IsReadOnly() const { return mbReadOnly; }
182 
183     void                SetSelectAllSingleClick( bool bSelectAllSingleClick );
184 
185     void                SetInsertMode( bool bInsert );
186     bool                IsInsertMode() const;
187 
188     virtual void        SetMaxTextLen( sal_Int32 nMaxLen );
GetMaxTextLen() const189     virtual sal_Int32   GetMaxTextLen() const { return mnMaxTextLen; }
190 
191     void                SetWidthInChars(sal_Int32 nWidthInChars);
GetWidthInChars() const192     sal_Int32           GetWidthInChars() const { return mnWidthInChars; }
193 
194     void                setMaxWidthChars(sal_Int32 nWidth);
195 
196     virtual void        SetSelection( const Selection& rSelection );
197     virtual const Selection&    GetSelection() const;
198 
199     virtual void        ReplaceSelected( const OUString& rStr );
200     virtual void        DeleteSelected();
201     virtual OUString    GetSelected() const;
202 
203     virtual void        Cut();
204     virtual void        Copy();
205     virtual void        Paste();
206     void                Undo();
207 
208     virtual void        SetText( const OUString& rStr ) override;
209     virtual void        SetText( const OUString& rStr, const Selection& rNewSelection );
210     virtual OUString    GetText() const override;
211 
212     void                SetCursorAtLast();
213 
214     void                SetPlaceholderText( const OUString& rStr );
215 
SaveValue()216     void                SaveValue() { maSaveValue = GetText(); }
GetSavedValue() const217     const OUString&     GetSavedValue() const { return maSaveValue; }
IsValueChangedFromSaved() const218     bool                IsValueChangedFromSaved() const { return maSaveValue != GetText(); }
219 
SetModifyHdl(const Link<Edit &,void> & rLink)220     virtual void        SetModifyHdl( const Link<Edit&,void>& rLink ) { maModifyHdl = rLink; }
GetModifyHdl() const221     virtual const Link<Edit&,void>& GetModifyHdl() const { return maModifyHdl; }
222 
SetActivateHdl(const Link<Edit &,bool> & rLink)223     void                SetActivateHdl(const Link<Edit&,bool>& rLink) { maActivateHdl = rLink; }
224 
225     void                SetSubEdit( Edit* pEdit );
GetSubEdit() const226     Edit*               GetSubEdit() const { return mpSubEdit; }
227 
SetAutocompleteHdl(const Link<Edit &,void> & rLink)228     void                SetAutocompleteHdl( const Link<Edit&,void>& rLink ) { maAutocompleteHdl = rLink; }
GetAutocompleteHdl() const229     const Link<Edit&,void>& GetAutocompleteHdl() const { return maAutocompleteHdl; }
230 
231     virtual Size        CalcMinimumSize() const;
232     virtual Size        CalcMinimumSizeForText(const OUString &rString) const;
233     virtual Size        GetOptimalSize() const override;
234     virtual Size        CalcSize(sal_Int32 nChars) const;
235     sal_Int32           GetMaxVisChars() const;
236 
237     sal_Int32           GetCharPos( const Point& rWindowPos ) const;
238 
239     // shows a warning box saying "text too long, truncated"
240     static void         ShowTruncationWarning(weld::Widget* pParent);
241 
242     static void                 SetGetSpecialCharsFunction( FncGetSpecialChars fn );
243     static FncGetSpecialChars   GetGetSpecialCharsFunction();
244 
245     VclPtr<PopupMenu>           CreatePopupMenu();
246 
247     virtual OUString GetSurroundingText() const override;
248     virtual Selection GetSurroundingTextSelection() const override;
249     virtual bool set_property(const OString &rKey, const OUString &rValue) override;
250 
SetTextFilter(TextFilter * pFilter)251     void SetTextFilter(TextFilter* pFilter) { mpFilterText = pFilter; }
252 
253     virtual FactoryFunction GetUITestFactory() const override;
254 
255     // returns the minimum size a bordered Edit should have given the current
256     // global style settings (needed by sc's inputwin.cxx)
257     static Size GetMinimumEditSize();
258 
SetForceControlBackground(bool b)259     void SetForceControlBackground(bool b) { mbForceControlBackground = b; }
260 
IsPassword() const261     bool IsPassword() const { return mbPassword; }
262 };
263 
264 #endif // INCLUDED_VCL_EDIT_HXX
265 
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
267