Detect email addresses in log messages and make them clickable
[TortoiseGit.git] / src / Utils / MiscUI / SciEdit.h
blob1f6908dcbdac9064bcd2ad9b80d40aef2d67d5bc
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2016 - 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 /**
96 * Execute a scintilla command, e.g. SCI_GETLINE.
98 LRESULT Call(UINT message, WPARAM wParam = 0, LPARAM lParam = 0);
99 /**
100 * The specified text is written to the scintilla control.
102 void SetText(const CString& sText);
104 * The specified text is inserted at the cursor position. If a text is
105 * selected, that text is replaced.
106 * \param sText test to insert
107 * \param bNewLine if set to true, a newline is appended.
109 void InsertText(const CString& sText, bool bNewLine = false);
111 * Retrieves the text in the scintilla control.
113 CString GetText(void);
115 * Sets the font for the control.
117 void SetFont(CString sFontName, int iFontSizeInPoints);
119 * Adds a list of words for use in auto completion.
121 void SetAutoCompletionList(const std::map<CString, int>& list, TCHAR separator = ';', TCHAR typeSeparator = '?');
123 * Returns the word located under the cursor.
125 CString GetWordUnderCursor(bool bSelectWord = false);
127 void RegisterContextMenuHandler(CSciEditContextMenuInterface * object) {m_arContextHandlers.Add(object);}
129 CStringA StringForControl(const CString& text);
130 CString StringFromControl(const CStringA& text);
131 int LoadFromFile(CString &filename);
132 void RestyleBugIDs();
134 private:
135 bool IsUTF8(LPVOID pBuffer, size_t cb);
136 CAutoLibrary m_hModule;
137 LRESULT m_DirectFunction;
138 LRESULT m_DirectPointer;
139 std::unique_ptr<Hunspell> pChecker;
140 std::unique_ptr<MyThes> pThesaur;
141 UINT m_spellcodepage;
142 std::map<CString, int> m_autolist;
143 TCHAR m_separator;
144 TCHAR m_typeSeparator;
145 CStringA m_sCommand;
146 CStringA m_sBugID;
147 CString m_sUrl;
148 CArray<CSciEditContextMenuInterface *, CSciEditContextMenuInterface *> m_arContextHandlers;
149 CPersonalDictionary m_personalDict;
150 bool m_bDoStyle;
151 int m_nAutoCompleteMinChars;
152 LruCache<std::wstring, BOOL> m_SpellingCache;
153 static bool IsValidURLChar(unsigned char ch);
154 protected:
155 virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult);
156 virtual BOOL PreTranslateMessage(MSG* pMsg);
157 void CheckSpelling(int startpos, int endpos);
158 void SuggestSpellingAlternatives(void);
159 void DoAutoCompletion(int nMinPrefixLength);
160 BOOL LoadDictionaries(LONG lLanguageID);
161 BOOL MarkEnteredBugID(int startstylepos, int endstylepos);
162 bool StyleEnteredText(int startstylepos, int endstylepos);
163 void StyleURLs(int startstylepos, int endstylepos);
164 bool WrapLines(int startpos, int endpos);
165 bool FindStyleChars(const char * line, char styler, int& start, int& end);
166 void AdvanceUTF8(const char * str, int& pos);
167 BOOL IsMisspelled(const CString& sWord);
168 DWORD GetStyleAt(int pos) { return (DWORD)Call(SCI_GETSTYLEAT, pos) & 0x1f; }
169 bool IsUrlOrEmail(const CStringA& sText);
170 CStringA GetWordForSpellChecker(const CString& sWord);
171 CString GetWordFromSpellChecker(const CStringA& sWordA);
173 virtual afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
174 afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/);
175 DECLARE_MESSAGE_MAP()