21st Century Smalltalk

April 22, 2007

Menu Examples in Lisp and MXML

Filed under: Lisp, Xaml — pfisk @ 5:57 pm

classbrowser3
Vista Smalltalk Site

Open Vista Smalltalk in your browser.

The image above shows the new menus that I am adding to the ClassBrowser. Here is the relevent Lisp code:

(setq mb (@items-callback- MenuBar
‘((File
  ((”File in” fileFileIn)
   (”File out” fileFileOut)
   -
   (Close fileClose)
  )
 )
 (View
 ((”Set colors” viewSetColors)
  (”Set fonts” viewSetFonts)
  -
  (Refresh viewRefresh)
  )
 )
)
(lambda(tag) (@self tag))
))

The first argument is simply a quoted list which defines the menu contents and the second argument is the “callback function” which will be called whenever the user clicks an item. In this case, the callback function simply sends a message to the menu owner - ie, if the user clicks “Set colors”, the message “viewSetColors” will be sent to the ClassBrowser instance.

Compare this to an example of how Flex/MXML sets up menus (note that the menus in the two examples are not identical). And setting up menus in WPF/XAML is very similar to Flex.

If you are using a tool like Flex Builder 2, the MXML code is automatically generated so, in practice, it is quite easy to do.

Both MXML and XAML are examples of declaring interface components statically using XML and then linking selected components to code at compile-time. For many situations, this will work well enough.

The Lisp approach is fundamentally different in that it is totally dynamic. In the Lisp example I showed above, the first list could have been read from a database or generated elsewhere in the program and it would have worked just as well.

I use Lisp because it has the structural expressiveness of XML, the power of a procedural language and the dynamic flexibility of JavaScript.

Microsoft and Adobe propose building RIA’s using three languages:

  • an XML (XAML or MXML) variant for structure
  • a Java variant (C# or ActionScript) for proceedures
  • JavaScript to add dynamic capabilities

I propose using one language, Lisp implemented identically in Flash and .Net, for all of the above.

Blog at WordPress.com.