Buttons

As the first component of the drawing tool, we will look at the toolbox. The toolbox is a collection of buttons packed in a frame. See slide draw-toolbox.
  class toolbutton : public button { 
\fbox{toolbutton}
public: toolbutton(widget* w, char* name) : button(w,name) {
(a)

text(name); handler(w,name); pack();
(b)

} }; class toolbox : public frame {
\fbox{toolbox}
public: toolbox(widget* w, tablet* t) : c(t), frame(w,"toolbox") {
(c)

button* b1 = new toolbutton(this,"move"); button* b2 = new toolbutton(this,"box"); button* b3 = new toolbutton(this,"circle"); button* b4 = new toolbutton(this,"arrow"); } int operator()() {
(d)

c->mode(argv[1]); return OK; } private: tablet* c; };

slide: The toolbox

Each button is an instance of the class toolbutton. When a toolbutton is created (a), the actual button is given the name of the button as its path. Next, (b) the button is given the name as its text, the ancestor widget w is declared to be the handler for the button and the button is packed. The function text is a member function of the class button, whereas both handler and pack are common widget functions. Note that the parameter name is used as a path name, as the text to display, and as an argument for the handler, that will be passed as a parameter when invoking the handler object. The toolbox class inherits from the frame widget class, and creates a frame widget with a path relative to the widget parameter provided by the constructor (c). The constructor further creates the four toolbuttons. The toolbox is both the superordinate widget and handler for each toolbutton. When the operator() function of the toolbox is invoked in response to pressing a button, the call is delegated to the mode function of the tablet (d). The argument given to mode corresponds to the name of the button pressed. The definition of the toolbutton and toolbox illustrates that a widget need not necessarily be its own handler. The decision, whether to define a subclass which is made its own handler or to install an external handler depends upon what is considered the most convenient way in which to access the resources needed. As a guideline, exploit the regularity of the application.