-
Notifications
You must be signed in to change notification settings - Fork 7
/
BBBikeScribble.pm
267 lines (245 loc) · 6.88 KB
/
BBBikeScribble.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- perl -*-
#
# Author: Slaven Rezic
#
# Copyright (C) 2002,2016 Slaven Rezic. All rights reserved.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: [email protected]
# WWW: http://bbbike.de
#
package BBBikeScribble;
use strict;
use vars qw($VERSION);
$VERSION = '1.07';
package Tk::Babybike; # XXX
no strict; # XXX make strict!!!
use vars qw(
$scribble_mode
$scribble_frame $scribble_color @scribble_colors $show_scribble
$show_scribble_labels
$scribble_file $scribble_bbd $scribble_desc
);
use vars qw($IDX_SCRIBBLE $IDX_TIME $IDX_LABEL);
$IDX_SCRIBBLE = 0;
$IDX_TIME = 1;
$IDX_LABEL = 2;
use vars qw($lastitem);
# init
$scribble_color = 'blue'
if !defined $scribble_color;
@scribble_colors = qw/red blue yellow3 green black white/
if !@scribble_colors;
$show_scribble = 1
if !defined $show_scribble;
$scribble_file = "$main::tmpdir/scribble.canvas"
if !defined $scribble_file;
$scribble_bbd = "$main::tmpdir/scribble.bbd"
if !defined $scribble_bbd;
$scribble_desc = "$main::tmpdir/scribble.desc"
if !defined $scribble_desc;
$show_scribble_labels = 1
if !defined $show_scribble_labels;
sub toggle_scribble_mode {
if ($scribble_mode) {
deselect_current_mode()
if defined &deselect_current_mode;
if (defined &MM_SCRIBBLE) {
$map_mode = MM_SCRIBBLE();
} else {
$main::map_mode = main::MM_SCRIBBLE(); # XXX
}
} else {
if (defined &MM_BABYBIKE_SELECT) {
$map_mode = MM_BABYBIKE_SELECT();
} else {
$main::map_mode = main::MM_SEARCH(); # XXX
}
}
if ((defined &MM_SCRIBBLE && $map_mode == MM_SCRIBBLE()) ||
(!defined &MM_SCRIBBLE && $main::map_mode == main::MM_SCRIBBLE())) { # XXX
set_scribble_mode();
} else {
deselect_scribble_mode();
}
}
sub set_scribble_mode {
if (!$scribble_frame || !Tk::Exists($scribble_frame)) {
$scribble_frame = $c->Frame
(-bg => '#c0c0c0');
foreach my $color_name (@scribble_colors, 'delete', 'label') {
$scribble_frame->Radiobutton
(-text => $color_name,
($color_name !~ /^(delete|label)$/ ? (-fg => $color_name) : ()),
-value => $color_name,
-variable => \$scribble_color,
-padx => 0, -pady => 0,
-highlightthickness => 0,
-anchor => 'w',
)->pack(-anchor => 'nw',
-fill => 'x');
}
}
$scribble_frame->idletasks;
$scribble_frame->place('-relx' => 1,
'-x' => -$scribble_frame->reqwidth,
'-rely' => 1,
'-y' => -$scribble_frame->reqheight);
if (!$show_scribble) {
$show_scribble = 1;
set_show_scribble();
}
$c->configure(-cursor => "hand2");
}
sub deselect_scribble_mode {
if ($scribble_frame && Tk::Exists($scribble_frame)) {
$scribble_frame->placeForget;
}
}
sub set_show_scribble {
$c->itemconfigure('scribble',
-state => ($show_scribble ? 'normal' : 'hidden'));
}
sub set_show_scribble_labels {
$c->itemconfigure('scribble-label',
-state => ($show_scribble_labels ? 'normal' : 'hidden'));
}
sub load_scribble {
my $mw = $c->toplevel;
if ($c->find("withtag" => "scribble")) {
if ($mw->messageBox(-title => 'Delete?',
-message => 'Delete existing scribble?',
-icon => 'question',
-type => 'YesNo') =~ /no/i) {
return;
}
}
$mw->Busy(-recurse => 1);
eval {
$c->delete("scribble");
$c->load_canvas($scribble_file);
};
my $err = $@;
$mw->Unbusy;
warn $err if $err;
}
sub save_scribble {
my @items = $c->find("withtag" => "scribble");
if (!@items) {
common_dialog("No scribble to save");
return;
}
my $mw = $c->toplevel;
$mw->Busy(-recurse => 1);
eval {
$c->save_canvas($scribble_file, withtag => "scribble");
warn "OK, saved to $scribble_file";
};
my $err = $@;
eval {
$anti_transpose = \&main::anti_transpose if !$anti_transpose;
# XXX does not work with bbbike, only with tkbabybike
# XXX is this still true???
if (open(BBD, ">$scribble_bbd")) {
foreach my $item (@items) {
my $color = $c->itemcget($item, -fill);
my @tags = $c->gettags($item);
(my $time = $tags[$IDX_TIME]) =~ s/^T//;
(my $label = $tags[$IDX_LABEL]) =~ s/^L//;
my @coords = $c->coords($item);
print BBD $label . ($label ne '' ? ' ' : '') .
scalar(localtime($time))."\t$color";
for(my $i=0; $i<$#coords; $i+=2) {
my($x,$y) = $anti_transpose->($coords[$i],$coords[$i+1]);
printf BBD " %d,%d", $x,$y;
}
print BBD "\n";
}
close BBD;
if (open(DESC, ">$scribble_desc")) {
foreach my $color_name (@scribble_colors) {
printf DESC "\$category_color{'$color_name'} = '#%02x%02x%02x';\n",
map { $_/256 } $mw->rgb($color_name);
}
close DESC;
}
}
};
$err .= $@;
$mw->Unbusy;
main::status_message($err, "error") if $err;
}
sub handle_button1_scribble {
my($w,$e) = @_;
if ($scribble_color eq 'delete') {
my(@tags) = $c->gettags('current');
if (grep {$_ eq 'scribble'} @tags) {
$c->delete('current');
}
} elsif ($scribble_color eq 'label') {
my(@tags) = $c->gettags('current');
if (grep {$_ eq 'scribble'} @tags) {
@tags = map { $_ eq 'current' ? () : $_ } @tags;
my $item = $c->find("withtag", "current");
(my $label = $tags[$IDX_LABEL]) =~ s/^L//;
my $mw = $c->toplevel;
my $t = $mw->Toplevel(-title => "Label");
$t->transient($mw);
my $e = $t->Entry(-textvariable => \$label)->grid(-columnspan => 2);
$e->focus;
my $cont = 0;
my $okcb = sub {
$c->dtag($item, $tags[$IDX_LABEL]);
$label =~ s/\s+/_/g;
$c->addtag("L$label",
"withtag", $item,
);
my(@c) = $c->coords($item);
$c->createText(@c[0,1], -text => $label, -anchor => "sw",
-state => ($show_scribble_labels ? "normal" : "hidden"),
-tags => ['scribble', 'scribble-label']);
# XXX deleting labels?
$cont++;
};
Tk::grid(my $okb = $t->Button
(-text => 'OK',
-command => $okcb),
$t->Button
(-text => 'Cancel',
-command => sub { $cont++ }
)
);
$e->bind("<Return>" => sub { $okcb->() });
$t->waitVariable(\$cont);
$t->destroy;
}
} else {
($lastx,$lasty) = ($c->canvasx($e->x), $c->canvasy($e->y));
$lastitem =
$c->createLine($lastx,$lasty,$lastx,$lasty,
-width => 2, -fill => $scribble_color,
-capstyle => 'round',
-tags => ['scribble', "T".time, "L"]);
}
}
sub handle_button1_motion_scribble {
my($w,$e) = @_;
my($cx,$cy) = ($c->canvasx($e->x), $c->canvasy($e->y));
if ($scribble_color eq 'delete') {
my(@items) = $c->find('overlapping', $cx-1,$cy-1,$cx+1,$cy+1);
foreach my $item (@items) {
if (grep {$_ eq 'scribble'} $c->gettags($item)) {
$c->delete($item);
}
}
} elsif ($scribble_color ne 'label') {
$c->createLine($lastx,$lasty,$cx,$cy,
-width => 2, -fill => $scribble_color,
-capstyle => 'round',
-tags => ['scribble', "T".time, "L"]);
($lastx,$lasty) = ($cx,$cy);
}
}
1;
__END__