1 |
package DC::UI::Dockable; |
2 |
|
3 |
use common::sense; |
4 |
|
5 |
our @ISA = DC::UI::Bin::; |
6 |
|
7 |
|
8 |
# A dockable can be docked into a DC::UI::Dockbar |
9 |
# Attributes: |
10 |
# 'can_close' says: This dockable has a close button as tab. |
11 |
# 'can_undock' says that the dockable tab has a 'undock' button. |
12 |
# 'title' is the title of the tab label or the title of the undocket window |
13 |
|
14 |
sub new { |
15 |
my $class = shift; |
16 |
|
17 |
my $self = $class->SUPER::new ( |
18 |
title => "unset", |
19 |
can_close => 1, |
20 |
can_undock => 0, # temporarily deactivated! |
21 |
@_, |
22 |
); |
23 |
|
24 |
$self->init; |
25 |
|
26 |
$self |
27 |
} |
28 |
|
29 |
# a setup method for the constructor |
30 |
sub init { |
31 |
my ($self) = @_; |
32 |
|
33 |
my $bb = $self->{c_tab} = DC::UI::ButtonBin->new (tooltip => $self->{tooltip}); |
34 |
$bb->add (my $vb = DC::UI::Box->new); |
35 |
$vb->add ( |
36 |
my $b = |
37 |
$self->{tab_label} = |
38 |
DC::UI::Label->new (expand => 1, valign => 0.5, align => 0, padding_x => 8, padding_y => 4) |
39 |
); |
40 |
|
41 |
if ($self->{can_close}) { |
42 |
$vb->add ( |
43 |
my $ib = DC::UI::ImageButton->new (path => 'x1_close.png', scale => 0.3) |
44 |
); |
45 |
$ib->connect (activate => sub { |
46 |
$self->close; |
47 |
0 |
48 |
}); |
49 |
} |
50 |
|
51 |
if ($self->{can_undock}) { |
52 |
$vb->add ( |
53 |
my $ib2 = DC::UI::ImageButton->new (path => 'x1_close.png', scale => 0.3) |
54 |
); |
55 |
$ib2->connect (activate => sub { |
56 |
$self->emit ("undock"); |
57 |
0 |
58 |
}); |
59 |
} |
60 |
|
61 |
|
62 |
$self->set_title ($self->{title}); |
63 |
} |
64 |
|
65 |
# This sets the title of the dockable. The title is displayed |
66 |
sub set_title { |
67 |
my ($self, $title) = @_; |
68 |
$self->{title} = $title; |
69 |
$self->update_tab; |
70 |
} |
71 |
|
72 |
# Returns the title |
73 |
sub get_title { $_[0]->{title} } |
74 |
|
75 |
# This method activates the tab of the dockable if it is docked |
76 |
sub select_my_tab { |
77 |
my ($self) = @_; |
78 |
if ($self->is_docked) { |
79 |
$self->{dockbar}->select_dockable ($self); |
80 |
} |
81 |
} |
82 |
|
83 |
# (private) This method is used by Dockbar to tell the dockable |
84 |
# it's position in the dockbar (if it has one, if it has no position, |
85 |
# $pos is undef). |
86 |
sub set_dockbar_pos { |
87 |
my ($self, $pos) = @_; |
88 |
$self->{dockbar_pos} = $pos; |
89 |
$self->update_tab; |
90 |
} |
91 |
|
92 |
# (private) This method tells the dockable that it is 'active', which means: |
93 |
# it is docked and it's tab has been activated and it is currently shown. |
94 |
sub set_dockbar_tab_active { |
95 |
my ($self, $active) = @_; |
96 |
$self->{dockbar_active} = $active; |
97 |
$self->update_tab; |
98 |
} |
99 |
|
100 |
# (private) This method updates the tab and other things of the dockable |
101 |
# whenever something has been changed (title, color, ...) |
102 |
sub update_tab { |
103 |
my ($self) = @_; |
104 |
# TODO: set color according to dockbar_active |
105 |
|
106 |
my $oldcolor = $self->{tab_label}->{fg}; |
107 |
if ($self->is_docked_active) { |
108 |
$self->{tab_label}->{fg} = $self->{active_fg} || [1, 1, 1]; |
109 |
} else { |
110 |
$self->{tab_label}->{fg} = $self->{inactive_fg} || [1, 1, 1,]; |
111 |
} |
112 |
if (join (',', @$oldcolor) ne join (',', @{$self->{tab_label}->{fg}})) { |
113 |
# update colors |
114 |
$self->{tab_label}->realloc; |
115 |
$self->{tab_label}->update; |
116 |
} |
117 |
|
118 |
$self->{tab_label}->set_markup ( |
119 |
$self->get_title |
120 |
. (defined $self->{dockbar_pos} |
121 |
? "-" . ($self->{dockbar_pos} + 1) |
122 |
: "") |
123 |
); |
124 |
} |
125 |
|
126 |
# This method sets the active foreground color of the dockable tab |
127 |
sub set_active_fg { |
128 |
my ($self, $fg) = @_; |
129 |
$self->{active_fg} = $fg; |
130 |
$self->update_tab; |
131 |
} |
132 |
|
133 |
# This method sets the inactive foreground color of the dockable tab |
134 |
sub set_inactive_fg { |
135 |
my ($self, $fg) = @_; |
136 |
$self->{inactive_fg} = $fg; |
137 |
$self->update_tab; |
138 |
} |
139 |
|
140 |
# (private) This method is used by the Dockbar to tell the Dockable which |
141 |
# Dockbar it belongs to. Do not call this method yourself, use the dockbars |
142 |
# add_dock and remove_dock methods instead. |
143 |
sub set_dockbar { |
144 |
my ($self, $dockbar) = @_; |
145 |
$self->{dockbar} = $dockbar; |
146 |
Scalar::Util::weaken $self->{dockbar}; |
147 |
} |
148 |
|
149 |
# This method is called when someone wants to 'activate' the dockable, |
150 |
# the meaning of being 'activated' is given by subclasses that inherit |
151 |
# from Dockable. Eg. In ChatView the 'activation' means that the entry field |
152 |
# of for chat is activated for input. |
153 |
sub activate { |
154 |
my ($self) = @_; |
155 |
$self->emit ("activate"); |
156 |
} |
157 |
|
158 |
# Returns whether this dockable is docked on a Dockbar. |
159 |
sub is_docked { |
160 |
my ($self) = @_; |
161 |
$self->{dockbar} or return 0; |
162 |
return $self->{dockbar}->is_docked ($self); |
163 |
} |
164 |
|
165 |
# Returns whether this dockable is docked _and_ it's tab |
166 |
# is active. |
167 |
sub is_docked_active { |
168 |
my ($self) = @_; |
169 |
$self->{dockbar} or return 0; |
170 |
return $self->{dockbar_active}; |
171 |
} |
172 |
|
173 |
# This method is called when the Dockable wants to be closed, which means |
174 |
# that it's window is closed or tab is removed from the dockbar and it |
175 |
# is removed from the dockbar. |
176 |
sub close { |
177 |
my ($self) = @_; |
178 |
return if !$self->{can_close}; |
179 |
$self->emit ("close_dock"); |
180 |
} |
181 |
|
182 |
1 |