root/lang/csharp/soyText/soyText/EditorTab.cs @ 33260

Revision 33260, 10.7 kB (checked in by hoge1e3, 4 years ago)

タブコントロールの準備

Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.Text.RegularExpressions;
10
11namespace SoyText
12{
13    public delegate void TitleChanged(string title);
14    public partial class EditorTab : UserControl
15    {
16        public TitleChanged titleChanged;
17        public TabPage tabPage;
18        public Workspace workspace;
19        Document _curDoc;
20        public Document curDoc
21        {
22            get { return _curDoc; }
23            set { open(value); }
24        }
25        static int inst = 0;
26        public static EditorTab defaultInstance;
27        //String _statusText = "";
28        Queue<SyncUIAction> uiActionQueue = new Queue<SyncUIAction>();
29        public void onLoad(object sender, EventArgs e)
30        {
31            documentSet.getRecents();
32            newDocument();
33            refreshList();
34        }
35        /*public String statusText
36        {
37            set
38            {
39                _statusText = value;
40            }
41        }*/
42        public EditorTab(Workspace w)
43        {
44            workspace = w;
45            InitializeComponent();
46            //curDoc = documentSet.createDocument();
47            inst++;
48            defaultInstance = this;
49        }
50
51        private void content_TextChanged(object sender, EventArgs e)
52        {
53            if (curDoc == null) return;
54            var wasNew = curDoc.isNew;
55            curDoc.content = content.Text;
56            curDoc.save();
57            if (wasNew && !docListBox.Items.Contains(curDoc))
58            {
59                docListBox.Items.Insert(0, curDoc);
60            }
61        }
62
63        IEnumerator<int> listings;
64        public DocumentSet documentSet
65        {
66            get { return workspace.documentSet; }
67        }
68        public void refreshList()
69        {
70            refreshList(null);
71        }
72        TerminableThread searchThread = new TerminableThread();
73        TerminableThread lineSearchThread = new TerminableThread();
74        DocumentSercher documentSearcher;
75        public void refreshList(String kw)
76        {
77            if (kw == "" || kw==null) Text = "soyText";
78            else Text = kw + " - " + "soyText";
79            if (titleChanged != null) titleChanged(kw);
80            if (documentSearcher != null) documentSearcher.terminate();
81            searchThread.exec(delegate(TerminableThread t)
82            {
83                var cnt = 0;
84                syncUI(delegate()
85                {
86                    docListBox.Items.Clear();
87                });
88                documentSearcher = documentSet.createDocumentSearcher(kw,
89                delegate(Document d)
90                {
91                    cnt++;
92                    if (cnt < 100)
93                    {
94                        syncUI(delegate() { docListBox.Items.Add(d); });
95                        return FoundHandlerAction.Accept;
96                    }
97                    else
98                    {
99                        return FoundHandlerAction.End;
100                    }
101                });
102                documentSearcher.search();
103                Debug.print("完了");
104            });
105        }
106        public void syncUI(SyncUIAction u)
107        {
108            uiActionQueue.Enqueue(u);
109        }
110        public void newDocument()
111        {
112            var d = documentSet.createDocument();
113            if (command.Text != "")
114            {
115                var lines = Regex.Split(d.content, "\r*\n");
116                d.content = lines[0] + "\r\ntag: " + command.Text + "\r\n" + "\r\n";
117            }
118            open(d);
119
120            content.Focus();
121            content.SelectionStart = content.TextLength;
122
123        }
124        private void newButton_Click(object sender, EventArgs e)
125        {
126            newDocument();
127
128            //content.GetFirstCharIndexFromLine
129        }
130        bool autoSresMove = true;
131        private void open(Document newDoc)
132        {
133            _curDoc = null;
134            content.Text = newDoc.content;
135            _curDoc = newDoc;
136            //curDocLastUpdate = curDoc.lastUpdate;
137            //command.Text = newDoc.lastUpdate+"";
138            if (command.Text != "")
139            {
140                lineSearchThread.exec(delegate(TerminableThread t)
141                {
142                    if (autoSresMove && splitContainerSres.SplitterDistance < 64)
143                    {
144                        syncUI(delegate()
145                        {
146                            splitContainerSres.SplitterDistance = 64;
147                        });
148                    }
149                    var sc = SearchCondition.fromExpression(command.Text);
150                    syncUI(delegate() { sresListBox.Items.Clear(); });
151                    foreach (var l in curDoc.lines)
152                    {
153                        var l2 = l;
154                        if (sc.matches(l))
155                        {
156                            syncUI(delegate() { sresListBox.Items.Add(l2); });
157                        }
158                    }
159                });
160            }
161        }
162
163        private void command_KeyDown(object sender, KeyEventArgs e)
164        {
165            if (e.KeyCode.Equals(Keys.Enter))
166            {
167                autoSresMove = true;
168                refreshList(command.Text);
169
170            }
171
172        }
173
174        int testTimer = 0;
175        public void timer1_Tick(object sender, EventArgs e)
176        {
177           
178            status.Text = Debug.statusText;
179            testTimer++;
180            if (listings != null)
181            {
182                if (listings.MoveNext())
183                {
184
185                }
186                else
187                {
188                    listings = null;
189                }
190            }
191            long t = System.Environment.TickCount;
192            while (uiActionQueue.Count > 0 && System.Environment.TickCount - t < 17)
193            {
194                var r=uiActionQueue.Dequeue();
195                r.Invoke();
196            }
197        }
198        bool seling = false;
199        private void docListBox_SelectedIndexChanged(object sender, EventArgs e)
200        {
201            if (seling) return;
202            if (docListBox.SelectedIndex < 0) return;
203            Document d = (Document)docListBox.Items[docListBox.SelectedIndex];
204            seling = true;
205            docListBox.Items[docListBox.SelectedIndex] = d;
206            seling = false;
207            open(d);
208            //         open(new Document(Int32.Parse(docListBox.Text + "")));
209
210        }
211
212        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
213        {
214            inst--;
215            if (inst == 0) Application.Exit();
216        }
217        TerminableThread tth = new TerminableThread();
218        private void Form1_KeyDown(object sender, KeyEventArgs e)
219        {
220            if (e.KeyCode.Equals(Keys.F11))
221            {
222                /*new Thread(delegate()
223                {
224                    var oldActive = documentSet.getOldestActiveDocument();
225                    workspace.indexer.make(oldActive);
226                }).Start();*/
227            }
228            if (e.KeyCode.Equals(Keys.F9))
229            {
230                var p = new PythonEngine();
231                var res = p.evaluate(this , curDoc.parsed.cdr);
232                //Debug.print(res);
233                Text = res;
234                /*
235                tth.exec(delegate(TerminableThread t)
236                {
237                    for (var i = 0; i < 20; i++)
238                    {
239                        Debug.print("TTH " + i);
240                        Thread.Sleep(500);
241                        if (t.terminated) break;
242                    }
243                });
244                Text = curDoc.parsed.getValue("title");*/
245            }
246        }
247
248        private void NewMenuItem_Click(object sender, EventArgs e)
249        {
250            newDocument();
251        }
252
253        /*private void ワークスペースWToolStripMenuItem_Click(object sender, EventArgs e)
254        {
255            var w = new WorkspaceSelector();
256            w.Visible = true;
257        }
258
259        private void アーカイブAToolStripMenuItem_Click(object sender, EventArgs e)
260        {
261            curDoc.state = DocumentState.archived;
262            docListBox.Items.Remove(curDoc);
263
264        }
265
266        private void 新規NToolStripMenuItem_Click(object sender, EventArgs e)
267        {
268            var f = new Form1(workspace);
269            f.Visible = true;
270        }*/
271        public void newWindow()
272        {
273            var f = new Form1(workspace);
274            f.Visible = true;
275
276        }
277
278        /*private void インポートToolStripMenuItem_Click(object sender, EventArgs e)
279        {
280            var i = new ImportDialog(workspace);
281            i.Visible = true;
282        }*/
283
284        private void command_TextChanged(object sender, EventArgs e)
285        {
286
287        }
288        /*
289        private void 最新の情報に更新RToolStripMenuItem_Click(object sender, EventArgs e)
290        {
291            refreshList(command.Text);
292        }*/
293
294        private void splitContainerSres_SplitterMoved(object sender, SplitterEventArgs e)
295        {
296            autoSresMove = false;
297        }
298
299        private void topPanel_MouseClick(object sender, MouseEventArgs e)
300        {
301            //status.Text = "[" + (testTimer & 255) + "] " + _statusText;
302            testTimer++;
303            if (listings != null)
304            {
305                if (listings.MoveNext())
306                {
307
308                }
309                else
310                {
311                    listings = null;
312                }
313            }
314            long t = System.Environment.TickCount;
315            while (uiActionQueue.Count > 0 && System.Environment.TickCount - t < 17)
316            {
317                var r = uiActionQueue.Dequeue();
318                r.Invoke();
319            }
320        }
321
322        private void sresListBox_SelectedIndexChanged(object sender, EventArgs e)
323        {
324            if (sresListBox.SelectedIndex >= sresListBox.Items.Count ||
325                sresListBox.SelectedIndex < 0) return;
326            var line = ((DocumentLine)sresListBox.Items[sresListBox.SelectedIndex]);
327            content.SelectionStart = line.document.GetFirstCharIndexFromLine(line.lineNo);
328            content.ScrollToCaret();
329            content.Focus();
330        }
331       // long curDocLastUpdate;
332        private void Form1_Activated(object sender, EventArgs e)
333        {
334            if (curDoc == null) return;
335            if (content.Text != curDoc.content)
336            {
337                content.Text = curDoc.content;
338            }
339
340        }
341    }
342}
Note: See TracBrowser for help on using the browser.