More documentation for the MatchHandler class.
[kaya.git] / lib / multiview.rb
blob2edb630c7b488cffaf028a89b38e81020e043b78
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'observer_utils'
10 class MultiView < KDE::TabWidget
11   attr_reader :index
12   include Observable
13   include Enumerable
14   
15   def initialize(parent, movelist_stack)
16     super(parent)
17     @movelist_stack = movelist_stack
18     @views = []
19     @index = -1
20     tab_bar.visible = false
21     tab_bar.tabs_closable = true
22     on(:current_changed) {|i| self.index = i; fire :changed }
23     tab_bar.on(:tab_close_requested) {|i| delete_at(i) }
24   end
25   
26   def index=(value)
27     if value >= 0 and value < size and value != @index
28       @index = value
29       self.current_index = value
30       @movelist_stack.current_index = value
31     end
32   end
33   
34   def current
35     if @index != -1
36       @views[@index]
37     end
38   end
39   
40   def add(view, opts = { })
41     @views << view
42     i = add_tab(view.main_widget, opts[:name] || "?")
43     unless i == size - 1
44       raise "[bug] inconsistent MultiView index #{size - 1}, expected #{i}"
45     end
46     @movelist_stack.insert_widget(i, view.movelist)
47     if opts[:activate] || index == -1
48       self.index = i
49     end
50     tab_bar.visible = size > 1
51     i
52   end
53   
54   def delete_at(index)
55     raise "Cannot delete last view" if size <= 1
56     if index >= 0 and index < size
57       self.index -= 1 if index <= @index
58       removeTab(index)
59       v = @views.delete_at(index)
60       @movelist_stack.remove_widget(v.movelist)
61       tab_bar.visible = size > 1
62       v
63     end
64   end
65   
66   def size
67     @views.size
68   end
69   
70   def each(&blk)
71     @views.each(&blk)
72   end
73 end