Class: Watobo::Gui::HexViewer

Inherits:
FXHorizontalFrame
  • Object
show all
Defined in:
lib/watobo/gui/hex_viewer.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ HexViewer

Returns a new instance of HexViewer.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/watobo/gui/hex_viewer.rb', line 80

def initialize(owner)
  super(owner, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y, :padding => 0)
  sunken = FXVerticalFrame.new(self, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, :padding => 0)
  @hexTable = FXTable.new(sunken, :opts => FRAME_SUNKEN|TABLE_COL_SIZABLE|TABLE_ROW_SIZABLE|LAYOUT_FILL_X|LAYOUT_FILL_Y|TABLE_READONLY|LAYOUT_SIDE_TOP, :padding => 2)
  f = FXFont.new(getApp(), "courier", 8,  FONTSLANT_REGULAR, FONTENCODING_DEFAULT)
  @hexTable.font = f
  @hexTable.columnHeaderFont = f
  @hexTable.rowHeaderFont = f
  
  
end

Instance Method Details

#normalizeText(text) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/watobo/gui/hex_viewer.rb', line 5

def normalizeText(text)
  dummy = []
  begin
    text.headers.each do |h|
      dummy.push h.strip.unpack("C*").pack("C*") + "\r\n"
    end
    dummy.push "\r\n"
    dummy.push text.body.unpack("C*").pack("C*")
    dummy = dummy.join
  rescue => bang
    dummy = text
  end
  return dummy
end

#setText(tobject) ⇒ Object



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
# File 'lib/watobo/gui/hex_viewer.rb', line 20

def setText(tobject)
  raw_text = tobject
  
  if tobject.respond_to? :has_body?
    raw_text = ""
    #raw_text << tobject.body.to_s unless tobject.body.nil? 
    raw_text << tobject.to_s
  end
  
  
  initTable()
  
  if raw_text and not raw_text.empty? then
    raw_text = normalizeText(raw_text)
    pos = 1
    col = 0
    
    row = @hexTable.getNumRows
    
    @hexTable.appendRows(1)
    @hexTable.rowHeader.setItemJustify(row, FXTableItem::LEFT)    
    @hexTable.setRowText(row, "%0.4X" % row.to_s)
    
    while pos <= raw_text.length
      chunk = raw_text[pos-1].unpack("H2")[0]
      @hexTable.setItemText(row, col, chunk)
      @hexTable.getItem(row, col).justify = FXTableItem::LEFT
      
      if pos % 16 == 0 then
        chunk = raw_text[row*16..pos-1]
        
       
       # Filter bad utf-8 chars
        printable = chunk.unpack("C*").pack("C*").gsub(/[^[:print:]]/,'.')
        @hexTable.setItemText(row, 16, printable) if !chunk.nil?
        @hexTable.getItem(row, 16).justify = FXTableItem::LEFT
        
        row = @hexTable.getNumRows
        @hexTable.appendRows(1)
        
        # puts "=#{pos}/#{row}"
        @hexTable.rowHeader.setItemJustify(row, FXTableItem::LEFT)        
        @hexTable.setRowText(row, "%0.4X" % (row*16).to_s)
        
        col = -1
      end
      pos += 1
      col += 1 
    end
    chunk = raw_text[row*16..pos-1]
    @hexTable.setItemText(row, 16, chunk.gsub(/[^[:print:]]/,'.')) if !chunk.nil?
        @hexTable.getItem(row, 16).justify = FXTableItem::LEFT
    
  end
  
  0.upto(16) do |i|
    @hexTable.fitColumnsToContents(i)
  end
end