1 |
package DC::UI::Dockbar; |
2 |
use DC::UI::Dockable; |
3 |
|
4 |
use strict; |
5 |
use utf8; |
6 |
|
7 |
our @ISA = DC::UI::Toplevel::; |
8 |
|
9 |
sub new { |
10 |
my $class = shift; |
11 |
|
12 |
my $self = $class->SUPER::new ( |
13 |
name => "message_window2", |
14 |
title => "Messages XXX", |
15 |
border_bg => [1, 1, 1, 1], |
16 |
x => "max", |
17 |
y => 0, |
18 |
force_w => $::WIDTH * 0.4, |
19 |
force_h => $::HEIGHT * 0.5, |
20 |
has_close_button => 1, |
21 |
child => (my $nb = DC::UI::Notebook->new (expand => 1)), |
22 |
@_, |
23 |
); |
24 |
|
25 |
$self->{notebook} = $nb; |
26 |
|
27 |
$nb->connect (page_changed => sub { |
28 |
my ($nb, $page) = @_; |
29 |
$self->update_active ($page); |
30 |
0 |
31 |
}); |
32 |
|
33 |
$self |
34 |
} |
35 |
|
36 |
# This method is which you want to call to add a Dockable |
37 |
sub add_dock { |
38 |
my ($self, $dockable) = @_; |
39 |
$self->{docks}->{"$dockable"} = $dockable; |
40 |
delete $self->{dock_windows}->{"$dockable"}; |
41 |
|
42 |
$dockable->set_dockbar ($self); |
43 |
|
44 |
# TODO: capture the guards to remove these connections |
45 |
$dockable->connect (dock => sub { |
46 |
my ($dockable) = @_; |
47 |
#d# warn "DOCKABLE DOCK"; |
48 |
$self->dock ($dockable); |
49 |
0 |
50 |
}); |
51 |
|
52 |
$dockable->connect (undock => sub { |
53 |
my ($dockable) = @_; |
54 |
#d# warn "DOCKABLE UNDOCK"; |
55 |
$self->undock ($dockable); |
56 |
0 |
57 |
}); |
58 |
|
59 |
$dockable->connect (close_dock => sub { |
60 |
my ($dockable) = @_; |
61 |
$self->remove_dock ($dockable); |
62 |
0 |
63 |
}); |
64 |
|
65 |
$self->dock ($dockable); |
66 |
} |
67 |
|
68 |
# This method will remove the dockable from the Dockbar. Which means that the |
69 |
# window for this dockable is also removed (if it was undocked). |
70 |
sub remove_dock { |
71 |
my ($self, $dockable) = @_; |
72 |
|
73 |
$self->undock_window ($dockable); |
74 |
$self->undock_notebook ($dockable); |
75 |
delete $self->{docks}->{"$dockable"}; |
76 |
$dockable->set_dockbar (undef); |
77 |
} |
78 |
|
79 |
# This method makes sure the dockable is 'docked' into the Dockbar |
80 |
# and eg. removes the free floating window it maybe has. |
81 |
sub dock { |
82 |
my ($self, $dockable) = @_; |
83 |
$self->undock_window ($dockable); |
84 |
|
85 |
# here the assumption is done that $dockable is inserted at the end of the |
86 |
# notebook tabs, so that the other tabs dont have to be updated |
87 |
$self->{notebook}->add ($dockable); |
88 |
$dockable->set_dockbar_pos ($self->{notebook}->page_index ($dockable)); |
89 |
$self->update_active; |
90 |
} |
91 |
|
92 |
# (private) This method updates all docked tabs and tells them whether their |
93 |
# tab is 'active'. |
94 |
sub update_active { |
95 |
my ($self, $page) = @_; |
96 |
|
97 |
unless ($page) { |
98 |
$page = $self->{notebook}->get_current_page; |
99 |
} |
100 |
|
101 |
for ($self->{notebook}->pages) { |
102 |
$_->set_dockbar_tab_active ($_ eq $page); |
103 |
} |
104 |
} |
105 |
|
106 |
# This method undocks the dockable (if it isn't already undocked) and |
107 |
# creates a floating window for it. |
108 |
sub undock { |
109 |
my ($self, $dockable) = @_; |
110 |
return if $self->{dock_windows}->{"$dockable"}; |
111 |
|
112 |
$self->undock_notebook ($dockable); |
113 |
my $win = |
114 |
$self->{dock_windows}->{"$dockable"} = |
115 |
DC::UI::Toplevel->new ( |
116 |
title => $dockable->get_title, |
117 |
child => $dockable, |
118 |
force_w => 100, force_h => 100, |
119 |
x => 100, y => 100, |
120 |
has_close_button => 1, |
121 |
); |
122 |
|
123 |
$win->connect (delete => sub { |
124 |
$self->dock ($dockable); |
125 |
0 |
126 |
}); |
127 |
|
128 |
$win->show; |
129 |
} |
130 |
|
131 |
# (private) This method does the cleanup stuff when the dockable is docked |
132 |
# into the dockbar and had a floating window. |
133 |
sub undock_window { |
134 |
my ($self, $dockable) = @_; |
135 |
my $win = |
136 |
$self->{dock_windows}->{"$dockable"} |
137 |
or return; |
138 |
|
139 |
$win->remove ($dockable); |
140 |
delete $self->{dock_windows}->{"$dockable"}; |
141 |
$win->hide; # XXX: neccessary? |
142 |
} |
143 |
|
144 |
# (private) This method does the cleanup stuff which is neccessary when the |
145 |
# dockable is removed from the notebook. |
146 |
sub undock_notebook { |
147 |
my ($self, $dockable) = @_; |
148 |
$self->{notebook}->remove ($dockable); |
149 |
my $nextpage = ($self->{notebook}->pages)[0]; |
150 |
$self->{notebook}->set_current_page ($nextpage) |
151 |
if $nextpage; |
152 |
$dockable->set_dockbar_pos (undef); |
153 |
$dockable->set_dockbar_tab_active (undef); |
154 |
$self->update_dockbar_positions; |
155 |
} |
156 |
|
157 |
# (private) This method updates the position of the dockables in the dockbar. |
158 |
sub update_dockbar_positions { |
159 |
my ($self) = @_; |
160 |
my $i = 0; |
161 |
for ($self->{notebook}->pages) { |
162 |
$_->set_dockbar_pos ($i++); |
163 |
} |
164 |
} |
165 |
|
166 |
# Returns all Dockables of this Dockbar |
167 |
sub dockables { |
168 |
my ($self) = @_; |
169 |
values %{$self->{docks}} |
170 |
} |
171 |
|
172 |
# Returns whether the dockable is currently docked. (and not |
173 |
# a floating window). |
174 |
sub is_docked { |
175 |
my ($self, $dockable) = @_; |
176 |
return not exists $self->{dock_windows}->{"$dockable"}; |
177 |
} |
178 |
|
179 |
# switiching to a page |
180 |
sub user_switch_to_page { |
181 |
my ($self, $page) = @_; |
182 |
$page = $page eq '0' ? 10 : $page; |
183 |
|
184 |
my @tabs = $self->{notebook}->pages; |
185 |
|
186 |
for (my $i = 0; $i < ($page - 1); $i++) { |
187 |
shift @tabs; |
188 |
} |
189 |
|
190 |
my $page = shift @tabs; |
191 |
return unless $page; |
192 |
|
193 |
$self->{notebook}->set_current_page ($page); |
194 |
} |
195 |
|
196 |
# This method activates the tab of the dockable if it is docked. |
197 |
sub select_dockable { |
198 |
my ($self, $dockable) = @_; |
199 |
return unless exists $self->{docks}->{"$dockable"}; |
200 |
$self->{notebook}->set_current_page ($dockable); |
201 |
} |
202 |
|
203 |
# close current tab |
204 |
sub close_current_tab { |
205 |
my ($self) = @_; |
206 |
$self->remove_dock ($self->{notebook}->get_current_page); |
207 |
} |
208 |
|
209 |
# "activates" the current page |
210 |
sub activate_current { |
211 |
my ($self) = @_; |
212 |
$self->{notebook}->get_current_page->activate |
213 |
} |
214 |
|
215 |
1 |