SelectionKeyProcessor.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:4k
源码类别:

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: SelectionKeyProcessor.cs,v 1.2 2005/04/20 08:45:47 okajima Exp $
  4. */
  5. using System;
  6. using System.Windows.Forms;
  7. using System.Diagnostics;
  8. using Poderosa;
  9. using Poderosa.Terminal;
  10. using Poderosa.Forms;
  11. namespace Poderosa.Text
  12. {
  13. /// <summary>
  14. /// 僥僉僗僩慖戰儌乕僪偵擖偭偨屻偱僉乕傪張棟偟偰慖戰椞堟偺僐儞僩儘乕儖傪峴偆
  15. /// </summary>
  16. internal class SelectionKeyProcessor {
  17. private TerminalPane _owner;
  18. //尰嵼偺僇乕僜儖埵抲
  19. private GLine _currentLine;
  20. private TerminalDocument _document;
  21. private int _caretPos;
  22. public TerminalPane Owner {
  23. get {
  24. return _owner;
  25. }
  26. }
  27. public GLine CurrentLine {
  28. get {
  29. return _currentLine;
  30. }
  31. }
  32. public int CaretPos {
  33. get {
  34. return _caretPos;
  35. }
  36. }
  37. //昞帵忋偺僉儍儗僢僩埵抲丅峴偺塃抂傛傝傕岦偙偆偵昞帵偟側偄傛偆偵偡傞偨傔
  38. public int UICaretPos {
  39. get {
  40. if(_caretPos>_currentLine.CharLength)
  41. return _currentLine.CharLength;
  42. else
  43. return _caretPos;
  44. }
  45. }
  46. public SelectionKeyProcessor(TerminalPane owner, TerminalDocument doc, GLine line, int pos) {
  47. _owner = owner;
  48. _document = doc;
  49. Debug.Assert(line!=null);
  50. _currentLine = line;
  51. _caretPos = pos;
  52. }
  53. public bool ProcessKey(Keys key) {
  54. Keys body = key & Keys.KeyCode;
  55. bool shift = (key & Keys.Shift) != Keys.None;
  56. bool control = (key & Keys.Control) != Keys.None;
  57. bool processed = false;
  58. //堏摦愭偺峴偲寘偺寁嶼
  59. GLine nextLine = _currentLine;
  60. _document.InvalidateLine(nextLine.ID);
  61. if(body==Keys.Up) {
  62. if(_currentLine.PrevLine!=null) nextLine = _currentLine.PrevLine;
  63. _document.InvalidateLine(nextLine.ID);
  64. processed = true;
  65. }
  66. else if(body==Keys.Down) {
  67. if(_currentLine.NextLine!=null) nextLine = _currentLine.NextLine;
  68. _document.InvalidateLine(nextLine.ID);
  69. processed = true;
  70. }
  71. else if(body==Keys.PageUp) {
  72. int n = _currentLine.ID - _owner.Connection.TerminalHeight;
  73. nextLine = n<=_document.FirstLineNumber? _document.FirstLine : _document.FindLine(n);
  74. _document.InvalidateAll();
  75. processed = true;
  76. }
  77. else if(body==Keys.PageDown) {
  78. int n = _currentLine.ID + _owner.Connection.TerminalHeight;
  79. nextLine = n>=_document.LastLineNumber? _document.LastLine : _document.FindLine(n);
  80. _document.InvalidateAll();
  81. processed = true;
  82. }
  83. int nextPos = _caretPos;
  84. if(body==Keys.Home) {
  85. nextPos = 0;
  86. processed = true;
  87. }
  88. else if(body==Keys.End) {
  89. nextPos = _currentLine.CharLength-1;
  90. processed = true;
  91. }
  92. else if(body==Keys.Left) {
  93. if(nextPos>0) {
  94. if(control)
  95. nextPos = _currentLine.FindPrevWordBreak(nextPos-1)+1;
  96. else
  97. nextPos--;
  98. }
  99. processed = true;
  100. }
  101. else if(body==Keys.Right) {
  102. if(nextPos<_currentLine.CharLength-1) {
  103. if(control)
  104. nextPos = _currentLine.FindNextWordBreak(nextPos+1);
  105. else
  106. nextPos++;
  107. }
  108. processed = true;
  109. }
  110. //慖戰椞堟偺挷惍
  111. TextSelection sel = GEnv.TextSelection;
  112. if(shift && processed) {
  113. if(sel.IsEmpty)
  114. sel.StartSelection(_owner, _currentLine, _caretPos, RangeType.Char, -1, -1);
  115. sel.ExpandTo(nextLine, nextPos, RangeType.Char);
  116. }
  117. else if(processed || body==Keys.Menu || body==Keys.ControlKey || body==Keys.ShiftKey) {
  118. if(processed)
  119. sel.Clear();
  120. processed = true;
  121. }
  122. else {
  123. //堦斒僉乕偺擖椡偑偁偭偨傜懄帪慖戰夝彍
  124. sel.Clear();
  125. }
  126. Debug.Assert(nextLine!=null);
  127. _currentLine = nextLine;
  128. _caretPos = nextPos;
  129. return processed;
  130. }
  131. }
  132. }