CTkView - a C++ class for embedding Tk in MFC views

The idea of embedding Tk in MFC windows always seemed very enticing but information was sparse and contradictory - on a scale between "very easy" and "not yet possible". The only thing for it was to have a go and lo, it wasn't that hard after all. 

CTkView is a C++ class which can be used in MFC SDI or MDI applications. An instance of CTkView hosts an embedded Tk toplevel widget and performs some management chores for the widget so that it can size, update and react correctly to Windows events. 

Here is where you can  download source and binaries for CTkView. (~45K) 

Description 
These programms are a testbed for testing Tk embedded in MFC applications. They demonstrate how to embed Tk in an MFC application, such that multiple concurrent Tk views can be open. The testbed consists of three parts: 

TKVIEW.EXE 
An MFC (app-wizard generated) program with the doc/view architecture. The View class does nothing except host an embedded Tk toplevel widget, which is created when the view is created. When the app starts it creates an interpreter for itself and initializes Tk, then calls Tcl_DoOneEvent() on a timer. 

TCLSRVR.TCL 
This script implements a "script server" using the TCL socket command. It is sourced into tkview.exe on startup and listens for scripts coming in over its' connection. Upon receiving a script it is evaluated in the interpreter running in tkview.exe. 

REMCON.TCL 
This script is started with wish80 and implements a remote console suitable for communicating with the script server running in tkview.exe. Use this console to interactively  create widgets for the Tk-Views, place or pack them, configure them etc. Upon startup it reads the file REMCON.TXT which contains the ip address of the tkview server and the script which user last evaluated 

You will need a correctly installed TCL80 setup and the MFC DLLs to get all this to work. The sources compile with VC++ 5.0 

How to use 

1) First start tkview.exe. It initializes an embedded TCL interpreter and sources the script tclsrvr.tcl. This creates a listening socket within tkview.exe which can be used to "inject" scripts from outside into the programm. The programm then opens a view with an embedded Tk toplevel called .tk1. You can open further Tk views with the file/new menu option 

2) Start wish80 with remcon.tcl as its argument, press the connect button and start issuing commands against the Tk window named .tk1. Press Ctrl-E or the Eval button to evaluate the highlighted text in the tkview interpreter. If no text is highlighted, then the current line is evaluated. 

Each new view which is opened is created with an embedded Tk toplevel widget. They are named .tk1, .tk2 etc By using these widget names you can pack child widgets into the MFC views 

It seems that most things work as one would expect. The windows size and update correctly, the widgets are active for mouse and keyboard input. However this has not been exhaustively tested and there will probably be some issues which need to be dealt with. 

I would appreciate any and all  feedback from people who test or use this code. 

Good Luck 

============================
PS: I had some feedback concerning compiler errors when compiling the code. In fact I have changed some of the prototypes in TCL.H and TK.H to read (for example)

int Tcl_Eval (Tcl_Interp* interp, const char* script)

instead of

int Tcl_Eval (Tcl_Interp* interp, char* script)

The insertion of "const" does no harm to TCL (and requires no recompilation of the TCL DLLs) but does allow the VC++ compiler to cast from CString to char* without complaining. This was a convenience for me since I use CStrings extensively. Sorry if anyone tripped up on that

============================
Another Bug was a missing call to Tcl_Init in the InitTclTk function. This was causing the error message "Cannot initialize Tcl/Tk" on some installations