|
|
1 | use utf8; |
|
|
2 | |
1 | package superchat; |
3 | package superchat; |
2 | |
4 | |
3 | # waaay cool widget. well... maybe at one point in the future |
5 | # waaay cool widget. well... maybe at one point in the future |
4 | |
6 | |
5 | use Gtk2; |
7 | use Gtk2; |
… | |
… | |
134 | } |
136 | } |
135 | |
137 | |
136 | sub append_text { |
138 | sub append_text { |
137 | my ($self, $text) = @_; |
139 | my ($self, $text) = @_; |
138 | |
140 | |
139 | my $at_end = $self->at_end; |
|
|
140 | $self->_append_text ($self->{end}, $text); |
141 | $self->_append_text ($self->{end}, $text); |
141 | $self->set_end if $at_end; |
|
|
142 | } |
142 | } |
143 | |
143 | |
144 | sub _append_text { |
144 | sub _append_text { |
145 | my ($self, $mark, $text) = @_; |
145 | my ($self, $mark, $text) = @_; |
|
|
146 | |
|
|
147 | my $at_end = $self->at_end; |
146 | |
148 | |
147 | $text = "<default>$text</default>"; |
149 | $text = "<default>$text</default>"; |
148 | |
150 | |
149 | my @tag; |
151 | my @tag; |
150 | # pseudo-simplistic-xml-parser |
152 | # pseudo-simplistic-xml-parser |
… | |
… | |
159 | |
161 | |
160 | $text =~ /\G([^<]*)/gc or last; |
162 | $text =~ /\G([^<]*)/gc or last; |
161 | $self->{buffer}->insert_with_tags_by_name ($self->{buffer}->get_iter_at_mark ($mark), util::xmlto $1, @tag) |
163 | $self->{buffer}->insert_with_tags_by_name ($self->{buffer}->get_iter_at_mark ($mark), util::xmlto $1, @tag) |
162 | if length $1; |
164 | if length $1; |
163 | } |
165 | } |
|
|
166 | |
|
|
167 | $self->set_end if $at_end; |
164 | } |
168 | } |
165 | |
169 | |
166 | sub set_text { |
170 | sub set_text { |
167 | my ($self, $text) = @_; |
171 | my ($self, $text) = @_; |
168 | |
172 | |
… | |
… | |
207 | sub new_switchable_inlay { |
211 | sub new_switchable_inlay { |
208 | my ($self, $header, $cb, $visible) = @_; |
212 | my ($self, $header, $cb, $visible) = @_; |
209 | |
213 | |
210 | my $inlay; |
214 | my $inlay; |
211 | |
215 | |
212 | my $setvisible = sub { |
|
|
213 | if ($inlay->{visible}) { |
|
|
214 | $inlay->{cb}->($inlay); |
|
|
215 | } else { |
|
|
216 | $inlay->clear; |
|
|
217 | } |
|
|
218 | }; |
|
|
219 | |
|
|
220 | my $tag = $self->new_eventtag (sub { |
216 | my $tag = $self->new_eventtag (sub { |
221 | my ($tag, $view, $event, $iter) = @_; |
217 | my ($tag, $view, $event, $iter) = @_; |
222 | |
218 | |
223 | if ($event->type eq "button-press") { |
219 | if ($event->type eq "button-press") { |
224 | $inlay->{visible} = !$inlay->{visible}; |
220 | $inlay->set_visible (!$inlay->{visible}); |
225 | $setvisible->(); |
|
|
226 | } |
221 | } |
227 | |
222 | |
228 | 1; |
223 | 1; |
229 | }); |
224 | }); |
230 | |
225 | |
231 | $tag->set (background => "#e0e0ff"); |
226 | $tag->set (background => "#e0e0ff"); |
232 | |
227 | |
233 | $self->{buffer}->insert ($self->{buffer}->get_end_iter, "\n"); |
|
|
234 | $self->{buffer}->insert_with_tags ($self->{buffer}->get_end_iter, util::xmlto "$header \x{21f3}", $tag); |
|
|
235 | |
|
|
236 | $inlay = $self->new_inlay; |
228 | $inlay = $self->new_inlay; |
237 | |
229 | |
238 | $inlay->{visible} = $visible; |
230 | $inlay->{visible} = 0; |
|
|
231 | $inlay->{header} = $header; |
239 | $inlay->{tag} = $tag; |
232 | $inlay->{tag} = $tag; |
240 | $inlay->{cb} = $cb; |
233 | $inlay->{cb} = $cb; |
241 | |
234 | |
242 | $setvisible->(); |
235 | Scalar::Util::weaken $inlay->{tag}; |
|
|
236 | |
|
|
237 | $inlay->set_visible ($visible); |
243 | |
238 | |
244 | $inlay; |
239 | $inlay; |
245 | } |
240 | } |
246 | |
241 | |
247 | package superchat::inlay; |
242 | package superchat::inlay; |
… | |
… | |
258 | my ($self, $text) = @_; |
253 | my ($self, $text) = @_; |
259 | |
254 | |
260 | $self->{parent}->_append_text ($self->{r}, $text); |
255 | $self->{parent}->_append_text ($self->{r}, $text); |
261 | } |
256 | } |
262 | |
257 | |
|
|
258 | sub visible { $_[0]{visible} } |
|
|
259 | sub set_visible { |
|
|
260 | my ($self, $visible) = @_; |
|
|
261 | |
|
|
262 | return if $self->{visible} == $visible; |
|
|
263 | $self->{visible} = $visible; |
|
|
264 | |
|
|
265 | $self->refresh; |
|
|
266 | } |
|
|
267 | |
|
|
268 | sub refresh { |
|
|
269 | my ($self) = @_; |
|
|
270 | |
|
|
271 | $self->clear; |
|
|
272 | |
|
|
273 | my $arrow = $self->{visible} ? "⊟" : "⊞"; |
|
|
274 | |
|
|
275 | $self->{buffer}->insert ($self->riter, "\n"); |
|
|
276 | $self->{buffer}->insert_with_tags ($self->riter, util::xmlto "$arrow $self->{header}", $self->{tag}); |
|
|
277 | |
|
|
278 | return unless $self->{visible}; |
|
|
279 | |
|
|
280 | $self->{cb}->($self); |
|
|
281 | } |
|
|
282 | |
263 | sub DESTROY { |
283 | sub DESTROY { |
264 | my ($self) = @_; |
284 | my ($self) = @_; |
265 | |
285 | |
266 | $self->{parent}{tagtable}->remove (delete $self->{tag}) if $self->{tag}; |
286 | $self->{parent}{tagtable}->remove (delete $self->{tag}) if $self->{tag}; |
267 | } |
287 | } |