GNU TeXmacsUser manualReferenceAboutEnglish
Dialogs and composite widgets

Dialogs are collections of widgets arranged in a window in order to perform a common task. You might want to create one of this in order to configure or interact with a plugin: add some configuration options as well as some common actions and have the window always open besides your document. A good example whose code might help is the preferences dialog (open-preferences).

In order to create more complex layouts than those we did before you'll need a few containers. Among these are aligned and tabs, which we explain below. A very useful macro which you'll be using often is dynamic: it allows you to embed one widget into another.

Let's see how you create a dialog. To get started here is one little example taken from

menu-test.scm

:

Scheme]

(tm-widget (widget1)

(centered

(aligned

(item (text "First:")

(toggle (display* "First " answer "\n") #f))

(item (text "Second:")

(toggle (display* "Second " answer "\n") #f)))))

The keyword centered is clear, just center whatever it contains, but aligned not so much: it builds two column tables, with each row of type item. As you can see, each item takes two arguments, which can be of any type.

The toggle is another example of a widget which triggers a Scheme command whenever it's clicked, or toggled in this case. The second argument stands for the default state of the toggle.

Again, in order to display this you create a top-window and give it a title.

Scheme]

(top-window widget1 "Two toggle widgets")

You'll notice that the created window is too small and the title is not wholly displayed. You can force it to be of a certain size using resize:

Scheme]

(tm-widget (widget1)

(centered

(resize "500px" "200px"

(aligned

(item (text "First:")

(toggle (display* "First " answer "\n") #f))

(item (text "Second:")

(toggle (display* "Second " answer "\n") #f))))))

Scheme]

(top-window widget1 "A bigger window")

resize is another of the several available container or content management widgets. It accepts two sorts of arguments. Either one sets a fixed size for the widget with two strings, as in the example above, or one passes two (quoted) lists, the first for widths, the second for heights, with the minimum, default and maximum values in that order, like this:

(resize '("100px" "200px" "400px") '("100px" "200px" "400px") (dynamic (some-widget-here)))

This sets the tm-widget some-widget-here, included with dynamic, to have a default square size of 200x200 pixels.

If you want to add the usual buttons you use bottom-buttons like in the following example. Notice that the widget now accepts one parameter cmd which will be called when the user clicks the “Ok” button.

Scheme]

(tm-widget (widget1-buttons cmd)

(centered

(aligned

(item (text "First:")

(toggle (display* "First " answer "\n") #f))

(item (text "Second:")

(toggle (display* "Second " answer "\n") #f))))

(bottom-buttons >> ("Ok" (cmd "Ok"))))

Since the widget now needs an argument, we must use another function to display it, namely (dialogue-window widget cmd title). It passes to the widget a function which first calls cmd with the same arguments and then closes the window. Both top-window and dialogue-window accept optional arguments after the title: first urls of buffers which belong to the window (a new window for the same buffers closes the former one), then a command which is executed when the window is closed. They are defined in

menu-widget.scm

.

Scheme]

(dialogue-window widget1-buttons (lambda (arg) (display* arg "\n")) "Two toggles")

That special >> at the end of the widget inserts as before whitespace, but it stretches and aligns the bottom-buttons to the right. This is just another example of a glue widget.

1.Composite widgets

Note that our second dialog, widget1-buttons is just a copy of widget1 with an extra line at the end. We could have spared us the keystrokes in this way:

Scheme]

(tm-widget (widget1-buttons-smarter cmd)

(dynamic (widget1))

(bottom-buttons >> ("Ok" (cmd "Ok"))))

Scheme]

(dialogue-window widget1-buttons-smarter (lambda (arg) (display* arg "\n")) "Two toggles")

As you can see, the approach we've shown has a shortcoming: there's no way to access all the values of the different widgets in your dialog at the same time. Of course you can use the function cmd passed to your widget to perform some computations, but in case you need to retrieve or store complicated data, what you need is a form.

Permission is granted to copy, distribute and/or modify this˙document under the terms of the GNU Free Documentation License, Version 1.1 or˙any later version published by the Free Software Foundation; with no Invariant˙Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of˙the license is included in the section entitled "GNU Free Documentation License".