Dear FLTK community,
I would like to integrate the test/editor.cxx editor shipped with FLTK 1.1 with
a
programming language based on Lua 5.1.
I would like to display strings generated by Lua in real-time in a
Fl_Text_Display or
Fl_Browser object.
My Lua programmes sometimes consume a lot of computing power but should write
any intermediate
results of the calculations immediately in a FLTK window.
However, only after the whole computation finishes, the output is displayed.
Even the
title bar of the output window appears after the computation ends.
Any calls to redisplay, redraw, Fl::flush, show(int line), etc. failed.
Object-oriented programming is new to me, as is GUI programming, so please be
patient
with me. I have not programmed any `update output` callback function yet.
Would you please help me ?
Alexander
---/-------------------------------------------------------------------------------------
Here is the code:
/* function that actually prints a value to stdout;
passes the string on top of the stack to the buffer; assumes that i is
negative ! */
void aux_printnonstruct (lua_State *L, int i) {
const char *str;
lua_getglobal(L, "tostring");
lua_pushvalue(L, i-1);
lua_call(L, 1, 1); /* this will also call the __string metamethod if one
exists; if toString is
unassigned, lua_call issues an error */
str = lua_tostring(L, -1);
buff->append(str);
lua_pop(L, 1); /* pop string */
}
/* open a new window, and pass any text entered to the interpreter to
luaL_loadbuffer; luaL_loadbuffer finally runs the `print` function (if entered
by the user) via the C aux_printnonstruct procedure (see above) */
void execute_cb (Fl_Widget*, void*) { /* 0.34.0 */
int error;
const char *buffer;
Fl_Window *win = new Fl_Window(640, 480);
buff = new Fl_Text_Buffer();
disp = new Fl_Text_Display(0, 20, 640, 480, "Output");
disp->buffer(buff);
win->resizable(*disp);
win->show();
buffer = textbuf->text();
error = luaL_loadbuffer(L, buffer, strlen(buffer), "line") || lua_pcall(L, 0,
0, 0);
if (error) {
buff->text(lua_tostring(L, -1));
agn_poptop(L);
}
}
/* the menu item `execute` of the main window */
Fl_Menu_Item menuitems[] = {
..
{ "&Run", 0, 0, 0, FL_SUBMENU },
{ "&Execute", FL_F+5, (Fl_Callback *)execute_cb },
{ 0 },
..
};
int main(int argc, char **argv) {
int r;
/* start the interpreter */
L = luaL_newstate();
if (L == NULL) {
fprintf(stderr, "Error initialising.");
return 1;
}
/* initialise libraries */
luaL_openlibs(L);
/* set libname, mainlibname and load library.agn */
luaL_setLibname(L, 1);
luaL_initialise(L, 0);
/* replace `print`, `io.write`, and `io.writeline` functions */
lua_pushcclosure (L, luaB_print, 0);
lua_setglobal (L, "print");
lua_getglobal(L, "io");
lua_pushcclosure(L, io_write, 0);
lua_setfield(L, -2, "write");
lua_pushcclosure(L, io_writeline, 0);
lua_setfield(L, -2, "writeline");
agn_poptop(L);
/* source code editor */
textbuf = new Fl_Text_Buffer;
style_init();
Fl_Window* window = new_view();
window->show(1, argv);
if (argc > 1) load_file(argv[1], -1);
r = Fl::run();
lua_close(L);
return r;
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk