1#
2#  tkextlib/iwidgets/notebook.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tkextlib/iwidgets.rb'
8
9module Tk
10  module Iwidgets
11    class Notebook < Tk::Itk::Widget
12    end
13  end
14end
15
16class Tk::Iwidgets::Notebook
17  TkCommandNames = ['::iwidgets::notebook'.freeze].freeze
18  WidgetClassName = 'Notebook'.freeze
19  WidgetClassNames[WidgetClassName] ||= self
20
21  ####################################
22
23  include TkItemConfigMethod
24
25  def __item_cget_cmd(id)
26    [self.path, 'pagecget', id]
27  end
28  private :__item_cget_cmd
29
30  def __item_config_cmd(id)
31    [self.path, 'pageconfigure', id]
32  end
33  private :__item_config_cmd
34
35  def tagid(tagOrId)
36    if tagOrId.kind_of?(Tk::Itk::Component)
37      tagOrId.name
38    else
39      #_get_eval_string(tagOrId)
40      tagOrId
41    end
42  end
43
44  alias pagecget_tkstring itemcget_tkstring
45  alias pagecget itemcget
46  alias pagecget_strict itemcget_strict
47  alias pageconfigure itemconfigure
48  alias pageconfiginfo itemconfiginfo
49  alias current_pageconfiginfo current_itemconfiginfo
50
51  private :itemcget_tkstring, :itemcget, :itemcget_strict
52  private :itemconfigure, :itemconfiginfo, :current_itemconfiginfo
53
54  ####################################
55
56  def __boolval_optkeys
57    super() << 'auto'
58  end
59  private :__boolval_optkeys
60
61  def add(keys={})
62    window(tk_call(@path, 'add', *hash_kv(keys)))
63  end
64
65  def child_site_list
66    list(tk_call(@path, 'childsite'))
67  end
68
69  def child_site(idx)
70    if (new_idx = self.index(idx)) < 0
71      new_idx = tagid(idx)
72    end
73    window(tk_call(@path, 'childsite', new_idx))
74  end
75
76  def delete(idx1, idx2=nil)
77    if (new_idx1 = self.index(idx1)) < 0
78      new_idx1 = tagid(idx1)
79    end
80    if idx2
81      if (new_idx2 = self.index(idx2)) < 0
82        new_idx2 = tagid(idx2)
83      end
84      tk_call(@path, 'delete', new_idx1, new_idx2)
85    else
86      tk_call(@path, 'delete', new_idx1)
87    end
88    self
89  end
90
91  def index(idx)
92    number(tk_call(@path, 'index', tagid(idx)))
93  end
94
95  def insert(idx, keys={})
96    if (new_idx = self.index(idx)) < 0
97      new_idx = tagid(idx)
98    end
99    window(tk_call(@path, 'insert', new_idx, *hash_kv(keys)))
100  end
101
102  def next
103    tk_call(@path, 'next')
104    self
105  end
106
107  def prev
108    tk_call(@path, 'prev')
109    self
110  end
111
112  def select(idx)
113    if (new_idx = self.index(idx)) < 0
114      new_idx = tagid(idx)
115    end
116    tk_call(@path, 'select', new_idx)
117    self
118  end
119
120  def scrollcommand(cmd=Proc.new)
121    configure_cmd 'scrollcommand', cmd
122    self
123  end
124  alias xscrollcommand scrollcommand
125  alias yscrollcommand scrollcommand
126
127  def xscrollbar(bar=nil)
128    if bar
129      @scrollbar = bar
130      @scrollbar.orient 'horizontal'
131      self.scrollcommand {|*arg| @scrollbar.set(*arg)}
132      @scrollbar.command {|*arg| self.xview(*arg)}
133      Tk.update  # avoid scrollbar trouble
134    end
135    @scrollbar
136  end
137  def yscrollbar(bar=nil)
138    if bar
139      @scrollbar = bar
140      @scrollbar.orient 'vertical'
141      self.scrollcommand {|*arg| @scrollbar.set(*arg)}
142      @scrollbar.command {|*arg| self.yview(*arg)}
143      Tk.update  # avoid scrollbar trouble
144    end
145    @scrollbar
146  end
147  alias scrollbar yscrollbar
148
149  def view(*idxs)
150    if idxs.size == 0
151      idx = num_or_str(tk_send_without_enc('view'))
152      if idx.kind_of?(Fixnum) && idx < 0
153        nil
154      else
155        idx
156      end
157    else
158      tk_send_without_enc('view', *idxs)
159      self
160    end
161  end
162  alias xview view
163  alias yview view
164
165  def view_moveto(*idxs)
166    view('moveto', *idxs)
167  end
168  alias xview_moveto view_moveto
169  alias yview_moveto view_moveto
170  def view_scroll(index, what='pages')
171    view('scroll', index, what)
172  end
173  alias xview_scroll view_scroll
174  alias yview_scroll view_scroll
175end
176