| 1 |
|
| 2 |
require 'gtk3' |
| 3 |
|
| 4 |
module Gdk |
| 5 |
module TextSelector |
| 6 |
START_TAG_PATTERN = /<\s*\w+.*?>/ |
| 7 |
END_TAG_PATTERN = %r{</.*?>} |
| 8 |
ENTITY_ENCODED_PATTERN = /&(?:gt|lt|amp);/ |
| 9 |
CHARACTER_PATTERN = /./m |
| 10 |
CHUNK_PATTERN = Regexp.union(START_TAG_PATTERN, |
| 11 |
END_TAG_PATTERN, |
| 12 |
ENTITY_ENCODED_PATTERN, |
| 13 |
CHARACTER_PATTERN) |
| 14 |
|
| 15 |
START_TAG_PATTERN_EXACT = /\A<\s*\w+.*?>\Z/ |
| 16 |
END_TAG_PATTERN_EXACT = %r{\A</.*?>\Z} |
| 17 |
ENTITY_ENCODED_PATTERN_EXACT = /\A&(?:gt|lt|amp);\Z/ |
| 18 |
CHARACTER_PATTERN_EXACT = /\A.\Z/m |
| 19 |
NON_TAG_PATTERN_EXACT = Regexp.union(ENTITY_ENCODED_PATTERN_EXACT, |
| 20 |
CHARACTER_PATTERN_EXACT) |
| 21 |
|
| 22 |
WHITE = [0xffff, 0xffff, 0xffff].freeze |
| 23 |
BLACK = [0, 0, 0].freeze |
| 24 |
|
| 25 |
def initialize(*args) |
| 26 |
@textselector_pressing = @textselect_start = @textselect_end = nil |
| 27 |
super end |
| 28 |
|
| 29 |
def textselector_range |
| 30 |
if(@textselect_start and @textselect_end and @textselect_start != @textselect_end) |
| 31 |
first, last = [@textselect_start, @textselect_end].sort |
| 32 |
Range.new(first, last, true) end end |
| 33 |
|
| 34 |
def textselector_press(index, trail=0) |
| 35 |
@textselector_pressing = true |
| 36 |
before = textselector_range |
| 37 |
@textselect_end = @textselect_start = index + trail |
| 38 |
queue_draw if before == textselector_range |
| 39 |
self end |
| 40 |
|
| 41 |
def textselector_release(index = nil, trail=0) |
| 42 |
textselector_select(index, trail) if index |
| 43 |
@textselector_pressing = false |
| 44 |
self end |
| 45 |
|
| 46 |
def textselector_unselect |
| 47 |
@textselect_end = @textselect_start = nil |
| 48 |
@textselector_pressing = false |
| 49 |
queue_draw |
| 50 |
self end |
| 51 |
|
| 52 |
def textselector_select(index, trail=0) |
| 53 |
if(@textselector_pressing) |
| 54 |
before = textselector_range |
| 55 |
@textselect_end = index + trail |
| 56 |
queue_draw if before == textselector_range |
| 57 |
end |
| 58 |
self end |
| 59 |
|
| 60 |
def textselector_attr_list(attr_list=Pango::AttrList.new) |
| 61 |
if textselector_range |
| 62 |
bg = ::Pango::AttrBackground.new(*BLACK) |
| 63 |
fg = ::Pango::AttrForeground.new(*WHITE) |
| 64 |
bg.start_index = fg.start_index = plain_description[0...textselector_range.first].bytesize |
| 65 |
bg.end_index = fg.end_index = plain_description[0...textselector_range.last].bytesize |
| 66 |
attr_list.insert(bg) |
| 67 |
attr_list.insert(fg) |
| 68 |
end |
| 69 |
attr_list |
| 70 |
end |
| 71 |
end |
| 72 |
end |