上面的例子足够的简单,但如何在 gnome 中使用 ORBit 呢,(无论如何,它就是为此设计的)。在本章中我们将查看在 gnome 使用 ORBit,给在前面章节中提供了 idl 的计算器一个简单的 GUI。
GUI 将被非常简单的示意性的并且将不提供太多的用户友善性。这个例子的目标是向你展示如何使用函数调用。我们将提供两个文本录入构件(entry widget)和两个用于两个值加减的按钮。
Example 6-1. Calculator Client with GUI C Source
#include <orb/orbit.h> #include "calculator.h" void add_it(GtkWidget* o, CORBA_Environment* ev) { GtkWidget* e1; GtkWidget* e2; GtkWidget* res; CORBA_double d1; CORBA_double d2; CORBA_double d3; /* extract the values from the e1, e2 entry fields. convert the strings to double values and assign these values to d1, d2 respectivly 从 e1, e2 录入字段中提取出数值。典型的, 把字符串转换成双精度数值,再把这两个值赋给 d1, d2 */ res = Calculator_add(d1, d2, ev); /* convert res to a string and display it in the res widget 把 res 转换成字符串并在 res 构件上显示 */ } GtkWidget* create_gui(CORBA_Object server) { /* create the gui with all the elemts. e1 is the entry widget for the first value; e2 is the entry widget for the second value; res is the widget to display the result; b_add is the button to add the two values; b_sub is the button to subtract the two values; window is the top level window 建立 GUI 及其所有的成员。 e1 是第一个数值的录入构件 e2 是第二个数值的录入构件 res 是显示结果的的构件 b_add 是加两个值的按钮 b_sub 是减两个值的按钮 window 是顶层窗口 */ /* * create all the widgets and place them in the window * 建立所有构件并放置在窗口上 */ ...... gtk_object_add(b_add, "calc-server", server); gtk_object_add(b_add, "e1", e1); gtk_object_add(b_add, "e2", e2); gtk_object_add(b_add, "res", res); gtk_object_add(b_sub, "calc-server", server); gtk_object_add(b_sub, "e1", e1); gtk_object_add(b_sub, "e2", e2); gtk_object_add(b_sub, "res", res); gtk_signal_connect(b_add, "clicked", add_it, 0); gtk_signal_connect(b_sub, "clicked", subtract_it, 0); return hbox; } int main(int argc, char* argv[]) { CORAB_Environment env; CORBA_ORB orb; CORBA_Object server; CORBA_double res; GtkWidget* app; gchar* dummy_argv[2] gint dummy_argc; CORBA_exception_init(&ev); dummy_argc = 1; dummy_argv[0] = argv[0]; dummy_argv[1] = 0; orb = gnome_CORBA_init("simple-calculator", NULL, &dummy_argc, dummy_argv, 0, NULL, &ev); server = CORBA_ORB_string_to_object(orb, argv[1], &ev); app = create_gui(server); gtk_widget_show_all(app); gtk_main(); exit(0); };
注意变量 dummy_argc 和 dummy_argv 的用法,如果你把原始的 argc、argv 传递给 gnome_CORBA_init,你必须提供一个 argp 分析器。我们在这个简单的示意性的例子中不打算这样做,但在真实的应用中这确实是有帮助的。
最大的不同是 GUI 部分。为了激活 CORBA 系统并初始化它,它必须与事件驱动的 Gtk 库协同工作,你必须调用 gnome_CORBA_init() 而不是 CORBA_ORB_init()。就是这样,无须动用什么大家伙。