我该如何改造一个GUI到现有的C程序? [英] How do I retrofit a GUI to an existing C program?

查看:150
本文介绍了我该如何改造一个GUI到现有的C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在移植一个旧的solaris CL程序在Linux上运行,并限制一些无关的硬件问题的一个项目,这是结束了。现在我想为它的GUI,这样用户可以使用下拉菜单的各种选项中进行选择和复选框,以及一些文本输入区域为没有这样的限制,如文件名的选择。 (该程序是运行一些spectroscanners,并将​​结果保存为CSV文件的内部工具它可以处理所有这些选项,运行扫描仪和处理信息,并将其存储在指定的文件名。我只是想要的东西更好比CL使用。 )

我见过这样的事情做的是一个PyGTK的+ GUI与Python绑定的C code(我认为这就是它是唯一的一次,那是我的第一个学期共opping,我不明白非常!)。这是多一点,比我想进入现在;有一个相对简单的方式做到这一点?当我用Google搜索,我发现痛饮( http://www.swig.org/index.php );这是一个很好的路要走?

解决方案

这听起来完全工作的Tcl / Tk 被设计为。它有一个非常简单的C API,可以让你用一个回调注册的命令。如果在一个Tcl程序中使用命令,它会调用回调函数,并提供一种机制来转换参数一个Tcl列表之间(本机数据结构)和char类型的ARGV样式数组*。

这是专门为很容易加装这种包装以命令行驱动的C程序。也有各种可以使用的接口间preTER以及其他模式的,而且很容易嵌入到程序作为一种脚本语言。从内存可用的接口机制是:

Ousterhout加入的书 Tcl和传统知识工具包是有点过时,但有很好的指导作用到C API。韦尔奇的中的Tcl / Tk 实际的编程是等经典的Tcl / Tk书,并更新更多经常。还有其他几个图书和互联网上相当多的电子资源。一些很好的出发点是: Tcl的教程,的 TK教程的Tcl宣传网站(可能是值得细读,以帮助您决定是否要沿着这条路走下去),的Tcl / Tk维基当然 的#1和。

TK会给你一个简单的GUI和很容易学会程序 - 如果有点简单化。它并不像的href=\"http://stackoverflow.com/questions/349409/why-are-tk-guis-considered-ugly\">难看,如果你需要一些时间来< A HREF =htt​​p://stackoverflow.com/questions/349409/why-are-tk-guis-considered-ugly#349792>调整外观或使用一个主题引擎如的瓷砖。

由于诺曼拉姆齐<一个href=\"http://stackoverflow.com/questions/385156/how-do-i-retrofit-a-gui-to-an-existing-c-program#385550\">points出(+1),用一个简单的C API另一种选择是的Lua。两者都有优点和缺点。的Tcl的主要力量是来自第三方库(例如蒂克斯简单,干净综合TK工具包和好,成熟的支持支持)。 Lua的主要力量是语言是好得多,但没有标准的GUI工具包,使用户界面做的好。 Lua中也有线程在国米preTER,已经从地上爬起来专为这个更好的支持。但是,如果你正在包装一个传统的C / UNIX应用程序,这不可能是一个显著特点。

wxWidgets的比传统知识更加复杂和携带更多行李的运行时,但具有更丰富的功能集。

如果你有真正的理由认为你的脚本项目将发展成你可以考虑的Lua一个更大的应用程序。然而,在更大的规模你进入实质性开发项目和的Python 或的红宝石开始变得可行的方案。随着项目的变大包裹C codeBase的将是整个项目和第三方库支持的一小部分将是一个更大的代价。

如果你去Tcl和发现项目都有自己的生活,可以考虑嵌入Tcl的跨preTER并重新铸造的应用程序作为一个插件API,人们可以连接自己的脚本之中。额外的功能是可以做到的脚本和可能来糊弄到第三方进行维护。一个带有内置的脚本语言系统的优点是,你个人并不一定要实现的功能。人们可以编写自己的扩展脚本语言或让第三方为他们做。

呷旨在生成的图书馆包装。它解析头文件并生成一个胶层是presents在目标语言的本机API。要使用它,你必须在你的程序重新因子到库中。

I've been working on a project of porting an old solaris CL program to run on Linux, and barring some unrelated hardware issues, that's finished. Now I want a GUI for it, so the user can choose among the various options with drop downs and check boxes, as well as some text input areas for options that aren't so restricted, like the filename. (The program is an internal tool to run some spectroscanners and store the results as CSV files. It handles all these options, runs the scanners and processes the info and stores it with the specified filename; I just want something nicer to use than CL.)

The only time I've seen something like this done was a PyGTK+ GUI with python bindings for the C code (I think that's what it was; that was my first semester co-opping and I didn't understand very much!). That's a bit more than I want to get into right now; is there a relatively easy way to do this? When I Googled I found SWIG (http://www.swig.org/index.php); is this a good way to go?

解决方案

This sounds like exactly the job Tcl/Tk was designed for. It has a very simple C API that allows you to register commands with a callback. If you use the command in a Tcl program, it will invoke the callback and provide a mechanism to convert the arguments between a Tcl list (native data structure) and an ARGV style array of char*.

It was designed specifically to be easy to retrofit this sort of wrapper to command-line driven C programs. There are also a variety of other modes you can use to interface the interpreter as well, and it is easy to embed into programs as a scripting language. From memory the available interfacing mechanisms are:

  • Register commands in the Tcl interpreter
  • Embed a Tcl interpreter in your program and use Tcl as an embedded scripting language (possibly including registration of commands and callbacks to your program)
  • Spawn a process with a full-duplex pipe and send commands via stdin/stdout (you can also attach an event handler to the pipe which is invoked when data is available)
  • Less Tcl specific mechanisms such as fork/exec or connection via sockets.

Ousterhout's book Tcl and the TK Toolkit is a bit dated but has a good guide to the C API. Welch's Practical Programming in Tcl/Tk is the other classic Tcl/Tk book and is updated more frequently. There are also several other books and quite a lot of electronic resources on the internet. Some good starting points are: Tcl tutorial, TK tutorial, Tcl advocacy site (might be worth perusing to help you decide if you want to go down this route), Tcl/Tk Wiki and of course Stackoverflow.

TK will give you a straightforward GUI and is very easy to learn to program - if a little simplistic. It's not as ugly as it used to be if you take some time to tweak the appearance or use a theming engine such as Tile.

As Norman Ramsey points out (+1), another alternative with a simple C API is Lua. Both have advantages and disadvantages. The principal strengths of Tcl are the simple and cleanly integrated TK toolkit and good, mature support support from third party libraries (e.g. Tix). The main strength of Lua is that the language is much nicer but there is no standard GUI toolkit, so the UI is not as nicely integrated. Lua also has much better support for threading in the interpreter, having been designed for this from the ground up. However, if you're wrapping a legacy C/unix application, this is unlikely to be a significant feature.

WXWidgets is considerably more complex than TK and carries more runtime baggage but has a richer feature set.

If you have genuine reason to think that your scripting project will grow into a larger application you might consider Lua. However at a larger scale you're into a substantial development project and Python or Ruby start becoming viable options. As the project gets larger wrapping the C codebase will be a smaller portion of the overall project and third-party library support will be a bigger consideration.

If you go with Tcl and discover your project gets a life of its own, consider embedding the Tcl interpreter and re-casting the application as a plugin API that people can hook their own scripts into. Extra features can be done as scripts and possibly fobbed off onto third parties for maintenance. One of the advantages of a system with a built-in scripting language is that you personally do not necessarily have to implement features. People can write their own extensions in the scripting language or get third parties to do it for them.

SWIG is designed to generate wrappers around libraries. It parses the header files and generates a glue layer that presents a native API in the target language. To use it, you would have to re-factor your program into a library.

这篇关于我该如何改造一个GUI到现有的C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆