Pass the auto completion map via move semantics to the Scintilla control to avoid...
[TortoiseGit.git] / src / Utils / MiscUI / SciEdit.h
blob2c5e14c2518acb4860266d17b796dd972cd69498
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2017 - TortoiseGit
4 // Copyright (C) 2003-2008, 2013 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "../SmartHandle.h"
22 #include "scintilla.h"
23 #include "SciLexer.h"
24 #include "hunspell.hxx"
25 #include "mythes.hxx"
26 #include "ProjectProperties.h"
27 #include "PersonalDictionary.h"
28 #include <regex>
29 #include "LruCache.h"
31 #define AUTOCOMPLETE_SPELLING 0
32 #define AUTOCOMPLETE_FILENAME 1
33 #define AUTOCOMPLETE_PROGRAMCODE 2
34 #define AUTOCOMPLETE_SNIPPET 3
36 //forward declaration
37 class CSciEdit;
39 /**
40 * \ingroup Utils
41 * This class acts as an interface so that CSciEdit can call these methods
42 * on other objects which implement this interface.
43 * Classes implementing this interface must call RegisterContextMenuHandler()
44 * in CSciEdit to register themselves.
46 class CSciEditContextMenuInterface
48 public:
49 /**
50 * When the handler is called with this method, it can add entries
51 * to the \a mPopup context menu itself. The \a nCmd param is the command
52 * ID number the handler must use for its commands. For every added command,
53 * the handler is responsible to increment the \a nCmd param by one.
55 virtual void InsertMenuItems(CMenu& mPopup, int& nCmd);
57 /**
58 * The handler is called when the user clicks on any context menu entry
59 * which isn't handled by CSciEdit itself. That means the handler might
60 * be called for entries it hasn't added itself!
61 * \remark the handler should return \a true if it handled the call, otherwise
62 * it should return \a false
64 virtual bool HandleMenuItemClick(int cmd, CSciEdit * pSciEdit);
66 virtual void HandleSnippet(int type, const CString &text, CSciEdit *pSciEdit);
69 /**
70 * \ingroup Utils
71 * Encapsulates the Scintilla edit control. Usable as a replacement for the
72 * MFC CEdit control, but not a drop-in replacement!
73 * Also provides additional features like spell checking, auto completion, ...
75 class CSciEdit : public CWnd
77 DECLARE_DYNAMIC(CSciEdit)
78 public:
79 CSciEdit(void);
80 ~CSciEdit(void);
82 void SetAStyle(int style, COLORREF fore, COLORREF back = ::GetSysColor(COLOR_WINDOW), int size = -1, const char* face = nullptr);
83 void SetUDiffStyle();
85 /**
86 * Initialize the scintilla control. Must be called prior to any other
87 * method!
89 void Init(const ProjectProperties& props);
90 /** Initialize the scintilla control.
91 * lLanguage for initialiring spell checker: 0 = auto-detect language, -1 disable, or language code
93 void Init(LONG lLanguage = 0);
94 void SetIcon(const std::map<int, UINT> &icons);
95 void SetColors(bool recolorize);
96 /**
97 * Execute a scintilla command, e.g. SCI_GETLINE.
99 LRESULT Call(UINT message, WPARAM wParam = 0, LPARAM lParam = 0);
101 * The specified text is written to the scintilla control.
103 void SetText(const CString& sText);
105 * The specified text is inserted at the cursor position. If a text is
106 * selected, that text is replaced.
107 * \param sText test to insert
108 * \param bNewLine if set to true, a newline is appended.
110 void InsertText(const CString& sText, bool bNewLine = false);
112 * Retrieves the text in the scintilla control.
114 CString GetText(void);
116 * Sets the font for the control.
118 void SetFont(CString sFontName, int iFontSizeInPoints);
120 * Adds a list of words for use in auto completion.
122 void SetAutoCompletionList(std::map<CString, int>&& list, TCHAR separator = ';', TCHAR typeSeparator = '?');
124 * Returns the word located under the cursor.
126 CString GetWordUnderCursor(bool bSelectWord = false, bool allchars = false);
128 void RegisterContextMenuHandler(CSciEditContextMenuInterface * object) {m_arContextHandlers.Add(object);}
129 void ClearContextMenuHandlers() { m_arContextHandlers.RemoveAll(); }
131 CStringA StringForControl(const CString& text);
132 CString StringFromControl(const CStringA& text);
133 int LoadFromFile(CString &filename);
134 void RestyleBugIDs();
136 private:
137 bool IsUTF8(LPVOID pBuffer, size_t cb);
138 CAutoLibrary m_hModule;
139 LRESULT m_DirectFunction;
140 LRESULT m_DirectPointer;
141 std::unique_ptr<Hunspell> pChecker;
142 std::unique_ptr<MyThes> pThesaur;
143 UINT m_spellcodepage;
144 std::map<CString, int> m_autolist;
145 TCHAR m_separator;
146 TCHAR m_typeSeparator;
147 CStringA m_sCommand;
148 CStringA m_sBugID;
149 CString m_sUrl;
150 CArray<CSciEditContextMenuInterface *, CSciEditContextMenuInterface *> m_arContextHandlers;
151 CPersonalDictionary m_personalDict;
152 bool m_bDoStyle;
153 int m_nAutoCompleteMinChars;
154 LruCache<std::wstring, BOOL> m_SpellingCache;
155 bool m_blockModifiedHandler;
156 static bool IsValidURLChar(unsigned char ch);
157 protected:
158 virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) override;
159 virtual BOOL PreTranslateMessage(MSG* pMsg) override;
160 virtual ULONG GetGestureStatus(CPoint ptTouch) override;
161 void CheckSpelling(int startpos, int endpos);
162 void SuggestSpellingAlternatives(void);
163 void DoAutoCompletion(int nMinPrefixLength);
164 BOOL LoadDictionaries(LONG lLanguageID);
165 BOOL MarkEnteredBugID(int startstylepos, int endstylepos);
166 bool StyleEnteredText(int startstylepos, int endstylepos);
167 void StyleURLs(int startstylepos, int endstylepos);
168 bool WrapLines(int startpos, int endpos);
169 bool FindStyleChars(const char * line, char styler, int& start, int& end);
170 void AdvanceUTF8(const char * str, int& pos);
171 BOOL IsMisspelled(const CString& sWord);
172 DWORD GetStyleAt(int pos) { return (DWORD)Call(SCI_GETSTYLEAT, pos) & 0x1f; }
173 bool IsUrlOrEmail(const CStringA& sText);
174 CStringA GetWordForSpellChecker(const CString& sWord);
175 CString GetWordFromSpellChecker(const CStringA& sWordA);
177 virtual afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
178 afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/);
179 DECLARE_MESSAGE_MAP()