PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/bless-0.6.0/src/gui/plugins/ConversionTablePlugin.cs

#
C# | 551 lines | 389 code | 107 blank | 55 comment | 62 complexity | ea26fd16fbff4a4272aafc4e87299b46 MD5 | raw file
Possible License(s): GPL-2.0
  1. // created on 2/18/2005 at 3:24 PM
  2. /*
  3. * Copyright (c) 2005, Alexandros Frantzis (alf82 [at] freemail [dot] gr)
  4. *
  5. * This file is part of Bless.
  6. *
  7. * Bless is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Bless is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Bless; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. using System;
  22. using System.Text;
  23. using Gtk;
  24. using Bless.Util;
  25. using Bless.Tools;
  26. using Bless.Gui;
  27. using Bless.Buffers;
  28. using Bless.Plugins;
  29. using Mono.Unix;
  30. namespace Bless.Gui.Plugins {
  31. public class ConversionTablePlugin : GuiPlugin
  32. {
  33. DataBook dataBook;
  34. ConversionTable widget;
  35. Window mainWindow;
  36. ToggleAction conversionTableAction;
  37. UIManager uiManager;
  38. const string uiXml =
  39. "<menubar>" +
  40. " <menu action=\"Tools\">" +
  41. " <menuitem name=\"ConversionTable\" action=\"ConversionTableAction\" />" +
  42. " </menu>" +
  43. "</menubar>";
  44. public ConversionTablePlugin(Window mw, UIManager uim)
  45. {
  46. mainWindow = mw;
  47. uiManager = uim;
  48. name = "ConversionTable";
  49. author = "Alexandros Frantzis";
  50. description = "Convert";
  51. }
  52. public override bool Load()
  53. {
  54. dataBook = (DataBook)GetDataBook(mainWindow);
  55. widget = new ConversionTable(dataBook);
  56. WidgetGroup wg1 = (WidgetGroup)GetWidgetGroup(mainWindow, 1);
  57. wg1.Add(widget);
  58. AddMenuItems(uiManager);
  59. Preferences.Proxy.Subscribe("Tools.ConversionTable.Show", "ct1", new PreferencesChangedHandler(OnPreferencesChanged));
  60. loaded = true;
  61. return true;
  62. }
  63. private void AddMenuItems(UIManager uim)
  64. {
  65. ToggleActionEntry[] toggleActionEntries = new ToggleActionEntry[] {
  66. new ToggleActionEntry ("ConversionTableAction", null, Catalog.GetString("Conversion Table"), null, null,
  67. new EventHandler(OnViewConversionTableToggled), false),
  68. };
  69. ActionGroup group = new ActionGroup ("ConversionTableActions");
  70. group.Add (toggleActionEntries);
  71. uim.InsertActionGroup(group, 0);
  72. uim.AddUiFromString(uiXml);
  73. conversionTableAction = (ToggleAction)uim.GetAction("/menubar/Tools/ConversionTable");
  74. uim.EnsureUpdate();
  75. }
  76. ///<summary>Handle the View->Conversion Table command</summary>
  77. public void OnViewConversionTableToggled(object o, EventArgs args)
  78. {
  79. Preferences.Proxy.Change("Tools.ConversionTable.Show", conversionTableAction.Active.ToString(), "ct1");
  80. }
  81. void OnPreferencesChanged(Preferences prefs)
  82. {
  83. if (prefs["Tools.ConversionTable.Show"] == "True")
  84. conversionTableAction.Active = true;
  85. else
  86. conversionTableAction.Active = false;
  87. }
  88. }
  89. ///<summary> A widget to convert the data at the current offset to various types</summary>
  90. public class ConversionTable: Gtk.HBox
  91. {
  92. [Glade.Widget] Gtk.Table ConversionTableWidget;
  93. [Glade.Widget] Gtk.Entry Signed8bitEntry;
  94. [Glade.Widget] Gtk.Entry Unsigned8bitEntry;
  95. [Glade.Widget] Gtk.Entry Signed16bitEntry;
  96. [Glade.Widget] Gtk.Entry Unsigned16bitEntry;
  97. [Glade.Widget] Gtk.Entry Signed32bitEntry;
  98. [Glade.Widget] Gtk.Entry Unsigned32bitEntry;
  99. [Glade.Widget] Gtk.Entry Float32bitEntry;
  100. [Glade.Widget] Gtk.Entry Float64bitEntry;
  101. [Glade.Widget] Gtk.Entry HexadecimalEntry;
  102. [Glade.Widget] Gtk.Entry DecimalEntry;
  103. [Glade.Widget] Gtk.Entry OctalEntry;
  104. [Glade.Widget] Gtk.Entry BinaryEntry;
  105. [Glade.Widget] Gtk.Entry AsciiEntry;
  106. [Glade.Widget] Gtk.CheckButton LittleEndianCheckButton;
  107. [Glade.Widget] Gtk.CheckButton UnsignedAsHexCheckButton;
  108. DataBook dataBook;
  109. bool littleEndian;
  110. bool unsignedAsHex;
  111. public ConversionTable(DataBook db)
  112. {
  113. Glade.XML gxml = new Glade.XML (FileResourcePath.GetDataPath("bless.glade"), "ConversionTableWidget", "bless");
  114. gxml.Autoconnect (this);
  115. littleEndian = true;
  116. unsignedAsHex = false;
  117. dataBook = db;
  118. foreach(DataViewDisplay dvd in dataBook.Children) {
  119. OnDataViewAdded(dvd.View);
  120. }
  121. dataBook.PageAdded += new DataView.DataViewEventHandler(OnDataViewAdded);
  122. dataBook.Removed += new RemovedHandler(OnDataViewRemoved);
  123. dataBook.SwitchPage += new SwitchPageHandler(OnSwitchPage);
  124. Preferences.Proxy.Subscribe("Tools.ConversionTable.Show", "ct2", new PreferencesChangedHandler(OnPreferencesChanged));
  125. this.Add(ConversionTableWidget);
  126. this.ShowAll();
  127. }
  128. void OnDataViewAdded(DataView dv)
  129. {
  130. dv.Buffer.Changed += new ByteBuffer.ChangedHandler(OnBufferContentsChanged);
  131. dv.BufferChanged += new DataView.DataViewEventHandler(OnBufferChanged);
  132. dv.CursorChanged += new DataView.DataViewEventHandler(OnCursorChanged);
  133. }
  134. void OnDataViewRemoved(object o, RemovedArgs args)
  135. {
  136. DataView dv = ((DataViewDisplay)args.Widget).View;
  137. dv.Buffer.Changed -= new ByteBuffer.ChangedHandler(OnBufferContentsChanged);
  138. dv.BufferChanged -= new DataView.DataViewEventHandler(OnBufferChanged);
  139. dv.CursorChanged -= new DataView.DataViewEventHandler(OnCursorChanged);
  140. Update();
  141. }
  142. void OnBufferChanged(DataView dv)
  143. {
  144. // if changed dataview is not the current one just ignore
  145. DataViewDisplay dvd = (DataViewDisplay)dataBook.CurrentPageWidget;
  146. if (dvd == null || dvd.View != dv)
  147. return;
  148. Update();
  149. }
  150. void OnBufferContentsChanged(ByteBuffer bb)
  151. {
  152. Gtk.Application.Invoke(delegate {
  153. DataView dv = null;
  154. // find DataView that owns bb
  155. foreach (DataViewDisplay dvtemp in dataBook.Children) {
  156. if (dvtemp.View.Buffer == bb) {
  157. dv = dvtemp.View;
  158. break;
  159. }
  160. }
  161. DataViewDisplay dvd = (DataViewDisplay)dataBook.CurrentPageWidget;
  162. if (dvd == null || dvd.View != dv)
  163. return;
  164. Update();
  165. });
  166. }
  167. void OnSwitchPage(object o, SwitchPageArgs args)
  168. {
  169. Update();
  170. }
  171. void OnCursorChanged(DataView dv)
  172. {
  173. DataView dvcur = ((DataViewDisplay)dataBook.CurrentPageWidget).View;
  174. if (dvcur != dv)
  175. return;
  176. Update();
  177. }
  178. // redefine Hide() method
  179. // to properly handle hiding
  180. protected override void OnHidden()
  181. {
  182. // if the focus is in the table
  183. // give it to the active dataview
  184. if (IsFocusInTable()) {
  185. DataViewDisplay dvd = (DataViewDisplay)dataBook.CurrentPageWidget;
  186. if (dvd != null)
  187. dvd.GrabKeyboardFocus();
  188. }
  189. Preferences.Proxy.Change("Tools.ConversionTable.Show", "False", "ct2");
  190. base.OnHidden();
  191. }
  192. protected override void OnShown()
  193. {
  194. Preferences.Proxy.Change("Tools.ConversionTable.Show", "True", "ct2");
  195. base.OnShown();
  196. Update();
  197. }
  198. // whether a widget in the table has the focus
  199. bool IsFocusInTable()
  200. {
  201. foreach (Gtk.Widget child in ConversionTableWidget.Children) {
  202. Widget realChild = child;
  203. if (child.GetType() == typeof(Gtk.Alignment))
  204. realChild = (child as Gtk.Alignment).Child;
  205. if (realChild.HasFocus)
  206. return true;
  207. }
  208. return false;
  209. }
  210. void OnLittleEndianToggled(object o, EventArgs args)
  211. {
  212. littleEndian = LittleEndianCheckButton.Active;
  213. Preferences.Proxy.Change("Tools.ConversionTable.LEDecoding", littleEndian.ToString(), "ct2");
  214. Update();
  215. }
  216. void OnUnsignedAsHexToggled(object o, EventArgs args)
  217. {
  218. unsignedAsHex = UnsignedAsHexCheckButton.Active;
  219. Preferences.Proxy.Change("Tools.ConversionTable.UnsignedAsHex", unsignedAsHex.ToString(), "ct2");
  220. Update();
  221. }
  222. void OnPreferencesChanged(Preferences prefs)
  223. {
  224. if (prefs["Tools.ConversionTable.LEDecoding"] == "True")
  225. LittleEndianCheckButton.Active = true;
  226. else
  227. LittleEndianCheckButton.Active = false;
  228. if (prefs["Tools.ConversionTable.UnsignedAsHex"] == "True")
  229. UnsignedAsHexCheckButton.Active = true;
  230. else
  231. UnsignedAsHexCheckButton.Active = false;
  232. if (prefs["Tools.ConversionTable.Show"] == "True")
  233. this.Visible = true;
  234. else
  235. this.Visible = false;
  236. }
  237. void OnCloseButtonClicked(object o, EventArgs args)
  238. {
  239. Preferences.Instance["Tools.ConversionTable.Show"] = "False";
  240. }
  241. void Clear8bit()
  242. {
  243. Signed8bitEntry.Text = "---";
  244. Unsigned8bitEntry.Text = "---";
  245. }
  246. void Clear16bit()
  247. {
  248. Signed16bitEntry.Text = "---";
  249. Unsigned16bitEntry.Text = "---";
  250. }
  251. void Clear32bit()
  252. {
  253. Signed32bitEntry.Text = "---";
  254. Unsigned32bitEntry.Text = "---";
  255. }
  256. void ClearFloat()
  257. {
  258. Float32bitEntry.Text = "---";
  259. Float64bitEntry.Text = "---";
  260. }
  261. void ClearBases()
  262. {
  263. HexadecimalEntry.Text = "---";
  264. DecimalEntry.Text = "---";
  265. OctalEntry.Text = "---";
  266. BinaryEntry.Text = "---";
  267. AsciiEntry.Text = "---";
  268. }
  269. ///<summary>Update the 8bit entries</summary>
  270. void Update8bit(DataView dv)
  271. {
  272. long offset = dv.CursorOffset;
  273. // make sure offset is valid
  274. if (offset < dv.Buffer.Size && offset >= 0) {
  275. byte uval = dv.Buffer[offset];
  276. sbyte val = (sbyte)uval;
  277. // set signed
  278. Signed8bitEntry.Text = val.ToString();
  279. // set unsigned
  280. if (unsignedAsHex)
  281. Unsigned8bitEntry.Text = string.Format("0x{0:x}", uval);
  282. else
  283. Unsigned8bitEntry.Text = uval.ToString();
  284. }
  285. else {
  286. Clear8bit();
  287. }
  288. }
  289. ///<summary>Update the 16bit entries</summary>
  290. void Update16bit(DataView dv)
  291. {
  292. long offset = dv.CursorOffset;
  293. // make sure offset is valid
  294. if (offset < dv.Buffer.Size - 1 && offset >= 0) {
  295. short val = 0;
  296. // create value according to endianess
  297. if (littleEndian) {
  298. val += (short)(dv.Buffer[offset+1] << 8);
  299. val += (short)dv.Buffer[offset];
  300. }
  301. else {
  302. val += (short)(dv.Buffer[offset] << 8);
  303. val += (short)dv.Buffer[offset+1];
  304. }
  305. ushort uval = (ushort)val;
  306. // set signed
  307. Signed16bitEntry.Text = val.ToString();
  308. // set unsigned
  309. if (unsignedAsHex)
  310. Unsigned16bitEntry.Text = string.Format("0x{0:x}", uval);
  311. else
  312. Unsigned16bitEntry.Text = uval.ToString();
  313. }
  314. else {
  315. Clear16bit();
  316. }
  317. }
  318. ///<summary>Update the 32bit entries</summary>
  319. void Update32bit(DataView dv)
  320. {
  321. long offset = dv.CursorOffset;
  322. // make sure offset is valid
  323. if (offset < dv.Buffer.Size - 3 && offset >= 0) {
  324. int val = 0;
  325. // create value according to endianess
  326. if (littleEndian) {
  327. val += dv.Buffer[offset+3] << 24;
  328. val += dv.Buffer[offset+2] << 16;
  329. val += dv.Buffer[offset+1] << 8;
  330. val += dv.Buffer[offset];
  331. }
  332. else {
  333. val += dv.Buffer[offset] << 24;
  334. val += dv.Buffer[offset+1] << 16;
  335. val += dv.Buffer[offset+2] << 8;
  336. val += dv.Buffer[offset+3];
  337. }
  338. uint uval = (uint)val;
  339. // set signed
  340. Signed32bitEntry.Text = val.ToString();
  341. // set unsigned
  342. if (unsignedAsHex)
  343. Unsigned32bitEntry.Text = string.Format("0x{0:x}", uval);
  344. else
  345. Unsigned32bitEntry.Text = uval.ToString();
  346. }
  347. else {
  348. Clear32bit();
  349. }
  350. }
  351. ///<summary>Update the floating point entries</summary>
  352. void UpdateFloat(DataView dv)
  353. {
  354. long offset = dv.CursorOffset;
  355. // make sure offset is valid for 32 bit float (and 64 bit)
  356. if (offset < dv.Buffer.Size - 3 && offset >= 0) {
  357. // create byte[] with float bytes
  358. byte[] ba = new byte[8];
  359. // fill byte[] according to endianess
  360. if (littleEndian)
  361. for (int i = 0; i < 4; i++)
  362. ba[i] = dv.Buffer[offset+i];
  363. else
  364. for (int i = 0; i < 4; i++)
  365. ba[3-i] = dv.Buffer[offset+i];
  366. // set float 32bit
  367. float f = BitConverter.ToSingle(ba, 0);
  368. Float32bitEntry.Text = f.ToString();
  369. // make sure offset is valid for 64 bit float
  370. if (offset < dv.Buffer.Size - 7) {
  371. // fill byte[] according to endianess
  372. if (littleEndian)
  373. for (int i = 4; i < 8; i++)
  374. ba[i] = dv.Buffer[offset+i];
  375. else
  376. for (int i = 0; i < 8; i++)
  377. ba[7-i] = dv.Buffer[offset+i];
  378. // set float 64bit
  379. double d = BitConverter.ToDouble(ba, 0);
  380. Float64bitEntry.Text = d.ToString();
  381. }
  382. else
  383. Float64bitEntry.Text = "---";
  384. }
  385. else {
  386. ClearFloat();
  387. }
  388. }
  389. ///<summary>Update the number base entries</summary>
  390. void UpdateBases(DataView dv)
  391. {
  392. long offset = dv.CursorOffset;
  393. long size;
  394. if (offset < 0 || offset >= dv.Buffer.Size)
  395. size = 0;
  396. else {
  397. size = dv.Buffer.Size - offset;
  398. size = size < 4 ? size : 4;
  399. }
  400. byte[] ba = new byte[(int)size];
  401. for (int i = 0; i < size; i++)
  402. ba[i] = dv.Buffer[offset+i];
  403. // make sure offset is valid
  404. if (size > 0) {
  405. HexadecimalEntry.Text = ByteArray.ToString(ba, 16);
  406. DecimalEntry.Text = ByteArray.ToString(ba, 10);
  407. OctalEntry.Text = ByteArray.ToString(ba, 8);
  408. BinaryEntry.Text = ByteArray.ToString(ba, 2);
  409. AsciiEntry.Text = Encoding.ASCII.GetString(ba);
  410. }
  411. else {
  412. ClearBases();
  413. }
  414. }
  415. ///<summary>Update all conversion entries</summary>
  416. public void Update()
  417. {
  418. if (!this.Visible)
  419. return;
  420. DataViewDisplay dvd = (DataViewDisplay)dataBook.CurrentPageWidget;
  421. if (dvd == null) {
  422. Clear();
  423. return;
  424. }
  425. DataView dv = dvd.View;
  426. Update8bit(dv);
  427. Update16bit(dv);
  428. Update32bit(dv);
  429. UpdateFloat(dv);
  430. UpdateBases(dv);
  431. }
  432. ///<summary>Clear all conversion entries</summary>
  433. public void Clear()
  434. {
  435. if (!this.Visible)
  436. return;
  437. Clear8bit();
  438. Clear16bit();
  439. Clear32bit();
  440. ClearFloat();
  441. ClearBases();
  442. }
  443. }
  444. } // end namespace