Mandrake Drak tools made easy
Published: 17 Oct 2002 11:49 BST
You'll see other code in interactive.pm that checks to see whether the app requires root user privileges and if so, asks for the root password. The code in Listing B illustrates how either interactive_gtk or interactive_newt will be used, based on the $DISPLAY variable and a test for the X environment.
That's all there is to it. If you want to display more text than a simple phrase, say, the contents of a file, you can use the Mandrake toolkit's simple function cat_. To use it, you'll want to include common.pm by adding use common to your code. If you take a look at /usr/lib/libDrakx/common.pm, you'll see a number of useful shortcut functions that will save you some coding work. This time, instead of passing the literal strings to ask_warn, I'll try variables. Look at Listing C. You can use a number of other standard widgets beyond a simple OK box. First, let's allow the user to make a decision without changing the code base. Now, the application will display OK or Cancel and proceed based on the user's choice. Figure C shows the GUI version, and Figure D presents the console version. To see the code, check out Listing D.
| Figure C |
![]() |
| Figure D |
![]() |
A more complex configuration tool: The list
Lists are also useful for configuration tools, and these too can be done quite easily. The ask_from_list function expects a title, a prompt, a list of selections, and the default value. Listing E shows how you can provide a popup ToolTip. (The ToolTip doesn't work in text mode.)
A more elaborate list might involve a tree or outline type selection. Listing F illustrates this technique.
In Listing F, you declare the pipe symbol [|] as the separator in argument three of ask_from_treelist, and you declare Fruit|Apple as the default, as well as a None option. In the GUI display, the pipe symbol parses the tree display. On the console, the string is displayed as is, without the visual tree effect, as shown in Figures E and F, respectively.
| Figure E |
![]() |
| Figure F |
![]() |
GUI or text interface?
Combinations of the above widgets, along with the other interfaces in interactive.pm, should be enough to allow you to create a user interface to cover most tasks. The text interface is not quite as sophisticated as the GUI. If you can keep the interface simple, being able to run in both the console and GUI is a definite plus. MandrakeSoft's toolkit is licensed under the GPL, and you are free to use and adapt the code even if you don't use Mandrake Linux.
Have your say instantly in the
Tech Update forum.
Find out what's where in the new Tech Update with our
Guided Tour.
Let the editors know what you think in the
Mailroom.












