|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using NGettext; |
| 4 | +using R7.Webmate.Text.Models; |
| 5 | +using R7.Webmate.Xwt.Icons; |
| 6 | +using Xwt; |
| 7 | + |
| 8 | +namespace R7.Webmate.Xwt.Text |
| 9 | +{ |
| 10 | + public class DecodeHexStringWidget: Widget |
| 11 | + { |
| 12 | + protected ICatalog T = TextCatalogKeeper.GetDefault (); |
| 13 | + |
| 14 | + protected DecodeHexStringModel Model = new DecodeHexStringModel (); |
| 15 | + |
| 16 | + protected Button btnPaste; |
| 17 | + |
| 18 | + protected Button btnDecode; |
| 19 | + |
| 20 | + protected VBox vboxResults = new VBox (); |
| 21 | + |
| 22 | + protected HBox hboxGenerate = new HBox (); |
| 23 | + |
| 24 | + protected HBox hboxPaste = new HBox(); |
| 25 | + |
| 26 | + protected ScrollView scrResults; |
| 27 | + |
| 28 | + protected TextViewLabel lblSrc = new TextViewLabel (); |
| 29 | + |
| 30 | + protected CheckBox chkAutoProcess = new CheckBox (); |
| 31 | + |
| 32 | + public DecodeHexStringWidget () |
| 33 | + { |
| 34 | + var vbox = new VBox (); |
| 35 | + |
| 36 | + lblSrc.AllowQuickCopy = false; |
| 37 | + lblSrc.AllowEdit = true; |
| 38 | + |
| 39 | + btnPaste = new Button (IconHelper.GetIcon ("paste").WithSize (IconSize.Medium), T.GetString ("Paste")); |
| 40 | + btnPaste.Clicked += BtnPaste_Clicked; |
| 41 | + |
| 42 | + chkAutoProcess.Label = T.GetString ("Process on paste?"); |
| 43 | + chkAutoProcess.Active = true; |
| 44 | + |
| 45 | + hboxPaste.PackStart(btnPaste, true, true); |
| 46 | + |
| 47 | + btnDecode = new Button (IconHelper.GetIcon ("play-circle").WithSize (IconSize.Medium), T.GetString("Decode")); |
| 48 | + btnDecode.Clicked += btnDecode_Clicked; |
| 49 | + |
| 50 | + hboxGenerate.PackStart (btnDecode, true, true); |
| 51 | + |
| 52 | + scrResults = new ScrollView (vboxResults); |
| 53 | + |
| 54 | + vbox.PackStart(hboxPaste, true, true); |
| 55 | + vbox.PackStart(lblSrc, false, true); |
| 56 | + vbox.PackStart (hboxGenerate, false, true); |
| 57 | + vbox.PackStart (chkAutoProcess, false, true); |
| 58 | + vbox.PackStart (scrResults, true, true); |
| 59 | + |
| 60 | + vbox.Margin = Const.VBOX_MARGIN; |
| 61 | + Content = vbox; |
| 62 | + Content.Show (); |
| 63 | + } |
| 64 | + |
| 65 | + void BtnPaste_Clicked (object sender, EventArgs e) |
| 66 | + { |
| 67 | + Model.HexString = Clipboard.GetText () ?? string.Empty; |
| 68 | + lblSrc.Text = Model.HexString; |
| 69 | + |
| 70 | + if (chkAutoProcess.Active) { |
| 71 | + btnDecode_Clicked(sender, e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void btnDecode_Clicked (object sender, EventArgs e) |
| 76 | + { |
| 77 | + Model.HexString = lblSrc.Text; |
| 78 | + var bytes = Model.Process(); |
| 79 | + |
| 80 | + var text1 = Encoding.GetEncoding("Windows-1251").GetString(bytes); |
| 81 | + var result1 = new TextResult { |
| 82 | + Text = text1, |
| 83 | + Label = "Windows-1251" |
| 84 | + }; |
| 85 | + |
| 86 | + var text2 = Encoding.UTF8.GetString(bytes); |
| 87 | + var result2 = new TextResult { |
| 88 | + Text = text2, |
| 89 | + Label = "UTF-8" |
| 90 | + }; |
| 91 | + |
| 92 | + vboxResults.Clear(); |
| 93 | + AddResult(1, result1); |
| 94 | + AddResult(2, result2); |
| 95 | + } |
| 96 | + |
| 97 | + protected void AddResult (int index, TextResult result) |
| 98 | + { |
| 99 | + var lblResult = new TextViewLabel(); |
| 100 | + lblResult.Text = result.Text; |
| 101 | + |
| 102 | + var vboxResult = new VBox(); |
| 103 | + vboxResult.MarginLeft = Const.VBOX_MARGIN; |
| 104 | + vboxResult.MarginRight = Const.VBOX_MARGIN; |
| 105 | + vboxResult.MarginBottom = 3; |
| 106 | + vboxResult.PackStart(lblResult, false, true); |
| 107 | + |
| 108 | + var frmResult = new Frame(); |
| 109 | + |
| 110 | + frmResult.Label = string.Format(T.GetString("Result #{0} - {1}"), index, T.GetString(result.Label)); |
| 111 | + |
| 112 | + frmResult.Content = vboxResult; |
| 113 | + vboxResults.PackStart(frmResult); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments