2.9 UI Composition with Delegates (or Views and Controllers)

As we saw before, it is possible to compose the interface for your application using multiple views; this holds true whether the view has an associated controller or not. I'm not going to provide an example embedding views and controllers because it's not very enlightening; it is easy to extend the original UI Composition example to use controller associated to the views, and that example is much more interesting if adapted to a CListDelegate.

Since delegates inherit from views, it is also possible to compose a delegate with multiple child delegates (and even mix and match them with views). The requirement that the parent delegate be based on glade is the same, too: you should subclass GladeDelegate for your main window, though any kind of view can be attached to it.

The nice part about doing UI composition using delegates is that the interface and interaction is well encapsulated in a single class, and if you are careful when writing a child delegate, you can reuse that class in as many places of your applications as you like. I'm going to use the CListDelegate from the last example to build a (rather fake) news browsing application in the next example:

Let's have a quick look at the code:

  1. I create a list of instances and the columns specification; I then define a class for the news shell, inheriting from GladeDelegate.
  2. In the constructor I show off some cosmetic features of Kiwi (the "header" widget is an eventbox, in which the label is a child; look at the glade file for more details).
  3. I create and attach the slave, storing a reference to it in the instance variable "slave", and set focus up.
  4. I define handlers for the buttons, and the simple handler row_selected() for the select_row signal, which will be called when a row is selected.
  5. I create and run the Shell, which renders the interface. When the interface closes, I catch the return value retval and use it as input to run a browser for the selected news item (okay, so the app is not really a browser, I lied).

This example shows a lot of the power of Kiwi; in a few lines, and with a little work in Glade, we have a working application that looks decent, behaves well and isn't hard to maintain. The next section will go on describing Proxies, which are high-level Views that represent an instance or part of it.