-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIDElayout.pm
1652 lines (1171 loc) · 52.7 KB
/
IDElayout.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=head1 NAME
Tk::IDElayout - Tk Widget for Layout of Frames Similar to an IDE.
=head1 SYNOPSIS
B<Simple Example>: (See t/simpleIDElayout2.t in the source distribution for complete example)
#### This example creates two IDEtabFrames for managing with IDElayout #####
my $TOP = MainWindow->new;
### Create layout structures ###
### This structure has the PaneWindow (P1) at the top level,
### and the two IDEtabFrames next lower level.
### Graphically, this looks like this:
### +------+ +------+
### | P1 | ==> | Tab1 |
### +------+ +------+
### H
### H
### v
### +------+
### | Tab2 |
### +------+
my @nodes = (
{ name => 'P1',
dir => 'V',
childOrder => ['Tab1', 'Tab2'],
type => 'panedWindow',
},
{ name => "Tab1", type => 'widget'
},
{ name => "Tab2", type => 'widget'
}
);
my @edges = (
[ 'P1', 'Tab1'],
[ 'P1', 'Tab2'],
);
#################### Create Widgets ##################################
# We will use the same default IDEtabFrame config that the IDElayout widget uses
my $IDEtabFrameConfig = Tk::IDElayout->defaultIDEtabFrameConfig();
### TabFrame 1 ###
my $dtf = $TOP->IDEtabFrame( @$IDEtabFrameConfig);
$dtf->configure(-height => 400);
### TabFrame 2 ###
my $dtf2 = $TOP->IDEtabFrame( @$IDEtabFrameConfig);
######### Populate widgets hash with the two TabFrames created ######
my %widgets = ( 'Tab1' => $dtf,
'Tab2' => $dtf2,
);
# Create simple menubar
my $MenuBar = $TOP->Frame(-class => 'Menubar'); # We don't pack this, it will be packed by IDElayout
# Create simple statusLine
my $statusText = "This is statusLine Text";
my $statusLine = $TOP->Label(-textvariable => \$statusText, -anchor => 'w');
# Structure Created, now buld the Tk::IDElayout
my $layout = $TOP->IDElayout(
-widgets => \%widgets,
-frameStructure => { nodes => \@nodes, edges => \@edges},
-menu => $MenuBar,
-statusLine => $statusLine,
);
$layout->pack(-side => 'top', -fill => 'both', -expand => 'yes');
=head1 DESCRIPTION
This is a widget for managing the layout of Tk frames (and other widgets) like an IDE (Integrated Development Environment)
like I<Eclipse> or I<Microsoft Visual Studio>.
B<Features:>
See I<Screenshots.pdf> in the source distribution for some screenshots demonstrating some of these features.
=over 1
=item *
Layout and management of subwidgets/frames similar to an IDE.
=item *
Resizable panes. Separate frames/widgets in the top-level window can be resized by dragging
the separator border between the frames.
=item *
Support for Tabbed-Frames (using of subclass of L<Tk::DynaTabFrame>, where each tab can be dragged/dropped;
to another tabframe in the IDE, or to a new separate window, or to "edge" ares in the mainwindow to create new tabframes.
=back
=head1 OPTIONS
=over 1
=item frameStructure
Hash ref representing the structure and layout of the frames managed by this widget. See L<FrameStructure Description>
below for details on this structure.
This option, or I<frameGraph> below is required to be supplied to the widget when it is created.
=item frameGraph
L<Graph> object representing the frame structure, built from the above frameStructure.
This is used internally by the object for manipulation of the frame structures. Normally this is built
from the I<frameStructure> data above, but can be directly supplied by setting this option.
This option, or I<frameStructure> above is required to be supplied to the widget when it is created.
=item widgets
Hashref (key-ed by name) of subwidgets managed by this widget.
=item menu
Required menu widget that will displayed at the top of the top-level window.
=item toolbar
Optional L<Tk::ToolBar> widget to display below the I<menu> above.
=item statusLine
Optional status-line frame to display at the bottom of the top-level window. A status-line is typically used
to display short top-level status items to the user.
=item IDEtabFrameConfig
Optional array ref of L<Tk::IDEtabFrame> options to be used when creating new L<Tk::IDEtabFrame> widgets.
New L<Tk::IDEtabFrame> Widgets are created when something is dragged/dropped to an
interior/exterior edge of the L<Tk::IDElayout> Widget. This array ref will be used to create the
new L<Tk::IDEtabFrame> widgets.
If not supplied, this defaults to:
[
-tabclose => 1,
-tabcolor => 'white',
-raisecolor => 'grey90',
-tabpady => 1,
-tabpadx => 1,
-padx => 0,
-pady => 0,
-bg => 'white',
-raisedfg => 'black',
-raisedCloseButtonfg => 'black',
-raisedCloseButtonbg => 'lightgrey',
-raisedCloseButtonActivefg => 'red',
-noraisedfg => 'grey60',
-noraisedActivefg => 'black',
-noraisedCloseButtonfg => 'lightgrey',
-noraisedCloseButtonbg => 'white',
-noraisedCloseButtonActivefg => 'red',
]
=item IDEpanedwindowConfig
Optional array ref of L<Tk::IDEpanedwindow> options to be used when creating new L<Tk::IDEpanedwindow> widgets.
New L<Tk::IDEpanedwindow> Widgets are created when something is dragged/dropped to an
interior/exterior edge of the L<Tk::IDElayout> Widget that is not compatible with the existing panedwidnow direction
(For example when a widget is dragged/dropped to the top or bottom of a horizontal panewindow). This array ref will be used to create the
new L<Tk::IDEpanedwindow> widgets.
If not supplied, this defaults to:
[ -sashpad => 1,
-sashwidth => 6,
-sashrelief=> 'ridge'
]
=item ResizeOnReconfig
Flag = 1 if the mainwindow should be resized when the IDElayout structure is reconfigured (i.e. widget
added or deleted to the structure, triggerd be a call to the I<addWidgetAtSide> or I<deleteWidget>
method. )
If the flag is 1, the top level window will be resized to the requested width/height (obtained
thru the I<reqwidth> and I<reqheight> methods) of the window. This can be useful in some situations
where the user has resized a window to be large, and then deletes some widgets, resulting in a big window
with not much in it. Setting this flag to 1 in this situation will cause the window to shrink to accomodate
the requested size of the widgets that are left in the window.
Defaults to 0.
=back
=head1 FrameStructure Description
The structure of the frames managed by this widget is defined by the I<frameStructure> option. This is a hash ref
with two entries, I<nodes> and I<edges>, which together describe a L<Graph::Directed> structure.
=head2 Node Entry
The node entry is an array ref containing descriptions for each node/frame managed by the widget. Each node entry is a
hash ref with the following entries:
=over 1
=item type
Node Type. Valid entries are I<panedWindow> or I<widget>.
I<panedWindow> nodes represent L<Tk::IDEpanedWindow> widgets that manage one or more subwidgets.
I<widget> nodes represent normal Tk widgets that are managed by a L<Tk::IDEpanedWindow> widget.
=item name
Name of the node. If I<type> above is 'widget', this should correspond to a entry in the I<widgets> option.
=item dir
Direction (H/V for Horizontal/Vertical) of the L<Tk::IDEpanedWindow> panes. Only applies for the I<panedWindow> type.
Horizontal/Verical (H/C) indicates the direction the frames of the L<Tk::IDEpanedwindow> widget are oriented.
=item childOrder
Order of the frames managed by the L<Tk::IDEpanedWindow> widget. Only applies for the I<panedWindow> type.
This is a list of widget names managed by the L<Tk::IDEpanedwindow> widget.
=item expandfactors
List of I<expandFactors> for the frames managed by the L<Tk::IDEpanedwindow> widget. Only applies for the I<panedWindow> type.
I<Expandfactors> determine how the individual widget frames expand or shrink when the entire window is resized.
See the L<Tk::IDEpanedwindow> docs for details.
=back
=head2 Edges Entry
The edges entry represents the edges (or connections) between the nodes described by the I<nodes> entry. It is
a list of 2-element arrays. Each 2-element array represents a connection between nodes.
=head2 Example Structure
The following is an example of a simple I<frameStructure>.
my $frameStructure = {
nodes = [
{ name => 'P1',
dir => 'H',
childOrder => ['Frame 1', 'P2']
},
{ name => "Frame 1",
},
{ name => 'P2',
dir => 'V',
childOrder => ['Frame 2', 'Frame 3']
},
{ name => 'Frame 2' },
{ name => 'Frame 3' },
],
edges = [
[ 'P1', 'Frame 1'],
[ 'P1', 'P2'],
[ 'P2', 'Frame 2'],
[ 'P2', 'Frame 3'],
]
};
The above will result in the following frame structure (as a L<Graph::Directed> object in the I<frameGraph> option).
+--------+ +--------+ +--------+
| P1 | --> | P2 | --> | Frame2 |
+--------+ +--------+ +--------+
| |
| |
v v
+--------+ +--------+
| Frame1 | | Frame3 |
+--------+ +--------+
The top-level PanedWindow, P1, manages Frame1 and another PanedWindow, P2, in the horizontal direction
The PanedWindow P2 manages Frame2 and Frame3 in the vertical direction.
The actual layout on the screen will look something like this.
+---------------+----------------+
| | Frame2 |
| Frame1 +----------------|
| | Frame3 |
|---------------+----------------+
=head1 Advertised Sub-widgets
=over 1
=item menuFrame
L<Tk::Frame> object that holds the menu bar at the top of the main window.
=item mainPW
L<Tk::IDEanedWindow> object used for main display of widgets.
=item toolbarFrame
L<Tk::Frame> object that holds the toolbar (if used).
=item statusLineFrame
L<Tk::Frame> object that holds the status-line widget.
=back
=head1 ATTRIBUTES
=over 1
=item indicator
L<Tk::Frame> object used to indicate drop target areas. This will appear as a horizontal or vertical Bar on the
sides or top/bottom of a frame to indicate where a drag/drop operation can be dropped.
=item currentFrame
Current frame that the mouse pointer is over during a drag/drop operation.
=item currentSide
Current side of the frame (e.g. left, right, top, bot) that the mouse pointer is over during a drag/drop operation.
=item lastSide
The last side of the frame (e.g. left, right, top, bot) that the mouse pointer was over during a drag/drop operation.
=back
=head1 Methods
=cut
package Tk::IDElayout;
our ($VERSION) = ('0.37_02');
use strict;
use Carp;
use Tk;
use Tk::Font;
use Tk::IDEpanedwindow;
use Graph::Directed; # Used for representation and manipulation of the frame layout structure
use Tk::IDElayoutDropSite;
use base qw/ Tk::Derived Tk::Frame/;
Tk::Widget->Construct("IDElayout");
sub Populate {
my ( $cw, $args ) = @_;
my $frameStructure;
if ( defined( $args->{-frameStructure} ) ) {
$frameStructure = $args->{-frameStructure};
}
my $frameGraph;
if ( defined( $args->{-frameGraph} ) ) {
$frameGraph = $args->{-frameGraph};
}
if( !defined($frameGraph) && !defined($frameStructure) ){
croak("Error: -frameGraph or -frameStructure option not supplied when creating IDElayout widget\n");
}
my $widgets;
if ( defined( $args->{-widgets} ) ) {
$widgets = $args->{-widgets};
$cw->{Configure}{-widgets} = $widgets; # Make sure $widgets is populated before other options
}
else {
croak("Error: -widgets not supplied when creating IDElayout widget\n");
}
# Get the default tabFrame config, for setting if IDEtabFrameConfig not supplied
my $defaultIDEtabFrameConfig = $cw->defaultIDEtabFrameConfig();
# Set the default IDEpanedwindow config. This is not broken out as a separate method,
# because it is not used outside of this routine.
my $defaultIDEpanedwindowConfig =
[ -sashpad => 1,
-sashwidth => 6,
-sashrelief=> 'ridge'
];
# Create Placeholder frames
# We use the grid geometry manager here for better control
# Using the pack geom manager, the status line along the bottom would disappear when
# the frame was shrunk vertically.
my $menuFrame = $cw->Frame()->grid( -column => 0, -row => 0, -sticky => 'ew');
my $toolbarFrame = $cw->Frame()->grid( -column => 0, -row => 1, -sticky => 'ew');
my $mainPW = $cw->Frame()->grid( -column => 0, -row => 2, -sticky => 'nsew');
my $statusLineFrame = $cw->Frame()->grid( -column => 0, -row => 3, -sticky => 'ew');
#
$cw->gridColumnconfigure(0, -weight => 1); # Allow the whole column to expand
$cw->gridRowconfigure(2, -weight => 1); # The mainPW window can expand, anything else doesn't
$cw->SUPER::Populate($args);
$cw->ConfigSpecs(
-frameStructure =>
[ qw/ METHOD frameStructure frameStructure /, $frameStructure ],
-frameGraph => [ qw/ METHOD frameGraph frameGraph /, $frameGraph ],
-widgets => [ qw/ PASSIVE widgets widgets /, $widgets ],
-menu => [ qw/ METHOD menu menu /, undef ],
-toolbar => [ qw/ METHOD toolbar toolbar /, undef ],
-statusLine => [ qw/ METHOD statusLine statusLine /, undef ],
-toolbar => [$frameStructure],
-IDEtabFrameConfig => [ qw/ PASSIVE IDEtabFrameConfig IDEtabFrameConfig /, $defaultIDEtabFrameConfig ],
-IDEpanedwindowConfig => [ qw/ PASSIVE IDEpanedwindowConfig IDEpanedwindowConfig /, $defaultIDEpanedwindowConfig ],
-ResizeOnReconfig => [ qw/ PASSIVE ResizeOnReconfig ResizeOnReconfig /, undef ],
);
# Advertise subwidgets
$cw->Advertise( 'menuFrame' => $menuFrame );
$cw->Advertise( 'mainPW' => $mainPW );
$cw->Advertise( 'toolbarFrame' => $toolbarFrame );
$cw->Advertise( 'statusLineFrame' => $statusLineFrame );
# Drop Indicator:
$cw->{indicator} = $cw->toplevel->Frame(-bg => 'blue', -relief => 'flat');
#### Create Dropsite for entire widget ############
#### This is used for moving widgets around in the enviroment by drag/dropping ###
my $site;
$site = $cw->IDElayoutDropSite
(-droptypes => ['Local'],
-entercommand => sub{
my $flag = shift;
#print "############ In Enter Command $flag\n";
if( $flag == 1){
# Save the current side/frame for use later
$cw->{currentSide} = $site->{currentSide};
$cw->{currentFrame} = $site->{currentFrame};
}
},
-dropcommand => [$cw, 'Drop'],
);
######## These bindings make all windows (including toolwindow) close/open when the main IDE
######## window is closed/opened.
$cw->toplevel->bind('<Unmap>', sub{
my $widget = shift;
return unless($widget->isa("Tk::MainWindow")); # Only response to mainwindow unmaps
my @childs = $widget->children();
#print "Childs = ".join(", ", @childs)."\n";
# Minimize all of our toplevels
foreach my $child(@childs){
eval{ $child->MainWindow::attributes() }; # This will only work for toplevels
unless( $@ ){
#print "Child can attrib\n";
$child->MainWindow::withdraw;
}
}
});
$cw->toplevel->bind('<Map>', sub{
# restore all of our toplevels
my $widget = shift;
return unless($widget->isa("Tk::MainWindow")); # Only response to mainwindow unmaps
my @childs = $widget->children();
foreach my $child(@childs){
eval{ $child->MainWindow::attributes() }; # This will only work for toplevels
unless( $@ ){
$child->MainWindow::deiconify;
}
}});
}
#----------------------------------------------
# Sub called when -frameStructure option changed
#
sub frameStructure{
my ($cw, $frameStructure) = @_;
if(! defined($frameStructure)){ # Handle case where $widget->cget(-frameStructure) is called
return $cw->{Configure}{-frameStructure};
}
unless( ref($frameStructure) eq 'HASH' && defined( $frameStructure->{nodes})){
croak("Error: -frameStructure option supplied doesn't have a 'nodes' key\n");
}
my $nodes = $frameStructure->{nodes};
unless( ref($frameStructure) eq 'HASH' && defined( $frameStructure->{edges})){
croak("Error: -frameStructure option supplied doesn't have a 'edges' key\n");
}
my $edges = $frameStructure->{edges};
my $widgets = $cw->cget(-widgets); # get the list of all widgets
unless( ref($widgets) eq 'HASH' && scalar(keys %$widgets) > 0){
croak("Error: -widgets option is empty. Populate it with some widgets before\nsetting the -frameStructure option\n");
}
# Create the directed graph structure used to store the layout
my $frameGraph = Graph::Directed->new;
# Build Graph Struct
foreach my $node (@$nodes) { # Build Nodes
$frameGraph->add_vertex( $node->{name} );
# Set any attributes
my $attr = {};
foreach my $attrName ( sort keys %$node ) {
next if ( $attrName eq 'name' ); # skip name, we are already using that for the vertex name
$attr->{$attrName} = $node->{$attrName};
}
$frameGraph->set_vertex_attribute($node->{name}, 'attr', $attr);
}
foreach my $edge (@$edges) { # Build Edges
$frameGraph->add_edge(@$edge);
}
# Set the frameGraph option from the Graph::Directed object we just built
$cw->configure(-frameGraph => $frameGraph);
}
#----------------------------------------------
# Sub called when -frameGraph option changed
#
sub frameGraph{
my ($cw, $frameGraph) = @_;
if(! defined($frameGraph)){ # Handle case where $widget->cget(-frameGraph) is called
return $cw->{Configure}{-frameGraph};
}
unless( ref($frameGraph) && $frameGraph->isa('Graph::Directed')){
croak("Error: -frameGraph option supplied isn't a Graph::Directed object\n");
}
my $widgets = $cw->cget(-widgets); # get the list of all widgets
unless( ref($widgets) eq 'HASH' && scalar(keys %$widgets) > 0){
croak("Error: -widgets option is empty. Populate it with some widgets before\nsetting the -frameStructure option\n");
}
# Get the toplevel frame
my $mainPW = $cw->Subwidget('mainPW');
# Get the name of the toplevel node
my ($topPaneWindowName) = $frameGraph->predecessorless_vertices();
# Populate the structure, using the supplied frameGraph
$cw->populateWindow($mainPW, $cw, $frameGraph, $topPaneWindowName, $widgets);
}
##############
## Recursive sub to build/populate a PanedWindow layout
## Based on a structure and already built widgets
sub populateWindow{
my $cw = shift;
my $window = shift;
my $top = shift; # Top level widget
my $frameStruct = shift;
my $name = shift; # node we are populating
my $widgets = shift;
# Create Paned Window
my $attr = $frameStruct->get_vertex_attribute($name, 'attr'); # Get the attribute hash
my $dir = ($attr->{dir} =~ /v/i) ? 'vertical' : 'horiz';
my @kids = $frameStruct->successors($name); # get the childs
my $childOrder = $attr->{childOrder}; # Get the order of the childs
my $expandfactors = $attr->{expandfactors} || [map 0, @$childOrder]; # Resize behavior of the childs, defaults to all zeroes
my $IDEpanedwindowConfig = $cw->cget(-IDEpanedwindowConfig);
my $pw = $top->IDEpanedwindow( -orient => $dir, @$IDEpanedwindowConfig);
$pw->pack(-in => $window, qw/-side top -expand yes -fill both /);
$widgets->{$name} = $pw; # save name
# Pack or create Each Child
my @widgetsToAdd;
my $childIndex = 0;
foreach my $childName(@$childOrder){
my $childAttr = $frameStruct->get_vertex_attribute($childName, 'attr');
if( defined( $childAttr->{childOrder})){ # Another paned window, recurse
my $childPW = $cw->populateWindow($pw, $top, $frameStruct, $childName, $widgets);
push @widgetsToAdd, $childPW;
}
else{ # not a child, just pack the widget
my $widget = $widgets->{$childName};
push @widgetsToAdd, $widget;
# Add callback to any Tk::IDEtabFrame widgets to handle when all tabs in the widget
# are closed
if( $widget->isa("Tk::IDEtabFrame")){
$widget->configure(
-lastTabDeleteCallback =>
[ $cw, 'deleteWidget', $childName]
);
}
# Specific Tk::Widget package called out here, because Tk::DynaTabFrame overrides the raise
# method to something different than what Tk::Widget::raise does.
$widget->Tk::Widget::raise; # Needed for widgets to show up that were created before the panedwindow
}
# Add expandFactors option, if they exist
if( defined (my $expandfactor = $expandfactors->[$childIndex])){
push @widgetsToAdd, -expandfactor => $expandfactor;
}
$childIndex++;
}
$pw->add(@widgetsToAdd);
return $pw;
}
#----------------------------------------------
# Sub called when -menu option changed
#
sub menu{
my ($cw, $menu) = @_;
if(! defined($menu)){ # Handle case where $widget->cget(-frameStructure) is called
return $cw->{Configure}{-menu};
}
my $menuFrame = $cw->Subwidget('menuFrame');
$menu->pack(-in => $menuFrame, -side => 'top', -fill => 'x', -expand => 0);
$menu->raise(); # Raise needed, because menuFrame might have been created after menu, and it would obscure the menu
}
#----------------------------------------------
# Sub called when -statusLine option changed
#
sub statusLine{
my ($cw, $statusLine) = @_;
if(! defined($statusLine)){ # Handle case where $widget->cget(-statusLine) is called
return $cw->{Configure}{-statusLine};
}
my $statusLineFrame = $cw->Subwidget('statusLineFrame');
$statusLine->pack(-in => $statusLineFrame, -side => 'bottom', -expand => 'no', -fill => 'x', -anchor => 'sw');
$statusLine->raise(); # Raise needed, because statusLineFrame might have been created after statusLine, and it would obscure the menu
}
#----------------------------------------------
# Sub called when -toolbar option changed
#
sub toolbar{
my ($cw, $toolbar) = @_;
if(! defined($toolbar)){ # Handle case where $widget->cget(-toolbar) is called
return $cw->{Configure}{-toolbar};
}
my $toolbarFrame = $cw->Subwidget('toolbarFrame');
$toolbar->pack(-in => $toolbarFrame, -side => 'top', -fill => 'x', -expand => 0);
$toolbar->raise(); # Raise needed, because Frame might have been created after widget, and it would obscure the widget
}
############################################################
=head2 findFrame
Given the current x/y position, return
the frame (if any) (and the bounding box coord) that the x/y position is within
B<Usage:>
my ($frameName, $x1,$y1, $x2,$y2) = $self->findFrame($x,$y);
returns an empty list if x/y position is not within any frame
=cut
sub findFrame{
my $self = shift;
my @pxy = (shift, shift);
my $frameList = $self->cget(-widgets);
my $frame; # Current frame we are looking at
my $geometry; # text geometry of the frame
my ( $width, $height); # Current frame width / height
my ( $rootX, $rootY); # root x/y coords of the current frame
my ( $x, $y, $x2, $y2); # Bounding box for the frame
#print "Framelist names = ".join(", ", keys %$frameList)."\n";
my %frameMatches; # hash of matches for frames we are in (i.e. we could be in multiple frames at once)
foreach my $frameName ( sort keys %$frameList ) {
$frame = $frameList->{$frameName};
$geometry = $frame->geometry;
( $width, $height) = $geometry =~ /(\d+)x(\d+)\+\d+\+\d+/;
( $rootX, $rootY ) = ( $frame->rootx, $frame->rooty );
#print "$frameName rootX/Y = $rootX/$rootY\n";
# convert Upper left corner to absolute coords
$x = $rootX;
$y = $rootY;
#print "frame $frameName = $geometry\n";
#print "frame $frameName x/y/w/h = $x $y $width $height\n";
( $x2, $y2 ) = ( ( $x + $width ), ( $y + $height ) );
# Check to see if we are within this frame
if ( $pxy[0] >= $x
&& $pxy[0] <= $x2
&& $pxy[1] >= $y
&& $pxy[1] <= $y2 ) {
#print "pointer is within $frameName\n";
$frameMatches{$frameName} = [$x,$y, $x2, $y2];
}
}
if( %frameMatches){
#print "We are in frames ".join(", ", sort keys %frameMatches)."\n";
# If multiple matches/frames, give priority to sink vertexes. These
# will frames that are inside of other frames. We are inside of the inner
# frames first. (e.g. A Text widget could be inside of a PanedWindow widget.
# We would count ourselves as being inside of the text widget first.)
my @frameMatches = sort keys %frameMatches;
my ($frameMatch) = @frameMatches; # default if no sink vertexes is the first one.
my $frameGraph = $self->cget('-frameGraph');
foreach my $frame(sort keys %frameMatches){
if( $frameGraph->is_sink_vertex($frame)){
$frameMatch = $frame;
last;
}
}
my $matchInfo = $frameMatches{$frameMatch};
#print "FindFrame Returning $frameMatch, ".join(", ",@$matchInfo)."\n";
return ($frameMatch, @$matchInfo);
}
return ();
}
#######################################################################
=head2 findSide
Method to find the side of a frame we are "close" to. Used for dragging, where the edges of
the IDElayout frames are drop targets.
B<Usage:>
my $side = $self->findSide($pointerx,$pointery, $frameName,
$x1, y1, $x2, $y2);
where: $pointerx x coord of the current pointer
$pointery y coord of the current pointer
$frameName Name of frame
$x1/y1/x2/y2 Frame Coords
Returns $side (top/bot/left/right) if we
are close to a side.
=cut
sub findSide{
my $self= shift;
my @pxy = (shift,shift);
my ( $frameName, $x1, $y1, $x2, $y2 ) = @_;
my $frameList = $self->cget(-widgets);
#print "In $frameName \n";
my $side; # side we are flashing, if any
# coords for top/bot left right sides
my @topCoord = ( $x2, $y1, $x1, $y1 );
my @botCoord = ( $x1, $y2, $x2, $y2 );
my @leftCoord = ( $x1, $y1, $x1, $y2 );
my @rightCoord = ( $x2, $y2, $x2, $y1 );
my %coords = ( top => \@topCoord,
bot => \@botCoord,
left => \@leftCoord,
right => \@rightCoord
);
# Find min distance and the side
my ( $minDist, $dist, $minSide );
foreach my $side ( keys %coords ) {
$dist = $self->distanceToPoint( @pxy, @{ $coords{$side} } );
#print "$side Dist $dist\n";
if ( !defined $minDist || $dist < $minDist ) {
$minDist = $dist;
$minSide = $side;
}
}
if( $minDist < 20){ # If we are close to a side, return it, else return undef
return $minSide;
}
else{
return undef;
}
}
########################################################################################
=head2 flashSide
Flash the drop indicator at a side of a frame.
B<Usage:>
$self->flashSide($side, $frame);
where:
$side: Name of side (e.g. top/bot/left/right)
$frame: Name of frame (as it appears in the frameList
=cut
sub flashSide{
my $self = shift;
my $side = shift;
my $frameName = shift;
my $indicator = $self->{indicator};
my $frameList = $self->cget(-widgets);
my $lastSide = $self->{lastSide};
#print "##### Flashing side $side, Frame $frameName\n";
# Flash a heavy line on this side, if close to it
if ( defined($side) && defined($frameName) ) {
$indicator->placeForget if( !defined($lastSide) || $lastSide ne $side);
# figure out where to place indicator
my ( $op, $pp );
if ( $side =~ /top/ ) {
$op = [qw/-height 5/];
$pp = [qw/-relx 0 -relwidth 1 -y 0 -x 0 -rely 0/];
}
elsif ( $side =~ /bot/ ) {
$op = [qw/-height 5/];
$pp = [qw/-relx 0 -relwidth 1 -y -5 -x 0 -rely 1/];
}
elsif ( $side =~ /left/ ) {
$op = [qw/-width 5/];
$pp = [qw/-x 0 -relheight 1 -y 0 -relx 0/];
}
elsif ( $side =~ /right/ ) {
$op = [qw/-width 5/];
$pp = [qw/-x -5 -relx 1 -relheight 1 -y 0/];
}
#print "Indicator configuring " . join( ", ", @$pp ) . "\n";
#print "Indicator is a $indicator\n";
#print "Indicator exists\n" if $indicator->Exists();
#print "Framename = $frameName " . ( $frameList->{$frameName} ) . "\n";
$indicator->configure(@$op);
$indicator->place( -in => $frameList->{$frameName}, @$pp );
$indicator->raise;
$self->{lastSide} = $side;
}
else {
$indicator->placeForget if( defined($lastSide) ); # Only placeforget after we have been placed
}
}
#############################
# Sub to find the shortest from a point (x0,y0) to a line (defined by x1,y1 x2,y2).
# See reference http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
sub distanceToPoint{
my $self = shift;
my ($x0,$y0, $x1,$y1, $x2, $y2) = @_;
my $denom = sqrt( ($x2-$x1)**2 + ($y2-$y1)**2);
my $distance = (($x2-$x1)*($y1-$y0) - ($x1-$x0)*($y2-$y1)) / $denom;
return $distance;
}
###############################################################################
=head2 addWidgetAtSide
Method to add a widget to the Panedwindow arrangement at the frame side.
B<Usage:>
$self->addWidgetAtSide($widget, $widgetName, $currentFrame, $currentSide, $attr);
where:
$widget: Widget to add.
$widgetName: Name of the widget to add
$currentFrame: Name of the current frame we are adding to
$currentSide: Name of the side of the frame we are adding to (e.g. top/bot/left/right)
$attr: Optional hash ref of options for the widget to add
(Only -expandfactor supported at this time)
=cut
sub addWidgetAtSide{
my $self = shift;
my $widget = shift;
my $widgetName = shift;
my $currentFrame = shift;
my $currentSide = shift;
my $attr = shift || {};
my $widgets = $self->cget('-widgets');
my $frameStruct = $self->cget('-frameGraph');
# Add callback to any Tk::IDEtabFrame widgets to handle when all tabs in the widget
# are closed
if( $widget->isa("Tk::IDEtabFrame")){
$widget->configure(