-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTextbox.cs
More file actions
1627 lines (1517 loc) · 51.4 KB
/
MyTextbox.cs
File metadata and controls
1627 lines (1517 loc) · 51.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyLibrary
{
/// <summary>
/// 自作テキストボックス
/// ・ハイパーリンク
/// ・
///
/// TODO:スクロール
/// TODO:コピー&ペースト
/// </summary>
public class MyTextbox : System.Windows.Forms.Control
{
#region Constants
#endregion
#region delegate
public delegate void MyMouseEventHandler(object sender, MyMouseEventArgs e);
public delegate void MyTextboxInfoEventHandler(object sender, MyTextboxInfoEventArgs e);
public delegate void MyTaskEventHandler(object sender, TaskEventArgs e);
#endregion
#region Event
public event MyMouseEventHandler MyMouseMove;
public event MyMouseEventHandler MyMouseDown;
public event MyTextboxInfoEventHandler MyTextboxInfoEvent;
public event MyTaskEventHandler MyTaskEvent;
#endregion
#region property
/// <summary>
/// 行数
/// </summary>
public int Count
{
get
{
return this.list.Count;
}
}
/// <summary>
/// これが位置情報の主体。キャレットはあくまで現在地の目安。
///
/// キャレットはPhysicalPosで管理。キャレット部分が画面外に行く可能性があるため。
/// </summary>
private LogicalPosition _currentlogicalpos;
private PhysicalPosition _currentphysicalpos;
/*
/// <summary>
///
/// </summary>
public Position CurrentPhysicalPosition
{
set
{
try
{
MyLine line = list[value.Line];
if (value.Pos > line.Length)
value.Pos = line.Length;
else if (value.Pos < 0)
value.Pos = 0;
_CurrentPhysicalPosition = value;
Point point = Position2Point(_CurrentPhysicalPosition);
caret.SetPos(point.X, point.Y);
if (MyTextboxInfoEvent != null)
{
MyTextboxInfoEvent(this, SetInfoEventArgs());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
get
{
return _CurrentPhysicalPosition;
}
}
*/
public PhysicalPosition CurrentPhysicalPosition
{
set
{
try
{
if (value.Line > (list.Count - 1))
{
value.Line = (list.Count - 1);
}
MyLine line = list[value.Line];
// もし、行の長さよりも大きいPosが指定されたら、行の最後の位置にする。
if (value.Pos > line.Length) {
value.Pos = line.Length;
}
else if (value.Pos < 0)
{
value.Pos = 0;
}
_currentphysicalpos = value;
SetCaretPos();
if (MyTextboxInfoEvent != null)
{
MyTextboxInfoEvent(this, SetInfoEventArgs());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"MyTextbox.CurrentPhysicalPosition");
}
}
get
{
return _currentphysicalpos;
}
}
/// <summary>
/// キャレットの位置をCurrentPhysicalPositionに設定する
/// </summary>
private void SetCaretPos()
{
Point point = LogicalPosition2Point(PhysicalPosition2LogicalPosition(CurrentPhysicalPosition));
caret.SetPos(point.X, point.Y);
}
/// <summary>
///
/// </summary>
public LogicalPosition CurrentLogicalPosition
{
set
{
CurrentPhysicalPosition = LogicalPosition2PhysicalPosition(value);
}
get
{
return PhysicalPosition2LogicalPosition(CurrentPhysicalPosition);
}
}
/// <summary>
/// 現在の行
/// </summary>
public MyLine CurrentPhysicalLine
{
get
{
return list[CurrentPhysicalPosition.Line];
}
}
#endregion
#region variable
/// <summary>
/// 行のリスト
/// </summary>
List<MyLine> list;
/// <summary>
/// デフォルトの改行コード
/// </summary>
MyReturnCode _returnCode;
/// <summary>
/// キャレット
/// </summary>
Caret caret;
List<Task> history;
#endregion
#region override
/// <summary>
/// 文字列の設定/取得
/// 外部との文字列のやりとりはこことInsert?
/// </summary>
public override string Text
{
set
{
list = String2MyLineList(value);
Replace task = new Replace();
history.Add(task);
if (MyTaskEvent != null)
{
TaskEventArgs args = new TaskEventArgs();
args.task = task;
MyTaskEvent(this, args);
}
}
get
{
string s = "";
for (int i = 0; i < list.Count; i++)
{
s += list[i].Text;
}
return s;
}
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private List<MyLine> String2MyLineList(string str)
{
// オリジナルの改行コードを保持する必要がある。
List<MyLine> tmp = new List<MyLine>();
tmp.Add(new MyLine(str,new MyReturnCode(returnCode.NULL)));
tmp = LineSplitter(new List<MyLine>(), tmp, new MyReturnCode(returnCode.CRLF));
tmp = LineSplitter(new List<MyLine>(), tmp, new MyReturnCode(returnCode.CR));
tmp = LineSplitter(new List<MyLine>(), tmp, new MyReturnCode(returnCode.LF));
return tmp;
}
/// <summary>
/// List<MyLine>の中のMyLine中にある文字列の改行コードを見つけて、MyLineを再帰的に分割する。
/// </summary>
/// <param name="ret">処理し終えた部分</param>
/// <param name="tmp">未処理部分</param>
/// <param name="returnCode"></param>
/// <returns></returns>
private List<MyLine> LineSplitter(List<MyLine> ret, List<MyLine> tmp, MyReturnCode returnCode)
{
// もう処理するものが無かったら、終わり
if (tmp.Count == 0) return ret;
else
{
// 未処理の配列の先頭行を取得し、未処理配列から消す。
MyLine a = tmp[0];
tmp.RemoveAt(0);
// 文字列をreturnCodeで分割する。
string[] arr = MyLibrary.MyStringTool.StringSplitter(a.TextWithoutReturnValue, returnCode.ToString());
// 分割した文字列を1つずつ配列に変換する。
for(int i = 0;i<arr.Length;++i)
{
ret.Add(new MyLine(arr[i], returnCode));
}
// 処理中配列の最後の要素の改行コードはreturnCodeではなく、a.ReturnCodeである。
ret[ret.Count - 1].ReturnCode = a.ReturnCode;
return LineSplitter(ret, tmp, returnCode);
}
}
/// <summary>
/// WndProc
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
// LogWriter logWriter = new LogWriter(@"C:\log\MyTextbox.txt");
// logWriter.Write(m.ToString());
IntPtr hIMC;
switch (m.Msg)
{
case NativeMethods.WM_CHAR:
hIMC = NativeMethods.ImmGetContext(this.Handle);
if (NativeMethods.ImmGetOpenStatus(hIMC) == 0)
{
char chr = Convert.ToChar(m.WParam.ToInt32() & 0xff);
Insert(chr.ToString());
// caret.SetPos(caret.GetPos().X + , caret.GetPos().Y);
}
NativeMethods.ImmReleaseContext(this.Handle, hIMC);
Invalidate();
break;
case NativeMethods.WM_IME_STARTCOMPOSITION:
hIMC = NativeMethods.ImmGetContext(this.Handle);
// ImmSetCompositionWindowとImmSetCandidateWindowがしっかり機能しているのかがイマイチ分からない。
// 変換ウィンドウの位置を設定
NativeMethods.COMPOSITIONFORM cf = new NativeMethods.COMPOSITIONFORM();
cf.dwStyle = NativeMethods.CFS_POINT;
cf.ptCurrentPos = new Point(100, 0);
cf.rcArea = new Rectangle();
NativeMethods.ImmSetCompositionWindow(hIMC, ref cf);
// 候補文字ウィンドウの位置調整を行う
NativeMethods.CANDIDATEFORM lpCandidate = new NativeMethods.CANDIDATEFORM();
lpCandidate.dwIndex = 0;
lpCandidate.dwStyle = NativeMethods.CFS_CANDIDATEPOS;
lpCandidate.ptCurrentPos = new Point(10, 50);
NativeMethods.ImmSetCandidateWindow(hIMC, ref lpCandidate);
NativeMethods.ImmReleaseContext(this.Handle, hIMC);
break;
case NativeMethods.WM_IME_COMPOSITION:
this.ImeComposition(m);
break;
case NativeMethods.WM_IME_ENDCOMPOSITION:
break;
case NativeMethods.WM_IME_NOTIFY:
switch (m.WParam.ToInt32())
{
case NativeMethods.IMN_OPENCANDIDATE:
// 候補文字ウィンドウが表示された
this.SetCandidateWindowPos(m.HWnd);
break;
case NativeMethods.IMN_CLOSECANDIDATE:
case NativeMethods.IMN_CHANGECANDIDATE:
case NativeMethods.IMN_SETOPENSTATUS:
// 何もしない。
break;
}
break;
}
base.WndProc(ref m);
}
/// <summary>
///
/// </summary>
List<Task> deleteCandidate = new List<Task>();
/// <summary>
///
/// </summary>
/// <param name="m"></param>
private void ImeComposition(Message m)
{
for (int i = 0; i < deleteCandidate.Count; i++)
{
Delete(deleteCandidate[i].fromLine, deleteCandidate[i].fromPos, deleteCandidate[i].length);
}
deleteCandidate.Clear();
char[] buf;
int len;
if ((m.LParam.ToInt32() & NativeMethods.GCS_RESULTSTR) != 0)
{
// 文字列の変換状態が全て確定した
// よって、確定文字の取得
IntPtr hImc = NativeMethods.ImmGetContext(m.HWnd);
// 必要なメモリサイズを取得
len = NativeMethods.ImmGetCompositionString(hImc, NativeMethods.GCS_RESULTSTR, null, 0);
buf = new char[len];
NativeMethods.ImmGetCompositionString(hImc, NativeMethods.GCS_RESULTSTR, buf, (uint)len);
string s = new string(buf);
s = s.Replace("\0", "");
Insert(s);
Invalidate();
}
else if ((m.LParam.ToInt32() & NativeMethods.GCS_COMPSTR) != 0)
{
IntPtr hImc = NativeMethods.ImmGetContext(m.HWnd);
// 必要なメモリサイズを取得
len = NativeMethods.ImmGetCompositionString(hImc, NativeMethods.GCS_COMPSTR, null, 0);
buf = new char[len];
NativeMethods.ImmGetCompositionString(hImc, NativeMethods.GCS_COMPSTR, buf, (uint)len);
string s = new string(buf);
s = s.Replace("\0", "");
Task task = Insert(s);
deleteCandidate.Add(task);
}
Invalidate();
return;
}
/// <summary>
/// 候補文字ウィンドウの位置を設定
/// </summary>
/// <param name="hWnd"></param>
private void SetCandidateWindowPos(IntPtr hWnd)
{
IntPtr hImc = NativeMethods.ImmGetContext(hWnd);
if (hImc != null)
{
NativeMethods.CANDIDATEFORM cndFrm = new NativeMethods.CANDIDATEFORM();
cndFrm.ptCurrentPos.X = caret.GetPos().X;
cndFrm.ptCurrentPos.Y = caret.GetPos().Y;
cndFrm.dwStyle = NativeMethods.CFS_CANDIDATEPOS;// 候補文字ウィンドウの位置を指定する。
NativeMethods.ImmSetCandidateWindow(hImc, ref cndFrm);
NativeMethods.ImmReleaseContext(hWnd, hImc);
}
}
/// <summary>
/// LogicalPositionからPhysicalPositionに変換する
/// </summary>
/// <param name="logicalPosition"></param>
/// <returns></returns>
private PhysicalPosition LogicalPosition2PhysicalPosition(LogicalPosition logicalPosition)
{
return new PhysicalPosition(logicalPosition.Line + GetShowFirstLine(), logicalPosition.Pos);
}
/// <summary>
///
/// </summary>
/// <param name="physicalPosition"></param>
/// <returns></returns>
private LogicalPosition PhysicalPosition2LogicalPosition(PhysicalPosition physicalPosition)
{
return new LogicalPosition(physicalPosition.Line - GetShowFirstLine(), physicalPosition.Pos);
}
/// <summary>
/// OnPaint
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
float x = 0F;
float y = 0F;
for (int nLine = GetShowFirstLine(); nLine < list.Count; nLine++)
{
MyLine line = list[nLine];
for (int nPos = 0; nPos < line.Length; nPos++)
{
MyChar c = line[nPos];
e.Graphics.DrawString(c.ToString(), c.Font, c.Brush, x, y);
x += c.Width;
}
x = 0F;
y += line.Height;
}
base.OnPaint(e);
}
/// <summary>
/// OnGotFocus
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(EventArgs e)
{
this.caret.ShowCaret();
this.BackColor = Color.LightBlue;
base.OnGotFocus(e);
}
/// <summary>
/// OnLostFocus
/// </summary>
/// <param name="e"></param>
protected override void OnLostFocus(EventArgs e)
{
this.caret.HideCaret();
this.BackColor = Color.White;
base.OnLostFocus(e);
}
/// <summary>
/// OnMouseDown
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
// 文字ごとにサイズが異なることを考慮しないといけない。
// 結構めんどうそうだ・・・まずは表示させるところからやるか・・・
// CurrentPhysicalPosition = Point2Position(new Point(e.X, e.Y));
CurrentLogicalPosition = Point2LogicalPosition(new Point(e.X, e.Y));
if (MyMouseDown != null)
{
MyMouseEventArgs myMouseEventArgs = new MyMouseEventArgs();
myMouseEventArgs.X = LogicalPosition2Point(CurrentLogicalPosition).X;
myMouseEventArgs.Y = LogicalPosition2Point(CurrentLogicalPosition).Y;
myMouseEventArgs.nLine = CurrentLogicalPosition.Line;
myMouseEventArgs.nPos = CurrentLogicalPosition.Pos;
MyMouseDown(this, myMouseEventArgs);
}
this.Focus();
base.OnMouseDown(e);
}
private bool isShowLine(MyLine line)
{
return false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (MyMouseMove != null)
{
MyMouseEventArgs myMouseEventArgs = new MyMouseEventArgs();
myMouseEventArgs.X = e.X;
myMouseEventArgs.Y = e.Y;
myMouseEventArgs.nLine = Point2Position(new Point(e.X, e.Y)).Line;
myMouseEventArgs.nPos = Point2Position(new Point(e.X, e.Y)).Pos;
MyMouseMove(this, myMouseEventArgs);
}
base.OnMouseMove(e);
}
/// <summary>
/// 通常はこのコントロールで補足しないようなキーが押された場合にも発生するイベント
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
// すべてのキーが補足されるようにする。これをしないと矢印キーなどが補足されない。
e.IsInputKey = true;
base.OnPreviewKeyDown(e);
}
/// <summary>
/// キーが押された時。特定のキーが押された時の動作を定義する。
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line - 1, CurrentPhysicalPosition.Pos);
e.SuppressKeyPress = true;
break;
case Keys.Down:
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line + 1, CurrentPhysicalPosition.Pos);
e.SuppressKeyPress = true;
break;
case Keys.Right:
if (CurrentPhysicalPosition.Pos == CurrentPhysicalLine.Length)
{
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line + 1, 0);
}
else
{
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line, CurrentPhysicalPosition.Pos + 1);
}
e.SuppressKeyPress = true;
break;
case Keys.Left:
if (CurrentPhysicalPosition.Pos == 0)
{
// pos == 0にいる時は、上の行の一番後ろに移動
MyLine upperLine = list[CurrentPhysicalPosition.Line - 1];
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line - 1, upperLine.Length);
}
else
{
// 左隣に移動
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line, CurrentPhysicalPosition.Pos - 1);
}
e.SuppressKeyPress = true;
break;
case Keys.Enter:
// 現在の行(Enterキーを押した行)
MyLine currentLine = list[CurrentPhysicalPosition.Line];
// 新しく作られる行。現在の行のEnterキーを押した場所(キャレットの位置)から後ろの文字列と現在の行の改行コードを移す。
MyLine newLine = new MyLine(currentLine.TextWithoutReturnValue.Substring(CurrentPhysicalPosition.Pos),currentLine.ReturnCode);
// 現在の行のEnterキーを押した場所(キャレットの位置)から後ろの文字列を削除する。
currentLine.Delete(CurrentPhysicalPosition.Pos, currentLine.Length - CurrentPhysicalPosition.Pos);
// 現在の行の改行コードにはMyTextboxのデフォルトの改行コードを入れる。
currentLine.ReturnCode = this._returnCode;
// リストに新しい行を追加する。
list.Insert(CurrentPhysicalPosition.Line + 1, newLine);
// キャレットの位置を新しく作成した行の先頭に設定する。
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line + 1, 0);
// Keyイベントをコントロールに渡さない場合はtrue。これをtrueにすると、同時にHandledもtrueとなる。
e.SuppressKeyPress = true;
// 再描画する。
Invalidate();
break;
case Keys.Back:
// 1文字deleteする。キャレット操作もDelete()でやる。
this.Delete(CurrentPhysicalPosition.Line, CurrentPhysicalPosition.Pos - 1, 1);
e.SuppressKeyPress = true;
break;
case Keys.Tab:
break;
case Keys.Delete:
break;
case Keys.Home:
break;
case Keys.End:
break;
}
//base.OnKeyDown(e);
}
protected override void OnMove(EventArgs e)
{
vScrollBar.Size = new Size(vScrollBar.Width, this.Height - vScrollBar.Width);
hScrollBar.Size = new Size(this.Width - hScrollBar.Height, hScrollBar.Height);
base.OnMove(e);
}
#endregion
#region ScrollBar
/// <summary>
/// 縦スクロールバー
/// </summary>
VScrollBar vScrollBar;
/// <summary>
/// 横スクロールバー
/// </summary>
HScrollBar hScrollBar;
#endregion
/// <summary>
///
/// </summary>
public MyTextbox()
{
vScrollBar = new VScrollBar();
hScrollBar = new HScrollBar();
vScrollBar.Dock = DockStyle.Right;
hScrollBar.Dock = DockStyle.Bottom;
vScrollBar.Value = 0;
vScrollBar.Minimum = 0;
vScrollBar.Scroll += vScrollBar_Scroll;
vScrollBar.ValueChanged += vScrollBar_ValueChanged;
this.Controls.Add(vScrollBar);
this.Controls.Add(hScrollBar);
list = new List<MyLine>();
MyLine firstLine = new MyLine();
list.Add(firstLine);
this.Font = MyTextboxDefaultValue.Font;
this._returnCode = MyTextboxDefaultValue.ReturnCode;
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(MyTextbox_DragDrop);
caret = new Caret(this.Handle, 2, this.Font.Height);
CurrentPhysicalPosition = new PhysicalPosition(0, 0);
history = new List<Task>();
}
void vScrollBar_ValueChanged(object sender, EventArgs e)
{
}
void vScrollBar_Scroll(object sender, ScrollEventArgs e)
{
SetCaretPos();
if (MyTextboxInfoEvent != null)
{
MyTextboxInfoEvent(this, SetInfoEventArgs());
}
Invalidate();
}
/// <summary>
/// デストラクタ
/// </summary>
~MyTextbox()
{
}
/// <summary>
/// クライアントエリアに表示すべき最初の行を取得する
/// </summary>
/// <returns></returns>
private int GetShowFirstLine()
{
return vScrollBar.Value;
}
/// <summary>
///
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
private LogicalPosition Point2LogicalPosition(Point point)
{
int nLine = 0;
int pos = 0;
float testHeight = 0;
for (int i = GetShowFirstLine();; i++)
{
if (i > (list.Count-1))
{
nLine = list.Count - 1;
break;
}
testHeight += list[i].Height;
if (point.Y < testHeight)
{
nLine = i;
break;
}
}
MyLine line = list[nLine];
if (point.X < 0)
pos = 0;
else if (point.X > line.Width)
pos = line.Length;
else
{
for (int i = 0; i < line.Length; i++)
{
float testWidth = line.GetWidth(0, i);
if (point.X < (int)testWidth)
{
// 文字列のサイズの方が上回ったらそこがポジション
// 文字の真ん中だった場合とか細かいことはまだ考慮してない。
// とりあえずこんな感じだろうとテストしてみる。
pos = i;
MyChar c = line[i];
float t = c.Width / 2.0F;
float k = testWidth - t;
if (point.X > (int)k)
{
pos += 1;
}
break;
}
}
}
PhysicalPosition po = new PhysicalPosition(nLine, pos);
return PhysicalPosition2LogicalPosition(po);
}
/// <summary>
/// ピクセルから何行目何列かを取得する
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public Position Point2Position(Point point)
{
int nLine = 0;
int pos = 0;
float testHeight = 0;
for (int i = 0; i < list.Count; i++)
{
testHeight += list[i].Height;
if (point.Y < testHeight)
{
nLine = i;
break;
}
}
MyLine line = list[nLine];
if (point.X < 0)
pos = 0;
else if (point.X > line.Width)
pos = line.Length;
else
{
for (int i = 0; i < line.Length; i++)
{
float testWidth = line.GetWidth(0, i);
if (point.X < (int)testWidth)
{
// 文字列のサイズの方が上回ったらそこがポジション
// 文字の真ん中だった場合とか細かいことはまだ考慮してない。
// とりあえずこんな感じだろうとテストしてみる。
pos = i;
MyChar c = line[i];
float t = c.Width / 2.0F;
float k = testWidth - t;
if (point.X > (int)k)
{
pos += 1;
}
break;
}
}
}
using(System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\log\point2position.txt",true))
{
string s = string.Format("X={0}, Y={1}, nLine={2}, pos={3}",point.X,point.Y,nLine,pos);
// sw.WriteLine(s);
}
return new Position(nLine, pos);
}
/// <summary>
///
/// </summary>
/// <param name="logicalposition"></param>
/// <returns></returns>
private Point LogicalPosition2Point(LogicalPosition logicalposition)
{
float testHeight = 0F;
int nlogical = 0;
for (; ;)
{
if (nlogical >= logicalposition.Line)
break;
testHeight += list[nlogical + GetShowFirstLine()].Height;
++nlogical;
}
int y = (int)testHeight;
int x;
if (logicalposition.Line < 0)
{
x = -1;
}
else
{
MyLine line = list[logicalposition.Line + GetShowFirstLine()];
if (logicalposition.Pos == 0)
x = 0;
else
x = (int)line.GetWidth(0, logicalposition.Pos - 1);// 前の文字までの長さを取得
}
return new Point(x, y);
}
/// <summary>
///
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
private bool isShowLine(PhysicalPosition position)
{
if (position.Line < GetShowFirstLine())
{
// ClientAreaより上に行っちゃって、表示されない。
return false;
}
else if (position.Line > GetShowLastLine())
{
// ClientAreaより下に行っちゃって、表示されない。
return false;
}
else
{
return true;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private int GetShowLastLine()
{
float sumOfHeight = 0F;
int i;
for (i = GetShowFirstLine(); ; ++i)
{
if (i > (list.Count-1))
break;
sumOfHeight += list[i].Height;
if (sumOfHeight > ClientSize.Height)
break;
}
return i;
}
/// <summary>
///
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
private Point PhysicalPosition2Point(PhysicalPosition position)
{
float sumOfHeight = 0F;
for (int i = 0; i < position.Line; i++)
{
sumOfHeight += list[i].Height;
}
int y = (int)sumOfHeight;
MyLine line = list[position.Line];
int x;
if (position.Pos == 0)
x = 0;
else
x = (int)line.GetWidth(0, position.Pos - 1);// 前の文字までの長さを取得
return new Point(x, y);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private Point ScrollValue2PhysicalPos(int value)
{
Position pos = Point2Position(new Point(0, value));
float testHeight = 0F;
for (int i = 0; i < pos.Line; i++)
{
testHeight += list[i].Height;
}
return new Point(0,(int)testHeight);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyTextbox_DragDrop(object sender, DragEventArgs e)
{
}
/// <summary>
///
/// </summary>
public new Size ClientSize
{
get
{
return new Size(base.ClientSize.Width - vScrollBar.Width, base.ClientSize.Height - hScrollBar.Height);
}
}
private MyTextboxInfoEventArgs SetInfoEventArgs()
{
MyTextboxInfoEventArgs args = null;
try
{
args = new MyTextboxInfoEventArgs();
args.nPhysicalLine = CurrentPhysicalPosition.Line;
args.nPhysicalPos = CurrentPhysicalPosition.Pos;
args.nLogicalLine = CurrentLogicalPosition.Line;
args.nLogicalPos = CurrentLogicalPosition.Pos;
args.CurrentLineLength = CurrentPhysicalLine.Length;
args.vScrollValue = vScrollBar.Value;
args.clinetSize = this.ClientSize;
args.ShowFirstLine = GetShowFirstLine();
args.ShowLastLine = GetShowLastLine();
}
catch(Exception)
{
}
return args;
}
/// <summary>
/// 新たに文字列を設定する。
/// </summary>
/// <param name="arr"></param>
public void Set(string[] arr)
{
list = new List<MyLine>();
for (int i = 0; i < arr.Length; i++)
{
this.Insert(i, 0, arr[i]);
}
}
/// <summary>
/// 現在の位置に文字列を挿入
/// </summary>
/// <param name="str"></param>
public Task Insert(string str)
{
return Insert(CurrentPhysicalPosition.Line, CurrentPhysicalPosition.Pos, str);
}
/// <summary>
/// 任意の位置に文字列を挿入
/// </summary>
/// <param name="nLine">Line number. 0 origin</param>
/// <param name="pos"></param>
/// <param name="str"></param>
public Task Insert(int nLine, int pos, string str)
{
//nLineがまだ存在しない行番号だった場合に備えて、その行までの存在しない行を全て作成
// 例えば、現在0-4行目が存在、8行目に文字を挿入する場合、
// 5-8行目を作成する。
for (int i = list.Count; i < nLine + 1; i++)
{
MyLine newLine = new MyLine();
list.Add(newLine);
}
MyLine line = list[nLine];
line.Insert(pos, str);
// 任意の位置に挿入するのだから、キャレットとは違う位置に行を増やさないように挿入した時にはキャレットに影響は無い。従って、現状ではバグがある。
CurrentPhysicalPosition = new PhysicalPosition(CurrentPhysicalPosition.Line, CurrentPhysicalPosition.Pos + str.Length);