0:   // MiscKeys.cs 
1:   // Copyright (C) 2000 Mike Krueger
2:   // Copyright (C) 2000 Andrea Paatz
3:   // Copyright (C) 2000 Jeppe Cramon
4:   //
5:   // This program is free software; you can redistribute it and/or modify
6:   // it under the terms of the GNU General Public License as published by
7:   // the Free Software Foundation; either version 2 of the License, or
8:   // (at your option) any later version.
9:   //
10:   // This program is distributed in the hope that it will be useful,
11:   // but WITHOUT ANY WARRANTY; without even the implied warranty of
12:   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13:   // GNU General Public License for more details.
14:   //
15:   // You should have received a copy of the GNU General Public License
16:   // along with this program; if not, write to the Free Software
17:   // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18:  
19:   using System.Drawing;
20:   using System.Windows.Forms;
21:   using System;
22:  
23:   using SharpDevelop.Tool.Data;
24:   using SharpDevelop.Gui.Edit.Text;
25:   using SharpDevelop.Gui.Edit.Text.TemplateCompletion;
26:   using SharpDevelop.Actions;
27:   using SharpDevelop.Internal.Text;
28:   using SharpDevelop.Gui.Window;
29:  
30:   namespace SharpDevelop.Actions.Edit {
31:  
32:       public class Tab : ISdEditAction
33:       {
34:           public void Execute(ISdEditActionExecutor executor)
35:           {
36:               if (executor.TextArea.Buffer.ReadOnly)
37:                   return;
38:               
39:               if (executor.TextArea.Selection.HasSomethingSelected) {
40:                   executor.TextArea.Buffer.BeginUpdate();
41:                   
42:                   int min executor.TextArea.Selection.RealStart.Y;
43:                   int max executor.TextArea.Selection.RealEnd.Y;
44:                   if (executor.TextArea.Selection.RealEnd.== 0)
45:                       --max;
46:                   
47:                   for (int min<= max; ++i
48:                       executor.TextArea.Buffer.Insert(new Point(0i), "\t");
49:                   
50:                   executor.TextArea.Buffer.UndoStack.UndoLast(max min 1);
51:                   
52:                   executor.TextArea.Buffer.EndUpdate();
53:                   executor.TextArea.UpdateLines(minmax);
54:               else {
55:                   executor.TextArea.ClipboardHandler.Delete(nullnull);
56:                   
57:                   bool overwritten false;
58:                   
59:                   if (!executor.TextArea.Caret.InsertMode && executor.TextArea.Caret.CaretPos.< executor.TextArea.Buffer[executor.TextArea.Caret.CaretPos.Y].Text.Length) {
60:                       executor.TextArea.Buffer.Delete(executor.TextArea.Caret.CaretPosnew Point(executor.TextArea.Caret.CaretPos.1executor.TextArea.Caret.CaretPos.Y));
61:                       overwritten true;
62:                   }
63:                   
64:                   Point pos executor.TextArea.Buffer.Insert(executor.TextArea.Caret.CaretPos"\t");
65:                   executor.TextArea.Caret.CaretPos  pos;
66:                   executor.TextArea.Caret.SetUpDownPos();
67:                   
68:                   executor.TextArea.UpdateLines(pos.Ypos.Y);
69:                   if (overwritten)
70:                       executor.TextArea.UndoStack.UndoLast(2);
71:                   
72:               }
73:           }
74:       }
75:       
76:       public class ShiftTab : ISdEditAction
77:       {
78:           public void Execute(ISdEditActionExecutor executor)
79:           {
80:               if (executor.TextArea.Buffer.ReadOnly)
81:                   return;
82:               
83:               if (executor.TextArea.Selection.HasSomethingSelected) {
84:                   executor.TextArea.Buffer.BeginUpdate();
85:                   int min executor.TextArea.Selection.RealStart.Y;
86:                   int max executor.TextArea.Selection.RealEnd.Y;
87:                   
88:                   if (executor.TextArea.Selection.RealEnd.== 0)
89:                       --max;
90:                   
91:                   for (int min<= max; ++i)
92:                       if (executor.TextArea.Buffer[i].Text.Length > && executor.TextArea.Buffer[i].Text[0] == '\t') {
93:                           executor.TextArea.Buffer.Delete(new Point(0i), new Point(1i));
94:                       }
95:                   
96:                   executor.TextArea.Buffer.UndoStack.UndoLast(max min 1);
97:                   
98:                   executor.TextArea.Buffer.EndUpdate();
99:                   executor.TextArea.UpdateLines(minmax);
100:               else {
101:                   Point pos executor.TextArea.Caret.CaretPos;
102:                   Point opos pos;
103:                   pos.Math.Max(0pos.executor.TextArea.Options.TabIndent);
104:                   executor.TextArea.Caret.CaretPos pos;
105:                   executor.TextArea.Caret.SetUpDownPos();
106:               }
107:           }
108:       }
109:       
110:       public class Backspace : ISdEditAction
111:       {
112:           public void Execute(ISdEditActionExecutor executor)
113:           {
114:               if (executor.TextArea.Buffer.ReadOnly)
115:                   return;
116:               
117:               Point pos executor.TextArea.Selection.RealStart;
118:               int min   pos.Y;
119:               
120:               if (executor.TextArea.Selection.HasSomethingSelected) {
121:                   executor.TextArea.ClipboardHandler.Delete(nullnull);
122:                   return;
123:               }
124:               
125:               pos executor.TextArea.Caret.CaretPos;
126:               if (pos.> 0) {
127:                   --pos.X;
128:                   executor.TextArea.Buffer.Delete(posnew Point(pos.1pos.Y));
129:                   executor.TextArea.UpdateLines(pos.Ypos.Y);
130:                   executor.TextArea.Caret.CaretPos pos;
131:               }
132:               else
133:                   if (pos.> 0) {
134:                       pos new Point(executor.TextArea.Buffer[pos.1].Text.Lengthpos.1);
135:                       executor.TextArea.Buffer.Delete(posnew Point(0pos.1));
136:                       executor.TextArea.UpdateToEnd(pos.Y);
137:                       executor.TextArea.Caret.CaretPos pos;
138:                   }
139:               executor.TextArea.Caret.SetUpDownPos();
140:           }
141:       }
142:       
143:       public class Delete : ISdEditAction
144:       {
145:           public void Execute(ISdEditActionExecutor executor)
146:           {
147:               if (executor.TextArea.Buffer.ReadOnly)
148:                   return;
149:               
150:               if (executor.TextArea.Selection.HasSomethingSelected) {
151:                   executor.TextArea.ClipboardHandler.Delete(nullnull);
152:                   return;
153:               }
154:               Point pos executor.TextArea.Caret.CaretPos;
155:               if (pos.< executor.TextArea.Buffer[pos.Y].Text.Length) {
156:                   executor.TextArea.Buffer.Delete(posnew Point(pos.1pos.Y));
157:                   executor.TextArea.UpdateLines(pos.Ypos.Y);
158:               else 
159:                   if (pos.< executor.TextArea.Buffer.Length 1) {
160:                       executor.TextArea.Buffer.Delete(posnew Point(0pos.1));
161:                       executor.TextArea.UpdateToEnd(pos.Y);
162:                   }
163:           }
164:       }
165:       
166:       public class Cut : ISdEditAction
167:       {
168:           public void Execute(ISdEditActionExecutor executor)
169:           {
170:               executor.TextArea.ClipboardHandler.Cut(nullnull);
171:           }
172:       }
173:       
174:       public class Copy : ISdEditAction
175:       {
176:           public void Execute(ISdEditActionExecutor executor)
177:           {
178:               executor.TextArea.ClipboardHandler.Copy(nullnull);
179:           }
180:       }
181:  
182:       public class Paste : ISdEditAction
183:       {
184:           public void Execute(ISdEditActionExecutor executor)
185:           {
186:               executor.TextArea.ClipboardHandler.Paste(nullnull);
187:           }
188:       }
189:  
190:       public class ReloadBuffer : ISdEditAction
191:       {
192:           public void Execute(ISdEditActionExecutor executor)
193:           {
194:               executor.TextArea.ReloadFile();
195:               executor.TextArea.Refresh();
196:           }
197:       }
198:       
199:       public class ToggleEditMode : ISdEditAction
200:       {
201:           public void Execute(ISdEditActionExecutor executor)
202:           {
203:               executor.TextArea.Caret.InsertMode = !executor.TextArea.Caret.InsertMode;
204:               executor.TextArea.Caret.CaretPos executor.TextArea.Caret.CaretPos;
205:           }
206:       }
207:       
208:       public class Return : ISdEditAction
209:       {    
210:           public void Execute(ISdEditActionExecutor executor)
211:           {
212:               if (executor.TextArea.Buffer.ReadOnly) {
213:                   return;
214:               }
215:               
216:               executor.TextArea.ClipboardHandler.Delete(nullnull);
217:               
218:               Point pos executor.TextArea.Buffer.Insert(executor.TextArea.Caret.CaretPos"\n");
219:               
220:               pos.Indent.IndentLine(executor.TextArea.Bufferpos.Yexecutor.TextArea.Options.IndentStyle);
221:               pos.CommentTagInserter.InsertCommentTag(executor.TextArea.Bufferpos.Ypos.X);
222:               
223:               if (executor.TextArea.Options.AutoInsertCurlyBracket && 
224:                   executor.TextArea.Buffer[executor.TextArea.Caret.CaretPos.Y].Text.EndsWith("{")) {
225:                           
226:                   executor.TextArea.Buffer.Insert(pos"\n}");
227:                   if (pos.< executor.TextArea.Buffer.Length) {
228:                       Indent.IndentLine(executor.TextArea.Bufferpos.1executor.TextArea.Options.IndentStyle);
229:                   }
230:                   if (pos.< executor.TextArea.Buffer.Length) {
231:                       Indent.IndentLine(executor.TextArea.Bufferpos.2executor.TextArea.Options.IndentStyle);
232:                   }
233:               }
234:                   
235:               executor.TextArea.Caret.CaretPos pos;
236:               executor.TextArea.UpdateToEnd(pos.1);
237:               executor.TextArea.Caret.SetUpDownPos();
238:           }
239:       }
240:       
241:       public class IndentSelection : ISdEditAction
242:       {
243:           public void Execute(ISdEditActionExecutor executor)
244:           {
245:               if (executor.TextArea.Buffer.ReadOnly
246:                   return;
247:               if (executor.TextArea.Selection.HasSomethingSelected) {
248:                   int y1 executor.TextArea.Selection.RealStart.Y;
249:                   int y2 executor.TextArea.Selection.RealEnd.Y;
250:                   Indent.IndentLines(executor.TextArea.Buffery1y2executor.TextArea.Options.IndentStyle);
251:                   executor.TextArea.Refresh(); // TODO : LineUpdate
252:               }
253:           }
254:       }
255:       
256:       public class TextAreaOptions : ISdEditAction
257:       {
258:           public void Execute(ISdEditActionExecutor executor)
259:           {
260:               new SharpDevelop.Gui.Dialogs.OptionsDialog("Buffer Options"executor.TextArea.Options).ShowDialog();
261:               executor.TextArea.Refresh();
262:           }
263:       }
264:       
265:       public class ToggleComment : ISdEditAction
266:       {
267:           public void Execute(ISdEditActionExecutor executor)
268:           {
269:               if (executor.TextArea.Buffer.ReadOnly
270:                   return;
271:               
272:               executor.TextArea.Buffer.BeginUpdate();
273:               int y1 executor.TextArea.Selection.RealStart.Y;
274:               int y2 executor.TextArea.Selection.RealEnd.Y;
275:               
276:               if (!executor.TextArea.Selection.HasSomethingSelected)
277:                   y1 y2 executor.TextArea.Caret.CaretPos.Y;
278:               
279:               if (y2 y1 > 0) {
280:                   if (executor.TextArea.Selection.SelectionEnd.== && executor.TextArea.Selection.SelectionEnd.== y2)
281:                       --y2;
282:                   if (executor.TextArea.Selection.SelectionStart.== && executor.TextArea.Selection.SelectionStart.== y2)
283:                       --y2;
284:               }
285:               
286:               for (int y1<= y2; ++i) {
287:                   if (executor.TextArea.Buffer[i].Text.Length > && executor.TextArea.Buffer[i].Text[0] == '/' && executor.TextArea.Buffer[i].Text[1] == '/')
288:                       executor.TextArea.Buffer.Delete(new Point(0i), new Point(2i));
289:                   else
290:                       executor.TextArea.Buffer.Insert(new Point(0i), "//");
291:               }
292:               executor.TextArea.Buffer.EndUpdate();
293:               executor.TextArea.UpdateLines(y1y2);
294:           }
295:       }
296:       
297:       public class MovePageDown : ISdEditAction
298:       {
299:           public virtual void Execute(ISdEditActionExecutor executor)
300:           {
301:               Point pos executor.TextArea.Caret.CaretPos;
302:               if (pos.< executor.TextArea.Buffer.Length executor.TextArea.lastVisibleLine) {
303:                   pos.executor.TextArea.Caret.GetFirstValidPosGreater(pos.executor.TextArea.lastVisibleLine);
304:                   pos.executor.TextArea.Caret.UpDownPos;
305:               }else{
306:                   pos.executor.TextArea.Caret.GetFirstValidPosLower(executor.TextArea.Buffer.Length);
307:               
308:               executor.TextArea.Caret.PhysicalCaretPos pos;
309:           }
310:       }
311:       
312:       public class MovePageUp : ISdEditAction
313:       {
314:           public virtual void Execute(ISdEditActionExecutor executor)
315:           {
316:               Point pos executor.TextArea.Caret.CaretPos;
317:               if (pos.> executor.TextArea.lastVisibleLine){
318:                   pos.executor.TextArea.Caret.GetFirstValidPosLower(