![]() |
Documentation: DataTargets
![]() |
Using FXDataTarget
![]() |
This process is common enough that we have found that it makes sense to support it with a much more declarative programming style:- instead of writing many callback routines, and routines to preset the GUI controls with their initial value, we have implemented a more easy to use method in the form of the FOX FXDataTarget class.
The FXDataTarget acts as an intermediary between a program variable, such as an integer or string, and a FOX widget such as a FXTextField or FXSlider. The FXDataTarget works by associating a variable in the application code with one or more control widgets in the GUI. Several controls may be connected to the same data target, although each data target is associated with only one variable at a time.
A FXDataTarget forms a bi-directional channel through which a GUI control can communicate with a variable in an application program. Thus, once the GUI is created and connected via the FXDataTarget, the GUI controls will automatically display the current value of that variable, and when the user starts interacting with a control, the variable will be automatically changed. Moreover, if several GUI controls are connected to one single FXDataTarget, each of these controls will automatically update when the variable has been changed.
Example Usage
![]() |
// Employee record
struct Employee { FXString name; FXint number; FXdouble salary; FXString address; }; |
Lets make a dialog box which obtains this information from the user,
and of course we'll use the FXDataTargets to eliminate as much coding as
possible; first comes the header file (we'll omit some details in the interest
of brevity):
/* EmployeeEntry.h */
// Employee Information Entry Dialog
|
Well, that's basically it. Now for the implementation file:
/* EmployeeEntry.cpp */
#include "EmployeeEntry.h" FXIMPLEMENT(EmployeeEntry,FXDialogBox,NULL,0) EmployeeEntry::EmployeeEntry(FXWindow* owner):
...
|
We have of course omitted some details here regarding the layout and other visual paraphernalia. Note that we have connected the data target controlling the salary member to both a text field as well as a slider, so we can set the salary either way. So far, it sounds rediculously simple, doesn't it? The secret is, it really is!
Next, we're getting ready to use this new panel. Here's how we
would do that. Lets say we have selected the employee from a big
array of employee records, and we enter the following callback handler
to edit one of the entries in this array:
/* EmployeeDatabase.cpp */
#include "EmployeeEntry.h" ... Employee *employeedatabase; // Database
of records
... long EmployeeDatabase::onCmdChangeInformation(FXObject*,FXSelector,void*){
|
In the above code, we copy the employee record from the database, and then pop up the EmployeeEntry dialog by calling its execute() member function.
When this dialog shows up, it will initially display the old information from employeedatabase[currentemployee]. After editing it with the dialog, if the user hits the Accept button, execute() will return true and we copy the changed record back into the database; if the user hit the Cancel button, execute() we will simply do nothing and return from the callback.
The EmployeeEntry dialog's destructors will automatically clean up the mess.
More Advanced Usage
![]() |
enum Color {Red, Green, Blue};
FXint color;
new FXRadioButton(matrix,"Red",option_target,FXDataTarget::ID_OPTION+Red,...);
|
Here we set the variable color to one of the three values {Red,
Green, Blue} by directly connecting the FXDataTarget to three FXRadioButtons.
When the FXDataTarget receives an ID_OPTION message, it changes the
program variable to (message-ID_OPTION). Using this method,
its easy to input yes/no values, lists of choices, and so
on, all without having to write explicit callback handlers.
How It Works
![]() |
When an FXDataTarget receives a message of type SEL_UPDATE, it reads the value of its associated variable and updates the sender of the message by means of another message. Note that the FXDataTarget does not need to know what type of Widget did the sending.
There are two ways the FXDataTarget can receive updates: by an ID_VALUE message or an ID_OPTION message. The former type is usually generated by a valuator control, such as an FXSlider. When a message from a valuator control is received, FXDataTarget responds by sending back an ID_SETINTVALUE, ID_SETREALVALUE, or ID_SETSTRINGVALUE message to the sender of the request.
When a FXDataTarget receives a message of the form (ID_OPTION+i), it resonds by sending back one of two messages: ID_CHECK or ID_UNCHECK, depending on whether the value of the associated variable is equal to i.
In the example above, the "Red" radio button will receive an ID_CHECK message, because the initial value of the variable color is red. The other radio buttons will receive the ID_UNCHECK message.
All of these methods explained here are deployed in the datatarget example program.