19. 工具和组件库

既然您已经了解了 ncurses 及其姊妹库的功能,您现在正摩拳擦掌,准备投入一个大量操作屏幕的项目。但是请稍等... 用纯 ncurses 甚至附加库编写和维护复杂的 GUI 组件可能非常困难。有一些现成的工具和组件库可以代替编写自己的组件。您可以使用其中一些,从代码中获取灵感,甚至扩展它们。

19.1. CDK (Curses 开发工具包)

用作者的话来说

CDK 代表“Curses 开发工具包”,它目前包含 21 个现成的组件,可以促进全屏 curses 程序的快速开发。

该工具包提供了一些有用的组件,可以直接在您的程序中使用。它编写得很好,文档也非常好。示例目录中的示例对于初学者来说是一个很好的起点。CDK 可以从 http://invisible-island.net/cdk/ 下载。按照 README 文件中的说明进行安装。

19.1.1. 组件列表

以下是 cdk 提供的组件列表及其描述。

Widget Type           Quick Description
===========================================================================
Alphalist             Allows a user to select from a list of words, with
                      the ability to narrow the search list by typing in a
                      few characters of the desired word.
Buttonbox             This creates a multiple button widget. 
Calendar              Creates a little simple calendar widget.
Dialog                Prompts the user with a message, and the user
                      can pick an answer from the buttons provided.
Entry                 Allows the user to enter various types of information.
File Selector         A file selector built from Cdk base widgets. This
                      example shows how to create more complicated widgets
                      using the Cdk widget library.
Graph                 Draws a graph.
Histogram             Draws a histogram.
Item List             Creates a pop up field which allows the user to select
                      one of several choices in a small field. Very useful
                      for things like days of the week or month names.
Label                 Displays messages in a pop up box, or the label can be
                      considered part of the screen.
Marquee               Displays a message in a scrolling marquee.
Matrix                Creates a complex matrix with lots of options.
Menu                  Creates a pull-down menu interface.
Multiple Line Entry   A multiple line entry field. Very useful
                      for long fields. (like a description
                      field)
Radio List            Creates a radio button list.
Scale                 Creates a numeric scale. Used for allowing a user to
                      pick a numeric value and restrict them to a range of 
                      values.
Scrolling List        Creates a scrolling list/menu list.
Scrolling Window      Creates a scrolling log file viewer. Can add 
                      information into the window while its running. 
                      A good widget for displaying the progress of
                      something. (akin to a console window)
Selection List        Creates a multiple option selection list.
Slider                Akin to the scale widget, this widget provides a
                      visual slide bar to represent the numeric value.
Template              Creates a entry field with character sensitive 
                      positions. Used for pre-formatted fields like
                      dates and phone numbers.
Viewer                This is a file/information viewer. Very useful
                      when you need to display loads of information.
===========================================================================

最近的版本中,Thomas Dickey 修改了一些组件。

19.1.2. 一些吸引人的特点

除了使用户更容易使用现成的组件外,cdk 还优雅地解决了打印多色字符串和对齐字符串的一个令人沮丧的问题。特殊的格式标记可以嵌入到传递给 CDK 函数的字符串中。例如

如果字符串

"</B/1>This line should have a yellow foreground and a blue
background.<!1>"

作为参数传递给 newCDKLabel(),它将以黄色前景色和蓝色背景色打印该行。还有其他标记可用于对齐字符串、嵌入特殊绘图字符等。有关详细信息,请参阅手册页 cdk_display(3X)。该手册页用很好的例子解释了用法。

19.1.3. 结论

总而言之,CDK 是一个编写良好的组件包,如果使用得当,可以形成开发复杂 GUI 的强大框架。

19.2. dialog

很久很久以前,在 1994 年 9 月,当很少有人知道 linux 时,Jeff Tranter 在 Linux Journal 上写了一篇关于 dialog 的文章。他在文章开头写道:

Linux 基于 Unix 操作系统,但也具有许多独特且有用的内核特性和应用程序,这些特性和应用程序通常超越了 Unix 下可用的特性和应用程序。一个鲜为人知的瑰宝是“dialog”,这是一个用于从 shell 脚本中创建专业外观对话框的实用程序。本文介绍了 dialog 实用程序的教程,并展示了如何以及在何处使用它的示例

正如他解释的那样,dialog 是一个真正的瑰宝,可以轻松制作出专业外观的对话框。它可以创建各种对话框、菜单、复选框列表等。它通常默认安装。如果没有安装,您可以从 Thomas Dickey 的网站下载。

上面提到的文章很好地概述了它的用途和功能。手册页有更多详细信息。它可以在各种情况下使用。一个很好的例子是在文本模式下构建 linux 内核。Linux 内核使用了针对其需求量身定制的 dialog 修改版本。

dialog 最初设计用于 shell 脚本。如果您想在 c 程序中使用其功能,那么可以使用 libdialog。关于这方面的文档很少。权威参考是库附带的 dialog.h 头文件。您可能需要在这里和那里进行一些修改才能获得所需的输出。源代码很容易定制。我曾在许多场合通过修改代码来使用它。

19.3. Perl Curses 模块 CURSES::FORM 和 CURSES::WIDGETS

perl 模块 Curses、Curses::Form 和 Curses::Widgets 提供了从 perl 访问 curses 的途径。如果您安装了 curses 和 basic perl,您可以从 CPAN 所有模块页面 获取这些模块。获取 Curses 类别中的三个压缩模块。安装后,您可以像使用任何其他模块一样从 perl 脚本中使用这些模块。有关 perl 模块的更多信息,请参阅 perlmod 手册页。上述模块附带良好的文档,并且它们有一些演示脚本来测试功能。虽然提供的组件非常简陋,但这些模块提供了从 perl 访问 curses 库的良好途径。

我的一些代码示例由 Anuradha Ratnaweera 转换为 perl,它们在perl目录中可用。

有关更多信息,请参阅手册页 Curses(3)、Curses::Form(3) 和 Curses::Widgets(3)。只有在获取并安装上述模块后才会安装这些页面。