summaryrefslogtreecommitdiff
path: root/ext/tk/sample/tktextframe.rb
blob: 78efdf09705f5dafe5127459620c0ec5604d0254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#
#  tktextframe.rb : a sample of TkComposite
#
#                         by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
require 'tk'

class TkTextFrame < TkText
  include TkComposite

  def initialize_composite(keys={})
    keys = _symbolkey2str(keys)

    # create scrollbars
    @v_scroll = TkScrollbar.new(@frame, 'orient'=>'vertical')
    @h_scroll = TkScrollbar.new(@frame, 'orient'=>'horizontal')

    # create a text widget
    @text = TkText.new(@frame, 'wrap'=>'none')

    # set default receiver of method calls
    @path = @text.path

    # assign scrollbars
    @text.xscrollbar(@h_scroll)
    @text.yscrollbar(@v_scroll)

    # allignment
    TkGrid.rowconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
    TkGrid.columnconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
    @text.grid('row'=>0, 'column'=>0, 'sticky'=>'news')

    # scrollbars ON
    vscroll(keys.delete('vscroll'){true})
    hscroll(keys.delete('hscroll'){true})

    # set background of the text widget
    color = keys.delete('textbackground')
    textbackground(color) if color

    # set receiver widgets for configure methods
    delegate('DEFAULT', @text)
    delegate('background', @frame, @h_scroll, @v_scroll)
    delegate('activebackground', @h_scroll, @v_scroll)
    delegate('troughcolor', @h_scroll, @v_scroll)
    delegate('repeatdelay', @h_scroll, @v_scroll)
    delegate('repeatinterval', @h_scroll, @v_scroll)
    delegate('borderwidth', @frame)
    delegate('relief', @frame)

    # do configure
    configure keys unless keys.empty?
  end
  private :initialize_composite

  # set background color of text widget
  def textbackground(color = nil)
    if color
      @text.background(color)
    else
      @text.background
    end
  end

  # vertical scrollbar : ON/OFF
  def vscroll(mode)
    st = TkGrid.info(@v_scroll)
    if mode && st == [] then
      @v_scroll.grid('row'=>0, 'column'=>1, 'sticky'=>'ns')
    elsif !mode && st != [] then
      @v_scroll.ungrid
    end
    self
  end

  # horizontal scrollbar : ON/OFF
  def hscroll(mode, wrap_mode="char")
    st = TkGrid.info(@h_scroll)
    if mode && st == [] then
      @h_scroll.grid('row'=>1, 'column'=>0, 'sticky'=>'ew')
      wrap 'none'  # => self.wrap('none')
    elsif !mode && st != [] then
      @h_scroll.ungrid
      wrap wrap_mode  # => self.wrap(wrap_mode)
    end
    self
  end
end


################################################
# test
################################################
if __FILE__ == $0
  f = TkFrame.new.pack('fill'=>'x')
  t = TkTextFrame.new.pack
  TkButton.new(f, 'text'=>'vscr OFF', 
	       'command'=>proc{t.vscroll(false)}).pack('side'=>'right')
  TkButton.new(f, 'text'=>'vscr ON', 
	       'command'=>proc{t.vscroll(true)}).pack('side'=>'right')
  TkButton.new(f, 'text'=>'hscr ON', 
	       'command'=>proc{t.hscroll(true)}).pack('side'=>'left')
  TkButton.new(f, 'text'=>'hscr OFF', 
	       'command'=>proc{t.hscroll(false)}).pack('side'=>'left')
  Tk.mainloop
end