Class: Gtk::HexEditor

Inherits:
Fixed
  • Object
show all
Defined in:
lib/gtkhex.rb

Defined Under Namespace

Modules: Group, View Classes: AutoHighlight, Highlight

Constant Summary collapse

DEFAULT_FONT =
"Monospace 12"
DEFAULT_CPL =
32
DEFAULT_LINES =
16
DISPLAY_BORDER =
4
SCROLL_TIMEOUT =
100
@@primary =
Clipboard.get(Gdk::Selection::PRIMARY)
@@clipboard =
Clipboard.get(Gdk::Selection::CLIPBOARD)

Instance Method Summary collapse

Constructor Details

#initialize(data = '') ⇒ HexEditor

Returns a new instance of HexEditor.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/gtkhex.rb', line 76

def initialize(data = '')
  super()

  @data = data
  if RUBY_VERSION >= '1.9'
    @data.force_encoding('binary')
  end

  @scroll_timeout = -1
  @disp_buffer = ""
  @starting_offset = 0

  @xdisp_width = @adisp_width = 200
  @xdisp_gc = @adisp_gc = nil
  @active_view = View::HEX
  @group_type = Group::BYTE
  @lines = @vis_lines = @top_line = @cpl = 0
  @cursor_pos = 0
  @lower_nibble = false
  @cursor_shown = false
  @button = 0
  @insert = false
  @selecting = false

  @selection = Highlight.new
  @selection.start = @selection.end = 0
  @selection.style = nil
  @selection.min_select = 1
  @selection.valid = false

  @highlights = [ @selection ]

  @auto_highlight = nil

  @disp_font_metrics = load_font DEFAULT_FONT
  @font_desc = Pango::FontDescription.new DEFAULT_FONT

  @char_width = get_max_char_width(@disp_font_metrics)
  @char_height = Pango.pixels(@disp_font_metrics.ascent) + Pango.pixels(@disp_font_metrics.descent) + 2

  self.can_focus = true
  self.events = Gdk::Event::KEY_PRESS_MASK
  self.border_width = DISPLAY_BORDER

  mouse_handler = lambda do |widget, event|
    if event.event_type == Gdk::Event::BUTTON_RELEASE and event.button == 1
      if @scroll_timeout
        GLib::Source.remove @scroll_timeout
        @scroll_timeout = nil
        @scroll_dir = 0
      end

      @selecting = false
      Gtk.grab_remove(widget)
      @button = 0
    elsif event.event_type == Gdk::Event::BUTTON_PRESS and event.button == 1
      self.grab_focus unless self.has_focus?

      Gtk.grab_add(widget)
      @button = event.button

      focus_view = (widget == @xdisp) ? View::HEX : View::ASCII

      if @active_view == focus_view
        if @active_view == View::HEX
          hex_to_pointer(event.x, event.y)
        else
          ascii_to_pointer(event.x, event.y)
        end

        unless @selecting
          @selecting = true
          set_selection(@cursor_pos, @cursor_pos)
        end
      else
        hide_cursor
        @active_view = focus_view
        show_cursor
      end
    elsif event.event_type == Gdk::Event::BUTTON_PRESS and event.button == 2
      # TODO
    else
      @button = 0
    end
  end

  @xdisp = DrawingArea.new
  @xdisp.modify_font @font_desc
  @xlayout = @xdisp.create_pango_layout('')
  @xdisp.events =
    Gdk::Event::EXPOSURE_MASK |
    Gdk::Event::BUTTON_PRESS_MASK |
    Gdk::Event::BUTTON_RELEASE_MASK |
    Gdk::Event::BUTTON_MOTION_MASK |
    Gdk::Event::SCROLL_MASK

  @xdisp.signal_connect 'realize' do
    @xdisp_gc = Gdk::GC.new(@xdisp.window)
    @xdisp_gc.set_exposures(true)
  end

  @xdisp.signal_connect 'expose_event' do |xdisp, event|
    imin = (event.area.y / @char_height).to_i
    imax = ((event.area.y + event.area.height) / @char_height).to_i
    imax += 1 if (event.area.y + event.area.height).to_i % @char_height != 0

    imax = [ imax, @vis_lines ].min

    render_hex_lines(imin, imax)
  end

  @xdisp.signal_connect 'scroll_event' do |xdisp, event|
    @scrollbar.event(event)
  end

  @xdisp.signal_connect 'button_press_event' do |xdisp, event|
    mouse_handler[xdisp, event]
  end

  @xdisp.signal_connect 'button_release_event' do |xdisp, event|
    mouse_handler[xdisp, event]
  end

  @xdisp.signal_connect 'motion_notify_event' do |xdisp, event|
    w, x, y, m = xdisp.window.pointer

    if y < 0
      @scroll_dir = -1
    elsif y >= xdisp.allocation.height
      @scroll_dir = 1
    else
      @scroll_dir = 0
    end

    if @scroll_dir != 0
      if @scroll_timeout == nil
        @scroll_timeout = GLib::Timeout.add(SCROLL_TIMEOUT) {
          if @scroll_dir < 0
            set_cursor([ 0, @cursor_pos - @cpl ].max)
          elsif @scroll_dir > 0
            set_cursor([ @data.size - 1, @cursor_pos + @cpl ].min) 
          end

          true
        }
        next
      end
    else
      if @scroll_timeout != nil
        GLib::Source.remove @scroll_timeout
        @scroll_timeout = nil
      end
    end

    next if event.window != xdisp.window

    hex_to_pointer(x,y) if @active_view == View::HEX and @button == 1
  end
  
  put @xdisp, 0, 0
  @xdisp.show

  @adisp = DrawingArea.new
  @adisp.modify_font @font_desc
  @alayout = @adisp.create_pango_layout('')
  @adisp.events =
    Gdk::Event::EXPOSURE_MASK |
    Gdk::Event::BUTTON_PRESS_MASK |
    Gdk::Event::BUTTON_RELEASE_MASK |
    Gdk::Event::BUTTON_MOTION_MASK |
    Gdk::Event::SCROLL_MASK

  
  @adisp.signal_connect 'realize' do
    @adisp_gc = Gdk::GC.new(@adisp.window)
    @adisp_gc.set_exposures(true)
  end

  @adisp.signal_connect 'expose_event' do |adisp, event|
    imin = (event.area.y / @char_height).to_i
    imax = ((event.area.y + event.area.height) / @char_height).to_i
    imax += 1 if (event.area.y + event.area.height).to_i % @char_height != 0

    imax = [ imax, @vis_lines ].min
    render_ascii_lines(imin, imax)
  end

  @adisp.signal_connect 'scroll_event' do |adisp, event|
    @scrollbar.event(event)
  end

  @adisp.signal_connect 'button_press_event' do |adisp, event|
    mouse_handler[adisp, event]
  end

  @adisp.signal_connect 'button_release_event' do |adisp, event|
    mouse_handler[adisp, event]
  end

  @adisp.signal_connect 'motion_notify_event' do |adisp, event|
    w, x, y, m = adisp.window.pointer

    if y < 0
      @scroll_dir = -1
    elsif y >= adisp.allocation.height
      @scroll_dir = 1
    else
      @scroll_dir = 0
    end

    if @scroll_dir != 0
      if @scroll_timeout == nil
        @scroll_timeout = GLib::Timeout.add(SCROLL_TIMEOUT) {
          if @scroll_dir < 0
            set_cursor([ 0, @cursor_pos - @cpl ].max)
          elsif @scroll_dir > 0
            set_cursor([ @data.size - 1, @cursor_pos + @cpl ].min) 
          end

          true
        }
        next
      end
    else
      if @scroll_timeout != nil
        GLib::Source.remove @scroll_timeout
        @scroll_timeout = nil
      end
    end

    next if event.window != adisp.window

    ascii_to_pointer(x,y) if @active_view == View::ASCII and @button == 1
  end
  
  put @adisp, 0, 0
  @adisp.show

  @adj = Gtk::Adjustment.new(0, 0, 0, 0, 0, 0)
  @scrollbar = VScrollbar.new(@adj)
  @adj.signal_connect 'value_changed' do |adj|
    unless @xdisp_gc.nil? or @adisp_gc.nil? or not @xdisp.drawable? or not @adisp.drawable?
      
      source_min = (adj.value.to_i - @top_line) * @char_height
      source_max = source_min + @xdisp.allocation.height
      dest_min = 0
      dest_max = @xdisp.allocation.height

      rect = Gdk::Rectangle.new(0, 0, 0, 0)
      @top_line = adj.value.to_i
      if source_min < 0
        rect.y = 0
        rect.height = -source_min
        rect_height = [ rect.height, @xdisp.allocation.height ].min
        source_min = 0
        dest_min = rect.height
      else
        rect.y = 2 * @xdisp.allocation.height - source_max
        rect.y = 0 if rect.y < 0
        rect.height = @xdisp.allocation.height - rect.y
        source_max = @xdisp.allocation.height
        dest_max = rect.y
      end

      if source_min != source_max
        @xdisp.window.draw_drawable(
          @xdisp_gc,
          @xdisp.window,
          0, source_min,
          0, dest_min,
          @xdisp.allocation.width,
          source_max - source_min
        )
        @adisp.window.draw_drawable(
          @adisp_gc,
          @adisp.window,
          0, source_min,
          0, dest_min,
          @adisp.allocation.width,
          source_max - source_min
        )

        if @offsets
          if @offsets_gc.nil?
            @offsets_gc = Gdk::GC.new(@offsets.window)
            @offsets_gc.set_exposures(true)
          end

          @offsets.window.draw_drawable(
            @offsets_gc,
            @offsets.window,
            0, source_min,
            0, dest_min,
            @offsets.allocation.width,
            source_max - source_min
          )
        end

        # TODO update_all_auto_highlights(true, true)
        invalidate_all_highlights
        
        rect.width = @xdisp.allocation.width
        @xdisp.window.invalidate(rect, false)
        rect.width = @adisp.allocation.width
        @adisp.window.invalidate(rect, false)

        if @offsets
          rect.width = @offsets.allocation.width
          @offsets.window.invalidate(rect, false)
        end
      end
    end
  end

  put @scrollbar, 0, 0
  @scrollbar.show
end

Instance Method Details

#clear_selectionObject



435
436
437
# File 'lib/gtkhex.rb', line 435

def clear_selection
  set_selection(0, 0)
end

#cursorObject



439
440
441
# File 'lib/gtkhex.rb', line 439

def cursor
  @cursor_pos
end

#get_selectionObject



431
432
433
# File 'lib/gtkhex.rb', line 431

def get_selection
  [ @selection.start, @selection.end ].sort
end

#invalidate_all_highlightsObject



563
564
565
# File 'lib/gtkhex.rb', line 563

def invalidate_all_highlights
  @highlights.each do |hl| invalidate_highlight(hl) end
end

#invalidate_highlight(hl) ⇒ Object



559
560
561
# File 'lib/gtkhex.rb', line 559

def invalidate_highlight(hl)
  hl.valid = false
end

#set_cursor(index) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/gtkhex.rb', line 443

def set_cursor(index)
  return if index < 0 or index > @data.size

  old_pos = @cursor_pos
  index -= 1 if @insert and index == @data.size
  index = [ 0, index ].max

  hide_cursor
  
  @cursor_pos = index
  return if @cpl == 0

  y = index / @cpl
  if y >= @top_line + @vis_lines
    @adj.value = [ y - @vis_lines + 1, @lines - @vis_lines ].min
    @adj.value = [ 0, @adj.value ].max
    @adj.signal_emit 'value_changed'
  elsif y < @top_line
    @adj.value = y
    @adj.signal_emit 'value_changed'
  end

  @lower_nibble = false if index == @data.size

  if @selecting
    set_selection(@selection.start, @cursor_pos)
    bytes_changed(*[@cursor_pos, old_pos].sort)
  else# @selection.start != @selection.end
    s, e = [@selection.start, @selection.end].sort
    @selection.end = @selection.start = @cursor_pos
    bytes_changed(s, e)
  end

  self.signal_emit 'cursor_moved'

  bytes_changed(old_pos, old_pos)
  show_cursor
end

#set_cursor_on_lower_nibble(bool) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/gtkhex.rb', line 489

def set_cursor_on_lower_nibble(bool)
  if @selecting
    bytes_changed(@cursor_pos, @cursor_pos)
    @lower_nibble = bool
  elsif @selection.start != @selection.end
    s, e = [ @selection.start, @selection.end ].sort

    @selection.start = @selection.end = 0
    bytes_changed(s, e)
    @lower_nibble = bool
  else
    hide_cursor
    @lower_nibble = bool
    show_cursor
  end
end

#set_cursor_xy(x, y) ⇒ Object



482
483
484
485
486
487
# File 'lib/gtkhex.rb', line 482

def set_cursor_xy(x, y)
  pos = y.to_i * @cpl + x.to_i
  return if y < 0 or y >= @lines or x < 0 or x >= @cpl or pos > @data.size

  set_cursor(pos)
end

#set_data(data) ⇒ Object



540
541
542
543
544
545
546
547
548
549
# File 'lib/gtkhex.rb', line 540

def set_data(data)
  prev_data_size = @data.size
  @data = data.dup

  recalc_displays(self.allocation.width, self.allocation.height)
  
  set_cursor 0
  bytes_changed(0, [ prev_data_size, @data.size ].max)
  redraw_widget
end

#set_font(fontname) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/gtkhex.rb', line 525

def set_font(fontname)
  @font_desc = Pango::FontDescription.new(fontname)
  @disp_font_metrics = load_font(fontname)

  @xdisp.modify_font(@font_desc) if @xdisp
  @adisp.modify_font(@font_desc) if @adisp
  @offsets.modify_font(@font_desc) if @offsets

  @char_width = get_max_char_width(@disp_font_metrics)
  @char_height = Pango.pixels(@disp_font_metrics.ascent) + Pango.pixels(@disp_font_metrics.descent) + 2
  recalc_displays(self.allocation.width, self.allocation.height)

  redraw_widget
end

#set_group_type(type) ⇒ Object



506
507
508
509
510
511
512
# File 'lib/gtkhex.rb', line 506

def set_group_type(type)
  hide_cursor
  @group_type = type
  recalc_displays(self.allocation.width, self.allocation.height)
  self.queue_resize
  show_cursor
end

#set_selection(s, e) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/gtkhex.rb', line 394

def set_selection(s, e)
  e = [ e, @data.size ].min

  @@primary.clear if @selection.start != @selection.end

  os, oe = [ @selection.start, @selection.end ].sort

  @selection.start = [ 0, s ].max
  @selection.start = [ @selection.start, @data.size ].min
  @selection.end = [ e, @data.size ].min

  invalidate_highlight(@selection)

  ns, ne = [ @selection.start, @selection.end ].sort

  if ns != os and ne != oe
    bytes_changed([ns, os].min, [ne, oe].max)
  elsif ne != oe
    bytes_changed(*[ne, oe].sort)
  elsif ns != os
    bytes_changed(*[ns, os].sort)
  end

  if @selection.start != @selection.end
    if @active_view == View::HEX
      brk_len = 2 * @cpl + @cpl / @group_type
      format_xblock(s,e)
      (@disp_buffer.size / brk_len + 1).times do |i| @disp_buffer.insert(i * (brk_len + 1), $/) end
    else
      brk_len = @cpl
      format_ablock(s,e)
    end

    @@primary.set_text(@disp_buffer)
  end
end

#show_offsets(bool) ⇒ Object



514
515
516
517
518
519
520
521
522
523
# File 'lib/gtkhex.rb', line 514

def show_offsets(bool)
  return unless @show_offsets ^ bool

  @show_offsets = bool
  if bool
    show_offsets_widget
  else
    hide_offsets_widget
  end
end

#validate_highlight(hl) ⇒ Object



551
552
553
554
555
556
557
# File 'lib/gtkhex.rb', line 551

def validate_highlight(hl)
  unless hl.valid
    hl.start_line = [ hl.start, hl.end ].min / @cpl - @top_line
    hl.end_line = [ hl.start, hl.end ].max / @cpl - @top_line
    hl.valid = true
  end
end