Class: FocusEventManager

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

Instance Method Summary collapse

Constructor Details

#initializeFocusEventManager

Returns a new instance of FocusEventManager.



3117
3118
3119
# File 'lib/a-core.rb', line 3117

def initialize
  Arcadia.attach_listener(self, FocusEvent)
end

Instance Method Details

#_replace_sel(_focused_widget, _method) ⇒ Object



3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
# File 'lib/a-core.rb', line 3201

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
  end
end

#do_copy(_focused_widget) ⇒ Object



3149
3150
3151
# File 'lib/a-core.rb', line 3149

def do_copy(_focused_widget)
  _focused_widget.text_copy if _focused_widget.respond_to?(:text_copy)
end

#do_cut(_focused_widget) ⇒ Object



3145
3146
3147
# File 'lib/a-core.rb', line 3145

def do_cut(_focused_widget)
  _focused_widget.text_cut if _focused_widget.respond_to?(:text_cut)
end

#do_invert_selection(_focused_widget) ⇒ Object



3177
3178
3179
3180
3181
3182
3183
# File 'lib/a-core.rb', line 3177

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



3193
3194
3195
3196
3197
3198
3199
# File 'lib/a-core.rb', line 3193

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



3153
3154
3155
# File 'lib/a-core.rb', line 3153

def do_paste(_focused_widget)
  _focused_widget.text_paste if _focused_widget.respond_to?(:text_paste)
end

#do_redo(_focused_widget) ⇒ Object



3165
3166
3167
3168
3169
3170
3171
# File 'lib/a-core.rb', line 3165

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



3173
3174
3175
# File 'lib/a-core.rb', line 3173

def do_select_all(_focused_widget)
  _focused_widget.tag_add('sel','1.0','end') if _focused_widget.respond_to?(:tag_add)
end

#do_undo(_focused_widget) ⇒ Object



3157
3158
3159
3160
3161
3162
3163
# File 'lib/a-core.rb', line 3157

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



3185
3186
3187
3188
3189
3190
3191
# File 'lib/a-core.rb', line 3185

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



3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
# File 'lib/a-core.rb', line 3121

def on_focus(_event)
  _event.focus_widget=Tk.focus
  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