aboutsummaryrefslogtreecommitdiffstats
path: root/lib/gs/examples/focus_demo.erl
blob: 1a69241a38285fa478ae1ded937bbba23b3008cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
%% ------------------------------------------------------------
%%  Focus Demo
%% ------------------------------------------------------------

-module(focus_demo).
-compile([{nowarn_deprecated_function,{gs,config,2}},
          {nowarn_deprecated_function,{gs,create,4}},
          {nowarn_deprecated_function,{gs,read,2}},
          {nowarn_deprecated_function,{gs,start,0}},
          {nowarn_deprecated_function,{gs,window,2}}]).

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


%% ----- File Selection ----
start() ->
    spawn(focus_demo,init,[]).


init() ->
    S=gs:start(),
    Font = case gs:read(S,{choose_font,{screen,12}}) of
	       {screen,_,12} = Screen ->
		   Screen;
	       _ ->
		   gs:read(S,{choose_font,{courier,12}})
	   end,
    Win=gs:window(S,[{title,"Focus Demo"},{width,200},{height,150}]),
    gs:create(entry,e1,Win,[{y,0},{focus,true}]),
    gs:create(entry,e2,Win,[{y,30},{focus,true}]),
    gs:create(entry,e3,Win,[{y,60},{focus,true}]),
    gs:create(entry,e4,Win,[{y,90},{focus,true}]),
    gs:create(button,b1,Win,[{x,100},{width,30},
			     {label,{text,"e1"}},{font,Font}]),
    gs:create(button,b2,Win,[{y,30},{x,100},{width,30},
			     {label,{text,"e2"}},{font,Font}]),
    gs:create(button,b3,Win,[{y,60},{x,100},{width,30},
			     {label,{text,"e3"}},{font,Font}]),
    gs:create(button,b4,Win,[{y,90},{x,100},{width,30},
			     {label,{text,"e4"}},{font,Font}]),
    gs:create(button,clear,Win,[{y,120},{x,35},{width,50},
				{label,{text,"Clear"}},{font,Font}]),
    gs:create(button,ask,Win,[{y,120},{x,85},{width,30},
			      {label,{text,"?"}},{font,Font}]),
    gs:create(button,quit,Win,[{y,120},{x,115},{width,50},
			       {label,{text,"Quit"}},{font,Font}]),
    gs:config(Win,{map,true}),
    loop().

loop() ->
    receive
	{gs,quit,_,_,_} -> exit(normal);
	{gs,ask,_,_,_} -> 
	    R1=gs:read(e1,setfocus),R2=gs:read(e2,setfocus),
	    R3=gs:read(e3,setfocus),R4=gs:read(e4,setfocus),
	    R= if R1==true -> e1; 
		   R2==true -> e2;
		   R3==true -> e3;
		   R4==true -> e4;
		   true -> nobody
	       end,
	    io:format("Focus status: ~w has focus.~n",[R]);
	{gs,clear,_,_,_} -> 
	    gs:config(clear,{setfocus,true}),
	    io:format("Focus is cleared.~n",[]);
	{gs,b1,_,_,_} -> gs:config(e1,{setfocus,true});
	{gs,b2,_,_,_} -> gs:config(e2,{setfocus,true});
	{gs,b3,_,_,_} -> gs:config(e3,{setfocus,true});
	{gs,b4,_,_,_} -> gs:config(e4,{setfocus,true});
	{gs,Id,focus,_,[0|_]} -> 
	    io:format("~w lost focus.~n",[Id]);
	{gs,Id,focus,_,[1|_]} -> 
	    io:format("~w gained focus.~n",[Id]);
	{gs,_,destroy,_,_} -> 
	    exit(normal);
	X ->
	    io:format("Got X=~w~n",[X])
    end,
    loop().

%% ----------------------------------------
%% done