Xfce
Foundation Classes |
|||
« Main Page | |||
Writing a Composite WidgetOne type of widget that you may be interested in creating is a composite widget that is merely an aggregate of other XFC widgets. This type of widget is does nothing that couldn't be done without creating new widgets, but it does provide a convenient way of packaging user interface elements for reuse. The FontSelection and ColorSelection widgets in the standard distribution are examples of this type of widget. The example widget that we'll create here is the Tictactoe widget, a 3x3 array of toggle buttons which triggers a signal when all three buttons in a row, column, or on one of the diagonals are depressed. The parent class for a composite widget is typically the container class that will hold all of the elements of the composite widget. For example, the parent of the FontSelection widget is the Dialog class. Since our buttons will be arranged in a 3x3 array, it might seem natural to make our parent class the Table class. Unfortunately, this turns out not to work that well because when tables are created they need to know the number of rows and columns, so we'll derive the widget from VBox instead and pack a table inside the VBox. Each widget class has a header file which declares the object and its members. A couple of features are worth pointing out. To prevent multiple inclusions at compile time, we wrap the entire header file in an inclusion guard: #ifndef TICTACTOE_HH The header file for the Tictactoe class is <tictactoe.hh>: #ifndef TICTACTOE_HH The Tictactoe class declares a two dimensional array of ToggleButtons pointers called 'buttons' and an on_toggle() signal handler that is called each time a button's active state is toggled. The handler is a common signal handler that takes a ToggleButton pointer as its only argument. The clear() method resets the state of all toggle buttons to inactive, thus restarting the game. The 'tictactoe_signal' is a libsigc++ signal that gets emitted whenever a winning combination of buttons is toggled. The source file for the Tictactoe class is <tictactoe.cc>: #include "tictactoe.hh" In the Tictactoe constructor a 3x3 table is added to the vertical box. Then nine ToggleButtons are created and pointers to them added to the 'buttons' array. Each button's "toggled" signal is connected to the on_toggle() signal handler and the button's size is set to (20, 20). You will notice that sigc::bind() is used to bind each button's "toggled" signal to the on_toggle() signal handler. The clear() method is straight forward; it loops over each button setting its state to inactive. Inside on_toggle() two arrays specifying the winning combinations are defined. Each time the handler is called it loops over all the buttons in the array, checking each button's active state. If a winning combination of buttons is found to have been toggled the 'tictactoe_signal' is emitted. When implementing your own libsigc++ signals, they need to be explicitly emitted. For example, you can emit the tictactoe_signal either by calling emit(), as above, or by calling the signal's function operator, like this: tictactoe_signal();
The source file for the application that will test the Tictactoe widget is <ttt_test.cc>: #include <xfc/main.hh> This program is fairly simple. The constructor creates a new Tictactoe widget and adds to the main window. Remember windows are bin widgets and can only contain one widget. The widget's 'tictactoe_signal' is connected to the on_win() signal handler which prints a congratulatory message to stdout (usually a console window). Compiling Tictactoe
If you compiled and installed XFC yourself, you will find the source
code for Tictactoe in the
<examples/tictactoe> source directory along with a Makefile. If
XFC came pre-installed, or you installed it from an RPM package, you
will
find the source code in the
</usr/share/doc/xfcui-X.X/examples/tictactoe> subdirectory. In
this case you will have to create the Makefile yourself (replace X.X
with the
version number of the libXFCui library you have installed). |
Copyright © 2004-2005 The XFC Development Team | Top |
XFC
4.4 |