CHAPTER 1 Introduction

Button example


Let's look an example that does deal with input. Listing 1-2 shows a short Fresco program that displays a button object that responds to mouse (input) events. Figure 1-5 shows a screen shot of the button. When the button is pressed, the word "pushed" is printed to standard output.



Lines 18-23 are pretty standard fare. These create the Fresco context, obtain references to figure, layout and widget kits, and then create a character string ("Push") for the button. Lines 27-30 create the button. The IDL operation WidgetKit::push_button is shown here.

interface WidgetKit : FrescoObject {
	Button push_button(in Glyph g, in Action a); 
	. . . 
};
A label glyph is passed to the push_button() operation on line 28, and an Action object is passed on line 29. Actions are typically implemented using callbacks. In our case we want the button's action to invoke the member function defined on line 10.

Since no kit in Fresco returns an implementation of an Action object, for the first time we must instantiate an implementation object explicitly. This is done by specifying new ActionCallback on line 29. Note the #include directive

#include <Fresco/impls/action.h>
on line 5. Here, we are reaching into non-standard, implementation objects (impls) in order to access the callback implementation. ActionCallBack(T) happens to be implemented using macros, which can be seen on lines 13 and 14. Some compilers still have difficulty with templates and pointers to member functions, or, otherwise, ActionCallback would be implemented using templates. In this case lines 13 and 14 would be omitted, and line 29 would read

    Action_var(new ActionCallback<App>(&a, App::push_me))
The ActionCallback constructor takes a pointer to an App class instance and a pointer to a member function. When the button is pressed, the function App::push_me is called causing "pushed" to be printed to standard output.

Lines 32 "wraps" the button within a margin glyph.

	Glyph_var margin = layouts->margin(button, Coord(15));
The purpose of the margin glyph is to alter the appearance of its body by placing space around it.

We then wrap the margin in a glyph that draws a background color. This is done on line 33.

	Glyph_var bg_filler = layouts->back(
		margin, Glyph_var(widgets->filler())
	);
This glyph will first draw its "back" glyph--the glyph returned by widgets->filler()--followed by its body glyph which is the margin surrounding the button. The
widgets->filler() glyph simply draws in the color gray.

Line 36 has the call to run().

     FrescoLib::run(button, bg_filler);
Recall that run() takes two arguments. The first argument is a Fresco viewer object, which is responsible for handling input. The second argument is a glyph to be displayed on the screen. Since the Button derives from Fresco's Viewer interface, it is passed as the viewer argument. For the second argument (the glyph argument) we pass the bg_filler--the glyph that wraps the margin which in turn wraps the button. Glyphs, such as the bg_filler and margin, that alter the appearance and behavior of their body are often called monoglyphs.

You may have noticed the placing of smart pointers around function returns. This can be seen on lines 28, 29, and 31. Memory management conventions state that an operation returning an object reference must return a duplicated reference. (This can be thought of as an additional reference count.) The smart pointer is needed, then, to ensure that the duplicated reference is released.


Copyright (c) 1994 by Steve Churchill
Comments or questions? Contact Steve Churchill (stevec@faslab.com)

Generated with CERN WebMaker