Exemplo 1


Enunciado

Para fazer pelo aluno.

OC 1999/2000 (Exercícios Práticos)


Solução

%% Copyright (C) 1996 Ericsson Software Technology AB, Erlang Systems
%% File : qes.erl
%% Author : Anders Dahlin <anders@erlang.ericsson.se>
%% Purpose : A solution to exercise 7.2 in the Introductory Course in Erlang
%% Created : 11 Oct 1996 by Anders Dahlin <anders@erlang.ericsson.se>
%% Modified: 25 Aug 1997 by Gunilla Hugosson <gunilla@erlang.ericsson.se>

-module(qes).
-author('anders@erlang.ericsson.se').

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

start() ->
	spawn(qes, init, [self()]).

init(Parent) ->
	process_flag(trap_exit, true),
	make_window(),
	loop(Parent).

loop(Parent) ->
	receive
		{gs, quitBtn, click, Data, Args} ->
			exit(normal);
		{gs, spawnBtn, click, Data, Args} ->
			spawn_link(qes, init, [self()]),
			loop(Parent);
		{gs, errorBtn, click, Data, Args} ->
			exit(error);
		{'EXIT', Parent, Reason} ->
			exit(Reason);
		{'EXIT', From, normal} ->
			loop(Parent);
		{'EXIT', From, Reason} ->
			spawn_link(qes, init, [self()]),
			loop(Parent);
		_ ->
			'message unknown'
	end.

%% ----------------------------------------------------------------------
%% Opens a window with three buttons for
%% QUIT: Closes window and all child windows
%% SPAWN: Spawns of a child window
%% ERROR: Dies because of an error, all children will also die.
%% The window should be restarted by the parent window.
%% Title of the window will be the pid of the calling process.
%% The calling process will receive messages when a button is pressed.
%% Messages look like:
%% {gs, ButtonName, click, _Data, _Args}
%% where ButtonName is: quitBtn, spawnBtn or errorBtn
%% Returns: ok
%% ----------------------------------------------------------------------
make_window() ->
	W = gs:create(window, gs:start(), [{width,150}, {height,30}]),
	Q = gs:create(button, quitBtn, W, [{bg, green}, {width,50}, {x,0}, {y,0},
		{label, {text, "Quit"}}]),
	S = gs:create(button, spawnBtn, W, [{bg, lightblue}, {width,50}, {x,50},
		{y,0}, {label, {text, "Spawn"}}]),
	E = gs:create(button, errorBtn, W, [{bg, red}, {width,50}, {x,100}, {y,0},
		{label, {text, "Error"}}]),
	gs:config(W, [{title, pid_to_list(self())}, {map, true}]), ok.

Última actualização: 18-04-2000

OC 1999/2000 (Exercícios Práticos)