3

Context: Windows7, optionally MinGW

I'm thinking of writing a Tk interface for Lhogho. Short of reading the source of Tkinter, how does one write that kind of a wrapper? It's been done for C++, Perl, Python, Tcl and others.

Charles
  • 50,943
  • 13
  • 104
  • 142
bugmagnet
  • 7,631
  • 8
  • 69
  • 131

1 Answers1

4

It depends on how close you want the mapping to be.

The trivial way is to embed a Tcl interpreter and just call Tcl_Eval() or one of the other functions of that family with the right Tcl code as strings, thats pretty easy to do when you can link C libraries or DLLs.

Once you have that, you can start to make your interface more natural, and for example provide mappings from your languages datastructures to Tcl/Tk internals (Tcl_Obj*), mostly for performance, or to add appropriate wrappers to make the bindings more natural for your language. The main problem usually is to get the event loop working properly with your language.

Have a look at perls tcl::tk module or pythons Tkinter for an example of this.

See: http://wiki.tcl.tk/13208

A different way to do it was done by PerlTk, which tried to NOT embed a full Tcl interpreter. Works but is much harder to do, as Tk uses a significant amount of Tcl code for the not performance critical parts like default binding scripts etc.

Some examples of embedding Tcl/Tk and discussions can be found at: http://wiki.tcl.tk/2074

schlenk
  • 7,002
  • 1
  • 25
  • 29
  • Hmm ... maybe I should think of some other GUI kit, like wxWidgets or IUP – bugmagnet Jan 09 '12 at 03:32
  • Doesn't make it easier, just makes you write more C-Code instead of a mix of C and Tcl code. But yes, you should surely have a look at other toolkits too. If you only target Windows you should also have a look at the native windows options. – schlenk Jan 09 '12 at 19:14
  • Does have to be cross-platform. Lhogho does Win and Lin. Someone might convince it to do Mac one day too. – bugmagnet Jan 10 '12 at 00:11
  • 1
    @boost: Whatever you're doing with graphics, you _have_ to manage resources and you _have_ to draw things and you _have_ to handle events. They're all fundamental, and they're all not hugely different between platforms. (The fine details do vary quite a bit though.) – Donal Fellows Jan 10 '12 at 11:04
  • @DonalFellows: Yes the fine details do indeed vary but you do make valid points all the same. Thanks. +1 and the next time you see SteveL, say hello for me. – bugmagnet Jan 10 '12 at 15:07