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

  1. =head1 NAME
  2.  
  3. Tk2portableTk - how to make your B<Tk> source portable to other
  4. interpreted languages.
  5.  
  6. =head1 Author
  7.  
  8. Ilya Zakharevich <ilya@math.ohio-state.edu>  has contributed most of 
  9. this document. Many thanks.
  10.  
  11.  
  12. =head1 DESCRIPTION
  13.  
  14. B<PortableTk> is an attempt to make B<Tk> useful from other
  15. languages. Currently tk4.0 runs under Perl using this
  16. approach. Below, I<Lang> is the notation for an external language to
  17. which B<PortableTk> glues B<Tk> code.
  18.  
  19. The main problem with using the code developed for B<TCL> with
  20. different languages is the absence of data types: almost anything is
  21. C<char*>. It makes automatic translation hopeless. However, if you
  22. C<typedef> several new symbols to be C<char*>, you can still use your
  23. code in B<TCL>, I<and> it will make the automatic translation
  24. possible.
  25.  
  26. Another problem with the approach that "everything is a string" is
  27. impossibility to have a result that says "NotApplicable" without
  28. setting an error. Thus different B<Tk> command return different string
  29. values that mean "error happened", like C<"">, C<" "> or
  30. C<"??">. Other languages can be more flexible, so in B<portableTk> you
  31. should inform the compiler that what you want to return means "error"
  32. (see L<Setting variables>).
  33.  
  34. Currently B<PortableTk> uses several different approachs
  35. to simplify translation: several B<TCL> functions that are especially
  36. dangerous to use are undefined, so you can easily find places that
  37. need to be updated to use Language-independent functions based on
  38. compiler warnings.  Eventually a way to use these Language-independent
  39. functions under proper B<TCL> will be also provided.  The end of this 
  40. document provides a starting point for such a project.
  41.  
  42. =head1 Structure of B<pTk>, porting your code
  43.  
  44. B<pTk>, that is a port of B<Tk>, is very special with respect to porting
  45. of other code to B<portableTk>. The problem is that currently there is
  46. very little hope to merge the modifications back into B<Tk>, so a
  47. special strategy is needed to maintain this port. Do not use this
  48. strategy to port your own code.
  49.  
  50. B<pTk> is produced from B<Tk> via a two-step process: first, some
  51. manual editing (the result is in the subdirectory C<mTk>), and second,
  52. automatic conversion by the C<munge> script (written in Perl). Thus the
  53. subdirectory C<pTk/mTk> contains code with minimal possible difference
  54. from the virgin B<Tk> code, so it is easier to merge(1) the
  55. differences between B<Tk> versions into modified code.
  56.  
  57. It looks like the strategy for a portable code should be exactly
  58. opposite: starting from B<TCL>-based code, apply C<munge>, and then
  59. hand-edit the resulting code. Probably it is also possible to target
  60. your code to B<portableTk> from scratch, since this will make it
  61. possible to run it under a lot of I<Lang>uages.
  62.  
  63. The only reason anyone would like to look into contents of C<pTk/mTk>
  64. directory is to find out which constructs are not supported by
  65. C<munge>. On the other hand, C<pTk> directory contains code that is
  66. conformant to B<portableTk>, so you can look there to find example code.
  67.  
  68. C<munge> is the script that converts most common B<Tk> constructs to
  69. their C<portableTk> equivalent. For your code to qualify, you should
  70. follow B<Tk> conventions on indentation and names of variables, in
  71. particular, the array of arguments for the C<...CmdProc> should be
  72. called C<argv>.
  73.  
  74. For details on what C<munge> can do, see 
  75. L<Translation of some TCL functions>. 
  76.  
  77. =head1 B<PortableTk> API
  78.  
  79. =head2 Checking what you are running under
  80.  
  81. B<PortableTk> provides a symbol C<????>. If this symbol is defined,
  82. your source is compiled with it.
  83.  
  84.  
  85. =head2 New types of configuration options
  86.  
  87. B<PortableTk> defines several new types of configuration options:
  88.  
  89.  TK_CONFIG_CALLBACK
  90.  TK_CONFIG_LANGARG
  91.  TK_CONFIG_SCALARVAR
  92.  TK_CONFIG_HASHVAR
  93.  TK_CONFIG_ARRAYVAR
  94.  TK_CONFIG_IMAGE
  95.  
  96. You should use them instead of TK_CONFIG_STRING whenever
  97. appropriate. This allows your application to receive a direct
  98. representation of the corresponding resource instead of the string
  99. representation, if this is possible under given language.
  100.  
  101. ???? It looks like C<TK_CONFIG_IMAGE> and C<TK_CONFIG_SCALARVAR> set
  102. variables of type C<char*>.
  103.  
  104. =head2 Language data
  105.  
  106. The following data types are defined:
  107.  
  108. =over 15
  109.  
  110. =item C<Arg>
  111.  
  112. is the main datatype of the language.  This is a type that your C
  113. function gets pointers to for arguments when the corresponding I<Lang>
  114. function is called.  The corresponding config type is
  115. C<TK_CONFIG_LANGARG>.
  116.  
  117. This is also a type that keeps information about contents of I<Lang>
  118. variable. 
  119.  
  120. =item C<Var>
  121.  
  122. Is a substitute for a C<char *> that contains name of variable. In
  123. I<Lang> it is an object that contains reference to another I<Lang>
  124. variable.
  125.  
  126. =item C<LangResultSave>
  127.  
  128. ????
  129.  
  130. =item C<LangCallback>
  131.  
  132. C<LangCallback*> a substitute for a C<char *> that contains command to
  133. call. The corresponding config type is C<TK_CONFIG_CALLBACK>.
  134.  
  135. =item C<LangFreeProc>
  136.  
  137. It is the type that the C<Lang_SplitList> sets. Before you call it,
  138. declare 
  139.  
  140.     Args *args;
  141.     LangFreeProc *freeProc = NULL;
  142.     ...
  143.     code = Lang_SplitList(interp, value, 
  144.     &argc, &args, &freeProc);
  145.  
  146. After you use the split values, call 
  147.  
  148.     if (args != NULL && freeProc) (*freeProc)(argc,args);
  149.  
  150. It is not guaranteed that the C<args> can survive deletion of C<value>.
  151.  
  152. =back
  153.  
  154. =head2 Conversion
  155.  
  156. The following macros and functions are used for conversion between
  157. strings and the additional types:
  158.  
  159.  LangCallback * LangMakeCallback(Arg)
  160.  Arg LangCallbackArg(LangCallback *)
  161.  char * LangString(Arg)
  162.  
  163. After you use the result of LangCallbackArg(), you should free it with
  164. C<freeProc> C<LANG_DYNAMIC> (it is not guaranteed that any change of
  165. C<Arg> will not be reflected in <LangCallback>, so you cannot do
  166. LangSet...() in between, and you should reset it to C<NULL> if you
  167. want to do any further assignments to this C<Arg>).
  168.  
  169. The following function returns the C<Arg> that is a reference to C<Var>:
  170.  
  171.  Arg LangVarArg(Var)
  172.  
  173. ???? It is very anti-intuitive, I hope the name is changed.
  174.  
  175.  int LangCmpCallback(LangCallback *a,Arg b)
  176.  
  177. (currently only a stub), and, at last,
  178.  
  179.  LangCallback * LangCopyCallback(LangCallback *)
  180.  
  181.  
  182. =head2 Callbacks
  183.  
  184. Above we have seen the new datatype C<LangCallback> and the
  185. corresponding I<Config option>  C<TK_CONFIG_CALLBACK>. The following
  186. functions are provided for manipulation of C<LangCallback>s:
  187.  
  188.  void LangFreeCallback(LangCallback *)
  189.  int LangDoCallback(Tcl_Interp *,LangCallback *,
  190.     int result,int argc, char *format,...)
  191.  
  192. The argument C<format> of C<LangDoCallback> should contain a string that is
  193. suitable for C<sprintf> with optional arguments of C<LangDoCallback>.
  194. C<result> should be false if result of callback is not needed.
  195.  
  196.  int LangMethodCall(Tcl_Interp *,Arg,char *method,
  197.     int result,int argc,...)
  198.  
  199. ????
  200.  
  201. Conceptually, C<LangCallback*> is a substitute for ubiquitous C<char *>
  202. in B<TCL>. So you should use C<LangFreeCallback> instead of C<ckfree>
  203. or C<free> if appropriate.
  204.  
  205.  
  206. =head2 Setting variables
  207.  
  208.  void LangFreeArg (Arg, Tcl_FreeProc *freeProc)
  209.  Arg  LangCopyArg (Arg);
  210.  void Tcl_AppendArg (Tcl_Interp *interp, Arg)
  211.  void LangSetString(Arg *, char *s)
  212.  void LangSetDefault(Arg *, char *s)
  213.  
  214. These two are equivalent unless s is an empty string. In this case
  215. C<LangSetDefault> behaves like C<LangSetString> with C<s==NULL>, i.e.,
  216. it sets the current value of the I<Lang> variable to be false.
  217.  
  218.  void LangSetInt(Arg *,int)
  219.  void LangSetDouble(Arg *,double)
  220.  
  221. The I<Lang> functions separate uninitialized and initialized data
  222. comparing data with C<NULL>. So the declaration for an C<Arg> should
  223. look like
  224.  
  225.  Arg arg = NULL;
  226.  
  227. if you want to use this C<arg> with the above functions. After you are
  228. done, you should use C<LangFreeArg> with C<TCL_DYNAMIC> as C<freeProc>.
  229.  
  230. =head2 Language functions
  231.  
  232. Use
  233.  
  234. =over 25
  235.  
  236.  
  237. =item C<int  LangNull(Arg)>
  238.  
  239. to check that an object is false;
  240.  
  241. =item C<int  LangStringMatch(char *string, Arg match)>
  242.  
  243. ????
  244.  
  245. =item C<void LangExit(int)>
  246.  
  247. to make a proper shutdown;
  248.  
  249. =item C<int LangEval(Tcl_Interp *interp, char *cmd, int global)>
  250.  
  251. to call I<Lang> C<eval>;
  252.  
  253. =item C<void Lang_SetErrorCode(Tcl_Interp *interp,char *code)>
  254.  
  255. =item C<char *Lang_GetErrorCode(Tcl_Interp *interp)>
  256.  
  257. =item C<char *Lang_GetErrorInfo(Tcl_Interp *interp)>
  258.  
  259. =item C<void LangCloseHandler(Tcl_Interp *interp,Arg arg,FILE *f,Lang_FileCloseProc *proc)>
  260.  
  261. currently stubs only;
  262.  
  263. =item C<int LangSaveVar(Tcl_Interp *,Arg arg,Var *varPtr,int type)>
  264.  
  265. to save the structure C<arg> into I<Lang> variable C<*varPtr>;
  266.  
  267. =item C<void LangFreeVar(Var var)>
  268.  
  269. to free the result;
  270.  
  271. =item C<int LangEventCallback(Tcl_Interp *,LangCallback *,XEvent *,KeySym)>
  272.  
  273. ????
  274.  
  275. =item C<int LangEventHook(int flags)>
  276.  
  277. =item C<void LangBadFile(int fd)>
  278.  
  279. =item C<int LangCmpConfig(char *spec, char *arg, size_t length)>
  280.  
  281. unsupported????;
  282.  
  283. =item  C<void Tcl_AppendArg (Tcl_Interp *interp, Arg)>
  284.  
  285. =back
  286.  
  287. Another useful construction is
  288.  
  289.  Arg variable = LangFindVar(interp, Tk_Window tkwin, char *name);
  290.  
  291. After using the above function, you should call
  292.  
  293.  LangFreeVar(Var variable);
  294.  
  295. ???? Note discrepancy in types!
  296.  
  297. If you want to find the value of a variable (of type C<Arg>) given the
  298. variable name, use C<Tcl_GetVar(interp, varName, flags)>. If you are
  299. interested in the string value of this variable, use
  300. C<LangString(Tcl_GetVar(...))>.
  301.  
  302. To get a B<C> array of C<Arg> of length C<n>, use 
  303.  
  304.     Arg *args = LangAllocVec(n);
  305.     ...
  306.     LangFreeVec(n,args);
  307.  
  308. You can set the values of the C<Arg>s using C<LangSet...> functions,
  309. and get string value using C<LangString>.
  310.  
  311. If you want to merge an array of C<Arg>s into one C<Arg> (that will
  312. be an array variable), use
  313.  
  314.     result = Tcl_Merge(listLength, list);
  315.  
  316.  
  317. =head2 Translation of some TCL functions
  318.  
  319. We mark items that can be dealt with by C<munge> by I<Autoconverted>.
  320.  
  321. =over 20
  322.  
  323. =item C<Tcl_AppendResult> 
  324.  
  325. does not take C<(char*)NULL>, but C<NULL> as delimiter. I<Autoconverted>.
  326.  
  327. =item C<Tcl_CreateCommand>, C<Tcl_DeleteCommand> 
  328.  
  329. C<Tk_CreateWidget>, C<Tk_DeleteWidget>, the second argument is the
  330. window itself, not the pathname. I<Autoconverted>.
  331.  
  332. =item C<sprintf(interp-E<gt>result, "%d %d %d %d",...)> 
  333.  
  334. C<Tcl_IntResults(interp,4,0,...)>. I<Autoconverted>.
  335.  
  336. =item C<interp-E<gt>result = "1";>
  337.  
  338. C<Tcl_SetResult(interp,"1", TCL_STATIC)>. I<Autoconverted>.
  339.  
  340. =item Reading C<interp-E<gt>result> 
  341.  
  342. C<Tcl_GetResult(interp)>. I<Autoconverted>.
  343.  
  344. =item C<interp-E<gt>result = Tk_PathName(textPtr-E<gt>tkwin);>
  345.  
  346. C<Tk_WidgetResult(interp,textPtr-E<gt>tkwin)>. I<Autoconverted>.
  347.  
  348. =item Sequence C<Tcl_PrintDouble, Tcl_PrintDouble, ..., Tcl_AppendResult> 
  349.  
  350. Use a single command 
  351.  
  352.  void Tcl_DoubleResults(Tcl_Interp *interp, int append,
  353.     int argc,...);
  354.  
  355. C<append> governs whether it is required to clear the result first.
  356.  
  357. A similar command for C<int> arguments is C<Tcl_IntResults>.
  358.  
  359. =item C<Tcl_SplitList> 
  360.  
  361. Use C<Lang_SplitList> (see the description above).
  362.  
  363. =back
  364.  
  365. =head1 Translation back to TCL
  366.  
  367. To use your B<portableTk> program with B<TCL>, put
  368.  
  369.  #include "ptcl.h"
  370.  
  371. I<before> inclusion of C<tk.h>, and link the resulting code with
  372. C<ptclGlue.c>.
  373.  
  374. These files currently implement the following:
  375.  
  376. =over 5
  377.  
  378. =item Additional config types:
  379.  
  380.  TK_CONFIG_CALLBACK
  381.  TK_CONFIG_LANGARG
  382.  TK_CONFIG_SCALARVAR
  383.  TK_CONFIG_HASHVAR
  384.  TK_CONFIG_ARRAYVAR
  385.  TK_CONFIG_IMAGE
  386.  
  387. =item Types:
  388.  
  389.  Var, Arg, LangCallback, LangFreeProc.
  390.  
  391.  
  392. =item Functions and macros:
  393.  
  394.  Lang_SplitList, LangString, LangSetString, LangSetDefault,
  395.  LangSetInt, LangSetDouble Tcl_ArgResult, LangCallbackArg,
  396.  LangSaveVar, LangFreeVar,
  397.  LangFreeSplitProc, LangFreeArg, Tcl_DoubleResults, Tcl_IntResults,
  398.  LangDoCallback, Tk_WidgetResult, Tcl_CreateCommand,
  399.  Tcl_DeleteCommand, Tcl_GetResult.
  400.  
  401. =back
  402.  
  403. Current implementation contains enough to make it possible to compile
  404. C<mTk/tkText*.[ch]> with the virgin B<Tk>.
  405.  
  406. =head2 New types of events ????
  407.  
  408. PortableTk defines following new types of events:
  409.  
  410.  TK_EVENTTYPE_NONE
  411.  TK_EVENTTYPE_STRING
  412.  TK_EVENTTYPE_NUMBER
  413.  TK_EVENTTYPE_WINDOW
  414.  TK_EVENTTYPE_ATOM
  415.  TK_EVENTTYPE_DISPLAY
  416.  TK_EVENTTYPE_DATA
  417.  
  418. and a function
  419.  
  420.  char *    Tk_EventInfo(int letter, 
  421.         Tk_Window tkwin, XEvent *eventPtr, 
  422.          KeySym keySym, int *numPtr, int *isNum, int *type, 
  423.             int num_size, char *numStorage)
  424.  
  425. =head1 Checking for trouble
  426.  
  427. If you start with working TCL code, you can start convertion using
  428. the above hints. Good indication that you are doing is OK is absence
  429. of C<sprintf> and C<sscanf> in your code (at least in the part that is
  430. working with interpreter).
  431.  
  432. =head1 Additional API
  433.  
  434. What is described here is not included into base B<portableTk>
  435. distribution. Currently it is coded in B<TCL> and as Perl macros (core
  436. is coded as functions, so theoretically you can use the same object
  437. files with different interpreted languages).
  438.  
  439. =head2 C<ListFactory>
  440.  
  441. Dynamic arrays in B<TCL> are used for two different purposes: to
  442. construct strings, and to construct lists. These two usages will have
  443. separate interfaces in other languages (since list is a different type
  444. from a string), so you should use a different interface in your code.
  445.  
  446. The type for construction of dynamic lists is C<ListFactory>. The API
  447. below is a counterpart of the API for construction of dynamic lists
  448. in B<TCL>:
  449.  
  450.  void ListFactoryInit(ListFactory *)
  451.  void ListFactoryFinish(ListFactory *)
  452.  void ListFactoryFree(ListFactory *)
  453.  Arg * ListFactoryArg(ListFactory *)
  454.  void ListFactoryAppend(ListFactory *, Arg *arg)
  455.  void ListFactoryAppendCopy(ListFactory *, Arg *arg)
  456.  ListFactory * ListFactoryNewLevel(ListFactory *)
  457.  ListFactory * ListFactoryEndLevel(ListFactory *)
  458.  void ListFactoryResult(Tcl_Interp *, ListFactory *)
  459.  
  460. The difference is that a call to C<ListFactoryFinish> should precede the
  461. actual usage of the value of C<ListFactory>, and there are two
  462. different ways to append an C<Arg> to a C<ListFactory>:
  463. ListFactoryAppendCopy() guarantees that the value of C<arg> is copied
  464. to the list, but ListFactoryAppend() may append to the list a
  465. reference to the current value of C<arg>. If you are not going to change
  466. the value of C<arg> after appending, the call to ListFactoryAppend may
  467. be quicker.
  468.  
  469. As in B<TCL>, the call to ListFactoryFree() does not free the
  470. C<ListFactory>, only the objects it references.
  471.  
  472. The functions ListFactoryNewLevel() and ListFactoryEndLevel() return a
  473. pointer to a C<ListFactory> to fill. The argument of
  474. ListFactoryEndLevel() cannot be used after a call to this function.
  475.  
  476. =head2 DStrings 
  477.  
  478. Production of strings are still supported in B<portableTk>.
  479.  
  480. =head2 Accessing C<Arg>s
  481.  
  482. The following functions for getting a value of an C<Arg> I<may> be
  483. provided:
  484.  
  485.  double LangDouble(Arg)
  486.  int LangInt(Arg)
  487.  long LangLong(Arg)
  488.  int LangIsList(Arg arg) 
  489.  
  490. The function LangIsList() is supported only partially under B<TCL>,
  491. since there is no data types. It checks whether there is a space
  492. inside the string C<arg>.
  493.  
  494. =head2 Assigning numbers to C<Arg>s
  495.  
  496. While LangSetDouble() and LangSetInt() are supported ways to assign
  497. numbers to assign an integer value to a variable, for the sake of
  498. efficiency under B<TCL> it is supposed that the destination of these
  499. commands was massaged before the call so it contains a long enough
  500. string to sprintf() the numbers inside it. If you are going to
  501. immediately use the resulting C<Arg>, the best way to do this is to
  502. declare a buffer in the beginning of a block by
  503.  
  504.    dArgBuffer;
  505.  
  506. and assign this buffer to the C<Arg> by
  507.  
  508.    void LangSetDefaultBuffer(Arg *)
  509.  
  510. You can also create the buffer(s) manually and assign them using
  511.  
  512.    void LangSetBuffer(Arg *, char *)
  513.  
  514. This is the only choice if you need to assign numeric values to
  515. several C<Arg>s simultaneously. The advantage of the first approach is
  516. that the above declarations can be made C<nop>s in different languages.
  517.  
  518. Note that if you apply C<LangSetDefaultBuffer> to an C<Arg> that
  519. contains some value, you can create a leak if you do not free that
  520. C<Arg> first. This is a non-problem in real languages, but can be a
  521. trouble in C<TCL>, unless you use only the above API.
  522.  
  523. =head2 Creating new C<Arg>s
  524.  
  525. The API for creating a new C<Arg> is
  526.  
  527.  void LangNewArg(Arg *, LangFreeProc *)
  528.  
  529. The API for creating a new C<Arg> is absent. Just initialize C<Arg> to
  530. be C<NULL>, and apply one of C<LangSet...> methods.
  531.  
  532. After you use this C<Arg>, it should be freed thusly:
  533.  
  534. C<LangFreeArg(arg, freeProc)>.
  535.  
  536. =head2 Evaluating a list
  537.  
  538. Use
  539.  
  540.  int LangArgEval(Tcl_Interp *, Arg arg)
  541.  
  542. Here C<arg> should be a list to evaluate, in particular, the first
  543. element should be a C<LangCallback> massaged to be an C<Arg>. The
  544. arguments can be send to the subroutine by reference or by value in
  545. different languages.
  546.  
  547. =head2 Getting result as C<Arg>
  548.  
  549. Use C<Tcl_ArgResult>. It is not guaranteed that result survives this
  550. operation, so the C<Arg> you get should be the only mean to access the
  551. data from this moment on. After you use this C<Arg>, you should free
  552. it with C<freeProc> C<LANG_DYNAMIC> (you can do LangSet...() in between).
  553.  
  554.