PageRenderTime 74ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/ScintillaNET/Scintilla.cs

https://bitbucket.org/nekokun/nekokun
C# | 3754 lines | 2491 code | 616 blank | 647 comment | 543 complexity | a1b3521f073405e8be9ce6667178001a MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. #region Using Directives
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Drawing.Design;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. using System.Security.Permissions;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. using ScintillaNET.Configuration;
  15. using ScintillaNET.Design;
  16. using ScintillaNET.Properties;
  17. #endregion Using Directives
  18. namespace ScintillaNET
  19. {
  20. /// <summary>
  21. /// Represents a Scintilla text editor control.
  22. /// </summary>
  23. [Designer(typeof(ScintillaDesigner))]
  24. [Docking(DockingBehavior.Ask)]
  25. [DefaultBindingProperty("Text")]
  26. [DefaultProperty("Text")]
  27. [DefaultEvent("DocumentChanged")]
  28. public partial class Scintilla : Control, INativeScintilla, ISupportInitialize
  29. {
  30. #region Fields
  31. private static IntPtr _moduleHandle;
  32. private static string _moduleName = (IntPtr.Size == 4 ? Resources.ModuleName : Resources.ModuleName64);
  33. private static NativeMethods.Scintilla_DirectFunction _directFunction;
  34. private IntPtr _directPointer;
  35. private static readonly object _annotationChangedEventKey = new object();
  36. private static readonly object _autoCompleteAcceptedEventKey = new object();
  37. private static readonly object _beforeTextDeleteEventKey = new object();
  38. private static readonly object _beforeTextInsertEventKey = new object();
  39. private static readonly object _borderStyleChangedEventKey = new object();
  40. private static readonly object _callTipClickEventKey = new object();
  41. private static readonly object _charAddedEventKey = new object();
  42. private static readonly object _documentChangeEventKey = new object();
  43. private static readonly object _dropMarkerCollectEventKey = new object();
  44. private static readonly object _dwellEndEventKey = new object();
  45. private static readonly object _dwellStartEventKey = new object();
  46. private static readonly object _fileDropEventKey = new object();
  47. private static readonly object _foldChangedEventKey = new object();
  48. private static readonly object _hotspotClickedEventKey = new object();
  49. private static readonly object _hotspotDoubleClickedEventKey = new object();
  50. private static readonly object _hotspotReleaseClickEventKey = new object();
  51. private static readonly object _indicatorClickEventKey = new object();
  52. private static readonly object _linesNeedShownEventKey = new object();
  53. private static readonly object _loadEventKey = new object();
  54. private static readonly object _macroRecordEventKey = new object();
  55. private static readonly object _marginClickEventKey = new object();
  56. private static readonly object _markerChangedEventKey = new object();
  57. private static readonly object _modifiedChangedEventKey = new object();
  58. private static readonly object _readOnlyModifyAttemptEventKey = new object();
  59. private static readonly object _scrollEventKey = new object();
  60. private static readonly object _selectionChangedEventKey = new object();
  61. private static readonly object _styleNeededEventKey = new object();
  62. private static readonly object _textDeletedEventKey = new object();
  63. private static readonly object _textInsertedEventKey = new object();
  64. private static readonly object _uriDroppedEventKey = new object();
  65. private static readonly object _userListEventKey = new object();
  66. private static readonly object _zoomChangedEventKey = new object();
  67. private static readonly int _modifiedState = BitVector32.CreateMask();
  68. private static readonly int _acceptsReturnState = BitVector32.CreateMask(_modifiedState);
  69. private static readonly int _acceptsTabState = BitVector32.CreateMask(_acceptsReturnState);
  70. private BitVector32 _state;
  71. private AnnotationCollection _annotations;
  72. private LineWrapping _lineWrapping;
  73. private List<TopLevelHelper> _helpers = new List<TopLevelHelper>();
  74. private AutoComplete _autoComplete;
  75. private CallTip _callTip;
  76. private CaretInfo _caret;
  77. private Clipboard _clipboard;
  78. private Commands _commands;
  79. private Configuration.ConfigurationManager _configurationManager;
  80. private DocumentHandler _documentHandler;
  81. private DocumentNavigation _documentNavigation;
  82. private DropMarkers _dropMarkers;
  83. private Encoding _encoding;
  84. private EndOfLine _endOfLine;
  85. private FindReplace _findReplace;
  86. private Folding _folding;
  87. private GoTo _goto;
  88. private HotspotStyle _hotspotStyle;
  89. private Indentation _indentation;
  90. private IndicatorCollection _indicators;
  91. private Lexing _lexing;
  92. private LineCollection _lines;
  93. private LongLines _longLines;
  94. private MarginCollection _margins;
  95. private MarkerCollection _markers;
  96. private Printing _printing;
  97. private Scrolling _scrolling;
  98. private Selection _selection;
  99. private SnippetManager _snippets;
  100. private StyleCollection _styles;
  101. private UndoRedo _undoRedo;
  102. private Whitespace _whitespace;
  103. private bool _allowDrop;
  104. private string _caption;
  105. private Dictionary<string, Color> _colorBag = new Dictionary<string, Color>();
  106. /// <summary>
  107. /// Enables the brace matching from current position.
  108. /// </summary>
  109. private bool _isBraceMatching = false;
  110. private bool _isCustomPaintingEnabled = true;
  111. private bool _isInitializing = false;
  112. private List<ManagedRange> _managedRanges = new List<ManagedRange>();
  113. private bool _matchBraces = true;
  114. private INativeScintilla _ns;
  115. private Hashtable _propertyBag = new Hashtable();
  116. private SearchFlags _searchFlags = SearchFlags.Empty;
  117. private bool _supressControlCharacters = true;
  118. // List of Scintilla Supported encodings
  119. internal static readonly IList<Encoding> ValidCodePages = new Encoding[]
  120. {
  121. Encoding.ASCII,
  122. Encoding.UTF8,
  123. Encoding.Unicode, // UTF-16
  124. Encoding.GetEncoding(932), // shift_jis - Japanese (Shift-JIS)
  125. Encoding.GetEncoding(936), // gb2312 - Chinese Simplified (GB2312)
  126. Encoding.GetEncoding(949), // ks_c_5601-1987 - Korean
  127. Encoding.GetEncoding(950), // big5 - Chinese Traditional (Big5)
  128. Encoding.GetEncoding(1361) // Johab - Korean (Johab)
  129. };
  130. // This has to be set *early* because CreateParams is called before our constructor
  131. private BorderStyle _borderStyle = BorderStyle.Fixed3D;
  132. #endregion Fields
  133. #region Methods
  134. /// <summary>
  135. /// Adds a line _end marker to the _end of the document
  136. /// </summary>
  137. public void AddLastLineEnd()
  138. {
  139. EndOfLineMode eolMode = _endOfLine.Mode;
  140. string eolMarker = "\r\n";
  141. if (eolMode == EndOfLineMode.CR)
  142. eolMarker = "\r";
  143. else if (eolMode == EndOfLineMode.LF)
  144. eolMarker = "\n";
  145. int tl = TextLength;
  146. int start = tl - eolMarker.Length;
  147. if (start < 0 || GetRange(start, start + eolMarker.Length).Text != eolMarker)
  148. AppendText(eolMarker);
  149. }
  150. /// <summary>
  151. /// Appends a copy of the specified string to the _end of this instance.
  152. /// </summary>
  153. /// <param name="text">The <see cref="String"/> to append.</param>
  154. /// <returns>A <see cref="Range"/> representing the appended text.</returns>
  155. public Range AppendText(string text)
  156. {
  157. int oldLength = TextLength;
  158. NativeInterface.AppendText(Encoding.GetByteCount(text), text);
  159. return GetRange(oldLength, TextLength);
  160. }
  161. public void BeginInit()
  162. {
  163. _isInitializing = true;
  164. }
  165. public char CharAt(int position)
  166. {
  167. return _ns.GetCharAt(position);
  168. }
  169. /// <summary>
  170. /// Creates and returns a new <see cref="AnnotationCollection" /> object.
  171. /// </summary>
  172. /// <returns>A new <see cref="AnnotationCollection" /> object.</returns>
  173. [EditorBrowsable(EditorBrowsableState.Advanced)]
  174. protected virtual AnnotationCollection CreateAnnotationsInstance()
  175. {
  176. return new AnnotationCollection(this);
  177. }
  178. /// <summary>
  179. /// Creates and returns a new <see cref="LineWrapping" /> object.
  180. /// </summary>
  181. /// <returns>A new <see cref="LineWrapping" /> object.</returns>
  182. [EditorBrowsable(EditorBrowsableState.Advanced)]
  183. protected virtual LineWrapping CreateLineWrappingInstance()
  184. {
  185. return new LineWrapping(this);
  186. }
  187. /// <summary>
  188. /// Sends the specified message directly to the native Scintilla window,
  189. /// bypassing any managed APIs.
  190. /// </summary>
  191. /// <param name="msg">The message ID.</param>
  192. /// <param name="wParam">The message <c>wparam</c> field.</param>
  193. /// <param name="lParam">The message <c>lparam</c> field.</param>
  194. /// <returns>An <see cref="IntPtr"/> representing the result of the message request.</returns>
  195. /// <remarks>
  196. /// Warning: The Surgeon General Has Determined that Calling the Underlying Scintilla
  197. /// Window Directly May Result in Unexpected Behavior!
  198. /// </remarks>
  199. /// <exception cref="InvalidOperationException">
  200. /// The method was called from a thread other than the thread it was created on.
  201. /// </exception>
  202. [EditorBrowsable(EditorBrowsableState.Advanced)]
  203. public IntPtr DirectMessage(int msg, IntPtr wParam, IntPtr lParam)
  204. {
  205. // Enforce illegal cross-thread calls
  206. if (Form.CheckForIllegalCrossThreadCalls && InvokeRequired)
  207. {
  208. string message = string.Format(Resources.Culture, Resources.Exception_IllegalCrossThreadCall, Name);
  209. throw new InvalidOperationException(message);
  210. }
  211. // Call the direct function delegate
  212. return _directFunction(_directPointer, msg, wParam, lParam);
  213. }
  214. /// <summary>
  215. /// Overridden. Releases the unmanaged resources used by the <see cref="Control" /> and
  216. /// its child controls and optionally releases the managed resources.
  217. /// </summary>
  218. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  219. protected override void Dispose(bool disposing)
  220. {
  221. foreach (ScintillaHelperBase heler in _helpers)
  222. {
  223. heler.Dispose();
  224. }
  225. if (disposing && IsHandleCreated)
  226. {
  227. // wi11811 2008-07-28 Chris Rickard
  228. // Since we eat the destroy message in WndProc
  229. // we have to manually let Scintilla know to
  230. // clean up its resources.
  231. Message destroyMessage = new Message();
  232. destroyMessage.Msg = NativeMethods.WM_DESTROY;
  233. destroyMessage.HWnd = Handle;
  234. base.DefWndProc(ref destroyMessage);
  235. }
  236. base.Dispose(disposing);
  237. }
  238. public void EndInit()
  239. {
  240. _isInitializing = false;
  241. foreach (ScintillaHelperBase helper in _helpers)
  242. {
  243. helper.Initialize();
  244. }
  245. }
  246. /// <summary>
  247. /// Exports a HTML representation of the current document.
  248. /// </summary>
  249. /// <returns>A <see cref="String"/> containing the contents of the document formatted as HTML.</returns>
  250. /// <remarks>Only ASCII documents are supported. Other encoding types have undefined behavior.</remarks>
  251. public string ExportHtml()
  252. {
  253. StringBuilder sb = new StringBuilder();
  254. using (StringWriter sw = new StringWriter(sb))
  255. ExportHtml(sw, "Untitled", false);
  256. return sb.ToString();
  257. }
  258. /// <summary>
  259. /// Exports a HTML representation of the current document.
  260. /// </summary>
  261. /// <param name="writer">The <see cref="TextWriter"/>with which to write. </param>
  262. /// <param name="title">The title of the HTML document.</param>
  263. /// <param name="allStyles">
  264. /// <c>true</c> to output all styles including those not
  265. /// used in the document; otherwise, <c>false</c>.
  266. /// </param>
  267. /// <remarks>Only ASCII documents are supported. Other encoding types have undefined behavior.</remarks>
  268. public void ExportHtml(TextWriter writer, string title, bool allStyles)
  269. {
  270. // Make sure the document is current
  271. // Lexing.Colorize();
  272. // Get the styles used
  273. int length = NativeInterface.GetLength();
  274. bool[] stylesUsed = new bool[(int)StylesCommon.Max + 1];
  275. if (allStyles)
  276. {
  277. for (int i = 0; i < stylesUsed.Length; i++)
  278. stylesUsed[i] = true;
  279. }
  280. else
  281. {
  282. // Record all the styles used
  283. for (int i = 0; i < length; i++)
  284. stylesUsed[Styles.GetStyleAt(i) & (int)StylesCommon.Max] = true;
  285. }
  286. // The tab width
  287. int tabWidth = Indentation.TabWidth;
  288. // Start writing
  289. writer.WriteLine(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">");
  290. writer.WriteLine("<html>");
  291. writer.WriteLine("<head>");
  292. writer.WriteLine("<title>{0}</title>", title);
  293. writer.WriteLine(@"<style type=""text/css"">");
  294. writer.WriteLine();
  295. // Write the body style
  296. writer.WriteLine("body {");
  297. writer.WriteLine("background-color: {0};", Utilities.ColorToHtml(Styles.Default.BackColor));
  298. if (LineWrapping.Mode == LineWrappingMode.None)
  299. writer.WriteLine("white-space: nowrap;");
  300. writer.WriteLine("}");
  301. writer.WriteLine();
  302. // Write the styles
  303. for (int i = 0; i < stylesUsed.Length; i++)
  304. {
  305. if (!stylesUsed[i])
  306. continue;
  307. Style s = Styles[i];
  308. writer.WriteLine("span.s{0} {{", i);
  309. writer.WriteLine("font-family: \"" + s.FontName + "\";");
  310. writer.WriteLine("font-size: {0}pt;", s.Size);
  311. if (s.Italic)
  312. writer.WriteLine("font-style: italic;");
  313. if (s.Bold)
  314. writer.WriteLine("font-weight: bold;");
  315. if (!s.ForeColor.IsEmpty && s.ForeColor != Color.Transparent)
  316. writer.WriteLine("color: {0};", Utilities.ColorToHtml(s.ForeColor));
  317. if (!s.BackColor.IsEmpty && s.BackColor != Color.Transparent)
  318. writer.WriteLine("background-color: {0};", Utilities.ColorToHtml(s.BackColor));
  319. writer.WriteLine("}");
  320. writer.WriteLine();
  321. }
  322. writer.WriteLine("</style>");
  323. writer.WriteLine("</head>");
  324. writer.WriteLine("<body>");
  325. // Write the document
  326. // TODO There's more to be done here to support codepages/UTF-8
  327. char lc;
  328. char c = '\0';
  329. int lastStyle = -1;
  330. for (int i = 0; i < length; i++)
  331. {
  332. lc = c;
  333. c = NativeInterface.GetCharAt(i);
  334. int style = Styles.GetStyleAt(i);
  335. if(style != lastStyle)
  336. {
  337. if(lastStyle != -1)
  338. writer.Write("</span>");
  339. writer.Write(@"<span class=""s{0}"">", style);
  340. lastStyle = style;
  341. }
  342. switch (c)
  343. {
  344. case '\0':
  345. continue;
  346. case ' ':
  347. if (lc == ' ')
  348. writer.Write("&nbsp;");
  349. else
  350. writer.Write(c);
  351. continue;
  352. case '\t':
  353. for (int t = 0; t < tabWidth; t++)
  354. writer.Write("&nbsp; ");
  355. continue;
  356. case '\r':
  357. case '\n':
  358. if (c == '\r' && i < length - 1 && NativeInterface.GetCharAt(i + 1) == '\n')
  359. i++;
  360. if (lastStyle != -1)
  361. writer.Write("</span>");
  362. writer.WriteLine("<br />");
  363. lastStyle = -1;
  364. continue;
  365. case '<':
  366. writer.Write("&lt;");
  367. continue;
  368. case '>':
  369. writer.Write("&gt;");
  370. continue;
  371. case '&':
  372. writer.Write("&amp;");
  373. continue;
  374. default:
  375. writer.Write(c);
  376. continue;
  377. }
  378. }
  379. if (lastStyle != -1)
  380. writer.Write("</span>");
  381. writer.WriteLine();
  382. writer.WriteLine("</body>");
  383. writer.WriteLine("</html>");
  384. }
  385. public int FindColumn(int line, int column)
  386. {
  387. return _ns.FindColumn(line, column);
  388. }
  389. internal void FireCallTipClick(int arrow)
  390. {
  391. CallTipArrow a = (CallTipArrow)arrow;
  392. OverloadList ol = CallTip.OverloadList;
  393. CallTipClickEventArgs e;
  394. if (ol == null)
  395. {
  396. e = new CallTipClickEventArgs(a, -1, -1, null, CallTip.HighlightStart, CallTip.HighlightEnd);
  397. }
  398. else
  399. {
  400. int newIndex = ol.CurrentIndex;
  401. if (a == CallTipArrow.Down)
  402. {
  403. if (ol.CurrentIndex == ol.Count - 1)
  404. newIndex = 0;
  405. else
  406. newIndex++;
  407. }
  408. else if (a == CallTipArrow.Up)
  409. {
  410. if (ol.CurrentIndex == 0)
  411. newIndex = ol.Count - 1;
  412. else
  413. newIndex--;
  414. }
  415. e = new CallTipClickEventArgs(a, ol.CurrentIndex, newIndex, ol, CallTip.HighlightStart, CallTip.HighlightEnd);
  416. }
  417. OnCallTipClick(e);
  418. if (e.Cancel)
  419. {
  420. CallTip.Cancel();
  421. }
  422. else
  423. {
  424. if (ol != null)
  425. {
  426. // We allow them to alse replace the list entirely or just
  427. // manipulate the New Index
  428. CallTip.OverloadList = e.OverloadList;
  429. CallTip.OverloadList.CurrentIndex = e.NewIndex;
  430. CallTip.ShowOverloadInternal();
  431. }
  432. CallTip.HighlightStart = e.HighlightStart;
  433. CallTip.HighlightEnd = e.HighlightEnd;
  434. }
  435. }
  436. internal void FireKeyDown(KeyEventArgs e)
  437. {
  438. OnKeyDown(e);
  439. }
  440. internal void FireMarginClick(SCNotification n)
  441. {
  442. Margin m = Margins[n.margin];
  443. Keys k = Keys.None;
  444. if ((n.modifiers & (int)KeyMod.Alt) == (int)KeyMod.Alt)
  445. k |= Keys.Alt;
  446. if ((n.modifiers & (int)KeyMod.Ctrl) == (int)KeyMod.Ctrl)
  447. k |= Keys.Control;
  448. if ((n.modifiers & (int)KeyMod.Shift) == (int)KeyMod.Shift)
  449. k |= Keys.Shift;
  450. OnMarginClick(new MarginClickEventArgs(k, n.position, Lines.FromPosition(n.position), m, m.AutoToggleMarkerNumber, m.IsFoldMargin));
  451. }
  452. public int GetColumn(int position)
  453. {
  454. return _ns.GetColumn(position);
  455. }
  456. /// <summary>
  457. /// Gets the text of the line containing the caret.
  458. /// </summary>
  459. /// <returns>A <see cref="String" /> representing the text of the line containing the caret.</returns>
  460. public unsafe string GetCurrentLine()
  461. {
  462. int tmp;
  463. return GetCurrentLine(out tmp);
  464. }
  465. /// <summary>
  466. /// Gets the text of the line containing the caret and the current caret position within that line.
  467. /// </summary>
  468. /// <param name="caretPosition">When this method returns, contains the byte offset of the current caret position with the line.</param>
  469. /// <returns>A <see cref="String" /> representing the text of the line containing the caret.</returns>
  470. public unsafe string GetCurrentLine(out int caretPosition)
  471. {
  472. int length = DirectMessage(NativeMethods.SCI_GETCURLINE, IntPtr.Zero, IntPtr.Zero).ToInt32();
  473. byte[] buffer = new byte[length];
  474. fixed (byte* bp = buffer)
  475. caretPosition = DirectMessage(NativeMethods.SCI_GETCURLINE, new IntPtr(buffer.Length), new IntPtr(bp)).ToInt32();
  476. return Encoding.GetString(buffer, 0, length - 1);
  477. }
  478. public Range GetRange()
  479. {
  480. return new Range(0, _ns.GetTextLength(), this);
  481. }
  482. public Range GetRange(int position)
  483. {
  484. return new Range(position, position + 1, this);
  485. }
  486. public Range GetRange(int startPosition, int endPosition)
  487. {
  488. return new Range(startPosition, endPosition, this);
  489. }
  490. /// <summary>
  491. /// Gets a word from the specified position
  492. /// </summary>
  493. public string GetWordFromPosition(int position)
  494. {
  495. // Chris Rickard 2008-07-28
  496. // Fixing implementation to actually return the word at the position...
  497. // Credit goes to Stumpii for the code.
  498. // As a side note: I think the previous code was implemented based off
  499. // some funky code I made for the snippet keyword detection, but since
  500. // it doesn't reference this method there's no reason to keep the buggy
  501. // behavior. I also removed the try..catch because in theory this
  502. // shouldn't throw and we REALLY shouldn't be eating exceptions at the
  503. // System.Exception level. If some _start popping up I can add some
  504. // conditionals or catch more specific Exceptions.
  505. int startPosition = NativeInterface.WordStartPosition(position, true);
  506. int endPosition = NativeInterface.WordEndPosition(position, true);
  507. return GetRange(startPosition, endPosition).Text;
  508. }
  509. private void HandleFileDrop(IntPtr hDrop)
  510. {
  511. StringBuilder buffer = null;
  512. uint nfiles = NativeMethods.DragQueryFile(hDrop, 0xffffffff, buffer, 0);
  513. List<string> files = new List<string>();
  514. for (uint i = 0; i < nfiles; i++)
  515. {
  516. buffer = new StringBuilder(512);
  517. NativeMethods.DragQueryFile(hDrop, i, buffer, 512);
  518. files.Add(buffer.ToString());
  519. }
  520. NativeMethods.DragFinish(hDrop);
  521. OnFileDrop(new FileDropEventArgs(files.ToArray()));
  522. }
  523. /// <summary>
  524. /// Inserts text at the current cursor position
  525. /// </summary>
  526. /// <param name="text">Text to insert</param>
  527. /// <returns>The range inserted</returns>
  528. public Range InsertText(string text)
  529. {
  530. NativeInterface.AddText(Encoding.GetByteCount(text), text);
  531. return GetRange(_caret.Position, Encoding.GetByteCount(text));
  532. }
  533. /// <summary>
  534. /// Inserts text at the given position
  535. /// </summary>
  536. /// <param name="position">The position to insert text in</param>
  537. /// <param name="text">Text to insert</param>
  538. /// <returns>The text range inserted</returns>
  539. public Range InsertText(int position, string text)
  540. {
  541. NativeInterface.InsertText(position, text);
  542. return GetRange(position, Encoding.GetByteCount(text));
  543. }
  544. /// <summary>
  545. /// Overridden. See <see cref="Control.IsInputKey"/>.
  546. /// </summary>
  547. protected override bool IsInputKey(Keys keyData)
  548. {
  549. if ((keyData & Keys.Shift) != Keys.None)
  550. keyData ^= Keys.Shift;
  551. switch (keyData)
  552. {
  553. case Keys.Tab:
  554. return _state[_acceptsTabState];
  555. case Keys.Enter:
  556. return _state[_acceptsReturnState];
  557. case Keys.Up:
  558. case Keys.Down:
  559. case Keys.Left:
  560. case Keys.Right:
  561. case Keys.F:
  562. return true;
  563. }
  564. return base.IsInputKey(keyData);
  565. }
  566. private static void LoadModule()
  567. {
  568. if (_moduleHandle == IntPtr.Zero)
  569. {
  570. // Load the Scintilla module into memory
  571. if ((_moduleHandle = NativeMethods.LoadLibrary(_moduleName)) == IntPtr.Zero)
  572. {
  573. string message = string.Format(Resources.Culture, Resources.Exception_CannotLoadModule, _moduleName);
  574. throw new Win32Exception(message, new Win32Exception(Marshal.GetLastWin32Error()));
  575. }
  576. // Get the direct function. We use GetProcAddress instead of DllImport
  577. // because we don't know the name of the module ahead of time.
  578. _directFunction = Marshal.GetDelegateForFunctionPointer(
  579. NativeMethods.GetProcAddress(_moduleHandle, "Scintilla_DirectFunction"),
  580. typeof(NativeMethods.Scintilla_DirectFunction)) as NativeMethods.Scintilla_DirectFunction;
  581. if (_directFunction == null)
  582. {
  583. string message = string.Format(Resources.Culture, Resources.Exception_InvalidModule, _moduleName);
  584. throw new Win32Exception(message, new Win32Exception(Marshal.GetLastWin32Error()));
  585. }
  586. }
  587. }
  588. private List<ManagedRange> ManagedRangesInRange(int firstPos, int lastPos)
  589. {
  590. // TODO: look into optimizing this so that it isn't a linear
  591. // search. This is fine for a few markers per document but
  592. // can be greatly improved if there are a large # of markers
  593. List<ManagedRange> ret = new List<ManagedRange>();
  594. foreach (ManagedRange mr in _managedRanges)
  595. if (mr.Start >= firstPos && mr.Start <= lastPos)
  596. ret.Add(mr);
  597. return ret;
  598. }
  599. /// <summary>
  600. /// Raises the <see cref="AnnotationChanged" /> event.
  601. /// </summary>
  602. /// <param name="e">An <see cref="AnnotationChangedEventArgs" /> that contains the event data.</param>
  603. protected virtual void OnAnnotationChanged(AnnotationChangedEventArgs e)
  604. {
  605. EventHandler<AnnotationChangedEventArgs> handler = Events[_annotationChangedEventKey] as EventHandler<AnnotationChangedEventArgs>;
  606. if (handler != null)
  607. handler(this, e);
  608. }
  609. /// <summary>
  610. /// Raises the <see cref="AutoCompleteAccepted"/> event.
  611. /// </summary>
  612. /// <param name="e">An <see cref="AutoCompleteAcceptedEventArgs"/> that contains the event data.</param>
  613. protected virtual void OnAutoCompleteAccepted(AutoCompleteAcceptedEventArgs e)
  614. {
  615. EventHandler<AutoCompleteAcceptedEventArgs> handler = Events[_autoCompleteAcceptedEventKey] as EventHandler<AutoCompleteAcceptedEventArgs>;
  616. if (handler != null)
  617. handler(this, e);
  618. if (e.Cancel)
  619. AutoComplete.Cancel();
  620. }
  621. /// <summary>
  622. /// Raises the <see cref="BackColorChanged"/> event.
  623. /// </summary>
  624. /// <param name="e">An <see cref="EventArgs"/> that contains the event data. </param>
  625. protected override void OnBackColorChanged(EventArgs e)
  626. {
  627. ResetStyles();
  628. base.OnBackColorChanged(e);
  629. }
  630. /// <summary>
  631. /// Raises the <see cref="BeforeTextDelete"/> event.
  632. /// </summary>
  633. /// <param name="e">An <see cref="TextModifiedEventArgs"/> that contains the event data.</param>
  634. protected virtual void OnBeforeTextDelete(TextModifiedEventArgs e)
  635. {
  636. int firstPos = e.Position;
  637. int lastPos = firstPos + e.Length;
  638. List<ManagedRange> deletedRanges = new List<ManagedRange>();
  639. foreach (ManagedRange mr in _managedRanges)
  640. {
  641. // These ranges lie within the deleted range so
  642. // the ranges themselves need to be deleted
  643. if (mr.Start >= firstPos && mr.End <= lastPos)
  644. {
  645. // If the entire range is being delete and NOT a superset of the range,
  646. // don't delete it, only collapse it.
  647. if (!mr.IsPoint && e.Position == mr.Start && (e.Position + e.Length == mr.End))
  648. {
  649. mr.Change(mr.Start, mr.Start);
  650. }
  651. else
  652. {
  653. // Notify the virtual Range that it needs to cleanup
  654. mr.Change(-1, -1);
  655. // Mark for deletion after this foreach:
  656. deletedRanges.Add(mr);
  657. }
  658. }
  659. else if (mr.Start >= lastPos)
  660. {
  661. // These ranges are merely offset by the deleted range
  662. mr.Change(mr.Start - e.Length, mr.End - e.Length);
  663. }
  664. else if (mr.Start >= firstPos && mr.Start <= lastPos)
  665. {
  666. // The left side of the managed range is getting
  667. // cut off
  668. mr.Change(firstPos, mr.End - e.Length);
  669. }
  670. else if (mr.Start < firstPos && mr.End >= firstPos && mr.End >= lastPos)
  671. {
  672. mr.Change(mr.Start, mr.End - e.Length);
  673. }
  674. else if (mr.Start < firstPos && mr.End >= firstPos && mr.End < lastPos)
  675. {
  676. mr.Change(mr.Start, firstPos);
  677. }
  678. }
  679. foreach (ManagedRange mr in deletedRanges)
  680. mr.Dispose();
  681. EventHandler<TextModifiedEventArgs> handler = Events[_beforeTextDeleteEventKey] as EventHandler<TextModifiedEventArgs>;
  682. if (handler != null)
  683. handler(this, e);
  684. }
  685. /// <summary>
  686. /// Raises the <see cref="BeforeTextInsert"/> event.
  687. /// </summary>
  688. /// <param name="e">An <see cref="TextModifiedEventArgs"/> that contains the event data.</param>
  689. protected virtual void OnBeforeTextInsert(TextModifiedEventArgs e)
  690. {
  691. List<ManagedRange> offsetRanges = new List<ManagedRange>();
  692. foreach (ManagedRange mr in _managedRanges)
  693. {
  694. if (mr.Start == e.Position && mr.PendingDeletion)
  695. {
  696. mr.PendingDeletion = false;
  697. ManagedRange lmr = mr;
  698. BeginInvoke(new MethodInvoker(delegate() { lmr.Change(e.Position, e.Position + e.Length); }));
  699. }
  700. // If the Range is a single point we treat it slightly
  701. // differently than a spanned range
  702. if (mr.IsPoint)
  703. {
  704. // Unlike a spanned range, if the insertion point of
  705. // the new text == the _start of the range (and thus
  706. // the _end as well) we offset the entire point.
  707. if (mr.Start >= e.Position)
  708. mr.Change(mr.Start + e.Length, mr.End + e.Length);
  709. else if (mr.End >= e.Position)
  710. mr.Change(mr.Start, mr.End + e.Length);
  711. }
  712. else
  713. {
  714. // We offset a spanned range entirely only if the
  715. // _start occurs after the insertion point of the new
  716. // text.
  717. if (mr.Start > e.Position)
  718. mr.Change(mr.Start + e.Length, mr.End + e.Length);
  719. else if (mr.End >= e.Position)
  720. {
  721. // However it the _start of the range == the insertion
  722. // point of the new text instead of offestting the
  723. // range we expand it.
  724. mr.Change(mr.Start, mr.End + e.Length);
  725. }
  726. }
  727. }
  728. EventHandler<TextModifiedEventArgs> handler = Events[_beforeTextInsertEventKey] as EventHandler<TextModifiedEventArgs>;
  729. if (handler != null)
  730. handler(this, e);
  731. }
  732. /// <summary>
  733. /// Raises the <see cref="BorderStyleChanged" /> event.
  734. /// </summary>
  735. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  736. protected virtual void OnBorderStyleChanged(EventArgs e)
  737. {
  738. EventHandler handler = Events[_borderStyleChangedEventKey] as EventHandler;
  739. if (handler != null)
  740. handler(this, e);
  741. }
  742. /// <summary>
  743. /// Raises the <see cref="CallTipClick"/> event.
  744. /// </summary>
  745. /// <param name="e">An <see cref="CallTipClickEventArgs"/> that contains the event data.</param>
  746. protected virtual void OnCallTipClick(CallTipClickEventArgs e)
  747. {
  748. EventHandler<CallTipClickEventArgs> handler = Events[_callTipClickEventKey] as EventHandler<CallTipClickEventArgs>;
  749. if (handler != null)
  750. handler(this, e);
  751. }
  752. /// <summary>
  753. /// Raises the <see cref="CharAdded"/> event.
  754. /// </summary>
  755. /// <param name="e">An <see cref="CharAddedEventArgs"/> that contains the event data.</param>
  756. protected virtual void OnCharAdded(CharAddedEventArgs e)
  757. {
  758. EventHandler<CharAddedEventArgs> handler = Events[_charAddedEventKey] as EventHandler<CharAddedEventArgs>;
  759. if (handler != null)
  760. handler(this, e);
  761. if (_indentation.SmartIndentType != SmartIndent.None)
  762. _indentation.CheckSmartIndent(e.Ch);
  763. }
  764. /// <summary>
  765. /// Overridden. See <see cref="Control.OnCreateControl"/>.
  766. /// </summary>
  767. protected override void OnCreateControl()
  768. {
  769. base.OnCreateControl();
  770. OnLoad(EventArgs.Empty);
  771. }
  772. /// <summary>
  773. /// Raises the <see cref="DocumentChange"/> event.
  774. /// </summary>
  775. /// <param name="e">An <see cref="NativeScintillaEventArgs"/> that contains the event data.</param>
  776. protected virtual void OnDocumentChange(NativeScintillaEventArgs e)
  777. {
  778. EventHandler<NativeScintillaEventArgs> handler = Events[_documentChangeEventKey] as EventHandler<NativeScintillaEventArgs>;
  779. if (handler != null)
  780. handler(this, e);
  781. }
  782. /// <summary>
  783. /// Provides the support for code block selection
  784. /// </summary>
  785. protected override void OnDoubleClick(EventArgs e)
  786. {
  787. base.OnDoubleClick(e);
  788. if (_isBraceMatching)
  789. {
  790. int position = CurrentPos - 1,
  791. bracePosStart = -1,
  792. bracePosEnd = -1;
  793. char character = (char)CharAt(position);
  794. switch (character)
  795. {
  796. case '{':
  797. case '(':
  798. case '[':
  799. if (!this.PositionIsOnComment(position))
  800. {
  801. bracePosStart = position;
  802. bracePosEnd = _ns.BraceMatch(position, 0) + 1;
  803. _selection.Start = bracePosStart;
  804. _selection.End = bracePosEnd;
  805. }
  806. break;
  807. }
  808. }
  809. }
  810. /// <summary>
  811. /// Raises the <see cref="DropMarkerCollect"/> event.
  812. /// </summary>
  813. /// <param name="e">An <see cref="DropMarkerCollectEventArgs"/> that contains the event data.</param>
  814. protected internal virtual void OnDropMarkerCollect(DropMarkerCollectEventArgs e)
  815. {
  816. EventHandler<DropMarkerCollectEventArgs> handler = Events[_dropMarkerCollectEventKey] as EventHandler<DropMarkerCollectEventArgs>;
  817. if (handler != null)
  818. handler(this, e);
  819. }
  820. /// <summary>
  821. /// Raises the <see cref="DwellEnd"/> event.
  822. /// </summary>
  823. /// <param name="e">An <see cref="ScintillaMouseEventArgs"/> that contains the event data.</param>
  824. protected virtual void OnDwellEnd(ScintillaMouseEventArgs e)
  825. {
  826. EventHandler<ScintillaMouseEventArgs> handler = Events[_dwellEndEventKey] as EventHandler<ScintillaMouseEventArgs>;
  827. if (handler != null)
  828. handler(this, e);
  829. }
  830. /// <summary>
  831. /// Raises the <see cref="DwellStart"/> event.
  832. /// </summary>
  833. /// <param name="e">An <see cref="ScintillaMouseEventArgs"/> that contains the event data.</param>
  834. protected virtual void OnDwellStart(ScintillaMouseEventArgs e)
  835. {
  836. EventHandler<ScintillaMouseEventArgs> handler = Events[_dwellStartEventKey] as EventHandler<ScintillaMouseEventArgs>;
  837. if (handler != null)
  838. handler(this, e);
  839. }
  840. /// <summary>
  841. /// Raises the <see cref="FileDrop"/> event.
  842. /// </summary>
  843. /// <param name="e">An <see cref="FileDropEventArgs"/> that contains the event data.</param>
  844. protected virtual void OnFileDrop(FileDropEventArgs e)
  845. {
  846. EventHandler<FileDropEventArgs> handler = Events[_fileDropEventKey] as EventHandler<FileDropEventArgs>;
  847. if (handler != null)
  848. handler(this, e);
  849. }
  850. /// <summary>
  851. /// Raises the <see cref="FoldChanged"/> event.
  852. /// </summary>
  853. /// <param name="e">An <see cref="FoldChangedEventArgs"/> that contains the event data.</param>
  854. protected virtual void OnFoldChanged(FoldChangedEventArgs e)
  855. {
  856. EventHandler<FoldChangedEventArgs> handler = Events[_foldChangedEventKey] as EventHandler<FoldChangedEventArgs>;
  857. if (handler != null)
  858. handler(this, e);
  859. }
  860. /// <summary>
  861. /// Raises the <see cref="FontChanged"/> event.
  862. /// </summary>
  863. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  864. protected override void OnFontChanged(EventArgs e)
  865. {
  866. ResetStyles();
  867. base.OnFontChanged(e);
  868. }
  869. /// <summary>
  870. /// Raises the <see cref="ForeColorChanged"/> event.
  871. /// </summary>
  872. /// <param name="e">An <see cref="EventArgs"/> that contains the event data. </param>
  873. protected override void OnForeColorChanged(EventArgs e)
  874. {
  875. ResetStyles();
  876. base.OnForeColorChanged(e);
  877. }
  878. /// <summary>
  879. /// Overridden. See <see cref="Control.OnGotFocus"/>.
  880. /// </summary>
  881. protected override void OnGotFocus(EventArgs e)
  882. {
  883. if (!Selection.Hidden)
  884. _ns.HideSelection(false);
  885. _ns.SetSelBack(Selection.BackColor != Color.Transparent, Utilities.ColorToRgb(Selection.BackColor));
  886. _ns.SetSelFore(Selection.ForeColor != Color.Transparent, Utilities.ColorToRgb(Selection.ForeColor));
  887. base.OnGotFocus(e);
  888. }
  889. /// <summary>
  890. /// Overridden. Raises the <see cref="Control.HandleCreated"/> event.
  891. /// </summary>
  892. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  893. protected override void OnHandleCreated(EventArgs e)
  894. {
  895. // TODO Recreating handle?
  896. // Get the Scintilla direct pointer
  897. _directPointer = NativeMethods.SendMessage(Handle, NativeMethods.SCI_GETDIRECTPOINTER, IntPtr.Zero, IntPtr.Zero);
  898. if (_directPointer == IntPtr.Zero)
  899. throw new Win32Exception(Resources.Exception_CannotCreateDirectFunction);
  900. base.OnHandleCreated(e);
  901. }
  902. /// <summary>
  903. /// Raises the <see cref="HotspotClick"/> event.
  904. /// </summary>
  905. /// <param name="e">A <see cref="HotspotClickEventArgs"/> that contains the event data.</param>
  906. protected virtual void OnHotspotClick(HotspotClickEventArgs e)
  907. {
  908. EventHandler<HotspotClickEventArgs> handler = Events[_hotspotClickEventKey] as EventHandler<HotspotClickEventArgs>;
  909. if (handler != null)
  910. handler(this, e);
  911. }
  912. /// <summary>
  913. /// Raises the <see cref="HotspotDoubleClick"/> event.
  914. /// </summary>
  915. /// <param name="e">A <see cref="HotspotClickEventArgs"/> that contains the event data.</param>
  916. protected virtual void OnHotspotDoubleClick(HotspotClickEventArgs e)
  917. {
  918. EventHandler<HotspotClickEventArgs> handler = Events[_hotspotDoubleClickEventKey] as EventHandler<HotspotClickEventArgs>;
  919. if (handler != null)
  920. handler(this, e);
  921. }
  922. /// <summary>
  923. /// Raises the <see cref="HotspotReleaseClick"/> event.
  924. /// </summary>
  925. /// <param name="e">A <see cref="HotspotClickEventArgs"/> that contains the event data.</param>
  926. protected virtual void OnHotspotReleaseClick(HotspotClickEventArgs e)
  927. {
  928. EventHandler<HotspotClickEventArgs> handler = Events[_hotspotReleaseClickEventKey] as EventHandler<HotspotClickEventArgs>;
  929. if (handler != null)
  930. handler(this, e);
  931. }
  932. /// <summary>
  933. /// Raises the <see cref="IndicatorClick"/> event.
  934. /// </summary>
  935. /// <param name="e">An <see cref="ScintillaMouseEventArgs"/> that contains the event data.</param>
  936. protected virtual void OnIndicatorClick(ScintillaMouseEventArgs e)
  937. {
  938. EventHandler<ScintillaMouseEventArgs> handler = Events[_indicatorClickEventKey] as EventHandler<ScintillaMouseEventArgs>;
  939. if (handler != null)
  940. handler(this, e);
  941. }
  942. /// <summary>
  943. /// Overridden. See <see cref="Control.OnKeyDown"/>.
  944. /// </summary>
  945. protected override void OnKeyDown(KeyEventArgs e)
  946. {
  947. base.OnKeyDown(e);
  948. if (!e.Handled)
  949. e.SuppressKeyPress = _commands.ProcessKey(e);
  950. }
  951. /// <summary>
  952. /// Overridden. See <see cref="Control.OnKeyPress"/>.
  953. /// </summary>
  954. protected override void OnKeyPress(KeyPressEventArgs e)
  955. {
  956. if (_supressControlCharacters && (int)e.KeyChar < 32)
  957. e.Handled = true;
  958. if (_snippets.IsEnabled && _snippets.IsOneKeySelectionEmbedEnabled && _selection.Length > 0)
  959. {
  960. Snippet s;
  961. if (_snippets.List.TryGetValue(e.KeyChar.ToString(), out s))
  962. {
  963. if (s.IsSurroundsWith)
  964. {
  965. _snippets.InsertSnippet(s);
  966. e.Handled = true;
  967. }
  968. }
  969. }
  970. base.OnKeyPress(e);
  971. }
  972. /// <summary>
  973. /// Raises the <see cref="LinesNeedShown"/> event.
  974. /// </summary>
  975. /// <param name="e">An <see cref="LinesNeedShownEventArgs"/> that contains the event data.</param>
  976. protected virtual void OnLinesNeedShown(LinesNeedShownEventArgs e)
  977. {
  978. EventHandler<LinesNeedShownEventArgs> handler = Events[_linesNeedShownEventKey] as EventHandler<LinesNeedShownEventArgs>;
  979. if (handler != null)
  980. handler(this, e);
  981. }
  982. /// <summary>
  983. /// Raises the <see cref="Load"/> event.
  984. /// </summary>
  985. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  986. protected virtual void OnLoad(EventArgs e)
  987. {
  988. EventHandler handler = Events[_loadEventKey] as EventHandler;
  989. if (handler != null)
  990. handler(this, e);
  991. }
  992. /// <summary>
  993. /// Overridden. See <see cref="Control.OnLostFocus"/>.
  994. /// </summary>
  995. protected override void OnLostFocus(EventArgs e)
  996. {
  997. if (Selection.HideSelection)
  998. _ns.HideSelection(true);
  999. _ns.SetSelBack(Selection.BackColorUnfocused != Color.Transparent, Utilities.ColorToRgb(Selection.BackColorUnfocused));
  1000. _ns.SetSelFore(Selection.ForeColorUnfocused != Color.Transparent, Utilities.ColorToRgb(Selection.ForeColorUnfocused));
  1001. base.OnLostFocus(e);
  1002. }
  1003. /// <summary>
  1004. /// Raises the <see cref="MacroRecord"/> event.
  1005. /// </summary>
  1006. /// <param name="e">An <see cref="MacroRecordEventArgs"/> that contains the event data.</param>
  1007. protected virtual void OnMacroRecord(MacroRecordEventArgs e)
  1008. {
  1009. EventHandler<MacroRecordEventArgs> handler = Events[_macroRecordEventKey] as EventHandler<MacroRecordEventArgs>;
  1010. if (handler != null)
  1011. handler(this, e);
  1012. }
  1013. /// <summary>
  1014. /// Raises the <see cref="MarginClick"/> event.
  1015. /// </summary>
  1016. /// <param name="e">An <see cref="MarginClickEventArgs"/> that contains the event data.</param>
  1017. protected virtual void OnMarginClick(MarginClickEventArgs e)
  1018. {
  1019. EventHandler<MarginClickEventArgs> handler = Events[_marginClickEventKey] as EventHandler<MarginClickEventArgs>;
  1020. if (handler != null)
  1021. handler(this, e);
  1022. if (e.ToggleMarkerNumber >= 0)
  1023. {
  1024. int mask = (int)Math.Pow(2, e.ToggleMarkerNumber);
  1025. if ((e.Line.GetMarkerMask() & mask) == mask)
  1026. e.Line.DeleteMarker(e.ToggleMarkerNumber);
  1027. else
  1028. e.Line.AddMarker(e.ToggleMarkerNumber);
  1029. }
  1030. if (e.ToggleFold)
  1031. e.Line.ToggleFoldExpanded();
  1032. }
  1033. /// <summary>
  1034. /// Raises the <see cref="MarkerChanged"/> event.
  1035. /// </summary>
  1036. /// <param name="e">An <see cref="MarkerChangedEventArgs"/> that contains the event data.</param>
  1037. protected virtual void OnMarkerChanged(MarkerChangedEventArgs e)
  1038. {
  1039. EventHandler<MarkerChangedEventArgs> handler = Events[_markerChangedEventKey] as EventHandler<MarkerChangedEventArgs>;
  1040. if (handler != null)
  1041. handler(this, e);
  1042. }
  1043. /// <summary>
  1044. /// Raises the <see cref="ModifiedChanged"/> event.
  1045. /// </summary>
  1046. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  1047. protected virtual void OnModifiedChanged(EventArgs e)
  1048. {
  1049. EventHandler handler = Events[_modifiedChangedEventKey] as EventHandler;
  1050. if (handler != null)
  1051. handler(this, e);
  1052. }
  1053. /// <summary>
  1054. /// Overridden. See <see cref="Control.OnPaint"/>.
  1055. /// </summary>
  1056. protected override void OnPaint(PaintEventArgs e)
  1057. {
  1058. base.OnPaint(e);
  1059. paintRanges(e.Graphics);
  1060. }
  1061. /// <summary>
  1062. /// Raises the <see cref="ReadOnlyModifyAttempt"/> event.
  1063. /// </summary>
  1064. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  1065. protected virtual void OnReadOnlyModifyAttempt(EventArgs e)
  1066. {
  1067. EventHandler handler = Events[_readOnlyModifyAttemptEventKey] as EventHandler;
  1068. if (handler != null)
  1069. handler(this, e);
  1070. }
  1071. /// <summary>
  1072. /// Raises the <see cref="Scroll"/> event.
  1073. /// </summary>
  1074. /// <param name="e">An <see cref="ScrollEventArgs"/> that contains the event data.</param>
  1075. protected virtual void OnScroll(ScrollEventArgs e)
  1076. {
  1077. EventHandler<ScrollEventArgs> handler = Events[_scrollEventKey] as EventHandler<ScrollEventArgs>;
  1078. if (handler != null)
  1079. handler(this, e);
  1080. }
  1081. /// <summary>
  1082. /// Raises the <see cref="SelectionChanged"/> event.
  1083. /// </summary>
  1084. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  1085. protected virtual void OnSelectionChanged(EventArgs e)
  1086. {
  1087. //this is being fired in tandem with the cursor blink...
  1088. EventHandler handler = Events[_selectionChangedEventKey] as EventHandler;
  1089. if (handler != null)
  1090. handler(this, e);
  1091. if (_isBraceMatching && (_selection.Length == 0))
  1092. {
  1093. int position = CurrentPos - 1,
  1094. bracePosStart = -1,
  1095. bracePosEnd = -1;
  1096. char character = (char)CharAt(position);
  1097. switch (character)
  1098. {
  1099. case '{':
  1100. case '}':
  1101. case '(':
  1102. case ')':
  1103. case '[':
  1104. case ']':
  1105. if (!this.PositionIsOnComment(position))
  1106. {
  1107. bracePosStart = position;
  1108. bracePosEnd = _ns.BraceMatch(position,0);
  1109. if(bracePosEnd >= 0)
  1110. {
  1111. _ns.BraceHighlight(bracePosStart, bracePosEnd);
  1112. }
  1113. else
  1114. {
  1115. _ns.BraceBadLight(bracePosStart);
  1116. }
  1117. }
  1118. break;
  1119. default:
  1120. position = CurrentPos;
  1121. character = (char)CharAt(position); //this is not being used anywhere... --Cory
  1122. _ns.BraceHighlight(bracePosStart, bracePosEnd);
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. /// <summary>
  1128. /// Raises the <see cref="StyleNeeded"/> event.
  1129. /// </summary>
  1130. /// <param name="e">An <see cref="StyleNeededEventArgs"/> that contains the event data.</param>
  1131. protected virtual void OnStyleNeeded(StyleNeededEventArgs e)
  1132. {
  1133. EventHandler<StyleNeededEventArgs> handler = Events[_styleNeededEventKey] as EventHandler<StyleNeededEventArgs>;
  1134. if (handler != null)
  1135. handler(this, e);
  1136. }
  1137. /// <summary>
  1138. /// Raises the <see cref="TextDeleted"/> event.
  1139. /// </summary>
  1140. /// <param name="e">An <see cref="TextModifiedEventArgs"/> that contains the event data.</param>
  1141. protected virtual void OnTextDeleted(TextModifiedEventArgs e)
  1142. {
  1143. EventHandler<TextModifiedEventArgs> handler = Events[_textDeletedEventKey] as EventHandler<TextModifiedEventArgs>;
  1144. if (handler != null)
  1145. handler(this, e);
  1146. }
  1147. /// <summary>
  1148. /// Raises the <see cref="TextInserted"/> event.
  1149. /// </summary>
  1150. /// <param name="e">An <see cref="TextModifiedEventArgs"/> that contains the event data.</param>
  1151. protected virtual void OnTextInserted(TextModifiedEventArgs e)
  1152. {
  1153. EventHandler<TextModifiedEventArgs> handler = Events[_textInsertedEventKey] as EventHandler<TextModifiedEventArgs>;
  1154. if (handler != null)
  1155. handler(this, e);
  1156. }
  1157. /// <summary>
  1158. /// Raises the <see cref="ZoomChanged"/> event.
  1159. /// </summary>
  1160. /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
  1161. protected virtual void OnZoomChanged(EventArgs e)
  1162. {
  1163. EventHandler handler = Events[_zoomChangedEventKey] as EventHandler;
  1164. if (handler != null)
  1165. handler(this, e);
  1166. }
  1167. private void paintRanges(Graphics g)
  1168. {
  1169. // First we want to get the range (in positions) of what
  1170. // will be painted so that we know which markers to paint
  1171. int firstLine = _ns.GetFirstVisibleLine();
  1172. int lastLine = firstLine + _ns.LinesOnScreen();
  1173. int firstPos = _ns.PositionFromLine(firstLine);
  1174. int lastPos = _ns.PositionFromLine(lastLine + 1) - 1;
  1175. // If the lastLine was outside the defined document range it will
  1176. // contain -1, defualt it to the last doc position
  1177. if (lastPos < 0)
  1178. lastPos = _ns.GetLength();
  1179. List<ManagedRange> mrs = ManagedRangesInRange(firstPos, lastPos);
  1180. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  1181. foreach (ManagedRange mr in mrs)
  1182. {
  1183. mr.Paint(g);
  1184. }
  1185. }
  1186. public int PointXFromPosition(int position)
  1187. {
  1188. return _ns.PointXFromPosition(position);
  1189. }
  1190. public int PointYFromPosition(int position)
  1191. {
  1192. return _ns.PointYFromPosition(position);
  1193. }
  1194. public int PositionFromPoint(int x, int y)
  1195. {
  1196. return _ns.PositionFromPoint(x, y);
  1197. }
  1198. public int PositionFromPointClose(int x, int y)
  1199. {
  1200. return _ns.PositionFromPointClose(x, y);
  1201. }
  1202. /// <summary>
  1203. /// Checks that if the specified position is on comment.
  1204. /// </summary>
  1205. public bool PositionIsOnComment(int position)
  1206. {
  1207. //this.Colorize(0, -1);
  1208. return PositionIsOnComment(position, _lexing.Lexer);
  1209. }
  1210. /// <summary>
  1211. /// Checks that if the specified position is on comment.
  1212. /// </summary>
  1213. public bool PositionIsOnComment(int position, Lexer lexer)
  1214. {
  1215. int style = _styles.GetStyleAt(position);
  1216. if ((lexer == Lexer.Python || lexer == Lexer.Lisp)
  1217. && (style == 1
  1218. || style == 12))
  1219. {
  1220. return true; // python or lisp
  1221. }
  1222. else if ((lexer == Lexer.Cpp || lexer == Lexer.Pascal || lexer == Lexer.Tcl || lexer == Lexer.Bullant)
  1223. && (style == 1
  1224. || style == 2
  1225. || style == 3
  1226. || style == 15
  1227. || style == 17
  1228. || style == 18))
  1229. {
  1230. return true; // cpp, tcl, bullant or pascal
  1231. }
  1232. else if ((lexer == Lexer.Hypertext || lexer == Lexer.Xml)
  1233. && (style == 9
  1234. || style == 20
  1235. || style == 29
  1236. || style == 30
  1237. || style == 42
  1238. || style == 43
  1239. || style == 44
  1240. || style == 57
  1241. || style == 58
  1242. || style == 59
  1243. || style == 72
  1244. || style == 82
  1245. || style == 92
  1246. || style == 107
  1247. || style == 124
  1248. || style == 125))
  1249. {
  1250. return true; // html or xml
  1251. }
  1252. else if ((lexer == Lexer.Perl || lexer == Lexer.Ruby || lexer == Lexer.Clw || lexer == Lexer.Bash)
  1253. && style == 2)
  1254. {
  1255. return true; // perl, bash, clarion/clw or ruby
  1256. }
  1257. else if ((lexer == Lexer.Sql)
  1258. && (style == 1
  1259. || style == 2
  1260. || style == 3
  1261. || style == 13
  1262. || style == 15
  1263. || style == 17
  1264. || style == 18))
  1265. {
  1266. return true; // sql
  1267. }
  1268. else if ((lexer == Lexer.VB || lexer == Lexer.Properties || lexer == Lexer.MakeFile || lexer == Lexer.Batch || lexer == Lexer.Diff || lexer == Lexer.Conf || lexer == Lexer.Ave || lexer == Lexer.Eiffel || lexer == Lexer.EiffelKw || lexer == Lexer.Tcl || lexer == Lexer.VBScript || lexer == Lexer.MatLab || lexer == Lexer.Fortran || lexer == Lexer.F77 || lexer == Lexer.Lout || lexer == Lexer.Mmixal || lexer == Lexer.Yaml || lexer == Lexer.PowerBasic || lexer == Lexer.ErLang || lexer == Lexer.Octave || lexer == Lexer.Kix || lexer == Lexer.Asn1)
  1269. && style == 1)
  1270. {
  1271. return true; // asn1, vb, diff, batch, makefile, avenue, eiffel, eiffelkw, vbscript, matlab, crontab, fortran, f77, lout, mmixal, yaml, powerbasic, erlang, octave, kix or properties
  1272. }
  1273. else if ((lexer == Lexer.Latex)
  1274. && style == 4)
  1275. {
  1276. return true; // latex
  1277. }
  1278. else if ((lexer == Lexer.Lua || lexer == Lexer.EScript || lexer == Lexer.Verilog)
  1279. && (style == 1
  1280. || style == 2
  1281. || style == 3))
  1282. {
  1283. return true; // lua, verilog or escript
  1284. }
  1285. else if ((lexer == Lexer.Ada)
  1286. && style == 10)
  1287. {
  1288. return true; // ada
  1289. }
  1290. else if ((lexer == Lexer.Baan || lexer == Lexer.Pov || lexer == Lexer.Ps || lexer == Lexer.Forth || lexer == Lexer.MsSql || lexer == Lexer.Gui4Cli || lexer == Lexer.Au3 || lexer == Lexer.Apdl || lexer == Lexer.Vhdl || lexer == Lexer.Rebol)
  1291. && (style == 1
  1292. || style == 2))
  1293. {
  1294. return true; // au3, apdl, baan, ps, mssql, rebol, forth, gui4cli, vhdl or pov
  1295. }
  1296. else if ((lexer == Lexer.Asm)
  1297. && (style == 1
  1298. || style == 11))
  1299. {
  1300. return true; // asm
  1301. }
  1302. else if ((lexer == Lexer.Nsis)
  1303. && (style == 1
  1304. || style == 18))
  1305. {
  1306. return true; // nsis
  1307. }
  1308. else if ((lexer == Lexer.Specman)
  1309. && (style == 2
  1310. || style == 3))
  1311. {
  1312. return true; // specman
  1313. }
  1314. else if ((lexer == Lexer.Tads3)
  1315. && (style == 3
  1316. || style == 4))
  1317. {
  1318. return true; // tads3
  1319. }
  1320. else if ((lexer == Lexer.CSound)
  1321. && (style == 1
  1322. || style == 9))
  1323. {
  1324. return true; // csound
  1325. }
  1326. else if ((lexer == Lexer.Caml)
  1327. && (style == 12
  1328. || style == 13
  1329. || style == 14
  1330. || style == 15))
  1331. {
  1332. return true; // caml
  1333. }
  1334. else if ((lexer == Lexer.Haskell)
  1335. && (style == 13
  1336. || style == 14
  1337. || style == 15
  1338. || style == 16))
  1339. {
  1340. return true; // haskell
  1341. }
  1342. else if ((lexer == Lexer.Flagship)
  1343. && (style == 1
  1344. || style == 2
  1345. || style == 3
  1346. || style == 4
  1347. || style == 5
  1348. || style == 6))
  1349. {
  1350. return true; // flagship
  1351. }
  1352. else if ((lexer == Lexer.Smalltalk)
  1353. && style == 3)
  1354. {
  1355. return true; // smalltalk
  1356. }
  1357. else if ((lexer == Lexer.Css)
  1358. && style == 9)
  1359. {
  1360. return true; // css
  1361. }
  1362. return false;
  1363. }
  1364. /// <summary>
  1365. /// Overridden. See <see cref="Control.ProcessKeyMessage"/>.
  1366. /// </summary>
  1367. protected override bool ProcessKeyMessage(ref Message m)
  1368. {
  1369. // For some reason IsInputKey isn't working for
  1370. // Key.Enter. This seems to make it work as expected
  1371. if ((int)m.WParam == (int)Keys.Enter && !AcceptsReturn)
  1372. {
  1373. return true;
  1374. }
  1375. else
  1376. {
  1377. return base.ProcessKeyMessage(ref m);
  1378. }
  1379. }
  1380. private void ResetCaption()
  1381. {
  1382. Caption = GetType().FullName;
  1383. }
  1384. private void ResetMargins()
  1385. {
  1386. _margins.Reset();
  1387. }
  1388. private void ResetStyles()
  1389. {
  1390. // One of the core appearance properties has changed. When this happens
  1391. // we restyle the document (overriding any existing styling) in the core
  1392. // appearance properties. This behavior is consistent with the RichTextBox.
  1393. NativeInterface.StartStyling(0, 0x7F);
  1394. NativeInterface.SetStyling(NativeInterface.GetLength(), 0);
  1395. Styles[0].Reset();
  1396. Styles[0].Font = Font;
  1397. Styles[0].ForeColor = ForeColor;
  1398. Styles[0].BackColor = BackColor;
  1399. Styles.Default.BackColor = BackColor;
  1400. }
  1401. /// <summary>
  1402. /// Custom way to find the matching brace when BraceMatch() does not work
  1403. /// </summary>
  1404. internal int SafeBraceMatch(int position)
  1405. {
  1406. int match = this.CharAt(position);
  1407. int toMatch = 0;
  1408. int length = TextLength;
  1409. int ch;
  1410. int sub = 0;
  1411. Lexer lexer = _lexing.Lexer;
  1412. _lexing.Colorize(0, -1);
  1413. bool comment = PositionIsOnComment(position, lexer);
  1414. switch (match)
  1415. {
  1416. case '{':
  1417. toMatch = '}';
  1418. goto down;
  1419. case '(':
  1420. toMatch = ')';
  1421. goto down;
  1422. case '[':
  1423. toMatch = ']';
  1424. goto down;
  1425. case '}':
  1426. toMatch = '{';
  1427. goto up;
  1428. case ')':
  1429. toMatch = '(';
  1430. goto up;
  1431. case ']':
  1432. toMatch = '[';
  1433. goto up;
  1434. }
  1435. return -1;
  1436. // search up
  1437. up:
  1438. while (position >= 0)
  1439. {
  1440. position--;
  1441. ch = CharAt(position);
  1442. if (ch == match)
  1443. {
  1444. if (comment == PositionIsOnComment(position, lexer)) sub++;
  1445. }
  1446. else if (ch == toMatch && comment == PositionIsOnComment(position, lexer))
  1447. {
  1448. sub--;
  1449. if (sub < 0) return position;
  1450. }
  1451. }
  1452. return -1;
  1453. // search down
  1454. down:
  1455. while (position < length)
  1456. {
  1457. position++;
  1458. ch = CharAt(position);
  1459. if (ch == match)
  1460. {
  1461. if (comment == PositionIsOnComment(position, lexer)) sub++;
  1462. }
  1463. else if (ch == toMatch && comment == PositionIsOnComment(position, lexer))
  1464. {
  1465. sub--;
  1466. if (sub < 0) return position;
  1467. }
  1468. }
  1469. return -1;
  1470. }
  1471. private void ScnModified(ref NativeMethods.SCNotification scn)
  1472. {
  1473. if ((scn.modificationType & NativeMethods.SC_MOD_CHANGEANNOTATION) == NativeMethods.SC_MOD_CHANGEANNOTATION)
  1474. {
  1475. AnnotationChangedEventArgs acea = new AnnotationChangedEventArgs(scn.line, scn.annotationLinesAdded);
  1476. OnAnnotationChanged(acea);
  1477. }
  1478. }
  1479. /// <summary>
  1480. /// Sets the application-wide default module name of the native Scintilla library.
  1481. /// </summary>
  1482. /// <param name="moduleName">The native Scintilla module name.</param>
  1483. /// <remarks>This method must be called prior to the first <see cref="Scintilla"/> control being created.</remarks>
  1484. /// <exception cref="ArgumentNullException">The <paramref name="moduleName"/> is <c>null</c>.</exception>
  1485. /// <exception cref="ArgumentException">The <paramref name="moduleName"/> is an empty string.</exception>
  1486. /// <exception cref="InvalidOperationException">This method was called after the first <see cref="Scintilla"/> control was created.</exception>
  1487. public static void SetModuleName(string moduleName)
  1488. {
  1489. const string paramName = "moduleName";
  1490. if (moduleName == null)
  1491. throw new ArgumentNullException(paramName);
  1492. if (moduleName.Length == 0)
  1493. throw new ArgumentException(string.Format(Resources.Culture, Resources.Exception_EmptyStringArgument, paramName), paramName);
  1494. if (_moduleHandle != IntPtr.Zero)
  1495. throw new InvalidOperationException(Resources.Exception_ModuleAlreadyLoaded);
  1496. _moduleName = moduleName;
  1497. }
  1498. private bool ShouldSerializeAnnotations()
  1499. {
  1500. return _annotations != null && _annotations.ShouldSerialize();
  1501. }
  1502. private bool ShouldSerializeAutoComplete()
  1503. {
  1504. return _autoComplete.ShouldSerialize();
  1505. }
  1506. private bool ShouldSerializeCallTip()
  1507. {
  1508. return _callTip.ShouldSerialize();
  1509. }
  1510. private bool ShouldSerializeCaption()
  1511. {
  1512. return Caption != GetType().FullName;
  1513. }
  1514. private bool ShouldSerializeCaret()
  1515. {
  1516. return _caret.ShouldSerialize();
  1517. }
  1518. private bool ShouldSerializeClipboard()
  1519. {
  1520. return _clipboard.ShouldSerialize();
  1521. }
  1522. private bool ShouldSerializeCommands()
  1523. {
  1524. return _commands.ShouldSerialize();
  1525. }
  1526. private bool ShouldSerializeConfigurationManager()
  1527. {
  1528. return _configurationManager.ShouldSerialize();
  1529. }
  1530. private bool ShouldSerializeDocumentNavigation()
  1531. {
  1532. return _documentNavigation.ShouldSerialize();
  1533. }
  1534. private bool ShouldSerializeDropMarkers()
  1535. {
  1536. return _dropMarkers.ShouldSerialize();
  1537. }
  1538. private bool ShouldSerializeEndOfLine()
  1539. {
  1540. return _endOfLine.ShouldSerialize();
  1541. }
  1542. private bool ShouldSerializeFindReplace()
  1543. {
  1544. return _findReplace.ShouldSerialize();
  1545. }
  1546. private bool ShouldSerializeFolding()
  1547. {
  1548. return _folding.ShouldSerialize();
  1549. }
  1550. private bool ShouldSerializeHotspotStyle()
  1551. {
  1552. return _hotspotStyle.ShouldSerialize();
  1553. }
  1554. private bool ShouldSerializeIndentation()
  1555. {
  1556. return _indentation.ShouldSerialize();
  1557. }
  1558. private bool ShouldSerializeLexing()
  1559. {
  1560. return _lexing.ShouldSerialize();
  1561. }
  1562. private bool ShouldSerializeLineWrapping()
  1563. {
  1564. return LineWrapping.ShouldSerialize();
  1565. }
  1566. private bool ShouldSerializeLongLines()
  1567. {
  1568. return _longLines.ShouldSerialize();
  1569. }
  1570. private bool ShouldSerializeMargins()
  1571. {
  1572. return _margins.ShouldSerialize();
  1573. }
  1574. private bool ShouldSerializeMarkers()
  1575. {
  1576. return _markers.ShouldSerialize();
  1577. }
  1578. private bool ShouldSerializePrinting()
  1579. {
  1580. return _printing.ShouldSerialize();
  1581. }
  1582. private bool ShouldSerializeScrolling()
  1583. {
  1584. return _scrolling.ShouldSerialize();
  1585. }
  1586. private bool ShouldSerializeSelection()
  1587. {
  1588. return _selection.ShouldSerialize();
  1589. }
  1590. private bool ShouldSerializeSnippets()
  1591. {
  1592. return _snippets.ShouldSerialize();
  1593. }
  1594. private bool ShouldSerializeStyles()
  1595. {
  1596. return _styles.ShouldSerialize();
  1597. }
  1598. public bool ShouldSerializeUndoRedo()
  1599. {
  1600. return _undoRedo.ShouldSerialize();
  1601. }
  1602. private void WmReflectCommand(ref Message m)
  1603. {
  1604. switch(Utilities.SignedHiWord(m.WParam))
  1605. {
  1606. case NativeMethods.SCEN_CHANGE:
  1607. // TODO It looks like TextChanged is firing twice for every change.
  1608. // This appears to be a Scintilla behavior, not us, but we might be able to work around it.
  1609. OnTextChanged(EventArgs.Empty);
  1610. break;
  1611. default:
  1612. base.WndProc(ref m);
  1613. break;
  1614. }
  1615. }
  1616. private void WmReflectNotify(ref Message m)
  1617. {
  1618. // New *internal* structure...
  1619. NativeMethods.SCNotification scn = (NativeMethods.SCNotification)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.SCNotification));
  1620. // Old *public* *outdated* structure and event args...
  1621. SCNotification scnOld = (SCNotification)Marshal.PtrToStructure(m.LParam, typeof(SCNotification));
  1622. NativeScintillaEventArgs nsea = new NativeScintillaEventArgs(m, scnOld);
  1623. switch (scnOld.nmhdr.code)
  1624. {
  1625. case Constants.SCN_AUTOCSELECTION:
  1626. FireAutoCSelection(nsea);
  1627. break;
  1628. case Constants.SCN_CALLTIPCLICK:
  1629. FireCallTipClick(nsea);
  1630. break;
  1631. case Constants.SCN_CHARADDED:
  1632. FireCharAdded(nsea);
  1633. break;
  1634. case Constants.SCN_DOUBLECLICK:
  1635. FireDoubleClick(nsea);
  1636. break;
  1637. case Constants.SCN_DWELLEND:
  1638. FireDwellEnd(nsea);
  1639. break;
  1640. case Constants.SCN_DWELLSTART:
  1641. FireDwellStart(nsea);
  1642. break;
  1643. case NativeMethods.SCN_HOTSPOTCLICK:
  1644. OnHotspotClick(new HotspotClickEventArgs(scn.position));
  1645. break;
  1646. case NativeMethods.SCN_HOTSPOTDOUBLECLICK:
  1647. OnHotspotDoubleClick(new HotspotClickEventArgs(scn.position));
  1648. break;
  1649. case NativeMethods.SCN_HOTSPOTRELEASECLICK:
  1650. OnHotspotReleaseClick(new HotspotClickEventArgs(scn.position));
  1651. break;
  1652. case Constants.SCN_INDICATORCLICK:
  1653. FireIndicatorClick(nsea);
  1654. break;
  1655. case Constants.SCN_INDICATORRELEASE:
  1656. FireIndicatorRelease(nsea);
  1657. break;
  1658. case Constants.SCN_KEY:
  1659. FireKey(nsea);
  1660. break;
  1661. case Constants.SCN_MACRORECORD:
  1662. FireMacroRecord(nsea);
  1663. break;
  1664. case Constants.SCN_MARGINCLICK:
  1665. FireMarginClick(nsea);
  1666. break;
  1667. case Constants.SCN_MODIFIED:
  1668. ScnModified(ref scn);
  1669. FireModified(nsea);
  1670. break;
  1671. case Constants.SCN_MODIFYATTEMPTRO:
  1672. FireModifyAttemptRO(nsea);
  1673. break;
  1674. case Constants.SCN_NEEDSHOWN:
  1675. FireNeedShown(nsea);
  1676. break;
  1677. case Constants.SCN_PAINTED:
  1678. FirePainted(nsea);
  1679. break;
  1680. case Constants.SCN_SAVEPOINTLEFT:
  1681. FireSavePointLeft(nsea);
  1682. break;
  1683. case Constants.SCN_SAVEPOINTREACHED:
  1684. FireSavePointReached(nsea);
  1685. break;
  1686. case Constants.SCN_STYLENEEDED:
  1687. FireStyleNeeded(nsea);
  1688. break;
  1689. case Constants.SCN_UPDATEUI:
  1690. FireUpdateUI(nsea);
  1691. break;
  1692. case Constants.SCN_URIDROPPED:
  1693. FireUriDropped(nsea);
  1694. break;
  1695. case Constants.SCN_USERLISTSELECTION:
  1696. FireUserListSelection(nsea);
  1697. break;
  1698. case Constants.SCN_ZOOM:
  1699. FireZoom(nsea);
  1700. break;
  1701. }
  1702. }
  1703. private void WmScroll(ref Message m)
  1704. {
  1705. ScrollOrientation so = ScrollOrientation.VerticalScroll;
  1706. int oldScroll = 0, newScroll = 0;
  1707. ScrollEventType set = (ScrollEventType)(Utilities.SignedLoWord(m.WParam));
  1708. if (m.Msg == NativeMethods.WM_HSCROLL)
  1709. {
  1710. so = ScrollOrientation.HorizontalScroll;
  1711. oldScroll = _ns.GetXOffset();
  1712. // Let Scintilla Handle the scroll Message to actually perform scrolling
  1713. base.WndProc(ref m);
  1714. newScroll = _ns.GetXOffset();
  1715. }
  1716. else
  1717. {
  1718. so = ScrollOrientation.VerticalScroll;
  1719. oldScroll = _ns.GetFirstVisibleLine();
  1720. base.WndProc(ref m);
  1721. newScroll = _ns.GetFirstVisibleLine();
  1722. }
  1723. OnScroll(new ScrollEventArgs(set, oldScroll, newScroll, so));
  1724. }
  1725. /// <summary>
  1726. /// Overridden. Processes Windows messages.
  1727. /// </summary>
  1728. /// <param name="m">The Windows <see cref="Message" /> to process.</param>
  1729. protected override void WndProc(ref Message m)
  1730. {
  1731. switch (m.Msg)
  1732. {
  1733. case NativeMethods.WM_DESTROY:
  1734. // wi11811 2008-07-28 Chris Rickard
  1735. // If we get a destroy message we make this window a message-only window so that it doesn't actually
  1736. // get destroyed, causing Scintilla to wipe out all its settings associated with this window handle.
  1737. // We do send a WM_DESTROY message to Scintilla in the Dispose() method so that it does clean up its
  1738. // resources when this control is actually done with. Credit (blame :) goes to tom103 for figuring
  1739. // this one out.
  1740. if (this.IsHandleCreated)
  1741. {
  1742. NativeMethods.SetParent(this.Handle, NativeMethods.HWND_MESSAGE);
  1743. return;
  1744. }
  1745. base.WndProc(ref m);
  1746. break;
  1747. case NativeMethods.WM_PAINT:
  1748. // I tried toggling the ControlStyles.UserPaint flag and sending the message
  1749. // to both base.WndProc and DefWndProc in order to get the best of both worlds,
  1750. // Scintilla Paints as normal and .NET fires the Paint Event with the proper
  1751. // clipping regions and such. This didn't work too well, I kept getting weird
  1752. // phantom paints, or sometimes the .NET paint events would seem to get painted
  1753. // over by Scintilla. This technique I use below seems to work perfectly.
  1754. base.WndProc(ref m);
  1755. if (_isCustomPaintingEnabled)
  1756. {
  1757. RECT r;
  1758. if (!NativeMethods.GetUpdateRect(Handle, out r, false))
  1759. r = ClientRectangle;
  1760. Graphics g = CreateGraphics();
  1761. g.SetClip(r);
  1762. OnPaint(new PaintEventArgs(CreateGraphics(), r));
  1763. }
  1764. break;
  1765. case NativeMethods.WM_DROPFILES:
  1766. HandleFileDrop(m.WParam);
  1767. break;
  1768. case NativeMethods.WM_SETCURSOR:
  1769. base.DefWndProc(ref m);
  1770. break;
  1771. case NativeMethods.WM_GETTEXT:
  1772. m.WParam = (IntPtr)(Caption.Length + 1);
  1773. Marshal.Copy(Caption.ToCharArray(), 0, m.LParam, Caption.Length);
  1774. m.Result = (IntPtr)Caption.Length;
  1775. break;
  1776. case NativeMethods.WM_GETTEXTLENGTH:
  1777. m.Result = (IntPtr)Caption.Length;
  1778. break;
  1779. case NativeMethods.WM_REFLECT + NativeMethods.WM_NOTIFY:
  1780. WmReflectNotify(ref m);
  1781. break;
  1782. case NativeMethods.WM_REFLECT + NativeMethods.WM_COMMAND:
  1783. WmReflectCommand(ref m);
  1784. break;
  1785. case NativeMethods.WM_HSCROLL:
  1786. case NativeMethods.WM_VSCROLL:
  1787. WmScroll(ref m);
  1788. break;
  1789. default:
  1790. if ((int)m.Msg >= 10000) // TODO Remove "magic number"
  1791. {
  1792. _commands.Execute((BindableCommand)m.Msg);
  1793. return;
  1794. }
  1795. base.WndProc(ref m);
  1796. break;
  1797. }
  1798. }
  1799. public void ZoomIn()
  1800. {
  1801. _ns.ZoomIn();
  1802. }
  1803. private void ZoomOut()
  1804. {
  1805. _ns.ZoomOut();
  1806. }
  1807. #endregion Methods
  1808. #region Properties
  1809. /// <summary>
  1810. /// Gets or sets a value indicating whether pressing ENTER creates a new line of text in the
  1811. /// control or activates the default button for the form.
  1812. /// </summary>
  1813. /// <returns>
  1814. /// <c>true</c> if the ENTER key creates a new line of text; <c>false</c> if the ENTER key activates
  1815. /// the default button for the form. The default is <c>false</c>.
  1816. /// </returns>
  1817. [DefaultValue(true), Category("Behavior")]
  1818. [Description("Indicates if return characters are accepted as text input.")]
  1819. public bool AcceptsReturn
  1820. {
  1821. get { return _state[_acceptsReturnState]; }
  1822. set { _state[_acceptsReturnState] = value; }
  1823. }
  1824. /// <summary>
  1825. /// Gets or sets a value indicating whether pressing the TAB key types a TAB character in the control
  1826. /// instead of moving the focus to the next control in the tab order.
  1827. /// </summary>
  1828. /// <returns>
  1829. /// <c>true</c> if users can enter tabs using the TAB key; <c>false</c> if pressing the TAB key
  1830. /// moves the focus. The default is <c>false</c>.
  1831. /// </returns>
  1832. [DefaultValue(true), Category("Behavior")]
  1833. [Description("Indicates if tab characters are accepted as text input.")]
  1834. public bool AcceptsTab
  1835. {
  1836. get { return _state[_acceptsTabState]; }
  1837. set { _state[_acceptsTabState] = value; }
  1838. }
  1839. /// <summary>
  1840. /// Gets or sets if .NET Drag and Drop operations are supported.
  1841. /// </summary>
  1842. public override bool AllowDrop
  1843. {
  1844. get
  1845. {
  1846. return _allowDrop;
  1847. }
  1848. set
  1849. {
  1850. NativeMethods.DragAcceptFiles(Handle, value);
  1851. _allowDrop = value;
  1852. }
  1853. }
  1854. /// <summary>
  1855. /// Gets a collection containing all annotations in the control.
  1856. /// </summary>
  1857. /// <returns>
  1858. /// A <see cref="AnnotationCollection" /> that contains all the annotations in the <see cref="Scintilla" /> control.
  1859. /// </returns>
  1860. [Category("Appearance")]
  1861. [Description("The annotations and options.")]
  1862. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  1863. public AnnotationCollection Annotations
  1864. {
  1865. get
  1866. {
  1867. if (_annotations == null)
  1868. _annotations = CreateAnnotationsInstance();
  1869. return _annotations;
  1870. }
  1871. }
  1872. /// <summary>
  1873. /// Controls autocompletion behavior.
  1874. /// </summary>
  1875. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  1876. public AutoComplete AutoComplete
  1877. {
  1878. get
  1879. {
  1880. return _autoComplete;
  1881. }
  1882. }
  1883. /// <summary>
  1884. /// Gets or sets the background color for the control.
  1885. /// </summary>
  1886. /// <value>
  1887. /// A <see cref="Color"/> that represents the background color of the control.
  1888. /// The default is <see cref="SystemColors.Window"/>.
  1889. /// </value>
  1890. /// <remarks>Settings this property resets any current document styling.</remarks>
  1891. [DefaultValue(typeof(Color), "Window")]
  1892. public override Color BackColor
  1893. {
  1894. get { return base.BackColor; }
  1895. set { base.BackColor = value; }
  1896. }
  1897. /// <summary>
  1898. /// This property is not relevant for this class.
  1899. /// </summary>
  1900. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  1901. public override Image BackgroundImage
  1902. {
  1903. get { return base.BackgroundImage; }
  1904. set { base.BackgroundImage = value; }
  1905. }
  1906. /// <summary>
  1907. /// This property is not relevant for this class.
  1908. /// </summary>
  1909. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  1910. public override ImageLayout BackgroundImageLayout
  1911. {
  1912. get { return base.BackgroundImageLayout; }
  1913. set { base.BackgroundImageLayout = value; }
  1914. }
  1915. /// <summary>
  1916. /// Gets or sets the border style of the control.
  1917. /// </summary>
  1918. /// <value>
  1919. /// A <see cref="BorderStyle" /> that represents the border type of the control.
  1920. /// The default is <see cref="BorderStyle.Fixed3D" />.
  1921. /// </value>
  1922. /// <exception cref="InvalidEnumArgumentException">
  1923. /// The value assigned is not one of the <see cref="BorderStyle" /> values.
  1924. /// </exception>
  1925. [DefaultValue(BorderStyle.Fixed3D), Category("Appearance")]
  1926. [Description("Indicates whether the control should have a border.")] // TODO Move to a resource
  1927. public BorderStyle BorderStyle
  1928. {
  1929. get
  1930. {
  1931. return _borderStyle;
  1932. }
  1933. set
  1934. {
  1935. if (!Enum.IsDefined(typeof(BorderStyle), value))
  1936. throw new InvalidEnumArgumentException("value", (int)value, typeof(BorderStyle));
  1937. if (value != _borderStyle)
  1938. {
  1939. _borderStyle = value;
  1940. // This will cause the CreateParams to be reapplied
  1941. UpdateStyles();
  1942. OnBorderStyleChanged(EventArgs.Empty);
  1943. }
  1944. }
  1945. }
  1946. /// <summary>
  1947. /// Manages CallTip (Visual Studio-like code Tooltip) behaviors
  1948. /// </summary>
  1949. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  1950. public CallTip CallTip
  1951. {
  1952. get
  1953. {
  1954. return _callTip;
  1955. }
  1956. set
  1957. {
  1958. _callTip = value;
  1959. }
  1960. }
  1961. /// <summary>
  1962. /// Gets/Sets the Win32 Window Caption. Defaults to Type's FullName
  1963. /// </summary>
  1964. [Category("Behavior")]
  1965. [Description("Win32 Window Caption")]
  1966. public string Caption
  1967. {
  1968. get { return _caption; }
  1969. set
  1970. {
  1971. if (_caption != value)
  1972. {
  1973. _caption = value;
  1974. // Triggers a new WM_GETTEXT query
  1975. base.Text = value;
  1976. }
  1977. }
  1978. }
  1979. /// <summary>
  1980. /// Controls Caret Behavior
  1981. /// </summary>
  1982. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Appearance")]
  1983. public CaretInfo Caret
  1984. {
  1985. get
  1986. {
  1987. return _caret;
  1988. }
  1989. }
  1990. /// <summary>
  1991. /// Gets Clipboard access for the control.
  1992. /// </summary>
  1993. /// <returns>A <see cref="Clipboard" /> object the provides Clipboard access for the control.</returns>
  1994. [Category("Behavior")] // TODO Place in resource file
  1995. [Description("Clipboard (cut, copy, paste) options.")] // TODO Place in resource file
  1996. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  1997. public Clipboard Clipboard
  1998. {
  1999. get
  2000. {
  2001. return _clipboard;
  2002. }
  2003. }
  2004. internal Dictionary<string, Color> ColorBag { get { return _colorBag; } }
  2005. /// <summary>
  2006. /// Controls behavior of keyboard bound commands.
  2007. /// </summary>
  2008. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2009. public Commands Commands
  2010. {
  2011. get
  2012. {
  2013. return _commands;
  2014. }
  2015. set
  2016. {
  2017. _commands = value;
  2018. }
  2019. }
  2020. /// <summary>
  2021. /// Controls behavior of loading/managing ScintillaNET configurations.
  2022. /// </summary>
  2023. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2024. public Configuration.ConfigurationManager ConfigurationManager
  2025. {
  2026. get
  2027. {
  2028. return _configurationManager;
  2029. }
  2030. set
  2031. {
  2032. _configurationManager = value;
  2033. }
  2034. }
  2035. /// <summary>
  2036. /// Overridden. See <see cref="Control.CreateParams"/>.
  2037. /// </summary>
  2038. protected override CreateParams CreateParams
  2039. {
  2040. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  2041. get
  2042. {
  2043. // Otherwise Scintilla won't paint. When UserPaint is set to
  2044. // true the base Class (Control) eats the WM_PAINT message.
  2045. // Of course when this set to false we can't use the Paint
  2046. // events. This is why I'm relying on the Paint notification
  2047. // sent from scintilla to paint the Marker Arrows.
  2048. SetStyle(ControlStyles.UserPaint, false);
  2049. // I hope the old man got that tractor beam out if commission,
  2050. // or this is going to be a real short trip. Okay, hit it!
  2051. LoadModule();
  2052. // Tell Windows Forms to create a Scintilla
  2053. // derived Window Class for this control
  2054. CreateParams cp = base.CreateParams;
  2055. cp.ClassName = "Scintilla";
  2056. // Set the window style or extended style
  2057. // to the appropriate border type.
  2058. switch (_borderStyle)
  2059. {
  2060. case BorderStyle.Fixed3D:
  2061. cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE;
  2062. cp.Style &= ~NativeMethods.WS_BORDER;
  2063. break;
  2064. case BorderStyle.FixedSingle:
  2065. cp.ExStyle &= ~NativeMethods.WS_EX_CLIENTEDGE;
  2066. cp.Style |= NativeMethods.WS_BORDER;
  2067. break;
  2068. default:
  2069. cp.ExStyle &= ~NativeMethods.WS_EX_CLIENTEDGE;
  2070. cp.Style &= ~NativeMethods.WS_BORDER;
  2071. break;
  2072. }
  2073. return cp;
  2074. }
  2075. }
  2076. /// <summary>
  2077. /// Gets or sets the character index of the current caret position.
  2078. /// </summary>
  2079. /// <returns>The character index of the current caret position.</returns>
  2080. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2081. public int CurrentPos
  2082. {
  2083. get
  2084. {
  2085. return NativeInterface.GetCurrentPos();
  2086. }
  2087. set
  2088. {
  2089. NativeInterface.GotoPos(value);
  2090. }
  2091. }
  2092. /// <summary>Gets or sets the default cursor for the control.</summary>
  2093. /// <returns>An object of type <see cref="T:System.Windows.Forms.Cursor"></see> representing the current default cursor.</returns>
  2094. protected override Cursor DefaultCursor
  2095. {
  2096. get
  2097. {
  2098. return Cursors.IBeam;
  2099. }
  2100. }
  2101. /// <summary>
  2102. /// Overridden. See <see cref="Control.DefaultSize"/>.
  2103. /// </summary>
  2104. protected override Size DefaultSize
  2105. {
  2106. get
  2107. {
  2108. return new Size(200, 100);
  2109. }
  2110. }
  2111. /// <summary>
  2112. /// Controls behavior of Documents
  2113. /// </summary>
  2114. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2115. public DocumentHandler DocumentHandler
  2116. {
  2117. get
  2118. {
  2119. return _documentHandler;
  2120. }
  2121. set
  2122. {
  2123. _documentHandler = value;
  2124. }
  2125. }
  2126. /// <summary>
  2127. /// Controls behavior of automatic document navigation
  2128. /// </summary>
  2129. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2130. public DocumentNavigation DocumentNavigation
  2131. {
  2132. get
  2133. {
  2134. return _documentNavigation;
  2135. }
  2136. set
  2137. {
  2138. _documentNavigation = value;
  2139. }
  2140. }
  2141. /// <summary>
  2142. /// Controls behavior of Drop Markers
  2143. /// </summary>
  2144. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2145. public DropMarkers DropMarkers
  2146. {
  2147. get
  2148. {
  2149. return _dropMarkers;
  2150. }
  2151. }
  2152. /// <summary>
  2153. /// Controls Encoding behavior
  2154. /// </summary>
  2155. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2156. public Encoding Encoding
  2157. {
  2158. get
  2159. {
  2160. return _encoding;
  2161. }
  2162. set
  2163. {
  2164. // EncoderFallbackException isn't really the correct exception but
  2165. // I'm being lazy and you get the point
  2166. if (!ValidCodePages.Contains(value))
  2167. throw new EncoderFallbackException("Scintilla only supports the following Encodings: " + ValidCodePages.ToString());
  2168. _encoding = value;
  2169. _ns.SetCodePage(_encoding.CodePage);
  2170. }
  2171. }
  2172. /// <summary>
  2173. /// Controls End Of Line Behavior
  2174. /// </summary>
  2175. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2176. public EndOfLine EndOfLine
  2177. {
  2178. get
  2179. {
  2180. return _endOfLine;
  2181. }
  2182. set
  2183. {
  2184. _endOfLine = value;
  2185. }
  2186. }
  2187. [Category("Behavior")]
  2188. public FindReplace FindReplace
  2189. {
  2190. get
  2191. {
  2192. return _findReplace;
  2193. }
  2194. set
  2195. {
  2196. _findReplace = value;
  2197. }
  2198. }
  2199. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2200. public Folding Folding
  2201. {
  2202. get
  2203. {
  2204. return _folding;
  2205. }
  2206. set
  2207. {
  2208. _folding = value;
  2209. }
  2210. }
  2211. /// <summary>
  2212. /// Gets or sets the font of the text displayed by the control.
  2213. /// </summary>
  2214. /// <value>
  2215. /// The <see cref="Font"/> to apply to the text displayed by the control.
  2216. /// The default is the value of the <see cref="DefaultFont"/> property.
  2217. /// </value>
  2218. /// <remarks>Settings this property resets any current document styling.</remarks>
  2219. public override Font Font
  2220. {
  2221. get { return base.Font; }
  2222. set { base.Font = value; }
  2223. }
  2224. /// <summary>
  2225. /// Gets or sets the foreground color of the control.
  2226. /// </summary>
  2227. /// <value>
  2228. /// The foreground <see cref="Color"/> of the control.
  2229. /// The default is <see cref="SystemColors.WindowText"/>.
  2230. /// </value>
  2231. /// <remarks>Settings this property resets any current document styling.</remarks>
  2232. [DefaultValue(typeof(Color), "WindowText")]
  2233. public override Color ForeColor
  2234. {
  2235. get { return base.ForeColor; }
  2236. set { base.ForeColor = value; }
  2237. }
  2238. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2239. public GoTo GoTo
  2240. {
  2241. get
  2242. {
  2243. return _goto;
  2244. }
  2245. set
  2246. {
  2247. _goto = value;
  2248. }
  2249. }
  2250. protected internal List<TopLevelHelper> Helpers
  2251. {
  2252. get
  2253. {
  2254. return _helpers;
  2255. }
  2256. set
  2257. {
  2258. _helpers = value;
  2259. }
  2260. }
  2261. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Appearance")]
  2262. public HotspotStyle HotspotStyle
  2263. {
  2264. get
  2265. {
  2266. return _hotspotStyle;
  2267. }
  2268. set
  2269. {
  2270. _hotspotStyle = value;
  2271. }
  2272. }
  2273. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2274. public Indentation Indentation
  2275. {
  2276. get
  2277. {
  2278. return _indentation;
  2279. }
  2280. set
  2281. {
  2282. _indentation = value;
  2283. }
  2284. }
  2285. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2286. public IndicatorCollection Indicators
  2287. {
  2288. get { return _indicators; }
  2289. }
  2290. [DefaultValue(false), Category("Behavior")]
  2291. public bool IsBraceMatching
  2292. {
  2293. get { return _isBraceMatching; }
  2294. set { _isBraceMatching = value; }
  2295. }
  2296. [DefaultValue(true), Category("Behavior")]
  2297. public bool IsCustomPaintingEnabled
  2298. {
  2299. get
  2300. {
  2301. return _isCustomPaintingEnabled;
  2302. }
  2303. set
  2304. {
  2305. _isCustomPaintingEnabled = value;
  2306. }
  2307. }
  2308. internal bool IsDesignMode
  2309. {
  2310. get
  2311. {
  2312. return DesignMode;
  2313. }
  2314. }
  2315. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2316. internal bool IsInitializing
  2317. {
  2318. get
  2319. {
  2320. return _isInitializing;
  2321. }
  2322. set
  2323. {
  2324. _isInitializing = value;
  2325. }
  2326. }
  2327. [DefaultValue(false), Category("Behavior")]
  2328. public bool IsReadOnly
  2329. {
  2330. get
  2331. {
  2332. return _ns.GetReadOnly();
  2333. }
  2334. set
  2335. {
  2336. _ns.SetReadOnly(value);
  2337. }
  2338. }
  2339. /// <summary>
  2340. /// Gets or sets the line layout caching strategy in a <see cref="Scintilla" /> control.
  2341. /// </summary>
  2342. /// <returns>
  2343. /// One of the <see cref="LayoutCacheMode"/> enumeration values.
  2344. /// The default is <see cref="LayoutCacheMode.Caret" />.
  2345. /// </returns>
  2346. /// <exception cref="InvalidEnumArgumentException">
  2347. /// The value assigned is not one of the <see cref="LayoutCacheMode" /> values.
  2348. /// </exception>
  2349. /// <remarks>Larger cache sizes increase performance at the expense of memory.</remarks>
  2350. [Browsable(false)]
  2351. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2352. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2353. public LayoutCacheMode LayoutCacheMode
  2354. {
  2355. get
  2356. {
  2357. return (LayoutCacheMode)DirectMessage(NativeMethods.SCI_GETLAYOUTCACHE, IntPtr.Zero, IntPtr.Zero);
  2358. }
  2359. set
  2360. {
  2361. if (!Enum.IsDefined(typeof(LayoutCacheMode), value))
  2362. throw new InvalidEnumArgumentException("value", (int)value, typeof(LayoutCacheMode));
  2363. DirectMessage(NativeMethods.SCI_SETLAYOUTCACHE, new IntPtr((int)value), IntPtr.Zero);
  2364. }
  2365. }
  2366. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2367. public Lexing Lexing
  2368. {
  2369. get
  2370. {
  2371. return _lexing;
  2372. }
  2373. set
  2374. {
  2375. _lexing = value;
  2376. }
  2377. }
  2378. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2379. public LineCollection Lines
  2380. {
  2381. get
  2382. {
  2383. return _lines;
  2384. }
  2385. }
  2386. /// <summary>
  2387. /// Gets an object that controls line wrapping options in the <see cref="Scintilla"/> control.
  2388. /// </summary>
  2389. /// <returns>A <see cref="LineWrapping"/> object that manages line wrapping options in a <see cref="Scintilla"/> control.</returns>
  2390. [Category("Behavior")] // TODO Move to resource file
  2391. [Description("The control's line wrapping options.")] // TODO Move to resource file
  2392. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  2393. public LineWrapping LineWrapping
  2394. {
  2395. get
  2396. {
  2397. if (_lineWrapping == null)
  2398. _lineWrapping = CreateLineWrappingInstance();
  2399. return _lineWrapping;
  2400. }
  2401. }
  2402. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2403. public LongLines LongLines
  2404. {
  2405. get
  2406. {
  2407. return _longLines;
  2408. }
  2409. set
  2410. {
  2411. _longLines = value;
  2412. }
  2413. }
  2414. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2415. public List<ManagedRange> ManagedRanges
  2416. {
  2417. get { return _managedRanges; }
  2418. }
  2419. [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Appearance")]
  2420. public MarginCollection Margins
  2421. {
  2422. get
  2423. {
  2424. return _margins;
  2425. }
  2426. }
  2427. /// <summary>
  2428. /// Gets a collection representing the marker objects and options within the control.
  2429. /// </summary>
  2430. /// <returns>A <see cref="MarkerCollection" /> representing the marker objects and options within the control.</returns>
  2431. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2432. public MarkerCollection Markers
  2433. {
  2434. get
  2435. {
  2436. return _markers;
  2437. }
  2438. set
  2439. {
  2440. _markers = value;
  2441. }
  2442. }
  2443. [DefaultValue(true), Category("Behavior")]
  2444. public bool MatchBraces
  2445. {
  2446. get
  2447. {
  2448. return _matchBraces;
  2449. }
  2450. set
  2451. {
  2452. _matchBraces = value;
  2453. // Clear any active Brace matching that may exist
  2454. if (!value)
  2455. _ns.BraceHighlight(-1, -1);
  2456. }
  2457. }
  2458. /// <summary>
  2459. /// Gets or sets a value that indicates that the control has been modified by the user since
  2460. /// the control was created or its contents were last set.
  2461. /// </summary>
  2462. /// <returns>
  2463. /// <c>true</c> if the control's contents have been modified; otherwise, <c>false</c>.
  2464. /// The default is <c>false</c>.
  2465. /// </returns>
  2466. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2467. public bool Modified
  2468. {
  2469. get { return _state[_modifiedState]; }
  2470. set
  2471. {
  2472. if (_state[_modifiedState] != value)
  2473. {
  2474. // Update the local (and native) state
  2475. _state[_modifiedState] = value;
  2476. if (!value)
  2477. _ns.SetSavePoint();
  2478. OnModifiedChanged(EventArgs.Empty);
  2479. }
  2480. }
  2481. }
  2482. [DefaultValue(true), Category("Behavior")]
  2483. public bool MouseDownCaptures
  2484. {
  2485. get
  2486. {
  2487. return NativeInterface.GetMouseDownCaptures();
  2488. }
  2489. set
  2490. {
  2491. NativeInterface.SetMouseDownCaptures(value);
  2492. }
  2493. }
  2494. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2495. public INativeScintilla NativeInterface
  2496. {
  2497. get
  2498. {
  2499. return this as INativeScintilla;
  2500. }
  2501. }
  2502. [DefaultValue(false), Category("Behavior")]
  2503. public bool OverType
  2504. {
  2505. get
  2506. {
  2507. return _ns.GetOvertype();
  2508. }
  2509. set
  2510. {
  2511. _ns.SetOvertype(value);
  2512. }
  2513. }
  2514. /// <summary>
  2515. /// Gets or sets the position cache size used to layout short runs of text in a <see cref="Scintilla" /> control.
  2516. /// </summary>
  2517. /// <returns>The size of the position cache in bytes. The default is 1024.</returns>
  2518. /// <remarks>Larger cache sizes increase performance at the expense of memory.</remarks>
  2519. [Browsable(false)]
  2520. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2521. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2522. public virtual int PositionCacheSize
  2523. {
  2524. get
  2525. {
  2526. return DirectMessage(NativeMethods.SCI_GETPOSITIONCACHE, IntPtr.Zero, IntPtr.Zero).ToInt32();
  2527. }
  2528. set
  2529. {
  2530. // TODO Some range checking? Scintilla provides no guidance
  2531. DirectMessage(NativeMethods.SCI_SETPOSITIONCACHE, new IntPtr(value), IntPtr.Zero);
  2532. }
  2533. }
  2534. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Layout")]
  2535. public Printing Printing
  2536. {
  2537. get
  2538. {
  2539. return _printing;
  2540. }
  2541. set
  2542. {
  2543. _printing = value;
  2544. }
  2545. }
  2546. internal Hashtable PropertyBag { get { return _propertyBag; } }
  2547. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2548. unsafe public byte[] RawText
  2549. {
  2550. get
  2551. {
  2552. int length = NativeInterface.GetTextLength() + 1;
  2553. // May as well avoid all the crap below if we know what the outcome
  2554. // is going to be :)
  2555. if (length == 1)
  2556. return new byte[] { 0 };
  2557. // Allocate a buffer the size of the string + 1 for
  2558. // the NULL terminator. Scintilla always sets this
  2559. // regardless of the encoding
  2560. byte[] buffer = new byte[length];
  2561. // Get a direct pointer to the the head of the buffer
  2562. // to pass to the message along with the wParam.
  2563. // Scintilla will fill the buffer with string data.
  2564. fixed (byte* bp = buffer)
  2565. {
  2566. _ns.SendMessageDirect(Constants.SCI_GETTEXT, (IntPtr)length, (IntPtr)bp);
  2567. return buffer;
  2568. }
  2569. }
  2570. set
  2571. {
  2572. if (value == null || value.Length == 0)
  2573. {
  2574. _ns.ClearAll();
  2575. }
  2576. else
  2577. {
  2578. // This byte[] HAS to be NULL terminated or who knows how big
  2579. // of an overrun we'll have on our hands
  2580. if (value[value.Length - 1] != 0)
  2581. {
  2582. // I hate to have to do this becuase it can be very inefficient.
  2583. // It can probably be done much better by the client app
  2584. Array.Resize<byte>(ref value, value.Length + 1);
  2585. value[value.Length - 1] = 0;
  2586. }
  2587. fixed (byte* bp = value)
  2588. _ns.SendMessageDirect(Constants.SCI_SETTEXT, IntPtr.Zero, (IntPtr)bp);
  2589. }
  2590. }
  2591. }
  2592. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Layout")]
  2593. public Scrolling Scrolling
  2594. {
  2595. get
  2596. {
  2597. return _scrolling;
  2598. }
  2599. set
  2600. {
  2601. _scrolling = value;
  2602. }
  2603. }
  2604. [DefaultValue(SearchFlags.Empty), Category("Behavior")]
  2605. [Editor(typeof(Design.FlagEnumUIEditor), typeof(System.Drawing.Design.UITypeEditor))]
  2606. public SearchFlags SearchFlags
  2607. {
  2608. get
  2609. {
  2610. return _searchFlags;
  2611. }
  2612. set
  2613. {
  2614. _searchFlags = value;
  2615. }
  2616. }
  2617. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Appearance")]
  2618. public Selection Selection
  2619. {
  2620. get
  2621. {
  2622. return _selection;
  2623. }
  2624. }
  2625. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2626. public SnippetManager Snippets
  2627. {
  2628. get
  2629. {
  2630. return _snippets;
  2631. }
  2632. }
  2633. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Appearance")]
  2634. public StyleCollection Styles
  2635. {
  2636. get
  2637. {
  2638. return _styles;
  2639. }
  2640. set
  2641. {
  2642. _styles = value;
  2643. }
  2644. }
  2645. /// <summary>
  2646. /// Gets or sets a value indicating whether characters not considered alphanumeric (ASCII values 0 through 31)
  2647. /// are prevented as text input.
  2648. /// </summary>
  2649. /// <returns>
  2650. /// <c>true</c> to prevent control characters as input; otherwise, <c>false</c>.
  2651. /// The default is <c>true</c>.
  2652. /// </returns>
  2653. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2654. public bool SupressControlCharacters
  2655. {
  2656. get
  2657. {
  2658. return _supressControlCharacters;
  2659. }
  2660. set
  2661. {
  2662. _supressControlCharacters = value;
  2663. }
  2664. }
  2665. /// <summary>
  2666. /// Gets or sets the current text in the <see cref="Scintilla" /> control.
  2667. /// </summary>
  2668. /// <returns>The text displayed in the control.</returns>
  2669. [Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design", typeof(UITypeEditor))]
  2670. public override string Text
  2671. {
  2672. get
  2673. {
  2674. string s;
  2675. _ns.GetText(_ns.GetLength() + 1, out s);
  2676. return s;
  2677. }
  2678. set
  2679. {
  2680. if (string.IsNullOrEmpty(value))
  2681. _ns.ClearAll();
  2682. else
  2683. _ns.SetText(value);
  2684. }
  2685. }
  2686. /// <summary>
  2687. /// Gets the _length of text in the control.
  2688. /// </summary>
  2689. /// <returns>The number of characters contained in the text of the control.</returns>
  2690. [Browsable(false)]
  2691. public int TextLength
  2692. {
  2693. get
  2694. {
  2695. return NativeInterface.GetTextLength();
  2696. }
  2697. }
  2698. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Behavior")]
  2699. public UndoRedo UndoRedo
  2700. {
  2701. get
  2702. {
  2703. return _undoRedo;
  2704. }
  2705. }
  2706. public new bool UseWaitCursor
  2707. {
  2708. get
  2709. {
  2710. return base.UseWaitCursor;
  2711. }
  2712. set
  2713. {
  2714. base.UseWaitCursor = value;
  2715. if (value)
  2716. NativeInterface.SetCursor(Constants.SC_CURSORWAIT);
  2717. else
  2718. NativeInterface.SetCursor(Constants.SC_CURSORNORMAL);
  2719. }
  2720. }
  2721. /// <summary>
  2722. /// Gets the <see cref="Whitespace"/> display mode and style behavior associated with the <see cref="Scintilla"/> control.
  2723. /// </summary>
  2724. /// <returns>A <see cref="Whitespace"/> object that represents whitespace display mode and style behavior in a <see cref="Scintilla"/> control.</returns>
  2725. [Category("Appearance"), Description("The display mode and style of whitespace characters.")]
  2726. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  2727. public Whitespace Whitespace
  2728. {
  2729. get { return _whitespace; }
  2730. }
  2731. /// <summary>
  2732. /// Gets or sets the current zoom level of the <see cref="Scintilla" /> control.
  2733. /// </summary>
  2734. /// <returns>The factor by which the contents of the control is zoomed.</returns>
  2735. [DefaultValue(0), Category("Appearance")]
  2736. [Description("Defines the current scaling factor of the text display; 0 is normal viewing.")]
  2737. public int Zoom
  2738. {
  2739. get
  2740. {
  2741. return _ns.GetZoom();
  2742. }
  2743. set
  2744. {
  2745. _ns.SetZoom(value);
  2746. }
  2747. }
  2748. #endregion Properties
  2749. #region Events
  2750. /// <summary>
  2751. /// Occurs when an annotation has changed.
  2752. /// </summary>
  2753. [Category("Scintilla")] // TODO Move to resource
  2754. [Description("Occurs when an annotation has changed.")] // TODO Move to resource
  2755. public event EventHandler<AnnotationChangedEventArgs> AnnotationChanged
  2756. {
  2757. add
  2758. {
  2759. Events.AddHandler(_annotationChangedEventKey, value);
  2760. }
  2761. remove
  2762. {
  2763. Events.RemoveHandler(_annotationChangedEventKey, value);
  2764. }
  2765. }
  2766. /// <summary>
  2767. /// Occurs when the user makes a selection from the auto-complete list.
  2768. /// </summary>
  2769. [Category("Scintilla"), Description("Occurs when the user makes a selection from the auto-complete list.")]
  2770. public event EventHandler<AutoCompleteAcceptedEventArgs> AutoCompleteAccepted
  2771. {
  2772. add { Events.AddHandler(_autoCompleteAcceptedEventKey, value); }
  2773. remove { Events.RemoveHandler(_autoCompleteAcceptedEventKey, value); }
  2774. }
  2775. /// <summary>
  2776. /// Occurs when text is about to be removed from the document.
  2777. /// </summary>
  2778. [Category("Scintilla"), Description("Occurs when text is about to be removed from the document.")]
  2779. public event EventHandler<TextModifiedEventArgs> BeforeTextDelete
  2780. {
  2781. add { Events.AddHandler(_beforeTextDeleteEventKey, value); }
  2782. remove { Events.RemoveHandler(_beforeTextDeleteEventKey, value); }
  2783. }
  2784. /// <summary>
  2785. /// Occurs when text is about to be inserted into the document.
  2786. /// </summary>
  2787. [Category("Scintilla"), Description("Occurs when text is about to be inserted into the document.")]
  2788. public event EventHandler<TextModifiedEventArgs> BeforeTextInsert
  2789. {
  2790. add { Events.AddHandler(_beforeTextInsertEventKey, value); }
  2791. remove { Events.RemoveHandler(_beforeTextInsertEventKey, value); }
  2792. }
  2793. /// <summary>
  2794. /// Occurs when the value of the <see cref="BorderStyle" /> property has changed.
  2795. /// </summary>
  2796. [Category("Property Changed")]
  2797. [Description("Occurs when the value of the BorderStyle property changes.")] // TODO Move to resource
  2798. public event EventHandler BorderStyleChanged
  2799. {
  2800. add
  2801. {
  2802. Events.AddHandler(_borderStyleChangedEventKey, value);
  2803. }
  2804. remove
  2805. {
  2806. Events.RemoveHandler(_borderStyleChangedEventKey, value);
  2807. }
  2808. }
  2809. /// <summary>
  2810. /// Occurs when a user clicks on a call tip.
  2811. /// </summary>
  2812. [Category("Scintilla"), Description("Occurs when a user clicks on a call tip.")]
  2813. public event EventHandler<CallTipClickEventArgs> CallTipClick
  2814. {
  2815. add { Events.AddHandler(_callTipClickEventKey, value); }
  2816. remove { Events.RemoveHandler(_callTipClickEventKey, value); }
  2817. }
  2818. /// <summary>
  2819. /// Occurs when the user types an ordinary text character (as opposed to a command character) into the text.
  2820. /// </summary>
  2821. [Category("Scintilla"), Description("Occurs when the user types a text character.")]
  2822. public event EventHandler<CharAddedEventArgs> CharAdded
  2823. {
  2824. add { Events.AddHandler(_charAddedEventKey, value); }
  2825. remove { Events.RemoveHandler(_charAddedEventKey, value); }
  2826. }
  2827. /// <summary>
  2828. /// Occurs when the text or styling of the document changes or is about to change.
  2829. /// </summary>
  2830. [Category("Scintilla"), Description("Occurs when the text or styling of the document changes or is about to change.")]
  2831. public event EventHandler<NativeScintillaEventArgs> DocumentChange
  2832. {
  2833. add { Events.AddHandler(_documentChangeEventKey, value); }
  2834. remove { Events.RemoveHandler(_documentChangeEventKey, value); }
  2835. }
  2836. /// <summary>
  2837. /// Occurs when a <see cref="DropMarker"/> is about to be collected.
  2838. /// </summary>
  2839. [Category("Scintilla"), Description("Occurs when a DropMarker is about to be collected.")]
  2840. public event EventHandler<DropMarkerCollectEventArgs> DropMarkerCollect
  2841. {
  2842. add { Events.AddHandler(_dropMarkerCollectEventKey, value); }
  2843. remove { Events.RemoveHandler(_dropMarkerCollectEventKey, value); }
  2844. }
  2845. /// <summary>
  2846. /// Occurs when a user actions such as a mouse move or key press ends a dwell (hover) activity.
  2847. /// </summary>
  2848. [Category("Scintilla"), Description("Occurs when a dwell (hover) activity has ended.")]
  2849. public event EventHandler<ScintillaMouseEventArgs> DwellEnd
  2850. {
  2851. add { Events.AddHandler(_dwellEndEventKey, value); }
  2852. remove { Events.RemoveHandler(_dwellEndEventKey, value); }
  2853. }
  2854. /// <summary>
  2855. /// Occurs when the user hovers the mouse (dwells) in one position for the dwell period.
  2856. /// </summary>
  2857. [Category("Scintilla"), Description("Occurs when the user hovers the mouse (dwells) in one position for the dwell period.")]
  2858. public event EventHandler<ScintillaMouseEventArgs> DwellStart
  2859. {
  2860. add { Events.AddHandler(_dwellStartEventKey, value); }
  2861. remove { Events.RemoveHandler(_dwellStartEventKey, value); }
  2862. }
  2863. /// <summary>
  2864. /// Occurs when a user drops a file on the <see cref="Scintilla"/> control.
  2865. /// </summary>
  2866. [Category("Scintilla"), Description("Occurs when a user drops a file on the control.")]
  2867. public event EventHandler<FileDropEventArgs> FileDrop
  2868. {
  2869. add { Events.AddHandler(_fileDropEventKey, value); }
  2870. remove { Events.RemoveHandler(_fileDropEventKey, value); }
  2871. }
  2872. /// <summary>
  2873. /// Occurs when a folding change has occurred.
  2874. /// </summary>
  2875. [Category("Scintilla"), Description("Occurs when a folding change has occurred.")]
  2876. public event EventHandler<FoldChangedEventArgs> FoldChanged
  2877. {
  2878. add { Events.AddHandler(_foldChangedEventKey, value); }
  2879. remove { Events.RemoveHandler(_foldChangedEventKey, value); }
  2880. }
  2881. /// <summary>
  2882. /// Occurs when a user clicks on text that is in a style with the hotspot attribute set.
  2883. /// </summary>
  2884. [Category("Scintilla")]
  2885. [Description("Occurs when a user clicks on text with the hotspot style.")] // TODO Move to resource
  2886. public event EventHandler<HotspotClickEventArgs> HotspotClick
  2887. {
  2888. add
  2889. {
  2890. Events.AddHandler(_hotspotClickEventKey, value);
  2891. }
  2892. remove
  2893. {
  2894. Events.RemoveHandler(_hotspotClickEventKey, value);
  2895. }
  2896. }
  2897. /// <summary>
  2898. /// Occurs when a user double-clicks on text that is in a style with the hotspot attribute set.
  2899. /// </summary>
  2900. [Category("Scintilla")]
  2901. [Description("Occurs when a user double-clicks on text with the hotspot style.")] // TODO Move to resource
  2902. public event EventHandler<HotspotClickEventArgs> HotspotDoubleClick
  2903. {
  2904. add
  2905. {
  2906. Events.AddHandler(_hotspotDoubleClickEventKey, value);
  2907. }
  2908. remove
  2909. {
  2910. Events.RemoveHandler(_hotspotDoubleClickEventKey, value);
  2911. }
  2912. }
  2913. /// <summary>
  2914. /// Occurs when a user releases a click on text that is in a style with the hotspot attribute set.
  2915. /// </summary>
  2916. [Category("Scintilla")]
  2917. [Description("Occurs when a user releases a click on text with the hotspot style.")] // TODO Move to resource
  2918. public event EventHandler<HotspotClickEventArgs> HotspotReleaseClick
  2919. {
  2920. add
  2921. {
  2922. Events.AddHandler(_hotspotReleaseClickEventKey, value);
  2923. }
  2924. remove
  2925. {
  2926. Events.RemoveHandler(_hotspotReleaseClickEventKey, value);
  2927. }
  2928. }
  2929. /// <summary>
  2930. /// Occurs when the a clicks or releases the mouse on text that has an indicator.
  2931. /// </summary>
  2932. [Category("Scintilla"), Description("Occurs when the a clicks or releases the mouse on text that has an indicator.")]
  2933. public event EventHandler<ScintillaMouseEventArgs> IndicatorClick
  2934. {
  2935. add { Events.AddHandler(_indicatorClickEventKey, value); }
  2936. remove { Events.RemoveHandler(_indicatorClickEventKey, value); }
  2937. }
  2938. /// <summary>
  2939. /// Occurs when a range of lines that is currently invisible should be made visible.
  2940. /// </summary>
  2941. [Category("Scintilla"), Description("Occurs when a range of lines that is currently invisible should be made visible.")]
  2942. public event EventHandler<LinesNeedShownEventArgs> LinesNeedShown
  2943. {
  2944. add { Events.AddHandler(_linesNeedShownEventKey, value); }
  2945. remove { Events.RemoveHandler(_linesNeedShownEventKey, value); }
  2946. }
  2947. /// <summary>
  2948. /// Occurs when the control is first loaded.
  2949. /// </summary>
  2950. [Category("Behavior"), Description("Occurs when the control is first loaded.")]
  2951. public event EventHandler Load
  2952. {
  2953. add { Events.AddHandler(_loadEventKey, value); }
  2954. remove { Events.RemoveHandler(_loadEventKey, value); }
  2955. }
  2956. /// <summary>
  2957. /// Occurs each time a recordable change occurs.
  2958. /// </summary>
  2959. [Category("Scintilla"), Description("Occurs each time a recordable change occurs.")]
  2960. public event EventHandler<MacroRecordEventArgs> MacroRecord
  2961. {
  2962. add { Events.AddHandler(_macroRecordEventKey, value); }
  2963. remove { Events.RemoveHandler(_macroRecordEventKey, value); }
  2964. }
  2965. /// <summary>
  2966. /// Occurs when the mouse was clicked inside a margin that was marked as sensitive.
  2967. /// </summary>
  2968. [Category("Scintilla"), Description("Occurs when the mouse was clicked inside a margin that was marked as sensitive.")]
  2969. public event EventHandler<MarginClickEventArgs> MarginClick
  2970. {
  2971. add { Events.AddHandler(_marginClickEventKey, value); }
  2972. remove { Events.RemoveHandler(_marginClickEventKey, value); }
  2973. }
  2974. /// <summary>
  2975. /// Occurs when one or more markers has changed in a line of text.
  2976. /// </summary>
  2977. [Category("Scintilla"), Description("Occurs when one or more markers has changed in a line of text.")]
  2978. public event EventHandler<MarkerChangedEventArgs> MarkerChanged
  2979. {
  2980. add { Events.AddHandler(_markerChangedEventKey, value); }
  2981. remove { Events.RemoveHandler(_markerChangedEventKey, value); }
  2982. }
  2983. /// <summary>
  2984. /// Occurs when the value of the <see cref="Modified" /> property has changed.
  2985. /// </summary>
  2986. [Category("Property Changed"), Description("Occurs when the value of the Modified property changes.")]
  2987. public event EventHandler ModifiedChanged
  2988. {
  2989. add { Events.AddHandler(_modifiedChangedEventKey, value); }
  2990. remove { Events.RemoveHandler(_modifiedChangedEventKey, value); }
  2991. }
  2992. /// <summary>
  2993. /// Occurs when a user tries to modify text when in read-only mode.
  2994. /// </summary>
  2995. [Category("Scintilla"), Description("Occurs when a user tries to modifiy text when in read-only mode.")]
  2996. public event EventHandler ReadOnlyModifyAttempt
  2997. {
  2998. add { Events.AddHandler(_readOnlyModifyAttemptEventKey, value); }
  2999. remove { Events.RemoveHandler(_readOnlyModifyAttemptEventKey, value); }
  3000. }
  3001. /// <summary>
  3002. /// Occurs when the control is scrolled.
  3003. /// </summary>
  3004. [Category("Action"), Description("Occurs when the control is scrolled.")]
  3005. public event EventHandler<ScrollEventArgs> Scroll
  3006. {
  3007. add { Events.AddHandler(_scrollEventKey, value); }
  3008. remove { Events.RemoveHandler(_scrollEventKey, value); }
  3009. }
  3010. /// <summary>
  3011. /// Occurs when the selection has changed.
  3012. /// </summary>
  3013. [Category("Scintilla"), Description("Occurs when the selection has changed.")]
  3014. public event EventHandler SelectionChanged
  3015. {
  3016. add { Events.AddHandler(_selectionChangedEventKey, value); }
  3017. remove { Events.RemoveHandler(_selectionChangedEventKey, value); }
  3018. }
  3019. /// <summary>
  3020. /// Occurs when the control is about to display or print text that requires styling.
  3021. /// </summary>
  3022. [Category("Scintilla"), Description("Occurs when the control is about to display or print text that requires styling.")]
  3023. public event EventHandler<StyleNeededEventArgs> StyleNeeded
  3024. {
  3025. add { Events.AddHandler(_styleNeededEventKey, value); }
  3026. remove { Events.RemoveHandler(_styleNeededEventKey, value); }
  3027. }
  3028. /// <summary>
  3029. /// Occurs when text has been removed from the document.
  3030. /// </summary>
  3031. [Category("Scintilla"), Description("Occurs when text has been removed from the document.")]
  3032. public event EventHandler<TextModifiedEventArgs> TextDeleted
  3033. {
  3034. add { Events.AddHandler(_textDeletedEventKey, value); }
  3035. remove { Events.RemoveHandler(_textDeletedEventKey, value); }
  3036. }
  3037. /// <summary>
  3038. /// Occurs when text has been inserted into the document.
  3039. /// </summary>
  3040. [Category("Scintilla"), Description("Occurs when text has been inserted into the document.")]
  3041. public event EventHandler<TextModifiedEventArgs> TextInserted
  3042. {
  3043. add { Events.AddHandler(_textInsertedEventKey, value); }
  3044. remove { Events.RemoveHandler(_textInsertedEventKey, value); }
  3045. }
  3046. /// <summary>
  3047. /// Occurs when the user zooms the display using the keyboard or the <see cref="Zoom"/> property is set.
  3048. /// </summary>
  3049. [Category("Scintilla"), Description("Occurs when the user zooms the display using the keyboard or the Zoom property is set.")]
  3050. public event EventHandler ZoomChanged
  3051. {
  3052. add { Events.AddHandler(_zoomChangedEventKey, value); }
  3053. remove { Events.RemoveHandler(_zoomChangedEventKey, value); }
  3054. }
  3055. #endregion Events
  3056. #region Constructors
  3057. public Scintilla()
  3058. {
  3059. this._state = new BitVector32(0);
  3060. this._state[_acceptsReturnState] = true;
  3061. this._state[_acceptsTabState] = true;
  3062. _ns = (INativeScintilla)this;
  3063. _caption = GetType().FullName;
  3064. // Set up default encoding to UTF-8 which is the Scintilla's best supported.
  3065. // .NET strings are UTF-16 but should be able to convert without any problems
  3066. this.Encoding = Encoding.UTF8;
  3067. // Ensure all style values have at least defaults
  3068. _ns.StyleClearAll();
  3069. _caret = new CaretInfo(this);
  3070. _lines = new LineCollection(this);
  3071. _selection = new Selection(this);
  3072. _indicators = new IndicatorCollection(this);
  3073. _snippets = new SnippetManager(this);
  3074. _margins = new MarginCollection(this);
  3075. _scrolling = new Scrolling(this);
  3076. _whitespace = new Whitespace(this);
  3077. _endOfLine = new EndOfLine(this);
  3078. _clipboard = new Clipboard(this);
  3079. _undoRedo = new UndoRedo(this);
  3080. _dropMarkers = new DropMarkers(this);
  3081. _hotspotStyle = new HotspotStyle(this);
  3082. _callTip = new CallTip(this);
  3083. _styles = new StyleCollection(this);
  3084. _indentation = new Indentation(this);
  3085. _markers = new MarkerCollection(this);
  3086. _autoComplete = new AutoComplete(this);
  3087. _documentHandler = new DocumentHandler(this);
  3088. _lexing = new Lexing(this);
  3089. _longLines = new LongLines(this);
  3090. _commands = new Commands(this);
  3091. _folding = new Folding(this);
  3092. _configurationManager = new ConfigurationManager(this);
  3093. _printing = new Printing(this);
  3094. _findReplace = new FindReplace(this);
  3095. _documentNavigation = new DocumentNavigation(this);
  3096. _goto = new GoTo(this);
  3097. _helpers.AddRange(new TopLevelHelper[]
  3098. {
  3099. _caret,
  3100. _lines,
  3101. _selection,
  3102. _indicators,
  3103. _snippets,
  3104. _margins,
  3105. _scrolling,
  3106. _whitespace,
  3107. _endOfLine,
  3108. _clipboard,
  3109. _undoRedo,
  3110. _dropMarkers,
  3111. _hotspotStyle,
  3112. _styles,
  3113. _indentation,
  3114. _markers,
  3115. _autoComplete,
  3116. _documentHandler,
  3117. _lexing,
  3118. _longLines,
  3119. _commands,
  3120. _folding,
  3121. _configurationManager,
  3122. _printing,
  3123. _findReplace,
  3124. _documentNavigation,
  3125. _goto
  3126. });
  3127. // Change from Scintilla's default black on white to
  3128. // platform defaults for edit controls.
  3129. base.BackColor = SystemColors.Window;
  3130. base.ForeColor = SystemColors.WindowText;
  3131. Styles[0].Font = Font;
  3132. Styles[0].ForeColor = ForeColor;
  3133. Styles[0].BackColor = BackColor;
  3134. Styles.Default.BackColor = BackColor;
  3135. }
  3136. #endregion Constructors
  3137. }
  3138. }