| 1 |
=head1 NAME |
| 2 |
|
| •3 |
Tcl_vs_perl - very old suspect documentation on porting. |
| 4 |
|
| 5 |
=for category Other Modules and Languages |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This isn't really a .pod yet, nor is it Tcl vs perl |
| 10 |
it is a copy of John's comparison of Malcolm's original perl/Tk |
| 11 |
port with the current one. It is also out-of-date in places. |
| 12 |
|
| 13 |
From: [email protected] (John Stoffel ) |
| 14 |
|
| 15 |
Here are some thoughts on the new Tk extension and how I think the |
| 16 |
organization of the commands looks. Mostly, I'm happy with it, it |
| 17 |
makes some things more organized and more consistent with tcl/tk, but |
| 18 |
since the overlying language is so different, I don't think we need to |
| 19 |
follow exactly the tcl/tk model for how to call the language. |
| 20 |
|
| 21 |
The basic structure of the Tk program is: |
| 22 |
|
| 23 |
require Tk; |
| 24 |
|
| 25 |
$top = MainWindow->new(); |
| 26 |
|
| 27 |
# |
| 28 |
# create widgets |
| 29 |
# |
| 30 |
|
| 31 |
Tk::MainLoop; |
| 32 |
|
| 33 |
sub method1 { |
| 34 |
} |
| 35 |
|
| 36 |
sub methodN { |
| 37 |
} |
| 38 |
|
| 39 |
This is pretty much the same as tkperl5a5, with some cosmetic naming |
| 40 |
changes, and some more useful command name and usage changes. A quick |
| 41 |
comparison in no particular order follows: |
| 42 |
|
| 43 |
tkperl5a5 Tk |
| 44 |
------------------------------- ----------------------------------- |
| 45 |
$top=tkinit(name,display,sync); $top=MainWindow->new(); |
| 46 |
|
| 47 |
tkpack $w, ... ; $w->pack(...) |
| 48 |
|
| 49 |
$w = Class::new($top, ...); $w = $top->Class(...); |
| 50 |
|
| 51 |
tkmainloop; Tk::MainLoop; |
| 52 |
|
| 53 |
tkbind($w,"<key>",sub); $w->bind("<key>",sub); |
| 54 |
|
| 55 |
tkdelete($w, ...); $w->delete(...); |
| 56 |
|
| 57 |
$w->scanmark(...); $w->scan("mark", ...); |
| 58 |
|
| 59 |
$w->scandragto(...); $w->scan("dragto", ...); |
| 60 |
|
| 61 |
$w->tkselect(); $w->Select(); |
| 62 |
|
| 63 |
$w->selectadjust(...); $w->selection("adjust", ...); |
| 64 |
|
| 65 |
$w->selectto(...); $w->selection("to", ...); |
| 66 |
|
| 67 |
$w->selectfrom(...); $w->selection("from", ...); |
| 68 |
|
| 69 |
$w->tkindex(...); $w->index(...); |
| 70 |
|
| 71 |
tclcmd("xxx",...); &Tk::xxx(...) # all Tk commands, but no Tcl at all |
| 72 |
|
| 73 |
tclcmd("winfo", xxx, $w, ...); $w->xxx(...); |
| 74 |
|
| 75 |
$w->mark(...); |
| 76 |
|
| 77 |
$w->tag(...); |
| 78 |
|
| 79 |
$w->grabstatus(); $w->grab("status"); |
| 80 |
|
| 81 |
$w->grabrelease(...); $w->grab("release", ...); |
| 82 |
|
| 83 |
focus($w); $w->focus; |
| 84 |
|
| 85 |
update(); Tk->update(); |
| 86 |
|
| 87 |
idletasks(); Tk->update("idletasks"); |
| 88 |
|
| 89 |
wm("cmd",$w, ...); $w->cmd(...); |
| 90 |
|
| 91 |
destroy($w); $w->destroy(); |
| 92 |
|
| 93 |
Tk::option(...); |
| 94 |
$w->OptionGet(name,Class) |
| 95 |
|
| 96 |
$w->place(...) |
| 97 |
|
| 98 |
Tk::property(...); |
| 99 |
|
| 100 |
$w = Entry::new($parent,...) |
| 101 |
|
| 102 |
is now |
| 103 |
|
| 104 |
$w = $parent->Entry(...) |
| 105 |
|
| 106 |
As this allows new to be inherited from a Window class. |
| 107 |
|
| 108 |
-method=>x,-slave=>y |
| 109 |
|
| 110 |
is now |
| 111 |
|
| 112 |
-command => [x,y] |
| 113 |
|
| 114 |
1st element of list is treated as "method" if y is an object reference. |
| 115 |
(You can have -command => [a,b,c,d,e] too; b..e get passed as args). |
| 116 |
|
| 117 |
Object references are now hashes rather than scalars and there |
| 118 |
is only ever one such per window. The Tcl_CmdInfo and PathName |
| 119 |
are entries in the hash. |
| 120 |
|
| 121 |
(This allows derived classes to |
| 122 |
re-bless the hash and keep their on stuff in it too.) |
| 123 |
|
| 124 |
Tk's "Tcl_Interp" is in fact a ref to "." window. |
| 125 |
You can find all the Tk windows descended from it as their object |
| 126 |
references get added (by PathName) into this hash. |
| 127 |
$w->MainWindow returns this hash from any window. |
| 128 |
|
| 129 |
I think that it should extend to multiple tkinits / Tk->news |
| 130 |
with different Display's - if Tk code does. |
| 131 |
|
| 132 |
Finally "bind" passes window as "extra" (or only) |
| 133 |
argument. Thus |
| 134 |
|
| 135 |
Tk::Button->bind(<Any-Enter>,"Enter"); |
| 136 |
|
| 137 |
Binds Enter events to Tk::Button::Enter by default |
| 138 |
but gets called as $w->Enter so derived class of Button can just |
| 139 |
define its own Enter method. &EvWref and associated globals and race |
| 140 |
conditions are no longer needed. |
| 141 |
|
| 142 |
One thing to beware of : commands bound to events with $widget->bind |
| 143 |
follow same pattern, but get passed extra args : |
| 144 |
|
| 145 |
$widget->bind(<Any-1>,[sub {print shift}, $one, $two ]); |
| 146 |
|
| 147 |
When sub gets called it has : |
| 148 |
|
| 149 |
$widget $one $two |
| 150 |
|
| 151 |
passed. |
| 152 |
|
| 153 |
1st extra arg is reference to the per-widget hash that serves as the |
| 154 |
perl object for the widget. |
| 155 |
|
| 156 |
Every time an XEvent a reference to a special class is placed |
| 157 |
in the widget hash. It can be retrieved by $w->XEvent method. |
| 158 |
|
| 159 |
The methods of the XEvent class are the |
| 160 |
Tcl/Tk % special characters. |
| 161 |
|
| 162 |
Thus: |
| 163 |
|
| 164 |
$widget->bind(<Any-KeyPress>, |
| 165 |
sub { |
| 166 |
my $w = shift; |
| 167 |
my $e = $w->XEvent; |
| 168 |
print $w->PathName," ",$e->A," pressed ,$e->xy,"\n"); |
| 169 |
}); |
| 170 |
|
| 171 |
XEvent->xy is a special case which returns "@" . $e->x . "," . $e->y |
| 172 |
which is common in Text package. |
| 173 |
|
| 174 |
Because of passing a blessed widget hash to "bound" subs they can be |
| 175 |
bound to (possibly inherited) methods of the widget's class: |
| 176 |
|
| 177 |
Class->bind(<Any-Down>,Down); |
| 178 |
|
| 179 |
sub Class::Down |
| 180 |
{ |
| 181 |
my $w = shift; |
| 182 |
# handle down arrow |
| 183 |
} |
| 184 |
|
| 185 |
Also: |
| 186 |
|
| 187 |
-command and friends can take a list the 1st element can be a ref to |
| 188 |
as sub or a method name. Remaining elements are passed as args to the |
| 189 |
sub at "invoke" time. Thus : |
| 190 |
|
| 191 |
$b= $w->Button(blah blah, '-command' => [sub{print shift} , $fred ]); |
| 192 |
|
| 193 |
Should do the trick, provided $fred is defined at time of button creation. |
| 194 |
|
| 195 |
Thus 1st element of list is equivalent to Malcolm's -method and second |
| 196 |
would be his -slave. Any further elements are a bonus and avoid |
| 197 |
having to pass ref to an array/hash as a slave. |
| 198 |
|
| 199 |
=cut |
| 200 |
|