Class: Shoes::Swt::TextBlock::CursorPainter

Inherits:
Object
  • Object
show all
Defined in:
shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb

Instance Method Summary collapse

Constructor Details

#initialize(text_block_dsl, collection) ⇒ CursorPainter

Returns a new instance of CursorPainter.



7
8
9
10
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 7

def initialize(text_block_dsl, collection)
  @text_block_dsl = text_block_dsl
  @collection = collection
end

Instance Method Details

#drawObject



12
13
14
15
16
17
18
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 12

def draw
  if @text_block_dsl.cursor
    draw_textcursor
  else
    remove_textcursor
  end
end

#draw_textcursorObject



20
21
22
23
24
25
26
27
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 20

def draw_textcursor
  segment = @collection.segment_at_text_position(@text_block_dsl.cursor)
  relative_cursor = @collection.relative_text_position(@text_block_dsl.cursor)
  position = segment.get_location(relative_cursor)

  move_if_necessary(segment.element_left + position.x,
                    segment.element_top + position.y)
end

#move_if_necessary(x, y) ⇒ Object

It’s important to only move when necessary to avoid constant redraws



30
31
32
33
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 30

def move_if_necessary(x, y)
  return if textcursor.left == x && textcursor.top == y
  move_textcursor(x, y)
end

#move_textcursor(x, y) ⇒ Object



35
36
37
38
39
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 35

def move_textcursor(x, y)
  height = textcursor.height
  textcursor.move(x, y, x, y + height)
  textcursor.show
end

#remove_textcursorObject



45
46
47
48
49
50
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 45

def remove_textcursor
  return unless @text_block_dsl.textcursor?

  @text_block_dsl.textcursor.remove
  @text_block_dsl.textcursor = nil
end

#textcursorObject



41
42
43
# File 'shoes-swt/lib/shoes/swt/text_block/cursor_painter.rb', line 41

def textcursor
  @text_block_dsl.textcursor @collection.cursor_height
end