1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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
18 def initialize(parent, movelist_stack, factories)
20 @movelist_stack = movelist_stack
21 @factories = factories
24 tab_bar.visible = false
25 tab_bar.tabs_closable = true
26 on(:current_changed, ["int"]) {|i| self.index = i; fire :changed }
27 tab_bar.on(:tab_close_requested) {|i| delete_at(i) }
31 if value >= 0 and value < size and value != @index
33 self.current_index = value
34 @movelist_stack.current_index = value
35 on_activity(current, false)
39 def activate(user, name = nil)
40 @views.each_with_index do |view, i|
41 if user == view.controller
43 set_tab_text(i, name) if name
50 @views.each_with_index do |view, i|
51 return i if yield view
68 def create(opts = { })
69 if @clean and not opts[:force]
72 set_tab_text(index, opts[:name])
78 table = @factories[:table].new(self)
79 controller = @factories[:controller].new(table)
80 movelist = @factories[:movelist].new(controller)
82 v = View.new(table, controller, movelist)
88 def add(view, opts = { })
89 view.add_observer(self)
91 i = add_tab(view.main_widget, opts[:name] || "?")
93 raise "[bug] inconsistent MultiView index #{size - 1}, expected #{i}"
95 @movelist_stack.insert_widget(i, view.movelist)
96 if opts[:activate] || index == -1
99 tab_bar.visible = size > 1
104 raise "Cannot delete last view" if size <= 1
105 if index >= 0 and index < size
106 self.index -= 1 if index <= @index
108 v = @views.delete_at(index)
109 @movelist_stack.remove_widget(v.movelist)
110 tab_bar.visible = size > 1
113 v.delete_observer(self)
126 def on_activity(view, value = true)
127 i = find { |v| v == view }
129 @@base_color ||= tab_bar.tab_text_color(i)
131 if i != @index && value
132 tab_bar.set_tab_text_color(i, KDE::active_color)
134 tab_bar.set_tab_text_color(i, @@base_color)
138 def on_dirty(view, value = true)