diff options
Diffstat (limited to 'ext/tk')
-rw-r--r-- | ext/tk/sample/tktextio.rb | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/ext/tk/sample/tktextio.rb b/ext/tk/sample/tktextio.rb index 3aa82fb38c..fecedf6ee1 100644 --- a/ext/tk/sample/tktextio.rb +++ b/ext/tk/sample/tktextio.rb @@ -21,11 +21,13 @@ class TkTextIO < TkText ovwt = false text = nil wrap = 'char' + show = :pos if keys.kind_of?(Hash) mode = keys.delete('mode') ovwt = keys.delete('overwrite') text = keys.delete('text') + show = keys.delete('show') if keys.has_key?('show') wrap = keys.delete('wrap') || 'char' end @@ -37,6 +39,8 @@ class TkTextIO < TkText @txtpos = TkTextMark.new(self, '1.0') @txtpos.gravity = :left + self.show_mode = show + @sync = true @overwrite = (ovwt)? true: false @@ -168,6 +172,7 @@ class TkTextIO < TkText return nil if eof? c = get(@txtpos) @txtpos.set(@txtpos + '1 char') + _see_pos c end @@ -217,6 +222,7 @@ class TkTextIO < TkText def index_pos=(idx) @txtpos.set(idx) @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end) + _see_pos idx end @@ -294,6 +300,7 @@ class TkTextIO < TkText s = get(@txtpos, epos) @txtpos.set(epos) @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end) + _see_pos s end private :_read @@ -307,6 +314,7 @@ class TkTextIO < TkText else s = get(@txtpos, 'end - 1 char') @txtpos.set('end - 1 char') + _see_pos end buf.replace(s) if buf.kind_of?(String) s @@ -317,6 +325,7 @@ class TkTextIO < TkText fail EOFError if eof? c = get(@txtpos) @txtpos.set(@txtpos + '1 char') + _see_pos c end @@ -340,12 +349,14 @@ class TkTextIO < TkText if idx s = get(@txtpos, "#{idx} + #{@count_var.value} char") @txtpos.set("#{idx} + #{@count_var.value} char") + @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end) else s = get(@txtpos, 'end - 1 char') @txtpos.set('end - 1 char') end end + _see_pos @lineno += 1 $_ = s end @@ -380,6 +391,7 @@ class TkTextIO < TkText def rewind @txtpos.set('1.0') + _see_pos @lineno = 0 @line_offset = 0 self @@ -404,11 +416,48 @@ class TkTextIO < TkText end @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end) + _see_pos 0 end alias sysseek seek + def _see_pos + see(@show) if @show + end + private :_see_pos + + def show_mode + (@show == @txtpos)? :pos : @show + end + + def show_mode=(mode) + # define show mode when file position is changed. + # mode == :pos or "pos" or true :: see current file position. + # mode == :insert or "insert" :: see insert cursor position. + # mode == nil or false :: do nothing + # else see 'mode' position ('mode' should be text index or mark) + case mode + when :pos, 'pos', true + @show = @txtpos + when :insert, 'insert' + @show = :insert + when nil, false + @show = false + else + begin + index(mode) + rescue + fail ArgumentError, 'invalid show-position' + end + @show = mode + end + + _see_pos + + mode + end + def stat fail NotImplementedError, 'stat is not implemented on TkTextIO' end @@ -450,6 +499,7 @@ class TkTextIO < TkText delete(@txtpos) insert(@txtpos, tk_call('string', 'range', c, 0, 1)) @txtpos.set(@txtpos - '1 char') if @txtpos.gravity == 'right' + _see_pos else fail IOError, 'cannot ungetc at head of stream' end @@ -462,6 +512,8 @@ class TkTextIO < TkText delete(@txtpos, @txtpos + "#{n} char") if @overwrite self.insert(@txtpos, s) @txtpos.set(@txtpos + "#{n} char") + @txtpos.set('end - 1 char') if compare(@txtpos, '>=', :end) + _see_pos Tk.update if @sync n end @@ -478,7 +530,8 @@ end #################### if __FILE__ == $0 f = TkFrame.new.pack - tio = TkTextIO.new(f, :text=>">>> This is an initial text line. <<<\n\n"){ + tio = TkTextIO.new(f, :show=>:pos, + :text=>">>> This is an initial text line. <<<\n\n"){ yscrollbar(TkScrollbar.new(f).pack(:side=>:right, :fill=>:y)) pack(:side=>:left, :fill=>:both, :expand=>true) } @@ -498,12 +551,21 @@ if __FILE__ == $0 puts "On this sample, a text widget works as if it is a I/O stream." puts "Please see the code." puts - printf("printf message: %d %X\n", 123456, 255); + printf("printf message: %d %X\n", 123456, 255) puts + printf("(output by 'p' method) This TkTextIO object is ...\n") p tio + print(" [ Current wrap mode of this object is 'char'. ]\n") puts warn("This is a warning message generated by 'warn' method.") puts + puts "current show_mode is #{tio.show_mode}." + if tio.show_mode == :pos + puts "So, you can see the current file position on this text widget." + else + puts "So, you can see the position '#{tio.show_mode}' on this text widget." + end + print("Please scroll up this text widget to see the head of lines.\n") print("---------------------------------------------------------\n") STDOUT.print("\n=============== TkTextIO#readlines =============\n\n") |