ubit::UCall Class Reference
UCall and its template subclasses define a family of classes for adding callback functions and methods.
More...
#include <ucall.hpp>
Inheritance diagram for ubit::UCall:
List of all members.
|
Public Member Functions |
virtual void | operator() (UEvent &)=0 |
| the method that fires callbacks; must be redefined by subclasses.
|
virtual UCall * | callCast () |
virtual void | addingTo (UChild *self, UGroup *parent) |
| impl.
|
Detailed Description
UCall and its template subclasses define a family of classes for adding callback functions and methods.
UClass is never instanciated directly. A variety of ucall( ) templates are used instead, Exemple:
void helpCB(const char* s) {...} // non member function
struct Demo {
void saveFile() {...} // member function (instance method)
....
};
Demo* d = new Demo;
UBox& b = ubutton(UPix::diskette + " Save..."
+ UOn::enter / ucall("Saves the file", helpCB)
+ UOn::action / ucall(d, &Demo::saveFile));
- UOn::xxx is an Event Condition such as: UOn::action, UOn::select, UOn::mpress... (see class UOn). if no condition is given UOn::action is taken as the default
- UOn::enter / ucall("Saves the file", helpCB) specifies that helpCB("Saves the file") is called when the mouse pointer enters the button. ucall() can have 1 or 2 arguments before the name of the callback function (1 argument in this example, the string "Saves the file"). These arguments are given to the callback function when it is called.
- UOn::action / ucall(d, &Demo::saveFile) specifies that d->saveFile() is called when the button is "activated" (e.g. clicked, etc.). Because saveFile() is an instance method, the instance 'd' must be given as the first argument of ucall(). As for the previous case, ucall() could also have 1 or 2 arguments that would be given to the callback function. Example:
struct Demo {
void saveFile(UFilebox* fb, UStr* suffix) {...}
....
};
UFileBox& fbox = ufilebox();
UStr& s = ustr(".doc");
UBox& b = ubutton("Save" + UOn::mpress / ucall(d, &fbox, &s, &Demo::saveFile));
Notes on callback prototypes and arguments
- Argument types : because ucall() is a template, its arguments must have EXACTLY the same types as those of the callback function. For instance, &fbox and &s have exactly the same types as the arguments of saveFile().
- Arguments that are objects : such arguments should not be objects but objects' pointers. This is because C++ copies arguments when they are passed to ucall(). Copying objects is time consuming and often meaningless or illegal (object copy is forbidden by many Ubit classes). This is the reason why the arguments of saveFile() are objects' pointers. Note that this remark only applies to objects (not to basic types such as int, float...)
- Optional UEvent& argument : Callback functions and methods can have an optional UEvent& argument. If provided, this argument must be the first one in the list. Example:
void helpCB(UEvent& e, const char* s) {...}
struct Demo {
void saveFile(UEvent& e, UFileBox* fb, UStr* suffix) {...}
....
};
This argument makes it possible to retreive the UEvent that provoked the call to this callback. UEvent methods give access to the object source (the widget that fired this event), its UView, and other useful data that depends on the event type (see class UEvent).
Misc. remarks
- there are several predefined callback objects: #- uset(a,b) performs: a.set(b) [b is passed by reference] #- uassign(a,b) performs: a = b [b is passed by reference] #- copy(a,b) performs: a = b [b is passed by value (ie. copied)] #- ushow(obj, state) performs: obj.show(state) #- ucloseWin() closes the first parent window that contains this expression
Example:
UStr a, b = "toto";
UDialog& dial = udialog(...whatever...
+ ubutton("Close" + uassign(a,b)) + ucloseWin()));
when the "Close" button is activated, a = b is performed then the dialog is closed.
The documentation for this class was generated from the following files:
Generated on Mon Jan 29 00:20:46 2007 for Ubit by
1.4.7