Skip to content

Introductory Usage Example Code

Ariel Vina-Rodriguez edited this page Jun 4, 2019 · 14 revisions

The examples here are written using what we might call "classic" c ++, following the personal style and the author's preferences. This is correct, but nana does not obligate (and does not even recommend) to follow that style. Splitting the construction of objects, calculating and enumerating each coordinate of the widgets and using a timer to trigger a full redraw several times per second are very special preferences that you must decide if they fit the expectations of modern C ++. Nevertheless, the examples illustrate one possible use and nana flexibility to allow coding using different styles.

The code in these introductory widget pages shows how to do something minimally useful with each widget. The code is intended for someone who has never before used the particular widget and an experienced nana coder who has not used the widget for some time and needs to quickly insert a simple widget usage into a complex GUI.

The example codes do not use the advanced features many of the widgets support, since they are intended to get the coder started without distraction. However, they are coded in a scaleable style which the coder should be able to continue using to develop large and complex GUIs. That is they do not always do what they do in the simplest possible way, but in a way that a coder will not need to unlearn to accomplish complex tasks.

The construction code is split into three steps. This may seem odd for these simple examples, but it is very helpful when maintaining the code for a complex GUI containing dozens of widgets. Step 1 constructs the widgets, specifying only the parent widget. This should be done in the GUI class initializer list. Step 2 moves the widgets to their locations in the parent widget. This should be done in a move method called from the GUI class constructor, which is important because the move method can also be called from a resize handler. Step 3 specifies remaining details such as captions. This should be done in the GUI class constructor.

There is no GUI class in the example codes. That would really be overkill! However, it is strongly recommended to use a GUI class for anything much more complex than these examples. It makes adding to and maintaining a GUI application so much easier if all the widget code is kept separate from the code that implements the application model. Here is a minimal example of a model-gui application showing the essentials of this approach to application design. Here is another example of this approach - even more minimal ( the model is included in the GUI class, not a good idea for complex models ) but the sample code is out of date.

The event handlers use lambda functions. Familiarity with lambda functions is essential to the use of the nana library. Here is a good introduction to lambda functions.

To find out about advanced widget features, there are links to the doxygen documentation. This lists every method available from the widget class, sometimes with brief and short notes about what they do. The doxygen documentation should be considered authoritative since it is generated directly from the library code, though it is hard to browse and not at all friendly to the inexperienced. There are also links in the doxygen documentation to detailed examples of elaborate widget use that exercise many of the advanced features. These examples are often minimally documented but are always actualized. The doxygen documentation also contains embeded examples not automaically generated from the test examples (in nana-demo repo) and thus some of them out of date.

Clone this wiki locally