Embedding TCL interpreter into wxWidgets application

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
CktDesigner
Earned a small fee
Earned a small fee
Posts: 17
Joined: Mon Sep 21, 2015 5:16 am

Embedding TCL interpreter into wxWidgets application

Post by CktDesigner »

I'd like to embed a TCL (not Tk) interpreter into my wxWidgets application. I saw a thread from long ago (2004? 2006?) that discusses this, but I didn't see any successes mentioned. I looked at cpptcl at SourceForge, but would like to do a more "formal" embed into my C++/wxWidgets application.

Tcl provides a mechanism for embedding itself into other applications via Tcl_SetNotifier and Tcl_SetMainLoop calls. Tcl_SetNotifier allows the definition of callback functions that Tcl can use to service certain events, and Tcl_SetMainLoop defines the main event loop that Tcl should call (normally the application's main event loop). This allows Tcl to use the application's event loop (and therefore the application is in control). Others have used this feature to embed Tcl into Qt...

If one builds Tcl (or the Tcl/Tk combination) as a standalone application, it includes its own event loop. Of course other applications (or application frameworks), like wxWidgets (or Qt, etc.) have their own event loops. When embedding a scripting interpreter like Tcl into an application framework, both can't control the processing of events, so one has to "give up" control to the other.
Tcl appears to provide a mechanism to yield event processing to the application it is being embedded into, as long as the events needed by Tcl can be processed/passed to it. It looks like Tcl does this via:

1. Defining callbacks for the events. Tcl defines:
typedef struct Tcl_NotifierProcs {
Tcl_SetTimerProc *setTimerProc;
Tcl_WaitForEventProc *waitForEventProc;
Tcl_CreateFileHandlerProc *createFileHandlerProc;
Tcl_DeleteFileHandlerProc *deleteFileHandlerProc;
Tcl_InitNotifierProc *initNotifierProc;
Tcl_FinalizeNotifierProc *finalizeNotifierProc;
Tcl_AlertNotifierProc *alertNotifierProc;
Tcl_ServiceModeHookProc *serviceModeHookProc;
} Tcl_NotifierProcs;

This defines a data structure that names functions for various events. I think I have the "timer" related functions covered. wxWidgets has functions that pretty closely parallel the requested Tcl timer needs. Most of the others don't seem too important for a simple script interpreter. The ones related to file handling are mentioned below.
2. This data structure is passed to Tcl using Tcl_SetNotifier() call
3. The application's event loop call is given to Tcl via the Tcl_SetMainLoop() call.
4. Then Tcl_Main() is called. Since step 3 gives Tcl the application's loop, Tcl will initialize itself, then give up control to the application event loop.

The wxWidgets event loop is then in control.

Since some of the information needed by the data structure mentioned above is dependent upon some of the inner-workings of wxWidgets, I could use suggestions on how best to meet the needs of this structure. In particular, the Create/Delete File Handlers seem to be functions that monitor the state of the file descriptor (ie. is data available for reading, can data be written, etc.) Is there a convenient way get this from wxWIdgets?

Is there a wxWidgets event loop function that can be passed to Tcl_SetMainLoop? Tcl wants a function with the prototype:
typedef void Tcl_MainLoopProc(void);

Has anyone been successful in using this to embed Tcl into wxWidgets?

Any assistance is appreciated!

Many thanks!
User avatar
doublemax
Moderator
Moderator
Posts: 19164
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Embedding TCL interpreter into wxWidgets application

Post by doublemax »

I saw you posted the same about a year ago :)

For the most of these callbacks i can't think of an equivalent in wxWidgets. After Googling a bit i found this: http://jefftrull.livejournal.com/4361.html

Did you already experiment with just calling Tcl_DoOneEvent(TCL_DONT_WAIT); periodically? E.g. you could call it from inside an IDLE event handler in wxWidgets.

Also, are you sure you need TCL events for your purpose?

Unfortunately i can't help more. You can also try asking on the wx-users group/mailing list where you can reach the core wx developers: https://groups.google.com/forum/#!forum/wx-users
Use the source, Luke!
CktDesigner
Earned a small fee
Earned a small fee
Posts: 17
Joined: Mon Sep 21, 2015 5:16 am

Re: Embedding TCL interpreter into wxWidgets application

Post by CktDesigner »

Yes, just getting back to this project after finishing another.
OK, thanks... I'll post the question over at wx-users and see if I can find an answer there.
Thanks again!