home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Tk / Internals.pod < prev    next >
Encoding:
Text File  |  1997-08-10  |  5.6 KB  |  140 lines

  1. =head1 NAME
  2.  
  3. CallingTk -  what is Perl Tk interface doing when you call Tk functions.
  4.  
  5. This information is worse than useless for C<perlTk> users, but can of
  6. some help for people interested in using modified Tk source with
  7. C<perlTk>.
  8.  
  9. I<This document is under construction. The information is believed to
  10. be pertinent to the version of> C<portableTk> I<available when it was
  11. created. All the details are subject to change.>
  12.  
  13. =head1 DESCRIPTION
  14.  
  15. =over 5
  16.  
  17. =item PreCompiling
  18.  
  19. Before the actual compilation stage a script scans the source
  20. and extracts the subcommands of different commands. This information
  21. resides in the file C<pTk/Methods.def>.
  22.  
  23. =item Compilation
  24.  
  25. During compilation the above file is included in the source of booting
  26. routine of dynamic (or static) library. More precisely, the booting
  27. code of module C<Tk> calls the subroutine Boot_Glue() from the module
  28. C<tkGlue.c>, and this subroutine includes the file (with appropriate
  29. macro definitions).
  30.  
  31. =item Inside C<use Tk;>
  32.  
  33. The module bootstraps the C code, then loads the Perl libraries. The
  34. heart of the Perl code is contained in the C<Tk::Widget> library, all the
  35. widgets inherit from this module. Code for toplevels is loaded from
  36. C<Tk::MainWindow>. 
  37.  
  38. During bootstrap of the C glue code the C<Xevent::?> codes and a
  39. handful of C<Tk::Widget> and C<Tk::Image> routines are defined. (Much
  40. more XSUBs are created from C<Tk.xs> code.) The widget subcommands are
  41. glued to Perl basing on the list included from C<pTk/Methods.def>. In
  42. fact all the subcommands are glued to XSUBs that are related to the
  43. same C subroutine XStoWidget(), but have different data parts.
  44.  
  45. During the Perl code bootstrap the method C<Tk::Widget::import> is
  46. called. This call requires all the code from particular widget
  47. packages.
  48.  
  49. Code from the widget packages calls an obscure command like
  50.  
  51.   (bless \"Text")->WidgetClass;
  52.  
  53. This command (actually Tk::Widget::WidgetClass()) creates three
  54. routines: Tk::Widget::Text(), Tk::Widget::isText(), and
  55. Tk::Text::isText(). The first one is basically C<new> of C<Tk::Text>,
  56. the other two return constants. It also puts the class into
  57. depository.
  58.  
  59. =item Inside C<$top = MainWindow-E<gt>new;>
  60.  
  61. This is quite intuitive. This call goes direct to
  62. C<Tk::MainWindow::new>, that calls XSUB
  63. C<Tk::MainWindow::CreateMainWindow>, that calls C subroutine
  64. Tk_CreateMainWindow(). It is a C<Tk> subroutine, so here black magic
  65. ends (almost).
  66.  
  67. The only remaining black magic is that the C<Tk> initialization
  68. routine creates a lot of commands, but the subroutine for creation is
  69. usurped by B<portableTk> and the commands are created in the package
  70. C<Tk>. They are associated to XSUBs that are related to one of three C
  71. subroutines XStoSubCmd(), XStoBind(), or XStoTk(), but have different
  72. data parts.
  73.  
  74. The result of the call is blessed into C<Tk::MainWindow>, as it should.
  75.  
  76.  
  77. =item Inside C<$top-E<gt>title('Text demo');>
  78.  
  79. The package C<Tk::Toplevel> defines a lot of subroutines on the fly on
  80. some list. All the commands from the list are converted to the
  81. corresponding subcommands of C<wm> method of the widget. Here
  82. subcommand is a command with some particular second argument (in this
  83. case C<"title">). Recall that the first argument is $self.
  84.  
  85. Now C<Tk::Toplevel> @ISA C<Tk::Widget>, that in turn @ISA C<Tk>. So a
  86. call to C<$top-E<gt>wm('title','Text demo')> calls C<Tk::wm>, that is
  87. defined during call to Tk_CreateMainWindow(). As it is described
  88. above, the XSUB associated to XStoSubCmd() is called.
  89.  
  90. This C routine is defined in C<tkGlue.c>. It gets the data part of
  91. XSUB, creates a C<SV> with the name of the command, and calls
  92. Call_Tk() with the XSUB data as the first argument, and with the name
  93. of XSUB stuffed into the Perl stack in the place there C<tk> expects
  94. it. (In fact it can also reorder the arguments if it thinks it is
  95. what you want).
  96.  
  97. The latter procedure extracts name of C<tk> procedure and
  98. C<clientData> from the first argument and makes a call, using Perl
  99. stack as C<argv> for the procedure. A lot of black magic is performed
  100. afterwards to convert result of the procedure to a Perl array return.
  101.  
  102. =item Inside C<$text = $top-E<gt>Text(background =E<gt> $txtBg);>
  103.  
  104. Above we discussed how the command C<Tk::Widget::Text> is created. The
  105. above command calls it via inheritance. It is translated to 
  106.  
  107.   Tk::Text::new($top, background => $txtBg);
  108.  
  109. The package C<Tk::Text> has no method C<new>, so the
  110. C<Tk::Widget::new> is called. In turn it calls
  111. C<Tk::Text-E<gt>DoInit($top)>, that is
  112. C<Tk::Widget::DoInit(Tk::Text,$top)>, that initializes the bindings if
  113. necessary. Then it creates the name for the widget of the form
  114. C<.text0>, and calls C<Tk::text('.text0', background =E<gt> $txtBg)>
  115. (note lowercase). The result of the call is blessed into C<Tk::Text>,
  116. and the method C<bindtags> for this object is called. 
  117.  
  118. Now the only thing to discuss is who defines the methods C<text> and
  119. C<bindtags>. The answer is that they are defined in C<tkWindow.c>,
  120. and these commands are created in the package C<Tk> in the same sweep
  121. that created the command C<Tk::wm> discussed above.
  122.  
  123. So the the same C code that corresponds to the processing of
  124. corresponding TCL commands is called here as well (this time via
  125. C<XStoTk> interface).
  126.  
  127. =item Inside C<$text-E<gt>insert('insert','Hello, world!');>
  128.  
  129. As we discussed above, the subcommands of widget procedures correspond
  130. to XSUB C<XStoWidget>. This XSUB substitutes the first argument $text
  131. (that is a hash reference) to an appropriate value from this hash,
  132. adds the additional argument after the first one that contains the
  133. name of the subcommand extracted from the data part of XSUB, and calls
  134. the corresponding Tk C subroutine via C<Call_Tk>.
  135.  
  136. =back
  137.  
  138. Ilya Zakharevich <ilya@math.ohio-state.edu> 
  139.  
  140.