6 Default Values

6.1  The Default Value Model

When a new object is created, a set of options is provided by the application. Options which are not explicitly given are taken care of by the parent (the container object).

B=gs:create(button,Win,[{x,0},{label,{text,"press Me"}}]).
    

In the example shown above, the window provides default values for options like location and background color. If an application cannot use the default values provided by GS, new ones can be configured. For example, the following code creates a red button at location y=30.

gs:config(Win,[{default,button,{y,30}},
{default,button,{font,{courier,18}}}]),
B=gs:create(button,Win,[{x,0},{label,{text,"press Me"}}]).
    

The syntax for the default option is {default,Objecttype,{Option,DefaultValue}}, where Objecttype is the name of any GS object. The special keywords all or buttons which denote button, radio button, and check button can be used.

The semantics for the default option can be expressed as follows: If an object of kind Objecttype is created and no value for Option is given, then use DefaultValue as the value. Only options of {Key,Value} syntax can be given a default values. Default values may be inherited in several steps. In the following example, the button will show the text "Cancel".

gs:config(Win,[{default,button,{label,{text,"Cancel"}}}]),
F=gs:create(frame,Win,[]),
B=gs:create(button,F,[]).
    

Default values are inherited so that changed default values only affect new objects, not existing objects.

Default values only have meaning when creating child objects, since objects which cannot have children cannot have default options. An example is buttons.

The following example illustrates how default options can be used:

IMAGE MISSING

Figure 6.1:   Example of Default Options


-module(ex16).
-copyright('Copyright (c) 1991-97 Ericsson Telecom AB').
-vsn('$Revision: /main/release/3 $ ').

-export([start/0,init/0]).

start() -> spawn(ex16, init, []).

init() ->
    I=gs:start(),
    Win=gs:create(window, I,
                  [{width, 200},{height, 200},
                   {title,"Default Demo"},{map, true}]),
    gs:create(canvas, can1,Win,
	      [{x,0},{y, 0},{width,200},{height,200},
	       {default,text,{font,{courier,bold,19}}},
	       {default,text,{fg,blue}},
	       {default,rectangle,{fill,red}},{default,text,{text,"Pow!"}},
	       {default,oval,{fill,green}}]),
    {A,B,C} = erlang:now(),
    random:seed(A,B,C),
    loop().

loop() ->
    receive
	{gs,_Id,destroy,_Data,_Arg} -> bye
    after 500 ->
	    XY = {random:uniform(200),random:uniform(200)},
	    draw(random:uniform(3),XY),
	    loop()
    end.

draw(1,XY) ->
    gs:create(text,can1,[{coords,[XY]}]);
draw(2,XY) ->
    XY2 = {random:uniform(200),random:uniform(200)},
    gs:create(rectangle,can1,[{coords,[XY,XY2]}]);
draw(3,XY) ->
    XY2 = {random:uniform(200),random:uniform(200)},
    gs:create(oval,can1,[{coords,[XY,XY2]}]).