~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/lib/tk/composite.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# tk/composite.rb : 
 
3
#
 
4
require 'tk'
 
5
 
 
6
module TkComposite
 
7
  include Tk
 
8
  extend Tk
 
9
 
 
10
=begin
 
11
  def initialize(parent=nil, *args)
 
12
    @delegates = {}
 
13
    @option_methods = {}
 
14
    @option_setting = {}
 
15
 
 
16
    if parent.kind_of? Hash
 
17
      keys = _symbolkey2str(parent)
 
18
      parent = keys.delete('parent')
 
19
      @frame = TkFrame.new(parent)
 
20
      @path = @epath = @frame.path
 
21
      initialize_composite(keys)
 
22
    else
 
23
      @frame = TkFrame.new(parent)
 
24
      @path = @epath = @frame.path
 
25
      initialize_composite(*args)
 
26
    end
 
27
  end
 
28
=end
 
29
 
 
30
  def _choice_classname_of_baseframe
 
31
    base_class_name = nil
 
32
 
 
33
    klass = WidgetClassNames[self.class::WidgetClassName]
 
34
 
 
35
    if klass
 
36
      # WidgetClassName is a known class
 
37
      if klass <= TkFrame || klass < TkComposite
 
38
        # klass is valid for the base frame
 
39
        if self.class <= klass
 
40
          # use my classname
 
41
          base_class_name = self.class.name
 
42
          if base_class_name == ''
 
43
            # anonymous class -> use ancestor's name
 
44
            base_class_name = klass.name
 
45
          end
 
46
        else
 
47
          # not subclass -> use WidgetClassName
 
48
          base_class_name = klass.name
 
49
        end
 
50
 
 
51
      else
 
52
        # klass is invalid for the base frame
 
53
        if self.class < TkFrame || self.class.superclass < TkComposite
 
54
          # my class name is valid for the base frame -> use my classname
 
55
          base_class_name = self.class.name
 
56
          if base_class_name == ''
 
57
            # anonymous class -> use TkFrame
 
58
            base_class_name = nil
 
59
          end
 
60
        else
 
61
          # no idea for the base frame -> use TkFrame
 
62
          base_class_name = nil
 
63
        end
 
64
      end
 
65
 
 
66
    elsif self.class::WidgetClassName && ! self.class::WidgetClassName.empty?
 
67
      # unknown WidgetClassName is defined -> use it for the base frame
 
68
      base_class_name = self.class::WidgetClassName
 
69
 
 
70
    else
 
71
      # no valid WidgetClassName
 
72
      if self.class < TkFrame || self.class.superclass < TkComposite
 
73
        # my class name is valid for the base frame -> use my classname
 
74
        base_class_name = self.class.name
 
75
        if base_class_name == ''
 
76
          # anonymous class -> use TkFrame
 
77
          base_class_name = nil
 
78
        end
 
79
      else
 
80
        # no idea for the base frame -> use TkFrame
 
81
        base_class_name = nil
 
82
      end
 
83
    end
 
84
 
 
85
    base_class_name
 
86
  end
 
87
  private :_choice_classname_of_baseframe
 
88
 
 
89
  # def initialize(parent=nil, *args)
 
90
  def initialize(*args)
 
91
    @delegates = {}
 
92
    @option_methods = {}
 
93
    @option_setting = {}
 
94
 
 
95
    if args[-1].kind_of?(Hash)
 
96
      keys = _symbolkey2str(args.pop)
 
97
    else
 
98
      keys = {}
 
99
    end
 
100
    parent = args.shift
 
101
    parent = keys.delete('parent') if keys.has_key?('parent')
 
102
 
 
103
    if keys.key?('classname')
 
104
      keys['class'] = keys.delete('classname')
 
105
    end
 
106
    if (base_class_name = (keys.delete('class')).to_s).empty?
 
107
      base_class_name = _choice_classname_of_baseframe
 
108
    end
 
109
 
 
110
    if base_class_name
 
111
      @frame = TkFrame.new(parent, :class=>base_class_name)
 
112
    else
 
113
      @frame = TkFrame.new(parent)
 
114
    end
 
115
    @path = @epath = @frame.path
 
116
 
 
117
    args.push(keys) unless keys.empty?
 
118
    initialize_composite(*args)
 
119
  end
 
120
 
 
121
  def database_classname
 
122
    @frame.database_classname
 
123
  end
 
124
 
 
125
  def database_class
 
126
    @frame.database_class
 
127
  end
 
128
 
 
129
  def epath
 
130
    @epath
 
131
  end
 
132
 
 
133
  def initialize_composite(*args) end
 
134
  private :initialize_composite
 
135
 
 
136
  def option_methods(*opts)
 
137
    opts.each{|m_set, m_cget, m_info|
 
138
      m_set  = m_set.to_s
 
139
      m_cget = m_set if !m_cget && self.method(m_set).arity == -1
 
140
      m_cget = m_cget.to_s if m_cget
 
141
      m_info = m_info.to_s if m_info
 
142
      @option_methods[m_set] = {
 
143
        :set  => m_set, :cget => m_cget, :info => m_info
 
144
      }
 
145
    }
 
146
  end
 
147
 
 
148
  def delegate_alias(alias_opt, option, *wins)
 
149
    if wins.length == 0
 
150
      fail ArgumentError, "target widgets are not given"
 
151
    end
 
152
    if alias_opt != option && (alias_opt == 'DEFAULT' || option == 'DEFAULT')
 
153
      fail ArgumentError, "cannot alias 'DEFAULT' option"
 
154
    end
 
155
    alias_opt = alias_opt.to_s
 
156
    option = option.to_s
 
157
    if @delegates[alias_opt].kind_of?(Array)
 
158
      if (elem = @delegates[alias_opt].assoc(option))
 
159
        wins.each{|w| elem[1].push(w)}
 
160
      else
 
161
        @delegates[alias_opt] << [option, wins]
 
162
      end
 
163
    else
 
164
      @delegates[alias_opt] = [ [option, wins] ]
 
165
    end
 
166
  end
 
167
 
 
168
  def delegate(option, *wins)
 
169
    delegate_alias(option, option, *wins)
 
170
  end
 
171
 
 
172
  def cget(slot)
 
173
    slot = slot.to_s
 
174
 
 
175
    if @option_methods.include?(slot)
 
176
      if @option_methods[slot][:cget]
 
177
        return self.__send__(@option_methods[slot][:cget])
 
178
      else
 
179
        if @option_setting[slot]
 
180
          return @option_setting[slot]
 
181
        else
 
182
          return ''
 
183
        end
 
184
      end
 
185
    end
 
186
 
 
187
    tbl = @delegates[slot]
 
188
    tbl = @delegates['DEFAULT'] unless tbl
 
189
 
 
190
    begin
 
191
      if tbl
 
192
        opt, wins = tbl[-1]
 
193
        opt = slot if opt == 'DEFAULT'
 
194
        if wins && wins[-1]
 
195
          return wins[-1].cget(opt)
 
196
        end
 
197
      end
 
198
    rescue
 
199
    end
 
200
 
 
201
    super(slot)
 
202
  end
 
203
 
 
204
  def configure(slot, value=None)
 
205
    if slot.kind_of? Hash
 
206
      slot.each{|slot,value| configure slot, value}
 
207
      return self
 
208
    end
 
209
 
 
210
    slot = slot.to_s
 
211
 
 
212
    if @option_methods.include?(slot)
 
213
      unless @option_methods[slot][:cget]
 
214
        if value.kind_of?(Symbol)
 
215
          @option_setting[slot] = value.to_s
 
216
        else
 
217
          @option_setting[slot] = value
 
218
        end
 
219
      end
 
220
      return self.__send__(@option_methods[slot][:set], value)
 
221
    end
 
222
 
 
223
    tbl = @delegates[slot]
 
224
    tbl = @delegates['DEFAULT'] unless tbl
 
225
 
 
226
    begin
 
227
      if tbl
 
228
        last = nil
 
229
        tbl.each{|opt, wins|
 
230
          opt = slot if opt == 'DEFAULT'
 
231
          wins.each{|w| last = w.configure(opt, value)}
 
232
        }
 
233
        return last
 
234
      end
 
235
    rescue
 
236
    end
 
237
 
 
238
    super(slot, value)
 
239
  end
 
240
 
 
241
  def configinfo(slot = nil)
 
242
    if TkComm::GET_CONFIGINFO_AS_ARRAY
 
243
      if slot
 
244
        slot = slot.to_s
 
245
        if @option_methods.include?(slot)
 
246
          if @option_methods[slot][:info]
 
247
            return self.__send__(@option_methods[slot][:info])
 
248
          else
 
249
            return [slot, '', '', '', self.cget(slot)]
 
250
          end
 
251
        end
 
252
 
 
253
        tbl = @delegates[slot]
 
254
        tbl = @delegates['DEFAULT'] unless tbl
 
255
 
 
256
        begin
 
257
          if tbl
 
258
            if tbl.length == 1
 
259
              opt, wins = tbl[0]
 
260
              if slot == opt || opt == 'DEFAULT'
 
261
                return wins[-1].configinfo(slot)
 
262
              else
 
263
                info = wins[-1].configinfo(opt)
 
264
                info[0] = slot
 
265
                return info
 
266
              end
 
267
            else
 
268
              opt, wins = tbl[-1]
 
269
              return [slot, '', '', '', wins[-1].cget(opt)]
 
270
            end
 
271
          end
 
272
        rescue
 
273
        end
 
274
 
 
275
        super(slot)
 
276
 
 
277
      else # slot == nil
 
278
        info_list = super(slot)
 
279
 
 
280
        tbl = @delegates['DEFAULT']
 
281
        if tbl
 
282
          wins = tbl[0][1]
 
283
          if wins && wins[-1]
 
284
            wins[-1].configinfo.each{|info|
 
285
              slot = info[0]
 
286
              info_list.delete_if{|i| i[0] == slot} << info
 
287
            }
 
288
          end
 
289
        end
 
290
 
 
291
        @delegates.each{|slot, tbl|
 
292
          next if slot == 'DEFAULT'
 
293
          if tbl.length == 1
 
294
            opt, wins = tbl[0]
 
295
            next unless wins && wins[-1]
 
296
            if slot == opt
 
297
              info_list.delete_if{|i| i[0] == slot} << 
 
298
                wins[-1].configinfo(slot)
 
299
            else
 
300
              info = wins[-1].configinfo(opt)
 
301
              info[0] = slot
 
302
              info_list.delete_if{|i| i[0] == slot} << info
 
303
            end
 
304
          else
 
305
            opt, wins = tbl[-1]
 
306
            info_list.delete_if{|i| i[0] == slot} << 
 
307
              [slot, '', '', '', wins[-1].cget(opt)]
 
308
          end
 
309
        }
 
310
 
 
311
        @option_methods.each{|slot, m|
 
312
          if m[:info]
 
313
            info = self.__send__(m[:info])
 
314
          else
 
315
            info = [slot, '', '', '', self.cget(slot)]
 
316
          end
 
317
          info_list.delete_if{|i| i[0] == slot} << info
 
318
        }
 
319
 
 
320
        info_list
 
321
      end
 
322
 
 
323
    else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
 
324
      if slot
 
325
        slot = slot.to_s
 
326
        if @option_methods.include?(slot)
 
327
          if @option_methods[slot][:info]
 
328
            return self.__send__(@option_methods[slot][:info])
 
329
          else
 
330
            return {slot => ['', '', '', self.cget(slot)]}
 
331
          end
 
332
        end
 
333
 
 
334
        tbl = @delegates[slot]
 
335
        tbl = @delegates['DEFAULT'] unless tbl
 
336
 
 
337
        begin
 
338
          if tbl
 
339
            if tbl.length == 1
 
340
              opt, wins = tbl[0]
 
341
              if slot == opt || opt == 'DEFAULT'
 
342
                return wins[-1].configinfo(slot)
 
343
              else
 
344
                return {slot => wins[-1].configinfo(opt)[opt]}
 
345
              end
 
346
            else
 
347
              opt, wins = tbl[-1]
 
348
              return {slot => ['', '', '', wins[-1].cget(opt)]}
 
349
            end
 
350
          end
 
351
        rescue
 
352
        end
 
353
 
 
354
        super(slot)
 
355
 
 
356
      else # slot == nil
 
357
        info_list = super(slot)
 
358
 
 
359
        tbl = @delegates['DEFAULT']
 
360
        if tbl
 
361
          wins = tbl[0][1]
 
362
          info_list.update(wins[-1].configinfo) if wins && wins[-1]
 
363
        end
 
364
 
 
365
        @delegates.each{|slot, tbl|
 
366
          next if slot == 'DEFAULT'
 
367
          if tbl.length == 1
 
368
            opt, wins = tbl[0]
 
369
            next unless wins && wins[-1]
 
370
            if slot == opt
 
371
              info_list.update(wins[-1].configinfo(slot))
 
372
            else
 
373
              info_list.update({slot => wins[-1].configinfo(opt)[opt]})
 
374
            end
 
375
          else
 
376
            opt, wins = tbl[-1]
 
377
            info_list.update({slot => ['', '', '', wins[-1].cget(opt)]})
 
378
          end
 
379
        }
 
380
 
 
381
        @option_methods.each{|slot, m|
 
382
          if m[:info]
 
383
            info = self.__send__(m[:info])
 
384
          else
 
385
            info = {slot => ['', '', '', self.cget(slot)]}
 
386
          end
 
387
          info_list.update(info)
 
388
        }
 
389
 
 
390
        info_list
 
391
      end
 
392
    end
 
393
  end
 
394
end