| Tk Browserのソースコードです (C#) |
|---|
|
Tk Browserのソースコードです (C#)
Download source files: http://www.jkofsp.net/files/ 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; using System.Web; using System.Net; using System.IO; using Takaku; namespace Browser { public partial class Form1 : Form { enum TkhtTokenType { None = 0, Keyword, Word, Quote, Equal, NewSentense, } struct TkhtToken { public int Line; public TkhtTokenType Type; public string Value; public TkhtToken(int line, TkhtTokenType type, string value) { Line = line; Type = type; Value = value; } } class TkhtField { public int Line; public string Name, Value; } class TkhtBlock { public int Line; public string Class, ID; public List<TkhtField> Fields = new List<TkhtField>(); } T Last<T>(List<T> list) { return list[list.Count - 1]; } abstract class Tag_common { public string on_mouse_click, on_mouse_down, on_mouse_up, on_mouse_move, on_mouse_enter, on_mouse_leave; } class Tag_link : Tag_common { public string url; public bool newtab; } class Tag_txt : Tag_common { } class Tag_edit : Tag_common { public string name; public uint minlength; public uint maxlength; } class Tag_picture : Tag_common { } class Tag_button : Tag_common { public string submit; } class Tag_check : Tag_common { public string name; } class Tag_select : Tag_common { public string name; public string options; public string default_; } class MyWebClient : WebClient { public Uri ResponseUri; protected override WebResponse GetWebResponse(WebRequest request) { WebResponse response = null; try { response = base.GetWebResponse(request); ResponseUri = response.ResponseUri; } catch (Exception) { } return response; } } public Form1() { InitializeComponent(); string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\TkBrowser"; string file = dir + @"\lang.ini"; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); StreamReader r = new StreamReader(new FileStream(file, FileMode.OpenOrCreate), true); string lang = r.ReadToEnd().Trim().ToLower(); r.Close(); if (lang == "ja") TakakuG.CurrentLang = Lang.Ja; else if (lang == "zh_s") TakakuG.CurrentLang = Lang.Zh_s; else TakakuG.CurrentLang = Lang.En; if (TakakuG.CurrentLang == Lang.Ja) UILang.Text = "日本語"; else if (TakakuG.CurrentLang == Lang.Zh_s) UILang.Text = "中文"; else UILang.Text = "English"; UIGo.Text = TakakuG.LangStr("移動", "转到", "Go"); UIRefresh.Text = TakakuG.LangStr("更新", "刷新", "Refresh"); UISource.Text = TakakuG.LangStr("ソース", "源码", "Source"); UIHome.Text = TakakuG.LangStr("html", "html", "html"); UIAddress.Text = "http://www.jkofsp.net/bbs/"; UINewTab_Click(null, new EventArgs()); } private void UILang_DropDownClosed(object sender, EventArgs e) { if (UILang.Text == "日本語") TakakuG.CurrentLang = Lang.Ja; else if (UILang.Text == "中文") TakakuG.CurrentLang = Lang.Zh_s; else TakakuG.CurrentLang = Lang.En; string lang; if (TakakuG.CurrentLang == Lang.Ja) lang = "ja"; else if (TakakuG.CurrentLang == Lang.Zh_s) lang = "zh_s"; else lang = "en"; string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\TkBrowser"; string file = dir + @"\lang.ini"; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); StreamWriter w = new StreamWriter(file, false, Encoding.UTF8); w.Write(lang); w.Close(); MessageBox.Show(TakakuG.LangStr("再起動してください!", "请重启!", "Please restart!")); } private void UIAddress_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) Navigate(UITabs.SelectedTab, UIAddress.Text); } private void UINewTab_Click(object sender, EventArgs e) { TabPage tmp = new TabPage(); tmp.BackColor = Color.White; tmp.AutoScroll = true; tmp.Tag = null; UITabs.TabPages.Add(tmp); } private void UIGo_Click(object sender, EventArgs e) { Navigate(UITabs.SelectedTab, UIAddress.Text); } private void UIRefresh_Click(object sender, EventArgs e) { if (UITabs.SelectedTab.Tag != null) Navigate(UITabs.SelectedTab, ((Takaku.Tuple<string, string>)UITabs.SelectedTab.Tag)._1); } private void UITabs_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (UITabs.SelectedTab.Tag == null) UIAddress.Text = ""; else UIAddress.Text = ((Takaku.Tuple<string, string>)UITabs.SelectedTab.Tag)._1; } else if (e.Button == MouseButtons.Middle) { TabPage tab = null; for (int i = 0; i < UITabs.TabPages.Count; i++) if (UITabs.GetTabRect(i).Contains(e.X, e.Y)) { tab = UITabs.TabPages[i]; break; } UITabs.TabPages.Remove(tab); if (UITabs.TabPages.Count == 0) UINewTab_Click(null, new EventArgs()); } } private void UISource_Click(object sender, EventArgs e) { if (UITabs.SelectedTab.Tag != null) { Form form = new Form(); RichTextBox rtb = new RichTextBox(); form.Controls.Add(rtb); rtb.Dock = DockStyle.Fill; rtb.ReadOnly = true; rtb.Font = new Font("Lucida Console", 9.0f); rtb.Text = ((Takaku.Tuple<string, string>)UITabs.SelectedTab.Tag)._2.Replace("\r\n", "\n").Replace('\r', '\n'); int index1 = rtb.Text.IndexOf("#title"); int index2 = rtb.Text.IndexOf("#body"); rtb.Select(index1, 6); rtb.SelectionColor = Color.Red; rtb.Select(index2, 5); rtb.SelectionColor = Color.Red; rtb.Select(0, 0); rtb.SelectionChanged += new EventHandler((sender2, e2) => { form.Text = rtb.GetLineFromCharIndex(rtb.SelectionStart).ToString(); }); form.Show(); } } private void UIHome_Click(object sender, EventArgs e) { Form form = new Form(); form.Width = 640; form.Height = 480; WebBrowser web = new WebBrowser(); form.Controls.Add(web); web.Dock = DockStyle.Fill; web.Navigate("http://www.jkofsp.net/"); form.Show(); } void Navigate(TabPage tab, string url) { //UIAddress.Text = url.Trim(); string codeWebPage; try { WebClient webClient = new WebClient(); codeWebPage = /*Encoding.UTF8.GetString*/MyGetUtf8String(webClient.DownloadData(url)); } catch (Exception) { MessageBox.Show(TakakuG.LangStr("ページの読み取りのエラーです!", "页面加载错误错误!", "error loading the page!")); return; } ShowPage(tab, url.Trim(), codeWebPage); } void ShowPage(TabPage tab, string url, string codeWebPage) { UIAddress.Text = url.Trim(); Takaku.Tuple<string, string, string> splitted; try { splitted = Split(codeWebPage); } catch (Exception) { MessageBox.Show(TakakuG.LangStr("split段階の構文エラーです!", "split阶段的语法错误!", "syntax error of the phase of split!")); tab.Tag = null; return; } tab.Tag = new Takaku.Tuple<string, string>(url, codeWebPage); // これより以降にて構文エラーにしてもtab.Tagはクリアされない! つまり構文エラーにしてもページのソースコードは表示できるということ! string version = splitted._1.Trim(); string title = splitted._2.Trim(); string tkht = splitted._3.Replace("\r\n", "\n").Replace('\r', '\n'); if (version != "v1") { MessageBox.Show(TakakuG.LangStr("バージョンがエラーです! (", "版本错误! (", "version error! (") + version + ")"); return; } List<TkhtToken> tkhtTokens = new List<TkhtToken>(); int l = 0; for (int i = 0; i < tkht.Length; ) { TkhtToken tkhtToken; try { tkhtToken = TkhtLexer(tkht, ref i, ref l); } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + l.ToString() + ")"); return; } if (tkhtToken.Type == TkhtTokenType.None) // 空白のページ return; if (tkhtToken.Type == TkhtTokenType.Word) tkhtToken = TkhtCheckKeyword(tkhtToken); if (tkhtToken.Type == TkhtTokenType.Quote) tkhtToken.Value = tkhtToken.Value.Replace("\n", "\r\n"); tkhtTokens.Add(tkhtToken); } List<TkhtBlock> tkhtBlocks = new List<TkhtBlock>(); int j = 0; try { goto _End2; _Begin: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.Keyword) { if (new List<string>(new string[] { "link", "txt", "edit", "picture", "button", "check", "select" }).Contains(tkhtTokens[j].Value)) { Last(tkhtBlocks).Class = tkhtTokens[j].Value; j++; goto _Begin2; } else throw new Exception(); } else throw new Exception(); _Begin2: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.Word) { Last(tkhtBlocks).ID = tkhtTokens[j].Value; j++; goto _Begin3; } else throw new Exception(); _Begin3: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.NewSentense) { j++; goto _Field; } else throw new Exception(); _Field: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.NewSentense) { j++; goto _Field; } else if (tkhtTokens[j].Type == TkhtTokenType.Word) { Last(tkhtBlocks).Fields.Add(new TkhtField()); Last(Last(tkhtBlocks).Fields).Line = tkhtTokens[j].Line; Last(Last(tkhtBlocks).Fields).Name = tkhtTokens[j].Value; j++; goto _Field2; } else if (tkhtTokens[j].Type == TkhtTokenType.Keyword && tkhtTokens[j].Value == "end") { j++; goto _End; } else throw new Exception(); _Field2: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.Equal) { j++; goto _Field3; } else throw new Exception(); _Field3: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.Word || tkhtTokens[j].Type == TkhtTokenType.Quote) { Last(Last(tkhtBlocks).Fields).Value = tkhtTokens[j].Value; j++; goto _Field4; } else throw new Exception(); _Field4: if (j >= tkhtTokens.Count) { throw new Exception(); } else if (tkhtTokens[j].Type == TkhtTokenType.NewSentense) { j++; goto _Field; } else throw new Exception(); _End: if (j >= tkhtTokens.Count) { goto _Break; } else if (tkhtTokens[j].Type == TkhtTokenType.NewSentense) { j++; goto _End2; } else throw new Exception(); _End2: if (j >= tkhtTokens.Count) { goto _Break; } else if (tkhtTokens[j].Type == TkhtTokenType.NewSentense) { j++; goto _End2; } else if (tkhtTokens[j].Type == TkhtTokenType.Keyword && tkhtTokens[j].Value == "begin") { j++; tkhtBlocks.Add(new TkhtBlock()); Last(tkhtBlocks).Line = tkhtTokens[j].Line; goto _Begin; } else throw new Exception(); _Break: ; } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtTokens[Math.Min(tkhtTokens.Count - 1, j)].Line.ToString() + ")"); return; } tab.Text = title; tab.Controls.Clear(); for (int k = 0; k < tkhtBlocks.Count; k++) { switch (tkhtBlocks[k].Class) { case "link": TextBox link = new TextBox(); link.Name = tkhtBlocks[k].ID.Trim(); link.Tag = new Tag_link(); link.Multiline = false; link.WordWrap = false; link.BorderStyle = BorderStyle.FixedSingle; link.ReadOnly = true; link.BackColor = Color.White; link.Click += new EventHandler(link_Click); int h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": link.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": link.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": link.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": link.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "text": link.Text = tkhtBlocks[k].Fields[h].Value; break; case "back_color": link.BackColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_color": link.ForeColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_family": link.Font = new Font(tkhtBlocks[k].Fields[h].Value.Trim(), link.Font.Size); break; case "font_size": link.Font = new Font(link.Font.FontFamily, float.Parse(tkhtBlocks[k].Fields[h].Value)); break; case "url": ((Tag_link)link.Tag).url = tkhtBlocks[k].Fields[h].Value.Trim(); break; case "newtab": ((Tag_link)link.Tag).newtab = bool.Parse(tkhtBlocks[k].Fields[h].Value); break; case "on_mouse_click": ((Tag_common)link.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)link.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)link.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)link.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)link.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)link.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } link.Select(0, 0); tab.Controls.Add(link); break; case "txt": TextBox txt = new TextBox(); txt.Name = tkhtBlocks[k].ID.Trim(); txt.Tag = new Tag_txt(); txt.Multiline = true; // txtは既定で多行! txt.WordWrap = false; txt.ScrollBars = ScrollBars.Both; txt.BorderStyle = BorderStyle.FixedSingle; txt.ReadOnly = true; txt.BackColor = Color.White; h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": txt.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": txt.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": txt.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": txt.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "text": txt.Text = tkhtBlocks[k].Fields[h].Value; break; case "back_color": txt.BackColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_color": txt.ForeColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_family": txt.Font = new Font(tkhtBlocks[k].Fields[h].Value.Trim(), txt.Font.Size); break; case "font_size": txt.Font = new Font(txt.Font.FontFamily, float.Parse(tkhtBlocks[k].Fields[h].Value)); break; case "multiline": txt.Multiline = bool.Parse(tkhtBlocks[k].Fields[h].Value); break; case "on_mouse_click": ((Tag_common)txt.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)txt.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)txt.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)txt.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)txt.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)txt.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } txt.Select(0, 0); tab.Controls.Add(txt); break; case "edit": TextBox edit = new TextBox(); edit.Name = tkhtBlocks[k].ID.Trim(); edit.Tag = new Tag_edit(); edit.WordWrap = false; edit.ScrollBars = ScrollBars.Both; // Multiline == falseでは無効! (だから単行のeditをスクロールバーなしのtxtとして使うこと!) edit.BorderStyle = BorderStyle.FixedSingle; edit.ReadOnly = false; edit.BackColor = Color.White; ((Tag_edit)edit.Tag).maxlength = 32767; h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": edit.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": edit.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": edit.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": edit.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "text": edit.Text = tkhtBlocks[k].Fields[h].Value; break; case "back_color": edit.BackColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_color": edit.ForeColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_family": edit.Font = new Font(tkhtBlocks[k].Fields[h].Value.Trim(), edit.Font.Size); break; case "font_size": edit.Font = new Font(edit.Font.FontFamily, float.Parse(tkhtBlocks[k].Fields[h].Value)); break; case "multiline": edit.Multiline = bool.Parse(tkhtBlocks[k].Fields[h].Value); break; case "password": edit.UseSystemPasswordChar = bool.Parse(tkhtBlocks[k].Fields[h].Value); break; case "name": ((Tag_edit)edit.Tag).name = tkhtBlocks[k].Fields[h].Value.Trim(); break; case "minlength": ((Tag_edit)edit.Tag).minlength = uint.Parse(tkhtBlocks[k].Fields[h].Value); break; case "maxlength": ((Tag_edit)edit.Tag).maxlength = uint.Parse(tkhtBlocks[k].Fields[h].Value); break; case "on_mouse_click": ((Tag_common)edit.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)edit.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)edit.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)edit.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)edit.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)edit.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } edit.Select(0, 0); tab.Controls.Add(edit); break; case "picture": PictureBox picture = new PictureBox(); picture.Name = tkhtBlocks[k].ID.Trim(); picture.Tag = new Tag_picture(); picture.SizeMode = PictureBoxSizeMode.StretchImage; h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": picture.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": picture.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": picture.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": picture.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "src": picture.ImageLocation = tkhtBlocks[k].Fields[h].Value.Trim(); break; case "on_mouse_click": ((Tag_common)picture.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)picture.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)picture.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)picture.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)picture.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)picture.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } tab.Controls.Add(picture); break; case "button": Button button = new Button(); button.Name = tkhtBlocks[k].ID.Trim(); button.Tag = new Tag_button(); button.Click += new EventHandler(button_Click); h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": button.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": button.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": button.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": button.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "text": button.Text = tkhtBlocks[k].Fields[h].Value; break; case "back_color": button.BackColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_color": button.ForeColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_family": button.Font = new Font(tkhtBlocks[k].Fields[h].Value.Trim(), button.Font.Size); break; case "font_size": button.Font = new Font(button.Font.FontFamily, float.Parse(tkhtBlocks[k].Fields[h].Value)); break; case "submit": ((Tag_button)button.Tag).submit = tkhtBlocks[k].Fields[h].Value.Trim(); break; case "on_mouse_click": ((Tag_common)button.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)button.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)button.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)button.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)button.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)button.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } tab.Controls.Add(button); break; case "check": CheckBox check = new CheckBox(); check.Name = tkhtBlocks[k].ID.Trim(); check.Tag = new Tag_check(); check.FlatStyle = FlatStyle.Flat; h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": check.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": check.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": check.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": check.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "text": check.Text = tkhtBlocks[k].Fields[h].Value; break; case "back_color": check.BackColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_color": check.ForeColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_family": check.Font = new Font(tkhtBlocks[k].Fields[h].Value.Trim(), check.Font.Size); break; case "font_size": check.Font = new Font(check.Font.FontFamily, float.Parse(tkhtBlocks[k].Fields[h].Value)); break; case "checked": check.Checked = bool.Parse(tkhtBlocks[k].Fields[h].Value); break; case "name": ((Tag_check)check.Tag).name = tkhtBlocks[k].Fields[h].Value.Trim(); break; case "on_mouse_click": ((Tag_common)check.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)check.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)check.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)check.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)check.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)check.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } tab.Controls.Add(check); break; case "select": ComboBox select = new ComboBox(); select.DropDownStyle = ComboBoxStyle.DropDownList; select.Tag = new Tag_select(); select.FlatStyle = FlatStyle.Flat; h = 0; try { for (h = 0; h < tkhtBlocks[k].Fields.Count; h++) { switch (tkhtBlocks[k].Fields[h].Name) { case "x": select.Left = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "y": select.Top = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dx": select.Width = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "dy": select.Height = int.Parse(tkhtBlocks[k].Fields[h].Value); break; case "text": select.Text = tkhtBlocks[k].Fields[h].Value; break; case "back_color": select.BackColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_color": select.ForeColor = Color.FromArgb(0xFF, Color.FromArgb((int)Convert.ToUInt32(tkhtBlocks[k].Fields[h].Value, 16))); break; case "font_family": select.Font = new Font(tkhtBlocks[k].Fields[h].Value.Trim(), select.Font.Size); break; case "font_size": select.Font = new Font(select.Font.FontFamily, float.Parse(tkhtBlocks[k].Fields[h].Value)); break; case "options": ((Tag_select)select.Tag).options = tkhtBlocks[k].Fields[h].Value; break; case "default": ((Tag_select)select.Tag).default_ = tkhtBlocks[k].Fields[h].Value; break; case "name": ((Tag_select)select.Tag).name = tkhtBlocks[k].Fields[h].Value.Trim(); break; case "on_mouse_click": ((Tag_common)select.Tag).on_mouse_click = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_down": ((Tag_common)select.Tag).on_mouse_down = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_up": ((Tag_common)select.Tag).on_mouse_up = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_move": ((Tag_common)select.Tag).on_mouse_move = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_enter": ((Tag_common)select.Tag).on_mouse_enter = tkhtBlocks[k].Fields[h].Value; break; case "on_mouse_leave": ((Tag_common)select.Tag).on_mouse_leave = tkhtBlocks[k].Fields[h].Value; break; } } } catch (Exception) { MessageBox.Show(TakakuG.LangStr("tkhtの構文エラーです! (行: ", "tkhk的语法错误! (行: ", "syntax error of tkht! (line: ") + tkhtBlocks[k].Fields[h].Line.ToString() + ")"); tab.Controls.Clear(); return; } if (((Tag_select)select.Tag).options != null) select.Items.AddRange(((Tag_select)select.Tag).options.Split('|')); if (((Tag_select)select.Tag).default_ != null) select.Text = ((Tag_select)select.Tag).default_; tab.Controls.Add(select); break; } } } |
|
Takaku.Tuple<string, string, string> Split(string x)
{ int index1 = x.IndexOf("#title"); int index2 = x.IndexOf("#body"); if (index1 < 0 || index2 < 0) throw new Exception(); if (index1 > index2) throw new Exception(); return new Takaku.Tuple<string, string, string>(x.Substring(0, index1), x.Substring(index1 + 6, index2 - index1 - 6), x.Substring(index2 + 5)); } TkhtToken TkhtLexer(string s, ref int i, ref int l) { TkhtToken t = new TkhtToken(l, TkhtTokenType.None, ""); _Start: if (i >= s.Length) { t.Value = null; return t; } else if (s[i] == ' ' || s[i] == '\t' || s[i] == ' ') { i++; goto _Start; } else if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '_') { t.Value += s[i]; i++; goto _Word; } // 負の数、小数は``で括ること(例 `-0.1`) else if (s[i] == '`') { i++; goto _Quote; } else if (s[i] == '=') { i++; t.Value = null; t.Type = TkhtTokenType.Equal; return t; } else if (s[i] == '\n') { i++; l++; t.Value = null; t.Type = TkhtTokenType.NewSentense; return t; } else { throw new Exception(); } _Word: if (i >= s.Length) { t.Type = TkhtTokenType.Word; return t; } else if (s[i] == ' ' || s[i] == '\t' || s[i] == ' ') { i++; t.Type = TkhtTokenType.Word; return t; } if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '_') { t.Value += s[i]; i++; goto _Word; } else if (s[i] == '\n') { t.Type = TkhtTokenType.Word; return t; } else { throw new Exception(); } _Quote: if (i >= s.Length) { t.Type = TkhtTokenType.Quote; return t; } // `は、複数行のため、tkhtの最後まで、ペアにならなくても許される! else if (s[i] == '`') { i++; t.Type = TkhtTokenType.Quote; return t; } else if (s[i] == '\n') { t.Value += s[i]; i++; l++; goto _Quote; } else { t.Value += s[i]; i++; goto _Quote; } } TkhtToken TkhtCheckKeyword(TkhtToken x) { if (x.Type != TkhtTokenType.Word) throw new Exception(); if (new List<string>(new string[] { "begin", "end", "link", "txt", "edit", "picture", "button", "check", "select" }).Contains(x.Value)) { x.Type = TkhtTokenType.Keyword; return x; } return x; } void Post(TabPage tab, string url) { //UIAddress.Text = url.Trim(); List<Takaku.Tuple<string, string>> formEdits = new List<Takaku.Tuple<string, string>>(); List<string> formChecks = new List<string>(); List<Takaku.Tuple<string, string>> formSelects = new List<Takaku.Tuple<string, string>>(); foreach (Control i in tab.Controls) { if (i.Tag is Tag_edit && (!string.IsNullOrEmpty(((Tag_edit)i.Tag).name))) formEdits.Add(new Takaku.Tuple<string, string>(((Tag_edit)i.Tag).name, ((TextBox)i).Text)); else if (i.Tag is Tag_check && ((CheckBox)i).Checked && (!string.IsNullOrEmpty(((Tag_check)i.Tag).name))) formChecks.Add(((Tag_check)i.Tag).name); else if (i.Tag is Tag_select && ((ComboBox)i).Text != "") formSelects.Add(new Takaku.Tuple<string, string>(((Tag_select)i.Tag).name, ((ComboBox)i).Text)); } string postStringEdits = formEdits.Count == 0 ? "" : TakakuG.For(formEdits, (a, b) => a + '&' + b, i => i._1 + '=' + Uri.EscapeDataString(i._2)); string postStringChecks = formChecks.Count == 0 ? "" : TakakuG.For(formChecks, (a, b) => a + '&' + b, i => i + '='); string postStringSelects = formSelects.Count == 0 ? "" : TakakuG.For(formSelects, (a, b) => a + '&' + b, i => i._1 + '=' + Uri.EscapeDataString(i._2)); List<string> postString_ = new List<string>(); if (postStringEdits != "") postString_.Add(postStringEdits); if (postStringChecks != "") postString_.Add(postStringChecks); if (postStringSelects != "") postString_.Add(postStringSelects); string postString = postString_.Count == 0 ? "" : TakakuG.For(postString_, (a, b) => a + '&' + b, i => i); //UIAddress.Text = url.Trim(); string url_; string ret; try { MyWebClient wc = new MyWebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); ret = /*Encoding.UTF8.GetString*/MyGetUtf8String(wc.UploadData(url, "POST", Encoding.ASCII.GetBytes(postString))); url_ = wc.ResponseUri.ToString(); //UIAddress.Text = url_; } catch (Exception) { MessageBox.Show(TakakuG.LangStr("ページの読み取りのエラーです!", "页面加载错误错误!", "error loading the page!")); return; } ShowPage(tab, url_.Trim(), ret); } void link_Click(object s, EventArgs e) { TextBox link = (TextBox)s; TabPage tab = (TabPage)link.Parent; Tag_link tag = (Tag_link)link.Tag; TabPage tab_; if (tag.newtab) { UINewTab_Click(null, new EventArgs()); tab_ = UITabs.TabPages[UITabs.TabPages.Count - 1]; } else { tab_ = tab; } Navigate(tab_, tag.url); } void button_Click(object s, EventArgs e) { Button button = (Button)s; TabPage tab = (TabPage)button.Parent; Tag_button tag = (Tag_button)button.Tag; if (!string.IsNullOrEmpty(((Tag_button)button.Tag).submit)) { foreach (Control i in tab.Controls) { if (i.Tag is Tag_edit) { TextBox _i = (TextBox)i; Tag_edit _t = (Tag_edit)i.Tag; if (_i.UseSystemPasswordChar) foreach (char c in _i.Text) if (c >= 128) { _i.SelectAll(); _i.Focus(); MessageBox.Show(TakakuG.LangStr("パスワードは半角文字で入力してください!", "请输入半角密码!", "please input half-width password!")); return; } if (_i.Text.Length < _t.minlength || _i.Text.Length > _t.maxlength) { _i.SelectAll(); _i.Focus(); MessageBox.Show(TakakuG.LangStr("入力の長さが間違っています! (", "输入长度出错! (", "input length error! (") + _t.minlength.ToString() + '-' + _t.maxlength.ToString() + ')'); return; } } else if (i.Tag is Tag_select) { ComboBox _i = (ComboBox)i; if (_i.Text == "") { _i.Focus(); MessageBox.Show(TakakuG.LangStr("リストから選択してください!", "请从列表中选择!", "please select one from list!")); return; } } } Post(tab, tag.submit); } } string MyGetUtf8String(byte[] bytes) { if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3); else return Encoding.UTF8.GetString(bytes); } } } |