Class: FocusEventManager

Inherits:
Object
  • Object
show all
Defined in:
lib/a-core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFocusEventManager

Returns a new instance of FocusEventManager.



3953
3954
3955
3956
# File 'lib/a-core.rb', line 3953

def initialize
  Arcadia.attach_listener(self, FocusEvent)
  Arcadia.attach_listener(self, InputEvent)
end

Instance Attribute Details

#last_focus_widgetObject (readonly)

Returns the value of attribute last_focus_widget.



3952
3953
3954
# File 'lib/a-core.rb', line 3952

def last_focus_widget
  @last_focus_widget
end

Instance Method Details

#_replace_sel(_focused_widget, _method) ⇒ Object



4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
# File 'lib/a-core.rb', line 4077

def _replace_sel(_focused_widget, _method)
  if _focused_widget.respond_to?(:tag_ranges)
    r = _focused_widget.tag_ranges('sel')
    if _focused_widget.respond_to?(:get) && r && r[0]
      target_text = _focused_widget.get(r[0][0],r[0][1])
      if target_text
        _focused_widget.delete(r[0][0],r[0][1])
        _focused_widget.insert(r[0][0],target_text.send(_method))
      end
    end
  elsif _focused_widget.kind_of?(Tk::Entry)
    if _focused_widget.selection_present
      i1= _focused_widget.index("sel.first")
      i2= _focused_widget.index("sel.last")
      target_text = _focused_widget.value[i1.to_i..i2.to_i-1]
      _focused_widget.delete(i1,i2)
      _focused_widget.insert(i1,target_text.send(_method))
    end
  end
end

#do_copy(_focused_widget) ⇒ Object



4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
# File 'lib/a-core.rb', line 4008

def do_copy(_focused_widget)
  if _focused_widget.respond_to?(:text_copy)
    _focused_widget.text_copy
  elsif _focused_widget.kind_of?(Tk::Entry)
    if _focused_widget.selection_present
      i1= _focused_widget.index("sel.first")
      i2= _focused_widget.index("sel.last")
      TkClipboard::set(_focused_widget.value[i1.to_i..i2.to_i-1])
    end
  end
end

#do_cut(_focused_widget) ⇒ Object



3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
# File 'lib/a-core.rb', line 3995

def do_cut(_focused_widget)
  if _focused_widget.respond_to?(:text_cut)
    _focused_widget.text_cut
  elsif _focused_widget.kind_of?(Tk::Entry)
    if _focused_widget.selection_present
      i1= _focused_widget.index("sel.first")
      i2= _focused_widget.index("sel.last")
      TkClipboard::set(_focused_widget.value[i1.to_i..i2.to_i-1])
      _focused_widget.delete(i1,i2)
    end
  end
end

#do_invert_selection(_focused_widget) ⇒ Object



4053
4054
4055
4056
4057
4058
4059
# File 'lib/a-core.rb', line 4053

def do_invert_selection(_focused_widget)
  if _focused_widget.respond_to?(:tag_ranges)
    r = _focused_widget.tag_ranges('sel')
    _focused_widget.tag_add('sel','1.0','end') if _focused_widget.respond_to?(:tag_add)
    _focused_widget.tag_remove('sel',r[0][0],r[0][1]) if _focused_widget.respond_to?(:tag_remove) && r && r[0]
  end
end

#do_lower_case(_focused_widget) ⇒ Object



4069
4070
4071
4072
4073
4074
4075
# File 'lib/a-core.rb', line 4069

def do_lower_case(_focused_widget)
  if _focused_widget.respond_to?(:do_lower_case)
    _focused_widget.do_lower_case
  else
    _replace_sel(_focused_widget, :downcase)
  end
end

#do_paste(_focused_widget) ⇒ Object



4020
4021
4022
4023
4024
4025
4026
# File 'lib/a-core.rb', line 4020

def do_paste(_focused_widget)
  if _focused_widget.respond_to?(:text_paste)
    _focused_widget.text_paste
  elsif _focused_widget.kind_of?(Tk::Entry)
    _focused_widget.insert(_focused_widget.index("insert"), TkClipboard::get)
  end
end

#do_redo(_focused_widget) ⇒ Object



4036
4037
4038
4039
4040
4041
4042
# File 'lib/a-core.rb', line 4036

def do_redo(_focused_widget)
  begin
    _focused_widget.edit_redo if _focused_widget.respond_to?(:edit_redo)
  rescue RuntimeError => e
    throw e unless e.to_s.include? "nothing to redo" # this is ok--we've done redo back to the beginning
  end
end

#do_select_all(_focused_widget) ⇒ Object



4044
4045
4046
4047
4048
4049
4050
4051
# File 'lib/a-core.rb', line 4044

def do_select_all(_focused_widget)
  if _focused_widget.respond_to?(:tag_add)
    _focused_widget.tag_add('sel','1.0','end')
  elsif _focused_widget.kind_of?(Tk::Entry)
    _focused_widget.selection_from('0')
    _focused_widget.selection_to('end')
  end
end

#do_undo(_focused_widget) ⇒ Object



4028
4029
4030
4031
4032
4033
4034
# File 'lib/a-core.rb', line 4028

def do_undo(_focused_widget)
  begin
    _focused_widget.edit_undo if _focused_widget.respond_to?(:edit_undo)
  rescue RuntimeError => e
    throw e unless e.to_s.include? "nothing to undo" # this is ok--we've done undo back to the beginning
  end
end

#do_upper_case(_focused_widget) ⇒ Object



4061
4062
4063
4064
4065
4066
4067
# File 'lib/a-core.rb', line 4061

def do_upper_case(_focused_widget)
  if _focused_widget.respond_to?(:do_upper_case)
    _focused_widget.do_upper_case
  else
    _replace_sel(_focused_widget, :upcase)
  end
end

#on_focus(_event) ⇒ Object



3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
# File 'lib/a-core.rb', line 3967

def on_focus(_event)
  if @last_focus_widget
    _event.focus_widget = @last_focus_widget
  else
    _event.focus_widget=Tk.focus
  end
  case _event
  when CutTextEvent
    do_cut(_event.focus_widget)
  when CopyTextEvent
    do_copy(_event.focus_widget)
  when PasteTextEvent
    do_paste(_event.focus_widget)
  when UndoTextEvent
    do_undo(_event.focus_widget)
  when RedoTextEvent
    do_redo(_event.focus_widget)
  when SelectAllTextEvent
    do_select_all(_event.focus_widget)
  when InvertSelectionTextEvent
    do_invert_selection(_event.focus_widget)
  when UpperCaseTextEvent
    do_upper_case(_event.focus_widget)
  when LowerCaseTextEvent
    do_lower_case(_event.focus_widget)
  end
end

#on_input(_event) ⇒ Object



3958
3959
3960
3961
3962
3963
3964
3965
# File 'lib/a-core.rb', line 3958

def on_input(_event)
  case _event
  when InputEnterEvent
    @last_focus_widget = _event.receiver
  when InputExitEvent
    @last_focus_widget = nil
  end
end