Can GTK work with a readline like library?

409 views Asked by At

Is it possible for a graphical GTK program, to also have a "command line interface" like those provided by GNU readline, editline or linenoise?

How to deal with blocking gtk_main() calls, and blocking loop steps from those libraries?

1

There are 1 answers

2
user4815162342 On BEST ANSWER

It is definitely possible to integrate a library such as GNU readline with a graphical program. The simplest option is to spawn a thread dedicated for readline, and communicate with the GUI thread using g_idle_add (which is thread-safe).

If you don't want to use threads, you can use the GIO machinery (g_io_channel_unix_new() and g_io_add_watch()) to instruct the GTK main loop notify you of pending input, and invoke readline using the altnernate interface designed for use as event loop callback. Python's readline support and PyGTK make use of these features to enable the following, all in one thread:

>>> import gtk
>>> w = gtk.Window()
>>> w.add(gtk.Label("foo"))
>>> w.show_all()       # at this point, a window with label is shown

A different question, however, is whether you want your program to sport a command-line interface based on terminal emulation in the 21st century. In GTK you have a multiline editing facility that far exceeds readline(), it's called a GtkTextView. It is quite straightforward (and, I might add, fun) to use it to build a command-line-like facility which has no problems with copy-paste, multiline editing, Unicode, colors, proportional fonts, embedded images, or with working on Windows — and which doesn't rely on arcane terminal codes to boot.