Tools & thanks gpeddler 2 wxLua Simple OO in Lua Code notes


Using wxLua
UI layout
Buffered drawing
Help in HTML
Missing features

Using wxLua

wxLua <http://www.luascript.thersgb.net> makes most of the power of wxWindows available to Lua programmers, without having to write a single line of C++. I wrote gpeddler 2 to try it out.

In my opinion, wxLua seems to be rather complete and usable, even if (at the time of this writing) some differences from Lua calls to C++ calls seems to be a bit under-documented. I got around this by using the wxWindows documentation together with the file classes.txt included in the wxLua distribution.

UI layout

wxLua offers different ways to design a window (frame) layout. The easiest one is probably to use a visual editor (e.g. xrced) to design an XRC resource file containing an XML description of the desired layout.

I chose to do things the hard way instead: gpeddler 2 creates wxLua interface elements from the program itself, then aligns them using wxWindows "sizers". The main reason was that I wanted to understand how things worked at low level, and also to learn how to create or change a layout at runtime.
This takes up a lot of code and time, so for unchanging layouts in a production environment a visual editor is recommended.

Sizers are a bit difficult to get a good grip of at first, but they enhance portability and handle window resizing very nicely (try resizing the main window).

The most interesting UI code is in the interface definition section in gpeddler2.wx.lua.

The only problem I encountered with wxWindows is an incorrect redesign of part of the Options dialog window, after showing an error message window (e.g. for an out-of-range value); it does not affect functionality, though.

Buffered drawing

To avoid flickering in the route diagrams, gpeddler 2 uses a buffer off-screen bitmap (Solver.bitmap): all drawing is done into this bitmap using a memory device context (wxMemoryDC), then updated to the screen when appropriate. The latter is done by the main "idle" function to avoid every possible conflict for screen access.

As the main window (frame) is resizable, the buffer bitmap is deallocated and  reallocated when the size of a route drawing area changes. Try resizing the window while the program is running to see it at work.

The buffer bitmap code is in Solver:Repaint() in solver.lua, while the drawing code is in Route:Draw() in route.lua.

To increase drawing efficiency, the concept could be further extended by creating a second buffer bitmap for the unchanging part of the route drawing area, e.g. the places (small squares) and their numeric labels.

Help in HTML

Since I didn't have time for a full-fledged 'official' HTML help, I just put together a window (frame) to show these pages. It is in any case much better than an "About" box and an external documentation, and doesn't require much more effort.

The help code in the help.lua file.

I created the help pages using the Mozilla Composer, which is more than adequate for simple HTML editing. Note that CSS must be disabled in the Composer, because it is not recognized by the wxWindow HTML renderer.

Missing features

I am perfectly aware that the interface could be better: I left behind a few things I wasn't able to do in a reasonable amount of time:

- Keyboard support is incomplete.
- The help window does not have a 'back' function.


Back to start of page