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