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.



3755
3756
3757
3758
# File 'lib/a-core.rb', line 3755

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.



3754
3755
3756
# File 'lib/a-core.rb', line 3754

def last_focus_widget
  @last_focus_widget
end

Instance Method Details

#_replace_sel(_focused_widget, _method) ⇒ Object



3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
# File 'lib/a-core.rb', line 3879

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



3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
# File 'lib/a-core.rb', line 3810

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



3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
# File 'lib/a-core.rb', line 3797

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



3855
3856
3857
3858
3859
3860
3861
# File 'lib/a-core.rb', line 3855

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



3871
3872
3873
3874
3875
3876
3877
# File 'lib/a-core.rb', line 3871

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



3822
3823
3824
3825
3826
3827
3828
# File 'lib/a-core.rb', line 3822

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



3838
3839
3840
3841
3842
3843
3844
# File 'lib/a-core.rb', line 3838

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



3846
3847
3848
3849
3850
3851
3852
3853
# File 'lib/a-core.rb', line 3846

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



3830
3831
3832
3833
3834
3835
3836
# File 'lib/a-core.rb', line 3830

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



3863
3864
3865
3866
3867
3868
3869
# File 'lib/a-core.rb', line 3863

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



3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
# File 'lib/a-core.rb', line 3769

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



3760
3761
3762
3763
3764
3765
3766
3767
# File 'lib/a-core.rb', line 3760

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