Class: TkComponent::Builder::TkText

Inherits:
TkItem
  • Object
show all
Includes:
Scrollable, ValueTyping
Defined in:
lib/tk_component/builder/tk_item.rb

Constant Summary

Constants included from Scrollable

Scrollable::ROOT_FRAME_OPTIONS

Instance Attribute Summary

Attributes inherited from TkItem

#native_item

Instance Method Summary collapse

Methods included from Scrollable

#initialize, #remove

Methods included from ValueTyping

#apply_option, #f_value, #i_value, #s_value, #update_value

Methods inherited from TkItem

#apply_internal_grid, #apply_option, #apply_options, #built, create, #create_native_item, #focus, #initialize, #native_item_class, #remove, #set_event_handlers, #set_grid

Instance Method Details

#append_text(text) ⇒ Object



250
251
252
# File 'lib/tk_component/builder/tk_item.rb', line 250

def append_text(text)
  native_item.insert('end', text)
end

#current_lineObject



246
247
248
# File 'lib/tk_component/builder/tk_item.rb', line 246

def current_line
  native_item.get('insert linestart', 'insert lineend')
end

#select_range(from, to) ⇒ Object



254
255
256
# File 'lib/tk_component/builder/tk_item.rb', line 254

def select_range(from, to)
  native_item.tag_add('sel', from, to)
end

#selected_textObject



240
241
242
243
244
# File 'lib/tk_component/builder/tk_item.rb', line 240

def selected_text
  ranges = native_item.tag_ranges('sel')
  return nil if ranges.empty?
  native_item.get(ranges.first.first, ranges.first.last)
end

#set_event_handler(event_handler) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/tk_component/builder/tk_item.rb', line 258

def set_event_handler(event_handler)
  case event_handler.name
  when :change
    pre_lambda = ->(e) do
      # Prevent the event if the text wasn't really modified
      # This is because setting "modified = false" triggers
      # the modification event itself, which makes not much sense.
      e.sender.is_a?(self.class) && !e.sender.native_item.modified?
    end
    post_lambda = ->(e) do
      if e.sender.is_a?(self.class)
        e.sender.native_item.modified = false
      end
    end
    Event.bind_event('<Modified>', self, event_handler.options, event_handler.lambda, pre_lambda, post_lambda)
  else
    super
  end
end

#valueObject



232
233
234
# File 'lib/tk_component/builder/tk_item.rb', line 232

def value
  native_item.get('1.0', 'end')
end

#value=(text) ⇒ Object



236
237
238
# File 'lib/tk_component/builder/tk_item.rb', line 236

def value=(text)
  native_item.replace('1.0', 'end', text)
end