PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Utils/MiscUI/SciEdit.h

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