Debian Codegraph @

examples/dnd_demo

raw
1 #!/usr/local/bin/perl -w
2 #
3 # Demo code to exhibit features of dynamic construction and destruction of
4 # local (within app.) Drag and Drop sources and sinks. Written for and tested
5 # with Perl v5.004_01 and Tk version 400.202, on a DEC Alpha (OSI) platform.
6 #
7 # Drag and drop is only possible after the 'New' button has been used, and
8 # DropSites cease to respond once they've been filled. After the 'Save' button
9 # is used, dragging from the listbox is no longer possible.
10 #
11 # Pod documentation appended is a draft outline for the official documentation,
12 # and is almost certainly neither complete nor correct. Comments/suggestions
13 # for improvements welcomed.
14 #
15 # John Attwood ([email protected]), May '98
16 #
17
18
19 use Tk;
20 use Tk::Table;
21 use Tk::DragDrop;
22 use Tk::DropSite;
23 use strict;
24
25 my ($ButtonClr, $ListClr, $DfltClr) = ("SkyBlue1", "LightSkyBlue2", "#d9d9d9");
26 my ($rows, $marker_repeat) = (9, 4);
27 my $font3 = '-*-fixed-*-*-*-*-*-140-*-*-*-*-*-*';
28 my ($rdgel_ref, $user_id, $session_id, $source, @clones, @enzymes, @reqs,
29 @lbls, @drops, $method);
30 my $changed = 0;
31
32 #
33 # Create a main window with a listbox, a table and some buttons
34 #
35
36 my $w = MainWindow->new();
37 $w->title("Drag and Drop demo");
38 $w->iconname("DnD demo");
39 my $w_buttons = $w->Frame;
40 $w->Label(-text => "The radiobuttons control the contents of the drag window " .
41 "(choice must be made before pressing New).")
42 ->pack(-side => 'bottom');
43 $w->Label(-text => "Use New to set up DnD, then drag rows from listbox to blue " .
44 "DropSites in the Table. Use Save to inhibit further " .
45 "Drag/Dropping.")
46 ->pack(-side => 'bottom');
47 my $tf = $w->Frame->pack(-side => 'right');
48 my $table = $tf->Table(-rows => 10,
49 -columns => 3,
50 -scrollbars => '',
51 -fixedrows => 1,
52 -fixedcolumns => 1,
53 -takefocus => 1,
54 );
55 $table->pack(-side => 'bottom', -fill => 'both');
56 my $geltitle = $tf->Label(-relief => 'raised')->pack(-side => 'top',
57 -fill => 'x');
58 $w_buttons->pack(qw(-side bottom -fill x -pady 0.5m));
59
60 $w_buttons->Button(
61 -background => $ButtonClr,
62 -text => 'Quit',
63 -command => [\&Done],
64 )->pack(qw(-side right -expand 1));
65 my $save_btn = $w_buttons->Button(
66 -background => $ButtonClr,
67 -text => 'Save',
68 -command => [\&SaveGel],
69 -state => 'disabled'
70 )->pack(qw(-side right -expand 1));
71 $method = 'variable';
72 $w_buttons->Button(-background => $ButtonClr, -text => "New",
73 -command => \&NewGel)->pack(-side => 'right');
74 my $bf = $w_buttons->Frame->pack(-side => 'left');
75 $bf->Radiobutton(-text => 'Image', -value => 'image',
76 -variable => \$method)->pack(-side => 'top',
77 -anchor => 'nw');
78 $bf->Radiobutton(-text => 'Text', -value => 'text',
79 -variable => \$method)->pack(-side => 'top',
80 -anchor => 'nw');
81 $bf->Radiobutton(-text => 'Variable', -value => 'variable',
82 -variable => \$method)->pack(-side => 'top',
83 -anchor => 'nw');
84 my $aw = $w->Frame->pack(qw(-side bottom));
85 $aw->Label(-text => "Requests Outstanding")->pack(-side => 'top',
86 -fill => 'x');
87 my $lb = $aw->Listbox(-height => 10, -width => 62,
88 -background => $DfltClr, -font => $font3);
89 $lb->pack(-side => 'left', -fill => 'y');
90 my $awsb = $aw->Scrollbar(-command => [$lb => 'yview']);
91 $awsb->pack(-side => 'right', -fill => 'y');
92 $lb->configure(-yscrollcommand => [$awsb => 'set']);
93
94 #
95 # Make the listbox a Drag and Drop source, but disable this for now by
96 # binding -startcommand to a routine which does nothing but return true
97 # (indicating that it has handled the Drag initialisation itself)
98 #
99 # The last 3 parameters to the DragDrop invocation are (mutually exclusive)
100 # alternatives, the commented-out versions allow fixed text or a fixed image
101 # to be displayed in the Drag window, whilst defining a -startcommand callback
102 # allows us to choose the text on the fly and show visually exactly what is
103 # being dragged.
104 #
105
106 $source = $lb->DragDrop(-event => '<B1-Motion>',
107 -sitetypes => [qw(Local)],
108 -handlers => [[\&send_string],
109 [-type => 'FILE_NAME',
110 \&send_file]],
111 -startcommand => [\&DragInhibit],
112 # -image => $image, #use an image, not text
113 # -text => "this is it", #fixed text string
114 );
115
116
117 #
118 # Generate a bitmap for use with Drag and Drop.
119 #
120
121 my $image = $w->Bitmap('win', # -file => 'win.xbm'
122 -data => "#define win.xbm_width 16
123 #define win.xbm_height 16
124 static char win.xbm_bits[] = {
125 0xff, 0xff, 0x0d, 0xb0, 0xff, 0xff, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
126 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
127 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0xff, 0xff};
128 ");
129
130 #
131 # Populate the table and listbox
132 #
133
134 BuildTable();
135 Fill_lb();
136
137 #
138 # Let the user interact
139 #
140
141 MainLoop();
142
143 ######################################################################
144 #
145 # End of main program, start of subroutines
146 #
147 #
148
149 #
150 # Put some data into the listbox so that it can be dragged and dropped
151 #
152
153 sub Fill_lb {
154 my @data = (
155 "W08A5 HindIII 01-JAN-98 ja1 Re-requested 19070",
156 "Ab113C11 BamHI 01-JAN-98 ja1 Re-requested 18399",
157 "Ab113C11 EcoRI 01-JAN-98 ja1 Re-requested 18400",
158 "Ab113C11 HindIII 01-JAN-98 ja1 Re-requested 18401",
159 "cP91H8 EcoRI 01-JAN-98 ja1 Re-requested 18535",
160 "cP91H8 HindIII 01-JAN-98 ja1 Re-requested 18536"
161 );
162 $lb->delete('0','end');
163 $lb->insert('end',@data);
164 }
165
166 #
167 # Populate the rows and columns of the table. Inactive (marker) rows just have
168 # a label; remainder have a label (which will become a dropsite) plus two
169 # readonly text-entry fields to display the dropped data
170 #
171
172 sub BuildTable {
173 my ($row);
174
175 $table->put(0, 1, $table->Label(-text => "Clone",
176 -relief => 'raised',
177 -width => 15));
178 $table->put(0, 2, $table->Label(-text => "Enzyme",
179 -relief => 'raised',
180 -width => 15));
181 foreach $row (1..$rows) {
182 if (($row - 1) % $marker_repeat == 0) {
183 my $lbl = $table->Label(-text => "Marker",
184 -relief => 'sunken',
185 -width => 15);
186 $table->put($rows - $row + 1,0,$lbl);
187 } else {
188 my $lbl = $table->Label(-text => "Lane $row",
189 -relief => 'sunken',
190 );
191 $lbls[$row] = $lbl;
192 $table->put($rows - $row + 1,0,$lbl);
193 my $centry = $table->Entry(-width => 15,
194 -textvariable => \$clones[$row],
195 -state => 'disabled');
196 $table->put($rows - $row + 1,1,$centry);
197 my $dentry = $table->Entry(-width => 15,
198 -textvariable => \$enzymes[$row],
199 -state => 'disabled');
200 $table->put($rows - $row + 1,2,$dentry);
201 }
202 }
203 }
204
205 #
206 # Make active Dropsites respond (by changing their relief) when dragged object
207 # passes over them
208 #
209
210 sub SiteEntry {
211 my ($lbl, $entry, @data) = @_;
212 $lbl->configure(-relief => $entry == 1 ? 'raised' : 'sunken');
213 }
214
215 #
216 # This handler would be invoked if our drop handler specified FILE_NAME as
217 # the data type. Not used here, but included for illustration.
218 #
219
220 sub send_file {
221 my ($offset,$max) = @_;
222
223 return __FILE__;
224 }
225
226 #
227 # get current selection from listbox and return it to dropsite
228 #
229
230 sub send_string
231 {
232 my ($offset,$max) = @_;
233 my ($lb_sel) = $lb->curselection;
234 my ($req) = $lb->get($lb_sel);
235 return $req;
236 }
237
238 #
239 # Handle a drop - get dragged text, break into components and place in arrays
240 # to be displayed in table, delete source from listbox, kill the dropsite and
241 # make its colour revert to default.
242 #
243
244 sub Dropit {
245 my ($row,$seln) = @_;
246 print "$row $seln\n";
247 my ($rdreq) = $lb->SelectionGet('-selection'=>$seln,'STRING');
248 print "$rdreq\n";
249 my ($clone, $enz) = split " ", $rdreq;
250 $clones[$row] = $clone;
251 $enzymes[$row] = $enz;
252 my ($lb_sel) = $lb->curselection;
253 $lb->delete($lb_sel);
254 $lb->selectionSet($lb_sel);
255 $reqs[$row] = $rdreq;
256 $drops[$row]->delete;
257 $lbls[$row]->configure(-bg => $DfltClr);
258 $changed = 1;
259 }
260
261 #
262 # Empty the table contents
263 #
264
265 sub cleargel {
266 my ($title) = @_;
267 my $i;
268
269 foreach $i (0..$rows) {
270 $clones[$i] = $enzymes[$i] = "";
271 }
272 $geltitle->configure(-text => "Gel: $title");
273 $changed = 0;
274 }
275
276 # Activate the DragDrop source by redefining -startcommand to something
277 # useful and which returns 0 to indicate that the StartDrag process hasn't
278 # been handled and should be managed by the object's own code.
279 # Make all labels in active table rows into dropsites, colouring them blue.
280 #
281
282 sub NewGel {
283 my $i;
284
285 cleargel("Untitled");
286 print "$method\n";
287 if ($method eq 'variable') {
288 $source->configure(-startcommand => [\&DragSetup, $lb]);
289 $source->configure(-image => undef);
290 } elsif ($method eq 'text') {
291 $source->configure(-text => "Dragging");
292 $source->configure(-image => undef);
293 $source->configure(-startcommand => \&DragOK);
294 } else {
295 $source->configure(-image => 'win');
296 $source->configure(-startcommand => \&DragOK);
297 }
298 $lb->configure(-background => $ListClr);
299 foreach $i (1..$rows) {
300 if (($i - 1) % $marker_repeat > 0) {
301 $lbls[$i]->configure(-background => $ButtonClr);
302 $drops[$i] = $lbls[$i]->DropSite(-droptypes => [qw(Local)],
303 -dropcommand => [\&Dropit,$i],
304 -entercommand => [\&SiteEntry, $lbls[$i]],
305 );
306 }
307 }
308 $save_btn->configure(-state => 'normal');
309 Fill_lb();
310 }
311
312 #
313 # Stop the listbox being a DnD source
314 # Kill off any active dropsites and turn everything grey again
315 #
316
317 sub SaveGel {
318 my $gel = "newgel2";
319 my ($statement, $sth, $lane);
320
321 cleargel("<none>");
322 $save_btn->configure(-state => 'disabled');
323 $source->configure(-startcommand => [\&DragInhibit]);
324 $lb->configure(-background => $DfltClr);
325 foreach $lane (1..$rows) {
326 if (($lane - 1) % $marker_repeat > 0) {
327 $drops[$lane]->delete;
328 $lbls[$lane]->configure(-bg => $DfltClr);
329 }
330 }
331 Fill_lb();
332 }
333
334 #
335 # Disable Dragging by pretending that we've handled the initialisation
336 # of the drag window ourselves
337 #
338
339 sub DragInhibit {
340 return 1;
341 }
342
343 #
344 # Enable dragging by asking the DragDrop object to initialise it for us
345 #
346
347 sub DragOK {
348 return 0;
349 }
350
351 #
352 # Make the drag window show the text of what's being transferred
353 #
354 # Caution advised here in case we turn out to be dragging 2K of text!
355 #
356 # Returns 0 to indicate that the drag initialisation still needs to be
357 # handled by the source object.
358
359 sub DragSetup {
360 my ($lb) = @_;
361
362 my ($lb_sel) = $lb->curselection;
363 my ($row) = $lb->get($lb_sel);
364 my ($clone, $enzyme) = split " ", $row;
365 my $text = sprintf "%s/%s", $clone, $enzyme;
366 $source->configure(-text => $text);
367
368 return 0;
369 }
370
371
372 #
373 # Clean up and quit
374 #
375
376 sub Done {
377 exit 0;
378 }
379
380 END {
381
382 }
383
384 __END__
385
386 =head1 NAME
387
388
389 DragDrop - create and manipulate widgets whose selections can be dragged and dropped
390
391 DropSite - create and manipulate DropSites for Dragged selections
392
393 =head1 SYNOPSIS
394
395 use Tk::DragDrop;
396 use Tk::DropSite;
397
398 $source = $widget->DragDrop(-event => '<Event>',
399 -sitetypes => ['Local'],
400 -handlers => [[\&callback],
401 [-type => 'TYPENAME',
402 \&callback]],
403 );
404
405 $drop = $widget->DropSite(-droptypes => ['Local'],
406 -dropcommand => [\&Dropit,?params?],
407 );
408
409 =head1 DESCRIPTION
410
411 B<NB> This is unofficial documentation, believed to be correct but neither
412 complete not definitive. B<Caveat programmer!>
413
414 B<DragDrop> implements drag-and-drop for Tk apps. It should work on
415 any platform for local (within one application) droptypes, but only the Sun
416 interface is defined for global (interapplication) droptypes.
417
418 User can drag objects with the mouse (Button-1 by default) from
419 DragDrop sources, and drop them on DropSites. By default, pressing any key
420 during the Drag operation will abort it. There is support for different
421 types of information transfer (eg STRING, ATOM, INTEGER - see Tk::Selection
422 and/or the X Inter-Client Communication Conventions Manual (ICCCM)
423 for details) and also user-defined types (eg FILE_NAME in the example
424 code below) via user-defined handlers.
425
426 Local (intra-application) transfer is via the X Selection mechanism. Global
427 (interapplication) transfer uses the Sun protocols defined in the
428 Tk::DragDrop::Sunconstant module.
429
430 =head2 Example Code
431
432
433 use Tk;
434 use Tk::DragDrop;
435 use Tk::DropSite;
436
437 @data = ('One','Two','Three', 'Four');
438
439 $w = MainWindow->new();
440 $lb = $w->Listbox->pack;
441 $lb->insert('end', @data);
442 $lab = $w->Label(-text => "Drop here!")->pack;
443
444 $source = $lb->DragDrop(-event => '<B1-Motion>',
445 -sitetypes => [qw(Local)],
446 -handlers => [[\&send_string],
447 [-type => 'FILE_NAME',
448 \&send_file]]);
449 $drop = $lab->DropSite(-droptypes => [qw(Local)],
450 -dropcommand => [\&Dropit, $lb],
451 -entercommand => [\&SiteEntry, $lab],
452 );
453 MainLoop();
454
455 sub send_file {
456 my ($offset,$max) = @_;
457
458 return __FILE__;
459 }
460
461 sub send_string {
462 my ($offset,$max) = @_;
463
464 my ($lb_sel) = $lb->curselection;
465 my ($req) = $lb->get($lb_sel);
466 return $req;
467 }
468
469 sub Dropit {
470 my ($lb,$seln) = @_;
471
472 my ($req) = $lb->SelectionGet('-selection'=>$seln,'STRING');
473 print "$req\n";
474 }
475
476 sub SiteEntry {
477 my ($w, $entry, @data) = @_;
478 $w->configure(-relief => $entry == 1 ? 'raised' : 'sunken');
479 }
480
481
482 =head1 CONFIGURATION
483
484 The non-standard options recognised by B<DragDrop> are as follows:-
485
486 =over 4
487
488 =item B<-event>
489
490 The event which will initiate dragging.
491
492 =item B<-sitetypes>
493
494 Whether the applications served by this source will be local ('local'),
495 global ('Sun') or both (I<undef>).
496
497 =item B<-handlers>
498
499 Handler routines for each supported type of data to be
500 transferred
501
502 =item B<-image>
503
504 Optionally a (bitmap) image to display instead of text when an
505 item is being dragged.
506
507 =item B<-startcommand>
508
509 Optionally a callback invoked before dragging is initiated. Subroutine
510 (which is called without any parameters) must return 0 if Drag is to be
511 allowed, otherwise the event will be ignored. Defaults to I<undef>.
512
513 =item B<-predropcommand>
514
515 Optionally a callback invoked before dropping occurs. Subroutine
516 (which will be called with the source application ID string and the
517 dropsite widget as parameters) must return 0 if Drop is allowed,
518 otherwise no handler is called. Defaults to I<undef>.
519
520 =item B<-postdropcommand>
521
522 Optionally a callback invoked after dropping occurs. Subroutine will
523 be called with the source application ID string and need not return
524 any specific value. Defaults to I<undef>.
525
526 =item B<-cursor>
527
528 Optionally the cursor to use whilst dragging. Defaults to 'hand2'
529
530 =item B<-text>
531
532 Optionally some fixed text to display in the drag window.
533 Defaults to the classname of the parent widget.
534
535 =back
536
537 The non-standard options recognised by B<DropSite> are as follows:-
538
539 =over 4
540
541 =item B<-droptypes>
542
543 Whether items will be dropped from local ('local'), global ('Sun')
544 or both (I<undef>) applications
545
546 =item B<-dropcommand>
547
548 Callback subroutine to invoke when something is dropped here
549
550 =item B<-entercommand>
551
552 Optionally, callback routine to invoke whenever dragged object
553 enters or leaves the DropSite. Defaults to I<undef>.
554
555 =back
556
557 =head1 METHODS
558
559 B<DragDrop> supports the following methods:-
560
561
562
563
564 B<DropSite> supports the following methods:-
565
566 =over 4
567
568 =item B<-delete>
569
570 Remove this object from the list of registered DropSites
571
572 =back
573
574 =head1 DEFAULT BINDINGS
575
576 =over 4
577
578 When the window representing the dragged object is mapped,
579 the Mapped method is called (class binding).
580
581 Any button motion invokes the Drag method (class binding).
582
583 Any button release invokes the Drop method (class binding).
584
585 Any keypress whilst dragging invokes the Done method,
586 aborting the drag (class binding).
587
588 Button 1 invokes the StartDrag method (instance binding).
589
590 =back
591
592 =head1 BUGS
593
594 There is currently no way a DragDrop source can remove an event
595 binding once it has been installed (however this can be done
596 manually by removing the binding from the parent widget).
597
598 Destroying a DragDrop source doesn't remove the binding from the
599 parent widget, causing Tk to complain if the bound callback is
600 later invoked.
601
602 DropSites can't be within a scrolling Table (this is a Table bug,
603 not a DragDrop one).
604
605 =head1 AUTHORS
606
607 B<Nick Ing-Simmons> [email protected] : Original module code
608
609 B<John Attwood> [email protected] : This demo and draft pod docs
610
611
612 =cut
613
614
615
616
617